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 void ext4_mb_generate_buddy(struct super_block
*sb
,
661 void *buddy
, void *bitmap
, ext4_group_t group
)
663 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
664 unsigned short max
= EXT4_BLOCKS_PER_GROUP(sb
);
665 unsigned short i
= 0;
666 unsigned short first
;
669 unsigned fragments
= 0;
670 unsigned long long period
= get_cycles();
672 /* initialize buddy from bitmap which is aggregation
673 * of on-disk bitmap and preallocations */
674 i
= mb_find_next_zero_bit(bitmap
, max
, 0);
675 grp
->bb_first_free
= i
;
679 i
= mb_find_next_bit(bitmap
, max
, i
);
683 ext4_mb_mark_free_simple(sb
, buddy
, first
, len
, grp
);
685 grp
->bb_counters
[0]++;
687 i
= mb_find_next_zero_bit(bitmap
, max
, i
);
689 grp
->bb_fragments
= fragments
;
691 if (free
!= grp
->bb_free
) {
692 ext4_grp_locked_error(sb
, group
, __func__
,
693 "EXT4-fs: group %u: %u blocks in bitmap, %u in gd",
694 group
, free
, grp
->bb_free
);
696 * If we intent to continue, we consider group descritor
697 * corrupt and update bb_free using bitmap value
702 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
, &(grp
->bb_state
));
704 period
= get_cycles() - period
;
705 spin_lock(&EXT4_SB(sb
)->s_bal_lock
);
706 EXT4_SB(sb
)->s_mb_buddies_generated
++;
707 EXT4_SB(sb
)->s_mb_generation_time
+= period
;
708 spin_unlock(&EXT4_SB(sb
)->s_bal_lock
);
711 /* The buddy information is attached the buddy cache inode
712 * for convenience. The information regarding each group
713 * is loaded via ext4_mb_load_buddy. The information involve
714 * block bitmap and buddy information. The information are
715 * stored in the inode as
718 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
721 * one block each for bitmap and buddy information.
722 * So for each group we take up 2 blocks. A page can
723 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
724 * So it can have information regarding groups_per_page which
725 * is blocks_per_page/2
728 static int ext4_mb_init_cache(struct page
*page
, char *incore
)
730 ext4_group_t ngroups
;
736 ext4_group_t first_group
;
738 struct super_block
*sb
;
739 struct buffer_head
*bhs
;
740 struct buffer_head
**bh
;
745 mb_debug("init page %lu\n", page
->index
);
747 inode
= page
->mapping
->host
;
749 ngroups
= ext4_get_groups_count(sb
);
750 blocksize
= 1 << inode
->i_blkbits
;
751 blocks_per_page
= PAGE_CACHE_SIZE
/ blocksize
;
753 groups_per_page
= blocks_per_page
>> 1;
754 if (groups_per_page
== 0)
757 /* allocate buffer_heads to read bitmaps */
758 if (groups_per_page
> 1) {
760 i
= sizeof(struct buffer_head
*) * groups_per_page
;
761 bh
= kzalloc(i
, GFP_NOFS
);
767 first_group
= page
->index
* blocks_per_page
/ 2;
769 /* read all groups the page covers into the cache */
770 for (i
= 0; i
< groups_per_page
; i
++) {
771 struct ext4_group_desc
*desc
;
773 if (first_group
+ i
>= ngroups
)
777 desc
= ext4_get_group_desc(sb
, first_group
+ i
, NULL
);
782 bh
[i
] = sb_getblk(sb
, ext4_block_bitmap(sb
, desc
));
786 if (bitmap_uptodate(bh
[i
]))
790 if (bitmap_uptodate(bh
[i
])) {
791 unlock_buffer(bh
[i
]);
794 ext4_lock_group(sb
, first_group
+ i
);
795 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
796 ext4_init_block_bitmap(sb
, bh
[i
],
797 first_group
+ i
, desc
);
798 set_bitmap_uptodate(bh
[i
]);
799 set_buffer_uptodate(bh
[i
]);
800 ext4_unlock_group(sb
, first_group
+ i
);
801 unlock_buffer(bh
[i
]);
804 ext4_unlock_group(sb
, first_group
+ i
);
805 if (buffer_uptodate(bh
[i
])) {
807 * if not uninit if bh is uptodate,
808 * bitmap is also uptodate
810 set_bitmap_uptodate(bh
[i
]);
811 unlock_buffer(bh
[i
]);
816 * submit the buffer_head for read. We can
817 * safely mark the bitmap as uptodate now.
818 * We do it here so the bitmap uptodate bit
819 * get set with buffer lock held.
821 set_bitmap_uptodate(bh
[i
]);
822 bh
[i
]->b_end_io
= end_buffer_read_sync
;
823 submit_bh(READ
, bh
[i
]);
824 mb_debug("read bitmap for group %u\n", first_group
+ i
);
827 /* wait for I/O completion */
828 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
829 wait_on_buffer(bh
[i
]);
832 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
833 if (!buffer_uptodate(bh
[i
]))
837 first_block
= page
->index
* blocks_per_page
;
839 memset(page_address(page
), 0xff, PAGE_CACHE_SIZE
);
840 for (i
= 0; i
< blocks_per_page
; i
++) {
842 struct ext4_group_info
*grinfo
;
844 group
= (first_block
+ i
) >> 1;
845 if (group
>= ngroups
)
849 * data carry information regarding this
850 * particular group in the format specified
854 data
= page_address(page
) + (i
* blocksize
);
855 bitmap
= bh
[group
- first_group
]->b_data
;
858 * We place the buddy block and bitmap block
861 if ((first_block
+ i
) & 1) {
862 /* this is block of buddy */
863 BUG_ON(incore
== NULL
);
864 mb_debug("put buddy for group %u in page %lu/%x\n",
865 group
, page
->index
, i
* blocksize
);
866 grinfo
= ext4_get_group_info(sb
, group
);
867 grinfo
->bb_fragments
= 0;
868 memset(grinfo
->bb_counters
, 0,
869 sizeof(unsigned short)*(sb
->s_blocksize_bits
+2));
871 * incore got set to the group block bitmap below
873 ext4_lock_group(sb
, group
);
874 ext4_mb_generate_buddy(sb
, data
, incore
, group
);
875 ext4_unlock_group(sb
, group
);
878 /* this is block of bitmap */
879 BUG_ON(incore
!= NULL
);
880 mb_debug("put bitmap for group %u in page %lu/%x\n",
881 group
, page
->index
, i
* blocksize
);
883 /* see comments in ext4_mb_put_pa() */
884 ext4_lock_group(sb
, group
);
885 memcpy(data
, bitmap
, blocksize
);
887 /* mark all preallocated blks used in in-core bitmap */
888 ext4_mb_generate_from_pa(sb
, data
, group
);
889 ext4_mb_generate_from_freelist(sb
, data
, group
);
890 ext4_unlock_group(sb
, group
);
892 /* set incore so that the buddy information can be
893 * generated using this
898 SetPageUptodate(page
);
902 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
910 static noinline_for_stack
int
911 ext4_mb_load_buddy(struct super_block
*sb
, ext4_group_t group
,
912 struct ext4_buddy
*e4b
)
920 struct ext4_group_info
*grp
;
921 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
922 struct inode
*inode
= sbi
->s_buddy_cache
;
924 mb_debug("load group %u\n", group
);
926 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
927 grp
= ext4_get_group_info(sb
, group
);
929 e4b
->bd_blkbits
= sb
->s_blocksize_bits
;
930 e4b
->bd_info
= ext4_get_group_info(sb
, group
);
932 e4b
->bd_group
= group
;
933 e4b
->bd_buddy_page
= NULL
;
934 e4b
->bd_bitmap_page
= NULL
;
935 e4b
->alloc_semp
= &grp
->alloc_sem
;
937 /* Take the read lock on the group alloc
938 * sem. This would make sure a parallel
939 * ext4_mb_init_group happening on other
940 * groups mapped by the page is blocked
941 * till we are done with allocation
943 down_read(e4b
->alloc_semp
);
946 * the buddy cache inode stores the block bitmap
947 * and buddy information in consecutive blocks.
948 * So for each group we need two blocks.
951 pnum
= block
/ blocks_per_page
;
952 poff
= block
% blocks_per_page
;
954 /* we could use find_or_create_page(), but it locks page
955 * what we'd like to avoid in fast path ... */
956 page
= find_get_page(inode
->i_mapping
, pnum
);
957 if (page
== NULL
|| !PageUptodate(page
)) {
960 * drop the page reference and try
961 * to get the page with lock. If we
962 * are not uptodate that implies
963 * somebody just created the page but
964 * is yet to initialize the same. So
965 * wait for it to initialize.
967 page_cache_release(page
);
968 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
970 BUG_ON(page
->mapping
!= inode
->i_mapping
);
971 if (!PageUptodate(page
)) {
972 ret
= ext4_mb_init_cache(page
, NULL
);
977 mb_cmp_bitmaps(e4b
, page_address(page
) +
978 (poff
* sb
->s_blocksize
));
983 if (page
== NULL
|| !PageUptodate(page
)) {
987 e4b
->bd_bitmap_page
= page
;
988 e4b
->bd_bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
989 mark_page_accessed(page
);
992 pnum
= block
/ blocks_per_page
;
993 poff
= block
% blocks_per_page
;
995 page
= find_get_page(inode
->i_mapping
, pnum
);
996 if (page
== NULL
|| !PageUptodate(page
)) {
998 page_cache_release(page
);
999 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1001 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1002 if (!PageUptodate(page
)) {
1003 ret
= ext4_mb_init_cache(page
, e4b
->bd_bitmap
);
1012 if (page
== NULL
|| !PageUptodate(page
)) {
1016 e4b
->bd_buddy_page
= page
;
1017 e4b
->bd_buddy
= page_address(page
) + (poff
* sb
->s_blocksize
);
1018 mark_page_accessed(page
);
1020 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
1021 BUG_ON(e4b
->bd_buddy_page
== NULL
);
1026 if (e4b
->bd_bitmap_page
)
1027 page_cache_release(e4b
->bd_bitmap_page
);
1028 if (e4b
->bd_buddy_page
)
1029 page_cache_release(e4b
->bd_buddy_page
);
1030 e4b
->bd_buddy
= NULL
;
1031 e4b
->bd_bitmap
= NULL
;
1033 /* Done with the buddy cache */
1034 up_read(e4b
->alloc_semp
);
1038 static void ext4_mb_release_desc(struct ext4_buddy
*e4b
)
1040 if (e4b
->bd_bitmap_page
)
1041 page_cache_release(e4b
->bd_bitmap_page
);
1042 if (e4b
->bd_buddy_page
)
1043 page_cache_release(e4b
->bd_buddy_page
);
1044 /* Done with the buddy cache */
1045 if (e4b
->alloc_semp
)
1046 up_read(e4b
->alloc_semp
);
1050 static int mb_find_order_for_block(struct ext4_buddy
*e4b
, int block
)
1055 BUG_ON(EXT4_MB_BITMAP(e4b
) == EXT4_MB_BUDDY(e4b
));
1056 BUG_ON(block
>= (1 << (e4b
->bd_blkbits
+ 3)));
1058 bb
= EXT4_MB_BUDDY(e4b
);
1059 while (order
<= e4b
->bd_blkbits
+ 1) {
1061 if (!mb_test_bit(block
, bb
)) {
1062 /* this block is part of buddy of order 'order' */
1065 bb
+= 1 << (e4b
->bd_blkbits
- order
);
1071 static void mb_clear_bits(void *bm
, int cur
, int len
)
1077 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1078 /* fast path: clear whole word at once */
1079 addr
= bm
+ (cur
>> 3);
1084 mb_clear_bit(cur
, bm
);
1089 static void mb_set_bits(void *bm
, int cur
, int len
)
1095 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1096 /* fast path: set whole word at once */
1097 addr
= bm
+ (cur
>> 3);
1102 mb_set_bit(cur
, bm
);
1107 static void mb_free_blocks(struct inode
*inode
, struct ext4_buddy
*e4b
,
1108 int first
, int count
)
1115 struct super_block
*sb
= e4b
->bd_sb
;
1117 BUG_ON(first
+ count
> (sb
->s_blocksize
<< 3));
1118 assert_spin_locked(ext4_group_lock_ptr(sb
, e4b
->bd_group
));
1119 mb_check_buddy(e4b
);
1120 mb_free_blocks_double(inode
, e4b
, first
, count
);
1122 e4b
->bd_info
->bb_free
+= count
;
1123 if (first
< e4b
->bd_info
->bb_first_free
)
1124 e4b
->bd_info
->bb_first_free
= first
;
1126 /* let's maintain fragments counter */
1128 block
= !mb_test_bit(first
- 1, EXT4_MB_BITMAP(e4b
));
1129 if (first
+ count
< EXT4_SB(sb
)->s_mb_maxs
[0])
1130 max
= !mb_test_bit(first
+ count
, EXT4_MB_BITMAP(e4b
));
1132 e4b
->bd_info
->bb_fragments
--;
1133 else if (!block
&& !max
)
1134 e4b
->bd_info
->bb_fragments
++;
1136 /* let's maintain buddy itself */
1137 while (count
-- > 0) {
1141 if (!mb_test_bit(block
, EXT4_MB_BITMAP(e4b
))) {
1142 ext4_fsblk_t blocknr
;
1143 blocknr
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
);
1146 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
1147 ext4_grp_locked_error(sb
, e4b
->bd_group
,
1148 __func__
, "double-free of inode"
1149 " %lu's block %llu(bit %u in group %u)",
1150 inode
? inode
->i_ino
: 0, blocknr
, block
,
1153 mb_clear_bit(block
, EXT4_MB_BITMAP(e4b
));
1154 e4b
->bd_info
->bb_counters
[order
]++;
1156 /* start of the buddy */
1157 buddy
= mb_find_buddy(e4b
, order
, &max
);
1161 if (mb_test_bit(block
, buddy
) ||
1162 mb_test_bit(block
+ 1, buddy
))
1165 /* both the buddies are free, try to coalesce them */
1166 buddy2
= mb_find_buddy(e4b
, order
+ 1, &max
);
1172 /* for special purposes, we don't set
1173 * free bits in bitmap */
1174 mb_set_bit(block
, buddy
);
1175 mb_set_bit(block
+ 1, buddy
);
1177 e4b
->bd_info
->bb_counters
[order
]--;
1178 e4b
->bd_info
->bb_counters
[order
]--;
1182 e4b
->bd_info
->bb_counters
[order
]++;
1184 mb_clear_bit(block
, buddy2
);
1188 mb_check_buddy(e4b
);
1191 static int mb_find_extent(struct ext4_buddy
*e4b
, int order
, int block
,
1192 int needed
, struct ext4_free_extent
*ex
)
1199 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
1202 buddy
= mb_find_buddy(e4b
, order
, &max
);
1203 BUG_ON(buddy
== NULL
);
1204 BUG_ON(block
>= max
);
1205 if (mb_test_bit(block
, buddy
)) {
1212 /* FIXME dorp order completely ? */
1213 if (likely(order
== 0)) {
1214 /* find actual order */
1215 order
= mb_find_order_for_block(e4b
, block
);
1216 block
= block
>> order
;
1219 ex
->fe_len
= 1 << order
;
1220 ex
->fe_start
= block
<< order
;
1221 ex
->fe_group
= e4b
->bd_group
;
1223 /* calc difference from given start */
1224 next
= next
- ex
->fe_start
;
1226 ex
->fe_start
+= next
;
1228 while (needed
> ex
->fe_len
&&
1229 (buddy
= mb_find_buddy(e4b
, order
, &max
))) {
1231 if (block
+ 1 >= max
)
1234 next
= (block
+ 1) * (1 << order
);
1235 if (mb_test_bit(next
, EXT4_MB_BITMAP(e4b
)))
1238 ord
= mb_find_order_for_block(e4b
, next
);
1241 block
= next
>> order
;
1242 ex
->fe_len
+= 1 << order
;
1245 BUG_ON(ex
->fe_start
+ ex
->fe_len
> (1 << (e4b
->bd_blkbits
+ 3)));
1249 static int mb_mark_used(struct ext4_buddy
*e4b
, struct ext4_free_extent
*ex
)
1255 int start
= ex
->fe_start
;
1256 int len
= ex
->fe_len
;
1261 BUG_ON(start
+ len
> (e4b
->bd_sb
->s_blocksize
<< 3));
1262 BUG_ON(e4b
->bd_group
!= ex
->fe_group
);
1263 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
1264 mb_check_buddy(e4b
);
1265 mb_mark_used_double(e4b
, start
, len
);
1267 e4b
->bd_info
->bb_free
-= len
;
1268 if (e4b
->bd_info
->bb_first_free
== start
)
1269 e4b
->bd_info
->bb_first_free
+= len
;
1271 /* let's maintain fragments counter */
1273 mlen
= !mb_test_bit(start
- 1, EXT4_MB_BITMAP(e4b
));
1274 if (start
+ len
< EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[0])
1275 max
= !mb_test_bit(start
+ len
, EXT4_MB_BITMAP(e4b
));
1277 e4b
->bd_info
->bb_fragments
++;
1278 else if (!mlen
&& !max
)
1279 e4b
->bd_info
->bb_fragments
--;
1281 /* let's maintain buddy itself */
1283 ord
= mb_find_order_for_block(e4b
, start
);
1285 if (((start
>> ord
) << ord
) == start
&& len
>= (1 << ord
)) {
1286 /* the whole chunk may be allocated at once! */
1288 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1289 BUG_ON((start
>> ord
) >= max
);
1290 mb_set_bit(start
>> ord
, buddy
);
1291 e4b
->bd_info
->bb_counters
[ord
]--;
1298 /* store for history */
1300 ret
= len
| (ord
<< 16);
1302 /* we have to split large buddy */
1304 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1305 mb_set_bit(start
>> ord
, buddy
);
1306 e4b
->bd_info
->bb_counters
[ord
]--;
1309 cur
= (start
>> ord
) & ~1U;
1310 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1311 mb_clear_bit(cur
, buddy
);
1312 mb_clear_bit(cur
+ 1, buddy
);
1313 e4b
->bd_info
->bb_counters
[ord
]++;
1314 e4b
->bd_info
->bb_counters
[ord
]++;
1317 mb_set_bits(EXT4_MB_BITMAP(e4b
), ex
->fe_start
, len0
);
1318 mb_check_buddy(e4b
);
1324 * Must be called under group lock!
1326 static void ext4_mb_use_best_found(struct ext4_allocation_context
*ac
,
1327 struct ext4_buddy
*e4b
)
1329 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1332 BUG_ON(ac
->ac_b_ex
.fe_group
!= e4b
->bd_group
);
1333 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1335 ac
->ac_b_ex
.fe_len
= min(ac
->ac_b_ex
.fe_len
, ac
->ac_g_ex
.fe_len
);
1336 ac
->ac_b_ex
.fe_logical
= ac
->ac_g_ex
.fe_logical
;
1337 ret
= mb_mark_used(e4b
, &ac
->ac_b_ex
);
1339 /* preallocation can change ac_b_ex, thus we store actually
1340 * allocated blocks for history */
1341 ac
->ac_f_ex
= ac
->ac_b_ex
;
1343 ac
->ac_status
= AC_STATUS_FOUND
;
1344 ac
->ac_tail
= ret
& 0xffff;
1345 ac
->ac_buddy
= ret
>> 16;
1348 * take the page reference. We want the page to be pinned
1349 * so that we don't get a ext4_mb_init_cache_call for this
1350 * group until we update the bitmap. That would mean we
1351 * double allocate blocks. The reference is dropped
1352 * in ext4_mb_release_context
1354 ac
->ac_bitmap_page
= e4b
->bd_bitmap_page
;
1355 get_page(ac
->ac_bitmap_page
);
1356 ac
->ac_buddy_page
= e4b
->bd_buddy_page
;
1357 get_page(ac
->ac_buddy_page
);
1358 /* on allocation we use ac to track the held semaphore */
1359 ac
->alloc_semp
= e4b
->alloc_semp
;
1360 e4b
->alloc_semp
= NULL
;
1361 /* store last allocated for subsequent stream allocation */
1362 if ((ac
->ac_flags
& EXT4_MB_HINT_DATA
)) {
1363 spin_lock(&sbi
->s_md_lock
);
1364 sbi
->s_mb_last_group
= ac
->ac_f_ex
.fe_group
;
1365 sbi
->s_mb_last_start
= ac
->ac_f_ex
.fe_start
;
1366 spin_unlock(&sbi
->s_md_lock
);
1371 * regular allocator, for general purposes allocation
1374 static void ext4_mb_check_limits(struct ext4_allocation_context
*ac
,
1375 struct ext4_buddy
*e4b
,
1378 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1379 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1380 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1381 struct ext4_free_extent ex
;
1384 if (ac
->ac_status
== AC_STATUS_FOUND
)
1387 * We don't want to scan for a whole year
1389 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
&&
1390 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1391 ac
->ac_status
= AC_STATUS_BREAK
;
1396 * Haven't found good chunk so far, let's continue
1398 if (bex
->fe_len
< gex
->fe_len
)
1401 if ((finish_group
|| ac
->ac_found
> sbi
->s_mb_min_to_scan
)
1402 && bex
->fe_group
== e4b
->bd_group
) {
1403 /* recheck chunk's availability - we don't know
1404 * when it was found (within this lock-unlock
1406 max
= mb_find_extent(e4b
, 0, bex
->fe_start
, gex
->fe_len
, &ex
);
1407 if (max
>= gex
->fe_len
) {
1408 ext4_mb_use_best_found(ac
, e4b
);
1415 * The routine checks whether found extent is good enough. If it is,
1416 * then the extent gets marked used and flag is set to the context
1417 * to stop scanning. Otherwise, the extent is compared with the
1418 * previous found extent and if new one is better, then it's stored
1419 * in the context. Later, the best found extent will be used, if
1420 * mballoc can't find good enough extent.
1422 * FIXME: real allocation policy is to be designed yet!
1424 static void ext4_mb_measure_extent(struct ext4_allocation_context
*ac
,
1425 struct ext4_free_extent
*ex
,
1426 struct ext4_buddy
*e4b
)
1428 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1429 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1431 BUG_ON(ex
->fe_len
<= 0);
1432 BUG_ON(ex
->fe_len
> EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
1433 BUG_ON(ex
->fe_start
>= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
1434 BUG_ON(ac
->ac_status
!= AC_STATUS_CONTINUE
);
1439 * The special case - take what you catch first
1441 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1443 ext4_mb_use_best_found(ac
, e4b
);
1448 * Let's check whether the chuck is good enough
1450 if (ex
->fe_len
== gex
->fe_len
) {
1452 ext4_mb_use_best_found(ac
, e4b
);
1457 * If this is first found extent, just store it in the context
1459 if (bex
->fe_len
== 0) {
1465 * If new found extent is better, store it in the context
1467 if (bex
->fe_len
< gex
->fe_len
) {
1468 /* if the request isn't satisfied, any found extent
1469 * larger than previous best one is better */
1470 if (ex
->fe_len
> bex
->fe_len
)
1472 } else if (ex
->fe_len
> gex
->fe_len
) {
1473 /* if the request is satisfied, then we try to find
1474 * an extent that still satisfy the request, but is
1475 * smaller than previous one */
1476 if (ex
->fe_len
< bex
->fe_len
)
1480 ext4_mb_check_limits(ac
, e4b
, 0);
1483 static int ext4_mb_try_best_found(struct ext4_allocation_context
*ac
,
1484 struct ext4_buddy
*e4b
)
1486 struct ext4_free_extent ex
= ac
->ac_b_ex
;
1487 ext4_group_t group
= ex
.fe_group
;
1491 BUG_ON(ex
.fe_len
<= 0);
1492 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1496 ext4_lock_group(ac
->ac_sb
, group
);
1497 max
= mb_find_extent(e4b
, 0, ex
.fe_start
, ex
.fe_len
, &ex
);
1501 ext4_mb_use_best_found(ac
, e4b
);
1504 ext4_unlock_group(ac
->ac_sb
, group
);
1505 ext4_mb_release_desc(e4b
);
1510 static int ext4_mb_find_by_goal(struct ext4_allocation_context
*ac
,
1511 struct ext4_buddy
*e4b
)
1513 ext4_group_t group
= ac
->ac_g_ex
.fe_group
;
1516 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1517 struct ext4_super_block
*es
= sbi
->s_es
;
1518 struct ext4_free_extent ex
;
1520 if (!(ac
->ac_flags
& EXT4_MB_HINT_TRY_GOAL
))
1523 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1527 ext4_lock_group(ac
->ac_sb
, group
);
1528 max
= mb_find_extent(e4b
, 0, ac
->ac_g_ex
.fe_start
,
1529 ac
->ac_g_ex
.fe_len
, &ex
);
1531 if (max
>= ac
->ac_g_ex
.fe_len
&& ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
) {
1534 start
= (e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
)) +
1535 ex
.fe_start
+ le32_to_cpu(es
->s_first_data_block
);
1536 /* use do_div to get remainder (would be 64-bit modulo) */
1537 if (do_div(start
, sbi
->s_stripe
) == 0) {
1540 ext4_mb_use_best_found(ac
, e4b
);
1542 } else if (max
>= ac
->ac_g_ex
.fe_len
) {
1543 BUG_ON(ex
.fe_len
<= 0);
1544 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1545 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1548 ext4_mb_use_best_found(ac
, e4b
);
1549 } else if (max
> 0 && (ac
->ac_flags
& EXT4_MB_HINT_MERGE
)) {
1550 /* Sometimes, caller may want to merge even small
1551 * number of blocks to an existing extent */
1552 BUG_ON(ex
.fe_len
<= 0);
1553 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1554 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1557 ext4_mb_use_best_found(ac
, e4b
);
1559 ext4_unlock_group(ac
->ac_sb
, group
);
1560 ext4_mb_release_desc(e4b
);
1566 * The routine scans buddy structures (not bitmap!) from given order
1567 * to max order and tries to find big enough chunk to satisfy the req
1569 static void ext4_mb_simple_scan_group(struct ext4_allocation_context
*ac
,
1570 struct ext4_buddy
*e4b
)
1572 struct super_block
*sb
= ac
->ac_sb
;
1573 struct ext4_group_info
*grp
= e4b
->bd_info
;
1579 BUG_ON(ac
->ac_2order
<= 0);
1580 for (i
= ac
->ac_2order
; i
<= sb
->s_blocksize_bits
+ 1; i
++) {
1581 if (grp
->bb_counters
[i
] == 0)
1584 buddy
= mb_find_buddy(e4b
, i
, &max
);
1585 BUG_ON(buddy
== NULL
);
1587 k
= mb_find_next_zero_bit(buddy
, max
, 0);
1592 ac
->ac_b_ex
.fe_len
= 1 << i
;
1593 ac
->ac_b_ex
.fe_start
= k
<< i
;
1594 ac
->ac_b_ex
.fe_group
= e4b
->bd_group
;
1596 ext4_mb_use_best_found(ac
, e4b
);
1598 BUG_ON(ac
->ac_b_ex
.fe_len
!= ac
->ac_g_ex
.fe_len
);
1600 if (EXT4_SB(sb
)->s_mb_stats
)
1601 atomic_inc(&EXT4_SB(sb
)->s_bal_2orders
);
1608 * The routine scans the group and measures all found extents.
1609 * In order to optimize scanning, caller must pass number of
1610 * free blocks in the group, so the routine can know upper limit.
1612 static void ext4_mb_complex_scan_group(struct ext4_allocation_context
*ac
,
1613 struct ext4_buddy
*e4b
)
1615 struct super_block
*sb
= ac
->ac_sb
;
1616 void *bitmap
= EXT4_MB_BITMAP(e4b
);
1617 struct ext4_free_extent ex
;
1621 free
= e4b
->bd_info
->bb_free
;
1624 i
= e4b
->bd_info
->bb_first_free
;
1626 while (free
&& ac
->ac_status
== AC_STATUS_CONTINUE
) {
1627 i
= mb_find_next_zero_bit(bitmap
,
1628 EXT4_BLOCKS_PER_GROUP(sb
), i
);
1629 if (i
>= EXT4_BLOCKS_PER_GROUP(sb
)) {
1631 * IF we have corrupt bitmap, we won't find any
1632 * free blocks even though group info says we
1633 * we have free blocks
1635 ext4_grp_locked_error(sb
, e4b
->bd_group
,
1636 __func__
, "%d free blocks as per "
1637 "group info. But bitmap says 0",
1642 mb_find_extent(e4b
, 0, i
, ac
->ac_g_ex
.fe_len
, &ex
);
1643 BUG_ON(ex
.fe_len
<= 0);
1644 if (free
< ex
.fe_len
) {
1645 ext4_grp_locked_error(sb
, e4b
->bd_group
,
1646 __func__
, "%d free blocks as per "
1647 "group info. But got %d blocks",
1650 * The number of free blocks differs. This mostly
1651 * indicate that the bitmap is corrupt. So exit
1652 * without claiming the space.
1657 ext4_mb_measure_extent(ac
, &ex
, e4b
);
1663 ext4_mb_check_limits(ac
, e4b
, 1);
1667 * This is a special case for storages like raid5
1668 * we try to find stripe-aligned chunks for stripe-size requests
1669 * XXX should do so at least for multiples of stripe size as well
1671 static void ext4_mb_scan_aligned(struct ext4_allocation_context
*ac
,
1672 struct ext4_buddy
*e4b
)
1674 struct super_block
*sb
= ac
->ac_sb
;
1675 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1676 void *bitmap
= EXT4_MB_BITMAP(e4b
);
1677 struct ext4_free_extent ex
;
1678 ext4_fsblk_t first_group_block
;
1683 BUG_ON(sbi
->s_stripe
== 0);
1685 /* find first stripe-aligned block in group */
1686 first_group_block
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
)
1687 + le32_to_cpu(sbi
->s_es
->s_first_data_block
);
1688 a
= first_group_block
+ sbi
->s_stripe
- 1;
1689 do_div(a
, sbi
->s_stripe
);
1690 i
= (a
* sbi
->s_stripe
) - first_group_block
;
1692 while (i
< EXT4_BLOCKS_PER_GROUP(sb
)) {
1693 if (!mb_test_bit(i
, bitmap
)) {
1694 max
= mb_find_extent(e4b
, 0, i
, sbi
->s_stripe
, &ex
);
1695 if (max
>= sbi
->s_stripe
) {
1698 ext4_mb_use_best_found(ac
, e4b
);
1706 static int ext4_mb_good_group(struct ext4_allocation_context
*ac
,
1707 ext4_group_t group
, int cr
)
1709 unsigned free
, fragments
;
1711 int flex_size
= ext4_flex_bg_size(EXT4_SB(ac
->ac_sb
));
1712 struct ext4_group_info
*grp
= ext4_get_group_info(ac
->ac_sb
, group
);
1714 BUG_ON(cr
< 0 || cr
>= 4);
1715 BUG_ON(EXT4_MB_GRP_NEED_INIT(grp
));
1717 free
= grp
->bb_free
;
1718 fragments
= grp
->bb_fragments
;
1726 BUG_ON(ac
->ac_2order
== 0);
1728 /* Avoid using the first bg of a flexgroup for data files */
1729 if ((ac
->ac_flags
& EXT4_MB_HINT_DATA
) &&
1730 (flex_size
>= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
) &&
1731 ((group
% flex_size
) == 0))
1734 bits
= ac
->ac_sb
->s_blocksize_bits
+ 1;
1735 for (i
= ac
->ac_2order
; i
<= bits
; i
++)
1736 if (grp
->bb_counters
[i
] > 0)
1740 if ((free
/ fragments
) >= ac
->ac_g_ex
.fe_len
)
1744 if (free
>= ac
->ac_g_ex
.fe_len
)
1757 * lock the group_info alloc_sem of all the groups
1758 * belonging to the same buddy cache page. This
1759 * make sure other parallel operation on the buddy
1760 * cache doesn't happen whild holding the buddy cache
1763 int ext4_mb_get_buddy_cache_lock(struct super_block
*sb
, ext4_group_t group
)
1767 int blocks_per_page
;
1768 int groups_per_page
;
1769 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
1770 ext4_group_t first_group
;
1771 struct ext4_group_info
*grp
;
1773 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1775 * the buddy cache inode stores the block bitmap
1776 * and buddy information in consecutive blocks.
1777 * So for each group we need two blocks.
1780 pnum
= block
/ blocks_per_page
;
1781 first_group
= pnum
* blocks_per_page
/ 2;
1783 groups_per_page
= blocks_per_page
>> 1;
1784 if (groups_per_page
== 0)
1785 groups_per_page
= 1;
1786 /* read all groups the page covers into the cache */
1787 for (i
= 0; i
< groups_per_page
; i
++) {
1789 if ((first_group
+ i
) >= ngroups
)
1791 grp
= ext4_get_group_info(sb
, first_group
+ i
);
1792 /* take all groups write allocation
1793 * semaphore. This make sure there is
1794 * no block allocation going on in any
1797 down_write_nested(&grp
->alloc_sem
, i
);
1802 void ext4_mb_put_buddy_cache_lock(struct super_block
*sb
,
1803 ext4_group_t group
, int locked_group
)
1807 int blocks_per_page
;
1808 ext4_group_t first_group
;
1809 struct ext4_group_info
*grp
;
1811 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1813 * the buddy cache inode stores the block bitmap
1814 * and buddy information in consecutive blocks.
1815 * So for each group we need two blocks.
1818 pnum
= block
/ blocks_per_page
;
1819 first_group
= pnum
* blocks_per_page
/ 2;
1820 /* release locks on all the groups */
1821 for (i
= 0; i
< locked_group
; i
++) {
1823 grp
= ext4_get_group_info(sb
, first_group
+ i
);
1824 /* take all groups write allocation
1825 * semaphore. This make sure there is
1826 * no block allocation going on in any
1829 up_write(&grp
->alloc_sem
);
1834 static int ext4_mb_init_group(struct super_block
*sb
, ext4_group_t group
)
1839 int blocks_per_page
;
1840 int block
, pnum
, poff
;
1841 int num_grp_locked
= 0;
1842 struct ext4_group_info
*this_grp
;
1843 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1844 struct inode
*inode
= sbi
->s_buddy_cache
;
1845 struct page
*page
= NULL
, *bitmap_page
= NULL
;
1847 mb_debug("init group %lu\n", group
);
1848 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1849 this_grp
= ext4_get_group_info(sb
, group
);
1851 * This ensures we don't add group
1852 * to this buddy cache via resize
1854 num_grp_locked
= ext4_mb_get_buddy_cache_lock(sb
, group
);
1855 if (!EXT4_MB_GRP_NEED_INIT(this_grp
)) {
1857 * somebody initialized the group
1858 * return without doing anything
1864 * the buddy cache inode stores the block bitmap
1865 * and buddy information in consecutive blocks.
1866 * So for each group we need two blocks.
1869 pnum
= block
/ blocks_per_page
;
1870 poff
= block
% blocks_per_page
;
1871 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1873 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1874 ret
= ext4_mb_init_cache(page
, NULL
);
1881 if (page
== NULL
|| !PageUptodate(page
)) {
1885 mark_page_accessed(page
);
1887 bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
1889 /* init buddy cache */
1891 pnum
= block
/ blocks_per_page
;
1892 poff
= block
% blocks_per_page
;
1893 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1894 if (page
== bitmap_page
) {
1896 * If both the bitmap and buddy are in
1897 * the same page we don't need to force
1902 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1903 ret
= ext4_mb_init_cache(page
, bitmap
);
1910 if (page
== NULL
|| !PageUptodate(page
)) {
1914 mark_page_accessed(page
);
1916 ext4_mb_put_buddy_cache_lock(sb
, group
, num_grp_locked
);
1918 page_cache_release(bitmap_page
);
1920 page_cache_release(page
);
1924 static noinline_for_stack
int
1925 ext4_mb_regular_allocator(struct ext4_allocation_context
*ac
)
1927 ext4_group_t ngroups
, group
, i
;
1931 struct ext4_sb_info
*sbi
;
1932 struct super_block
*sb
;
1933 struct ext4_buddy e4b
;
1938 ngroups
= ext4_get_groups_count(sb
);
1939 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1941 /* first, try the goal */
1942 err
= ext4_mb_find_by_goal(ac
, &e4b
);
1943 if (err
|| ac
->ac_status
== AC_STATUS_FOUND
)
1946 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
1950 * ac->ac2_order is set only if the fe_len is a power of 2
1951 * if ac2_order is set we also set criteria to 0 so that we
1952 * try exact allocation using buddy.
1954 i
= fls(ac
->ac_g_ex
.fe_len
);
1957 * We search using buddy data only if the order of the request
1958 * is greater than equal to the sbi_s_mb_order2_reqs
1959 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
1961 if (i
>= sbi
->s_mb_order2_reqs
) {
1963 * This should tell if fe_len is exactly power of 2
1965 if ((ac
->ac_g_ex
.fe_len
& (~(1 << (i
- 1)))) == 0)
1966 ac
->ac_2order
= i
- 1;
1969 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
1970 /* if stream allocation is enabled, use global goal */
1971 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
1972 isize
= i_size_read(ac
->ac_inode
) >> bsbits
;
1976 if (size
< sbi
->s_mb_stream_request
&&
1977 (ac
->ac_flags
& EXT4_MB_HINT_DATA
)) {
1978 /* TBD: may be hot point */
1979 spin_lock(&sbi
->s_md_lock
);
1980 ac
->ac_g_ex
.fe_group
= sbi
->s_mb_last_group
;
1981 ac
->ac_g_ex
.fe_start
= sbi
->s_mb_last_start
;
1982 spin_unlock(&sbi
->s_md_lock
);
1984 /* Let's just scan groups to find more-less suitable blocks */
1985 cr
= ac
->ac_2order
? 0 : 1;
1987 * cr == 0 try to get exact allocation,
1988 * cr == 3 try to get anything
1991 for (; cr
< 4 && ac
->ac_status
== AC_STATUS_CONTINUE
; cr
++) {
1992 ac
->ac_criteria
= cr
;
1994 * searching for the right group start
1995 * from the goal value specified
1997 group
= ac
->ac_g_ex
.fe_group
;
1999 for (i
= 0; i
< ngroups
; group
++, i
++) {
2000 struct ext4_group_info
*grp
;
2001 struct ext4_group_desc
*desc
;
2003 if (group
== ngroups
)
2006 /* quick check to skip empty groups */
2007 grp
= ext4_get_group_info(sb
, group
);
2008 if (grp
->bb_free
== 0)
2012 * if the group is already init we check whether it is
2013 * a good group and if not we don't load the buddy
2015 if (EXT4_MB_GRP_NEED_INIT(grp
)) {
2017 * we need full data about the group
2018 * to make a good selection
2020 err
= ext4_mb_init_group(sb
, group
);
2026 * If the particular group doesn't satisfy our
2027 * criteria we continue with the next group
2029 if (!ext4_mb_good_group(ac
, group
, cr
))
2032 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2036 ext4_lock_group(sb
, group
);
2037 if (!ext4_mb_good_group(ac
, group
, cr
)) {
2038 /* someone did allocation from this group */
2039 ext4_unlock_group(sb
, group
);
2040 ext4_mb_release_desc(&e4b
);
2044 ac
->ac_groups_scanned
++;
2045 desc
= ext4_get_group_desc(sb
, group
, NULL
);
2047 ext4_mb_simple_scan_group(ac
, &e4b
);
2049 ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
)
2050 ext4_mb_scan_aligned(ac
, &e4b
);
2052 ext4_mb_complex_scan_group(ac
, &e4b
);
2054 ext4_unlock_group(sb
, group
);
2055 ext4_mb_release_desc(&e4b
);
2057 if (ac
->ac_status
!= AC_STATUS_CONTINUE
)
2062 if (ac
->ac_b_ex
.fe_len
> 0 && ac
->ac_status
!= AC_STATUS_FOUND
&&
2063 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
2065 * We've been searching too long. Let's try to allocate
2066 * the best chunk we've found so far
2069 ext4_mb_try_best_found(ac
, &e4b
);
2070 if (ac
->ac_status
!= AC_STATUS_FOUND
) {
2072 * Someone more lucky has already allocated it.
2073 * The only thing we can do is just take first
2075 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2077 ac
->ac_b_ex
.fe_group
= 0;
2078 ac
->ac_b_ex
.fe_start
= 0;
2079 ac
->ac_b_ex
.fe_len
= 0;
2080 ac
->ac_status
= AC_STATUS_CONTINUE
;
2081 ac
->ac_flags
|= EXT4_MB_HINT_FIRST
;
2083 atomic_inc(&sbi
->s_mb_lost_chunks
);
2091 #ifdef EXT4_MB_HISTORY
2092 struct ext4_mb_proc_session
{
2093 struct ext4_mb_history
*history
;
2094 struct super_block
*sb
;
2099 static void *ext4_mb_history_skip_empty(struct ext4_mb_proc_session
*s
,
2100 struct ext4_mb_history
*hs
,
2103 if (hs
== s
->history
+ s
->max
)
2105 if (!first
&& hs
== s
->history
+ s
->start
)
2107 while (hs
->orig
.fe_len
== 0) {
2109 if (hs
== s
->history
+ s
->max
)
2111 if (hs
== s
->history
+ s
->start
)
2117 static void *ext4_mb_seq_history_start(struct seq_file
*seq
, loff_t
*pos
)
2119 struct ext4_mb_proc_session
*s
= seq
->private;
2120 struct ext4_mb_history
*hs
;
2124 return SEQ_START_TOKEN
;
2125 hs
= ext4_mb_history_skip_empty(s
, s
->history
+ s
->start
, 1);
2128 while (--l
&& (hs
= ext4_mb_history_skip_empty(s
, ++hs
, 0)) != NULL
);
2132 static void *ext4_mb_seq_history_next(struct seq_file
*seq
, void *v
,
2135 struct ext4_mb_proc_session
*s
= seq
->private;
2136 struct ext4_mb_history
*hs
= v
;
2139 if (v
== SEQ_START_TOKEN
)
2140 return ext4_mb_history_skip_empty(s
, s
->history
+ s
->start
, 1);
2142 return ext4_mb_history_skip_empty(s
, ++hs
, 0);
2145 static int ext4_mb_seq_history_show(struct seq_file
*seq
, void *v
)
2147 char buf
[25], buf2
[25], buf3
[25], *fmt
;
2148 struct ext4_mb_history
*hs
= v
;
2150 if (v
== SEQ_START_TOKEN
) {
2151 seq_printf(seq
, "%-5s %-8s %-23s %-23s %-23s %-5s "
2152 "%-5s %-2s %-5s %-5s %-5s %-6s\n",
2153 "pid", "inode", "original", "goal", "result", "found",
2154 "grps", "cr", "flags", "merge", "tail", "broken");
2158 if (hs
->op
== EXT4_MB_HISTORY_ALLOC
) {
2159 fmt
= "%-5u %-8u %-23s %-23s %-23s %-5u %-5u %-2u "
2160 "%-5u %-5s %-5u %-6u\n";
2161 sprintf(buf2
, "%u/%d/%u@%u", hs
->result
.fe_group
,
2162 hs
->result
.fe_start
, hs
->result
.fe_len
,
2163 hs
->result
.fe_logical
);
2164 sprintf(buf
, "%u/%d/%u@%u", hs
->orig
.fe_group
,
2165 hs
->orig
.fe_start
, hs
->orig
.fe_len
,
2166 hs
->orig
.fe_logical
);
2167 sprintf(buf3
, "%u/%d/%u@%u", hs
->goal
.fe_group
,
2168 hs
->goal
.fe_start
, hs
->goal
.fe_len
,
2169 hs
->goal
.fe_logical
);
2170 seq_printf(seq
, fmt
, hs
->pid
, hs
->ino
, buf
, buf3
, buf2
,
2171 hs
->found
, hs
->groups
, hs
->cr
, hs
->flags
,
2172 hs
->merged
? "M" : "", hs
->tail
,
2173 hs
->buddy
? 1 << hs
->buddy
: 0);
2174 } else if (hs
->op
== EXT4_MB_HISTORY_PREALLOC
) {
2175 fmt
= "%-5u %-8u %-23s %-23s %-23s\n";
2176 sprintf(buf2
, "%u/%d/%u@%u", hs
->result
.fe_group
,
2177 hs
->result
.fe_start
, hs
->result
.fe_len
,
2178 hs
->result
.fe_logical
);
2179 sprintf(buf
, "%u/%d/%u@%u", hs
->orig
.fe_group
,
2180 hs
->orig
.fe_start
, hs
->orig
.fe_len
,
2181 hs
->orig
.fe_logical
);
2182 seq_printf(seq
, fmt
, hs
->pid
, hs
->ino
, buf
, "", buf2
);
2183 } else if (hs
->op
== EXT4_MB_HISTORY_DISCARD
) {
2184 sprintf(buf2
, "%u/%d/%u", hs
->result
.fe_group
,
2185 hs
->result
.fe_start
, hs
->result
.fe_len
);
2186 seq_printf(seq
, "%-5u %-8u %-23s discard\n",
2187 hs
->pid
, hs
->ino
, buf2
);
2188 } else if (hs
->op
== EXT4_MB_HISTORY_FREE
) {
2189 sprintf(buf2
, "%u/%d/%u", hs
->result
.fe_group
,
2190 hs
->result
.fe_start
, hs
->result
.fe_len
);
2191 seq_printf(seq
, "%-5u %-8u %-23s free\n",
2192 hs
->pid
, hs
->ino
, buf2
);
2197 static void ext4_mb_seq_history_stop(struct seq_file
*seq
, void *v
)
2201 static struct seq_operations ext4_mb_seq_history_ops
= {
2202 .start
= ext4_mb_seq_history_start
,
2203 .next
= ext4_mb_seq_history_next
,
2204 .stop
= ext4_mb_seq_history_stop
,
2205 .show
= ext4_mb_seq_history_show
,
2208 static int ext4_mb_seq_history_open(struct inode
*inode
, struct file
*file
)
2210 struct super_block
*sb
= PDE(inode
)->data
;
2211 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2212 struct ext4_mb_proc_session
*s
;
2216 if (unlikely(sbi
->s_mb_history
== NULL
))
2218 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
2222 size
= sizeof(struct ext4_mb_history
) * sbi
->s_mb_history_max
;
2223 s
->history
= kmalloc(size
, GFP_KERNEL
);
2224 if (s
->history
== NULL
) {
2229 spin_lock(&sbi
->s_mb_history_lock
);
2230 memcpy(s
->history
, sbi
->s_mb_history
, size
);
2231 s
->max
= sbi
->s_mb_history_max
;
2232 s
->start
= sbi
->s_mb_history_cur
% s
->max
;
2233 spin_unlock(&sbi
->s_mb_history_lock
);
2235 rc
= seq_open(file
, &ext4_mb_seq_history_ops
);
2237 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2247 static int ext4_mb_seq_history_release(struct inode
*inode
, struct file
*file
)
2249 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
2250 struct ext4_mb_proc_session
*s
= seq
->private;
2253 return seq_release(inode
, file
);
2256 static ssize_t
ext4_mb_seq_history_write(struct file
*file
,
2257 const char __user
*buffer
,
2258 size_t count
, loff_t
*ppos
)
2260 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
2261 struct ext4_mb_proc_session
*s
= seq
->private;
2262 struct super_block
*sb
= s
->sb
;
2266 if (count
>= sizeof(str
)) {
2267 printk(KERN_ERR
"EXT4-fs: %s string too long, max %u bytes\n",
2268 "mb_history", (int)sizeof(str
));
2272 if (copy_from_user(str
, buffer
, count
))
2275 value
= simple_strtol(str
, NULL
, 0);
2278 EXT4_SB(sb
)->s_mb_history_filter
= value
;
2283 static struct file_operations ext4_mb_seq_history_fops
= {
2284 .owner
= THIS_MODULE
,
2285 .open
= ext4_mb_seq_history_open
,
2287 .write
= ext4_mb_seq_history_write
,
2288 .llseek
= seq_lseek
,
2289 .release
= ext4_mb_seq_history_release
,
2292 static void *ext4_mb_seq_groups_start(struct seq_file
*seq
, loff_t
*pos
)
2294 struct super_block
*sb
= seq
->private;
2297 if (*pos
< 0 || *pos
>= ext4_get_groups_count(sb
))
2300 return (void *) ((unsigned long) group
);
2303 static void *ext4_mb_seq_groups_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2305 struct super_block
*sb
= seq
->private;
2309 if (*pos
< 0 || *pos
>= ext4_get_groups_count(sb
))
2312 return (void *) ((unsigned long) group
);
2315 static int ext4_mb_seq_groups_show(struct seq_file
*seq
, void *v
)
2317 struct super_block
*sb
= seq
->private;
2318 ext4_group_t group
= (ext4_group_t
) ((unsigned long) v
);
2321 struct ext4_buddy e4b
;
2323 struct ext4_group_info info
;
2324 unsigned short counters
[16];
2329 seq_printf(seq
, "#%-5s: %-5s %-5s %-5s "
2330 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2331 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2332 "group", "free", "frags", "first",
2333 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2334 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2336 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(sg
.info
.bb_counters
[0]) +
2337 sizeof(struct ext4_group_info
);
2338 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2340 seq_printf(seq
, "#%-5u: I/O error\n", group
);
2343 ext4_lock_group(sb
, group
);
2344 memcpy(&sg
, ext4_get_group_info(sb
, group
), i
);
2345 ext4_unlock_group(sb
, group
);
2346 ext4_mb_release_desc(&e4b
);
2348 seq_printf(seq
, "#%-5u: %-5u %-5u %-5u [", group
, sg
.info
.bb_free
,
2349 sg
.info
.bb_fragments
, sg
.info
.bb_first_free
);
2350 for (i
= 0; i
<= 13; i
++)
2351 seq_printf(seq
, " %-5u", i
<= sb
->s_blocksize_bits
+ 1 ?
2352 sg
.info
.bb_counters
[i
] : 0);
2353 seq_printf(seq
, " ]\n");
2358 static void ext4_mb_seq_groups_stop(struct seq_file
*seq
, void *v
)
2362 static struct seq_operations ext4_mb_seq_groups_ops
= {
2363 .start
= ext4_mb_seq_groups_start
,
2364 .next
= ext4_mb_seq_groups_next
,
2365 .stop
= ext4_mb_seq_groups_stop
,
2366 .show
= ext4_mb_seq_groups_show
,
2369 static int ext4_mb_seq_groups_open(struct inode
*inode
, struct file
*file
)
2371 struct super_block
*sb
= PDE(inode
)->data
;
2374 rc
= seq_open(file
, &ext4_mb_seq_groups_ops
);
2376 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2383 static struct file_operations ext4_mb_seq_groups_fops
= {
2384 .owner
= THIS_MODULE
,
2385 .open
= ext4_mb_seq_groups_open
,
2387 .llseek
= seq_lseek
,
2388 .release
= seq_release
,
2391 static void ext4_mb_history_release(struct super_block
*sb
)
2393 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2395 if (sbi
->s_proc
!= NULL
) {
2396 remove_proc_entry("mb_groups", sbi
->s_proc
);
2397 if (sbi
->s_mb_history_max
)
2398 remove_proc_entry("mb_history", sbi
->s_proc
);
2400 kfree(sbi
->s_mb_history
);
2403 static void ext4_mb_history_init(struct super_block
*sb
)
2405 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2408 if (sbi
->s_proc
!= NULL
) {
2409 if (sbi
->s_mb_history_max
)
2410 proc_create_data("mb_history", S_IRUGO
, sbi
->s_proc
,
2411 &ext4_mb_seq_history_fops
, sb
);
2412 proc_create_data("mb_groups", S_IRUGO
, sbi
->s_proc
,
2413 &ext4_mb_seq_groups_fops
, sb
);
2416 sbi
->s_mb_history_cur
= 0;
2417 spin_lock_init(&sbi
->s_mb_history_lock
);
2418 i
= sbi
->s_mb_history_max
* sizeof(struct ext4_mb_history
);
2419 sbi
->s_mb_history
= i
? kzalloc(i
, GFP_KERNEL
) : NULL
;
2420 /* if we can't allocate history, then we simple won't use it */
2423 static noinline_for_stack
void
2424 ext4_mb_store_history(struct ext4_allocation_context
*ac
)
2426 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
2427 struct ext4_mb_history h
;
2429 if (sbi
->s_mb_history
== NULL
)
2432 if (!(ac
->ac_op
& sbi
->s_mb_history_filter
))
2436 h
.pid
= current
->pid
;
2437 h
.ino
= ac
->ac_inode
? ac
->ac_inode
->i_ino
: 0;
2438 h
.orig
= ac
->ac_o_ex
;
2439 h
.result
= ac
->ac_b_ex
;
2440 h
.flags
= ac
->ac_flags
;
2441 h
.found
= ac
->ac_found
;
2442 h
.groups
= ac
->ac_groups_scanned
;
2443 h
.cr
= ac
->ac_criteria
;
2444 h
.tail
= ac
->ac_tail
;
2445 h
.buddy
= ac
->ac_buddy
;
2447 if (ac
->ac_op
== EXT4_MB_HISTORY_ALLOC
) {
2448 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
2449 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
2451 h
.goal
= ac
->ac_g_ex
;
2452 h
.result
= ac
->ac_f_ex
;
2455 spin_lock(&sbi
->s_mb_history_lock
);
2456 memcpy(sbi
->s_mb_history
+ sbi
->s_mb_history_cur
, &h
, sizeof(h
));
2457 if (++sbi
->s_mb_history_cur
>= sbi
->s_mb_history_max
)
2458 sbi
->s_mb_history_cur
= 0;
2459 spin_unlock(&sbi
->s_mb_history_lock
);
2463 #define ext4_mb_history_release(sb)
2464 #define ext4_mb_history_init(sb)
2468 /* Create and initialize ext4_group_info data for the given group. */
2469 int ext4_mb_add_groupinfo(struct super_block
*sb
, ext4_group_t group
,
2470 struct ext4_group_desc
*desc
)
2474 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2475 struct ext4_group_info
**meta_group_info
;
2478 * First check if this group is the first of a reserved block.
2479 * If it's true, we have to allocate a new table of pointers
2480 * to ext4_group_info structures
2482 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0) {
2483 metalen
= sizeof(*meta_group_info
) <<
2484 EXT4_DESC_PER_BLOCK_BITS(sb
);
2485 meta_group_info
= kmalloc(metalen
, GFP_KERNEL
);
2486 if (meta_group_info
== NULL
) {
2487 printk(KERN_ERR
"EXT4-fs: can't allocate mem for a "
2489 goto exit_meta_group_info
;
2491 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)] =
2496 * calculate needed size. if change bb_counters size,
2497 * don't forget about ext4_mb_generate_buddy()
2499 len
= offsetof(typeof(**meta_group_info
),
2500 bb_counters
[sb
->s_blocksize_bits
+ 2]);
2503 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)];
2504 i
= group
& (EXT4_DESC_PER_BLOCK(sb
) - 1);
2506 meta_group_info
[i
] = kzalloc(len
, GFP_KERNEL
);
2507 if (meta_group_info
[i
] == NULL
) {
2508 printk(KERN_ERR
"EXT4-fs: can't allocate buddy mem\n");
2509 goto exit_group_info
;
2511 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
,
2512 &(meta_group_info
[i
]->bb_state
));
2515 * initialize bb_free to be able to skip
2516 * empty groups without initialization
2518 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2519 meta_group_info
[i
]->bb_free
=
2520 ext4_free_blocks_after_init(sb
, group
, desc
);
2522 meta_group_info
[i
]->bb_free
=
2523 ext4_free_blks_count(sb
, desc
);
2526 INIT_LIST_HEAD(&meta_group_info
[i
]->bb_prealloc_list
);
2527 init_rwsem(&meta_group_info
[i
]->alloc_sem
);
2528 meta_group_info
[i
]->bb_free_root
.rb_node
= NULL
;;
2532 struct buffer_head
*bh
;
2533 meta_group_info
[i
]->bb_bitmap
=
2534 kmalloc(sb
->s_blocksize
, GFP_KERNEL
);
2535 BUG_ON(meta_group_info
[i
]->bb_bitmap
== NULL
);
2536 bh
= ext4_read_block_bitmap(sb
, group
);
2538 memcpy(meta_group_info
[i
]->bb_bitmap
, bh
->b_data
,
2547 /* If a meta_group_info table has been allocated, release it now */
2548 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0)
2549 kfree(sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)]);
2550 exit_meta_group_info
:
2552 } /* ext4_mb_add_groupinfo */
2555 * Update an existing group.
2556 * This function is used for online resize
2558 void ext4_mb_update_group_info(struct ext4_group_info
*grp
, ext4_grpblk_t add
)
2560 grp
->bb_free
+= add
;
2563 static int ext4_mb_init_backend(struct super_block
*sb
)
2565 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
2568 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2569 struct ext4_super_block
*es
= sbi
->s_es
;
2570 int num_meta_group_infos
;
2571 int num_meta_group_infos_max
;
2573 struct ext4_group_info
**meta_group_info
;
2574 struct ext4_group_desc
*desc
;
2576 /* This is the number of blocks used by GDT */
2577 num_meta_group_infos
= (ngroups
+ EXT4_DESC_PER_BLOCK(sb
) -
2578 1) >> EXT4_DESC_PER_BLOCK_BITS(sb
);
2581 * This is the total number of blocks used by GDT including
2582 * the number of reserved blocks for GDT.
2583 * The s_group_info array is allocated with this value
2584 * to allow a clean online resize without a complex
2585 * manipulation of pointer.
2586 * The drawback is the unused memory when no resize
2587 * occurs but it's very low in terms of pages
2588 * (see comments below)
2589 * Need to handle this properly when META_BG resizing is allowed
2591 num_meta_group_infos_max
= num_meta_group_infos
+
2592 le16_to_cpu(es
->s_reserved_gdt_blocks
);
2595 * array_size is the size of s_group_info array. We round it
2596 * to the next power of two because this approximation is done
2597 * internally by kmalloc so we can have some more memory
2598 * for free here (e.g. may be used for META_BG resize).
2601 while (array_size
< sizeof(*sbi
->s_group_info
) *
2602 num_meta_group_infos_max
)
2603 array_size
= array_size
<< 1;
2604 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2605 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2606 * So a two level scheme suffices for now. */
2607 sbi
->s_group_info
= kmalloc(array_size
, GFP_KERNEL
);
2608 if (sbi
->s_group_info
== NULL
) {
2609 printk(KERN_ERR
"EXT4-fs: can't allocate buddy meta group\n");
2612 sbi
->s_buddy_cache
= new_inode(sb
);
2613 if (sbi
->s_buddy_cache
== NULL
) {
2614 printk(KERN_ERR
"EXT4-fs: can't get new inode\n");
2617 EXT4_I(sbi
->s_buddy_cache
)->i_disksize
= 0;
2619 metalen
= sizeof(*meta_group_info
) << EXT4_DESC_PER_BLOCK_BITS(sb
);
2620 for (i
= 0; i
< num_meta_group_infos
; i
++) {
2621 if ((i
+ 1) == num_meta_group_infos
)
2622 metalen
= sizeof(*meta_group_info
) *
2624 (i
<< EXT4_DESC_PER_BLOCK_BITS(sb
)));
2625 meta_group_info
= kmalloc(metalen
, GFP_KERNEL
);
2626 if (meta_group_info
== NULL
) {
2627 printk(KERN_ERR
"EXT4-fs: can't allocate mem for a "
2631 sbi
->s_group_info
[i
] = meta_group_info
;
2634 for (i
= 0; i
< ngroups
; i
++) {
2635 desc
= ext4_get_group_desc(sb
, i
, NULL
);
2638 "EXT4-fs: can't read descriptor %u\n", i
);
2641 if (ext4_mb_add_groupinfo(sb
, i
, desc
) != 0)
2649 kfree(ext4_get_group_info(sb
, i
));
2650 i
= num_meta_group_infos
;
2653 kfree(sbi
->s_group_info
[i
]);
2654 iput(sbi
->s_buddy_cache
);
2656 kfree(sbi
->s_group_info
);
2660 int ext4_mb_init(struct super_block
*sb
, int needs_recovery
)
2662 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2668 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(unsigned short);
2670 sbi
->s_mb_offsets
= kmalloc(i
, GFP_KERNEL
);
2671 if (sbi
->s_mb_offsets
== NULL
) {
2675 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(unsigned int);
2676 sbi
->s_mb_maxs
= kmalloc(i
, GFP_KERNEL
);
2677 if (sbi
->s_mb_maxs
== NULL
) {
2678 kfree(sbi
->s_mb_offsets
);
2682 /* order 0 is regular bitmap */
2683 sbi
->s_mb_maxs
[0] = sb
->s_blocksize
<< 3;
2684 sbi
->s_mb_offsets
[0] = 0;
2688 max
= sb
->s_blocksize
<< 2;
2690 sbi
->s_mb_offsets
[i
] = offset
;
2691 sbi
->s_mb_maxs
[i
] = max
;
2692 offset
+= 1 << (sb
->s_blocksize_bits
- i
);
2695 } while (i
<= sb
->s_blocksize_bits
+ 1);
2697 /* init file for buddy data */
2698 ret
= ext4_mb_init_backend(sb
);
2700 kfree(sbi
->s_mb_offsets
);
2701 kfree(sbi
->s_mb_maxs
);
2705 spin_lock_init(&sbi
->s_md_lock
);
2706 spin_lock_init(&sbi
->s_bal_lock
);
2708 sbi
->s_mb_max_to_scan
= MB_DEFAULT_MAX_TO_SCAN
;
2709 sbi
->s_mb_min_to_scan
= MB_DEFAULT_MIN_TO_SCAN
;
2710 sbi
->s_mb_stats
= MB_DEFAULT_STATS
;
2711 sbi
->s_mb_stream_request
= MB_DEFAULT_STREAM_THRESHOLD
;
2712 sbi
->s_mb_order2_reqs
= MB_DEFAULT_ORDER2_REQS
;
2713 sbi
->s_mb_history_filter
= EXT4_MB_HISTORY_DEFAULT
;
2714 sbi
->s_mb_group_prealloc
= MB_DEFAULT_GROUP_PREALLOC
;
2716 sbi
->s_locality_groups
= alloc_percpu(struct ext4_locality_group
);
2717 if (sbi
->s_locality_groups
== NULL
) {
2718 kfree(sbi
->s_mb_offsets
);
2719 kfree(sbi
->s_mb_maxs
);
2722 for_each_possible_cpu(i
) {
2723 struct ext4_locality_group
*lg
;
2724 lg
= per_cpu_ptr(sbi
->s_locality_groups
, i
);
2725 mutex_init(&lg
->lg_mutex
);
2726 for (j
= 0; j
< PREALLOC_TB_SIZE
; j
++)
2727 INIT_LIST_HEAD(&lg
->lg_prealloc_list
[j
]);
2728 spin_lock_init(&lg
->lg_prealloc_lock
);
2731 ext4_mb_history_init(sb
);
2734 sbi
->s_journal
->j_commit_callback
= release_blocks_on_commit
;
2736 printk(KERN_INFO
"EXT4-fs: mballoc enabled\n");
2740 /* need to called with the ext4 group lock held */
2741 static void ext4_mb_cleanup_pa(struct ext4_group_info
*grp
)
2743 struct ext4_prealloc_space
*pa
;
2744 struct list_head
*cur
, *tmp
;
2747 list_for_each_safe(cur
, tmp
, &grp
->bb_prealloc_list
) {
2748 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
2749 list_del(&pa
->pa_group_list
);
2751 kmem_cache_free(ext4_pspace_cachep
, pa
);
2754 mb_debug("mballoc: %u PAs left\n", count
);
2758 int ext4_mb_release(struct super_block
*sb
)
2760 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
2762 int num_meta_group_infos
;
2763 struct ext4_group_info
*grinfo
;
2764 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2766 if (sbi
->s_group_info
) {
2767 for (i
= 0; i
< ngroups
; i
++) {
2768 grinfo
= ext4_get_group_info(sb
, i
);
2770 kfree(grinfo
->bb_bitmap
);
2772 ext4_lock_group(sb
, i
);
2773 ext4_mb_cleanup_pa(grinfo
);
2774 ext4_unlock_group(sb
, i
);
2777 num_meta_group_infos
= (ngroups
+
2778 EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2779 EXT4_DESC_PER_BLOCK_BITS(sb
);
2780 for (i
= 0; i
< num_meta_group_infos
; i
++)
2781 kfree(sbi
->s_group_info
[i
]);
2782 kfree(sbi
->s_group_info
);
2784 kfree(sbi
->s_mb_offsets
);
2785 kfree(sbi
->s_mb_maxs
);
2786 if (sbi
->s_buddy_cache
)
2787 iput(sbi
->s_buddy_cache
);
2788 if (sbi
->s_mb_stats
) {
2790 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2791 atomic_read(&sbi
->s_bal_allocated
),
2792 atomic_read(&sbi
->s_bal_reqs
),
2793 atomic_read(&sbi
->s_bal_success
));
2795 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2796 "%u 2^N hits, %u breaks, %u lost\n",
2797 atomic_read(&sbi
->s_bal_ex_scanned
),
2798 atomic_read(&sbi
->s_bal_goals
),
2799 atomic_read(&sbi
->s_bal_2orders
),
2800 atomic_read(&sbi
->s_bal_breaks
),
2801 atomic_read(&sbi
->s_mb_lost_chunks
));
2803 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2804 sbi
->s_mb_buddies_generated
++,
2805 sbi
->s_mb_generation_time
);
2807 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2808 atomic_read(&sbi
->s_mb_preallocated
),
2809 atomic_read(&sbi
->s_mb_discarded
));
2812 free_percpu(sbi
->s_locality_groups
);
2813 ext4_mb_history_release(sb
);
2819 * This function is called by the jbd2 layer once the commit has finished,
2820 * so we know we can free the blocks that were released with that commit.
2822 static void release_blocks_on_commit(journal_t
*journal
, transaction_t
*txn
)
2824 struct super_block
*sb
= journal
->j_private
;
2825 struct ext4_buddy e4b
;
2826 struct ext4_group_info
*db
;
2827 int err
, count
= 0, count2
= 0;
2828 struct ext4_free_data
*entry
;
2829 ext4_fsblk_t discard_block
;
2830 struct list_head
*l
, *ltmp
;
2832 list_for_each_safe(l
, ltmp
, &txn
->t_private_list
) {
2833 entry
= list_entry(l
, struct ext4_free_data
, list
);
2835 mb_debug("gonna free %u blocks in group %u (0x%p):",
2836 entry
->count
, entry
->group
, entry
);
2838 err
= ext4_mb_load_buddy(sb
, entry
->group
, &e4b
);
2839 /* we expect to find existing buddy because it's pinned */
2843 /* there are blocks to put in buddy to make them really free */
2844 count
+= entry
->count
;
2846 ext4_lock_group(sb
, entry
->group
);
2847 /* Take it out of per group rb tree */
2848 rb_erase(&entry
->node
, &(db
->bb_free_root
));
2849 mb_free_blocks(NULL
, &e4b
, entry
->start_blk
, entry
->count
);
2851 if (!db
->bb_free_root
.rb_node
) {
2852 /* No more items in the per group rb tree
2853 * balance refcounts from ext4_mb_free_metadata()
2855 page_cache_release(e4b
.bd_buddy_page
);
2856 page_cache_release(e4b
.bd_bitmap_page
);
2858 ext4_unlock_group(sb
, entry
->group
);
2859 discard_block
= (ext4_fsblk_t
) entry
->group
* EXT4_BLOCKS_PER_GROUP(sb
)
2861 + le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
2862 trace_ext4_discard_blocks(sb
, (unsigned long long)discard_block
,
2864 sb_issue_discard(sb
, discard_block
, entry
->count
);
2866 kmem_cache_free(ext4_free_ext_cachep
, entry
);
2867 ext4_mb_release_desc(&e4b
);
2870 mb_debug("freed %u blocks in %u structures\n", count
, count2
);
2873 int __init
init_ext4_mballoc(void)
2875 ext4_pspace_cachep
=
2876 kmem_cache_create("ext4_prealloc_space",
2877 sizeof(struct ext4_prealloc_space
),
2878 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2879 if (ext4_pspace_cachep
== NULL
)
2883 kmem_cache_create("ext4_alloc_context",
2884 sizeof(struct ext4_allocation_context
),
2885 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2886 if (ext4_ac_cachep
== NULL
) {
2887 kmem_cache_destroy(ext4_pspace_cachep
);
2891 ext4_free_ext_cachep
=
2892 kmem_cache_create("ext4_free_block_extents",
2893 sizeof(struct ext4_free_data
),
2894 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2895 if (ext4_free_ext_cachep
== NULL
) {
2896 kmem_cache_destroy(ext4_pspace_cachep
);
2897 kmem_cache_destroy(ext4_ac_cachep
);
2903 void exit_ext4_mballoc(void)
2905 /* XXX: synchronize_rcu(); */
2906 kmem_cache_destroy(ext4_pspace_cachep
);
2907 kmem_cache_destroy(ext4_ac_cachep
);
2908 kmem_cache_destroy(ext4_free_ext_cachep
);
2913 * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
2914 * Returns 0 if success or error code
2916 static noinline_for_stack
int
2917 ext4_mb_mark_diskspace_used(struct ext4_allocation_context
*ac
,
2918 handle_t
*handle
, unsigned int reserv_blks
)
2920 struct buffer_head
*bitmap_bh
= NULL
;
2921 struct ext4_super_block
*es
;
2922 struct ext4_group_desc
*gdp
;
2923 struct buffer_head
*gdp_bh
;
2924 struct ext4_sb_info
*sbi
;
2925 struct super_block
*sb
;
2929 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
2930 BUG_ON(ac
->ac_b_ex
.fe_len
<= 0);
2938 bitmap_bh
= ext4_read_block_bitmap(sb
, ac
->ac_b_ex
.fe_group
);
2942 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
2947 gdp
= ext4_get_group_desc(sb
, ac
->ac_b_ex
.fe_group
, &gdp_bh
);
2951 ext4_debug("using block group %u(%d)\n", ac
->ac_b_ex
.fe_group
,
2952 ext4_free_blks_count(sb
, gdp
));
2954 err
= ext4_journal_get_write_access(handle
, gdp_bh
);
2958 block
= ac
->ac_b_ex
.fe_group
* EXT4_BLOCKS_PER_GROUP(sb
)
2959 + ac
->ac_b_ex
.fe_start
2960 + le32_to_cpu(es
->s_first_data_block
);
2962 len
= ac
->ac_b_ex
.fe_len
;
2963 if (!ext4_data_block_valid(sbi
, block
, len
)) {
2964 ext4_error(sb
, __func__
,
2965 "Allocating blocks %llu-%llu which overlap "
2966 "fs metadata\n", block
, block
+len
);
2967 /* File system mounted not to panic on error
2968 * Fix the bitmap and repeat the block allocation
2969 * We leak some of the blocks here.
2971 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
2972 mb_set_bits(bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,
2973 ac
->ac_b_ex
.fe_len
);
2974 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
2975 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
2981 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
2982 #ifdef AGGRESSIVE_CHECK
2985 for (i
= 0; i
< ac
->ac_b_ex
.fe_len
; i
++) {
2986 BUG_ON(mb_test_bit(ac
->ac_b_ex
.fe_start
+ i
,
2987 bitmap_bh
->b_data
));
2991 mb_set_bits(bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,ac
->ac_b_ex
.fe_len
);
2992 if (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2993 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT
);
2994 ext4_free_blks_set(sb
, gdp
,
2995 ext4_free_blocks_after_init(sb
,
2996 ac
->ac_b_ex
.fe_group
, gdp
));
2998 len
= ext4_free_blks_count(sb
, gdp
) - ac
->ac_b_ex
.fe_len
;
2999 ext4_free_blks_set(sb
, gdp
, len
);
3000 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, ac
->ac_b_ex
.fe_group
, gdp
);
3002 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3003 percpu_counter_sub(&sbi
->s_freeblocks_counter
, ac
->ac_b_ex
.fe_len
);
3005 * Now reduce the dirty block count also. Should not go negative
3007 if (!(ac
->ac_flags
& EXT4_MB_DELALLOC_RESERVED
))
3008 /* release all the reserved blocks if non delalloc */
3009 percpu_counter_sub(&sbi
->s_dirtyblocks_counter
, reserv_blks
);
3011 percpu_counter_sub(&sbi
->s_dirtyblocks_counter
,
3012 ac
->ac_b_ex
.fe_len
);
3013 /* convert reserved quota blocks to real quota blocks */
3014 vfs_dq_claim_block(ac
->ac_inode
, ac
->ac_b_ex
.fe_len
);
3017 if (sbi
->s_log_groups_per_flex
) {
3018 ext4_group_t flex_group
= ext4_flex_group(sbi
,
3019 ac
->ac_b_ex
.fe_group
);
3020 atomic_sub(ac
->ac_b_ex
.fe_len
,
3021 &sbi
->s_flex_groups
[flex_group
].free_blocks
);
3024 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
3027 err
= ext4_handle_dirty_metadata(handle
, NULL
, gdp_bh
);
3036 * here we normalize request for locality group
3037 * Group request are normalized to s_strip size if we set the same via mount
3038 * option. If not we set it to s_mb_group_prealloc which can be configured via
3039 * /sys/fs/ext4/<partition>/mb_group_prealloc
3041 * XXX: should we try to preallocate more than the group has now?
3043 static void ext4_mb_normalize_group_request(struct ext4_allocation_context
*ac
)
3045 struct super_block
*sb
= ac
->ac_sb
;
3046 struct ext4_locality_group
*lg
= ac
->ac_lg
;
3049 if (EXT4_SB(sb
)->s_stripe
)
3050 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_stripe
;
3052 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_mb_group_prealloc
;
3053 mb_debug("#%u: goal %u blocks for locality group\n",
3054 current
->pid
, ac
->ac_g_ex
.fe_len
);
3058 * Normalization means making request better in terms of
3059 * size and alignment
3061 static noinline_for_stack
void
3062 ext4_mb_normalize_request(struct ext4_allocation_context
*ac
,
3063 struct ext4_allocation_request
*ar
)
3067 loff_t size
, orig_size
, start_off
;
3068 ext4_lblk_t start
, orig_start
;
3069 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3070 struct ext4_prealloc_space
*pa
;
3072 /* do normalize only data requests, metadata requests
3073 do not need preallocation */
3074 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3077 /* sometime caller may want exact blocks */
3078 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
3081 /* caller may indicate that preallocation isn't
3082 * required (it's a tail, for example) */
3083 if (ac
->ac_flags
& EXT4_MB_HINT_NOPREALLOC
)
3086 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
) {
3087 ext4_mb_normalize_group_request(ac
);
3091 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
3093 /* first, let's learn actual file size
3094 * given current request is allocated */
3095 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
3096 size
= size
<< bsbits
;
3097 if (size
< i_size_read(ac
->ac_inode
))
3098 size
= i_size_read(ac
->ac_inode
);
3100 /* max size of free chunks */
3103 #define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3104 (req <= (size) || max <= (chunk_size))
3106 /* first, try to predict filesize */
3107 /* XXX: should this table be tunable? */
3109 if (size
<= 16 * 1024) {
3111 } else if (size
<= 32 * 1024) {
3113 } else if (size
<= 64 * 1024) {
3115 } else if (size
<= 128 * 1024) {
3117 } else if (size
<= 256 * 1024) {
3119 } else if (size
<= 512 * 1024) {
3121 } else if (size
<= 1024 * 1024) {
3123 } else if (NRL_CHECK_SIZE(size
, 4 * 1024 * 1024, max
, 2 * 1024)) {
3124 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3125 (21 - bsbits
)) << 21;
3126 size
= 2 * 1024 * 1024;
3127 } else if (NRL_CHECK_SIZE(size
, 8 * 1024 * 1024, max
, 4 * 1024)) {
3128 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3129 (22 - bsbits
)) << 22;
3130 size
= 4 * 1024 * 1024;
3131 } else if (NRL_CHECK_SIZE(ac
->ac_o_ex
.fe_len
,
3132 (8<<20)>>bsbits
, max
, 8 * 1024)) {
3133 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3134 (23 - bsbits
)) << 23;
3135 size
= 8 * 1024 * 1024;
3137 start_off
= (loff_t
)ac
->ac_o_ex
.fe_logical
<< bsbits
;
3138 size
= ac
->ac_o_ex
.fe_len
<< bsbits
;
3140 orig_size
= size
= size
>> bsbits
;
3141 orig_start
= start
= start_off
>> bsbits
;
3143 /* don't cover already allocated blocks in selected range */
3144 if (ar
->pleft
&& start
<= ar
->lleft
) {
3145 size
-= ar
->lleft
+ 1 - start
;
3146 start
= ar
->lleft
+ 1;
3148 if (ar
->pright
&& start
+ size
- 1 >= ar
->lright
)
3149 size
-= start
+ size
- ar
->lright
;
3153 /* check we don't cross already preallocated blocks */
3155 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3160 spin_lock(&pa
->pa_lock
);
3161 if (pa
->pa_deleted
) {
3162 spin_unlock(&pa
->pa_lock
);
3166 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
3168 /* PA must not overlap original request */
3169 BUG_ON(!(ac
->ac_o_ex
.fe_logical
>= pa_end
||
3170 ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
));
3172 /* skip PA normalized request doesn't overlap with */
3173 if (pa
->pa_lstart
>= end
) {
3174 spin_unlock(&pa
->pa_lock
);
3177 if (pa_end
<= start
) {
3178 spin_unlock(&pa
->pa_lock
);
3181 BUG_ON(pa
->pa_lstart
<= start
&& pa_end
>= end
);
3183 if (pa_end
<= ac
->ac_o_ex
.fe_logical
) {
3184 BUG_ON(pa_end
< start
);
3188 if (pa
->pa_lstart
> ac
->ac_o_ex
.fe_logical
) {
3189 BUG_ON(pa
->pa_lstart
> end
);
3190 end
= pa
->pa_lstart
;
3192 spin_unlock(&pa
->pa_lock
);
3197 /* XXX: extra loop to check we really don't overlap preallocations */
3199 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3201 spin_lock(&pa
->pa_lock
);
3202 if (pa
->pa_deleted
== 0) {
3203 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
3204 BUG_ON(!(start
>= pa_end
|| end
<= pa
->pa_lstart
));
3206 spin_unlock(&pa
->pa_lock
);
3210 if (start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3211 start
> ac
->ac_o_ex
.fe_logical
) {
3212 printk(KERN_ERR
"start %lu, size %lu, fe_logical %lu\n",
3213 (unsigned long) start
, (unsigned long) size
,
3214 (unsigned long) ac
->ac_o_ex
.fe_logical
);
3216 BUG_ON(start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3217 start
> ac
->ac_o_ex
.fe_logical
);
3218 BUG_ON(size
<= 0 || size
> EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
3220 /* now prepare goal request */
3222 /* XXX: is it better to align blocks WRT to logical
3223 * placement or satisfy big request as is */
3224 ac
->ac_g_ex
.fe_logical
= start
;
3225 ac
->ac_g_ex
.fe_len
= size
;
3227 /* define goal start in order to merge */
3228 if (ar
->pright
&& (ar
->lright
== (start
+ size
))) {
3229 /* merge to the right */
3230 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pright
- size
,
3231 &ac
->ac_f_ex
.fe_group
,
3232 &ac
->ac_f_ex
.fe_start
);
3233 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3235 if (ar
->pleft
&& (ar
->lleft
+ 1 == start
)) {
3236 /* merge to the left */
3237 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pleft
+ 1,
3238 &ac
->ac_f_ex
.fe_group
,
3239 &ac
->ac_f_ex
.fe_start
);
3240 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3243 mb_debug("goal: %u(was %u) blocks at %u\n", (unsigned) size
,
3244 (unsigned) orig_size
, (unsigned) start
);
3247 static void ext4_mb_collect_stats(struct ext4_allocation_context
*ac
)
3249 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3251 if (sbi
->s_mb_stats
&& ac
->ac_g_ex
.fe_len
> 1) {
3252 atomic_inc(&sbi
->s_bal_reqs
);
3253 atomic_add(ac
->ac_b_ex
.fe_len
, &sbi
->s_bal_allocated
);
3254 if (ac
->ac_o_ex
.fe_len
>= ac
->ac_g_ex
.fe_len
)
3255 atomic_inc(&sbi
->s_bal_success
);
3256 atomic_add(ac
->ac_found
, &sbi
->s_bal_ex_scanned
);
3257 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
3258 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
3259 atomic_inc(&sbi
->s_bal_goals
);
3260 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
)
3261 atomic_inc(&sbi
->s_bal_breaks
);
3264 ext4_mb_store_history(ac
);
3268 * use blocks preallocated to inode
3270 static void ext4_mb_use_inode_pa(struct ext4_allocation_context
*ac
,
3271 struct ext4_prealloc_space
*pa
)
3277 /* found preallocated blocks, use them */
3278 start
= pa
->pa_pstart
+ (ac
->ac_o_ex
.fe_logical
- pa
->pa_lstart
);
3279 end
= min(pa
->pa_pstart
+ pa
->pa_len
, start
+ ac
->ac_o_ex
.fe_len
);
3281 ext4_get_group_no_and_offset(ac
->ac_sb
, start
, &ac
->ac_b_ex
.fe_group
,
3282 &ac
->ac_b_ex
.fe_start
);
3283 ac
->ac_b_ex
.fe_len
= len
;
3284 ac
->ac_status
= AC_STATUS_FOUND
;
3287 BUG_ON(start
< pa
->pa_pstart
);
3288 BUG_ON(start
+ len
> pa
->pa_pstart
+ pa
->pa_len
);
3289 BUG_ON(pa
->pa_free
< len
);
3292 mb_debug("use %llu/%u from inode pa %p\n", start
, len
, pa
);
3296 * use blocks preallocated to locality group
3298 static void ext4_mb_use_group_pa(struct ext4_allocation_context
*ac
,
3299 struct ext4_prealloc_space
*pa
)
3301 unsigned int len
= ac
->ac_o_ex
.fe_len
;
3303 ext4_get_group_no_and_offset(ac
->ac_sb
, pa
->pa_pstart
,
3304 &ac
->ac_b_ex
.fe_group
,
3305 &ac
->ac_b_ex
.fe_start
);
3306 ac
->ac_b_ex
.fe_len
= len
;
3307 ac
->ac_status
= AC_STATUS_FOUND
;
3310 /* we don't correct pa_pstart or pa_plen here to avoid
3311 * possible race when the group is being loaded concurrently
3312 * instead we correct pa later, after blocks are marked
3313 * in on-disk bitmap -- see ext4_mb_release_context()
3314 * Other CPUs are prevented from allocating from this pa by lg_mutex
3316 mb_debug("use %u/%u from group pa %p\n", pa
->pa_lstart
-len
, len
, pa
);
3320 * Return the prealloc space that have minimal distance
3321 * from the goal block. @cpa is the prealloc
3322 * space that is having currently known minimal distance
3323 * from the goal block.
3325 static struct ext4_prealloc_space
*
3326 ext4_mb_check_group_pa(ext4_fsblk_t goal_block
,
3327 struct ext4_prealloc_space
*pa
,
3328 struct ext4_prealloc_space
*cpa
)
3330 ext4_fsblk_t cur_distance
, new_distance
;
3333 atomic_inc(&pa
->pa_count
);
3336 cur_distance
= abs(goal_block
- cpa
->pa_pstart
);
3337 new_distance
= abs(goal_block
- pa
->pa_pstart
);
3339 if (cur_distance
< new_distance
)
3342 /* drop the previous reference */
3343 atomic_dec(&cpa
->pa_count
);
3344 atomic_inc(&pa
->pa_count
);
3349 * search goal blocks in preallocated space
3351 static noinline_for_stack
int
3352 ext4_mb_use_preallocated(struct ext4_allocation_context
*ac
)
3355 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3356 struct ext4_locality_group
*lg
;
3357 struct ext4_prealloc_space
*pa
, *cpa
= NULL
;
3358 ext4_fsblk_t goal_block
;
3360 /* only data can be preallocated */
3361 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3364 /* first, try per-file preallocation */
3366 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3368 /* all fields in this condition don't change,
3369 * so we can skip locking for them */
3370 if (ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
||
3371 ac
->ac_o_ex
.fe_logical
>= pa
->pa_lstart
+ pa
->pa_len
)
3374 /* found preallocated blocks, use them */
3375 spin_lock(&pa
->pa_lock
);
3376 if (pa
->pa_deleted
== 0 && pa
->pa_free
) {
3377 atomic_inc(&pa
->pa_count
);
3378 ext4_mb_use_inode_pa(ac
, pa
);
3379 spin_unlock(&pa
->pa_lock
);
3380 ac
->ac_criteria
= 10;
3384 spin_unlock(&pa
->pa_lock
);
3388 /* can we use group allocation? */
3389 if (!(ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
))
3392 /* inode may have no locality group for some reason */
3396 order
= fls(ac
->ac_o_ex
.fe_len
) - 1;
3397 if (order
> PREALLOC_TB_SIZE
- 1)
3398 /* The max size of hash table is PREALLOC_TB_SIZE */
3399 order
= PREALLOC_TB_SIZE
- 1;
3401 goal_block
= ac
->ac_g_ex
.fe_group
* EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
) +
3402 ac
->ac_g_ex
.fe_start
+
3403 le32_to_cpu(EXT4_SB(ac
->ac_sb
)->s_es
->s_first_data_block
);
3405 * search for the prealloc space that is having
3406 * minimal distance from the goal block.
3408 for (i
= order
; i
< PREALLOC_TB_SIZE
; i
++) {
3410 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[i
],
3412 spin_lock(&pa
->pa_lock
);
3413 if (pa
->pa_deleted
== 0 &&
3414 pa
->pa_free
>= ac
->ac_o_ex
.fe_len
) {
3416 cpa
= ext4_mb_check_group_pa(goal_block
,
3419 spin_unlock(&pa
->pa_lock
);
3424 ext4_mb_use_group_pa(ac
, cpa
);
3425 ac
->ac_criteria
= 20;
3432 * the function goes through all block freed in the group
3433 * but not yet committed and marks them used in in-core bitmap.
3434 * buddy must be generated from this bitmap
3435 * Need to be called with the ext4 group lock held
3437 static void ext4_mb_generate_from_freelist(struct super_block
*sb
, void *bitmap
,
3441 struct ext4_group_info
*grp
;
3442 struct ext4_free_data
*entry
;
3444 grp
= ext4_get_group_info(sb
, group
);
3445 n
= rb_first(&(grp
->bb_free_root
));
3448 entry
= rb_entry(n
, struct ext4_free_data
, node
);
3449 mb_set_bits(bitmap
, entry
->start_blk
, entry
->count
);
3456 * the function goes through all preallocation in this group and marks them
3457 * used in in-core bitmap. buddy must be generated from this bitmap
3458 * Need to be called with ext4 group lock held
3460 static void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
3463 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3464 struct ext4_prealloc_space
*pa
;
3465 struct list_head
*cur
;
3466 ext4_group_t groupnr
;
3467 ext4_grpblk_t start
;
3468 int preallocated
= 0;
3472 /* all form of preallocation discards first load group,
3473 * so the only competing code is preallocation use.
3474 * we don't need any locking here
3475 * notice we do NOT ignore preallocations with pa_deleted
3476 * otherwise we could leave used blocks available for
3477 * allocation in buddy when concurrent ext4_mb_put_pa()
3478 * is dropping preallocation
3480 list_for_each(cur
, &grp
->bb_prealloc_list
) {
3481 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
3482 spin_lock(&pa
->pa_lock
);
3483 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
3486 spin_unlock(&pa
->pa_lock
);
3487 if (unlikely(len
== 0))
3489 BUG_ON(groupnr
!= group
);
3490 mb_set_bits(bitmap
, start
, len
);
3491 preallocated
+= len
;
3494 mb_debug("prellocated %u for group %u\n", preallocated
, group
);
3497 static void ext4_mb_pa_callback(struct rcu_head
*head
)
3499 struct ext4_prealloc_space
*pa
;
3500 pa
= container_of(head
, struct ext4_prealloc_space
, u
.pa_rcu
);
3501 kmem_cache_free(ext4_pspace_cachep
, pa
);
3505 * drops a reference to preallocated space descriptor
3506 * if this was the last reference and the space is consumed
3508 static void ext4_mb_put_pa(struct ext4_allocation_context
*ac
,
3509 struct super_block
*sb
, struct ext4_prealloc_space
*pa
)
3512 ext4_fsblk_t grp_blk
;
3514 if (!atomic_dec_and_test(&pa
->pa_count
) || pa
->pa_free
!= 0)
3517 /* in this short window concurrent discard can set pa_deleted */
3518 spin_lock(&pa
->pa_lock
);
3519 if (pa
->pa_deleted
== 1) {
3520 spin_unlock(&pa
->pa_lock
);
3525 spin_unlock(&pa
->pa_lock
);
3527 grp_blk
= pa
->pa_pstart
;
3529 * If doing group-based preallocation, pa_pstart may be in the
3530 * next group when pa is used up
3532 if (pa
->pa_type
== MB_GROUP_PA
)
3535 ext4_get_group_no_and_offset(sb
, grp_blk
, &grp
, NULL
);
3540 * P1 (buddy init) P2 (regular allocation)
3541 * find block B in PA
3542 * copy on-disk bitmap to buddy
3543 * mark B in on-disk bitmap
3544 * drop PA from group
3545 * mark all PAs in buddy
3547 * thus, P1 initializes buddy with B available. to prevent this
3548 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3551 ext4_lock_group(sb
, grp
);
3552 list_del(&pa
->pa_group_list
);
3553 ext4_unlock_group(sb
, grp
);
3555 spin_lock(pa
->pa_obj_lock
);
3556 list_del_rcu(&pa
->pa_inode_list
);
3557 spin_unlock(pa
->pa_obj_lock
);
3559 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3563 * creates new preallocated space for given inode
3565 static noinline_for_stack
int
3566 ext4_mb_new_inode_pa(struct ext4_allocation_context
*ac
)
3568 struct super_block
*sb
= ac
->ac_sb
;
3569 struct ext4_prealloc_space
*pa
;
3570 struct ext4_group_info
*grp
;
3571 struct ext4_inode_info
*ei
;
3573 /* preallocate only when found space is larger then requested */
3574 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3575 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3576 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3578 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3582 if (ac
->ac_b_ex
.fe_len
< ac
->ac_g_ex
.fe_len
) {
3588 /* we can't allocate as much as normalizer wants.
3589 * so, found space must get proper lstart
3590 * to cover original request */
3591 BUG_ON(ac
->ac_g_ex
.fe_logical
> ac
->ac_o_ex
.fe_logical
);
3592 BUG_ON(ac
->ac_g_ex
.fe_len
< ac
->ac_o_ex
.fe_len
);
3594 /* we're limited by original request in that
3595 * logical block must be covered any way
3596 * winl is window we can move our chunk within */
3597 winl
= ac
->ac_o_ex
.fe_logical
- ac
->ac_g_ex
.fe_logical
;
3599 /* also, we should cover whole original request */
3600 wins
= ac
->ac_b_ex
.fe_len
- ac
->ac_o_ex
.fe_len
;
3602 /* the smallest one defines real window */
3603 win
= min(winl
, wins
);
3605 offs
= ac
->ac_o_ex
.fe_logical
% ac
->ac_b_ex
.fe_len
;
3606 if (offs
&& offs
< win
)
3609 ac
->ac_b_ex
.fe_logical
= ac
->ac_o_ex
.fe_logical
- win
;
3610 BUG_ON(ac
->ac_o_ex
.fe_logical
< ac
->ac_b_ex
.fe_logical
);
3611 BUG_ON(ac
->ac_o_ex
.fe_len
> ac
->ac_b_ex
.fe_len
);
3614 /* preallocation can change ac_b_ex, thus we store actually
3615 * allocated blocks for history */
3616 ac
->ac_f_ex
= ac
->ac_b_ex
;
3618 pa
->pa_lstart
= ac
->ac_b_ex
.fe_logical
;
3619 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3620 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3621 pa
->pa_free
= pa
->pa_len
;
3622 atomic_set(&pa
->pa_count
, 1);
3623 spin_lock_init(&pa
->pa_lock
);
3624 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3625 INIT_LIST_HEAD(&pa
->pa_group_list
);
3627 pa
->pa_type
= MB_INODE_PA
;
3629 mb_debug("new inode pa %p: %llu/%u for %u\n", pa
,
3630 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3631 trace_ext4_mb_new_inode_pa(ac
, pa
);
3633 ext4_mb_use_inode_pa(ac
, pa
);
3634 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3636 ei
= EXT4_I(ac
->ac_inode
);
3637 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3639 pa
->pa_obj_lock
= &ei
->i_prealloc_lock
;
3640 pa
->pa_inode
= ac
->ac_inode
;
3642 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3643 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3644 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3646 spin_lock(pa
->pa_obj_lock
);
3647 list_add_rcu(&pa
->pa_inode_list
, &ei
->i_prealloc_list
);
3648 spin_unlock(pa
->pa_obj_lock
);
3654 * creates new preallocated space for locality group inodes belongs to
3656 static noinline_for_stack
int
3657 ext4_mb_new_group_pa(struct ext4_allocation_context
*ac
)
3659 struct super_block
*sb
= ac
->ac_sb
;
3660 struct ext4_locality_group
*lg
;
3661 struct ext4_prealloc_space
*pa
;
3662 struct ext4_group_info
*grp
;
3664 /* preallocate only when found space is larger then requested */
3665 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3666 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3667 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3669 BUG_ON(ext4_pspace_cachep
== NULL
);
3670 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3674 /* preallocation can change ac_b_ex, thus we store actually
3675 * allocated blocks for history */
3676 ac
->ac_f_ex
= ac
->ac_b_ex
;
3678 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3679 pa
->pa_lstart
= pa
->pa_pstart
;
3680 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3681 pa
->pa_free
= pa
->pa_len
;
3682 atomic_set(&pa
->pa_count
, 1);
3683 spin_lock_init(&pa
->pa_lock
);
3684 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3685 INIT_LIST_HEAD(&pa
->pa_group_list
);
3687 pa
->pa_type
= MB_GROUP_PA
;
3689 mb_debug("new group pa %p: %llu/%u for %u\n", pa
,
3690 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3691 trace_ext4_mb_new_group_pa(ac
, pa
);
3693 ext4_mb_use_group_pa(ac
, pa
);
3694 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3696 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3700 pa
->pa_obj_lock
= &lg
->lg_prealloc_lock
;
3701 pa
->pa_inode
= NULL
;
3703 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3704 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3705 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3708 * We will later add the new pa to the right bucket
3709 * after updating the pa_free in ext4_mb_release_context
3714 static int ext4_mb_new_preallocation(struct ext4_allocation_context
*ac
)
3718 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
3719 err
= ext4_mb_new_group_pa(ac
);
3721 err
= ext4_mb_new_inode_pa(ac
);
3726 * finds all unused blocks in on-disk bitmap, frees them in
3727 * in-core bitmap and buddy.
3728 * @pa must be unlinked from inode and group lists, so that
3729 * nobody else can find/use it.
3730 * the caller MUST hold group/inode locks.
3731 * TODO: optimize the case when there are no in-core structures yet
3733 static noinline_for_stack
int
3734 ext4_mb_release_inode_pa(struct ext4_buddy
*e4b
, struct buffer_head
*bitmap_bh
,
3735 struct ext4_prealloc_space
*pa
,
3736 struct ext4_allocation_context
*ac
)
3738 struct super_block
*sb
= e4b
->bd_sb
;
3739 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3744 unsigned long long grp_blk_start
;
3749 BUG_ON(pa
->pa_deleted
== 0);
3750 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3751 grp_blk_start
= pa
->pa_pstart
- bit
;
3752 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3753 end
= bit
+ pa
->pa_len
;
3757 ac
->ac_inode
= pa
->pa_inode
;
3758 ac
->ac_op
= EXT4_MB_HISTORY_DISCARD
;
3762 bit
= mb_find_next_zero_bit(bitmap_bh
->b_data
, end
, bit
);
3765 next
= mb_find_next_bit(bitmap_bh
->b_data
, end
, bit
);
3766 start
= group
* EXT4_BLOCKS_PER_GROUP(sb
) + bit
+
3767 le32_to_cpu(sbi
->s_es
->s_first_data_block
);
3768 mb_debug(" free preallocated %u/%u in group %u\n",
3769 (unsigned) start
, (unsigned) next
- bit
,
3774 ac
->ac_b_ex
.fe_group
= group
;
3775 ac
->ac_b_ex
.fe_start
= bit
;
3776 ac
->ac_b_ex
.fe_len
= next
- bit
;
3777 ac
->ac_b_ex
.fe_logical
= 0;
3778 ext4_mb_store_history(ac
);
3781 trace_ext4_mb_release_inode_pa(ac
, pa
, grp_blk_start
+ bit
,
3783 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, next
- bit
);
3786 if (free
!= pa
->pa_free
) {
3787 printk(KERN_CRIT
"pa %p: logic %lu, phys. %lu, len %lu\n",
3788 pa
, (unsigned long) pa
->pa_lstart
,
3789 (unsigned long) pa
->pa_pstart
,
3790 (unsigned long) pa
->pa_len
);
3791 ext4_grp_locked_error(sb
, group
,
3792 __func__
, "free %u, pa_free %u",
3795 * pa is already deleted so we use the value obtained
3796 * from the bitmap and continue.
3799 atomic_add(free
, &sbi
->s_mb_discarded
);
3804 static noinline_for_stack
int
3805 ext4_mb_release_group_pa(struct ext4_buddy
*e4b
,
3806 struct ext4_prealloc_space
*pa
,
3807 struct ext4_allocation_context
*ac
)
3809 struct super_block
*sb
= e4b
->bd_sb
;
3814 ac
->ac_op
= EXT4_MB_HISTORY_DISCARD
;
3816 trace_ext4_mb_release_group_pa(ac
, pa
);
3817 BUG_ON(pa
->pa_deleted
== 0);
3818 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3819 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3820 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, pa
->pa_len
);
3821 atomic_add(pa
->pa_len
, &EXT4_SB(sb
)->s_mb_discarded
);
3825 ac
->ac_inode
= NULL
;
3826 ac
->ac_b_ex
.fe_group
= group
;
3827 ac
->ac_b_ex
.fe_start
= bit
;
3828 ac
->ac_b_ex
.fe_len
= pa
->pa_len
;
3829 ac
->ac_b_ex
.fe_logical
= 0;
3830 ext4_mb_store_history(ac
);
3837 * releases all preallocations in given group
3839 * first, we need to decide discard policy:
3840 * - when do we discard
3842 * - how many do we discard
3843 * 1) how many requested
3845 static noinline_for_stack
int
3846 ext4_mb_discard_group_preallocations(struct super_block
*sb
,
3847 ext4_group_t group
, int needed
)
3849 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3850 struct buffer_head
*bitmap_bh
= NULL
;
3851 struct ext4_prealloc_space
*pa
, *tmp
;
3852 struct ext4_allocation_context
*ac
;
3853 struct list_head list
;
3854 struct ext4_buddy e4b
;
3859 mb_debug("discard preallocation for group %u\n", group
);
3861 if (list_empty(&grp
->bb_prealloc_list
))
3864 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
3865 if (bitmap_bh
== NULL
) {
3866 ext4_error(sb
, __func__
, "Error in reading block "
3867 "bitmap for %u", group
);
3871 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
3873 ext4_error(sb
, __func__
, "Error in loading buddy "
3874 "information for %u", group
);
3880 needed
= EXT4_BLOCKS_PER_GROUP(sb
) + 1;
3882 INIT_LIST_HEAD(&list
);
3883 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
3887 ext4_lock_group(sb
, group
);
3888 list_for_each_entry_safe(pa
, tmp
,
3889 &grp
->bb_prealloc_list
, pa_group_list
) {
3890 spin_lock(&pa
->pa_lock
);
3891 if (atomic_read(&pa
->pa_count
)) {
3892 spin_unlock(&pa
->pa_lock
);
3896 if (pa
->pa_deleted
) {
3897 spin_unlock(&pa
->pa_lock
);
3901 /* seems this one can be freed ... */
3904 /* we can trust pa_free ... */
3905 free
+= pa
->pa_free
;
3907 spin_unlock(&pa
->pa_lock
);
3909 list_del(&pa
->pa_group_list
);
3910 list_add(&pa
->u
.pa_tmp_list
, &list
);
3913 /* if we still need more blocks and some PAs were used, try again */
3914 if (free
< needed
&& busy
) {
3916 ext4_unlock_group(sb
, group
);
3918 * Yield the CPU here so that we don't get soft lockup
3919 * in non preempt case.
3925 /* found anything to free? */
3926 if (list_empty(&list
)) {
3931 /* now free all selected PAs */
3932 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
3934 /* remove from object (inode or locality group) */
3935 spin_lock(pa
->pa_obj_lock
);
3936 list_del_rcu(&pa
->pa_inode_list
);
3937 spin_unlock(pa
->pa_obj_lock
);
3939 if (pa
->pa_type
== MB_GROUP_PA
)
3940 ext4_mb_release_group_pa(&e4b
, pa
, ac
);
3942 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
, ac
);
3944 list_del(&pa
->u
.pa_tmp_list
);
3945 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3949 ext4_unlock_group(sb
, group
);
3951 kmem_cache_free(ext4_ac_cachep
, ac
);
3952 ext4_mb_release_desc(&e4b
);
3958 * releases all non-used preallocated blocks for given inode
3960 * It's important to discard preallocations under i_data_sem
3961 * We don't want another block to be served from the prealloc
3962 * space when we are discarding the inode prealloc space.
3964 * FIXME!! Make sure it is valid at all the call sites
3966 void ext4_discard_preallocations(struct inode
*inode
)
3968 struct ext4_inode_info
*ei
= EXT4_I(inode
);
3969 struct super_block
*sb
= inode
->i_sb
;
3970 struct buffer_head
*bitmap_bh
= NULL
;
3971 struct ext4_prealloc_space
*pa
, *tmp
;
3972 struct ext4_allocation_context
*ac
;
3973 ext4_group_t group
= 0;
3974 struct list_head list
;
3975 struct ext4_buddy e4b
;
3978 if (!S_ISREG(inode
->i_mode
)) {
3979 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3983 mb_debug("discard preallocation for inode %lu\n", inode
->i_ino
);
3984 trace_ext4_discard_preallocations(inode
);
3986 INIT_LIST_HEAD(&list
);
3988 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
3991 ac
->ac_inode
= inode
;
3994 /* first, collect all pa's in the inode */
3995 spin_lock(&ei
->i_prealloc_lock
);
3996 while (!list_empty(&ei
->i_prealloc_list
)) {
3997 pa
= list_entry(ei
->i_prealloc_list
.next
,
3998 struct ext4_prealloc_space
, pa_inode_list
);
3999 BUG_ON(pa
->pa_obj_lock
!= &ei
->i_prealloc_lock
);
4000 spin_lock(&pa
->pa_lock
);
4001 if (atomic_read(&pa
->pa_count
)) {
4002 /* this shouldn't happen often - nobody should
4003 * use preallocation while we're discarding it */
4004 spin_unlock(&pa
->pa_lock
);
4005 spin_unlock(&ei
->i_prealloc_lock
);
4006 printk(KERN_ERR
"uh-oh! used pa while discarding\n");
4008 schedule_timeout_uninterruptible(HZ
);
4012 if (pa
->pa_deleted
== 0) {
4014 spin_unlock(&pa
->pa_lock
);
4015 list_del_rcu(&pa
->pa_inode_list
);
4016 list_add(&pa
->u
.pa_tmp_list
, &list
);
4020 /* someone is deleting pa right now */
4021 spin_unlock(&pa
->pa_lock
);
4022 spin_unlock(&ei
->i_prealloc_lock
);
4024 /* we have to wait here because pa_deleted
4025 * doesn't mean pa is already unlinked from
4026 * the list. as we might be called from
4027 * ->clear_inode() the inode will get freed
4028 * and concurrent thread which is unlinking
4029 * pa from inode's list may access already
4030 * freed memory, bad-bad-bad */
4032 /* XXX: if this happens too often, we can
4033 * add a flag to force wait only in case
4034 * of ->clear_inode(), but not in case of
4035 * regular truncate */
4036 schedule_timeout_uninterruptible(HZ
);
4039 spin_unlock(&ei
->i_prealloc_lock
);
4041 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
4042 BUG_ON(pa
->pa_type
!= MB_INODE_PA
);
4043 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
4045 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
4047 ext4_error(sb
, __func__
, "Error in loading buddy "
4048 "information for %u", group
);
4052 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
4053 if (bitmap_bh
== NULL
) {
4054 ext4_error(sb
, __func__
, "Error in reading block "
4055 "bitmap for %u", group
);
4056 ext4_mb_release_desc(&e4b
);
4060 ext4_lock_group(sb
, group
);
4061 list_del(&pa
->pa_group_list
);
4062 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
, ac
);
4063 ext4_unlock_group(sb
, group
);
4065 ext4_mb_release_desc(&e4b
);
4068 list_del(&pa
->u
.pa_tmp_list
);
4069 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4072 kmem_cache_free(ext4_ac_cachep
, ac
);
4076 * finds all preallocated spaces and return blocks being freed to them
4077 * if preallocated space becomes full (no block is used from the space)
4078 * then the function frees space in buddy
4079 * XXX: at the moment, truncate (which is the only way to free blocks)
4080 * discards all preallocations
4082 static void ext4_mb_return_to_preallocation(struct inode
*inode
,
4083 struct ext4_buddy
*e4b
,
4084 sector_t block
, int count
)
4086 BUG_ON(!list_empty(&EXT4_I(inode
)->i_prealloc_list
));
4089 static void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
4091 struct super_block
*sb
= ac
->ac_sb
;
4092 ext4_group_t ngroups
, i
;
4094 printk(KERN_ERR
"EXT4-fs: Can't allocate:"
4095 " Allocation context details:\n");
4096 printk(KERN_ERR
"EXT4-fs: status %d flags %d\n",
4097 ac
->ac_status
, ac
->ac_flags
);
4098 printk(KERN_ERR
"EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
4099 "best %lu/%lu/%lu@%lu cr %d\n",
4100 (unsigned long)ac
->ac_o_ex
.fe_group
,
4101 (unsigned long)ac
->ac_o_ex
.fe_start
,
4102 (unsigned long)ac
->ac_o_ex
.fe_len
,
4103 (unsigned long)ac
->ac_o_ex
.fe_logical
,
4104 (unsigned long)ac
->ac_g_ex
.fe_group
,
4105 (unsigned long)ac
->ac_g_ex
.fe_start
,
4106 (unsigned long)ac
->ac_g_ex
.fe_len
,
4107 (unsigned long)ac
->ac_g_ex
.fe_logical
,
4108 (unsigned long)ac
->ac_b_ex
.fe_group
,
4109 (unsigned long)ac
->ac_b_ex
.fe_start
,
4110 (unsigned long)ac
->ac_b_ex
.fe_len
,
4111 (unsigned long)ac
->ac_b_ex
.fe_logical
,
4112 (int)ac
->ac_criteria
);
4113 printk(KERN_ERR
"EXT4-fs: %lu scanned, %d found\n", ac
->ac_ex_scanned
,
4115 printk(KERN_ERR
"EXT4-fs: groups: \n");
4116 ngroups
= ext4_get_groups_count(sb
);
4117 for (i
= 0; i
< ngroups
; i
++) {
4118 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, i
);
4119 struct ext4_prealloc_space
*pa
;
4120 ext4_grpblk_t start
;
4121 struct list_head
*cur
;
4122 ext4_lock_group(sb
, i
);
4123 list_for_each(cur
, &grp
->bb_prealloc_list
) {
4124 pa
= list_entry(cur
, struct ext4_prealloc_space
,
4126 spin_lock(&pa
->pa_lock
);
4127 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
4129 spin_unlock(&pa
->pa_lock
);
4130 printk(KERN_ERR
"PA:%lu:%d:%u \n", i
,
4133 ext4_unlock_group(sb
, i
);
4135 if (grp
->bb_free
== 0)
4137 printk(KERN_ERR
"%lu: %d/%d \n",
4138 i
, grp
->bb_free
, grp
->bb_fragments
);
4140 printk(KERN_ERR
"\n");
4143 static inline void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
4150 * We use locality group preallocation for small size file. The size of the
4151 * file is determined by the current size or the resulting size after
4152 * allocation which ever is larger
4154 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
4156 static void ext4_mb_group_or_file(struct ext4_allocation_context
*ac
)
4158 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
4159 int bsbits
= ac
->ac_sb
->s_blocksize_bits
;
4162 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
4165 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
4166 isize
= i_size_read(ac
->ac_inode
) >> bsbits
;
4167 size
= max(size
, isize
);
4169 /* don't use group allocation for large files */
4170 if (size
>= sbi
->s_mb_stream_request
)
4173 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
4176 BUG_ON(ac
->ac_lg
!= NULL
);
4178 * locality group prealloc space are per cpu. The reason for having
4179 * per cpu locality group is to reduce the contention between block
4180 * request from multiple CPUs.
4182 ac
->ac_lg
= per_cpu_ptr(sbi
->s_locality_groups
, raw_smp_processor_id());
4184 /* we're going to use group allocation */
4185 ac
->ac_flags
|= EXT4_MB_HINT_GROUP_ALLOC
;
4187 /* serialize all allocations in the group */
4188 mutex_lock(&ac
->ac_lg
->lg_mutex
);
4191 static noinline_for_stack
int
4192 ext4_mb_initialize_context(struct ext4_allocation_context
*ac
,
4193 struct ext4_allocation_request
*ar
)
4195 struct super_block
*sb
= ar
->inode
->i_sb
;
4196 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4197 struct ext4_super_block
*es
= sbi
->s_es
;
4201 ext4_grpblk_t block
;
4203 /* we can't allocate > group size */
4206 /* just a dirty hack to filter too big requests */
4207 if (len
>= EXT4_BLOCKS_PER_GROUP(sb
) - 10)
4208 len
= EXT4_BLOCKS_PER_GROUP(sb
) - 10;
4210 /* start searching from the goal */
4212 if (goal
< le32_to_cpu(es
->s_first_data_block
) ||
4213 goal
>= ext4_blocks_count(es
))
4214 goal
= le32_to_cpu(es
->s_first_data_block
);
4215 ext4_get_group_no_and_offset(sb
, goal
, &group
, &block
);
4217 /* set up allocation goals */
4218 ac
->ac_b_ex
.fe_logical
= ar
->logical
;
4219 ac
->ac_b_ex
.fe_group
= 0;
4220 ac
->ac_b_ex
.fe_start
= 0;
4221 ac
->ac_b_ex
.fe_len
= 0;
4222 ac
->ac_status
= AC_STATUS_CONTINUE
;
4223 ac
->ac_groups_scanned
= 0;
4224 ac
->ac_ex_scanned
= 0;
4227 ac
->ac_inode
= ar
->inode
;
4228 ac
->ac_o_ex
.fe_logical
= ar
->logical
;
4229 ac
->ac_o_ex
.fe_group
= group
;
4230 ac
->ac_o_ex
.fe_start
= block
;
4231 ac
->ac_o_ex
.fe_len
= len
;
4232 ac
->ac_g_ex
.fe_logical
= ar
->logical
;
4233 ac
->ac_g_ex
.fe_group
= group
;
4234 ac
->ac_g_ex
.fe_start
= block
;
4235 ac
->ac_g_ex
.fe_len
= len
;
4236 ac
->ac_f_ex
.fe_len
= 0;
4237 ac
->ac_flags
= ar
->flags
;
4239 ac
->ac_criteria
= 0;
4241 ac
->ac_bitmap_page
= NULL
;
4242 ac
->ac_buddy_page
= NULL
;
4243 ac
->alloc_semp
= NULL
;
4246 /* we have to define context: we'll we work with a file or
4247 * locality group. this is a policy, actually */
4248 ext4_mb_group_or_file(ac
);
4250 mb_debug("init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
4251 "left: %u/%u, right %u/%u to %swritable\n",
4252 (unsigned) ar
->len
, (unsigned) ar
->logical
,
4253 (unsigned) ar
->goal
, ac
->ac_flags
, ac
->ac_2order
,
4254 (unsigned) ar
->lleft
, (unsigned) ar
->pleft
,
4255 (unsigned) ar
->lright
, (unsigned) ar
->pright
,
4256 atomic_read(&ar
->inode
->i_writecount
) ? "" : "non-");
4261 static noinline_for_stack
void
4262 ext4_mb_discard_lg_preallocations(struct super_block
*sb
,
4263 struct ext4_locality_group
*lg
,
4264 int order
, int total_entries
)
4266 ext4_group_t group
= 0;
4267 struct ext4_buddy e4b
;
4268 struct list_head discard_list
;
4269 struct ext4_prealloc_space
*pa
, *tmp
;
4270 struct ext4_allocation_context
*ac
;
4272 mb_debug("discard locality group preallocation\n");
4274 INIT_LIST_HEAD(&discard_list
);
4275 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4279 spin_lock(&lg
->lg_prealloc_lock
);
4280 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[order
],
4282 spin_lock(&pa
->pa_lock
);
4283 if (atomic_read(&pa
->pa_count
)) {
4285 * This is the pa that we just used
4286 * for block allocation. So don't
4289 spin_unlock(&pa
->pa_lock
);
4292 if (pa
->pa_deleted
) {
4293 spin_unlock(&pa
->pa_lock
);
4296 /* only lg prealloc space */
4297 BUG_ON(pa
->pa_type
!= MB_GROUP_PA
);
4299 /* seems this one can be freed ... */
4301 spin_unlock(&pa
->pa_lock
);
4303 list_del_rcu(&pa
->pa_inode_list
);
4304 list_add(&pa
->u
.pa_tmp_list
, &discard_list
);
4307 if (total_entries
<= 5) {
4309 * we want to keep only 5 entries
4310 * allowing it to grow to 8. This
4311 * mak sure we don't call discard
4312 * soon for this list.
4317 spin_unlock(&lg
->lg_prealloc_lock
);
4319 list_for_each_entry_safe(pa
, tmp
, &discard_list
, u
.pa_tmp_list
) {
4321 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
4322 if (ext4_mb_load_buddy(sb
, group
, &e4b
)) {
4323 ext4_error(sb
, __func__
, "Error in loading buddy "
4324 "information for %u", group
);
4327 ext4_lock_group(sb
, group
);
4328 list_del(&pa
->pa_group_list
);
4329 ext4_mb_release_group_pa(&e4b
, pa
, ac
);
4330 ext4_unlock_group(sb
, group
);
4332 ext4_mb_release_desc(&e4b
);
4333 list_del(&pa
->u
.pa_tmp_list
);
4334 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4337 kmem_cache_free(ext4_ac_cachep
, ac
);
4341 * We have incremented pa_count. So it cannot be freed at this
4342 * point. Also we hold lg_mutex. So no parallel allocation is
4343 * possible from this lg. That means pa_free cannot be updated.
4345 * A parallel ext4_mb_discard_group_preallocations is possible.
4346 * which can cause the lg_prealloc_list to be updated.
4349 static void ext4_mb_add_n_trim(struct ext4_allocation_context
*ac
)
4351 int order
, added
= 0, lg_prealloc_count
= 1;
4352 struct super_block
*sb
= ac
->ac_sb
;
4353 struct ext4_locality_group
*lg
= ac
->ac_lg
;
4354 struct ext4_prealloc_space
*tmp_pa
, *pa
= ac
->ac_pa
;
4356 order
= fls(pa
->pa_free
) - 1;
4357 if (order
> PREALLOC_TB_SIZE
- 1)
4358 /* The max size of hash table is PREALLOC_TB_SIZE */
4359 order
= PREALLOC_TB_SIZE
- 1;
4360 /* Add the prealloc space to lg */
4362 list_for_each_entry_rcu(tmp_pa
, &lg
->lg_prealloc_list
[order
],
4364 spin_lock(&tmp_pa
->pa_lock
);
4365 if (tmp_pa
->pa_deleted
) {
4366 spin_unlock(&tmp_pa
->pa_lock
);
4369 if (!added
&& pa
->pa_free
< tmp_pa
->pa_free
) {
4370 /* Add to the tail of the previous entry */
4371 list_add_tail_rcu(&pa
->pa_inode_list
,
4372 &tmp_pa
->pa_inode_list
);
4375 * we want to count the total
4376 * number of entries in the list
4379 spin_unlock(&tmp_pa
->pa_lock
);
4380 lg_prealloc_count
++;
4383 list_add_tail_rcu(&pa
->pa_inode_list
,
4384 &lg
->lg_prealloc_list
[order
]);
4387 /* Now trim the list to be not more than 8 elements */
4388 if (lg_prealloc_count
> 8) {
4389 ext4_mb_discard_lg_preallocations(sb
, lg
,
4390 order
, lg_prealloc_count
);
4397 * release all resource we used in allocation
4399 static int ext4_mb_release_context(struct ext4_allocation_context
*ac
)
4401 struct ext4_prealloc_space
*pa
= ac
->ac_pa
;
4403 if (pa
->pa_type
== MB_GROUP_PA
) {
4404 /* see comment in ext4_mb_use_group_pa() */
4405 spin_lock(&pa
->pa_lock
);
4406 pa
->pa_pstart
+= ac
->ac_b_ex
.fe_len
;
4407 pa
->pa_lstart
+= ac
->ac_b_ex
.fe_len
;
4408 pa
->pa_free
-= ac
->ac_b_ex
.fe_len
;
4409 pa
->pa_len
-= ac
->ac_b_ex
.fe_len
;
4410 spin_unlock(&pa
->pa_lock
);
4414 up_read(ac
->alloc_semp
);
4417 * We want to add the pa to the right bucket.
4418 * Remove it from the list and while adding
4419 * make sure the list to which we are adding
4420 * doesn't grow big. We need to release
4421 * alloc_semp before calling ext4_mb_add_n_trim()
4423 if ((pa
->pa_type
== MB_GROUP_PA
) && likely(pa
->pa_free
)) {
4424 spin_lock(pa
->pa_obj_lock
);
4425 list_del_rcu(&pa
->pa_inode_list
);
4426 spin_unlock(pa
->pa_obj_lock
);
4427 ext4_mb_add_n_trim(ac
);
4429 ext4_mb_put_pa(ac
, ac
->ac_sb
, pa
);
4431 if (ac
->ac_bitmap_page
)
4432 page_cache_release(ac
->ac_bitmap_page
);
4433 if (ac
->ac_buddy_page
)
4434 page_cache_release(ac
->ac_buddy_page
);
4435 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
4436 mutex_unlock(&ac
->ac_lg
->lg_mutex
);
4437 ext4_mb_collect_stats(ac
);
4441 static int ext4_mb_discard_preallocations(struct super_block
*sb
, int needed
)
4443 ext4_group_t i
, ngroups
= ext4_get_groups_count(sb
);
4447 trace_ext4_mb_discard_preallocations(sb
, needed
);
4448 for (i
= 0; i
< ngroups
&& needed
> 0; i
++) {
4449 ret
= ext4_mb_discard_group_preallocations(sb
, i
, needed
);
4458 * Main entry point into mballoc to allocate blocks
4459 * it tries to use preallocation first, then falls back
4460 * to usual allocation
4462 ext4_fsblk_t
ext4_mb_new_blocks(handle_t
*handle
,
4463 struct ext4_allocation_request
*ar
, int *errp
)
4466 struct ext4_allocation_context
*ac
= NULL
;
4467 struct ext4_sb_info
*sbi
;
4468 struct super_block
*sb
;
4469 ext4_fsblk_t block
= 0;
4470 unsigned int inquota
= 0;
4471 unsigned int reserv_blks
= 0;
4473 sb
= ar
->inode
->i_sb
;
4476 trace_ext4_request_blocks(ar
);
4479 * For delayed allocation, we could skip the ENOSPC and
4480 * EDQUOT check, as blocks and quotas have been already
4481 * reserved when data being copied into pagecache.
4483 if (EXT4_I(ar
->inode
)->i_delalloc_reserved_flag
)
4484 ar
->flags
|= EXT4_MB_DELALLOC_RESERVED
;
4486 /* Without delayed allocation we need to verify
4487 * there is enough free blocks to do block allocation
4488 * and verify allocation doesn't exceed the quota limits.
4490 while (ar
->len
&& ext4_claim_free_blocks(sbi
, ar
->len
)) {
4491 /* let others to free the space */
4493 ar
->len
= ar
->len
>> 1;
4499 reserv_blks
= ar
->len
;
4500 while (ar
->len
&& vfs_dq_alloc_block(ar
->inode
, ar
->len
)) {
4501 ar
->flags
|= EXT4_MB_HINT_NOPREALLOC
;
4511 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4514 ac
->ac_inode
= ar
->inode
;
4521 *errp
= ext4_mb_initialize_context(ac
, ar
);
4527 ac
->ac_op
= EXT4_MB_HISTORY_PREALLOC
;
4528 if (!ext4_mb_use_preallocated(ac
)) {
4529 ac
->ac_op
= EXT4_MB_HISTORY_ALLOC
;
4530 ext4_mb_normalize_request(ac
, ar
);
4532 /* allocate space in core */
4533 ext4_mb_regular_allocator(ac
);
4535 /* as we've just preallocated more space than
4536 * user requested orinally, we store allocated
4537 * space in a special descriptor */
4538 if (ac
->ac_status
== AC_STATUS_FOUND
&&
4539 ac
->ac_o_ex
.fe_len
< ac
->ac_b_ex
.fe_len
)
4540 ext4_mb_new_preallocation(ac
);
4542 if (likely(ac
->ac_status
== AC_STATUS_FOUND
)) {
4543 *errp
= ext4_mb_mark_diskspace_used(ac
, handle
, reserv_blks
);
4544 if (*errp
== -EAGAIN
) {
4546 * drop the reference that we took
4547 * in ext4_mb_use_best_found
4549 ext4_mb_release_context(ac
);
4550 ac
->ac_b_ex
.fe_group
= 0;
4551 ac
->ac_b_ex
.fe_start
= 0;
4552 ac
->ac_b_ex
.fe_len
= 0;
4553 ac
->ac_status
= AC_STATUS_CONTINUE
;
4556 ac
->ac_b_ex
.fe_len
= 0;
4558 ext4_mb_show_ac(ac
);
4560 block
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
4561 ar
->len
= ac
->ac_b_ex
.fe_len
;
4564 freed
= ext4_mb_discard_preallocations(sb
, ac
->ac_o_ex
.fe_len
);
4568 ac
->ac_b_ex
.fe_len
= 0;
4570 ext4_mb_show_ac(ac
);
4573 ext4_mb_release_context(ac
);
4576 kmem_cache_free(ext4_ac_cachep
, ac
);
4578 if (inquota
&& ar
->len
< inquota
)
4579 vfs_dq_free_block(ar
->inode
, inquota
- ar
->len
);
4582 if (!EXT4_I(ar
->inode
)->i_delalloc_reserved_flag
)
4583 /* release all the reserved blocks if non delalloc */
4584 percpu_counter_sub(&sbi
->s_dirtyblocks_counter
,
4588 trace_ext4_allocate_blocks(ar
, (unsigned long long)block
);
4594 * We can merge two free data extents only if the physical blocks
4595 * are contiguous, AND the extents were freed by the same transaction,
4596 * AND the blocks are associated with the same group.
4598 static int can_merge(struct ext4_free_data
*entry1
,
4599 struct ext4_free_data
*entry2
)
4601 if ((entry1
->t_tid
== entry2
->t_tid
) &&
4602 (entry1
->group
== entry2
->group
) &&
4603 ((entry1
->start_blk
+ entry1
->count
) == entry2
->start_blk
))
4608 static noinline_for_stack
int
4609 ext4_mb_free_metadata(handle_t
*handle
, struct ext4_buddy
*e4b
,
4610 struct ext4_free_data
*new_entry
)
4612 ext4_grpblk_t block
;
4613 struct ext4_free_data
*entry
;
4614 struct ext4_group_info
*db
= e4b
->bd_info
;
4615 struct super_block
*sb
= e4b
->bd_sb
;
4616 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4617 struct rb_node
**n
= &db
->bb_free_root
.rb_node
, *node
;
4618 struct rb_node
*parent
= NULL
, *new_node
;
4620 BUG_ON(!ext4_handle_valid(handle
));
4621 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
4622 BUG_ON(e4b
->bd_buddy_page
== NULL
);
4624 new_node
= &new_entry
->node
;
4625 block
= new_entry
->start_blk
;
4628 /* first free block exent. We need to
4629 protect buddy cache from being freed,
4630 * otherwise we'll refresh it from
4631 * on-disk bitmap and lose not-yet-available
4633 page_cache_get(e4b
->bd_buddy_page
);
4634 page_cache_get(e4b
->bd_bitmap_page
);
4638 entry
= rb_entry(parent
, struct ext4_free_data
, node
);
4639 if (block
< entry
->start_blk
)
4641 else if (block
>= (entry
->start_blk
+ entry
->count
))
4642 n
= &(*n
)->rb_right
;
4644 ext4_grp_locked_error(sb
, e4b
->bd_group
, __func__
,
4645 "Double free of blocks %d (%d %d)",
4646 block
, entry
->start_blk
, entry
->count
);
4651 rb_link_node(new_node
, parent
, n
);
4652 rb_insert_color(new_node
, &db
->bb_free_root
);
4654 /* Now try to see the extent can be merged to left and right */
4655 node
= rb_prev(new_node
);
4657 entry
= rb_entry(node
, struct ext4_free_data
, node
);
4658 if (can_merge(entry
, new_entry
)) {
4659 new_entry
->start_blk
= entry
->start_blk
;
4660 new_entry
->count
+= entry
->count
;
4661 rb_erase(node
, &(db
->bb_free_root
));
4662 spin_lock(&sbi
->s_md_lock
);
4663 list_del(&entry
->list
);
4664 spin_unlock(&sbi
->s_md_lock
);
4665 kmem_cache_free(ext4_free_ext_cachep
, entry
);
4669 node
= rb_next(new_node
);
4671 entry
= rb_entry(node
, struct ext4_free_data
, node
);
4672 if (can_merge(new_entry
, entry
)) {
4673 new_entry
->count
+= entry
->count
;
4674 rb_erase(node
, &(db
->bb_free_root
));
4675 spin_lock(&sbi
->s_md_lock
);
4676 list_del(&entry
->list
);
4677 spin_unlock(&sbi
->s_md_lock
);
4678 kmem_cache_free(ext4_free_ext_cachep
, entry
);
4681 /* Add the extent to transaction's private list */
4682 spin_lock(&sbi
->s_md_lock
);
4683 list_add(&new_entry
->list
, &handle
->h_transaction
->t_private_list
);
4684 spin_unlock(&sbi
->s_md_lock
);
4689 * Main entry point into mballoc to free blocks
4691 void ext4_mb_free_blocks(handle_t
*handle
, struct inode
*inode
,
4692 ext4_fsblk_t block
, unsigned long count
,
4693 int metadata
, unsigned long *freed
)
4695 struct buffer_head
*bitmap_bh
= NULL
;
4696 struct super_block
*sb
= inode
->i_sb
;
4697 struct ext4_allocation_context
*ac
= NULL
;
4698 struct ext4_group_desc
*gdp
;
4699 struct ext4_super_block
*es
;
4700 unsigned int overflow
;
4702 struct buffer_head
*gd_bh
;
4703 ext4_group_t block_group
;
4704 struct ext4_sb_info
*sbi
;
4705 struct ext4_buddy e4b
;
4712 es
= EXT4_SB(sb
)->s_es
;
4713 if (block
< le32_to_cpu(es
->s_first_data_block
) ||
4714 block
+ count
< block
||
4715 block
+ count
> ext4_blocks_count(es
)) {
4716 ext4_error(sb
, __func__
,
4717 "Freeing blocks not in datazone - "
4718 "block = %llu, count = %lu", block
, count
);
4722 ext4_debug("freeing block %llu\n", block
);
4723 trace_ext4_free_blocks(inode
, block
, count
, metadata
);
4725 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4727 ac
->ac_op
= EXT4_MB_HISTORY_FREE
;
4728 ac
->ac_inode
= inode
;
4734 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
4737 * Check to see if we are freeing blocks across a group
4740 if (bit
+ count
> EXT4_BLOCKS_PER_GROUP(sb
)) {
4741 overflow
= bit
+ count
- EXT4_BLOCKS_PER_GROUP(sb
);
4744 bitmap_bh
= ext4_read_block_bitmap(sb
, block_group
);
4749 gdp
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
4755 if (in_range(ext4_block_bitmap(sb
, gdp
), block
, count
) ||
4756 in_range(ext4_inode_bitmap(sb
, gdp
), block
, count
) ||
4757 in_range(block
, ext4_inode_table(sb
, gdp
),
4758 EXT4_SB(sb
)->s_itb_per_group
) ||
4759 in_range(block
+ count
- 1, ext4_inode_table(sb
, gdp
),
4760 EXT4_SB(sb
)->s_itb_per_group
)) {
4762 ext4_error(sb
, __func__
,
4763 "Freeing blocks in system zone - "
4764 "Block = %llu, count = %lu", block
, count
);
4765 /* err = 0. ext4_std_error should be a no op */
4769 BUFFER_TRACE(bitmap_bh
, "getting write access");
4770 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
4775 * We are about to modify some metadata. Call the journal APIs
4776 * to unshare ->b_data if a currently-committing transaction is
4779 BUFFER_TRACE(gd_bh
, "get_write_access");
4780 err
= ext4_journal_get_write_access(handle
, gd_bh
);
4783 #ifdef AGGRESSIVE_CHECK
4786 for (i
= 0; i
< count
; i
++)
4787 BUG_ON(!mb_test_bit(bit
+ i
, bitmap_bh
->b_data
));
4791 ac
->ac_b_ex
.fe_group
= block_group
;
4792 ac
->ac_b_ex
.fe_start
= bit
;
4793 ac
->ac_b_ex
.fe_len
= count
;
4794 ext4_mb_store_history(ac
);
4797 err
= ext4_mb_load_buddy(sb
, block_group
, &e4b
);
4800 if (metadata
&& ext4_handle_valid(handle
)) {
4801 struct ext4_free_data
*new_entry
;
4803 * blocks being freed are metadata. these blocks shouldn't
4804 * be used until this transaction is committed
4806 new_entry
= kmem_cache_alloc(ext4_free_ext_cachep
, GFP_NOFS
);
4807 new_entry
->start_blk
= bit
;
4808 new_entry
->group
= block_group
;
4809 new_entry
->count
= count
;
4810 new_entry
->t_tid
= handle
->h_transaction
->t_tid
;
4812 ext4_lock_group(sb
, block_group
);
4813 mb_clear_bits(bitmap_bh
->b_data
, bit
, count
);
4814 ext4_mb_free_metadata(handle
, &e4b
, new_entry
);
4816 /* need to update group_info->bb_free and bitmap
4817 * with group lock held. generate_buddy look at
4818 * them with group lock_held
4820 ext4_lock_group(sb
, block_group
);
4821 mb_clear_bits(bitmap_bh
->b_data
, bit
, count
);
4822 mb_free_blocks(inode
, &e4b
, bit
, count
);
4823 ext4_mb_return_to_preallocation(inode
, &e4b
, block
, count
);
4826 ret
= ext4_free_blks_count(sb
, gdp
) + count
;
4827 ext4_free_blks_set(sb
, gdp
, ret
);
4828 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, block_group
, gdp
);
4829 ext4_unlock_group(sb
, block_group
);
4830 percpu_counter_add(&sbi
->s_freeblocks_counter
, count
);
4832 if (sbi
->s_log_groups_per_flex
) {
4833 ext4_group_t flex_group
= ext4_flex_group(sbi
, block_group
);
4834 atomic_add(count
, &sbi
->s_flex_groups
[flex_group
].free_blocks
);
4837 ext4_mb_release_desc(&e4b
);
4841 /* We dirtied the bitmap block */
4842 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
4843 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
4845 /* And the group descriptor block */
4846 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
4847 ret
= ext4_handle_dirty_metadata(handle
, NULL
, gd_bh
);
4851 if (overflow
&& !err
) {
4860 ext4_std_error(sb
, err
);
4862 kmem_cache_free(ext4_ac_cachep
, ac
);