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
;
1014 mb_debug(1, "init group %u\n", group
);
1015 this_grp
= ext4_get_group_info(sb
, group
);
1017 * This ensures that we don't reinit the buddy cache
1018 * page which map to the group from which we are already
1019 * allocating. If we are looking at the buddy cache we would
1020 * have taken a reference using ext4_mb_load_buddy and that
1021 * would have pinned buddy page to page cache.
1023 ret
= ext4_mb_get_buddy_page_lock(sb
, group
, &e4b
);
1024 if (ret
|| !EXT4_MB_GRP_NEED_INIT(this_grp
)) {
1026 * somebody initialized the group
1027 * return without doing anything
1032 page
= e4b
.bd_bitmap_page
;
1033 ret
= ext4_mb_init_cache(page
, NULL
);
1036 if (!PageUptodate(page
)) {
1040 mark_page_accessed(page
);
1042 if (e4b
.bd_buddy_page
== NULL
) {
1044 * If both the bitmap and buddy are in
1045 * the same page we don't need to force
1051 /* init buddy cache */
1052 page
= e4b
.bd_buddy_page
;
1053 ret
= ext4_mb_init_cache(page
, e4b
.bd_bitmap
);
1056 if (!PageUptodate(page
)) {
1060 mark_page_accessed(page
);
1062 ext4_mb_put_buddy_page_lock(&e4b
);
1067 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1068 * block group lock of all groups for this page; do not hold the BG lock when
1069 * calling this routine!
1071 static noinline_for_stack
int
1072 ext4_mb_load_buddy(struct super_block
*sb
, ext4_group_t group
,
1073 struct ext4_buddy
*e4b
)
1075 int blocks_per_page
;
1081 struct ext4_group_info
*grp
;
1082 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1083 struct inode
*inode
= sbi
->s_buddy_cache
;
1085 mb_debug(1, "load group %u\n", group
);
1087 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1088 grp
= ext4_get_group_info(sb
, group
);
1090 e4b
->bd_blkbits
= sb
->s_blocksize_bits
;
1093 e4b
->bd_group
= group
;
1094 e4b
->bd_buddy_page
= NULL
;
1095 e4b
->bd_bitmap_page
= NULL
;
1097 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp
))) {
1099 * we need full data about the group
1100 * to make a good selection
1102 ret
= ext4_mb_init_group(sb
, group
);
1108 * the buddy cache inode stores the block bitmap
1109 * and buddy information in consecutive blocks.
1110 * So for each group we need two blocks.
1113 pnum
= block
/ blocks_per_page
;
1114 poff
= block
% blocks_per_page
;
1116 /* we could use find_or_create_page(), but it locks page
1117 * what we'd like to avoid in fast path ... */
1118 page
= find_get_page(inode
->i_mapping
, pnum
);
1119 if (page
== NULL
|| !PageUptodate(page
)) {
1122 * drop the page reference and try
1123 * to get the page with lock. If we
1124 * are not uptodate that implies
1125 * somebody just created the page but
1126 * is yet to initialize the same. So
1127 * wait for it to initialize.
1129 page_cache_release(page
);
1130 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1132 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1133 if (!PageUptodate(page
)) {
1134 ret
= ext4_mb_init_cache(page
, NULL
);
1139 mb_cmp_bitmaps(e4b
, page_address(page
) +
1140 (poff
* sb
->s_blocksize
));
1145 if (page
== NULL
|| !PageUptodate(page
)) {
1149 e4b
->bd_bitmap_page
= page
;
1150 e4b
->bd_bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
1151 mark_page_accessed(page
);
1154 pnum
= block
/ blocks_per_page
;
1155 poff
= block
% blocks_per_page
;
1157 page
= find_get_page(inode
->i_mapping
, pnum
);
1158 if (page
== NULL
|| !PageUptodate(page
)) {
1160 page_cache_release(page
);
1161 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1163 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1164 if (!PageUptodate(page
)) {
1165 ret
= ext4_mb_init_cache(page
, e4b
->bd_bitmap
);
1174 if (page
== NULL
|| !PageUptodate(page
)) {
1178 e4b
->bd_buddy_page
= page
;
1179 e4b
->bd_buddy
= page_address(page
) + (poff
* sb
->s_blocksize
);
1180 mark_page_accessed(page
);
1182 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
1183 BUG_ON(e4b
->bd_buddy_page
== NULL
);
1189 page_cache_release(page
);
1190 if (e4b
->bd_bitmap_page
)
1191 page_cache_release(e4b
->bd_bitmap_page
);
1192 if (e4b
->bd_buddy_page
)
1193 page_cache_release(e4b
->bd_buddy_page
);
1194 e4b
->bd_buddy
= NULL
;
1195 e4b
->bd_bitmap
= NULL
;
1199 static void ext4_mb_unload_buddy(struct ext4_buddy
*e4b
)
1201 if (e4b
->bd_bitmap_page
)
1202 page_cache_release(e4b
->bd_bitmap_page
);
1203 if (e4b
->bd_buddy_page
)
1204 page_cache_release(e4b
->bd_buddy_page
);
1208 static int mb_find_order_for_block(struct ext4_buddy
*e4b
, int block
)
1213 BUG_ON(e4b
->bd_bitmap
== e4b
->bd_buddy
);
1214 BUG_ON(block
>= (1 << (e4b
->bd_blkbits
+ 3)));
1217 while (order
<= e4b
->bd_blkbits
+ 1) {
1219 if (!mb_test_bit(block
, bb
)) {
1220 /* this block is part of buddy of order 'order' */
1223 bb
+= 1 << (e4b
->bd_blkbits
- order
);
1229 static void mb_clear_bits(void *bm
, int cur
, int len
)
1235 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1236 /* fast path: clear whole word at once */
1237 addr
= bm
+ (cur
>> 3);
1242 mb_clear_bit(cur
, bm
);
1247 void ext4_set_bits(void *bm
, int cur
, int len
)
1253 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1254 /* fast path: set whole word at once */
1255 addr
= bm
+ (cur
>> 3);
1260 mb_set_bit(cur
, bm
);
1265 static void mb_free_blocks(struct inode
*inode
, struct ext4_buddy
*e4b
,
1266 int first
, int count
)
1273 struct super_block
*sb
= e4b
->bd_sb
;
1275 BUG_ON(first
+ count
> (sb
->s_blocksize
<< 3));
1276 assert_spin_locked(ext4_group_lock_ptr(sb
, e4b
->bd_group
));
1277 mb_check_buddy(e4b
);
1278 mb_free_blocks_double(inode
, e4b
, first
, count
);
1280 e4b
->bd_info
->bb_free
+= count
;
1281 if (first
< e4b
->bd_info
->bb_first_free
)
1282 e4b
->bd_info
->bb_first_free
= first
;
1284 /* let's maintain fragments counter */
1286 block
= !mb_test_bit(first
- 1, e4b
->bd_bitmap
);
1287 if (first
+ count
< EXT4_SB(sb
)->s_mb_maxs
[0])
1288 max
= !mb_test_bit(first
+ count
, e4b
->bd_bitmap
);
1290 e4b
->bd_info
->bb_fragments
--;
1291 else if (!block
&& !max
)
1292 e4b
->bd_info
->bb_fragments
++;
1294 /* let's maintain buddy itself */
1295 while (count
-- > 0) {
1299 if (!mb_test_bit(block
, e4b
->bd_bitmap
)) {
1300 ext4_fsblk_t blocknr
;
1302 blocknr
= ext4_group_first_block_no(sb
, e4b
->bd_group
);
1303 blocknr
+= EXT4_C2B(EXT4_SB(sb
), block
);
1304 ext4_grp_locked_error(sb
, e4b
->bd_group
,
1305 inode
? inode
->i_ino
: 0,
1307 "freeing already freed block "
1310 mb_clear_bit(block
, e4b
->bd_bitmap
);
1311 e4b
->bd_info
->bb_counters
[order
]++;
1313 /* start of the buddy */
1314 buddy
= mb_find_buddy(e4b
, order
, &max
);
1318 if (mb_test_bit(block
, buddy
) ||
1319 mb_test_bit(block
+ 1, buddy
))
1322 /* both the buddies are free, try to coalesce them */
1323 buddy2
= mb_find_buddy(e4b
, order
+ 1, &max
);
1329 /* for special purposes, we don't set
1330 * free bits in bitmap */
1331 mb_set_bit(block
, buddy
);
1332 mb_set_bit(block
+ 1, buddy
);
1334 e4b
->bd_info
->bb_counters
[order
]--;
1335 e4b
->bd_info
->bb_counters
[order
]--;
1339 e4b
->bd_info
->bb_counters
[order
]++;
1341 mb_clear_bit(block
, buddy2
);
1345 mb_set_largest_free_order(sb
, e4b
->bd_info
);
1346 mb_check_buddy(e4b
);
1349 static int mb_find_extent(struct ext4_buddy
*e4b
, int block
,
1350 int needed
, struct ext4_free_extent
*ex
)
1356 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
1359 buddy
= mb_find_buddy(e4b
, 0, &max
);
1360 BUG_ON(buddy
== NULL
);
1361 BUG_ON(block
>= max
);
1362 if (mb_test_bit(block
, buddy
)) {
1369 /* find actual order */
1370 order
= mb_find_order_for_block(e4b
, block
);
1371 block
= block
>> order
;
1373 ex
->fe_len
= 1 << order
;
1374 ex
->fe_start
= block
<< order
;
1375 ex
->fe_group
= e4b
->bd_group
;
1377 /* calc difference from given start */
1378 next
= next
- ex
->fe_start
;
1380 ex
->fe_start
+= next
;
1382 while (needed
> ex
->fe_len
&&
1383 mb_find_buddy(e4b
, order
, &max
)) {
1385 if (block
+ 1 >= max
)
1388 next
= (block
+ 1) * (1 << order
);
1389 if (mb_test_bit(next
, e4b
->bd_bitmap
))
1392 order
= mb_find_order_for_block(e4b
, next
);
1394 block
= next
>> order
;
1395 ex
->fe_len
+= 1 << order
;
1398 BUG_ON(ex
->fe_start
+ ex
->fe_len
> (1 << (e4b
->bd_blkbits
+ 3)));
1402 static int mb_mark_used(struct ext4_buddy
*e4b
, struct ext4_free_extent
*ex
)
1408 int start
= ex
->fe_start
;
1409 int len
= ex
->fe_len
;
1414 BUG_ON(start
+ len
> (e4b
->bd_sb
->s_blocksize
<< 3));
1415 BUG_ON(e4b
->bd_group
!= ex
->fe_group
);
1416 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
1417 mb_check_buddy(e4b
);
1418 mb_mark_used_double(e4b
, start
, len
);
1420 e4b
->bd_info
->bb_free
-= len
;
1421 if (e4b
->bd_info
->bb_first_free
== start
)
1422 e4b
->bd_info
->bb_first_free
+= len
;
1424 /* let's maintain fragments counter */
1426 mlen
= !mb_test_bit(start
- 1, e4b
->bd_bitmap
);
1427 if (start
+ len
< EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[0])
1428 max
= !mb_test_bit(start
+ len
, e4b
->bd_bitmap
);
1430 e4b
->bd_info
->bb_fragments
++;
1431 else if (!mlen
&& !max
)
1432 e4b
->bd_info
->bb_fragments
--;
1434 /* let's maintain buddy itself */
1436 ord
= mb_find_order_for_block(e4b
, start
);
1438 if (((start
>> ord
) << ord
) == start
&& len
>= (1 << ord
)) {
1439 /* the whole chunk may be allocated at once! */
1441 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1442 BUG_ON((start
>> ord
) >= max
);
1443 mb_set_bit(start
>> ord
, buddy
);
1444 e4b
->bd_info
->bb_counters
[ord
]--;
1451 /* store for history */
1453 ret
= len
| (ord
<< 16);
1455 /* we have to split large buddy */
1457 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1458 mb_set_bit(start
>> ord
, buddy
);
1459 e4b
->bd_info
->bb_counters
[ord
]--;
1462 cur
= (start
>> ord
) & ~1U;
1463 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1464 mb_clear_bit(cur
, buddy
);
1465 mb_clear_bit(cur
+ 1, buddy
);
1466 e4b
->bd_info
->bb_counters
[ord
]++;
1467 e4b
->bd_info
->bb_counters
[ord
]++;
1469 mb_set_largest_free_order(e4b
->bd_sb
, e4b
->bd_info
);
1471 ext4_set_bits(e4b
->bd_bitmap
, ex
->fe_start
, len0
);
1472 mb_check_buddy(e4b
);
1478 * Must be called under group lock!
1480 static void ext4_mb_use_best_found(struct ext4_allocation_context
*ac
,
1481 struct ext4_buddy
*e4b
)
1483 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1486 BUG_ON(ac
->ac_b_ex
.fe_group
!= e4b
->bd_group
);
1487 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1489 ac
->ac_b_ex
.fe_len
= min(ac
->ac_b_ex
.fe_len
, ac
->ac_g_ex
.fe_len
);
1490 ac
->ac_b_ex
.fe_logical
= ac
->ac_g_ex
.fe_logical
;
1491 ret
= mb_mark_used(e4b
, &ac
->ac_b_ex
);
1493 /* preallocation can change ac_b_ex, thus we store actually
1494 * allocated blocks for history */
1495 ac
->ac_f_ex
= ac
->ac_b_ex
;
1497 ac
->ac_status
= AC_STATUS_FOUND
;
1498 ac
->ac_tail
= ret
& 0xffff;
1499 ac
->ac_buddy
= ret
>> 16;
1502 * take the page reference. We want the page to be pinned
1503 * so that we don't get a ext4_mb_init_cache_call for this
1504 * group until we update the bitmap. That would mean we
1505 * double allocate blocks. The reference is dropped
1506 * in ext4_mb_release_context
1508 ac
->ac_bitmap_page
= e4b
->bd_bitmap_page
;
1509 get_page(ac
->ac_bitmap_page
);
1510 ac
->ac_buddy_page
= e4b
->bd_buddy_page
;
1511 get_page(ac
->ac_buddy_page
);
1512 /* store last allocated for subsequent stream allocation */
1513 if (ac
->ac_flags
& EXT4_MB_STREAM_ALLOC
) {
1514 spin_lock(&sbi
->s_md_lock
);
1515 sbi
->s_mb_last_group
= ac
->ac_f_ex
.fe_group
;
1516 sbi
->s_mb_last_start
= ac
->ac_f_ex
.fe_start
;
1517 spin_unlock(&sbi
->s_md_lock
);
1522 * regular allocator, for general purposes allocation
1525 static void ext4_mb_check_limits(struct ext4_allocation_context
*ac
,
1526 struct ext4_buddy
*e4b
,
1529 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1530 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1531 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1532 struct ext4_free_extent ex
;
1535 if (ac
->ac_status
== AC_STATUS_FOUND
)
1538 * We don't want to scan for a whole year
1540 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
&&
1541 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1542 ac
->ac_status
= AC_STATUS_BREAK
;
1547 * Haven't found good chunk so far, let's continue
1549 if (bex
->fe_len
< gex
->fe_len
)
1552 if ((finish_group
|| ac
->ac_found
> sbi
->s_mb_min_to_scan
)
1553 && bex
->fe_group
== e4b
->bd_group
) {
1554 /* recheck chunk's availability - we don't know
1555 * when it was found (within this lock-unlock
1557 max
= mb_find_extent(e4b
, bex
->fe_start
, gex
->fe_len
, &ex
);
1558 if (max
>= gex
->fe_len
) {
1559 ext4_mb_use_best_found(ac
, e4b
);
1566 * The routine checks whether found extent is good enough. If it is,
1567 * then the extent gets marked used and flag is set to the context
1568 * to stop scanning. Otherwise, the extent is compared with the
1569 * previous found extent and if new one is better, then it's stored
1570 * in the context. Later, the best found extent will be used, if
1571 * mballoc can't find good enough extent.
1573 * FIXME: real allocation policy is to be designed yet!
1575 static void ext4_mb_measure_extent(struct ext4_allocation_context
*ac
,
1576 struct ext4_free_extent
*ex
,
1577 struct ext4_buddy
*e4b
)
1579 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1580 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1582 BUG_ON(ex
->fe_len
<= 0);
1583 BUG_ON(ex
->fe_len
> EXT4_CLUSTERS_PER_GROUP(ac
->ac_sb
));
1584 BUG_ON(ex
->fe_start
>= EXT4_CLUSTERS_PER_GROUP(ac
->ac_sb
));
1585 BUG_ON(ac
->ac_status
!= AC_STATUS_CONTINUE
);
1590 * The special case - take what you catch first
1592 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1594 ext4_mb_use_best_found(ac
, e4b
);
1599 * Let's check whether the chuck is good enough
1601 if (ex
->fe_len
== gex
->fe_len
) {
1603 ext4_mb_use_best_found(ac
, e4b
);
1608 * If this is first found extent, just store it in the context
1610 if (bex
->fe_len
== 0) {
1616 * If new found extent is better, store it in the context
1618 if (bex
->fe_len
< gex
->fe_len
) {
1619 /* if the request isn't satisfied, any found extent
1620 * larger than previous best one is better */
1621 if (ex
->fe_len
> bex
->fe_len
)
1623 } else if (ex
->fe_len
> gex
->fe_len
) {
1624 /* if the request is satisfied, then we try to find
1625 * an extent that still satisfy the request, but is
1626 * smaller than previous one */
1627 if (ex
->fe_len
< bex
->fe_len
)
1631 ext4_mb_check_limits(ac
, e4b
, 0);
1634 static noinline_for_stack
1635 int ext4_mb_try_best_found(struct ext4_allocation_context
*ac
,
1636 struct ext4_buddy
*e4b
)
1638 struct ext4_free_extent ex
= ac
->ac_b_ex
;
1639 ext4_group_t group
= ex
.fe_group
;
1643 BUG_ON(ex
.fe_len
<= 0);
1644 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1648 ext4_lock_group(ac
->ac_sb
, group
);
1649 max
= mb_find_extent(e4b
, ex
.fe_start
, ex
.fe_len
, &ex
);
1653 ext4_mb_use_best_found(ac
, e4b
);
1656 ext4_unlock_group(ac
->ac_sb
, group
);
1657 ext4_mb_unload_buddy(e4b
);
1662 static noinline_for_stack
1663 int ext4_mb_find_by_goal(struct ext4_allocation_context
*ac
,
1664 struct ext4_buddy
*e4b
)
1666 ext4_group_t group
= ac
->ac_g_ex
.fe_group
;
1669 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1670 struct ext4_group_info
*grp
= ext4_get_group_info(ac
->ac_sb
, group
);
1671 struct ext4_free_extent ex
;
1673 if (!(ac
->ac_flags
& EXT4_MB_HINT_TRY_GOAL
))
1675 if (grp
->bb_free
== 0)
1678 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1682 ext4_lock_group(ac
->ac_sb
, group
);
1683 max
= mb_find_extent(e4b
, ac
->ac_g_ex
.fe_start
,
1684 ac
->ac_g_ex
.fe_len
, &ex
);
1686 if (max
>= ac
->ac_g_ex
.fe_len
&& ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
) {
1689 start
= ext4_group_first_block_no(ac
->ac_sb
, e4b
->bd_group
) +
1691 /* use do_div to get remainder (would be 64-bit modulo) */
1692 if (do_div(start
, sbi
->s_stripe
) == 0) {
1695 ext4_mb_use_best_found(ac
, e4b
);
1697 } else if (max
>= ac
->ac_g_ex
.fe_len
) {
1698 BUG_ON(ex
.fe_len
<= 0);
1699 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1700 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1703 ext4_mb_use_best_found(ac
, e4b
);
1704 } else if (max
> 0 && (ac
->ac_flags
& EXT4_MB_HINT_MERGE
)) {
1705 /* Sometimes, caller may want to merge even small
1706 * number of blocks to an existing extent */
1707 BUG_ON(ex
.fe_len
<= 0);
1708 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1709 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1712 ext4_mb_use_best_found(ac
, e4b
);
1714 ext4_unlock_group(ac
->ac_sb
, group
);
1715 ext4_mb_unload_buddy(e4b
);
1721 * The routine scans buddy structures (not bitmap!) from given order
1722 * to max order and tries to find big enough chunk to satisfy the req
1724 static noinline_for_stack
1725 void ext4_mb_simple_scan_group(struct ext4_allocation_context
*ac
,
1726 struct ext4_buddy
*e4b
)
1728 struct super_block
*sb
= ac
->ac_sb
;
1729 struct ext4_group_info
*grp
= e4b
->bd_info
;
1735 BUG_ON(ac
->ac_2order
<= 0);
1736 for (i
= ac
->ac_2order
; i
<= sb
->s_blocksize_bits
+ 1; i
++) {
1737 if (grp
->bb_counters
[i
] == 0)
1740 buddy
= mb_find_buddy(e4b
, i
, &max
);
1741 BUG_ON(buddy
== NULL
);
1743 k
= mb_find_next_zero_bit(buddy
, max
, 0);
1748 ac
->ac_b_ex
.fe_len
= 1 << i
;
1749 ac
->ac_b_ex
.fe_start
= k
<< i
;
1750 ac
->ac_b_ex
.fe_group
= e4b
->bd_group
;
1752 ext4_mb_use_best_found(ac
, e4b
);
1754 BUG_ON(ac
->ac_b_ex
.fe_len
!= ac
->ac_g_ex
.fe_len
);
1756 if (EXT4_SB(sb
)->s_mb_stats
)
1757 atomic_inc(&EXT4_SB(sb
)->s_bal_2orders
);
1764 * The routine scans the group and measures all found extents.
1765 * In order to optimize scanning, caller must pass number of
1766 * free blocks in the group, so the routine can know upper limit.
1768 static noinline_for_stack
1769 void ext4_mb_complex_scan_group(struct ext4_allocation_context
*ac
,
1770 struct ext4_buddy
*e4b
)
1772 struct super_block
*sb
= ac
->ac_sb
;
1773 void *bitmap
= e4b
->bd_bitmap
;
1774 struct ext4_free_extent ex
;
1778 free
= e4b
->bd_info
->bb_free
;
1781 i
= e4b
->bd_info
->bb_first_free
;
1783 while (free
&& ac
->ac_status
== AC_STATUS_CONTINUE
) {
1784 i
= mb_find_next_zero_bit(bitmap
,
1785 EXT4_CLUSTERS_PER_GROUP(sb
), i
);
1786 if (i
>= EXT4_CLUSTERS_PER_GROUP(sb
)) {
1788 * IF we have corrupt bitmap, we won't find any
1789 * free blocks even though group info says we
1790 * we have free blocks
1792 ext4_grp_locked_error(sb
, e4b
->bd_group
, 0, 0,
1793 "%d free clusters as per "
1794 "group info. But bitmap says 0",
1799 mb_find_extent(e4b
, i
, ac
->ac_g_ex
.fe_len
, &ex
);
1800 BUG_ON(ex
.fe_len
<= 0);
1801 if (free
< ex
.fe_len
) {
1802 ext4_grp_locked_error(sb
, e4b
->bd_group
, 0, 0,
1803 "%d free clusters as per "
1804 "group info. But got %d blocks",
1807 * The number of free blocks differs. This mostly
1808 * indicate that the bitmap is corrupt. So exit
1809 * without claiming the space.
1814 ext4_mb_measure_extent(ac
, &ex
, e4b
);
1820 ext4_mb_check_limits(ac
, e4b
, 1);
1824 * This is a special case for storages like raid5
1825 * we try to find stripe-aligned chunks for stripe-size-multiple requests
1827 static noinline_for_stack
1828 void ext4_mb_scan_aligned(struct ext4_allocation_context
*ac
,
1829 struct ext4_buddy
*e4b
)
1831 struct super_block
*sb
= ac
->ac_sb
;
1832 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1833 void *bitmap
= e4b
->bd_bitmap
;
1834 struct ext4_free_extent ex
;
1835 ext4_fsblk_t first_group_block
;
1840 BUG_ON(sbi
->s_stripe
== 0);
1842 /* find first stripe-aligned block in group */
1843 first_group_block
= ext4_group_first_block_no(sb
, e4b
->bd_group
);
1845 a
= first_group_block
+ sbi
->s_stripe
- 1;
1846 do_div(a
, sbi
->s_stripe
);
1847 i
= (a
* sbi
->s_stripe
) - first_group_block
;
1849 while (i
< EXT4_CLUSTERS_PER_GROUP(sb
)) {
1850 if (!mb_test_bit(i
, bitmap
)) {
1851 max
= mb_find_extent(e4b
, i
, sbi
->s_stripe
, &ex
);
1852 if (max
>= sbi
->s_stripe
) {
1855 ext4_mb_use_best_found(ac
, e4b
);
1863 /* This is now called BEFORE we load the buddy bitmap. */
1864 static int ext4_mb_good_group(struct ext4_allocation_context
*ac
,
1865 ext4_group_t group
, int cr
)
1867 unsigned free
, fragments
;
1868 int flex_size
= ext4_flex_bg_size(EXT4_SB(ac
->ac_sb
));
1869 struct ext4_group_info
*grp
= ext4_get_group_info(ac
->ac_sb
, group
);
1871 BUG_ON(cr
< 0 || cr
>= 4);
1873 free
= grp
->bb_free
;
1876 if (cr
<= 2 && free
< ac
->ac_g_ex
.fe_len
)
1879 /* We only do this if the grp has never been initialized */
1880 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp
))) {
1881 int ret
= ext4_mb_init_group(ac
->ac_sb
, group
);
1886 fragments
= grp
->bb_fragments
;
1892 BUG_ON(ac
->ac_2order
== 0);
1894 /* Avoid using the first bg of a flexgroup for data files */
1895 if ((ac
->ac_flags
& EXT4_MB_HINT_DATA
) &&
1896 (flex_size
>= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
) &&
1897 ((group
% flex_size
) == 0))
1900 if ((ac
->ac_2order
> ac
->ac_sb
->s_blocksize_bits
+1) ||
1901 (free
/ fragments
) >= ac
->ac_g_ex
.fe_len
)
1904 if (grp
->bb_largest_free_order
< ac
->ac_2order
)
1909 if ((free
/ fragments
) >= ac
->ac_g_ex
.fe_len
)
1913 if (free
>= ac
->ac_g_ex
.fe_len
)
1925 static noinline_for_stack
int
1926 ext4_mb_regular_allocator(struct ext4_allocation_context
*ac
)
1928 ext4_group_t ngroups
, group
, i
;
1931 struct ext4_sb_info
*sbi
;
1932 struct super_block
*sb
;
1933 struct ext4_buddy e4b
;
1937 ngroups
= ext4_get_groups_count(sb
);
1938 /* non-extent files are limited to low blocks/groups */
1939 if (!(ext4_test_inode_flag(ac
->ac_inode
, EXT4_INODE_EXTENTS
)))
1940 ngroups
= sbi
->s_blockfile_groups
;
1942 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1944 /* first, try the goal */
1945 err
= ext4_mb_find_by_goal(ac
, &e4b
);
1946 if (err
|| ac
->ac_status
== AC_STATUS_FOUND
)
1949 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
1953 * ac->ac2_order is set only if the fe_len is a power of 2
1954 * if ac2_order is set we also set criteria to 0 so that we
1955 * try exact allocation using buddy.
1957 i
= fls(ac
->ac_g_ex
.fe_len
);
1960 * We search using buddy data only if the order of the request
1961 * is greater than equal to the sbi_s_mb_order2_reqs
1962 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
1964 if (i
>= sbi
->s_mb_order2_reqs
) {
1966 * This should tell if fe_len is exactly power of 2
1968 if ((ac
->ac_g_ex
.fe_len
& (~(1 << (i
- 1)))) == 0)
1969 ac
->ac_2order
= i
- 1;
1972 /* if stream allocation is enabled, use global goal */
1973 if (ac
->ac_flags
& EXT4_MB_STREAM_ALLOC
) {
1974 /* TBD: may be hot point */
1975 spin_lock(&sbi
->s_md_lock
);
1976 ac
->ac_g_ex
.fe_group
= sbi
->s_mb_last_group
;
1977 ac
->ac_g_ex
.fe_start
= sbi
->s_mb_last_start
;
1978 spin_unlock(&sbi
->s_md_lock
);
1981 /* Let's just scan groups to find more-less suitable blocks */
1982 cr
= ac
->ac_2order
? 0 : 1;
1984 * cr == 0 try to get exact allocation,
1985 * cr == 3 try to get anything
1988 for (; cr
< 4 && ac
->ac_status
== AC_STATUS_CONTINUE
; cr
++) {
1989 ac
->ac_criteria
= cr
;
1991 * searching for the right group start
1992 * from the goal value specified
1994 group
= ac
->ac_g_ex
.fe_group
;
1996 for (i
= 0; i
< ngroups
; group
++, i
++) {
1997 if (group
== ngroups
)
2000 /* This now checks without needing the buddy page */
2001 if (!ext4_mb_good_group(ac
, group
, cr
))
2004 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2008 ext4_lock_group(sb
, group
);
2011 * We need to check again after locking the
2014 if (!ext4_mb_good_group(ac
, group
, cr
)) {
2015 ext4_unlock_group(sb
, group
);
2016 ext4_mb_unload_buddy(&e4b
);
2020 ac
->ac_groups_scanned
++;
2021 if (cr
== 0 && ac
->ac_2order
< sb
->s_blocksize_bits
+2)
2022 ext4_mb_simple_scan_group(ac
, &e4b
);
2023 else if (cr
== 1 && sbi
->s_stripe
&&
2024 !(ac
->ac_g_ex
.fe_len
% sbi
->s_stripe
))
2025 ext4_mb_scan_aligned(ac
, &e4b
);
2027 ext4_mb_complex_scan_group(ac
, &e4b
);
2029 ext4_unlock_group(sb
, group
);
2030 ext4_mb_unload_buddy(&e4b
);
2032 if (ac
->ac_status
!= AC_STATUS_CONTINUE
)
2037 if (ac
->ac_b_ex
.fe_len
> 0 && ac
->ac_status
!= AC_STATUS_FOUND
&&
2038 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
2040 * We've been searching too long. Let's try to allocate
2041 * the best chunk we've found so far
2044 ext4_mb_try_best_found(ac
, &e4b
);
2045 if (ac
->ac_status
!= AC_STATUS_FOUND
) {
2047 * Someone more lucky has already allocated it.
2048 * The only thing we can do is just take first
2050 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2052 ac
->ac_b_ex
.fe_group
= 0;
2053 ac
->ac_b_ex
.fe_start
= 0;
2054 ac
->ac_b_ex
.fe_len
= 0;
2055 ac
->ac_status
= AC_STATUS_CONTINUE
;
2056 ac
->ac_flags
|= EXT4_MB_HINT_FIRST
;
2058 atomic_inc(&sbi
->s_mb_lost_chunks
);
2066 static void *ext4_mb_seq_groups_start(struct seq_file
*seq
, loff_t
*pos
)
2068 struct super_block
*sb
= seq
->private;
2071 if (*pos
< 0 || *pos
>= ext4_get_groups_count(sb
))
2074 return (void *) ((unsigned long) group
);
2077 static void *ext4_mb_seq_groups_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2079 struct super_block
*sb
= seq
->private;
2083 if (*pos
< 0 || *pos
>= ext4_get_groups_count(sb
))
2086 return (void *) ((unsigned long) group
);
2089 static int ext4_mb_seq_groups_show(struct seq_file
*seq
, void *v
)
2091 struct super_block
*sb
= seq
->private;
2092 ext4_group_t group
= (ext4_group_t
) ((unsigned long) v
);
2094 int err
, buddy_loaded
= 0;
2095 struct ext4_buddy e4b
;
2096 struct ext4_group_info
*grinfo
;
2098 struct ext4_group_info info
;
2099 ext4_grpblk_t counters
[16];
2104 seq_printf(seq
, "#%-5s: %-5s %-5s %-5s "
2105 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2106 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2107 "group", "free", "frags", "first",
2108 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2109 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2111 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(sg
.info
.bb_counters
[0]) +
2112 sizeof(struct ext4_group_info
);
2113 grinfo
= ext4_get_group_info(sb
, group
);
2114 /* Load the group info in memory only if not already loaded. */
2115 if (unlikely(EXT4_MB_GRP_NEED_INIT(grinfo
))) {
2116 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2118 seq_printf(seq
, "#%-5u: I/O error\n", group
);
2124 memcpy(&sg
, ext4_get_group_info(sb
, group
), i
);
2127 ext4_mb_unload_buddy(&e4b
);
2129 seq_printf(seq
, "#%-5u: %-5u %-5u %-5u [", group
, sg
.info
.bb_free
,
2130 sg
.info
.bb_fragments
, sg
.info
.bb_first_free
);
2131 for (i
= 0; i
<= 13; i
++)
2132 seq_printf(seq
, " %-5u", i
<= sb
->s_blocksize_bits
+ 1 ?
2133 sg
.info
.bb_counters
[i
] : 0);
2134 seq_printf(seq
, " ]\n");
2139 static void ext4_mb_seq_groups_stop(struct seq_file
*seq
, void *v
)
2143 static const struct seq_operations ext4_mb_seq_groups_ops
= {
2144 .start
= ext4_mb_seq_groups_start
,
2145 .next
= ext4_mb_seq_groups_next
,
2146 .stop
= ext4_mb_seq_groups_stop
,
2147 .show
= ext4_mb_seq_groups_show
,
2150 static int ext4_mb_seq_groups_open(struct inode
*inode
, struct file
*file
)
2152 struct super_block
*sb
= PDE_DATA(inode
);
2155 rc
= seq_open(file
, &ext4_mb_seq_groups_ops
);
2157 struct seq_file
*m
= file
->private_data
;
2164 static const struct file_operations ext4_mb_seq_groups_fops
= {
2165 .owner
= THIS_MODULE
,
2166 .open
= ext4_mb_seq_groups_open
,
2168 .llseek
= seq_lseek
,
2169 .release
= seq_release
,
2172 static struct kmem_cache
*get_groupinfo_cache(int blocksize_bits
)
2174 int cache_index
= blocksize_bits
- EXT4_MIN_BLOCK_LOG_SIZE
;
2175 struct kmem_cache
*cachep
= ext4_groupinfo_caches
[cache_index
];
2182 * Allocate the top-level s_group_info array for the specified number
2185 int ext4_mb_alloc_groupinfo(struct super_block
*sb
, ext4_group_t ngroups
)
2187 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2189 struct ext4_group_info
***new_groupinfo
;
2191 size
= (ngroups
+ EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2192 EXT4_DESC_PER_BLOCK_BITS(sb
);
2193 if (size
<= sbi
->s_group_info_size
)
2196 size
= roundup_pow_of_two(sizeof(*sbi
->s_group_info
) * size
);
2197 new_groupinfo
= ext4_kvzalloc(size
, GFP_KERNEL
);
2198 if (!new_groupinfo
) {
2199 ext4_msg(sb
, KERN_ERR
, "can't allocate buddy meta group");
2202 if (sbi
->s_group_info
) {
2203 memcpy(new_groupinfo
, sbi
->s_group_info
,
2204 sbi
->s_group_info_size
* sizeof(*sbi
->s_group_info
));
2205 ext4_kvfree(sbi
->s_group_info
);
2207 sbi
->s_group_info
= new_groupinfo
;
2208 sbi
->s_group_info_size
= size
/ sizeof(*sbi
->s_group_info
);
2209 ext4_debug("allocated s_groupinfo array for %d meta_bg's\n",
2210 sbi
->s_group_info_size
);
2214 /* Create and initialize ext4_group_info data for the given group. */
2215 int ext4_mb_add_groupinfo(struct super_block
*sb
, ext4_group_t group
,
2216 struct ext4_group_desc
*desc
)
2220 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2221 struct ext4_group_info
**meta_group_info
;
2222 struct kmem_cache
*cachep
= get_groupinfo_cache(sb
->s_blocksize_bits
);
2225 * First check if this group is the first of a reserved block.
2226 * If it's true, we have to allocate a new table of pointers
2227 * to ext4_group_info structures
2229 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0) {
2230 metalen
= sizeof(*meta_group_info
) <<
2231 EXT4_DESC_PER_BLOCK_BITS(sb
);
2232 meta_group_info
= kmalloc(metalen
, GFP_KERNEL
);
2233 if (meta_group_info
== NULL
) {
2234 ext4_msg(sb
, KERN_ERR
, "can't allocate mem "
2235 "for a buddy group");
2236 goto exit_meta_group_info
;
2238 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)] =
2243 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)];
2244 i
= group
& (EXT4_DESC_PER_BLOCK(sb
) - 1);
2246 meta_group_info
[i
] = kmem_cache_zalloc(cachep
, GFP_KERNEL
);
2247 if (meta_group_info
[i
] == NULL
) {
2248 ext4_msg(sb
, KERN_ERR
, "can't allocate buddy mem");
2249 goto exit_group_info
;
2251 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
,
2252 &(meta_group_info
[i
]->bb_state
));
2255 * initialize bb_free to be able to skip
2256 * empty groups without initialization
2258 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2259 meta_group_info
[i
]->bb_free
=
2260 ext4_free_clusters_after_init(sb
, group
, desc
);
2262 meta_group_info
[i
]->bb_free
=
2263 ext4_free_group_clusters(sb
, desc
);
2266 INIT_LIST_HEAD(&meta_group_info
[i
]->bb_prealloc_list
);
2267 init_rwsem(&meta_group_info
[i
]->alloc_sem
);
2268 meta_group_info
[i
]->bb_free_root
= RB_ROOT
;
2269 meta_group_info
[i
]->bb_largest_free_order
= -1; /* uninit */
2273 struct buffer_head
*bh
;
2274 meta_group_info
[i
]->bb_bitmap
=
2275 kmalloc(sb
->s_blocksize
, GFP_KERNEL
);
2276 BUG_ON(meta_group_info
[i
]->bb_bitmap
== NULL
);
2277 bh
= ext4_read_block_bitmap(sb
, group
);
2279 memcpy(meta_group_info
[i
]->bb_bitmap
, bh
->b_data
,
2288 /* If a meta_group_info table has been allocated, release it now */
2289 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0) {
2290 kfree(sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)]);
2291 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)] = NULL
;
2293 exit_meta_group_info
:
2295 } /* ext4_mb_add_groupinfo */
2297 static int ext4_mb_init_backend(struct super_block
*sb
)
2299 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
2301 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2303 struct ext4_group_desc
*desc
;
2304 struct kmem_cache
*cachep
;
2306 err
= ext4_mb_alloc_groupinfo(sb
, ngroups
);
2310 sbi
->s_buddy_cache
= new_inode(sb
);
2311 if (sbi
->s_buddy_cache
== NULL
) {
2312 ext4_msg(sb
, KERN_ERR
, "can't get new inode");
2315 /* To avoid potentially colliding with an valid on-disk inode number,
2316 * use EXT4_BAD_INO for the buddy cache inode number. This inode is
2317 * not in the inode hash, so it should never be found by iget(), but
2318 * this will avoid confusion if it ever shows up during debugging. */
2319 sbi
->s_buddy_cache
->i_ino
= EXT4_BAD_INO
;
2320 EXT4_I(sbi
->s_buddy_cache
)->i_disksize
= 0;
2321 for (i
= 0; i
< ngroups
; i
++) {
2322 desc
= ext4_get_group_desc(sb
, i
, NULL
);
2324 ext4_msg(sb
, KERN_ERR
, "can't read descriptor %u", i
);
2327 if (ext4_mb_add_groupinfo(sb
, i
, desc
) != 0)
2334 cachep
= get_groupinfo_cache(sb
->s_blocksize_bits
);
2336 kmem_cache_free(cachep
, ext4_get_group_info(sb
, i
));
2337 i
= sbi
->s_group_info_size
;
2339 kfree(sbi
->s_group_info
[i
]);
2340 iput(sbi
->s_buddy_cache
);
2342 ext4_kvfree(sbi
->s_group_info
);
2346 static void ext4_groupinfo_destroy_slabs(void)
2350 for (i
= 0; i
< NR_GRPINFO_CACHES
; i
++) {
2351 if (ext4_groupinfo_caches
[i
])
2352 kmem_cache_destroy(ext4_groupinfo_caches
[i
]);
2353 ext4_groupinfo_caches
[i
] = NULL
;
2357 static int ext4_groupinfo_create_slab(size_t size
)
2359 static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex
);
2361 int blocksize_bits
= order_base_2(size
);
2362 int cache_index
= blocksize_bits
- EXT4_MIN_BLOCK_LOG_SIZE
;
2363 struct kmem_cache
*cachep
;
2365 if (cache_index
>= NR_GRPINFO_CACHES
)
2368 if (unlikely(cache_index
< 0))
2371 mutex_lock(&ext4_grpinfo_slab_create_mutex
);
2372 if (ext4_groupinfo_caches
[cache_index
]) {
2373 mutex_unlock(&ext4_grpinfo_slab_create_mutex
);
2374 return 0; /* Already created */
2377 slab_size
= offsetof(struct ext4_group_info
,
2378 bb_counters
[blocksize_bits
+ 2]);
2380 cachep
= kmem_cache_create(ext4_groupinfo_slab_names
[cache_index
],
2381 slab_size
, 0, SLAB_RECLAIM_ACCOUNT
,
2384 ext4_groupinfo_caches
[cache_index
] = cachep
;
2386 mutex_unlock(&ext4_grpinfo_slab_create_mutex
);
2389 "EXT4-fs: no memory for groupinfo slab cache\n");
2396 int ext4_mb_init(struct super_block
*sb
)
2398 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2404 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(*sbi
->s_mb_offsets
);
2406 sbi
->s_mb_offsets
= kmalloc(i
, GFP_KERNEL
);
2407 if (sbi
->s_mb_offsets
== NULL
) {
2412 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(*sbi
->s_mb_maxs
);
2413 sbi
->s_mb_maxs
= kmalloc(i
, GFP_KERNEL
);
2414 if (sbi
->s_mb_maxs
== NULL
) {
2419 ret
= ext4_groupinfo_create_slab(sb
->s_blocksize
);
2423 /* order 0 is regular bitmap */
2424 sbi
->s_mb_maxs
[0] = sb
->s_blocksize
<< 3;
2425 sbi
->s_mb_offsets
[0] = 0;
2429 max
= sb
->s_blocksize
<< 2;
2431 sbi
->s_mb_offsets
[i
] = offset
;
2432 sbi
->s_mb_maxs
[i
] = max
;
2433 offset
+= 1 << (sb
->s_blocksize_bits
- i
);
2436 } while (i
<= sb
->s_blocksize_bits
+ 1);
2438 spin_lock_init(&sbi
->s_md_lock
);
2439 spin_lock_init(&sbi
->s_bal_lock
);
2441 sbi
->s_mb_max_to_scan
= MB_DEFAULT_MAX_TO_SCAN
;
2442 sbi
->s_mb_min_to_scan
= MB_DEFAULT_MIN_TO_SCAN
;
2443 sbi
->s_mb_stats
= MB_DEFAULT_STATS
;
2444 sbi
->s_mb_stream_request
= MB_DEFAULT_STREAM_THRESHOLD
;
2445 sbi
->s_mb_order2_reqs
= MB_DEFAULT_ORDER2_REQS
;
2447 * The default group preallocation is 512, which for 4k block
2448 * sizes translates to 2 megabytes. However for bigalloc file
2449 * systems, this is probably too big (i.e, if the cluster size
2450 * is 1 megabyte, then group preallocation size becomes half a
2451 * gigabyte!). As a default, we will keep a two megabyte
2452 * group pralloc size for cluster sizes up to 64k, and after
2453 * that, we will force a minimum group preallocation size of
2454 * 32 clusters. This translates to 8 megs when the cluster
2455 * size is 256k, and 32 megs when the cluster size is 1 meg,
2456 * which seems reasonable as a default.
2458 sbi
->s_mb_group_prealloc
= max(MB_DEFAULT_GROUP_PREALLOC
>>
2459 sbi
->s_cluster_bits
, 32);
2461 * If there is a s_stripe > 1, then we set the s_mb_group_prealloc
2462 * to the lowest multiple of s_stripe which is bigger than
2463 * the s_mb_group_prealloc as determined above. We want
2464 * the preallocation size to be an exact multiple of the
2465 * RAID stripe size so that preallocations don't fragment
2468 if (sbi
->s_stripe
> 1) {
2469 sbi
->s_mb_group_prealloc
= roundup(
2470 sbi
->s_mb_group_prealloc
, sbi
->s_stripe
);
2473 sbi
->s_locality_groups
= alloc_percpu(struct ext4_locality_group
);
2474 if (sbi
->s_locality_groups
== NULL
) {
2476 goto out_free_groupinfo_slab
;
2478 for_each_possible_cpu(i
) {
2479 struct ext4_locality_group
*lg
;
2480 lg
= per_cpu_ptr(sbi
->s_locality_groups
, i
);
2481 mutex_init(&lg
->lg_mutex
);
2482 for (j
= 0; j
< PREALLOC_TB_SIZE
; j
++)
2483 INIT_LIST_HEAD(&lg
->lg_prealloc_list
[j
]);
2484 spin_lock_init(&lg
->lg_prealloc_lock
);
2487 /* init file for buddy data */
2488 ret
= ext4_mb_init_backend(sb
);
2490 goto out_free_locality_groups
;
2493 proc_create_data("mb_groups", S_IRUGO
, sbi
->s_proc
,
2494 &ext4_mb_seq_groups_fops
, sb
);
2498 out_free_locality_groups
:
2499 free_percpu(sbi
->s_locality_groups
);
2500 sbi
->s_locality_groups
= NULL
;
2501 out_free_groupinfo_slab
:
2502 ext4_groupinfo_destroy_slabs();
2504 kfree(sbi
->s_mb_offsets
);
2505 sbi
->s_mb_offsets
= NULL
;
2506 kfree(sbi
->s_mb_maxs
);
2507 sbi
->s_mb_maxs
= NULL
;
2511 /* need to called with the ext4 group lock held */
2512 static void ext4_mb_cleanup_pa(struct ext4_group_info
*grp
)
2514 struct ext4_prealloc_space
*pa
;
2515 struct list_head
*cur
, *tmp
;
2518 list_for_each_safe(cur
, tmp
, &grp
->bb_prealloc_list
) {
2519 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
2520 list_del(&pa
->pa_group_list
);
2522 kmem_cache_free(ext4_pspace_cachep
, pa
);
2525 mb_debug(1, "mballoc: %u PAs left\n", count
);
2529 int ext4_mb_release(struct super_block
*sb
)
2531 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
2533 int num_meta_group_infos
;
2534 struct ext4_group_info
*grinfo
;
2535 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2536 struct kmem_cache
*cachep
= get_groupinfo_cache(sb
->s_blocksize_bits
);
2539 remove_proc_entry("mb_groups", sbi
->s_proc
);
2541 if (sbi
->s_group_info
) {
2542 for (i
= 0; i
< ngroups
; i
++) {
2543 grinfo
= ext4_get_group_info(sb
, i
);
2545 kfree(grinfo
->bb_bitmap
);
2547 ext4_lock_group(sb
, i
);
2548 ext4_mb_cleanup_pa(grinfo
);
2549 ext4_unlock_group(sb
, i
);
2550 kmem_cache_free(cachep
, grinfo
);
2552 num_meta_group_infos
= (ngroups
+
2553 EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2554 EXT4_DESC_PER_BLOCK_BITS(sb
);
2555 for (i
= 0; i
< num_meta_group_infos
; i
++)
2556 kfree(sbi
->s_group_info
[i
]);
2557 ext4_kvfree(sbi
->s_group_info
);
2559 kfree(sbi
->s_mb_offsets
);
2560 kfree(sbi
->s_mb_maxs
);
2561 if (sbi
->s_buddy_cache
)
2562 iput(sbi
->s_buddy_cache
);
2563 if (sbi
->s_mb_stats
) {
2564 ext4_msg(sb
, KERN_INFO
,
2565 "mballoc: %u blocks %u reqs (%u success)",
2566 atomic_read(&sbi
->s_bal_allocated
),
2567 atomic_read(&sbi
->s_bal_reqs
),
2568 atomic_read(&sbi
->s_bal_success
));
2569 ext4_msg(sb
, KERN_INFO
,
2570 "mballoc: %u extents scanned, %u goal hits, "
2571 "%u 2^N hits, %u breaks, %u lost",
2572 atomic_read(&sbi
->s_bal_ex_scanned
),
2573 atomic_read(&sbi
->s_bal_goals
),
2574 atomic_read(&sbi
->s_bal_2orders
),
2575 atomic_read(&sbi
->s_bal_breaks
),
2576 atomic_read(&sbi
->s_mb_lost_chunks
));
2577 ext4_msg(sb
, KERN_INFO
,
2578 "mballoc: %lu generated and it took %Lu",
2579 sbi
->s_mb_buddies_generated
,
2580 sbi
->s_mb_generation_time
);
2581 ext4_msg(sb
, KERN_INFO
,
2582 "mballoc: %u preallocated, %u discarded",
2583 atomic_read(&sbi
->s_mb_preallocated
),
2584 atomic_read(&sbi
->s_mb_discarded
));
2587 free_percpu(sbi
->s_locality_groups
);
2592 static inline int ext4_issue_discard(struct super_block
*sb
,
2593 ext4_group_t block_group
, ext4_grpblk_t cluster
, int count
)
2595 ext4_fsblk_t discard_block
;
2597 discard_block
= (EXT4_C2B(EXT4_SB(sb
), cluster
) +
2598 ext4_group_first_block_no(sb
, block_group
));
2599 count
= EXT4_C2B(EXT4_SB(sb
), count
);
2600 trace_ext4_discard_blocks(sb
,
2601 (unsigned long long) discard_block
, count
);
2602 return sb_issue_discard(sb
, discard_block
, count
, GFP_NOFS
, 0);
2606 * This function is called by the jbd2 layer once the commit has finished,
2607 * so we know we can free the blocks that were released with that commit.
2609 static void ext4_free_data_callback(struct super_block
*sb
,
2610 struct ext4_journal_cb_entry
*jce
,
2613 struct ext4_free_data
*entry
= (struct ext4_free_data
*)jce
;
2614 struct ext4_buddy e4b
;
2615 struct ext4_group_info
*db
;
2616 int err
, count
= 0, count2
= 0;
2618 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
2619 entry
->efd_count
, entry
->efd_group
, entry
);
2621 if (test_opt(sb
, DISCARD
)) {
2622 err
= ext4_issue_discard(sb
, entry
->efd_group
,
2623 entry
->efd_start_cluster
,
2625 if (err
&& err
!= -EOPNOTSUPP
)
2626 ext4_msg(sb
, KERN_WARNING
, "discard request in"
2627 " group:%d block:%d count:%d failed"
2628 " with %d", entry
->efd_group
,
2629 entry
->efd_start_cluster
,
2630 entry
->efd_count
, err
);
2633 err
= ext4_mb_load_buddy(sb
, entry
->efd_group
, &e4b
);
2634 /* we expect to find existing buddy because it's pinned */
2639 /* there are blocks to put in buddy to make them really free */
2640 count
+= entry
->efd_count
;
2642 ext4_lock_group(sb
, entry
->efd_group
);
2643 /* Take it out of per group rb tree */
2644 rb_erase(&entry
->efd_node
, &(db
->bb_free_root
));
2645 mb_free_blocks(NULL
, &e4b
, entry
->efd_start_cluster
, entry
->efd_count
);
2648 * Clear the trimmed flag for the group so that the next
2649 * ext4_trim_fs can trim it.
2650 * If the volume is mounted with -o discard, online discard
2651 * is supported and the free blocks will be trimmed online.
2653 if (!test_opt(sb
, DISCARD
))
2654 EXT4_MB_GRP_CLEAR_TRIMMED(db
);
2656 if (!db
->bb_free_root
.rb_node
) {
2657 /* No more items in the per group rb tree
2658 * balance refcounts from ext4_mb_free_metadata()
2660 page_cache_release(e4b
.bd_buddy_page
);
2661 page_cache_release(e4b
.bd_bitmap_page
);
2663 ext4_unlock_group(sb
, entry
->efd_group
);
2664 kmem_cache_free(ext4_free_data_cachep
, entry
);
2665 ext4_mb_unload_buddy(&e4b
);
2667 mb_debug(1, "freed %u blocks in %u structures\n", count
, count2
);
2670 int __init
ext4_init_mballoc(void)
2672 ext4_pspace_cachep
= KMEM_CACHE(ext4_prealloc_space
,
2673 SLAB_RECLAIM_ACCOUNT
);
2674 if (ext4_pspace_cachep
== NULL
)
2677 ext4_ac_cachep
= KMEM_CACHE(ext4_allocation_context
,
2678 SLAB_RECLAIM_ACCOUNT
);
2679 if (ext4_ac_cachep
== NULL
) {
2680 kmem_cache_destroy(ext4_pspace_cachep
);
2684 ext4_free_data_cachep
= KMEM_CACHE(ext4_free_data
,
2685 SLAB_RECLAIM_ACCOUNT
);
2686 if (ext4_free_data_cachep
== NULL
) {
2687 kmem_cache_destroy(ext4_pspace_cachep
);
2688 kmem_cache_destroy(ext4_ac_cachep
);
2694 void ext4_exit_mballoc(void)
2697 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2698 * before destroying the slab cache.
2701 kmem_cache_destroy(ext4_pspace_cachep
);
2702 kmem_cache_destroy(ext4_ac_cachep
);
2703 kmem_cache_destroy(ext4_free_data_cachep
);
2704 ext4_groupinfo_destroy_slabs();
2709 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
2710 * Returns 0 if success or error code
2712 static noinline_for_stack
int
2713 ext4_mb_mark_diskspace_used(struct ext4_allocation_context
*ac
,
2714 handle_t
*handle
, unsigned int reserv_clstrs
)
2716 struct buffer_head
*bitmap_bh
= NULL
;
2717 struct ext4_group_desc
*gdp
;
2718 struct buffer_head
*gdp_bh
;
2719 struct ext4_sb_info
*sbi
;
2720 struct super_block
*sb
;
2724 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
2725 BUG_ON(ac
->ac_b_ex
.fe_len
<= 0);
2731 bitmap_bh
= ext4_read_block_bitmap(sb
, ac
->ac_b_ex
.fe_group
);
2735 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
2740 gdp
= ext4_get_group_desc(sb
, ac
->ac_b_ex
.fe_group
, &gdp_bh
);
2744 ext4_debug("using block group %u(%d)\n", ac
->ac_b_ex
.fe_group
,
2745 ext4_free_group_clusters(sb
, gdp
));
2747 err
= ext4_journal_get_write_access(handle
, gdp_bh
);
2751 block
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
2753 len
= EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
);
2754 if (!ext4_data_block_valid(sbi
, block
, len
)) {
2755 ext4_error(sb
, "Allocating blocks %llu-%llu which overlap "
2756 "fs metadata", block
, block
+len
);
2757 /* File system mounted not to panic on error
2758 * Fix the bitmap and repeat the block allocation
2759 * We leak some of the blocks here.
2761 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
2762 ext4_set_bits(bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,
2763 ac
->ac_b_ex
.fe_len
);
2764 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
2765 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
2771 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
2772 #ifdef AGGRESSIVE_CHECK
2775 for (i
= 0; i
< ac
->ac_b_ex
.fe_len
; i
++) {
2776 BUG_ON(mb_test_bit(ac
->ac_b_ex
.fe_start
+ i
,
2777 bitmap_bh
->b_data
));
2781 ext4_set_bits(bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,
2782 ac
->ac_b_ex
.fe_len
);
2783 if (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2784 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT
);
2785 ext4_free_group_clusters_set(sb
, gdp
,
2786 ext4_free_clusters_after_init(sb
,
2787 ac
->ac_b_ex
.fe_group
, gdp
));
2789 len
= ext4_free_group_clusters(sb
, gdp
) - ac
->ac_b_ex
.fe_len
;
2790 ext4_free_group_clusters_set(sb
, gdp
, len
);
2791 ext4_block_bitmap_csum_set(sb
, ac
->ac_b_ex
.fe_group
, gdp
, bitmap_bh
);
2792 ext4_group_desc_csum_set(sb
, ac
->ac_b_ex
.fe_group
, gdp
);
2794 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
2795 percpu_counter_sub(&sbi
->s_freeclusters_counter
, ac
->ac_b_ex
.fe_len
);
2797 * Now reduce the dirty block count also. Should not go negative
2799 if (!(ac
->ac_flags
& EXT4_MB_DELALLOC_RESERVED
))
2800 /* release all the reserved blocks if non delalloc */
2801 percpu_counter_sub(&sbi
->s_dirtyclusters_counter
,
2804 if (sbi
->s_log_groups_per_flex
) {
2805 ext4_group_t flex_group
= ext4_flex_group(sbi
,
2806 ac
->ac_b_ex
.fe_group
);
2807 atomic64_sub(ac
->ac_b_ex
.fe_len
,
2808 &sbi
->s_flex_groups
[flex_group
].free_clusters
);
2811 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
2814 err
= ext4_handle_dirty_metadata(handle
, NULL
, gdp_bh
);
2822 * here we normalize request for locality group
2823 * Group request are normalized to s_mb_group_prealloc, which goes to
2824 * s_strip if we set the same via mount option.
2825 * s_mb_group_prealloc can be configured via
2826 * /sys/fs/ext4/<partition>/mb_group_prealloc
2828 * XXX: should we try to preallocate more than the group has now?
2830 static void ext4_mb_normalize_group_request(struct ext4_allocation_context
*ac
)
2832 struct super_block
*sb
= ac
->ac_sb
;
2833 struct ext4_locality_group
*lg
= ac
->ac_lg
;
2836 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_mb_group_prealloc
;
2837 mb_debug(1, "#%u: goal %u blocks for locality group\n",
2838 current
->pid
, ac
->ac_g_ex
.fe_len
);
2842 * Normalization means making request better in terms of
2843 * size and alignment
2845 static noinline_for_stack
void
2846 ext4_mb_normalize_request(struct ext4_allocation_context
*ac
,
2847 struct ext4_allocation_request
*ar
)
2849 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
2852 loff_t size
, start_off
;
2853 loff_t orig_size __maybe_unused
;
2855 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
2856 struct ext4_prealloc_space
*pa
;
2858 /* do normalize only data requests, metadata requests
2859 do not need preallocation */
2860 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
2863 /* sometime caller may want exact blocks */
2864 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
2867 /* caller may indicate that preallocation isn't
2868 * required (it's a tail, for example) */
2869 if (ac
->ac_flags
& EXT4_MB_HINT_NOPREALLOC
)
2872 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
) {
2873 ext4_mb_normalize_group_request(ac
);
2877 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
2879 /* first, let's learn actual file size
2880 * given current request is allocated */
2881 size
= ac
->ac_o_ex
.fe_logical
+ EXT4_C2B(sbi
, ac
->ac_o_ex
.fe_len
);
2882 size
= size
<< bsbits
;
2883 if (size
< i_size_read(ac
->ac_inode
))
2884 size
= i_size_read(ac
->ac_inode
);
2887 /* max size of free chunks */
2890 #define NRL_CHECK_SIZE(req, size, max, chunk_size) \
2891 (req <= (size) || max <= (chunk_size))
2893 /* first, try to predict filesize */
2894 /* XXX: should this table be tunable? */
2896 if (size
<= 16 * 1024) {
2898 } else if (size
<= 32 * 1024) {
2900 } else if (size
<= 64 * 1024) {
2902 } else if (size
<= 128 * 1024) {
2904 } else if (size
<= 256 * 1024) {
2906 } else if (size
<= 512 * 1024) {
2908 } else if (size
<= 1024 * 1024) {
2910 } else if (NRL_CHECK_SIZE(size
, 4 * 1024 * 1024, max
, 2 * 1024)) {
2911 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
2912 (21 - bsbits
)) << 21;
2913 size
= 2 * 1024 * 1024;
2914 } else if (NRL_CHECK_SIZE(size
, 8 * 1024 * 1024, max
, 4 * 1024)) {
2915 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
2916 (22 - bsbits
)) << 22;
2917 size
= 4 * 1024 * 1024;
2918 } else if (NRL_CHECK_SIZE(ac
->ac_o_ex
.fe_len
,
2919 (8<<20)>>bsbits
, max
, 8 * 1024)) {
2920 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
2921 (23 - bsbits
)) << 23;
2922 size
= 8 * 1024 * 1024;
2924 start_off
= (loff_t
)ac
->ac_o_ex
.fe_logical
<< bsbits
;
2925 size
= ac
->ac_o_ex
.fe_len
<< bsbits
;
2927 size
= size
>> bsbits
;
2928 start
= start_off
>> bsbits
;
2930 /* don't cover already allocated blocks in selected range */
2931 if (ar
->pleft
&& start
<= ar
->lleft
) {
2932 size
-= ar
->lleft
+ 1 - start
;
2933 start
= ar
->lleft
+ 1;
2935 if (ar
->pright
&& start
+ size
- 1 >= ar
->lright
)
2936 size
-= start
+ size
- ar
->lright
;
2940 /* check we don't cross already preallocated blocks */
2942 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
2947 spin_lock(&pa
->pa_lock
);
2948 if (pa
->pa_deleted
) {
2949 spin_unlock(&pa
->pa_lock
);
2953 pa_end
= pa
->pa_lstart
+ EXT4_C2B(EXT4_SB(ac
->ac_sb
),
2956 /* PA must not overlap original request */
2957 BUG_ON(!(ac
->ac_o_ex
.fe_logical
>= pa_end
||
2958 ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
));
2960 /* skip PAs this normalized request doesn't overlap with */
2961 if (pa
->pa_lstart
>= end
|| pa_end
<= start
) {
2962 spin_unlock(&pa
->pa_lock
);
2965 BUG_ON(pa
->pa_lstart
<= start
&& pa_end
>= end
);
2967 /* adjust start or end to be adjacent to this pa */
2968 if (pa_end
<= ac
->ac_o_ex
.fe_logical
) {
2969 BUG_ON(pa_end
< start
);
2971 } else if (pa
->pa_lstart
> ac
->ac_o_ex
.fe_logical
) {
2972 BUG_ON(pa
->pa_lstart
> end
);
2973 end
= pa
->pa_lstart
;
2975 spin_unlock(&pa
->pa_lock
);
2980 /* XXX: extra loop to check we really don't overlap preallocations */
2982 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
2985 spin_lock(&pa
->pa_lock
);
2986 if (pa
->pa_deleted
== 0) {
2987 pa_end
= pa
->pa_lstart
+ EXT4_C2B(EXT4_SB(ac
->ac_sb
),
2989 BUG_ON(!(start
>= pa_end
|| end
<= pa
->pa_lstart
));
2991 spin_unlock(&pa
->pa_lock
);
2995 if (start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
2996 start
> ac
->ac_o_ex
.fe_logical
) {
2997 ext4_msg(ac
->ac_sb
, KERN_ERR
,
2998 "start %lu, size %lu, fe_logical %lu",
2999 (unsigned long) start
, (unsigned long) size
,
3000 (unsigned long) ac
->ac_o_ex
.fe_logical
);
3002 BUG_ON(start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3003 start
> ac
->ac_o_ex
.fe_logical
);
3004 BUG_ON(size
<= 0 || size
> EXT4_CLUSTERS_PER_GROUP(ac
->ac_sb
));
3006 /* now prepare goal request */
3008 /* XXX: is it better to align blocks WRT to logical
3009 * placement or satisfy big request as is */
3010 ac
->ac_g_ex
.fe_logical
= start
;
3011 ac
->ac_g_ex
.fe_len
= EXT4_NUM_B2C(sbi
, size
);
3013 /* define goal start in order to merge */
3014 if (ar
->pright
&& (ar
->lright
== (start
+ size
))) {
3015 /* merge to the right */
3016 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pright
- size
,
3017 &ac
->ac_f_ex
.fe_group
,
3018 &ac
->ac_f_ex
.fe_start
);
3019 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3021 if (ar
->pleft
&& (ar
->lleft
+ 1 == start
)) {
3022 /* merge to the left */
3023 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pleft
+ 1,
3024 &ac
->ac_f_ex
.fe_group
,
3025 &ac
->ac_f_ex
.fe_start
);
3026 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3029 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size
,
3030 (unsigned) orig_size
, (unsigned) start
);
3033 static void ext4_mb_collect_stats(struct ext4_allocation_context
*ac
)
3035 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3037 if (sbi
->s_mb_stats
&& ac
->ac_g_ex
.fe_len
> 1) {
3038 atomic_inc(&sbi
->s_bal_reqs
);
3039 atomic_add(ac
->ac_b_ex
.fe_len
, &sbi
->s_bal_allocated
);
3040 if (ac
->ac_b_ex
.fe_len
>= ac
->ac_o_ex
.fe_len
)
3041 atomic_inc(&sbi
->s_bal_success
);
3042 atomic_add(ac
->ac_found
, &sbi
->s_bal_ex_scanned
);
3043 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
3044 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
3045 atomic_inc(&sbi
->s_bal_goals
);
3046 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
)
3047 atomic_inc(&sbi
->s_bal_breaks
);
3050 if (ac
->ac_op
== EXT4_MB_HISTORY_ALLOC
)
3051 trace_ext4_mballoc_alloc(ac
);
3053 trace_ext4_mballoc_prealloc(ac
);
3057 * Called on failure; free up any blocks from the inode PA for this
3058 * context. We don't need this for MB_GROUP_PA because we only change
3059 * pa_free in ext4_mb_release_context(), but on failure, we've already
3060 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3062 static void ext4_discard_allocated_blocks(struct ext4_allocation_context
*ac
)
3064 struct ext4_prealloc_space
*pa
= ac
->ac_pa
;
3066 if (pa
&& pa
->pa_type
== MB_INODE_PA
)
3067 pa
->pa_free
+= ac
->ac_b_ex
.fe_len
;
3071 * use blocks preallocated to inode
3073 static void ext4_mb_use_inode_pa(struct ext4_allocation_context
*ac
,
3074 struct ext4_prealloc_space
*pa
)
3076 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3081 /* found preallocated blocks, use them */
3082 start
= pa
->pa_pstart
+ (ac
->ac_o_ex
.fe_logical
- pa
->pa_lstart
);
3083 end
= min(pa
->pa_pstart
+ EXT4_C2B(sbi
, pa
->pa_len
),
3084 start
+ EXT4_C2B(sbi
, ac
->ac_o_ex
.fe_len
));
3085 len
= EXT4_NUM_B2C(sbi
, end
- start
);
3086 ext4_get_group_no_and_offset(ac
->ac_sb
, start
, &ac
->ac_b_ex
.fe_group
,
3087 &ac
->ac_b_ex
.fe_start
);
3088 ac
->ac_b_ex
.fe_len
= len
;
3089 ac
->ac_status
= AC_STATUS_FOUND
;
3092 BUG_ON(start
< pa
->pa_pstart
);
3093 BUG_ON(end
> pa
->pa_pstart
+ EXT4_C2B(sbi
, pa
->pa_len
));
3094 BUG_ON(pa
->pa_free
< len
);
3097 mb_debug(1, "use %llu/%u from inode pa %p\n", start
, len
, pa
);
3101 * use blocks preallocated to locality group
3103 static void ext4_mb_use_group_pa(struct ext4_allocation_context
*ac
,
3104 struct ext4_prealloc_space
*pa
)
3106 unsigned int len
= ac
->ac_o_ex
.fe_len
;
3108 ext4_get_group_no_and_offset(ac
->ac_sb
, pa
->pa_pstart
,
3109 &ac
->ac_b_ex
.fe_group
,
3110 &ac
->ac_b_ex
.fe_start
);
3111 ac
->ac_b_ex
.fe_len
= len
;
3112 ac
->ac_status
= AC_STATUS_FOUND
;
3115 /* we don't correct pa_pstart or pa_plen here to avoid
3116 * possible race when the group is being loaded concurrently
3117 * instead we correct pa later, after blocks are marked
3118 * in on-disk bitmap -- see ext4_mb_release_context()
3119 * Other CPUs are prevented from allocating from this pa by lg_mutex
3121 mb_debug(1, "use %u/%u from group pa %p\n", pa
->pa_lstart
-len
, len
, pa
);
3125 * Return the prealloc space that have minimal distance
3126 * from the goal block. @cpa is the prealloc
3127 * space that is having currently known minimal distance
3128 * from the goal block.
3130 static struct ext4_prealloc_space
*
3131 ext4_mb_check_group_pa(ext4_fsblk_t goal_block
,
3132 struct ext4_prealloc_space
*pa
,
3133 struct ext4_prealloc_space
*cpa
)
3135 ext4_fsblk_t cur_distance
, new_distance
;
3138 atomic_inc(&pa
->pa_count
);
3141 cur_distance
= abs(goal_block
- cpa
->pa_pstart
);
3142 new_distance
= abs(goal_block
- pa
->pa_pstart
);
3144 if (cur_distance
<= new_distance
)
3147 /* drop the previous reference */
3148 atomic_dec(&cpa
->pa_count
);
3149 atomic_inc(&pa
->pa_count
);
3154 * search goal blocks in preallocated space
3156 static noinline_for_stack
int
3157 ext4_mb_use_preallocated(struct ext4_allocation_context
*ac
)
3159 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3161 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3162 struct ext4_locality_group
*lg
;
3163 struct ext4_prealloc_space
*pa
, *cpa
= NULL
;
3164 ext4_fsblk_t goal_block
;
3166 /* only data can be preallocated */
3167 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3170 /* first, try per-file preallocation */
3172 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3174 /* all fields in this condition don't change,
3175 * so we can skip locking for them */
3176 if (ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
||
3177 ac
->ac_o_ex
.fe_logical
>= (pa
->pa_lstart
+
3178 EXT4_C2B(sbi
, pa
->pa_len
)))
3181 /* non-extent files can't have physical blocks past 2^32 */
3182 if (!(ext4_test_inode_flag(ac
->ac_inode
, EXT4_INODE_EXTENTS
)) &&
3183 (pa
->pa_pstart
+ EXT4_C2B(sbi
, pa
->pa_len
) >
3184 EXT4_MAX_BLOCK_FILE_PHYS
))
3187 /* found preallocated blocks, use them */
3188 spin_lock(&pa
->pa_lock
);
3189 if (pa
->pa_deleted
== 0 && pa
->pa_free
) {
3190 atomic_inc(&pa
->pa_count
);
3191 ext4_mb_use_inode_pa(ac
, pa
);
3192 spin_unlock(&pa
->pa_lock
);
3193 ac
->ac_criteria
= 10;
3197 spin_unlock(&pa
->pa_lock
);
3201 /* can we use group allocation? */
3202 if (!(ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
))
3205 /* inode may have no locality group for some reason */
3209 order
= fls(ac
->ac_o_ex
.fe_len
) - 1;
3210 if (order
> PREALLOC_TB_SIZE
- 1)
3211 /* The max size of hash table is PREALLOC_TB_SIZE */
3212 order
= PREALLOC_TB_SIZE
- 1;
3214 goal_block
= ext4_grp_offs_to_block(ac
->ac_sb
, &ac
->ac_g_ex
);
3216 * search for the prealloc space that is having
3217 * minimal distance from the goal block.
3219 for (i
= order
; i
< PREALLOC_TB_SIZE
; i
++) {
3221 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[i
],
3223 spin_lock(&pa
->pa_lock
);
3224 if (pa
->pa_deleted
== 0 &&
3225 pa
->pa_free
>= ac
->ac_o_ex
.fe_len
) {
3227 cpa
= ext4_mb_check_group_pa(goal_block
,
3230 spin_unlock(&pa
->pa_lock
);
3235 ext4_mb_use_group_pa(ac
, cpa
);
3236 ac
->ac_criteria
= 20;
3243 * the function goes through all block freed in the group
3244 * but not yet committed and marks them used in in-core bitmap.
3245 * buddy must be generated from this bitmap
3246 * Need to be called with the ext4 group lock held
3248 static void ext4_mb_generate_from_freelist(struct super_block
*sb
, void *bitmap
,
3252 struct ext4_group_info
*grp
;
3253 struct ext4_free_data
*entry
;
3255 grp
= ext4_get_group_info(sb
, group
);
3256 n
= rb_first(&(grp
->bb_free_root
));
3259 entry
= rb_entry(n
, struct ext4_free_data
, efd_node
);
3260 ext4_set_bits(bitmap
, entry
->efd_start_cluster
, entry
->efd_count
);
3267 * the function goes through all preallocation in this group and marks them
3268 * used in in-core bitmap. buddy must be generated from this bitmap
3269 * Need to be called with ext4 group lock held
3271 static noinline_for_stack
3272 void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
3275 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3276 struct ext4_prealloc_space
*pa
;
3277 struct list_head
*cur
;
3278 ext4_group_t groupnr
;
3279 ext4_grpblk_t start
;
3280 int preallocated
= 0;
3283 /* all form of preallocation discards first load group,
3284 * so the only competing code is preallocation use.
3285 * we don't need any locking here
3286 * notice we do NOT ignore preallocations with pa_deleted
3287 * otherwise we could leave used blocks available for
3288 * allocation in buddy when concurrent ext4_mb_put_pa()
3289 * is dropping preallocation
3291 list_for_each(cur
, &grp
->bb_prealloc_list
) {
3292 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
3293 spin_lock(&pa
->pa_lock
);
3294 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
3297 spin_unlock(&pa
->pa_lock
);
3298 if (unlikely(len
== 0))
3300 BUG_ON(groupnr
!= group
);
3301 ext4_set_bits(bitmap
, start
, len
);
3302 preallocated
+= len
;
3304 mb_debug(1, "prellocated %u for group %u\n", preallocated
, group
);
3307 static void ext4_mb_pa_callback(struct rcu_head
*head
)
3309 struct ext4_prealloc_space
*pa
;
3310 pa
= container_of(head
, struct ext4_prealloc_space
, u
.pa_rcu
);
3311 kmem_cache_free(ext4_pspace_cachep
, pa
);
3315 * drops a reference to preallocated space descriptor
3316 * if this was the last reference and the space is consumed
3318 static void ext4_mb_put_pa(struct ext4_allocation_context
*ac
,
3319 struct super_block
*sb
, struct ext4_prealloc_space
*pa
)
3322 ext4_fsblk_t grp_blk
;
3324 if (!atomic_dec_and_test(&pa
->pa_count
) || pa
->pa_free
!= 0)
3327 /* in this short window concurrent discard can set pa_deleted */
3328 spin_lock(&pa
->pa_lock
);
3329 if (pa
->pa_deleted
== 1) {
3330 spin_unlock(&pa
->pa_lock
);
3335 spin_unlock(&pa
->pa_lock
);
3337 grp_blk
= pa
->pa_pstart
;
3339 * If doing group-based preallocation, pa_pstart may be in the
3340 * next group when pa is used up
3342 if (pa
->pa_type
== MB_GROUP_PA
)
3345 ext4_get_group_no_and_offset(sb
, grp_blk
, &grp
, NULL
);
3350 * P1 (buddy init) P2 (regular allocation)
3351 * find block B in PA
3352 * copy on-disk bitmap to buddy
3353 * mark B in on-disk bitmap
3354 * drop PA from group
3355 * mark all PAs in buddy
3357 * thus, P1 initializes buddy with B available. to prevent this
3358 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3361 ext4_lock_group(sb
, grp
);
3362 list_del(&pa
->pa_group_list
);
3363 ext4_unlock_group(sb
, grp
);
3365 spin_lock(pa
->pa_obj_lock
);
3366 list_del_rcu(&pa
->pa_inode_list
);
3367 spin_unlock(pa
->pa_obj_lock
);
3369 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3373 * creates new preallocated space for given inode
3375 static noinline_for_stack
int
3376 ext4_mb_new_inode_pa(struct ext4_allocation_context
*ac
)
3378 struct super_block
*sb
= ac
->ac_sb
;
3379 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3380 struct ext4_prealloc_space
*pa
;
3381 struct ext4_group_info
*grp
;
3382 struct ext4_inode_info
*ei
;
3384 /* preallocate only when found space is larger then requested */
3385 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3386 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3387 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3389 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3393 if (ac
->ac_b_ex
.fe_len
< ac
->ac_g_ex
.fe_len
) {
3399 /* we can't allocate as much as normalizer wants.
3400 * so, found space must get proper lstart
3401 * to cover original request */
3402 BUG_ON(ac
->ac_g_ex
.fe_logical
> ac
->ac_o_ex
.fe_logical
);
3403 BUG_ON(ac
->ac_g_ex
.fe_len
< ac
->ac_o_ex
.fe_len
);
3405 /* we're limited by original request in that
3406 * logical block must be covered any way
3407 * winl is window we can move our chunk within */
3408 winl
= ac
->ac_o_ex
.fe_logical
- ac
->ac_g_ex
.fe_logical
;
3410 /* also, we should cover whole original request */
3411 wins
= EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
- ac
->ac_o_ex
.fe_len
);
3413 /* the smallest one defines real window */
3414 win
= min(winl
, wins
);
3416 offs
= ac
->ac_o_ex
.fe_logical
%
3417 EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
);
3418 if (offs
&& offs
< win
)
3421 ac
->ac_b_ex
.fe_logical
= ac
->ac_o_ex
.fe_logical
-
3422 EXT4_NUM_B2C(sbi
, win
);
3423 BUG_ON(ac
->ac_o_ex
.fe_logical
< ac
->ac_b_ex
.fe_logical
);
3424 BUG_ON(ac
->ac_o_ex
.fe_len
> ac
->ac_b_ex
.fe_len
);
3427 /* preallocation can change ac_b_ex, thus we store actually
3428 * allocated blocks for history */
3429 ac
->ac_f_ex
= ac
->ac_b_ex
;
3431 pa
->pa_lstart
= ac
->ac_b_ex
.fe_logical
;
3432 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3433 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3434 pa
->pa_free
= pa
->pa_len
;
3435 atomic_set(&pa
->pa_count
, 1);
3436 spin_lock_init(&pa
->pa_lock
);
3437 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3438 INIT_LIST_HEAD(&pa
->pa_group_list
);
3440 pa
->pa_type
= MB_INODE_PA
;
3442 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa
,
3443 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3444 trace_ext4_mb_new_inode_pa(ac
, pa
);
3446 ext4_mb_use_inode_pa(ac
, pa
);
3447 atomic_add(pa
->pa_free
, &sbi
->s_mb_preallocated
);
3449 ei
= EXT4_I(ac
->ac_inode
);
3450 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3452 pa
->pa_obj_lock
= &ei
->i_prealloc_lock
;
3453 pa
->pa_inode
= ac
->ac_inode
;
3455 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3456 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3457 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3459 spin_lock(pa
->pa_obj_lock
);
3460 list_add_rcu(&pa
->pa_inode_list
, &ei
->i_prealloc_list
);
3461 spin_unlock(pa
->pa_obj_lock
);
3467 * creates new preallocated space for locality group inodes belongs to
3469 static noinline_for_stack
int
3470 ext4_mb_new_group_pa(struct ext4_allocation_context
*ac
)
3472 struct super_block
*sb
= ac
->ac_sb
;
3473 struct ext4_locality_group
*lg
;
3474 struct ext4_prealloc_space
*pa
;
3475 struct ext4_group_info
*grp
;
3477 /* preallocate only when found space is larger then requested */
3478 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3479 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3480 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3482 BUG_ON(ext4_pspace_cachep
== NULL
);
3483 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3487 /* preallocation can change ac_b_ex, thus we store actually
3488 * allocated blocks for history */
3489 ac
->ac_f_ex
= ac
->ac_b_ex
;
3491 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3492 pa
->pa_lstart
= pa
->pa_pstart
;
3493 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3494 pa
->pa_free
= pa
->pa_len
;
3495 atomic_set(&pa
->pa_count
, 1);
3496 spin_lock_init(&pa
->pa_lock
);
3497 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3498 INIT_LIST_HEAD(&pa
->pa_group_list
);
3500 pa
->pa_type
= MB_GROUP_PA
;
3502 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa
,
3503 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3504 trace_ext4_mb_new_group_pa(ac
, pa
);
3506 ext4_mb_use_group_pa(ac
, pa
);
3507 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3509 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3513 pa
->pa_obj_lock
= &lg
->lg_prealloc_lock
;
3514 pa
->pa_inode
= NULL
;
3516 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3517 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3518 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3521 * We will later add the new pa to the right bucket
3522 * after updating the pa_free in ext4_mb_release_context
3527 static int ext4_mb_new_preallocation(struct ext4_allocation_context
*ac
)
3531 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
3532 err
= ext4_mb_new_group_pa(ac
);
3534 err
= ext4_mb_new_inode_pa(ac
);
3539 * finds all unused blocks in on-disk bitmap, frees them in
3540 * in-core bitmap and buddy.
3541 * @pa must be unlinked from inode and group lists, so that
3542 * nobody else can find/use it.
3543 * the caller MUST hold group/inode locks.
3544 * TODO: optimize the case when there are no in-core structures yet
3546 static noinline_for_stack
int
3547 ext4_mb_release_inode_pa(struct ext4_buddy
*e4b
, struct buffer_head
*bitmap_bh
,
3548 struct ext4_prealloc_space
*pa
)
3550 struct super_block
*sb
= e4b
->bd_sb
;
3551 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3556 unsigned long long grp_blk_start
;
3560 BUG_ON(pa
->pa_deleted
== 0);
3561 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3562 grp_blk_start
= pa
->pa_pstart
- EXT4_C2B(sbi
, bit
);
3563 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3564 end
= bit
+ pa
->pa_len
;
3567 bit
= mb_find_next_zero_bit(bitmap_bh
->b_data
, end
, bit
);
3570 next
= mb_find_next_bit(bitmap_bh
->b_data
, end
, bit
);
3571 mb_debug(1, " free preallocated %u/%u in group %u\n",
3572 (unsigned) ext4_group_first_block_no(sb
, group
) + bit
,
3573 (unsigned) next
- bit
, (unsigned) group
);
3576 trace_ext4_mballoc_discard(sb
, NULL
, group
, bit
, next
- bit
);
3577 trace_ext4_mb_release_inode_pa(pa
, (grp_blk_start
+
3578 EXT4_C2B(sbi
, bit
)),
3580 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, next
- bit
);
3583 if (free
!= pa
->pa_free
) {
3584 ext4_msg(e4b
->bd_sb
, KERN_CRIT
,
3585 "pa %p: logic %lu, phys. %lu, len %lu",
3586 pa
, (unsigned long) pa
->pa_lstart
,
3587 (unsigned long) pa
->pa_pstart
,
3588 (unsigned long) pa
->pa_len
);
3589 ext4_grp_locked_error(sb
, group
, 0, 0, "free %u, pa_free %u",
3592 * pa is already deleted so we use the value obtained
3593 * from the bitmap and continue.
3596 atomic_add(free
, &sbi
->s_mb_discarded
);
3601 static noinline_for_stack
int
3602 ext4_mb_release_group_pa(struct ext4_buddy
*e4b
,
3603 struct ext4_prealloc_space
*pa
)
3605 struct super_block
*sb
= e4b
->bd_sb
;
3609 trace_ext4_mb_release_group_pa(sb
, pa
);
3610 BUG_ON(pa
->pa_deleted
== 0);
3611 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3612 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3613 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, pa
->pa_len
);
3614 atomic_add(pa
->pa_len
, &EXT4_SB(sb
)->s_mb_discarded
);
3615 trace_ext4_mballoc_discard(sb
, NULL
, group
, bit
, pa
->pa_len
);
3621 * releases all preallocations in given group
3623 * first, we need to decide discard policy:
3624 * - when do we discard
3626 * - how many do we discard
3627 * 1) how many requested
3629 static noinline_for_stack
int
3630 ext4_mb_discard_group_preallocations(struct super_block
*sb
,
3631 ext4_group_t group
, int needed
)
3633 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3634 struct buffer_head
*bitmap_bh
= NULL
;
3635 struct ext4_prealloc_space
*pa
, *tmp
;
3636 struct list_head list
;
3637 struct ext4_buddy e4b
;
3642 mb_debug(1, "discard preallocation for group %u\n", group
);
3644 if (list_empty(&grp
->bb_prealloc_list
))
3647 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
3648 if (bitmap_bh
== NULL
) {
3649 ext4_error(sb
, "Error reading block bitmap for %u", group
);
3653 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
3655 ext4_error(sb
, "Error loading buddy information for %u", group
);
3661 needed
= EXT4_CLUSTERS_PER_GROUP(sb
) + 1;
3663 INIT_LIST_HEAD(&list
);
3665 ext4_lock_group(sb
, group
);
3666 list_for_each_entry_safe(pa
, tmp
,
3667 &grp
->bb_prealloc_list
, pa_group_list
) {
3668 spin_lock(&pa
->pa_lock
);
3669 if (atomic_read(&pa
->pa_count
)) {
3670 spin_unlock(&pa
->pa_lock
);
3674 if (pa
->pa_deleted
) {
3675 spin_unlock(&pa
->pa_lock
);
3679 /* seems this one can be freed ... */
3682 /* we can trust pa_free ... */
3683 free
+= pa
->pa_free
;
3685 spin_unlock(&pa
->pa_lock
);
3687 list_del(&pa
->pa_group_list
);
3688 list_add(&pa
->u
.pa_tmp_list
, &list
);
3691 /* if we still need more blocks and some PAs were used, try again */
3692 if (free
< needed
&& busy
) {
3694 ext4_unlock_group(sb
, group
);
3699 /* found anything to free? */
3700 if (list_empty(&list
)) {
3705 /* now free all selected PAs */
3706 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
3708 /* remove from object (inode or locality group) */
3709 spin_lock(pa
->pa_obj_lock
);
3710 list_del_rcu(&pa
->pa_inode_list
);
3711 spin_unlock(pa
->pa_obj_lock
);
3713 if (pa
->pa_type
== MB_GROUP_PA
)
3714 ext4_mb_release_group_pa(&e4b
, pa
);
3716 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
);
3718 list_del(&pa
->u
.pa_tmp_list
);
3719 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3723 ext4_unlock_group(sb
, group
);
3724 ext4_mb_unload_buddy(&e4b
);
3730 * releases all non-used preallocated blocks for given inode
3732 * It's important to discard preallocations under i_data_sem
3733 * We don't want another block to be served from the prealloc
3734 * space when we are discarding the inode prealloc space.
3736 * FIXME!! Make sure it is valid at all the call sites
3738 void ext4_discard_preallocations(struct inode
*inode
)
3740 struct ext4_inode_info
*ei
= EXT4_I(inode
);
3741 struct super_block
*sb
= inode
->i_sb
;
3742 struct buffer_head
*bitmap_bh
= NULL
;
3743 struct ext4_prealloc_space
*pa
, *tmp
;
3744 ext4_group_t group
= 0;
3745 struct list_head list
;
3746 struct ext4_buddy e4b
;
3749 if (!S_ISREG(inode
->i_mode
)) {
3750 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3754 mb_debug(1, "discard preallocation for inode %lu\n", inode
->i_ino
);
3755 trace_ext4_discard_preallocations(inode
);
3757 INIT_LIST_HEAD(&list
);
3760 /* first, collect all pa's in the inode */
3761 spin_lock(&ei
->i_prealloc_lock
);
3762 while (!list_empty(&ei
->i_prealloc_list
)) {
3763 pa
= list_entry(ei
->i_prealloc_list
.next
,
3764 struct ext4_prealloc_space
, pa_inode_list
);
3765 BUG_ON(pa
->pa_obj_lock
!= &ei
->i_prealloc_lock
);
3766 spin_lock(&pa
->pa_lock
);
3767 if (atomic_read(&pa
->pa_count
)) {
3768 /* this shouldn't happen often - nobody should
3769 * use preallocation while we're discarding it */
3770 spin_unlock(&pa
->pa_lock
);
3771 spin_unlock(&ei
->i_prealloc_lock
);
3772 ext4_msg(sb
, KERN_ERR
,
3773 "uh-oh! used pa while discarding");
3775 schedule_timeout_uninterruptible(HZ
);
3779 if (pa
->pa_deleted
== 0) {
3781 spin_unlock(&pa
->pa_lock
);
3782 list_del_rcu(&pa
->pa_inode_list
);
3783 list_add(&pa
->u
.pa_tmp_list
, &list
);
3787 /* someone is deleting pa right now */
3788 spin_unlock(&pa
->pa_lock
);
3789 spin_unlock(&ei
->i_prealloc_lock
);
3791 /* we have to wait here because pa_deleted
3792 * doesn't mean pa is already unlinked from
3793 * the list. as we might be called from
3794 * ->clear_inode() the inode will get freed
3795 * and concurrent thread which is unlinking
3796 * pa from inode's list may access already
3797 * freed memory, bad-bad-bad */
3799 /* XXX: if this happens too often, we can
3800 * add a flag to force wait only in case
3801 * of ->clear_inode(), but not in case of
3802 * regular truncate */
3803 schedule_timeout_uninterruptible(HZ
);
3806 spin_unlock(&ei
->i_prealloc_lock
);
3808 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
3809 BUG_ON(pa
->pa_type
!= MB_INODE_PA
);
3810 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
3812 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
3814 ext4_error(sb
, "Error loading buddy information for %u",
3819 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
3820 if (bitmap_bh
== NULL
) {
3821 ext4_error(sb
, "Error reading block bitmap for %u",
3823 ext4_mb_unload_buddy(&e4b
);
3827 ext4_lock_group(sb
, group
);
3828 list_del(&pa
->pa_group_list
);
3829 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
);
3830 ext4_unlock_group(sb
, group
);
3832 ext4_mb_unload_buddy(&e4b
);
3835 list_del(&pa
->u
.pa_tmp_list
);
3836 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3840 #ifdef CONFIG_EXT4_DEBUG
3841 static void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
3843 struct super_block
*sb
= ac
->ac_sb
;
3844 ext4_group_t ngroups
, i
;
3846 if (!ext4_mballoc_debug
||
3847 (EXT4_SB(sb
)->s_mount_flags
& EXT4_MF_FS_ABORTED
))
3850 ext4_msg(ac
->ac_sb
, KERN_ERR
, "Can't allocate:"
3851 " Allocation context details:");
3852 ext4_msg(ac
->ac_sb
, KERN_ERR
, "status %d flags %d",
3853 ac
->ac_status
, ac
->ac_flags
);
3854 ext4_msg(ac
->ac_sb
, KERN_ERR
, "orig %lu/%lu/%lu@%lu, "
3855 "goal %lu/%lu/%lu@%lu, "
3856 "best %lu/%lu/%lu@%lu cr %d",
3857 (unsigned long)ac
->ac_o_ex
.fe_group
,
3858 (unsigned long)ac
->ac_o_ex
.fe_start
,
3859 (unsigned long)ac
->ac_o_ex
.fe_len
,
3860 (unsigned long)ac
->ac_o_ex
.fe_logical
,
3861 (unsigned long)ac
->ac_g_ex
.fe_group
,
3862 (unsigned long)ac
->ac_g_ex
.fe_start
,
3863 (unsigned long)ac
->ac_g_ex
.fe_len
,
3864 (unsigned long)ac
->ac_g_ex
.fe_logical
,
3865 (unsigned long)ac
->ac_b_ex
.fe_group
,
3866 (unsigned long)ac
->ac_b_ex
.fe_start
,
3867 (unsigned long)ac
->ac_b_ex
.fe_len
,
3868 (unsigned long)ac
->ac_b_ex
.fe_logical
,
3869 (int)ac
->ac_criteria
);
3870 ext4_msg(ac
->ac_sb
, KERN_ERR
, "%lu scanned, %d found",
3871 ac
->ac_ex_scanned
, ac
->ac_found
);
3872 ext4_msg(ac
->ac_sb
, KERN_ERR
, "groups: ");
3873 ngroups
= ext4_get_groups_count(sb
);
3874 for (i
= 0; i
< ngroups
; i
++) {
3875 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, i
);
3876 struct ext4_prealloc_space
*pa
;
3877 ext4_grpblk_t start
;
3878 struct list_head
*cur
;
3879 ext4_lock_group(sb
, i
);
3880 list_for_each(cur
, &grp
->bb_prealloc_list
) {
3881 pa
= list_entry(cur
, struct ext4_prealloc_space
,
3883 spin_lock(&pa
->pa_lock
);
3884 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
3886 spin_unlock(&pa
->pa_lock
);
3887 printk(KERN_ERR
"PA:%u:%d:%u \n", i
,
3890 ext4_unlock_group(sb
, i
);
3892 if (grp
->bb_free
== 0)
3894 printk(KERN_ERR
"%u: %d/%d \n",
3895 i
, grp
->bb_free
, grp
->bb_fragments
);
3897 printk(KERN_ERR
"\n");
3900 static inline void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
3907 * We use locality group preallocation for small size file. The size of the
3908 * file is determined by the current size or the resulting size after
3909 * allocation which ever is larger
3911 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
3913 static void ext4_mb_group_or_file(struct ext4_allocation_context
*ac
)
3915 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3916 int bsbits
= ac
->ac_sb
->s_blocksize_bits
;
3919 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3922 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
3925 size
= ac
->ac_o_ex
.fe_logical
+ EXT4_C2B(sbi
, ac
->ac_o_ex
.fe_len
);
3926 isize
= (i_size_read(ac
->ac_inode
) + ac
->ac_sb
->s_blocksize
- 1)
3929 if ((size
== isize
) &&
3930 !ext4_fs_is_busy(sbi
) &&
3931 (atomic_read(&ac
->ac_inode
->i_writecount
) == 0)) {
3932 ac
->ac_flags
|= EXT4_MB_HINT_NOPREALLOC
;
3936 if (sbi
->s_mb_group_prealloc
<= 0) {
3937 ac
->ac_flags
|= EXT4_MB_STREAM_ALLOC
;
3941 /* don't use group allocation for large files */
3942 size
= max(size
, isize
);
3943 if (size
> sbi
->s_mb_stream_request
) {
3944 ac
->ac_flags
|= EXT4_MB_STREAM_ALLOC
;
3948 BUG_ON(ac
->ac_lg
!= NULL
);
3950 * locality group prealloc space are per cpu. The reason for having
3951 * per cpu locality group is to reduce the contention between block
3952 * request from multiple CPUs.
3954 ac
->ac_lg
= __this_cpu_ptr(sbi
->s_locality_groups
);
3956 /* we're going to use group allocation */
3957 ac
->ac_flags
|= EXT4_MB_HINT_GROUP_ALLOC
;
3959 /* serialize all allocations in the group */
3960 mutex_lock(&ac
->ac_lg
->lg_mutex
);
3963 static noinline_for_stack
int
3964 ext4_mb_initialize_context(struct ext4_allocation_context
*ac
,
3965 struct ext4_allocation_request
*ar
)
3967 struct super_block
*sb
= ar
->inode
->i_sb
;
3968 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3969 struct ext4_super_block
*es
= sbi
->s_es
;
3973 ext4_grpblk_t block
;
3975 /* we can't allocate > group size */
3978 /* just a dirty hack to filter too big requests */
3979 if (len
>= EXT4_CLUSTERS_PER_GROUP(sb
))
3980 len
= EXT4_CLUSTERS_PER_GROUP(sb
);
3982 /* start searching from the goal */
3984 if (goal
< le32_to_cpu(es
->s_first_data_block
) ||
3985 goal
>= ext4_blocks_count(es
))
3986 goal
= le32_to_cpu(es
->s_first_data_block
);
3987 ext4_get_group_no_and_offset(sb
, goal
, &group
, &block
);
3989 /* set up allocation goals */
3990 ac
->ac_b_ex
.fe_logical
= ar
->logical
& ~(sbi
->s_cluster_ratio
- 1);
3991 ac
->ac_status
= AC_STATUS_CONTINUE
;
3993 ac
->ac_inode
= ar
->inode
;
3994 ac
->ac_o_ex
.fe_logical
= ac
->ac_b_ex
.fe_logical
;
3995 ac
->ac_o_ex
.fe_group
= group
;
3996 ac
->ac_o_ex
.fe_start
= block
;
3997 ac
->ac_o_ex
.fe_len
= len
;
3998 ac
->ac_g_ex
= ac
->ac_o_ex
;
3999 ac
->ac_flags
= ar
->flags
;
4001 /* we have to define context: we'll we work with a file or
4002 * locality group. this is a policy, actually */
4003 ext4_mb_group_or_file(ac
);
4005 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
4006 "left: %u/%u, right %u/%u to %swritable\n",
4007 (unsigned) ar
->len
, (unsigned) ar
->logical
,
4008 (unsigned) ar
->goal
, ac
->ac_flags
, ac
->ac_2order
,
4009 (unsigned) ar
->lleft
, (unsigned) ar
->pleft
,
4010 (unsigned) ar
->lright
, (unsigned) ar
->pright
,
4011 atomic_read(&ar
->inode
->i_writecount
) ? "" : "non-");
4016 static noinline_for_stack
void
4017 ext4_mb_discard_lg_preallocations(struct super_block
*sb
,
4018 struct ext4_locality_group
*lg
,
4019 int order
, int total_entries
)
4021 ext4_group_t group
= 0;
4022 struct ext4_buddy e4b
;
4023 struct list_head discard_list
;
4024 struct ext4_prealloc_space
*pa
, *tmp
;
4026 mb_debug(1, "discard locality group preallocation\n");
4028 INIT_LIST_HEAD(&discard_list
);
4030 spin_lock(&lg
->lg_prealloc_lock
);
4031 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[order
],
4033 spin_lock(&pa
->pa_lock
);
4034 if (atomic_read(&pa
->pa_count
)) {
4036 * This is the pa that we just used
4037 * for block allocation. So don't
4040 spin_unlock(&pa
->pa_lock
);
4043 if (pa
->pa_deleted
) {
4044 spin_unlock(&pa
->pa_lock
);
4047 /* only lg prealloc space */
4048 BUG_ON(pa
->pa_type
!= MB_GROUP_PA
);
4050 /* seems this one can be freed ... */
4052 spin_unlock(&pa
->pa_lock
);
4054 list_del_rcu(&pa
->pa_inode_list
);
4055 list_add(&pa
->u
.pa_tmp_list
, &discard_list
);
4058 if (total_entries
<= 5) {
4060 * we want to keep only 5 entries
4061 * allowing it to grow to 8. This
4062 * mak sure we don't call discard
4063 * soon for this list.
4068 spin_unlock(&lg
->lg_prealloc_lock
);
4070 list_for_each_entry_safe(pa
, tmp
, &discard_list
, u
.pa_tmp_list
) {
4072 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
4073 if (ext4_mb_load_buddy(sb
, group
, &e4b
)) {
4074 ext4_error(sb
, "Error loading buddy information for %u",
4078 ext4_lock_group(sb
, group
);
4079 list_del(&pa
->pa_group_list
);
4080 ext4_mb_release_group_pa(&e4b
, pa
);
4081 ext4_unlock_group(sb
, group
);
4083 ext4_mb_unload_buddy(&e4b
);
4084 list_del(&pa
->u
.pa_tmp_list
);
4085 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4090 * We have incremented pa_count. So it cannot be freed at this
4091 * point. Also we hold lg_mutex. So no parallel allocation is
4092 * possible from this lg. That means pa_free cannot be updated.
4094 * A parallel ext4_mb_discard_group_preallocations is possible.
4095 * which can cause the lg_prealloc_list to be updated.
4098 static void ext4_mb_add_n_trim(struct ext4_allocation_context
*ac
)
4100 int order
, added
= 0, lg_prealloc_count
= 1;
4101 struct super_block
*sb
= ac
->ac_sb
;
4102 struct ext4_locality_group
*lg
= ac
->ac_lg
;
4103 struct ext4_prealloc_space
*tmp_pa
, *pa
= ac
->ac_pa
;
4105 order
= fls(pa
->pa_free
) - 1;
4106 if (order
> PREALLOC_TB_SIZE
- 1)
4107 /* The max size of hash table is PREALLOC_TB_SIZE */
4108 order
= PREALLOC_TB_SIZE
- 1;
4109 /* Add the prealloc space to lg */
4110 spin_lock(&lg
->lg_prealloc_lock
);
4111 list_for_each_entry_rcu(tmp_pa
, &lg
->lg_prealloc_list
[order
],
4113 spin_lock(&tmp_pa
->pa_lock
);
4114 if (tmp_pa
->pa_deleted
) {
4115 spin_unlock(&tmp_pa
->pa_lock
);
4118 if (!added
&& pa
->pa_free
< tmp_pa
->pa_free
) {
4119 /* Add to the tail of the previous entry */
4120 list_add_tail_rcu(&pa
->pa_inode_list
,
4121 &tmp_pa
->pa_inode_list
);
4124 * we want to count the total
4125 * number of entries in the list
4128 spin_unlock(&tmp_pa
->pa_lock
);
4129 lg_prealloc_count
++;
4132 list_add_tail_rcu(&pa
->pa_inode_list
,
4133 &lg
->lg_prealloc_list
[order
]);
4134 spin_unlock(&lg
->lg_prealloc_lock
);
4136 /* Now trim the list to be not more than 8 elements */
4137 if (lg_prealloc_count
> 8) {
4138 ext4_mb_discard_lg_preallocations(sb
, lg
,
4139 order
, lg_prealloc_count
);
4146 * release all resource we used in allocation
4148 static int ext4_mb_release_context(struct ext4_allocation_context
*ac
)
4150 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
4151 struct ext4_prealloc_space
*pa
= ac
->ac_pa
;
4153 if (pa
->pa_type
== MB_GROUP_PA
) {
4154 /* see comment in ext4_mb_use_group_pa() */
4155 spin_lock(&pa
->pa_lock
);
4156 pa
->pa_pstart
+= EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
);
4157 pa
->pa_lstart
+= EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
);
4158 pa
->pa_free
-= ac
->ac_b_ex
.fe_len
;
4159 pa
->pa_len
-= ac
->ac_b_ex
.fe_len
;
4160 spin_unlock(&pa
->pa_lock
);
4165 * We want to add the pa to the right bucket.
4166 * Remove it from the list and while adding
4167 * make sure the list to which we are adding
4170 if ((pa
->pa_type
== MB_GROUP_PA
) && likely(pa
->pa_free
)) {
4171 spin_lock(pa
->pa_obj_lock
);
4172 list_del_rcu(&pa
->pa_inode_list
);
4173 spin_unlock(pa
->pa_obj_lock
);
4174 ext4_mb_add_n_trim(ac
);
4176 ext4_mb_put_pa(ac
, ac
->ac_sb
, pa
);
4178 if (ac
->ac_bitmap_page
)
4179 page_cache_release(ac
->ac_bitmap_page
);
4180 if (ac
->ac_buddy_page
)
4181 page_cache_release(ac
->ac_buddy_page
);
4182 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
4183 mutex_unlock(&ac
->ac_lg
->lg_mutex
);
4184 ext4_mb_collect_stats(ac
);
4188 static int ext4_mb_discard_preallocations(struct super_block
*sb
, int needed
)
4190 ext4_group_t i
, ngroups
= ext4_get_groups_count(sb
);
4194 trace_ext4_mb_discard_preallocations(sb
, needed
);
4195 for (i
= 0; i
< ngroups
&& needed
> 0; i
++) {
4196 ret
= ext4_mb_discard_group_preallocations(sb
, i
, needed
);
4205 * Main entry point into mballoc to allocate blocks
4206 * it tries to use preallocation first, then falls back
4207 * to usual allocation
4209 ext4_fsblk_t
ext4_mb_new_blocks(handle_t
*handle
,
4210 struct ext4_allocation_request
*ar
, int *errp
)
4213 struct ext4_allocation_context
*ac
= NULL
;
4214 struct ext4_sb_info
*sbi
;
4215 struct super_block
*sb
;
4216 ext4_fsblk_t block
= 0;
4217 unsigned int inquota
= 0;
4218 unsigned int reserv_clstrs
= 0;
4220 sb
= ar
->inode
->i_sb
;
4223 trace_ext4_request_blocks(ar
);
4225 /* Allow to use superuser reservation for quota file */
4226 if (IS_NOQUOTA(ar
->inode
))
4227 ar
->flags
|= EXT4_MB_USE_ROOT_BLOCKS
;
4230 * For delayed allocation, we could skip the ENOSPC and
4231 * EDQUOT check, as blocks and quotas have been already
4232 * reserved when data being copied into pagecache.
4234 if (ext4_test_inode_state(ar
->inode
, EXT4_STATE_DELALLOC_RESERVED
))
4235 ar
->flags
|= EXT4_MB_DELALLOC_RESERVED
;
4237 /* Without delayed allocation we need to verify
4238 * there is enough free blocks to do block allocation
4239 * and verify allocation doesn't exceed the quota limits.
4242 ext4_claim_free_clusters(sbi
, ar
->len
, ar
->flags
)) {
4244 /* let others to free the space */
4246 ar
->len
= ar
->len
>> 1;
4252 reserv_clstrs
= ar
->len
;
4253 if (ar
->flags
& EXT4_MB_USE_ROOT_BLOCKS
) {
4254 dquot_alloc_block_nofail(ar
->inode
,
4255 EXT4_C2B(sbi
, ar
->len
));
4258 dquot_alloc_block(ar
->inode
,
4259 EXT4_C2B(sbi
, ar
->len
))) {
4261 ar
->flags
|= EXT4_MB_HINT_NOPREALLOC
;
4272 ac
= kmem_cache_zalloc(ext4_ac_cachep
, GFP_NOFS
);
4279 *errp
= ext4_mb_initialize_context(ac
, ar
);
4285 ac
->ac_op
= EXT4_MB_HISTORY_PREALLOC
;
4286 if (!ext4_mb_use_preallocated(ac
)) {
4287 ac
->ac_op
= EXT4_MB_HISTORY_ALLOC
;
4288 ext4_mb_normalize_request(ac
, ar
);
4290 /* allocate space in core */
4291 *errp
= ext4_mb_regular_allocator(ac
);
4293 ext4_discard_allocated_blocks(ac
);
4297 /* as we've just preallocated more space than
4298 * user requested orinally, we store allocated
4299 * space in a special descriptor */
4300 if (ac
->ac_status
== AC_STATUS_FOUND
&&
4301 ac
->ac_o_ex
.fe_len
< ac
->ac_b_ex
.fe_len
)
4302 ext4_mb_new_preallocation(ac
);
4304 if (likely(ac
->ac_status
== AC_STATUS_FOUND
)) {
4305 *errp
= ext4_mb_mark_diskspace_used(ac
, handle
, reserv_clstrs
);
4306 if (*errp
== -EAGAIN
) {
4308 * drop the reference that we took
4309 * in ext4_mb_use_best_found
4311 ext4_mb_release_context(ac
);
4312 ac
->ac_b_ex
.fe_group
= 0;
4313 ac
->ac_b_ex
.fe_start
= 0;
4314 ac
->ac_b_ex
.fe_len
= 0;
4315 ac
->ac_status
= AC_STATUS_CONTINUE
;
4318 ext4_discard_allocated_blocks(ac
);
4321 block
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
4322 ar
->len
= ac
->ac_b_ex
.fe_len
;
4325 freed
= ext4_mb_discard_preallocations(sb
, ac
->ac_o_ex
.fe_len
);
4333 ac
->ac_b_ex
.fe_len
= 0;
4335 ext4_mb_show_ac(ac
);
4337 ext4_mb_release_context(ac
);
4340 kmem_cache_free(ext4_ac_cachep
, ac
);
4341 if (inquota
&& ar
->len
< inquota
)
4342 dquot_free_block(ar
->inode
, EXT4_C2B(sbi
, inquota
- ar
->len
));
4344 if (!ext4_test_inode_state(ar
->inode
,
4345 EXT4_STATE_DELALLOC_RESERVED
))
4346 /* release all the reserved blocks if non delalloc */
4347 percpu_counter_sub(&sbi
->s_dirtyclusters_counter
,
4351 trace_ext4_allocate_blocks(ar
, (unsigned long long)block
);
4357 * We can merge two free data extents only if the physical blocks
4358 * are contiguous, AND the extents were freed by the same transaction,
4359 * AND the blocks are associated with the same group.
4361 static int can_merge(struct ext4_free_data
*entry1
,
4362 struct ext4_free_data
*entry2
)
4364 if ((entry1
->efd_tid
== entry2
->efd_tid
) &&
4365 (entry1
->efd_group
== entry2
->efd_group
) &&
4366 ((entry1
->efd_start_cluster
+ entry1
->efd_count
) == entry2
->efd_start_cluster
))
4371 static noinline_for_stack
int
4372 ext4_mb_free_metadata(handle_t
*handle
, struct ext4_buddy
*e4b
,
4373 struct ext4_free_data
*new_entry
)
4375 ext4_group_t group
= e4b
->bd_group
;
4376 ext4_grpblk_t cluster
;
4377 struct ext4_free_data
*entry
;
4378 struct ext4_group_info
*db
= e4b
->bd_info
;
4379 struct super_block
*sb
= e4b
->bd_sb
;
4380 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4381 struct rb_node
**n
= &db
->bb_free_root
.rb_node
, *node
;
4382 struct rb_node
*parent
= NULL
, *new_node
;
4384 BUG_ON(!ext4_handle_valid(handle
));
4385 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
4386 BUG_ON(e4b
->bd_buddy_page
== NULL
);
4388 new_node
= &new_entry
->efd_node
;
4389 cluster
= new_entry
->efd_start_cluster
;
4392 /* first free block exent. We need to
4393 protect buddy cache from being freed,
4394 * otherwise we'll refresh it from
4395 * on-disk bitmap and lose not-yet-available
4397 page_cache_get(e4b
->bd_buddy_page
);
4398 page_cache_get(e4b
->bd_bitmap_page
);
4402 entry
= rb_entry(parent
, struct ext4_free_data
, efd_node
);
4403 if (cluster
< entry
->efd_start_cluster
)
4405 else if (cluster
>= (entry
->efd_start_cluster
+ entry
->efd_count
))
4406 n
= &(*n
)->rb_right
;
4408 ext4_grp_locked_error(sb
, group
, 0,
4409 ext4_group_first_block_no(sb
, group
) +
4410 EXT4_C2B(sbi
, cluster
),
4411 "Block already on to-be-freed list");
4416 rb_link_node(new_node
, parent
, n
);
4417 rb_insert_color(new_node
, &db
->bb_free_root
);
4419 /* Now try to see the extent can be merged to left and right */
4420 node
= rb_prev(new_node
);
4422 entry
= rb_entry(node
, struct ext4_free_data
, efd_node
);
4423 if (can_merge(entry
, new_entry
)) {
4424 new_entry
->efd_start_cluster
= entry
->efd_start_cluster
;
4425 new_entry
->efd_count
+= entry
->efd_count
;
4426 rb_erase(node
, &(db
->bb_free_root
));
4427 ext4_journal_callback_del(handle
, &entry
->efd_jce
);
4428 kmem_cache_free(ext4_free_data_cachep
, entry
);
4432 node
= rb_next(new_node
);
4434 entry
= rb_entry(node
, struct ext4_free_data
, efd_node
);
4435 if (can_merge(new_entry
, entry
)) {
4436 new_entry
->efd_count
+= entry
->efd_count
;
4437 rb_erase(node
, &(db
->bb_free_root
));
4438 ext4_journal_callback_del(handle
, &entry
->efd_jce
);
4439 kmem_cache_free(ext4_free_data_cachep
, entry
);
4442 /* Add the extent to transaction's private list */
4443 ext4_journal_callback_add(handle
, ext4_free_data_callback
,
4444 &new_entry
->efd_jce
);
4449 * ext4_free_blocks() -- Free given blocks and update quota
4450 * @handle: handle for this transaction
4452 * @block: start physical block to free
4453 * @count: number of blocks to count
4454 * @flags: flags used by ext4_free_blocks
4456 void ext4_free_blocks(handle_t
*handle
, struct inode
*inode
,
4457 struct buffer_head
*bh
, ext4_fsblk_t block
,
4458 unsigned long count
, int flags
)
4460 struct buffer_head
*bitmap_bh
= NULL
;
4461 struct super_block
*sb
= inode
->i_sb
;
4462 struct ext4_group_desc
*gdp
;
4463 unsigned int overflow
;
4465 struct buffer_head
*gd_bh
;
4466 ext4_group_t block_group
;
4467 struct ext4_sb_info
*sbi
;
4468 struct ext4_buddy e4b
;
4469 unsigned int count_clusters
;
4475 BUG_ON(block
!= bh
->b_blocknr
);
4477 block
= bh
->b_blocknr
;
4481 if (!(flags
& EXT4_FREE_BLOCKS_VALIDATED
) &&
4482 !ext4_data_block_valid(sbi
, block
, count
)) {
4483 ext4_error(sb
, "Freeing blocks not in datazone - "
4484 "block = %llu, count = %lu", block
, count
);
4488 ext4_debug("freeing block %llu\n", block
);
4489 trace_ext4_free_blocks(inode
, block
, count
, flags
);
4491 if (flags
& EXT4_FREE_BLOCKS_FORGET
) {
4492 struct buffer_head
*tbh
= bh
;
4495 BUG_ON(bh
&& (count
> 1));
4497 for (i
= 0; i
< count
; i
++) {
4499 tbh
= sb_find_get_block(inode
->i_sb
,
4503 ext4_forget(handle
, flags
& EXT4_FREE_BLOCKS_METADATA
,
4504 inode
, tbh
, block
+ i
);
4509 * We need to make sure we don't reuse the freed block until
4510 * after the transaction is committed, which we can do by
4511 * treating the block as metadata, below. We make an
4512 * exception if the inode is to be written in writeback mode
4513 * since writeback mode has weak data consistency guarantees.
4515 if (!ext4_should_writeback_data(inode
))
4516 flags
|= EXT4_FREE_BLOCKS_METADATA
;
4519 * If the extent to be freed does not begin on a cluster
4520 * boundary, we need to deal with partial clusters at the
4521 * beginning and end of the extent. Normally we will free
4522 * blocks at the beginning or the end unless we are explicitly
4523 * requested to avoid doing so.
4525 overflow
= block
& (sbi
->s_cluster_ratio
- 1);
4527 if (flags
& EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER
) {
4528 overflow
= sbi
->s_cluster_ratio
- overflow
;
4530 if (count
> overflow
)
4539 overflow
= count
& (sbi
->s_cluster_ratio
- 1);
4541 if (flags
& EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER
) {
4542 if (count
> overflow
)
4547 count
+= sbi
->s_cluster_ratio
- overflow
;
4552 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
4555 * Check to see if we are freeing blocks across a group
4558 if (EXT4_C2B(sbi
, bit
) + count
> EXT4_BLOCKS_PER_GROUP(sb
)) {
4559 overflow
= EXT4_C2B(sbi
, bit
) + count
-
4560 EXT4_BLOCKS_PER_GROUP(sb
);
4563 count_clusters
= EXT4_NUM_B2C(sbi
, count
);
4564 bitmap_bh
= ext4_read_block_bitmap(sb
, block_group
);
4569 gdp
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
4575 if (in_range(ext4_block_bitmap(sb
, gdp
), block
, count
) ||
4576 in_range(ext4_inode_bitmap(sb
, gdp
), block
, count
) ||
4577 in_range(block
, ext4_inode_table(sb
, gdp
),
4578 EXT4_SB(sb
)->s_itb_per_group
) ||
4579 in_range(block
+ count
- 1, ext4_inode_table(sb
, gdp
),
4580 EXT4_SB(sb
)->s_itb_per_group
)) {
4582 ext4_error(sb
, "Freeing blocks in system zone - "
4583 "Block = %llu, count = %lu", block
, count
);
4584 /* err = 0. ext4_std_error should be a no op */
4588 BUFFER_TRACE(bitmap_bh
, "getting write access");
4589 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
4594 * We are about to modify some metadata. Call the journal APIs
4595 * to unshare ->b_data if a currently-committing transaction is
4598 BUFFER_TRACE(gd_bh
, "get_write_access");
4599 err
= ext4_journal_get_write_access(handle
, gd_bh
);
4602 #ifdef AGGRESSIVE_CHECK
4605 for (i
= 0; i
< count_clusters
; i
++)
4606 BUG_ON(!mb_test_bit(bit
+ i
, bitmap_bh
->b_data
));
4609 trace_ext4_mballoc_free(sb
, inode
, block_group
, bit
, count_clusters
);
4611 err
= ext4_mb_load_buddy(sb
, block_group
, &e4b
);
4615 if ((flags
& EXT4_FREE_BLOCKS_METADATA
) && ext4_handle_valid(handle
)) {
4616 struct ext4_free_data
*new_entry
;
4618 * blocks being freed are metadata. these blocks shouldn't
4619 * be used until this transaction is committed
4621 new_entry
= kmem_cache_alloc(ext4_free_data_cachep
, GFP_NOFS
);
4623 ext4_mb_unload_buddy(&e4b
);
4627 new_entry
->efd_start_cluster
= bit
;
4628 new_entry
->efd_group
= block_group
;
4629 new_entry
->efd_count
= count_clusters
;
4630 new_entry
->efd_tid
= handle
->h_transaction
->t_tid
;
4632 ext4_lock_group(sb
, block_group
);
4633 mb_clear_bits(bitmap_bh
->b_data
, bit
, count_clusters
);
4634 ext4_mb_free_metadata(handle
, &e4b
, new_entry
);
4636 /* need to update group_info->bb_free and bitmap
4637 * with group lock held. generate_buddy look at
4638 * them with group lock_held
4640 if (test_opt(sb
, DISCARD
)) {
4641 err
= ext4_issue_discard(sb
, block_group
, bit
, count
);
4642 if (err
&& err
!= -EOPNOTSUPP
)
4643 ext4_msg(sb
, KERN_WARNING
, "discard request in"
4644 " group:%d block:%d count:%lu failed"
4645 " with %d", block_group
, bit
, count
,
4650 ext4_lock_group(sb
, block_group
);
4651 mb_clear_bits(bitmap_bh
->b_data
, bit
, count_clusters
);
4652 mb_free_blocks(inode
, &e4b
, bit
, count_clusters
);
4655 ret
= ext4_free_group_clusters(sb
, gdp
) + count_clusters
;
4656 ext4_free_group_clusters_set(sb
, gdp
, ret
);
4657 ext4_block_bitmap_csum_set(sb
, block_group
, gdp
, bitmap_bh
);
4658 ext4_group_desc_csum_set(sb
, block_group
, gdp
);
4659 ext4_unlock_group(sb
, block_group
);
4660 percpu_counter_add(&sbi
->s_freeclusters_counter
, count_clusters
);
4662 if (sbi
->s_log_groups_per_flex
) {
4663 ext4_group_t flex_group
= ext4_flex_group(sbi
, block_group
);
4664 atomic64_add(count_clusters
,
4665 &sbi
->s_flex_groups
[flex_group
].free_clusters
);
4668 ext4_mb_unload_buddy(&e4b
);
4670 if (!(flags
& EXT4_FREE_BLOCKS_NO_QUOT_UPDATE
))
4671 dquot_free_block(inode
, EXT4_C2B(sbi
, count_clusters
));
4673 /* We dirtied the bitmap block */
4674 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
4675 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
4677 /* And the group descriptor block */
4678 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
4679 ret
= ext4_handle_dirty_metadata(handle
, NULL
, gd_bh
);
4683 if (overflow
&& !err
) {
4691 ext4_std_error(sb
, err
);
4696 * ext4_group_add_blocks() -- Add given blocks to an existing group
4697 * @handle: handle to this transaction
4699 * @block: start physical block to add to the block group
4700 * @count: number of blocks to free
4702 * This marks the blocks as free in the bitmap and buddy.
4704 int ext4_group_add_blocks(handle_t
*handle
, struct super_block
*sb
,
4705 ext4_fsblk_t block
, unsigned long count
)
4707 struct buffer_head
*bitmap_bh
= NULL
;
4708 struct buffer_head
*gd_bh
;
4709 ext4_group_t block_group
;
4712 struct ext4_group_desc
*desc
;
4713 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4714 struct ext4_buddy e4b
;
4715 int err
= 0, ret
, blk_free_count
;
4716 ext4_grpblk_t blocks_freed
;
4718 ext4_debug("Adding block(s) %llu-%llu\n", block
, block
+ count
- 1);
4723 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
4725 * Check to see if we are freeing blocks across a group
4728 if (bit
+ count
> EXT4_BLOCKS_PER_GROUP(sb
)) {
4729 ext4_warning(sb
, "too much blocks added to group %u\n",
4735 bitmap_bh
= ext4_read_block_bitmap(sb
, block_group
);
4741 desc
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
4747 if (in_range(ext4_block_bitmap(sb
, desc
), block
, count
) ||
4748 in_range(ext4_inode_bitmap(sb
, desc
), block
, count
) ||
4749 in_range(block
, ext4_inode_table(sb
, desc
), sbi
->s_itb_per_group
) ||
4750 in_range(block
+ count
- 1, ext4_inode_table(sb
, desc
),
4751 sbi
->s_itb_per_group
)) {
4752 ext4_error(sb
, "Adding blocks in system zones - "
4753 "Block = %llu, count = %lu",
4759 BUFFER_TRACE(bitmap_bh
, "getting write access");
4760 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
4765 * We are about to modify some metadata. Call the journal APIs
4766 * to unshare ->b_data if a currently-committing transaction is
4769 BUFFER_TRACE(gd_bh
, "get_write_access");
4770 err
= ext4_journal_get_write_access(handle
, gd_bh
);
4774 for (i
= 0, blocks_freed
= 0; i
< count
; i
++) {
4775 BUFFER_TRACE(bitmap_bh
, "clear bit");
4776 if (!mb_test_bit(bit
+ i
, bitmap_bh
->b_data
)) {
4777 ext4_error(sb
, "bit already cleared for block %llu",
4778 (ext4_fsblk_t
)(block
+ i
));
4779 BUFFER_TRACE(bitmap_bh
, "bit already cleared");
4785 err
= ext4_mb_load_buddy(sb
, block_group
, &e4b
);
4790 * need to update group_info->bb_free and bitmap
4791 * with group lock held. generate_buddy look at
4792 * them with group lock_held
4794 ext4_lock_group(sb
, block_group
);
4795 mb_clear_bits(bitmap_bh
->b_data
, bit
, count
);
4796 mb_free_blocks(NULL
, &e4b
, bit
, count
);
4797 blk_free_count
= blocks_freed
+ ext4_free_group_clusters(sb
, desc
);
4798 ext4_free_group_clusters_set(sb
, desc
, blk_free_count
);
4799 ext4_block_bitmap_csum_set(sb
, block_group
, desc
, bitmap_bh
);
4800 ext4_group_desc_csum_set(sb
, block_group
, desc
);
4801 ext4_unlock_group(sb
, block_group
);
4802 percpu_counter_add(&sbi
->s_freeclusters_counter
,
4803 EXT4_NUM_B2C(sbi
, blocks_freed
));
4805 if (sbi
->s_log_groups_per_flex
) {
4806 ext4_group_t flex_group
= ext4_flex_group(sbi
, block_group
);
4807 atomic64_add(EXT4_NUM_B2C(sbi
, blocks_freed
),
4808 &sbi
->s_flex_groups
[flex_group
].free_clusters
);
4811 ext4_mb_unload_buddy(&e4b
);
4813 /* We dirtied the bitmap block */
4814 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
4815 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
4817 /* And the group descriptor block */
4818 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
4819 ret
= ext4_handle_dirty_metadata(handle
, NULL
, gd_bh
);
4825 ext4_std_error(sb
, err
);
4830 * ext4_trim_extent -- function to TRIM one single free extent in the group
4831 * @sb: super block for the file system
4832 * @start: starting block of the free extent in the alloc. group
4833 * @count: number of blocks to TRIM
4834 * @group: alloc. group we are working with
4835 * @e4b: ext4 buddy for the group
4837 * Trim "count" blocks starting at "start" in the "group". To assure that no
4838 * one will allocate those blocks, mark it as used in buddy bitmap. This must
4839 * be called with under the group lock.
4841 static int ext4_trim_extent(struct super_block
*sb
, int start
, int count
,
4842 ext4_group_t group
, struct ext4_buddy
*e4b
)
4844 struct ext4_free_extent ex
;
4847 trace_ext4_trim_extent(sb
, group
, start
, count
);
4849 assert_spin_locked(ext4_group_lock_ptr(sb
, group
));
4851 ex
.fe_start
= start
;
4852 ex
.fe_group
= group
;
4856 * Mark blocks used, so no one can reuse them while
4859 mb_mark_used(e4b
, &ex
);
4860 ext4_unlock_group(sb
, group
);
4861 ret
= ext4_issue_discard(sb
, group
, start
, count
);
4862 ext4_lock_group(sb
, group
);
4863 mb_free_blocks(NULL
, e4b
, start
, ex
.fe_len
);
4868 * ext4_trim_all_free -- function to trim all free space in alloc. group
4869 * @sb: super block for file system
4870 * @group: group to be trimmed
4871 * @start: first group block to examine
4872 * @max: last group block to examine
4873 * @minblocks: minimum extent block count
4875 * ext4_trim_all_free walks through group's buddy bitmap searching for free
4876 * extents. When the free block is found, ext4_trim_extent is called to TRIM
4880 * ext4_trim_all_free walks through group's block bitmap searching for free
4881 * extents. When the free extent is found, mark it as used in group buddy
4882 * bitmap. Then issue a TRIM command on this extent and free the extent in
4883 * the group buddy bitmap. This is done until whole group is scanned.
4885 static ext4_grpblk_t
4886 ext4_trim_all_free(struct super_block
*sb
, ext4_group_t group
,
4887 ext4_grpblk_t start
, ext4_grpblk_t max
,
4888 ext4_grpblk_t minblocks
)
4891 ext4_grpblk_t next
, count
= 0, free_count
= 0;
4892 struct ext4_buddy e4b
;
4895 trace_ext4_trim_all_free(sb
, group
, start
, max
);
4897 ret
= ext4_mb_load_buddy(sb
, group
, &e4b
);
4899 ext4_error(sb
, "Error in loading buddy "
4900 "information for %u", group
);
4903 bitmap
= e4b
.bd_bitmap
;
4905 ext4_lock_group(sb
, group
);
4906 if (EXT4_MB_GRP_WAS_TRIMMED(e4b
.bd_info
) &&
4907 minblocks
>= atomic_read(&EXT4_SB(sb
)->s_last_trim_minblks
))
4910 start
= (e4b
.bd_info
->bb_first_free
> start
) ?
4911 e4b
.bd_info
->bb_first_free
: start
;
4913 while (start
<= max
) {
4914 start
= mb_find_next_zero_bit(bitmap
, max
+ 1, start
);
4917 next
= mb_find_next_bit(bitmap
, max
+ 1, start
);
4919 if ((next
- start
) >= minblocks
) {
4920 ret
= ext4_trim_extent(sb
, start
,
4921 next
- start
, group
, &e4b
);
4922 if (ret
&& ret
!= -EOPNOTSUPP
)
4925 count
+= next
- start
;
4927 free_count
+= next
- start
;
4930 if (fatal_signal_pending(current
)) {
4931 count
= -ERESTARTSYS
;
4935 if (need_resched()) {
4936 ext4_unlock_group(sb
, group
);
4938 ext4_lock_group(sb
, group
);
4941 if ((e4b
.bd_info
->bb_free
- free_count
) < minblocks
)
4947 EXT4_MB_GRP_SET_TRIMMED(e4b
.bd_info
);
4950 ext4_unlock_group(sb
, group
);
4951 ext4_mb_unload_buddy(&e4b
);
4953 ext4_debug("trimmed %d blocks in the group %d\n",
4960 * ext4_trim_fs() -- trim ioctl handle function
4961 * @sb: superblock for filesystem
4962 * @range: fstrim_range structure
4964 * start: First Byte to trim
4965 * len: number of Bytes to trim from start
4966 * minlen: minimum extent length in Bytes
4967 * ext4_trim_fs goes through all allocation groups containing Bytes from
4968 * start to start+len. For each such a group ext4_trim_all_free function
4969 * is invoked to trim all free space.
4971 int ext4_trim_fs(struct super_block
*sb
, struct fstrim_range
*range
)
4973 struct ext4_group_info
*grp
;
4974 ext4_group_t group
, first_group
, last_group
;
4975 ext4_grpblk_t cnt
= 0, first_cluster
, last_cluster
;
4976 uint64_t start
, end
, minlen
, trimmed
= 0;
4977 ext4_fsblk_t first_data_blk
=
4978 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
4979 ext4_fsblk_t max_blks
= ext4_blocks_count(EXT4_SB(sb
)->s_es
);
4982 start
= range
->start
>> sb
->s_blocksize_bits
;
4983 end
= start
+ (range
->len
>> sb
->s_blocksize_bits
) - 1;
4984 minlen
= EXT4_NUM_B2C(EXT4_SB(sb
),
4985 range
->minlen
>> sb
->s_blocksize_bits
);
4987 if (minlen
> EXT4_CLUSTERS_PER_GROUP(sb
) ||
4988 start
>= max_blks
||
4989 range
->len
< sb
->s_blocksize
)
4991 if (end
>= max_blks
)
4993 if (end
<= first_data_blk
)
4995 if (start
< first_data_blk
)
4996 start
= first_data_blk
;
4998 /* Determine first and last group to examine based on start and end */
4999 ext4_get_group_no_and_offset(sb
, (ext4_fsblk_t
) start
,
5000 &first_group
, &first_cluster
);
5001 ext4_get_group_no_and_offset(sb
, (ext4_fsblk_t
) end
,
5002 &last_group
, &last_cluster
);
5004 /* end now represents the last cluster to discard in this group */
5005 end
= EXT4_CLUSTERS_PER_GROUP(sb
) - 1;
5007 for (group
= first_group
; group
<= last_group
; group
++) {
5008 grp
= ext4_get_group_info(sb
, group
);
5009 /* We only do this if the grp has never been initialized */
5010 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp
))) {
5011 ret
= ext4_mb_init_group(sb
, group
);
5017 * For all the groups except the last one, last cluster will
5018 * always be EXT4_CLUSTERS_PER_GROUP(sb)-1, so we only need to
5019 * change it for the last group, note that last_cluster is
5020 * already computed earlier by ext4_get_group_no_and_offset()
5022 if (group
== last_group
)
5025 if (grp
->bb_free
>= minlen
) {
5026 cnt
= ext4_trim_all_free(sb
, group
, first_cluster
,
5036 * For every group except the first one, we are sure
5037 * that the first cluster to discard will be cluster #0.
5043 atomic_set(&EXT4_SB(sb
)->s_last_trim_minblks
, minlen
);
5046 range
->len
= EXT4_C2B(EXT4_SB(sb
), trimmed
) << sb
->s_blocksize_bits
;