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 <trace/events/ext4.h>
29 * - test ext4_ext_search_left() and ext4_ext_search_right()
30 * - search for metadata in few groups
33 * - normalization should take into account whether file is still open
34 * - discard preallocations if no free space left (policy?)
35 * - don't normalize tails
37 * - reservation for superuser
40 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
41 * - track min/max extents in each group for better group selection
42 * - mb_mark_used() may allocate chunk right after splitting buddy
43 * - tree of groups sorted by number of free blocks
48 * The allocation request involve request for multiple number of blocks
49 * near to the goal(block) value specified.
51 * During initialization phase of the allocator we decide to use the
52 * group preallocation or inode preallocation depending on the size of
53 * the file. The size of the file could be the resulting file size we
54 * would have after allocation, or the current file size, which ever
55 * is larger. If the size is less than sbi->s_mb_stream_request we
56 * select to use the group preallocation. The default value of
57 * s_mb_stream_request is 16 blocks. This can also be tuned via
58 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
59 * terms of number of blocks.
61 * The main motivation for having small file use group preallocation is to
62 * ensure that we have small files closer together on the disk.
64 * First stage the allocator looks at the inode prealloc list,
65 * ext4_inode_info->i_prealloc_list, which contains list of prealloc
66 * spaces for this particular inode. The inode prealloc space is
69 * pa_lstart -> the logical start block for this prealloc space
70 * pa_pstart -> the physical start block for this prealloc space
71 * pa_len -> lenght for this prealloc space
72 * pa_free -> free space available in this prealloc space
74 * The inode preallocation space is used looking at the _logical_ start
75 * block. If only the logical file block falls within the range of prealloc
76 * space we will consume the particular prealloc space. This make sure that
77 * that the we have contiguous physical blocks representing the file blocks
79 * The important thing to be noted in case of inode prealloc space is that
80 * we don't modify the values associated to inode prealloc space except
83 * If we are not able to find blocks in the inode prealloc space and if we
84 * have the group allocation flag set then we look at the locality group
85 * prealloc space. These are per CPU prealloc list repreasented as
87 * ext4_sb_info.s_locality_groups[smp_processor_id()]
89 * The reason for having a per cpu locality group is to reduce the contention
90 * between CPUs. It is possible to get scheduled at this point.
92 * The locality group prealloc space is used looking at whether we have
93 * enough free space (pa_free) withing the prealloc space.
95 * If we can't allocate blocks via inode prealloc or/and locality group
96 * prealloc then we look at the buddy cache. The buddy cache is represented
97 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
98 * mapped to the buddy and bitmap information regarding different
99 * groups. The buddy information is attached to buddy cache inode so that
100 * we can access them through the page cache. The information regarding
101 * each group is loaded via ext4_mb_load_buddy. The information involve
102 * block bitmap and buddy information. The information are stored in the
106 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
109 * one block each for bitmap and buddy information. So for each group we
110 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
111 * blocksize) blocks. So it can have information regarding groups_per_page
112 * which is blocks_per_page/2
114 * The buddy cache inode is not stored on disk. The inode is thrown
115 * away when the filesystem is unmounted.
117 * We look for count number of blocks in the buddy cache. If we were able
118 * to locate that many free blocks we return with additional information
119 * regarding rest of the contiguous physical block available
121 * Before allocating blocks via buddy cache we normalize the request
122 * blocks. This ensure we ask for more blocks that we needed. The extra
123 * blocks that we get after allocation is added to the respective prealloc
124 * list. In case of inode preallocation we follow a list of heuristics
125 * based on file size. This can be found in ext4_mb_normalize_request. If
126 * we are doing a group prealloc we try to normalize the request to
127 * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is
128 * 512 blocks. This can be tuned via
129 * /sys/fs/ext4/<partition/mb_group_prealloc. The value is represented in
130 * terms of number of blocks. If we have mounted the file system with -O
131 * stripe=<value> option the group prealloc request is normalized to the
132 * stripe value (sbi->s_stripe)
134 * The regular allocator(using the buddy cache) supports few tunables.
136 * /sys/fs/ext4/<partition>/mb_min_to_scan
137 * /sys/fs/ext4/<partition>/mb_max_to_scan
138 * /sys/fs/ext4/<partition>/mb_order2_req
140 * The regular allocator uses buddy scan only if the request len is power of
141 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
142 * value of s_mb_order2_reqs can be tuned via
143 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
144 * stripe size (sbi->s_stripe), we try to search for contigous block in
145 * stripe size. This should result in better allocation on RAID setups. If
146 * not, we search in the specific group using bitmap for best extents. The
147 * tunable min_to_scan and max_to_scan control the behaviour here.
148 * min_to_scan indicate how long the mballoc __must__ look for a best
149 * extent and max_to_scan indicates how long the mballoc __can__ look for a
150 * best extent in the found extents. Searching for the blocks starts with
151 * the group specified as the goal value in allocation context via
152 * ac_g_ex. Each group is first checked based on the criteria whether it
153 * can used for allocation. ext4_mb_good_group explains how the groups are
156 * Both the prealloc space are getting populated as above. So for the first
157 * request we will hit the buddy cache which will result in this prealloc
158 * space getting filled. The prealloc space is then later used for the
159 * subsequent request.
163 * mballoc operates on the following data:
165 * - in-core buddy (actually includes buddy and bitmap)
166 * - preallocation descriptors (PAs)
168 * there are two types of preallocations:
170 * assiged to specific inode and can be used for this inode only.
171 * it describes part of inode's space preallocated to specific
172 * physical blocks. any block from that preallocated can be used
173 * independent. the descriptor just tracks number of blocks left
174 * unused. so, before taking some block from descriptor, one must
175 * make sure corresponded logical block isn't allocated yet. this
176 * also means that freeing any block within descriptor's range
177 * must discard all preallocated blocks.
179 * assigned to specific locality group which does not translate to
180 * permanent set of inodes: inode can join and leave group. space
181 * from this type of preallocation can be used for any inode. thus
182 * it's consumed from the beginning to the end.
184 * relation between them can be expressed as:
185 * in-core buddy = on-disk bitmap + preallocation descriptors
187 * this mean blocks mballoc considers used are:
188 * - allocated blocks (persistent)
189 * - preallocated blocks (non-persistent)
191 * consistency in mballoc world means that at any time a block is either
192 * free or used in ALL structures. notice: "any time" should not be read
193 * literally -- time is discrete and delimited by locks.
195 * to keep it simple, we don't use block numbers, instead we count number of
196 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
198 * all operations can be expressed as:
199 * - init buddy: buddy = on-disk + PAs
200 * - new PA: buddy += N; PA = N
201 * - use inode PA: on-disk += N; PA -= N
202 * - discard inode PA buddy -= on-disk - PA; PA = 0
203 * - use locality group PA on-disk += N; PA -= N
204 * - discard locality group PA buddy -= PA; PA = 0
205 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
206 * is used in real operation because we can't know actual used
207 * bits from PA, only from on-disk bitmap
209 * if we follow this strict logic, then all operations above should be atomic.
210 * given some of them can block, we'd have to use something like semaphores
211 * killing performance on high-end SMP hardware. let's try to relax it using
212 * the following knowledge:
213 * 1) if buddy is referenced, it's already initialized
214 * 2) while block is used in buddy and the buddy is referenced,
215 * nobody can re-allocate that block
216 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
217 * bit set and PA claims same block, it's OK. IOW, one can set bit in
218 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
221 * so, now we're building a concurrency table:
224 * blocks for PA are allocated in the buddy, buddy must be referenced
225 * until PA is linked to allocation group to avoid concurrent buddy init
227 * we need to make sure that either on-disk bitmap or PA has uptodate data
228 * given (3) we care that PA-=N operation doesn't interfere with init
230 * the simplest way would be to have buddy initialized by the discard
231 * - use locality group PA
232 * again PA-=N must be serialized with init
233 * - discard locality group PA
234 * the simplest way would be to have buddy initialized by the discard
237 * i_data_sem serializes them
239 * discard process must wait until PA isn't used by another process
240 * - use locality group PA
241 * some mutex should serialize them
242 * - discard locality group PA
243 * discard process must wait until PA isn't used by another process
246 * i_data_sem or another mutex should serializes them
248 * discard process must wait until PA isn't used by another process
249 * - use locality group PA
250 * nothing wrong here -- they're different PAs covering different blocks
251 * - discard locality group PA
252 * discard process must wait until PA isn't used by another process
254 * now we're ready to make few consequences:
255 * - PA is referenced and while it is no discard is possible
256 * - PA is referenced until block isn't marked in on-disk bitmap
257 * - PA changes only after on-disk bitmap
258 * - discard must not compete with init. either init is done before
259 * any discard or they're serialized somehow
260 * - buddy init as sum of on-disk bitmap and PAs is done atomically
262 * a special case when we've used PA to emptiness. no need to modify buddy
263 * in this case, but we should care about concurrent init
268 * Logic in few words:
273 * mark bits in on-disk bitmap
276 * - use preallocation:
277 * find proper PA (per-inode or group)
279 * mark bits in on-disk bitmap
285 * mark bits in on-disk bitmap
288 * - discard preallocations in group:
290 * move them onto local list
291 * load on-disk bitmap
293 * remove PA from object (inode or locality group)
294 * mark free blocks in-core
296 * - discard inode's preallocations:
303 * - bitlock on a group (group)
304 * - object (inode/locality) (object)
315 * - release consumed pa:
320 * - generate in-core bitmap:
324 * - discard all for given object (inode, locality group):
329 * - discard all for given group:
336 static struct kmem_cache
*ext4_pspace_cachep
;
337 static struct kmem_cache
*ext4_ac_cachep
;
338 static struct kmem_cache
*ext4_free_ext_cachep
;
339 static void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
341 static void ext4_mb_generate_from_freelist(struct super_block
*sb
, void *bitmap
,
343 static void release_blocks_on_commit(journal_t
*journal
, transaction_t
*txn
);
345 static inline void *mb_correct_addr_and_bit(int *bit
, void *addr
)
347 #if BITS_PER_LONG == 64
348 *bit
+= ((unsigned long) addr
& 7UL) << 3;
349 addr
= (void *) ((unsigned long) addr
& ~7UL);
350 #elif BITS_PER_LONG == 32
351 *bit
+= ((unsigned long) addr
& 3UL) << 3;
352 addr
= (void *) ((unsigned long) addr
& ~3UL);
354 #error "how many bits you are?!"
359 static inline int mb_test_bit(int bit
, void *addr
)
362 * ext4_test_bit on architecture like powerpc
363 * needs unsigned long aligned address
365 addr
= mb_correct_addr_and_bit(&bit
, addr
);
366 return ext4_test_bit(bit
, addr
);
369 static inline void mb_set_bit(int bit
, void *addr
)
371 addr
= mb_correct_addr_and_bit(&bit
, addr
);
372 ext4_set_bit(bit
, addr
);
375 static inline void mb_clear_bit(int bit
, void *addr
)
377 addr
= mb_correct_addr_and_bit(&bit
, addr
);
378 ext4_clear_bit(bit
, addr
);
381 static inline int mb_find_next_zero_bit(void *addr
, int max
, int start
)
383 int fix
= 0, ret
, tmpmax
;
384 addr
= mb_correct_addr_and_bit(&fix
, addr
);
388 ret
= ext4_find_next_zero_bit(addr
, tmpmax
, start
) - fix
;
394 static inline int mb_find_next_bit(void *addr
, int max
, int start
)
396 int fix
= 0, ret
, tmpmax
;
397 addr
= mb_correct_addr_and_bit(&fix
, addr
);
401 ret
= ext4_find_next_bit(addr
, tmpmax
, start
) - fix
;
407 static void *mb_find_buddy(struct ext4_buddy
*e4b
, int order
, int *max
)
411 BUG_ON(EXT4_MB_BITMAP(e4b
) == EXT4_MB_BUDDY(e4b
));
414 if (order
> e4b
->bd_blkbits
+ 1) {
419 /* at order 0 we see each particular block */
420 *max
= 1 << (e4b
->bd_blkbits
+ 3);
422 return EXT4_MB_BITMAP(e4b
);
424 bb
= EXT4_MB_BUDDY(e4b
) + EXT4_SB(e4b
->bd_sb
)->s_mb_offsets
[order
];
425 *max
= EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[order
];
431 static void mb_free_blocks_double(struct inode
*inode
, struct ext4_buddy
*e4b
,
432 int first
, int count
)
435 struct super_block
*sb
= e4b
->bd_sb
;
437 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
439 assert_spin_locked(ext4_group_lock_ptr(sb
, e4b
->bd_group
));
440 for (i
= 0; i
< count
; i
++) {
441 if (!mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
)) {
442 ext4_fsblk_t blocknr
;
443 blocknr
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
);
444 blocknr
+= first
+ i
;
446 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
447 ext4_grp_locked_error(sb
, e4b
->bd_group
,
448 __func__
, "double-free of inode"
449 " %lu's block %llu(bit %u in group %u)",
450 inode
? inode
->i_ino
: 0, blocknr
,
451 first
+ i
, e4b
->bd_group
);
453 mb_clear_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
457 static void mb_mark_used_double(struct ext4_buddy
*e4b
, int first
, int count
)
461 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
463 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
464 for (i
= 0; i
< count
; i
++) {
465 BUG_ON(mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
));
466 mb_set_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
470 static void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
472 if (memcmp(e4b
->bd_info
->bb_bitmap
, bitmap
, e4b
->bd_sb
->s_blocksize
)) {
473 unsigned char *b1
, *b2
;
475 b1
= (unsigned char *) e4b
->bd_info
->bb_bitmap
;
476 b2
= (unsigned char *) bitmap
;
477 for (i
= 0; i
< e4b
->bd_sb
->s_blocksize
; i
++) {
478 if (b1
[i
] != b2
[i
]) {
479 printk(KERN_ERR
"corruption in group %u "
480 "at byte %u(%u): %x in copy != %x "
481 "on disk/prealloc\n",
482 e4b
->bd_group
, i
, i
* 8, b1
[i
], b2
[i
]);
490 static inline void mb_free_blocks_double(struct inode
*inode
,
491 struct ext4_buddy
*e4b
, int first
, int count
)
495 static inline void mb_mark_used_double(struct ext4_buddy
*e4b
,
496 int first
, int count
)
500 static inline void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
506 #ifdef AGGRESSIVE_CHECK
508 #define MB_CHECK_ASSERT(assert) \
512 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
513 function, file, line, # assert); \
518 static int __mb_check_buddy(struct ext4_buddy
*e4b
, char *file
,
519 const char *function
, int line
)
521 struct super_block
*sb
= e4b
->bd_sb
;
522 int order
= e4b
->bd_blkbits
+ 1;
529 struct ext4_group_info
*grp
;
532 struct list_head
*cur
;
537 static int mb_check_counter
;
538 if (mb_check_counter
++ % 100 != 0)
543 buddy
= mb_find_buddy(e4b
, order
, &max
);
544 MB_CHECK_ASSERT(buddy
);
545 buddy2
= mb_find_buddy(e4b
, order
- 1, &max2
);
546 MB_CHECK_ASSERT(buddy2
);
547 MB_CHECK_ASSERT(buddy
!= buddy2
);
548 MB_CHECK_ASSERT(max
* 2 == max2
);
551 for (i
= 0; i
< max
; i
++) {
553 if (mb_test_bit(i
, buddy
)) {
554 /* only single bit in buddy2 may be 1 */
555 if (!mb_test_bit(i
<< 1, buddy2
)) {
557 mb_test_bit((i
<<1)+1, buddy2
));
558 } else if (!mb_test_bit((i
<< 1) + 1, buddy2
)) {
560 mb_test_bit(i
<< 1, buddy2
));
565 /* both bits in buddy2 must be 0 */
566 MB_CHECK_ASSERT(mb_test_bit(i
<< 1, buddy2
));
567 MB_CHECK_ASSERT(mb_test_bit((i
<< 1) + 1, buddy2
));
569 for (j
= 0; j
< (1 << order
); j
++) {
570 k
= (i
* (1 << order
)) + j
;
572 !mb_test_bit(k
, EXT4_MB_BITMAP(e4b
)));
576 MB_CHECK_ASSERT(e4b
->bd_info
->bb_counters
[order
] == count
);
581 buddy
= mb_find_buddy(e4b
, 0, &max
);
582 for (i
= 0; i
< max
; i
++) {
583 if (!mb_test_bit(i
, buddy
)) {
584 MB_CHECK_ASSERT(i
>= e4b
->bd_info
->bb_first_free
);
592 /* check used bits only */
593 for (j
= 0; j
< e4b
->bd_blkbits
+ 1; j
++) {
594 buddy2
= mb_find_buddy(e4b
, j
, &max2
);
596 MB_CHECK_ASSERT(k
< max2
);
597 MB_CHECK_ASSERT(mb_test_bit(k
, buddy2
));
600 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b
->bd_info
));
601 MB_CHECK_ASSERT(e4b
->bd_info
->bb_fragments
== fragments
);
603 grp
= ext4_get_group_info(sb
, e4b
->bd_group
);
604 buddy
= mb_find_buddy(e4b
, 0, &max
);
605 list_for_each(cur
, &grp
->bb_prealloc_list
) {
606 ext4_group_t groupnr
;
607 struct ext4_prealloc_space
*pa
;
608 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
609 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &groupnr
, &k
);
610 MB_CHECK_ASSERT(groupnr
== e4b
->bd_group
);
611 for (i
= 0; i
< pa
->pa_len
; i
++)
612 MB_CHECK_ASSERT(mb_test_bit(k
+ i
, buddy
));
616 #undef MB_CHECK_ASSERT
617 #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
618 __FILE__, __func__, __LINE__)
620 #define mb_check_buddy(e4b)
623 /* FIXME!! need more doc */
624 static void ext4_mb_mark_free_simple(struct super_block
*sb
,
625 void *buddy
, unsigned first
, int len
,
626 struct ext4_group_info
*grp
)
628 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
631 unsigned short chunk
;
632 unsigned short border
;
634 BUG_ON(len
> EXT4_BLOCKS_PER_GROUP(sb
));
636 border
= 2 << sb
->s_blocksize_bits
;
639 /* find how many blocks can be covered since this position */
640 max
= ffs(first
| border
) - 1;
642 /* find how many blocks of power 2 we need to mark */
649 /* mark multiblock chunks only */
650 grp
->bb_counters
[min
]++;
652 mb_clear_bit(first
>> min
,
653 buddy
+ sbi
->s_mb_offsets
[min
]);
660 static noinline_for_stack
661 void ext4_mb_generate_buddy(struct super_block
*sb
,
662 void *buddy
, void *bitmap
, ext4_group_t group
)
664 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
665 unsigned short max
= EXT4_BLOCKS_PER_GROUP(sb
);
666 unsigned short i
= 0;
667 unsigned short first
;
670 unsigned fragments
= 0;
671 unsigned long long period
= get_cycles();
673 /* initialize buddy from bitmap which is aggregation
674 * of on-disk bitmap and preallocations */
675 i
= mb_find_next_zero_bit(bitmap
, max
, 0);
676 grp
->bb_first_free
= i
;
680 i
= mb_find_next_bit(bitmap
, max
, i
);
684 ext4_mb_mark_free_simple(sb
, buddy
, first
, len
, grp
);
686 grp
->bb_counters
[0]++;
688 i
= mb_find_next_zero_bit(bitmap
, max
, i
);
690 grp
->bb_fragments
= fragments
;
692 if (free
!= grp
->bb_free
) {
693 ext4_grp_locked_error(sb
, group
, __func__
,
694 "EXT4-fs: group %u: %u blocks in bitmap, %u in gd",
695 group
, free
, grp
->bb_free
);
697 * If we intent to continue, we consider group descritor
698 * corrupt and update bb_free using bitmap value
703 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
, &(grp
->bb_state
));
705 period
= get_cycles() - period
;
706 spin_lock(&EXT4_SB(sb
)->s_bal_lock
);
707 EXT4_SB(sb
)->s_mb_buddies_generated
++;
708 EXT4_SB(sb
)->s_mb_generation_time
+= period
;
709 spin_unlock(&EXT4_SB(sb
)->s_bal_lock
);
712 /* The buddy information is attached the buddy cache inode
713 * for convenience. The information regarding each group
714 * is loaded via ext4_mb_load_buddy. The information involve
715 * block bitmap and buddy information. The information are
716 * stored in the inode as
719 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
722 * one block each for bitmap and buddy information.
723 * So for each group we take up 2 blocks. A page can
724 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
725 * So it can have information regarding groups_per_page which
726 * is blocks_per_page/2
729 static int ext4_mb_init_cache(struct page
*page
, char *incore
)
731 ext4_group_t ngroups
;
737 ext4_group_t first_group
;
739 struct super_block
*sb
;
740 struct buffer_head
*bhs
;
741 struct buffer_head
**bh
;
746 mb_debug("init page %lu\n", page
->index
);
748 inode
= page
->mapping
->host
;
750 ngroups
= ext4_get_groups_count(sb
);
751 blocksize
= 1 << inode
->i_blkbits
;
752 blocks_per_page
= PAGE_CACHE_SIZE
/ blocksize
;
754 groups_per_page
= blocks_per_page
>> 1;
755 if (groups_per_page
== 0)
758 /* allocate buffer_heads to read bitmaps */
759 if (groups_per_page
> 1) {
761 i
= sizeof(struct buffer_head
*) * groups_per_page
;
762 bh
= kzalloc(i
, GFP_NOFS
);
768 first_group
= page
->index
* blocks_per_page
/ 2;
770 /* read all groups the page covers into the cache */
771 for (i
= 0; i
< groups_per_page
; i
++) {
772 struct ext4_group_desc
*desc
;
774 if (first_group
+ i
>= ngroups
)
778 desc
= ext4_get_group_desc(sb
, first_group
+ i
, NULL
);
783 bh
[i
] = sb_getblk(sb
, ext4_block_bitmap(sb
, desc
));
787 if (bitmap_uptodate(bh
[i
]))
791 if (bitmap_uptodate(bh
[i
])) {
792 unlock_buffer(bh
[i
]);
795 ext4_lock_group(sb
, first_group
+ i
);
796 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
797 ext4_init_block_bitmap(sb
, bh
[i
],
798 first_group
+ i
, desc
);
799 set_bitmap_uptodate(bh
[i
]);
800 set_buffer_uptodate(bh
[i
]);
801 ext4_unlock_group(sb
, first_group
+ i
);
802 unlock_buffer(bh
[i
]);
805 ext4_unlock_group(sb
, first_group
+ i
);
806 if (buffer_uptodate(bh
[i
])) {
808 * if not uninit if bh is uptodate,
809 * bitmap is also uptodate
811 set_bitmap_uptodate(bh
[i
]);
812 unlock_buffer(bh
[i
]);
817 * submit the buffer_head for read. We can
818 * safely mark the bitmap as uptodate now.
819 * We do it here so the bitmap uptodate bit
820 * get set with buffer lock held.
822 set_bitmap_uptodate(bh
[i
]);
823 bh
[i
]->b_end_io
= end_buffer_read_sync
;
824 submit_bh(READ
, bh
[i
]);
825 mb_debug("read bitmap for group %u\n", first_group
+ i
);
828 /* wait for I/O completion */
829 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
830 wait_on_buffer(bh
[i
]);
833 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
834 if (!buffer_uptodate(bh
[i
]))
838 first_block
= page
->index
* blocks_per_page
;
840 memset(page_address(page
), 0xff, PAGE_CACHE_SIZE
);
841 for (i
= 0; i
< blocks_per_page
; i
++) {
843 struct ext4_group_info
*grinfo
;
845 group
= (first_block
+ i
) >> 1;
846 if (group
>= ngroups
)
850 * data carry information regarding this
851 * particular group in the format specified
855 data
= page_address(page
) + (i
* blocksize
);
856 bitmap
= bh
[group
- first_group
]->b_data
;
859 * We place the buddy block and bitmap block
862 if ((first_block
+ i
) & 1) {
863 /* this is block of buddy */
864 BUG_ON(incore
== NULL
);
865 mb_debug("put buddy for group %u in page %lu/%x\n",
866 group
, page
->index
, i
* blocksize
);
867 grinfo
= ext4_get_group_info(sb
, group
);
868 grinfo
->bb_fragments
= 0;
869 memset(grinfo
->bb_counters
, 0,
870 sizeof(unsigned short)*(sb
->s_blocksize_bits
+2));
872 * incore got set to the group block bitmap below
874 ext4_lock_group(sb
, group
);
875 ext4_mb_generate_buddy(sb
, data
, incore
, group
);
876 ext4_unlock_group(sb
, group
);
879 /* this is block of bitmap */
880 BUG_ON(incore
!= NULL
);
881 mb_debug("put bitmap for group %u in page %lu/%x\n",
882 group
, page
->index
, i
* blocksize
);
884 /* see comments in ext4_mb_put_pa() */
885 ext4_lock_group(sb
, group
);
886 memcpy(data
, bitmap
, blocksize
);
888 /* mark all preallocated blks used in in-core bitmap */
889 ext4_mb_generate_from_pa(sb
, data
, group
);
890 ext4_mb_generate_from_freelist(sb
, data
, group
);
891 ext4_unlock_group(sb
, group
);
893 /* set incore so that the buddy information can be
894 * generated using this
899 SetPageUptodate(page
);
903 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
911 static noinline_for_stack
int
912 ext4_mb_load_buddy(struct super_block
*sb
, ext4_group_t group
,
913 struct ext4_buddy
*e4b
)
921 struct ext4_group_info
*grp
;
922 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
923 struct inode
*inode
= sbi
->s_buddy_cache
;
925 mb_debug("load group %u\n", group
);
927 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
928 grp
= ext4_get_group_info(sb
, group
);
930 e4b
->bd_blkbits
= sb
->s_blocksize_bits
;
931 e4b
->bd_info
= ext4_get_group_info(sb
, group
);
933 e4b
->bd_group
= group
;
934 e4b
->bd_buddy_page
= NULL
;
935 e4b
->bd_bitmap_page
= NULL
;
936 e4b
->alloc_semp
= &grp
->alloc_sem
;
938 /* Take the read lock on the group alloc
939 * sem. This would make sure a parallel
940 * ext4_mb_init_group happening on other
941 * groups mapped by the page is blocked
942 * till we are done with allocation
944 down_read(e4b
->alloc_semp
);
947 * the buddy cache inode stores the block bitmap
948 * and buddy information in consecutive blocks.
949 * So for each group we need two blocks.
952 pnum
= block
/ blocks_per_page
;
953 poff
= block
% blocks_per_page
;
955 /* we could use find_or_create_page(), but it locks page
956 * what we'd like to avoid in fast path ... */
957 page
= find_get_page(inode
->i_mapping
, pnum
);
958 if (page
== NULL
|| !PageUptodate(page
)) {
961 * drop the page reference and try
962 * to get the page with lock. If we
963 * are not uptodate that implies
964 * somebody just created the page but
965 * is yet to initialize the same. So
966 * wait for it to initialize.
968 page_cache_release(page
);
969 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
971 BUG_ON(page
->mapping
!= inode
->i_mapping
);
972 if (!PageUptodate(page
)) {
973 ret
= ext4_mb_init_cache(page
, NULL
);
978 mb_cmp_bitmaps(e4b
, page_address(page
) +
979 (poff
* sb
->s_blocksize
));
984 if (page
== NULL
|| !PageUptodate(page
)) {
988 e4b
->bd_bitmap_page
= page
;
989 e4b
->bd_bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
990 mark_page_accessed(page
);
993 pnum
= block
/ blocks_per_page
;
994 poff
= block
% blocks_per_page
;
996 page
= find_get_page(inode
->i_mapping
, pnum
);
997 if (page
== NULL
|| !PageUptodate(page
)) {
999 page_cache_release(page
);
1000 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1002 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1003 if (!PageUptodate(page
)) {
1004 ret
= ext4_mb_init_cache(page
, e4b
->bd_bitmap
);
1013 if (page
== NULL
|| !PageUptodate(page
)) {
1017 e4b
->bd_buddy_page
= page
;
1018 e4b
->bd_buddy
= page_address(page
) + (poff
* sb
->s_blocksize
);
1019 mark_page_accessed(page
);
1021 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
1022 BUG_ON(e4b
->bd_buddy_page
== NULL
);
1027 if (e4b
->bd_bitmap_page
)
1028 page_cache_release(e4b
->bd_bitmap_page
);
1029 if (e4b
->bd_buddy_page
)
1030 page_cache_release(e4b
->bd_buddy_page
);
1031 e4b
->bd_buddy
= NULL
;
1032 e4b
->bd_bitmap
= NULL
;
1034 /* Done with the buddy cache */
1035 up_read(e4b
->alloc_semp
);
1039 static void ext4_mb_release_desc(struct ext4_buddy
*e4b
)
1041 if (e4b
->bd_bitmap_page
)
1042 page_cache_release(e4b
->bd_bitmap_page
);
1043 if (e4b
->bd_buddy_page
)
1044 page_cache_release(e4b
->bd_buddy_page
);
1045 /* Done with the buddy cache */
1046 if (e4b
->alloc_semp
)
1047 up_read(e4b
->alloc_semp
);
1051 static int mb_find_order_for_block(struct ext4_buddy
*e4b
, int block
)
1056 BUG_ON(EXT4_MB_BITMAP(e4b
) == EXT4_MB_BUDDY(e4b
));
1057 BUG_ON(block
>= (1 << (e4b
->bd_blkbits
+ 3)));
1059 bb
= EXT4_MB_BUDDY(e4b
);
1060 while (order
<= e4b
->bd_blkbits
+ 1) {
1062 if (!mb_test_bit(block
, bb
)) {
1063 /* this block is part of buddy of order 'order' */
1066 bb
+= 1 << (e4b
->bd_blkbits
- order
);
1072 static void mb_clear_bits(void *bm
, int cur
, int len
)
1078 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1079 /* fast path: clear whole word at once */
1080 addr
= bm
+ (cur
>> 3);
1085 mb_clear_bit(cur
, bm
);
1090 static void mb_set_bits(void *bm
, int cur
, int len
)
1096 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1097 /* fast path: set whole word at once */
1098 addr
= bm
+ (cur
>> 3);
1103 mb_set_bit(cur
, bm
);
1108 static void mb_free_blocks(struct inode
*inode
, struct ext4_buddy
*e4b
,
1109 int first
, int count
)
1116 struct super_block
*sb
= e4b
->bd_sb
;
1118 BUG_ON(first
+ count
> (sb
->s_blocksize
<< 3));
1119 assert_spin_locked(ext4_group_lock_ptr(sb
, e4b
->bd_group
));
1120 mb_check_buddy(e4b
);
1121 mb_free_blocks_double(inode
, e4b
, first
, count
);
1123 e4b
->bd_info
->bb_free
+= count
;
1124 if (first
< e4b
->bd_info
->bb_first_free
)
1125 e4b
->bd_info
->bb_first_free
= first
;
1127 /* let's maintain fragments counter */
1129 block
= !mb_test_bit(first
- 1, EXT4_MB_BITMAP(e4b
));
1130 if (first
+ count
< EXT4_SB(sb
)->s_mb_maxs
[0])
1131 max
= !mb_test_bit(first
+ count
, EXT4_MB_BITMAP(e4b
));
1133 e4b
->bd_info
->bb_fragments
--;
1134 else if (!block
&& !max
)
1135 e4b
->bd_info
->bb_fragments
++;
1137 /* let's maintain buddy itself */
1138 while (count
-- > 0) {
1142 if (!mb_test_bit(block
, EXT4_MB_BITMAP(e4b
))) {
1143 ext4_fsblk_t blocknr
;
1144 blocknr
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
);
1147 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
1148 ext4_grp_locked_error(sb
, e4b
->bd_group
,
1149 __func__
, "double-free of inode"
1150 " %lu's block %llu(bit %u in group %u)",
1151 inode
? inode
->i_ino
: 0, blocknr
, block
,
1154 mb_clear_bit(block
, EXT4_MB_BITMAP(e4b
));
1155 e4b
->bd_info
->bb_counters
[order
]++;
1157 /* start of the buddy */
1158 buddy
= mb_find_buddy(e4b
, order
, &max
);
1162 if (mb_test_bit(block
, buddy
) ||
1163 mb_test_bit(block
+ 1, buddy
))
1166 /* both the buddies are free, try to coalesce them */
1167 buddy2
= mb_find_buddy(e4b
, order
+ 1, &max
);
1173 /* for special purposes, we don't set
1174 * free bits in bitmap */
1175 mb_set_bit(block
, buddy
);
1176 mb_set_bit(block
+ 1, buddy
);
1178 e4b
->bd_info
->bb_counters
[order
]--;
1179 e4b
->bd_info
->bb_counters
[order
]--;
1183 e4b
->bd_info
->bb_counters
[order
]++;
1185 mb_clear_bit(block
, buddy2
);
1189 mb_check_buddy(e4b
);
1192 static int mb_find_extent(struct ext4_buddy
*e4b
, int order
, int block
,
1193 int needed
, struct ext4_free_extent
*ex
)
1200 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
1203 buddy
= mb_find_buddy(e4b
, order
, &max
);
1204 BUG_ON(buddy
== NULL
);
1205 BUG_ON(block
>= max
);
1206 if (mb_test_bit(block
, buddy
)) {
1213 /* FIXME dorp order completely ? */
1214 if (likely(order
== 0)) {
1215 /* find actual order */
1216 order
= mb_find_order_for_block(e4b
, block
);
1217 block
= block
>> order
;
1220 ex
->fe_len
= 1 << order
;
1221 ex
->fe_start
= block
<< order
;
1222 ex
->fe_group
= e4b
->bd_group
;
1224 /* calc difference from given start */
1225 next
= next
- ex
->fe_start
;
1227 ex
->fe_start
+= next
;
1229 while (needed
> ex
->fe_len
&&
1230 (buddy
= mb_find_buddy(e4b
, order
, &max
))) {
1232 if (block
+ 1 >= max
)
1235 next
= (block
+ 1) * (1 << order
);
1236 if (mb_test_bit(next
, EXT4_MB_BITMAP(e4b
)))
1239 ord
= mb_find_order_for_block(e4b
, next
);
1242 block
= next
>> order
;
1243 ex
->fe_len
+= 1 << order
;
1246 BUG_ON(ex
->fe_start
+ ex
->fe_len
> (1 << (e4b
->bd_blkbits
+ 3)));
1250 static int mb_mark_used(struct ext4_buddy
*e4b
, struct ext4_free_extent
*ex
)
1256 int start
= ex
->fe_start
;
1257 int len
= ex
->fe_len
;
1262 BUG_ON(start
+ len
> (e4b
->bd_sb
->s_blocksize
<< 3));
1263 BUG_ON(e4b
->bd_group
!= ex
->fe_group
);
1264 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
1265 mb_check_buddy(e4b
);
1266 mb_mark_used_double(e4b
, start
, len
);
1268 e4b
->bd_info
->bb_free
-= len
;
1269 if (e4b
->bd_info
->bb_first_free
== start
)
1270 e4b
->bd_info
->bb_first_free
+= len
;
1272 /* let's maintain fragments counter */
1274 mlen
= !mb_test_bit(start
- 1, EXT4_MB_BITMAP(e4b
));
1275 if (start
+ len
< EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[0])
1276 max
= !mb_test_bit(start
+ len
, EXT4_MB_BITMAP(e4b
));
1278 e4b
->bd_info
->bb_fragments
++;
1279 else if (!mlen
&& !max
)
1280 e4b
->bd_info
->bb_fragments
--;
1282 /* let's maintain buddy itself */
1284 ord
= mb_find_order_for_block(e4b
, start
);
1286 if (((start
>> ord
) << ord
) == start
&& len
>= (1 << ord
)) {
1287 /* the whole chunk may be allocated at once! */
1289 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1290 BUG_ON((start
>> ord
) >= max
);
1291 mb_set_bit(start
>> ord
, buddy
);
1292 e4b
->bd_info
->bb_counters
[ord
]--;
1299 /* store for history */
1301 ret
= len
| (ord
<< 16);
1303 /* we have to split large buddy */
1305 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1306 mb_set_bit(start
>> ord
, buddy
);
1307 e4b
->bd_info
->bb_counters
[ord
]--;
1310 cur
= (start
>> ord
) & ~1U;
1311 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1312 mb_clear_bit(cur
, buddy
);
1313 mb_clear_bit(cur
+ 1, buddy
);
1314 e4b
->bd_info
->bb_counters
[ord
]++;
1315 e4b
->bd_info
->bb_counters
[ord
]++;
1318 mb_set_bits(EXT4_MB_BITMAP(e4b
), ex
->fe_start
, len0
);
1319 mb_check_buddy(e4b
);
1325 * Must be called under group lock!
1327 static void ext4_mb_use_best_found(struct ext4_allocation_context
*ac
,
1328 struct ext4_buddy
*e4b
)
1330 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1333 BUG_ON(ac
->ac_b_ex
.fe_group
!= e4b
->bd_group
);
1334 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1336 ac
->ac_b_ex
.fe_len
= min(ac
->ac_b_ex
.fe_len
, ac
->ac_g_ex
.fe_len
);
1337 ac
->ac_b_ex
.fe_logical
= ac
->ac_g_ex
.fe_logical
;
1338 ret
= mb_mark_used(e4b
, &ac
->ac_b_ex
);
1340 /* preallocation can change ac_b_ex, thus we store actually
1341 * allocated blocks for history */
1342 ac
->ac_f_ex
= ac
->ac_b_ex
;
1344 ac
->ac_status
= AC_STATUS_FOUND
;
1345 ac
->ac_tail
= ret
& 0xffff;
1346 ac
->ac_buddy
= ret
>> 16;
1349 * take the page reference. We want the page to be pinned
1350 * so that we don't get a ext4_mb_init_cache_call for this
1351 * group until we update the bitmap. That would mean we
1352 * double allocate blocks. The reference is dropped
1353 * in ext4_mb_release_context
1355 ac
->ac_bitmap_page
= e4b
->bd_bitmap_page
;
1356 get_page(ac
->ac_bitmap_page
);
1357 ac
->ac_buddy_page
= e4b
->bd_buddy_page
;
1358 get_page(ac
->ac_buddy_page
);
1359 /* on allocation we use ac to track the held semaphore */
1360 ac
->alloc_semp
= e4b
->alloc_semp
;
1361 e4b
->alloc_semp
= NULL
;
1362 /* store last allocated for subsequent stream allocation */
1363 if ((ac
->ac_flags
& EXT4_MB_HINT_DATA
)) {
1364 spin_lock(&sbi
->s_md_lock
);
1365 sbi
->s_mb_last_group
= ac
->ac_f_ex
.fe_group
;
1366 sbi
->s_mb_last_start
= ac
->ac_f_ex
.fe_start
;
1367 spin_unlock(&sbi
->s_md_lock
);
1372 * regular allocator, for general purposes allocation
1375 static void ext4_mb_check_limits(struct ext4_allocation_context
*ac
,
1376 struct ext4_buddy
*e4b
,
1379 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1380 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1381 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1382 struct ext4_free_extent ex
;
1385 if (ac
->ac_status
== AC_STATUS_FOUND
)
1388 * We don't want to scan for a whole year
1390 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
&&
1391 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1392 ac
->ac_status
= AC_STATUS_BREAK
;
1397 * Haven't found good chunk so far, let's continue
1399 if (bex
->fe_len
< gex
->fe_len
)
1402 if ((finish_group
|| ac
->ac_found
> sbi
->s_mb_min_to_scan
)
1403 && bex
->fe_group
== e4b
->bd_group
) {
1404 /* recheck chunk's availability - we don't know
1405 * when it was found (within this lock-unlock
1407 max
= mb_find_extent(e4b
, 0, bex
->fe_start
, gex
->fe_len
, &ex
);
1408 if (max
>= gex
->fe_len
) {
1409 ext4_mb_use_best_found(ac
, e4b
);
1416 * The routine checks whether found extent is good enough. If it is,
1417 * then the extent gets marked used and flag is set to the context
1418 * to stop scanning. Otherwise, the extent is compared with the
1419 * previous found extent and if new one is better, then it's stored
1420 * in the context. Later, the best found extent will be used, if
1421 * mballoc can't find good enough extent.
1423 * FIXME: real allocation policy is to be designed yet!
1425 static void ext4_mb_measure_extent(struct ext4_allocation_context
*ac
,
1426 struct ext4_free_extent
*ex
,
1427 struct ext4_buddy
*e4b
)
1429 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1430 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1432 BUG_ON(ex
->fe_len
<= 0);
1433 BUG_ON(ex
->fe_len
> EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
1434 BUG_ON(ex
->fe_start
>= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
1435 BUG_ON(ac
->ac_status
!= AC_STATUS_CONTINUE
);
1440 * The special case - take what you catch first
1442 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1444 ext4_mb_use_best_found(ac
, e4b
);
1449 * Let's check whether the chuck is good enough
1451 if (ex
->fe_len
== gex
->fe_len
) {
1453 ext4_mb_use_best_found(ac
, e4b
);
1458 * If this is first found extent, just store it in the context
1460 if (bex
->fe_len
== 0) {
1466 * If new found extent is better, store it in the context
1468 if (bex
->fe_len
< gex
->fe_len
) {
1469 /* if the request isn't satisfied, any found extent
1470 * larger than previous best one is better */
1471 if (ex
->fe_len
> bex
->fe_len
)
1473 } else if (ex
->fe_len
> gex
->fe_len
) {
1474 /* if the request is satisfied, then we try to find
1475 * an extent that still satisfy the request, but is
1476 * smaller than previous one */
1477 if (ex
->fe_len
< bex
->fe_len
)
1481 ext4_mb_check_limits(ac
, e4b
, 0);
1484 static noinline_for_stack
1485 int ext4_mb_try_best_found(struct ext4_allocation_context
*ac
,
1486 struct ext4_buddy
*e4b
)
1488 struct ext4_free_extent ex
= ac
->ac_b_ex
;
1489 ext4_group_t group
= ex
.fe_group
;
1493 BUG_ON(ex
.fe_len
<= 0);
1494 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1498 ext4_lock_group(ac
->ac_sb
, group
);
1499 max
= mb_find_extent(e4b
, 0, ex
.fe_start
, ex
.fe_len
, &ex
);
1503 ext4_mb_use_best_found(ac
, e4b
);
1506 ext4_unlock_group(ac
->ac_sb
, group
);
1507 ext4_mb_release_desc(e4b
);
1512 static noinline_for_stack
1513 int ext4_mb_find_by_goal(struct ext4_allocation_context
*ac
,
1514 struct ext4_buddy
*e4b
)
1516 ext4_group_t group
= ac
->ac_g_ex
.fe_group
;
1519 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1520 struct ext4_super_block
*es
= sbi
->s_es
;
1521 struct ext4_free_extent ex
;
1523 if (!(ac
->ac_flags
& EXT4_MB_HINT_TRY_GOAL
))
1526 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1530 ext4_lock_group(ac
->ac_sb
, group
);
1531 max
= mb_find_extent(e4b
, 0, ac
->ac_g_ex
.fe_start
,
1532 ac
->ac_g_ex
.fe_len
, &ex
);
1534 if (max
>= ac
->ac_g_ex
.fe_len
&& ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
) {
1537 start
= (e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
)) +
1538 ex
.fe_start
+ le32_to_cpu(es
->s_first_data_block
);
1539 /* use do_div to get remainder (would be 64-bit modulo) */
1540 if (do_div(start
, sbi
->s_stripe
) == 0) {
1543 ext4_mb_use_best_found(ac
, e4b
);
1545 } else if (max
>= ac
->ac_g_ex
.fe_len
) {
1546 BUG_ON(ex
.fe_len
<= 0);
1547 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1548 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1551 ext4_mb_use_best_found(ac
, e4b
);
1552 } else if (max
> 0 && (ac
->ac_flags
& EXT4_MB_HINT_MERGE
)) {
1553 /* Sometimes, caller may want to merge even small
1554 * number of blocks to an existing extent */
1555 BUG_ON(ex
.fe_len
<= 0);
1556 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1557 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1560 ext4_mb_use_best_found(ac
, e4b
);
1562 ext4_unlock_group(ac
->ac_sb
, group
);
1563 ext4_mb_release_desc(e4b
);
1569 * The routine scans buddy structures (not bitmap!) from given order
1570 * to max order and tries to find big enough chunk to satisfy the req
1572 static noinline_for_stack
1573 void ext4_mb_simple_scan_group(struct ext4_allocation_context
*ac
,
1574 struct ext4_buddy
*e4b
)
1576 struct super_block
*sb
= ac
->ac_sb
;
1577 struct ext4_group_info
*grp
= e4b
->bd_info
;
1583 BUG_ON(ac
->ac_2order
<= 0);
1584 for (i
= ac
->ac_2order
; i
<= sb
->s_blocksize_bits
+ 1; i
++) {
1585 if (grp
->bb_counters
[i
] == 0)
1588 buddy
= mb_find_buddy(e4b
, i
, &max
);
1589 BUG_ON(buddy
== NULL
);
1591 k
= mb_find_next_zero_bit(buddy
, max
, 0);
1596 ac
->ac_b_ex
.fe_len
= 1 << i
;
1597 ac
->ac_b_ex
.fe_start
= k
<< i
;
1598 ac
->ac_b_ex
.fe_group
= e4b
->bd_group
;
1600 ext4_mb_use_best_found(ac
, e4b
);
1602 BUG_ON(ac
->ac_b_ex
.fe_len
!= ac
->ac_g_ex
.fe_len
);
1604 if (EXT4_SB(sb
)->s_mb_stats
)
1605 atomic_inc(&EXT4_SB(sb
)->s_bal_2orders
);
1612 * The routine scans the group and measures all found extents.
1613 * In order to optimize scanning, caller must pass number of
1614 * free blocks in the group, so the routine can know upper limit.
1616 static noinline_for_stack
1617 void ext4_mb_complex_scan_group(struct ext4_allocation_context
*ac
,
1618 struct ext4_buddy
*e4b
)
1620 struct super_block
*sb
= ac
->ac_sb
;
1621 void *bitmap
= EXT4_MB_BITMAP(e4b
);
1622 struct ext4_free_extent ex
;
1626 free
= e4b
->bd_info
->bb_free
;
1629 i
= e4b
->bd_info
->bb_first_free
;
1631 while (free
&& ac
->ac_status
== AC_STATUS_CONTINUE
) {
1632 i
= mb_find_next_zero_bit(bitmap
,
1633 EXT4_BLOCKS_PER_GROUP(sb
), i
);
1634 if (i
>= EXT4_BLOCKS_PER_GROUP(sb
)) {
1636 * IF we have corrupt bitmap, we won't find any
1637 * free blocks even though group info says we
1638 * we have free blocks
1640 ext4_grp_locked_error(sb
, e4b
->bd_group
,
1641 __func__
, "%d free blocks as per "
1642 "group info. But bitmap says 0",
1647 mb_find_extent(e4b
, 0, i
, ac
->ac_g_ex
.fe_len
, &ex
);
1648 BUG_ON(ex
.fe_len
<= 0);
1649 if (free
< ex
.fe_len
) {
1650 ext4_grp_locked_error(sb
, e4b
->bd_group
,
1651 __func__
, "%d free blocks as per "
1652 "group info. But got %d blocks",
1655 * The number of free blocks differs. This mostly
1656 * indicate that the bitmap is corrupt. So exit
1657 * without claiming the space.
1662 ext4_mb_measure_extent(ac
, &ex
, e4b
);
1668 ext4_mb_check_limits(ac
, e4b
, 1);
1672 * This is a special case for storages like raid5
1673 * we try to find stripe-aligned chunks for stripe-size requests
1674 * XXX should do so at least for multiples of stripe size as well
1676 static noinline_for_stack
1677 void ext4_mb_scan_aligned(struct ext4_allocation_context
*ac
,
1678 struct ext4_buddy
*e4b
)
1680 struct super_block
*sb
= ac
->ac_sb
;
1681 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1682 void *bitmap
= EXT4_MB_BITMAP(e4b
);
1683 struct ext4_free_extent ex
;
1684 ext4_fsblk_t first_group_block
;
1689 BUG_ON(sbi
->s_stripe
== 0);
1691 /* find first stripe-aligned block in group */
1692 first_group_block
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
)
1693 + le32_to_cpu(sbi
->s_es
->s_first_data_block
);
1694 a
= first_group_block
+ sbi
->s_stripe
- 1;
1695 do_div(a
, sbi
->s_stripe
);
1696 i
= (a
* sbi
->s_stripe
) - first_group_block
;
1698 while (i
< EXT4_BLOCKS_PER_GROUP(sb
)) {
1699 if (!mb_test_bit(i
, bitmap
)) {
1700 max
= mb_find_extent(e4b
, 0, i
, sbi
->s_stripe
, &ex
);
1701 if (max
>= sbi
->s_stripe
) {
1704 ext4_mb_use_best_found(ac
, e4b
);
1712 static int ext4_mb_good_group(struct ext4_allocation_context
*ac
,
1713 ext4_group_t group
, int cr
)
1715 unsigned free
, fragments
;
1717 int flex_size
= ext4_flex_bg_size(EXT4_SB(ac
->ac_sb
));
1718 struct ext4_group_info
*grp
= ext4_get_group_info(ac
->ac_sb
, group
);
1720 BUG_ON(cr
< 0 || cr
>= 4);
1721 BUG_ON(EXT4_MB_GRP_NEED_INIT(grp
));
1723 free
= grp
->bb_free
;
1724 fragments
= grp
->bb_fragments
;
1732 BUG_ON(ac
->ac_2order
== 0);
1734 /* Avoid using the first bg of a flexgroup for data files */
1735 if ((ac
->ac_flags
& EXT4_MB_HINT_DATA
) &&
1736 (flex_size
>= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
) &&
1737 ((group
% flex_size
) == 0))
1740 bits
= ac
->ac_sb
->s_blocksize_bits
+ 1;
1741 for (i
= ac
->ac_2order
; i
<= bits
; i
++)
1742 if (grp
->bb_counters
[i
] > 0)
1746 if ((free
/ fragments
) >= ac
->ac_g_ex
.fe_len
)
1750 if (free
>= ac
->ac_g_ex
.fe_len
)
1763 * lock the group_info alloc_sem of all the groups
1764 * belonging to the same buddy cache page. This
1765 * make sure other parallel operation on the buddy
1766 * cache doesn't happen whild holding the buddy cache
1769 int ext4_mb_get_buddy_cache_lock(struct super_block
*sb
, ext4_group_t group
)
1773 int blocks_per_page
;
1774 int groups_per_page
;
1775 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
1776 ext4_group_t first_group
;
1777 struct ext4_group_info
*grp
;
1779 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1781 * the buddy cache inode stores the block bitmap
1782 * and buddy information in consecutive blocks.
1783 * So for each group we need two blocks.
1786 pnum
= block
/ blocks_per_page
;
1787 first_group
= pnum
* blocks_per_page
/ 2;
1789 groups_per_page
= blocks_per_page
>> 1;
1790 if (groups_per_page
== 0)
1791 groups_per_page
= 1;
1792 /* read all groups the page covers into the cache */
1793 for (i
= 0; i
< groups_per_page
; i
++) {
1795 if ((first_group
+ i
) >= ngroups
)
1797 grp
= ext4_get_group_info(sb
, first_group
+ i
);
1798 /* take all groups write allocation
1799 * semaphore. This make sure there is
1800 * no block allocation going on in any
1803 down_write_nested(&grp
->alloc_sem
, i
);
1808 void ext4_mb_put_buddy_cache_lock(struct super_block
*sb
,
1809 ext4_group_t group
, int locked_group
)
1813 int blocks_per_page
;
1814 ext4_group_t first_group
;
1815 struct ext4_group_info
*grp
;
1817 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1819 * the buddy cache inode stores the block bitmap
1820 * and buddy information in consecutive blocks.
1821 * So for each group we need two blocks.
1824 pnum
= block
/ blocks_per_page
;
1825 first_group
= pnum
* blocks_per_page
/ 2;
1826 /* release locks on all the groups */
1827 for (i
= 0; i
< locked_group
; i
++) {
1829 grp
= ext4_get_group_info(sb
, first_group
+ i
);
1830 /* take all groups write allocation
1831 * semaphore. This make sure there is
1832 * no block allocation going on in any
1835 up_write(&grp
->alloc_sem
);
1840 static noinline_for_stack
1841 int ext4_mb_init_group(struct super_block
*sb
, ext4_group_t group
)
1846 int blocks_per_page
;
1847 int block
, pnum
, poff
;
1848 int num_grp_locked
= 0;
1849 struct ext4_group_info
*this_grp
;
1850 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1851 struct inode
*inode
= sbi
->s_buddy_cache
;
1852 struct page
*page
= NULL
, *bitmap_page
= NULL
;
1854 mb_debug("init group %lu\n", group
);
1855 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1856 this_grp
= ext4_get_group_info(sb
, group
);
1858 * This ensures we don't add group
1859 * to this buddy cache via resize
1861 num_grp_locked
= ext4_mb_get_buddy_cache_lock(sb
, group
);
1862 if (!EXT4_MB_GRP_NEED_INIT(this_grp
)) {
1864 * somebody initialized the group
1865 * return without doing anything
1871 * the buddy cache inode stores the block bitmap
1872 * and buddy information in consecutive blocks.
1873 * So for each group we need two blocks.
1876 pnum
= block
/ blocks_per_page
;
1877 poff
= block
% blocks_per_page
;
1878 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1880 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1881 ret
= ext4_mb_init_cache(page
, NULL
);
1888 if (page
== NULL
|| !PageUptodate(page
)) {
1892 mark_page_accessed(page
);
1894 bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
1896 /* init buddy cache */
1898 pnum
= block
/ blocks_per_page
;
1899 poff
= block
% blocks_per_page
;
1900 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1901 if (page
== bitmap_page
) {
1903 * If both the bitmap and buddy are in
1904 * the same page we don't need to force
1909 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1910 ret
= ext4_mb_init_cache(page
, bitmap
);
1917 if (page
== NULL
|| !PageUptodate(page
)) {
1921 mark_page_accessed(page
);
1923 ext4_mb_put_buddy_cache_lock(sb
, group
, num_grp_locked
);
1925 page_cache_release(bitmap_page
);
1927 page_cache_release(page
);
1931 static noinline_for_stack
int
1932 ext4_mb_regular_allocator(struct ext4_allocation_context
*ac
)
1934 ext4_group_t ngroups
, group
, i
;
1938 struct ext4_sb_info
*sbi
;
1939 struct super_block
*sb
;
1940 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
;
1977 /* if stream allocation is enabled, use global goal */
1978 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
1979 isize
= i_size_read(ac
->ac_inode
) >> bsbits
;
1983 if (size
< sbi
->s_mb_stream_request
&&
1984 (ac
->ac_flags
& EXT4_MB_HINT_DATA
)) {
1985 /* TBD: may be hot point */
1986 spin_lock(&sbi
->s_md_lock
);
1987 ac
->ac_g_ex
.fe_group
= sbi
->s_mb_last_group
;
1988 ac
->ac_g_ex
.fe_start
= sbi
->s_mb_last_start
;
1989 spin_unlock(&sbi
->s_md_lock
);
1991 /* Let's just scan groups to find more-less suitable blocks */
1992 cr
= ac
->ac_2order
? 0 : 1;
1994 * cr == 0 try to get exact allocation,
1995 * cr == 3 try to get anything
1998 for (; cr
< 4 && ac
->ac_status
== AC_STATUS_CONTINUE
; cr
++) {
1999 ac
->ac_criteria
= cr
;
2001 * searching for the right group start
2002 * from the goal value specified
2004 group
= ac
->ac_g_ex
.fe_group
;
2006 for (i
= 0; i
< ngroups
; group
++, i
++) {
2007 struct ext4_group_info
*grp
;
2008 struct ext4_group_desc
*desc
;
2010 if (group
== ngroups
)
2013 /* quick check to skip empty groups */
2014 grp
= ext4_get_group_info(sb
, group
);
2015 if (grp
->bb_free
== 0)
2019 * if the group is already init we check whether it is
2020 * a good group and if not we don't load the buddy
2022 if (EXT4_MB_GRP_NEED_INIT(grp
)) {
2024 * we need full data about the group
2025 * to make a good selection
2027 err
= ext4_mb_init_group(sb
, group
);
2033 * If the particular group doesn't satisfy our
2034 * criteria we continue with the next group
2036 if (!ext4_mb_good_group(ac
, group
, cr
))
2039 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2043 ext4_lock_group(sb
, group
);
2044 if (!ext4_mb_good_group(ac
, group
, cr
)) {
2045 /* someone did allocation from this group */
2046 ext4_unlock_group(sb
, group
);
2047 ext4_mb_release_desc(&e4b
);
2051 ac
->ac_groups_scanned
++;
2052 desc
= ext4_get_group_desc(sb
, group
, NULL
);
2054 ext4_mb_simple_scan_group(ac
, &e4b
);
2056 ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
)
2057 ext4_mb_scan_aligned(ac
, &e4b
);
2059 ext4_mb_complex_scan_group(ac
, &e4b
);
2061 ext4_unlock_group(sb
, group
);
2062 ext4_mb_release_desc(&e4b
);
2064 if (ac
->ac_status
!= AC_STATUS_CONTINUE
)
2069 if (ac
->ac_b_ex
.fe_len
> 0 && ac
->ac_status
!= AC_STATUS_FOUND
&&
2070 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
2072 * We've been searching too long. Let's try to allocate
2073 * the best chunk we've found so far
2076 ext4_mb_try_best_found(ac
, &e4b
);
2077 if (ac
->ac_status
!= AC_STATUS_FOUND
) {
2079 * Someone more lucky has already allocated it.
2080 * The only thing we can do is just take first
2082 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2084 ac
->ac_b_ex
.fe_group
= 0;
2085 ac
->ac_b_ex
.fe_start
= 0;
2086 ac
->ac_b_ex
.fe_len
= 0;
2087 ac
->ac_status
= AC_STATUS_CONTINUE
;
2088 ac
->ac_flags
|= EXT4_MB_HINT_FIRST
;
2090 atomic_inc(&sbi
->s_mb_lost_chunks
);
2098 #ifdef EXT4_MB_HISTORY
2099 struct ext4_mb_proc_session
{
2100 struct ext4_mb_history
*history
;
2101 struct super_block
*sb
;
2106 static void *ext4_mb_history_skip_empty(struct ext4_mb_proc_session
*s
,
2107 struct ext4_mb_history
*hs
,
2110 if (hs
== s
->history
+ s
->max
)
2112 if (!first
&& hs
== s
->history
+ s
->start
)
2114 while (hs
->orig
.fe_len
== 0) {
2116 if (hs
== s
->history
+ s
->max
)
2118 if (hs
== s
->history
+ s
->start
)
2124 static void *ext4_mb_seq_history_start(struct seq_file
*seq
, loff_t
*pos
)
2126 struct ext4_mb_proc_session
*s
= seq
->private;
2127 struct ext4_mb_history
*hs
;
2131 return SEQ_START_TOKEN
;
2132 hs
= ext4_mb_history_skip_empty(s
, s
->history
+ s
->start
, 1);
2135 while (--l
&& (hs
= ext4_mb_history_skip_empty(s
, ++hs
, 0)) != NULL
);
2139 static void *ext4_mb_seq_history_next(struct seq_file
*seq
, void *v
,
2142 struct ext4_mb_proc_session
*s
= seq
->private;
2143 struct ext4_mb_history
*hs
= v
;
2146 if (v
== SEQ_START_TOKEN
)
2147 return ext4_mb_history_skip_empty(s
, s
->history
+ s
->start
, 1);
2149 return ext4_mb_history_skip_empty(s
, ++hs
, 0);
2152 static int ext4_mb_seq_history_show(struct seq_file
*seq
, void *v
)
2154 char buf
[25], buf2
[25], buf3
[25], *fmt
;
2155 struct ext4_mb_history
*hs
= v
;
2157 if (v
== SEQ_START_TOKEN
) {
2158 seq_printf(seq
, "%-5s %-8s %-23s %-23s %-23s %-5s "
2159 "%-5s %-2s %-5s %-5s %-5s %-6s\n",
2160 "pid", "inode", "original", "goal", "result", "found",
2161 "grps", "cr", "flags", "merge", "tail", "broken");
2165 if (hs
->op
== EXT4_MB_HISTORY_ALLOC
) {
2166 fmt
= "%-5u %-8u %-23s %-23s %-23s %-5u %-5u %-2u "
2167 "%-5u %-5s %-5u %-6u\n";
2168 sprintf(buf2
, "%u/%d/%u@%u", hs
->result
.fe_group
,
2169 hs
->result
.fe_start
, hs
->result
.fe_len
,
2170 hs
->result
.fe_logical
);
2171 sprintf(buf
, "%u/%d/%u@%u", hs
->orig
.fe_group
,
2172 hs
->orig
.fe_start
, hs
->orig
.fe_len
,
2173 hs
->orig
.fe_logical
);
2174 sprintf(buf3
, "%u/%d/%u@%u", hs
->goal
.fe_group
,
2175 hs
->goal
.fe_start
, hs
->goal
.fe_len
,
2176 hs
->goal
.fe_logical
);
2177 seq_printf(seq
, fmt
, hs
->pid
, hs
->ino
, buf
, buf3
, buf2
,
2178 hs
->found
, hs
->groups
, hs
->cr
, hs
->flags
,
2179 hs
->merged
? "M" : "", hs
->tail
,
2180 hs
->buddy
? 1 << hs
->buddy
: 0);
2181 } else if (hs
->op
== EXT4_MB_HISTORY_PREALLOC
) {
2182 fmt
= "%-5u %-8u %-23s %-23s %-23s\n";
2183 sprintf(buf2
, "%u/%d/%u@%u", hs
->result
.fe_group
,
2184 hs
->result
.fe_start
, hs
->result
.fe_len
,
2185 hs
->result
.fe_logical
);
2186 sprintf(buf
, "%u/%d/%u@%u", hs
->orig
.fe_group
,
2187 hs
->orig
.fe_start
, hs
->orig
.fe_len
,
2188 hs
->orig
.fe_logical
);
2189 seq_printf(seq
, fmt
, hs
->pid
, hs
->ino
, buf
, "", buf2
);
2190 } else if (hs
->op
== EXT4_MB_HISTORY_DISCARD
) {
2191 sprintf(buf2
, "%u/%d/%u", hs
->result
.fe_group
,
2192 hs
->result
.fe_start
, hs
->result
.fe_len
);
2193 seq_printf(seq
, "%-5u %-8u %-23s discard\n",
2194 hs
->pid
, hs
->ino
, buf2
);
2195 } else if (hs
->op
== EXT4_MB_HISTORY_FREE
) {
2196 sprintf(buf2
, "%u/%d/%u", hs
->result
.fe_group
,
2197 hs
->result
.fe_start
, hs
->result
.fe_len
);
2198 seq_printf(seq
, "%-5u %-8u %-23s free\n",
2199 hs
->pid
, hs
->ino
, buf2
);
2204 static void ext4_mb_seq_history_stop(struct seq_file
*seq
, void *v
)
2208 static struct seq_operations ext4_mb_seq_history_ops
= {
2209 .start
= ext4_mb_seq_history_start
,
2210 .next
= ext4_mb_seq_history_next
,
2211 .stop
= ext4_mb_seq_history_stop
,
2212 .show
= ext4_mb_seq_history_show
,
2215 static int ext4_mb_seq_history_open(struct inode
*inode
, struct file
*file
)
2217 struct super_block
*sb
= PDE(inode
)->data
;
2218 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2219 struct ext4_mb_proc_session
*s
;
2223 if (unlikely(sbi
->s_mb_history
== NULL
))
2225 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
2229 size
= sizeof(struct ext4_mb_history
) * sbi
->s_mb_history_max
;
2230 s
->history
= kmalloc(size
, GFP_KERNEL
);
2231 if (s
->history
== NULL
) {
2236 spin_lock(&sbi
->s_mb_history_lock
);
2237 memcpy(s
->history
, sbi
->s_mb_history
, size
);
2238 s
->max
= sbi
->s_mb_history_max
;
2239 s
->start
= sbi
->s_mb_history_cur
% s
->max
;
2240 spin_unlock(&sbi
->s_mb_history_lock
);
2242 rc
= seq_open(file
, &ext4_mb_seq_history_ops
);
2244 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2254 static int ext4_mb_seq_history_release(struct inode
*inode
, struct file
*file
)
2256 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
2257 struct ext4_mb_proc_session
*s
= seq
->private;
2260 return seq_release(inode
, file
);
2263 static ssize_t
ext4_mb_seq_history_write(struct file
*file
,
2264 const char __user
*buffer
,
2265 size_t count
, loff_t
*ppos
)
2267 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
2268 struct ext4_mb_proc_session
*s
= seq
->private;
2269 struct super_block
*sb
= s
->sb
;
2273 if (count
>= sizeof(str
)) {
2274 printk(KERN_ERR
"EXT4-fs: %s string too long, max %u bytes\n",
2275 "mb_history", (int)sizeof(str
));
2279 if (copy_from_user(str
, buffer
, count
))
2282 value
= simple_strtol(str
, NULL
, 0);
2285 EXT4_SB(sb
)->s_mb_history_filter
= value
;
2290 static struct file_operations ext4_mb_seq_history_fops
= {
2291 .owner
= THIS_MODULE
,
2292 .open
= ext4_mb_seq_history_open
,
2294 .write
= ext4_mb_seq_history_write
,
2295 .llseek
= seq_lseek
,
2296 .release
= ext4_mb_seq_history_release
,
2299 static void *ext4_mb_seq_groups_start(struct seq_file
*seq
, loff_t
*pos
)
2301 struct super_block
*sb
= seq
->private;
2304 if (*pos
< 0 || *pos
>= ext4_get_groups_count(sb
))
2307 return (void *) ((unsigned long) group
);
2310 static void *ext4_mb_seq_groups_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2312 struct super_block
*sb
= seq
->private;
2316 if (*pos
< 0 || *pos
>= ext4_get_groups_count(sb
))
2319 return (void *) ((unsigned long) group
);
2322 static int ext4_mb_seq_groups_show(struct seq_file
*seq
, void *v
)
2324 struct super_block
*sb
= seq
->private;
2325 ext4_group_t group
= (ext4_group_t
) ((unsigned long) v
);
2328 struct ext4_buddy e4b
;
2330 struct ext4_group_info info
;
2331 unsigned short counters
[16];
2336 seq_printf(seq
, "#%-5s: %-5s %-5s %-5s "
2337 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2338 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2339 "group", "free", "frags", "first",
2340 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2341 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2343 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(sg
.info
.bb_counters
[0]) +
2344 sizeof(struct ext4_group_info
);
2345 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2347 seq_printf(seq
, "#%-5u: I/O error\n", group
);
2350 ext4_lock_group(sb
, group
);
2351 memcpy(&sg
, ext4_get_group_info(sb
, group
), i
);
2352 ext4_unlock_group(sb
, group
);
2353 ext4_mb_release_desc(&e4b
);
2355 seq_printf(seq
, "#%-5u: %-5u %-5u %-5u [", group
, sg
.info
.bb_free
,
2356 sg
.info
.bb_fragments
, sg
.info
.bb_first_free
);
2357 for (i
= 0; i
<= 13; i
++)
2358 seq_printf(seq
, " %-5u", i
<= sb
->s_blocksize_bits
+ 1 ?
2359 sg
.info
.bb_counters
[i
] : 0);
2360 seq_printf(seq
, " ]\n");
2365 static void ext4_mb_seq_groups_stop(struct seq_file
*seq
, void *v
)
2369 static struct seq_operations ext4_mb_seq_groups_ops
= {
2370 .start
= ext4_mb_seq_groups_start
,
2371 .next
= ext4_mb_seq_groups_next
,
2372 .stop
= ext4_mb_seq_groups_stop
,
2373 .show
= ext4_mb_seq_groups_show
,
2376 static int ext4_mb_seq_groups_open(struct inode
*inode
, struct file
*file
)
2378 struct super_block
*sb
= PDE(inode
)->data
;
2381 rc
= seq_open(file
, &ext4_mb_seq_groups_ops
);
2383 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2390 static struct file_operations ext4_mb_seq_groups_fops
= {
2391 .owner
= THIS_MODULE
,
2392 .open
= ext4_mb_seq_groups_open
,
2394 .llseek
= seq_lseek
,
2395 .release
= seq_release
,
2398 static void ext4_mb_history_release(struct super_block
*sb
)
2400 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2402 if (sbi
->s_proc
!= NULL
) {
2403 remove_proc_entry("mb_groups", sbi
->s_proc
);
2404 if (sbi
->s_mb_history_max
)
2405 remove_proc_entry("mb_history", sbi
->s_proc
);
2407 kfree(sbi
->s_mb_history
);
2410 static void ext4_mb_history_init(struct super_block
*sb
)
2412 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2415 if (sbi
->s_proc
!= NULL
) {
2416 if (sbi
->s_mb_history_max
)
2417 proc_create_data("mb_history", S_IRUGO
, sbi
->s_proc
,
2418 &ext4_mb_seq_history_fops
, sb
);
2419 proc_create_data("mb_groups", S_IRUGO
, sbi
->s_proc
,
2420 &ext4_mb_seq_groups_fops
, sb
);
2423 sbi
->s_mb_history_cur
= 0;
2424 spin_lock_init(&sbi
->s_mb_history_lock
);
2425 i
= sbi
->s_mb_history_max
* sizeof(struct ext4_mb_history
);
2426 sbi
->s_mb_history
= i
? kzalloc(i
, GFP_KERNEL
) : NULL
;
2427 /* if we can't allocate history, then we simple won't use it */
2430 static noinline_for_stack
void
2431 ext4_mb_store_history(struct ext4_allocation_context
*ac
)
2433 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
2434 struct ext4_mb_history h
;
2436 if (sbi
->s_mb_history
== NULL
)
2439 if (!(ac
->ac_op
& sbi
->s_mb_history_filter
))
2443 h
.pid
= current
->pid
;
2444 h
.ino
= ac
->ac_inode
? ac
->ac_inode
->i_ino
: 0;
2445 h
.orig
= ac
->ac_o_ex
;
2446 h
.result
= ac
->ac_b_ex
;
2447 h
.flags
= ac
->ac_flags
;
2448 h
.found
= ac
->ac_found
;
2449 h
.groups
= ac
->ac_groups_scanned
;
2450 h
.cr
= ac
->ac_criteria
;
2451 h
.tail
= ac
->ac_tail
;
2452 h
.buddy
= ac
->ac_buddy
;
2454 if (ac
->ac_op
== EXT4_MB_HISTORY_ALLOC
) {
2455 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
2456 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
2458 h
.goal
= ac
->ac_g_ex
;
2459 h
.result
= ac
->ac_f_ex
;
2462 spin_lock(&sbi
->s_mb_history_lock
);
2463 memcpy(sbi
->s_mb_history
+ sbi
->s_mb_history_cur
, &h
, sizeof(h
));
2464 if (++sbi
->s_mb_history_cur
>= sbi
->s_mb_history_max
)
2465 sbi
->s_mb_history_cur
= 0;
2466 spin_unlock(&sbi
->s_mb_history_lock
);
2470 #define ext4_mb_history_release(sb)
2471 #define ext4_mb_history_init(sb)
2475 /* Create and initialize ext4_group_info data for the given group. */
2476 int ext4_mb_add_groupinfo(struct super_block
*sb
, ext4_group_t group
,
2477 struct ext4_group_desc
*desc
)
2481 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2482 struct ext4_group_info
**meta_group_info
;
2485 * First check if this group is the first of a reserved block.
2486 * If it's true, we have to allocate a new table of pointers
2487 * to ext4_group_info structures
2489 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0) {
2490 metalen
= sizeof(*meta_group_info
) <<
2491 EXT4_DESC_PER_BLOCK_BITS(sb
);
2492 meta_group_info
= kmalloc(metalen
, GFP_KERNEL
);
2493 if (meta_group_info
== NULL
) {
2494 printk(KERN_ERR
"EXT4-fs: can't allocate mem for a "
2496 goto exit_meta_group_info
;
2498 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)] =
2503 * calculate needed size. if change bb_counters size,
2504 * don't forget about ext4_mb_generate_buddy()
2506 len
= offsetof(typeof(**meta_group_info
),
2507 bb_counters
[sb
->s_blocksize_bits
+ 2]);
2510 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)];
2511 i
= group
& (EXT4_DESC_PER_BLOCK(sb
) - 1);
2513 meta_group_info
[i
] = kzalloc(len
, GFP_KERNEL
);
2514 if (meta_group_info
[i
] == NULL
) {
2515 printk(KERN_ERR
"EXT4-fs: can't allocate buddy mem\n");
2516 goto exit_group_info
;
2518 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
,
2519 &(meta_group_info
[i
]->bb_state
));
2522 * initialize bb_free to be able to skip
2523 * empty groups without initialization
2525 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2526 meta_group_info
[i
]->bb_free
=
2527 ext4_free_blocks_after_init(sb
, group
, desc
);
2529 meta_group_info
[i
]->bb_free
=
2530 ext4_free_blks_count(sb
, desc
);
2533 INIT_LIST_HEAD(&meta_group_info
[i
]->bb_prealloc_list
);
2534 init_rwsem(&meta_group_info
[i
]->alloc_sem
);
2535 meta_group_info
[i
]->bb_free_root
.rb_node
= NULL
;;
2539 struct buffer_head
*bh
;
2540 meta_group_info
[i
]->bb_bitmap
=
2541 kmalloc(sb
->s_blocksize
, GFP_KERNEL
);
2542 BUG_ON(meta_group_info
[i
]->bb_bitmap
== NULL
);
2543 bh
= ext4_read_block_bitmap(sb
, group
);
2545 memcpy(meta_group_info
[i
]->bb_bitmap
, bh
->b_data
,
2554 /* If a meta_group_info table has been allocated, release it now */
2555 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0)
2556 kfree(sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)]);
2557 exit_meta_group_info
:
2559 } /* ext4_mb_add_groupinfo */
2562 * Update an existing group.
2563 * This function is used for online resize
2565 void ext4_mb_update_group_info(struct ext4_group_info
*grp
, ext4_grpblk_t add
)
2567 grp
->bb_free
+= add
;
2570 static int ext4_mb_init_backend(struct super_block
*sb
)
2572 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
2574 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2575 struct ext4_super_block
*es
= sbi
->s_es
;
2576 int num_meta_group_infos
;
2577 int num_meta_group_infos_max
;
2579 struct ext4_group_desc
*desc
;
2581 /* This is the number of blocks used by GDT */
2582 num_meta_group_infos
= (ngroups
+ EXT4_DESC_PER_BLOCK(sb
) -
2583 1) >> EXT4_DESC_PER_BLOCK_BITS(sb
);
2586 * This is the total number of blocks used by GDT including
2587 * the number of reserved blocks for GDT.
2588 * The s_group_info array is allocated with this value
2589 * to allow a clean online resize without a complex
2590 * manipulation of pointer.
2591 * The drawback is the unused memory when no resize
2592 * occurs but it's very low in terms of pages
2593 * (see comments below)
2594 * Need to handle this properly when META_BG resizing is allowed
2596 num_meta_group_infos_max
= num_meta_group_infos
+
2597 le16_to_cpu(es
->s_reserved_gdt_blocks
);
2600 * array_size is the size of s_group_info array. We round it
2601 * to the next power of two because this approximation is done
2602 * internally by kmalloc so we can have some more memory
2603 * for free here (e.g. may be used for META_BG resize).
2606 while (array_size
< sizeof(*sbi
->s_group_info
) *
2607 num_meta_group_infos_max
)
2608 array_size
= array_size
<< 1;
2609 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2610 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2611 * So a two level scheme suffices for now. */
2612 sbi
->s_group_info
= kmalloc(array_size
, GFP_KERNEL
);
2613 if (sbi
->s_group_info
== NULL
) {
2614 printk(KERN_ERR
"EXT4-fs: can't allocate buddy meta group\n");
2617 sbi
->s_buddy_cache
= new_inode(sb
);
2618 if (sbi
->s_buddy_cache
== NULL
) {
2619 printk(KERN_ERR
"EXT4-fs: can't get new inode\n");
2622 EXT4_I(sbi
->s_buddy_cache
)->i_disksize
= 0;
2623 for (i
= 0; i
< ngroups
; i
++) {
2624 desc
= ext4_get_group_desc(sb
, i
, NULL
);
2627 "EXT4-fs: can't read descriptor %u\n", i
);
2630 if (ext4_mb_add_groupinfo(sb
, i
, desc
) != 0)
2638 kfree(ext4_get_group_info(sb
, i
));
2639 i
= num_meta_group_infos
;
2641 kfree(sbi
->s_group_info
[i
]);
2642 iput(sbi
->s_buddy_cache
);
2644 kfree(sbi
->s_group_info
);
2648 int ext4_mb_init(struct super_block
*sb
, int needs_recovery
)
2650 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2656 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(unsigned short);
2658 sbi
->s_mb_offsets
= kmalloc(i
, GFP_KERNEL
);
2659 if (sbi
->s_mb_offsets
== NULL
) {
2663 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(unsigned int);
2664 sbi
->s_mb_maxs
= kmalloc(i
, GFP_KERNEL
);
2665 if (sbi
->s_mb_maxs
== NULL
) {
2666 kfree(sbi
->s_mb_offsets
);
2670 /* order 0 is regular bitmap */
2671 sbi
->s_mb_maxs
[0] = sb
->s_blocksize
<< 3;
2672 sbi
->s_mb_offsets
[0] = 0;
2676 max
= sb
->s_blocksize
<< 2;
2678 sbi
->s_mb_offsets
[i
] = offset
;
2679 sbi
->s_mb_maxs
[i
] = max
;
2680 offset
+= 1 << (sb
->s_blocksize_bits
- i
);
2683 } while (i
<= sb
->s_blocksize_bits
+ 1);
2685 /* init file for buddy data */
2686 ret
= ext4_mb_init_backend(sb
);
2688 kfree(sbi
->s_mb_offsets
);
2689 kfree(sbi
->s_mb_maxs
);
2693 spin_lock_init(&sbi
->s_md_lock
);
2694 spin_lock_init(&sbi
->s_bal_lock
);
2696 sbi
->s_mb_max_to_scan
= MB_DEFAULT_MAX_TO_SCAN
;
2697 sbi
->s_mb_min_to_scan
= MB_DEFAULT_MIN_TO_SCAN
;
2698 sbi
->s_mb_stats
= MB_DEFAULT_STATS
;
2699 sbi
->s_mb_stream_request
= MB_DEFAULT_STREAM_THRESHOLD
;
2700 sbi
->s_mb_order2_reqs
= MB_DEFAULT_ORDER2_REQS
;
2701 sbi
->s_mb_history_filter
= EXT4_MB_HISTORY_DEFAULT
;
2702 sbi
->s_mb_group_prealloc
= MB_DEFAULT_GROUP_PREALLOC
;
2704 sbi
->s_locality_groups
= alloc_percpu(struct ext4_locality_group
);
2705 if (sbi
->s_locality_groups
== NULL
) {
2706 kfree(sbi
->s_mb_offsets
);
2707 kfree(sbi
->s_mb_maxs
);
2710 for_each_possible_cpu(i
) {
2711 struct ext4_locality_group
*lg
;
2712 lg
= per_cpu_ptr(sbi
->s_locality_groups
, i
);
2713 mutex_init(&lg
->lg_mutex
);
2714 for (j
= 0; j
< PREALLOC_TB_SIZE
; j
++)
2715 INIT_LIST_HEAD(&lg
->lg_prealloc_list
[j
]);
2716 spin_lock_init(&lg
->lg_prealloc_lock
);
2719 ext4_mb_history_init(sb
);
2722 sbi
->s_journal
->j_commit_callback
= release_blocks_on_commit
;
2724 printk(KERN_INFO
"EXT4-fs: mballoc enabled\n");
2728 /* need to called with the ext4 group lock held */
2729 static void ext4_mb_cleanup_pa(struct ext4_group_info
*grp
)
2731 struct ext4_prealloc_space
*pa
;
2732 struct list_head
*cur
, *tmp
;
2735 list_for_each_safe(cur
, tmp
, &grp
->bb_prealloc_list
) {
2736 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
2737 list_del(&pa
->pa_group_list
);
2739 kmem_cache_free(ext4_pspace_cachep
, pa
);
2742 mb_debug("mballoc: %u PAs left\n", count
);
2746 int ext4_mb_release(struct super_block
*sb
)
2748 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
2750 int num_meta_group_infos
;
2751 struct ext4_group_info
*grinfo
;
2752 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2754 if (sbi
->s_group_info
) {
2755 for (i
= 0; i
< ngroups
; i
++) {
2756 grinfo
= ext4_get_group_info(sb
, i
);
2758 kfree(grinfo
->bb_bitmap
);
2760 ext4_lock_group(sb
, i
);
2761 ext4_mb_cleanup_pa(grinfo
);
2762 ext4_unlock_group(sb
, i
);
2765 num_meta_group_infos
= (ngroups
+
2766 EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2767 EXT4_DESC_PER_BLOCK_BITS(sb
);
2768 for (i
= 0; i
< num_meta_group_infos
; i
++)
2769 kfree(sbi
->s_group_info
[i
]);
2770 kfree(sbi
->s_group_info
);
2772 kfree(sbi
->s_mb_offsets
);
2773 kfree(sbi
->s_mb_maxs
);
2774 if (sbi
->s_buddy_cache
)
2775 iput(sbi
->s_buddy_cache
);
2776 if (sbi
->s_mb_stats
) {
2778 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2779 atomic_read(&sbi
->s_bal_allocated
),
2780 atomic_read(&sbi
->s_bal_reqs
),
2781 atomic_read(&sbi
->s_bal_success
));
2783 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2784 "%u 2^N hits, %u breaks, %u lost\n",
2785 atomic_read(&sbi
->s_bal_ex_scanned
),
2786 atomic_read(&sbi
->s_bal_goals
),
2787 atomic_read(&sbi
->s_bal_2orders
),
2788 atomic_read(&sbi
->s_bal_breaks
),
2789 atomic_read(&sbi
->s_mb_lost_chunks
));
2791 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2792 sbi
->s_mb_buddies_generated
++,
2793 sbi
->s_mb_generation_time
);
2795 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2796 atomic_read(&sbi
->s_mb_preallocated
),
2797 atomic_read(&sbi
->s_mb_discarded
));
2800 free_percpu(sbi
->s_locality_groups
);
2801 ext4_mb_history_release(sb
);
2807 * This function is called by the jbd2 layer once the commit has finished,
2808 * so we know we can free the blocks that were released with that commit.
2810 static void release_blocks_on_commit(journal_t
*journal
, transaction_t
*txn
)
2812 struct super_block
*sb
= journal
->j_private
;
2813 struct ext4_buddy e4b
;
2814 struct ext4_group_info
*db
;
2815 int err
, count
= 0, count2
= 0;
2816 struct ext4_free_data
*entry
;
2817 ext4_fsblk_t discard_block
;
2818 struct list_head
*l
, *ltmp
;
2820 list_for_each_safe(l
, ltmp
, &txn
->t_private_list
) {
2821 entry
= list_entry(l
, struct ext4_free_data
, list
);
2823 mb_debug("gonna free %u blocks in group %u (0x%p):",
2824 entry
->count
, entry
->group
, entry
);
2826 err
= ext4_mb_load_buddy(sb
, entry
->group
, &e4b
);
2827 /* we expect to find existing buddy because it's pinned */
2831 /* there are blocks to put in buddy to make them really free */
2832 count
+= entry
->count
;
2834 ext4_lock_group(sb
, entry
->group
);
2835 /* Take it out of per group rb tree */
2836 rb_erase(&entry
->node
, &(db
->bb_free_root
));
2837 mb_free_blocks(NULL
, &e4b
, entry
->start_blk
, entry
->count
);
2839 if (!db
->bb_free_root
.rb_node
) {
2840 /* No more items in the per group rb tree
2841 * balance refcounts from ext4_mb_free_metadata()
2843 page_cache_release(e4b
.bd_buddy_page
);
2844 page_cache_release(e4b
.bd_bitmap_page
);
2846 ext4_unlock_group(sb
, entry
->group
);
2847 discard_block
= (ext4_fsblk_t
) entry
->group
* EXT4_BLOCKS_PER_GROUP(sb
)
2849 + le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
2850 trace_ext4_discard_blocks(sb
, (unsigned long long)discard_block
,
2852 sb_issue_discard(sb
, discard_block
, entry
->count
);
2854 kmem_cache_free(ext4_free_ext_cachep
, entry
);
2855 ext4_mb_release_desc(&e4b
);
2858 mb_debug("freed %u blocks in %u structures\n", count
, count2
);
2861 int __init
init_ext4_mballoc(void)
2863 ext4_pspace_cachep
=
2864 kmem_cache_create("ext4_prealloc_space",
2865 sizeof(struct ext4_prealloc_space
),
2866 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2867 if (ext4_pspace_cachep
== NULL
)
2871 kmem_cache_create("ext4_alloc_context",
2872 sizeof(struct ext4_allocation_context
),
2873 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2874 if (ext4_ac_cachep
== NULL
) {
2875 kmem_cache_destroy(ext4_pspace_cachep
);
2879 ext4_free_ext_cachep
=
2880 kmem_cache_create("ext4_free_block_extents",
2881 sizeof(struct ext4_free_data
),
2882 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2883 if (ext4_free_ext_cachep
== NULL
) {
2884 kmem_cache_destroy(ext4_pspace_cachep
);
2885 kmem_cache_destroy(ext4_ac_cachep
);
2891 void exit_ext4_mballoc(void)
2894 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2895 * before destroying the slab cache.
2898 kmem_cache_destroy(ext4_pspace_cachep
);
2899 kmem_cache_destroy(ext4_ac_cachep
);
2900 kmem_cache_destroy(ext4_free_ext_cachep
);
2905 * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
2906 * Returns 0 if success or error code
2908 static noinline_for_stack
int
2909 ext4_mb_mark_diskspace_used(struct ext4_allocation_context
*ac
,
2910 handle_t
*handle
, unsigned int reserv_blks
)
2912 struct buffer_head
*bitmap_bh
= NULL
;
2913 struct ext4_super_block
*es
;
2914 struct ext4_group_desc
*gdp
;
2915 struct buffer_head
*gdp_bh
;
2916 struct ext4_sb_info
*sbi
;
2917 struct super_block
*sb
;
2921 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
2922 BUG_ON(ac
->ac_b_ex
.fe_len
<= 0);
2930 bitmap_bh
= ext4_read_block_bitmap(sb
, ac
->ac_b_ex
.fe_group
);
2934 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
2939 gdp
= ext4_get_group_desc(sb
, ac
->ac_b_ex
.fe_group
, &gdp_bh
);
2943 ext4_debug("using block group %u(%d)\n", ac
->ac_b_ex
.fe_group
,
2944 ext4_free_blks_count(sb
, gdp
));
2946 err
= ext4_journal_get_write_access(handle
, gdp_bh
);
2950 block
= ac
->ac_b_ex
.fe_group
* EXT4_BLOCKS_PER_GROUP(sb
)
2951 + ac
->ac_b_ex
.fe_start
2952 + le32_to_cpu(es
->s_first_data_block
);
2954 len
= ac
->ac_b_ex
.fe_len
;
2955 if (!ext4_data_block_valid(sbi
, block
, len
)) {
2956 ext4_error(sb
, __func__
,
2957 "Allocating blocks %llu-%llu which overlap "
2958 "fs metadata\n", block
, block
+len
);
2959 /* File system mounted not to panic on error
2960 * Fix the bitmap and repeat the block allocation
2961 * We leak some of the blocks here.
2963 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
2964 mb_set_bits(bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,
2965 ac
->ac_b_ex
.fe_len
);
2966 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
2967 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
2973 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
2974 #ifdef AGGRESSIVE_CHECK
2977 for (i
= 0; i
< ac
->ac_b_ex
.fe_len
; i
++) {
2978 BUG_ON(mb_test_bit(ac
->ac_b_ex
.fe_start
+ i
,
2979 bitmap_bh
->b_data
));
2983 mb_set_bits(bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,ac
->ac_b_ex
.fe_len
);
2984 if (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2985 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT
);
2986 ext4_free_blks_set(sb
, gdp
,
2987 ext4_free_blocks_after_init(sb
,
2988 ac
->ac_b_ex
.fe_group
, gdp
));
2990 len
= ext4_free_blks_count(sb
, gdp
) - ac
->ac_b_ex
.fe_len
;
2991 ext4_free_blks_set(sb
, gdp
, len
);
2992 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, ac
->ac_b_ex
.fe_group
, gdp
);
2994 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
2995 percpu_counter_sub(&sbi
->s_freeblocks_counter
, ac
->ac_b_ex
.fe_len
);
2997 * Now reduce the dirty block count also. Should not go negative
2999 if (!(ac
->ac_flags
& EXT4_MB_DELALLOC_RESERVED
))
3000 /* release all the reserved blocks if non delalloc */
3001 percpu_counter_sub(&sbi
->s_dirtyblocks_counter
, reserv_blks
);
3003 percpu_counter_sub(&sbi
->s_dirtyblocks_counter
,
3004 ac
->ac_b_ex
.fe_len
);
3005 /* convert reserved quota blocks to real quota blocks */
3006 vfs_dq_claim_block(ac
->ac_inode
, ac
->ac_b_ex
.fe_len
);
3009 if (sbi
->s_log_groups_per_flex
) {
3010 ext4_group_t flex_group
= ext4_flex_group(sbi
,
3011 ac
->ac_b_ex
.fe_group
);
3012 atomic_sub(ac
->ac_b_ex
.fe_len
,
3013 &sbi
->s_flex_groups
[flex_group
].free_blocks
);
3016 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
3019 err
= ext4_handle_dirty_metadata(handle
, NULL
, gdp_bh
);
3028 * here we normalize request for locality group
3029 * Group request are normalized to s_strip size if we set the same via mount
3030 * option. If not we set it to s_mb_group_prealloc which can be configured via
3031 * /sys/fs/ext4/<partition>/mb_group_prealloc
3033 * XXX: should we try to preallocate more than the group has now?
3035 static void ext4_mb_normalize_group_request(struct ext4_allocation_context
*ac
)
3037 struct super_block
*sb
= ac
->ac_sb
;
3038 struct ext4_locality_group
*lg
= ac
->ac_lg
;
3041 if (EXT4_SB(sb
)->s_stripe
)
3042 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_stripe
;
3044 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_mb_group_prealloc
;
3045 mb_debug("#%u: goal %u blocks for locality group\n",
3046 current
->pid
, ac
->ac_g_ex
.fe_len
);
3050 * Normalization means making request better in terms of
3051 * size and alignment
3053 static noinline_for_stack
void
3054 ext4_mb_normalize_request(struct ext4_allocation_context
*ac
,
3055 struct ext4_allocation_request
*ar
)
3059 loff_t size
, orig_size
, start_off
;
3060 ext4_lblk_t start
, orig_start
;
3061 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3062 struct ext4_prealloc_space
*pa
;
3064 /* do normalize only data requests, metadata requests
3065 do not need preallocation */
3066 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3069 /* sometime caller may want exact blocks */
3070 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
3073 /* caller may indicate that preallocation isn't
3074 * required (it's a tail, for example) */
3075 if (ac
->ac_flags
& EXT4_MB_HINT_NOPREALLOC
)
3078 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
) {
3079 ext4_mb_normalize_group_request(ac
);
3083 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
3085 /* first, let's learn actual file size
3086 * given current request is allocated */
3087 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
3088 size
= size
<< bsbits
;
3089 if (size
< i_size_read(ac
->ac_inode
))
3090 size
= i_size_read(ac
->ac_inode
);
3092 /* max size of free chunks */
3095 #define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3096 (req <= (size) || max <= (chunk_size))
3098 /* first, try to predict filesize */
3099 /* XXX: should this table be tunable? */
3101 if (size
<= 16 * 1024) {
3103 } else if (size
<= 32 * 1024) {
3105 } else if (size
<= 64 * 1024) {
3107 } else if (size
<= 128 * 1024) {
3109 } else if (size
<= 256 * 1024) {
3111 } else if (size
<= 512 * 1024) {
3113 } else if (size
<= 1024 * 1024) {
3115 } else if (NRL_CHECK_SIZE(size
, 4 * 1024 * 1024, max
, 2 * 1024)) {
3116 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3117 (21 - bsbits
)) << 21;
3118 size
= 2 * 1024 * 1024;
3119 } else if (NRL_CHECK_SIZE(size
, 8 * 1024 * 1024, max
, 4 * 1024)) {
3120 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3121 (22 - bsbits
)) << 22;
3122 size
= 4 * 1024 * 1024;
3123 } else if (NRL_CHECK_SIZE(ac
->ac_o_ex
.fe_len
,
3124 (8<<20)>>bsbits
, max
, 8 * 1024)) {
3125 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3126 (23 - bsbits
)) << 23;
3127 size
= 8 * 1024 * 1024;
3129 start_off
= (loff_t
)ac
->ac_o_ex
.fe_logical
<< bsbits
;
3130 size
= ac
->ac_o_ex
.fe_len
<< bsbits
;
3132 orig_size
= size
= size
>> bsbits
;
3133 orig_start
= start
= start_off
>> bsbits
;
3135 /* don't cover already allocated blocks in selected range */
3136 if (ar
->pleft
&& start
<= ar
->lleft
) {
3137 size
-= ar
->lleft
+ 1 - start
;
3138 start
= ar
->lleft
+ 1;
3140 if (ar
->pright
&& start
+ size
- 1 >= ar
->lright
)
3141 size
-= start
+ size
- ar
->lright
;
3145 /* check we don't cross already preallocated blocks */
3147 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3152 spin_lock(&pa
->pa_lock
);
3153 if (pa
->pa_deleted
) {
3154 spin_unlock(&pa
->pa_lock
);
3158 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
3160 /* PA must not overlap original request */
3161 BUG_ON(!(ac
->ac_o_ex
.fe_logical
>= pa_end
||
3162 ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
));
3164 /* skip PA normalized request doesn't overlap with */
3165 if (pa
->pa_lstart
>= end
) {
3166 spin_unlock(&pa
->pa_lock
);
3169 if (pa_end
<= start
) {
3170 spin_unlock(&pa
->pa_lock
);
3173 BUG_ON(pa
->pa_lstart
<= start
&& pa_end
>= end
);
3175 if (pa_end
<= ac
->ac_o_ex
.fe_logical
) {
3176 BUG_ON(pa_end
< start
);
3180 if (pa
->pa_lstart
> ac
->ac_o_ex
.fe_logical
) {
3181 BUG_ON(pa
->pa_lstart
> end
);
3182 end
= pa
->pa_lstart
;
3184 spin_unlock(&pa
->pa_lock
);
3189 /* XXX: extra loop to check we really don't overlap preallocations */
3191 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3193 spin_lock(&pa
->pa_lock
);
3194 if (pa
->pa_deleted
== 0) {
3195 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
3196 BUG_ON(!(start
>= pa_end
|| end
<= pa
->pa_lstart
));
3198 spin_unlock(&pa
->pa_lock
);
3202 if (start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3203 start
> ac
->ac_o_ex
.fe_logical
) {
3204 printk(KERN_ERR
"start %lu, size %lu, fe_logical %lu\n",
3205 (unsigned long) start
, (unsigned long) size
,
3206 (unsigned long) ac
->ac_o_ex
.fe_logical
);
3208 BUG_ON(start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3209 start
> ac
->ac_o_ex
.fe_logical
);
3210 BUG_ON(size
<= 0 || size
> EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
3212 /* now prepare goal request */
3214 /* XXX: is it better to align blocks WRT to logical
3215 * placement or satisfy big request as is */
3216 ac
->ac_g_ex
.fe_logical
= start
;
3217 ac
->ac_g_ex
.fe_len
= size
;
3219 /* define goal start in order to merge */
3220 if (ar
->pright
&& (ar
->lright
== (start
+ size
))) {
3221 /* merge to the right */
3222 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pright
- size
,
3223 &ac
->ac_f_ex
.fe_group
,
3224 &ac
->ac_f_ex
.fe_start
);
3225 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3227 if (ar
->pleft
&& (ar
->lleft
+ 1 == start
)) {
3228 /* merge to the left */
3229 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pleft
+ 1,
3230 &ac
->ac_f_ex
.fe_group
,
3231 &ac
->ac_f_ex
.fe_start
);
3232 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3235 mb_debug("goal: %u(was %u) blocks at %u\n", (unsigned) size
,
3236 (unsigned) orig_size
, (unsigned) start
);
3239 static void ext4_mb_collect_stats(struct ext4_allocation_context
*ac
)
3241 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3243 if (sbi
->s_mb_stats
&& ac
->ac_g_ex
.fe_len
> 1) {
3244 atomic_inc(&sbi
->s_bal_reqs
);
3245 atomic_add(ac
->ac_b_ex
.fe_len
, &sbi
->s_bal_allocated
);
3246 if (ac
->ac_o_ex
.fe_len
>= ac
->ac_g_ex
.fe_len
)
3247 atomic_inc(&sbi
->s_bal_success
);
3248 atomic_add(ac
->ac_found
, &sbi
->s_bal_ex_scanned
);
3249 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
3250 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
3251 atomic_inc(&sbi
->s_bal_goals
);
3252 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
)
3253 atomic_inc(&sbi
->s_bal_breaks
);
3256 ext4_mb_store_history(ac
);
3260 * use blocks preallocated to inode
3262 static void ext4_mb_use_inode_pa(struct ext4_allocation_context
*ac
,
3263 struct ext4_prealloc_space
*pa
)
3269 /* found preallocated blocks, use them */
3270 start
= pa
->pa_pstart
+ (ac
->ac_o_ex
.fe_logical
- pa
->pa_lstart
);
3271 end
= min(pa
->pa_pstart
+ pa
->pa_len
, start
+ ac
->ac_o_ex
.fe_len
);
3273 ext4_get_group_no_and_offset(ac
->ac_sb
, start
, &ac
->ac_b_ex
.fe_group
,
3274 &ac
->ac_b_ex
.fe_start
);
3275 ac
->ac_b_ex
.fe_len
= len
;
3276 ac
->ac_status
= AC_STATUS_FOUND
;
3279 BUG_ON(start
< pa
->pa_pstart
);
3280 BUG_ON(start
+ len
> pa
->pa_pstart
+ pa
->pa_len
);
3281 BUG_ON(pa
->pa_free
< len
);
3284 mb_debug("use %llu/%u from inode pa %p\n", start
, len
, pa
);
3288 * use blocks preallocated to locality group
3290 static void ext4_mb_use_group_pa(struct ext4_allocation_context
*ac
,
3291 struct ext4_prealloc_space
*pa
)
3293 unsigned int len
= ac
->ac_o_ex
.fe_len
;
3295 ext4_get_group_no_and_offset(ac
->ac_sb
, pa
->pa_pstart
,
3296 &ac
->ac_b_ex
.fe_group
,
3297 &ac
->ac_b_ex
.fe_start
);
3298 ac
->ac_b_ex
.fe_len
= len
;
3299 ac
->ac_status
= AC_STATUS_FOUND
;
3302 /* we don't correct pa_pstart or pa_plen here to avoid
3303 * possible race when the group is being loaded concurrently
3304 * instead we correct pa later, after blocks are marked
3305 * in on-disk bitmap -- see ext4_mb_release_context()
3306 * Other CPUs are prevented from allocating from this pa by lg_mutex
3308 mb_debug("use %u/%u from group pa %p\n", pa
->pa_lstart
-len
, len
, pa
);
3312 * Return the prealloc space that have minimal distance
3313 * from the goal block. @cpa is the prealloc
3314 * space that is having currently known minimal distance
3315 * from the goal block.
3317 static struct ext4_prealloc_space
*
3318 ext4_mb_check_group_pa(ext4_fsblk_t goal_block
,
3319 struct ext4_prealloc_space
*pa
,
3320 struct ext4_prealloc_space
*cpa
)
3322 ext4_fsblk_t cur_distance
, new_distance
;
3325 atomic_inc(&pa
->pa_count
);
3328 cur_distance
= abs(goal_block
- cpa
->pa_pstart
);
3329 new_distance
= abs(goal_block
- pa
->pa_pstart
);
3331 if (cur_distance
< new_distance
)
3334 /* drop the previous reference */
3335 atomic_dec(&cpa
->pa_count
);
3336 atomic_inc(&pa
->pa_count
);
3341 * search goal blocks in preallocated space
3343 static noinline_for_stack
int
3344 ext4_mb_use_preallocated(struct ext4_allocation_context
*ac
)
3347 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3348 struct ext4_locality_group
*lg
;
3349 struct ext4_prealloc_space
*pa
, *cpa
= NULL
;
3350 ext4_fsblk_t goal_block
;
3352 /* only data can be preallocated */
3353 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3356 /* first, try per-file preallocation */
3358 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3360 /* all fields in this condition don't change,
3361 * so we can skip locking for them */
3362 if (ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
||
3363 ac
->ac_o_ex
.fe_logical
>= pa
->pa_lstart
+ pa
->pa_len
)
3366 /* found preallocated blocks, use them */
3367 spin_lock(&pa
->pa_lock
);
3368 if (pa
->pa_deleted
== 0 && pa
->pa_free
) {
3369 atomic_inc(&pa
->pa_count
);
3370 ext4_mb_use_inode_pa(ac
, pa
);
3371 spin_unlock(&pa
->pa_lock
);
3372 ac
->ac_criteria
= 10;
3376 spin_unlock(&pa
->pa_lock
);
3380 /* can we use group allocation? */
3381 if (!(ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
))
3384 /* inode may have no locality group for some reason */
3388 order
= fls(ac
->ac_o_ex
.fe_len
) - 1;
3389 if (order
> PREALLOC_TB_SIZE
- 1)
3390 /* The max size of hash table is PREALLOC_TB_SIZE */
3391 order
= PREALLOC_TB_SIZE
- 1;
3393 goal_block
= ac
->ac_g_ex
.fe_group
* EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
) +
3394 ac
->ac_g_ex
.fe_start
+
3395 le32_to_cpu(EXT4_SB(ac
->ac_sb
)->s_es
->s_first_data_block
);
3397 * search for the prealloc space that is having
3398 * minimal distance from the goal block.
3400 for (i
= order
; i
< PREALLOC_TB_SIZE
; i
++) {
3402 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[i
],
3404 spin_lock(&pa
->pa_lock
);
3405 if (pa
->pa_deleted
== 0 &&
3406 pa
->pa_free
>= ac
->ac_o_ex
.fe_len
) {
3408 cpa
= ext4_mb_check_group_pa(goal_block
,
3411 spin_unlock(&pa
->pa_lock
);
3416 ext4_mb_use_group_pa(ac
, cpa
);
3417 ac
->ac_criteria
= 20;
3424 * the function goes through all block freed in the group
3425 * but not yet committed and marks them used in in-core bitmap.
3426 * buddy must be generated from this bitmap
3427 * Need to be called with the ext4 group lock held
3429 static void ext4_mb_generate_from_freelist(struct super_block
*sb
, void *bitmap
,
3433 struct ext4_group_info
*grp
;
3434 struct ext4_free_data
*entry
;
3436 grp
= ext4_get_group_info(sb
, group
);
3437 n
= rb_first(&(grp
->bb_free_root
));
3440 entry
= rb_entry(n
, struct ext4_free_data
, node
);
3441 mb_set_bits(bitmap
, entry
->start_blk
, entry
->count
);
3448 * the function goes through all preallocation in this group and marks them
3449 * used in in-core bitmap. buddy must be generated from this bitmap
3450 * Need to be called with ext4 group lock held
3452 static noinline_for_stack
3453 void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
3456 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3457 struct ext4_prealloc_space
*pa
;
3458 struct list_head
*cur
;
3459 ext4_group_t groupnr
;
3460 ext4_grpblk_t start
;
3461 int preallocated
= 0;
3465 /* all form of preallocation discards first load group,
3466 * so the only competing code is preallocation use.
3467 * we don't need any locking here
3468 * notice we do NOT ignore preallocations with pa_deleted
3469 * otherwise we could leave used blocks available for
3470 * allocation in buddy when concurrent ext4_mb_put_pa()
3471 * is dropping preallocation
3473 list_for_each(cur
, &grp
->bb_prealloc_list
) {
3474 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
3475 spin_lock(&pa
->pa_lock
);
3476 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
3479 spin_unlock(&pa
->pa_lock
);
3480 if (unlikely(len
== 0))
3482 BUG_ON(groupnr
!= group
);
3483 mb_set_bits(bitmap
, start
, len
);
3484 preallocated
+= len
;
3487 mb_debug("prellocated %u for group %u\n", preallocated
, group
);
3490 static void ext4_mb_pa_callback(struct rcu_head
*head
)
3492 struct ext4_prealloc_space
*pa
;
3493 pa
= container_of(head
, struct ext4_prealloc_space
, u
.pa_rcu
);
3494 kmem_cache_free(ext4_pspace_cachep
, pa
);
3498 * drops a reference to preallocated space descriptor
3499 * if this was the last reference and the space is consumed
3501 static void ext4_mb_put_pa(struct ext4_allocation_context
*ac
,
3502 struct super_block
*sb
, struct ext4_prealloc_space
*pa
)
3505 ext4_fsblk_t grp_blk
;
3507 if (!atomic_dec_and_test(&pa
->pa_count
) || pa
->pa_free
!= 0)
3510 /* in this short window concurrent discard can set pa_deleted */
3511 spin_lock(&pa
->pa_lock
);
3512 if (pa
->pa_deleted
== 1) {
3513 spin_unlock(&pa
->pa_lock
);
3518 spin_unlock(&pa
->pa_lock
);
3520 grp_blk
= pa
->pa_pstart
;
3522 * If doing group-based preallocation, pa_pstart may be in the
3523 * next group when pa is used up
3525 if (pa
->pa_type
== MB_GROUP_PA
)
3528 ext4_get_group_no_and_offset(sb
, grp_blk
, &grp
, NULL
);
3533 * P1 (buddy init) P2 (regular allocation)
3534 * find block B in PA
3535 * copy on-disk bitmap to buddy
3536 * mark B in on-disk bitmap
3537 * drop PA from group
3538 * mark all PAs in buddy
3540 * thus, P1 initializes buddy with B available. to prevent this
3541 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3544 ext4_lock_group(sb
, grp
);
3545 list_del(&pa
->pa_group_list
);
3546 ext4_unlock_group(sb
, grp
);
3548 spin_lock(pa
->pa_obj_lock
);
3549 list_del_rcu(&pa
->pa_inode_list
);
3550 spin_unlock(pa
->pa_obj_lock
);
3552 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3556 * creates new preallocated space for given inode
3558 static noinline_for_stack
int
3559 ext4_mb_new_inode_pa(struct ext4_allocation_context
*ac
)
3561 struct super_block
*sb
= ac
->ac_sb
;
3562 struct ext4_prealloc_space
*pa
;
3563 struct ext4_group_info
*grp
;
3564 struct ext4_inode_info
*ei
;
3566 /* preallocate only when found space is larger then requested */
3567 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3568 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3569 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3571 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3575 if (ac
->ac_b_ex
.fe_len
< ac
->ac_g_ex
.fe_len
) {
3581 /* we can't allocate as much as normalizer wants.
3582 * so, found space must get proper lstart
3583 * to cover original request */
3584 BUG_ON(ac
->ac_g_ex
.fe_logical
> ac
->ac_o_ex
.fe_logical
);
3585 BUG_ON(ac
->ac_g_ex
.fe_len
< ac
->ac_o_ex
.fe_len
);
3587 /* we're limited by original request in that
3588 * logical block must be covered any way
3589 * winl is window we can move our chunk within */
3590 winl
= ac
->ac_o_ex
.fe_logical
- ac
->ac_g_ex
.fe_logical
;
3592 /* also, we should cover whole original request */
3593 wins
= ac
->ac_b_ex
.fe_len
- ac
->ac_o_ex
.fe_len
;
3595 /* the smallest one defines real window */
3596 win
= min(winl
, wins
);
3598 offs
= ac
->ac_o_ex
.fe_logical
% ac
->ac_b_ex
.fe_len
;
3599 if (offs
&& offs
< win
)
3602 ac
->ac_b_ex
.fe_logical
= ac
->ac_o_ex
.fe_logical
- win
;
3603 BUG_ON(ac
->ac_o_ex
.fe_logical
< ac
->ac_b_ex
.fe_logical
);
3604 BUG_ON(ac
->ac_o_ex
.fe_len
> ac
->ac_b_ex
.fe_len
);
3607 /* preallocation can change ac_b_ex, thus we store actually
3608 * allocated blocks for history */
3609 ac
->ac_f_ex
= ac
->ac_b_ex
;
3611 pa
->pa_lstart
= ac
->ac_b_ex
.fe_logical
;
3612 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3613 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3614 pa
->pa_free
= pa
->pa_len
;
3615 atomic_set(&pa
->pa_count
, 1);
3616 spin_lock_init(&pa
->pa_lock
);
3617 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3618 INIT_LIST_HEAD(&pa
->pa_group_list
);
3620 pa
->pa_type
= MB_INODE_PA
;
3622 mb_debug("new inode pa %p: %llu/%u for %u\n", pa
,
3623 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3624 trace_ext4_mb_new_inode_pa(ac
, pa
);
3626 ext4_mb_use_inode_pa(ac
, pa
);
3627 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3629 ei
= EXT4_I(ac
->ac_inode
);
3630 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3632 pa
->pa_obj_lock
= &ei
->i_prealloc_lock
;
3633 pa
->pa_inode
= ac
->ac_inode
;
3635 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3636 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3637 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3639 spin_lock(pa
->pa_obj_lock
);
3640 list_add_rcu(&pa
->pa_inode_list
, &ei
->i_prealloc_list
);
3641 spin_unlock(pa
->pa_obj_lock
);
3647 * creates new preallocated space for locality group inodes belongs to
3649 static noinline_for_stack
int
3650 ext4_mb_new_group_pa(struct ext4_allocation_context
*ac
)
3652 struct super_block
*sb
= ac
->ac_sb
;
3653 struct ext4_locality_group
*lg
;
3654 struct ext4_prealloc_space
*pa
;
3655 struct ext4_group_info
*grp
;
3657 /* preallocate only when found space is larger then requested */
3658 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3659 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3660 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3662 BUG_ON(ext4_pspace_cachep
== NULL
);
3663 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3667 /* preallocation can change ac_b_ex, thus we store actually
3668 * allocated blocks for history */
3669 ac
->ac_f_ex
= ac
->ac_b_ex
;
3671 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3672 pa
->pa_lstart
= pa
->pa_pstart
;
3673 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3674 pa
->pa_free
= pa
->pa_len
;
3675 atomic_set(&pa
->pa_count
, 1);
3676 spin_lock_init(&pa
->pa_lock
);
3677 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3678 INIT_LIST_HEAD(&pa
->pa_group_list
);
3680 pa
->pa_type
= MB_GROUP_PA
;
3682 mb_debug("new group pa %p: %llu/%u for %u\n", pa
,
3683 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3684 trace_ext4_mb_new_group_pa(ac
, pa
);
3686 ext4_mb_use_group_pa(ac
, pa
);
3687 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3689 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3693 pa
->pa_obj_lock
= &lg
->lg_prealloc_lock
;
3694 pa
->pa_inode
= NULL
;
3696 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3697 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3698 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3701 * We will later add the new pa to the right bucket
3702 * after updating the pa_free in ext4_mb_release_context
3707 static int ext4_mb_new_preallocation(struct ext4_allocation_context
*ac
)
3711 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
3712 err
= ext4_mb_new_group_pa(ac
);
3714 err
= ext4_mb_new_inode_pa(ac
);
3719 * finds all unused blocks in on-disk bitmap, frees them in
3720 * in-core bitmap and buddy.
3721 * @pa must be unlinked from inode and group lists, so that
3722 * nobody else can find/use it.
3723 * the caller MUST hold group/inode locks.
3724 * TODO: optimize the case when there are no in-core structures yet
3726 static noinline_for_stack
int
3727 ext4_mb_release_inode_pa(struct ext4_buddy
*e4b
, struct buffer_head
*bitmap_bh
,
3728 struct ext4_prealloc_space
*pa
,
3729 struct ext4_allocation_context
*ac
)
3731 struct super_block
*sb
= e4b
->bd_sb
;
3732 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3737 unsigned long long grp_blk_start
;
3742 BUG_ON(pa
->pa_deleted
== 0);
3743 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3744 grp_blk_start
= pa
->pa_pstart
- bit
;
3745 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3746 end
= bit
+ pa
->pa_len
;
3750 ac
->ac_inode
= pa
->pa_inode
;
3751 ac
->ac_op
= EXT4_MB_HISTORY_DISCARD
;
3755 bit
= mb_find_next_zero_bit(bitmap_bh
->b_data
, end
, bit
);
3758 next
= mb_find_next_bit(bitmap_bh
->b_data
, end
, bit
);
3759 start
= group
* EXT4_BLOCKS_PER_GROUP(sb
) + bit
+
3760 le32_to_cpu(sbi
->s_es
->s_first_data_block
);
3761 mb_debug(" free preallocated %u/%u in group %u\n",
3762 (unsigned) start
, (unsigned) next
- bit
,
3767 ac
->ac_b_ex
.fe_group
= group
;
3768 ac
->ac_b_ex
.fe_start
= bit
;
3769 ac
->ac_b_ex
.fe_len
= next
- bit
;
3770 ac
->ac_b_ex
.fe_logical
= 0;
3771 ext4_mb_store_history(ac
);
3774 trace_ext4_mb_release_inode_pa(ac
, pa
, grp_blk_start
+ bit
,
3776 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, next
- bit
);
3779 if (free
!= pa
->pa_free
) {
3780 printk(KERN_CRIT
"pa %p: logic %lu, phys. %lu, len %lu\n",
3781 pa
, (unsigned long) pa
->pa_lstart
,
3782 (unsigned long) pa
->pa_pstart
,
3783 (unsigned long) pa
->pa_len
);
3784 ext4_grp_locked_error(sb
, group
,
3785 __func__
, "free %u, pa_free %u",
3788 * pa is already deleted so we use the value obtained
3789 * from the bitmap and continue.
3792 atomic_add(free
, &sbi
->s_mb_discarded
);
3797 static noinline_for_stack
int
3798 ext4_mb_release_group_pa(struct ext4_buddy
*e4b
,
3799 struct ext4_prealloc_space
*pa
,
3800 struct ext4_allocation_context
*ac
)
3802 struct super_block
*sb
= e4b
->bd_sb
;
3807 ac
->ac_op
= EXT4_MB_HISTORY_DISCARD
;
3809 trace_ext4_mb_release_group_pa(ac
, pa
);
3810 BUG_ON(pa
->pa_deleted
== 0);
3811 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3812 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3813 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, pa
->pa_len
);
3814 atomic_add(pa
->pa_len
, &EXT4_SB(sb
)->s_mb_discarded
);
3818 ac
->ac_inode
= NULL
;
3819 ac
->ac_b_ex
.fe_group
= group
;
3820 ac
->ac_b_ex
.fe_start
= bit
;
3821 ac
->ac_b_ex
.fe_len
= pa
->pa_len
;
3822 ac
->ac_b_ex
.fe_logical
= 0;
3823 ext4_mb_store_history(ac
);
3830 * releases all preallocations in given group
3832 * first, we need to decide discard policy:
3833 * - when do we discard
3835 * - how many do we discard
3836 * 1) how many requested
3838 static noinline_for_stack
int
3839 ext4_mb_discard_group_preallocations(struct super_block
*sb
,
3840 ext4_group_t group
, int needed
)
3842 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3843 struct buffer_head
*bitmap_bh
= NULL
;
3844 struct ext4_prealloc_space
*pa
, *tmp
;
3845 struct ext4_allocation_context
*ac
;
3846 struct list_head list
;
3847 struct ext4_buddy e4b
;
3852 mb_debug("discard preallocation for group %u\n", group
);
3854 if (list_empty(&grp
->bb_prealloc_list
))
3857 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
3858 if (bitmap_bh
== NULL
) {
3859 ext4_error(sb
, __func__
, "Error in reading block "
3860 "bitmap for %u", group
);
3864 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
3866 ext4_error(sb
, __func__
, "Error in loading buddy "
3867 "information for %u", group
);
3873 needed
= EXT4_BLOCKS_PER_GROUP(sb
) + 1;
3875 INIT_LIST_HEAD(&list
);
3876 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
3880 ext4_lock_group(sb
, group
);
3881 list_for_each_entry_safe(pa
, tmp
,
3882 &grp
->bb_prealloc_list
, pa_group_list
) {
3883 spin_lock(&pa
->pa_lock
);
3884 if (atomic_read(&pa
->pa_count
)) {
3885 spin_unlock(&pa
->pa_lock
);
3889 if (pa
->pa_deleted
) {
3890 spin_unlock(&pa
->pa_lock
);
3894 /* seems this one can be freed ... */
3897 /* we can trust pa_free ... */
3898 free
+= pa
->pa_free
;
3900 spin_unlock(&pa
->pa_lock
);
3902 list_del(&pa
->pa_group_list
);
3903 list_add(&pa
->u
.pa_tmp_list
, &list
);
3906 /* if we still need more blocks and some PAs were used, try again */
3907 if (free
< needed
&& busy
) {
3909 ext4_unlock_group(sb
, group
);
3911 * Yield the CPU here so that we don't get soft lockup
3912 * in non preempt case.
3918 /* found anything to free? */
3919 if (list_empty(&list
)) {
3924 /* now free all selected PAs */
3925 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
3927 /* remove from object (inode or locality group) */
3928 spin_lock(pa
->pa_obj_lock
);
3929 list_del_rcu(&pa
->pa_inode_list
);
3930 spin_unlock(pa
->pa_obj_lock
);
3932 if (pa
->pa_type
== MB_GROUP_PA
)
3933 ext4_mb_release_group_pa(&e4b
, pa
, ac
);
3935 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
, ac
);
3937 list_del(&pa
->u
.pa_tmp_list
);
3938 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3942 ext4_unlock_group(sb
, group
);
3944 kmem_cache_free(ext4_ac_cachep
, ac
);
3945 ext4_mb_release_desc(&e4b
);
3951 * releases all non-used preallocated blocks for given inode
3953 * It's important to discard preallocations under i_data_sem
3954 * We don't want another block to be served from the prealloc
3955 * space when we are discarding the inode prealloc space.
3957 * FIXME!! Make sure it is valid at all the call sites
3959 void ext4_discard_preallocations(struct inode
*inode
)
3961 struct ext4_inode_info
*ei
= EXT4_I(inode
);
3962 struct super_block
*sb
= inode
->i_sb
;
3963 struct buffer_head
*bitmap_bh
= NULL
;
3964 struct ext4_prealloc_space
*pa
, *tmp
;
3965 struct ext4_allocation_context
*ac
;
3966 ext4_group_t group
= 0;
3967 struct list_head list
;
3968 struct ext4_buddy e4b
;
3971 if (!S_ISREG(inode
->i_mode
)) {
3972 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3976 mb_debug("discard preallocation for inode %lu\n", inode
->i_ino
);
3977 trace_ext4_discard_preallocations(inode
);
3979 INIT_LIST_HEAD(&list
);
3981 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
3984 ac
->ac_inode
= inode
;
3987 /* first, collect all pa's in the inode */
3988 spin_lock(&ei
->i_prealloc_lock
);
3989 while (!list_empty(&ei
->i_prealloc_list
)) {
3990 pa
= list_entry(ei
->i_prealloc_list
.next
,
3991 struct ext4_prealloc_space
, pa_inode_list
);
3992 BUG_ON(pa
->pa_obj_lock
!= &ei
->i_prealloc_lock
);
3993 spin_lock(&pa
->pa_lock
);
3994 if (atomic_read(&pa
->pa_count
)) {
3995 /* this shouldn't happen often - nobody should
3996 * use preallocation while we're discarding it */
3997 spin_unlock(&pa
->pa_lock
);
3998 spin_unlock(&ei
->i_prealloc_lock
);
3999 printk(KERN_ERR
"uh-oh! used pa while discarding\n");
4001 schedule_timeout_uninterruptible(HZ
);
4005 if (pa
->pa_deleted
== 0) {
4007 spin_unlock(&pa
->pa_lock
);
4008 list_del_rcu(&pa
->pa_inode_list
);
4009 list_add(&pa
->u
.pa_tmp_list
, &list
);
4013 /* someone is deleting pa right now */
4014 spin_unlock(&pa
->pa_lock
);
4015 spin_unlock(&ei
->i_prealloc_lock
);
4017 /* we have to wait here because pa_deleted
4018 * doesn't mean pa is already unlinked from
4019 * the list. as we might be called from
4020 * ->clear_inode() the inode will get freed
4021 * and concurrent thread which is unlinking
4022 * pa from inode's list may access already
4023 * freed memory, bad-bad-bad */
4025 /* XXX: if this happens too often, we can
4026 * add a flag to force wait only in case
4027 * of ->clear_inode(), but not in case of
4028 * regular truncate */
4029 schedule_timeout_uninterruptible(HZ
);
4032 spin_unlock(&ei
->i_prealloc_lock
);
4034 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
4035 BUG_ON(pa
->pa_type
!= MB_INODE_PA
);
4036 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
4038 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
4040 ext4_error(sb
, __func__
, "Error in loading buddy "
4041 "information for %u", group
);
4045 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
4046 if (bitmap_bh
== NULL
) {
4047 ext4_error(sb
, __func__
, "Error in reading block "
4048 "bitmap for %u", group
);
4049 ext4_mb_release_desc(&e4b
);
4053 ext4_lock_group(sb
, group
);
4054 list_del(&pa
->pa_group_list
);
4055 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
, ac
);
4056 ext4_unlock_group(sb
, group
);
4058 ext4_mb_release_desc(&e4b
);
4061 list_del(&pa
->u
.pa_tmp_list
);
4062 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4065 kmem_cache_free(ext4_ac_cachep
, ac
);
4069 * finds all preallocated spaces and return blocks being freed to them
4070 * if preallocated space becomes full (no block is used from the space)
4071 * then the function frees space in buddy
4072 * XXX: at the moment, truncate (which is the only way to free blocks)
4073 * discards all preallocations
4075 static void ext4_mb_return_to_preallocation(struct inode
*inode
,
4076 struct ext4_buddy
*e4b
,
4077 sector_t block
, int count
)
4079 BUG_ON(!list_empty(&EXT4_I(inode
)->i_prealloc_list
));
4082 static void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
4084 struct super_block
*sb
= ac
->ac_sb
;
4085 ext4_group_t ngroups
, i
;
4087 printk(KERN_ERR
"EXT4-fs: Can't allocate:"
4088 " Allocation context details:\n");
4089 printk(KERN_ERR
"EXT4-fs: status %d flags %d\n",
4090 ac
->ac_status
, ac
->ac_flags
);
4091 printk(KERN_ERR
"EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
4092 "best %lu/%lu/%lu@%lu cr %d\n",
4093 (unsigned long)ac
->ac_o_ex
.fe_group
,
4094 (unsigned long)ac
->ac_o_ex
.fe_start
,
4095 (unsigned long)ac
->ac_o_ex
.fe_len
,
4096 (unsigned long)ac
->ac_o_ex
.fe_logical
,
4097 (unsigned long)ac
->ac_g_ex
.fe_group
,
4098 (unsigned long)ac
->ac_g_ex
.fe_start
,
4099 (unsigned long)ac
->ac_g_ex
.fe_len
,
4100 (unsigned long)ac
->ac_g_ex
.fe_logical
,
4101 (unsigned long)ac
->ac_b_ex
.fe_group
,
4102 (unsigned long)ac
->ac_b_ex
.fe_start
,
4103 (unsigned long)ac
->ac_b_ex
.fe_len
,
4104 (unsigned long)ac
->ac_b_ex
.fe_logical
,
4105 (int)ac
->ac_criteria
);
4106 printk(KERN_ERR
"EXT4-fs: %lu scanned, %d found\n", ac
->ac_ex_scanned
,
4108 printk(KERN_ERR
"EXT4-fs: groups: \n");
4109 ngroups
= ext4_get_groups_count(sb
);
4110 for (i
= 0; i
< ngroups
; i
++) {
4111 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, i
);
4112 struct ext4_prealloc_space
*pa
;
4113 ext4_grpblk_t start
;
4114 struct list_head
*cur
;
4115 ext4_lock_group(sb
, i
);
4116 list_for_each(cur
, &grp
->bb_prealloc_list
) {
4117 pa
= list_entry(cur
, struct ext4_prealloc_space
,
4119 spin_lock(&pa
->pa_lock
);
4120 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
4122 spin_unlock(&pa
->pa_lock
);
4123 printk(KERN_ERR
"PA:%lu:%d:%u \n", i
,
4126 ext4_unlock_group(sb
, i
);
4128 if (grp
->bb_free
== 0)
4130 printk(KERN_ERR
"%lu: %d/%d \n",
4131 i
, grp
->bb_free
, grp
->bb_fragments
);
4133 printk(KERN_ERR
"\n");
4136 static inline void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
4143 * We use locality group preallocation for small size file. The size of the
4144 * file is determined by the current size or the resulting size after
4145 * allocation which ever is larger
4147 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
4149 static void ext4_mb_group_or_file(struct ext4_allocation_context
*ac
)
4151 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
4152 int bsbits
= ac
->ac_sb
->s_blocksize_bits
;
4155 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
4158 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
4159 isize
= i_size_read(ac
->ac_inode
) >> bsbits
;
4160 size
= max(size
, isize
);
4162 /* don't use group allocation for large files */
4163 if (size
>= sbi
->s_mb_stream_request
)
4166 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
4169 BUG_ON(ac
->ac_lg
!= NULL
);
4171 * locality group prealloc space are per cpu. The reason for having
4172 * per cpu locality group is to reduce the contention between block
4173 * request from multiple CPUs.
4175 ac
->ac_lg
= per_cpu_ptr(sbi
->s_locality_groups
, raw_smp_processor_id());
4177 /* we're going to use group allocation */
4178 ac
->ac_flags
|= EXT4_MB_HINT_GROUP_ALLOC
;
4180 /* serialize all allocations in the group */
4181 mutex_lock(&ac
->ac_lg
->lg_mutex
);
4184 static noinline_for_stack
int
4185 ext4_mb_initialize_context(struct ext4_allocation_context
*ac
,
4186 struct ext4_allocation_request
*ar
)
4188 struct super_block
*sb
= ar
->inode
->i_sb
;
4189 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4190 struct ext4_super_block
*es
= sbi
->s_es
;
4194 ext4_grpblk_t block
;
4196 /* we can't allocate > group size */
4199 /* just a dirty hack to filter too big requests */
4200 if (len
>= EXT4_BLOCKS_PER_GROUP(sb
) - 10)
4201 len
= EXT4_BLOCKS_PER_GROUP(sb
) - 10;
4203 /* start searching from the goal */
4205 if (goal
< le32_to_cpu(es
->s_first_data_block
) ||
4206 goal
>= ext4_blocks_count(es
))
4207 goal
= le32_to_cpu(es
->s_first_data_block
);
4208 ext4_get_group_no_and_offset(sb
, goal
, &group
, &block
);
4210 /* set up allocation goals */
4211 memset(ac
, 0, sizeof(struct ext4_allocation_context
));
4212 ac
->ac_b_ex
.fe_logical
= ar
->logical
;
4213 ac
->ac_status
= AC_STATUS_CONTINUE
;
4215 ac
->ac_inode
= ar
->inode
;
4216 ac
->ac_o_ex
.fe_logical
= ar
->logical
;
4217 ac
->ac_o_ex
.fe_group
= group
;
4218 ac
->ac_o_ex
.fe_start
= block
;
4219 ac
->ac_o_ex
.fe_len
= len
;
4220 ac
->ac_g_ex
.fe_logical
= ar
->logical
;
4221 ac
->ac_g_ex
.fe_group
= group
;
4222 ac
->ac_g_ex
.fe_start
= block
;
4223 ac
->ac_g_ex
.fe_len
= len
;
4224 ac
->ac_flags
= ar
->flags
;
4226 /* we have to define context: we'll we work with a file or
4227 * locality group. this is a policy, actually */
4228 ext4_mb_group_or_file(ac
);
4230 mb_debug("init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
4231 "left: %u/%u, right %u/%u to %swritable\n",
4232 (unsigned) ar
->len
, (unsigned) ar
->logical
,
4233 (unsigned) ar
->goal
, ac
->ac_flags
, ac
->ac_2order
,
4234 (unsigned) ar
->lleft
, (unsigned) ar
->pleft
,
4235 (unsigned) ar
->lright
, (unsigned) ar
->pright
,
4236 atomic_read(&ar
->inode
->i_writecount
) ? "" : "non-");
4241 static noinline_for_stack
void
4242 ext4_mb_discard_lg_preallocations(struct super_block
*sb
,
4243 struct ext4_locality_group
*lg
,
4244 int order
, int total_entries
)
4246 ext4_group_t group
= 0;
4247 struct ext4_buddy e4b
;
4248 struct list_head discard_list
;
4249 struct ext4_prealloc_space
*pa
, *tmp
;
4250 struct ext4_allocation_context
*ac
;
4252 mb_debug("discard locality group preallocation\n");
4254 INIT_LIST_HEAD(&discard_list
);
4255 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4259 spin_lock(&lg
->lg_prealloc_lock
);
4260 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[order
],
4262 spin_lock(&pa
->pa_lock
);
4263 if (atomic_read(&pa
->pa_count
)) {
4265 * This is the pa that we just used
4266 * for block allocation. So don't
4269 spin_unlock(&pa
->pa_lock
);
4272 if (pa
->pa_deleted
) {
4273 spin_unlock(&pa
->pa_lock
);
4276 /* only lg prealloc space */
4277 BUG_ON(pa
->pa_type
!= MB_GROUP_PA
);
4279 /* seems this one can be freed ... */
4281 spin_unlock(&pa
->pa_lock
);
4283 list_del_rcu(&pa
->pa_inode_list
);
4284 list_add(&pa
->u
.pa_tmp_list
, &discard_list
);
4287 if (total_entries
<= 5) {
4289 * we want to keep only 5 entries
4290 * allowing it to grow to 8. This
4291 * mak sure we don't call discard
4292 * soon for this list.
4297 spin_unlock(&lg
->lg_prealloc_lock
);
4299 list_for_each_entry_safe(pa
, tmp
, &discard_list
, u
.pa_tmp_list
) {
4301 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
4302 if (ext4_mb_load_buddy(sb
, group
, &e4b
)) {
4303 ext4_error(sb
, __func__
, "Error in loading buddy "
4304 "information for %u", group
);
4307 ext4_lock_group(sb
, group
);
4308 list_del(&pa
->pa_group_list
);
4309 ext4_mb_release_group_pa(&e4b
, pa
, ac
);
4310 ext4_unlock_group(sb
, group
);
4312 ext4_mb_release_desc(&e4b
);
4313 list_del(&pa
->u
.pa_tmp_list
);
4314 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4317 kmem_cache_free(ext4_ac_cachep
, ac
);
4321 * We have incremented pa_count. So it cannot be freed at this
4322 * point. Also we hold lg_mutex. So no parallel allocation is
4323 * possible from this lg. That means pa_free cannot be updated.
4325 * A parallel ext4_mb_discard_group_preallocations is possible.
4326 * which can cause the lg_prealloc_list to be updated.
4329 static void ext4_mb_add_n_trim(struct ext4_allocation_context
*ac
)
4331 int order
, added
= 0, lg_prealloc_count
= 1;
4332 struct super_block
*sb
= ac
->ac_sb
;
4333 struct ext4_locality_group
*lg
= ac
->ac_lg
;
4334 struct ext4_prealloc_space
*tmp_pa
, *pa
= ac
->ac_pa
;
4336 order
= fls(pa
->pa_free
) - 1;
4337 if (order
> PREALLOC_TB_SIZE
- 1)
4338 /* The max size of hash table is PREALLOC_TB_SIZE */
4339 order
= PREALLOC_TB_SIZE
- 1;
4340 /* Add the prealloc space to lg */
4342 list_for_each_entry_rcu(tmp_pa
, &lg
->lg_prealloc_list
[order
],
4344 spin_lock(&tmp_pa
->pa_lock
);
4345 if (tmp_pa
->pa_deleted
) {
4346 spin_unlock(&tmp_pa
->pa_lock
);
4349 if (!added
&& pa
->pa_free
< tmp_pa
->pa_free
) {
4350 /* Add to the tail of the previous entry */
4351 list_add_tail_rcu(&pa
->pa_inode_list
,
4352 &tmp_pa
->pa_inode_list
);
4355 * we want to count the total
4356 * number of entries in the list
4359 spin_unlock(&tmp_pa
->pa_lock
);
4360 lg_prealloc_count
++;
4363 list_add_tail_rcu(&pa
->pa_inode_list
,
4364 &lg
->lg_prealloc_list
[order
]);
4367 /* Now trim the list to be not more than 8 elements */
4368 if (lg_prealloc_count
> 8) {
4369 ext4_mb_discard_lg_preallocations(sb
, lg
,
4370 order
, lg_prealloc_count
);
4377 * release all resource we used in allocation
4379 static int ext4_mb_release_context(struct ext4_allocation_context
*ac
)
4381 struct ext4_prealloc_space
*pa
= ac
->ac_pa
;
4383 if (pa
->pa_type
== MB_GROUP_PA
) {
4384 /* see comment in ext4_mb_use_group_pa() */
4385 spin_lock(&pa
->pa_lock
);
4386 pa
->pa_pstart
+= ac
->ac_b_ex
.fe_len
;
4387 pa
->pa_lstart
+= ac
->ac_b_ex
.fe_len
;
4388 pa
->pa_free
-= ac
->ac_b_ex
.fe_len
;
4389 pa
->pa_len
-= ac
->ac_b_ex
.fe_len
;
4390 spin_unlock(&pa
->pa_lock
);
4394 up_read(ac
->alloc_semp
);
4397 * We want to add the pa to the right bucket.
4398 * Remove it from the list and while adding
4399 * make sure the list to which we are adding
4400 * doesn't grow big. We need to release
4401 * alloc_semp before calling ext4_mb_add_n_trim()
4403 if ((pa
->pa_type
== MB_GROUP_PA
) && likely(pa
->pa_free
)) {
4404 spin_lock(pa
->pa_obj_lock
);
4405 list_del_rcu(&pa
->pa_inode_list
);
4406 spin_unlock(pa
->pa_obj_lock
);
4407 ext4_mb_add_n_trim(ac
);
4409 ext4_mb_put_pa(ac
, ac
->ac_sb
, pa
);
4411 if (ac
->ac_bitmap_page
)
4412 page_cache_release(ac
->ac_bitmap_page
);
4413 if (ac
->ac_buddy_page
)
4414 page_cache_release(ac
->ac_buddy_page
);
4415 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
4416 mutex_unlock(&ac
->ac_lg
->lg_mutex
);
4417 ext4_mb_collect_stats(ac
);
4421 static int ext4_mb_discard_preallocations(struct super_block
*sb
, int needed
)
4423 ext4_group_t i
, ngroups
= ext4_get_groups_count(sb
);
4427 trace_ext4_mb_discard_preallocations(sb
, needed
);
4428 for (i
= 0; i
< ngroups
&& needed
> 0; i
++) {
4429 ret
= ext4_mb_discard_group_preallocations(sb
, i
, needed
);
4438 * Main entry point into mballoc to allocate blocks
4439 * it tries to use preallocation first, then falls back
4440 * to usual allocation
4442 ext4_fsblk_t
ext4_mb_new_blocks(handle_t
*handle
,
4443 struct ext4_allocation_request
*ar
, int *errp
)
4446 struct ext4_allocation_context
*ac
= NULL
;
4447 struct ext4_sb_info
*sbi
;
4448 struct super_block
*sb
;
4449 ext4_fsblk_t block
= 0;
4450 unsigned int inquota
= 0;
4451 unsigned int reserv_blks
= 0;
4453 sb
= ar
->inode
->i_sb
;
4456 trace_ext4_request_blocks(ar
);
4459 * For delayed allocation, we could skip the ENOSPC and
4460 * EDQUOT check, as blocks and quotas have been already
4461 * reserved when data being copied into pagecache.
4463 if (EXT4_I(ar
->inode
)->i_delalloc_reserved_flag
)
4464 ar
->flags
|= EXT4_MB_DELALLOC_RESERVED
;
4466 /* Without delayed allocation we need to verify
4467 * there is enough free blocks to do block allocation
4468 * and verify allocation doesn't exceed the quota limits.
4470 while (ar
->len
&& ext4_claim_free_blocks(sbi
, ar
->len
)) {
4471 /* let others to free the space */
4473 ar
->len
= ar
->len
>> 1;
4479 reserv_blks
= ar
->len
;
4480 while (ar
->len
&& vfs_dq_alloc_block(ar
->inode
, ar
->len
)) {
4481 ar
->flags
|= EXT4_MB_HINT_NOPREALLOC
;
4491 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4498 *errp
= ext4_mb_initialize_context(ac
, ar
);
4504 ac
->ac_op
= EXT4_MB_HISTORY_PREALLOC
;
4505 if (!ext4_mb_use_preallocated(ac
)) {
4506 ac
->ac_op
= EXT4_MB_HISTORY_ALLOC
;
4507 ext4_mb_normalize_request(ac
, ar
);
4509 /* allocate space in core */
4510 ext4_mb_regular_allocator(ac
);
4512 /* as we've just preallocated more space than
4513 * user requested orinally, we store allocated
4514 * space in a special descriptor */
4515 if (ac
->ac_status
== AC_STATUS_FOUND
&&
4516 ac
->ac_o_ex
.fe_len
< ac
->ac_b_ex
.fe_len
)
4517 ext4_mb_new_preallocation(ac
);
4519 if (likely(ac
->ac_status
== AC_STATUS_FOUND
)) {
4520 *errp
= ext4_mb_mark_diskspace_used(ac
, handle
, reserv_blks
);
4521 if (*errp
== -EAGAIN
) {
4523 * drop the reference that we took
4524 * in ext4_mb_use_best_found
4526 ext4_mb_release_context(ac
);
4527 ac
->ac_b_ex
.fe_group
= 0;
4528 ac
->ac_b_ex
.fe_start
= 0;
4529 ac
->ac_b_ex
.fe_len
= 0;
4530 ac
->ac_status
= AC_STATUS_CONTINUE
;
4533 ac
->ac_b_ex
.fe_len
= 0;
4535 ext4_mb_show_ac(ac
);
4537 block
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
4538 ar
->len
= ac
->ac_b_ex
.fe_len
;
4541 freed
= ext4_mb_discard_preallocations(sb
, ac
->ac_o_ex
.fe_len
);
4545 ac
->ac_b_ex
.fe_len
= 0;
4547 ext4_mb_show_ac(ac
);
4550 ext4_mb_release_context(ac
);
4553 kmem_cache_free(ext4_ac_cachep
, ac
);
4555 if (inquota
&& ar
->len
< inquota
)
4556 vfs_dq_free_block(ar
->inode
, inquota
- ar
->len
);
4559 if (!EXT4_I(ar
->inode
)->i_delalloc_reserved_flag
)
4560 /* release all the reserved blocks if non delalloc */
4561 percpu_counter_sub(&sbi
->s_dirtyblocks_counter
,
4565 trace_ext4_allocate_blocks(ar
, (unsigned long long)block
);
4571 * We can merge two free data extents only if the physical blocks
4572 * are contiguous, AND the extents were freed by the same transaction,
4573 * AND the blocks are associated with the same group.
4575 static int can_merge(struct ext4_free_data
*entry1
,
4576 struct ext4_free_data
*entry2
)
4578 if ((entry1
->t_tid
== entry2
->t_tid
) &&
4579 (entry1
->group
== entry2
->group
) &&
4580 ((entry1
->start_blk
+ entry1
->count
) == entry2
->start_blk
))
4585 static noinline_for_stack
int
4586 ext4_mb_free_metadata(handle_t
*handle
, struct ext4_buddy
*e4b
,
4587 struct ext4_free_data
*new_entry
)
4589 ext4_grpblk_t block
;
4590 struct ext4_free_data
*entry
;
4591 struct ext4_group_info
*db
= e4b
->bd_info
;
4592 struct super_block
*sb
= e4b
->bd_sb
;
4593 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4594 struct rb_node
**n
= &db
->bb_free_root
.rb_node
, *node
;
4595 struct rb_node
*parent
= NULL
, *new_node
;
4597 BUG_ON(!ext4_handle_valid(handle
));
4598 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
4599 BUG_ON(e4b
->bd_buddy_page
== NULL
);
4601 new_node
= &new_entry
->node
;
4602 block
= new_entry
->start_blk
;
4605 /* first free block exent. We need to
4606 protect buddy cache from being freed,
4607 * otherwise we'll refresh it from
4608 * on-disk bitmap and lose not-yet-available
4610 page_cache_get(e4b
->bd_buddy_page
);
4611 page_cache_get(e4b
->bd_bitmap_page
);
4615 entry
= rb_entry(parent
, struct ext4_free_data
, node
);
4616 if (block
< entry
->start_blk
)
4618 else if (block
>= (entry
->start_blk
+ entry
->count
))
4619 n
= &(*n
)->rb_right
;
4621 ext4_grp_locked_error(sb
, e4b
->bd_group
, __func__
,
4622 "Double free of blocks %d (%d %d)",
4623 block
, entry
->start_blk
, entry
->count
);
4628 rb_link_node(new_node
, parent
, n
);
4629 rb_insert_color(new_node
, &db
->bb_free_root
);
4631 /* Now try to see the extent can be merged to left and right */
4632 node
= rb_prev(new_node
);
4634 entry
= rb_entry(node
, struct ext4_free_data
, node
);
4635 if (can_merge(entry
, new_entry
)) {
4636 new_entry
->start_blk
= entry
->start_blk
;
4637 new_entry
->count
+= entry
->count
;
4638 rb_erase(node
, &(db
->bb_free_root
));
4639 spin_lock(&sbi
->s_md_lock
);
4640 list_del(&entry
->list
);
4641 spin_unlock(&sbi
->s_md_lock
);
4642 kmem_cache_free(ext4_free_ext_cachep
, entry
);
4646 node
= rb_next(new_node
);
4648 entry
= rb_entry(node
, struct ext4_free_data
, node
);
4649 if (can_merge(new_entry
, entry
)) {
4650 new_entry
->count
+= entry
->count
;
4651 rb_erase(node
, &(db
->bb_free_root
));
4652 spin_lock(&sbi
->s_md_lock
);
4653 list_del(&entry
->list
);
4654 spin_unlock(&sbi
->s_md_lock
);
4655 kmem_cache_free(ext4_free_ext_cachep
, entry
);
4658 /* Add the extent to transaction's private list */
4659 spin_lock(&sbi
->s_md_lock
);
4660 list_add(&new_entry
->list
, &handle
->h_transaction
->t_private_list
);
4661 spin_unlock(&sbi
->s_md_lock
);
4666 * Main entry point into mballoc to free blocks
4668 void ext4_mb_free_blocks(handle_t
*handle
, struct inode
*inode
,
4669 ext4_fsblk_t block
, unsigned long count
,
4670 int metadata
, unsigned long *freed
)
4672 struct buffer_head
*bitmap_bh
= NULL
;
4673 struct super_block
*sb
= inode
->i_sb
;
4674 struct ext4_allocation_context
*ac
= NULL
;
4675 struct ext4_group_desc
*gdp
;
4676 struct ext4_super_block
*es
;
4677 unsigned int overflow
;
4679 struct buffer_head
*gd_bh
;
4680 ext4_group_t block_group
;
4681 struct ext4_sb_info
*sbi
;
4682 struct ext4_buddy e4b
;
4689 es
= EXT4_SB(sb
)->s_es
;
4690 if (block
< le32_to_cpu(es
->s_first_data_block
) ||
4691 block
+ count
< block
||
4692 block
+ count
> ext4_blocks_count(es
)) {
4693 ext4_error(sb
, __func__
,
4694 "Freeing blocks not in datazone - "
4695 "block = %llu, count = %lu", block
, count
);
4699 ext4_debug("freeing block %llu\n", block
);
4700 trace_ext4_free_blocks(inode
, block
, count
, metadata
);
4702 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4704 ac
->ac_op
= EXT4_MB_HISTORY_FREE
;
4705 ac
->ac_inode
= inode
;
4711 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
4714 * Check to see if we are freeing blocks across a group
4717 if (bit
+ count
> EXT4_BLOCKS_PER_GROUP(sb
)) {
4718 overflow
= bit
+ count
- EXT4_BLOCKS_PER_GROUP(sb
);
4721 bitmap_bh
= ext4_read_block_bitmap(sb
, block_group
);
4726 gdp
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
4732 if (in_range(ext4_block_bitmap(sb
, gdp
), block
, count
) ||
4733 in_range(ext4_inode_bitmap(sb
, gdp
), block
, count
) ||
4734 in_range(block
, ext4_inode_table(sb
, gdp
),
4735 EXT4_SB(sb
)->s_itb_per_group
) ||
4736 in_range(block
+ count
- 1, ext4_inode_table(sb
, gdp
),
4737 EXT4_SB(sb
)->s_itb_per_group
)) {
4739 ext4_error(sb
, __func__
,
4740 "Freeing blocks in system zone - "
4741 "Block = %llu, count = %lu", block
, count
);
4742 /* err = 0. ext4_std_error should be a no op */
4746 BUFFER_TRACE(bitmap_bh
, "getting write access");
4747 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
4752 * We are about to modify some metadata. Call the journal APIs
4753 * to unshare ->b_data if a currently-committing transaction is
4756 BUFFER_TRACE(gd_bh
, "get_write_access");
4757 err
= ext4_journal_get_write_access(handle
, gd_bh
);
4760 #ifdef AGGRESSIVE_CHECK
4763 for (i
= 0; i
< count
; i
++)
4764 BUG_ON(!mb_test_bit(bit
+ i
, bitmap_bh
->b_data
));
4768 ac
->ac_b_ex
.fe_group
= block_group
;
4769 ac
->ac_b_ex
.fe_start
= bit
;
4770 ac
->ac_b_ex
.fe_len
= count
;
4771 ext4_mb_store_history(ac
);
4774 err
= ext4_mb_load_buddy(sb
, block_group
, &e4b
);
4777 if (metadata
&& ext4_handle_valid(handle
)) {
4778 struct ext4_free_data
*new_entry
;
4780 * blocks being freed are metadata. these blocks shouldn't
4781 * be used until this transaction is committed
4783 new_entry
= kmem_cache_alloc(ext4_free_ext_cachep
, GFP_NOFS
);
4784 new_entry
->start_blk
= bit
;
4785 new_entry
->group
= block_group
;
4786 new_entry
->count
= count
;
4787 new_entry
->t_tid
= handle
->h_transaction
->t_tid
;
4789 ext4_lock_group(sb
, block_group
);
4790 mb_clear_bits(bitmap_bh
->b_data
, bit
, count
);
4791 ext4_mb_free_metadata(handle
, &e4b
, new_entry
);
4793 /* need to update group_info->bb_free and bitmap
4794 * with group lock held. generate_buddy look at
4795 * them with group lock_held
4797 ext4_lock_group(sb
, block_group
);
4798 mb_clear_bits(bitmap_bh
->b_data
, bit
, count
);
4799 mb_free_blocks(inode
, &e4b
, bit
, count
);
4800 ext4_mb_return_to_preallocation(inode
, &e4b
, block
, count
);
4803 ret
= ext4_free_blks_count(sb
, gdp
) + count
;
4804 ext4_free_blks_set(sb
, gdp
, ret
);
4805 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, block_group
, gdp
);
4806 ext4_unlock_group(sb
, block_group
);
4807 percpu_counter_add(&sbi
->s_freeblocks_counter
, count
);
4809 if (sbi
->s_log_groups_per_flex
) {
4810 ext4_group_t flex_group
= ext4_flex_group(sbi
, block_group
);
4811 atomic_add(count
, &sbi
->s_flex_groups
[flex_group
].free_blocks
);
4814 ext4_mb_release_desc(&e4b
);
4818 /* We dirtied the bitmap block */
4819 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
4820 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
4822 /* And the group descriptor block */
4823 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
4824 ret
= ext4_handle_dirty_metadata(handle
, NULL
, gd_bh
);
4828 if (overflow
&& !err
) {
4837 ext4_std_error(sb
, err
);
4839 kmem_cache_free(ext4_ac_cachep
, ac
);