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
24 #include "ext4_jbd2.h"
26 #include <linux/log2.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <trace/events/ext4.h>
31 #ifdef CONFIG_EXT4_DEBUG
32 ushort ext4_mballoc_debug __read_mostly
;
34 module_param_named(mballoc_debug
, ext4_mballoc_debug
, ushort
, 0644);
35 MODULE_PARM_DESC(mballoc_debug
, "Debugging level for ext4's mballoc");
40 * - test ext4_ext_search_left() and ext4_ext_search_right()
41 * - search for metadata in few groups
44 * - normalization should take into account whether file is still open
45 * - discard preallocations if no free space left (policy?)
46 * - don't normalize tails
48 * - reservation for superuser
51 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
52 * - track min/max extents in each group for better group selection
53 * - mb_mark_used() may allocate chunk right after splitting buddy
54 * - tree of groups sorted by number of free blocks
59 * The allocation request involve request for multiple number of blocks
60 * near to the goal(block) value specified.
62 * During initialization phase of the allocator we decide to use the
63 * group preallocation or inode preallocation depending on the size of
64 * the file. The size of the file could be the resulting file size we
65 * would have after allocation, or the current file size, which ever
66 * is larger. If the size is less than sbi->s_mb_stream_request we
67 * select to use the group preallocation. The default value of
68 * s_mb_stream_request is 16 blocks. This can also be tuned via
69 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
70 * terms of number of blocks.
72 * The main motivation for having small file use group preallocation is to
73 * ensure that we have small files closer together on the disk.
75 * First stage the allocator looks at the inode prealloc list,
76 * ext4_inode_info->i_prealloc_list, which contains list of prealloc
77 * spaces for this particular inode. The inode prealloc space is
80 * pa_lstart -> the logical start block for this prealloc space
81 * pa_pstart -> the physical start block for this prealloc space
82 * pa_len -> length for this prealloc space (in clusters)
83 * pa_free -> free space available in this prealloc space (in clusters)
85 * The inode preallocation space is used looking at the _logical_ start
86 * block. If only the logical file block falls within the range of prealloc
87 * space we will consume the particular prealloc space. This makes sure that
88 * we have contiguous physical blocks representing the file blocks
90 * The important thing to be noted in case of inode prealloc space is that
91 * we don't modify the values associated to inode prealloc space except
94 * If we are not able to find blocks in the inode prealloc space and if we
95 * have the group allocation flag set then we look at the locality group
96 * prealloc space. These are per CPU prealloc list represented as
98 * ext4_sb_info.s_locality_groups[smp_processor_id()]
100 * The reason for having a per cpu locality group is to reduce the contention
101 * between CPUs. It is possible to get scheduled at this point.
103 * The locality group prealloc space is used looking at whether we have
104 * enough free space (pa_free) within the prealloc space.
106 * If we can't allocate blocks via inode prealloc or/and locality group
107 * prealloc then we look at the buddy cache. The buddy cache is represented
108 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
109 * mapped to the buddy and bitmap information regarding different
110 * groups. The buddy information is attached to buddy cache inode so that
111 * we can access them through the page cache. The information regarding
112 * each group is loaded via ext4_mb_load_buddy. The information involve
113 * block bitmap and buddy information. The information are stored in the
117 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
120 * one block each for bitmap and buddy information. So for each group we
121 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
122 * blocksize) blocks. So it can have information regarding groups_per_page
123 * which is blocks_per_page/2
125 * The buddy cache inode is not stored on disk. The inode is thrown
126 * away when the filesystem is unmounted.
128 * We look for count number of blocks in the buddy cache. If we were able
129 * to locate that many free blocks we return with additional information
130 * regarding rest of the contiguous physical block available
132 * Before allocating blocks via buddy cache we normalize the request
133 * blocks. This ensure we ask for more blocks that we needed. The extra
134 * blocks that we get after allocation is added to the respective prealloc
135 * list. In case of inode preallocation we follow a list of heuristics
136 * based on file size. This can be found in ext4_mb_normalize_request. If
137 * we are doing a group prealloc we try to normalize the request to
138 * sbi->s_mb_group_prealloc. The default value of s_mb_group_prealloc is
139 * dependent on the cluster size; for non-bigalloc file systems, it is
140 * 512 blocks. This can be tuned via
141 * /sys/fs/ext4/<partition>/mb_group_prealloc. The value is represented in
142 * terms of number of blocks. If we have mounted the file system with -O
143 * stripe=<value> option the group prealloc request is normalized to the
144 * the smallest multiple of the stripe value (sbi->s_stripe) which is
145 * greater than the default mb_group_prealloc.
147 * The regular allocator (using the buddy cache) supports a few tunables.
149 * /sys/fs/ext4/<partition>/mb_min_to_scan
150 * /sys/fs/ext4/<partition>/mb_max_to_scan
151 * /sys/fs/ext4/<partition>/mb_order2_req
153 * The regular allocator uses buddy scan only if the request len is power of
154 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
155 * value of s_mb_order2_reqs can be tuned via
156 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
157 * stripe size (sbi->s_stripe), we try to search for contiguous block in
158 * stripe size. This should result in better allocation on RAID setups. If
159 * not, we search in the specific group using bitmap for best extents. The
160 * tunable min_to_scan and max_to_scan control the behaviour here.
161 * min_to_scan indicate how long the mballoc __must__ look for a best
162 * extent and max_to_scan indicates how long the mballoc __can__ look for a
163 * best extent in the found extents. Searching for the blocks starts with
164 * the group specified as the goal value in allocation context via
165 * ac_g_ex. Each group is first checked based on the criteria whether it
166 * can be used for allocation. ext4_mb_good_group explains how the groups are
169 * Both the prealloc space are getting populated as above. So for the first
170 * request we will hit the buddy cache which will result in this prealloc
171 * space getting filled. The prealloc space is then later used for the
172 * subsequent request.
176 * mballoc operates on the following data:
178 * - in-core buddy (actually includes buddy and bitmap)
179 * - preallocation descriptors (PAs)
181 * there are two types of preallocations:
183 * assiged to specific inode and can be used for this inode only.
184 * it describes part of inode's space preallocated to specific
185 * physical blocks. any block from that preallocated can be used
186 * independent. the descriptor just tracks number of blocks left
187 * unused. so, before taking some block from descriptor, one must
188 * make sure corresponded logical block isn't allocated yet. this
189 * also means that freeing any block within descriptor's range
190 * must discard all preallocated blocks.
192 * assigned to specific locality group which does not translate to
193 * permanent set of inodes: inode can join and leave group. space
194 * from this type of preallocation can be used for any inode. thus
195 * it's consumed from the beginning to the end.
197 * relation between them can be expressed as:
198 * in-core buddy = on-disk bitmap + preallocation descriptors
200 * this mean blocks mballoc considers used are:
201 * - allocated blocks (persistent)
202 * - preallocated blocks (non-persistent)
204 * consistency in mballoc world means that at any time a block is either
205 * free or used in ALL structures. notice: "any time" should not be read
206 * literally -- time is discrete and delimited by locks.
208 * to keep it simple, we don't use block numbers, instead we count number of
209 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
211 * all operations can be expressed as:
212 * - init buddy: buddy = on-disk + PAs
213 * - new PA: buddy += N; PA = N
214 * - use inode PA: on-disk += N; PA -= N
215 * - discard inode PA buddy -= on-disk - PA; PA = 0
216 * - use locality group PA on-disk += N; PA -= N
217 * - discard locality group PA buddy -= PA; PA = 0
218 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
219 * is used in real operation because we can't know actual used
220 * bits from PA, only from on-disk bitmap
222 * if we follow this strict logic, then all operations above should be atomic.
223 * given some of them can block, we'd have to use something like semaphores
224 * killing performance on high-end SMP hardware. let's try to relax it using
225 * the following knowledge:
226 * 1) if buddy is referenced, it's already initialized
227 * 2) while block is used in buddy and the buddy is referenced,
228 * nobody can re-allocate that block
229 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
230 * bit set and PA claims same block, it's OK. IOW, one can set bit in
231 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
234 * so, now we're building a concurrency table:
237 * blocks for PA are allocated in the buddy, buddy must be referenced
238 * until PA is linked to allocation group to avoid concurrent buddy init
240 * we need to make sure that either on-disk bitmap or PA has uptodate data
241 * given (3) we care that PA-=N operation doesn't interfere with init
243 * the simplest way would be to have buddy initialized by the discard
244 * - use locality group PA
245 * again PA-=N must be serialized with init
246 * - discard locality group PA
247 * the simplest way would be to have buddy initialized by the discard
250 * i_data_sem serializes them
252 * discard process must wait until PA isn't used by another process
253 * - use locality group PA
254 * some mutex should serialize them
255 * - discard locality group PA
256 * discard process must wait until PA isn't used by another process
259 * i_data_sem or another mutex should serializes them
261 * discard process must wait until PA isn't used by another process
262 * - use locality group PA
263 * nothing wrong here -- they're different PAs covering different blocks
264 * - discard locality group PA
265 * discard process must wait until PA isn't used by another process
267 * now we're ready to make few consequences:
268 * - PA is referenced and while it is no discard is possible
269 * - PA is referenced until block isn't marked in on-disk bitmap
270 * - PA changes only after on-disk bitmap
271 * - discard must not compete with init. either init is done before
272 * any discard or they're serialized somehow
273 * - buddy init as sum of on-disk bitmap and PAs is done atomically
275 * a special case when we've used PA to emptiness. no need to modify buddy
276 * in this case, but we should care about concurrent init
281 * Logic in few words:
286 * mark bits in on-disk bitmap
289 * - use preallocation:
290 * find proper PA (per-inode or group)
292 * mark bits in on-disk bitmap
298 * mark bits in on-disk bitmap
301 * - discard preallocations in group:
303 * move them onto local list
304 * load on-disk bitmap
306 * remove PA from object (inode or locality group)
307 * mark free blocks in-core
309 * - discard inode's preallocations:
316 * - bitlock on a group (group)
317 * - object (inode/locality) (object)
328 * - release consumed pa:
333 * - generate in-core bitmap:
337 * - discard all for given object (inode, locality group):
342 * - discard all for given group:
349 static struct kmem_cache
*ext4_pspace_cachep
;
350 static struct kmem_cache
*ext4_ac_cachep
;
351 static struct kmem_cache
*ext4_free_data_cachep
;
353 /* We create slab caches for groupinfo data structures based on the
354 * superblock block size. There will be one per mounted filesystem for
355 * each unique s_blocksize_bits */
356 #define NR_GRPINFO_CACHES 8
357 static struct kmem_cache
*ext4_groupinfo_caches
[NR_GRPINFO_CACHES
];
359 static const char *ext4_groupinfo_slab_names
[NR_GRPINFO_CACHES
] = {
360 "ext4_groupinfo_1k", "ext4_groupinfo_2k", "ext4_groupinfo_4k",
361 "ext4_groupinfo_8k", "ext4_groupinfo_16k", "ext4_groupinfo_32k",
362 "ext4_groupinfo_64k", "ext4_groupinfo_128k"
365 static void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
367 static void ext4_mb_generate_from_freelist(struct super_block
*sb
, void *bitmap
,
369 static void ext4_free_data_callback(struct super_block
*sb
,
370 struct ext4_journal_cb_entry
*jce
, int rc
);
372 static inline void *mb_correct_addr_and_bit(int *bit
, void *addr
)
374 #if BITS_PER_LONG == 64
375 *bit
+= ((unsigned long) addr
& 7UL) << 3;
376 addr
= (void *) ((unsigned long) addr
& ~7UL);
377 #elif BITS_PER_LONG == 32
378 *bit
+= ((unsigned long) addr
& 3UL) << 3;
379 addr
= (void *) ((unsigned long) addr
& ~3UL);
381 #error "how many bits you are?!"
386 static inline int mb_test_bit(int bit
, void *addr
)
389 * ext4_test_bit on architecture like powerpc
390 * needs unsigned long aligned address
392 addr
= mb_correct_addr_and_bit(&bit
, addr
);
393 return ext4_test_bit(bit
, addr
);
396 static inline void mb_set_bit(int bit
, void *addr
)
398 addr
= mb_correct_addr_and_bit(&bit
, addr
);
399 ext4_set_bit(bit
, addr
);
402 static inline void mb_clear_bit(int bit
, void *addr
)
404 addr
= mb_correct_addr_and_bit(&bit
, addr
);
405 ext4_clear_bit(bit
, addr
);
408 static inline int mb_find_next_zero_bit(void *addr
, int max
, int start
)
410 int fix
= 0, ret
, tmpmax
;
411 addr
= mb_correct_addr_and_bit(&fix
, addr
);
415 ret
= ext4_find_next_zero_bit(addr
, tmpmax
, start
) - fix
;
421 static inline int mb_find_next_bit(void *addr
, int max
, int start
)
423 int fix
= 0, ret
, tmpmax
;
424 addr
= mb_correct_addr_and_bit(&fix
, addr
);
428 ret
= ext4_find_next_bit(addr
, tmpmax
, start
) - fix
;
434 static void *mb_find_buddy(struct ext4_buddy
*e4b
, int order
, int *max
)
438 BUG_ON(e4b
->bd_bitmap
== e4b
->bd_buddy
);
441 if (order
> e4b
->bd_blkbits
+ 1) {
446 /* at order 0 we see each particular block */
448 *max
= 1 << (e4b
->bd_blkbits
+ 3);
449 return e4b
->bd_bitmap
;
452 bb
= e4b
->bd_buddy
+ EXT4_SB(e4b
->bd_sb
)->s_mb_offsets
[order
];
453 *max
= EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[order
];
459 static void mb_free_blocks_double(struct inode
*inode
, struct ext4_buddy
*e4b
,
460 int first
, int count
)
463 struct super_block
*sb
= e4b
->bd_sb
;
465 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
467 assert_spin_locked(ext4_group_lock_ptr(sb
, e4b
->bd_group
));
468 for (i
= 0; i
< count
; i
++) {
469 if (!mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
)) {
470 ext4_fsblk_t blocknr
;
472 blocknr
= ext4_group_first_block_no(sb
, e4b
->bd_group
);
473 blocknr
+= EXT4_C2B(EXT4_SB(sb
), first
+ i
);
474 ext4_grp_locked_error(sb
, e4b
->bd_group
,
475 inode
? inode
->i_ino
: 0,
477 "freeing block already freed "
481 mb_clear_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
485 static void mb_mark_used_double(struct ext4_buddy
*e4b
, int first
, int count
)
489 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
491 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
492 for (i
= 0; i
< count
; i
++) {
493 BUG_ON(mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
));
494 mb_set_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
498 static void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
500 if (memcmp(e4b
->bd_info
->bb_bitmap
, bitmap
, e4b
->bd_sb
->s_blocksize
)) {
501 unsigned char *b1
, *b2
;
503 b1
= (unsigned char *) e4b
->bd_info
->bb_bitmap
;
504 b2
= (unsigned char *) bitmap
;
505 for (i
= 0; i
< e4b
->bd_sb
->s_blocksize
; i
++) {
506 if (b1
[i
] != b2
[i
]) {
507 ext4_msg(e4b
->bd_sb
, KERN_ERR
,
508 "corruption in group %u "
509 "at byte %u(%u): %x in copy != %x "
511 e4b
->bd_group
, i
, i
* 8, b1
[i
], b2
[i
]);
519 static inline void mb_free_blocks_double(struct inode
*inode
,
520 struct ext4_buddy
*e4b
, int first
, int count
)
524 static inline void mb_mark_used_double(struct ext4_buddy
*e4b
,
525 int first
, int count
)
529 static inline void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
535 #ifdef AGGRESSIVE_CHECK
537 #define MB_CHECK_ASSERT(assert) \
541 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
542 function, file, line, # assert); \
547 static int __mb_check_buddy(struct ext4_buddy
*e4b
, char *file
,
548 const char *function
, int line
)
550 struct super_block
*sb
= e4b
->bd_sb
;
551 int order
= e4b
->bd_blkbits
+ 1;
558 struct ext4_group_info
*grp
;
561 struct list_head
*cur
;
566 static int mb_check_counter
;
567 if (mb_check_counter
++ % 100 != 0)
572 buddy
= mb_find_buddy(e4b
, order
, &max
);
573 MB_CHECK_ASSERT(buddy
);
574 buddy2
= mb_find_buddy(e4b
, order
- 1, &max2
);
575 MB_CHECK_ASSERT(buddy2
);
576 MB_CHECK_ASSERT(buddy
!= buddy2
);
577 MB_CHECK_ASSERT(max
* 2 == max2
);
580 for (i
= 0; i
< max
; i
++) {
582 if (mb_test_bit(i
, buddy
)) {
583 /* only single bit in buddy2 may be 1 */
584 if (!mb_test_bit(i
<< 1, buddy2
)) {
586 mb_test_bit((i
<<1)+1, buddy2
));
587 } else if (!mb_test_bit((i
<< 1) + 1, buddy2
)) {
589 mb_test_bit(i
<< 1, buddy2
));
594 /* both bits in buddy2 must be 1 */
595 MB_CHECK_ASSERT(mb_test_bit(i
<< 1, buddy2
));
596 MB_CHECK_ASSERT(mb_test_bit((i
<< 1) + 1, buddy2
));
598 for (j
= 0; j
< (1 << order
); j
++) {
599 k
= (i
* (1 << order
)) + j
;
601 !mb_test_bit(k
, e4b
->bd_bitmap
));
605 MB_CHECK_ASSERT(e4b
->bd_info
->bb_counters
[order
] == count
);
610 buddy
= mb_find_buddy(e4b
, 0, &max
);
611 for (i
= 0; i
< max
; i
++) {
612 if (!mb_test_bit(i
, buddy
)) {
613 MB_CHECK_ASSERT(i
>= e4b
->bd_info
->bb_first_free
);
621 /* check used bits only */
622 for (j
= 0; j
< e4b
->bd_blkbits
+ 1; j
++) {
623 buddy2
= mb_find_buddy(e4b
, j
, &max2
);
625 MB_CHECK_ASSERT(k
< max2
);
626 MB_CHECK_ASSERT(mb_test_bit(k
, buddy2
));
629 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b
->bd_info
));
630 MB_CHECK_ASSERT(e4b
->bd_info
->bb_fragments
== fragments
);
632 grp
= ext4_get_group_info(sb
, e4b
->bd_group
);
633 list_for_each(cur
, &grp
->bb_prealloc_list
) {
634 ext4_group_t groupnr
;
635 struct ext4_prealloc_space
*pa
;
636 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
637 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &groupnr
, &k
);
638 MB_CHECK_ASSERT(groupnr
== e4b
->bd_group
);
639 for (i
= 0; i
< pa
->pa_len
; i
++)
640 MB_CHECK_ASSERT(mb_test_bit(k
+ i
, buddy
));
644 #undef MB_CHECK_ASSERT
645 #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
646 __FILE__, __func__, __LINE__)
648 #define mb_check_buddy(e4b)
652 * Divide blocks started from @first with length @len into
653 * smaller chunks with power of 2 blocks.
654 * Clear the bits in bitmap which the blocks of the chunk(s) covered,
655 * then increase bb_counters[] for corresponded chunk size.
657 static void ext4_mb_mark_free_simple(struct super_block
*sb
,
658 void *buddy
, ext4_grpblk_t first
, ext4_grpblk_t len
,
659 struct ext4_group_info
*grp
)
661 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
665 unsigned short border
;
667 BUG_ON(len
> EXT4_CLUSTERS_PER_GROUP(sb
));
669 border
= 2 << sb
->s_blocksize_bits
;
672 /* find how many blocks can be covered since this position */
673 max
= ffs(first
| border
) - 1;
675 /* find how many blocks of power 2 we need to mark */
682 /* mark multiblock chunks only */
683 grp
->bb_counters
[min
]++;
685 mb_clear_bit(first
>> min
,
686 buddy
+ sbi
->s_mb_offsets
[min
]);
694 * Cache the order of the largest free extent we have available in this block
698 mb_set_largest_free_order(struct super_block
*sb
, struct ext4_group_info
*grp
)
703 grp
->bb_largest_free_order
= -1; /* uninit */
705 bits
= sb
->s_blocksize_bits
+ 1;
706 for (i
= bits
; i
>= 0; i
--) {
707 if (grp
->bb_counters
[i
] > 0) {
708 grp
->bb_largest_free_order
= i
;
714 static noinline_for_stack
715 void ext4_mb_generate_buddy(struct super_block
*sb
,
716 void *buddy
, void *bitmap
, ext4_group_t group
)
718 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
719 ext4_grpblk_t max
= EXT4_CLUSTERS_PER_GROUP(sb
);
724 unsigned fragments
= 0;
725 unsigned long long period
= get_cycles();
727 /* initialize buddy from bitmap which is aggregation
728 * of on-disk bitmap and preallocations */
729 i
= mb_find_next_zero_bit(bitmap
, max
, 0);
730 grp
->bb_first_free
= i
;
734 i
= mb_find_next_bit(bitmap
, max
, i
);
738 ext4_mb_mark_free_simple(sb
, buddy
, first
, len
, grp
);
740 grp
->bb_counters
[0]++;
742 i
= mb_find_next_zero_bit(bitmap
, max
, i
);
744 grp
->bb_fragments
= fragments
;
746 if (free
!= grp
->bb_free
) {
747 ext4_grp_locked_error(sb
, group
, 0, 0,
748 "%u clusters in bitmap, %u in gd",
751 * If we intent to continue, we consider group descritor
752 * corrupt and update bb_free using bitmap value
756 mb_set_largest_free_order(sb
, grp
);
758 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
, &(grp
->bb_state
));
760 period
= get_cycles() - period
;
761 spin_lock(&EXT4_SB(sb
)->s_bal_lock
);
762 EXT4_SB(sb
)->s_mb_buddies_generated
++;
763 EXT4_SB(sb
)->s_mb_generation_time
+= period
;
764 spin_unlock(&EXT4_SB(sb
)->s_bal_lock
);
767 /* The buddy information is attached the buddy cache inode
768 * for convenience. The information regarding each group
769 * is loaded via ext4_mb_load_buddy. The information involve
770 * block bitmap and buddy information. The information are
771 * stored in the inode as
774 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
777 * one block each for bitmap and buddy information.
778 * So for each group we take up 2 blocks. A page can
779 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
780 * So it can have information regarding groups_per_page which
781 * is blocks_per_page/2
783 * Locking note: This routine takes the block group lock of all groups
784 * for this page; do not hold this lock when calling this routine!
787 static int ext4_mb_init_cache(struct page
*page
, char *incore
)
789 ext4_group_t ngroups
;
795 ext4_group_t first_group
, group
;
797 struct super_block
*sb
;
798 struct buffer_head
*bhs
;
799 struct buffer_head
**bh
= NULL
;
803 struct ext4_group_info
*grinfo
;
805 mb_debug(1, "init page %lu\n", page
->index
);
807 inode
= page
->mapping
->host
;
809 ngroups
= ext4_get_groups_count(sb
);
810 blocksize
= 1 << inode
->i_blkbits
;
811 blocks_per_page
= PAGE_CACHE_SIZE
/ blocksize
;
813 groups_per_page
= blocks_per_page
>> 1;
814 if (groups_per_page
== 0)
817 /* allocate buffer_heads to read bitmaps */
818 if (groups_per_page
> 1) {
819 i
= sizeof(struct buffer_head
*) * groups_per_page
;
820 bh
= kzalloc(i
, GFP_NOFS
);
828 first_group
= page
->index
* blocks_per_page
/ 2;
830 /* read all groups the page covers into the cache */
831 for (i
= 0, group
= first_group
; i
< groups_per_page
; i
++, group
++) {
832 if (group
>= ngroups
)
835 grinfo
= ext4_get_group_info(sb
, group
);
837 * If page is uptodate then we came here after online resize
838 * which added some new uninitialized group info structs, so
839 * we must skip all initialized uptodate buddies on the page,
840 * which may be currently in use by an allocating task.
842 if (PageUptodate(page
) && !EXT4_MB_GRP_NEED_INIT(grinfo
)) {
846 if (!(bh
[i
] = ext4_read_block_bitmap_nowait(sb
, group
))) {
850 mb_debug(1, "read bitmap for group %u\n", group
);
853 /* wait for I/O completion */
854 for (i
= 0, group
= first_group
; i
< groups_per_page
; i
++, group
++) {
855 if (bh
[i
] && ext4_wait_block_bitmap(sb
, group
, bh
[i
])) {
861 first_block
= page
->index
* blocks_per_page
;
862 for (i
= 0; i
< blocks_per_page
; i
++) {
865 group
= (first_block
+ i
) >> 1;
866 if (group
>= ngroups
)
869 if (!bh
[group
- first_group
])
870 /* skip initialized uptodate buddy */
874 * data carry information regarding this
875 * particular group in the format specified
879 data
= page_address(page
) + (i
* blocksize
);
880 bitmap
= bh
[group
- first_group
]->b_data
;
883 * We place the buddy block and bitmap block
886 if ((first_block
+ i
) & 1) {
887 /* this is block of buddy */
888 BUG_ON(incore
== NULL
);
889 mb_debug(1, "put buddy for group %u in page %lu/%x\n",
890 group
, page
->index
, i
* blocksize
);
891 trace_ext4_mb_buddy_bitmap_load(sb
, group
);
892 grinfo
= ext4_get_group_info(sb
, group
);
893 grinfo
->bb_fragments
= 0;
894 memset(grinfo
->bb_counters
, 0,
895 sizeof(*grinfo
->bb_counters
) *
896 (sb
->s_blocksize_bits
+2));
898 * incore got set to the group block bitmap below
900 ext4_lock_group(sb
, group
);
902 memset(data
, 0xff, blocksize
);
903 ext4_mb_generate_buddy(sb
, data
, incore
, group
);
904 ext4_unlock_group(sb
, group
);
907 /* this is block of bitmap */
908 BUG_ON(incore
!= NULL
);
909 mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
910 group
, page
->index
, i
* blocksize
);
911 trace_ext4_mb_bitmap_load(sb
, group
);
913 /* see comments in ext4_mb_put_pa() */
914 ext4_lock_group(sb
, group
);
915 memcpy(data
, bitmap
, blocksize
);
917 /* mark all preallocated blks used in in-core bitmap */
918 ext4_mb_generate_from_pa(sb
, data
, group
);
919 ext4_mb_generate_from_freelist(sb
, data
, group
);
920 ext4_unlock_group(sb
, group
);
922 /* set incore so that the buddy information can be
923 * generated using this
928 SetPageUptodate(page
);
932 for (i
= 0; i
< groups_per_page
; i
++)
941 * Lock the buddy and bitmap pages. This make sure other parallel init_group
942 * on the same buddy page doesn't happen whild holding the buddy page lock.
943 * Return locked buddy and bitmap pages on e4b struct. If buddy and bitmap
944 * are on the same page e4b->bd_buddy_page is NULL and return value is 0.
946 static int ext4_mb_get_buddy_page_lock(struct super_block
*sb
,
947 ext4_group_t group
, struct ext4_buddy
*e4b
)
949 struct inode
*inode
= EXT4_SB(sb
)->s_buddy_cache
;
950 int block
, pnum
, poff
;
954 e4b
->bd_buddy_page
= NULL
;
955 e4b
->bd_bitmap_page
= NULL
;
957 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
959 * the buddy cache inode stores the block bitmap
960 * and buddy information in consecutive blocks.
961 * So for each group we need two blocks.
964 pnum
= block
/ blocks_per_page
;
965 poff
= block
% blocks_per_page
;
966 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
969 BUG_ON(page
->mapping
!= inode
->i_mapping
);
970 e4b
->bd_bitmap_page
= page
;
971 e4b
->bd_bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
973 if (blocks_per_page
>= 2) {
974 /* buddy and bitmap are on the same page */
979 pnum
= block
/ blocks_per_page
;
980 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
983 BUG_ON(page
->mapping
!= inode
->i_mapping
);
984 e4b
->bd_buddy_page
= page
;
988 static void ext4_mb_put_buddy_page_lock(struct ext4_buddy
*e4b
)
990 if (e4b
->bd_bitmap_page
) {
991 unlock_page(e4b
->bd_bitmap_page
);
992 page_cache_release(e4b
->bd_bitmap_page
);
994 if (e4b
->bd_buddy_page
) {
995 unlock_page(e4b
->bd_buddy_page
);
996 page_cache_release(e4b
->bd_buddy_page
);
1001 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1002 * block group lock of all groups for this page; do not hold the BG lock when
1003 * calling this routine!
1005 static noinline_for_stack
1006 int ext4_mb_init_group(struct super_block
*sb
, ext4_group_t group
)
1009 struct ext4_group_info
*this_grp
;
1010 struct ext4_buddy e4b
;
1015 mb_debug(1, "init group %u\n", group
);
1016 this_grp
= ext4_get_group_info(sb
, group
);
1018 * This ensures that we don't reinit the buddy cache
1019 * page which map to the group from which we are already
1020 * allocating. If we are looking at the buddy cache we would
1021 * have taken a reference using ext4_mb_load_buddy and that
1022 * would have pinned buddy page to page cache.
1024 ret
= ext4_mb_get_buddy_page_lock(sb
, group
, &e4b
);
1025 if (ret
|| !EXT4_MB_GRP_NEED_INIT(this_grp
)) {
1027 * somebody initialized the group
1028 * return without doing anything
1033 page
= e4b
.bd_bitmap_page
;
1034 ret
= ext4_mb_init_cache(page
, NULL
);
1037 if (!PageUptodate(page
)) {
1041 mark_page_accessed(page
);
1043 if (e4b
.bd_buddy_page
== NULL
) {
1045 * If both the bitmap and buddy are in
1046 * the same page we don't need to force
1052 /* init buddy cache */
1053 page
= e4b
.bd_buddy_page
;
1054 ret
= ext4_mb_init_cache(page
, e4b
.bd_bitmap
);
1057 if (!PageUptodate(page
)) {
1061 mark_page_accessed(page
);
1063 ext4_mb_put_buddy_page_lock(&e4b
);
1068 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1069 * block group lock of all groups for this page; do not hold the BG lock when
1070 * calling this routine!
1072 static noinline_for_stack
int
1073 ext4_mb_load_buddy(struct super_block
*sb
, ext4_group_t group
,
1074 struct ext4_buddy
*e4b
)
1076 int blocks_per_page
;
1082 struct ext4_group_info
*grp
;
1083 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1084 struct inode
*inode
= sbi
->s_buddy_cache
;
1087 mb_debug(1, "load group %u\n", group
);
1089 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1090 grp
= ext4_get_group_info(sb
, group
);
1092 e4b
->bd_blkbits
= sb
->s_blocksize_bits
;
1095 e4b
->bd_group
= group
;
1096 e4b
->bd_buddy_page
= NULL
;
1097 e4b
->bd_bitmap_page
= NULL
;
1099 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp
))) {
1101 * we need full data about the group
1102 * to make a good selection
1104 ret
= ext4_mb_init_group(sb
, group
);
1110 * the buddy cache inode stores the block bitmap
1111 * and buddy information in consecutive blocks.
1112 * So for each group we need two blocks.
1115 pnum
= block
/ blocks_per_page
;
1116 poff
= block
% blocks_per_page
;
1118 /* we could use find_or_create_page(), but it locks page
1119 * what we'd like to avoid in fast path ... */
1120 page
= find_get_page(inode
->i_mapping
, pnum
);
1121 if (page
== NULL
|| !PageUptodate(page
)) {
1124 * drop the page reference and try
1125 * to get the page with lock. If we
1126 * are not uptodate that implies
1127 * somebody just created the page but
1128 * is yet to initialize the same. So
1129 * wait for it to initialize.
1131 page_cache_release(page
);
1132 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1134 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1135 if (!PageUptodate(page
)) {
1136 ret
= ext4_mb_init_cache(page
, NULL
);
1141 mb_cmp_bitmaps(e4b
, page_address(page
) +
1142 (poff
* sb
->s_blocksize
));
1147 if (page
== NULL
|| !PageUptodate(page
)) {
1151 e4b
->bd_bitmap_page
= page
;
1152 e4b
->bd_bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
1153 mark_page_accessed(page
);
1156 pnum
= block
/ blocks_per_page
;
1157 poff
= block
% blocks_per_page
;
1159 page
= find_get_page(inode
->i_mapping
, pnum
);
1160 if (page
== NULL
|| !PageUptodate(page
)) {
1162 page_cache_release(page
);
1163 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1165 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1166 if (!PageUptodate(page
)) {
1167 ret
= ext4_mb_init_cache(page
, e4b
->bd_bitmap
);
1176 if (page
== NULL
|| !PageUptodate(page
)) {
1180 e4b
->bd_buddy_page
= page
;
1181 e4b
->bd_buddy
= page_address(page
) + (poff
* sb
->s_blocksize
);
1182 mark_page_accessed(page
);
1184 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
1185 BUG_ON(e4b
->bd_buddy_page
== NULL
);
1191 page_cache_release(page
);
1192 if (e4b
->bd_bitmap_page
)
1193 page_cache_release(e4b
->bd_bitmap_page
);
1194 if (e4b
->bd_buddy_page
)
1195 page_cache_release(e4b
->bd_buddy_page
);
1196 e4b
->bd_buddy
= NULL
;
1197 e4b
->bd_bitmap
= NULL
;
1201 static void ext4_mb_unload_buddy(struct ext4_buddy
*e4b
)
1203 if (e4b
->bd_bitmap_page
)
1204 page_cache_release(e4b
->bd_bitmap_page
);
1205 if (e4b
->bd_buddy_page
)
1206 page_cache_release(e4b
->bd_buddy_page
);
1210 static int mb_find_order_for_block(struct ext4_buddy
*e4b
, int block
)
1215 BUG_ON(e4b
->bd_bitmap
== e4b
->bd_buddy
);
1216 BUG_ON(block
>= (1 << (e4b
->bd_blkbits
+ 3)));
1219 while (order
<= e4b
->bd_blkbits
+ 1) {
1221 if (!mb_test_bit(block
, bb
)) {
1222 /* this block is part of buddy of order 'order' */
1225 bb
+= 1 << (e4b
->bd_blkbits
- order
);
1231 static void mb_clear_bits(void *bm
, int cur
, int len
)
1237 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1238 /* fast path: clear whole word at once */
1239 addr
= bm
+ (cur
>> 3);
1244 mb_clear_bit(cur
, bm
);
1249 void ext4_set_bits(void *bm
, int cur
, int len
)
1255 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1256 /* fast path: set whole word at once */
1257 addr
= bm
+ (cur
>> 3);
1262 mb_set_bit(cur
, bm
);
1267 static void mb_free_blocks(struct inode
*inode
, struct ext4_buddy
*e4b
,
1268 int first
, int count
)
1275 struct super_block
*sb
= e4b
->bd_sb
;
1277 BUG_ON(first
+ count
> (sb
->s_blocksize
<< 3));
1278 assert_spin_locked(ext4_group_lock_ptr(sb
, e4b
->bd_group
));
1279 mb_check_buddy(e4b
);
1280 mb_free_blocks_double(inode
, e4b
, first
, count
);
1282 e4b
->bd_info
->bb_free
+= count
;
1283 if (first
< e4b
->bd_info
->bb_first_free
)
1284 e4b
->bd_info
->bb_first_free
= first
;
1286 /* let's maintain fragments counter */
1288 block
= !mb_test_bit(first
- 1, e4b
->bd_bitmap
);
1289 if (first
+ count
< EXT4_SB(sb
)->s_mb_maxs
[0])
1290 max
= !mb_test_bit(first
+ count
, e4b
->bd_bitmap
);
1292 e4b
->bd_info
->bb_fragments
--;
1293 else if (!block
&& !max
)
1294 e4b
->bd_info
->bb_fragments
++;
1296 /* let's maintain buddy itself */
1297 while (count
-- > 0) {
1301 if (!mb_test_bit(block
, e4b
->bd_bitmap
)) {
1302 ext4_fsblk_t blocknr
;
1304 blocknr
= ext4_group_first_block_no(sb
, e4b
->bd_group
);
1305 blocknr
+= EXT4_C2B(EXT4_SB(sb
), block
);
1306 ext4_grp_locked_error(sb
, e4b
->bd_group
,
1307 inode
? inode
->i_ino
: 0,
1309 "freeing already freed block "
1312 mb_clear_bit(block
, e4b
->bd_bitmap
);
1313 e4b
->bd_info
->bb_counters
[order
]++;
1315 /* start of the buddy */
1316 buddy
= mb_find_buddy(e4b
, order
, &max
);
1320 if (mb_test_bit(block
, buddy
) ||
1321 mb_test_bit(block
+ 1, buddy
))
1324 /* both the buddies are free, try to coalesce them */
1325 buddy2
= mb_find_buddy(e4b
, order
+ 1, &max
);
1331 /* for special purposes, we don't set
1332 * free bits in bitmap */
1333 mb_set_bit(block
, buddy
);
1334 mb_set_bit(block
+ 1, buddy
);
1336 e4b
->bd_info
->bb_counters
[order
]--;
1337 e4b
->bd_info
->bb_counters
[order
]--;
1341 e4b
->bd_info
->bb_counters
[order
]++;
1343 mb_clear_bit(block
, buddy2
);
1347 mb_set_largest_free_order(sb
, e4b
->bd_info
);
1348 mb_check_buddy(e4b
);
1351 static int mb_find_extent(struct ext4_buddy
*e4b
, int block
,
1352 int needed
, struct ext4_free_extent
*ex
)
1358 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
1361 buddy
= mb_find_buddy(e4b
, 0, &max
);
1362 BUG_ON(buddy
== NULL
);
1363 BUG_ON(block
>= max
);
1364 if (mb_test_bit(block
, buddy
)) {
1371 /* find actual order */
1372 order
= mb_find_order_for_block(e4b
, block
);
1373 block
= block
>> order
;
1375 ex
->fe_len
= 1 << order
;
1376 ex
->fe_start
= block
<< order
;
1377 ex
->fe_group
= e4b
->bd_group
;
1379 /* calc difference from given start */
1380 next
= next
- ex
->fe_start
;
1382 ex
->fe_start
+= next
;
1384 while (needed
> ex
->fe_len
&&
1385 mb_find_buddy(e4b
, order
, &max
)) {
1387 if (block
+ 1 >= max
)
1390 next
= (block
+ 1) * (1 << order
);
1391 if (mb_test_bit(next
, e4b
->bd_bitmap
))
1394 order
= mb_find_order_for_block(e4b
, next
);
1396 block
= next
>> order
;
1397 ex
->fe_len
+= 1 << order
;
1400 BUG_ON(ex
->fe_start
+ ex
->fe_len
> (1 << (e4b
->bd_blkbits
+ 3)));
1404 static int mb_mark_used(struct ext4_buddy
*e4b
, struct ext4_free_extent
*ex
)
1410 int start
= ex
->fe_start
;
1411 int len
= ex
->fe_len
;
1416 BUG_ON(start
+ len
> (e4b
->bd_sb
->s_blocksize
<< 3));
1417 BUG_ON(e4b
->bd_group
!= ex
->fe_group
);
1418 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
1419 mb_check_buddy(e4b
);
1420 mb_mark_used_double(e4b
, start
, len
);
1422 e4b
->bd_info
->bb_free
-= len
;
1423 if (e4b
->bd_info
->bb_first_free
== start
)
1424 e4b
->bd_info
->bb_first_free
+= len
;
1426 /* let's maintain fragments counter */
1428 mlen
= !mb_test_bit(start
- 1, e4b
->bd_bitmap
);
1429 if (start
+ len
< EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[0])
1430 max
= !mb_test_bit(start
+ len
, e4b
->bd_bitmap
);
1432 e4b
->bd_info
->bb_fragments
++;
1433 else if (!mlen
&& !max
)
1434 e4b
->bd_info
->bb_fragments
--;
1436 /* let's maintain buddy itself */
1438 ord
= mb_find_order_for_block(e4b
, start
);
1440 if (((start
>> ord
) << ord
) == start
&& len
>= (1 << ord
)) {
1441 /* the whole chunk may be allocated at once! */
1443 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1444 BUG_ON((start
>> ord
) >= max
);
1445 mb_set_bit(start
>> ord
, buddy
);
1446 e4b
->bd_info
->bb_counters
[ord
]--;
1453 /* store for history */
1455 ret
= len
| (ord
<< 16);
1457 /* we have to split large buddy */
1459 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1460 mb_set_bit(start
>> ord
, buddy
);
1461 e4b
->bd_info
->bb_counters
[ord
]--;
1464 cur
= (start
>> ord
) & ~1U;
1465 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1466 mb_clear_bit(cur
, buddy
);
1467 mb_clear_bit(cur
+ 1, buddy
);
1468 e4b
->bd_info
->bb_counters
[ord
]++;
1469 e4b
->bd_info
->bb_counters
[ord
]++;
1471 mb_set_largest_free_order(e4b
->bd_sb
, e4b
->bd_info
);
1473 ext4_set_bits(e4b
->bd_bitmap
, ex
->fe_start
, len0
);
1474 mb_check_buddy(e4b
);
1480 * Must be called under group lock!
1482 static void ext4_mb_use_best_found(struct ext4_allocation_context
*ac
,
1483 struct ext4_buddy
*e4b
)
1485 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1488 BUG_ON(ac
->ac_b_ex
.fe_group
!= e4b
->bd_group
);
1489 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1491 ac
->ac_b_ex
.fe_len
= min(ac
->ac_b_ex
.fe_len
, ac
->ac_g_ex
.fe_len
);
1492 ac
->ac_b_ex
.fe_logical
= ac
->ac_g_ex
.fe_logical
;
1493 ret
= mb_mark_used(e4b
, &ac
->ac_b_ex
);
1495 /* preallocation can change ac_b_ex, thus we store actually
1496 * allocated blocks for history */
1497 ac
->ac_f_ex
= ac
->ac_b_ex
;
1499 ac
->ac_status
= AC_STATUS_FOUND
;
1500 ac
->ac_tail
= ret
& 0xffff;
1501 ac
->ac_buddy
= ret
>> 16;
1504 * take the page reference. We want the page to be pinned
1505 * so that we don't get a ext4_mb_init_cache_call for this
1506 * group until we update the bitmap. That would mean we
1507 * double allocate blocks. The reference is dropped
1508 * in ext4_mb_release_context
1510 ac
->ac_bitmap_page
= e4b
->bd_bitmap_page
;
1511 get_page(ac
->ac_bitmap_page
);
1512 ac
->ac_buddy_page
= e4b
->bd_buddy_page
;
1513 get_page(ac
->ac_buddy_page
);
1514 /* store last allocated for subsequent stream allocation */
1515 if (ac
->ac_flags
& EXT4_MB_STREAM_ALLOC
) {
1516 spin_lock(&sbi
->s_md_lock
);
1517 sbi
->s_mb_last_group
= ac
->ac_f_ex
.fe_group
;
1518 sbi
->s_mb_last_start
= ac
->ac_f_ex
.fe_start
;
1519 spin_unlock(&sbi
->s_md_lock
);
1524 * regular allocator, for general purposes allocation
1527 static void ext4_mb_check_limits(struct ext4_allocation_context
*ac
,
1528 struct ext4_buddy
*e4b
,
1531 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1532 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1533 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1534 struct ext4_free_extent ex
;
1537 if (ac
->ac_status
== AC_STATUS_FOUND
)
1540 * We don't want to scan for a whole year
1542 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
&&
1543 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1544 ac
->ac_status
= AC_STATUS_BREAK
;
1549 * Haven't found good chunk so far, let's continue
1551 if (bex
->fe_len
< gex
->fe_len
)
1554 if ((finish_group
|| ac
->ac_found
> sbi
->s_mb_min_to_scan
)
1555 && bex
->fe_group
== e4b
->bd_group
) {
1556 /* recheck chunk's availability - we don't know
1557 * when it was found (within this lock-unlock
1559 max
= mb_find_extent(e4b
, bex
->fe_start
, gex
->fe_len
, &ex
);
1560 if (max
>= gex
->fe_len
) {
1561 ext4_mb_use_best_found(ac
, e4b
);
1568 * The routine checks whether found extent is good enough. If it is,
1569 * then the extent gets marked used and flag is set to the context
1570 * to stop scanning. Otherwise, the extent is compared with the
1571 * previous found extent and if new one is better, then it's stored
1572 * in the context. Later, the best found extent will be used, if
1573 * mballoc can't find good enough extent.
1575 * FIXME: real allocation policy is to be designed yet!
1577 static void ext4_mb_measure_extent(struct ext4_allocation_context
*ac
,
1578 struct ext4_free_extent
*ex
,
1579 struct ext4_buddy
*e4b
)
1581 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1582 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1584 BUG_ON(ex
->fe_len
<= 0);
1585 BUG_ON(ex
->fe_len
> EXT4_CLUSTERS_PER_GROUP(ac
->ac_sb
));
1586 BUG_ON(ex
->fe_start
>= EXT4_CLUSTERS_PER_GROUP(ac
->ac_sb
));
1587 BUG_ON(ac
->ac_status
!= AC_STATUS_CONTINUE
);
1592 * The special case - take what you catch first
1594 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1596 ext4_mb_use_best_found(ac
, e4b
);
1601 * Let's check whether the chuck is good enough
1603 if (ex
->fe_len
== gex
->fe_len
) {
1605 ext4_mb_use_best_found(ac
, e4b
);
1610 * If this is first found extent, just store it in the context
1612 if (bex
->fe_len
== 0) {
1618 * If new found extent is better, store it in the context
1620 if (bex
->fe_len
< gex
->fe_len
) {
1621 /* if the request isn't satisfied, any found extent
1622 * larger than previous best one is better */
1623 if (ex
->fe_len
> bex
->fe_len
)
1625 } else if (ex
->fe_len
> gex
->fe_len
) {
1626 /* if the request is satisfied, then we try to find
1627 * an extent that still satisfy the request, but is
1628 * smaller than previous one */
1629 if (ex
->fe_len
< bex
->fe_len
)
1633 ext4_mb_check_limits(ac
, e4b
, 0);
1636 static noinline_for_stack
1637 int ext4_mb_try_best_found(struct ext4_allocation_context
*ac
,
1638 struct ext4_buddy
*e4b
)
1640 struct ext4_free_extent ex
= ac
->ac_b_ex
;
1641 ext4_group_t group
= ex
.fe_group
;
1645 BUG_ON(ex
.fe_len
<= 0);
1646 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1650 ext4_lock_group(ac
->ac_sb
, group
);
1651 max
= mb_find_extent(e4b
, ex
.fe_start
, ex
.fe_len
, &ex
);
1655 ext4_mb_use_best_found(ac
, e4b
);
1658 ext4_unlock_group(ac
->ac_sb
, group
);
1659 ext4_mb_unload_buddy(e4b
);
1664 static noinline_for_stack
1665 int ext4_mb_find_by_goal(struct ext4_allocation_context
*ac
,
1666 struct ext4_buddy
*e4b
)
1668 ext4_group_t group
= ac
->ac_g_ex
.fe_group
;
1671 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1672 struct ext4_group_info
*grp
= ext4_get_group_info(ac
->ac_sb
, group
);
1673 struct ext4_free_extent ex
;
1675 if (!(ac
->ac_flags
& EXT4_MB_HINT_TRY_GOAL
))
1677 if (grp
->bb_free
== 0)
1680 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1684 ext4_lock_group(ac
->ac_sb
, group
);
1685 max
= mb_find_extent(e4b
, ac
->ac_g_ex
.fe_start
,
1686 ac
->ac_g_ex
.fe_len
, &ex
);
1688 if (max
>= ac
->ac_g_ex
.fe_len
&& ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
) {
1691 start
= ext4_group_first_block_no(ac
->ac_sb
, e4b
->bd_group
) +
1693 /* use do_div to get remainder (would be 64-bit modulo) */
1694 if (do_div(start
, sbi
->s_stripe
) == 0) {
1697 ext4_mb_use_best_found(ac
, e4b
);
1699 } else if (max
>= ac
->ac_g_ex
.fe_len
) {
1700 BUG_ON(ex
.fe_len
<= 0);
1701 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1702 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1705 ext4_mb_use_best_found(ac
, e4b
);
1706 } else if (max
> 0 && (ac
->ac_flags
& EXT4_MB_HINT_MERGE
)) {
1707 /* Sometimes, caller may want to merge even small
1708 * number of blocks to an existing extent */
1709 BUG_ON(ex
.fe_len
<= 0);
1710 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1711 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1714 ext4_mb_use_best_found(ac
, e4b
);
1716 ext4_unlock_group(ac
->ac_sb
, group
);
1717 ext4_mb_unload_buddy(e4b
);
1723 * The routine scans buddy structures (not bitmap!) from given order
1724 * to max order and tries to find big enough chunk to satisfy the req
1726 static noinline_for_stack
1727 void ext4_mb_simple_scan_group(struct ext4_allocation_context
*ac
,
1728 struct ext4_buddy
*e4b
)
1730 struct super_block
*sb
= ac
->ac_sb
;
1731 struct ext4_group_info
*grp
= e4b
->bd_info
;
1737 BUG_ON(ac
->ac_2order
<= 0);
1738 for (i
= ac
->ac_2order
; i
<= sb
->s_blocksize_bits
+ 1; i
++) {
1739 if (grp
->bb_counters
[i
] == 0)
1742 buddy
= mb_find_buddy(e4b
, i
, &max
);
1743 BUG_ON(buddy
== NULL
);
1745 k
= mb_find_next_zero_bit(buddy
, max
, 0);
1750 ac
->ac_b_ex
.fe_len
= 1 << i
;
1751 ac
->ac_b_ex
.fe_start
= k
<< i
;
1752 ac
->ac_b_ex
.fe_group
= e4b
->bd_group
;
1754 ext4_mb_use_best_found(ac
, e4b
);
1756 BUG_ON(ac
->ac_b_ex
.fe_len
!= ac
->ac_g_ex
.fe_len
);
1758 if (EXT4_SB(sb
)->s_mb_stats
)
1759 atomic_inc(&EXT4_SB(sb
)->s_bal_2orders
);
1766 * The routine scans the group and measures all found extents.
1767 * In order to optimize scanning, caller must pass number of
1768 * free blocks in the group, so the routine can know upper limit.
1770 static noinline_for_stack
1771 void ext4_mb_complex_scan_group(struct ext4_allocation_context
*ac
,
1772 struct ext4_buddy
*e4b
)
1774 struct super_block
*sb
= ac
->ac_sb
;
1775 void *bitmap
= e4b
->bd_bitmap
;
1776 struct ext4_free_extent ex
;
1780 free
= e4b
->bd_info
->bb_free
;
1783 i
= e4b
->bd_info
->bb_first_free
;
1785 while (free
&& ac
->ac_status
== AC_STATUS_CONTINUE
) {
1786 i
= mb_find_next_zero_bit(bitmap
,
1787 EXT4_CLUSTERS_PER_GROUP(sb
), i
);
1788 if (i
>= EXT4_CLUSTERS_PER_GROUP(sb
)) {
1790 * IF we have corrupt bitmap, we won't find any
1791 * free blocks even though group info says we
1792 * we have free blocks
1794 ext4_grp_locked_error(sb
, e4b
->bd_group
, 0, 0,
1795 "%d free clusters as per "
1796 "group info. But bitmap says 0",
1801 mb_find_extent(e4b
, i
, ac
->ac_g_ex
.fe_len
, &ex
);
1802 BUG_ON(ex
.fe_len
<= 0);
1803 if (free
< ex
.fe_len
) {
1804 ext4_grp_locked_error(sb
, e4b
->bd_group
, 0, 0,
1805 "%d free clusters as per "
1806 "group info. But got %d blocks",
1809 * The number of free blocks differs. This mostly
1810 * indicate that the bitmap is corrupt. So exit
1811 * without claiming the space.
1816 ext4_mb_measure_extent(ac
, &ex
, e4b
);
1822 ext4_mb_check_limits(ac
, e4b
, 1);
1826 * This is a special case for storages like raid5
1827 * we try to find stripe-aligned chunks for stripe-size-multiple requests
1829 static noinline_for_stack
1830 void ext4_mb_scan_aligned(struct ext4_allocation_context
*ac
,
1831 struct ext4_buddy
*e4b
)
1833 struct super_block
*sb
= ac
->ac_sb
;
1834 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1835 void *bitmap
= e4b
->bd_bitmap
;
1836 struct ext4_free_extent ex
;
1837 ext4_fsblk_t first_group_block
;
1842 BUG_ON(sbi
->s_stripe
== 0);
1844 /* find first stripe-aligned block in group */
1845 first_group_block
= ext4_group_first_block_no(sb
, e4b
->bd_group
);
1847 a
= first_group_block
+ sbi
->s_stripe
- 1;
1848 do_div(a
, sbi
->s_stripe
);
1849 i
= (a
* sbi
->s_stripe
) - first_group_block
;
1851 while (i
< EXT4_CLUSTERS_PER_GROUP(sb
)) {
1852 if (!mb_test_bit(i
, bitmap
)) {
1853 max
= mb_find_extent(e4b
, i
, sbi
->s_stripe
, &ex
);
1854 if (max
>= sbi
->s_stripe
) {
1857 ext4_mb_use_best_found(ac
, e4b
);
1865 /* This is now called BEFORE we load the buddy bitmap. */
1866 static int ext4_mb_good_group(struct ext4_allocation_context
*ac
,
1867 ext4_group_t group
, int cr
)
1869 unsigned free
, fragments
;
1870 int flex_size
= ext4_flex_bg_size(EXT4_SB(ac
->ac_sb
));
1871 struct ext4_group_info
*grp
= ext4_get_group_info(ac
->ac_sb
, group
);
1873 BUG_ON(cr
< 0 || cr
>= 4);
1875 free
= grp
->bb_free
;
1878 if (cr
<= 2 && free
< ac
->ac_g_ex
.fe_len
)
1881 /* We only do this if the grp has never been initialized */
1882 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp
))) {
1883 int ret
= ext4_mb_init_group(ac
->ac_sb
, group
);
1888 fragments
= grp
->bb_fragments
;
1894 BUG_ON(ac
->ac_2order
== 0);
1896 /* Avoid using the first bg of a flexgroup for data files */
1897 if ((ac
->ac_flags
& EXT4_MB_HINT_DATA
) &&
1898 (flex_size
>= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
) &&
1899 ((group
% flex_size
) == 0))
1902 if ((ac
->ac_2order
> ac
->ac_sb
->s_blocksize_bits
+1) ||
1903 (free
/ fragments
) >= ac
->ac_g_ex
.fe_len
)
1906 if (grp
->bb_largest_free_order
< ac
->ac_2order
)
1911 if ((free
/ fragments
) >= ac
->ac_g_ex
.fe_len
)
1915 if (free
>= ac
->ac_g_ex
.fe_len
)
1927 static noinline_for_stack
int
1928 ext4_mb_regular_allocator(struct ext4_allocation_context
*ac
)
1930 ext4_group_t ngroups
, group
, i
;
1933 struct ext4_sb_info
*sbi
;
1934 struct super_block
*sb
;
1935 struct ext4_buddy e4b
;
1939 ngroups
= ext4_get_groups_count(sb
);
1940 /* non-extent files are limited to low blocks/groups */
1941 if (!(ext4_test_inode_flag(ac
->ac_inode
, EXT4_INODE_EXTENTS
)))
1942 ngroups
= sbi
->s_blockfile_groups
;
1944 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1946 /* first, try the goal */
1947 err
= ext4_mb_find_by_goal(ac
, &e4b
);
1948 if (err
|| ac
->ac_status
== AC_STATUS_FOUND
)
1951 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
1955 * ac->ac2_order is set only if the fe_len is a power of 2
1956 * if ac2_order is set we also set criteria to 0 so that we
1957 * try exact allocation using buddy.
1959 i
= fls(ac
->ac_g_ex
.fe_len
);
1962 * We search using buddy data only if the order of the request
1963 * is greater than equal to the sbi_s_mb_order2_reqs
1964 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
1966 if (i
>= sbi
->s_mb_order2_reqs
) {
1968 * This should tell if fe_len is exactly power of 2
1970 if ((ac
->ac_g_ex
.fe_len
& (~(1 << (i
- 1)))) == 0)
1971 ac
->ac_2order
= i
- 1;
1974 /* if stream allocation is enabled, use global goal */
1975 if (ac
->ac_flags
& EXT4_MB_STREAM_ALLOC
) {
1976 /* TBD: may be hot point */
1977 spin_lock(&sbi
->s_md_lock
);
1978 ac
->ac_g_ex
.fe_group
= sbi
->s_mb_last_group
;
1979 ac
->ac_g_ex
.fe_start
= sbi
->s_mb_last_start
;
1980 spin_unlock(&sbi
->s_md_lock
);
1983 /* Let's just scan groups to find more-less suitable blocks */
1984 cr
= ac
->ac_2order
? 0 : 1;
1986 * cr == 0 try to get exact allocation,
1987 * cr == 3 try to get anything
1990 for (; cr
< 4 && ac
->ac_status
== AC_STATUS_CONTINUE
; cr
++) {
1991 ac
->ac_criteria
= cr
;
1993 * searching for the right group start
1994 * from the goal value specified
1996 group
= ac
->ac_g_ex
.fe_group
;
1998 for (i
= 0; i
< ngroups
; group
++, i
++) {
1999 if (group
== ngroups
)
2002 /* This now checks without needing the buddy page */
2003 if (!ext4_mb_good_group(ac
, group
, cr
))
2006 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2010 ext4_lock_group(sb
, group
);
2013 * We need to check again after locking the
2016 if (!ext4_mb_good_group(ac
, group
, cr
)) {
2017 ext4_unlock_group(sb
, group
);
2018 ext4_mb_unload_buddy(&e4b
);
2022 ac
->ac_groups_scanned
++;
2023 if (cr
== 0 && ac
->ac_2order
< sb
->s_blocksize_bits
+2)
2024 ext4_mb_simple_scan_group(ac
, &e4b
);
2025 else if (cr
== 1 && sbi
->s_stripe
&&
2026 !(ac
->ac_g_ex
.fe_len
% sbi
->s_stripe
))
2027 ext4_mb_scan_aligned(ac
, &e4b
);
2029 ext4_mb_complex_scan_group(ac
, &e4b
);
2031 ext4_unlock_group(sb
, group
);
2032 ext4_mb_unload_buddy(&e4b
);
2034 if (ac
->ac_status
!= AC_STATUS_CONTINUE
)
2039 if (ac
->ac_b_ex
.fe_len
> 0 && ac
->ac_status
!= AC_STATUS_FOUND
&&
2040 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
2042 * We've been searching too long. Let's try to allocate
2043 * the best chunk we've found so far
2046 ext4_mb_try_best_found(ac
, &e4b
);
2047 if (ac
->ac_status
!= AC_STATUS_FOUND
) {
2049 * Someone more lucky has already allocated it.
2050 * The only thing we can do is just take first
2052 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2054 ac
->ac_b_ex
.fe_group
= 0;
2055 ac
->ac_b_ex
.fe_start
= 0;
2056 ac
->ac_b_ex
.fe_len
= 0;
2057 ac
->ac_status
= AC_STATUS_CONTINUE
;
2058 ac
->ac_flags
|= EXT4_MB_HINT_FIRST
;
2060 atomic_inc(&sbi
->s_mb_lost_chunks
);
2068 static void *ext4_mb_seq_groups_start(struct seq_file
*seq
, loff_t
*pos
)
2070 struct super_block
*sb
= seq
->private;
2073 if (*pos
< 0 || *pos
>= ext4_get_groups_count(sb
))
2076 return (void *) ((unsigned long) group
);
2079 static void *ext4_mb_seq_groups_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2081 struct super_block
*sb
= seq
->private;
2085 if (*pos
< 0 || *pos
>= ext4_get_groups_count(sb
))
2088 return (void *) ((unsigned long) group
);
2091 static int ext4_mb_seq_groups_show(struct seq_file
*seq
, void *v
)
2093 struct super_block
*sb
= seq
->private;
2094 ext4_group_t group
= (ext4_group_t
) ((unsigned long) v
);
2096 int err
, buddy_loaded
= 0;
2097 struct ext4_buddy e4b
;
2098 struct ext4_group_info
*grinfo
;
2100 struct ext4_group_info info
;
2101 ext4_grpblk_t counters
[16];
2106 seq_printf(seq
, "#%-5s: %-5s %-5s %-5s "
2107 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2108 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2109 "group", "free", "frags", "first",
2110 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2111 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2113 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(sg
.info
.bb_counters
[0]) +
2114 sizeof(struct ext4_group_info
);
2115 grinfo
= ext4_get_group_info(sb
, group
);
2116 /* Load the group info in memory only if not already loaded. */
2117 if (unlikely(EXT4_MB_GRP_NEED_INIT(grinfo
))) {
2118 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2120 seq_printf(seq
, "#%-5u: I/O error\n", group
);
2126 memcpy(&sg
, ext4_get_group_info(sb
, group
), i
);
2129 ext4_mb_unload_buddy(&e4b
);
2131 seq_printf(seq
, "#%-5u: %-5u %-5u %-5u [", group
, sg
.info
.bb_free
,
2132 sg
.info
.bb_fragments
, sg
.info
.bb_first_free
);
2133 for (i
= 0; i
<= 13; i
++)
2134 seq_printf(seq
, " %-5u", i
<= sb
->s_blocksize_bits
+ 1 ?
2135 sg
.info
.bb_counters
[i
] : 0);
2136 seq_printf(seq
, " ]\n");
2141 static void ext4_mb_seq_groups_stop(struct seq_file
*seq
, void *v
)
2145 static const struct seq_operations ext4_mb_seq_groups_ops
= {
2146 .start
= ext4_mb_seq_groups_start
,
2147 .next
= ext4_mb_seq_groups_next
,
2148 .stop
= ext4_mb_seq_groups_stop
,
2149 .show
= ext4_mb_seq_groups_show
,
2152 static int ext4_mb_seq_groups_open(struct inode
*inode
, struct file
*file
)
2154 struct super_block
*sb
= PDE(inode
)->data
;
2157 rc
= seq_open(file
, &ext4_mb_seq_groups_ops
);
2159 struct seq_file
*m
= file
->private_data
;
2166 static const struct file_operations ext4_mb_seq_groups_fops
= {
2167 .owner
= THIS_MODULE
,
2168 .open
= ext4_mb_seq_groups_open
,
2170 .llseek
= seq_lseek
,
2171 .release
= seq_release
,
2174 static struct kmem_cache
*get_groupinfo_cache(int blocksize_bits
)
2176 int cache_index
= blocksize_bits
- EXT4_MIN_BLOCK_LOG_SIZE
;
2177 struct kmem_cache
*cachep
= ext4_groupinfo_caches
[cache_index
];
2184 * Allocate the top-level s_group_info array for the specified number
2187 int ext4_mb_alloc_groupinfo(struct super_block
*sb
, ext4_group_t ngroups
)
2189 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2191 struct ext4_group_info
***new_groupinfo
;
2193 size
= (ngroups
+ EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2194 EXT4_DESC_PER_BLOCK_BITS(sb
);
2195 if (size
<= sbi
->s_group_info_size
)
2198 size
= roundup_pow_of_two(sizeof(*sbi
->s_group_info
) * size
);
2199 new_groupinfo
= ext4_kvzalloc(size
, GFP_KERNEL
);
2200 if (!new_groupinfo
) {
2201 ext4_msg(sb
, KERN_ERR
, "can't allocate buddy meta group");
2204 if (sbi
->s_group_info
) {
2205 memcpy(new_groupinfo
, sbi
->s_group_info
,
2206 sbi
->s_group_info_size
* sizeof(*sbi
->s_group_info
));
2207 ext4_kvfree(sbi
->s_group_info
);
2209 sbi
->s_group_info
= new_groupinfo
;
2210 sbi
->s_group_info_size
= size
/ sizeof(*sbi
->s_group_info
);
2211 ext4_debug("allocated s_groupinfo array for %d meta_bg's\n",
2212 sbi
->s_group_info_size
);
2216 /* Create and initialize ext4_group_info data for the given group. */
2217 int ext4_mb_add_groupinfo(struct super_block
*sb
, ext4_group_t group
,
2218 struct ext4_group_desc
*desc
)
2222 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2223 struct ext4_group_info
**meta_group_info
;
2224 struct kmem_cache
*cachep
= get_groupinfo_cache(sb
->s_blocksize_bits
);
2227 * First check if this group is the first of a reserved block.
2228 * If it's true, we have to allocate a new table of pointers
2229 * to ext4_group_info structures
2231 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0) {
2232 metalen
= sizeof(*meta_group_info
) <<
2233 EXT4_DESC_PER_BLOCK_BITS(sb
);
2234 meta_group_info
= kmalloc(metalen
, GFP_KERNEL
);
2235 if (meta_group_info
== NULL
) {
2236 ext4_msg(sb
, KERN_ERR
, "can't allocate mem "
2237 "for a buddy group");
2238 goto exit_meta_group_info
;
2240 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)] =
2245 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)];
2246 i
= group
& (EXT4_DESC_PER_BLOCK(sb
) - 1);
2248 meta_group_info
[i
] = kmem_cache_zalloc(cachep
, GFP_KERNEL
);
2249 if (meta_group_info
[i
] == NULL
) {
2250 ext4_msg(sb
, KERN_ERR
, "can't allocate buddy mem");
2251 goto exit_group_info
;
2253 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
,
2254 &(meta_group_info
[i
]->bb_state
));
2257 * initialize bb_free to be able to skip
2258 * empty groups without initialization
2260 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2261 meta_group_info
[i
]->bb_free
=
2262 ext4_free_clusters_after_init(sb
, group
, desc
);
2264 meta_group_info
[i
]->bb_free
=
2265 ext4_free_group_clusters(sb
, desc
);
2268 INIT_LIST_HEAD(&meta_group_info
[i
]->bb_prealloc_list
);
2269 init_rwsem(&meta_group_info
[i
]->alloc_sem
);
2270 meta_group_info
[i
]->bb_free_root
= RB_ROOT
;
2271 meta_group_info
[i
]->bb_largest_free_order
= -1; /* uninit */
2275 struct buffer_head
*bh
;
2276 meta_group_info
[i
]->bb_bitmap
=
2277 kmalloc(sb
->s_blocksize
, GFP_KERNEL
);
2278 BUG_ON(meta_group_info
[i
]->bb_bitmap
== NULL
);
2279 bh
= ext4_read_block_bitmap(sb
, group
);
2281 memcpy(meta_group_info
[i
]->bb_bitmap
, bh
->b_data
,
2290 /* If a meta_group_info table has been allocated, release it now */
2291 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0) {
2292 kfree(sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)]);
2293 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)] = NULL
;
2295 exit_meta_group_info
:
2297 } /* ext4_mb_add_groupinfo */
2299 static int ext4_mb_init_backend(struct super_block
*sb
)
2301 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
2303 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2305 struct ext4_group_desc
*desc
;
2306 struct kmem_cache
*cachep
;
2308 err
= ext4_mb_alloc_groupinfo(sb
, ngroups
);
2312 sbi
->s_buddy_cache
= new_inode(sb
);
2313 if (sbi
->s_buddy_cache
== NULL
) {
2314 ext4_msg(sb
, KERN_ERR
, "can't get new inode");
2317 /* To avoid potentially colliding with an valid on-disk inode number,
2318 * use EXT4_BAD_INO for the buddy cache inode number. This inode is
2319 * not in the inode hash, so it should never be found by iget(), but
2320 * this will avoid confusion if it ever shows up during debugging. */
2321 sbi
->s_buddy_cache
->i_ino
= EXT4_BAD_INO
;
2322 EXT4_I(sbi
->s_buddy_cache
)->i_disksize
= 0;
2323 for (i
= 0; i
< ngroups
; i
++) {
2324 desc
= ext4_get_group_desc(sb
, i
, NULL
);
2326 ext4_msg(sb
, KERN_ERR
, "can't read descriptor %u", i
);
2329 if (ext4_mb_add_groupinfo(sb
, i
, desc
) != 0)
2336 cachep
= get_groupinfo_cache(sb
->s_blocksize_bits
);
2338 kmem_cache_free(cachep
, ext4_get_group_info(sb
, i
));
2339 i
= sbi
->s_group_info_size
;
2341 kfree(sbi
->s_group_info
[i
]);
2342 iput(sbi
->s_buddy_cache
);
2344 ext4_kvfree(sbi
->s_group_info
);
2348 static void ext4_groupinfo_destroy_slabs(void)
2352 for (i
= 0; i
< NR_GRPINFO_CACHES
; i
++) {
2353 if (ext4_groupinfo_caches
[i
])
2354 kmem_cache_destroy(ext4_groupinfo_caches
[i
]);
2355 ext4_groupinfo_caches
[i
] = NULL
;
2359 static int ext4_groupinfo_create_slab(size_t size
)
2361 static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex
);
2363 int blocksize_bits
= order_base_2(size
);
2364 int cache_index
= blocksize_bits
- EXT4_MIN_BLOCK_LOG_SIZE
;
2365 struct kmem_cache
*cachep
;
2367 if (cache_index
>= NR_GRPINFO_CACHES
)
2370 if (unlikely(cache_index
< 0))
2373 mutex_lock(&ext4_grpinfo_slab_create_mutex
);
2374 if (ext4_groupinfo_caches
[cache_index
]) {
2375 mutex_unlock(&ext4_grpinfo_slab_create_mutex
);
2376 return 0; /* Already created */
2379 slab_size
= offsetof(struct ext4_group_info
,
2380 bb_counters
[blocksize_bits
+ 2]);
2382 cachep
= kmem_cache_create(ext4_groupinfo_slab_names
[cache_index
],
2383 slab_size
, 0, SLAB_RECLAIM_ACCOUNT
,
2386 ext4_groupinfo_caches
[cache_index
] = cachep
;
2388 mutex_unlock(&ext4_grpinfo_slab_create_mutex
);
2391 "EXT4-fs: no memory for groupinfo slab cache\n");
2398 int ext4_mb_init(struct super_block
*sb
)
2400 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2406 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(*sbi
->s_mb_offsets
);
2408 sbi
->s_mb_offsets
= kmalloc(i
, GFP_KERNEL
);
2409 if (sbi
->s_mb_offsets
== NULL
) {
2414 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(*sbi
->s_mb_maxs
);
2415 sbi
->s_mb_maxs
= kmalloc(i
, GFP_KERNEL
);
2416 if (sbi
->s_mb_maxs
== NULL
) {
2421 ret
= ext4_groupinfo_create_slab(sb
->s_blocksize
);
2425 /* order 0 is regular bitmap */
2426 sbi
->s_mb_maxs
[0] = sb
->s_blocksize
<< 3;
2427 sbi
->s_mb_offsets
[0] = 0;
2431 max
= sb
->s_blocksize
<< 2;
2433 sbi
->s_mb_offsets
[i
] = offset
;
2434 sbi
->s_mb_maxs
[i
] = max
;
2435 offset
+= 1 << (sb
->s_blocksize_bits
- i
);
2438 } while (i
<= sb
->s_blocksize_bits
+ 1);
2440 spin_lock_init(&sbi
->s_md_lock
);
2441 spin_lock_init(&sbi
->s_bal_lock
);
2443 sbi
->s_mb_max_to_scan
= MB_DEFAULT_MAX_TO_SCAN
;
2444 sbi
->s_mb_min_to_scan
= MB_DEFAULT_MIN_TO_SCAN
;
2445 sbi
->s_mb_stats
= MB_DEFAULT_STATS
;
2446 sbi
->s_mb_stream_request
= MB_DEFAULT_STREAM_THRESHOLD
;
2447 sbi
->s_mb_order2_reqs
= MB_DEFAULT_ORDER2_REQS
;
2449 * The default group preallocation is 512, which for 4k block
2450 * sizes translates to 2 megabytes. However for bigalloc file
2451 * systems, this is probably too big (i.e, if the cluster size
2452 * is 1 megabyte, then group preallocation size becomes half a
2453 * gigabyte!). As a default, we will keep a two megabyte
2454 * group pralloc size for cluster sizes up to 64k, and after
2455 * that, we will force a minimum group preallocation size of
2456 * 32 clusters. This translates to 8 megs when the cluster
2457 * size is 256k, and 32 megs when the cluster size is 1 meg,
2458 * which seems reasonable as a default.
2460 sbi
->s_mb_group_prealloc
= max(MB_DEFAULT_GROUP_PREALLOC
>>
2461 sbi
->s_cluster_bits
, 32);
2463 * If there is a s_stripe > 1, then we set the s_mb_group_prealloc
2464 * to the lowest multiple of s_stripe which is bigger than
2465 * the s_mb_group_prealloc as determined above. We want
2466 * the preallocation size to be an exact multiple of the
2467 * RAID stripe size so that preallocations don't fragment
2470 if (sbi
->s_stripe
> 1) {
2471 sbi
->s_mb_group_prealloc
= roundup(
2472 sbi
->s_mb_group_prealloc
, sbi
->s_stripe
);
2475 sbi
->s_locality_groups
= alloc_percpu(struct ext4_locality_group
);
2476 if (sbi
->s_locality_groups
== NULL
) {
2478 goto out_free_groupinfo_slab
;
2480 for_each_possible_cpu(i
) {
2481 struct ext4_locality_group
*lg
;
2482 lg
= per_cpu_ptr(sbi
->s_locality_groups
, i
);
2483 mutex_init(&lg
->lg_mutex
);
2484 for (j
= 0; j
< PREALLOC_TB_SIZE
; j
++)
2485 INIT_LIST_HEAD(&lg
->lg_prealloc_list
[j
]);
2486 spin_lock_init(&lg
->lg_prealloc_lock
);
2489 /* init file for buddy data */
2490 ret
= ext4_mb_init_backend(sb
);
2492 goto out_free_locality_groups
;
2495 proc_create_data("mb_groups", S_IRUGO
, sbi
->s_proc
,
2496 &ext4_mb_seq_groups_fops
, sb
);
2500 out_free_locality_groups
:
2501 free_percpu(sbi
->s_locality_groups
);
2502 sbi
->s_locality_groups
= NULL
;
2503 out_free_groupinfo_slab
:
2504 ext4_groupinfo_destroy_slabs();
2506 kfree(sbi
->s_mb_offsets
);
2507 sbi
->s_mb_offsets
= NULL
;
2508 kfree(sbi
->s_mb_maxs
);
2509 sbi
->s_mb_maxs
= NULL
;
2513 /* need to called with the ext4 group lock held */
2514 static void ext4_mb_cleanup_pa(struct ext4_group_info
*grp
)
2516 struct ext4_prealloc_space
*pa
;
2517 struct list_head
*cur
, *tmp
;
2520 list_for_each_safe(cur
, tmp
, &grp
->bb_prealloc_list
) {
2521 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
2522 list_del(&pa
->pa_group_list
);
2524 kmem_cache_free(ext4_pspace_cachep
, pa
);
2527 mb_debug(1, "mballoc: %u PAs left\n", count
);
2531 int ext4_mb_release(struct super_block
*sb
)
2533 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
2535 int num_meta_group_infos
;
2536 struct ext4_group_info
*grinfo
;
2537 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2538 struct kmem_cache
*cachep
= get_groupinfo_cache(sb
->s_blocksize_bits
);
2541 remove_proc_entry("mb_groups", sbi
->s_proc
);
2543 if (sbi
->s_group_info
) {
2544 for (i
= 0; i
< ngroups
; i
++) {
2545 grinfo
= ext4_get_group_info(sb
, i
);
2547 kfree(grinfo
->bb_bitmap
);
2549 ext4_lock_group(sb
, i
);
2550 ext4_mb_cleanup_pa(grinfo
);
2551 ext4_unlock_group(sb
, i
);
2552 kmem_cache_free(cachep
, grinfo
);
2554 num_meta_group_infos
= (ngroups
+
2555 EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2556 EXT4_DESC_PER_BLOCK_BITS(sb
);
2557 for (i
= 0; i
< num_meta_group_infos
; i
++)
2558 kfree(sbi
->s_group_info
[i
]);
2559 ext4_kvfree(sbi
->s_group_info
);
2561 kfree(sbi
->s_mb_offsets
);
2562 kfree(sbi
->s_mb_maxs
);
2563 if (sbi
->s_buddy_cache
)
2564 iput(sbi
->s_buddy_cache
);
2565 if (sbi
->s_mb_stats
) {
2566 ext4_msg(sb
, KERN_INFO
,
2567 "mballoc: %u blocks %u reqs (%u success)",
2568 atomic_read(&sbi
->s_bal_allocated
),
2569 atomic_read(&sbi
->s_bal_reqs
),
2570 atomic_read(&sbi
->s_bal_success
));
2571 ext4_msg(sb
, KERN_INFO
,
2572 "mballoc: %u extents scanned, %u goal hits, "
2573 "%u 2^N hits, %u breaks, %u lost",
2574 atomic_read(&sbi
->s_bal_ex_scanned
),
2575 atomic_read(&sbi
->s_bal_goals
),
2576 atomic_read(&sbi
->s_bal_2orders
),
2577 atomic_read(&sbi
->s_bal_breaks
),
2578 atomic_read(&sbi
->s_mb_lost_chunks
));
2579 ext4_msg(sb
, KERN_INFO
,
2580 "mballoc: %lu generated and it took %Lu",
2581 sbi
->s_mb_buddies_generated
,
2582 sbi
->s_mb_generation_time
);
2583 ext4_msg(sb
, KERN_INFO
,
2584 "mballoc: %u preallocated, %u discarded",
2585 atomic_read(&sbi
->s_mb_preallocated
),
2586 atomic_read(&sbi
->s_mb_discarded
));
2589 free_percpu(sbi
->s_locality_groups
);
2594 static inline int ext4_issue_discard(struct super_block
*sb
,
2595 ext4_group_t block_group
, ext4_grpblk_t cluster
, int count
)
2597 ext4_fsblk_t discard_block
;
2599 discard_block
= (EXT4_C2B(EXT4_SB(sb
), cluster
) +
2600 ext4_group_first_block_no(sb
, block_group
));
2601 count
= EXT4_C2B(EXT4_SB(sb
), count
);
2602 trace_ext4_discard_blocks(sb
,
2603 (unsigned long long) discard_block
, count
);
2604 return sb_issue_discard(sb
, discard_block
, count
, GFP_NOFS
, 0);
2608 * This function is called by the jbd2 layer once the commit has finished,
2609 * so we know we can free the blocks that were released with that commit.
2611 static void ext4_free_data_callback(struct super_block
*sb
,
2612 struct ext4_journal_cb_entry
*jce
,
2615 struct ext4_free_data
*entry
= (struct ext4_free_data
*)jce
;
2616 struct ext4_buddy e4b
;
2617 struct ext4_group_info
*db
;
2618 int err
, count
= 0, count2
= 0;
2620 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
2621 entry
->efd_count
, entry
->efd_group
, entry
);
2623 if (test_opt(sb
, DISCARD
)) {
2624 err
= ext4_issue_discard(sb
, entry
->efd_group
,
2625 entry
->efd_start_cluster
,
2627 if (err
&& err
!= -EOPNOTSUPP
)
2628 ext4_msg(sb
, KERN_WARNING
, "discard request in"
2629 " group:%d block:%d count:%d failed"
2630 " with %d", entry
->efd_group
,
2631 entry
->efd_start_cluster
,
2632 entry
->efd_count
, err
);
2635 err
= ext4_mb_load_buddy(sb
, entry
->efd_group
, &e4b
);
2636 /* we expect to find existing buddy because it's pinned */
2641 /* there are blocks to put in buddy to make them really free */
2642 count
+= entry
->efd_count
;
2644 ext4_lock_group(sb
, entry
->efd_group
);
2645 /* Take it out of per group rb tree */
2646 rb_erase(&entry
->efd_node
, &(db
->bb_free_root
));
2647 mb_free_blocks(NULL
, &e4b
, entry
->efd_start_cluster
, entry
->efd_count
);
2650 * Clear the trimmed flag for the group so that the next
2651 * ext4_trim_fs can trim it.
2652 * If the volume is mounted with -o discard, online discard
2653 * is supported and the free blocks will be trimmed online.
2655 if (!test_opt(sb
, DISCARD
))
2656 EXT4_MB_GRP_CLEAR_TRIMMED(db
);
2658 if (!db
->bb_free_root
.rb_node
) {
2659 /* No more items in the per group rb tree
2660 * balance refcounts from ext4_mb_free_metadata()
2662 page_cache_release(e4b
.bd_buddy_page
);
2663 page_cache_release(e4b
.bd_bitmap_page
);
2665 ext4_unlock_group(sb
, entry
->efd_group
);
2666 kmem_cache_free(ext4_free_data_cachep
, entry
);
2667 ext4_mb_unload_buddy(&e4b
);
2669 mb_debug(1, "freed %u blocks in %u structures\n", count
, count2
);
2672 int __init
ext4_init_mballoc(void)
2674 ext4_pspace_cachep
= KMEM_CACHE(ext4_prealloc_space
,
2675 SLAB_RECLAIM_ACCOUNT
);
2676 if (ext4_pspace_cachep
== NULL
)
2679 ext4_ac_cachep
= KMEM_CACHE(ext4_allocation_context
,
2680 SLAB_RECLAIM_ACCOUNT
);
2681 if (ext4_ac_cachep
== NULL
) {
2682 kmem_cache_destroy(ext4_pspace_cachep
);
2686 ext4_free_data_cachep
= KMEM_CACHE(ext4_free_data
,
2687 SLAB_RECLAIM_ACCOUNT
);
2688 if (ext4_free_data_cachep
== NULL
) {
2689 kmem_cache_destroy(ext4_pspace_cachep
);
2690 kmem_cache_destroy(ext4_ac_cachep
);
2696 void ext4_exit_mballoc(void)
2699 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2700 * before destroying the slab cache.
2703 kmem_cache_destroy(ext4_pspace_cachep
);
2704 kmem_cache_destroy(ext4_ac_cachep
);
2705 kmem_cache_destroy(ext4_free_data_cachep
);
2706 ext4_groupinfo_destroy_slabs();
2711 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
2712 * Returns 0 if success or error code
2714 static noinline_for_stack
int
2715 ext4_mb_mark_diskspace_used(struct ext4_allocation_context
*ac
,
2716 handle_t
*handle
, unsigned int reserv_clstrs
)
2718 struct buffer_head
*bitmap_bh
= NULL
;
2719 struct ext4_group_desc
*gdp
;
2720 struct buffer_head
*gdp_bh
;
2721 struct ext4_sb_info
*sbi
;
2722 struct super_block
*sb
;
2726 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
2727 BUG_ON(ac
->ac_b_ex
.fe_len
<= 0);
2733 bitmap_bh
= ext4_read_block_bitmap(sb
, ac
->ac_b_ex
.fe_group
);
2737 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
2742 gdp
= ext4_get_group_desc(sb
, ac
->ac_b_ex
.fe_group
, &gdp_bh
);
2746 ext4_debug("using block group %u(%d)\n", ac
->ac_b_ex
.fe_group
,
2747 ext4_free_group_clusters(sb
, gdp
));
2749 err
= ext4_journal_get_write_access(handle
, gdp_bh
);
2753 block
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
2755 len
= EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
);
2756 if (!ext4_data_block_valid(sbi
, block
, len
)) {
2757 ext4_error(sb
, "Allocating blocks %llu-%llu which overlap "
2758 "fs metadata", block
, block
+len
);
2759 /* File system mounted not to panic on error
2760 * Fix the bitmap and repeat the block allocation
2761 * We leak some of the blocks here.
2763 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
2764 ext4_set_bits(bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,
2765 ac
->ac_b_ex
.fe_len
);
2766 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
2767 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
2773 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
2774 #ifdef AGGRESSIVE_CHECK
2777 for (i
= 0; i
< ac
->ac_b_ex
.fe_len
; i
++) {
2778 BUG_ON(mb_test_bit(ac
->ac_b_ex
.fe_start
+ i
,
2779 bitmap_bh
->b_data
));
2783 ext4_set_bits(bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,
2784 ac
->ac_b_ex
.fe_len
);
2785 if (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2786 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT
);
2787 ext4_free_group_clusters_set(sb
, gdp
,
2788 ext4_free_clusters_after_init(sb
,
2789 ac
->ac_b_ex
.fe_group
, gdp
));
2791 len
= ext4_free_group_clusters(sb
, gdp
) - ac
->ac_b_ex
.fe_len
;
2792 ext4_free_group_clusters_set(sb
, gdp
, len
);
2793 ext4_block_bitmap_csum_set(sb
, ac
->ac_b_ex
.fe_group
, gdp
, bitmap_bh
);
2794 ext4_group_desc_csum_set(sb
, ac
->ac_b_ex
.fe_group
, gdp
);
2796 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
2797 percpu_counter_sub(&sbi
->s_freeclusters_counter
, ac
->ac_b_ex
.fe_len
);
2799 * Now reduce the dirty block count also. Should not go negative
2801 if (!(ac
->ac_flags
& EXT4_MB_DELALLOC_RESERVED
))
2802 /* release all the reserved blocks if non delalloc */
2803 percpu_counter_sub(&sbi
->s_dirtyclusters_counter
,
2806 if (sbi
->s_log_groups_per_flex
) {
2807 ext4_group_t flex_group
= ext4_flex_group(sbi
,
2808 ac
->ac_b_ex
.fe_group
);
2809 atomic64_sub(ac
->ac_b_ex
.fe_len
,
2810 &sbi
->s_flex_groups
[flex_group
].free_clusters
);
2813 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
2816 err
= ext4_handle_dirty_metadata(handle
, NULL
, gdp_bh
);
2824 * here we normalize request for locality group
2825 * Group request are normalized to s_mb_group_prealloc, which goes to
2826 * s_strip if we set the same via mount option.
2827 * s_mb_group_prealloc can be configured via
2828 * /sys/fs/ext4/<partition>/mb_group_prealloc
2830 * XXX: should we try to preallocate more than the group has now?
2832 static void ext4_mb_normalize_group_request(struct ext4_allocation_context
*ac
)
2834 struct super_block
*sb
= ac
->ac_sb
;
2835 struct ext4_locality_group
*lg
= ac
->ac_lg
;
2838 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_mb_group_prealloc
;
2839 mb_debug(1, "#%u: goal %u blocks for locality group\n",
2840 current
->pid
, ac
->ac_g_ex
.fe_len
);
2844 * Normalization means making request better in terms of
2845 * size and alignment
2847 static noinline_for_stack
void
2848 ext4_mb_normalize_request(struct ext4_allocation_context
*ac
,
2849 struct ext4_allocation_request
*ar
)
2851 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
2854 loff_t size
, start_off
;
2855 loff_t orig_size __maybe_unused
;
2857 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
2858 struct ext4_prealloc_space
*pa
;
2860 /* do normalize only data requests, metadata requests
2861 do not need preallocation */
2862 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
2865 /* sometime caller may want exact blocks */
2866 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
2869 /* caller may indicate that preallocation isn't
2870 * required (it's a tail, for example) */
2871 if (ac
->ac_flags
& EXT4_MB_HINT_NOPREALLOC
)
2874 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
) {
2875 ext4_mb_normalize_group_request(ac
);
2879 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
2881 /* first, let's learn actual file size
2882 * given current request is allocated */
2883 size
= ac
->ac_o_ex
.fe_logical
+ EXT4_C2B(sbi
, ac
->ac_o_ex
.fe_len
);
2884 size
= size
<< bsbits
;
2885 if (size
< i_size_read(ac
->ac_inode
))
2886 size
= i_size_read(ac
->ac_inode
);
2889 /* max size of free chunks */
2892 #define NRL_CHECK_SIZE(req, size, max, chunk_size) \
2893 (req <= (size) || max <= (chunk_size))
2895 /* first, try to predict filesize */
2896 /* XXX: should this table be tunable? */
2898 if (size
<= 16 * 1024) {
2900 } else if (size
<= 32 * 1024) {
2902 } else if (size
<= 64 * 1024) {
2904 } else if (size
<= 128 * 1024) {
2906 } else if (size
<= 256 * 1024) {
2908 } else if (size
<= 512 * 1024) {
2910 } else if (size
<= 1024 * 1024) {
2912 } else if (NRL_CHECK_SIZE(size
, 4 * 1024 * 1024, max
, 2 * 1024)) {
2913 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
2914 (21 - bsbits
)) << 21;
2915 size
= 2 * 1024 * 1024;
2916 } else if (NRL_CHECK_SIZE(size
, 8 * 1024 * 1024, max
, 4 * 1024)) {
2917 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
2918 (22 - bsbits
)) << 22;
2919 size
= 4 * 1024 * 1024;
2920 } else if (NRL_CHECK_SIZE(ac
->ac_o_ex
.fe_len
,
2921 (8<<20)>>bsbits
, max
, 8 * 1024)) {
2922 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
2923 (23 - bsbits
)) << 23;
2924 size
= 8 * 1024 * 1024;
2926 start_off
= (loff_t
)ac
->ac_o_ex
.fe_logical
<< bsbits
;
2927 size
= ac
->ac_o_ex
.fe_len
<< bsbits
;
2929 size
= size
>> bsbits
;
2930 start
= start_off
>> bsbits
;
2932 /* don't cover already allocated blocks in selected range */
2933 if (ar
->pleft
&& start
<= ar
->lleft
) {
2934 size
-= ar
->lleft
+ 1 - start
;
2935 start
= ar
->lleft
+ 1;
2937 if (ar
->pright
&& start
+ size
- 1 >= ar
->lright
)
2938 size
-= start
+ size
- ar
->lright
;
2942 /* check we don't cross already preallocated blocks */
2944 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
2949 spin_lock(&pa
->pa_lock
);
2950 if (pa
->pa_deleted
) {
2951 spin_unlock(&pa
->pa_lock
);
2955 pa_end
= pa
->pa_lstart
+ EXT4_C2B(EXT4_SB(ac
->ac_sb
),
2958 /* PA must not overlap original request */
2959 BUG_ON(!(ac
->ac_o_ex
.fe_logical
>= pa_end
||
2960 ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
));
2962 /* skip PAs this normalized request doesn't overlap with */
2963 if (pa
->pa_lstart
>= end
|| pa_end
<= start
) {
2964 spin_unlock(&pa
->pa_lock
);
2967 BUG_ON(pa
->pa_lstart
<= start
&& pa_end
>= end
);
2969 /* adjust start or end to be adjacent to this pa */
2970 if (pa_end
<= ac
->ac_o_ex
.fe_logical
) {
2971 BUG_ON(pa_end
< start
);
2973 } else if (pa
->pa_lstart
> ac
->ac_o_ex
.fe_logical
) {
2974 BUG_ON(pa
->pa_lstart
> end
);
2975 end
= pa
->pa_lstart
;
2977 spin_unlock(&pa
->pa_lock
);
2982 /* XXX: extra loop to check we really don't overlap preallocations */
2984 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
2987 spin_lock(&pa
->pa_lock
);
2988 if (pa
->pa_deleted
== 0) {
2989 pa_end
= pa
->pa_lstart
+ EXT4_C2B(EXT4_SB(ac
->ac_sb
),
2991 BUG_ON(!(start
>= pa_end
|| end
<= pa
->pa_lstart
));
2993 spin_unlock(&pa
->pa_lock
);
2997 if (start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
2998 start
> ac
->ac_o_ex
.fe_logical
) {
2999 ext4_msg(ac
->ac_sb
, KERN_ERR
,
3000 "start %lu, size %lu, fe_logical %lu",
3001 (unsigned long) start
, (unsigned long) size
,
3002 (unsigned long) ac
->ac_o_ex
.fe_logical
);
3004 BUG_ON(start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3005 start
> ac
->ac_o_ex
.fe_logical
);
3006 BUG_ON(size
<= 0 || size
> EXT4_CLUSTERS_PER_GROUP(ac
->ac_sb
));
3008 /* now prepare goal request */
3010 /* XXX: is it better to align blocks WRT to logical
3011 * placement or satisfy big request as is */
3012 ac
->ac_g_ex
.fe_logical
= start
;
3013 ac
->ac_g_ex
.fe_len
= EXT4_NUM_B2C(sbi
, size
);
3015 /* define goal start in order to merge */
3016 if (ar
->pright
&& (ar
->lright
== (start
+ size
))) {
3017 /* merge to the right */
3018 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pright
- size
,
3019 &ac
->ac_f_ex
.fe_group
,
3020 &ac
->ac_f_ex
.fe_start
);
3021 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3023 if (ar
->pleft
&& (ar
->lleft
+ 1 == start
)) {
3024 /* merge to the left */
3025 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pleft
+ 1,
3026 &ac
->ac_f_ex
.fe_group
,
3027 &ac
->ac_f_ex
.fe_start
);
3028 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3031 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size
,
3032 (unsigned) orig_size
, (unsigned) start
);
3035 static void ext4_mb_collect_stats(struct ext4_allocation_context
*ac
)
3037 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3039 if (sbi
->s_mb_stats
&& ac
->ac_g_ex
.fe_len
> 1) {
3040 atomic_inc(&sbi
->s_bal_reqs
);
3041 atomic_add(ac
->ac_b_ex
.fe_len
, &sbi
->s_bal_allocated
);
3042 if (ac
->ac_b_ex
.fe_len
>= ac
->ac_o_ex
.fe_len
)
3043 atomic_inc(&sbi
->s_bal_success
);
3044 atomic_add(ac
->ac_found
, &sbi
->s_bal_ex_scanned
);
3045 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
3046 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
3047 atomic_inc(&sbi
->s_bal_goals
);
3048 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
)
3049 atomic_inc(&sbi
->s_bal_breaks
);
3052 if (ac
->ac_op
== EXT4_MB_HISTORY_ALLOC
)
3053 trace_ext4_mballoc_alloc(ac
);
3055 trace_ext4_mballoc_prealloc(ac
);
3059 * Called on failure; free up any blocks from the inode PA for this
3060 * context. We don't need this for MB_GROUP_PA because we only change
3061 * pa_free in ext4_mb_release_context(), but on failure, we've already
3062 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3064 static void ext4_discard_allocated_blocks(struct ext4_allocation_context
*ac
)
3066 struct ext4_prealloc_space
*pa
= ac
->ac_pa
;
3068 if (pa
&& pa
->pa_type
== MB_INODE_PA
)
3069 pa
->pa_free
+= ac
->ac_b_ex
.fe_len
;
3073 * use blocks preallocated to inode
3075 static void ext4_mb_use_inode_pa(struct ext4_allocation_context
*ac
,
3076 struct ext4_prealloc_space
*pa
)
3078 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3083 /* found preallocated blocks, use them */
3084 start
= pa
->pa_pstart
+ (ac
->ac_o_ex
.fe_logical
- pa
->pa_lstart
);
3085 end
= min(pa
->pa_pstart
+ EXT4_C2B(sbi
, pa
->pa_len
),
3086 start
+ EXT4_C2B(sbi
, ac
->ac_o_ex
.fe_len
));
3087 len
= EXT4_NUM_B2C(sbi
, end
- start
);
3088 ext4_get_group_no_and_offset(ac
->ac_sb
, start
, &ac
->ac_b_ex
.fe_group
,
3089 &ac
->ac_b_ex
.fe_start
);
3090 ac
->ac_b_ex
.fe_len
= len
;
3091 ac
->ac_status
= AC_STATUS_FOUND
;
3094 BUG_ON(start
< pa
->pa_pstart
);
3095 BUG_ON(end
> pa
->pa_pstart
+ EXT4_C2B(sbi
, pa
->pa_len
));
3096 BUG_ON(pa
->pa_free
< len
);
3099 mb_debug(1, "use %llu/%u from inode pa %p\n", start
, len
, pa
);
3103 * use blocks preallocated to locality group
3105 static void ext4_mb_use_group_pa(struct ext4_allocation_context
*ac
,
3106 struct ext4_prealloc_space
*pa
)
3108 unsigned int len
= ac
->ac_o_ex
.fe_len
;
3110 ext4_get_group_no_and_offset(ac
->ac_sb
, pa
->pa_pstart
,
3111 &ac
->ac_b_ex
.fe_group
,
3112 &ac
->ac_b_ex
.fe_start
);
3113 ac
->ac_b_ex
.fe_len
= len
;
3114 ac
->ac_status
= AC_STATUS_FOUND
;
3117 /* we don't correct pa_pstart or pa_plen here to avoid
3118 * possible race when the group is being loaded concurrently
3119 * instead we correct pa later, after blocks are marked
3120 * in on-disk bitmap -- see ext4_mb_release_context()
3121 * Other CPUs are prevented from allocating from this pa by lg_mutex
3123 mb_debug(1, "use %u/%u from group pa %p\n", pa
->pa_lstart
-len
, len
, pa
);
3127 * Return the prealloc space that have minimal distance
3128 * from the goal block. @cpa is the prealloc
3129 * space that is having currently known minimal distance
3130 * from the goal block.
3132 static struct ext4_prealloc_space
*
3133 ext4_mb_check_group_pa(ext4_fsblk_t goal_block
,
3134 struct ext4_prealloc_space
*pa
,
3135 struct ext4_prealloc_space
*cpa
)
3137 ext4_fsblk_t cur_distance
, new_distance
;
3140 atomic_inc(&pa
->pa_count
);
3143 cur_distance
= abs(goal_block
- cpa
->pa_pstart
);
3144 new_distance
= abs(goal_block
- pa
->pa_pstart
);
3146 if (cur_distance
<= new_distance
)
3149 /* drop the previous reference */
3150 atomic_dec(&cpa
->pa_count
);
3151 atomic_inc(&pa
->pa_count
);
3156 * search goal blocks in preallocated space
3158 static noinline_for_stack
int
3159 ext4_mb_use_preallocated(struct ext4_allocation_context
*ac
)
3161 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3163 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3164 struct ext4_locality_group
*lg
;
3165 struct ext4_prealloc_space
*pa
, *cpa
= NULL
;
3166 ext4_fsblk_t goal_block
;
3168 /* only data can be preallocated */
3169 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3172 /* first, try per-file preallocation */
3174 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3176 /* all fields in this condition don't change,
3177 * so we can skip locking for them */
3178 if (ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
||
3179 ac
->ac_o_ex
.fe_logical
>= (pa
->pa_lstart
+
3180 EXT4_C2B(sbi
, pa
->pa_len
)))
3183 /* non-extent files can't have physical blocks past 2^32 */
3184 if (!(ext4_test_inode_flag(ac
->ac_inode
, EXT4_INODE_EXTENTS
)) &&
3185 (pa
->pa_pstart
+ EXT4_C2B(sbi
, pa
->pa_len
) >
3186 EXT4_MAX_BLOCK_FILE_PHYS
))
3189 /* found preallocated blocks, use them */
3190 spin_lock(&pa
->pa_lock
);
3191 if (pa
->pa_deleted
== 0 && pa
->pa_free
) {
3192 atomic_inc(&pa
->pa_count
);
3193 ext4_mb_use_inode_pa(ac
, pa
);
3194 spin_unlock(&pa
->pa_lock
);
3195 ac
->ac_criteria
= 10;
3199 spin_unlock(&pa
->pa_lock
);
3203 /* can we use group allocation? */
3204 if (!(ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
))
3207 /* inode may have no locality group for some reason */
3211 order
= fls(ac
->ac_o_ex
.fe_len
) - 1;
3212 if (order
> PREALLOC_TB_SIZE
- 1)
3213 /* The max size of hash table is PREALLOC_TB_SIZE */
3214 order
= PREALLOC_TB_SIZE
- 1;
3216 goal_block
= ext4_grp_offs_to_block(ac
->ac_sb
, &ac
->ac_g_ex
);
3218 * search for the prealloc space that is having
3219 * minimal distance from the goal block.
3221 for (i
= order
; i
< PREALLOC_TB_SIZE
; i
++) {
3223 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[i
],
3225 spin_lock(&pa
->pa_lock
);
3226 if (pa
->pa_deleted
== 0 &&
3227 pa
->pa_free
>= ac
->ac_o_ex
.fe_len
) {
3229 cpa
= ext4_mb_check_group_pa(goal_block
,
3232 spin_unlock(&pa
->pa_lock
);
3237 ext4_mb_use_group_pa(ac
, cpa
);
3238 ac
->ac_criteria
= 20;
3245 * the function goes through all block freed in the group
3246 * but not yet committed and marks them used in in-core bitmap.
3247 * buddy must be generated from this bitmap
3248 * Need to be called with the ext4 group lock held
3250 static void ext4_mb_generate_from_freelist(struct super_block
*sb
, void *bitmap
,
3254 struct ext4_group_info
*grp
;
3255 struct ext4_free_data
*entry
;
3257 grp
= ext4_get_group_info(sb
, group
);
3258 n
= rb_first(&(grp
->bb_free_root
));
3261 entry
= rb_entry(n
, struct ext4_free_data
, efd_node
);
3262 ext4_set_bits(bitmap
, entry
->efd_start_cluster
, entry
->efd_count
);
3269 * the function goes through all preallocation in this group and marks them
3270 * used in in-core bitmap. buddy must be generated from this bitmap
3271 * Need to be called with ext4 group lock held
3273 static noinline_for_stack
3274 void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
3277 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3278 struct ext4_prealloc_space
*pa
;
3279 struct list_head
*cur
;
3280 ext4_group_t groupnr
;
3281 ext4_grpblk_t start
;
3282 int preallocated
= 0;
3285 /* all form of preallocation discards first load group,
3286 * so the only competing code is preallocation use.
3287 * we don't need any locking here
3288 * notice we do NOT ignore preallocations with pa_deleted
3289 * otherwise we could leave used blocks available for
3290 * allocation in buddy when concurrent ext4_mb_put_pa()
3291 * is dropping preallocation
3293 list_for_each(cur
, &grp
->bb_prealloc_list
) {
3294 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
3295 spin_lock(&pa
->pa_lock
);
3296 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
3299 spin_unlock(&pa
->pa_lock
);
3300 if (unlikely(len
== 0))
3302 BUG_ON(groupnr
!= group
);
3303 ext4_set_bits(bitmap
, start
, len
);
3304 preallocated
+= len
;
3306 mb_debug(1, "prellocated %u for group %u\n", preallocated
, group
);
3309 static void ext4_mb_pa_callback(struct rcu_head
*head
)
3311 struct ext4_prealloc_space
*pa
;
3312 pa
= container_of(head
, struct ext4_prealloc_space
, u
.pa_rcu
);
3313 kmem_cache_free(ext4_pspace_cachep
, pa
);
3317 * drops a reference to preallocated space descriptor
3318 * if this was the last reference and the space is consumed
3320 static void ext4_mb_put_pa(struct ext4_allocation_context
*ac
,
3321 struct super_block
*sb
, struct ext4_prealloc_space
*pa
)
3324 ext4_fsblk_t grp_blk
;
3326 if (!atomic_dec_and_test(&pa
->pa_count
) || pa
->pa_free
!= 0)
3329 /* in this short window concurrent discard can set pa_deleted */
3330 spin_lock(&pa
->pa_lock
);
3331 if (pa
->pa_deleted
== 1) {
3332 spin_unlock(&pa
->pa_lock
);
3337 spin_unlock(&pa
->pa_lock
);
3339 grp_blk
= pa
->pa_pstart
;
3341 * If doing group-based preallocation, pa_pstart may be in the
3342 * next group when pa is used up
3344 if (pa
->pa_type
== MB_GROUP_PA
)
3347 ext4_get_group_no_and_offset(sb
, grp_blk
, &grp
, NULL
);
3352 * P1 (buddy init) P2 (regular allocation)
3353 * find block B in PA
3354 * copy on-disk bitmap to buddy
3355 * mark B in on-disk bitmap
3356 * drop PA from group
3357 * mark all PAs in buddy
3359 * thus, P1 initializes buddy with B available. to prevent this
3360 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3363 ext4_lock_group(sb
, grp
);
3364 list_del(&pa
->pa_group_list
);
3365 ext4_unlock_group(sb
, grp
);
3367 spin_lock(pa
->pa_obj_lock
);
3368 list_del_rcu(&pa
->pa_inode_list
);
3369 spin_unlock(pa
->pa_obj_lock
);
3371 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3375 * creates new preallocated space for given inode
3377 static noinline_for_stack
int
3378 ext4_mb_new_inode_pa(struct ext4_allocation_context
*ac
)
3380 struct super_block
*sb
= ac
->ac_sb
;
3381 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3382 struct ext4_prealloc_space
*pa
;
3383 struct ext4_group_info
*grp
;
3384 struct ext4_inode_info
*ei
;
3386 /* preallocate only when found space is larger then requested */
3387 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3388 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3389 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3391 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3395 if (ac
->ac_b_ex
.fe_len
< ac
->ac_g_ex
.fe_len
) {
3401 /* we can't allocate as much as normalizer wants.
3402 * so, found space must get proper lstart
3403 * to cover original request */
3404 BUG_ON(ac
->ac_g_ex
.fe_logical
> ac
->ac_o_ex
.fe_logical
);
3405 BUG_ON(ac
->ac_g_ex
.fe_len
< ac
->ac_o_ex
.fe_len
);
3407 /* we're limited by original request in that
3408 * logical block must be covered any way
3409 * winl is window we can move our chunk within */
3410 winl
= ac
->ac_o_ex
.fe_logical
- ac
->ac_g_ex
.fe_logical
;
3412 /* also, we should cover whole original request */
3413 wins
= EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
- ac
->ac_o_ex
.fe_len
);
3415 /* the smallest one defines real window */
3416 win
= min(winl
, wins
);
3418 offs
= ac
->ac_o_ex
.fe_logical
%
3419 EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
);
3420 if (offs
&& offs
< win
)
3423 ac
->ac_b_ex
.fe_logical
= ac
->ac_o_ex
.fe_logical
-
3424 EXT4_NUM_B2C(sbi
, win
);
3425 BUG_ON(ac
->ac_o_ex
.fe_logical
< ac
->ac_b_ex
.fe_logical
);
3426 BUG_ON(ac
->ac_o_ex
.fe_len
> ac
->ac_b_ex
.fe_len
);
3429 /* preallocation can change ac_b_ex, thus we store actually
3430 * allocated blocks for history */
3431 ac
->ac_f_ex
= ac
->ac_b_ex
;
3433 pa
->pa_lstart
= ac
->ac_b_ex
.fe_logical
;
3434 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3435 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3436 pa
->pa_free
= pa
->pa_len
;
3437 atomic_set(&pa
->pa_count
, 1);
3438 spin_lock_init(&pa
->pa_lock
);
3439 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3440 INIT_LIST_HEAD(&pa
->pa_group_list
);
3442 pa
->pa_type
= MB_INODE_PA
;
3444 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa
,
3445 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3446 trace_ext4_mb_new_inode_pa(ac
, pa
);
3448 ext4_mb_use_inode_pa(ac
, pa
);
3449 atomic_add(pa
->pa_free
, &sbi
->s_mb_preallocated
);
3451 ei
= EXT4_I(ac
->ac_inode
);
3452 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3454 pa
->pa_obj_lock
= &ei
->i_prealloc_lock
;
3455 pa
->pa_inode
= ac
->ac_inode
;
3457 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3458 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3459 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3461 spin_lock(pa
->pa_obj_lock
);
3462 list_add_rcu(&pa
->pa_inode_list
, &ei
->i_prealloc_list
);
3463 spin_unlock(pa
->pa_obj_lock
);
3469 * creates new preallocated space for locality group inodes belongs to
3471 static noinline_for_stack
int
3472 ext4_mb_new_group_pa(struct ext4_allocation_context
*ac
)
3474 struct super_block
*sb
= ac
->ac_sb
;
3475 struct ext4_locality_group
*lg
;
3476 struct ext4_prealloc_space
*pa
;
3477 struct ext4_group_info
*grp
;
3479 /* preallocate only when found space is larger then requested */
3480 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3481 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3482 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3484 BUG_ON(ext4_pspace_cachep
== NULL
);
3485 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3489 /* preallocation can change ac_b_ex, thus we store actually
3490 * allocated blocks for history */
3491 ac
->ac_f_ex
= ac
->ac_b_ex
;
3493 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3494 pa
->pa_lstart
= pa
->pa_pstart
;
3495 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3496 pa
->pa_free
= pa
->pa_len
;
3497 atomic_set(&pa
->pa_count
, 1);
3498 spin_lock_init(&pa
->pa_lock
);
3499 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3500 INIT_LIST_HEAD(&pa
->pa_group_list
);
3502 pa
->pa_type
= MB_GROUP_PA
;
3504 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa
,
3505 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3506 trace_ext4_mb_new_group_pa(ac
, pa
);
3508 ext4_mb_use_group_pa(ac
, pa
);
3509 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3511 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3515 pa
->pa_obj_lock
= &lg
->lg_prealloc_lock
;
3516 pa
->pa_inode
= NULL
;
3518 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3519 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3520 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3523 * We will later add the new pa to the right bucket
3524 * after updating the pa_free in ext4_mb_release_context
3529 static int ext4_mb_new_preallocation(struct ext4_allocation_context
*ac
)
3533 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
3534 err
= ext4_mb_new_group_pa(ac
);
3536 err
= ext4_mb_new_inode_pa(ac
);
3541 * finds all unused blocks in on-disk bitmap, frees them in
3542 * in-core bitmap and buddy.
3543 * @pa must be unlinked from inode and group lists, so that
3544 * nobody else can find/use it.
3545 * the caller MUST hold group/inode locks.
3546 * TODO: optimize the case when there are no in-core structures yet
3548 static noinline_for_stack
int
3549 ext4_mb_release_inode_pa(struct ext4_buddy
*e4b
, struct buffer_head
*bitmap_bh
,
3550 struct ext4_prealloc_space
*pa
)
3552 struct super_block
*sb
= e4b
->bd_sb
;
3553 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3558 unsigned long long grp_blk_start
;
3562 BUG_ON(pa
->pa_deleted
== 0);
3563 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3564 grp_blk_start
= pa
->pa_pstart
- EXT4_C2B(sbi
, bit
);
3565 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3566 end
= bit
+ pa
->pa_len
;
3569 bit
= mb_find_next_zero_bit(bitmap_bh
->b_data
, end
, bit
);
3572 next
= mb_find_next_bit(bitmap_bh
->b_data
, end
, bit
);
3573 mb_debug(1, " free preallocated %u/%u in group %u\n",
3574 (unsigned) ext4_group_first_block_no(sb
, group
) + bit
,
3575 (unsigned) next
- bit
, (unsigned) group
);
3578 trace_ext4_mballoc_discard(sb
, NULL
, group
, bit
, next
- bit
);
3579 trace_ext4_mb_release_inode_pa(pa
, (grp_blk_start
+
3580 EXT4_C2B(sbi
, bit
)),
3582 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, next
- bit
);
3585 if (free
!= pa
->pa_free
) {
3586 ext4_msg(e4b
->bd_sb
, KERN_CRIT
,
3587 "pa %p: logic %lu, phys. %lu, len %lu",
3588 pa
, (unsigned long) pa
->pa_lstart
,
3589 (unsigned long) pa
->pa_pstart
,
3590 (unsigned long) pa
->pa_len
);
3591 ext4_grp_locked_error(sb
, group
, 0, 0, "free %u, pa_free %u",
3594 * pa is already deleted so we use the value obtained
3595 * from the bitmap and continue.
3598 atomic_add(free
, &sbi
->s_mb_discarded
);
3603 static noinline_for_stack
int
3604 ext4_mb_release_group_pa(struct ext4_buddy
*e4b
,
3605 struct ext4_prealloc_space
*pa
)
3607 struct super_block
*sb
= e4b
->bd_sb
;
3611 trace_ext4_mb_release_group_pa(sb
, pa
);
3612 BUG_ON(pa
->pa_deleted
== 0);
3613 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3614 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3615 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, pa
->pa_len
);
3616 atomic_add(pa
->pa_len
, &EXT4_SB(sb
)->s_mb_discarded
);
3617 trace_ext4_mballoc_discard(sb
, NULL
, group
, bit
, pa
->pa_len
);
3623 * releases all preallocations in given group
3625 * first, we need to decide discard policy:
3626 * - when do we discard
3628 * - how many do we discard
3629 * 1) how many requested
3631 static noinline_for_stack
int
3632 ext4_mb_discard_group_preallocations(struct super_block
*sb
,
3633 ext4_group_t group
, int needed
)
3635 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3636 struct buffer_head
*bitmap_bh
= NULL
;
3637 struct ext4_prealloc_space
*pa
, *tmp
;
3638 struct list_head list
;
3639 struct ext4_buddy e4b
;
3644 mb_debug(1, "discard preallocation for group %u\n", group
);
3646 if (list_empty(&grp
->bb_prealloc_list
))
3649 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
3650 if (bitmap_bh
== NULL
) {
3651 ext4_error(sb
, "Error reading block bitmap for %u", group
);
3655 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
3657 ext4_error(sb
, "Error loading buddy information for %u", group
);
3663 needed
= EXT4_CLUSTERS_PER_GROUP(sb
) + 1;
3665 INIT_LIST_HEAD(&list
);
3667 ext4_lock_group(sb
, group
);
3668 list_for_each_entry_safe(pa
, tmp
,
3669 &grp
->bb_prealloc_list
, pa_group_list
) {
3670 spin_lock(&pa
->pa_lock
);
3671 if (atomic_read(&pa
->pa_count
)) {
3672 spin_unlock(&pa
->pa_lock
);
3676 if (pa
->pa_deleted
) {
3677 spin_unlock(&pa
->pa_lock
);
3681 /* seems this one can be freed ... */
3684 /* we can trust pa_free ... */
3685 free
+= pa
->pa_free
;
3687 spin_unlock(&pa
->pa_lock
);
3689 list_del(&pa
->pa_group_list
);
3690 list_add(&pa
->u
.pa_tmp_list
, &list
);
3693 /* if we still need more blocks and some PAs were used, try again */
3694 if (free
< needed
&& busy
) {
3696 ext4_unlock_group(sb
, group
);
3701 /* found anything to free? */
3702 if (list_empty(&list
)) {
3707 /* now free all selected PAs */
3708 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
3710 /* remove from object (inode or locality group) */
3711 spin_lock(pa
->pa_obj_lock
);
3712 list_del_rcu(&pa
->pa_inode_list
);
3713 spin_unlock(pa
->pa_obj_lock
);
3715 if (pa
->pa_type
== MB_GROUP_PA
)
3716 ext4_mb_release_group_pa(&e4b
, pa
);
3718 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
);
3720 list_del(&pa
->u
.pa_tmp_list
);
3721 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3725 ext4_unlock_group(sb
, group
);
3726 ext4_mb_unload_buddy(&e4b
);
3732 * releases all non-used preallocated blocks for given inode
3734 * It's important to discard preallocations under i_data_sem
3735 * We don't want another block to be served from the prealloc
3736 * space when we are discarding the inode prealloc space.
3738 * FIXME!! Make sure it is valid at all the call sites
3740 void ext4_discard_preallocations(struct inode
*inode
)
3742 struct ext4_inode_info
*ei
= EXT4_I(inode
);
3743 struct super_block
*sb
= inode
->i_sb
;
3744 struct buffer_head
*bitmap_bh
= NULL
;
3745 struct ext4_prealloc_space
*pa
, *tmp
;
3746 ext4_group_t group
= 0;
3747 struct list_head list
;
3748 struct ext4_buddy e4b
;
3751 if (!S_ISREG(inode
->i_mode
)) {
3752 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3756 mb_debug(1, "discard preallocation for inode %lu\n", inode
->i_ino
);
3757 trace_ext4_discard_preallocations(inode
);
3759 INIT_LIST_HEAD(&list
);
3762 /* first, collect all pa's in the inode */
3763 spin_lock(&ei
->i_prealloc_lock
);
3764 while (!list_empty(&ei
->i_prealloc_list
)) {
3765 pa
= list_entry(ei
->i_prealloc_list
.next
,
3766 struct ext4_prealloc_space
, pa_inode_list
);
3767 BUG_ON(pa
->pa_obj_lock
!= &ei
->i_prealloc_lock
);
3768 spin_lock(&pa
->pa_lock
);
3769 if (atomic_read(&pa
->pa_count
)) {
3770 /* this shouldn't happen often - nobody should
3771 * use preallocation while we're discarding it */
3772 spin_unlock(&pa
->pa_lock
);
3773 spin_unlock(&ei
->i_prealloc_lock
);
3774 ext4_msg(sb
, KERN_ERR
,
3775 "uh-oh! used pa while discarding");
3777 schedule_timeout_uninterruptible(HZ
);
3781 if (pa
->pa_deleted
== 0) {
3783 spin_unlock(&pa
->pa_lock
);
3784 list_del_rcu(&pa
->pa_inode_list
);
3785 list_add(&pa
->u
.pa_tmp_list
, &list
);
3789 /* someone is deleting pa right now */
3790 spin_unlock(&pa
->pa_lock
);
3791 spin_unlock(&ei
->i_prealloc_lock
);
3793 /* we have to wait here because pa_deleted
3794 * doesn't mean pa is already unlinked from
3795 * the list. as we might be called from
3796 * ->clear_inode() the inode will get freed
3797 * and concurrent thread which is unlinking
3798 * pa from inode's list may access already
3799 * freed memory, bad-bad-bad */
3801 /* XXX: if this happens too often, we can
3802 * add a flag to force wait only in case
3803 * of ->clear_inode(), but not in case of
3804 * regular truncate */
3805 schedule_timeout_uninterruptible(HZ
);
3808 spin_unlock(&ei
->i_prealloc_lock
);
3810 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
3811 BUG_ON(pa
->pa_type
!= MB_INODE_PA
);
3812 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
3814 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
3816 ext4_error(sb
, "Error loading buddy information for %u",
3821 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
3822 if (bitmap_bh
== NULL
) {
3823 ext4_error(sb
, "Error reading block bitmap for %u",
3825 ext4_mb_unload_buddy(&e4b
);
3829 ext4_lock_group(sb
, group
);
3830 list_del(&pa
->pa_group_list
);
3831 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
);
3832 ext4_unlock_group(sb
, group
);
3834 ext4_mb_unload_buddy(&e4b
);
3837 list_del(&pa
->u
.pa_tmp_list
);
3838 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3842 #ifdef CONFIG_EXT4_DEBUG
3843 static void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
3845 struct super_block
*sb
= ac
->ac_sb
;
3846 ext4_group_t ngroups
, i
;
3848 if (!ext4_mballoc_debug
||
3849 (EXT4_SB(sb
)->s_mount_flags
& EXT4_MF_FS_ABORTED
))
3852 ext4_msg(ac
->ac_sb
, KERN_ERR
, "Can't allocate:"
3853 " Allocation context details:");
3854 ext4_msg(ac
->ac_sb
, KERN_ERR
, "status %d flags %d",
3855 ac
->ac_status
, ac
->ac_flags
);
3856 ext4_msg(ac
->ac_sb
, KERN_ERR
, "orig %lu/%lu/%lu@%lu, "
3857 "goal %lu/%lu/%lu@%lu, "
3858 "best %lu/%lu/%lu@%lu cr %d",
3859 (unsigned long)ac
->ac_o_ex
.fe_group
,
3860 (unsigned long)ac
->ac_o_ex
.fe_start
,
3861 (unsigned long)ac
->ac_o_ex
.fe_len
,
3862 (unsigned long)ac
->ac_o_ex
.fe_logical
,
3863 (unsigned long)ac
->ac_g_ex
.fe_group
,
3864 (unsigned long)ac
->ac_g_ex
.fe_start
,
3865 (unsigned long)ac
->ac_g_ex
.fe_len
,
3866 (unsigned long)ac
->ac_g_ex
.fe_logical
,
3867 (unsigned long)ac
->ac_b_ex
.fe_group
,
3868 (unsigned long)ac
->ac_b_ex
.fe_start
,
3869 (unsigned long)ac
->ac_b_ex
.fe_len
,
3870 (unsigned long)ac
->ac_b_ex
.fe_logical
,
3871 (int)ac
->ac_criteria
);
3872 ext4_msg(ac
->ac_sb
, KERN_ERR
, "%lu scanned, %d found",
3873 ac
->ac_ex_scanned
, ac
->ac_found
);
3874 ext4_msg(ac
->ac_sb
, KERN_ERR
, "groups: ");
3875 ngroups
= ext4_get_groups_count(sb
);
3876 for (i
= 0; i
< ngroups
; i
++) {
3877 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, i
);
3878 struct ext4_prealloc_space
*pa
;
3879 ext4_grpblk_t start
;
3880 struct list_head
*cur
;
3881 ext4_lock_group(sb
, i
);
3882 list_for_each(cur
, &grp
->bb_prealloc_list
) {
3883 pa
= list_entry(cur
, struct ext4_prealloc_space
,
3885 spin_lock(&pa
->pa_lock
);
3886 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
3888 spin_unlock(&pa
->pa_lock
);
3889 printk(KERN_ERR
"PA:%u:%d:%u \n", i
,
3892 ext4_unlock_group(sb
, i
);
3894 if (grp
->bb_free
== 0)
3896 printk(KERN_ERR
"%u: %d/%d \n",
3897 i
, grp
->bb_free
, grp
->bb_fragments
);
3899 printk(KERN_ERR
"\n");
3902 static inline void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
3909 * We use locality group preallocation for small size file. The size of the
3910 * file is determined by the current size or the resulting size after
3911 * allocation which ever is larger
3913 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
3915 static void ext4_mb_group_or_file(struct ext4_allocation_context
*ac
)
3917 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3918 int bsbits
= ac
->ac_sb
->s_blocksize_bits
;
3921 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3924 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
3927 size
= ac
->ac_o_ex
.fe_logical
+ EXT4_C2B(sbi
, ac
->ac_o_ex
.fe_len
);
3928 isize
= (i_size_read(ac
->ac_inode
) + ac
->ac_sb
->s_blocksize
- 1)
3931 if ((size
== isize
) &&
3932 !ext4_fs_is_busy(sbi
) &&
3933 (atomic_read(&ac
->ac_inode
->i_writecount
) == 0)) {
3934 ac
->ac_flags
|= EXT4_MB_HINT_NOPREALLOC
;
3938 if (sbi
->s_mb_group_prealloc
<= 0) {
3939 ac
->ac_flags
|= EXT4_MB_STREAM_ALLOC
;
3943 /* don't use group allocation for large files */
3944 size
= max(size
, isize
);
3945 if (size
> sbi
->s_mb_stream_request
) {
3946 ac
->ac_flags
|= EXT4_MB_STREAM_ALLOC
;
3950 BUG_ON(ac
->ac_lg
!= NULL
);
3952 * locality group prealloc space are per cpu. The reason for having
3953 * per cpu locality group is to reduce the contention between block
3954 * request from multiple CPUs.
3956 ac
->ac_lg
= __this_cpu_ptr(sbi
->s_locality_groups
);
3958 /* we're going to use group allocation */
3959 ac
->ac_flags
|= EXT4_MB_HINT_GROUP_ALLOC
;
3961 /* serialize all allocations in the group */
3962 mutex_lock(&ac
->ac_lg
->lg_mutex
);
3965 static noinline_for_stack
int
3966 ext4_mb_initialize_context(struct ext4_allocation_context
*ac
,
3967 struct ext4_allocation_request
*ar
)
3969 struct super_block
*sb
= ar
->inode
->i_sb
;
3970 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3971 struct ext4_super_block
*es
= sbi
->s_es
;
3975 ext4_grpblk_t block
;
3977 /* we can't allocate > group size */
3980 /* just a dirty hack to filter too big requests */
3981 if (len
>= EXT4_CLUSTERS_PER_GROUP(sb
))
3982 len
= EXT4_CLUSTERS_PER_GROUP(sb
);
3984 /* start searching from the goal */
3986 if (goal
< le32_to_cpu(es
->s_first_data_block
) ||
3987 goal
>= ext4_blocks_count(es
))
3988 goal
= le32_to_cpu(es
->s_first_data_block
);
3989 ext4_get_group_no_and_offset(sb
, goal
, &group
, &block
);
3991 /* set up allocation goals */
3992 ac
->ac_b_ex
.fe_logical
= ar
->logical
& ~(sbi
->s_cluster_ratio
- 1);
3993 ac
->ac_status
= AC_STATUS_CONTINUE
;
3995 ac
->ac_inode
= ar
->inode
;
3996 ac
->ac_o_ex
.fe_logical
= ac
->ac_b_ex
.fe_logical
;
3997 ac
->ac_o_ex
.fe_group
= group
;
3998 ac
->ac_o_ex
.fe_start
= block
;
3999 ac
->ac_o_ex
.fe_len
= len
;
4000 ac
->ac_g_ex
= ac
->ac_o_ex
;
4001 ac
->ac_flags
= ar
->flags
;
4003 /* we have to define context: we'll we work with a file or
4004 * locality group. this is a policy, actually */
4005 ext4_mb_group_or_file(ac
);
4007 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
4008 "left: %u/%u, right %u/%u to %swritable\n",
4009 (unsigned) ar
->len
, (unsigned) ar
->logical
,
4010 (unsigned) ar
->goal
, ac
->ac_flags
, ac
->ac_2order
,
4011 (unsigned) ar
->lleft
, (unsigned) ar
->pleft
,
4012 (unsigned) ar
->lright
, (unsigned) ar
->pright
,
4013 atomic_read(&ar
->inode
->i_writecount
) ? "" : "non-");
4018 static noinline_for_stack
void
4019 ext4_mb_discard_lg_preallocations(struct super_block
*sb
,
4020 struct ext4_locality_group
*lg
,
4021 int order
, int total_entries
)
4023 ext4_group_t group
= 0;
4024 struct ext4_buddy e4b
;
4025 struct list_head discard_list
;
4026 struct ext4_prealloc_space
*pa
, *tmp
;
4028 mb_debug(1, "discard locality group preallocation\n");
4030 INIT_LIST_HEAD(&discard_list
);
4032 spin_lock(&lg
->lg_prealloc_lock
);
4033 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[order
],
4035 spin_lock(&pa
->pa_lock
);
4036 if (atomic_read(&pa
->pa_count
)) {
4038 * This is the pa that we just used
4039 * for block allocation. So don't
4042 spin_unlock(&pa
->pa_lock
);
4045 if (pa
->pa_deleted
) {
4046 spin_unlock(&pa
->pa_lock
);
4049 /* only lg prealloc space */
4050 BUG_ON(pa
->pa_type
!= MB_GROUP_PA
);
4052 /* seems this one can be freed ... */
4054 spin_unlock(&pa
->pa_lock
);
4056 list_del_rcu(&pa
->pa_inode_list
);
4057 list_add(&pa
->u
.pa_tmp_list
, &discard_list
);
4060 if (total_entries
<= 5) {
4062 * we want to keep only 5 entries
4063 * allowing it to grow to 8. This
4064 * mak sure we don't call discard
4065 * soon for this list.
4070 spin_unlock(&lg
->lg_prealloc_lock
);
4072 list_for_each_entry_safe(pa
, tmp
, &discard_list
, u
.pa_tmp_list
) {
4074 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
4075 if (ext4_mb_load_buddy(sb
, group
, &e4b
)) {
4076 ext4_error(sb
, "Error loading buddy information for %u",
4080 ext4_lock_group(sb
, group
);
4081 list_del(&pa
->pa_group_list
);
4082 ext4_mb_release_group_pa(&e4b
, pa
);
4083 ext4_unlock_group(sb
, group
);
4085 ext4_mb_unload_buddy(&e4b
);
4086 list_del(&pa
->u
.pa_tmp_list
);
4087 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4092 * We have incremented pa_count. So it cannot be freed at this
4093 * point. Also we hold lg_mutex. So no parallel allocation is
4094 * possible from this lg. That means pa_free cannot be updated.
4096 * A parallel ext4_mb_discard_group_preallocations is possible.
4097 * which can cause the lg_prealloc_list to be updated.
4100 static void ext4_mb_add_n_trim(struct ext4_allocation_context
*ac
)
4102 int order
, added
= 0, lg_prealloc_count
= 1;
4103 struct super_block
*sb
= ac
->ac_sb
;
4104 struct ext4_locality_group
*lg
= ac
->ac_lg
;
4105 struct ext4_prealloc_space
*tmp_pa
, *pa
= ac
->ac_pa
;
4107 order
= fls(pa
->pa_free
) - 1;
4108 if (order
> PREALLOC_TB_SIZE
- 1)
4109 /* The max size of hash table is PREALLOC_TB_SIZE */
4110 order
= PREALLOC_TB_SIZE
- 1;
4111 /* Add the prealloc space to lg */
4112 spin_lock(&lg
->lg_prealloc_lock
);
4113 list_for_each_entry_rcu(tmp_pa
, &lg
->lg_prealloc_list
[order
],
4115 spin_lock(&tmp_pa
->pa_lock
);
4116 if (tmp_pa
->pa_deleted
) {
4117 spin_unlock(&tmp_pa
->pa_lock
);
4120 if (!added
&& pa
->pa_free
< tmp_pa
->pa_free
) {
4121 /* Add to the tail of the previous entry */
4122 list_add_tail_rcu(&pa
->pa_inode_list
,
4123 &tmp_pa
->pa_inode_list
);
4126 * we want to count the total
4127 * number of entries in the list
4130 spin_unlock(&tmp_pa
->pa_lock
);
4131 lg_prealloc_count
++;
4134 list_add_tail_rcu(&pa
->pa_inode_list
,
4135 &lg
->lg_prealloc_list
[order
]);
4136 spin_unlock(&lg
->lg_prealloc_lock
);
4138 /* Now trim the list to be not more than 8 elements */
4139 if (lg_prealloc_count
> 8) {
4140 ext4_mb_discard_lg_preallocations(sb
, lg
,
4141 order
, lg_prealloc_count
);
4148 * release all resource we used in allocation
4150 static int ext4_mb_release_context(struct ext4_allocation_context
*ac
)
4152 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
4153 struct ext4_prealloc_space
*pa
= ac
->ac_pa
;
4155 if (pa
->pa_type
== MB_GROUP_PA
) {
4156 /* see comment in ext4_mb_use_group_pa() */
4157 spin_lock(&pa
->pa_lock
);
4158 pa
->pa_pstart
+= EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
);
4159 pa
->pa_lstart
+= EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
);
4160 pa
->pa_free
-= ac
->ac_b_ex
.fe_len
;
4161 pa
->pa_len
-= ac
->ac_b_ex
.fe_len
;
4162 spin_unlock(&pa
->pa_lock
);
4167 * We want to add the pa to the right bucket.
4168 * Remove it from the list and while adding
4169 * make sure the list to which we are adding
4172 if ((pa
->pa_type
== MB_GROUP_PA
) && likely(pa
->pa_free
)) {
4173 spin_lock(pa
->pa_obj_lock
);
4174 list_del_rcu(&pa
->pa_inode_list
);
4175 spin_unlock(pa
->pa_obj_lock
);
4176 ext4_mb_add_n_trim(ac
);
4178 ext4_mb_put_pa(ac
, ac
->ac_sb
, pa
);
4180 if (ac
->ac_bitmap_page
)
4181 page_cache_release(ac
->ac_bitmap_page
);
4182 if (ac
->ac_buddy_page
)
4183 page_cache_release(ac
->ac_buddy_page
);
4184 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
4185 mutex_unlock(&ac
->ac_lg
->lg_mutex
);
4186 ext4_mb_collect_stats(ac
);
4190 static int ext4_mb_discard_preallocations(struct super_block
*sb
, int needed
)
4192 ext4_group_t i
, ngroups
= ext4_get_groups_count(sb
);
4196 trace_ext4_mb_discard_preallocations(sb
, needed
);
4197 for (i
= 0; i
< ngroups
&& needed
> 0; i
++) {
4198 ret
= ext4_mb_discard_group_preallocations(sb
, i
, needed
);
4207 * Main entry point into mballoc to allocate blocks
4208 * it tries to use preallocation first, then falls back
4209 * to usual allocation
4211 ext4_fsblk_t
ext4_mb_new_blocks(handle_t
*handle
,
4212 struct ext4_allocation_request
*ar
, int *errp
)
4215 struct ext4_allocation_context
*ac
= NULL
;
4216 struct ext4_sb_info
*sbi
;
4217 struct super_block
*sb
;
4218 ext4_fsblk_t block
= 0;
4219 unsigned int inquota
= 0;
4220 unsigned int reserv_clstrs
= 0;
4223 sb
= ar
->inode
->i_sb
;
4226 trace_ext4_request_blocks(ar
);
4228 /* Allow to use superuser reservation for quota file */
4229 if (IS_NOQUOTA(ar
->inode
))
4230 ar
->flags
|= EXT4_MB_USE_ROOT_BLOCKS
;
4233 * For delayed allocation, we could skip the ENOSPC and
4234 * EDQUOT check, as blocks and quotas have been already
4235 * reserved when data being copied into pagecache.
4237 if (ext4_test_inode_state(ar
->inode
, EXT4_STATE_DELALLOC_RESERVED
))
4238 ar
->flags
|= EXT4_MB_DELALLOC_RESERVED
;
4240 /* Without delayed allocation we need to verify
4241 * there is enough free blocks to do block allocation
4242 * and verify allocation doesn't exceed the quota limits.
4245 ext4_claim_free_clusters(sbi
, ar
->len
, ar
->flags
)) {
4247 /* let others to free the space */
4249 ar
->len
= ar
->len
>> 1;
4255 reserv_clstrs
= ar
->len
;
4256 if (ar
->flags
& EXT4_MB_USE_ROOT_BLOCKS
) {
4257 dquot_alloc_block_nofail(ar
->inode
,
4258 EXT4_C2B(sbi
, ar
->len
));
4261 dquot_alloc_block(ar
->inode
,
4262 EXT4_C2B(sbi
, ar
->len
))) {
4264 ar
->flags
|= EXT4_MB_HINT_NOPREALLOC
;
4275 ac
= kmem_cache_zalloc(ext4_ac_cachep
, GFP_NOFS
);
4282 *errp
= ext4_mb_initialize_context(ac
, ar
);
4288 ac
->ac_op
= EXT4_MB_HISTORY_PREALLOC
;
4289 if (!ext4_mb_use_preallocated(ac
)) {
4290 ac
->ac_op
= EXT4_MB_HISTORY_ALLOC
;
4291 ext4_mb_normalize_request(ac
, ar
);
4293 /* allocate space in core */
4294 *errp
= ext4_mb_regular_allocator(ac
);
4296 ext4_discard_allocated_blocks(ac
);
4300 /* as we've just preallocated more space than
4301 * user requested orinally, we store allocated
4302 * space in a special descriptor */
4303 if (ac
->ac_status
== AC_STATUS_FOUND
&&
4304 ac
->ac_o_ex
.fe_len
< ac
->ac_b_ex
.fe_len
)
4305 ext4_mb_new_preallocation(ac
);
4307 if (likely(ac
->ac_status
== AC_STATUS_FOUND
)) {
4308 *errp
= ext4_mb_mark_diskspace_used(ac
, handle
, reserv_clstrs
);
4309 if (*errp
== -EAGAIN
) {
4311 * drop the reference that we took
4312 * in ext4_mb_use_best_found
4314 ext4_mb_release_context(ac
);
4315 ac
->ac_b_ex
.fe_group
= 0;
4316 ac
->ac_b_ex
.fe_start
= 0;
4317 ac
->ac_b_ex
.fe_len
= 0;
4318 ac
->ac_status
= AC_STATUS_CONTINUE
;
4321 ext4_discard_allocated_blocks(ac
);
4324 block
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
4325 ar
->len
= ac
->ac_b_ex
.fe_len
;
4328 freed
= ext4_mb_discard_preallocations(sb
, ac
->ac_o_ex
.fe_len
);
4336 ac
->ac_b_ex
.fe_len
= 0;
4338 ext4_mb_show_ac(ac
);
4340 ext4_mb_release_context(ac
);
4343 kmem_cache_free(ext4_ac_cachep
, ac
);
4344 if (inquota
&& ar
->len
< inquota
)
4345 dquot_free_block(ar
->inode
, EXT4_C2B(sbi
, inquota
- ar
->len
));
4347 if (!ext4_test_inode_state(ar
->inode
,
4348 EXT4_STATE_DELALLOC_RESERVED
))
4349 /* release all the reserved blocks if non delalloc */
4350 percpu_counter_sub(&sbi
->s_dirtyclusters_counter
,
4354 trace_ext4_allocate_blocks(ar
, (unsigned long long)block
);
4360 * We can merge two free data extents only if the physical blocks
4361 * are contiguous, AND the extents were freed by the same transaction,
4362 * AND the blocks are associated with the same group.
4364 static int can_merge(struct ext4_free_data
*entry1
,
4365 struct ext4_free_data
*entry2
)
4367 if ((entry1
->efd_tid
== entry2
->efd_tid
) &&
4368 (entry1
->efd_group
== entry2
->efd_group
) &&
4369 ((entry1
->efd_start_cluster
+ entry1
->efd_count
) == entry2
->efd_start_cluster
))
4374 static noinline_for_stack
int
4375 ext4_mb_free_metadata(handle_t
*handle
, struct ext4_buddy
*e4b
,
4376 struct ext4_free_data
*new_entry
)
4378 ext4_group_t group
= e4b
->bd_group
;
4379 ext4_grpblk_t cluster
;
4380 struct ext4_free_data
*entry
;
4381 struct ext4_group_info
*db
= e4b
->bd_info
;
4382 struct super_block
*sb
= e4b
->bd_sb
;
4383 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4384 struct rb_node
**n
= &db
->bb_free_root
.rb_node
, *node
;
4385 struct rb_node
*parent
= NULL
, *new_node
;
4387 BUG_ON(!ext4_handle_valid(handle
));
4388 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
4389 BUG_ON(e4b
->bd_buddy_page
== NULL
);
4391 new_node
= &new_entry
->efd_node
;
4392 cluster
= new_entry
->efd_start_cluster
;
4395 /* first free block exent. We need to
4396 protect buddy cache from being freed,
4397 * otherwise we'll refresh it from
4398 * on-disk bitmap and lose not-yet-available
4400 page_cache_get(e4b
->bd_buddy_page
);
4401 page_cache_get(e4b
->bd_bitmap_page
);
4405 entry
= rb_entry(parent
, struct ext4_free_data
, efd_node
);
4406 if (cluster
< entry
->efd_start_cluster
)
4408 else if (cluster
>= (entry
->efd_start_cluster
+ entry
->efd_count
))
4409 n
= &(*n
)->rb_right
;
4411 ext4_grp_locked_error(sb
, group
, 0,
4412 ext4_group_first_block_no(sb
, group
) +
4413 EXT4_C2B(sbi
, cluster
),
4414 "Block already on to-be-freed list");
4419 rb_link_node(new_node
, parent
, n
);
4420 rb_insert_color(new_node
, &db
->bb_free_root
);
4422 /* Now try to see the extent can be merged to left and right */
4423 node
= rb_prev(new_node
);
4425 entry
= rb_entry(node
, struct ext4_free_data
, efd_node
);
4426 if (can_merge(entry
, new_entry
) &&
4427 ext4_journal_callback_try_del(handle
, &entry
->efd_jce
)) {
4428 new_entry
->efd_start_cluster
= entry
->efd_start_cluster
;
4429 new_entry
->efd_count
+= entry
->efd_count
;
4430 rb_erase(node
, &(db
->bb_free_root
));
4431 kmem_cache_free(ext4_free_data_cachep
, entry
);
4435 node
= rb_next(new_node
);
4437 entry
= rb_entry(node
, struct ext4_free_data
, efd_node
);
4438 if (can_merge(new_entry
, entry
) &&
4439 ext4_journal_callback_try_del(handle
, &entry
->efd_jce
)) {
4440 new_entry
->efd_count
+= entry
->efd_count
;
4441 rb_erase(node
, &(db
->bb_free_root
));
4442 kmem_cache_free(ext4_free_data_cachep
, entry
);
4445 /* Add the extent to transaction's private list */
4446 ext4_journal_callback_add(handle
, ext4_free_data_callback
,
4447 &new_entry
->efd_jce
);
4452 * ext4_free_blocks() -- Free given blocks and update quota
4453 * @handle: handle for this transaction
4455 * @block: start physical block to free
4456 * @count: number of blocks to count
4457 * @flags: flags used by ext4_free_blocks
4459 void ext4_free_blocks(handle_t
*handle
, struct inode
*inode
,
4460 struct buffer_head
*bh
, ext4_fsblk_t block
,
4461 unsigned long count
, int flags
)
4463 struct buffer_head
*bitmap_bh
= NULL
;
4464 struct super_block
*sb
= inode
->i_sb
;
4465 struct ext4_group_desc
*gdp
;
4466 unsigned int overflow
;
4468 struct buffer_head
*gd_bh
;
4469 ext4_group_t block_group
;
4470 struct ext4_sb_info
*sbi
;
4471 struct ext4_buddy e4b
;
4472 unsigned int count_clusters
;
4479 BUG_ON(block
!= bh
->b_blocknr
);
4481 block
= bh
->b_blocknr
;
4485 if (!(flags
& EXT4_FREE_BLOCKS_VALIDATED
) &&
4486 !ext4_data_block_valid(sbi
, block
, count
)) {
4487 ext4_error(sb
, "Freeing blocks not in datazone - "
4488 "block = %llu, count = %lu", block
, count
);
4492 ext4_debug("freeing block %llu\n", block
);
4493 trace_ext4_free_blocks(inode
, block
, count
, flags
);
4495 if (flags
& EXT4_FREE_BLOCKS_FORGET
) {
4496 struct buffer_head
*tbh
= bh
;
4499 BUG_ON(bh
&& (count
> 1));
4501 for (i
= 0; i
< count
; i
++) {
4503 tbh
= sb_find_get_block(inode
->i_sb
,
4507 ext4_forget(handle
, flags
& EXT4_FREE_BLOCKS_METADATA
,
4508 inode
, tbh
, block
+ i
);
4513 * We need to make sure we don't reuse the freed block until
4514 * after the transaction is committed, which we can do by
4515 * treating the block as metadata, below. We make an
4516 * exception if the inode is to be written in writeback mode
4517 * since writeback mode has weak data consistency guarantees.
4519 if (!ext4_should_writeback_data(inode
))
4520 flags
|= EXT4_FREE_BLOCKS_METADATA
;
4523 * If the extent to be freed does not begin on a cluster
4524 * boundary, we need to deal with partial clusters at the
4525 * beginning and end of the extent. Normally we will free
4526 * blocks at the beginning or the end unless we are explicitly
4527 * requested to avoid doing so.
4529 overflow
= block
& (sbi
->s_cluster_ratio
- 1);
4531 if (flags
& EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER
) {
4532 overflow
= sbi
->s_cluster_ratio
- overflow
;
4534 if (count
> overflow
)
4543 overflow
= count
& (sbi
->s_cluster_ratio
- 1);
4545 if (flags
& EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER
) {
4546 if (count
> overflow
)
4551 count
+= sbi
->s_cluster_ratio
- overflow
;
4556 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
4559 * Check to see if we are freeing blocks across a group
4562 if (EXT4_C2B(sbi
, bit
) + count
> EXT4_BLOCKS_PER_GROUP(sb
)) {
4563 overflow
= EXT4_C2B(sbi
, bit
) + count
-
4564 EXT4_BLOCKS_PER_GROUP(sb
);
4567 count_clusters
= EXT4_NUM_B2C(sbi
, count
);
4568 bitmap_bh
= ext4_read_block_bitmap(sb
, block_group
);
4573 gdp
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
4579 if (in_range(ext4_block_bitmap(sb
, gdp
), block
, count
) ||
4580 in_range(ext4_inode_bitmap(sb
, gdp
), block
, count
) ||
4581 in_range(block
, ext4_inode_table(sb
, gdp
),
4582 EXT4_SB(sb
)->s_itb_per_group
) ||
4583 in_range(block
+ count
- 1, ext4_inode_table(sb
, gdp
),
4584 EXT4_SB(sb
)->s_itb_per_group
)) {
4586 ext4_error(sb
, "Freeing blocks in system zone - "
4587 "Block = %llu, count = %lu", block
, count
);
4588 /* err = 0. ext4_std_error should be a no op */
4592 BUFFER_TRACE(bitmap_bh
, "getting write access");
4593 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
4598 * We are about to modify some metadata. Call the journal APIs
4599 * to unshare ->b_data if a currently-committing transaction is
4602 BUFFER_TRACE(gd_bh
, "get_write_access");
4603 err
= ext4_journal_get_write_access(handle
, gd_bh
);
4606 #ifdef AGGRESSIVE_CHECK
4609 for (i
= 0; i
< count_clusters
; i
++)
4610 BUG_ON(!mb_test_bit(bit
+ i
, bitmap_bh
->b_data
));
4613 trace_ext4_mballoc_free(sb
, inode
, block_group
, bit
, count_clusters
);
4615 err
= ext4_mb_load_buddy(sb
, block_group
, &e4b
);
4619 if ((flags
& EXT4_FREE_BLOCKS_METADATA
) && ext4_handle_valid(handle
)) {
4620 struct ext4_free_data
*new_entry
;
4622 * blocks being freed are metadata. these blocks shouldn't
4623 * be used until this transaction is committed
4625 new_entry
= kmem_cache_alloc(ext4_free_data_cachep
, GFP_NOFS
);
4627 ext4_mb_unload_buddy(&e4b
);
4631 new_entry
->efd_start_cluster
= bit
;
4632 new_entry
->efd_group
= block_group
;
4633 new_entry
->efd_count
= count_clusters
;
4634 new_entry
->efd_tid
= handle
->h_transaction
->t_tid
;
4636 ext4_lock_group(sb
, block_group
);
4637 mb_clear_bits(bitmap_bh
->b_data
, bit
, count_clusters
);
4638 ext4_mb_free_metadata(handle
, &e4b
, new_entry
);
4640 /* need to update group_info->bb_free and bitmap
4641 * with group lock held. generate_buddy look at
4642 * them with group lock_held
4644 if (test_opt(sb
, DISCARD
)) {
4645 err
= ext4_issue_discard(sb
, block_group
, bit
, count
);
4646 if (err
&& err
!= -EOPNOTSUPP
)
4647 ext4_msg(sb
, KERN_WARNING
, "discard request in"
4648 " group:%d block:%d count:%lu failed"
4649 " with %d", block_group
, bit
, count
,
4654 ext4_lock_group(sb
, block_group
);
4655 mb_clear_bits(bitmap_bh
->b_data
, bit
, count_clusters
);
4656 mb_free_blocks(inode
, &e4b
, bit
, count_clusters
);
4659 ret
= ext4_free_group_clusters(sb
, gdp
) + count_clusters
;
4660 ext4_free_group_clusters_set(sb
, gdp
, ret
);
4661 ext4_block_bitmap_csum_set(sb
, block_group
, gdp
, bitmap_bh
);
4662 ext4_group_desc_csum_set(sb
, block_group
, gdp
);
4663 ext4_unlock_group(sb
, block_group
);
4664 percpu_counter_add(&sbi
->s_freeclusters_counter
, count_clusters
);
4666 if (sbi
->s_log_groups_per_flex
) {
4667 ext4_group_t flex_group
= ext4_flex_group(sbi
, block_group
);
4668 atomic64_add(count_clusters
,
4669 &sbi
->s_flex_groups
[flex_group
].free_clusters
);
4672 ext4_mb_unload_buddy(&e4b
);
4674 if (!(flags
& EXT4_FREE_BLOCKS_NO_QUOT_UPDATE
))
4675 dquot_free_block(inode
, EXT4_C2B(sbi
, count_clusters
));
4677 /* We dirtied the bitmap block */
4678 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
4679 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
4681 /* And the group descriptor block */
4682 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
4683 ret
= ext4_handle_dirty_metadata(handle
, NULL
, gd_bh
);
4687 if (overflow
&& !err
) {
4695 ext4_std_error(sb
, err
);
4700 * ext4_group_add_blocks() -- Add given blocks to an existing group
4701 * @handle: handle to this transaction
4703 * @block: start physical block to add to the block group
4704 * @count: number of blocks to free
4706 * This marks the blocks as free in the bitmap and buddy.
4708 int ext4_group_add_blocks(handle_t
*handle
, struct super_block
*sb
,
4709 ext4_fsblk_t block
, unsigned long count
)
4711 struct buffer_head
*bitmap_bh
= NULL
;
4712 struct buffer_head
*gd_bh
;
4713 ext4_group_t block_group
;
4716 struct ext4_group_desc
*desc
;
4717 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4718 struct ext4_buddy e4b
;
4719 int err
= 0, ret
, blk_free_count
;
4720 ext4_grpblk_t blocks_freed
;
4722 ext4_debug("Adding block(s) %llu-%llu\n", block
, block
+ count
- 1);
4727 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
4729 * Check to see if we are freeing blocks across a group
4732 if (bit
+ count
> EXT4_BLOCKS_PER_GROUP(sb
)) {
4733 ext4_warning(sb
, "too much blocks added to group %u\n",
4739 bitmap_bh
= ext4_read_block_bitmap(sb
, block_group
);
4745 desc
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
4751 if (in_range(ext4_block_bitmap(sb
, desc
), block
, count
) ||
4752 in_range(ext4_inode_bitmap(sb
, desc
), block
, count
) ||
4753 in_range(block
, ext4_inode_table(sb
, desc
), sbi
->s_itb_per_group
) ||
4754 in_range(block
+ count
- 1, ext4_inode_table(sb
, desc
),
4755 sbi
->s_itb_per_group
)) {
4756 ext4_error(sb
, "Adding blocks in system zones - "
4757 "Block = %llu, count = %lu",
4763 BUFFER_TRACE(bitmap_bh
, "getting write access");
4764 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
4769 * We are about to modify some metadata. Call the journal APIs
4770 * to unshare ->b_data if a currently-committing transaction is
4773 BUFFER_TRACE(gd_bh
, "get_write_access");
4774 err
= ext4_journal_get_write_access(handle
, gd_bh
);
4778 for (i
= 0, blocks_freed
= 0; i
< count
; i
++) {
4779 BUFFER_TRACE(bitmap_bh
, "clear bit");
4780 if (!mb_test_bit(bit
+ i
, bitmap_bh
->b_data
)) {
4781 ext4_error(sb
, "bit already cleared for block %llu",
4782 (ext4_fsblk_t
)(block
+ i
));
4783 BUFFER_TRACE(bitmap_bh
, "bit already cleared");
4789 err
= ext4_mb_load_buddy(sb
, block_group
, &e4b
);
4794 * need to update group_info->bb_free and bitmap
4795 * with group lock held. generate_buddy look at
4796 * them with group lock_held
4798 ext4_lock_group(sb
, block_group
);
4799 mb_clear_bits(bitmap_bh
->b_data
, bit
, count
);
4800 mb_free_blocks(NULL
, &e4b
, bit
, count
);
4801 blk_free_count
= blocks_freed
+ ext4_free_group_clusters(sb
, desc
);
4802 ext4_free_group_clusters_set(sb
, desc
, blk_free_count
);
4803 ext4_block_bitmap_csum_set(sb
, block_group
, desc
, bitmap_bh
);
4804 ext4_group_desc_csum_set(sb
, block_group
, desc
);
4805 ext4_unlock_group(sb
, block_group
);
4806 percpu_counter_add(&sbi
->s_freeclusters_counter
,
4807 EXT4_NUM_B2C(sbi
, blocks_freed
));
4809 if (sbi
->s_log_groups_per_flex
) {
4810 ext4_group_t flex_group
= ext4_flex_group(sbi
, block_group
);
4811 atomic64_add(EXT4_NUM_B2C(sbi
, blocks_freed
),
4812 &sbi
->s_flex_groups
[flex_group
].free_clusters
);
4815 ext4_mb_unload_buddy(&e4b
);
4817 /* We dirtied the bitmap block */
4818 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
4819 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
4821 /* And the group descriptor block */
4822 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
4823 ret
= ext4_handle_dirty_metadata(handle
, NULL
, gd_bh
);
4829 ext4_std_error(sb
, err
);
4834 * ext4_trim_extent -- function to TRIM one single free extent in the group
4835 * @sb: super block for the file system
4836 * @start: starting block of the free extent in the alloc. group
4837 * @count: number of blocks to TRIM
4838 * @group: alloc. group we are working with
4839 * @e4b: ext4 buddy for the group
4841 * Trim "count" blocks starting at "start" in the "group". To assure that no
4842 * one will allocate those blocks, mark it as used in buddy bitmap. This must
4843 * be called with under the group lock.
4845 static int ext4_trim_extent(struct super_block
*sb
, int start
, int count
,
4846 ext4_group_t group
, struct ext4_buddy
*e4b
)
4848 struct ext4_free_extent ex
;
4851 trace_ext4_trim_extent(sb
, group
, start
, count
);
4853 assert_spin_locked(ext4_group_lock_ptr(sb
, group
));
4855 ex
.fe_start
= start
;
4856 ex
.fe_group
= group
;
4860 * Mark blocks used, so no one can reuse them while
4863 mb_mark_used(e4b
, &ex
);
4864 ext4_unlock_group(sb
, group
);
4865 ret
= ext4_issue_discard(sb
, group
, start
, count
);
4866 ext4_lock_group(sb
, group
);
4867 mb_free_blocks(NULL
, e4b
, start
, ex
.fe_len
);
4872 * ext4_trim_all_free -- function to trim all free space in alloc. group
4873 * @sb: super block for file system
4874 * @group: group to be trimmed
4875 * @start: first group block to examine
4876 * @max: last group block to examine
4877 * @minblocks: minimum extent block count
4879 * ext4_trim_all_free walks through group's buddy bitmap searching for free
4880 * extents. When the free block is found, ext4_trim_extent is called to TRIM
4884 * ext4_trim_all_free walks through group's block bitmap searching for free
4885 * extents. When the free extent is found, mark it as used in group buddy
4886 * bitmap. Then issue a TRIM command on this extent and free the extent in
4887 * the group buddy bitmap. This is done until whole group is scanned.
4889 static ext4_grpblk_t
4890 ext4_trim_all_free(struct super_block
*sb
, ext4_group_t group
,
4891 ext4_grpblk_t start
, ext4_grpblk_t max
,
4892 ext4_grpblk_t minblocks
)
4895 ext4_grpblk_t next
, count
= 0, free_count
= 0;
4896 struct ext4_buddy e4b
;
4899 trace_ext4_trim_all_free(sb
, group
, start
, max
);
4901 ret
= ext4_mb_load_buddy(sb
, group
, &e4b
);
4903 ext4_error(sb
, "Error in loading buddy "
4904 "information for %u", group
);
4907 bitmap
= e4b
.bd_bitmap
;
4909 ext4_lock_group(sb
, group
);
4910 if (EXT4_MB_GRP_WAS_TRIMMED(e4b
.bd_info
) &&
4911 minblocks
>= atomic_read(&EXT4_SB(sb
)->s_last_trim_minblks
))
4914 start
= (e4b
.bd_info
->bb_first_free
> start
) ?
4915 e4b
.bd_info
->bb_first_free
: start
;
4917 while (start
<= max
) {
4918 start
= mb_find_next_zero_bit(bitmap
, max
+ 1, start
);
4921 next
= mb_find_next_bit(bitmap
, max
+ 1, start
);
4923 if ((next
- start
) >= minblocks
) {
4924 ret
= ext4_trim_extent(sb
, start
,
4925 next
- start
, group
, &e4b
);
4926 if (ret
&& ret
!= -EOPNOTSUPP
)
4929 count
+= next
- start
;
4931 free_count
+= next
- start
;
4934 if (fatal_signal_pending(current
)) {
4935 count
= -ERESTARTSYS
;
4939 if (need_resched()) {
4940 ext4_unlock_group(sb
, group
);
4942 ext4_lock_group(sb
, group
);
4945 if ((e4b
.bd_info
->bb_free
- free_count
) < minblocks
)
4951 EXT4_MB_GRP_SET_TRIMMED(e4b
.bd_info
);
4954 ext4_unlock_group(sb
, group
);
4955 ext4_mb_unload_buddy(&e4b
);
4957 ext4_debug("trimmed %d blocks in the group %d\n",
4964 * ext4_trim_fs() -- trim ioctl handle function
4965 * @sb: superblock for filesystem
4966 * @range: fstrim_range structure
4968 * start: First Byte to trim
4969 * len: number of Bytes to trim from start
4970 * minlen: minimum extent length in Bytes
4971 * ext4_trim_fs goes through all allocation groups containing Bytes from
4972 * start to start+len. For each such a group ext4_trim_all_free function
4973 * is invoked to trim all free space.
4975 int ext4_trim_fs(struct super_block
*sb
, struct fstrim_range
*range
)
4977 struct ext4_group_info
*grp
;
4978 ext4_group_t group
, first_group
, last_group
;
4979 ext4_grpblk_t cnt
= 0, first_cluster
, last_cluster
;
4980 uint64_t start
, end
, minlen
, trimmed
= 0;
4981 ext4_fsblk_t first_data_blk
=
4982 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
4983 ext4_fsblk_t max_blks
= ext4_blocks_count(EXT4_SB(sb
)->s_es
);
4986 start
= range
->start
>> sb
->s_blocksize_bits
;
4987 end
= start
+ (range
->len
>> sb
->s_blocksize_bits
) - 1;
4988 minlen
= EXT4_NUM_B2C(EXT4_SB(sb
),
4989 range
->minlen
>> sb
->s_blocksize_bits
);
4991 if (minlen
> EXT4_CLUSTERS_PER_GROUP(sb
) ||
4992 start
>= max_blks
||
4993 range
->len
< sb
->s_blocksize
)
4995 if (end
>= max_blks
)
4997 if (end
<= first_data_blk
)
4999 if (start
< first_data_blk
)
5000 start
= first_data_blk
;
5002 /* Determine first and last group to examine based on start and end */
5003 ext4_get_group_no_and_offset(sb
, (ext4_fsblk_t
) start
,
5004 &first_group
, &first_cluster
);
5005 ext4_get_group_no_and_offset(sb
, (ext4_fsblk_t
) end
,
5006 &last_group
, &last_cluster
);
5008 /* end now represents the last cluster to discard in this group */
5009 end
= EXT4_CLUSTERS_PER_GROUP(sb
) - 1;
5011 for (group
= first_group
; group
<= last_group
; group
++) {
5012 grp
= ext4_get_group_info(sb
, group
);
5013 /* We only do this if the grp has never been initialized */
5014 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp
))) {
5015 ret
= ext4_mb_init_group(sb
, group
);
5021 * For all the groups except the last one, last cluster will
5022 * always be EXT4_CLUSTERS_PER_GROUP(sb)-1, so we only need to
5023 * change it for the last group, note that last_cluster is
5024 * already computed earlier by ext4_get_group_no_and_offset()
5026 if (group
== last_group
)
5029 if (grp
->bb_free
>= minlen
) {
5030 cnt
= ext4_trim_all_free(sb
, group
, first_cluster
,
5040 * For every group except the first one, we are sure
5041 * that the first cluster to discard will be cluster #0.
5047 atomic_set(&EXT4_SB(sb
)->s_last_trim_minblks
, minlen
);
5050 range
->len
= EXT4_C2B(EXT4_SB(sb
), trimmed
) << sb
->s_blocksize_bits
;