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 <linux/backing-dev.h>
30 #include <trace/events/ext4.h>
32 #ifdef CONFIG_EXT4_DEBUG
33 ushort ext4_mballoc_debug __read_mostly
;
35 module_param_named(mballoc_debug
, ext4_mballoc_debug
, ushort
, 0644);
36 MODULE_PARM_DESC(mballoc_debug
, "Debugging level for ext4's mballoc");
41 * - test ext4_ext_search_left() and ext4_ext_search_right()
42 * - search for metadata in few groups
45 * - normalization should take into account whether file is still open
46 * - discard preallocations if no free space left (policy?)
47 * - don't normalize tails
49 * - reservation for superuser
52 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
53 * - track min/max extents in each group for better group selection
54 * - mb_mark_used() may allocate chunk right after splitting buddy
55 * - tree of groups sorted by number of free blocks
60 * The allocation request involve request for multiple number of blocks
61 * near to the goal(block) value specified.
63 * During initialization phase of the allocator we decide to use the
64 * group preallocation or inode preallocation depending on the size of
65 * the file. The size of the file could be the resulting file size we
66 * would have after allocation, or the current file size, which ever
67 * is larger. If the size is less than sbi->s_mb_stream_request we
68 * select to use the group preallocation. The default value of
69 * s_mb_stream_request is 16 blocks. This can also be tuned via
70 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
71 * terms of number of blocks.
73 * The main motivation for having small file use group preallocation is to
74 * ensure that we have small files closer together on the disk.
76 * First stage the allocator looks at the inode prealloc list,
77 * ext4_inode_info->i_prealloc_list, which contains list of prealloc
78 * spaces for this particular inode. The inode prealloc space is
81 * pa_lstart -> the logical start block for this prealloc space
82 * pa_pstart -> the physical start block for this prealloc space
83 * pa_len -> length for this prealloc space (in clusters)
84 * pa_free -> free space available in this prealloc space (in clusters)
86 * The inode preallocation space is used looking at the _logical_ start
87 * block. If only the logical file block falls within the range of prealloc
88 * space we will consume the particular prealloc space. This makes sure that
89 * we have contiguous physical blocks representing the file blocks
91 * The important thing to be noted in case of inode prealloc space is that
92 * we don't modify the values associated to inode prealloc space except
95 * If we are not able to find blocks in the inode prealloc space and if we
96 * have the group allocation flag set then we look at the locality group
97 * prealloc space. These are per CPU prealloc list represented as
99 * ext4_sb_info.s_locality_groups[smp_processor_id()]
101 * The reason for having a per cpu locality group is to reduce the contention
102 * between CPUs. It is possible to get scheduled at this point.
104 * The locality group prealloc space is used looking at whether we have
105 * enough free space (pa_free) within the prealloc space.
107 * If we can't allocate blocks via inode prealloc or/and locality group
108 * prealloc then we look at the buddy cache. The buddy cache is represented
109 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
110 * mapped to the buddy and bitmap information regarding different
111 * groups. The buddy information is attached to buddy cache inode so that
112 * we can access them through the page cache. The information regarding
113 * each group is loaded via ext4_mb_load_buddy. The information involve
114 * block bitmap and buddy information. The information are stored in the
118 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
121 * one block each for bitmap and buddy information. So for each group we
122 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
123 * blocksize) blocks. So it can have information regarding groups_per_page
124 * which is blocks_per_page/2
126 * The buddy cache inode is not stored on disk. The inode is thrown
127 * away when the filesystem is unmounted.
129 * We look for count number of blocks in the buddy cache. If we were able
130 * to locate that many free blocks we return with additional information
131 * regarding rest of the contiguous physical block available
133 * Before allocating blocks via buddy cache we normalize the request
134 * blocks. This ensure we ask for more blocks that we needed. The extra
135 * blocks that we get after allocation is added to the respective prealloc
136 * list. In case of inode preallocation we follow a list of heuristics
137 * based on file size. This can be found in ext4_mb_normalize_request. If
138 * we are doing a group prealloc we try to normalize the request to
139 * sbi->s_mb_group_prealloc. The default value of s_mb_group_prealloc is
140 * dependent on the cluster size; for non-bigalloc file systems, it is
141 * 512 blocks. This can be tuned via
142 * /sys/fs/ext4/<partition>/mb_group_prealloc. The value is represented in
143 * terms of number of blocks. If we have mounted the file system with -O
144 * stripe=<value> option the group prealloc request is normalized to the
145 * the smallest multiple of the stripe value (sbi->s_stripe) which is
146 * greater than the default mb_group_prealloc.
148 * The regular allocator (using the buddy cache) supports a few tunables.
150 * /sys/fs/ext4/<partition>/mb_min_to_scan
151 * /sys/fs/ext4/<partition>/mb_max_to_scan
152 * /sys/fs/ext4/<partition>/mb_order2_req
154 * The regular allocator uses buddy scan only if the request len is power of
155 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
156 * value of s_mb_order2_reqs can be tuned via
157 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
158 * stripe size (sbi->s_stripe), we try to search for contiguous block in
159 * stripe size. This should result in better allocation on RAID setups. If
160 * not, we search in the specific group using bitmap for best extents. The
161 * tunable min_to_scan and max_to_scan control the behaviour here.
162 * min_to_scan indicate how long the mballoc __must__ look for a best
163 * extent and max_to_scan indicates how long the mballoc __can__ look for a
164 * best extent in the found extents. Searching for the blocks starts with
165 * the group specified as the goal value in allocation context via
166 * ac_g_ex. Each group is first checked based on the criteria whether it
167 * can be used for allocation. ext4_mb_good_group explains how the groups are
170 * Both the prealloc space are getting populated as above. So for the first
171 * request we will hit the buddy cache which will result in this prealloc
172 * space getting filled. The prealloc space is then later used for the
173 * subsequent request.
177 * mballoc operates on the following data:
179 * - in-core buddy (actually includes buddy and bitmap)
180 * - preallocation descriptors (PAs)
182 * there are two types of preallocations:
184 * assiged to specific inode and can be used for this inode only.
185 * it describes part of inode's space preallocated to specific
186 * physical blocks. any block from that preallocated can be used
187 * independent. the descriptor just tracks number of blocks left
188 * unused. so, before taking some block from descriptor, one must
189 * make sure corresponded logical block isn't allocated yet. this
190 * also means that freeing any block within descriptor's range
191 * must discard all preallocated blocks.
193 * assigned to specific locality group which does not translate to
194 * permanent set of inodes: inode can join and leave group. space
195 * from this type of preallocation can be used for any inode. thus
196 * it's consumed from the beginning to the end.
198 * relation between them can be expressed as:
199 * in-core buddy = on-disk bitmap + preallocation descriptors
201 * this mean blocks mballoc considers used are:
202 * - allocated blocks (persistent)
203 * - preallocated blocks (non-persistent)
205 * consistency in mballoc world means that at any time a block is either
206 * free or used in ALL structures. notice: "any time" should not be read
207 * literally -- time is discrete and delimited by locks.
209 * to keep it simple, we don't use block numbers, instead we count number of
210 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
212 * all operations can be expressed as:
213 * - init buddy: buddy = on-disk + PAs
214 * - new PA: buddy += N; PA = N
215 * - use inode PA: on-disk += N; PA -= N
216 * - discard inode PA buddy -= on-disk - PA; PA = 0
217 * - use locality group PA on-disk += N; PA -= N
218 * - discard locality group PA buddy -= PA; PA = 0
219 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
220 * is used in real operation because we can't know actual used
221 * bits from PA, only from on-disk bitmap
223 * if we follow this strict logic, then all operations above should be atomic.
224 * given some of them can block, we'd have to use something like semaphores
225 * killing performance on high-end SMP hardware. let's try to relax it using
226 * the following knowledge:
227 * 1) if buddy is referenced, it's already initialized
228 * 2) while block is used in buddy and the buddy is referenced,
229 * nobody can re-allocate that block
230 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
231 * bit set and PA claims same block, it's OK. IOW, one can set bit in
232 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
235 * so, now we're building a concurrency table:
238 * blocks for PA are allocated in the buddy, buddy must be referenced
239 * until PA is linked to allocation group to avoid concurrent buddy init
241 * we need to make sure that either on-disk bitmap or PA has uptodate data
242 * given (3) we care that PA-=N operation doesn't interfere with init
244 * the simplest way would be to have buddy initialized by the discard
245 * - use locality group PA
246 * again PA-=N must be serialized with init
247 * - discard locality group PA
248 * the simplest way would be to have buddy initialized by the discard
251 * i_data_sem serializes them
253 * discard process must wait until PA isn't used by another process
254 * - use locality group PA
255 * some mutex should serialize them
256 * - discard locality group PA
257 * discard process must wait until PA isn't used by another process
260 * i_data_sem or another mutex should serializes them
262 * discard process must wait until PA isn't used by another process
263 * - use locality group PA
264 * nothing wrong here -- they're different PAs covering different blocks
265 * - discard locality group PA
266 * discard process must wait until PA isn't used by another process
268 * now we're ready to make few consequences:
269 * - PA is referenced and while it is no discard is possible
270 * - PA is referenced until block isn't marked in on-disk bitmap
271 * - PA changes only after on-disk bitmap
272 * - discard must not compete with init. either init is done before
273 * any discard or they're serialized somehow
274 * - buddy init as sum of on-disk bitmap and PAs is done atomically
276 * a special case when we've used PA to emptiness. no need to modify buddy
277 * in this case, but we should care about concurrent init
282 * Logic in few words:
287 * mark bits in on-disk bitmap
290 * - use preallocation:
291 * find proper PA (per-inode or group)
293 * mark bits in on-disk bitmap
299 * mark bits in on-disk bitmap
302 * - discard preallocations in group:
304 * move them onto local list
305 * load on-disk bitmap
307 * remove PA from object (inode or locality group)
308 * mark free blocks in-core
310 * - discard inode's preallocations:
317 * - bitlock on a group (group)
318 * - object (inode/locality) (object)
329 * - release consumed pa:
334 * - generate in-core bitmap:
338 * - discard all for given object (inode, locality group):
343 * - discard all for given group:
350 static struct kmem_cache
*ext4_pspace_cachep
;
351 static struct kmem_cache
*ext4_ac_cachep
;
352 static struct kmem_cache
*ext4_free_data_cachep
;
354 /* We create slab caches for groupinfo data structures based on the
355 * superblock block size. There will be one per mounted filesystem for
356 * each unique s_blocksize_bits */
357 #define NR_GRPINFO_CACHES 8
358 static struct kmem_cache
*ext4_groupinfo_caches
[NR_GRPINFO_CACHES
];
360 static const char *ext4_groupinfo_slab_names
[NR_GRPINFO_CACHES
] = {
361 "ext4_groupinfo_1k", "ext4_groupinfo_2k", "ext4_groupinfo_4k",
362 "ext4_groupinfo_8k", "ext4_groupinfo_16k", "ext4_groupinfo_32k",
363 "ext4_groupinfo_64k", "ext4_groupinfo_128k"
366 static void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
368 static void ext4_mb_generate_from_freelist(struct super_block
*sb
, void *bitmap
,
371 static inline void *mb_correct_addr_and_bit(int *bit
, void *addr
)
373 #if BITS_PER_LONG == 64
374 *bit
+= ((unsigned long) addr
& 7UL) << 3;
375 addr
= (void *) ((unsigned long) addr
& ~7UL);
376 #elif BITS_PER_LONG == 32
377 *bit
+= ((unsigned long) addr
& 3UL) << 3;
378 addr
= (void *) ((unsigned long) addr
& ~3UL);
380 #error "how many bits you are?!"
385 static inline int mb_test_bit(int bit
, void *addr
)
388 * ext4_test_bit on architecture like powerpc
389 * needs unsigned long aligned address
391 addr
= mb_correct_addr_and_bit(&bit
, addr
);
392 return ext4_test_bit(bit
, addr
);
395 static inline void mb_set_bit(int bit
, void *addr
)
397 addr
= mb_correct_addr_and_bit(&bit
, addr
);
398 ext4_set_bit(bit
, addr
);
401 static inline void mb_clear_bit(int bit
, void *addr
)
403 addr
= mb_correct_addr_and_bit(&bit
, addr
);
404 ext4_clear_bit(bit
, addr
);
407 static inline int mb_test_and_clear_bit(int bit
, void *addr
)
409 addr
= mb_correct_addr_and_bit(&bit
, addr
);
410 return ext4_test_and_clear_bit(bit
, addr
);
413 static inline int mb_find_next_zero_bit(void *addr
, int max
, int start
)
415 int fix
= 0, ret
, tmpmax
;
416 addr
= mb_correct_addr_and_bit(&fix
, addr
);
420 ret
= ext4_find_next_zero_bit(addr
, tmpmax
, start
) - fix
;
426 static inline int mb_find_next_bit(void *addr
, int max
, int start
)
428 int fix
= 0, ret
, tmpmax
;
429 addr
= mb_correct_addr_and_bit(&fix
, addr
);
433 ret
= ext4_find_next_bit(addr
, tmpmax
, start
) - fix
;
439 static void *mb_find_buddy(struct ext4_buddy
*e4b
, int order
, int *max
)
443 BUG_ON(e4b
->bd_bitmap
== e4b
->bd_buddy
);
446 if (order
> e4b
->bd_blkbits
+ 1) {
451 /* at order 0 we see each particular block */
453 *max
= 1 << (e4b
->bd_blkbits
+ 3);
454 return e4b
->bd_bitmap
;
457 bb
= e4b
->bd_buddy
+ EXT4_SB(e4b
->bd_sb
)->s_mb_offsets
[order
];
458 *max
= EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[order
];
464 static void mb_free_blocks_double(struct inode
*inode
, struct ext4_buddy
*e4b
,
465 int first
, int count
)
468 struct super_block
*sb
= e4b
->bd_sb
;
470 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
472 assert_spin_locked(ext4_group_lock_ptr(sb
, e4b
->bd_group
));
473 for (i
= 0; i
< count
; i
++) {
474 if (!mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
)) {
475 ext4_fsblk_t blocknr
;
477 blocknr
= ext4_group_first_block_no(sb
, e4b
->bd_group
);
478 blocknr
+= EXT4_C2B(EXT4_SB(sb
), first
+ i
);
479 ext4_grp_locked_error(sb
, e4b
->bd_group
,
480 inode
? inode
->i_ino
: 0,
482 "freeing block already freed "
486 mb_clear_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
490 static void mb_mark_used_double(struct ext4_buddy
*e4b
, int first
, int count
)
494 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
496 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
497 for (i
= 0; i
< count
; i
++) {
498 BUG_ON(mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
));
499 mb_set_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
503 static void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
505 if (memcmp(e4b
->bd_info
->bb_bitmap
, bitmap
, e4b
->bd_sb
->s_blocksize
)) {
506 unsigned char *b1
, *b2
;
508 b1
= (unsigned char *) e4b
->bd_info
->bb_bitmap
;
509 b2
= (unsigned char *) bitmap
;
510 for (i
= 0; i
< e4b
->bd_sb
->s_blocksize
; i
++) {
511 if (b1
[i
] != b2
[i
]) {
512 ext4_msg(e4b
->bd_sb
, KERN_ERR
,
513 "corruption in group %u "
514 "at byte %u(%u): %x in copy != %x "
516 e4b
->bd_group
, i
, i
* 8, b1
[i
], b2
[i
]);
524 static inline void mb_free_blocks_double(struct inode
*inode
,
525 struct ext4_buddy
*e4b
, int first
, int count
)
529 static inline void mb_mark_used_double(struct ext4_buddy
*e4b
,
530 int first
, int count
)
534 static inline void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
540 #ifdef AGGRESSIVE_CHECK
542 #define MB_CHECK_ASSERT(assert) \
546 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
547 function, file, line, # assert); \
552 static int __mb_check_buddy(struct ext4_buddy
*e4b
, char *file
,
553 const char *function
, int line
)
555 struct super_block
*sb
= e4b
->bd_sb
;
556 int order
= e4b
->bd_blkbits
+ 1;
563 struct ext4_group_info
*grp
;
566 struct list_head
*cur
;
571 static int mb_check_counter
;
572 if (mb_check_counter
++ % 100 != 0)
577 buddy
= mb_find_buddy(e4b
, order
, &max
);
578 MB_CHECK_ASSERT(buddy
);
579 buddy2
= mb_find_buddy(e4b
, order
- 1, &max2
);
580 MB_CHECK_ASSERT(buddy2
);
581 MB_CHECK_ASSERT(buddy
!= buddy2
);
582 MB_CHECK_ASSERT(max
* 2 == max2
);
585 for (i
= 0; i
< max
; i
++) {
587 if (mb_test_bit(i
, buddy
)) {
588 /* only single bit in buddy2 may be 1 */
589 if (!mb_test_bit(i
<< 1, buddy2
)) {
591 mb_test_bit((i
<<1)+1, buddy2
));
592 } else if (!mb_test_bit((i
<< 1) + 1, buddy2
)) {
594 mb_test_bit(i
<< 1, buddy2
));
599 /* both bits in buddy2 must be 1 */
600 MB_CHECK_ASSERT(mb_test_bit(i
<< 1, buddy2
));
601 MB_CHECK_ASSERT(mb_test_bit((i
<< 1) + 1, buddy2
));
603 for (j
= 0; j
< (1 << order
); j
++) {
604 k
= (i
* (1 << order
)) + j
;
606 !mb_test_bit(k
, e4b
->bd_bitmap
));
610 MB_CHECK_ASSERT(e4b
->bd_info
->bb_counters
[order
] == count
);
615 buddy
= mb_find_buddy(e4b
, 0, &max
);
616 for (i
= 0; i
< max
; i
++) {
617 if (!mb_test_bit(i
, buddy
)) {
618 MB_CHECK_ASSERT(i
>= e4b
->bd_info
->bb_first_free
);
626 /* check used bits only */
627 for (j
= 0; j
< e4b
->bd_blkbits
+ 1; j
++) {
628 buddy2
= mb_find_buddy(e4b
, j
, &max2
);
630 MB_CHECK_ASSERT(k
< max2
);
631 MB_CHECK_ASSERT(mb_test_bit(k
, buddy2
));
634 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b
->bd_info
));
635 MB_CHECK_ASSERT(e4b
->bd_info
->bb_fragments
== fragments
);
637 grp
= ext4_get_group_info(sb
, e4b
->bd_group
);
638 list_for_each(cur
, &grp
->bb_prealloc_list
) {
639 ext4_group_t groupnr
;
640 struct ext4_prealloc_space
*pa
;
641 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
642 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &groupnr
, &k
);
643 MB_CHECK_ASSERT(groupnr
== e4b
->bd_group
);
644 for (i
= 0; i
< pa
->pa_len
; i
++)
645 MB_CHECK_ASSERT(mb_test_bit(k
+ i
, buddy
));
649 #undef MB_CHECK_ASSERT
650 #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
651 __FILE__, __func__, __LINE__)
653 #define mb_check_buddy(e4b)
657 * Divide blocks started from @first with length @len into
658 * smaller chunks with power of 2 blocks.
659 * Clear the bits in bitmap which the blocks of the chunk(s) covered,
660 * then increase bb_counters[] for corresponded chunk size.
662 static void ext4_mb_mark_free_simple(struct super_block
*sb
,
663 void *buddy
, ext4_grpblk_t first
, ext4_grpblk_t len
,
664 struct ext4_group_info
*grp
)
666 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
672 BUG_ON(len
> EXT4_CLUSTERS_PER_GROUP(sb
));
674 border
= 2 << sb
->s_blocksize_bits
;
677 /* find how many blocks can be covered since this position */
678 max
= ffs(first
| border
) - 1;
680 /* find how many blocks of power 2 we need to mark */
687 /* mark multiblock chunks only */
688 grp
->bb_counters
[min
]++;
690 mb_clear_bit(first
>> min
,
691 buddy
+ sbi
->s_mb_offsets
[min
]);
699 * Cache the order of the largest free extent we have available in this block
703 mb_set_largest_free_order(struct super_block
*sb
, struct ext4_group_info
*grp
)
708 grp
->bb_largest_free_order
= -1; /* uninit */
710 bits
= sb
->s_blocksize_bits
+ 1;
711 for (i
= bits
; i
>= 0; i
--) {
712 if (grp
->bb_counters
[i
] > 0) {
713 grp
->bb_largest_free_order
= i
;
719 static noinline_for_stack
720 void ext4_mb_generate_buddy(struct super_block
*sb
,
721 void *buddy
, void *bitmap
, ext4_group_t group
)
723 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
724 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
725 ext4_grpblk_t max
= EXT4_CLUSTERS_PER_GROUP(sb
);
730 unsigned fragments
= 0;
731 unsigned long long period
= get_cycles();
733 /* initialize buddy from bitmap which is aggregation
734 * of on-disk bitmap and preallocations */
735 i
= mb_find_next_zero_bit(bitmap
, max
, 0);
736 grp
->bb_first_free
= i
;
740 i
= mb_find_next_bit(bitmap
, max
, i
);
744 ext4_mb_mark_free_simple(sb
, buddy
, first
, len
, grp
);
746 grp
->bb_counters
[0]++;
748 i
= mb_find_next_zero_bit(bitmap
, max
, i
);
750 grp
->bb_fragments
= fragments
;
752 if (free
!= grp
->bb_free
) {
753 /* for more specific debugging, sangwoo2.lee */
754 struct ext4_group_desc
*desc
;
755 ext4_fsblk_t bitmap_blk
;
757 desc
= ext4_get_group_desc(sb
, group
, NULL
);
758 bitmap_blk
= ext4_block_bitmap(sb
, desc
);
760 print_block_data(sb
, bitmap_blk
, bitmap
, 0, EXT4_BLOCK_SIZE(sb
));
761 /* for more specific debugging */
763 ext4_grp_locked_error(sb
, group
, 0, 0,
764 "block bitmap and bg descriptor "
765 "inconsistent: %u vs %u free clusters",
768 * If we intend to continue, we consider group descriptor
769 * corrupt and update bb_free using bitmap value
772 if (!EXT4_MB_GRP_BBITMAP_CORRUPT(grp
))
773 percpu_counter_sub(&sbi
->s_freeclusters_counter
,
775 set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT
, &grp
->bb_state
);
777 mb_set_largest_free_order(sb
, grp
);
779 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
, &(grp
->bb_state
));
781 period
= get_cycles() - period
;
782 spin_lock(&EXT4_SB(sb
)->s_bal_lock
);
783 EXT4_SB(sb
)->s_mb_buddies_generated
++;
784 EXT4_SB(sb
)->s_mb_generation_time
+= period
;
785 spin_unlock(&EXT4_SB(sb
)->s_bal_lock
);
788 static void mb_regenerate_buddy(struct ext4_buddy
*e4b
)
794 while ((buddy
= mb_find_buddy(e4b
, order
++, &count
))) {
795 ext4_set_bits(buddy
, 0, count
);
797 e4b
->bd_info
->bb_fragments
= 0;
798 memset(e4b
->bd_info
->bb_counters
, 0,
799 sizeof(*e4b
->bd_info
->bb_counters
) *
800 (e4b
->bd_sb
->s_blocksize_bits
+ 2));
802 ext4_mb_generate_buddy(e4b
->bd_sb
, e4b
->bd_buddy
,
803 e4b
->bd_bitmap
, e4b
->bd_group
);
806 /* The buddy information is attached the buddy cache inode
807 * for convenience. The information regarding each group
808 * is loaded via ext4_mb_load_buddy. The information involve
809 * block bitmap and buddy information. The information are
810 * stored in the inode as
813 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
816 * one block each for bitmap and buddy information.
817 * So for each group we take up 2 blocks. A page can
818 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
819 * So it can have information regarding groups_per_page which
820 * is blocks_per_page/2
822 * Locking note: This routine takes the block group lock of all groups
823 * for this page; do not hold this lock when calling this routine!
826 static int ext4_mb_init_cache(struct page
*page
, char *incore
, gfp_t gfp
)
828 ext4_group_t ngroups
;
834 ext4_group_t first_group
, group
;
836 struct super_block
*sb
;
837 struct buffer_head
*bhs
;
838 struct buffer_head
**bh
= NULL
;
842 struct ext4_group_info
*grinfo
;
844 mb_debug(1, "init page %lu\n", page
->index
);
846 inode
= page
->mapping
->host
;
848 ngroups
= ext4_get_groups_count(sb
);
849 blocksize
= 1 << inode
->i_blkbits
;
850 blocks_per_page
= PAGE_CACHE_SIZE
/ blocksize
;
852 groups_per_page
= blocks_per_page
>> 1;
853 if (groups_per_page
== 0)
856 /* allocate buffer_heads to read bitmaps */
857 if (groups_per_page
> 1) {
858 i
= sizeof(struct buffer_head
*) * groups_per_page
;
859 bh
= kzalloc(i
, gfp
);
867 first_group
= page
->index
* blocks_per_page
/ 2;
869 /* read all groups the page covers into the cache */
870 for (i
= 0, group
= first_group
; i
< groups_per_page
; i
++, group
++) {
871 if (group
>= ngroups
)
874 grinfo
= ext4_get_group_info(sb
, group
);
876 * If page is uptodate then we came here after online resize
877 * which added some new uninitialized group info structs, so
878 * we must skip all initialized uptodate buddies on the page,
879 * which may be currently in use by an allocating task.
881 if (PageUptodate(page
) && !EXT4_MB_GRP_NEED_INIT(grinfo
)) {
885 bh
[i
] = ext4_read_block_bitmap_nowait(sb
, group
);
887 err
= PTR_ERR(bh
[i
]);
891 mb_debug(1, "read bitmap for group %u\n", group
);
894 /* wait for I/O completion */
895 for (i
= 0, group
= first_group
; i
< groups_per_page
; i
++, group
++) {
900 err2
= ext4_wait_block_bitmap(sb
, group
, bh
[i
]);
905 first_block
= page
->index
* blocks_per_page
;
906 for (i
= 0; i
< blocks_per_page
; i
++) {
907 group
= (first_block
+ i
) >> 1;
908 if (group
>= ngroups
)
911 if (!bh
[group
- first_group
])
912 /* skip initialized uptodate buddy */
915 if (!buffer_verified(bh
[group
- first_group
]))
916 /* Skip faulty bitmaps */
921 * data carry information regarding this
922 * particular group in the format specified
926 data
= page_address(page
) + (i
* blocksize
);
927 bitmap
= bh
[group
- first_group
]->b_data
;
930 * We place the buddy block and bitmap block
933 if ((first_block
+ i
) & 1) {
934 /* this is block of buddy */
935 BUG_ON(incore
== NULL
);
936 mb_debug(1, "put buddy for group %u in page %lu/%x\n",
937 group
, page
->index
, i
* blocksize
);
938 trace_ext4_mb_buddy_bitmap_load(sb
, group
);
939 grinfo
= ext4_get_group_info(sb
, group
);
940 grinfo
->bb_fragments
= 0;
941 memset(grinfo
->bb_counters
, 0,
942 sizeof(*grinfo
->bb_counters
) *
943 (sb
->s_blocksize_bits
+2));
945 * incore got set to the group block bitmap below
947 ext4_lock_group(sb
, group
);
949 memset(data
, 0xff, blocksize
);
950 ext4_mb_generate_buddy(sb
, data
, incore
, group
);
951 ext4_unlock_group(sb
, group
);
954 /* this is block of bitmap */
955 BUG_ON(incore
!= NULL
);
956 mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
957 group
, page
->index
, i
* blocksize
);
958 trace_ext4_mb_bitmap_load(sb
, group
);
960 /* see comments in ext4_mb_put_pa() */
961 ext4_lock_group(sb
, group
);
962 memcpy(data
, bitmap
, blocksize
);
964 /* mark all preallocated blks used in in-core bitmap */
965 ext4_mb_generate_from_pa(sb
, data
, group
);
966 ext4_mb_generate_from_freelist(sb
, data
, group
);
967 ext4_unlock_group(sb
, group
);
969 /* set incore so that the buddy information can be
970 * generated using this
975 SetPageUptodate(page
);
979 for (i
= 0; i
< groups_per_page
; i
++)
988 * Lock the buddy and bitmap pages. This make sure other parallel init_group
989 * on the same buddy page doesn't happen whild holding the buddy page lock.
990 * Return locked buddy and bitmap pages on e4b struct. If buddy and bitmap
991 * are on the same page e4b->bd_buddy_page is NULL and return value is 0.
993 static int ext4_mb_get_buddy_page_lock(struct super_block
*sb
,
994 ext4_group_t group
, struct ext4_buddy
*e4b
, gfp_t gfp
)
996 struct inode
*inode
= EXT4_SB(sb
)->s_buddy_cache
;
997 int block
, pnum
, poff
;
1001 e4b
->bd_buddy_page
= NULL
;
1002 e4b
->bd_bitmap_page
= NULL
;
1004 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1006 * the buddy cache inode stores the block bitmap
1007 * and buddy information in consecutive blocks.
1008 * So for each group we need two blocks.
1011 pnum
= block
/ blocks_per_page
;
1012 poff
= block
% blocks_per_page
;
1013 page
= find_or_create_page(inode
->i_mapping
, pnum
, gfp
);
1016 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1017 e4b
->bd_bitmap_page
= page
;
1018 e4b
->bd_bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
1020 if (blocks_per_page
>= 2) {
1021 /* buddy and bitmap are on the same page */
1026 pnum
= block
/ blocks_per_page
;
1027 page
= find_or_create_page(inode
->i_mapping
, pnum
, gfp
);
1030 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1031 e4b
->bd_buddy_page
= page
;
1035 static void ext4_mb_put_buddy_page_lock(struct ext4_buddy
*e4b
)
1037 if (e4b
->bd_bitmap_page
) {
1038 unlock_page(e4b
->bd_bitmap_page
);
1039 page_cache_release(e4b
->bd_bitmap_page
);
1041 if (e4b
->bd_buddy_page
) {
1042 unlock_page(e4b
->bd_buddy_page
);
1043 page_cache_release(e4b
->bd_buddy_page
);
1048 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1049 * block group lock of all groups for this page; do not hold the BG lock when
1050 * calling this routine!
1052 static noinline_for_stack
1053 int ext4_mb_init_group(struct super_block
*sb
, ext4_group_t group
, gfp_t gfp
)
1056 struct ext4_group_info
*this_grp
;
1057 struct ext4_buddy e4b
;
1062 mb_debug(1, "init group %u\n", group
);
1063 this_grp
= ext4_get_group_info(sb
, group
);
1065 * This ensures that we don't reinit the buddy cache
1066 * page which map to the group from which we are already
1067 * allocating. If we are looking at the buddy cache we would
1068 * have taken a reference using ext4_mb_load_buddy and that
1069 * would have pinned buddy page to page cache.
1070 * The call to ext4_mb_get_buddy_page_lock will mark the
1073 ret
= ext4_mb_get_buddy_page_lock(sb
, group
, &e4b
, gfp
);
1074 if (ret
|| !EXT4_MB_GRP_NEED_INIT(this_grp
)) {
1076 * somebody initialized the group
1077 * return without doing anything
1082 page
= e4b
.bd_bitmap_page
;
1083 ret
= ext4_mb_init_cache(page
, NULL
, gfp
);
1086 if (!PageUptodate(page
)) {
1091 if (e4b
.bd_buddy_page
== NULL
) {
1093 * If both the bitmap and buddy are in
1094 * the same page we don't need to force
1100 /* init buddy cache */
1101 page
= e4b
.bd_buddy_page
;
1102 ret
= ext4_mb_init_cache(page
, e4b
.bd_bitmap
, gfp
);
1105 if (!PageUptodate(page
)) {
1110 ext4_mb_put_buddy_page_lock(&e4b
);
1115 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1116 * block group lock of all groups for this page; do not hold the BG lock when
1117 * calling this routine!
1119 static noinline_for_stack
int
1120 ext4_mb_load_buddy_gfp(struct super_block
*sb
, ext4_group_t group
,
1121 struct ext4_buddy
*e4b
, gfp_t gfp
)
1123 int blocks_per_page
;
1129 struct ext4_group_info
*grp
;
1130 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1131 struct inode
*inode
= sbi
->s_buddy_cache
;
1134 mb_debug(1, "load group %u\n", group
);
1136 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1137 grp
= ext4_get_group_info(sb
, group
);
1139 e4b
->bd_blkbits
= sb
->s_blocksize_bits
;
1142 e4b
->bd_group
= group
;
1143 e4b
->bd_buddy_page
= NULL
;
1144 e4b
->bd_bitmap_page
= NULL
;
1146 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp
))) {
1148 * we need full data about the group
1149 * to make a good selection
1151 ret
= ext4_mb_init_group(sb
, group
, gfp
);
1157 * the buddy cache inode stores the block bitmap
1158 * and buddy information in consecutive blocks.
1159 * So for each group we need two blocks.
1162 pnum
= block
/ blocks_per_page
;
1163 poff
= block
% blocks_per_page
;
1165 /* we could use find_or_create_page(), but it locks page
1166 * what we'd like to avoid in fast path ... */
1167 page
= find_get_page_flags(inode
->i_mapping
, pnum
, FGP_ACCESSED
);
1168 if (page
== NULL
|| !PageUptodate(page
)) {
1171 * drop the page reference and try
1172 * to get the page with lock. If we
1173 * are not uptodate that implies
1174 * somebody just created the page but
1175 * is yet to initialize the same. So
1176 * wait for it to initialize.
1178 page_cache_release(page
);
1179 page
= find_or_create_page(inode
->i_mapping
, pnum
, gfp
);
1181 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1182 if (!PageUptodate(page
)) {
1183 ret
= ext4_mb_init_cache(page
, NULL
, gfp
);
1188 mb_cmp_bitmaps(e4b
, page_address(page
) +
1189 (poff
* sb
->s_blocksize
));
1198 if (!PageUptodate(page
)) {
1203 /* Pages marked accessed already */
1204 e4b
->bd_bitmap_page
= page
;
1205 e4b
->bd_bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
1208 pnum
= block
/ blocks_per_page
;
1209 poff
= block
% blocks_per_page
;
1211 page
= find_get_page_flags(inode
->i_mapping
, pnum
, FGP_ACCESSED
);
1212 if (page
== NULL
|| !PageUptodate(page
)) {
1214 page_cache_release(page
);
1215 page
= find_or_create_page(inode
->i_mapping
, pnum
, gfp
);
1217 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1218 if (!PageUptodate(page
)) {
1219 ret
= ext4_mb_init_cache(page
, e4b
->bd_bitmap
,
1233 if (!PageUptodate(page
)) {
1238 /* Pages marked accessed already */
1239 e4b
->bd_buddy_page
= page
;
1240 e4b
->bd_buddy
= page_address(page
) + (poff
* sb
->s_blocksize
);
1242 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
1243 BUG_ON(e4b
->bd_buddy_page
== NULL
);
1249 page_cache_release(page
);
1250 if (e4b
->bd_bitmap_page
)
1251 page_cache_release(e4b
->bd_bitmap_page
);
1252 if (e4b
->bd_buddy_page
)
1253 page_cache_release(e4b
->bd_buddy_page
);
1254 e4b
->bd_buddy
= NULL
;
1255 e4b
->bd_bitmap
= NULL
;
1259 static int ext4_mb_load_buddy(struct super_block
*sb
, ext4_group_t group
,
1260 struct ext4_buddy
*e4b
)
1262 return ext4_mb_load_buddy_gfp(sb
, group
, e4b
, GFP_NOFS
);
1265 static void ext4_mb_unload_buddy(struct ext4_buddy
*e4b
)
1267 if (e4b
->bd_bitmap_page
)
1268 page_cache_release(e4b
->bd_bitmap_page
);
1269 if (e4b
->bd_buddy_page
)
1270 page_cache_release(e4b
->bd_buddy_page
);
1274 static int mb_find_order_for_block(struct ext4_buddy
*e4b
, int block
)
1277 int bb_incr
= 1 << (e4b
->bd_blkbits
- 1);
1280 BUG_ON(e4b
->bd_bitmap
== e4b
->bd_buddy
);
1281 BUG_ON(block
>= (1 << (e4b
->bd_blkbits
+ 3)));
1284 while (order
<= e4b
->bd_blkbits
+ 1) {
1286 if (!mb_test_bit(block
, bb
)) {
1287 /* this block is part of buddy of order 'order' */
1297 static void mb_clear_bits(void *bm
, int cur
, int len
)
1303 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1304 /* fast path: clear whole word at once */
1305 addr
= bm
+ (cur
>> 3);
1310 mb_clear_bit(cur
, bm
);
1315 /* clear bits in given range
1316 * will return first found zero bit if any, -1 otherwise
1318 static int mb_test_and_clear_bits(void *bm
, int cur
, int len
)
1325 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1326 /* fast path: clear whole word at once */
1327 addr
= bm
+ (cur
>> 3);
1328 if (*addr
!= (__u32
)(-1) && zero_bit
== -1)
1329 zero_bit
= cur
+ mb_find_next_zero_bit(addr
, 32, 0);
1334 if (!mb_test_and_clear_bit(cur
, bm
) && zero_bit
== -1)
1342 void ext4_set_bits(void *bm
, int cur
, int len
)
1348 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1349 /* fast path: set whole word at once */
1350 addr
= bm
+ (cur
>> 3);
1355 mb_set_bit(cur
, bm
);
1361 * _________________________________________________________________ */
1363 static inline int mb_buddy_adjust_border(int* bit
, void* bitmap
, int side
)
1365 if (mb_test_bit(*bit
+ side
, bitmap
)) {
1366 mb_clear_bit(*bit
, bitmap
);
1372 mb_set_bit(*bit
, bitmap
);
1377 static void mb_buddy_mark_free(struct ext4_buddy
*e4b
, int first
, int last
)
1381 void *buddy
= mb_find_buddy(e4b
, order
, &max
);
1386 /* Bits in range [first; last] are known to be set since
1387 * corresponding blocks were allocated. Bits in range
1388 * (first; last) will stay set because they form buddies on
1389 * upper layer. We just deal with borders if they don't
1390 * align with upper layer and then go up.
1391 * Releasing entire group is all about clearing
1392 * single bit of highest order buddy.
1396 * ---------------------------------
1398 * ---------------------------------
1399 * | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
1400 * ---------------------------------
1402 * \_____________________/
1404 * Neither [1] nor [6] is aligned to above layer.
1405 * Left neighbour [0] is free, so mark it busy,
1406 * decrease bb_counters and extend range to
1408 * Right neighbour [7] is busy. It can't be coaleasced with [6], so
1409 * mark [6] free, increase bb_counters and shrink range to
1411 * Then shift range to [0; 2], go up and do the same.
1416 e4b
->bd_info
->bb_counters
[order
] += mb_buddy_adjust_border(&first
, buddy
, -1);
1418 e4b
->bd_info
->bb_counters
[order
] += mb_buddy_adjust_border(&last
, buddy
, 1);
1423 if (first
== last
|| !(buddy2
= mb_find_buddy(e4b
, order
, &max
))) {
1424 mb_clear_bits(buddy
, first
, last
- first
+ 1);
1425 e4b
->bd_info
->bb_counters
[order
- 1] += last
- first
+ 1;
1434 static void mb_free_blocks(struct inode
*inode
, struct ext4_buddy
*e4b
,
1435 int first
, int count
)
1437 int left_is_free
= 0;
1438 int right_is_free
= 0;
1440 int last
= first
+ count
- 1;
1441 struct super_block
*sb
= e4b
->bd_sb
;
1443 if (WARN_ON(count
== 0))
1445 BUG_ON(last
>= (sb
->s_blocksize
<< 3));
1446 assert_spin_locked(ext4_group_lock_ptr(sb
, e4b
->bd_group
));
1447 /* Don't bother if the block group is corrupt. */
1448 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b
->bd_info
)))
1451 mb_check_buddy(e4b
);
1452 mb_free_blocks_double(inode
, e4b
, first
, count
);
1454 e4b
->bd_info
->bb_free
+= count
;
1455 if (first
< e4b
->bd_info
->bb_first_free
)
1456 e4b
->bd_info
->bb_first_free
= first
;
1458 /* access memory sequentially: check left neighbour,
1459 * clear range and then check right neighbour
1462 left_is_free
= !mb_test_bit(first
- 1, e4b
->bd_bitmap
);
1463 block
= mb_test_and_clear_bits(e4b
->bd_bitmap
, first
, count
);
1464 if (last
+ 1 < EXT4_SB(sb
)->s_mb_maxs
[0])
1465 right_is_free
= !mb_test_bit(last
+ 1, e4b
->bd_bitmap
);
1467 if (unlikely(block
!= -1)) {
1468 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1469 /* for debugging, sangwoo2.lee */
1470 struct ext4_group_desc
*desc
;
1471 ext4_fsblk_t blocknr
, bitmap_blk
;
1473 desc
= ext4_get_group_desc(sb
, e4b
->bd_group
, NULL
);
1474 bitmap_blk
= ext4_block_bitmap(sb
, desc
);
1476 blocknr
= ext4_group_first_block_no(sb
, e4b
->bd_group
);
1477 blocknr
+= EXT4_C2B(EXT4_SB(sb
), block
);
1479 print_block_data(sb
, bitmap_blk
, e4b
->bd_bitmap
, 0
1480 , EXT4_BLOCK_SIZE(sb
));
1482 ext4_grp_locked_error(sb
, e4b
->bd_group
,
1483 inode
? inode
->i_ino
: 0,
1485 "freeing already freed block "
1486 "(bit %u); block bitmap corrupt.",
1488 if (!EXT4_MB_GRP_BBITMAP_CORRUPT(e4b
->bd_info
))
1489 percpu_counter_sub(&sbi
->s_freeclusters_counter
,
1490 e4b
->bd_info
->bb_free
);
1491 /* Mark the block group as corrupt. */
1492 set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT
,
1493 &e4b
->bd_info
->bb_state
);
1494 mb_regenerate_buddy(e4b
);
1498 /* let's maintain fragments counter */
1499 if (left_is_free
&& right_is_free
)
1500 e4b
->bd_info
->bb_fragments
--;
1501 else if (!left_is_free
&& !right_is_free
)
1502 e4b
->bd_info
->bb_fragments
++;
1504 /* buddy[0] == bd_bitmap is a special case, so handle
1505 * it right away and let mb_buddy_mark_free stay free of
1506 * zero order checks.
1507 * Check if neighbours are to be coaleasced,
1508 * adjust bitmap bb_counters and borders appropriately.
1511 first
+= !left_is_free
;
1512 e4b
->bd_info
->bb_counters
[0] += left_is_free
? -1 : 1;
1515 last
-= !right_is_free
;
1516 e4b
->bd_info
->bb_counters
[0] += right_is_free
? -1 : 1;
1520 mb_buddy_mark_free(e4b
, first
>> 1, last
>> 1);
1523 mb_set_largest_free_order(sb
, e4b
->bd_info
);
1524 mb_check_buddy(e4b
);
1527 static int mb_find_extent(struct ext4_buddy
*e4b
, int block
,
1528 int needed
, struct ext4_free_extent
*ex
)
1534 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
1537 buddy
= mb_find_buddy(e4b
, 0, &max
);
1538 BUG_ON(buddy
== NULL
);
1539 BUG_ON(block
>= max
);
1540 if (mb_test_bit(block
, buddy
)) {
1547 /* find actual order */
1548 order
= mb_find_order_for_block(e4b
, block
);
1549 block
= block
>> order
;
1551 ex
->fe_len
= 1 << order
;
1552 ex
->fe_start
= block
<< order
;
1553 ex
->fe_group
= e4b
->bd_group
;
1555 /* calc difference from given start */
1556 next
= next
- ex
->fe_start
;
1558 ex
->fe_start
+= next
;
1560 while (needed
> ex
->fe_len
&&
1561 mb_find_buddy(e4b
, order
, &max
)) {
1563 if (block
+ 1 >= max
)
1566 next
= (block
+ 1) * (1 << order
);
1567 if (mb_test_bit(next
, e4b
->bd_bitmap
))
1570 order
= mb_find_order_for_block(e4b
, next
);
1572 block
= next
>> order
;
1573 ex
->fe_len
+= 1 << order
;
1576 BUG_ON(ex
->fe_start
+ ex
->fe_len
> (1 << (e4b
->bd_blkbits
+ 3)));
1580 static int mb_mark_used(struct ext4_buddy
*e4b
, struct ext4_free_extent
*ex
)
1586 int start
= ex
->fe_start
;
1587 int len
= ex
->fe_len
;
1592 BUG_ON(start
+ len
> (e4b
->bd_sb
->s_blocksize
<< 3));
1593 BUG_ON(e4b
->bd_group
!= ex
->fe_group
);
1594 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
1595 mb_check_buddy(e4b
);
1596 mb_mark_used_double(e4b
, start
, len
);
1598 e4b
->bd_info
->bb_free
-= len
;
1599 if (e4b
->bd_info
->bb_first_free
== start
)
1600 e4b
->bd_info
->bb_first_free
+= len
;
1602 /* let's maintain fragments counter */
1604 mlen
= !mb_test_bit(start
- 1, e4b
->bd_bitmap
);
1605 if (start
+ len
< EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[0])
1606 max
= !mb_test_bit(start
+ len
, e4b
->bd_bitmap
);
1608 e4b
->bd_info
->bb_fragments
++;
1609 else if (!mlen
&& !max
)
1610 e4b
->bd_info
->bb_fragments
--;
1612 /* let's maintain buddy itself */
1614 ord
= mb_find_order_for_block(e4b
, start
);
1616 if (((start
>> ord
) << ord
) == start
&& len
>= (1 << ord
)) {
1617 /* the whole chunk may be allocated at once! */
1619 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1620 BUG_ON((start
>> ord
) >= max
);
1621 mb_set_bit(start
>> ord
, buddy
);
1622 e4b
->bd_info
->bb_counters
[ord
]--;
1629 /* store for history */
1631 ret
= len
| (ord
<< 16);
1633 /* we have to split large buddy */
1635 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1636 mb_set_bit(start
>> ord
, buddy
);
1637 e4b
->bd_info
->bb_counters
[ord
]--;
1640 cur
= (start
>> ord
) & ~1U;
1641 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1642 mb_clear_bit(cur
, buddy
);
1643 mb_clear_bit(cur
+ 1, buddy
);
1644 e4b
->bd_info
->bb_counters
[ord
]++;
1645 e4b
->bd_info
->bb_counters
[ord
]++;
1647 mb_set_largest_free_order(e4b
->bd_sb
, e4b
->bd_info
);
1649 ext4_set_bits(e4b
->bd_bitmap
, ex
->fe_start
, len0
);
1650 mb_check_buddy(e4b
);
1656 * Must be called under group lock!
1658 static void ext4_mb_use_best_found(struct ext4_allocation_context
*ac
,
1659 struct ext4_buddy
*e4b
)
1661 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1664 BUG_ON(ac
->ac_b_ex
.fe_group
!= e4b
->bd_group
);
1665 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1667 ac
->ac_b_ex
.fe_len
= min(ac
->ac_b_ex
.fe_len
, ac
->ac_g_ex
.fe_len
);
1668 ac
->ac_b_ex
.fe_logical
= ac
->ac_g_ex
.fe_logical
;
1669 ret
= mb_mark_used(e4b
, &ac
->ac_b_ex
);
1671 /* preallocation can change ac_b_ex, thus we store actually
1672 * allocated blocks for history */
1673 ac
->ac_f_ex
= ac
->ac_b_ex
;
1675 ac
->ac_status
= AC_STATUS_FOUND
;
1676 ac
->ac_tail
= ret
& 0xffff;
1677 ac
->ac_buddy
= ret
>> 16;
1680 * take the page reference. We want the page to be pinned
1681 * so that we don't get a ext4_mb_init_cache_call for this
1682 * group until we update the bitmap. That would mean we
1683 * double allocate blocks. The reference is dropped
1684 * in ext4_mb_release_context
1686 ac
->ac_bitmap_page
= e4b
->bd_bitmap_page
;
1687 get_page(ac
->ac_bitmap_page
);
1688 ac
->ac_buddy_page
= e4b
->bd_buddy_page
;
1689 get_page(ac
->ac_buddy_page
);
1690 /* store last allocated for subsequent stream allocation */
1691 if (ac
->ac_flags
& EXT4_MB_STREAM_ALLOC
) {
1692 spin_lock(&sbi
->s_md_lock
);
1693 sbi
->s_mb_last_group
= ac
->ac_f_ex
.fe_group
;
1694 sbi
->s_mb_last_start
= ac
->ac_f_ex
.fe_start
;
1695 spin_unlock(&sbi
->s_md_lock
);
1700 * regular allocator, for general purposes allocation
1703 static void ext4_mb_check_limits(struct ext4_allocation_context
*ac
,
1704 struct ext4_buddy
*e4b
,
1707 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1708 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1709 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1710 struct ext4_free_extent ex
;
1713 if (ac
->ac_status
== AC_STATUS_FOUND
)
1716 * We don't want to scan for a whole year
1718 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
&&
1719 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1720 ac
->ac_status
= AC_STATUS_BREAK
;
1725 * Haven't found good chunk so far, let's continue
1727 if (bex
->fe_len
< gex
->fe_len
)
1730 if ((finish_group
|| ac
->ac_found
> sbi
->s_mb_min_to_scan
)
1731 && bex
->fe_group
== e4b
->bd_group
) {
1732 /* recheck chunk's availability - we don't know
1733 * when it was found (within this lock-unlock
1735 max
= mb_find_extent(e4b
, bex
->fe_start
, gex
->fe_len
, &ex
);
1736 if (max
>= gex
->fe_len
) {
1737 ext4_mb_use_best_found(ac
, e4b
);
1744 * The routine checks whether found extent is good enough. If it is,
1745 * then the extent gets marked used and flag is set to the context
1746 * to stop scanning. Otherwise, the extent is compared with the
1747 * previous found extent and if new one is better, then it's stored
1748 * in the context. Later, the best found extent will be used, if
1749 * mballoc can't find good enough extent.
1751 * FIXME: real allocation policy is to be designed yet!
1753 static void ext4_mb_measure_extent(struct ext4_allocation_context
*ac
,
1754 struct ext4_free_extent
*ex
,
1755 struct ext4_buddy
*e4b
)
1757 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1758 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1760 BUG_ON(ex
->fe_len
<= 0);
1761 BUG_ON(ex
->fe_len
> EXT4_CLUSTERS_PER_GROUP(ac
->ac_sb
));
1762 BUG_ON(ex
->fe_start
>= EXT4_CLUSTERS_PER_GROUP(ac
->ac_sb
));
1763 BUG_ON(ac
->ac_status
!= AC_STATUS_CONTINUE
);
1768 * The special case - take what you catch first
1770 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1772 ext4_mb_use_best_found(ac
, e4b
);
1777 * Let's check whether the chuck is good enough
1779 if (ex
->fe_len
== gex
->fe_len
) {
1781 ext4_mb_use_best_found(ac
, e4b
);
1786 * If this is first found extent, just store it in the context
1788 if (bex
->fe_len
== 0) {
1794 * If new found extent is better, store it in the context
1796 if (bex
->fe_len
< gex
->fe_len
) {
1797 /* if the request isn't satisfied, any found extent
1798 * larger than previous best one is better */
1799 if (ex
->fe_len
> bex
->fe_len
)
1801 } else if (ex
->fe_len
> gex
->fe_len
) {
1802 /* if the request is satisfied, then we try to find
1803 * an extent that still satisfy the request, but is
1804 * smaller than previous one */
1805 if (ex
->fe_len
< bex
->fe_len
)
1809 ext4_mb_check_limits(ac
, e4b
, 0);
1812 static noinline_for_stack
1813 int ext4_mb_try_best_found(struct ext4_allocation_context
*ac
,
1814 struct ext4_buddy
*e4b
)
1816 struct ext4_free_extent ex
= ac
->ac_b_ex
;
1817 ext4_group_t group
= ex
.fe_group
;
1821 BUG_ON(ex
.fe_len
<= 0);
1822 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1826 ext4_lock_group(ac
->ac_sb
, group
);
1827 max
= mb_find_extent(e4b
, ex
.fe_start
, ex
.fe_len
, &ex
);
1831 ext4_mb_use_best_found(ac
, e4b
);
1834 ext4_unlock_group(ac
->ac_sb
, group
);
1835 ext4_mb_unload_buddy(e4b
);
1840 static noinline_for_stack
1841 int ext4_mb_find_by_goal(struct ext4_allocation_context
*ac
,
1842 struct ext4_buddy
*e4b
)
1844 ext4_group_t group
= ac
->ac_g_ex
.fe_group
;
1847 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1848 struct ext4_group_info
*grp
= ext4_get_group_info(ac
->ac_sb
, group
);
1849 struct ext4_free_extent ex
;
1851 if (!(ac
->ac_flags
& EXT4_MB_HINT_TRY_GOAL
))
1853 if (grp
->bb_free
== 0)
1856 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1860 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b
->bd_info
))) {
1861 ext4_mb_unload_buddy(e4b
);
1865 ext4_lock_group(ac
->ac_sb
, group
);
1866 max
= mb_find_extent(e4b
, ac
->ac_g_ex
.fe_start
,
1867 ac
->ac_g_ex
.fe_len
, &ex
);
1868 ex
.fe_logical
= 0xDEADFA11; /* debug value */
1870 if (max
>= ac
->ac_g_ex
.fe_len
&& ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
) {
1873 start
= ext4_group_first_block_no(ac
->ac_sb
, e4b
->bd_group
) +
1875 /* use do_div to get remainder (would be 64-bit modulo) */
1876 if (do_div(start
, sbi
->s_stripe
) == 0) {
1879 ext4_mb_use_best_found(ac
, e4b
);
1881 } else if (max
>= ac
->ac_g_ex
.fe_len
) {
1882 BUG_ON(ex
.fe_len
<= 0);
1883 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1884 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1887 ext4_mb_use_best_found(ac
, e4b
);
1888 } else if (max
> 0 && (ac
->ac_flags
& EXT4_MB_HINT_MERGE
)) {
1889 /* Sometimes, caller may want to merge even small
1890 * number of blocks to an existing extent */
1891 BUG_ON(ex
.fe_len
<= 0);
1892 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1893 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1896 ext4_mb_use_best_found(ac
, e4b
);
1898 ext4_unlock_group(ac
->ac_sb
, group
);
1899 ext4_mb_unload_buddy(e4b
);
1905 * The routine scans buddy structures (not bitmap!) from given order
1906 * to max order and tries to find big enough chunk to satisfy the req
1908 static noinline_for_stack
1909 void ext4_mb_simple_scan_group(struct ext4_allocation_context
*ac
,
1910 struct ext4_buddy
*e4b
)
1912 struct super_block
*sb
= ac
->ac_sb
;
1913 struct ext4_group_info
*grp
= e4b
->bd_info
;
1919 BUG_ON(ac
->ac_2order
<= 0);
1920 for (i
= ac
->ac_2order
; i
<= sb
->s_blocksize_bits
+ 1; i
++) {
1921 if (grp
->bb_counters
[i
] == 0)
1924 buddy
= mb_find_buddy(e4b
, i
, &max
);
1925 BUG_ON(buddy
== NULL
);
1927 k
= mb_find_next_zero_bit(buddy
, max
, 0);
1932 ac
->ac_b_ex
.fe_len
= 1 << i
;
1933 ac
->ac_b_ex
.fe_start
= k
<< i
;
1934 ac
->ac_b_ex
.fe_group
= e4b
->bd_group
;
1936 ext4_mb_use_best_found(ac
, e4b
);
1938 BUG_ON(ac
->ac_b_ex
.fe_len
!= ac
->ac_g_ex
.fe_len
);
1940 if (EXT4_SB(sb
)->s_mb_stats
)
1941 atomic_inc(&EXT4_SB(sb
)->s_bal_2orders
);
1948 * The routine scans the group and measures all found extents.
1949 * In order to optimize scanning, caller must pass number of
1950 * free blocks in the group, so the routine can know upper limit.
1952 static noinline_for_stack
1953 void ext4_mb_complex_scan_group(struct ext4_allocation_context
*ac
,
1954 struct ext4_buddy
*e4b
)
1956 struct super_block
*sb
= ac
->ac_sb
;
1957 void *bitmap
= e4b
->bd_bitmap
;
1958 struct ext4_free_extent ex
;
1962 free
= e4b
->bd_info
->bb_free
;
1965 i
= e4b
->bd_info
->bb_first_free
;
1967 while (free
&& ac
->ac_status
== AC_STATUS_CONTINUE
) {
1968 i
= mb_find_next_zero_bit(bitmap
,
1969 EXT4_CLUSTERS_PER_GROUP(sb
), i
);
1970 if (i
>= EXT4_CLUSTERS_PER_GROUP(sb
)) {
1972 * IF we have corrupt bitmap, we won't find any
1973 * free blocks even though group info says we
1974 * we have free blocks
1976 ext4_grp_locked_error(sb
, e4b
->bd_group
, 0, 0,
1977 "%d free clusters as per "
1978 "group info. But bitmap says 0",
1983 mb_find_extent(e4b
, i
, ac
->ac_g_ex
.fe_len
, &ex
);
1984 BUG_ON(ex
.fe_len
<= 0);
1985 if (free
< ex
.fe_len
) {
1986 ext4_grp_locked_error(sb
, e4b
->bd_group
, 0, 0,
1987 "%d free clusters as per "
1988 "group info. But got %d blocks",
1991 * The number of free blocks differs. This mostly
1992 * indicate that the bitmap is corrupt. So exit
1993 * without claiming the space.
1997 ex
.fe_logical
= 0xDEADC0DE; /* debug value */
1998 ext4_mb_measure_extent(ac
, &ex
, e4b
);
2004 ext4_mb_check_limits(ac
, e4b
, 1);
2008 * This is a special case for storages like raid5
2009 * we try to find stripe-aligned chunks for stripe-size-multiple requests
2011 static noinline_for_stack
2012 void ext4_mb_scan_aligned(struct ext4_allocation_context
*ac
,
2013 struct ext4_buddy
*e4b
)
2015 struct super_block
*sb
= ac
->ac_sb
;
2016 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2017 void *bitmap
= e4b
->bd_bitmap
;
2018 struct ext4_free_extent ex
;
2019 ext4_fsblk_t first_group_block
;
2024 BUG_ON(sbi
->s_stripe
== 0);
2026 /* find first stripe-aligned block in group */
2027 first_group_block
= ext4_group_first_block_no(sb
, e4b
->bd_group
);
2029 a
= first_group_block
+ sbi
->s_stripe
- 1;
2030 do_div(a
, sbi
->s_stripe
);
2031 i
= (a
* sbi
->s_stripe
) - first_group_block
;
2033 while (i
< EXT4_CLUSTERS_PER_GROUP(sb
)) {
2034 if (!mb_test_bit(i
, bitmap
)) {
2035 max
= mb_find_extent(e4b
, i
, sbi
->s_stripe
, &ex
);
2036 if (max
>= sbi
->s_stripe
) {
2038 ex
.fe_logical
= 0xDEADF00D; /* debug value */
2040 ext4_mb_use_best_found(ac
, e4b
);
2049 * This is now called BEFORE we load the buddy bitmap.
2050 * Returns either 1 or 0 indicating that the group is either suitable
2051 * for the allocation or not. In addition it can also return negative
2052 * error code when something goes wrong.
2054 static int ext4_mb_good_group(struct ext4_allocation_context
*ac
,
2055 ext4_group_t group
, int cr
)
2057 unsigned free
, fragments
;
2058 int flex_size
= ext4_flex_bg_size(EXT4_SB(ac
->ac_sb
));
2059 struct ext4_group_info
*grp
= ext4_get_group_info(ac
->ac_sb
, group
);
2061 BUG_ON(cr
< 0 || cr
>= 4);
2063 free
= grp
->bb_free
;
2066 if (cr
<= 2 && free
< ac
->ac_g_ex
.fe_len
)
2069 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp
)))
2072 /* We only do this if the grp has never been initialized */
2073 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp
))) {
2074 int ret
= ext4_mb_init_group(ac
->ac_sb
, group
, GFP_NOFS
);
2079 fragments
= grp
->bb_fragments
;
2085 BUG_ON(ac
->ac_2order
== 0);
2087 /* Avoid using the first bg of a flexgroup for data files */
2088 if ((ac
->ac_flags
& EXT4_MB_HINT_DATA
) &&
2089 (flex_size
>= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
) &&
2090 ((group
% flex_size
) == 0))
2093 if ((ac
->ac_2order
> ac
->ac_sb
->s_blocksize_bits
+1) ||
2094 (free
/ fragments
) >= ac
->ac_g_ex
.fe_len
)
2097 if (grp
->bb_largest_free_order
< ac
->ac_2order
)
2102 if ((free
/ fragments
) >= ac
->ac_g_ex
.fe_len
)
2106 if (free
>= ac
->ac_g_ex
.fe_len
)
2118 static noinline_for_stack
int
2119 ext4_mb_regular_allocator(struct ext4_allocation_context
*ac
)
2121 ext4_group_t ngroups
, group
, i
;
2123 int err
= 0, first_err
= 0;
2124 struct ext4_sb_info
*sbi
;
2125 struct super_block
*sb
;
2126 struct ext4_buddy e4b
;
2130 ngroups
= ext4_get_groups_count(sb
);
2131 /* non-extent files are limited to low blocks/groups */
2132 if (!(ext4_test_inode_flag(ac
->ac_inode
, EXT4_INODE_EXTENTS
)))
2133 ngroups
= sbi
->s_blockfile_groups
;
2135 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
2137 /* first, try the goal */
2138 err
= ext4_mb_find_by_goal(ac
, &e4b
);
2139 if (err
|| ac
->ac_status
== AC_STATUS_FOUND
)
2142 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
2146 * ac->ac2_order is set only if the fe_len is a power of 2
2147 * if ac2_order is set we also set criteria to 0 so that we
2148 * try exact allocation using buddy.
2150 i
= fls(ac
->ac_g_ex
.fe_len
);
2153 * We search using buddy data only if the order of the request
2154 * is greater than equal to the sbi_s_mb_order2_reqs
2155 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
2156 * We also support searching for power-of-two requests only for
2157 * requests upto maximum buddy size we have constructed.
2159 if (i
>= sbi
->s_mb_order2_reqs
&& i
<= sb
->s_blocksize_bits
+ 2) {
2161 * This should tell if fe_len is exactly power of 2
2163 if ((ac
->ac_g_ex
.fe_len
& (~(1 << (i
- 1)))) == 0)
2164 ac
->ac_2order
= i
- 1;
2167 /* if stream allocation is enabled, use global goal */
2168 if (ac
->ac_flags
& EXT4_MB_STREAM_ALLOC
) {
2169 /* TBD: may be hot point */
2170 spin_lock(&sbi
->s_md_lock
);
2171 ac
->ac_g_ex
.fe_group
= sbi
->s_mb_last_group
;
2172 ac
->ac_g_ex
.fe_start
= sbi
->s_mb_last_start
;
2173 spin_unlock(&sbi
->s_md_lock
);
2176 /* Let's just scan groups to find more-less suitable blocks */
2177 cr
= ac
->ac_2order
? 0 : 1;
2179 * cr == 0 try to get exact allocation,
2180 * cr == 3 try to get anything
2183 for (; cr
< 4 && ac
->ac_status
== AC_STATUS_CONTINUE
; cr
++) {
2184 ac
->ac_criteria
= cr
;
2186 * searching for the right group start
2187 * from the goal value specified
2189 group
= ac
->ac_g_ex
.fe_group
;
2191 for (i
= 0; i
< ngroups
; group
++, i
++) {
2195 * Artificially restricted ngroups for non-extent
2196 * files makes group > ngroups possible on first loop.
2198 if (group
>= ngroups
)
2201 /* This now checks without needing the buddy page */
2202 ret
= ext4_mb_good_group(ac
, group
, cr
);
2209 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2213 ext4_lock_group(sb
, group
);
2216 * We need to check again after locking the
2219 ret
= ext4_mb_good_group(ac
, group
, cr
);
2221 ext4_unlock_group(sb
, group
);
2222 ext4_mb_unload_buddy(&e4b
);
2228 ac
->ac_groups_scanned
++;
2230 ext4_mb_simple_scan_group(ac
, &e4b
);
2231 else if (cr
== 1 && sbi
->s_stripe
&&
2232 !(ac
->ac_g_ex
.fe_len
% sbi
->s_stripe
))
2233 ext4_mb_scan_aligned(ac
, &e4b
);
2235 ext4_mb_complex_scan_group(ac
, &e4b
);
2237 ext4_unlock_group(sb
, group
);
2238 ext4_mb_unload_buddy(&e4b
);
2240 if (ac
->ac_status
!= AC_STATUS_CONTINUE
)
2245 if (ac
->ac_b_ex
.fe_len
> 0 && ac
->ac_status
!= AC_STATUS_FOUND
&&
2246 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
2248 * We've been searching too long. Let's try to allocate
2249 * the best chunk we've found so far
2252 ext4_mb_try_best_found(ac
, &e4b
);
2253 if (ac
->ac_status
!= AC_STATUS_FOUND
) {
2255 * Someone more lucky has already allocated it.
2256 * The only thing we can do is just take first
2258 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2260 ac
->ac_b_ex
.fe_group
= 0;
2261 ac
->ac_b_ex
.fe_start
= 0;
2262 ac
->ac_b_ex
.fe_len
= 0;
2263 ac
->ac_status
= AC_STATUS_CONTINUE
;
2264 ac
->ac_flags
|= EXT4_MB_HINT_FIRST
;
2266 atomic_inc(&sbi
->s_mb_lost_chunks
);
2271 if (!err
&& ac
->ac_status
!= AC_STATUS_FOUND
&& first_err
)
2276 static void *ext4_mb_seq_groups_start(struct seq_file
*seq
, loff_t
*pos
)
2278 struct super_block
*sb
= seq
->private;
2281 if (*pos
< 0 || *pos
>= ext4_get_groups_count(sb
))
2284 return (void *) ((unsigned long) group
);
2287 static void *ext4_mb_seq_groups_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2289 struct super_block
*sb
= seq
->private;
2293 if (*pos
< 0 || *pos
>= ext4_get_groups_count(sb
))
2296 return (void *) ((unsigned long) group
);
2299 static int ext4_mb_seq_groups_show(struct seq_file
*seq
, void *v
)
2301 struct super_block
*sb
= seq
->private;
2302 ext4_group_t group
= (ext4_group_t
) ((unsigned long) v
);
2304 int err
, buddy_loaded
= 0;
2305 struct ext4_buddy e4b
;
2306 struct ext4_group_info
*grinfo
;
2308 struct ext4_group_info info
;
2309 ext4_grpblk_t counters
[EXT4_MAX_BLOCK_LOG_SIZE
+ 2];
2314 seq_puts(seq
, "#group: free frags first ["
2315 " 2^0 2^1 2^2 2^3 2^4 2^5 2^6 "
2316 " 2^7 2^8 2^9 2^10 2^11 2^12 2^13 ]");
2318 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(sg
.info
.bb_counters
[0]) +
2319 sizeof(struct ext4_group_info
);
2320 grinfo
= ext4_get_group_info(sb
, group
);
2321 /* Load the group info in memory only if not already loaded. */
2322 if (unlikely(EXT4_MB_GRP_NEED_INIT(grinfo
))) {
2323 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2325 seq_printf(seq
, "#%-5u: I/O error\n", group
);
2331 memcpy(&sg
, ext4_get_group_info(sb
, group
), i
);
2334 ext4_mb_unload_buddy(&e4b
);
2336 seq_printf(seq
, "#%-5u: %-5u %-5u %-5u [", group
, sg
.info
.bb_free
,
2337 sg
.info
.bb_fragments
, sg
.info
.bb_first_free
);
2338 for (i
= 0; i
<= 13; i
++)
2339 seq_printf(seq
, " %-5u", i
<= sb
->s_blocksize_bits
+ 1 ?
2340 sg
.info
.bb_counters
[i
] : 0);
2341 seq_printf(seq
, " ]\n");
2346 static void ext4_mb_seq_groups_stop(struct seq_file
*seq
, void *v
)
2350 static const struct seq_operations ext4_mb_seq_groups_ops
= {
2351 .start
= ext4_mb_seq_groups_start
,
2352 .next
= ext4_mb_seq_groups_next
,
2353 .stop
= ext4_mb_seq_groups_stop
,
2354 .show
= ext4_mb_seq_groups_show
,
2357 static int ext4_mb_seq_groups_open(struct inode
*inode
, struct file
*file
)
2359 struct super_block
*sb
= PDE_DATA(inode
);
2362 rc
= seq_open(file
, &ext4_mb_seq_groups_ops
);
2364 struct seq_file
*m
= file
->private_data
;
2371 const struct file_operations ext4_seq_mb_groups_fops
= {
2372 .owner
= THIS_MODULE
,
2373 .open
= ext4_mb_seq_groups_open
,
2375 .llseek
= seq_lseek
,
2376 .release
= seq_release
,
2379 ssize_t
ext4_mb_freefrag_show(struct ext4_sb_info
*sbi
, char *buf
)
2381 #define EXT4_FREEFRAG_COLUMN 14 /* sb->s_blocksize_bits + 2 */
2382 ext4_group_t group
= 0;
2384 ext4_fsblk_t freeblock
[EXT4_FREEFRAG_COLUMN
] = {0,};
2385 char *size
[EXT4_FREEFRAG_COLUMN
] = {"4K","8K","16K","32K","64K",
2386 "128K","256K","512K","1M","2M","4M","8M","16M","32M"};
2388 for (group
= 0; group
< sbi
->s_groups_count
; group
++) {
2389 struct super_block
*sb
= sbi
->s_sb
;
2390 int err
, buddy_loaded
= 0;
2391 struct ext4_buddy e4b
;
2392 struct ext4_group_info
*grinfo
;
2394 struct ext4_group_info info
;
2395 ext4_grpblk_t counters
[EXT4_FREEFRAG_COLUMN
+2];
2398 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(sg
.info
.bb_counters
[0]) +
2399 sizeof(struct ext4_group_info
);
2400 grinfo
= ext4_get_group_info(sb
, group
);
2401 /* Load the group info in memory only if not already loaded. */
2402 if (unlikely(EXT4_MB_GRP_NEED_INIT(grinfo
))) {
2403 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2405 freeblock
[0] = ULLONG_MAX
;
2411 memcpy(&sg
, ext4_get_group_info(sb
, group
), i
);
2414 ext4_mb_unload_buddy(&e4b
);
2415 for (i
= 0; i
< EXT4_FREEFRAG_COLUMN
; i
++)
2416 freeblock
[i
] += (i
<= sb
->s_blocksize_bits
+ 1) ?
2417 sg
.info
.bb_counters
[i
]: 0;
2420 for (i
=0; i
< EXT4_FREEFRAG_COLUMN
; i
++)
2421 snprintf(buf
, PAGE_SIZE
, "%s\"%s\":\"%llu\",", buf
, size
[i
],
2422 (unsigned long long)freeblock
[i
]);
2423 buf
[strlen(buf
)-1] = '\n';
2428 static struct kmem_cache
*get_groupinfo_cache(int blocksize_bits
)
2430 int cache_index
= blocksize_bits
- EXT4_MIN_BLOCK_LOG_SIZE
;
2431 struct kmem_cache
*cachep
= ext4_groupinfo_caches
[cache_index
];
2438 * Allocate the top-level s_group_info array for the specified number
2441 int ext4_mb_alloc_groupinfo(struct super_block
*sb
, ext4_group_t ngroups
)
2443 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2445 struct ext4_group_info
***new_groupinfo
;
2447 size
= (ngroups
+ EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2448 EXT4_DESC_PER_BLOCK_BITS(sb
);
2449 if (size
<= sbi
->s_group_info_size
)
2452 size
= roundup_pow_of_two(sizeof(*sbi
->s_group_info
) * size
);
2453 new_groupinfo
= ext4_kvzalloc(size
, GFP_KERNEL
);
2454 if (!new_groupinfo
) {
2455 ext4_msg(sb
, KERN_ERR
, "can't allocate buddy meta group");
2458 if (sbi
->s_group_info
) {
2459 memcpy(new_groupinfo
, sbi
->s_group_info
,
2460 sbi
->s_group_info_size
* sizeof(*sbi
->s_group_info
));
2461 kvfree(sbi
->s_group_info
);
2463 sbi
->s_group_info
= new_groupinfo
;
2464 sbi
->s_group_info_size
= size
/ sizeof(*sbi
->s_group_info
);
2465 ext4_debug("allocated s_groupinfo array for %d meta_bg's\n",
2466 sbi
->s_group_info_size
);
2470 /* Create and initialize ext4_group_info data for the given group. */
2471 int ext4_mb_add_groupinfo(struct super_block
*sb
, ext4_group_t group
,
2472 struct ext4_group_desc
*desc
)
2476 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2477 struct ext4_group_info
**meta_group_info
;
2478 struct kmem_cache
*cachep
= get_groupinfo_cache(sb
->s_blocksize_bits
);
2481 * First check if this group is the first of a reserved block.
2482 * If it's true, we have to allocate a new table of pointers
2483 * to ext4_group_info structures
2485 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0) {
2486 metalen
= sizeof(*meta_group_info
) <<
2487 EXT4_DESC_PER_BLOCK_BITS(sb
);
2488 meta_group_info
= kmalloc(metalen
, GFP_NOFS
);
2489 if (meta_group_info
== NULL
) {
2490 ext4_msg(sb
, KERN_ERR
, "can't allocate mem "
2491 "for a buddy group");
2492 goto exit_meta_group_info
;
2494 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)] =
2499 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)];
2500 i
= group
& (EXT4_DESC_PER_BLOCK(sb
) - 1);
2502 meta_group_info
[i
] = kmem_cache_zalloc(cachep
, GFP_NOFS
);
2503 if (meta_group_info
[i
] == NULL
) {
2504 ext4_msg(sb
, KERN_ERR
, "can't allocate buddy mem");
2505 goto exit_group_info
;
2507 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
,
2508 &(meta_group_info
[i
]->bb_state
));
2511 * initialize bb_free to be able to skip
2512 * empty groups without initialization
2514 if (ext4_has_group_desc_csum(sb
) &&
2515 (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
))) {
2516 meta_group_info
[i
]->bb_free
=
2517 ext4_free_clusters_after_init(sb
, group
, desc
);
2519 meta_group_info
[i
]->bb_free
=
2520 ext4_free_group_clusters(sb
, desc
);
2523 INIT_LIST_HEAD(&meta_group_info
[i
]->bb_prealloc_list
);
2524 init_rwsem(&meta_group_info
[i
]->alloc_sem
);
2525 meta_group_info
[i
]->bb_free_root
= RB_ROOT
;
2526 meta_group_info
[i
]->bb_largest_free_order
= -1; /* uninit */
2530 struct buffer_head
*bh
;
2531 meta_group_info
[i
]->bb_bitmap
=
2532 kmalloc(sb
->s_blocksize
, GFP_NOFS
);
2533 BUG_ON(meta_group_info
[i
]->bb_bitmap
== NULL
);
2534 bh
= ext4_read_block_bitmap(sb
, group
);
2535 BUG_ON(IS_ERR_OR_NULL(bh
));
2536 memcpy(meta_group_info
[i
]->bb_bitmap
, bh
->b_data
,
2545 /* If a meta_group_info table has been allocated, release it now */
2546 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0) {
2547 kfree(sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)]);
2548 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)] = NULL
;
2550 exit_meta_group_info
:
2552 } /* ext4_mb_add_groupinfo */
2554 static int ext4_mb_init_backend(struct super_block
*sb
)
2556 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
2558 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2560 struct ext4_group_desc
*desc
;
2561 struct kmem_cache
*cachep
;
2563 err
= ext4_mb_alloc_groupinfo(sb
, ngroups
);
2567 sbi
->s_buddy_cache
= new_inode(sb
);
2568 if (sbi
->s_buddy_cache
== NULL
) {
2569 ext4_msg(sb
, KERN_ERR
, "can't get new inode");
2572 /* To avoid potentially colliding with an valid on-disk inode number,
2573 * use EXT4_BAD_INO for the buddy cache inode number. This inode is
2574 * not in the inode hash, so it should never be found by iget(), but
2575 * this will avoid confusion if it ever shows up during debugging. */
2576 sbi
->s_buddy_cache
->i_ino
= EXT4_BAD_INO
;
2577 EXT4_I(sbi
->s_buddy_cache
)->i_disksize
= 0;
2578 for (i
= 0; i
< ngroups
; i
++) {
2579 desc
= ext4_get_group_desc(sb
, i
, NULL
);
2581 ext4_msg(sb
, KERN_ERR
, "can't read descriptor %u", i
);
2584 if (ext4_mb_add_groupinfo(sb
, i
, desc
) != 0)
2591 cachep
= get_groupinfo_cache(sb
->s_blocksize_bits
);
2593 kmem_cache_free(cachep
, ext4_get_group_info(sb
, i
));
2594 i
= sbi
->s_group_info_size
;
2596 kfree(sbi
->s_group_info
[i
]);
2597 iput(sbi
->s_buddy_cache
);
2599 kvfree(sbi
->s_group_info
);
2603 static void ext4_groupinfo_destroy_slabs(void)
2607 for (i
= 0; i
< NR_GRPINFO_CACHES
; i
++) {
2608 if (ext4_groupinfo_caches
[i
])
2609 kmem_cache_destroy(ext4_groupinfo_caches
[i
]);
2610 ext4_groupinfo_caches
[i
] = NULL
;
2614 static int ext4_groupinfo_create_slab(size_t size
)
2616 static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex
);
2618 int blocksize_bits
= order_base_2(size
);
2619 int cache_index
= blocksize_bits
- EXT4_MIN_BLOCK_LOG_SIZE
;
2620 struct kmem_cache
*cachep
;
2622 if (cache_index
>= NR_GRPINFO_CACHES
)
2625 if (unlikely(cache_index
< 0))
2628 mutex_lock(&ext4_grpinfo_slab_create_mutex
);
2629 if (ext4_groupinfo_caches
[cache_index
]) {
2630 mutex_unlock(&ext4_grpinfo_slab_create_mutex
);
2631 return 0; /* Already created */
2634 slab_size
= offsetof(struct ext4_group_info
,
2635 bb_counters
[blocksize_bits
+ 2]);
2637 cachep
= kmem_cache_create(ext4_groupinfo_slab_names
[cache_index
],
2638 slab_size
, 0, SLAB_RECLAIM_ACCOUNT
,
2641 ext4_groupinfo_caches
[cache_index
] = cachep
;
2643 mutex_unlock(&ext4_grpinfo_slab_create_mutex
);
2646 "EXT4-fs: no memory for groupinfo slab cache\n");
2653 int ext4_mb_init(struct super_block
*sb
)
2655 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2657 unsigned offset
, offset_incr
;
2661 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(*sbi
->s_mb_offsets
);
2663 sbi
->s_mb_offsets
= kmalloc(i
, GFP_KERNEL
);
2664 if (sbi
->s_mb_offsets
== NULL
) {
2669 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(*sbi
->s_mb_maxs
);
2670 sbi
->s_mb_maxs
= kmalloc(i
, GFP_KERNEL
);
2671 if (sbi
->s_mb_maxs
== NULL
) {
2676 ret
= ext4_groupinfo_create_slab(sb
->s_blocksize
);
2680 /* order 0 is regular bitmap */
2681 sbi
->s_mb_maxs
[0] = sb
->s_blocksize
<< 3;
2682 sbi
->s_mb_offsets
[0] = 0;
2686 offset_incr
= 1 << (sb
->s_blocksize_bits
- 1);
2687 max
= sb
->s_blocksize
<< 2;
2689 sbi
->s_mb_offsets
[i
] = offset
;
2690 sbi
->s_mb_maxs
[i
] = max
;
2691 offset
+= offset_incr
;
2692 offset_incr
= offset_incr
>> 1;
2695 } while (i
<= sb
->s_blocksize_bits
+ 1);
2697 spin_lock_init(&sbi
->s_md_lock
);
2698 spin_lock_init(&sbi
->s_bal_lock
);
2699 INIT_LIST_HEAD(&sbi
->s_freed_data_list
);
2701 sbi
->s_mb_max_to_scan
= MB_DEFAULT_MAX_TO_SCAN
;
2702 sbi
->s_mb_min_to_scan
= MB_DEFAULT_MIN_TO_SCAN
;
2703 sbi
->s_mb_stats
= MB_DEFAULT_STATS
;
2704 sbi
->s_mb_stream_request
= MB_DEFAULT_STREAM_THRESHOLD
;
2705 sbi
->s_mb_order2_reqs
= MB_DEFAULT_ORDER2_REQS
;
2707 * The default group preallocation is 512, which for 4k block
2708 * sizes translates to 2 megabytes. However for bigalloc file
2709 * systems, this is probably too big (i.e, if the cluster size
2710 * is 1 megabyte, then group preallocation size becomes half a
2711 * gigabyte!). As a default, we will keep a two megabyte
2712 * group pralloc size for cluster sizes up to 64k, and after
2713 * that, we will force a minimum group preallocation size of
2714 * 32 clusters. This translates to 8 megs when the cluster
2715 * size is 256k, and 32 megs when the cluster size is 1 meg,
2716 * which seems reasonable as a default.
2718 sbi
->s_mb_group_prealloc
= max(MB_DEFAULT_GROUP_PREALLOC
>>
2719 sbi
->s_cluster_bits
, 32);
2721 * If there is a s_stripe > 1, then we set the s_mb_group_prealloc
2722 * to the lowest multiple of s_stripe which is bigger than
2723 * the s_mb_group_prealloc as determined above. We want
2724 * the preallocation size to be an exact multiple of the
2725 * RAID stripe size so that preallocations don't fragment
2728 if (sbi
->s_stripe
> 1) {
2729 sbi
->s_mb_group_prealloc
= roundup(
2730 sbi
->s_mb_group_prealloc
, sbi
->s_stripe
);
2733 sbi
->s_locality_groups
= alloc_percpu(struct ext4_locality_group
);
2734 if (sbi
->s_locality_groups
== NULL
) {
2738 for_each_possible_cpu(i
) {
2739 struct ext4_locality_group
*lg
;
2740 lg
= per_cpu_ptr(sbi
->s_locality_groups
, i
);
2741 mutex_init(&lg
->lg_mutex
);
2742 for (j
= 0; j
< PREALLOC_TB_SIZE
; j
++)
2743 INIT_LIST_HEAD(&lg
->lg_prealloc_list
[j
]);
2744 spin_lock_init(&lg
->lg_prealloc_lock
);
2747 /* init file for buddy data */
2748 ret
= ext4_mb_init_backend(sb
);
2750 goto out_free_locality_groups
;
2754 out_free_locality_groups
:
2755 free_percpu(sbi
->s_locality_groups
);
2756 sbi
->s_locality_groups
= NULL
;
2758 kfree(sbi
->s_mb_offsets
);
2759 sbi
->s_mb_offsets
= NULL
;
2760 kfree(sbi
->s_mb_maxs
);
2761 sbi
->s_mb_maxs
= NULL
;
2765 /* need to called with the ext4 group lock held */
2766 static void ext4_mb_cleanup_pa(struct ext4_group_info
*grp
)
2768 struct ext4_prealloc_space
*pa
;
2769 struct list_head
*cur
, *tmp
;
2772 list_for_each_safe(cur
, tmp
, &grp
->bb_prealloc_list
) {
2773 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
2774 list_del(&pa
->pa_group_list
);
2776 kmem_cache_free(ext4_pspace_cachep
, pa
);
2779 mb_debug(1, "mballoc: %u PAs left\n", count
);
2783 int ext4_mb_release(struct super_block
*sb
)
2785 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
2787 int num_meta_group_infos
;
2788 struct ext4_group_info
*grinfo
;
2789 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2790 struct kmem_cache
*cachep
= get_groupinfo_cache(sb
->s_blocksize_bits
);
2792 if (sbi
->s_group_info
) {
2793 for (i
= 0; i
< ngroups
; i
++) {
2794 grinfo
= ext4_get_group_info(sb
, i
);
2796 kfree(grinfo
->bb_bitmap
);
2798 ext4_lock_group(sb
, i
);
2799 ext4_mb_cleanup_pa(grinfo
);
2800 ext4_unlock_group(sb
, i
);
2801 kmem_cache_free(cachep
, grinfo
);
2803 num_meta_group_infos
= (ngroups
+
2804 EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2805 EXT4_DESC_PER_BLOCK_BITS(sb
);
2806 for (i
= 0; i
< num_meta_group_infos
; i
++)
2807 kfree(sbi
->s_group_info
[i
]);
2808 kvfree(sbi
->s_group_info
);
2810 kfree(sbi
->s_mb_offsets
);
2811 kfree(sbi
->s_mb_maxs
);
2812 iput(sbi
->s_buddy_cache
);
2813 if (sbi
->s_mb_stats
) {
2814 ext4_msg(sb
, KERN_INFO
,
2815 "mballoc: %u blocks %u reqs (%u success)",
2816 atomic_read(&sbi
->s_bal_allocated
),
2817 atomic_read(&sbi
->s_bal_reqs
),
2818 atomic_read(&sbi
->s_bal_success
));
2819 ext4_msg(sb
, KERN_INFO
,
2820 "mballoc: %u extents scanned, %u goal hits, "
2821 "%u 2^N hits, %u breaks, %u lost",
2822 atomic_read(&sbi
->s_bal_ex_scanned
),
2823 atomic_read(&sbi
->s_bal_goals
),
2824 atomic_read(&sbi
->s_bal_2orders
),
2825 atomic_read(&sbi
->s_bal_breaks
),
2826 atomic_read(&sbi
->s_mb_lost_chunks
));
2827 ext4_msg(sb
, KERN_INFO
,
2828 "mballoc: %lu generated and it took %Lu",
2829 sbi
->s_mb_buddies_generated
,
2830 sbi
->s_mb_generation_time
);
2831 ext4_msg(sb
, KERN_INFO
,
2832 "mballoc: %u preallocated, %u discarded",
2833 atomic_read(&sbi
->s_mb_preallocated
),
2834 atomic_read(&sbi
->s_mb_discarded
));
2837 free_percpu(sbi
->s_locality_groups
);
2842 static inline int ext4_issue_discard(struct super_block
*sb
,
2843 ext4_group_t block_group
, ext4_grpblk_t cluster
, int count
,
2844 unsigned long flags
, struct bio
**biop
)
2846 ext4_fsblk_t discard_block
;
2848 discard_block
= (EXT4_C2B(EXT4_SB(sb
), cluster
) +
2849 ext4_group_first_block_no(sb
, block_group
));
2850 count
= EXT4_C2B(EXT4_SB(sb
), count
);
2851 trace_ext4_discard_blocks(sb
,
2852 (unsigned long long) discard_block
, count
);
2854 return __blkdev_issue_discard(sb
->s_bdev
,
2855 (sector_t
)discard_block
<< (sb
->s_blocksize_bits
- 9),
2856 (sector_t
)count
<< (sb
->s_blocksize_bits
- 9),
2857 GFP_NOFS
, flags
, biop
);
2859 return sb_issue_discard(sb
, discard_block
, count
,
2863 static void ext4_free_data_in_buddy(struct super_block
*sb
,
2864 struct ext4_free_data
*entry
)
2866 struct ext4_buddy e4b
;
2867 struct ext4_group_info
*db
;
2868 int err
, count
= 0, count2
= 0;
2870 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
2871 entry
->efd_count
, entry
->efd_group
, entry
);
2873 err
= ext4_mb_load_buddy(sb
, entry
->efd_group
, &e4b
);
2874 /* we expect to find existing buddy because it's pinned */
2878 /* there are blocks to put in buddy to make them really free */
2879 count
+= entry
->efd_count
;
2881 ext4_lock_group(sb
, entry
->efd_group
);
2882 /* Take it out of per group rb tree */
2883 rb_erase(&entry
->efd_node
, &(db
->bb_free_root
));
2884 mb_free_blocks(NULL
, &e4b
, entry
->efd_start_cluster
, entry
->efd_count
);
2887 * Clear the trimmed flag for the group so that the next
2888 * ext4_trim_fs can trim it.
2889 * If the volume is mounted with -o discard, online discard
2890 * is supported and the free blocks will be trimmed online.
2892 if (!test_opt(sb
, DISCARD
))
2893 EXT4_MB_GRP_CLEAR_TRIMMED(db
);
2895 if (!db
->bb_free_root
.rb_node
) {
2896 /* No more items in the per group rb tree
2897 * balance refcounts from ext4_mb_free_metadata()
2899 page_cache_release(e4b
.bd_buddy_page
);
2900 page_cache_release(e4b
.bd_bitmap_page
);
2902 ext4_unlock_group(sb
, entry
->efd_group
);
2903 kmem_cache_free(ext4_free_data_cachep
, entry
);
2904 ext4_mb_unload_buddy(&e4b
);
2906 mb_debug(1, "freed %u blocks in %u structures\n", count
, count2
);
2910 * This function is called by the jbd2 layer once the commit has finished,
2911 * so we know we can free the blocks that were released with that commit.
2913 void ext4_process_freed_data(struct super_block
*sb
, tid_t commit_tid
)
2915 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2916 struct ext4_free_data
*entry
, *tmp
;
2917 struct bio
*discard_bio
= NULL
;
2918 struct list_head freed_data_list
;
2919 struct list_head
*cut_pos
= NULL
;
2920 int type
= REQ_WRITE
| REQ_DISCARD
| REQ_PRIO
;
2923 INIT_LIST_HEAD(&freed_data_list
);
2925 spin_lock(&sbi
->s_md_lock
);
2926 list_for_each_entry(entry
, &sbi
->s_freed_data_list
, efd_list
) {
2927 if (entry
->efd_tid
!= commit_tid
)
2929 cut_pos
= &entry
->efd_list
;
2932 list_cut_position(&freed_data_list
, &sbi
->s_freed_data_list
,
2934 spin_unlock(&sbi
->s_md_lock
);
2936 if (test_opt(sb
, DISCARD
)) {
2937 list_for_each_entry(entry
, &freed_data_list
, efd_list
) {
2938 err
= ext4_issue_discard(sb
, entry
->efd_group
,
2939 entry
->efd_start_cluster
,
2941 BLKDEV_DISCARD_SYNC
,
2943 if (err
&& err
!= -EOPNOTSUPP
) {
2944 ext4_msg(sb
, KERN_WARNING
, "discard request in"
2945 " group:%d block:%d count:%d failed"
2946 " with %d", entry
->efd_group
,
2947 entry
->efd_start_cluster
,
2948 entry
->efd_count
, err
);
2949 } else if (err
== -EOPNOTSUPP
)
2954 submit_bio_wait(type
, discard_bio
);
2955 bio_put(discard_bio
);
2959 list_for_each_entry_safe(entry
, tmp
, &freed_data_list
, efd_list
)
2960 ext4_free_data_in_buddy(sb
, entry
);
2963 int __init
ext4_init_mballoc(void)
2965 ext4_pspace_cachep
= KMEM_CACHE(ext4_prealloc_space
,
2966 SLAB_RECLAIM_ACCOUNT
);
2967 if (ext4_pspace_cachep
== NULL
)
2970 ext4_ac_cachep
= KMEM_CACHE(ext4_allocation_context
,
2971 SLAB_RECLAIM_ACCOUNT
);
2972 if (ext4_ac_cachep
== NULL
) {
2973 kmem_cache_destroy(ext4_pspace_cachep
);
2977 ext4_free_data_cachep
= KMEM_CACHE(ext4_free_data
,
2978 SLAB_RECLAIM_ACCOUNT
);
2979 if (ext4_free_data_cachep
== NULL
) {
2980 kmem_cache_destroy(ext4_pspace_cachep
);
2981 kmem_cache_destroy(ext4_ac_cachep
);
2987 void ext4_exit_mballoc(void)
2990 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2991 * before destroying the slab cache.
2994 kmem_cache_destroy(ext4_pspace_cachep
);
2995 kmem_cache_destroy(ext4_ac_cachep
);
2996 kmem_cache_destroy(ext4_free_data_cachep
);
2997 ext4_groupinfo_destroy_slabs();
3002 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
3003 * Returns 0 if success or error code
3005 static noinline_for_stack
int
3006 ext4_mb_mark_diskspace_used(struct ext4_allocation_context
*ac
,
3007 handle_t
*handle
, unsigned int reserv_clstrs
)
3009 struct buffer_head
*bitmap_bh
= NULL
;
3010 struct ext4_group_desc
*gdp
;
3011 struct buffer_head
*gdp_bh
;
3012 struct ext4_sb_info
*sbi
;
3013 struct super_block
*sb
;
3017 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3018 BUG_ON(ac
->ac_b_ex
.fe_len
<= 0);
3023 bitmap_bh
= ext4_read_block_bitmap(sb
, ac
->ac_b_ex
.fe_group
);
3024 if (IS_ERR(bitmap_bh
)) {
3025 err
= PTR_ERR(bitmap_bh
);
3030 BUFFER_TRACE(bitmap_bh
, "getting write access");
3031 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
3036 gdp
= ext4_get_group_desc(sb
, ac
->ac_b_ex
.fe_group
, &gdp_bh
);
3040 ext4_debug("using block group %u(%d)\n", ac
->ac_b_ex
.fe_group
,
3041 ext4_free_group_clusters(sb
, gdp
));
3043 BUFFER_TRACE(gdp_bh
, "get_write_access");
3044 err
= ext4_journal_get_write_access(handle
, gdp_bh
);
3048 block
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3050 len
= EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
);
3051 if (!ext4_data_block_valid(sbi
, block
, len
)) {
3052 ext4_error(sb
, "Allocating blocks %llu-%llu which overlap "
3053 "fs metadata", block
, block
+len
);
3054 /* File system mounted not to panic on error
3055 * Fix the bitmap and return EFSCORRUPTED
3056 * We leak some of the blocks here.
3058 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3059 ext4_set_bits(bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,
3060 ac
->ac_b_ex
.fe_len
);
3061 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3062 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
3064 err
= -EFSCORRUPTED
;
3068 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3069 #ifdef AGGRESSIVE_CHECK
3072 for (i
= 0; i
< ac
->ac_b_ex
.fe_len
; i
++) {
3073 BUG_ON(mb_test_bit(ac
->ac_b_ex
.fe_start
+ i
,
3074 bitmap_bh
->b_data
));
3078 ext4_set_bits(bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,
3079 ac
->ac_b_ex
.fe_len
);
3080 if (ext4_has_group_desc_csum(sb
) &&
3081 (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
))) {
3082 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT
);
3083 ext4_free_group_clusters_set(sb
, gdp
,
3084 ext4_free_clusters_after_init(sb
,
3085 ac
->ac_b_ex
.fe_group
, gdp
));
3087 len
= ext4_free_group_clusters(sb
, gdp
) - ac
->ac_b_ex
.fe_len
;
3088 ext4_free_group_clusters_set(sb
, gdp
, len
);
3089 ext4_block_bitmap_csum_set(sb
, ac
->ac_b_ex
.fe_group
, gdp
, bitmap_bh
);
3090 ext4_group_desc_csum_set(sb
, ac
->ac_b_ex
.fe_group
, gdp
);
3092 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3093 percpu_counter_sub(&sbi
->s_freeclusters_counter
, ac
->ac_b_ex
.fe_len
);
3095 * Now reduce the dirty block count also. Should not go negative
3097 if (!(ac
->ac_flags
& EXT4_MB_DELALLOC_RESERVED
))
3098 /* release all the reserved blocks if non delalloc */
3099 percpu_counter_sub(&sbi
->s_dirtyclusters_counter
,
3102 if (sbi
->s_log_groups_per_flex
) {
3103 ext4_group_t flex_group
= ext4_flex_group(sbi
,
3104 ac
->ac_b_ex
.fe_group
);
3105 atomic64_sub(ac
->ac_b_ex
.fe_len
,
3106 &sbi
->s_flex_groups
[flex_group
].free_clusters
);
3109 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
3112 err
= ext4_handle_dirty_metadata(handle
, NULL
, gdp_bh
);
3120 * here we normalize request for locality group
3121 * Group request are normalized to s_mb_group_prealloc, which goes to
3122 * s_strip if we set the same via mount option.
3123 * s_mb_group_prealloc can be configured via
3124 * /sys/fs/ext4/<partition>/mb_group_prealloc
3126 * XXX: should we try to preallocate more than the group has now?
3128 static void ext4_mb_normalize_group_request(struct ext4_allocation_context
*ac
)
3130 struct super_block
*sb
= ac
->ac_sb
;
3131 struct ext4_locality_group
*lg
= ac
->ac_lg
;
3134 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_mb_group_prealloc
;
3135 mb_debug(1, "#%u: goal %u blocks for locality group\n",
3136 current
->pid
, ac
->ac_g_ex
.fe_len
);
3140 * Normalization means making request better in terms of
3141 * size and alignment
3143 static noinline_for_stack
void
3144 ext4_mb_normalize_request(struct ext4_allocation_context
*ac
,
3145 struct ext4_allocation_request
*ar
)
3147 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3150 loff_t size
, start_off
;
3151 loff_t orig_size __maybe_unused
;
3153 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3154 struct ext4_prealloc_space
*pa
;
3156 /* do normalize only data requests, metadata requests
3157 do not need preallocation */
3158 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3161 /* sometime caller may want exact blocks */
3162 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
3165 /* caller may indicate that preallocation isn't
3166 * required (it's a tail, for example) */
3167 if (ac
->ac_flags
& EXT4_MB_HINT_NOPREALLOC
)
3170 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
) {
3171 ext4_mb_normalize_group_request(ac
);
3175 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
3177 /* first, let's learn actual file size
3178 * given current request is allocated */
3179 size
= ac
->ac_o_ex
.fe_logical
+ EXT4_C2B(sbi
, ac
->ac_o_ex
.fe_len
);
3180 size
= size
<< bsbits
;
3181 if (size
< i_size_read(ac
->ac_inode
))
3182 size
= i_size_read(ac
->ac_inode
);
3185 /* max size of free chunks */
3188 #define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3189 (req <= (size) || max <= (chunk_size))
3191 /* first, try to predict filesize */
3192 /* XXX: should this table be tunable? */
3194 if (size
<= 16 * 1024) {
3196 } else if (size
<= 32 * 1024) {
3198 } else if (size
<= 64 * 1024) {
3200 } else if (size
<= 128 * 1024) {
3202 } else if (size
<= 256 * 1024) {
3204 } else if (size
<= 512 * 1024) {
3206 } else if (size
<= 1024 * 1024) {
3208 } else if (NRL_CHECK_SIZE(size
, 4 * 1024 * 1024, max
, 2 * 1024)) {
3209 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3210 (21 - bsbits
)) << 21;
3211 size
= 2 * 1024 * 1024;
3212 } else if (NRL_CHECK_SIZE(size
, 8 * 1024 * 1024, max
, 4 * 1024)) {
3213 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3214 (22 - bsbits
)) << 22;
3215 size
= 4 * 1024 * 1024;
3216 } else if (NRL_CHECK_SIZE(ac
->ac_o_ex
.fe_len
,
3217 (8<<20)>>bsbits
, max
, 8 * 1024)) {
3218 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3219 (23 - bsbits
)) << 23;
3220 size
= 8 * 1024 * 1024;
3222 start_off
= (loff_t
) ac
->ac_o_ex
.fe_logical
<< bsbits
;
3223 size
= (loff_t
) EXT4_C2B(EXT4_SB(ac
->ac_sb
),
3224 ac
->ac_o_ex
.fe_len
) << bsbits
;
3226 size
= size
>> bsbits
;
3227 start
= start_off
>> bsbits
;
3229 /* don't cover already allocated blocks in selected range */
3230 if (ar
->pleft
&& start
<= ar
->lleft
) {
3231 size
-= ar
->lleft
+ 1 - start
;
3232 start
= ar
->lleft
+ 1;
3234 if (ar
->pright
&& start
+ size
- 1 >= ar
->lright
)
3235 size
-= start
+ size
- ar
->lright
;
3238 * Trim allocation request for filesystems with artificially small
3241 if (size
> EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
))
3242 size
= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
);
3246 /* check we don't cross already preallocated blocks */
3248 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3253 spin_lock(&pa
->pa_lock
);
3254 if (pa
->pa_deleted
) {
3255 spin_unlock(&pa
->pa_lock
);
3259 pa_end
= pa
->pa_lstart
+ EXT4_C2B(EXT4_SB(ac
->ac_sb
),
3262 /* PA must not overlap original request */
3263 BUG_ON(!(ac
->ac_o_ex
.fe_logical
>= pa_end
||
3264 ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
));
3266 /* skip PAs this normalized request doesn't overlap with */
3267 if (pa
->pa_lstart
>= end
|| pa_end
<= start
) {
3268 spin_unlock(&pa
->pa_lock
);
3271 BUG_ON(pa
->pa_lstart
<= start
&& pa_end
>= end
);
3273 /* adjust start or end to be adjacent to this pa */
3274 if (pa_end
<= ac
->ac_o_ex
.fe_logical
) {
3275 BUG_ON(pa_end
< start
);
3277 } else if (pa
->pa_lstart
> ac
->ac_o_ex
.fe_logical
) {
3278 BUG_ON(pa
->pa_lstart
> end
);
3279 end
= pa
->pa_lstart
;
3281 spin_unlock(&pa
->pa_lock
);
3286 /* XXX: extra loop to check we really don't overlap preallocations */
3288 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3291 spin_lock(&pa
->pa_lock
);
3292 if (pa
->pa_deleted
== 0) {
3293 pa_end
= pa
->pa_lstart
+ EXT4_C2B(EXT4_SB(ac
->ac_sb
),
3295 BUG_ON(!(start
>= pa_end
|| end
<= pa
->pa_lstart
));
3297 spin_unlock(&pa
->pa_lock
);
3301 if (start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3302 start
> ac
->ac_o_ex
.fe_logical
) {
3303 ext4_msg(ac
->ac_sb
, KERN_ERR
,
3304 "start %lu, size %lu, fe_logical %lu",
3305 (unsigned long) start
, (unsigned long) size
,
3306 (unsigned long) ac
->ac_o_ex
.fe_logical
);
3309 BUG_ON(size
<= 0 || size
> EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
3311 /* now prepare goal request */
3313 /* XXX: is it better to align blocks WRT to logical
3314 * placement or satisfy big request as is */
3315 ac
->ac_g_ex
.fe_logical
= start
;
3316 ac
->ac_g_ex
.fe_len
= EXT4_NUM_B2C(sbi
, size
);
3318 /* define goal start in order to merge */
3319 if (ar
->pright
&& (ar
->lright
== (start
+ size
))) {
3320 /* merge to the right */
3321 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pright
- size
,
3322 &ac
->ac_f_ex
.fe_group
,
3323 &ac
->ac_f_ex
.fe_start
);
3324 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3326 if (ar
->pleft
&& (ar
->lleft
+ 1 == start
)) {
3327 /* merge to the left */
3328 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pleft
+ 1,
3329 &ac
->ac_f_ex
.fe_group
,
3330 &ac
->ac_f_ex
.fe_start
);
3331 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3334 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size
,
3335 (unsigned) orig_size
, (unsigned) start
);
3338 static void ext4_mb_collect_stats(struct ext4_allocation_context
*ac
)
3340 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3342 if (sbi
->s_mb_stats
&& ac
->ac_g_ex
.fe_len
> 1) {
3343 atomic_inc(&sbi
->s_bal_reqs
);
3344 atomic_add(ac
->ac_b_ex
.fe_len
, &sbi
->s_bal_allocated
);
3345 if (ac
->ac_b_ex
.fe_len
>= ac
->ac_o_ex
.fe_len
)
3346 atomic_inc(&sbi
->s_bal_success
);
3347 atomic_add(ac
->ac_found
, &sbi
->s_bal_ex_scanned
);
3348 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
3349 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
3350 atomic_inc(&sbi
->s_bal_goals
);
3351 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
)
3352 atomic_inc(&sbi
->s_bal_breaks
);
3355 if (ac
->ac_op
== EXT4_MB_HISTORY_ALLOC
)
3356 trace_ext4_mballoc_alloc(ac
);
3358 trace_ext4_mballoc_prealloc(ac
);
3362 * Called on failure; free up any blocks from the inode PA for this
3363 * context. We don't need this for MB_GROUP_PA because we only change
3364 * pa_free in ext4_mb_release_context(), but on failure, we've already
3365 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3367 static void ext4_discard_allocated_blocks(struct ext4_allocation_context
*ac
)
3369 struct ext4_prealloc_space
*pa
= ac
->ac_pa
;
3370 struct ext4_buddy e4b
;
3374 if (ac
->ac_f_ex
.fe_len
== 0)
3376 err
= ext4_mb_load_buddy(ac
->ac_sb
, ac
->ac_f_ex
.fe_group
, &e4b
);
3379 * This should never happen since we pin the
3380 * pages in the ext4_allocation_context so
3381 * ext4_mb_load_buddy() should never fail.
3383 WARN(1, "mb_load_buddy failed (%d)", err
);
3386 ext4_lock_group(ac
->ac_sb
, ac
->ac_f_ex
.fe_group
);
3387 mb_free_blocks(ac
->ac_inode
, &e4b
, ac
->ac_f_ex
.fe_start
,
3388 ac
->ac_f_ex
.fe_len
);
3389 ext4_unlock_group(ac
->ac_sb
, ac
->ac_f_ex
.fe_group
);
3390 ext4_mb_unload_buddy(&e4b
);
3393 if (pa
->pa_type
== MB_INODE_PA
)
3394 pa
->pa_free
+= ac
->ac_b_ex
.fe_len
;
3398 * use blocks preallocated to inode
3400 static void ext4_mb_use_inode_pa(struct ext4_allocation_context
*ac
,
3401 struct ext4_prealloc_space
*pa
)
3403 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3408 /* found preallocated blocks, use them */
3409 start
= pa
->pa_pstart
+ (ac
->ac_o_ex
.fe_logical
- pa
->pa_lstart
);
3410 end
= min(pa
->pa_pstart
+ EXT4_C2B(sbi
, pa
->pa_len
),
3411 start
+ EXT4_C2B(sbi
, ac
->ac_o_ex
.fe_len
));
3412 len
= EXT4_NUM_B2C(sbi
, end
- start
);
3413 ext4_get_group_no_and_offset(ac
->ac_sb
, start
, &ac
->ac_b_ex
.fe_group
,
3414 &ac
->ac_b_ex
.fe_start
);
3415 ac
->ac_b_ex
.fe_len
= len
;
3416 ac
->ac_status
= AC_STATUS_FOUND
;
3419 BUG_ON(start
< pa
->pa_pstart
);
3420 BUG_ON(end
> pa
->pa_pstart
+ EXT4_C2B(sbi
, pa
->pa_len
));
3421 BUG_ON(pa
->pa_free
< len
);
3424 mb_debug(1, "use %llu/%u from inode pa %p\n", start
, len
, pa
);
3428 * use blocks preallocated to locality group
3430 static void ext4_mb_use_group_pa(struct ext4_allocation_context
*ac
,
3431 struct ext4_prealloc_space
*pa
)
3433 unsigned int len
= ac
->ac_o_ex
.fe_len
;
3435 ext4_get_group_no_and_offset(ac
->ac_sb
, pa
->pa_pstart
,
3436 &ac
->ac_b_ex
.fe_group
,
3437 &ac
->ac_b_ex
.fe_start
);
3438 ac
->ac_b_ex
.fe_len
= len
;
3439 ac
->ac_status
= AC_STATUS_FOUND
;
3442 /* we don't correct pa_pstart or pa_plen here to avoid
3443 * possible race when the group is being loaded concurrently
3444 * instead we correct pa later, after blocks are marked
3445 * in on-disk bitmap -- see ext4_mb_release_context()
3446 * Other CPUs are prevented from allocating from this pa by lg_mutex
3448 mb_debug(1, "use %u/%u from group pa %p\n", pa
->pa_lstart
-len
, len
, pa
);
3452 * Return the prealloc space that have minimal distance
3453 * from the goal block. @cpa is the prealloc
3454 * space that is having currently known minimal distance
3455 * from the goal block.
3457 static struct ext4_prealloc_space
*
3458 ext4_mb_check_group_pa(ext4_fsblk_t goal_block
,
3459 struct ext4_prealloc_space
*pa
,
3460 struct ext4_prealloc_space
*cpa
)
3462 ext4_fsblk_t cur_distance
, new_distance
;
3465 atomic_inc(&pa
->pa_count
);
3468 cur_distance
= abs(goal_block
- cpa
->pa_pstart
);
3469 new_distance
= abs(goal_block
- pa
->pa_pstart
);
3471 if (cur_distance
<= new_distance
)
3474 /* drop the previous reference */
3475 atomic_dec(&cpa
->pa_count
);
3476 atomic_inc(&pa
->pa_count
);
3481 * search goal blocks in preallocated space
3483 static noinline_for_stack
int
3484 ext4_mb_use_preallocated(struct ext4_allocation_context
*ac
)
3486 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3488 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3489 struct ext4_locality_group
*lg
;
3490 struct ext4_prealloc_space
*pa
, *cpa
= NULL
;
3491 ext4_fsblk_t goal_block
;
3493 /* only data can be preallocated */
3494 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3497 /* first, try per-file preallocation */
3499 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3501 /* all fields in this condition don't change,
3502 * so we can skip locking for them */
3503 if (ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
||
3504 ac
->ac_o_ex
.fe_logical
>= (pa
->pa_lstart
+
3505 EXT4_C2B(sbi
, pa
->pa_len
)))
3508 /* non-extent files can't have physical blocks past 2^32 */
3509 if (!(ext4_test_inode_flag(ac
->ac_inode
, EXT4_INODE_EXTENTS
)) &&
3510 (pa
->pa_pstart
+ EXT4_C2B(sbi
, pa
->pa_len
) >
3511 EXT4_MAX_BLOCK_FILE_PHYS
))
3514 /* found preallocated blocks, use them */
3515 spin_lock(&pa
->pa_lock
);
3516 if (pa
->pa_deleted
== 0 && pa
->pa_free
) {
3517 atomic_inc(&pa
->pa_count
);
3518 ext4_mb_use_inode_pa(ac
, pa
);
3519 spin_unlock(&pa
->pa_lock
);
3520 ac
->ac_criteria
= 10;
3524 spin_unlock(&pa
->pa_lock
);
3528 /* can we use group allocation? */
3529 if (!(ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
))
3532 /* inode may have no locality group for some reason */
3536 order
= fls(ac
->ac_o_ex
.fe_len
) - 1;
3537 if (order
> PREALLOC_TB_SIZE
- 1)
3538 /* The max size of hash table is PREALLOC_TB_SIZE */
3539 order
= PREALLOC_TB_SIZE
- 1;
3541 goal_block
= ext4_grp_offs_to_block(ac
->ac_sb
, &ac
->ac_g_ex
);
3543 * search for the prealloc space that is having
3544 * minimal distance from the goal block.
3546 for (i
= order
; i
< PREALLOC_TB_SIZE
; i
++) {
3548 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[i
],
3550 spin_lock(&pa
->pa_lock
);
3551 if (pa
->pa_deleted
== 0 &&
3552 pa
->pa_free
>= ac
->ac_o_ex
.fe_len
) {
3554 cpa
= ext4_mb_check_group_pa(goal_block
,
3557 spin_unlock(&pa
->pa_lock
);
3562 ext4_mb_use_group_pa(ac
, cpa
);
3563 ac
->ac_criteria
= 20;
3570 * the function goes through all block freed in the group
3571 * but not yet committed and marks them used in in-core bitmap.
3572 * buddy must be generated from this bitmap
3573 * Need to be called with the ext4 group lock held
3575 static void ext4_mb_generate_from_freelist(struct super_block
*sb
, void *bitmap
,
3579 struct ext4_group_info
*grp
;
3580 struct ext4_free_data
*entry
;
3582 grp
= ext4_get_group_info(sb
, group
);
3583 n
= rb_first(&(grp
->bb_free_root
));
3586 entry
= rb_entry(n
, struct ext4_free_data
, efd_node
);
3587 ext4_set_bits(bitmap
, entry
->efd_start_cluster
, entry
->efd_count
);
3594 * the function goes through all preallocation in this group and marks them
3595 * used in in-core bitmap. buddy must be generated from this bitmap
3596 * Need to be called with ext4 group lock held
3598 static noinline_for_stack
3599 void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
3602 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3603 struct ext4_prealloc_space
*pa
;
3604 struct list_head
*cur
;
3605 ext4_group_t groupnr
;
3606 ext4_grpblk_t start
;
3607 int preallocated
= 0;
3610 /* all form of preallocation discards first load group,
3611 * so the only competing code is preallocation use.
3612 * we don't need any locking here
3613 * notice we do NOT ignore preallocations with pa_deleted
3614 * otherwise we could leave used blocks available for
3615 * allocation in buddy when concurrent ext4_mb_put_pa()
3616 * is dropping preallocation
3618 list_for_each(cur
, &grp
->bb_prealloc_list
) {
3619 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
3620 spin_lock(&pa
->pa_lock
);
3621 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
3624 spin_unlock(&pa
->pa_lock
);
3625 if (unlikely(len
== 0))
3627 BUG_ON(groupnr
!= group
);
3628 ext4_set_bits(bitmap
, start
, len
);
3629 preallocated
+= len
;
3631 mb_debug(1, "prellocated %u for group %u\n", preallocated
, group
);
3634 static void ext4_mb_pa_callback(struct rcu_head
*head
)
3636 struct ext4_prealloc_space
*pa
;
3637 pa
= container_of(head
, struct ext4_prealloc_space
, u
.pa_rcu
);
3639 BUG_ON(atomic_read(&pa
->pa_count
));
3640 BUG_ON(pa
->pa_deleted
== 0);
3641 kmem_cache_free(ext4_pspace_cachep
, pa
);
3645 * drops a reference to preallocated space descriptor
3646 * if this was the last reference and the space is consumed
3648 static void ext4_mb_put_pa(struct ext4_allocation_context
*ac
,
3649 struct super_block
*sb
, struct ext4_prealloc_space
*pa
)
3652 ext4_fsblk_t grp_blk
;
3654 /* in this short window concurrent discard can set pa_deleted */
3655 spin_lock(&pa
->pa_lock
);
3656 if (!atomic_dec_and_test(&pa
->pa_count
) || pa
->pa_free
!= 0) {
3657 spin_unlock(&pa
->pa_lock
);
3661 if (pa
->pa_deleted
== 1) {
3662 spin_unlock(&pa
->pa_lock
);
3667 spin_unlock(&pa
->pa_lock
);
3669 grp_blk
= pa
->pa_pstart
;
3671 * If doing group-based preallocation, pa_pstart may be in the
3672 * next group when pa is used up
3674 if (pa
->pa_type
== MB_GROUP_PA
)
3677 grp
= ext4_get_group_number(sb
, grp_blk
);
3682 * P1 (buddy init) P2 (regular allocation)
3683 * find block B in PA
3684 * copy on-disk bitmap to buddy
3685 * mark B in on-disk bitmap
3686 * drop PA from group
3687 * mark all PAs in buddy
3689 * thus, P1 initializes buddy with B available. to prevent this
3690 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3693 ext4_lock_group(sb
, grp
);
3694 list_del(&pa
->pa_group_list
);
3695 ext4_unlock_group(sb
, grp
);
3697 spin_lock(pa
->pa_obj_lock
);
3698 list_del_rcu(&pa
->pa_inode_list
);
3699 spin_unlock(pa
->pa_obj_lock
);
3701 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3705 * creates new preallocated space for given inode
3707 static noinline_for_stack
int
3708 ext4_mb_new_inode_pa(struct ext4_allocation_context
*ac
)
3710 struct super_block
*sb
= ac
->ac_sb
;
3711 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3712 struct ext4_prealloc_space
*pa
;
3713 struct ext4_group_info
*grp
;
3714 struct ext4_inode_info
*ei
;
3716 /* preallocate only when found space is larger then requested */
3717 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3718 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3719 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3721 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3725 if (ac
->ac_b_ex
.fe_len
< ac
->ac_g_ex
.fe_len
) {
3731 /* we can't allocate as much as normalizer wants.
3732 * so, found space must get proper lstart
3733 * to cover original request */
3734 BUG_ON(ac
->ac_g_ex
.fe_logical
> ac
->ac_o_ex
.fe_logical
);
3735 BUG_ON(ac
->ac_g_ex
.fe_len
< ac
->ac_o_ex
.fe_len
);
3737 /* we're limited by original request in that
3738 * logical block must be covered any way
3739 * winl is window we can move our chunk within */
3740 winl
= ac
->ac_o_ex
.fe_logical
- ac
->ac_g_ex
.fe_logical
;
3742 /* also, we should cover whole original request */
3743 wins
= EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
- ac
->ac_o_ex
.fe_len
);
3745 /* the smallest one defines real window */
3746 win
= min(winl
, wins
);
3748 offs
= ac
->ac_o_ex
.fe_logical
%
3749 EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
);
3750 if (offs
&& offs
< win
)
3753 ac
->ac_b_ex
.fe_logical
= ac
->ac_o_ex
.fe_logical
-
3754 EXT4_NUM_B2C(sbi
, win
);
3755 BUG_ON(ac
->ac_o_ex
.fe_logical
< ac
->ac_b_ex
.fe_logical
);
3756 BUG_ON(ac
->ac_o_ex
.fe_len
> ac
->ac_b_ex
.fe_len
);
3759 /* preallocation can change ac_b_ex, thus we store actually
3760 * allocated blocks for history */
3761 ac
->ac_f_ex
= ac
->ac_b_ex
;
3763 pa
->pa_lstart
= ac
->ac_b_ex
.fe_logical
;
3764 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3765 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3766 pa
->pa_free
= pa
->pa_len
;
3767 atomic_set(&pa
->pa_count
, 1);
3768 spin_lock_init(&pa
->pa_lock
);
3769 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3770 INIT_LIST_HEAD(&pa
->pa_group_list
);
3772 pa
->pa_type
= MB_INODE_PA
;
3774 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa
,
3775 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3776 trace_ext4_mb_new_inode_pa(ac
, pa
);
3778 ext4_mb_use_inode_pa(ac
, pa
);
3779 atomic_add(pa
->pa_free
, &sbi
->s_mb_preallocated
);
3781 ei
= EXT4_I(ac
->ac_inode
);
3782 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3784 pa
->pa_obj_lock
= &ei
->i_prealloc_lock
;
3785 pa
->pa_inode
= ac
->ac_inode
;
3787 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3788 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3789 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3791 spin_lock(pa
->pa_obj_lock
);
3792 list_add_rcu(&pa
->pa_inode_list
, &ei
->i_prealloc_list
);
3793 spin_unlock(pa
->pa_obj_lock
);
3799 * creates new preallocated space for locality group inodes belongs to
3801 static noinline_for_stack
int
3802 ext4_mb_new_group_pa(struct ext4_allocation_context
*ac
)
3804 struct super_block
*sb
= ac
->ac_sb
;
3805 struct ext4_locality_group
*lg
;
3806 struct ext4_prealloc_space
*pa
;
3807 struct ext4_group_info
*grp
;
3809 /* preallocate only when found space is larger then requested */
3810 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3811 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3812 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3814 BUG_ON(ext4_pspace_cachep
== NULL
);
3815 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3819 /* preallocation can change ac_b_ex, thus we store actually
3820 * allocated blocks for history */
3821 ac
->ac_f_ex
= ac
->ac_b_ex
;
3823 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3824 pa
->pa_lstart
= pa
->pa_pstart
;
3825 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3826 pa
->pa_free
= pa
->pa_len
;
3827 atomic_set(&pa
->pa_count
, 1);
3828 spin_lock_init(&pa
->pa_lock
);
3829 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3830 INIT_LIST_HEAD(&pa
->pa_group_list
);
3832 pa
->pa_type
= MB_GROUP_PA
;
3834 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa
,
3835 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3836 trace_ext4_mb_new_group_pa(ac
, pa
);
3838 ext4_mb_use_group_pa(ac
, pa
);
3839 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3841 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3845 pa
->pa_obj_lock
= &lg
->lg_prealloc_lock
;
3846 pa
->pa_inode
= NULL
;
3848 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3849 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3850 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3853 * We will later add the new pa to the right bucket
3854 * after updating the pa_free in ext4_mb_release_context
3859 static int ext4_mb_new_preallocation(struct ext4_allocation_context
*ac
)
3863 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
3864 err
= ext4_mb_new_group_pa(ac
);
3866 err
= ext4_mb_new_inode_pa(ac
);
3871 * finds all unused blocks in on-disk bitmap, frees them in
3872 * in-core bitmap and buddy.
3873 * @pa must be unlinked from inode and group lists, so that
3874 * nobody else can find/use it.
3875 * the caller MUST hold group/inode locks.
3876 * TODO: optimize the case when there are no in-core structures yet
3878 static noinline_for_stack
int
3879 ext4_mb_release_inode_pa(struct ext4_buddy
*e4b
, struct buffer_head
*bitmap_bh
,
3880 struct ext4_prealloc_space
*pa
)
3882 struct super_block
*sb
= e4b
->bd_sb
;
3883 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3888 unsigned long long grp_blk_start
;
3892 BUG_ON(pa
->pa_deleted
== 0);
3893 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3894 grp_blk_start
= pa
->pa_pstart
- EXT4_C2B(sbi
, bit
);
3895 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3896 end
= bit
+ pa
->pa_len
;
3899 bit
= mb_find_next_zero_bit(bitmap_bh
->b_data
, end
, bit
);
3902 next
= mb_find_next_bit(bitmap_bh
->b_data
, end
, bit
);
3903 mb_debug(1, " free preallocated %u/%u in group %u\n",
3904 (unsigned) ext4_group_first_block_no(sb
, group
) + bit
,
3905 (unsigned) next
- bit
, (unsigned) group
);
3908 trace_ext4_mballoc_discard(sb
, NULL
, group
, bit
, next
- bit
);
3909 trace_ext4_mb_release_inode_pa(pa
, (grp_blk_start
+
3910 EXT4_C2B(sbi
, bit
)),
3912 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, next
- bit
);
3915 if (free
!= pa
->pa_free
) {
3916 ext4_msg(e4b
->bd_sb
, KERN_CRIT
,
3917 "pa %p: logic %lu, phys. %lu, len %lu",
3918 pa
, (unsigned long) pa
->pa_lstart
,
3919 (unsigned long) pa
->pa_pstart
,
3920 (unsigned long) pa
->pa_len
);
3921 ext4_grp_locked_error(sb
, group
, 0, 0, "free %u, pa_free %u",
3924 * pa is already deleted so we use the value obtained
3925 * from the bitmap and continue.
3928 atomic_add(free
, &sbi
->s_mb_discarded
);
3933 static noinline_for_stack
int
3934 ext4_mb_release_group_pa(struct ext4_buddy
*e4b
,
3935 struct ext4_prealloc_space
*pa
)
3937 struct super_block
*sb
= e4b
->bd_sb
;
3941 trace_ext4_mb_release_group_pa(sb
, pa
);
3942 BUG_ON(pa
->pa_deleted
== 0);
3943 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3944 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3945 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, pa
->pa_len
);
3946 atomic_add(pa
->pa_len
, &EXT4_SB(sb
)->s_mb_discarded
);
3947 trace_ext4_mballoc_discard(sb
, NULL
, group
, bit
, pa
->pa_len
);
3953 * releases all preallocations in given group
3955 * first, we need to decide discard policy:
3956 * - when do we discard
3958 * - how many do we discard
3959 * 1) how many requested
3961 static noinline_for_stack
int
3962 ext4_mb_discard_group_preallocations(struct super_block
*sb
,
3963 ext4_group_t group
, int needed
)
3965 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3966 struct buffer_head
*bitmap_bh
= NULL
;
3967 struct ext4_prealloc_space
*pa
, *tmp
;
3968 struct list_head list
;
3969 struct ext4_buddy e4b
;
3974 mb_debug(1, "discard preallocation for group %u\n", group
);
3976 if (list_empty(&grp
->bb_prealloc_list
))
3979 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
3980 if (IS_ERR(bitmap_bh
)) {
3981 err
= PTR_ERR(bitmap_bh
);
3982 ext4_error(sb
, "Error %d reading block bitmap for %u",
3987 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
3989 ext4_error(sb
, "Error loading buddy information for %u", group
);
3995 needed
= EXT4_CLUSTERS_PER_GROUP(sb
) + 1;
3997 INIT_LIST_HEAD(&list
);
3999 ext4_lock_group(sb
, group
);
4000 list_for_each_entry_safe(pa
, tmp
,
4001 &grp
->bb_prealloc_list
, pa_group_list
) {
4002 spin_lock(&pa
->pa_lock
);
4003 if (atomic_read(&pa
->pa_count
)) {
4004 spin_unlock(&pa
->pa_lock
);
4008 if (pa
->pa_deleted
) {
4009 spin_unlock(&pa
->pa_lock
);
4013 /* seems this one can be freed ... */
4016 /* we can trust pa_free ... */
4017 free
+= pa
->pa_free
;
4019 spin_unlock(&pa
->pa_lock
);
4021 list_del(&pa
->pa_group_list
);
4022 list_add(&pa
->u
.pa_tmp_list
, &list
);
4025 /* if we still need more blocks and some PAs were used, try again */
4026 if (free
< needed
&& busy
) {
4028 ext4_unlock_group(sb
, group
);
4033 /* found anything to free? */
4034 if (list_empty(&list
)) {
4039 /* now free all selected PAs */
4040 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
4042 /* remove from object (inode or locality group) */
4043 spin_lock(pa
->pa_obj_lock
);
4044 list_del_rcu(&pa
->pa_inode_list
);
4045 spin_unlock(pa
->pa_obj_lock
);
4047 if (pa
->pa_type
== MB_GROUP_PA
)
4048 ext4_mb_release_group_pa(&e4b
, pa
);
4050 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
);
4052 list_del(&pa
->u
.pa_tmp_list
);
4053 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4057 ext4_unlock_group(sb
, group
);
4058 ext4_mb_unload_buddy(&e4b
);
4064 * releases all non-used preallocated blocks for given inode
4066 * It's important to discard preallocations under i_data_sem
4067 * We don't want another block to be served from the prealloc
4068 * space when we are discarding the inode prealloc space.
4070 * FIXME!! Make sure it is valid at all the call sites
4072 void ext4_discard_preallocations(struct inode
*inode
)
4074 struct ext4_inode_info
*ei
= EXT4_I(inode
);
4075 struct super_block
*sb
= inode
->i_sb
;
4076 struct buffer_head
*bitmap_bh
= NULL
;
4077 struct ext4_prealloc_space
*pa
, *tmp
;
4078 ext4_group_t group
= 0;
4079 struct list_head list
;
4080 struct ext4_buddy e4b
;
4083 if (!S_ISREG(inode
->i_mode
)) {
4084 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
4088 mb_debug(1, "discard preallocation for inode %lu\n", inode
->i_ino
);
4089 trace_ext4_discard_preallocations(inode
);
4091 INIT_LIST_HEAD(&list
);
4094 /* first, collect all pa's in the inode */
4095 spin_lock(&ei
->i_prealloc_lock
);
4096 while (!list_empty(&ei
->i_prealloc_list
)) {
4097 pa
= list_entry(ei
->i_prealloc_list
.next
,
4098 struct ext4_prealloc_space
, pa_inode_list
);
4099 BUG_ON(pa
->pa_obj_lock
!= &ei
->i_prealloc_lock
);
4100 spin_lock(&pa
->pa_lock
);
4101 if (atomic_read(&pa
->pa_count
)) {
4102 /* this shouldn't happen often - nobody should
4103 * use preallocation while we're discarding it */
4104 spin_unlock(&pa
->pa_lock
);
4105 spin_unlock(&ei
->i_prealloc_lock
);
4106 ext4_msg(sb
, KERN_ERR
,
4107 "uh-oh! used pa while discarding");
4109 schedule_timeout_uninterruptible(HZ
);
4113 if (pa
->pa_deleted
== 0) {
4115 spin_unlock(&pa
->pa_lock
);
4116 list_del_rcu(&pa
->pa_inode_list
);
4117 list_add(&pa
->u
.pa_tmp_list
, &list
);
4121 /* someone is deleting pa right now */
4122 spin_unlock(&pa
->pa_lock
);
4123 spin_unlock(&ei
->i_prealloc_lock
);
4125 /* we have to wait here because pa_deleted
4126 * doesn't mean pa is already unlinked from
4127 * the list. as we might be called from
4128 * ->clear_inode() the inode will get freed
4129 * and concurrent thread which is unlinking
4130 * pa from inode's list may access already
4131 * freed memory, bad-bad-bad */
4133 /* XXX: if this happens too often, we can
4134 * add a flag to force wait only in case
4135 * of ->clear_inode(), but not in case of
4136 * regular truncate */
4137 schedule_timeout_uninterruptible(HZ
);
4140 spin_unlock(&ei
->i_prealloc_lock
);
4142 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
4143 BUG_ON(pa
->pa_type
!= MB_INODE_PA
);
4144 group
= ext4_get_group_number(sb
, pa
->pa_pstart
);
4146 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
4148 ext4_error(sb
, "Error loading buddy information for %u",
4153 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
4154 if (IS_ERR(bitmap_bh
)) {
4155 err
= PTR_ERR(bitmap_bh
);
4156 ext4_error(sb
, "Error %d reading block bitmap for %u",
4158 ext4_mb_unload_buddy(&e4b
);
4162 ext4_lock_group(sb
, group
);
4163 list_del(&pa
->pa_group_list
);
4164 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
);
4165 ext4_unlock_group(sb
, group
);
4167 ext4_mb_unload_buddy(&e4b
);
4170 list_del(&pa
->u
.pa_tmp_list
);
4171 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4175 #ifdef CONFIG_EXT4_DEBUG
4176 static void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
4178 struct super_block
*sb
= ac
->ac_sb
;
4179 ext4_group_t ngroups
, i
;
4181 if (!ext4_mballoc_debug
||
4182 (EXT4_SB(sb
)->s_mount_flags
& EXT4_MF_FS_ABORTED
))
4185 ext4_msg(ac
->ac_sb
, KERN_ERR
, "Can't allocate:"
4186 " Allocation context details:");
4187 ext4_msg(ac
->ac_sb
, KERN_ERR
, "status %d flags %d",
4188 ac
->ac_status
, ac
->ac_flags
);
4189 ext4_msg(ac
->ac_sb
, KERN_ERR
, "orig %lu/%lu/%lu@%lu, "
4190 "goal %lu/%lu/%lu@%lu, "
4191 "best %lu/%lu/%lu@%lu cr %d",
4192 (unsigned long)ac
->ac_o_ex
.fe_group
,
4193 (unsigned long)ac
->ac_o_ex
.fe_start
,
4194 (unsigned long)ac
->ac_o_ex
.fe_len
,
4195 (unsigned long)ac
->ac_o_ex
.fe_logical
,
4196 (unsigned long)ac
->ac_g_ex
.fe_group
,
4197 (unsigned long)ac
->ac_g_ex
.fe_start
,
4198 (unsigned long)ac
->ac_g_ex
.fe_len
,
4199 (unsigned long)ac
->ac_g_ex
.fe_logical
,
4200 (unsigned long)ac
->ac_b_ex
.fe_group
,
4201 (unsigned long)ac
->ac_b_ex
.fe_start
,
4202 (unsigned long)ac
->ac_b_ex
.fe_len
,
4203 (unsigned long)ac
->ac_b_ex
.fe_logical
,
4204 (int)ac
->ac_criteria
);
4205 ext4_msg(ac
->ac_sb
, KERN_ERR
, "%d found", ac
->ac_found
);
4206 ext4_msg(ac
->ac_sb
, KERN_ERR
, "groups: ");
4207 ngroups
= ext4_get_groups_count(sb
);
4208 for (i
= 0; i
< ngroups
; i
++) {
4209 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, i
);
4210 struct ext4_prealloc_space
*pa
;
4211 ext4_grpblk_t start
;
4212 struct list_head
*cur
;
4213 ext4_lock_group(sb
, i
);
4214 list_for_each(cur
, &grp
->bb_prealloc_list
) {
4215 pa
= list_entry(cur
, struct ext4_prealloc_space
,
4217 spin_lock(&pa
->pa_lock
);
4218 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
4220 spin_unlock(&pa
->pa_lock
);
4221 printk(KERN_ERR
"PA:%u:%d:%u \n", i
,
4224 ext4_unlock_group(sb
, i
);
4226 if (grp
->bb_free
== 0)
4228 printk(KERN_ERR
"%u: %d/%d \n",
4229 i
, grp
->bb_free
, grp
->bb_fragments
);
4231 printk(KERN_ERR
"\n");
4234 static inline void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
4241 * We use locality group preallocation for small size file. The size of the
4242 * file is determined by the current size or the resulting size after
4243 * allocation which ever is larger
4245 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
4247 static void ext4_mb_group_or_file(struct ext4_allocation_context
*ac
)
4249 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
4250 int bsbits
= ac
->ac_sb
->s_blocksize_bits
;
4253 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
4256 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
4259 size
= ac
->ac_o_ex
.fe_logical
+ EXT4_C2B(sbi
, ac
->ac_o_ex
.fe_len
);
4260 isize
= (i_size_read(ac
->ac_inode
) + ac
->ac_sb
->s_blocksize
- 1)
4263 if ((size
== isize
) &&
4264 !ext4_fs_is_busy(sbi
) &&
4265 (atomic_read(&ac
->ac_inode
->i_writecount
) == 0)) {
4266 ac
->ac_flags
|= EXT4_MB_HINT_NOPREALLOC
;
4270 if (sbi
->s_mb_group_prealloc
<= 0) {
4271 ac
->ac_flags
|= EXT4_MB_STREAM_ALLOC
;
4275 /* don't use group allocation for large files */
4276 size
= max(size
, isize
);
4277 if (size
> sbi
->s_mb_stream_request
) {
4278 ac
->ac_flags
|= EXT4_MB_STREAM_ALLOC
;
4282 BUG_ON(ac
->ac_lg
!= NULL
);
4284 * locality group prealloc space are per cpu. The reason for having
4285 * per cpu locality group is to reduce the contention between block
4286 * request from multiple CPUs.
4288 ac
->ac_lg
= raw_cpu_ptr(sbi
->s_locality_groups
);
4290 /* we're going to use group allocation */
4291 ac
->ac_flags
|= EXT4_MB_HINT_GROUP_ALLOC
;
4293 /* serialize all allocations in the group */
4294 mutex_lock(&ac
->ac_lg
->lg_mutex
);
4297 static noinline_for_stack
int
4298 ext4_mb_initialize_context(struct ext4_allocation_context
*ac
,
4299 struct ext4_allocation_request
*ar
)
4301 struct super_block
*sb
= ar
->inode
->i_sb
;
4302 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4303 struct ext4_super_block
*es
= sbi
->s_es
;
4307 ext4_grpblk_t block
;
4309 /* we can't allocate > group size */
4312 /* just a dirty hack to filter too big requests */
4313 if (len
>= EXT4_CLUSTERS_PER_GROUP(sb
))
4314 len
= EXT4_CLUSTERS_PER_GROUP(sb
);
4316 /* start searching from the goal */
4318 if (goal
< le32_to_cpu(es
->s_first_data_block
) ||
4319 goal
>= ext4_blocks_count(es
))
4320 goal
= le32_to_cpu(es
->s_first_data_block
);
4321 ext4_get_group_no_and_offset(sb
, goal
, &group
, &block
);
4323 /* set up allocation goals */
4324 ac
->ac_b_ex
.fe_logical
= EXT4_LBLK_CMASK(sbi
, ar
->logical
);
4325 ac
->ac_status
= AC_STATUS_CONTINUE
;
4327 ac
->ac_inode
= ar
->inode
;
4328 ac
->ac_o_ex
.fe_logical
= ac
->ac_b_ex
.fe_logical
;
4329 ac
->ac_o_ex
.fe_group
= group
;
4330 ac
->ac_o_ex
.fe_start
= block
;
4331 ac
->ac_o_ex
.fe_len
= len
;
4332 ac
->ac_g_ex
= ac
->ac_o_ex
;
4333 ac
->ac_flags
= ar
->flags
;
4335 /* we have to define context: we'll we work with a file or
4336 * locality group. this is a policy, actually */
4337 ext4_mb_group_or_file(ac
);
4339 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
4340 "left: %u/%u, right %u/%u to %swritable\n",
4341 (unsigned) ar
->len
, (unsigned) ar
->logical
,
4342 (unsigned) ar
->goal
, ac
->ac_flags
, ac
->ac_2order
,
4343 (unsigned) ar
->lleft
, (unsigned) ar
->pleft
,
4344 (unsigned) ar
->lright
, (unsigned) ar
->pright
,
4345 atomic_read(&ar
->inode
->i_writecount
) ? "" : "non-");
4350 static noinline_for_stack
void
4351 ext4_mb_discard_lg_preallocations(struct super_block
*sb
,
4352 struct ext4_locality_group
*lg
,
4353 int order
, int total_entries
)
4355 ext4_group_t group
= 0;
4356 struct ext4_buddy e4b
;
4357 struct list_head discard_list
;
4358 struct ext4_prealloc_space
*pa
, *tmp
;
4360 mb_debug(1, "discard locality group preallocation\n");
4362 INIT_LIST_HEAD(&discard_list
);
4364 spin_lock(&lg
->lg_prealloc_lock
);
4365 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[order
],
4367 spin_lock(&pa
->pa_lock
);
4368 if (atomic_read(&pa
->pa_count
)) {
4370 * This is the pa that we just used
4371 * for block allocation. So don't
4374 spin_unlock(&pa
->pa_lock
);
4377 if (pa
->pa_deleted
) {
4378 spin_unlock(&pa
->pa_lock
);
4381 /* only lg prealloc space */
4382 BUG_ON(pa
->pa_type
!= MB_GROUP_PA
);
4384 /* seems this one can be freed ... */
4386 spin_unlock(&pa
->pa_lock
);
4388 list_del_rcu(&pa
->pa_inode_list
);
4389 list_add(&pa
->u
.pa_tmp_list
, &discard_list
);
4392 if (total_entries
<= 5) {
4394 * we want to keep only 5 entries
4395 * allowing it to grow to 8. This
4396 * mak sure we don't call discard
4397 * soon for this list.
4402 spin_unlock(&lg
->lg_prealloc_lock
);
4404 list_for_each_entry_safe(pa
, tmp
, &discard_list
, u
.pa_tmp_list
) {
4406 group
= ext4_get_group_number(sb
, pa
->pa_pstart
);
4407 if (ext4_mb_load_buddy(sb
, group
, &e4b
)) {
4408 ext4_error(sb
, "Error loading buddy information for %u",
4412 ext4_lock_group(sb
, group
);
4413 list_del(&pa
->pa_group_list
);
4414 ext4_mb_release_group_pa(&e4b
, pa
);
4415 ext4_unlock_group(sb
, group
);
4417 ext4_mb_unload_buddy(&e4b
);
4418 list_del(&pa
->u
.pa_tmp_list
);
4419 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4424 * We have incremented pa_count. So it cannot be freed at this
4425 * point. Also we hold lg_mutex. So no parallel allocation is
4426 * possible from this lg. That means pa_free cannot be updated.
4428 * A parallel ext4_mb_discard_group_preallocations is possible.
4429 * which can cause the lg_prealloc_list to be updated.
4432 static void ext4_mb_add_n_trim(struct ext4_allocation_context
*ac
)
4434 int order
, added
= 0, lg_prealloc_count
= 1;
4435 struct super_block
*sb
= ac
->ac_sb
;
4436 struct ext4_locality_group
*lg
= ac
->ac_lg
;
4437 struct ext4_prealloc_space
*tmp_pa
, *pa
= ac
->ac_pa
;
4439 order
= fls(pa
->pa_free
) - 1;
4440 if (order
> PREALLOC_TB_SIZE
- 1)
4441 /* The max size of hash table is PREALLOC_TB_SIZE */
4442 order
= PREALLOC_TB_SIZE
- 1;
4443 /* Add the prealloc space to lg */
4444 spin_lock(&lg
->lg_prealloc_lock
);
4445 list_for_each_entry_rcu(tmp_pa
, &lg
->lg_prealloc_list
[order
],
4447 spin_lock(&tmp_pa
->pa_lock
);
4448 if (tmp_pa
->pa_deleted
) {
4449 spin_unlock(&tmp_pa
->pa_lock
);
4452 if (!added
&& pa
->pa_free
< tmp_pa
->pa_free
) {
4453 /* Add to the tail of the previous entry */
4454 list_add_tail_rcu(&pa
->pa_inode_list
,
4455 &tmp_pa
->pa_inode_list
);
4458 * we want to count the total
4459 * number of entries in the list
4462 spin_unlock(&tmp_pa
->pa_lock
);
4463 lg_prealloc_count
++;
4466 list_add_tail_rcu(&pa
->pa_inode_list
,
4467 &lg
->lg_prealloc_list
[order
]);
4468 spin_unlock(&lg
->lg_prealloc_lock
);
4470 /* Now trim the list to be not more than 8 elements */
4471 if (lg_prealloc_count
> 8) {
4472 ext4_mb_discard_lg_preallocations(sb
, lg
,
4473 order
, lg_prealloc_count
);
4480 * release all resource we used in allocation
4482 static int ext4_mb_release_context(struct ext4_allocation_context
*ac
)
4484 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
4485 struct ext4_prealloc_space
*pa
= ac
->ac_pa
;
4487 if (pa
->pa_type
== MB_GROUP_PA
) {
4488 /* see comment in ext4_mb_use_group_pa() */
4489 spin_lock(&pa
->pa_lock
);
4490 pa
->pa_pstart
+= EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
);
4491 pa
->pa_lstart
+= EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
);
4492 pa
->pa_free
-= ac
->ac_b_ex
.fe_len
;
4493 pa
->pa_len
-= ac
->ac_b_ex
.fe_len
;
4494 spin_unlock(&pa
->pa_lock
);
4499 * We want to add the pa to the right bucket.
4500 * Remove it from the list and while adding
4501 * make sure the list to which we are adding
4504 if ((pa
->pa_type
== MB_GROUP_PA
) && likely(pa
->pa_free
)) {
4505 spin_lock(pa
->pa_obj_lock
);
4506 list_del_rcu(&pa
->pa_inode_list
);
4507 spin_unlock(pa
->pa_obj_lock
);
4508 ext4_mb_add_n_trim(ac
);
4510 ext4_mb_put_pa(ac
, ac
->ac_sb
, pa
);
4512 if (ac
->ac_bitmap_page
)
4513 page_cache_release(ac
->ac_bitmap_page
);
4514 if (ac
->ac_buddy_page
)
4515 page_cache_release(ac
->ac_buddy_page
);
4516 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
4517 mutex_unlock(&ac
->ac_lg
->lg_mutex
);
4518 ext4_mb_collect_stats(ac
);
4522 static int ext4_mb_discard_preallocations(struct super_block
*sb
, int needed
)
4524 ext4_group_t i
, ngroups
= ext4_get_groups_count(sb
);
4528 trace_ext4_mb_discard_preallocations(sb
, needed
);
4529 for (i
= 0; i
< ngroups
&& needed
> 0; i
++) {
4530 ret
= ext4_mb_discard_group_preallocations(sb
, i
, needed
);
4539 * Main entry point into mballoc to allocate blocks
4540 * it tries to use preallocation first, then falls back
4541 * to usual allocation
4543 ext4_fsblk_t
ext4_mb_new_blocks(handle_t
*handle
,
4544 struct ext4_allocation_request
*ar
, int *errp
)
4547 struct ext4_allocation_context
*ac
= NULL
;
4548 struct ext4_sb_info
*sbi
;
4549 struct super_block
*sb
;
4550 ext4_fsblk_t block
= 0;
4551 unsigned int inquota
= 0;
4552 unsigned int reserv_clstrs
= 0;
4555 sb
= ar
->inode
->i_sb
;
4558 trace_ext4_request_blocks(ar
);
4560 /* Allow to use superuser reservation for quota file */
4561 if (IS_NOQUOTA(ar
->inode
))
4562 ar
->flags
|= EXT4_MB_USE_ROOT_BLOCKS
;
4564 if (ext4_test_inode_flag(ar
->inode
, EXT4_INODE_CORE_FILE
))
4565 ar
->flags
|= EXT4_MB_USE_EXTRA_ROOT_BLOCKS
;
4567 if ((ar
->flags
& EXT4_MB_DELALLOC_RESERVED
) == 0) {
4568 /* Without delayed allocation we need to verify
4569 * there is enough free blocks to do block allocation
4570 * and verify allocation doesn't exceed the quota limits.
4573 ext4_claim_free_clusters(sbi
, ar
->len
, ar
->flags
)) {
4575 /* let others to free the space */
4577 ar
->len
= ar
->len
>> 1;
4583 reserv_clstrs
= ar
->len
;
4584 if (ar
->flags
& EXT4_MB_USE_ROOT_BLOCKS
) {
4585 dquot_alloc_block_nofail(ar
->inode
,
4586 EXT4_C2B(sbi
, ar
->len
));
4589 dquot_alloc_block(ar
->inode
,
4590 EXT4_C2B(sbi
, ar
->len
))) {
4592 ar
->flags
|= EXT4_MB_HINT_NOPREALLOC
;
4603 ac
= kmem_cache_zalloc(ext4_ac_cachep
, GFP_NOFS
);
4610 *errp
= ext4_mb_initialize_context(ac
, ar
);
4616 ac
->ac_op
= EXT4_MB_HISTORY_PREALLOC
;
4617 if (!ext4_mb_use_preallocated(ac
)) {
4618 ac
->ac_op
= EXT4_MB_HISTORY_ALLOC
;
4619 ext4_mb_normalize_request(ac
, ar
);
4621 /* allocate space in core */
4622 *errp
= ext4_mb_regular_allocator(ac
);
4624 goto discard_and_exit
;
4626 /* as we've just preallocated more space than
4627 * user requested originally, we store allocated
4628 * space in a special descriptor */
4629 if (ac
->ac_status
== AC_STATUS_FOUND
&&
4630 ac
->ac_o_ex
.fe_len
< ac
->ac_b_ex
.fe_len
)
4631 *errp
= ext4_mb_new_preallocation(ac
);
4634 ext4_discard_allocated_blocks(ac
);
4638 if (likely(ac
->ac_status
== AC_STATUS_FOUND
)) {
4639 *errp
= ext4_mb_mark_diskspace_used(ac
, handle
, reserv_clstrs
);
4641 ext4_discard_allocated_blocks(ac
);
4644 block
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
4645 ar
->len
= ac
->ac_b_ex
.fe_len
;
4648 freed
= ext4_mb_discard_preallocations(sb
, ac
->ac_o_ex
.fe_len
);
4656 ac
->ac_b_ex
.fe_len
= 0;
4658 ext4_mb_show_ac(ac
);
4660 ext4_mb_release_context(ac
);
4663 kmem_cache_free(ext4_ac_cachep
, ac
);
4664 if (inquota
&& ar
->len
< inquota
)
4665 dquot_free_block(ar
->inode
, EXT4_C2B(sbi
, inquota
- ar
->len
));
4667 if ((ar
->flags
& EXT4_MB_DELALLOC_RESERVED
) == 0)
4668 /* release all the reserved blocks if non delalloc */
4669 percpu_counter_sub(&sbi
->s_dirtyclusters_counter
,
4673 trace_ext4_allocate_blocks(ar
, (unsigned long long)block
);
4679 * We can merge two free data extents only if the physical blocks
4680 * are contiguous, AND the extents were freed by the same transaction,
4681 * AND the blocks are associated with the same group.
4683 static void ext4_try_merge_freed_extent(struct ext4_sb_info
*sbi
,
4684 struct ext4_free_data
*entry
,
4685 struct ext4_free_data
*new_entry
,
4686 struct rb_root
*entry_rb_root
)
4688 if ((entry
->efd_tid
!= new_entry
->efd_tid
) ||
4689 (entry
->efd_group
!= new_entry
->efd_group
))
4691 if (entry
->efd_start_cluster
+ entry
->efd_count
==
4692 new_entry
->efd_start_cluster
) {
4693 new_entry
->efd_start_cluster
= entry
->efd_start_cluster
;
4694 new_entry
->efd_count
+= entry
->efd_count
;
4695 } else if (new_entry
->efd_start_cluster
+ new_entry
->efd_count
==
4696 entry
->efd_start_cluster
) {
4697 new_entry
->efd_count
+= entry
->efd_count
;
4700 spin_lock(&sbi
->s_md_lock
);
4701 list_del(&entry
->efd_list
);
4702 spin_unlock(&sbi
->s_md_lock
);
4703 rb_erase(&entry
->efd_node
, entry_rb_root
);
4704 kmem_cache_free(ext4_free_data_cachep
, entry
);
4707 static noinline_for_stack
int
4708 ext4_mb_free_metadata(handle_t
*handle
, struct ext4_buddy
*e4b
,
4709 struct ext4_free_data
*new_entry
)
4711 ext4_group_t group
= e4b
->bd_group
;
4712 ext4_grpblk_t cluster
;
4713 struct ext4_free_data
*entry
;
4714 struct ext4_group_info
*db
= e4b
->bd_info
;
4715 struct super_block
*sb
= e4b
->bd_sb
;
4716 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4717 struct rb_node
**n
= &db
->bb_free_root
.rb_node
, *node
;
4718 struct rb_node
*parent
= NULL
, *new_node
;
4720 BUG_ON(!ext4_handle_valid(handle
));
4721 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
4722 BUG_ON(e4b
->bd_buddy_page
== NULL
);
4724 new_node
= &new_entry
->efd_node
;
4725 cluster
= new_entry
->efd_start_cluster
;
4728 /* first free block exent. We need to
4729 protect buddy cache from being freed,
4730 * otherwise we'll refresh it from
4731 * on-disk bitmap and lose not-yet-available
4733 page_cache_get(e4b
->bd_buddy_page
);
4734 page_cache_get(e4b
->bd_bitmap_page
);
4738 entry
= rb_entry(parent
, struct ext4_free_data
, efd_node
);
4739 if (cluster
< entry
->efd_start_cluster
)
4741 else if (cluster
>= (entry
->efd_start_cluster
+ entry
->efd_count
))
4742 n
= &(*n
)->rb_right
;
4744 ext4_grp_locked_error(sb
, group
, 0,
4745 ext4_group_first_block_no(sb
, group
) +
4746 EXT4_C2B(sbi
, cluster
),
4747 "Block already on to-be-freed list");
4752 rb_link_node(new_node
, parent
, n
);
4753 rb_insert_color(new_node
, &db
->bb_free_root
);
4755 /* Now try to see the extent can be merged to left and right */
4756 node
= rb_prev(new_node
);
4758 entry
= rb_entry(node
, struct ext4_free_data
, efd_node
);
4759 ext4_try_merge_freed_extent(sbi
, entry
, new_entry
,
4760 &(db
->bb_free_root
));
4763 node
= rb_next(new_node
);
4765 entry
= rb_entry(node
, struct ext4_free_data
, efd_node
);
4766 ext4_try_merge_freed_extent(sbi
, entry
, new_entry
,
4767 &(db
->bb_free_root
));
4770 spin_lock(&sbi
->s_md_lock
);
4771 list_add_tail(&new_entry
->efd_list
, &sbi
->s_freed_data_list
);
4772 spin_unlock(&sbi
->s_md_lock
);
4778 * ext4_free_blocks() -- Free given blocks and update quota
4779 * @handle: handle for this transaction
4781 * @block: start physical block to free
4782 * @count: number of blocks to count
4783 * @flags: flags used by ext4_free_blocks
4785 void ext4_free_blocks(handle_t
*handle
, struct inode
*inode
,
4786 struct buffer_head
*bh
, ext4_fsblk_t block
,
4787 unsigned long count
, int flags
)
4789 struct buffer_head
*bitmap_bh
= NULL
;
4790 struct super_block
*sb
= inode
->i_sb
;
4791 struct ext4_group_desc
*gdp
;
4792 unsigned int overflow
;
4794 struct buffer_head
*gd_bh
;
4795 ext4_group_t block_group
;
4796 struct ext4_sb_info
*sbi
;
4797 struct ext4_buddy e4b
;
4798 unsigned int count_clusters
;
4805 BUG_ON(block
!= bh
->b_blocknr
);
4807 block
= bh
->b_blocknr
;
4811 if (!(flags
& EXT4_FREE_BLOCKS_VALIDATED
) &&
4812 !ext4_data_block_valid(sbi
, block
, count
)) {
4813 ext4_error(sb
, "Freeing blocks not in datazone - "
4814 "block = %llu, count = %lu", block
, count
);
4818 ext4_debug("freeing block %llu\n", block
);
4819 trace_ext4_free_blocks(inode
, block
, count
, flags
);
4821 if (bh
&& (flags
& EXT4_FREE_BLOCKS_FORGET
)) {
4824 ext4_forget(handle
, flags
& EXT4_FREE_BLOCKS_METADATA
,
4829 * We need to make sure we don't reuse the freed block until
4830 * after the transaction is committed, which we can do by
4831 * treating the block as metadata, below. We make an
4832 * exception if the inode is to be written in writeback mode
4833 * since writeback mode has weak data consistency guarantees.
4835 if (!ext4_should_writeback_data(inode
))
4836 flags
|= EXT4_FREE_BLOCKS_METADATA
;
4839 * If the extent to be freed does not begin on a cluster
4840 * boundary, we need to deal with partial clusters at the
4841 * beginning and end of the extent. Normally we will free
4842 * blocks at the beginning or the end unless we are explicitly
4843 * requested to avoid doing so.
4845 overflow
= EXT4_PBLK_COFF(sbi
, block
);
4847 if (flags
& EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER
) {
4848 overflow
= sbi
->s_cluster_ratio
- overflow
;
4850 if (count
> overflow
)
4859 overflow
= EXT4_LBLK_COFF(sbi
, count
);
4861 if (flags
& EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER
) {
4862 if (count
> overflow
)
4867 count
+= sbi
->s_cluster_ratio
- overflow
;
4870 if (!bh
&& (flags
& EXT4_FREE_BLOCKS_FORGET
)) {
4873 for (i
= 0; i
< count
; i
++) {
4875 bh
= sb_find_get_block(inode
->i_sb
, block
+ i
);
4878 ext4_forget(handle
, flags
& EXT4_FREE_BLOCKS_METADATA
,
4879 inode
, bh
, block
+ i
);
4885 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
4887 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(
4888 ext4_get_group_info(sb
, block_group
))))
4892 * Check to see if we are freeing blocks across a group
4895 if (EXT4_C2B(sbi
, bit
) + count
> EXT4_BLOCKS_PER_GROUP(sb
)) {
4896 overflow
= EXT4_C2B(sbi
, bit
) + count
-
4897 EXT4_BLOCKS_PER_GROUP(sb
);
4900 count_clusters
= EXT4_NUM_B2C(sbi
, count
);
4901 bitmap_bh
= ext4_read_block_bitmap(sb
, block_group
);
4902 if (IS_ERR(bitmap_bh
)) {
4903 err
= PTR_ERR(bitmap_bh
);
4907 gdp
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
4913 if (in_range(ext4_block_bitmap(sb
, gdp
), block
, count
) ||
4914 in_range(ext4_inode_bitmap(sb
, gdp
), block
, count
) ||
4915 in_range(block
, ext4_inode_table(sb
, gdp
),
4916 EXT4_SB(sb
)->s_itb_per_group
) ||
4917 in_range(block
+ count
- 1, ext4_inode_table(sb
, gdp
),
4918 EXT4_SB(sb
)->s_itb_per_group
)) {
4920 ext4_error(sb
, "Freeing blocks in system zone - "
4921 "Block = %llu, count = %lu", block
, count
);
4922 /* err = 0. ext4_std_error should be a no op */
4926 BUFFER_TRACE(bitmap_bh
, "getting write access");
4927 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
4932 * We are about to modify some metadata. Call the journal APIs
4933 * to unshare ->b_data if a currently-committing transaction is
4936 BUFFER_TRACE(gd_bh
, "get_write_access");
4937 err
= ext4_journal_get_write_access(handle
, gd_bh
);
4940 #ifdef AGGRESSIVE_CHECK
4943 for (i
= 0; i
< count_clusters
; i
++)
4944 BUG_ON(!mb_test_bit(bit
+ i
, bitmap_bh
->b_data
));
4947 trace_ext4_mballoc_free(sb
, inode
, block_group
, bit
, count_clusters
);
4949 /* __GFP_NOFAIL: retry infinitely, ignore TIF_MEMDIE and memcg limit. */
4950 err
= ext4_mb_load_buddy_gfp(sb
, block_group
, &e4b
,
4951 GFP_NOFS
|__GFP_NOFAIL
);
4955 if ((flags
& EXT4_FREE_BLOCKS_METADATA
) && ext4_handle_valid(handle
)) {
4956 struct ext4_free_data
*new_entry
;
4958 * blocks being freed are metadata. these blocks shouldn't
4959 * be used until this transaction is committed
4961 * We use __GFP_NOFAIL because ext4_free_blocks() is not allowed
4964 new_entry
= kmem_cache_alloc(ext4_free_data_cachep
,
4965 GFP_NOFS
|__GFP_NOFAIL
);
4966 new_entry
->efd_start_cluster
= bit
;
4967 new_entry
->efd_group
= block_group
;
4968 new_entry
->efd_count
= count_clusters
;
4969 new_entry
->efd_tid
= handle
->h_transaction
->t_tid
;
4971 ext4_lock_group(sb
, block_group
);
4972 mb_clear_bits(bitmap_bh
->b_data
, bit
, count_clusters
);
4973 ext4_mb_free_metadata(handle
, &e4b
, new_entry
);
4975 /* need to update group_info->bb_free and bitmap
4976 * with group lock held. generate_buddy look at
4977 * them with group lock_held
4979 if (test_opt(sb
, DISCARD
)) {
4980 err
= ext4_issue_discard(sb
, block_group
, bit
, count
,
4982 if (err
&& err
!= -EOPNOTSUPP
)
4983 ext4_msg(sb
, KERN_WARNING
, "discard request in"
4984 " group:%d block:%d count:%lu failed"
4985 " with %d", block_group
, bit
, count
,
4988 EXT4_MB_GRP_CLEAR_TRIMMED(e4b
.bd_info
);
4990 ext4_lock_group(sb
, block_group
);
4991 mb_clear_bits(bitmap_bh
->b_data
, bit
, count_clusters
);
4992 mb_free_blocks(inode
, &e4b
, bit
, count_clusters
);
4995 ret
= ext4_free_group_clusters(sb
, gdp
) + count_clusters
;
4996 ext4_free_group_clusters_set(sb
, gdp
, ret
);
4997 ext4_block_bitmap_csum_set(sb
, block_group
, gdp
, bitmap_bh
);
4998 ext4_group_desc_csum_set(sb
, block_group
, gdp
);
4999 ext4_unlock_group(sb
, block_group
);
5001 if (sbi
->s_log_groups_per_flex
) {
5002 ext4_group_t flex_group
= ext4_flex_group(sbi
, block_group
);
5003 atomic64_add(count_clusters
,
5004 &sbi
->s_flex_groups
[flex_group
].free_clusters
);
5007 if (!(flags
& EXT4_FREE_BLOCKS_NO_QUOT_UPDATE
))
5008 dquot_free_block(inode
, EXT4_C2B(sbi
, count_clusters
));
5009 percpu_counter_add(&sbi
->s_freeclusters_counter
, count_clusters
);
5011 ext4_mb_unload_buddy(&e4b
);
5013 /* We dirtied the bitmap block */
5014 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
5015 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
5017 /* And the group descriptor block */
5018 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
5019 ret
= ext4_handle_dirty_metadata(handle
, NULL
, gd_bh
);
5023 if (overflow
&& !err
) {
5031 ext4_std_error(sb
, err
);
5036 * ext4_group_add_blocks() -- Add given blocks to an existing group
5037 * @handle: handle to this transaction
5039 * @block: start physical block to add to the block group
5040 * @count: number of blocks to free
5042 * This marks the blocks as free in the bitmap and buddy.
5044 int ext4_group_add_blocks(handle_t
*handle
, struct super_block
*sb
,
5045 ext4_fsblk_t block
, unsigned long count
)
5047 struct buffer_head
*bitmap_bh
= NULL
;
5048 struct buffer_head
*gd_bh
;
5049 ext4_group_t block_group
;
5052 struct ext4_group_desc
*desc
;
5053 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
5054 struct ext4_buddy e4b
;
5055 int err
= 0, ret
, blk_free_count
;
5056 ext4_grpblk_t blocks_freed
;
5058 ext4_debug("Adding block(s) %llu-%llu\n", block
, block
+ count
- 1);
5063 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
5065 * Check to see if we are freeing blocks across a group
5068 if (bit
+ count
> EXT4_BLOCKS_PER_GROUP(sb
)) {
5069 ext4_warning(sb
, "too much blocks added to group %u\n",
5075 bitmap_bh
= ext4_read_block_bitmap(sb
, block_group
);
5076 if (IS_ERR(bitmap_bh
)) {
5077 err
= PTR_ERR(bitmap_bh
);
5082 desc
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
5088 if (in_range(ext4_block_bitmap(sb
, desc
), block
, count
) ||
5089 in_range(ext4_inode_bitmap(sb
, desc
), block
, count
) ||
5090 in_range(block
, ext4_inode_table(sb
, desc
), sbi
->s_itb_per_group
) ||
5091 in_range(block
+ count
- 1, ext4_inode_table(sb
, desc
),
5092 sbi
->s_itb_per_group
)) {
5093 ext4_error(sb
, "Adding blocks in system zones - "
5094 "Block = %llu, count = %lu",
5100 BUFFER_TRACE(bitmap_bh
, "getting write access");
5101 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
5106 * We are about to modify some metadata. Call the journal APIs
5107 * to unshare ->b_data if a currently-committing transaction is
5110 BUFFER_TRACE(gd_bh
, "get_write_access");
5111 err
= ext4_journal_get_write_access(handle
, gd_bh
);
5115 for (i
= 0, blocks_freed
= 0; i
< count
; i
++) {
5116 BUFFER_TRACE(bitmap_bh
, "clear bit");
5117 if (!mb_test_bit(bit
+ i
, bitmap_bh
->b_data
)) {
5118 ext4_error(sb
, "bit already cleared for block %llu",
5119 (ext4_fsblk_t
)(block
+ i
));
5120 BUFFER_TRACE(bitmap_bh
, "bit already cleared");
5126 err
= ext4_mb_load_buddy(sb
, block_group
, &e4b
);
5131 * need to update group_info->bb_free and bitmap
5132 * with group lock held. generate_buddy look at
5133 * them with group lock_held
5135 ext4_lock_group(sb
, block_group
);
5136 mb_clear_bits(bitmap_bh
->b_data
, bit
, count
);
5137 mb_free_blocks(NULL
, &e4b
, bit
, count
);
5138 blk_free_count
= blocks_freed
+ ext4_free_group_clusters(sb
, desc
);
5139 ext4_free_group_clusters_set(sb
, desc
, blk_free_count
);
5140 ext4_block_bitmap_csum_set(sb
, block_group
, desc
, bitmap_bh
);
5141 ext4_group_desc_csum_set(sb
, block_group
, desc
);
5142 ext4_unlock_group(sb
, block_group
);
5143 percpu_counter_add(&sbi
->s_freeclusters_counter
,
5144 EXT4_NUM_B2C(sbi
, blocks_freed
));
5146 if (sbi
->s_log_groups_per_flex
) {
5147 ext4_group_t flex_group
= ext4_flex_group(sbi
, block_group
);
5148 atomic64_add(EXT4_NUM_B2C(sbi
, blocks_freed
),
5149 &sbi
->s_flex_groups
[flex_group
].free_clusters
);
5152 ext4_mb_unload_buddy(&e4b
);
5154 /* We dirtied the bitmap block */
5155 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
5156 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
5158 /* And the group descriptor block */
5159 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
5160 ret
= ext4_handle_dirty_metadata(handle
, NULL
, gd_bh
);
5166 ext4_std_error(sb
, err
);
5171 * ext4_trim_extent -- function to TRIM one single free extent in the group
5172 * @sb: super block for the file system
5173 * @start: starting block of the free extent in the alloc. group
5174 * @count: number of blocks to TRIM
5175 * @group: alloc. group we are working with
5176 * @e4b: ext4 buddy for the group
5177 * @blkdev_flags: flags for the block device
5179 * Trim "count" blocks starting at "start" in the "group". To assure that no
5180 * one will allocate those blocks, mark it as used in buddy bitmap. This must
5181 * be called with under the group lock.
5183 static int ext4_trim_extent(struct super_block
*sb
, int start
, int count
,
5184 ext4_group_t group
, struct ext4_buddy
*e4b
,
5185 unsigned long blkdev_flags
)
5189 struct ext4_free_extent ex
;
5192 trace_ext4_trim_extent(sb
, group
, start
, count
);
5194 assert_spin_locked(ext4_group_lock_ptr(sb
, group
));
5196 ex
.fe_start
= start
;
5197 ex
.fe_group
= group
;
5201 * Mark blocks used, so no one can reuse them while
5204 mb_mark_used(e4b
, &ex
);
5205 ext4_unlock_group(sb
, group
);
5206 ret
= ext4_issue_discard(sb
, group
, start
, count
, blkdev_flags
, NULL
);
5207 ext4_lock_group(sb
, group
);
5208 mb_free_blocks(NULL
, e4b
, start
, ex
.fe_len
);
5213 * ext4_trim_all_free -- function to trim all free space in alloc. group
5214 * @sb: super block for file system
5215 * @group: group to be trimmed
5216 * @start: first group block to examine
5217 * @max: last group block to examine
5218 * @minblocks: minimum extent block count
5219 * @blkdev_flags: flags for the block device
5221 * ext4_trim_all_free walks through group's buddy bitmap searching for free
5222 * extents. When the free block is found, ext4_trim_extent is called to TRIM
5226 * ext4_trim_all_free walks through group's block bitmap searching for free
5227 * extents. When the free extent is found, mark it as used in group buddy
5228 * bitmap. Then issue a TRIM command on this extent and free the extent in
5229 * the group buddy bitmap. This is done until whole group is scanned.
5231 static ext4_grpblk_t
5232 ext4_trim_all_free(struct super_block
*sb
, ext4_group_t group
,
5233 ext4_grpblk_t start
, ext4_grpblk_t max
,
5234 ext4_grpblk_t minblocks
, unsigned long blkdev_flags
)
5237 ext4_grpblk_t next
, count
= 0, free_count
= 0;
5238 struct ext4_buddy e4b
;
5241 trace_ext4_trim_all_free(sb
, group
, start
, max
);
5243 ret
= ext4_mb_load_buddy(sb
, group
, &e4b
);
5245 ext4_error(sb
, "Error in loading buddy "
5246 "information for %u", group
);
5249 bitmap
= e4b
.bd_bitmap
;
5251 ext4_lock_group(sb
, group
);
5252 if (EXT4_MB_GRP_WAS_TRIMMED(e4b
.bd_info
) &&
5253 minblocks
>= atomic_read(&EXT4_SB(sb
)->s_last_trim_minblks
))
5256 start
= (e4b
.bd_info
->bb_first_free
> start
) ?
5257 e4b
.bd_info
->bb_first_free
: start
;
5259 while (start
<= max
) {
5260 start
= mb_find_next_zero_bit(bitmap
, max
+ 1, start
);
5263 next
= mb_find_next_bit(bitmap
, max
+ 1, start
);
5265 if ((next
- start
) >= minblocks
) {
5266 ret
= ext4_trim_extent(sb
, start
,
5267 next
- start
, group
, &e4b
,
5269 if (ret
&& ret
!= -EOPNOTSUPP
)
5272 count
+= next
- start
;
5274 free_count
+= next
- start
;
5277 if (fatal_signal_pending(current
)) {
5278 count
= -ERESTARTSYS
;
5282 if (need_resched()) {
5283 ext4_unlock_group(sb
, group
);
5285 ext4_lock_group(sb
, group
);
5288 if ((e4b
.bd_info
->bb_free
- free_count
) < minblocks
)
5294 EXT4_MB_GRP_SET_TRIMMED(e4b
.bd_info
);
5297 ext4_unlock_group(sb
, group
);
5298 ext4_mb_unload_buddy(&e4b
);
5300 ext4_debug("trimmed %d blocks in the group %d\n",
5307 * ext4_trim_fs() -- trim ioctl handle function
5308 * @sb: superblock for filesystem
5309 * @range: fstrim_range structure
5310 * @blkdev_flags: flags for the block device
5312 * start: First Byte to trim
5313 * len: number of Bytes to trim from start
5314 * minlen: minimum extent length in Bytes
5315 * ext4_trim_fs goes through all allocation groups containing Bytes from
5316 * start to start+len. For each such a group ext4_trim_all_free function
5317 * is invoked to trim all free space.
5319 int ext4_trim_fs(struct super_block
*sb
, struct fstrim_range
*range
,
5320 unsigned long blkdev_flags
)
5322 struct ext4_group_info
*grp
;
5323 ext4_group_t group
, first_group
, last_group
;
5324 ext4_grpblk_t cnt
= 0, first_cluster
, last_cluster
;
5325 uint64_t start
, end
, minlen
, trimmed
= 0;
5326 ext4_fsblk_t first_data_blk
=
5327 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
5328 ext4_fsblk_t max_blks
= ext4_blocks_count(EXT4_SB(sb
)->s_es
);
5331 start
= range
->start
>> sb
->s_blocksize_bits
;
5332 end
= start
+ (range
->len
>> sb
->s_blocksize_bits
) - 1;
5333 minlen
= EXT4_NUM_B2C(EXT4_SB(sb
),
5334 range
->minlen
>> sb
->s_blocksize_bits
);
5336 if (minlen
> EXT4_CLUSTERS_PER_GROUP(sb
) ||
5337 start
>= max_blks
||
5338 range
->len
< sb
->s_blocksize
)
5340 if (end
>= max_blks
)
5342 if (end
<= first_data_blk
)
5344 if (start
< first_data_blk
)
5345 start
= first_data_blk
;
5347 /* Determine first and last group to examine based on start and end */
5348 ext4_get_group_no_and_offset(sb
, (ext4_fsblk_t
) start
,
5349 &first_group
, &first_cluster
);
5350 ext4_get_group_no_and_offset(sb
, (ext4_fsblk_t
) end
,
5351 &last_group
, &last_cluster
);
5353 /* end now represents the last cluster to discard in this group */
5354 end
= EXT4_CLUSTERS_PER_GROUP(sb
) - 1;
5356 for (group
= first_group
; group
<= last_group
; group
++) {
5357 grp
= ext4_get_group_info(sb
, group
);
5358 /* We only do this if the grp has never been initialized */
5359 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp
))) {
5360 ret
= ext4_mb_init_group(sb
, group
, GFP_NOFS
);
5366 * For all the groups except the last one, last cluster will
5367 * always be EXT4_CLUSTERS_PER_GROUP(sb)-1, so we only need to
5368 * change it for the last group, note that last_cluster is
5369 * already computed earlier by ext4_get_group_no_and_offset()
5371 if (group
== last_group
)
5374 if (grp
->bb_free
>= minlen
) {
5375 cnt
= ext4_trim_all_free(sb
, group
, first_cluster
,
5376 end
, minlen
, blkdev_flags
);
5385 * For every group except the first one, we are sure
5386 * that the first cluster to discard will be cluster #0.
5392 atomic_set(&EXT4_SB(sb
)->s_last_trim_minblks
, minlen
);
5395 range
->len
= EXT4_C2B(EXT4_SB(sb
), trimmed
) << sb
->s_blocksize_bits
;