2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public Licens
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
21 * mballoc.c contains the multiblocks allocation routines
25 #include <linux/debugfs.h>
26 #include <trace/events/ext4.h>
30 * - test ext4_ext_search_left() and ext4_ext_search_right()
31 * - search for metadata in few groups
34 * - normalization should take into account whether file is still open
35 * - discard preallocations if no free space left (policy?)
36 * - don't normalize tails
38 * - reservation for superuser
41 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
42 * - track min/max extents in each group for better group selection
43 * - mb_mark_used() may allocate chunk right after splitting buddy
44 * - tree of groups sorted by number of free blocks
49 * The allocation request involve request for multiple number of blocks
50 * near to the goal(block) value specified.
52 * During initialization phase of the allocator we decide to use the
53 * group preallocation or inode preallocation depending on the size of
54 * the file. The size of the file could be the resulting file size we
55 * would have after allocation, or the current file size, which ever
56 * is larger. If the size is less than sbi->s_mb_stream_request we
57 * select to use the group preallocation. The default value of
58 * s_mb_stream_request is 16 blocks. This can also be tuned via
59 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
60 * terms of number of blocks.
62 * The main motivation for having small file use group preallocation is to
63 * ensure that we have small files closer together on the disk.
65 * First stage the allocator looks at the inode prealloc list,
66 * ext4_inode_info->i_prealloc_list, which contains list of prealloc
67 * spaces for this particular inode. The inode prealloc space is
70 * pa_lstart -> the logical start block for this prealloc space
71 * pa_pstart -> the physical start block for this prealloc space
72 * pa_len -> lenght for this prealloc space
73 * pa_free -> free space available in this prealloc space
75 * The inode preallocation space is used looking at the _logical_ start
76 * block. If only the logical file block falls within the range of prealloc
77 * space we will consume the particular prealloc space. This make sure that
78 * that the we have contiguous physical blocks representing the file blocks
80 * The important thing to be noted in case of inode prealloc space is that
81 * we don't modify the values associated to inode prealloc space except
84 * If we are not able to find blocks in the inode prealloc space and if we
85 * have the group allocation flag set then we look at the locality group
86 * prealloc space. These are per CPU prealloc list repreasented as
88 * ext4_sb_info.s_locality_groups[smp_processor_id()]
90 * The reason for having a per cpu locality group is to reduce the contention
91 * between CPUs. It is possible to get scheduled at this point.
93 * The locality group prealloc space is used looking at whether we have
94 * enough free space (pa_free) withing the prealloc space.
96 * If we can't allocate blocks via inode prealloc or/and locality group
97 * prealloc then we look at the buddy cache. The buddy cache is represented
98 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
99 * mapped to the buddy and bitmap information regarding different
100 * groups. The buddy information is attached to buddy cache inode so that
101 * we can access them through the page cache. The information regarding
102 * each group is loaded via ext4_mb_load_buddy. The information involve
103 * block bitmap and buddy information. The information are stored in the
107 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
110 * one block each for bitmap and buddy information. So for each group we
111 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
112 * blocksize) blocks. So it can have information regarding groups_per_page
113 * which is blocks_per_page/2
115 * The buddy cache inode is not stored on disk. The inode is thrown
116 * away when the filesystem is unmounted.
118 * We look for count number of blocks in the buddy cache. If we were able
119 * to locate that many free blocks we return with additional information
120 * regarding rest of the contiguous physical block available
122 * Before allocating blocks via buddy cache we normalize the request
123 * blocks. This ensure we ask for more blocks that we needed. The extra
124 * blocks that we get after allocation is added to the respective prealloc
125 * list. In case of inode preallocation we follow a list of heuristics
126 * based on file size. This can be found in ext4_mb_normalize_request. If
127 * we are doing a group prealloc we try to normalize the request to
128 * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is
129 * 512 blocks. This can be tuned via
130 * /sys/fs/ext4/<partition/mb_group_prealloc. The value is represented in
131 * terms of number of blocks. If we have mounted the file system with -O
132 * stripe=<value> option the group prealloc request is normalized to the
133 * stripe value (sbi->s_stripe)
135 * The regular allocator(using the buddy cache) supports few tunables.
137 * /sys/fs/ext4/<partition>/mb_min_to_scan
138 * /sys/fs/ext4/<partition>/mb_max_to_scan
139 * /sys/fs/ext4/<partition>/mb_order2_req
141 * The regular allocator uses buddy scan only if the request len is power of
142 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
143 * value of s_mb_order2_reqs can be tuned via
144 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
145 * stripe size (sbi->s_stripe), we try to search for contigous block in
146 * stripe size. This should result in better allocation on RAID setups. If
147 * not, we search in the specific group using bitmap for best extents. The
148 * tunable min_to_scan and max_to_scan control the behaviour here.
149 * min_to_scan indicate how long the mballoc __must__ look for a best
150 * extent and max_to_scan indicates how long the mballoc __can__ look for a
151 * best extent in the found extents. Searching for the blocks starts with
152 * the group specified as the goal value in allocation context via
153 * ac_g_ex. Each group is first checked based on the criteria whether it
154 * can used for allocation. ext4_mb_good_group explains how the groups are
157 * Both the prealloc space are getting populated as above. So for the first
158 * request we will hit the buddy cache which will result in this prealloc
159 * space getting filled. The prealloc space is then later used for the
160 * subsequent request.
164 * mballoc operates on the following data:
166 * - in-core buddy (actually includes buddy and bitmap)
167 * - preallocation descriptors (PAs)
169 * there are two types of preallocations:
171 * assiged to specific inode and can be used for this inode only.
172 * it describes part of inode's space preallocated to specific
173 * physical blocks. any block from that preallocated can be used
174 * independent. the descriptor just tracks number of blocks left
175 * unused. so, before taking some block from descriptor, one must
176 * make sure corresponded logical block isn't allocated yet. this
177 * also means that freeing any block within descriptor's range
178 * must discard all preallocated blocks.
180 * assigned to specific locality group which does not translate to
181 * permanent set of inodes: inode can join and leave group. space
182 * from this type of preallocation can be used for any inode. thus
183 * it's consumed from the beginning to the end.
185 * relation between them can be expressed as:
186 * in-core buddy = on-disk bitmap + preallocation descriptors
188 * this mean blocks mballoc considers used are:
189 * - allocated blocks (persistent)
190 * - preallocated blocks (non-persistent)
192 * consistency in mballoc world means that at any time a block is either
193 * free or used in ALL structures. notice: "any time" should not be read
194 * literally -- time is discrete and delimited by locks.
196 * to keep it simple, we don't use block numbers, instead we count number of
197 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
199 * all operations can be expressed as:
200 * - init buddy: buddy = on-disk + PAs
201 * - new PA: buddy += N; PA = N
202 * - use inode PA: on-disk += N; PA -= N
203 * - discard inode PA buddy -= on-disk - PA; PA = 0
204 * - use locality group PA on-disk += N; PA -= N
205 * - discard locality group PA buddy -= PA; PA = 0
206 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
207 * is used in real operation because we can't know actual used
208 * bits from PA, only from on-disk bitmap
210 * if we follow this strict logic, then all operations above should be atomic.
211 * given some of them can block, we'd have to use something like semaphores
212 * killing performance on high-end SMP hardware. let's try to relax it using
213 * the following knowledge:
214 * 1) if buddy is referenced, it's already initialized
215 * 2) while block is used in buddy and the buddy is referenced,
216 * nobody can re-allocate that block
217 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
218 * bit set and PA claims same block, it's OK. IOW, one can set bit in
219 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
222 * so, now we're building a concurrency table:
225 * blocks for PA are allocated in the buddy, buddy must be referenced
226 * until PA is linked to allocation group to avoid concurrent buddy init
228 * we need to make sure that either on-disk bitmap or PA has uptodate data
229 * given (3) we care that PA-=N operation doesn't interfere with init
231 * the simplest way would be to have buddy initialized by the discard
232 * - use locality group PA
233 * again PA-=N must be serialized with init
234 * - discard locality group PA
235 * the simplest way would be to have buddy initialized by the discard
238 * i_data_sem serializes them
240 * discard process must wait until PA isn't used by another process
241 * - use locality group PA
242 * some mutex should serialize them
243 * - discard locality group PA
244 * discard process must wait until PA isn't used by another process
247 * i_data_sem or another mutex should serializes them
249 * discard process must wait until PA isn't used by another process
250 * - use locality group PA
251 * nothing wrong here -- they're different PAs covering different blocks
252 * - discard locality group PA
253 * discard process must wait until PA isn't used by another process
255 * now we're ready to make few consequences:
256 * - PA is referenced and while it is no discard is possible
257 * - PA is referenced until block isn't marked in on-disk bitmap
258 * - PA changes only after on-disk bitmap
259 * - discard must not compete with init. either init is done before
260 * any discard or they're serialized somehow
261 * - buddy init as sum of on-disk bitmap and PAs is done atomically
263 * a special case when we've used PA to emptiness. no need to modify buddy
264 * in this case, but we should care about concurrent init
269 * Logic in few words:
274 * mark bits in on-disk bitmap
277 * - use preallocation:
278 * find proper PA (per-inode or group)
280 * mark bits in on-disk bitmap
286 * mark bits in on-disk bitmap
289 * - discard preallocations in group:
291 * move them onto local list
292 * load on-disk bitmap
294 * remove PA from object (inode or locality group)
295 * mark free blocks in-core
297 * - discard inode's preallocations:
304 * - bitlock on a group (group)
305 * - object (inode/locality) (object)
316 * - release consumed pa:
321 * - generate in-core bitmap:
325 * - discard all for given object (inode, locality group):
330 * - discard all for given group:
337 static struct kmem_cache
*ext4_pspace_cachep
;
338 static struct kmem_cache
*ext4_ac_cachep
;
339 static struct kmem_cache
*ext4_free_ext_cachep
;
340 static void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
342 static void ext4_mb_generate_from_freelist(struct super_block
*sb
, void *bitmap
,
344 static void release_blocks_on_commit(journal_t
*journal
, transaction_t
*txn
);
346 static inline void *mb_correct_addr_and_bit(int *bit
, void *addr
)
348 #if BITS_PER_LONG == 64
349 *bit
+= ((unsigned long) addr
& 7UL) << 3;
350 addr
= (void *) ((unsigned long) addr
& ~7UL);
351 #elif BITS_PER_LONG == 32
352 *bit
+= ((unsigned long) addr
& 3UL) << 3;
353 addr
= (void *) ((unsigned long) addr
& ~3UL);
355 #error "how many bits you are?!"
360 static inline int mb_test_bit(int bit
, void *addr
)
363 * ext4_test_bit on architecture like powerpc
364 * needs unsigned long aligned address
366 addr
= mb_correct_addr_and_bit(&bit
, addr
);
367 return ext4_test_bit(bit
, addr
);
370 static inline void mb_set_bit(int bit
, void *addr
)
372 addr
= mb_correct_addr_and_bit(&bit
, addr
);
373 ext4_set_bit(bit
, addr
);
376 static inline void mb_clear_bit(int bit
, void *addr
)
378 addr
= mb_correct_addr_and_bit(&bit
, addr
);
379 ext4_clear_bit(bit
, addr
);
382 static inline int mb_find_next_zero_bit(void *addr
, int max
, int start
)
384 int fix
= 0, ret
, tmpmax
;
385 addr
= mb_correct_addr_and_bit(&fix
, addr
);
389 ret
= ext4_find_next_zero_bit(addr
, tmpmax
, start
) - fix
;
395 static inline int mb_find_next_bit(void *addr
, int max
, int start
)
397 int fix
= 0, ret
, tmpmax
;
398 addr
= mb_correct_addr_and_bit(&fix
, addr
);
402 ret
= ext4_find_next_bit(addr
, tmpmax
, start
) - fix
;
408 static void *mb_find_buddy(struct ext4_buddy
*e4b
, int order
, int *max
)
412 BUG_ON(EXT4_MB_BITMAP(e4b
) == EXT4_MB_BUDDY(e4b
));
415 if (order
> e4b
->bd_blkbits
+ 1) {
420 /* at order 0 we see each particular block */
421 *max
= 1 << (e4b
->bd_blkbits
+ 3);
423 return EXT4_MB_BITMAP(e4b
);
425 bb
= EXT4_MB_BUDDY(e4b
) + EXT4_SB(e4b
->bd_sb
)->s_mb_offsets
[order
];
426 *max
= EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[order
];
432 static void mb_free_blocks_double(struct inode
*inode
, struct ext4_buddy
*e4b
,
433 int first
, int count
)
436 struct super_block
*sb
= e4b
->bd_sb
;
438 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
440 assert_spin_locked(ext4_group_lock_ptr(sb
, e4b
->bd_group
));
441 for (i
= 0; i
< count
; i
++) {
442 if (!mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
)) {
443 ext4_fsblk_t blocknr
;
444 blocknr
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
);
445 blocknr
+= first
+ i
;
447 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
448 ext4_grp_locked_error(sb
, e4b
->bd_group
,
449 __func__
, "double-free of inode"
450 " %lu's block %llu(bit %u in group %u)",
451 inode
? inode
->i_ino
: 0, blocknr
,
452 first
+ i
, e4b
->bd_group
);
454 mb_clear_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
458 static void mb_mark_used_double(struct ext4_buddy
*e4b
, int first
, int count
)
462 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
464 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
465 for (i
= 0; i
< count
; i
++) {
466 BUG_ON(mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
));
467 mb_set_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
471 static void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
473 if (memcmp(e4b
->bd_info
->bb_bitmap
, bitmap
, e4b
->bd_sb
->s_blocksize
)) {
474 unsigned char *b1
, *b2
;
476 b1
= (unsigned char *) e4b
->bd_info
->bb_bitmap
;
477 b2
= (unsigned char *) bitmap
;
478 for (i
= 0; i
< e4b
->bd_sb
->s_blocksize
; i
++) {
479 if (b1
[i
] != b2
[i
]) {
480 printk(KERN_ERR
"corruption in group %u "
481 "at byte %u(%u): %x in copy != %x "
482 "on disk/prealloc\n",
483 e4b
->bd_group
, i
, i
* 8, b1
[i
], b2
[i
]);
491 static inline void mb_free_blocks_double(struct inode
*inode
,
492 struct ext4_buddy
*e4b
, int first
, int count
)
496 static inline void mb_mark_used_double(struct ext4_buddy
*e4b
,
497 int first
, int count
)
501 static inline void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
507 #ifdef AGGRESSIVE_CHECK
509 #define MB_CHECK_ASSERT(assert) \
513 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
514 function, file, line, # assert); \
519 static int __mb_check_buddy(struct ext4_buddy
*e4b
, char *file
,
520 const char *function
, int line
)
522 struct super_block
*sb
= e4b
->bd_sb
;
523 int order
= e4b
->bd_blkbits
+ 1;
530 struct ext4_group_info
*grp
;
533 struct list_head
*cur
;
538 static int mb_check_counter
;
539 if (mb_check_counter
++ % 100 != 0)
544 buddy
= mb_find_buddy(e4b
, order
, &max
);
545 MB_CHECK_ASSERT(buddy
);
546 buddy2
= mb_find_buddy(e4b
, order
- 1, &max2
);
547 MB_CHECK_ASSERT(buddy2
);
548 MB_CHECK_ASSERT(buddy
!= buddy2
);
549 MB_CHECK_ASSERT(max
* 2 == max2
);
552 for (i
= 0; i
< max
; i
++) {
554 if (mb_test_bit(i
, buddy
)) {
555 /* only single bit in buddy2 may be 1 */
556 if (!mb_test_bit(i
<< 1, buddy2
)) {
558 mb_test_bit((i
<<1)+1, buddy2
));
559 } else if (!mb_test_bit((i
<< 1) + 1, buddy2
)) {
561 mb_test_bit(i
<< 1, buddy2
));
566 /* both bits in buddy2 must be 0 */
567 MB_CHECK_ASSERT(mb_test_bit(i
<< 1, buddy2
));
568 MB_CHECK_ASSERT(mb_test_bit((i
<< 1) + 1, buddy2
));
570 for (j
= 0; j
< (1 << order
); j
++) {
571 k
= (i
* (1 << order
)) + j
;
573 !mb_test_bit(k
, EXT4_MB_BITMAP(e4b
)));
577 MB_CHECK_ASSERT(e4b
->bd_info
->bb_counters
[order
] == count
);
582 buddy
= mb_find_buddy(e4b
, 0, &max
);
583 for (i
= 0; i
< max
; i
++) {
584 if (!mb_test_bit(i
, buddy
)) {
585 MB_CHECK_ASSERT(i
>= e4b
->bd_info
->bb_first_free
);
593 /* check used bits only */
594 for (j
= 0; j
< e4b
->bd_blkbits
+ 1; j
++) {
595 buddy2
= mb_find_buddy(e4b
, j
, &max2
);
597 MB_CHECK_ASSERT(k
< max2
);
598 MB_CHECK_ASSERT(mb_test_bit(k
, buddy2
));
601 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b
->bd_info
));
602 MB_CHECK_ASSERT(e4b
->bd_info
->bb_fragments
== fragments
);
604 grp
= ext4_get_group_info(sb
, e4b
->bd_group
);
605 buddy
= mb_find_buddy(e4b
, 0, &max
);
606 list_for_each(cur
, &grp
->bb_prealloc_list
) {
607 ext4_group_t groupnr
;
608 struct ext4_prealloc_space
*pa
;
609 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
610 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &groupnr
, &k
);
611 MB_CHECK_ASSERT(groupnr
== e4b
->bd_group
);
612 for (i
= 0; i
< pa
->pa_len
; i
++)
613 MB_CHECK_ASSERT(mb_test_bit(k
+ i
, buddy
));
617 #undef MB_CHECK_ASSERT
618 #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
619 __FILE__, __func__, __LINE__)
621 #define mb_check_buddy(e4b)
624 /* FIXME!! need more doc */
625 static void ext4_mb_mark_free_simple(struct super_block
*sb
,
626 void *buddy
, unsigned first
, int len
,
627 struct ext4_group_info
*grp
)
629 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
632 unsigned short chunk
;
633 unsigned short border
;
635 BUG_ON(len
> EXT4_BLOCKS_PER_GROUP(sb
));
637 border
= 2 << sb
->s_blocksize_bits
;
640 /* find how many blocks can be covered since this position */
641 max
= ffs(first
| border
) - 1;
643 /* find how many blocks of power 2 we need to mark */
650 /* mark multiblock chunks only */
651 grp
->bb_counters
[min
]++;
653 mb_clear_bit(first
>> min
,
654 buddy
+ sbi
->s_mb_offsets
[min
]);
661 static noinline_for_stack
662 void ext4_mb_generate_buddy(struct super_block
*sb
,
663 void *buddy
, void *bitmap
, ext4_group_t group
)
665 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
666 unsigned short max
= EXT4_BLOCKS_PER_GROUP(sb
);
667 unsigned short i
= 0;
668 unsigned short first
;
671 unsigned fragments
= 0;
672 unsigned long long period
= get_cycles();
674 /* initialize buddy from bitmap which is aggregation
675 * of on-disk bitmap and preallocations */
676 i
= mb_find_next_zero_bit(bitmap
, max
, 0);
677 grp
->bb_first_free
= i
;
681 i
= mb_find_next_bit(bitmap
, max
, i
);
685 ext4_mb_mark_free_simple(sb
, buddy
, first
, len
, grp
);
687 grp
->bb_counters
[0]++;
689 i
= mb_find_next_zero_bit(bitmap
, max
, i
);
691 grp
->bb_fragments
= fragments
;
693 if (free
!= grp
->bb_free
) {
694 ext4_grp_locked_error(sb
, group
, __func__
,
695 "EXT4-fs: group %u: %u blocks in bitmap, %u in gd",
696 group
, free
, grp
->bb_free
);
698 * If we intent to continue, we consider group descritor
699 * corrupt and update bb_free using bitmap value
704 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
, &(grp
->bb_state
));
706 period
= get_cycles() - period
;
707 spin_lock(&EXT4_SB(sb
)->s_bal_lock
);
708 EXT4_SB(sb
)->s_mb_buddies_generated
++;
709 EXT4_SB(sb
)->s_mb_generation_time
+= period
;
710 spin_unlock(&EXT4_SB(sb
)->s_bal_lock
);
713 /* The buddy information is attached the buddy cache inode
714 * for convenience. The information regarding each group
715 * is loaded via ext4_mb_load_buddy. The information involve
716 * block bitmap and buddy information. The information are
717 * stored in the inode as
720 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
723 * one block each for bitmap and buddy information.
724 * So for each group we take up 2 blocks. A page can
725 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
726 * So it can have information regarding groups_per_page which
727 * is blocks_per_page/2
730 static int ext4_mb_init_cache(struct page
*page
, char *incore
)
732 ext4_group_t ngroups
;
738 ext4_group_t first_group
;
740 struct super_block
*sb
;
741 struct buffer_head
*bhs
;
742 struct buffer_head
**bh
;
747 mb_debug(1, "init page %lu\n", page
->index
);
749 inode
= page
->mapping
->host
;
751 ngroups
= ext4_get_groups_count(sb
);
752 blocksize
= 1 << inode
->i_blkbits
;
753 blocks_per_page
= PAGE_CACHE_SIZE
/ blocksize
;
755 groups_per_page
= blocks_per_page
>> 1;
756 if (groups_per_page
== 0)
759 /* allocate buffer_heads to read bitmaps */
760 if (groups_per_page
> 1) {
762 i
= sizeof(struct buffer_head
*) * groups_per_page
;
763 bh
= kzalloc(i
, GFP_NOFS
);
769 first_group
= page
->index
* blocks_per_page
/ 2;
771 /* read all groups the page covers into the cache */
772 for (i
= 0; i
< groups_per_page
; i
++) {
773 struct ext4_group_desc
*desc
;
775 if (first_group
+ i
>= ngroups
)
779 desc
= ext4_get_group_desc(sb
, first_group
+ i
, NULL
);
784 bh
[i
] = sb_getblk(sb
, ext4_block_bitmap(sb
, desc
));
788 if (bitmap_uptodate(bh
[i
]))
792 if (bitmap_uptodate(bh
[i
])) {
793 unlock_buffer(bh
[i
]);
796 ext4_lock_group(sb
, first_group
+ i
);
797 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
798 ext4_init_block_bitmap(sb
, bh
[i
],
799 first_group
+ i
, desc
);
800 set_bitmap_uptodate(bh
[i
]);
801 set_buffer_uptodate(bh
[i
]);
802 ext4_unlock_group(sb
, first_group
+ i
);
803 unlock_buffer(bh
[i
]);
806 ext4_unlock_group(sb
, first_group
+ i
);
807 if (buffer_uptodate(bh
[i
])) {
809 * if not uninit if bh is uptodate,
810 * bitmap is also uptodate
812 set_bitmap_uptodate(bh
[i
]);
813 unlock_buffer(bh
[i
]);
818 * submit the buffer_head for read. We can
819 * safely mark the bitmap as uptodate now.
820 * We do it here so the bitmap uptodate bit
821 * get set with buffer lock held.
823 set_bitmap_uptodate(bh
[i
]);
824 bh
[i
]->b_end_io
= end_buffer_read_sync
;
825 submit_bh(READ
, bh
[i
]);
826 mb_debug(1, "read bitmap for group %u\n", first_group
+ i
);
829 /* wait for I/O completion */
830 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
831 wait_on_buffer(bh
[i
]);
834 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
835 if (!buffer_uptodate(bh
[i
]))
839 first_block
= page
->index
* blocks_per_page
;
841 memset(page_address(page
), 0xff, PAGE_CACHE_SIZE
);
842 for (i
= 0; i
< blocks_per_page
; i
++) {
844 struct ext4_group_info
*grinfo
;
846 group
= (first_block
+ i
) >> 1;
847 if (group
>= ngroups
)
851 * data carry information regarding this
852 * particular group in the format specified
856 data
= page_address(page
) + (i
* blocksize
);
857 bitmap
= bh
[group
- first_group
]->b_data
;
860 * We place the buddy block and bitmap block
863 if ((first_block
+ i
) & 1) {
864 /* this is block of buddy */
865 BUG_ON(incore
== NULL
);
866 mb_debug(1, "put buddy for group %u in page %lu/%x\n",
867 group
, page
->index
, i
* blocksize
);
868 grinfo
= ext4_get_group_info(sb
, group
);
869 grinfo
->bb_fragments
= 0;
870 memset(grinfo
->bb_counters
, 0,
871 sizeof(unsigned short)*(sb
->s_blocksize_bits
+2));
873 * incore got set to the group block bitmap below
875 ext4_lock_group(sb
, group
);
876 ext4_mb_generate_buddy(sb
, data
, incore
, group
);
877 ext4_unlock_group(sb
, group
);
880 /* this is block of bitmap */
881 BUG_ON(incore
!= NULL
);
882 mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
883 group
, page
->index
, i
* blocksize
);
885 /* see comments in ext4_mb_put_pa() */
886 ext4_lock_group(sb
, group
);
887 memcpy(data
, bitmap
, blocksize
);
889 /* mark all preallocated blks used in in-core bitmap */
890 ext4_mb_generate_from_pa(sb
, data
, group
);
891 ext4_mb_generate_from_freelist(sb
, data
, group
);
892 ext4_unlock_group(sb
, group
);
894 /* set incore so that the buddy information can be
895 * generated using this
900 SetPageUptodate(page
);
904 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
912 static noinline_for_stack
int
913 ext4_mb_load_buddy(struct super_block
*sb
, ext4_group_t group
,
914 struct ext4_buddy
*e4b
)
922 struct ext4_group_info
*grp
;
923 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
924 struct inode
*inode
= sbi
->s_buddy_cache
;
926 mb_debug(1, "load group %u\n", group
);
928 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
929 grp
= ext4_get_group_info(sb
, group
);
931 e4b
->bd_blkbits
= sb
->s_blocksize_bits
;
932 e4b
->bd_info
= ext4_get_group_info(sb
, group
);
934 e4b
->bd_group
= group
;
935 e4b
->bd_buddy_page
= NULL
;
936 e4b
->bd_bitmap_page
= NULL
;
937 e4b
->alloc_semp
= &grp
->alloc_sem
;
939 /* Take the read lock on the group alloc
940 * sem. This would make sure a parallel
941 * ext4_mb_init_group happening on other
942 * groups mapped by the page is blocked
943 * till we are done with allocation
945 down_read(e4b
->alloc_semp
);
948 * the buddy cache inode stores the block bitmap
949 * and buddy information in consecutive blocks.
950 * So for each group we need two blocks.
953 pnum
= block
/ blocks_per_page
;
954 poff
= block
% blocks_per_page
;
956 /* we could use find_or_create_page(), but it locks page
957 * what we'd like to avoid in fast path ... */
958 page
= find_get_page(inode
->i_mapping
, pnum
);
959 if (page
== NULL
|| !PageUptodate(page
)) {
962 * drop the page reference and try
963 * to get the page with lock. If we
964 * are not uptodate that implies
965 * somebody just created the page but
966 * is yet to initialize the same. So
967 * wait for it to initialize.
969 page_cache_release(page
);
970 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
972 BUG_ON(page
->mapping
!= inode
->i_mapping
);
973 if (!PageUptodate(page
)) {
974 ret
= ext4_mb_init_cache(page
, NULL
);
979 mb_cmp_bitmaps(e4b
, page_address(page
) +
980 (poff
* sb
->s_blocksize
));
985 if (page
== NULL
|| !PageUptodate(page
)) {
989 e4b
->bd_bitmap_page
= page
;
990 e4b
->bd_bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
991 mark_page_accessed(page
);
994 pnum
= block
/ blocks_per_page
;
995 poff
= block
% blocks_per_page
;
997 page
= find_get_page(inode
->i_mapping
, pnum
);
998 if (page
== NULL
|| !PageUptodate(page
)) {
1000 page_cache_release(page
);
1001 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1003 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1004 if (!PageUptodate(page
)) {
1005 ret
= ext4_mb_init_cache(page
, e4b
->bd_bitmap
);
1014 if (page
== NULL
|| !PageUptodate(page
)) {
1018 e4b
->bd_buddy_page
= page
;
1019 e4b
->bd_buddy
= page_address(page
) + (poff
* sb
->s_blocksize
);
1020 mark_page_accessed(page
);
1022 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
1023 BUG_ON(e4b
->bd_buddy_page
== NULL
);
1028 if (e4b
->bd_bitmap_page
)
1029 page_cache_release(e4b
->bd_bitmap_page
);
1030 if (e4b
->bd_buddy_page
)
1031 page_cache_release(e4b
->bd_buddy_page
);
1032 e4b
->bd_buddy
= NULL
;
1033 e4b
->bd_bitmap
= NULL
;
1035 /* Done with the buddy cache */
1036 up_read(e4b
->alloc_semp
);
1040 static void ext4_mb_release_desc(struct ext4_buddy
*e4b
)
1042 if (e4b
->bd_bitmap_page
)
1043 page_cache_release(e4b
->bd_bitmap_page
);
1044 if (e4b
->bd_buddy_page
)
1045 page_cache_release(e4b
->bd_buddy_page
);
1046 /* Done with the buddy cache */
1047 if (e4b
->alloc_semp
)
1048 up_read(e4b
->alloc_semp
);
1052 static int mb_find_order_for_block(struct ext4_buddy
*e4b
, int block
)
1057 BUG_ON(EXT4_MB_BITMAP(e4b
) == EXT4_MB_BUDDY(e4b
));
1058 BUG_ON(block
>= (1 << (e4b
->bd_blkbits
+ 3)));
1060 bb
= EXT4_MB_BUDDY(e4b
);
1061 while (order
<= e4b
->bd_blkbits
+ 1) {
1063 if (!mb_test_bit(block
, bb
)) {
1064 /* this block is part of buddy of order 'order' */
1067 bb
+= 1 << (e4b
->bd_blkbits
- order
);
1073 static void mb_clear_bits(void *bm
, int cur
, int len
)
1079 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1080 /* fast path: clear whole word at once */
1081 addr
= bm
+ (cur
>> 3);
1086 mb_clear_bit(cur
, bm
);
1091 static void mb_set_bits(void *bm
, int cur
, int len
)
1097 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1098 /* fast path: set whole word at once */
1099 addr
= bm
+ (cur
>> 3);
1104 mb_set_bit(cur
, bm
);
1109 static void mb_free_blocks(struct inode
*inode
, struct ext4_buddy
*e4b
,
1110 int first
, int count
)
1117 struct super_block
*sb
= e4b
->bd_sb
;
1119 BUG_ON(first
+ count
> (sb
->s_blocksize
<< 3));
1120 assert_spin_locked(ext4_group_lock_ptr(sb
, e4b
->bd_group
));
1121 mb_check_buddy(e4b
);
1122 mb_free_blocks_double(inode
, e4b
, first
, count
);
1124 e4b
->bd_info
->bb_free
+= count
;
1125 if (first
< e4b
->bd_info
->bb_first_free
)
1126 e4b
->bd_info
->bb_first_free
= first
;
1128 /* let's maintain fragments counter */
1130 block
= !mb_test_bit(first
- 1, EXT4_MB_BITMAP(e4b
));
1131 if (first
+ count
< EXT4_SB(sb
)->s_mb_maxs
[0])
1132 max
= !mb_test_bit(first
+ count
, EXT4_MB_BITMAP(e4b
));
1134 e4b
->bd_info
->bb_fragments
--;
1135 else if (!block
&& !max
)
1136 e4b
->bd_info
->bb_fragments
++;
1138 /* let's maintain buddy itself */
1139 while (count
-- > 0) {
1143 if (!mb_test_bit(block
, EXT4_MB_BITMAP(e4b
))) {
1144 ext4_fsblk_t blocknr
;
1145 blocknr
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
);
1148 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
1149 ext4_grp_locked_error(sb
, e4b
->bd_group
,
1150 __func__
, "double-free of inode"
1151 " %lu's block %llu(bit %u in group %u)",
1152 inode
? inode
->i_ino
: 0, blocknr
, block
,
1155 mb_clear_bit(block
, EXT4_MB_BITMAP(e4b
));
1156 e4b
->bd_info
->bb_counters
[order
]++;
1158 /* start of the buddy */
1159 buddy
= mb_find_buddy(e4b
, order
, &max
);
1163 if (mb_test_bit(block
, buddy
) ||
1164 mb_test_bit(block
+ 1, buddy
))
1167 /* both the buddies are free, try to coalesce them */
1168 buddy2
= mb_find_buddy(e4b
, order
+ 1, &max
);
1174 /* for special purposes, we don't set
1175 * free bits in bitmap */
1176 mb_set_bit(block
, buddy
);
1177 mb_set_bit(block
+ 1, buddy
);
1179 e4b
->bd_info
->bb_counters
[order
]--;
1180 e4b
->bd_info
->bb_counters
[order
]--;
1184 e4b
->bd_info
->bb_counters
[order
]++;
1186 mb_clear_bit(block
, buddy2
);
1190 mb_check_buddy(e4b
);
1193 static int mb_find_extent(struct ext4_buddy
*e4b
, int order
, int block
,
1194 int needed
, struct ext4_free_extent
*ex
)
1201 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
1204 buddy
= mb_find_buddy(e4b
, order
, &max
);
1205 BUG_ON(buddy
== NULL
);
1206 BUG_ON(block
>= max
);
1207 if (mb_test_bit(block
, buddy
)) {
1214 /* FIXME dorp order completely ? */
1215 if (likely(order
== 0)) {
1216 /* find actual order */
1217 order
= mb_find_order_for_block(e4b
, block
);
1218 block
= block
>> order
;
1221 ex
->fe_len
= 1 << order
;
1222 ex
->fe_start
= block
<< order
;
1223 ex
->fe_group
= e4b
->bd_group
;
1225 /* calc difference from given start */
1226 next
= next
- ex
->fe_start
;
1228 ex
->fe_start
+= next
;
1230 while (needed
> ex
->fe_len
&&
1231 (buddy
= mb_find_buddy(e4b
, order
, &max
))) {
1233 if (block
+ 1 >= max
)
1236 next
= (block
+ 1) * (1 << order
);
1237 if (mb_test_bit(next
, EXT4_MB_BITMAP(e4b
)))
1240 ord
= mb_find_order_for_block(e4b
, next
);
1243 block
= next
>> order
;
1244 ex
->fe_len
+= 1 << order
;
1247 BUG_ON(ex
->fe_start
+ ex
->fe_len
> (1 << (e4b
->bd_blkbits
+ 3)));
1251 static int mb_mark_used(struct ext4_buddy
*e4b
, struct ext4_free_extent
*ex
)
1257 int start
= ex
->fe_start
;
1258 int len
= ex
->fe_len
;
1263 BUG_ON(start
+ len
> (e4b
->bd_sb
->s_blocksize
<< 3));
1264 BUG_ON(e4b
->bd_group
!= ex
->fe_group
);
1265 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
1266 mb_check_buddy(e4b
);
1267 mb_mark_used_double(e4b
, start
, len
);
1269 e4b
->bd_info
->bb_free
-= len
;
1270 if (e4b
->bd_info
->bb_first_free
== start
)
1271 e4b
->bd_info
->bb_first_free
+= len
;
1273 /* let's maintain fragments counter */
1275 mlen
= !mb_test_bit(start
- 1, EXT4_MB_BITMAP(e4b
));
1276 if (start
+ len
< EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[0])
1277 max
= !mb_test_bit(start
+ len
, EXT4_MB_BITMAP(e4b
));
1279 e4b
->bd_info
->bb_fragments
++;
1280 else if (!mlen
&& !max
)
1281 e4b
->bd_info
->bb_fragments
--;
1283 /* let's maintain buddy itself */
1285 ord
= mb_find_order_for_block(e4b
, start
);
1287 if (((start
>> ord
) << ord
) == start
&& len
>= (1 << ord
)) {
1288 /* the whole chunk may be allocated at once! */
1290 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1291 BUG_ON((start
>> ord
) >= max
);
1292 mb_set_bit(start
>> ord
, buddy
);
1293 e4b
->bd_info
->bb_counters
[ord
]--;
1300 /* store for history */
1302 ret
= len
| (ord
<< 16);
1304 /* we have to split large buddy */
1306 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1307 mb_set_bit(start
>> ord
, buddy
);
1308 e4b
->bd_info
->bb_counters
[ord
]--;
1311 cur
= (start
>> ord
) & ~1U;
1312 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1313 mb_clear_bit(cur
, buddy
);
1314 mb_clear_bit(cur
+ 1, buddy
);
1315 e4b
->bd_info
->bb_counters
[ord
]++;
1316 e4b
->bd_info
->bb_counters
[ord
]++;
1319 mb_set_bits(EXT4_MB_BITMAP(e4b
), ex
->fe_start
, len0
);
1320 mb_check_buddy(e4b
);
1326 * Must be called under group lock!
1328 static void ext4_mb_use_best_found(struct ext4_allocation_context
*ac
,
1329 struct ext4_buddy
*e4b
)
1331 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1334 BUG_ON(ac
->ac_b_ex
.fe_group
!= e4b
->bd_group
);
1335 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1337 ac
->ac_b_ex
.fe_len
= min(ac
->ac_b_ex
.fe_len
, ac
->ac_g_ex
.fe_len
);
1338 ac
->ac_b_ex
.fe_logical
= ac
->ac_g_ex
.fe_logical
;
1339 ret
= mb_mark_used(e4b
, &ac
->ac_b_ex
);
1341 /* preallocation can change ac_b_ex, thus we store actually
1342 * allocated blocks for history */
1343 ac
->ac_f_ex
= ac
->ac_b_ex
;
1345 ac
->ac_status
= AC_STATUS_FOUND
;
1346 ac
->ac_tail
= ret
& 0xffff;
1347 ac
->ac_buddy
= ret
>> 16;
1350 * take the page reference. We want the page to be pinned
1351 * so that we don't get a ext4_mb_init_cache_call for this
1352 * group until we update the bitmap. That would mean we
1353 * double allocate blocks. The reference is dropped
1354 * in ext4_mb_release_context
1356 ac
->ac_bitmap_page
= e4b
->bd_bitmap_page
;
1357 get_page(ac
->ac_bitmap_page
);
1358 ac
->ac_buddy_page
= e4b
->bd_buddy_page
;
1359 get_page(ac
->ac_buddy_page
);
1360 /* on allocation we use ac to track the held semaphore */
1361 ac
->alloc_semp
= e4b
->alloc_semp
;
1362 e4b
->alloc_semp
= NULL
;
1363 /* store last allocated for subsequent stream allocation */
1364 if (ac
->ac_flags
& EXT4_MB_STREAM_ALLOC
) {
1365 spin_lock(&sbi
->s_md_lock
);
1366 sbi
->s_mb_last_group
= ac
->ac_f_ex
.fe_group
;
1367 sbi
->s_mb_last_start
= ac
->ac_f_ex
.fe_start
;
1368 spin_unlock(&sbi
->s_md_lock
);
1373 * regular allocator, for general purposes allocation
1376 static void ext4_mb_check_limits(struct ext4_allocation_context
*ac
,
1377 struct ext4_buddy
*e4b
,
1380 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1381 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1382 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1383 struct ext4_free_extent ex
;
1386 if (ac
->ac_status
== AC_STATUS_FOUND
)
1389 * We don't want to scan for a whole year
1391 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
&&
1392 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1393 ac
->ac_status
= AC_STATUS_BREAK
;
1398 * Haven't found good chunk so far, let's continue
1400 if (bex
->fe_len
< gex
->fe_len
)
1403 if ((finish_group
|| ac
->ac_found
> sbi
->s_mb_min_to_scan
)
1404 && bex
->fe_group
== e4b
->bd_group
) {
1405 /* recheck chunk's availability - we don't know
1406 * when it was found (within this lock-unlock
1408 max
= mb_find_extent(e4b
, 0, bex
->fe_start
, gex
->fe_len
, &ex
);
1409 if (max
>= gex
->fe_len
) {
1410 ext4_mb_use_best_found(ac
, e4b
);
1417 * The routine checks whether found extent is good enough. If it is,
1418 * then the extent gets marked used and flag is set to the context
1419 * to stop scanning. Otherwise, the extent is compared with the
1420 * previous found extent and if new one is better, then it's stored
1421 * in the context. Later, the best found extent will be used, if
1422 * mballoc can't find good enough extent.
1424 * FIXME: real allocation policy is to be designed yet!
1426 static void ext4_mb_measure_extent(struct ext4_allocation_context
*ac
,
1427 struct ext4_free_extent
*ex
,
1428 struct ext4_buddy
*e4b
)
1430 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1431 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1433 BUG_ON(ex
->fe_len
<= 0);
1434 BUG_ON(ex
->fe_len
> EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
1435 BUG_ON(ex
->fe_start
>= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
1436 BUG_ON(ac
->ac_status
!= AC_STATUS_CONTINUE
);
1441 * The special case - take what you catch first
1443 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1445 ext4_mb_use_best_found(ac
, e4b
);
1450 * Let's check whether the chuck is good enough
1452 if (ex
->fe_len
== gex
->fe_len
) {
1454 ext4_mb_use_best_found(ac
, e4b
);
1459 * If this is first found extent, just store it in the context
1461 if (bex
->fe_len
== 0) {
1467 * If new found extent is better, store it in the context
1469 if (bex
->fe_len
< gex
->fe_len
) {
1470 /* if the request isn't satisfied, any found extent
1471 * larger than previous best one is better */
1472 if (ex
->fe_len
> bex
->fe_len
)
1474 } else if (ex
->fe_len
> gex
->fe_len
) {
1475 /* if the request is satisfied, then we try to find
1476 * an extent that still satisfy the request, but is
1477 * smaller than previous one */
1478 if (ex
->fe_len
< bex
->fe_len
)
1482 ext4_mb_check_limits(ac
, e4b
, 0);
1485 static noinline_for_stack
1486 int ext4_mb_try_best_found(struct ext4_allocation_context
*ac
,
1487 struct ext4_buddy
*e4b
)
1489 struct ext4_free_extent ex
= ac
->ac_b_ex
;
1490 ext4_group_t group
= ex
.fe_group
;
1494 BUG_ON(ex
.fe_len
<= 0);
1495 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1499 ext4_lock_group(ac
->ac_sb
, group
);
1500 max
= mb_find_extent(e4b
, 0, ex
.fe_start
, ex
.fe_len
, &ex
);
1504 ext4_mb_use_best_found(ac
, e4b
);
1507 ext4_unlock_group(ac
->ac_sb
, group
);
1508 ext4_mb_release_desc(e4b
);
1513 static noinline_for_stack
1514 int ext4_mb_find_by_goal(struct ext4_allocation_context
*ac
,
1515 struct ext4_buddy
*e4b
)
1517 ext4_group_t group
= ac
->ac_g_ex
.fe_group
;
1520 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1521 struct ext4_super_block
*es
= sbi
->s_es
;
1522 struct ext4_free_extent ex
;
1524 if (!(ac
->ac_flags
& EXT4_MB_HINT_TRY_GOAL
))
1527 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1531 ext4_lock_group(ac
->ac_sb
, group
);
1532 max
= mb_find_extent(e4b
, 0, ac
->ac_g_ex
.fe_start
,
1533 ac
->ac_g_ex
.fe_len
, &ex
);
1535 if (max
>= ac
->ac_g_ex
.fe_len
&& ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
) {
1538 start
= (e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
)) +
1539 ex
.fe_start
+ le32_to_cpu(es
->s_first_data_block
);
1540 /* use do_div to get remainder (would be 64-bit modulo) */
1541 if (do_div(start
, sbi
->s_stripe
) == 0) {
1544 ext4_mb_use_best_found(ac
, e4b
);
1546 } else if (max
>= ac
->ac_g_ex
.fe_len
) {
1547 BUG_ON(ex
.fe_len
<= 0);
1548 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1549 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1552 ext4_mb_use_best_found(ac
, e4b
);
1553 } else if (max
> 0 && (ac
->ac_flags
& EXT4_MB_HINT_MERGE
)) {
1554 /* Sometimes, caller may want to merge even small
1555 * number of blocks to an existing extent */
1556 BUG_ON(ex
.fe_len
<= 0);
1557 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1558 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1561 ext4_mb_use_best_found(ac
, e4b
);
1563 ext4_unlock_group(ac
->ac_sb
, group
);
1564 ext4_mb_release_desc(e4b
);
1570 * The routine scans buddy structures (not bitmap!) from given order
1571 * to max order and tries to find big enough chunk to satisfy the req
1573 static noinline_for_stack
1574 void ext4_mb_simple_scan_group(struct ext4_allocation_context
*ac
,
1575 struct ext4_buddy
*e4b
)
1577 struct super_block
*sb
= ac
->ac_sb
;
1578 struct ext4_group_info
*grp
= e4b
->bd_info
;
1584 BUG_ON(ac
->ac_2order
<= 0);
1585 for (i
= ac
->ac_2order
; i
<= sb
->s_blocksize_bits
+ 1; i
++) {
1586 if (grp
->bb_counters
[i
] == 0)
1589 buddy
= mb_find_buddy(e4b
, i
, &max
);
1590 BUG_ON(buddy
== NULL
);
1592 k
= mb_find_next_zero_bit(buddy
, max
, 0);
1597 ac
->ac_b_ex
.fe_len
= 1 << i
;
1598 ac
->ac_b_ex
.fe_start
= k
<< i
;
1599 ac
->ac_b_ex
.fe_group
= e4b
->bd_group
;
1601 ext4_mb_use_best_found(ac
, e4b
);
1603 BUG_ON(ac
->ac_b_ex
.fe_len
!= ac
->ac_g_ex
.fe_len
);
1605 if (EXT4_SB(sb
)->s_mb_stats
)
1606 atomic_inc(&EXT4_SB(sb
)->s_bal_2orders
);
1613 * The routine scans the group and measures all found extents.
1614 * In order to optimize scanning, caller must pass number of
1615 * free blocks in the group, so the routine can know upper limit.
1617 static noinline_for_stack
1618 void ext4_mb_complex_scan_group(struct ext4_allocation_context
*ac
,
1619 struct ext4_buddy
*e4b
)
1621 struct super_block
*sb
= ac
->ac_sb
;
1622 void *bitmap
= EXT4_MB_BITMAP(e4b
);
1623 struct ext4_free_extent ex
;
1627 free
= e4b
->bd_info
->bb_free
;
1630 i
= e4b
->bd_info
->bb_first_free
;
1632 while (free
&& ac
->ac_status
== AC_STATUS_CONTINUE
) {
1633 i
= mb_find_next_zero_bit(bitmap
,
1634 EXT4_BLOCKS_PER_GROUP(sb
), i
);
1635 if (i
>= EXT4_BLOCKS_PER_GROUP(sb
)) {
1637 * IF we have corrupt bitmap, we won't find any
1638 * free blocks even though group info says we
1639 * we have free blocks
1641 ext4_grp_locked_error(sb
, e4b
->bd_group
,
1642 __func__
, "%d free blocks as per "
1643 "group info. But bitmap says 0",
1648 mb_find_extent(e4b
, 0, i
, ac
->ac_g_ex
.fe_len
, &ex
);
1649 BUG_ON(ex
.fe_len
<= 0);
1650 if (free
< ex
.fe_len
) {
1651 ext4_grp_locked_error(sb
, e4b
->bd_group
,
1652 __func__
, "%d free blocks as per "
1653 "group info. But got %d blocks",
1656 * The number of free blocks differs. This mostly
1657 * indicate that the bitmap is corrupt. So exit
1658 * without claiming the space.
1663 ext4_mb_measure_extent(ac
, &ex
, e4b
);
1669 ext4_mb_check_limits(ac
, e4b
, 1);
1673 * This is a special case for storages like raid5
1674 * we try to find stripe-aligned chunks for stripe-size requests
1675 * XXX should do so at least for multiples of stripe size as well
1677 static noinline_for_stack
1678 void ext4_mb_scan_aligned(struct ext4_allocation_context
*ac
,
1679 struct ext4_buddy
*e4b
)
1681 struct super_block
*sb
= ac
->ac_sb
;
1682 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1683 void *bitmap
= EXT4_MB_BITMAP(e4b
);
1684 struct ext4_free_extent ex
;
1685 ext4_fsblk_t first_group_block
;
1690 BUG_ON(sbi
->s_stripe
== 0);
1692 /* find first stripe-aligned block in group */
1693 first_group_block
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
)
1694 + le32_to_cpu(sbi
->s_es
->s_first_data_block
);
1695 a
= first_group_block
+ sbi
->s_stripe
- 1;
1696 do_div(a
, sbi
->s_stripe
);
1697 i
= (a
* sbi
->s_stripe
) - first_group_block
;
1699 while (i
< EXT4_BLOCKS_PER_GROUP(sb
)) {
1700 if (!mb_test_bit(i
, bitmap
)) {
1701 max
= mb_find_extent(e4b
, 0, i
, sbi
->s_stripe
, &ex
);
1702 if (max
>= sbi
->s_stripe
) {
1705 ext4_mb_use_best_found(ac
, e4b
);
1713 static int ext4_mb_good_group(struct ext4_allocation_context
*ac
,
1714 ext4_group_t group
, int cr
)
1716 unsigned free
, fragments
;
1718 int flex_size
= ext4_flex_bg_size(EXT4_SB(ac
->ac_sb
));
1719 struct ext4_group_info
*grp
= ext4_get_group_info(ac
->ac_sb
, group
);
1721 BUG_ON(cr
< 0 || cr
>= 4);
1722 BUG_ON(EXT4_MB_GRP_NEED_INIT(grp
));
1724 free
= grp
->bb_free
;
1725 fragments
= grp
->bb_fragments
;
1733 BUG_ON(ac
->ac_2order
== 0);
1735 /* Avoid using the first bg of a flexgroup for data files */
1736 if ((ac
->ac_flags
& EXT4_MB_HINT_DATA
) &&
1737 (flex_size
>= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
) &&
1738 ((group
% flex_size
) == 0))
1741 bits
= ac
->ac_sb
->s_blocksize_bits
+ 1;
1742 for (i
= ac
->ac_2order
; i
<= bits
; i
++)
1743 if (grp
->bb_counters
[i
] > 0)
1747 if ((free
/ fragments
) >= ac
->ac_g_ex
.fe_len
)
1751 if (free
>= ac
->ac_g_ex
.fe_len
)
1764 * lock the group_info alloc_sem of all the groups
1765 * belonging to the same buddy cache page. This
1766 * make sure other parallel operation on the buddy
1767 * cache doesn't happen whild holding the buddy cache
1770 int ext4_mb_get_buddy_cache_lock(struct super_block
*sb
, ext4_group_t group
)
1774 int blocks_per_page
;
1775 int groups_per_page
;
1776 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
1777 ext4_group_t first_group
;
1778 struct ext4_group_info
*grp
;
1780 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1782 * the buddy cache inode stores the block bitmap
1783 * and buddy information in consecutive blocks.
1784 * So for each group we need two blocks.
1787 pnum
= block
/ blocks_per_page
;
1788 first_group
= pnum
* blocks_per_page
/ 2;
1790 groups_per_page
= blocks_per_page
>> 1;
1791 if (groups_per_page
== 0)
1792 groups_per_page
= 1;
1793 /* read all groups the page covers into the cache */
1794 for (i
= 0; i
< groups_per_page
; i
++) {
1796 if ((first_group
+ i
) >= ngroups
)
1798 grp
= ext4_get_group_info(sb
, first_group
+ i
);
1799 /* take all groups write allocation
1800 * semaphore. This make sure there is
1801 * no block allocation going on in any
1804 down_write_nested(&grp
->alloc_sem
, i
);
1809 void ext4_mb_put_buddy_cache_lock(struct super_block
*sb
,
1810 ext4_group_t group
, int locked_group
)
1814 int blocks_per_page
;
1815 ext4_group_t first_group
;
1816 struct ext4_group_info
*grp
;
1818 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1820 * the buddy cache inode stores the block bitmap
1821 * and buddy information in consecutive blocks.
1822 * So for each group we need two blocks.
1825 pnum
= block
/ blocks_per_page
;
1826 first_group
= pnum
* blocks_per_page
/ 2;
1827 /* release locks on all the groups */
1828 for (i
= 0; i
< locked_group
; i
++) {
1830 grp
= ext4_get_group_info(sb
, first_group
+ i
);
1831 /* take all groups write allocation
1832 * semaphore. This make sure there is
1833 * no block allocation going on in any
1836 up_write(&grp
->alloc_sem
);
1841 static noinline_for_stack
1842 int ext4_mb_init_group(struct super_block
*sb
, ext4_group_t group
)
1847 int blocks_per_page
;
1848 int block
, pnum
, poff
;
1849 int num_grp_locked
= 0;
1850 struct ext4_group_info
*this_grp
;
1851 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1852 struct inode
*inode
= sbi
->s_buddy_cache
;
1853 struct page
*page
= NULL
, *bitmap_page
= NULL
;
1855 mb_debug(1, "init group %u\n", group
);
1856 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1857 this_grp
= ext4_get_group_info(sb
, group
);
1859 * This ensures we don't add group
1860 * to this buddy cache via resize
1862 num_grp_locked
= ext4_mb_get_buddy_cache_lock(sb
, group
);
1863 if (!EXT4_MB_GRP_NEED_INIT(this_grp
)) {
1865 * somebody initialized the group
1866 * return without doing anything
1872 * the buddy cache inode stores the block bitmap
1873 * and buddy information in consecutive blocks.
1874 * So for each group we need two blocks.
1877 pnum
= block
/ blocks_per_page
;
1878 poff
= block
% blocks_per_page
;
1879 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1881 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1882 ret
= ext4_mb_init_cache(page
, NULL
);
1889 if (page
== NULL
|| !PageUptodate(page
)) {
1893 mark_page_accessed(page
);
1895 bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
1897 /* init buddy cache */
1899 pnum
= block
/ blocks_per_page
;
1900 poff
= block
% blocks_per_page
;
1901 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1902 if (page
== bitmap_page
) {
1904 * If both the bitmap and buddy are in
1905 * the same page we don't need to force
1910 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1911 ret
= ext4_mb_init_cache(page
, bitmap
);
1918 if (page
== NULL
|| !PageUptodate(page
)) {
1922 mark_page_accessed(page
);
1924 ext4_mb_put_buddy_cache_lock(sb
, group
, num_grp_locked
);
1926 page_cache_release(bitmap_page
);
1928 page_cache_release(page
);
1932 static noinline_for_stack
int
1933 ext4_mb_regular_allocator(struct ext4_allocation_context
*ac
)
1935 ext4_group_t ngroups
, group
, i
;
1939 struct ext4_sb_info
*sbi
;
1940 struct super_block
*sb
;
1941 struct ext4_buddy e4b
;
1945 ngroups
= ext4_get_groups_count(sb
);
1946 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1948 /* first, try the goal */
1949 err
= ext4_mb_find_by_goal(ac
, &e4b
);
1950 if (err
|| ac
->ac_status
== AC_STATUS_FOUND
)
1953 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
1957 * ac->ac2_order is set only if the fe_len is a power of 2
1958 * if ac2_order is set we also set criteria to 0 so that we
1959 * try exact allocation using buddy.
1961 i
= fls(ac
->ac_g_ex
.fe_len
);
1964 * We search using buddy data only if the order of the request
1965 * is greater than equal to the sbi_s_mb_order2_reqs
1966 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
1968 if (i
>= sbi
->s_mb_order2_reqs
) {
1970 * This should tell if fe_len is exactly power of 2
1972 if ((ac
->ac_g_ex
.fe_len
& (~(1 << (i
- 1)))) == 0)
1973 ac
->ac_2order
= i
- 1;
1976 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
1978 /* if stream allocation is enabled, use global goal */
1979 if (ac
->ac_flags
& EXT4_MB_STREAM_ALLOC
) {
1980 /* TBD: may be hot point */
1981 spin_lock(&sbi
->s_md_lock
);
1982 ac
->ac_g_ex
.fe_group
= sbi
->s_mb_last_group
;
1983 ac
->ac_g_ex
.fe_start
= sbi
->s_mb_last_start
;
1984 spin_unlock(&sbi
->s_md_lock
);
1987 /* Let's just scan groups to find more-less suitable blocks */
1988 cr
= ac
->ac_2order
? 0 : 1;
1990 * cr == 0 try to get exact allocation,
1991 * cr == 3 try to get anything
1994 for (; cr
< 4 && ac
->ac_status
== AC_STATUS_CONTINUE
; cr
++) {
1995 ac
->ac_criteria
= cr
;
1997 * searching for the right group start
1998 * from the goal value specified
2000 group
= ac
->ac_g_ex
.fe_group
;
2002 for (i
= 0; i
< ngroups
; group
++, i
++) {
2003 struct ext4_group_info
*grp
;
2004 struct ext4_group_desc
*desc
;
2006 if (group
== ngroups
)
2009 /* quick check to skip empty groups */
2010 grp
= ext4_get_group_info(sb
, group
);
2011 if (grp
->bb_free
== 0)
2015 * if the group is already init we check whether it is
2016 * a good group and if not we don't load the buddy
2018 if (EXT4_MB_GRP_NEED_INIT(grp
)) {
2020 * we need full data about the group
2021 * to make a good selection
2023 err
= ext4_mb_init_group(sb
, group
);
2029 * If the particular group doesn't satisfy our
2030 * criteria we continue with the next group
2032 if (!ext4_mb_good_group(ac
, group
, cr
))
2035 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2039 ext4_lock_group(sb
, group
);
2040 if (!ext4_mb_good_group(ac
, group
, cr
)) {
2041 /* someone did allocation from this group */
2042 ext4_unlock_group(sb
, group
);
2043 ext4_mb_release_desc(&e4b
);
2047 ac
->ac_groups_scanned
++;
2048 desc
= ext4_get_group_desc(sb
, group
, NULL
);
2050 ext4_mb_simple_scan_group(ac
, &e4b
);
2052 ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
)
2053 ext4_mb_scan_aligned(ac
, &e4b
);
2055 ext4_mb_complex_scan_group(ac
, &e4b
);
2057 ext4_unlock_group(sb
, group
);
2058 ext4_mb_release_desc(&e4b
);
2060 if (ac
->ac_status
!= AC_STATUS_CONTINUE
)
2065 if (ac
->ac_b_ex
.fe_len
> 0 && ac
->ac_status
!= AC_STATUS_FOUND
&&
2066 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
2068 * We've been searching too long. Let's try to allocate
2069 * the best chunk we've found so far
2072 ext4_mb_try_best_found(ac
, &e4b
);
2073 if (ac
->ac_status
!= AC_STATUS_FOUND
) {
2075 * Someone more lucky has already allocated it.
2076 * The only thing we can do is just take first
2078 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2080 ac
->ac_b_ex
.fe_group
= 0;
2081 ac
->ac_b_ex
.fe_start
= 0;
2082 ac
->ac_b_ex
.fe_len
= 0;
2083 ac
->ac_status
= AC_STATUS_CONTINUE
;
2084 ac
->ac_flags
|= EXT4_MB_HINT_FIRST
;
2086 atomic_inc(&sbi
->s_mb_lost_chunks
);
2094 #ifdef EXT4_MB_HISTORY
2095 struct ext4_mb_proc_session
{
2096 struct ext4_mb_history
*history
;
2097 struct super_block
*sb
;
2102 static void *ext4_mb_history_skip_empty(struct ext4_mb_proc_session
*s
,
2103 struct ext4_mb_history
*hs
,
2106 if (hs
== s
->history
+ s
->max
)
2108 if (!first
&& hs
== s
->history
+ s
->start
)
2110 while (hs
->orig
.fe_len
== 0) {
2112 if (hs
== s
->history
+ s
->max
)
2114 if (hs
== s
->history
+ s
->start
)
2120 static void *ext4_mb_seq_history_start(struct seq_file
*seq
, loff_t
*pos
)
2122 struct ext4_mb_proc_session
*s
= seq
->private;
2123 struct ext4_mb_history
*hs
;
2127 return SEQ_START_TOKEN
;
2128 hs
= ext4_mb_history_skip_empty(s
, s
->history
+ s
->start
, 1);
2131 while (--l
&& (hs
= ext4_mb_history_skip_empty(s
, ++hs
, 0)) != NULL
);
2135 static void *ext4_mb_seq_history_next(struct seq_file
*seq
, void *v
,
2138 struct ext4_mb_proc_session
*s
= seq
->private;
2139 struct ext4_mb_history
*hs
= v
;
2142 if (v
== SEQ_START_TOKEN
)
2143 return ext4_mb_history_skip_empty(s
, s
->history
+ s
->start
, 1);
2145 return ext4_mb_history_skip_empty(s
, ++hs
, 0);
2148 static int ext4_mb_seq_history_show(struct seq_file
*seq
, void *v
)
2150 char buf
[25], buf2
[25], buf3
[25], *fmt
;
2151 struct ext4_mb_history
*hs
= v
;
2153 if (v
== SEQ_START_TOKEN
) {
2154 seq_printf(seq
, "%-5s %-8s %-23s %-23s %-23s %-5s "
2155 "%-5s %-2s %-6s %-5s %-5s %-6s\n",
2156 "pid", "inode", "original", "goal", "result", "found",
2157 "grps", "cr", "flags", "merge", "tail", "broken");
2161 if (hs
->op
== EXT4_MB_HISTORY_ALLOC
) {
2162 fmt
= "%-5u %-8u %-23s %-23s %-23s %-5u %-5u %-2u "
2163 "0x%04x %-5s %-5u %-6u\n";
2164 sprintf(buf2
, "%u/%d/%u@%u", hs
->result
.fe_group
,
2165 hs
->result
.fe_start
, hs
->result
.fe_len
,
2166 hs
->result
.fe_logical
);
2167 sprintf(buf
, "%u/%d/%u@%u", hs
->orig
.fe_group
,
2168 hs
->orig
.fe_start
, hs
->orig
.fe_len
,
2169 hs
->orig
.fe_logical
);
2170 sprintf(buf3
, "%u/%d/%u@%u", hs
->goal
.fe_group
,
2171 hs
->goal
.fe_start
, hs
->goal
.fe_len
,
2172 hs
->goal
.fe_logical
);
2173 seq_printf(seq
, fmt
, hs
->pid
, hs
->ino
, buf
, buf3
, buf2
,
2174 hs
->found
, hs
->groups
, hs
->cr
, hs
->flags
,
2175 hs
->merged
? "M" : "", hs
->tail
,
2176 hs
->buddy
? 1 << hs
->buddy
: 0);
2177 } else if (hs
->op
== EXT4_MB_HISTORY_PREALLOC
) {
2178 fmt
= "%-5u %-8u %-23s %-23s %-23s\n";
2179 sprintf(buf2
, "%u/%d/%u@%u", hs
->result
.fe_group
,
2180 hs
->result
.fe_start
, hs
->result
.fe_len
,
2181 hs
->result
.fe_logical
);
2182 sprintf(buf
, "%u/%d/%u@%u", hs
->orig
.fe_group
,
2183 hs
->orig
.fe_start
, hs
->orig
.fe_len
,
2184 hs
->orig
.fe_logical
);
2185 seq_printf(seq
, fmt
, hs
->pid
, hs
->ino
, buf
, "", buf2
);
2186 } else if (hs
->op
== EXT4_MB_HISTORY_DISCARD
) {
2187 sprintf(buf2
, "%u/%d/%u", hs
->result
.fe_group
,
2188 hs
->result
.fe_start
, hs
->result
.fe_len
);
2189 seq_printf(seq
, "%-5u %-8u %-23s discard\n",
2190 hs
->pid
, hs
->ino
, buf2
);
2191 } else if (hs
->op
== EXT4_MB_HISTORY_FREE
) {
2192 sprintf(buf2
, "%u/%d/%u", hs
->result
.fe_group
,
2193 hs
->result
.fe_start
, hs
->result
.fe_len
);
2194 seq_printf(seq
, "%-5u %-8u %-23s free\n",
2195 hs
->pid
, hs
->ino
, buf2
);
2200 static void ext4_mb_seq_history_stop(struct seq_file
*seq
, void *v
)
2204 static struct seq_operations ext4_mb_seq_history_ops
= {
2205 .start
= ext4_mb_seq_history_start
,
2206 .next
= ext4_mb_seq_history_next
,
2207 .stop
= ext4_mb_seq_history_stop
,
2208 .show
= ext4_mb_seq_history_show
,
2211 static int ext4_mb_seq_history_open(struct inode
*inode
, struct file
*file
)
2213 struct super_block
*sb
= PDE(inode
)->data
;
2214 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2215 struct ext4_mb_proc_session
*s
;
2219 if (unlikely(sbi
->s_mb_history
== NULL
))
2221 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
2225 size
= sizeof(struct ext4_mb_history
) * sbi
->s_mb_history_max
;
2226 s
->history
= kmalloc(size
, GFP_KERNEL
);
2227 if (s
->history
== NULL
) {
2232 spin_lock(&sbi
->s_mb_history_lock
);
2233 memcpy(s
->history
, sbi
->s_mb_history
, size
);
2234 s
->max
= sbi
->s_mb_history_max
;
2235 s
->start
= sbi
->s_mb_history_cur
% s
->max
;
2236 spin_unlock(&sbi
->s_mb_history_lock
);
2238 rc
= seq_open(file
, &ext4_mb_seq_history_ops
);
2240 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2250 static int ext4_mb_seq_history_release(struct inode
*inode
, struct file
*file
)
2252 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
2253 struct ext4_mb_proc_session
*s
= seq
->private;
2256 return seq_release(inode
, file
);
2259 static ssize_t
ext4_mb_seq_history_write(struct file
*file
,
2260 const char __user
*buffer
,
2261 size_t count
, loff_t
*ppos
)
2263 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
2264 struct ext4_mb_proc_session
*s
= seq
->private;
2265 struct super_block
*sb
= s
->sb
;
2269 if (count
>= sizeof(str
)) {
2270 printk(KERN_ERR
"EXT4-fs: %s string too long, max %u bytes\n",
2271 "mb_history", (int)sizeof(str
));
2275 if (copy_from_user(str
, buffer
, count
))
2278 value
= simple_strtol(str
, NULL
, 0);
2281 EXT4_SB(sb
)->s_mb_history_filter
= value
;
2286 static struct file_operations ext4_mb_seq_history_fops
= {
2287 .owner
= THIS_MODULE
,
2288 .open
= ext4_mb_seq_history_open
,
2290 .write
= ext4_mb_seq_history_write
,
2291 .llseek
= seq_lseek
,
2292 .release
= ext4_mb_seq_history_release
,
2295 static void *ext4_mb_seq_groups_start(struct seq_file
*seq
, loff_t
*pos
)
2297 struct super_block
*sb
= seq
->private;
2300 if (*pos
< 0 || *pos
>= ext4_get_groups_count(sb
))
2303 return (void *) ((unsigned long) group
);
2306 static void *ext4_mb_seq_groups_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2308 struct super_block
*sb
= seq
->private;
2312 if (*pos
< 0 || *pos
>= ext4_get_groups_count(sb
))
2315 return (void *) ((unsigned long) group
);
2318 static int ext4_mb_seq_groups_show(struct seq_file
*seq
, void *v
)
2320 struct super_block
*sb
= seq
->private;
2321 ext4_group_t group
= (ext4_group_t
) ((unsigned long) v
);
2324 struct ext4_buddy e4b
;
2326 struct ext4_group_info info
;
2327 unsigned short counters
[16];
2332 seq_printf(seq
, "#%-5s: %-5s %-5s %-5s "
2333 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2334 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2335 "group", "free", "frags", "first",
2336 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2337 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2339 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(sg
.info
.bb_counters
[0]) +
2340 sizeof(struct ext4_group_info
);
2341 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2343 seq_printf(seq
, "#%-5u: I/O error\n", group
);
2346 ext4_lock_group(sb
, group
);
2347 memcpy(&sg
, ext4_get_group_info(sb
, group
), i
);
2348 ext4_unlock_group(sb
, group
);
2349 ext4_mb_release_desc(&e4b
);
2351 seq_printf(seq
, "#%-5u: %-5u %-5u %-5u [", group
, sg
.info
.bb_free
,
2352 sg
.info
.bb_fragments
, sg
.info
.bb_first_free
);
2353 for (i
= 0; i
<= 13; i
++)
2354 seq_printf(seq
, " %-5u", i
<= sb
->s_blocksize_bits
+ 1 ?
2355 sg
.info
.bb_counters
[i
] : 0);
2356 seq_printf(seq
, " ]\n");
2361 static void ext4_mb_seq_groups_stop(struct seq_file
*seq
, void *v
)
2365 static struct seq_operations ext4_mb_seq_groups_ops
= {
2366 .start
= ext4_mb_seq_groups_start
,
2367 .next
= ext4_mb_seq_groups_next
,
2368 .stop
= ext4_mb_seq_groups_stop
,
2369 .show
= ext4_mb_seq_groups_show
,
2372 static int ext4_mb_seq_groups_open(struct inode
*inode
, struct file
*file
)
2374 struct super_block
*sb
= PDE(inode
)->data
;
2377 rc
= seq_open(file
, &ext4_mb_seq_groups_ops
);
2379 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2386 static struct file_operations ext4_mb_seq_groups_fops
= {
2387 .owner
= THIS_MODULE
,
2388 .open
= ext4_mb_seq_groups_open
,
2390 .llseek
= seq_lseek
,
2391 .release
= seq_release
,
2394 static void ext4_mb_history_release(struct super_block
*sb
)
2396 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2398 if (sbi
->s_proc
!= NULL
) {
2399 remove_proc_entry("mb_groups", sbi
->s_proc
);
2400 if (sbi
->s_mb_history_max
)
2401 remove_proc_entry("mb_history", sbi
->s_proc
);
2403 kfree(sbi
->s_mb_history
);
2406 static void ext4_mb_history_init(struct super_block
*sb
)
2408 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2411 if (sbi
->s_proc
!= NULL
) {
2412 if (sbi
->s_mb_history_max
)
2413 proc_create_data("mb_history", S_IRUGO
, sbi
->s_proc
,
2414 &ext4_mb_seq_history_fops
, sb
);
2415 proc_create_data("mb_groups", S_IRUGO
, sbi
->s_proc
,
2416 &ext4_mb_seq_groups_fops
, sb
);
2419 sbi
->s_mb_history_cur
= 0;
2420 spin_lock_init(&sbi
->s_mb_history_lock
);
2421 i
= sbi
->s_mb_history_max
* sizeof(struct ext4_mb_history
);
2422 sbi
->s_mb_history
= i
? kzalloc(i
, GFP_KERNEL
) : NULL
;
2423 /* if we can't allocate history, then we simple won't use it */
2426 static noinline_for_stack
void
2427 ext4_mb_store_history(struct ext4_allocation_context
*ac
)
2429 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
2430 struct ext4_mb_history h
;
2432 if (sbi
->s_mb_history
== NULL
)
2435 if (!(ac
->ac_op
& sbi
->s_mb_history_filter
))
2439 h
.pid
= current
->pid
;
2440 h
.ino
= ac
->ac_inode
? ac
->ac_inode
->i_ino
: 0;
2441 h
.orig
= ac
->ac_o_ex
;
2442 h
.result
= ac
->ac_b_ex
;
2443 h
.flags
= ac
->ac_flags
;
2444 h
.found
= ac
->ac_found
;
2445 h
.groups
= ac
->ac_groups_scanned
;
2446 h
.cr
= ac
->ac_criteria
;
2447 h
.tail
= ac
->ac_tail
;
2448 h
.buddy
= ac
->ac_buddy
;
2450 if (ac
->ac_op
== EXT4_MB_HISTORY_ALLOC
) {
2451 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
2452 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
2454 h
.goal
= ac
->ac_g_ex
;
2455 h
.result
= ac
->ac_f_ex
;
2458 spin_lock(&sbi
->s_mb_history_lock
);
2459 memcpy(sbi
->s_mb_history
+ sbi
->s_mb_history_cur
, &h
, sizeof(h
));
2460 if (++sbi
->s_mb_history_cur
>= sbi
->s_mb_history_max
)
2461 sbi
->s_mb_history_cur
= 0;
2462 spin_unlock(&sbi
->s_mb_history_lock
);
2466 #define ext4_mb_history_release(sb)
2467 #define ext4_mb_history_init(sb)
2471 /* Create and initialize ext4_group_info data for the given group. */
2472 int ext4_mb_add_groupinfo(struct super_block
*sb
, ext4_group_t group
,
2473 struct ext4_group_desc
*desc
)
2477 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2478 struct ext4_group_info
**meta_group_info
;
2481 * First check if this group is the first of a reserved block.
2482 * If it's true, we have to allocate a new table of pointers
2483 * to ext4_group_info structures
2485 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0) {
2486 metalen
= sizeof(*meta_group_info
) <<
2487 EXT4_DESC_PER_BLOCK_BITS(sb
);
2488 meta_group_info
= kmalloc(metalen
, GFP_KERNEL
);
2489 if (meta_group_info
== NULL
) {
2490 printk(KERN_ERR
"EXT4-fs: can't allocate mem for a "
2492 goto exit_meta_group_info
;
2494 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)] =
2499 * calculate needed size. if change bb_counters size,
2500 * don't forget about ext4_mb_generate_buddy()
2502 len
= offsetof(typeof(**meta_group_info
),
2503 bb_counters
[sb
->s_blocksize_bits
+ 2]);
2506 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)];
2507 i
= group
& (EXT4_DESC_PER_BLOCK(sb
) - 1);
2509 meta_group_info
[i
] = kzalloc(len
, GFP_KERNEL
);
2510 if (meta_group_info
[i
] == NULL
) {
2511 printk(KERN_ERR
"EXT4-fs: can't allocate buddy mem\n");
2512 goto exit_group_info
;
2514 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
,
2515 &(meta_group_info
[i
]->bb_state
));
2518 * initialize bb_free to be able to skip
2519 * empty groups without initialization
2521 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2522 meta_group_info
[i
]->bb_free
=
2523 ext4_free_blocks_after_init(sb
, group
, desc
);
2525 meta_group_info
[i
]->bb_free
=
2526 ext4_free_blks_count(sb
, desc
);
2529 INIT_LIST_HEAD(&meta_group_info
[i
]->bb_prealloc_list
);
2530 init_rwsem(&meta_group_info
[i
]->alloc_sem
);
2531 meta_group_info
[i
]->bb_free_root
.rb_node
= NULL
;
2535 struct buffer_head
*bh
;
2536 meta_group_info
[i
]->bb_bitmap
=
2537 kmalloc(sb
->s_blocksize
, GFP_KERNEL
);
2538 BUG_ON(meta_group_info
[i
]->bb_bitmap
== NULL
);
2539 bh
= ext4_read_block_bitmap(sb
, group
);
2541 memcpy(meta_group_info
[i
]->bb_bitmap
, bh
->b_data
,
2550 /* If a meta_group_info table has been allocated, release it now */
2551 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0)
2552 kfree(sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)]);
2553 exit_meta_group_info
:
2555 } /* ext4_mb_add_groupinfo */
2557 static int ext4_mb_init_backend(struct super_block
*sb
)
2559 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
2561 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2562 struct ext4_super_block
*es
= sbi
->s_es
;
2563 int num_meta_group_infos
;
2564 int num_meta_group_infos_max
;
2566 struct ext4_group_desc
*desc
;
2568 /* This is the number of blocks used by GDT */
2569 num_meta_group_infos
= (ngroups
+ EXT4_DESC_PER_BLOCK(sb
) -
2570 1) >> EXT4_DESC_PER_BLOCK_BITS(sb
);
2573 * This is the total number of blocks used by GDT including
2574 * the number of reserved blocks for GDT.
2575 * The s_group_info array is allocated with this value
2576 * to allow a clean online resize without a complex
2577 * manipulation of pointer.
2578 * The drawback is the unused memory when no resize
2579 * occurs but it's very low in terms of pages
2580 * (see comments below)
2581 * Need to handle this properly when META_BG resizing is allowed
2583 num_meta_group_infos_max
= num_meta_group_infos
+
2584 le16_to_cpu(es
->s_reserved_gdt_blocks
);
2587 * array_size is the size of s_group_info array. We round it
2588 * to the next power of two because this approximation is done
2589 * internally by kmalloc so we can have some more memory
2590 * for free here (e.g. may be used for META_BG resize).
2593 while (array_size
< sizeof(*sbi
->s_group_info
) *
2594 num_meta_group_infos_max
)
2595 array_size
= array_size
<< 1;
2596 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2597 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2598 * So a two level scheme suffices for now. */
2599 sbi
->s_group_info
= kmalloc(array_size
, GFP_KERNEL
);
2600 if (sbi
->s_group_info
== NULL
) {
2601 printk(KERN_ERR
"EXT4-fs: can't allocate buddy meta group\n");
2604 sbi
->s_buddy_cache
= new_inode(sb
);
2605 if (sbi
->s_buddy_cache
== NULL
) {
2606 printk(KERN_ERR
"EXT4-fs: can't get new inode\n");
2609 EXT4_I(sbi
->s_buddy_cache
)->i_disksize
= 0;
2610 for (i
= 0; i
< ngroups
; i
++) {
2611 desc
= ext4_get_group_desc(sb
, i
, NULL
);
2614 "EXT4-fs: can't read descriptor %u\n", i
);
2617 if (ext4_mb_add_groupinfo(sb
, i
, desc
) != 0)
2625 kfree(ext4_get_group_info(sb
, i
));
2626 i
= num_meta_group_infos
;
2628 kfree(sbi
->s_group_info
[i
]);
2629 iput(sbi
->s_buddy_cache
);
2631 kfree(sbi
->s_group_info
);
2635 int ext4_mb_init(struct super_block
*sb
, int needs_recovery
)
2637 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2643 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(unsigned short);
2645 sbi
->s_mb_offsets
= kmalloc(i
, GFP_KERNEL
);
2646 if (sbi
->s_mb_offsets
== NULL
) {
2650 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(unsigned int);
2651 sbi
->s_mb_maxs
= kmalloc(i
, GFP_KERNEL
);
2652 if (sbi
->s_mb_maxs
== NULL
) {
2653 kfree(sbi
->s_mb_offsets
);
2657 /* order 0 is regular bitmap */
2658 sbi
->s_mb_maxs
[0] = sb
->s_blocksize
<< 3;
2659 sbi
->s_mb_offsets
[0] = 0;
2663 max
= sb
->s_blocksize
<< 2;
2665 sbi
->s_mb_offsets
[i
] = offset
;
2666 sbi
->s_mb_maxs
[i
] = max
;
2667 offset
+= 1 << (sb
->s_blocksize_bits
- i
);
2670 } while (i
<= sb
->s_blocksize_bits
+ 1);
2672 /* init file for buddy data */
2673 ret
= ext4_mb_init_backend(sb
);
2675 kfree(sbi
->s_mb_offsets
);
2676 kfree(sbi
->s_mb_maxs
);
2680 spin_lock_init(&sbi
->s_md_lock
);
2681 spin_lock_init(&sbi
->s_bal_lock
);
2683 sbi
->s_mb_max_to_scan
= MB_DEFAULT_MAX_TO_SCAN
;
2684 sbi
->s_mb_min_to_scan
= MB_DEFAULT_MIN_TO_SCAN
;
2685 sbi
->s_mb_stats
= MB_DEFAULT_STATS
;
2686 sbi
->s_mb_stream_request
= MB_DEFAULT_STREAM_THRESHOLD
;
2687 sbi
->s_mb_order2_reqs
= MB_DEFAULT_ORDER2_REQS
;
2688 sbi
->s_mb_history_filter
= EXT4_MB_HISTORY_DEFAULT
;
2689 sbi
->s_mb_group_prealloc
= MB_DEFAULT_GROUP_PREALLOC
;
2691 sbi
->s_locality_groups
= alloc_percpu(struct ext4_locality_group
);
2692 if (sbi
->s_locality_groups
== NULL
) {
2693 kfree(sbi
->s_mb_offsets
);
2694 kfree(sbi
->s_mb_maxs
);
2697 for_each_possible_cpu(i
) {
2698 struct ext4_locality_group
*lg
;
2699 lg
= per_cpu_ptr(sbi
->s_locality_groups
, i
);
2700 mutex_init(&lg
->lg_mutex
);
2701 for (j
= 0; j
< PREALLOC_TB_SIZE
; j
++)
2702 INIT_LIST_HEAD(&lg
->lg_prealloc_list
[j
]);
2703 spin_lock_init(&lg
->lg_prealloc_lock
);
2706 ext4_mb_history_init(sb
);
2709 sbi
->s_journal
->j_commit_callback
= release_blocks_on_commit
;
2711 printk(KERN_INFO
"EXT4-fs: mballoc enabled\n");
2715 /* need to called with the ext4 group lock held */
2716 static void ext4_mb_cleanup_pa(struct ext4_group_info
*grp
)
2718 struct ext4_prealloc_space
*pa
;
2719 struct list_head
*cur
, *tmp
;
2722 list_for_each_safe(cur
, tmp
, &grp
->bb_prealloc_list
) {
2723 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
2724 list_del(&pa
->pa_group_list
);
2726 kmem_cache_free(ext4_pspace_cachep
, pa
);
2729 mb_debug(1, "mballoc: %u PAs left\n", count
);
2733 int ext4_mb_release(struct super_block
*sb
)
2735 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
2737 int num_meta_group_infos
;
2738 struct ext4_group_info
*grinfo
;
2739 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2741 if (sbi
->s_group_info
) {
2742 for (i
= 0; i
< ngroups
; i
++) {
2743 grinfo
= ext4_get_group_info(sb
, i
);
2745 kfree(grinfo
->bb_bitmap
);
2747 ext4_lock_group(sb
, i
);
2748 ext4_mb_cleanup_pa(grinfo
);
2749 ext4_unlock_group(sb
, i
);
2752 num_meta_group_infos
= (ngroups
+
2753 EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2754 EXT4_DESC_PER_BLOCK_BITS(sb
);
2755 for (i
= 0; i
< num_meta_group_infos
; i
++)
2756 kfree(sbi
->s_group_info
[i
]);
2757 kfree(sbi
->s_group_info
);
2759 kfree(sbi
->s_mb_offsets
);
2760 kfree(sbi
->s_mb_maxs
);
2761 if (sbi
->s_buddy_cache
)
2762 iput(sbi
->s_buddy_cache
);
2763 if (sbi
->s_mb_stats
) {
2765 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2766 atomic_read(&sbi
->s_bal_allocated
),
2767 atomic_read(&sbi
->s_bal_reqs
),
2768 atomic_read(&sbi
->s_bal_success
));
2770 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2771 "%u 2^N hits, %u breaks, %u lost\n",
2772 atomic_read(&sbi
->s_bal_ex_scanned
),
2773 atomic_read(&sbi
->s_bal_goals
),
2774 atomic_read(&sbi
->s_bal_2orders
),
2775 atomic_read(&sbi
->s_bal_breaks
),
2776 atomic_read(&sbi
->s_mb_lost_chunks
));
2778 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2779 sbi
->s_mb_buddies_generated
++,
2780 sbi
->s_mb_generation_time
);
2782 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2783 atomic_read(&sbi
->s_mb_preallocated
),
2784 atomic_read(&sbi
->s_mb_discarded
));
2787 free_percpu(sbi
->s_locality_groups
);
2788 ext4_mb_history_release(sb
);
2794 * This function is called by the jbd2 layer once the commit has finished,
2795 * so we know we can free the blocks that were released with that commit.
2797 static void release_blocks_on_commit(journal_t
*journal
, transaction_t
*txn
)
2799 struct super_block
*sb
= journal
->j_private
;
2800 struct ext4_buddy e4b
;
2801 struct ext4_group_info
*db
;
2802 int err
, count
= 0, count2
= 0;
2803 struct ext4_free_data
*entry
;
2804 ext4_fsblk_t discard_block
;
2805 struct list_head
*l
, *ltmp
;
2807 list_for_each_safe(l
, ltmp
, &txn
->t_private_list
) {
2808 entry
= list_entry(l
, struct ext4_free_data
, list
);
2810 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
2811 entry
->count
, entry
->group
, entry
);
2813 err
= ext4_mb_load_buddy(sb
, entry
->group
, &e4b
);
2814 /* we expect to find existing buddy because it's pinned */
2818 /* there are blocks to put in buddy to make them really free */
2819 count
+= entry
->count
;
2821 ext4_lock_group(sb
, entry
->group
);
2822 /* Take it out of per group rb tree */
2823 rb_erase(&entry
->node
, &(db
->bb_free_root
));
2824 mb_free_blocks(NULL
, &e4b
, entry
->start_blk
, entry
->count
);
2826 if (!db
->bb_free_root
.rb_node
) {
2827 /* No more items in the per group rb tree
2828 * balance refcounts from ext4_mb_free_metadata()
2830 page_cache_release(e4b
.bd_buddy_page
);
2831 page_cache_release(e4b
.bd_bitmap_page
);
2833 ext4_unlock_group(sb
, entry
->group
);
2834 discard_block
= (ext4_fsblk_t
) entry
->group
* EXT4_BLOCKS_PER_GROUP(sb
)
2836 + le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
2837 trace_ext4_discard_blocks(sb
, (unsigned long long)discard_block
,
2839 sb_issue_discard(sb
, discard_block
, entry
->count
);
2841 kmem_cache_free(ext4_free_ext_cachep
, entry
);
2842 ext4_mb_release_desc(&e4b
);
2845 mb_debug(1, "freed %u blocks in %u structures\n", count
, count2
);
2848 #ifdef CONFIG_EXT4_DEBUG
2849 u8 mb_enable_debug __read_mostly
;
2851 static struct dentry
*debugfs_dir
;
2852 static struct dentry
*debugfs_debug
;
2854 static void __init
ext4_create_debugfs_entry(void)
2856 debugfs_dir
= debugfs_create_dir("ext4", NULL
);
2858 debugfs_debug
= debugfs_create_u8("mballoc-debug",
2864 static void ext4_remove_debugfs_entry(void)
2866 debugfs_remove(debugfs_debug
);
2867 debugfs_remove(debugfs_dir
);
2872 static void __init
ext4_create_debugfs_entry(void)
2876 static void ext4_remove_debugfs_entry(void)
2882 int __init
init_ext4_mballoc(void)
2884 ext4_pspace_cachep
=
2885 kmem_cache_create("ext4_prealloc_space",
2886 sizeof(struct ext4_prealloc_space
),
2887 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2888 if (ext4_pspace_cachep
== NULL
)
2892 kmem_cache_create("ext4_alloc_context",
2893 sizeof(struct ext4_allocation_context
),
2894 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2895 if (ext4_ac_cachep
== NULL
) {
2896 kmem_cache_destroy(ext4_pspace_cachep
);
2900 ext4_free_ext_cachep
=
2901 kmem_cache_create("ext4_free_block_extents",
2902 sizeof(struct ext4_free_data
),
2903 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2904 if (ext4_free_ext_cachep
== NULL
) {
2905 kmem_cache_destroy(ext4_pspace_cachep
);
2906 kmem_cache_destroy(ext4_ac_cachep
);
2909 ext4_create_debugfs_entry();
2913 void exit_ext4_mballoc(void)
2916 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2917 * before destroying the slab cache.
2920 kmem_cache_destroy(ext4_pspace_cachep
);
2921 kmem_cache_destroy(ext4_ac_cachep
);
2922 kmem_cache_destroy(ext4_free_ext_cachep
);
2923 ext4_remove_debugfs_entry();
2928 * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
2929 * Returns 0 if success or error code
2931 static noinline_for_stack
int
2932 ext4_mb_mark_diskspace_used(struct ext4_allocation_context
*ac
,
2933 handle_t
*handle
, unsigned int reserv_blks
)
2935 struct buffer_head
*bitmap_bh
= NULL
;
2936 struct ext4_super_block
*es
;
2937 struct ext4_group_desc
*gdp
;
2938 struct buffer_head
*gdp_bh
;
2939 struct ext4_sb_info
*sbi
;
2940 struct super_block
*sb
;
2944 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
2945 BUG_ON(ac
->ac_b_ex
.fe_len
<= 0);
2953 bitmap_bh
= ext4_read_block_bitmap(sb
, ac
->ac_b_ex
.fe_group
);
2957 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
2962 gdp
= ext4_get_group_desc(sb
, ac
->ac_b_ex
.fe_group
, &gdp_bh
);
2966 ext4_debug("using block group %u(%d)\n", ac
->ac_b_ex
.fe_group
,
2967 ext4_free_blks_count(sb
, gdp
));
2969 err
= ext4_journal_get_write_access(handle
, gdp_bh
);
2973 block
= ac
->ac_b_ex
.fe_group
* EXT4_BLOCKS_PER_GROUP(sb
)
2974 + ac
->ac_b_ex
.fe_start
2975 + le32_to_cpu(es
->s_first_data_block
);
2977 len
= ac
->ac_b_ex
.fe_len
;
2978 if (!ext4_data_block_valid(sbi
, block
, len
)) {
2979 ext4_error(sb
, __func__
,
2980 "Allocating blocks %llu-%llu which overlap "
2981 "fs metadata\n", block
, block
+len
);
2982 /* File system mounted not to panic on error
2983 * Fix the bitmap and repeat the block allocation
2984 * We leak some of the blocks here.
2986 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
2987 mb_set_bits(bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,
2988 ac
->ac_b_ex
.fe_len
);
2989 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
2990 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
2996 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
2997 #ifdef AGGRESSIVE_CHECK
3000 for (i
= 0; i
< ac
->ac_b_ex
.fe_len
; i
++) {
3001 BUG_ON(mb_test_bit(ac
->ac_b_ex
.fe_start
+ i
,
3002 bitmap_bh
->b_data
));
3006 mb_set_bits(bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,ac
->ac_b_ex
.fe_len
);
3007 if (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
3008 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT
);
3009 ext4_free_blks_set(sb
, gdp
,
3010 ext4_free_blocks_after_init(sb
,
3011 ac
->ac_b_ex
.fe_group
, gdp
));
3013 len
= ext4_free_blks_count(sb
, gdp
) - ac
->ac_b_ex
.fe_len
;
3014 ext4_free_blks_set(sb
, gdp
, len
);
3015 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, ac
->ac_b_ex
.fe_group
, gdp
);
3017 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3018 percpu_counter_sub(&sbi
->s_freeblocks_counter
, ac
->ac_b_ex
.fe_len
);
3020 * Now reduce the dirty block count also. Should not go negative
3022 if (!(ac
->ac_flags
& EXT4_MB_DELALLOC_RESERVED
))
3023 /* release all the reserved blocks if non delalloc */
3024 percpu_counter_sub(&sbi
->s_dirtyblocks_counter
, reserv_blks
);
3026 percpu_counter_sub(&sbi
->s_dirtyblocks_counter
,
3027 ac
->ac_b_ex
.fe_len
);
3028 /* convert reserved quota blocks to real quota blocks */
3029 vfs_dq_claim_block(ac
->ac_inode
, ac
->ac_b_ex
.fe_len
);
3032 if (sbi
->s_log_groups_per_flex
) {
3033 ext4_group_t flex_group
= ext4_flex_group(sbi
,
3034 ac
->ac_b_ex
.fe_group
);
3035 atomic_sub(ac
->ac_b_ex
.fe_len
,
3036 &sbi
->s_flex_groups
[flex_group
].free_blocks
);
3039 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
3042 err
= ext4_handle_dirty_metadata(handle
, NULL
, gdp_bh
);
3051 * here we normalize request for locality group
3052 * Group request are normalized to s_strip size if we set the same via mount
3053 * option. If not we set it to s_mb_group_prealloc which can be configured via
3054 * /sys/fs/ext4/<partition>/mb_group_prealloc
3056 * XXX: should we try to preallocate more than the group has now?
3058 static void ext4_mb_normalize_group_request(struct ext4_allocation_context
*ac
)
3060 struct super_block
*sb
= ac
->ac_sb
;
3061 struct ext4_locality_group
*lg
= ac
->ac_lg
;
3064 if (EXT4_SB(sb
)->s_stripe
)
3065 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_stripe
;
3067 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_mb_group_prealloc
;
3068 mb_debug(1, "#%u: goal %u blocks for locality group\n",
3069 current
->pid
, ac
->ac_g_ex
.fe_len
);
3073 * Normalization means making request better in terms of
3074 * size and alignment
3076 static noinline_for_stack
void
3077 ext4_mb_normalize_request(struct ext4_allocation_context
*ac
,
3078 struct ext4_allocation_request
*ar
)
3082 loff_t size
, orig_size
, start_off
;
3083 ext4_lblk_t start
, orig_start
;
3084 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3085 struct ext4_prealloc_space
*pa
;
3087 /* do normalize only data requests, metadata requests
3088 do not need preallocation */
3089 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3092 /* sometime caller may want exact blocks */
3093 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
3096 /* caller may indicate that preallocation isn't
3097 * required (it's a tail, for example) */
3098 if (ac
->ac_flags
& EXT4_MB_HINT_NOPREALLOC
)
3101 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
) {
3102 ext4_mb_normalize_group_request(ac
);
3106 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
3108 /* first, let's learn actual file size
3109 * given current request is allocated */
3110 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
3111 size
= size
<< bsbits
;
3112 if (size
< i_size_read(ac
->ac_inode
))
3113 size
= i_size_read(ac
->ac_inode
);
3115 /* max size of free chunks */
3118 #define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3119 (req <= (size) || max <= (chunk_size))
3121 /* first, try to predict filesize */
3122 /* XXX: should this table be tunable? */
3124 if (size
<= 16 * 1024) {
3126 } else if (size
<= 32 * 1024) {
3128 } else if (size
<= 64 * 1024) {
3130 } else if (size
<= 128 * 1024) {
3132 } else if (size
<= 256 * 1024) {
3134 } else if (size
<= 512 * 1024) {
3136 } else if (size
<= 1024 * 1024) {
3138 } else if (NRL_CHECK_SIZE(size
, 4 * 1024 * 1024, max
, 2 * 1024)) {
3139 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3140 (21 - bsbits
)) << 21;
3141 size
= 2 * 1024 * 1024;
3142 } else if (NRL_CHECK_SIZE(size
, 8 * 1024 * 1024, max
, 4 * 1024)) {
3143 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3144 (22 - bsbits
)) << 22;
3145 size
= 4 * 1024 * 1024;
3146 } else if (NRL_CHECK_SIZE(ac
->ac_o_ex
.fe_len
,
3147 (8<<20)>>bsbits
, max
, 8 * 1024)) {
3148 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3149 (23 - bsbits
)) << 23;
3150 size
= 8 * 1024 * 1024;
3152 start_off
= (loff_t
)ac
->ac_o_ex
.fe_logical
<< bsbits
;
3153 size
= ac
->ac_o_ex
.fe_len
<< bsbits
;
3155 orig_size
= size
= size
>> bsbits
;
3156 orig_start
= start
= start_off
>> bsbits
;
3158 /* don't cover already allocated blocks in selected range */
3159 if (ar
->pleft
&& start
<= ar
->lleft
) {
3160 size
-= ar
->lleft
+ 1 - start
;
3161 start
= ar
->lleft
+ 1;
3163 if (ar
->pright
&& start
+ size
- 1 >= ar
->lright
)
3164 size
-= start
+ size
- ar
->lright
;
3168 /* check we don't cross already preallocated blocks */
3170 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3175 spin_lock(&pa
->pa_lock
);
3176 if (pa
->pa_deleted
) {
3177 spin_unlock(&pa
->pa_lock
);
3181 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
3183 /* PA must not overlap original request */
3184 BUG_ON(!(ac
->ac_o_ex
.fe_logical
>= pa_end
||
3185 ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
));
3187 /* skip PAs this normalized request doesn't overlap with */
3188 if (pa
->pa_lstart
>= end
|| pa_end
<= start
) {
3189 spin_unlock(&pa
->pa_lock
);
3192 BUG_ON(pa
->pa_lstart
<= start
&& pa_end
>= end
);
3194 /* adjust start or end to be adjacent to this pa */
3195 if (pa_end
<= ac
->ac_o_ex
.fe_logical
) {
3196 BUG_ON(pa_end
< start
);
3198 } else if (pa
->pa_lstart
> ac
->ac_o_ex
.fe_logical
) {
3199 BUG_ON(pa
->pa_lstart
> end
);
3200 end
= pa
->pa_lstart
;
3202 spin_unlock(&pa
->pa_lock
);
3207 /* XXX: extra loop to check we really don't overlap preallocations */
3209 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3211 spin_lock(&pa
->pa_lock
);
3212 if (pa
->pa_deleted
== 0) {
3213 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
3214 BUG_ON(!(start
>= pa_end
|| end
<= pa
->pa_lstart
));
3216 spin_unlock(&pa
->pa_lock
);
3220 if (start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3221 start
> ac
->ac_o_ex
.fe_logical
) {
3222 printk(KERN_ERR
"start %lu, size %lu, fe_logical %lu\n",
3223 (unsigned long) start
, (unsigned long) size
,
3224 (unsigned long) ac
->ac_o_ex
.fe_logical
);
3226 BUG_ON(start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3227 start
> ac
->ac_o_ex
.fe_logical
);
3228 BUG_ON(size
<= 0 || size
> EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
3230 /* now prepare goal request */
3232 /* XXX: is it better to align blocks WRT to logical
3233 * placement or satisfy big request as is */
3234 ac
->ac_g_ex
.fe_logical
= start
;
3235 ac
->ac_g_ex
.fe_len
= size
;
3237 /* define goal start in order to merge */
3238 if (ar
->pright
&& (ar
->lright
== (start
+ size
))) {
3239 /* merge to the right */
3240 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pright
- size
,
3241 &ac
->ac_f_ex
.fe_group
,
3242 &ac
->ac_f_ex
.fe_start
);
3243 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3245 if (ar
->pleft
&& (ar
->lleft
+ 1 == start
)) {
3246 /* merge to the left */
3247 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pleft
+ 1,
3248 &ac
->ac_f_ex
.fe_group
,
3249 &ac
->ac_f_ex
.fe_start
);
3250 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3253 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size
,
3254 (unsigned) orig_size
, (unsigned) start
);
3257 static void ext4_mb_collect_stats(struct ext4_allocation_context
*ac
)
3259 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3261 if (sbi
->s_mb_stats
&& ac
->ac_g_ex
.fe_len
> 1) {
3262 atomic_inc(&sbi
->s_bal_reqs
);
3263 atomic_add(ac
->ac_b_ex
.fe_len
, &sbi
->s_bal_allocated
);
3264 if (ac
->ac_o_ex
.fe_len
>= ac
->ac_g_ex
.fe_len
)
3265 atomic_inc(&sbi
->s_bal_success
);
3266 atomic_add(ac
->ac_found
, &sbi
->s_bal_ex_scanned
);
3267 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
3268 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
3269 atomic_inc(&sbi
->s_bal_goals
);
3270 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
)
3271 atomic_inc(&sbi
->s_bal_breaks
);
3274 ext4_mb_store_history(ac
);
3278 * use blocks preallocated to inode
3280 static void ext4_mb_use_inode_pa(struct ext4_allocation_context
*ac
,
3281 struct ext4_prealloc_space
*pa
)
3287 /* found preallocated blocks, use them */
3288 start
= pa
->pa_pstart
+ (ac
->ac_o_ex
.fe_logical
- pa
->pa_lstart
);
3289 end
= min(pa
->pa_pstart
+ pa
->pa_len
, start
+ ac
->ac_o_ex
.fe_len
);
3291 ext4_get_group_no_and_offset(ac
->ac_sb
, start
, &ac
->ac_b_ex
.fe_group
,
3292 &ac
->ac_b_ex
.fe_start
);
3293 ac
->ac_b_ex
.fe_len
= len
;
3294 ac
->ac_status
= AC_STATUS_FOUND
;
3297 BUG_ON(start
< pa
->pa_pstart
);
3298 BUG_ON(start
+ len
> pa
->pa_pstart
+ pa
->pa_len
);
3299 BUG_ON(pa
->pa_free
< len
);
3302 mb_debug(1, "use %llu/%u from inode pa %p\n", start
, len
, pa
);
3306 * use blocks preallocated to locality group
3308 static void ext4_mb_use_group_pa(struct ext4_allocation_context
*ac
,
3309 struct ext4_prealloc_space
*pa
)
3311 unsigned int len
= ac
->ac_o_ex
.fe_len
;
3313 ext4_get_group_no_and_offset(ac
->ac_sb
, pa
->pa_pstart
,
3314 &ac
->ac_b_ex
.fe_group
,
3315 &ac
->ac_b_ex
.fe_start
);
3316 ac
->ac_b_ex
.fe_len
= len
;
3317 ac
->ac_status
= AC_STATUS_FOUND
;
3320 /* we don't correct pa_pstart or pa_plen here to avoid
3321 * possible race when the group is being loaded concurrently
3322 * instead we correct pa later, after blocks are marked
3323 * in on-disk bitmap -- see ext4_mb_release_context()
3324 * Other CPUs are prevented from allocating from this pa by lg_mutex
3326 mb_debug(1, "use %u/%u from group pa %p\n", pa
->pa_lstart
-len
, len
, pa
);
3330 * Return the prealloc space that have minimal distance
3331 * from the goal block. @cpa is the prealloc
3332 * space that is having currently known minimal distance
3333 * from the goal block.
3335 static struct ext4_prealloc_space
*
3336 ext4_mb_check_group_pa(ext4_fsblk_t goal_block
,
3337 struct ext4_prealloc_space
*pa
,
3338 struct ext4_prealloc_space
*cpa
)
3340 ext4_fsblk_t cur_distance
, new_distance
;
3343 atomic_inc(&pa
->pa_count
);
3346 cur_distance
= abs(goal_block
- cpa
->pa_pstart
);
3347 new_distance
= abs(goal_block
- pa
->pa_pstart
);
3349 if (cur_distance
< new_distance
)
3352 /* drop the previous reference */
3353 atomic_dec(&cpa
->pa_count
);
3354 atomic_inc(&pa
->pa_count
);
3359 * search goal blocks in preallocated space
3361 static noinline_for_stack
int
3362 ext4_mb_use_preallocated(struct ext4_allocation_context
*ac
)
3365 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3366 struct ext4_locality_group
*lg
;
3367 struct ext4_prealloc_space
*pa
, *cpa
= NULL
;
3368 ext4_fsblk_t goal_block
;
3370 /* only data can be preallocated */
3371 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3374 /* first, try per-file preallocation */
3376 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3378 /* all fields in this condition don't change,
3379 * so we can skip locking for them */
3380 if (ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
||
3381 ac
->ac_o_ex
.fe_logical
>= pa
->pa_lstart
+ pa
->pa_len
)
3384 /* found preallocated blocks, use them */
3385 spin_lock(&pa
->pa_lock
);
3386 if (pa
->pa_deleted
== 0 && pa
->pa_free
) {
3387 atomic_inc(&pa
->pa_count
);
3388 ext4_mb_use_inode_pa(ac
, pa
);
3389 spin_unlock(&pa
->pa_lock
);
3390 ac
->ac_criteria
= 10;
3394 spin_unlock(&pa
->pa_lock
);
3398 /* can we use group allocation? */
3399 if (!(ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
))
3402 /* inode may have no locality group for some reason */
3406 order
= fls(ac
->ac_o_ex
.fe_len
) - 1;
3407 if (order
> PREALLOC_TB_SIZE
- 1)
3408 /* The max size of hash table is PREALLOC_TB_SIZE */
3409 order
= PREALLOC_TB_SIZE
- 1;
3411 goal_block
= ac
->ac_g_ex
.fe_group
* EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
) +
3412 ac
->ac_g_ex
.fe_start
+
3413 le32_to_cpu(EXT4_SB(ac
->ac_sb
)->s_es
->s_first_data_block
);
3415 * search for the prealloc space that is having
3416 * minimal distance from the goal block.
3418 for (i
= order
; i
< PREALLOC_TB_SIZE
; i
++) {
3420 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[i
],
3422 spin_lock(&pa
->pa_lock
);
3423 if (pa
->pa_deleted
== 0 &&
3424 pa
->pa_free
>= ac
->ac_o_ex
.fe_len
) {
3426 cpa
= ext4_mb_check_group_pa(goal_block
,
3429 spin_unlock(&pa
->pa_lock
);
3434 ext4_mb_use_group_pa(ac
, cpa
);
3435 ac
->ac_criteria
= 20;
3442 * the function goes through all block freed in the group
3443 * but not yet committed and marks them used in in-core bitmap.
3444 * buddy must be generated from this bitmap
3445 * Need to be called with the ext4 group lock held
3447 static void ext4_mb_generate_from_freelist(struct super_block
*sb
, void *bitmap
,
3451 struct ext4_group_info
*grp
;
3452 struct ext4_free_data
*entry
;
3454 grp
= ext4_get_group_info(sb
, group
);
3455 n
= rb_first(&(grp
->bb_free_root
));
3458 entry
= rb_entry(n
, struct ext4_free_data
, node
);
3459 mb_set_bits(bitmap
, entry
->start_blk
, entry
->count
);
3466 * the function goes through all preallocation in this group and marks them
3467 * used in in-core bitmap. buddy must be generated from this bitmap
3468 * Need to be called with ext4 group lock held
3470 static noinline_for_stack
3471 void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
3474 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3475 struct ext4_prealloc_space
*pa
;
3476 struct list_head
*cur
;
3477 ext4_group_t groupnr
;
3478 ext4_grpblk_t start
;
3479 int preallocated
= 0;
3483 /* all form of preallocation discards first load group,
3484 * so the only competing code is preallocation use.
3485 * we don't need any locking here
3486 * notice we do NOT ignore preallocations with pa_deleted
3487 * otherwise we could leave used blocks available for
3488 * allocation in buddy when concurrent ext4_mb_put_pa()
3489 * is dropping preallocation
3491 list_for_each(cur
, &grp
->bb_prealloc_list
) {
3492 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
3493 spin_lock(&pa
->pa_lock
);
3494 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
3497 spin_unlock(&pa
->pa_lock
);
3498 if (unlikely(len
== 0))
3500 BUG_ON(groupnr
!= group
);
3501 mb_set_bits(bitmap
, start
, len
);
3502 preallocated
+= len
;
3505 mb_debug(1, "prellocated %u for group %u\n", preallocated
, group
);
3508 static void ext4_mb_pa_callback(struct rcu_head
*head
)
3510 struct ext4_prealloc_space
*pa
;
3511 pa
= container_of(head
, struct ext4_prealloc_space
, u
.pa_rcu
);
3512 kmem_cache_free(ext4_pspace_cachep
, pa
);
3516 * drops a reference to preallocated space descriptor
3517 * if this was the last reference and the space is consumed
3519 static void ext4_mb_put_pa(struct ext4_allocation_context
*ac
,
3520 struct super_block
*sb
, struct ext4_prealloc_space
*pa
)
3523 ext4_fsblk_t grp_blk
;
3525 if (!atomic_dec_and_test(&pa
->pa_count
) || pa
->pa_free
!= 0)
3528 /* in this short window concurrent discard can set pa_deleted */
3529 spin_lock(&pa
->pa_lock
);
3530 if (pa
->pa_deleted
== 1) {
3531 spin_unlock(&pa
->pa_lock
);
3536 spin_unlock(&pa
->pa_lock
);
3538 grp_blk
= pa
->pa_pstart
;
3540 * If doing group-based preallocation, pa_pstart may be in the
3541 * next group when pa is used up
3543 if (pa
->pa_type
== MB_GROUP_PA
)
3546 ext4_get_group_no_and_offset(sb
, grp_blk
, &grp
, NULL
);
3551 * P1 (buddy init) P2 (regular allocation)
3552 * find block B in PA
3553 * copy on-disk bitmap to buddy
3554 * mark B in on-disk bitmap
3555 * drop PA from group
3556 * mark all PAs in buddy
3558 * thus, P1 initializes buddy with B available. to prevent this
3559 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3562 ext4_lock_group(sb
, grp
);
3563 list_del(&pa
->pa_group_list
);
3564 ext4_unlock_group(sb
, grp
);
3566 spin_lock(pa
->pa_obj_lock
);
3567 list_del_rcu(&pa
->pa_inode_list
);
3568 spin_unlock(pa
->pa_obj_lock
);
3570 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3574 * creates new preallocated space for given inode
3576 static noinline_for_stack
int
3577 ext4_mb_new_inode_pa(struct ext4_allocation_context
*ac
)
3579 struct super_block
*sb
= ac
->ac_sb
;
3580 struct ext4_prealloc_space
*pa
;
3581 struct ext4_group_info
*grp
;
3582 struct ext4_inode_info
*ei
;
3584 /* preallocate only when found space is larger then requested */
3585 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3586 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3587 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3589 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3593 if (ac
->ac_b_ex
.fe_len
< ac
->ac_g_ex
.fe_len
) {
3599 /* we can't allocate as much as normalizer wants.
3600 * so, found space must get proper lstart
3601 * to cover original request */
3602 BUG_ON(ac
->ac_g_ex
.fe_logical
> ac
->ac_o_ex
.fe_logical
);
3603 BUG_ON(ac
->ac_g_ex
.fe_len
< ac
->ac_o_ex
.fe_len
);
3605 /* we're limited by original request in that
3606 * logical block must be covered any way
3607 * winl is window we can move our chunk within */
3608 winl
= ac
->ac_o_ex
.fe_logical
- ac
->ac_g_ex
.fe_logical
;
3610 /* also, we should cover whole original request */
3611 wins
= ac
->ac_b_ex
.fe_len
- ac
->ac_o_ex
.fe_len
;
3613 /* the smallest one defines real window */
3614 win
= min(winl
, wins
);
3616 offs
= ac
->ac_o_ex
.fe_logical
% ac
->ac_b_ex
.fe_len
;
3617 if (offs
&& offs
< win
)
3620 ac
->ac_b_ex
.fe_logical
= ac
->ac_o_ex
.fe_logical
- win
;
3621 BUG_ON(ac
->ac_o_ex
.fe_logical
< ac
->ac_b_ex
.fe_logical
);
3622 BUG_ON(ac
->ac_o_ex
.fe_len
> ac
->ac_b_ex
.fe_len
);
3625 /* preallocation can change ac_b_ex, thus we store actually
3626 * allocated blocks for history */
3627 ac
->ac_f_ex
= ac
->ac_b_ex
;
3629 pa
->pa_lstart
= ac
->ac_b_ex
.fe_logical
;
3630 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3631 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3632 pa
->pa_free
= pa
->pa_len
;
3633 atomic_set(&pa
->pa_count
, 1);
3634 spin_lock_init(&pa
->pa_lock
);
3635 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3636 INIT_LIST_HEAD(&pa
->pa_group_list
);
3638 pa
->pa_type
= MB_INODE_PA
;
3640 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa
,
3641 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3642 trace_ext4_mb_new_inode_pa(ac
, pa
);
3644 ext4_mb_use_inode_pa(ac
, pa
);
3645 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3647 ei
= EXT4_I(ac
->ac_inode
);
3648 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3650 pa
->pa_obj_lock
= &ei
->i_prealloc_lock
;
3651 pa
->pa_inode
= ac
->ac_inode
;
3653 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3654 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3655 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3657 spin_lock(pa
->pa_obj_lock
);
3658 list_add_rcu(&pa
->pa_inode_list
, &ei
->i_prealloc_list
);
3659 spin_unlock(pa
->pa_obj_lock
);
3665 * creates new preallocated space for locality group inodes belongs to
3667 static noinline_for_stack
int
3668 ext4_mb_new_group_pa(struct ext4_allocation_context
*ac
)
3670 struct super_block
*sb
= ac
->ac_sb
;
3671 struct ext4_locality_group
*lg
;
3672 struct ext4_prealloc_space
*pa
;
3673 struct ext4_group_info
*grp
;
3675 /* preallocate only when found space is larger then requested */
3676 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3677 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3678 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3680 BUG_ON(ext4_pspace_cachep
== NULL
);
3681 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3685 /* preallocation can change ac_b_ex, thus we store actually
3686 * allocated blocks for history */
3687 ac
->ac_f_ex
= ac
->ac_b_ex
;
3689 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3690 pa
->pa_lstart
= pa
->pa_pstart
;
3691 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3692 pa
->pa_free
= pa
->pa_len
;
3693 atomic_set(&pa
->pa_count
, 1);
3694 spin_lock_init(&pa
->pa_lock
);
3695 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3696 INIT_LIST_HEAD(&pa
->pa_group_list
);
3698 pa
->pa_type
= MB_GROUP_PA
;
3700 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa
,
3701 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3702 trace_ext4_mb_new_group_pa(ac
, pa
);
3704 ext4_mb_use_group_pa(ac
, pa
);
3705 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3707 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3711 pa
->pa_obj_lock
= &lg
->lg_prealloc_lock
;
3712 pa
->pa_inode
= NULL
;
3714 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3715 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3716 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3719 * We will later add the new pa to the right bucket
3720 * after updating the pa_free in ext4_mb_release_context
3725 static int ext4_mb_new_preallocation(struct ext4_allocation_context
*ac
)
3729 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
3730 err
= ext4_mb_new_group_pa(ac
);
3732 err
= ext4_mb_new_inode_pa(ac
);
3737 * finds all unused blocks in on-disk bitmap, frees them in
3738 * in-core bitmap and buddy.
3739 * @pa must be unlinked from inode and group lists, so that
3740 * nobody else can find/use it.
3741 * the caller MUST hold group/inode locks.
3742 * TODO: optimize the case when there are no in-core structures yet
3744 static noinline_for_stack
int
3745 ext4_mb_release_inode_pa(struct ext4_buddy
*e4b
, struct buffer_head
*bitmap_bh
,
3746 struct ext4_prealloc_space
*pa
,
3747 struct ext4_allocation_context
*ac
)
3749 struct super_block
*sb
= e4b
->bd_sb
;
3750 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3755 unsigned long long grp_blk_start
;
3760 BUG_ON(pa
->pa_deleted
== 0);
3761 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3762 grp_blk_start
= pa
->pa_pstart
- bit
;
3763 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3764 end
= bit
+ pa
->pa_len
;
3768 ac
->ac_inode
= pa
->pa_inode
;
3769 ac
->ac_op
= EXT4_MB_HISTORY_DISCARD
;
3773 bit
= mb_find_next_zero_bit(bitmap_bh
->b_data
, end
, bit
);
3776 next
= mb_find_next_bit(bitmap_bh
->b_data
, end
, bit
);
3777 start
= group
* EXT4_BLOCKS_PER_GROUP(sb
) + bit
+
3778 le32_to_cpu(sbi
->s_es
->s_first_data_block
);
3779 mb_debug(1, " free preallocated %u/%u in group %u\n",
3780 (unsigned) start
, (unsigned) next
- bit
,
3785 ac
->ac_b_ex
.fe_group
= group
;
3786 ac
->ac_b_ex
.fe_start
= bit
;
3787 ac
->ac_b_ex
.fe_len
= next
- bit
;
3788 ac
->ac_b_ex
.fe_logical
= 0;
3789 ext4_mb_store_history(ac
);
3792 trace_ext4_mb_release_inode_pa(ac
, pa
, grp_blk_start
+ bit
,
3794 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, next
- bit
);
3797 if (free
!= pa
->pa_free
) {
3798 printk(KERN_CRIT
"pa %p: logic %lu, phys. %lu, len %lu\n",
3799 pa
, (unsigned long) pa
->pa_lstart
,
3800 (unsigned long) pa
->pa_pstart
,
3801 (unsigned long) pa
->pa_len
);
3802 ext4_grp_locked_error(sb
, group
,
3803 __func__
, "free %u, pa_free %u",
3806 * pa is already deleted so we use the value obtained
3807 * from the bitmap and continue.
3810 atomic_add(free
, &sbi
->s_mb_discarded
);
3815 static noinline_for_stack
int
3816 ext4_mb_release_group_pa(struct ext4_buddy
*e4b
,
3817 struct ext4_prealloc_space
*pa
,
3818 struct ext4_allocation_context
*ac
)
3820 struct super_block
*sb
= e4b
->bd_sb
;
3825 ac
->ac_op
= EXT4_MB_HISTORY_DISCARD
;
3827 trace_ext4_mb_release_group_pa(ac
, pa
);
3828 BUG_ON(pa
->pa_deleted
== 0);
3829 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3830 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3831 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, pa
->pa_len
);
3832 atomic_add(pa
->pa_len
, &EXT4_SB(sb
)->s_mb_discarded
);
3836 ac
->ac_inode
= NULL
;
3837 ac
->ac_b_ex
.fe_group
= group
;
3838 ac
->ac_b_ex
.fe_start
= bit
;
3839 ac
->ac_b_ex
.fe_len
= pa
->pa_len
;
3840 ac
->ac_b_ex
.fe_logical
= 0;
3841 ext4_mb_store_history(ac
);
3848 * releases all preallocations in given group
3850 * first, we need to decide discard policy:
3851 * - when do we discard
3853 * - how many do we discard
3854 * 1) how many requested
3856 static noinline_for_stack
int
3857 ext4_mb_discard_group_preallocations(struct super_block
*sb
,
3858 ext4_group_t group
, int needed
)
3860 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3861 struct buffer_head
*bitmap_bh
= NULL
;
3862 struct ext4_prealloc_space
*pa
, *tmp
;
3863 struct ext4_allocation_context
*ac
;
3864 struct list_head list
;
3865 struct ext4_buddy e4b
;
3870 mb_debug(1, "discard preallocation for group %u\n", group
);
3872 if (list_empty(&grp
->bb_prealloc_list
))
3875 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
3876 if (bitmap_bh
== NULL
) {
3877 ext4_error(sb
, __func__
, "Error in reading block "
3878 "bitmap for %u", group
);
3882 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
3884 ext4_error(sb
, __func__
, "Error in loading buddy "
3885 "information for %u", group
);
3891 needed
= EXT4_BLOCKS_PER_GROUP(sb
) + 1;
3893 INIT_LIST_HEAD(&list
);
3894 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
3898 ext4_lock_group(sb
, group
);
3899 list_for_each_entry_safe(pa
, tmp
,
3900 &grp
->bb_prealloc_list
, pa_group_list
) {
3901 spin_lock(&pa
->pa_lock
);
3902 if (atomic_read(&pa
->pa_count
)) {
3903 spin_unlock(&pa
->pa_lock
);
3907 if (pa
->pa_deleted
) {
3908 spin_unlock(&pa
->pa_lock
);
3912 /* seems this one can be freed ... */
3915 /* we can trust pa_free ... */
3916 free
+= pa
->pa_free
;
3918 spin_unlock(&pa
->pa_lock
);
3920 list_del(&pa
->pa_group_list
);
3921 list_add(&pa
->u
.pa_tmp_list
, &list
);
3924 /* if we still need more blocks and some PAs were used, try again */
3925 if (free
< needed
&& busy
) {
3927 ext4_unlock_group(sb
, group
);
3929 * Yield the CPU here so that we don't get soft lockup
3930 * in non preempt case.
3936 /* found anything to free? */
3937 if (list_empty(&list
)) {
3942 /* now free all selected PAs */
3943 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
3945 /* remove from object (inode or locality group) */
3946 spin_lock(pa
->pa_obj_lock
);
3947 list_del_rcu(&pa
->pa_inode_list
);
3948 spin_unlock(pa
->pa_obj_lock
);
3950 if (pa
->pa_type
== MB_GROUP_PA
)
3951 ext4_mb_release_group_pa(&e4b
, pa
, ac
);
3953 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
, ac
);
3955 list_del(&pa
->u
.pa_tmp_list
);
3956 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3960 ext4_unlock_group(sb
, group
);
3962 kmem_cache_free(ext4_ac_cachep
, ac
);
3963 ext4_mb_release_desc(&e4b
);
3969 * releases all non-used preallocated blocks for given inode
3971 * It's important to discard preallocations under i_data_sem
3972 * We don't want another block to be served from the prealloc
3973 * space when we are discarding the inode prealloc space.
3975 * FIXME!! Make sure it is valid at all the call sites
3977 void ext4_discard_preallocations(struct inode
*inode
)
3979 struct ext4_inode_info
*ei
= EXT4_I(inode
);
3980 struct super_block
*sb
= inode
->i_sb
;
3981 struct buffer_head
*bitmap_bh
= NULL
;
3982 struct ext4_prealloc_space
*pa
, *tmp
;
3983 struct ext4_allocation_context
*ac
;
3984 ext4_group_t group
= 0;
3985 struct list_head list
;
3986 struct ext4_buddy e4b
;
3989 if (!S_ISREG(inode
->i_mode
)) {
3990 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3994 mb_debug(1, "discard preallocation for inode %lu\n", inode
->i_ino
);
3995 trace_ext4_discard_preallocations(inode
);
3997 INIT_LIST_HEAD(&list
);
3999 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4002 ac
->ac_inode
= inode
;
4005 /* first, collect all pa's in the inode */
4006 spin_lock(&ei
->i_prealloc_lock
);
4007 while (!list_empty(&ei
->i_prealloc_list
)) {
4008 pa
= list_entry(ei
->i_prealloc_list
.next
,
4009 struct ext4_prealloc_space
, pa_inode_list
);
4010 BUG_ON(pa
->pa_obj_lock
!= &ei
->i_prealloc_lock
);
4011 spin_lock(&pa
->pa_lock
);
4012 if (atomic_read(&pa
->pa_count
)) {
4013 /* this shouldn't happen often - nobody should
4014 * use preallocation while we're discarding it */
4015 spin_unlock(&pa
->pa_lock
);
4016 spin_unlock(&ei
->i_prealloc_lock
);
4017 printk(KERN_ERR
"uh-oh! used pa while discarding\n");
4019 schedule_timeout_uninterruptible(HZ
);
4023 if (pa
->pa_deleted
== 0) {
4025 spin_unlock(&pa
->pa_lock
);
4026 list_del_rcu(&pa
->pa_inode_list
);
4027 list_add(&pa
->u
.pa_tmp_list
, &list
);
4031 /* someone is deleting pa right now */
4032 spin_unlock(&pa
->pa_lock
);
4033 spin_unlock(&ei
->i_prealloc_lock
);
4035 /* we have to wait here because pa_deleted
4036 * doesn't mean pa is already unlinked from
4037 * the list. as we might be called from
4038 * ->clear_inode() the inode will get freed
4039 * and concurrent thread which is unlinking
4040 * pa from inode's list may access already
4041 * freed memory, bad-bad-bad */
4043 /* XXX: if this happens too often, we can
4044 * add a flag to force wait only in case
4045 * of ->clear_inode(), but not in case of
4046 * regular truncate */
4047 schedule_timeout_uninterruptible(HZ
);
4050 spin_unlock(&ei
->i_prealloc_lock
);
4052 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
4053 BUG_ON(pa
->pa_type
!= MB_INODE_PA
);
4054 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
4056 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
4058 ext4_error(sb
, __func__
, "Error in loading buddy "
4059 "information for %u", group
);
4063 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
4064 if (bitmap_bh
== NULL
) {
4065 ext4_error(sb
, __func__
, "Error in reading block "
4066 "bitmap for %u", group
);
4067 ext4_mb_release_desc(&e4b
);
4071 ext4_lock_group(sb
, group
);
4072 list_del(&pa
->pa_group_list
);
4073 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
, ac
);
4074 ext4_unlock_group(sb
, group
);
4076 ext4_mb_release_desc(&e4b
);
4079 list_del(&pa
->u
.pa_tmp_list
);
4080 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4083 kmem_cache_free(ext4_ac_cachep
, ac
);
4087 * finds all preallocated spaces and return blocks being freed to them
4088 * if preallocated space becomes full (no block is used from the space)
4089 * then the function frees space in buddy
4090 * XXX: at the moment, truncate (which is the only way to free blocks)
4091 * discards all preallocations
4093 static void ext4_mb_return_to_preallocation(struct inode
*inode
,
4094 struct ext4_buddy
*e4b
,
4095 sector_t block
, int count
)
4097 BUG_ON(!list_empty(&EXT4_I(inode
)->i_prealloc_list
));
4099 #ifdef CONFIG_EXT4_DEBUG
4100 static void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
4102 struct super_block
*sb
= ac
->ac_sb
;
4103 ext4_group_t ngroups
, i
;
4105 printk(KERN_ERR
"EXT4-fs: Can't allocate:"
4106 " Allocation context details:\n");
4107 printk(KERN_ERR
"EXT4-fs: status %d flags %d\n",
4108 ac
->ac_status
, ac
->ac_flags
);
4109 printk(KERN_ERR
"EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
4110 "best %lu/%lu/%lu@%lu cr %d\n",
4111 (unsigned long)ac
->ac_o_ex
.fe_group
,
4112 (unsigned long)ac
->ac_o_ex
.fe_start
,
4113 (unsigned long)ac
->ac_o_ex
.fe_len
,
4114 (unsigned long)ac
->ac_o_ex
.fe_logical
,
4115 (unsigned long)ac
->ac_g_ex
.fe_group
,
4116 (unsigned long)ac
->ac_g_ex
.fe_start
,
4117 (unsigned long)ac
->ac_g_ex
.fe_len
,
4118 (unsigned long)ac
->ac_g_ex
.fe_logical
,
4119 (unsigned long)ac
->ac_b_ex
.fe_group
,
4120 (unsigned long)ac
->ac_b_ex
.fe_start
,
4121 (unsigned long)ac
->ac_b_ex
.fe_len
,
4122 (unsigned long)ac
->ac_b_ex
.fe_logical
,
4123 (int)ac
->ac_criteria
);
4124 printk(KERN_ERR
"EXT4-fs: %lu scanned, %d found\n", ac
->ac_ex_scanned
,
4126 printk(KERN_ERR
"EXT4-fs: groups: \n");
4127 ngroups
= ext4_get_groups_count(sb
);
4128 for (i
= 0; i
< ngroups
; i
++) {
4129 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, i
);
4130 struct ext4_prealloc_space
*pa
;
4131 ext4_grpblk_t start
;
4132 struct list_head
*cur
;
4133 ext4_lock_group(sb
, i
);
4134 list_for_each(cur
, &grp
->bb_prealloc_list
) {
4135 pa
= list_entry(cur
, struct ext4_prealloc_space
,
4137 spin_lock(&pa
->pa_lock
);
4138 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
4140 spin_unlock(&pa
->pa_lock
);
4141 printk(KERN_ERR
"PA:%u:%d:%u \n", i
,
4144 ext4_unlock_group(sb
, i
);
4146 if (grp
->bb_free
== 0)
4148 printk(KERN_ERR
"%u: %d/%d \n",
4149 i
, grp
->bb_free
, grp
->bb_fragments
);
4151 printk(KERN_ERR
"\n");
4154 static inline void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
4161 * We use locality group preallocation for small size file. The size of the
4162 * file is determined by the current size or the resulting size after
4163 * allocation which ever is larger
4165 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
4167 static void ext4_mb_group_or_file(struct ext4_allocation_context
*ac
)
4169 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
4170 int bsbits
= ac
->ac_sb
->s_blocksize_bits
;
4173 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
4176 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
4179 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
4180 isize
= (i_size_read(ac
->ac_inode
) + ac
->ac_sb
->s_blocksize
- 1)
4182 size
= max(size
, isize
);
4184 if ((size
== isize
) &&
4185 !ext4_fs_is_busy(sbi
) &&
4186 (atomic_read(&ac
->ac_inode
->i_writecount
) == 0)) {
4187 ac
->ac_flags
|= EXT4_MB_HINT_NOPREALLOC
;
4191 /* don't use group allocation for large files */
4192 if (size
>= sbi
->s_mb_stream_request
) {
4193 ac
->ac_flags
|= EXT4_MB_STREAM_ALLOC
;
4197 BUG_ON(ac
->ac_lg
!= NULL
);
4199 * locality group prealloc space are per cpu. The reason for having
4200 * per cpu locality group is to reduce the contention between block
4201 * request from multiple CPUs.
4203 ac
->ac_lg
= per_cpu_ptr(sbi
->s_locality_groups
, raw_smp_processor_id());
4205 /* we're going to use group allocation */
4206 ac
->ac_flags
|= EXT4_MB_HINT_GROUP_ALLOC
;
4208 /* serialize all allocations in the group */
4209 mutex_lock(&ac
->ac_lg
->lg_mutex
);
4212 static noinline_for_stack
int
4213 ext4_mb_initialize_context(struct ext4_allocation_context
*ac
,
4214 struct ext4_allocation_request
*ar
)
4216 struct super_block
*sb
= ar
->inode
->i_sb
;
4217 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4218 struct ext4_super_block
*es
= sbi
->s_es
;
4222 ext4_grpblk_t block
;
4224 /* we can't allocate > group size */
4227 /* just a dirty hack to filter too big requests */
4228 if (len
>= EXT4_BLOCKS_PER_GROUP(sb
) - 10)
4229 len
= EXT4_BLOCKS_PER_GROUP(sb
) - 10;
4231 /* start searching from the goal */
4233 if (goal
< le32_to_cpu(es
->s_first_data_block
) ||
4234 goal
>= ext4_blocks_count(es
))
4235 goal
= le32_to_cpu(es
->s_first_data_block
);
4236 ext4_get_group_no_and_offset(sb
, goal
, &group
, &block
);
4238 /* set up allocation goals */
4239 memset(ac
, 0, sizeof(struct ext4_allocation_context
));
4240 ac
->ac_b_ex
.fe_logical
= ar
->logical
;
4241 ac
->ac_status
= AC_STATUS_CONTINUE
;
4243 ac
->ac_inode
= ar
->inode
;
4244 ac
->ac_o_ex
.fe_logical
= ar
->logical
;
4245 ac
->ac_o_ex
.fe_group
= group
;
4246 ac
->ac_o_ex
.fe_start
= block
;
4247 ac
->ac_o_ex
.fe_len
= len
;
4248 ac
->ac_g_ex
.fe_logical
= ar
->logical
;
4249 ac
->ac_g_ex
.fe_group
= group
;
4250 ac
->ac_g_ex
.fe_start
= block
;
4251 ac
->ac_g_ex
.fe_len
= len
;
4252 ac
->ac_flags
= ar
->flags
;
4254 /* we have to define context: we'll we work with a file or
4255 * locality group. this is a policy, actually */
4256 ext4_mb_group_or_file(ac
);
4258 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
4259 "left: %u/%u, right %u/%u to %swritable\n",
4260 (unsigned) ar
->len
, (unsigned) ar
->logical
,
4261 (unsigned) ar
->goal
, ac
->ac_flags
, ac
->ac_2order
,
4262 (unsigned) ar
->lleft
, (unsigned) ar
->pleft
,
4263 (unsigned) ar
->lright
, (unsigned) ar
->pright
,
4264 atomic_read(&ar
->inode
->i_writecount
) ? "" : "non-");
4269 static noinline_for_stack
void
4270 ext4_mb_discard_lg_preallocations(struct super_block
*sb
,
4271 struct ext4_locality_group
*lg
,
4272 int order
, int total_entries
)
4274 ext4_group_t group
= 0;
4275 struct ext4_buddy e4b
;
4276 struct list_head discard_list
;
4277 struct ext4_prealloc_space
*pa
, *tmp
;
4278 struct ext4_allocation_context
*ac
;
4280 mb_debug(1, "discard locality group preallocation\n");
4282 INIT_LIST_HEAD(&discard_list
);
4283 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4287 spin_lock(&lg
->lg_prealloc_lock
);
4288 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[order
],
4290 spin_lock(&pa
->pa_lock
);
4291 if (atomic_read(&pa
->pa_count
)) {
4293 * This is the pa that we just used
4294 * for block allocation. So don't
4297 spin_unlock(&pa
->pa_lock
);
4300 if (pa
->pa_deleted
) {
4301 spin_unlock(&pa
->pa_lock
);
4304 /* only lg prealloc space */
4305 BUG_ON(pa
->pa_type
!= MB_GROUP_PA
);
4307 /* seems this one can be freed ... */
4309 spin_unlock(&pa
->pa_lock
);
4311 list_del_rcu(&pa
->pa_inode_list
);
4312 list_add(&pa
->u
.pa_tmp_list
, &discard_list
);
4315 if (total_entries
<= 5) {
4317 * we want to keep only 5 entries
4318 * allowing it to grow to 8. This
4319 * mak sure we don't call discard
4320 * soon for this list.
4325 spin_unlock(&lg
->lg_prealloc_lock
);
4327 list_for_each_entry_safe(pa
, tmp
, &discard_list
, u
.pa_tmp_list
) {
4329 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
4330 if (ext4_mb_load_buddy(sb
, group
, &e4b
)) {
4331 ext4_error(sb
, __func__
, "Error in loading buddy "
4332 "information for %u", group
);
4335 ext4_lock_group(sb
, group
);
4336 list_del(&pa
->pa_group_list
);
4337 ext4_mb_release_group_pa(&e4b
, pa
, ac
);
4338 ext4_unlock_group(sb
, group
);
4340 ext4_mb_release_desc(&e4b
);
4341 list_del(&pa
->u
.pa_tmp_list
);
4342 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4345 kmem_cache_free(ext4_ac_cachep
, ac
);
4349 * We have incremented pa_count. So it cannot be freed at this
4350 * point. Also we hold lg_mutex. So no parallel allocation is
4351 * possible from this lg. That means pa_free cannot be updated.
4353 * A parallel ext4_mb_discard_group_preallocations is possible.
4354 * which can cause the lg_prealloc_list to be updated.
4357 static void ext4_mb_add_n_trim(struct ext4_allocation_context
*ac
)
4359 int order
, added
= 0, lg_prealloc_count
= 1;
4360 struct super_block
*sb
= ac
->ac_sb
;
4361 struct ext4_locality_group
*lg
= ac
->ac_lg
;
4362 struct ext4_prealloc_space
*tmp_pa
, *pa
= ac
->ac_pa
;
4364 order
= fls(pa
->pa_free
) - 1;
4365 if (order
> PREALLOC_TB_SIZE
- 1)
4366 /* The max size of hash table is PREALLOC_TB_SIZE */
4367 order
= PREALLOC_TB_SIZE
- 1;
4368 /* Add the prealloc space to lg */
4370 list_for_each_entry_rcu(tmp_pa
, &lg
->lg_prealloc_list
[order
],
4372 spin_lock(&tmp_pa
->pa_lock
);
4373 if (tmp_pa
->pa_deleted
) {
4374 spin_unlock(&tmp_pa
->pa_lock
);
4377 if (!added
&& pa
->pa_free
< tmp_pa
->pa_free
) {
4378 /* Add to the tail of the previous entry */
4379 list_add_tail_rcu(&pa
->pa_inode_list
,
4380 &tmp_pa
->pa_inode_list
);
4383 * we want to count the total
4384 * number of entries in the list
4387 spin_unlock(&tmp_pa
->pa_lock
);
4388 lg_prealloc_count
++;
4391 list_add_tail_rcu(&pa
->pa_inode_list
,
4392 &lg
->lg_prealloc_list
[order
]);
4395 /* Now trim the list to be not more than 8 elements */
4396 if (lg_prealloc_count
> 8) {
4397 ext4_mb_discard_lg_preallocations(sb
, lg
,
4398 order
, lg_prealloc_count
);
4405 * release all resource we used in allocation
4407 static int ext4_mb_release_context(struct ext4_allocation_context
*ac
)
4409 struct ext4_prealloc_space
*pa
= ac
->ac_pa
;
4411 if (pa
->pa_type
== MB_GROUP_PA
) {
4412 /* see comment in ext4_mb_use_group_pa() */
4413 spin_lock(&pa
->pa_lock
);
4414 pa
->pa_pstart
+= ac
->ac_b_ex
.fe_len
;
4415 pa
->pa_lstart
+= ac
->ac_b_ex
.fe_len
;
4416 pa
->pa_free
-= ac
->ac_b_ex
.fe_len
;
4417 pa
->pa_len
-= ac
->ac_b_ex
.fe_len
;
4418 spin_unlock(&pa
->pa_lock
);
4422 up_read(ac
->alloc_semp
);
4425 * We want to add the pa to the right bucket.
4426 * Remove it from the list and while adding
4427 * make sure the list to which we are adding
4428 * doesn't grow big. We need to release
4429 * alloc_semp before calling ext4_mb_add_n_trim()
4431 if ((pa
->pa_type
== MB_GROUP_PA
) && likely(pa
->pa_free
)) {
4432 spin_lock(pa
->pa_obj_lock
);
4433 list_del_rcu(&pa
->pa_inode_list
);
4434 spin_unlock(pa
->pa_obj_lock
);
4435 ext4_mb_add_n_trim(ac
);
4437 ext4_mb_put_pa(ac
, ac
->ac_sb
, pa
);
4439 if (ac
->ac_bitmap_page
)
4440 page_cache_release(ac
->ac_bitmap_page
);
4441 if (ac
->ac_buddy_page
)
4442 page_cache_release(ac
->ac_buddy_page
);
4443 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
4444 mutex_unlock(&ac
->ac_lg
->lg_mutex
);
4445 ext4_mb_collect_stats(ac
);
4449 static int ext4_mb_discard_preallocations(struct super_block
*sb
, int needed
)
4451 ext4_group_t i
, ngroups
= ext4_get_groups_count(sb
);
4455 trace_ext4_mb_discard_preallocations(sb
, needed
);
4456 for (i
= 0; i
< ngroups
&& needed
> 0; i
++) {
4457 ret
= ext4_mb_discard_group_preallocations(sb
, i
, needed
);
4466 * Main entry point into mballoc to allocate blocks
4467 * it tries to use preallocation first, then falls back
4468 * to usual allocation
4470 ext4_fsblk_t
ext4_mb_new_blocks(handle_t
*handle
,
4471 struct ext4_allocation_request
*ar
, int *errp
)
4474 struct ext4_allocation_context
*ac
= NULL
;
4475 struct ext4_sb_info
*sbi
;
4476 struct super_block
*sb
;
4477 ext4_fsblk_t block
= 0;
4478 unsigned int inquota
= 0;
4479 unsigned int reserv_blks
= 0;
4481 sb
= ar
->inode
->i_sb
;
4484 trace_ext4_request_blocks(ar
);
4487 * For delayed allocation, we could skip the ENOSPC and
4488 * EDQUOT check, as blocks and quotas have been already
4489 * reserved when data being copied into pagecache.
4491 if (EXT4_I(ar
->inode
)->i_delalloc_reserved_flag
)
4492 ar
->flags
|= EXT4_MB_DELALLOC_RESERVED
;
4494 /* Without delayed allocation we need to verify
4495 * there is enough free blocks to do block allocation
4496 * and verify allocation doesn't exceed the quota limits.
4498 while (ar
->len
&& ext4_claim_free_blocks(sbi
, ar
->len
)) {
4499 /* let others to free the space */
4501 ar
->len
= ar
->len
>> 1;
4507 reserv_blks
= ar
->len
;
4508 while (ar
->len
&& vfs_dq_alloc_block(ar
->inode
, ar
->len
)) {
4509 ar
->flags
|= EXT4_MB_HINT_NOPREALLOC
;
4519 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4526 *errp
= ext4_mb_initialize_context(ac
, ar
);
4532 ac
->ac_op
= EXT4_MB_HISTORY_PREALLOC
;
4533 if (!ext4_mb_use_preallocated(ac
)) {
4534 ac
->ac_op
= EXT4_MB_HISTORY_ALLOC
;
4535 ext4_mb_normalize_request(ac
, ar
);
4537 /* allocate space in core */
4538 ext4_mb_regular_allocator(ac
);
4540 /* as we've just preallocated more space than
4541 * user requested orinally, we store allocated
4542 * space in a special descriptor */
4543 if (ac
->ac_status
== AC_STATUS_FOUND
&&
4544 ac
->ac_o_ex
.fe_len
< ac
->ac_b_ex
.fe_len
)
4545 ext4_mb_new_preallocation(ac
);
4547 if (likely(ac
->ac_status
== AC_STATUS_FOUND
)) {
4548 *errp
= ext4_mb_mark_diskspace_used(ac
, handle
, reserv_blks
);
4549 if (*errp
== -EAGAIN
) {
4551 * drop the reference that we took
4552 * in ext4_mb_use_best_found
4554 ext4_mb_release_context(ac
);
4555 ac
->ac_b_ex
.fe_group
= 0;
4556 ac
->ac_b_ex
.fe_start
= 0;
4557 ac
->ac_b_ex
.fe_len
= 0;
4558 ac
->ac_status
= AC_STATUS_CONTINUE
;
4561 ac
->ac_b_ex
.fe_len
= 0;
4563 ext4_mb_show_ac(ac
);
4565 block
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
4566 ar
->len
= ac
->ac_b_ex
.fe_len
;
4569 freed
= ext4_mb_discard_preallocations(sb
, ac
->ac_o_ex
.fe_len
);
4573 ac
->ac_b_ex
.fe_len
= 0;
4575 ext4_mb_show_ac(ac
);
4578 ext4_mb_release_context(ac
);
4581 kmem_cache_free(ext4_ac_cachep
, ac
);
4583 if (inquota
&& ar
->len
< inquota
)
4584 vfs_dq_free_block(ar
->inode
, inquota
- ar
->len
);
4587 if (!EXT4_I(ar
->inode
)->i_delalloc_reserved_flag
)
4588 /* release all the reserved blocks if non delalloc */
4589 percpu_counter_sub(&sbi
->s_dirtyblocks_counter
,
4593 trace_ext4_allocate_blocks(ar
, (unsigned long long)block
);
4599 * We can merge two free data extents only if the physical blocks
4600 * are contiguous, AND the extents were freed by the same transaction,
4601 * AND the blocks are associated with the same group.
4603 static int can_merge(struct ext4_free_data
*entry1
,
4604 struct ext4_free_data
*entry2
)
4606 if ((entry1
->t_tid
== entry2
->t_tid
) &&
4607 (entry1
->group
== entry2
->group
) &&
4608 ((entry1
->start_blk
+ entry1
->count
) == entry2
->start_blk
))
4613 static noinline_for_stack
int
4614 ext4_mb_free_metadata(handle_t
*handle
, struct ext4_buddy
*e4b
,
4615 struct ext4_free_data
*new_entry
)
4617 ext4_grpblk_t block
;
4618 struct ext4_free_data
*entry
;
4619 struct ext4_group_info
*db
= e4b
->bd_info
;
4620 struct super_block
*sb
= e4b
->bd_sb
;
4621 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4622 struct rb_node
**n
= &db
->bb_free_root
.rb_node
, *node
;
4623 struct rb_node
*parent
= NULL
, *new_node
;
4625 BUG_ON(!ext4_handle_valid(handle
));
4626 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
4627 BUG_ON(e4b
->bd_buddy_page
== NULL
);
4629 new_node
= &new_entry
->node
;
4630 block
= new_entry
->start_blk
;
4633 /* first free block exent. We need to
4634 protect buddy cache from being freed,
4635 * otherwise we'll refresh it from
4636 * on-disk bitmap and lose not-yet-available
4638 page_cache_get(e4b
->bd_buddy_page
);
4639 page_cache_get(e4b
->bd_bitmap_page
);
4643 entry
= rb_entry(parent
, struct ext4_free_data
, node
);
4644 if (block
< entry
->start_blk
)
4646 else if (block
>= (entry
->start_blk
+ entry
->count
))
4647 n
= &(*n
)->rb_right
;
4649 ext4_grp_locked_error(sb
, e4b
->bd_group
, __func__
,
4650 "Double free of blocks %d (%d %d)",
4651 block
, entry
->start_blk
, entry
->count
);
4656 rb_link_node(new_node
, parent
, n
);
4657 rb_insert_color(new_node
, &db
->bb_free_root
);
4659 /* Now try to see the extent can be merged to left and right */
4660 node
= rb_prev(new_node
);
4662 entry
= rb_entry(node
, struct ext4_free_data
, node
);
4663 if (can_merge(entry
, new_entry
)) {
4664 new_entry
->start_blk
= entry
->start_blk
;
4665 new_entry
->count
+= entry
->count
;
4666 rb_erase(node
, &(db
->bb_free_root
));
4667 spin_lock(&sbi
->s_md_lock
);
4668 list_del(&entry
->list
);
4669 spin_unlock(&sbi
->s_md_lock
);
4670 kmem_cache_free(ext4_free_ext_cachep
, entry
);
4674 node
= rb_next(new_node
);
4676 entry
= rb_entry(node
, struct ext4_free_data
, node
);
4677 if (can_merge(new_entry
, entry
)) {
4678 new_entry
->count
+= entry
->count
;
4679 rb_erase(node
, &(db
->bb_free_root
));
4680 spin_lock(&sbi
->s_md_lock
);
4681 list_del(&entry
->list
);
4682 spin_unlock(&sbi
->s_md_lock
);
4683 kmem_cache_free(ext4_free_ext_cachep
, entry
);
4686 /* Add the extent to transaction's private list */
4687 spin_lock(&sbi
->s_md_lock
);
4688 list_add(&new_entry
->list
, &handle
->h_transaction
->t_private_list
);
4689 spin_unlock(&sbi
->s_md_lock
);
4694 * Main entry point into mballoc to free blocks
4696 void ext4_mb_free_blocks(handle_t
*handle
, struct inode
*inode
,
4697 ext4_fsblk_t block
, unsigned long count
,
4698 int metadata
, unsigned long *freed
)
4700 struct buffer_head
*bitmap_bh
= NULL
;
4701 struct super_block
*sb
= inode
->i_sb
;
4702 struct ext4_allocation_context
*ac
= NULL
;
4703 struct ext4_group_desc
*gdp
;
4704 struct ext4_super_block
*es
;
4705 unsigned int overflow
;
4707 struct buffer_head
*gd_bh
;
4708 ext4_group_t block_group
;
4709 struct ext4_sb_info
*sbi
;
4710 struct ext4_buddy e4b
;
4717 es
= EXT4_SB(sb
)->s_es
;
4718 if (block
< le32_to_cpu(es
->s_first_data_block
) ||
4719 block
+ count
< block
||
4720 block
+ count
> ext4_blocks_count(es
)) {
4721 ext4_error(sb
, __func__
,
4722 "Freeing blocks not in datazone - "
4723 "block = %llu, count = %lu", block
, count
);
4727 ext4_debug("freeing block %llu\n", block
);
4728 trace_ext4_free_blocks(inode
, block
, count
, metadata
);
4730 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4732 ac
->ac_op
= EXT4_MB_HISTORY_FREE
;
4733 ac
->ac_inode
= inode
;
4739 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
4742 * Check to see if we are freeing blocks across a group
4745 if (bit
+ count
> EXT4_BLOCKS_PER_GROUP(sb
)) {
4746 overflow
= bit
+ count
- EXT4_BLOCKS_PER_GROUP(sb
);
4749 bitmap_bh
= ext4_read_block_bitmap(sb
, block_group
);
4754 gdp
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
4760 if (in_range(ext4_block_bitmap(sb
, gdp
), block
, count
) ||
4761 in_range(ext4_inode_bitmap(sb
, gdp
), block
, count
) ||
4762 in_range(block
, ext4_inode_table(sb
, gdp
),
4763 EXT4_SB(sb
)->s_itb_per_group
) ||
4764 in_range(block
+ count
- 1, ext4_inode_table(sb
, gdp
),
4765 EXT4_SB(sb
)->s_itb_per_group
)) {
4767 ext4_error(sb
, __func__
,
4768 "Freeing blocks in system zone - "
4769 "Block = %llu, count = %lu", block
, count
);
4770 /* err = 0. ext4_std_error should be a no op */
4774 BUFFER_TRACE(bitmap_bh
, "getting write access");
4775 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
4780 * We are about to modify some metadata. Call the journal APIs
4781 * to unshare ->b_data if a currently-committing transaction is
4784 BUFFER_TRACE(gd_bh
, "get_write_access");
4785 err
= ext4_journal_get_write_access(handle
, gd_bh
);
4788 #ifdef AGGRESSIVE_CHECK
4791 for (i
= 0; i
< count
; i
++)
4792 BUG_ON(!mb_test_bit(bit
+ i
, bitmap_bh
->b_data
));
4796 ac
->ac_b_ex
.fe_group
= block_group
;
4797 ac
->ac_b_ex
.fe_start
= bit
;
4798 ac
->ac_b_ex
.fe_len
= count
;
4799 ext4_mb_store_history(ac
);
4802 err
= ext4_mb_load_buddy(sb
, block_group
, &e4b
);
4805 if (metadata
&& ext4_handle_valid(handle
)) {
4806 struct ext4_free_data
*new_entry
;
4808 * blocks being freed are metadata. these blocks shouldn't
4809 * be used until this transaction is committed
4811 new_entry
= kmem_cache_alloc(ext4_free_ext_cachep
, GFP_NOFS
);
4812 new_entry
->start_blk
= bit
;
4813 new_entry
->group
= block_group
;
4814 new_entry
->count
= count
;
4815 new_entry
->t_tid
= handle
->h_transaction
->t_tid
;
4817 ext4_lock_group(sb
, block_group
);
4818 mb_clear_bits(bitmap_bh
->b_data
, bit
, count
);
4819 ext4_mb_free_metadata(handle
, &e4b
, new_entry
);
4821 /* need to update group_info->bb_free and bitmap
4822 * with group lock held. generate_buddy look at
4823 * them with group lock_held
4825 ext4_lock_group(sb
, block_group
);
4826 mb_clear_bits(bitmap_bh
->b_data
, bit
, count
);
4827 mb_free_blocks(inode
, &e4b
, bit
, count
);
4828 ext4_mb_return_to_preallocation(inode
, &e4b
, block
, count
);
4831 ret
= ext4_free_blks_count(sb
, gdp
) + count
;
4832 ext4_free_blks_set(sb
, gdp
, ret
);
4833 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, block_group
, gdp
);
4834 ext4_unlock_group(sb
, block_group
);
4835 percpu_counter_add(&sbi
->s_freeblocks_counter
, count
);
4837 if (sbi
->s_log_groups_per_flex
) {
4838 ext4_group_t flex_group
= ext4_flex_group(sbi
, block_group
);
4839 atomic_add(count
, &sbi
->s_flex_groups
[flex_group
].free_blocks
);
4842 ext4_mb_release_desc(&e4b
);
4846 /* We dirtied the bitmap block */
4847 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
4848 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
4850 /* And the group descriptor block */
4851 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
4852 ret
= ext4_handle_dirty_metadata(handle
, NULL
, gd_bh
);
4856 if (overflow
&& !err
) {
4865 ext4_std_error(sb
, err
);
4867 kmem_cache_free(ext4_ac_cachep
, ac
);