2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public Licens
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
21 * mballoc.c contains the multiblocks allocation routines
25 #include <linux/debugfs.h>
26 #include <linux/slab.h>
27 #include <trace/events/ext4.h>
31 * - test ext4_ext_search_left() and ext4_ext_search_right()
32 * - search for metadata in few groups
35 * - normalization should take into account whether file is still open
36 * - discard preallocations if no free space left (policy?)
37 * - don't normalize tails
39 * - reservation for superuser
42 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
43 * - track min/max extents in each group for better group selection
44 * - mb_mark_used() may allocate chunk right after splitting buddy
45 * - tree of groups sorted by number of free blocks
50 * The allocation request involve request for multiple number of blocks
51 * near to the goal(block) value specified.
53 * During initialization phase of the allocator we decide to use the
54 * group preallocation or inode preallocation depending on the size of
55 * the file. The size of the file could be the resulting file size we
56 * would have after allocation, or the current file size, which ever
57 * is larger. If the size is less than sbi->s_mb_stream_request we
58 * select to use the group preallocation. The default value of
59 * s_mb_stream_request is 16 blocks. This can also be tuned via
60 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
61 * terms of number of blocks.
63 * The main motivation for having small file use group preallocation is to
64 * ensure that we have small files closer together on the disk.
66 * First stage the allocator looks at the inode prealloc list,
67 * ext4_inode_info->i_prealloc_list, which contains list of prealloc
68 * spaces for this particular inode. The inode prealloc space is
71 * pa_lstart -> the logical start block for this prealloc space
72 * pa_pstart -> the physical start block for this prealloc space
73 * pa_len -> length for this prealloc space
74 * pa_free -> free space available in this prealloc space
76 * The inode preallocation space is used looking at the _logical_ start
77 * block. If only the logical file block falls within the range of prealloc
78 * space we will consume the particular prealloc space. This make sure that
79 * that the we have contiguous physical blocks representing the file blocks
81 * The important thing to be noted in case of inode prealloc space is that
82 * we don't modify the values associated to inode prealloc space except
85 * If we are not able to find blocks in the inode prealloc space and if we
86 * have the group allocation flag set then we look at the locality group
87 * prealloc space. These are per CPU prealloc list repreasented as
89 * ext4_sb_info.s_locality_groups[smp_processor_id()]
91 * The reason for having a per cpu locality group is to reduce the contention
92 * between CPUs. It is possible to get scheduled at this point.
94 * The locality group prealloc space is used looking at whether we have
95 * enough free space (pa_free) withing the prealloc space.
97 * If we can't allocate blocks via inode prealloc or/and locality group
98 * prealloc then we look at the buddy cache. The buddy cache is represented
99 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
100 * mapped to the buddy and bitmap information regarding different
101 * groups. The buddy information is attached to buddy cache inode so that
102 * we can access them through the page cache. The information regarding
103 * each group is loaded via ext4_mb_load_buddy. The information involve
104 * block bitmap and buddy information. The information are stored in the
108 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
111 * one block each for bitmap and buddy information. So for each group we
112 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
113 * blocksize) blocks. So it can have information regarding groups_per_page
114 * which is blocks_per_page/2
116 * The buddy cache inode is not stored on disk. The inode is thrown
117 * away when the filesystem is unmounted.
119 * We look for count number of blocks in the buddy cache. If we were able
120 * to locate that many free blocks we return with additional information
121 * regarding rest of the contiguous physical block available
123 * Before allocating blocks via buddy cache we normalize the request
124 * blocks. This ensure we ask for more blocks that we needed. The extra
125 * blocks that we get after allocation is added to the respective prealloc
126 * list. In case of inode preallocation we follow a list of heuristics
127 * based on file size. This can be found in ext4_mb_normalize_request. If
128 * we are doing a group prealloc we try to normalize the request to
129 * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is
130 * 512 blocks. This can be tuned via
131 * /sys/fs/ext4/<partition/mb_group_prealloc. The value is represented in
132 * terms of number of blocks. If we have mounted the file system with -O
133 * stripe=<value> option the group prealloc request is normalized to the
134 * stripe value (sbi->s_stripe)
136 * The regular allocator(using the buddy cache) supports few tunables.
138 * /sys/fs/ext4/<partition>/mb_min_to_scan
139 * /sys/fs/ext4/<partition>/mb_max_to_scan
140 * /sys/fs/ext4/<partition>/mb_order2_req
142 * The regular allocator uses buddy scan only if the request len is power of
143 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
144 * value of s_mb_order2_reqs can be tuned via
145 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
146 * stripe size (sbi->s_stripe), we try to search for contiguous block in
147 * stripe size. This should result in better allocation on RAID setups. If
148 * not, we search in the specific group using bitmap for best extents. The
149 * tunable min_to_scan and max_to_scan control the behaviour here.
150 * min_to_scan indicate how long the mballoc __must__ look for a best
151 * extent and max_to_scan indicates how long the mballoc __can__ look for a
152 * best extent in the found extents. Searching for the blocks starts with
153 * the group specified as the goal value in allocation context via
154 * ac_g_ex. Each group is first checked based on the criteria whether it
155 * can used for allocation. ext4_mb_good_group explains how the groups are
158 * Both the prealloc space are getting populated as above. So for the first
159 * request we will hit the buddy cache which will result in this prealloc
160 * space getting filled. The prealloc space is then later used for the
161 * subsequent request.
165 * mballoc operates on the following data:
167 * - in-core buddy (actually includes buddy and bitmap)
168 * - preallocation descriptors (PAs)
170 * there are two types of preallocations:
172 * assiged to specific inode and can be used for this inode only.
173 * it describes part of inode's space preallocated to specific
174 * physical blocks. any block from that preallocated can be used
175 * independent. the descriptor just tracks number of blocks left
176 * unused. so, before taking some block from descriptor, one must
177 * make sure corresponded logical block isn't allocated yet. this
178 * also means that freeing any block within descriptor's range
179 * must discard all preallocated blocks.
181 * assigned to specific locality group which does not translate to
182 * permanent set of inodes: inode can join and leave group. space
183 * from this type of preallocation can be used for any inode. thus
184 * it's consumed from the beginning to the end.
186 * relation between them can be expressed as:
187 * in-core buddy = on-disk bitmap + preallocation descriptors
189 * this mean blocks mballoc considers used are:
190 * - allocated blocks (persistent)
191 * - preallocated blocks (non-persistent)
193 * consistency in mballoc world means that at any time a block is either
194 * free or used in ALL structures. notice: "any time" should not be read
195 * literally -- time is discrete and delimited by locks.
197 * to keep it simple, we don't use block numbers, instead we count number of
198 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
200 * all operations can be expressed as:
201 * - init buddy: buddy = on-disk + PAs
202 * - new PA: buddy += N; PA = N
203 * - use inode PA: on-disk += N; PA -= N
204 * - discard inode PA buddy -= on-disk - PA; PA = 0
205 * - use locality group PA on-disk += N; PA -= N
206 * - discard locality group PA buddy -= PA; PA = 0
207 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
208 * is used in real operation because we can't know actual used
209 * bits from PA, only from on-disk bitmap
211 * if we follow this strict logic, then all operations above should be atomic.
212 * given some of them can block, we'd have to use something like semaphores
213 * killing performance on high-end SMP hardware. let's try to relax it using
214 * the following knowledge:
215 * 1) if buddy is referenced, it's already initialized
216 * 2) while block is used in buddy and the buddy is referenced,
217 * nobody can re-allocate that block
218 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
219 * bit set and PA claims same block, it's OK. IOW, one can set bit in
220 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
223 * so, now we're building a concurrency table:
226 * blocks for PA are allocated in the buddy, buddy must be referenced
227 * until PA is linked to allocation group to avoid concurrent buddy init
229 * we need to make sure that either on-disk bitmap or PA has uptodate data
230 * given (3) we care that PA-=N operation doesn't interfere with init
232 * the simplest way would be to have buddy initialized by the discard
233 * - use locality group PA
234 * again PA-=N must be serialized with init
235 * - discard locality group PA
236 * the simplest way would be to have buddy initialized by the discard
239 * i_data_sem serializes them
241 * discard process must wait until PA isn't used by another process
242 * - use locality group PA
243 * some mutex should serialize them
244 * - discard locality group PA
245 * discard process must wait until PA isn't used by another process
248 * i_data_sem or another mutex should serializes them
250 * discard process must wait until PA isn't used by another process
251 * - use locality group PA
252 * nothing wrong here -- they're different PAs covering different blocks
253 * - discard locality group PA
254 * discard process must wait until PA isn't used by another process
256 * now we're ready to make few consequences:
257 * - PA is referenced and while it is no discard is possible
258 * - PA is referenced until block isn't marked in on-disk bitmap
259 * - PA changes only after on-disk bitmap
260 * - discard must not compete with init. either init is done before
261 * any discard or they're serialized somehow
262 * - buddy init as sum of on-disk bitmap and PAs is done atomically
264 * a special case when we've used PA to emptiness. no need to modify buddy
265 * in this case, but we should care about concurrent init
270 * Logic in few words:
275 * mark bits in on-disk bitmap
278 * - use preallocation:
279 * find proper PA (per-inode or group)
281 * mark bits in on-disk bitmap
287 * mark bits in on-disk bitmap
290 * - discard preallocations in group:
292 * move them onto local list
293 * load on-disk bitmap
295 * remove PA from object (inode or locality group)
296 * mark free blocks in-core
298 * - discard inode's preallocations:
305 * - bitlock on a group (group)
306 * - object (inode/locality) (object)
317 * - release consumed pa:
322 * - generate in-core bitmap:
326 * - discard all for given object (inode, locality group):
331 * - discard all for given group:
338 static struct kmem_cache
*ext4_pspace_cachep
;
339 static struct kmem_cache
*ext4_ac_cachep
;
340 static struct kmem_cache
*ext4_free_ext_cachep
;
342 /* We create slab caches for groupinfo data structures based on the
343 * superblock block size. There will be one per mounted filesystem for
344 * each unique s_blocksize_bits */
345 #define NR_GRPINFO_CACHES \
346 (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE + 1)
347 static struct kmem_cache
*ext4_groupinfo_caches
[NR_GRPINFO_CACHES
];
349 static void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
351 static void ext4_mb_generate_from_freelist(struct super_block
*sb
, void *bitmap
,
353 static void release_blocks_on_commit(journal_t
*journal
, transaction_t
*txn
);
355 static inline void *mb_correct_addr_and_bit(int *bit
, void *addr
)
357 #if BITS_PER_LONG == 64
358 *bit
+= ((unsigned long) addr
& 7UL) << 3;
359 addr
= (void *) ((unsigned long) addr
& ~7UL);
360 #elif BITS_PER_LONG == 32
361 *bit
+= ((unsigned long) addr
& 3UL) << 3;
362 addr
= (void *) ((unsigned long) addr
& ~3UL);
364 #error "how many bits you are?!"
369 static inline int mb_test_bit(int bit
, void *addr
)
372 * ext4_test_bit on architecture like powerpc
373 * needs unsigned long aligned address
375 addr
= mb_correct_addr_and_bit(&bit
, addr
);
376 return ext4_test_bit(bit
, addr
);
379 static inline void mb_set_bit(int bit
, void *addr
)
381 addr
= mb_correct_addr_and_bit(&bit
, addr
);
382 ext4_set_bit(bit
, addr
);
385 static inline void mb_clear_bit(int bit
, void *addr
)
387 addr
= mb_correct_addr_and_bit(&bit
, addr
);
388 ext4_clear_bit(bit
, addr
);
391 static inline int mb_find_next_zero_bit(void *addr
, int max
, int start
)
393 int fix
= 0, ret
, tmpmax
;
394 addr
= mb_correct_addr_and_bit(&fix
, addr
);
398 ret
= ext4_find_next_zero_bit(addr
, tmpmax
, start
) - fix
;
404 static inline int mb_find_next_bit(void *addr
, int max
, int start
)
406 int fix
= 0, ret
, tmpmax
;
407 addr
= mb_correct_addr_and_bit(&fix
, addr
);
411 ret
= ext4_find_next_bit(addr
, tmpmax
, start
) - fix
;
417 static void *mb_find_buddy(struct ext4_buddy
*e4b
, int order
, int *max
)
421 BUG_ON(EXT4_MB_BITMAP(e4b
) == EXT4_MB_BUDDY(e4b
));
424 if (order
> e4b
->bd_blkbits
+ 1) {
429 /* at order 0 we see each particular block */
430 *max
= 1 << (e4b
->bd_blkbits
+ 3);
432 return EXT4_MB_BITMAP(e4b
);
434 bb
= EXT4_MB_BUDDY(e4b
) + EXT4_SB(e4b
->bd_sb
)->s_mb_offsets
[order
];
435 *max
= EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[order
];
441 static void mb_free_blocks_double(struct inode
*inode
, struct ext4_buddy
*e4b
,
442 int first
, int count
)
445 struct super_block
*sb
= e4b
->bd_sb
;
447 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
449 assert_spin_locked(ext4_group_lock_ptr(sb
, e4b
->bd_group
));
450 for (i
= 0; i
< count
; i
++) {
451 if (!mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
)) {
452 ext4_fsblk_t blocknr
;
454 blocknr
= ext4_group_first_block_no(sb
, e4b
->bd_group
);
455 blocknr
+= first
+ i
;
456 ext4_grp_locked_error(sb
, e4b
->bd_group
,
457 inode
? inode
->i_ino
: 0,
459 "freeing block already freed "
463 mb_clear_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
467 static void mb_mark_used_double(struct ext4_buddy
*e4b
, int first
, int count
)
471 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
473 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
474 for (i
= 0; i
< count
; i
++) {
475 BUG_ON(mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
));
476 mb_set_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
480 static void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
482 if (memcmp(e4b
->bd_info
->bb_bitmap
, bitmap
, e4b
->bd_sb
->s_blocksize
)) {
483 unsigned char *b1
, *b2
;
485 b1
= (unsigned char *) e4b
->bd_info
->bb_bitmap
;
486 b2
= (unsigned char *) bitmap
;
487 for (i
= 0; i
< e4b
->bd_sb
->s_blocksize
; i
++) {
488 if (b1
[i
] != b2
[i
]) {
489 printk(KERN_ERR
"corruption in group %u "
490 "at byte %u(%u): %x in copy != %x "
491 "on disk/prealloc\n",
492 e4b
->bd_group
, i
, i
* 8, b1
[i
], b2
[i
]);
500 static inline void mb_free_blocks_double(struct inode
*inode
,
501 struct ext4_buddy
*e4b
, int first
, int count
)
505 static inline void mb_mark_used_double(struct ext4_buddy
*e4b
,
506 int first
, int count
)
510 static inline void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
516 #ifdef AGGRESSIVE_CHECK
518 #define MB_CHECK_ASSERT(assert) \
522 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
523 function, file, line, # assert); \
528 static int __mb_check_buddy(struct ext4_buddy
*e4b
, char *file
,
529 const char *function
, int line
)
531 struct super_block
*sb
= e4b
->bd_sb
;
532 int order
= e4b
->bd_blkbits
+ 1;
539 struct ext4_group_info
*grp
;
542 struct list_head
*cur
;
547 static int mb_check_counter
;
548 if (mb_check_counter
++ % 100 != 0)
553 buddy
= mb_find_buddy(e4b
, order
, &max
);
554 MB_CHECK_ASSERT(buddy
);
555 buddy2
= mb_find_buddy(e4b
, order
- 1, &max2
);
556 MB_CHECK_ASSERT(buddy2
);
557 MB_CHECK_ASSERT(buddy
!= buddy2
);
558 MB_CHECK_ASSERT(max
* 2 == max2
);
561 for (i
= 0; i
< max
; i
++) {
563 if (mb_test_bit(i
, buddy
)) {
564 /* only single bit in buddy2 may be 1 */
565 if (!mb_test_bit(i
<< 1, buddy2
)) {
567 mb_test_bit((i
<<1)+1, buddy2
));
568 } else if (!mb_test_bit((i
<< 1) + 1, buddy2
)) {
570 mb_test_bit(i
<< 1, buddy2
));
575 /* both bits in buddy2 must be 0 */
576 MB_CHECK_ASSERT(mb_test_bit(i
<< 1, buddy2
));
577 MB_CHECK_ASSERT(mb_test_bit((i
<< 1) + 1, buddy2
));
579 for (j
= 0; j
< (1 << order
); j
++) {
580 k
= (i
* (1 << order
)) + j
;
582 !mb_test_bit(k
, EXT4_MB_BITMAP(e4b
)));
586 MB_CHECK_ASSERT(e4b
->bd_info
->bb_counters
[order
] == count
);
591 buddy
= mb_find_buddy(e4b
, 0, &max
);
592 for (i
= 0; i
< max
; i
++) {
593 if (!mb_test_bit(i
, buddy
)) {
594 MB_CHECK_ASSERT(i
>= e4b
->bd_info
->bb_first_free
);
602 /* check used bits only */
603 for (j
= 0; j
< e4b
->bd_blkbits
+ 1; j
++) {
604 buddy2
= mb_find_buddy(e4b
, j
, &max2
);
606 MB_CHECK_ASSERT(k
< max2
);
607 MB_CHECK_ASSERT(mb_test_bit(k
, buddy2
));
610 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b
->bd_info
));
611 MB_CHECK_ASSERT(e4b
->bd_info
->bb_fragments
== fragments
);
613 grp
= ext4_get_group_info(sb
, e4b
->bd_group
);
614 buddy
= mb_find_buddy(e4b
, 0, &max
);
615 list_for_each(cur
, &grp
->bb_prealloc_list
) {
616 ext4_group_t groupnr
;
617 struct ext4_prealloc_space
*pa
;
618 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
619 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &groupnr
, &k
);
620 MB_CHECK_ASSERT(groupnr
== e4b
->bd_group
);
621 for (i
= 0; i
< pa
->pa_len
; i
++)
622 MB_CHECK_ASSERT(mb_test_bit(k
+ i
, buddy
));
626 #undef MB_CHECK_ASSERT
627 #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
628 __FILE__, __func__, __LINE__)
630 #define mb_check_buddy(e4b)
633 /* FIXME!! need more doc */
634 static void ext4_mb_mark_free_simple(struct super_block
*sb
,
635 void *buddy
, ext4_grpblk_t first
, ext4_grpblk_t len
,
636 struct ext4_group_info
*grp
)
638 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
642 unsigned short border
;
644 BUG_ON(len
> EXT4_BLOCKS_PER_GROUP(sb
));
646 border
= 2 << sb
->s_blocksize_bits
;
649 /* find how many blocks can be covered since this position */
650 max
= ffs(first
| border
) - 1;
652 /* find how many blocks of power 2 we need to mark */
659 /* mark multiblock chunks only */
660 grp
->bb_counters
[min
]++;
662 mb_clear_bit(first
>> min
,
663 buddy
+ sbi
->s_mb_offsets
[min
]);
671 * Cache the order of the largest free extent we have available in this block
675 mb_set_largest_free_order(struct super_block
*sb
, struct ext4_group_info
*grp
)
680 grp
->bb_largest_free_order
= -1; /* uninit */
682 bits
= sb
->s_blocksize_bits
+ 1;
683 for (i
= bits
; i
>= 0; i
--) {
684 if (grp
->bb_counters
[i
] > 0) {
685 grp
->bb_largest_free_order
= i
;
691 static noinline_for_stack
692 void ext4_mb_generate_buddy(struct super_block
*sb
,
693 void *buddy
, void *bitmap
, ext4_group_t group
)
695 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
696 ext4_grpblk_t max
= EXT4_BLOCKS_PER_GROUP(sb
);
701 unsigned fragments
= 0;
702 unsigned long long period
= get_cycles();
704 /* initialize buddy from bitmap which is aggregation
705 * of on-disk bitmap and preallocations */
706 i
= mb_find_next_zero_bit(bitmap
, max
, 0);
707 grp
->bb_first_free
= i
;
711 i
= mb_find_next_bit(bitmap
, max
, i
);
715 ext4_mb_mark_free_simple(sb
, buddy
, first
, len
, grp
);
717 grp
->bb_counters
[0]++;
719 i
= mb_find_next_zero_bit(bitmap
, max
, i
);
721 grp
->bb_fragments
= fragments
;
723 if (free
!= grp
->bb_free
) {
724 ext4_grp_locked_error(sb
, group
, 0, 0,
725 "%u blocks in bitmap, %u in gd",
728 * If we intent to continue, we consider group descritor
729 * corrupt and update bb_free using bitmap value
733 mb_set_largest_free_order(sb
, grp
);
735 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
, &(grp
->bb_state
));
737 period
= get_cycles() - period
;
738 spin_lock(&EXT4_SB(sb
)->s_bal_lock
);
739 EXT4_SB(sb
)->s_mb_buddies_generated
++;
740 EXT4_SB(sb
)->s_mb_generation_time
+= period
;
741 spin_unlock(&EXT4_SB(sb
)->s_bal_lock
);
744 /* The buddy information is attached the buddy cache inode
745 * for convenience. The information regarding each group
746 * is loaded via ext4_mb_load_buddy. The information involve
747 * block bitmap and buddy information. The information are
748 * stored in the inode as
751 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
754 * one block each for bitmap and buddy information.
755 * So for each group we take up 2 blocks. A page can
756 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
757 * So it can have information regarding groups_per_page which
758 * is blocks_per_page/2
760 * Locking note: This routine takes the block group lock of all groups
761 * for this page; do not hold this lock when calling this routine!
764 static int ext4_mb_init_cache(struct page
*page
, char *incore
)
766 ext4_group_t ngroups
;
772 ext4_group_t first_group
;
774 struct super_block
*sb
;
775 struct buffer_head
*bhs
;
776 struct buffer_head
**bh
;
781 mb_debug(1, "init page %lu\n", page
->index
);
783 inode
= page
->mapping
->host
;
785 ngroups
= ext4_get_groups_count(sb
);
786 blocksize
= 1 << inode
->i_blkbits
;
787 blocks_per_page
= PAGE_CACHE_SIZE
/ blocksize
;
789 groups_per_page
= blocks_per_page
>> 1;
790 if (groups_per_page
== 0)
793 /* allocate buffer_heads to read bitmaps */
794 if (groups_per_page
> 1) {
796 i
= sizeof(struct buffer_head
*) * groups_per_page
;
797 bh
= kzalloc(i
, GFP_NOFS
);
803 first_group
= page
->index
* blocks_per_page
/ 2;
805 /* read all groups the page covers into the cache */
806 for (i
= 0; i
< groups_per_page
; i
++) {
807 struct ext4_group_desc
*desc
;
809 if (first_group
+ i
>= ngroups
)
813 desc
= ext4_get_group_desc(sb
, first_group
+ i
, NULL
);
818 bh
[i
] = sb_getblk(sb
, ext4_block_bitmap(sb
, desc
));
822 if (bitmap_uptodate(bh
[i
]))
826 if (bitmap_uptodate(bh
[i
])) {
827 unlock_buffer(bh
[i
]);
830 ext4_lock_group(sb
, first_group
+ i
);
831 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
832 ext4_init_block_bitmap(sb
, bh
[i
],
833 first_group
+ i
, desc
);
834 set_bitmap_uptodate(bh
[i
]);
835 set_buffer_uptodate(bh
[i
]);
836 ext4_unlock_group(sb
, first_group
+ i
);
837 unlock_buffer(bh
[i
]);
840 ext4_unlock_group(sb
, first_group
+ i
);
841 if (buffer_uptodate(bh
[i
])) {
843 * if not uninit if bh is uptodate,
844 * bitmap is also uptodate
846 set_bitmap_uptodate(bh
[i
]);
847 unlock_buffer(bh
[i
]);
852 * submit the buffer_head for read. We can
853 * safely mark the bitmap as uptodate now.
854 * We do it here so the bitmap uptodate bit
855 * get set with buffer lock held.
857 set_bitmap_uptodate(bh
[i
]);
858 bh
[i
]->b_end_io
= end_buffer_read_sync
;
859 submit_bh(READ
, bh
[i
]);
860 mb_debug(1, "read bitmap for group %u\n", first_group
+ i
);
863 /* wait for I/O completion */
864 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
865 wait_on_buffer(bh
[i
]);
868 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
869 if (!buffer_uptodate(bh
[i
]))
873 first_block
= page
->index
* blocks_per_page
;
875 memset(page_address(page
), 0xff, PAGE_CACHE_SIZE
);
876 for (i
= 0; i
< blocks_per_page
; i
++) {
878 struct ext4_group_info
*grinfo
;
880 group
= (first_block
+ i
) >> 1;
881 if (group
>= ngroups
)
885 * data carry information regarding this
886 * particular group in the format specified
890 data
= page_address(page
) + (i
* blocksize
);
891 bitmap
= bh
[group
- first_group
]->b_data
;
894 * We place the buddy block and bitmap block
897 if ((first_block
+ i
) & 1) {
898 /* this is block of buddy */
899 BUG_ON(incore
== NULL
);
900 mb_debug(1, "put buddy for group %u in page %lu/%x\n",
901 group
, page
->index
, i
* blocksize
);
902 trace_ext4_mb_buddy_bitmap_load(sb
, group
);
903 grinfo
= ext4_get_group_info(sb
, group
);
904 grinfo
->bb_fragments
= 0;
905 memset(grinfo
->bb_counters
, 0,
906 sizeof(*grinfo
->bb_counters
) *
907 (sb
->s_blocksize_bits
+2));
909 * incore got set to the group block bitmap below
911 ext4_lock_group(sb
, group
);
912 ext4_mb_generate_buddy(sb
, data
, incore
, group
);
913 ext4_unlock_group(sb
, group
);
916 /* this is block of bitmap */
917 BUG_ON(incore
!= NULL
);
918 mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
919 group
, page
->index
, i
* blocksize
);
920 trace_ext4_mb_bitmap_load(sb
, group
);
922 /* see comments in ext4_mb_put_pa() */
923 ext4_lock_group(sb
, group
);
924 memcpy(data
, bitmap
, blocksize
);
926 /* mark all preallocated blks used in in-core bitmap */
927 ext4_mb_generate_from_pa(sb
, data
, group
);
928 ext4_mb_generate_from_freelist(sb
, data
, group
);
929 ext4_unlock_group(sb
, group
);
931 /* set incore so that the buddy information can be
932 * generated using this
937 SetPageUptodate(page
);
941 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
950 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
951 * block group lock of all groups for this page; do not hold the BG lock when
952 * calling this routine!
954 static noinline_for_stack
955 int ext4_mb_init_group(struct super_block
*sb
, ext4_group_t group
)
961 int block
, pnum
, poff
;
962 int num_grp_locked
= 0;
963 struct ext4_group_info
*this_grp
;
964 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
965 struct inode
*inode
= sbi
->s_buddy_cache
;
966 struct page
*page
= NULL
, *bitmap_page
= NULL
;
968 mb_debug(1, "init group %u\n", group
);
969 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
970 this_grp
= ext4_get_group_info(sb
, group
);
972 * This ensures that we don't reinit the buddy cache
973 * page which map to the group from which we are already
974 * allocating. If we are looking at the buddy cache we would
975 * have taken a reference using ext4_mb_load_buddy and that
976 * would have taken the alloc_sem lock.
978 num_grp_locked
= ext4_mb_get_buddy_cache_lock(sb
, group
);
979 if (!EXT4_MB_GRP_NEED_INIT(this_grp
)) {
981 * somebody initialized the group
982 * return without doing anything
988 * the buddy cache inode stores the block bitmap
989 * and buddy information in consecutive blocks.
990 * So for each group we need two blocks.
993 pnum
= block
/ blocks_per_page
;
994 poff
= block
% blocks_per_page
;
995 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
997 BUG_ON(page
->mapping
!= inode
->i_mapping
);
998 ret
= ext4_mb_init_cache(page
, NULL
);
1005 if (page
== NULL
|| !PageUptodate(page
)) {
1009 mark_page_accessed(page
);
1011 bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
1013 /* init buddy cache */
1015 pnum
= block
/ blocks_per_page
;
1016 poff
= block
% blocks_per_page
;
1017 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1018 if (page
== bitmap_page
) {
1020 * If both the bitmap and buddy are in
1021 * the same page we don't need to force
1026 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1027 ret
= ext4_mb_init_cache(page
, bitmap
);
1034 if (page
== NULL
|| !PageUptodate(page
)) {
1038 mark_page_accessed(page
);
1040 ext4_mb_put_buddy_cache_lock(sb
, group
, num_grp_locked
);
1042 page_cache_release(bitmap_page
);
1044 page_cache_release(page
);
1049 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1050 * block group lock of all groups for this page; do not hold the BG lock when
1051 * calling this routine!
1053 static noinline_for_stack
int
1054 ext4_mb_load_buddy(struct super_block
*sb
, ext4_group_t group
,
1055 struct ext4_buddy
*e4b
)
1057 int blocks_per_page
;
1063 struct ext4_group_info
*grp
;
1064 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1065 struct inode
*inode
= sbi
->s_buddy_cache
;
1067 mb_debug(1, "load group %u\n", group
);
1069 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1070 grp
= ext4_get_group_info(sb
, group
);
1072 e4b
->bd_blkbits
= sb
->s_blocksize_bits
;
1073 e4b
->bd_info
= ext4_get_group_info(sb
, group
);
1075 e4b
->bd_group
= group
;
1076 e4b
->bd_buddy_page
= NULL
;
1077 e4b
->bd_bitmap_page
= NULL
;
1078 e4b
->alloc_semp
= &grp
->alloc_sem
;
1080 /* Take the read lock on the group alloc
1081 * sem. This would make sure a parallel
1082 * ext4_mb_init_group happening on other
1083 * groups mapped by the page is blocked
1084 * till we are done with allocation
1087 down_read(e4b
->alloc_semp
);
1089 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp
))) {
1090 /* we need to check for group need init flag
1091 * with alloc_semp held so that we can be sure
1092 * that new blocks didn't get added to the group
1093 * when we are loading the buddy cache
1095 up_read(e4b
->alloc_semp
);
1097 * we need full data about the group
1098 * to make a good selection
1100 ret
= ext4_mb_init_group(sb
, group
);
1103 goto repeat_load_buddy
;
1107 * the buddy cache inode stores the block bitmap
1108 * and buddy information in consecutive blocks.
1109 * So for each group we need two blocks.
1112 pnum
= block
/ blocks_per_page
;
1113 poff
= block
% blocks_per_page
;
1115 /* we could use find_or_create_page(), but it locks page
1116 * what we'd like to avoid in fast path ... */
1117 page
= find_get_page(inode
->i_mapping
, pnum
);
1118 if (page
== NULL
|| !PageUptodate(page
)) {
1121 * drop the page reference and try
1122 * to get the page with lock. If we
1123 * are not uptodate that implies
1124 * somebody just created the page but
1125 * is yet to initialize the same. So
1126 * wait for it to initialize.
1128 page_cache_release(page
);
1129 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1131 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1132 if (!PageUptodate(page
)) {
1133 ret
= ext4_mb_init_cache(page
, NULL
);
1138 mb_cmp_bitmaps(e4b
, page_address(page
) +
1139 (poff
* sb
->s_blocksize
));
1144 if (page
== NULL
|| !PageUptodate(page
)) {
1148 e4b
->bd_bitmap_page
= page
;
1149 e4b
->bd_bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
1150 mark_page_accessed(page
);
1153 pnum
= block
/ blocks_per_page
;
1154 poff
= block
% blocks_per_page
;
1156 page
= find_get_page(inode
->i_mapping
, pnum
);
1157 if (page
== NULL
|| !PageUptodate(page
)) {
1159 page_cache_release(page
);
1160 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1162 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1163 if (!PageUptodate(page
)) {
1164 ret
= ext4_mb_init_cache(page
, e4b
->bd_bitmap
);
1173 if (page
== NULL
|| !PageUptodate(page
)) {
1177 e4b
->bd_buddy_page
= page
;
1178 e4b
->bd_buddy
= page_address(page
) + (poff
* sb
->s_blocksize
);
1179 mark_page_accessed(page
);
1181 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
1182 BUG_ON(e4b
->bd_buddy_page
== NULL
);
1187 if (e4b
->bd_bitmap_page
)
1188 page_cache_release(e4b
->bd_bitmap_page
);
1189 if (e4b
->bd_buddy_page
)
1190 page_cache_release(e4b
->bd_buddy_page
);
1191 e4b
->bd_buddy
= NULL
;
1192 e4b
->bd_bitmap
= NULL
;
1194 /* Done with the buddy cache */
1195 up_read(e4b
->alloc_semp
);
1199 static void ext4_mb_unload_buddy(struct ext4_buddy
*e4b
)
1201 if (e4b
->bd_bitmap_page
)
1202 page_cache_release(e4b
->bd_bitmap_page
);
1203 if (e4b
->bd_buddy_page
)
1204 page_cache_release(e4b
->bd_buddy_page
);
1205 /* Done with the buddy cache */
1206 if (e4b
->alloc_semp
)
1207 up_read(e4b
->alloc_semp
);
1211 static int mb_find_order_for_block(struct ext4_buddy
*e4b
, int block
)
1216 BUG_ON(EXT4_MB_BITMAP(e4b
) == EXT4_MB_BUDDY(e4b
));
1217 BUG_ON(block
>= (1 << (e4b
->bd_blkbits
+ 3)));
1219 bb
= EXT4_MB_BUDDY(e4b
);
1220 while (order
<= e4b
->bd_blkbits
+ 1) {
1222 if (!mb_test_bit(block
, bb
)) {
1223 /* this block is part of buddy of order 'order' */
1226 bb
+= 1 << (e4b
->bd_blkbits
- order
);
1232 static void mb_clear_bits(void *bm
, int cur
, int len
)
1238 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1239 /* fast path: clear whole word at once */
1240 addr
= bm
+ (cur
>> 3);
1245 mb_clear_bit(cur
, bm
);
1250 static void mb_set_bits(void *bm
, int cur
, int len
)
1256 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1257 /* fast path: set whole word at once */
1258 addr
= bm
+ (cur
>> 3);
1263 mb_set_bit(cur
, bm
);
1268 static void mb_free_blocks(struct inode
*inode
, struct ext4_buddy
*e4b
,
1269 int first
, int count
)
1276 struct super_block
*sb
= e4b
->bd_sb
;
1278 BUG_ON(first
+ count
> (sb
->s_blocksize
<< 3));
1279 assert_spin_locked(ext4_group_lock_ptr(sb
, e4b
->bd_group
));
1280 mb_check_buddy(e4b
);
1281 mb_free_blocks_double(inode
, e4b
, first
, count
);
1283 e4b
->bd_info
->bb_free
+= count
;
1284 if (first
< e4b
->bd_info
->bb_first_free
)
1285 e4b
->bd_info
->bb_first_free
= first
;
1287 /* let's maintain fragments counter */
1289 block
= !mb_test_bit(first
- 1, EXT4_MB_BITMAP(e4b
));
1290 if (first
+ count
< EXT4_SB(sb
)->s_mb_maxs
[0])
1291 max
= !mb_test_bit(first
+ count
, EXT4_MB_BITMAP(e4b
));
1293 e4b
->bd_info
->bb_fragments
--;
1294 else if (!block
&& !max
)
1295 e4b
->bd_info
->bb_fragments
++;
1297 /* let's maintain buddy itself */
1298 while (count
-- > 0) {
1302 if (!mb_test_bit(block
, EXT4_MB_BITMAP(e4b
))) {
1303 ext4_fsblk_t blocknr
;
1305 blocknr
= ext4_group_first_block_no(sb
, e4b
->bd_group
);
1307 ext4_grp_locked_error(sb
, e4b
->bd_group
,
1308 inode
? inode
->i_ino
: 0,
1310 "freeing already freed block "
1313 mb_clear_bit(block
, EXT4_MB_BITMAP(e4b
));
1314 e4b
->bd_info
->bb_counters
[order
]++;
1316 /* start of the buddy */
1317 buddy
= mb_find_buddy(e4b
, order
, &max
);
1321 if (mb_test_bit(block
, buddy
) ||
1322 mb_test_bit(block
+ 1, buddy
))
1325 /* both the buddies are free, try to coalesce them */
1326 buddy2
= mb_find_buddy(e4b
, order
+ 1, &max
);
1332 /* for special purposes, we don't set
1333 * free bits in bitmap */
1334 mb_set_bit(block
, buddy
);
1335 mb_set_bit(block
+ 1, buddy
);
1337 e4b
->bd_info
->bb_counters
[order
]--;
1338 e4b
->bd_info
->bb_counters
[order
]--;
1342 e4b
->bd_info
->bb_counters
[order
]++;
1344 mb_clear_bit(block
, buddy2
);
1348 mb_set_largest_free_order(sb
, e4b
->bd_info
);
1349 mb_check_buddy(e4b
);
1352 static int mb_find_extent(struct ext4_buddy
*e4b
, int order
, int block
,
1353 int needed
, struct ext4_free_extent
*ex
)
1360 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
1363 buddy
= mb_find_buddy(e4b
, order
, &max
);
1364 BUG_ON(buddy
== NULL
);
1365 BUG_ON(block
>= max
);
1366 if (mb_test_bit(block
, buddy
)) {
1373 /* FIXME dorp order completely ? */
1374 if (likely(order
== 0)) {
1375 /* find actual order */
1376 order
= mb_find_order_for_block(e4b
, block
);
1377 block
= block
>> order
;
1380 ex
->fe_len
= 1 << order
;
1381 ex
->fe_start
= block
<< order
;
1382 ex
->fe_group
= e4b
->bd_group
;
1384 /* calc difference from given start */
1385 next
= next
- ex
->fe_start
;
1387 ex
->fe_start
+= next
;
1389 while (needed
> ex
->fe_len
&&
1390 (buddy
= mb_find_buddy(e4b
, order
, &max
))) {
1392 if (block
+ 1 >= max
)
1395 next
= (block
+ 1) * (1 << order
);
1396 if (mb_test_bit(next
, EXT4_MB_BITMAP(e4b
)))
1399 ord
= mb_find_order_for_block(e4b
, next
);
1402 block
= next
>> order
;
1403 ex
->fe_len
+= 1 << order
;
1406 BUG_ON(ex
->fe_start
+ ex
->fe_len
> (1 << (e4b
->bd_blkbits
+ 3)));
1410 static int mb_mark_used(struct ext4_buddy
*e4b
, struct ext4_free_extent
*ex
)
1416 int start
= ex
->fe_start
;
1417 int len
= ex
->fe_len
;
1422 BUG_ON(start
+ len
> (e4b
->bd_sb
->s_blocksize
<< 3));
1423 BUG_ON(e4b
->bd_group
!= ex
->fe_group
);
1424 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
1425 mb_check_buddy(e4b
);
1426 mb_mark_used_double(e4b
, start
, len
);
1428 e4b
->bd_info
->bb_free
-= len
;
1429 if (e4b
->bd_info
->bb_first_free
== start
)
1430 e4b
->bd_info
->bb_first_free
+= len
;
1432 /* let's maintain fragments counter */
1434 mlen
= !mb_test_bit(start
- 1, EXT4_MB_BITMAP(e4b
));
1435 if (start
+ len
< EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[0])
1436 max
= !mb_test_bit(start
+ len
, EXT4_MB_BITMAP(e4b
));
1438 e4b
->bd_info
->bb_fragments
++;
1439 else if (!mlen
&& !max
)
1440 e4b
->bd_info
->bb_fragments
--;
1442 /* let's maintain buddy itself */
1444 ord
= mb_find_order_for_block(e4b
, start
);
1446 if (((start
>> ord
) << ord
) == start
&& len
>= (1 << ord
)) {
1447 /* the whole chunk may be allocated at once! */
1449 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1450 BUG_ON((start
>> ord
) >= max
);
1451 mb_set_bit(start
>> ord
, buddy
);
1452 e4b
->bd_info
->bb_counters
[ord
]--;
1459 /* store for history */
1461 ret
= len
| (ord
<< 16);
1463 /* we have to split large buddy */
1465 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1466 mb_set_bit(start
>> ord
, buddy
);
1467 e4b
->bd_info
->bb_counters
[ord
]--;
1470 cur
= (start
>> ord
) & ~1U;
1471 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1472 mb_clear_bit(cur
, buddy
);
1473 mb_clear_bit(cur
+ 1, buddy
);
1474 e4b
->bd_info
->bb_counters
[ord
]++;
1475 e4b
->bd_info
->bb_counters
[ord
]++;
1477 mb_set_largest_free_order(e4b
->bd_sb
, e4b
->bd_info
);
1479 mb_set_bits(EXT4_MB_BITMAP(e4b
), ex
->fe_start
, len0
);
1480 mb_check_buddy(e4b
);
1486 * Must be called under group lock!
1488 static void ext4_mb_use_best_found(struct ext4_allocation_context
*ac
,
1489 struct ext4_buddy
*e4b
)
1491 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1494 BUG_ON(ac
->ac_b_ex
.fe_group
!= e4b
->bd_group
);
1495 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1497 ac
->ac_b_ex
.fe_len
= min(ac
->ac_b_ex
.fe_len
, ac
->ac_g_ex
.fe_len
);
1498 ac
->ac_b_ex
.fe_logical
= ac
->ac_g_ex
.fe_logical
;
1499 ret
= mb_mark_used(e4b
, &ac
->ac_b_ex
);
1501 /* preallocation can change ac_b_ex, thus we store actually
1502 * allocated blocks for history */
1503 ac
->ac_f_ex
= ac
->ac_b_ex
;
1505 ac
->ac_status
= AC_STATUS_FOUND
;
1506 ac
->ac_tail
= ret
& 0xffff;
1507 ac
->ac_buddy
= ret
>> 16;
1510 * take the page reference. We want the page to be pinned
1511 * so that we don't get a ext4_mb_init_cache_call for this
1512 * group until we update the bitmap. That would mean we
1513 * double allocate blocks. The reference is dropped
1514 * in ext4_mb_release_context
1516 ac
->ac_bitmap_page
= e4b
->bd_bitmap_page
;
1517 get_page(ac
->ac_bitmap_page
);
1518 ac
->ac_buddy_page
= e4b
->bd_buddy_page
;
1519 get_page(ac
->ac_buddy_page
);
1520 /* on allocation we use ac to track the held semaphore */
1521 ac
->alloc_semp
= e4b
->alloc_semp
;
1522 e4b
->alloc_semp
= NULL
;
1523 /* store last allocated for subsequent stream allocation */
1524 if (ac
->ac_flags
& EXT4_MB_STREAM_ALLOC
) {
1525 spin_lock(&sbi
->s_md_lock
);
1526 sbi
->s_mb_last_group
= ac
->ac_f_ex
.fe_group
;
1527 sbi
->s_mb_last_start
= ac
->ac_f_ex
.fe_start
;
1528 spin_unlock(&sbi
->s_md_lock
);
1533 * regular allocator, for general purposes allocation
1536 static void ext4_mb_check_limits(struct ext4_allocation_context
*ac
,
1537 struct ext4_buddy
*e4b
,
1540 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1541 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1542 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1543 struct ext4_free_extent ex
;
1546 if (ac
->ac_status
== AC_STATUS_FOUND
)
1549 * We don't want to scan for a whole year
1551 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
&&
1552 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1553 ac
->ac_status
= AC_STATUS_BREAK
;
1558 * Haven't found good chunk so far, let's continue
1560 if (bex
->fe_len
< gex
->fe_len
)
1563 if ((finish_group
|| ac
->ac_found
> sbi
->s_mb_min_to_scan
)
1564 && bex
->fe_group
== e4b
->bd_group
) {
1565 /* recheck chunk's availability - we don't know
1566 * when it was found (within this lock-unlock
1568 max
= mb_find_extent(e4b
, 0, bex
->fe_start
, gex
->fe_len
, &ex
);
1569 if (max
>= gex
->fe_len
) {
1570 ext4_mb_use_best_found(ac
, e4b
);
1577 * The routine checks whether found extent is good enough. If it is,
1578 * then the extent gets marked used and flag is set to the context
1579 * to stop scanning. Otherwise, the extent is compared with the
1580 * previous found extent and if new one is better, then it's stored
1581 * in the context. Later, the best found extent will be used, if
1582 * mballoc can't find good enough extent.
1584 * FIXME: real allocation policy is to be designed yet!
1586 static void ext4_mb_measure_extent(struct ext4_allocation_context
*ac
,
1587 struct ext4_free_extent
*ex
,
1588 struct ext4_buddy
*e4b
)
1590 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1591 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1593 BUG_ON(ex
->fe_len
<= 0);
1594 BUG_ON(ex
->fe_len
> EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
1595 BUG_ON(ex
->fe_start
>= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
1596 BUG_ON(ac
->ac_status
!= AC_STATUS_CONTINUE
);
1601 * The special case - take what you catch first
1603 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1605 ext4_mb_use_best_found(ac
, e4b
);
1610 * Let's check whether the chuck is good enough
1612 if (ex
->fe_len
== gex
->fe_len
) {
1614 ext4_mb_use_best_found(ac
, e4b
);
1619 * If this is first found extent, just store it in the context
1621 if (bex
->fe_len
== 0) {
1627 * If new found extent is better, store it in the context
1629 if (bex
->fe_len
< gex
->fe_len
) {
1630 /* if the request isn't satisfied, any found extent
1631 * larger than previous best one is better */
1632 if (ex
->fe_len
> bex
->fe_len
)
1634 } else if (ex
->fe_len
> gex
->fe_len
) {
1635 /* if the request is satisfied, then we try to find
1636 * an extent that still satisfy the request, but is
1637 * smaller than previous one */
1638 if (ex
->fe_len
< bex
->fe_len
)
1642 ext4_mb_check_limits(ac
, e4b
, 0);
1645 static noinline_for_stack
1646 int ext4_mb_try_best_found(struct ext4_allocation_context
*ac
,
1647 struct ext4_buddy
*e4b
)
1649 struct ext4_free_extent ex
= ac
->ac_b_ex
;
1650 ext4_group_t group
= ex
.fe_group
;
1654 BUG_ON(ex
.fe_len
<= 0);
1655 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1659 ext4_lock_group(ac
->ac_sb
, group
);
1660 max
= mb_find_extent(e4b
, 0, ex
.fe_start
, ex
.fe_len
, &ex
);
1664 ext4_mb_use_best_found(ac
, e4b
);
1667 ext4_unlock_group(ac
->ac_sb
, group
);
1668 ext4_mb_unload_buddy(e4b
);
1673 static noinline_for_stack
1674 int ext4_mb_find_by_goal(struct ext4_allocation_context
*ac
,
1675 struct ext4_buddy
*e4b
)
1677 ext4_group_t group
= ac
->ac_g_ex
.fe_group
;
1680 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1681 struct ext4_free_extent ex
;
1683 if (!(ac
->ac_flags
& EXT4_MB_HINT_TRY_GOAL
))
1686 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1690 ext4_lock_group(ac
->ac_sb
, group
);
1691 max
= mb_find_extent(e4b
, 0, ac
->ac_g_ex
.fe_start
,
1692 ac
->ac_g_ex
.fe_len
, &ex
);
1694 if (max
>= ac
->ac_g_ex
.fe_len
&& ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
) {
1697 start
= ext4_group_first_block_no(ac
->ac_sb
, e4b
->bd_group
) +
1699 /* use do_div to get remainder (would be 64-bit modulo) */
1700 if (do_div(start
, sbi
->s_stripe
) == 0) {
1703 ext4_mb_use_best_found(ac
, e4b
);
1705 } else if (max
>= ac
->ac_g_ex
.fe_len
) {
1706 BUG_ON(ex
.fe_len
<= 0);
1707 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1708 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1711 ext4_mb_use_best_found(ac
, e4b
);
1712 } else if (max
> 0 && (ac
->ac_flags
& EXT4_MB_HINT_MERGE
)) {
1713 /* Sometimes, caller may want to merge even small
1714 * number of blocks to an existing extent */
1715 BUG_ON(ex
.fe_len
<= 0);
1716 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1717 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1720 ext4_mb_use_best_found(ac
, e4b
);
1722 ext4_unlock_group(ac
->ac_sb
, group
);
1723 ext4_mb_unload_buddy(e4b
);
1729 * The routine scans buddy structures (not bitmap!) from given order
1730 * to max order and tries to find big enough chunk to satisfy the req
1732 static noinline_for_stack
1733 void ext4_mb_simple_scan_group(struct ext4_allocation_context
*ac
,
1734 struct ext4_buddy
*e4b
)
1736 struct super_block
*sb
= ac
->ac_sb
;
1737 struct ext4_group_info
*grp
= e4b
->bd_info
;
1743 BUG_ON(ac
->ac_2order
<= 0);
1744 for (i
= ac
->ac_2order
; i
<= sb
->s_blocksize_bits
+ 1; i
++) {
1745 if (grp
->bb_counters
[i
] == 0)
1748 buddy
= mb_find_buddy(e4b
, i
, &max
);
1749 BUG_ON(buddy
== NULL
);
1751 k
= mb_find_next_zero_bit(buddy
, max
, 0);
1756 ac
->ac_b_ex
.fe_len
= 1 << i
;
1757 ac
->ac_b_ex
.fe_start
= k
<< i
;
1758 ac
->ac_b_ex
.fe_group
= e4b
->bd_group
;
1760 ext4_mb_use_best_found(ac
, e4b
);
1762 BUG_ON(ac
->ac_b_ex
.fe_len
!= ac
->ac_g_ex
.fe_len
);
1764 if (EXT4_SB(sb
)->s_mb_stats
)
1765 atomic_inc(&EXT4_SB(sb
)->s_bal_2orders
);
1772 * The routine scans the group and measures all found extents.
1773 * In order to optimize scanning, caller must pass number of
1774 * free blocks in the group, so the routine can know upper limit.
1776 static noinline_for_stack
1777 void ext4_mb_complex_scan_group(struct ext4_allocation_context
*ac
,
1778 struct ext4_buddy
*e4b
)
1780 struct super_block
*sb
= ac
->ac_sb
;
1781 void *bitmap
= EXT4_MB_BITMAP(e4b
);
1782 struct ext4_free_extent ex
;
1786 free
= e4b
->bd_info
->bb_free
;
1789 i
= e4b
->bd_info
->bb_first_free
;
1791 while (free
&& ac
->ac_status
== AC_STATUS_CONTINUE
) {
1792 i
= mb_find_next_zero_bit(bitmap
,
1793 EXT4_BLOCKS_PER_GROUP(sb
), i
);
1794 if (i
>= EXT4_BLOCKS_PER_GROUP(sb
)) {
1796 * IF we have corrupt bitmap, we won't find any
1797 * free blocks even though group info says we
1798 * we have free blocks
1800 ext4_grp_locked_error(sb
, e4b
->bd_group
, 0, 0,
1801 "%d free blocks as per "
1802 "group info. But bitmap says 0",
1807 mb_find_extent(e4b
, 0, i
, ac
->ac_g_ex
.fe_len
, &ex
);
1808 BUG_ON(ex
.fe_len
<= 0);
1809 if (free
< ex
.fe_len
) {
1810 ext4_grp_locked_error(sb
, e4b
->bd_group
, 0, 0,
1811 "%d free blocks as per "
1812 "group info. But got %d blocks",
1815 * The number of free blocks differs. This mostly
1816 * indicate that the bitmap is corrupt. So exit
1817 * without claiming the space.
1822 ext4_mb_measure_extent(ac
, &ex
, e4b
);
1828 ext4_mb_check_limits(ac
, e4b
, 1);
1832 * This is a special case for storages like raid5
1833 * we try to find stripe-aligned chunks for stripe-size-multiple requests
1835 static noinline_for_stack
1836 void ext4_mb_scan_aligned(struct ext4_allocation_context
*ac
,
1837 struct ext4_buddy
*e4b
)
1839 struct super_block
*sb
= ac
->ac_sb
;
1840 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1841 void *bitmap
= EXT4_MB_BITMAP(e4b
);
1842 struct ext4_free_extent ex
;
1843 ext4_fsblk_t first_group_block
;
1848 BUG_ON(sbi
->s_stripe
== 0);
1850 /* find first stripe-aligned block in group */
1851 first_group_block
= ext4_group_first_block_no(sb
, e4b
->bd_group
);
1853 a
= first_group_block
+ sbi
->s_stripe
- 1;
1854 do_div(a
, sbi
->s_stripe
);
1855 i
= (a
* sbi
->s_stripe
) - first_group_block
;
1857 while (i
< EXT4_BLOCKS_PER_GROUP(sb
)) {
1858 if (!mb_test_bit(i
, bitmap
)) {
1859 max
= mb_find_extent(e4b
, 0, i
, sbi
->s_stripe
, &ex
);
1860 if (max
>= sbi
->s_stripe
) {
1863 ext4_mb_use_best_found(ac
, e4b
);
1871 /* This is now called BEFORE we load the buddy bitmap. */
1872 static int ext4_mb_good_group(struct ext4_allocation_context
*ac
,
1873 ext4_group_t group
, int cr
)
1875 unsigned free
, fragments
;
1876 int flex_size
= ext4_flex_bg_size(EXT4_SB(ac
->ac_sb
));
1877 struct ext4_group_info
*grp
= ext4_get_group_info(ac
->ac_sb
, group
);
1879 BUG_ON(cr
< 0 || cr
>= 4);
1881 /* We only do this if the grp has never been initialized */
1882 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp
))) {
1883 int ret
= ext4_mb_init_group(ac
->ac_sb
, group
);
1888 free
= grp
->bb_free
;
1889 fragments
= grp
->bb_fragments
;
1897 BUG_ON(ac
->ac_2order
== 0);
1899 if (grp
->bb_largest_free_order
< ac
->ac_2order
)
1902 /* Avoid using the first bg of a flexgroup for data files */
1903 if ((ac
->ac_flags
& EXT4_MB_HINT_DATA
) &&
1904 (flex_size
>= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
) &&
1905 ((group
% flex_size
) == 0))
1910 if ((free
/ fragments
) >= ac
->ac_g_ex
.fe_len
)
1914 if (free
>= ac
->ac_g_ex
.fe_len
)
1927 * lock the group_info alloc_sem of all the groups
1928 * belonging to the same buddy cache page. This
1929 * make sure other parallel operation on the buddy
1930 * cache doesn't happen whild holding the buddy cache
1933 int ext4_mb_get_buddy_cache_lock(struct super_block
*sb
, ext4_group_t group
)
1937 int blocks_per_page
;
1938 int groups_per_page
;
1939 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
1940 ext4_group_t first_group
;
1941 struct ext4_group_info
*grp
;
1943 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1945 * the buddy cache inode stores the block bitmap
1946 * and buddy information in consecutive blocks.
1947 * So for each group we need two blocks.
1950 pnum
= block
/ blocks_per_page
;
1951 first_group
= pnum
* blocks_per_page
/ 2;
1953 groups_per_page
= blocks_per_page
>> 1;
1954 if (groups_per_page
== 0)
1955 groups_per_page
= 1;
1956 /* read all groups the page covers into the cache */
1957 for (i
= 0; i
< groups_per_page
; i
++) {
1959 if ((first_group
+ i
) >= ngroups
)
1961 grp
= ext4_get_group_info(sb
, first_group
+ i
);
1962 /* take all groups write allocation
1963 * semaphore. This make sure there is
1964 * no block allocation going on in any
1967 down_write_nested(&grp
->alloc_sem
, i
);
1972 void ext4_mb_put_buddy_cache_lock(struct super_block
*sb
,
1973 ext4_group_t group
, int locked_group
)
1977 int blocks_per_page
;
1978 ext4_group_t first_group
;
1979 struct ext4_group_info
*grp
;
1981 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1983 * the buddy cache inode stores the block bitmap
1984 * and buddy information in consecutive blocks.
1985 * So for each group we need two blocks.
1988 pnum
= block
/ blocks_per_page
;
1989 first_group
= pnum
* blocks_per_page
/ 2;
1990 /* release locks on all the groups */
1991 for (i
= 0; i
< locked_group
; i
++) {
1993 grp
= ext4_get_group_info(sb
, first_group
+ i
);
1994 /* take all groups write allocation
1995 * semaphore. This make sure there is
1996 * no block allocation going on in any
1999 up_write(&grp
->alloc_sem
);
2004 static noinline_for_stack
int
2005 ext4_mb_regular_allocator(struct ext4_allocation_context
*ac
)
2007 ext4_group_t ngroups
, group
, i
;
2010 struct ext4_sb_info
*sbi
;
2011 struct super_block
*sb
;
2012 struct ext4_buddy e4b
;
2016 ngroups
= ext4_get_groups_count(sb
);
2017 /* non-extent files are limited to low blocks/groups */
2018 if (!(ext4_test_inode_flag(ac
->ac_inode
, EXT4_INODE_EXTENTS
)))
2019 ngroups
= sbi
->s_blockfile_groups
;
2021 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
2023 /* first, try the goal */
2024 err
= ext4_mb_find_by_goal(ac
, &e4b
);
2025 if (err
|| ac
->ac_status
== AC_STATUS_FOUND
)
2028 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
2032 * ac->ac2_order is set only if the fe_len is a power of 2
2033 * if ac2_order is set we also set criteria to 0 so that we
2034 * try exact allocation using buddy.
2036 i
= fls(ac
->ac_g_ex
.fe_len
);
2039 * We search using buddy data only if the order of the request
2040 * is greater than equal to the sbi_s_mb_order2_reqs
2041 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
2043 if (i
>= sbi
->s_mb_order2_reqs
) {
2045 * This should tell if fe_len is exactly power of 2
2047 if ((ac
->ac_g_ex
.fe_len
& (~(1 << (i
- 1)))) == 0)
2048 ac
->ac_2order
= i
- 1;
2051 /* if stream allocation is enabled, use global goal */
2052 if (ac
->ac_flags
& EXT4_MB_STREAM_ALLOC
) {
2053 /* TBD: may be hot point */
2054 spin_lock(&sbi
->s_md_lock
);
2055 ac
->ac_g_ex
.fe_group
= sbi
->s_mb_last_group
;
2056 ac
->ac_g_ex
.fe_start
= sbi
->s_mb_last_start
;
2057 spin_unlock(&sbi
->s_md_lock
);
2060 /* Let's just scan groups to find more-less suitable blocks */
2061 cr
= ac
->ac_2order
? 0 : 1;
2063 * cr == 0 try to get exact allocation,
2064 * cr == 3 try to get anything
2067 for (; cr
< 4 && ac
->ac_status
== AC_STATUS_CONTINUE
; cr
++) {
2068 ac
->ac_criteria
= cr
;
2070 * searching for the right group start
2071 * from the goal value specified
2073 group
= ac
->ac_g_ex
.fe_group
;
2075 for (i
= 0; i
< ngroups
; group
++, i
++) {
2076 if (group
== ngroups
)
2079 /* This now checks without needing the buddy page */
2080 if (!ext4_mb_good_group(ac
, group
, cr
))
2083 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2087 ext4_lock_group(sb
, group
);
2090 * We need to check again after locking the
2093 if (!ext4_mb_good_group(ac
, group
, cr
)) {
2094 ext4_unlock_group(sb
, group
);
2095 ext4_mb_unload_buddy(&e4b
);
2099 ac
->ac_groups_scanned
++;
2101 ext4_mb_simple_scan_group(ac
, &e4b
);
2102 else if (cr
== 1 && sbi
->s_stripe
&&
2103 !(ac
->ac_g_ex
.fe_len
% sbi
->s_stripe
))
2104 ext4_mb_scan_aligned(ac
, &e4b
);
2106 ext4_mb_complex_scan_group(ac
, &e4b
);
2108 ext4_unlock_group(sb
, group
);
2109 ext4_mb_unload_buddy(&e4b
);
2111 if (ac
->ac_status
!= AC_STATUS_CONTINUE
)
2116 if (ac
->ac_b_ex
.fe_len
> 0 && ac
->ac_status
!= AC_STATUS_FOUND
&&
2117 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
2119 * We've been searching too long. Let's try to allocate
2120 * the best chunk we've found so far
2123 ext4_mb_try_best_found(ac
, &e4b
);
2124 if (ac
->ac_status
!= AC_STATUS_FOUND
) {
2126 * Someone more lucky has already allocated it.
2127 * The only thing we can do is just take first
2129 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2131 ac
->ac_b_ex
.fe_group
= 0;
2132 ac
->ac_b_ex
.fe_start
= 0;
2133 ac
->ac_b_ex
.fe_len
= 0;
2134 ac
->ac_status
= AC_STATUS_CONTINUE
;
2135 ac
->ac_flags
|= EXT4_MB_HINT_FIRST
;
2137 atomic_inc(&sbi
->s_mb_lost_chunks
);
2145 static void *ext4_mb_seq_groups_start(struct seq_file
*seq
, loff_t
*pos
)
2147 struct super_block
*sb
= seq
->private;
2150 if (*pos
< 0 || *pos
>= ext4_get_groups_count(sb
))
2153 return (void *) ((unsigned long) group
);
2156 static void *ext4_mb_seq_groups_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2158 struct super_block
*sb
= seq
->private;
2162 if (*pos
< 0 || *pos
>= ext4_get_groups_count(sb
))
2165 return (void *) ((unsigned long) group
);
2168 static int ext4_mb_seq_groups_show(struct seq_file
*seq
, void *v
)
2170 struct super_block
*sb
= seq
->private;
2171 ext4_group_t group
= (ext4_group_t
) ((unsigned long) v
);
2174 struct ext4_buddy e4b
;
2176 struct ext4_group_info info
;
2177 ext4_grpblk_t counters
[16];
2182 seq_printf(seq
, "#%-5s: %-5s %-5s %-5s "
2183 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2184 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2185 "group", "free", "frags", "first",
2186 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2187 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2189 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(sg
.info
.bb_counters
[0]) +
2190 sizeof(struct ext4_group_info
);
2191 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2193 seq_printf(seq
, "#%-5u: I/O error\n", group
);
2196 ext4_lock_group(sb
, group
);
2197 memcpy(&sg
, ext4_get_group_info(sb
, group
), i
);
2198 ext4_unlock_group(sb
, group
);
2199 ext4_mb_unload_buddy(&e4b
);
2201 seq_printf(seq
, "#%-5u: %-5u %-5u %-5u [", group
, sg
.info
.bb_free
,
2202 sg
.info
.bb_fragments
, sg
.info
.bb_first_free
);
2203 for (i
= 0; i
<= 13; i
++)
2204 seq_printf(seq
, " %-5u", i
<= sb
->s_blocksize_bits
+ 1 ?
2205 sg
.info
.bb_counters
[i
] : 0);
2206 seq_printf(seq
, " ]\n");
2211 static void ext4_mb_seq_groups_stop(struct seq_file
*seq
, void *v
)
2215 static const struct seq_operations ext4_mb_seq_groups_ops
= {
2216 .start
= ext4_mb_seq_groups_start
,
2217 .next
= ext4_mb_seq_groups_next
,
2218 .stop
= ext4_mb_seq_groups_stop
,
2219 .show
= ext4_mb_seq_groups_show
,
2222 static int ext4_mb_seq_groups_open(struct inode
*inode
, struct file
*file
)
2224 struct super_block
*sb
= PDE(inode
)->data
;
2227 rc
= seq_open(file
, &ext4_mb_seq_groups_ops
);
2229 struct seq_file
*m
= file
->private_data
;
2236 static const struct file_operations ext4_mb_seq_groups_fops
= {
2237 .owner
= THIS_MODULE
,
2238 .open
= ext4_mb_seq_groups_open
,
2240 .llseek
= seq_lseek
,
2241 .release
= seq_release
,
2244 static struct kmem_cache
*get_groupinfo_cache(int blocksize_bits
)
2246 int cache_index
= blocksize_bits
- EXT4_MIN_BLOCK_LOG_SIZE
;
2247 struct kmem_cache
*cachep
= ext4_groupinfo_caches
[cache_index
];
2253 /* Create and initialize ext4_group_info data for the given group. */
2254 int ext4_mb_add_groupinfo(struct super_block
*sb
, ext4_group_t group
,
2255 struct ext4_group_desc
*desc
)
2259 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2260 struct ext4_group_info
**meta_group_info
;
2261 struct kmem_cache
*cachep
= get_groupinfo_cache(sb
->s_blocksize_bits
);
2264 * First check if this group is the first of a reserved block.
2265 * If it's true, we have to allocate a new table of pointers
2266 * to ext4_group_info structures
2268 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0) {
2269 metalen
= sizeof(*meta_group_info
) <<
2270 EXT4_DESC_PER_BLOCK_BITS(sb
);
2271 meta_group_info
= kmalloc(metalen
, GFP_KERNEL
);
2272 if (meta_group_info
== NULL
) {
2273 printk(KERN_ERR
"EXT4-fs: can't allocate mem for a "
2275 goto exit_meta_group_info
;
2277 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)] =
2282 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)];
2283 i
= group
& (EXT4_DESC_PER_BLOCK(sb
) - 1);
2285 meta_group_info
[i
] = kmem_cache_alloc(cachep
, GFP_KERNEL
);
2286 if (meta_group_info
[i
] == NULL
) {
2287 printk(KERN_ERR
"EXT4-fs: can't allocate buddy mem\n");
2288 goto exit_group_info
;
2290 memset(meta_group_info
[i
], 0, kmem_cache_size(cachep
));
2291 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
,
2292 &(meta_group_info
[i
]->bb_state
));
2295 * initialize bb_free to be able to skip
2296 * empty groups without initialization
2298 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2299 meta_group_info
[i
]->bb_free
=
2300 ext4_free_blocks_after_init(sb
, group
, desc
);
2302 meta_group_info
[i
]->bb_free
=
2303 ext4_free_blks_count(sb
, desc
);
2306 INIT_LIST_HEAD(&meta_group_info
[i
]->bb_prealloc_list
);
2307 init_rwsem(&meta_group_info
[i
]->alloc_sem
);
2308 meta_group_info
[i
]->bb_free_root
= RB_ROOT
;
2309 meta_group_info
[i
]->bb_largest_free_order
= -1; /* uninit */
2313 struct buffer_head
*bh
;
2314 meta_group_info
[i
]->bb_bitmap
=
2315 kmalloc(sb
->s_blocksize
, GFP_KERNEL
);
2316 BUG_ON(meta_group_info
[i
]->bb_bitmap
== NULL
);
2317 bh
= ext4_read_block_bitmap(sb
, group
);
2319 memcpy(meta_group_info
[i
]->bb_bitmap
, bh
->b_data
,
2328 /* If a meta_group_info table has been allocated, release it now */
2329 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0)
2330 kfree(sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)]);
2331 exit_meta_group_info
:
2333 } /* ext4_mb_add_groupinfo */
2335 static int ext4_mb_init_backend(struct super_block
*sb
)
2337 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
2339 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2340 struct ext4_super_block
*es
= sbi
->s_es
;
2341 int num_meta_group_infos
;
2342 int num_meta_group_infos_max
;
2344 struct ext4_group_desc
*desc
;
2345 struct kmem_cache
*cachep
;
2347 /* This is the number of blocks used by GDT */
2348 num_meta_group_infos
= (ngroups
+ EXT4_DESC_PER_BLOCK(sb
) -
2349 1) >> EXT4_DESC_PER_BLOCK_BITS(sb
);
2352 * This is the total number of blocks used by GDT including
2353 * the number of reserved blocks for GDT.
2354 * The s_group_info array is allocated with this value
2355 * to allow a clean online resize without a complex
2356 * manipulation of pointer.
2357 * The drawback is the unused memory when no resize
2358 * occurs but it's very low in terms of pages
2359 * (see comments below)
2360 * Need to handle this properly when META_BG resizing is allowed
2362 num_meta_group_infos_max
= num_meta_group_infos
+
2363 le16_to_cpu(es
->s_reserved_gdt_blocks
);
2366 * array_size is the size of s_group_info array. We round it
2367 * to the next power of two because this approximation is done
2368 * internally by kmalloc so we can have some more memory
2369 * for free here (e.g. may be used for META_BG resize).
2372 while (array_size
< sizeof(*sbi
->s_group_info
) *
2373 num_meta_group_infos_max
)
2374 array_size
= array_size
<< 1;
2375 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2376 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2377 * So a two level scheme suffices for now. */
2378 sbi
->s_group_info
= kmalloc(array_size
, GFP_KERNEL
);
2379 if (sbi
->s_group_info
== NULL
) {
2380 printk(KERN_ERR
"EXT4-fs: can't allocate buddy meta group\n");
2383 sbi
->s_buddy_cache
= new_inode(sb
);
2384 if (sbi
->s_buddy_cache
== NULL
) {
2385 printk(KERN_ERR
"EXT4-fs: can't get new inode\n");
2388 EXT4_I(sbi
->s_buddy_cache
)->i_disksize
= 0;
2389 for (i
= 0; i
< ngroups
; i
++) {
2390 desc
= ext4_get_group_desc(sb
, i
, NULL
);
2393 "EXT4-fs: can't read descriptor %u\n", i
);
2396 if (ext4_mb_add_groupinfo(sb
, i
, desc
) != 0)
2403 cachep
= get_groupinfo_cache(sb
->s_blocksize_bits
);
2405 kmem_cache_free(cachep
, ext4_get_group_info(sb
, i
));
2406 i
= num_meta_group_infos
;
2408 kfree(sbi
->s_group_info
[i
]);
2409 iput(sbi
->s_buddy_cache
);
2411 kfree(sbi
->s_group_info
);
2415 int ext4_mb_init(struct super_block
*sb
, int needs_recovery
)
2417 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2423 struct kmem_cache
*cachep
;
2426 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(*sbi
->s_mb_offsets
);
2428 sbi
->s_mb_offsets
= kmalloc(i
, GFP_KERNEL
);
2429 if (sbi
->s_mb_offsets
== NULL
) {
2434 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(*sbi
->s_mb_maxs
);
2435 sbi
->s_mb_maxs
= kmalloc(i
, GFP_KERNEL
);
2436 if (sbi
->s_mb_maxs
== NULL
) {
2441 cache_index
= sb
->s_blocksize_bits
- EXT4_MIN_BLOCK_LOG_SIZE
;
2442 cachep
= ext4_groupinfo_caches
[cache_index
];
2445 int len
= offsetof(struct ext4_group_info
,
2446 bb_counters
[sb
->s_blocksize_bits
+ 2]);
2448 sprintf(name
, "ext4_groupinfo_%d", sb
->s_blocksize_bits
);
2449 namep
= kstrdup(name
, GFP_KERNEL
);
2455 /* Need to free the kmem_cache_name() when we
2456 * destroy the slab */
2457 cachep
= kmem_cache_create(namep
, len
, 0,
2458 SLAB_RECLAIM_ACCOUNT
, NULL
);
2463 ext4_groupinfo_caches
[cache_index
] = cachep
;
2466 /* order 0 is regular bitmap */
2467 sbi
->s_mb_maxs
[0] = sb
->s_blocksize
<< 3;
2468 sbi
->s_mb_offsets
[0] = 0;
2472 max
= sb
->s_blocksize
<< 2;
2474 sbi
->s_mb_offsets
[i
] = offset
;
2475 sbi
->s_mb_maxs
[i
] = max
;
2476 offset
+= 1 << (sb
->s_blocksize_bits
- i
);
2479 } while (i
<= sb
->s_blocksize_bits
+ 1);
2481 /* init file for buddy data */
2482 ret
= ext4_mb_init_backend(sb
);
2487 spin_lock_init(&sbi
->s_md_lock
);
2488 spin_lock_init(&sbi
->s_bal_lock
);
2490 sbi
->s_mb_max_to_scan
= MB_DEFAULT_MAX_TO_SCAN
;
2491 sbi
->s_mb_min_to_scan
= MB_DEFAULT_MIN_TO_SCAN
;
2492 sbi
->s_mb_stats
= MB_DEFAULT_STATS
;
2493 sbi
->s_mb_stream_request
= MB_DEFAULT_STREAM_THRESHOLD
;
2494 sbi
->s_mb_order2_reqs
= MB_DEFAULT_ORDER2_REQS
;
2495 sbi
->s_mb_group_prealloc
= MB_DEFAULT_GROUP_PREALLOC
;
2497 sbi
->s_locality_groups
= alloc_percpu(struct ext4_locality_group
);
2498 if (sbi
->s_locality_groups
== NULL
) {
2502 for_each_possible_cpu(i
) {
2503 struct ext4_locality_group
*lg
;
2504 lg
= per_cpu_ptr(sbi
->s_locality_groups
, i
);
2505 mutex_init(&lg
->lg_mutex
);
2506 for (j
= 0; j
< PREALLOC_TB_SIZE
; j
++)
2507 INIT_LIST_HEAD(&lg
->lg_prealloc_list
[j
]);
2508 spin_lock_init(&lg
->lg_prealloc_lock
);
2512 proc_create_data("mb_groups", S_IRUGO
, sbi
->s_proc
,
2513 &ext4_mb_seq_groups_fops
, sb
);
2516 sbi
->s_journal
->j_commit_callback
= release_blocks_on_commit
;
2519 kfree(sbi
->s_mb_offsets
);
2520 kfree(sbi
->s_mb_maxs
);
2526 /* need to called with the ext4 group lock held */
2527 static void ext4_mb_cleanup_pa(struct ext4_group_info
*grp
)
2529 struct ext4_prealloc_space
*pa
;
2530 struct list_head
*cur
, *tmp
;
2533 list_for_each_safe(cur
, tmp
, &grp
->bb_prealloc_list
) {
2534 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
2535 list_del(&pa
->pa_group_list
);
2537 kmem_cache_free(ext4_pspace_cachep
, pa
);
2540 mb_debug(1, "mballoc: %u PAs left\n", count
);
2544 int ext4_mb_release(struct super_block
*sb
)
2546 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
2548 int num_meta_group_infos
;
2549 struct ext4_group_info
*grinfo
;
2550 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2551 struct kmem_cache
*cachep
= get_groupinfo_cache(sb
->s_blocksize_bits
);
2553 if (sbi
->s_group_info
) {
2554 for (i
= 0; i
< ngroups
; i
++) {
2555 grinfo
= ext4_get_group_info(sb
, i
);
2557 kfree(grinfo
->bb_bitmap
);
2559 ext4_lock_group(sb
, i
);
2560 ext4_mb_cleanup_pa(grinfo
);
2561 ext4_unlock_group(sb
, i
);
2562 kmem_cache_free(cachep
, grinfo
);
2564 num_meta_group_infos
= (ngroups
+
2565 EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2566 EXT4_DESC_PER_BLOCK_BITS(sb
);
2567 for (i
= 0; i
< num_meta_group_infos
; i
++)
2568 kfree(sbi
->s_group_info
[i
]);
2569 kfree(sbi
->s_group_info
);
2571 kfree(sbi
->s_mb_offsets
);
2572 kfree(sbi
->s_mb_maxs
);
2573 if (sbi
->s_buddy_cache
)
2574 iput(sbi
->s_buddy_cache
);
2575 if (sbi
->s_mb_stats
) {
2577 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2578 atomic_read(&sbi
->s_bal_allocated
),
2579 atomic_read(&sbi
->s_bal_reqs
),
2580 atomic_read(&sbi
->s_bal_success
));
2582 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2583 "%u 2^N hits, %u breaks, %u lost\n",
2584 atomic_read(&sbi
->s_bal_ex_scanned
),
2585 atomic_read(&sbi
->s_bal_goals
),
2586 atomic_read(&sbi
->s_bal_2orders
),
2587 atomic_read(&sbi
->s_bal_breaks
),
2588 atomic_read(&sbi
->s_mb_lost_chunks
));
2590 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2591 sbi
->s_mb_buddies_generated
++,
2592 sbi
->s_mb_generation_time
);
2594 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2595 atomic_read(&sbi
->s_mb_preallocated
),
2596 atomic_read(&sbi
->s_mb_discarded
));
2599 free_percpu(sbi
->s_locality_groups
);
2601 remove_proc_entry("mb_groups", sbi
->s_proc
);
2606 static inline void ext4_issue_discard(struct super_block
*sb
,
2607 ext4_group_t block_group
, ext4_grpblk_t block
, int count
)
2610 ext4_fsblk_t discard_block
;
2612 discard_block
= block
+ ext4_group_first_block_no(sb
, block_group
);
2613 trace_ext4_discard_blocks(sb
,
2614 (unsigned long long) discard_block
, count
);
2615 ret
= sb_issue_discard(sb
, discard_block
, count
);
2616 if (ret
== -EOPNOTSUPP
) {
2617 ext4_warning(sb
, "discard not supported, disabling");
2618 clear_opt(EXT4_SB(sb
)->s_mount_opt
, DISCARD
);
2623 * This function is called by the jbd2 layer once the commit has finished,
2624 * so we know we can free the blocks that were released with that commit.
2626 static void release_blocks_on_commit(journal_t
*journal
, transaction_t
*txn
)
2628 struct super_block
*sb
= journal
->j_private
;
2629 struct ext4_buddy e4b
;
2630 struct ext4_group_info
*db
;
2631 int err
, count
= 0, count2
= 0;
2632 struct ext4_free_data
*entry
;
2633 struct list_head
*l
, *ltmp
;
2635 list_for_each_safe(l
, ltmp
, &txn
->t_private_list
) {
2636 entry
= list_entry(l
, struct ext4_free_data
, list
);
2638 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
2639 entry
->count
, entry
->group
, entry
);
2641 if (test_opt(sb
, DISCARD
))
2642 ext4_issue_discard(sb
, entry
->group
,
2643 entry
->start_blk
, entry
->count
);
2645 err
= ext4_mb_load_buddy(sb
, entry
->group
, &e4b
);
2646 /* we expect to find existing buddy because it's pinned */
2650 /* there are blocks to put in buddy to make them really free */
2651 count
+= entry
->count
;
2653 ext4_lock_group(sb
, entry
->group
);
2654 /* Take it out of per group rb tree */
2655 rb_erase(&entry
->node
, &(db
->bb_free_root
));
2656 mb_free_blocks(NULL
, &e4b
, entry
->start_blk
, entry
->count
);
2658 if (!db
->bb_free_root
.rb_node
) {
2659 /* No more items in the per group rb tree
2660 * balance refcounts from ext4_mb_free_metadata()
2662 page_cache_release(e4b
.bd_buddy_page
);
2663 page_cache_release(e4b
.bd_bitmap_page
);
2665 ext4_unlock_group(sb
, entry
->group
);
2666 kmem_cache_free(ext4_free_ext_cachep
, entry
);
2667 ext4_mb_unload_buddy(&e4b
);
2670 mb_debug(1, "freed %u blocks in %u structures\n", count
, count2
);
2673 #ifdef CONFIG_EXT4_DEBUG
2674 u8 mb_enable_debug __read_mostly
;
2676 static struct dentry
*debugfs_dir
;
2677 static struct dentry
*debugfs_debug
;
2679 static void __init
ext4_create_debugfs_entry(void)
2681 debugfs_dir
= debugfs_create_dir("ext4", NULL
);
2683 debugfs_debug
= debugfs_create_u8("mballoc-debug",
2689 static void ext4_remove_debugfs_entry(void)
2691 debugfs_remove(debugfs_debug
);
2692 debugfs_remove(debugfs_dir
);
2697 static void __init
ext4_create_debugfs_entry(void)
2701 static void ext4_remove_debugfs_entry(void)
2707 int __init
init_ext4_mballoc(void)
2709 ext4_pspace_cachep
= KMEM_CACHE(ext4_prealloc_space
,
2710 SLAB_RECLAIM_ACCOUNT
);
2711 if (ext4_pspace_cachep
== NULL
)
2714 ext4_ac_cachep
= KMEM_CACHE(ext4_allocation_context
,
2715 SLAB_RECLAIM_ACCOUNT
);
2716 if (ext4_ac_cachep
== NULL
) {
2717 kmem_cache_destroy(ext4_pspace_cachep
);
2721 ext4_free_ext_cachep
= KMEM_CACHE(ext4_free_data
,
2722 SLAB_RECLAIM_ACCOUNT
);
2723 if (ext4_free_ext_cachep
== NULL
) {
2724 kmem_cache_destroy(ext4_pspace_cachep
);
2725 kmem_cache_destroy(ext4_ac_cachep
);
2728 ext4_create_debugfs_entry();
2732 void exit_ext4_mballoc(void)
2736 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2737 * before destroying the slab cache.
2740 kmem_cache_destroy(ext4_pspace_cachep
);
2741 kmem_cache_destroy(ext4_ac_cachep
);
2742 kmem_cache_destroy(ext4_free_ext_cachep
);
2744 for (i
= 0; i
< NR_GRPINFO_CACHES
; i
++) {
2745 struct kmem_cache
*cachep
= ext4_groupinfo_caches
[i
];
2747 char *name
= (char *)kmem_cache_name(cachep
);
2748 kmem_cache_destroy(cachep
);
2752 ext4_remove_debugfs_entry();
2757 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
2758 * Returns 0 if success or error code
2760 static noinline_for_stack
int
2761 ext4_mb_mark_diskspace_used(struct ext4_allocation_context
*ac
,
2762 handle_t
*handle
, unsigned int reserv_blks
)
2764 struct buffer_head
*bitmap_bh
= NULL
;
2765 struct ext4_group_desc
*gdp
;
2766 struct buffer_head
*gdp_bh
;
2767 struct ext4_sb_info
*sbi
;
2768 struct super_block
*sb
;
2772 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
2773 BUG_ON(ac
->ac_b_ex
.fe_len
<= 0);
2779 bitmap_bh
= ext4_read_block_bitmap(sb
, ac
->ac_b_ex
.fe_group
);
2783 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
2788 gdp
= ext4_get_group_desc(sb
, ac
->ac_b_ex
.fe_group
, &gdp_bh
);
2792 ext4_debug("using block group %u(%d)\n", ac
->ac_b_ex
.fe_group
,
2793 ext4_free_blks_count(sb
, gdp
));
2795 err
= ext4_journal_get_write_access(handle
, gdp_bh
);
2799 block
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
2801 len
= ac
->ac_b_ex
.fe_len
;
2802 if (!ext4_data_block_valid(sbi
, block
, len
)) {
2803 ext4_error(sb
, "Allocating blocks %llu-%llu which overlap "
2804 "fs metadata\n", block
, block
+len
);
2805 /* File system mounted not to panic on error
2806 * Fix the bitmap and repeat the block allocation
2807 * We leak some of the blocks here.
2809 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
2810 mb_set_bits(bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,
2811 ac
->ac_b_ex
.fe_len
);
2812 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
2813 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
2819 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
2820 #ifdef AGGRESSIVE_CHECK
2823 for (i
= 0; i
< ac
->ac_b_ex
.fe_len
; i
++) {
2824 BUG_ON(mb_test_bit(ac
->ac_b_ex
.fe_start
+ i
,
2825 bitmap_bh
->b_data
));
2829 mb_set_bits(bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,ac
->ac_b_ex
.fe_len
);
2830 if (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2831 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT
);
2832 ext4_free_blks_set(sb
, gdp
,
2833 ext4_free_blocks_after_init(sb
,
2834 ac
->ac_b_ex
.fe_group
, gdp
));
2836 len
= ext4_free_blks_count(sb
, gdp
) - ac
->ac_b_ex
.fe_len
;
2837 ext4_free_blks_set(sb
, gdp
, len
);
2838 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, ac
->ac_b_ex
.fe_group
, gdp
);
2840 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
2841 percpu_counter_sub(&sbi
->s_freeblocks_counter
, ac
->ac_b_ex
.fe_len
);
2843 * Now reduce the dirty block count also. Should not go negative
2845 if (!(ac
->ac_flags
& EXT4_MB_DELALLOC_RESERVED
))
2846 /* release all the reserved blocks if non delalloc */
2847 percpu_counter_sub(&sbi
->s_dirtyblocks_counter
, reserv_blks
);
2849 if (sbi
->s_log_groups_per_flex
) {
2850 ext4_group_t flex_group
= ext4_flex_group(sbi
,
2851 ac
->ac_b_ex
.fe_group
);
2852 atomic_sub(ac
->ac_b_ex
.fe_len
,
2853 &sbi
->s_flex_groups
[flex_group
].free_blocks
);
2856 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
2859 err
= ext4_handle_dirty_metadata(handle
, NULL
, gdp_bh
);
2862 ext4_mark_super_dirty(sb
);
2868 * here we normalize request for locality group
2869 * Group request are normalized to s_strip size if we set the same via mount
2870 * option. If not we set it to s_mb_group_prealloc which can be configured via
2871 * /sys/fs/ext4/<partition>/mb_group_prealloc
2873 * XXX: should we try to preallocate more than the group has now?
2875 static void ext4_mb_normalize_group_request(struct ext4_allocation_context
*ac
)
2877 struct super_block
*sb
= ac
->ac_sb
;
2878 struct ext4_locality_group
*lg
= ac
->ac_lg
;
2881 if (EXT4_SB(sb
)->s_stripe
)
2882 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_stripe
;
2884 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_mb_group_prealloc
;
2885 mb_debug(1, "#%u: goal %u blocks for locality group\n",
2886 current
->pid
, ac
->ac_g_ex
.fe_len
);
2890 * Normalization means making request better in terms of
2891 * size and alignment
2893 static noinline_for_stack
void
2894 ext4_mb_normalize_request(struct ext4_allocation_context
*ac
,
2895 struct ext4_allocation_request
*ar
)
2899 loff_t size
, orig_size
, start_off
;
2901 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
2902 struct ext4_prealloc_space
*pa
;
2904 /* do normalize only data requests, metadata requests
2905 do not need preallocation */
2906 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
2909 /* sometime caller may want exact blocks */
2910 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
2913 /* caller may indicate that preallocation isn't
2914 * required (it's a tail, for example) */
2915 if (ac
->ac_flags
& EXT4_MB_HINT_NOPREALLOC
)
2918 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
) {
2919 ext4_mb_normalize_group_request(ac
);
2923 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
2925 /* first, let's learn actual file size
2926 * given current request is allocated */
2927 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
2928 size
= size
<< bsbits
;
2929 if (size
< i_size_read(ac
->ac_inode
))
2930 size
= i_size_read(ac
->ac_inode
);
2933 /* max size of free chunks */
2936 #define NRL_CHECK_SIZE(req, size, max, chunk_size) \
2937 (req <= (size) || max <= (chunk_size))
2939 /* first, try to predict filesize */
2940 /* XXX: should this table be tunable? */
2942 if (size
<= 16 * 1024) {
2944 } else if (size
<= 32 * 1024) {
2946 } else if (size
<= 64 * 1024) {
2948 } else if (size
<= 128 * 1024) {
2950 } else if (size
<= 256 * 1024) {
2952 } else if (size
<= 512 * 1024) {
2954 } else if (size
<= 1024 * 1024) {
2956 } else if (NRL_CHECK_SIZE(size
, 4 * 1024 * 1024, max
, 2 * 1024)) {
2957 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
2958 (21 - bsbits
)) << 21;
2959 size
= 2 * 1024 * 1024;
2960 } else if (NRL_CHECK_SIZE(size
, 8 * 1024 * 1024, max
, 4 * 1024)) {
2961 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
2962 (22 - bsbits
)) << 22;
2963 size
= 4 * 1024 * 1024;
2964 } else if (NRL_CHECK_SIZE(ac
->ac_o_ex
.fe_len
,
2965 (8<<20)>>bsbits
, max
, 8 * 1024)) {
2966 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
2967 (23 - bsbits
)) << 23;
2968 size
= 8 * 1024 * 1024;
2970 start_off
= (loff_t
)ac
->ac_o_ex
.fe_logical
<< bsbits
;
2971 size
= ac
->ac_o_ex
.fe_len
<< bsbits
;
2973 size
= size
>> bsbits
;
2974 start
= start_off
>> bsbits
;
2976 /* don't cover already allocated blocks in selected range */
2977 if (ar
->pleft
&& start
<= ar
->lleft
) {
2978 size
-= ar
->lleft
+ 1 - start
;
2979 start
= ar
->lleft
+ 1;
2981 if (ar
->pright
&& start
+ size
- 1 >= ar
->lright
)
2982 size
-= start
+ size
- ar
->lright
;
2986 /* check we don't cross already preallocated blocks */
2988 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
2993 spin_lock(&pa
->pa_lock
);
2994 if (pa
->pa_deleted
) {
2995 spin_unlock(&pa
->pa_lock
);
2999 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
3001 /* PA must not overlap original request */
3002 BUG_ON(!(ac
->ac_o_ex
.fe_logical
>= pa_end
||
3003 ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
));
3005 /* skip PAs this normalized request doesn't overlap with */
3006 if (pa
->pa_lstart
>= end
|| pa_end
<= start
) {
3007 spin_unlock(&pa
->pa_lock
);
3010 BUG_ON(pa
->pa_lstart
<= start
&& pa_end
>= end
);
3012 /* adjust start or end to be adjacent to this pa */
3013 if (pa_end
<= ac
->ac_o_ex
.fe_logical
) {
3014 BUG_ON(pa_end
< start
);
3016 } else if (pa
->pa_lstart
> ac
->ac_o_ex
.fe_logical
) {
3017 BUG_ON(pa
->pa_lstart
> end
);
3018 end
= pa
->pa_lstart
;
3020 spin_unlock(&pa
->pa_lock
);
3025 /* XXX: extra loop to check we really don't overlap preallocations */
3027 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3029 spin_lock(&pa
->pa_lock
);
3030 if (pa
->pa_deleted
== 0) {
3031 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
3032 BUG_ON(!(start
>= pa_end
|| end
<= pa
->pa_lstart
));
3034 spin_unlock(&pa
->pa_lock
);
3038 if (start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3039 start
> ac
->ac_o_ex
.fe_logical
) {
3040 printk(KERN_ERR
"start %lu, size %lu, fe_logical %lu\n",
3041 (unsigned long) start
, (unsigned long) size
,
3042 (unsigned long) ac
->ac_o_ex
.fe_logical
);
3044 BUG_ON(start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3045 start
> ac
->ac_o_ex
.fe_logical
);
3046 BUG_ON(size
<= 0 || size
> EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
3048 /* now prepare goal request */
3050 /* XXX: is it better to align blocks WRT to logical
3051 * placement or satisfy big request as is */
3052 ac
->ac_g_ex
.fe_logical
= start
;
3053 ac
->ac_g_ex
.fe_len
= size
;
3055 /* define goal start in order to merge */
3056 if (ar
->pright
&& (ar
->lright
== (start
+ size
))) {
3057 /* merge to the right */
3058 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pright
- size
,
3059 &ac
->ac_f_ex
.fe_group
,
3060 &ac
->ac_f_ex
.fe_start
);
3061 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3063 if (ar
->pleft
&& (ar
->lleft
+ 1 == start
)) {
3064 /* merge to the left */
3065 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pleft
+ 1,
3066 &ac
->ac_f_ex
.fe_group
,
3067 &ac
->ac_f_ex
.fe_start
);
3068 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3071 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size
,
3072 (unsigned) orig_size
, (unsigned) start
);
3075 static void ext4_mb_collect_stats(struct ext4_allocation_context
*ac
)
3077 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3079 if (sbi
->s_mb_stats
&& ac
->ac_g_ex
.fe_len
> 1) {
3080 atomic_inc(&sbi
->s_bal_reqs
);
3081 atomic_add(ac
->ac_b_ex
.fe_len
, &sbi
->s_bal_allocated
);
3082 if (ac
->ac_b_ex
.fe_len
>= ac
->ac_o_ex
.fe_len
)
3083 atomic_inc(&sbi
->s_bal_success
);
3084 atomic_add(ac
->ac_found
, &sbi
->s_bal_ex_scanned
);
3085 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
3086 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
3087 atomic_inc(&sbi
->s_bal_goals
);
3088 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
)
3089 atomic_inc(&sbi
->s_bal_breaks
);
3092 if (ac
->ac_op
== EXT4_MB_HISTORY_ALLOC
)
3093 trace_ext4_mballoc_alloc(ac
);
3095 trace_ext4_mballoc_prealloc(ac
);
3099 * Called on failure; free up any blocks from the inode PA for this
3100 * context. We don't need this for MB_GROUP_PA because we only change
3101 * pa_free in ext4_mb_release_context(), but on failure, we've already
3102 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3104 static void ext4_discard_allocated_blocks(struct ext4_allocation_context
*ac
)
3106 struct ext4_prealloc_space
*pa
= ac
->ac_pa
;
3109 if (pa
&& pa
->pa_type
== MB_INODE_PA
) {
3110 len
= ac
->ac_b_ex
.fe_len
;
3117 * use blocks preallocated to inode
3119 static void ext4_mb_use_inode_pa(struct ext4_allocation_context
*ac
,
3120 struct ext4_prealloc_space
*pa
)
3126 /* found preallocated blocks, use them */
3127 start
= pa
->pa_pstart
+ (ac
->ac_o_ex
.fe_logical
- pa
->pa_lstart
);
3128 end
= min(pa
->pa_pstart
+ pa
->pa_len
, start
+ ac
->ac_o_ex
.fe_len
);
3130 ext4_get_group_no_and_offset(ac
->ac_sb
, start
, &ac
->ac_b_ex
.fe_group
,
3131 &ac
->ac_b_ex
.fe_start
);
3132 ac
->ac_b_ex
.fe_len
= len
;
3133 ac
->ac_status
= AC_STATUS_FOUND
;
3136 BUG_ON(start
< pa
->pa_pstart
);
3137 BUG_ON(start
+ len
> pa
->pa_pstart
+ pa
->pa_len
);
3138 BUG_ON(pa
->pa_free
< len
);
3141 mb_debug(1, "use %llu/%u from inode pa %p\n", start
, len
, pa
);
3145 * use blocks preallocated to locality group
3147 static void ext4_mb_use_group_pa(struct ext4_allocation_context
*ac
,
3148 struct ext4_prealloc_space
*pa
)
3150 unsigned int len
= ac
->ac_o_ex
.fe_len
;
3152 ext4_get_group_no_and_offset(ac
->ac_sb
, pa
->pa_pstart
,
3153 &ac
->ac_b_ex
.fe_group
,
3154 &ac
->ac_b_ex
.fe_start
);
3155 ac
->ac_b_ex
.fe_len
= len
;
3156 ac
->ac_status
= AC_STATUS_FOUND
;
3159 /* we don't correct pa_pstart or pa_plen here to avoid
3160 * possible race when the group is being loaded concurrently
3161 * instead we correct pa later, after blocks are marked
3162 * in on-disk bitmap -- see ext4_mb_release_context()
3163 * Other CPUs are prevented from allocating from this pa by lg_mutex
3165 mb_debug(1, "use %u/%u from group pa %p\n", pa
->pa_lstart
-len
, len
, pa
);
3169 * Return the prealloc space that have minimal distance
3170 * from the goal block. @cpa is the prealloc
3171 * space that is having currently known minimal distance
3172 * from the goal block.
3174 static struct ext4_prealloc_space
*
3175 ext4_mb_check_group_pa(ext4_fsblk_t goal_block
,
3176 struct ext4_prealloc_space
*pa
,
3177 struct ext4_prealloc_space
*cpa
)
3179 ext4_fsblk_t cur_distance
, new_distance
;
3182 atomic_inc(&pa
->pa_count
);
3185 cur_distance
= abs(goal_block
- cpa
->pa_pstart
);
3186 new_distance
= abs(goal_block
- pa
->pa_pstart
);
3188 if (cur_distance
< new_distance
)
3191 /* drop the previous reference */
3192 atomic_dec(&cpa
->pa_count
);
3193 atomic_inc(&pa
->pa_count
);
3198 * search goal blocks in preallocated space
3200 static noinline_for_stack
int
3201 ext4_mb_use_preallocated(struct ext4_allocation_context
*ac
)
3204 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3205 struct ext4_locality_group
*lg
;
3206 struct ext4_prealloc_space
*pa
, *cpa
= NULL
;
3207 ext4_fsblk_t goal_block
;
3209 /* only data can be preallocated */
3210 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3213 /* first, try per-file preallocation */
3215 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3217 /* all fields in this condition don't change,
3218 * so we can skip locking for them */
3219 if (ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
||
3220 ac
->ac_o_ex
.fe_logical
>= pa
->pa_lstart
+ pa
->pa_len
)
3223 /* non-extent files can't have physical blocks past 2^32 */
3224 if (!(ext4_test_inode_flag(ac
->ac_inode
, EXT4_INODE_EXTENTS
)) &&
3225 pa
->pa_pstart
+ pa
->pa_len
> EXT4_MAX_BLOCK_FILE_PHYS
)
3228 /* found preallocated blocks, use them */
3229 spin_lock(&pa
->pa_lock
);
3230 if (pa
->pa_deleted
== 0 && pa
->pa_free
) {
3231 atomic_inc(&pa
->pa_count
);
3232 ext4_mb_use_inode_pa(ac
, pa
);
3233 spin_unlock(&pa
->pa_lock
);
3234 ac
->ac_criteria
= 10;
3238 spin_unlock(&pa
->pa_lock
);
3242 /* can we use group allocation? */
3243 if (!(ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
))
3246 /* inode may have no locality group for some reason */
3250 order
= fls(ac
->ac_o_ex
.fe_len
) - 1;
3251 if (order
> PREALLOC_TB_SIZE
- 1)
3252 /* The max size of hash table is PREALLOC_TB_SIZE */
3253 order
= PREALLOC_TB_SIZE
- 1;
3255 goal_block
= ext4_grp_offs_to_block(ac
->ac_sb
, &ac
->ac_g_ex
);
3257 * search for the prealloc space that is having
3258 * minimal distance from the goal block.
3260 for (i
= order
; i
< PREALLOC_TB_SIZE
; i
++) {
3262 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[i
],
3264 spin_lock(&pa
->pa_lock
);
3265 if (pa
->pa_deleted
== 0 &&
3266 pa
->pa_free
>= ac
->ac_o_ex
.fe_len
) {
3268 cpa
= ext4_mb_check_group_pa(goal_block
,
3271 spin_unlock(&pa
->pa_lock
);
3276 ext4_mb_use_group_pa(ac
, cpa
);
3277 ac
->ac_criteria
= 20;
3284 * the function goes through all block freed in the group
3285 * but not yet committed and marks them used in in-core bitmap.
3286 * buddy must be generated from this bitmap
3287 * Need to be called with the ext4 group lock held
3289 static void ext4_mb_generate_from_freelist(struct super_block
*sb
, void *bitmap
,
3293 struct ext4_group_info
*grp
;
3294 struct ext4_free_data
*entry
;
3296 grp
= ext4_get_group_info(sb
, group
);
3297 n
= rb_first(&(grp
->bb_free_root
));
3300 entry
= rb_entry(n
, struct ext4_free_data
, node
);
3301 mb_set_bits(bitmap
, entry
->start_blk
, entry
->count
);
3308 * the function goes through all preallocation in this group and marks them
3309 * used in in-core bitmap. buddy must be generated from this bitmap
3310 * Need to be called with ext4 group lock held
3312 static noinline_for_stack
3313 void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
3316 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3317 struct ext4_prealloc_space
*pa
;
3318 struct list_head
*cur
;
3319 ext4_group_t groupnr
;
3320 ext4_grpblk_t start
;
3321 int preallocated
= 0;
3325 /* all form of preallocation discards first load group,
3326 * so the only competing code is preallocation use.
3327 * we don't need any locking here
3328 * notice we do NOT ignore preallocations with pa_deleted
3329 * otherwise we could leave used blocks available for
3330 * allocation in buddy when concurrent ext4_mb_put_pa()
3331 * is dropping preallocation
3333 list_for_each(cur
, &grp
->bb_prealloc_list
) {
3334 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
3335 spin_lock(&pa
->pa_lock
);
3336 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
3339 spin_unlock(&pa
->pa_lock
);
3340 if (unlikely(len
== 0))
3342 BUG_ON(groupnr
!= group
);
3343 mb_set_bits(bitmap
, start
, len
);
3344 preallocated
+= len
;
3347 mb_debug(1, "prellocated %u for group %u\n", preallocated
, group
);
3350 static void ext4_mb_pa_callback(struct rcu_head
*head
)
3352 struct ext4_prealloc_space
*pa
;
3353 pa
= container_of(head
, struct ext4_prealloc_space
, u
.pa_rcu
);
3354 kmem_cache_free(ext4_pspace_cachep
, pa
);
3358 * drops a reference to preallocated space descriptor
3359 * if this was the last reference and the space is consumed
3361 static void ext4_mb_put_pa(struct ext4_allocation_context
*ac
,
3362 struct super_block
*sb
, struct ext4_prealloc_space
*pa
)
3365 ext4_fsblk_t grp_blk
;
3367 if (!atomic_dec_and_test(&pa
->pa_count
) || pa
->pa_free
!= 0)
3370 /* in this short window concurrent discard can set pa_deleted */
3371 spin_lock(&pa
->pa_lock
);
3372 if (pa
->pa_deleted
== 1) {
3373 spin_unlock(&pa
->pa_lock
);
3378 spin_unlock(&pa
->pa_lock
);
3380 grp_blk
= pa
->pa_pstart
;
3382 * If doing group-based preallocation, pa_pstart may be in the
3383 * next group when pa is used up
3385 if (pa
->pa_type
== MB_GROUP_PA
)
3388 ext4_get_group_no_and_offset(sb
, grp_blk
, &grp
, NULL
);
3393 * P1 (buddy init) P2 (regular allocation)
3394 * find block B in PA
3395 * copy on-disk bitmap to buddy
3396 * mark B in on-disk bitmap
3397 * drop PA from group
3398 * mark all PAs in buddy
3400 * thus, P1 initializes buddy with B available. to prevent this
3401 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3404 ext4_lock_group(sb
, grp
);
3405 list_del(&pa
->pa_group_list
);
3406 ext4_unlock_group(sb
, grp
);
3408 spin_lock(pa
->pa_obj_lock
);
3409 list_del_rcu(&pa
->pa_inode_list
);
3410 spin_unlock(pa
->pa_obj_lock
);
3412 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3416 * creates new preallocated space for given inode
3418 static noinline_for_stack
int
3419 ext4_mb_new_inode_pa(struct ext4_allocation_context
*ac
)
3421 struct super_block
*sb
= ac
->ac_sb
;
3422 struct ext4_prealloc_space
*pa
;
3423 struct ext4_group_info
*grp
;
3424 struct ext4_inode_info
*ei
;
3426 /* preallocate only when found space is larger then requested */
3427 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3428 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3429 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3431 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3435 if (ac
->ac_b_ex
.fe_len
< ac
->ac_g_ex
.fe_len
) {
3441 /* we can't allocate as much as normalizer wants.
3442 * so, found space must get proper lstart
3443 * to cover original request */
3444 BUG_ON(ac
->ac_g_ex
.fe_logical
> ac
->ac_o_ex
.fe_logical
);
3445 BUG_ON(ac
->ac_g_ex
.fe_len
< ac
->ac_o_ex
.fe_len
);
3447 /* we're limited by original request in that
3448 * logical block must be covered any way
3449 * winl is window we can move our chunk within */
3450 winl
= ac
->ac_o_ex
.fe_logical
- ac
->ac_g_ex
.fe_logical
;
3452 /* also, we should cover whole original request */
3453 wins
= ac
->ac_b_ex
.fe_len
- ac
->ac_o_ex
.fe_len
;
3455 /* the smallest one defines real window */
3456 win
= min(winl
, wins
);
3458 offs
= ac
->ac_o_ex
.fe_logical
% ac
->ac_b_ex
.fe_len
;
3459 if (offs
&& offs
< win
)
3462 ac
->ac_b_ex
.fe_logical
= ac
->ac_o_ex
.fe_logical
- win
;
3463 BUG_ON(ac
->ac_o_ex
.fe_logical
< ac
->ac_b_ex
.fe_logical
);
3464 BUG_ON(ac
->ac_o_ex
.fe_len
> ac
->ac_b_ex
.fe_len
);
3467 /* preallocation can change ac_b_ex, thus we store actually
3468 * allocated blocks for history */
3469 ac
->ac_f_ex
= ac
->ac_b_ex
;
3471 pa
->pa_lstart
= ac
->ac_b_ex
.fe_logical
;
3472 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3473 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3474 pa
->pa_free
= pa
->pa_len
;
3475 atomic_set(&pa
->pa_count
, 1);
3476 spin_lock_init(&pa
->pa_lock
);
3477 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3478 INIT_LIST_HEAD(&pa
->pa_group_list
);
3480 pa
->pa_type
= MB_INODE_PA
;
3482 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa
,
3483 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3484 trace_ext4_mb_new_inode_pa(ac
, pa
);
3486 ext4_mb_use_inode_pa(ac
, pa
);
3487 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3489 ei
= EXT4_I(ac
->ac_inode
);
3490 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3492 pa
->pa_obj_lock
= &ei
->i_prealloc_lock
;
3493 pa
->pa_inode
= ac
->ac_inode
;
3495 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3496 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3497 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3499 spin_lock(pa
->pa_obj_lock
);
3500 list_add_rcu(&pa
->pa_inode_list
, &ei
->i_prealloc_list
);
3501 spin_unlock(pa
->pa_obj_lock
);
3507 * creates new preallocated space for locality group inodes belongs to
3509 static noinline_for_stack
int
3510 ext4_mb_new_group_pa(struct ext4_allocation_context
*ac
)
3512 struct super_block
*sb
= ac
->ac_sb
;
3513 struct ext4_locality_group
*lg
;
3514 struct ext4_prealloc_space
*pa
;
3515 struct ext4_group_info
*grp
;
3517 /* preallocate only when found space is larger then requested */
3518 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3519 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3520 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3522 BUG_ON(ext4_pspace_cachep
== NULL
);
3523 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3527 /* preallocation can change ac_b_ex, thus we store actually
3528 * allocated blocks for history */
3529 ac
->ac_f_ex
= ac
->ac_b_ex
;
3531 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3532 pa
->pa_lstart
= pa
->pa_pstart
;
3533 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3534 pa
->pa_free
= pa
->pa_len
;
3535 atomic_set(&pa
->pa_count
, 1);
3536 spin_lock_init(&pa
->pa_lock
);
3537 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3538 INIT_LIST_HEAD(&pa
->pa_group_list
);
3540 pa
->pa_type
= MB_GROUP_PA
;
3542 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa
,
3543 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3544 trace_ext4_mb_new_group_pa(ac
, pa
);
3546 ext4_mb_use_group_pa(ac
, pa
);
3547 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3549 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3553 pa
->pa_obj_lock
= &lg
->lg_prealloc_lock
;
3554 pa
->pa_inode
= NULL
;
3556 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3557 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3558 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3561 * We will later add the new pa to the right bucket
3562 * after updating the pa_free in ext4_mb_release_context
3567 static int ext4_mb_new_preallocation(struct ext4_allocation_context
*ac
)
3571 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
3572 err
= ext4_mb_new_group_pa(ac
);
3574 err
= ext4_mb_new_inode_pa(ac
);
3579 * finds all unused blocks in on-disk bitmap, frees them in
3580 * in-core bitmap and buddy.
3581 * @pa must be unlinked from inode and group lists, so that
3582 * nobody else can find/use it.
3583 * the caller MUST hold group/inode locks.
3584 * TODO: optimize the case when there are no in-core structures yet
3586 static noinline_for_stack
int
3587 ext4_mb_release_inode_pa(struct ext4_buddy
*e4b
, struct buffer_head
*bitmap_bh
,
3588 struct ext4_prealloc_space
*pa
)
3590 struct super_block
*sb
= e4b
->bd_sb
;
3591 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3596 unsigned long long grp_blk_start
;
3600 BUG_ON(pa
->pa_deleted
== 0);
3601 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3602 grp_blk_start
= pa
->pa_pstart
- bit
;
3603 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3604 end
= bit
+ pa
->pa_len
;
3607 bit
= mb_find_next_zero_bit(bitmap_bh
->b_data
, end
, bit
);
3610 next
= mb_find_next_bit(bitmap_bh
->b_data
, end
, bit
);
3611 mb_debug(1, " free preallocated %u/%u in group %u\n",
3612 (unsigned) ext4_group_first_block_no(sb
, group
) + bit
,
3613 (unsigned) next
- bit
, (unsigned) group
);
3616 trace_ext4_mballoc_discard(sb
, NULL
, group
, bit
, next
- bit
);
3617 trace_ext4_mb_release_inode_pa(sb
, pa
->pa_inode
, pa
,
3618 grp_blk_start
+ bit
, next
- bit
);
3619 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, next
- bit
);
3622 if (free
!= pa
->pa_free
) {
3623 printk(KERN_CRIT
"pa %p: logic %lu, phys. %lu, len %lu\n",
3624 pa
, (unsigned long) pa
->pa_lstart
,
3625 (unsigned long) pa
->pa_pstart
,
3626 (unsigned long) pa
->pa_len
);
3627 ext4_grp_locked_error(sb
, group
, 0, 0, "free %u, pa_free %u",
3630 * pa is already deleted so we use the value obtained
3631 * from the bitmap and continue.
3634 atomic_add(free
, &sbi
->s_mb_discarded
);
3639 static noinline_for_stack
int
3640 ext4_mb_release_group_pa(struct ext4_buddy
*e4b
,
3641 struct ext4_prealloc_space
*pa
)
3643 struct super_block
*sb
= e4b
->bd_sb
;
3647 trace_ext4_mb_release_group_pa(sb
, pa
);
3648 BUG_ON(pa
->pa_deleted
== 0);
3649 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3650 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3651 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, pa
->pa_len
);
3652 atomic_add(pa
->pa_len
, &EXT4_SB(sb
)->s_mb_discarded
);
3653 trace_ext4_mballoc_discard(sb
, NULL
, group
, bit
, pa
->pa_len
);
3659 * releases all preallocations in given group
3661 * first, we need to decide discard policy:
3662 * - when do we discard
3664 * - how many do we discard
3665 * 1) how many requested
3667 static noinline_for_stack
int
3668 ext4_mb_discard_group_preallocations(struct super_block
*sb
,
3669 ext4_group_t group
, int needed
)
3671 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3672 struct buffer_head
*bitmap_bh
= NULL
;
3673 struct ext4_prealloc_space
*pa
, *tmp
;
3674 struct list_head list
;
3675 struct ext4_buddy e4b
;
3680 mb_debug(1, "discard preallocation for group %u\n", group
);
3682 if (list_empty(&grp
->bb_prealloc_list
))
3685 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
3686 if (bitmap_bh
== NULL
) {
3687 ext4_error(sb
, "Error reading block bitmap for %u", group
);
3691 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
3693 ext4_error(sb
, "Error loading buddy information for %u", group
);
3699 needed
= EXT4_BLOCKS_PER_GROUP(sb
) + 1;
3701 INIT_LIST_HEAD(&list
);
3703 ext4_lock_group(sb
, group
);
3704 list_for_each_entry_safe(pa
, tmp
,
3705 &grp
->bb_prealloc_list
, pa_group_list
) {
3706 spin_lock(&pa
->pa_lock
);
3707 if (atomic_read(&pa
->pa_count
)) {
3708 spin_unlock(&pa
->pa_lock
);
3712 if (pa
->pa_deleted
) {
3713 spin_unlock(&pa
->pa_lock
);
3717 /* seems this one can be freed ... */
3720 /* we can trust pa_free ... */
3721 free
+= pa
->pa_free
;
3723 spin_unlock(&pa
->pa_lock
);
3725 list_del(&pa
->pa_group_list
);
3726 list_add(&pa
->u
.pa_tmp_list
, &list
);
3729 /* if we still need more blocks and some PAs were used, try again */
3730 if (free
< needed
&& busy
) {
3732 ext4_unlock_group(sb
, group
);
3734 * Yield the CPU here so that we don't get soft lockup
3735 * in non preempt case.
3741 /* found anything to free? */
3742 if (list_empty(&list
)) {
3747 /* now free all selected PAs */
3748 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
3750 /* remove from object (inode or locality group) */
3751 spin_lock(pa
->pa_obj_lock
);
3752 list_del_rcu(&pa
->pa_inode_list
);
3753 spin_unlock(pa
->pa_obj_lock
);
3755 if (pa
->pa_type
== MB_GROUP_PA
)
3756 ext4_mb_release_group_pa(&e4b
, pa
);
3758 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
);
3760 list_del(&pa
->u
.pa_tmp_list
);
3761 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3765 ext4_unlock_group(sb
, group
);
3766 ext4_mb_unload_buddy(&e4b
);
3772 * releases all non-used preallocated blocks for given inode
3774 * It's important to discard preallocations under i_data_sem
3775 * We don't want another block to be served from the prealloc
3776 * space when we are discarding the inode prealloc space.
3778 * FIXME!! Make sure it is valid at all the call sites
3780 void ext4_discard_preallocations(struct inode
*inode
)
3782 struct ext4_inode_info
*ei
= EXT4_I(inode
);
3783 struct super_block
*sb
= inode
->i_sb
;
3784 struct buffer_head
*bitmap_bh
= NULL
;
3785 struct ext4_prealloc_space
*pa
, *tmp
;
3786 ext4_group_t group
= 0;
3787 struct list_head list
;
3788 struct ext4_buddy e4b
;
3791 if (!S_ISREG(inode
->i_mode
)) {
3792 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3796 mb_debug(1, "discard preallocation for inode %lu\n", inode
->i_ino
);
3797 trace_ext4_discard_preallocations(inode
);
3799 INIT_LIST_HEAD(&list
);
3802 /* first, collect all pa's in the inode */
3803 spin_lock(&ei
->i_prealloc_lock
);
3804 while (!list_empty(&ei
->i_prealloc_list
)) {
3805 pa
= list_entry(ei
->i_prealloc_list
.next
,
3806 struct ext4_prealloc_space
, pa_inode_list
);
3807 BUG_ON(pa
->pa_obj_lock
!= &ei
->i_prealloc_lock
);
3808 spin_lock(&pa
->pa_lock
);
3809 if (atomic_read(&pa
->pa_count
)) {
3810 /* this shouldn't happen often - nobody should
3811 * use preallocation while we're discarding it */
3812 spin_unlock(&pa
->pa_lock
);
3813 spin_unlock(&ei
->i_prealloc_lock
);
3814 printk(KERN_ERR
"uh-oh! used pa while discarding\n");
3816 schedule_timeout_uninterruptible(HZ
);
3820 if (pa
->pa_deleted
== 0) {
3822 spin_unlock(&pa
->pa_lock
);
3823 list_del_rcu(&pa
->pa_inode_list
);
3824 list_add(&pa
->u
.pa_tmp_list
, &list
);
3828 /* someone is deleting pa right now */
3829 spin_unlock(&pa
->pa_lock
);
3830 spin_unlock(&ei
->i_prealloc_lock
);
3832 /* we have to wait here because pa_deleted
3833 * doesn't mean pa is already unlinked from
3834 * the list. as we might be called from
3835 * ->clear_inode() the inode will get freed
3836 * and concurrent thread which is unlinking
3837 * pa from inode's list may access already
3838 * freed memory, bad-bad-bad */
3840 /* XXX: if this happens too often, we can
3841 * add a flag to force wait only in case
3842 * of ->clear_inode(), but not in case of
3843 * regular truncate */
3844 schedule_timeout_uninterruptible(HZ
);
3847 spin_unlock(&ei
->i_prealloc_lock
);
3849 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
3850 BUG_ON(pa
->pa_type
!= MB_INODE_PA
);
3851 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
3853 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
3855 ext4_error(sb
, "Error loading buddy information for %u",
3860 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
3861 if (bitmap_bh
== NULL
) {
3862 ext4_error(sb
, "Error reading block bitmap for %u",
3864 ext4_mb_unload_buddy(&e4b
);
3868 ext4_lock_group(sb
, group
);
3869 list_del(&pa
->pa_group_list
);
3870 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
);
3871 ext4_unlock_group(sb
, group
);
3873 ext4_mb_unload_buddy(&e4b
);
3876 list_del(&pa
->u
.pa_tmp_list
);
3877 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3882 * finds all preallocated spaces and return blocks being freed to them
3883 * if preallocated space becomes full (no block is used from the space)
3884 * then the function frees space in buddy
3885 * XXX: at the moment, truncate (which is the only way to free blocks)
3886 * discards all preallocations
3888 static void ext4_mb_return_to_preallocation(struct inode
*inode
,
3889 struct ext4_buddy
*e4b
,
3890 sector_t block
, int count
)
3892 BUG_ON(!list_empty(&EXT4_I(inode
)->i_prealloc_list
));
3894 #ifdef CONFIG_EXT4_DEBUG
3895 static void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
3897 struct super_block
*sb
= ac
->ac_sb
;
3898 ext4_group_t ngroups
, i
;
3900 if (EXT4_SB(sb
)->s_mount_flags
& EXT4_MF_FS_ABORTED
)
3903 printk(KERN_ERR
"EXT4-fs: Can't allocate:"
3904 " Allocation context details:\n");
3905 printk(KERN_ERR
"EXT4-fs: status %d flags %d\n",
3906 ac
->ac_status
, ac
->ac_flags
);
3907 printk(KERN_ERR
"EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
3908 "best %lu/%lu/%lu@%lu cr %d\n",
3909 (unsigned long)ac
->ac_o_ex
.fe_group
,
3910 (unsigned long)ac
->ac_o_ex
.fe_start
,
3911 (unsigned long)ac
->ac_o_ex
.fe_len
,
3912 (unsigned long)ac
->ac_o_ex
.fe_logical
,
3913 (unsigned long)ac
->ac_g_ex
.fe_group
,
3914 (unsigned long)ac
->ac_g_ex
.fe_start
,
3915 (unsigned long)ac
->ac_g_ex
.fe_len
,
3916 (unsigned long)ac
->ac_g_ex
.fe_logical
,
3917 (unsigned long)ac
->ac_b_ex
.fe_group
,
3918 (unsigned long)ac
->ac_b_ex
.fe_start
,
3919 (unsigned long)ac
->ac_b_ex
.fe_len
,
3920 (unsigned long)ac
->ac_b_ex
.fe_logical
,
3921 (int)ac
->ac_criteria
);
3922 printk(KERN_ERR
"EXT4-fs: %lu scanned, %d found\n", ac
->ac_ex_scanned
,
3924 printk(KERN_ERR
"EXT4-fs: groups: \n");
3925 ngroups
= ext4_get_groups_count(sb
);
3926 for (i
= 0; i
< ngroups
; i
++) {
3927 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, i
);
3928 struct ext4_prealloc_space
*pa
;
3929 ext4_grpblk_t start
;
3930 struct list_head
*cur
;
3931 ext4_lock_group(sb
, i
);
3932 list_for_each(cur
, &grp
->bb_prealloc_list
) {
3933 pa
= list_entry(cur
, struct ext4_prealloc_space
,
3935 spin_lock(&pa
->pa_lock
);
3936 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
3938 spin_unlock(&pa
->pa_lock
);
3939 printk(KERN_ERR
"PA:%u:%d:%u \n", i
,
3942 ext4_unlock_group(sb
, i
);
3944 if (grp
->bb_free
== 0)
3946 printk(KERN_ERR
"%u: %d/%d \n",
3947 i
, grp
->bb_free
, grp
->bb_fragments
);
3949 printk(KERN_ERR
"\n");
3952 static inline void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
3959 * We use locality group preallocation for small size file. The size of the
3960 * file is determined by the current size or the resulting size after
3961 * allocation which ever is larger
3963 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
3965 static void ext4_mb_group_or_file(struct ext4_allocation_context
*ac
)
3967 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3968 int bsbits
= ac
->ac_sb
->s_blocksize_bits
;
3971 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3974 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
3977 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
3978 isize
= (i_size_read(ac
->ac_inode
) + ac
->ac_sb
->s_blocksize
- 1)
3981 if ((size
== isize
) &&
3982 !ext4_fs_is_busy(sbi
) &&
3983 (atomic_read(&ac
->ac_inode
->i_writecount
) == 0)) {
3984 ac
->ac_flags
|= EXT4_MB_HINT_NOPREALLOC
;
3988 /* don't use group allocation for large files */
3989 size
= max(size
, isize
);
3990 if (size
> sbi
->s_mb_stream_request
) {
3991 ac
->ac_flags
|= EXT4_MB_STREAM_ALLOC
;
3995 BUG_ON(ac
->ac_lg
!= NULL
);
3997 * locality group prealloc space are per cpu. The reason for having
3998 * per cpu locality group is to reduce the contention between block
3999 * request from multiple CPUs.
4001 ac
->ac_lg
= __this_cpu_ptr(sbi
->s_locality_groups
);
4003 /* we're going to use group allocation */
4004 ac
->ac_flags
|= EXT4_MB_HINT_GROUP_ALLOC
;
4006 /* serialize all allocations in the group */
4007 mutex_lock(&ac
->ac_lg
->lg_mutex
);
4010 static noinline_for_stack
int
4011 ext4_mb_initialize_context(struct ext4_allocation_context
*ac
,
4012 struct ext4_allocation_request
*ar
)
4014 struct super_block
*sb
= ar
->inode
->i_sb
;
4015 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4016 struct ext4_super_block
*es
= sbi
->s_es
;
4020 ext4_grpblk_t block
;
4022 /* we can't allocate > group size */
4025 /* just a dirty hack to filter too big requests */
4026 if (len
>= EXT4_BLOCKS_PER_GROUP(sb
) - 10)
4027 len
= EXT4_BLOCKS_PER_GROUP(sb
) - 10;
4029 /* start searching from the goal */
4031 if (goal
< le32_to_cpu(es
->s_first_data_block
) ||
4032 goal
>= ext4_blocks_count(es
))
4033 goal
= le32_to_cpu(es
->s_first_data_block
);
4034 ext4_get_group_no_and_offset(sb
, goal
, &group
, &block
);
4036 /* set up allocation goals */
4037 memset(ac
, 0, sizeof(struct ext4_allocation_context
));
4038 ac
->ac_b_ex
.fe_logical
= ar
->logical
;
4039 ac
->ac_status
= AC_STATUS_CONTINUE
;
4041 ac
->ac_inode
= ar
->inode
;
4042 ac
->ac_o_ex
.fe_logical
= ar
->logical
;
4043 ac
->ac_o_ex
.fe_group
= group
;
4044 ac
->ac_o_ex
.fe_start
= block
;
4045 ac
->ac_o_ex
.fe_len
= len
;
4046 ac
->ac_g_ex
.fe_logical
= ar
->logical
;
4047 ac
->ac_g_ex
.fe_group
= group
;
4048 ac
->ac_g_ex
.fe_start
= block
;
4049 ac
->ac_g_ex
.fe_len
= len
;
4050 ac
->ac_flags
= ar
->flags
;
4052 /* we have to define context: we'll we work with a file or
4053 * locality group. this is a policy, actually */
4054 ext4_mb_group_or_file(ac
);
4056 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
4057 "left: %u/%u, right %u/%u to %swritable\n",
4058 (unsigned) ar
->len
, (unsigned) ar
->logical
,
4059 (unsigned) ar
->goal
, ac
->ac_flags
, ac
->ac_2order
,
4060 (unsigned) ar
->lleft
, (unsigned) ar
->pleft
,
4061 (unsigned) ar
->lright
, (unsigned) ar
->pright
,
4062 atomic_read(&ar
->inode
->i_writecount
) ? "" : "non-");
4067 static noinline_for_stack
void
4068 ext4_mb_discard_lg_preallocations(struct super_block
*sb
,
4069 struct ext4_locality_group
*lg
,
4070 int order
, int total_entries
)
4072 ext4_group_t group
= 0;
4073 struct ext4_buddy e4b
;
4074 struct list_head discard_list
;
4075 struct ext4_prealloc_space
*pa
, *tmp
;
4077 mb_debug(1, "discard locality group preallocation\n");
4079 INIT_LIST_HEAD(&discard_list
);
4081 spin_lock(&lg
->lg_prealloc_lock
);
4082 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[order
],
4084 spin_lock(&pa
->pa_lock
);
4085 if (atomic_read(&pa
->pa_count
)) {
4087 * This is the pa that we just used
4088 * for block allocation. So don't
4091 spin_unlock(&pa
->pa_lock
);
4094 if (pa
->pa_deleted
) {
4095 spin_unlock(&pa
->pa_lock
);
4098 /* only lg prealloc space */
4099 BUG_ON(pa
->pa_type
!= MB_GROUP_PA
);
4101 /* seems this one can be freed ... */
4103 spin_unlock(&pa
->pa_lock
);
4105 list_del_rcu(&pa
->pa_inode_list
);
4106 list_add(&pa
->u
.pa_tmp_list
, &discard_list
);
4109 if (total_entries
<= 5) {
4111 * we want to keep only 5 entries
4112 * allowing it to grow to 8. This
4113 * mak sure we don't call discard
4114 * soon for this list.
4119 spin_unlock(&lg
->lg_prealloc_lock
);
4121 list_for_each_entry_safe(pa
, tmp
, &discard_list
, u
.pa_tmp_list
) {
4123 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
4124 if (ext4_mb_load_buddy(sb
, group
, &e4b
)) {
4125 ext4_error(sb
, "Error loading buddy information for %u",
4129 ext4_lock_group(sb
, group
);
4130 list_del(&pa
->pa_group_list
);
4131 ext4_mb_release_group_pa(&e4b
, pa
);
4132 ext4_unlock_group(sb
, group
);
4134 ext4_mb_unload_buddy(&e4b
);
4135 list_del(&pa
->u
.pa_tmp_list
);
4136 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4141 * We have incremented pa_count. So it cannot be freed at this
4142 * point. Also we hold lg_mutex. So no parallel allocation is
4143 * possible from this lg. That means pa_free cannot be updated.
4145 * A parallel ext4_mb_discard_group_preallocations is possible.
4146 * which can cause the lg_prealloc_list to be updated.
4149 static void ext4_mb_add_n_trim(struct ext4_allocation_context
*ac
)
4151 int order
, added
= 0, lg_prealloc_count
= 1;
4152 struct super_block
*sb
= ac
->ac_sb
;
4153 struct ext4_locality_group
*lg
= ac
->ac_lg
;
4154 struct ext4_prealloc_space
*tmp_pa
, *pa
= ac
->ac_pa
;
4156 order
= fls(pa
->pa_free
) - 1;
4157 if (order
> PREALLOC_TB_SIZE
- 1)
4158 /* The max size of hash table is PREALLOC_TB_SIZE */
4159 order
= PREALLOC_TB_SIZE
- 1;
4160 /* Add the prealloc space to lg */
4162 list_for_each_entry_rcu(tmp_pa
, &lg
->lg_prealloc_list
[order
],
4164 spin_lock(&tmp_pa
->pa_lock
);
4165 if (tmp_pa
->pa_deleted
) {
4166 spin_unlock(&tmp_pa
->pa_lock
);
4169 if (!added
&& pa
->pa_free
< tmp_pa
->pa_free
) {
4170 /* Add to the tail of the previous entry */
4171 list_add_tail_rcu(&pa
->pa_inode_list
,
4172 &tmp_pa
->pa_inode_list
);
4175 * we want to count the total
4176 * number of entries in the list
4179 spin_unlock(&tmp_pa
->pa_lock
);
4180 lg_prealloc_count
++;
4183 list_add_tail_rcu(&pa
->pa_inode_list
,
4184 &lg
->lg_prealloc_list
[order
]);
4187 /* Now trim the list to be not more than 8 elements */
4188 if (lg_prealloc_count
> 8) {
4189 ext4_mb_discard_lg_preallocations(sb
, lg
,
4190 order
, lg_prealloc_count
);
4197 * release all resource we used in allocation
4199 static int ext4_mb_release_context(struct ext4_allocation_context
*ac
)
4201 struct ext4_prealloc_space
*pa
= ac
->ac_pa
;
4203 if (pa
->pa_type
== MB_GROUP_PA
) {
4204 /* see comment in ext4_mb_use_group_pa() */
4205 spin_lock(&pa
->pa_lock
);
4206 pa
->pa_pstart
+= ac
->ac_b_ex
.fe_len
;
4207 pa
->pa_lstart
+= ac
->ac_b_ex
.fe_len
;
4208 pa
->pa_free
-= ac
->ac_b_ex
.fe_len
;
4209 pa
->pa_len
-= ac
->ac_b_ex
.fe_len
;
4210 spin_unlock(&pa
->pa_lock
);
4214 up_read(ac
->alloc_semp
);
4217 * We want to add the pa to the right bucket.
4218 * Remove it from the list and while adding
4219 * make sure the list to which we are adding
4220 * doesn't grow big. We need to release
4221 * alloc_semp before calling ext4_mb_add_n_trim()
4223 if ((pa
->pa_type
== MB_GROUP_PA
) && likely(pa
->pa_free
)) {
4224 spin_lock(pa
->pa_obj_lock
);
4225 list_del_rcu(&pa
->pa_inode_list
);
4226 spin_unlock(pa
->pa_obj_lock
);
4227 ext4_mb_add_n_trim(ac
);
4229 ext4_mb_put_pa(ac
, ac
->ac_sb
, pa
);
4231 if (ac
->ac_bitmap_page
)
4232 page_cache_release(ac
->ac_bitmap_page
);
4233 if (ac
->ac_buddy_page
)
4234 page_cache_release(ac
->ac_buddy_page
);
4235 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
4236 mutex_unlock(&ac
->ac_lg
->lg_mutex
);
4237 ext4_mb_collect_stats(ac
);
4241 static int ext4_mb_discard_preallocations(struct super_block
*sb
, int needed
)
4243 ext4_group_t i
, ngroups
= ext4_get_groups_count(sb
);
4247 trace_ext4_mb_discard_preallocations(sb
, needed
);
4248 for (i
= 0; i
< ngroups
&& needed
> 0; i
++) {
4249 ret
= ext4_mb_discard_group_preallocations(sb
, i
, needed
);
4258 * Main entry point into mballoc to allocate blocks
4259 * it tries to use preallocation first, then falls back
4260 * to usual allocation
4262 ext4_fsblk_t
ext4_mb_new_blocks(handle_t
*handle
,
4263 struct ext4_allocation_request
*ar
, int *errp
)
4266 struct ext4_allocation_context
*ac
= NULL
;
4267 struct ext4_sb_info
*sbi
;
4268 struct super_block
*sb
;
4269 ext4_fsblk_t block
= 0;
4270 unsigned int inquota
= 0;
4271 unsigned int reserv_blks
= 0;
4273 sb
= ar
->inode
->i_sb
;
4276 trace_ext4_request_blocks(ar
);
4279 * For delayed allocation, we could skip the ENOSPC and
4280 * EDQUOT check, as blocks and quotas have been already
4281 * reserved when data being copied into pagecache.
4283 if (EXT4_I(ar
->inode
)->i_delalloc_reserved_flag
)
4284 ar
->flags
|= EXT4_MB_DELALLOC_RESERVED
;
4286 /* Without delayed allocation we need to verify
4287 * there is enough free blocks to do block allocation
4288 * and verify allocation doesn't exceed the quota limits.
4290 while (ar
->len
&& ext4_claim_free_blocks(sbi
, ar
->len
)) {
4291 /* let others to free the space */
4293 ar
->len
= ar
->len
>> 1;
4299 reserv_blks
= ar
->len
;
4300 while (ar
->len
&& dquot_alloc_block(ar
->inode
, ar
->len
)) {
4301 ar
->flags
|= EXT4_MB_HINT_NOPREALLOC
;
4311 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4318 *errp
= ext4_mb_initialize_context(ac
, ar
);
4324 ac
->ac_op
= EXT4_MB_HISTORY_PREALLOC
;
4325 if (!ext4_mb_use_preallocated(ac
)) {
4326 ac
->ac_op
= EXT4_MB_HISTORY_ALLOC
;
4327 ext4_mb_normalize_request(ac
, ar
);
4329 /* allocate space in core */
4330 *errp
= ext4_mb_regular_allocator(ac
);
4334 /* as we've just preallocated more space than
4335 * user requested orinally, we store allocated
4336 * space in a special descriptor */
4337 if (ac
->ac_status
== AC_STATUS_FOUND
&&
4338 ac
->ac_o_ex
.fe_len
< ac
->ac_b_ex
.fe_len
)
4339 ext4_mb_new_preallocation(ac
);
4341 if (likely(ac
->ac_status
== AC_STATUS_FOUND
)) {
4342 *errp
= ext4_mb_mark_diskspace_used(ac
, handle
, reserv_blks
);
4343 if (*errp
== -EAGAIN
) {
4345 * drop the reference that we took
4346 * in ext4_mb_use_best_found
4348 ext4_mb_release_context(ac
);
4349 ac
->ac_b_ex
.fe_group
= 0;
4350 ac
->ac_b_ex
.fe_start
= 0;
4351 ac
->ac_b_ex
.fe_len
= 0;
4352 ac
->ac_status
= AC_STATUS_CONTINUE
;
4356 ext4_discard_allocated_blocks(ac
);
4358 block
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
4359 ar
->len
= ac
->ac_b_ex
.fe_len
;
4362 freed
= ext4_mb_discard_preallocations(sb
, ac
->ac_o_ex
.fe_len
);
4369 ac
->ac_b_ex
.fe_len
= 0;
4371 ext4_mb_show_ac(ac
);
4373 ext4_mb_release_context(ac
);
4376 kmem_cache_free(ext4_ac_cachep
, ac
);
4377 if (inquota
&& ar
->len
< inquota
)
4378 dquot_free_block(ar
->inode
, inquota
- ar
->len
);
4380 if (!EXT4_I(ar
->inode
)->i_delalloc_reserved_flag
)
4381 /* release all the reserved blocks if non delalloc */
4382 percpu_counter_sub(&sbi
->s_dirtyblocks_counter
,
4386 trace_ext4_allocate_blocks(ar
, (unsigned long long)block
);
4392 * We can merge two free data extents only if the physical blocks
4393 * are contiguous, AND the extents were freed by the same transaction,
4394 * AND the blocks are associated with the same group.
4396 static int can_merge(struct ext4_free_data
*entry1
,
4397 struct ext4_free_data
*entry2
)
4399 if ((entry1
->t_tid
== entry2
->t_tid
) &&
4400 (entry1
->group
== entry2
->group
) &&
4401 ((entry1
->start_blk
+ entry1
->count
) == entry2
->start_blk
))
4406 static noinline_for_stack
int
4407 ext4_mb_free_metadata(handle_t
*handle
, struct ext4_buddy
*e4b
,
4408 struct ext4_free_data
*new_entry
)
4410 ext4_group_t group
= e4b
->bd_group
;
4411 ext4_grpblk_t block
;
4412 struct ext4_free_data
*entry
;
4413 struct ext4_group_info
*db
= e4b
->bd_info
;
4414 struct super_block
*sb
= e4b
->bd_sb
;
4415 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4416 struct rb_node
**n
= &db
->bb_free_root
.rb_node
, *node
;
4417 struct rb_node
*parent
= NULL
, *new_node
;
4419 BUG_ON(!ext4_handle_valid(handle
));
4420 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
4421 BUG_ON(e4b
->bd_buddy_page
== NULL
);
4423 new_node
= &new_entry
->node
;
4424 block
= new_entry
->start_blk
;
4427 /* first free block exent. We need to
4428 protect buddy cache from being freed,
4429 * otherwise we'll refresh it from
4430 * on-disk bitmap and lose not-yet-available
4432 page_cache_get(e4b
->bd_buddy_page
);
4433 page_cache_get(e4b
->bd_bitmap_page
);
4437 entry
= rb_entry(parent
, struct ext4_free_data
, node
);
4438 if (block
< entry
->start_blk
)
4440 else if (block
>= (entry
->start_blk
+ entry
->count
))
4441 n
= &(*n
)->rb_right
;
4443 ext4_grp_locked_error(sb
, group
, 0,
4444 ext4_group_first_block_no(sb
, group
) + block
,
4445 "Block already on to-be-freed list");
4450 rb_link_node(new_node
, parent
, n
);
4451 rb_insert_color(new_node
, &db
->bb_free_root
);
4453 /* Now try to see the extent can be merged to left and right */
4454 node
= rb_prev(new_node
);
4456 entry
= rb_entry(node
, struct ext4_free_data
, node
);
4457 if (can_merge(entry
, new_entry
)) {
4458 new_entry
->start_blk
= entry
->start_blk
;
4459 new_entry
->count
+= entry
->count
;
4460 rb_erase(node
, &(db
->bb_free_root
));
4461 spin_lock(&sbi
->s_md_lock
);
4462 list_del(&entry
->list
);
4463 spin_unlock(&sbi
->s_md_lock
);
4464 kmem_cache_free(ext4_free_ext_cachep
, entry
);
4468 node
= rb_next(new_node
);
4470 entry
= rb_entry(node
, struct ext4_free_data
, node
);
4471 if (can_merge(new_entry
, entry
)) {
4472 new_entry
->count
+= entry
->count
;
4473 rb_erase(node
, &(db
->bb_free_root
));
4474 spin_lock(&sbi
->s_md_lock
);
4475 list_del(&entry
->list
);
4476 spin_unlock(&sbi
->s_md_lock
);
4477 kmem_cache_free(ext4_free_ext_cachep
, entry
);
4480 /* Add the extent to transaction's private list */
4481 spin_lock(&sbi
->s_md_lock
);
4482 list_add(&new_entry
->list
, &handle
->h_transaction
->t_private_list
);
4483 spin_unlock(&sbi
->s_md_lock
);
4488 * ext4_free_blocks() -- Free given blocks and update quota
4489 * @handle: handle for this transaction
4491 * @block: start physical block to free
4492 * @count: number of blocks to count
4493 * @metadata: Are these metadata blocks
4495 void ext4_free_blocks(handle_t
*handle
, struct inode
*inode
,
4496 struct buffer_head
*bh
, ext4_fsblk_t block
,
4497 unsigned long count
, int flags
)
4499 struct buffer_head
*bitmap_bh
= NULL
;
4500 struct super_block
*sb
= inode
->i_sb
;
4501 struct ext4_group_desc
*gdp
;
4502 unsigned long freed
= 0;
4503 unsigned int overflow
;
4505 struct buffer_head
*gd_bh
;
4506 ext4_group_t block_group
;
4507 struct ext4_sb_info
*sbi
;
4508 struct ext4_buddy e4b
;
4514 BUG_ON(block
!= bh
->b_blocknr
);
4516 block
= bh
->b_blocknr
;
4520 if (!(flags
& EXT4_FREE_BLOCKS_VALIDATED
) &&
4521 !ext4_data_block_valid(sbi
, block
, count
)) {
4522 ext4_error(sb
, "Freeing blocks not in datazone - "
4523 "block = %llu, count = %lu", block
, count
);
4527 ext4_debug("freeing block %llu\n", block
);
4528 trace_ext4_free_blocks(inode
, block
, count
, flags
);
4530 if (flags
& EXT4_FREE_BLOCKS_FORGET
) {
4531 struct buffer_head
*tbh
= bh
;
4534 BUG_ON(bh
&& (count
> 1));
4536 for (i
= 0; i
< count
; i
++) {
4538 tbh
= sb_find_get_block(inode
->i_sb
,
4540 ext4_forget(handle
, flags
& EXT4_FREE_BLOCKS_METADATA
,
4541 inode
, tbh
, block
+ i
);
4546 * We need to make sure we don't reuse the freed block until
4547 * after the transaction is committed, which we can do by
4548 * treating the block as metadata, below. We make an
4549 * exception if the inode is to be written in writeback mode
4550 * since writeback mode has weak data consistency guarantees.
4552 if (!ext4_should_writeback_data(inode
))
4553 flags
|= EXT4_FREE_BLOCKS_METADATA
;
4557 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
4560 * Check to see if we are freeing blocks across a group
4563 if (bit
+ count
> EXT4_BLOCKS_PER_GROUP(sb
)) {
4564 overflow
= bit
+ count
- EXT4_BLOCKS_PER_GROUP(sb
);
4567 bitmap_bh
= ext4_read_block_bitmap(sb
, block_group
);
4572 gdp
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
4578 if (in_range(ext4_block_bitmap(sb
, gdp
), block
, count
) ||
4579 in_range(ext4_inode_bitmap(sb
, gdp
), block
, count
) ||
4580 in_range(block
, ext4_inode_table(sb
, gdp
),
4581 EXT4_SB(sb
)->s_itb_per_group
) ||
4582 in_range(block
+ count
- 1, ext4_inode_table(sb
, gdp
),
4583 EXT4_SB(sb
)->s_itb_per_group
)) {
4585 ext4_error(sb
, "Freeing blocks in system zone - "
4586 "Block = %llu, count = %lu", block
, count
);
4587 /* err = 0. ext4_std_error should be a no op */
4591 BUFFER_TRACE(bitmap_bh
, "getting write access");
4592 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
4597 * We are about to modify some metadata. Call the journal APIs
4598 * to unshare ->b_data if a currently-committing transaction is
4601 BUFFER_TRACE(gd_bh
, "get_write_access");
4602 err
= ext4_journal_get_write_access(handle
, gd_bh
);
4605 #ifdef AGGRESSIVE_CHECK
4608 for (i
= 0; i
< count
; i
++)
4609 BUG_ON(!mb_test_bit(bit
+ i
, bitmap_bh
->b_data
));
4612 trace_ext4_mballoc_free(sb
, inode
, block_group
, bit
, count
);
4614 err
= ext4_mb_load_buddy(sb
, block_group
, &e4b
);
4618 if ((flags
& EXT4_FREE_BLOCKS_METADATA
) && ext4_handle_valid(handle
)) {
4619 struct ext4_free_data
*new_entry
;
4621 * blocks being freed are metadata. these blocks shouldn't
4622 * be used until this transaction is committed
4624 new_entry
= kmem_cache_alloc(ext4_free_ext_cachep
, GFP_NOFS
);
4625 new_entry
->start_blk
= bit
;
4626 new_entry
->group
= block_group
;
4627 new_entry
->count
= count
;
4628 new_entry
->t_tid
= handle
->h_transaction
->t_tid
;
4630 ext4_lock_group(sb
, block_group
);
4631 mb_clear_bits(bitmap_bh
->b_data
, bit
, count
);
4632 ext4_mb_free_metadata(handle
, &e4b
, new_entry
);
4634 /* need to update group_info->bb_free and bitmap
4635 * with group lock held. generate_buddy look at
4636 * them with group lock_held
4638 if (test_opt(sb
, DISCARD
))
4639 ext4_issue_discard(sb
, block_group
, bit
, count
);
4640 ext4_lock_group(sb
, block_group
);
4641 mb_clear_bits(bitmap_bh
->b_data
, bit
, count
);
4642 mb_free_blocks(inode
, &e4b
, bit
, count
);
4643 ext4_mb_return_to_preallocation(inode
, &e4b
, block
, count
);
4646 ret
= ext4_free_blks_count(sb
, gdp
) + count
;
4647 ext4_free_blks_set(sb
, gdp
, ret
);
4648 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, block_group
, gdp
);
4649 ext4_unlock_group(sb
, block_group
);
4650 percpu_counter_add(&sbi
->s_freeblocks_counter
, count
);
4652 if (sbi
->s_log_groups_per_flex
) {
4653 ext4_group_t flex_group
= ext4_flex_group(sbi
, block_group
);
4654 atomic_add(count
, &sbi
->s_flex_groups
[flex_group
].free_blocks
);
4657 ext4_mb_unload_buddy(&e4b
);
4661 /* We dirtied the bitmap block */
4662 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
4663 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
4665 /* And the group descriptor block */
4666 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
4667 ret
= ext4_handle_dirty_metadata(handle
, NULL
, gd_bh
);
4671 if (overflow
&& !err
) {
4677 ext4_mark_super_dirty(sb
);
4680 dquot_free_block(inode
, freed
);
4682 ext4_std_error(sb
, err
);