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
27 * - test ext4_ext_search_left() and ext4_ext_search_right()
28 * - search for metadata in few groups
31 * - normalization should take into account whether file is still open
32 * - discard preallocations if no free space left (policy?)
33 * - don't normalize tails
35 * - reservation for superuser
38 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
39 * - track min/max extents in each group for better group selection
40 * - mb_mark_used() may allocate chunk right after splitting buddy
41 * - tree of groups sorted by number of free blocks
46 * The allocation request involve request for multiple number of blocks
47 * near to the goal(block) value specified.
49 * During initialization phase of the allocator we decide to use the
50 * group preallocation or inode preallocation depending on the size of
51 * the file. The size of the file could be the resulting file size we
52 * would have after allocation, or the current file size, which ever
53 * is larger. If the size is less than sbi->s_mb_stream_request we
54 * select to use the group preallocation. The default value of
55 * s_mb_stream_request is 16 blocks. This can also be tuned via
56 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
57 * terms of number of blocks.
59 * The main motivation for having small file use group preallocation is to
60 * ensure that we have small files closer together on the disk.
62 * First stage the allocator looks at the inode prealloc list,
63 * ext4_inode_info->i_prealloc_list, which contains list of prealloc
64 * spaces for this particular inode. The inode prealloc space is
67 * pa_lstart -> the logical start block for this prealloc space
68 * pa_pstart -> the physical start block for this prealloc space
69 * pa_len -> lenght for this prealloc space
70 * pa_free -> free space available in this prealloc space
72 * The inode preallocation space is used looking at the _logical_ start
73 * block. If only the logical file block falls within the range of prealloc
74 * space we will consume the particular prealloc space. This make sure that
75 * that the we have contiguous physical blocks representing the file blocks
77 * The important thing to be noted in case of inode prealloc space is that
78 * we don't modify the values associated to inode prealloc space except
81 * If we are not able to find blocks in the inode prealloc space and if we
82 * have the group allocation flag set then we look at the locality group
83 * prealloc space. These are per CPU prealloc list repreasented as
85 * ext4_sb_info.s_locality_groups[smp_processor_id()]
87 * The reason for having a per cpu locality group is to reduce the contention
88 * between CPUs. It is possible to get scheduled at this point.
90 * The locality group prealloc space is used looking at whether we have
91 * enough free space (pa_free) withing the prealloc space.
93 * If we can't allocate blocks via inode prealloc or/and locality group
94 * prealloc then we look at the buddy cache. The buddy cache is represented
95 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
96 * mapped to the buddy and bitmap information regarding different
97 * groups. The buddy information is attached to buddy cache inode so that
98 * we can access them through the page cache. The information regarding
99 * each group is loaded via ext4_mb_load_buddy. The information involve
100 * block bitmap and buddy information. The information are stored in the
104 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
107 * one block each for bitmap and buddy information. So for each group we
108 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
109 * blocksize) blocks. So it can have information regarding groups_per_page
110 * which is blocks_per_page/2
112 * The buddy cache inode is not stored on disk. The inode is thrown
113 * away when the filesystem is unmounted.
115 * We look for count number of blocks in the buddy cache. If we were able
116 * to locate that many free blocks we return with additional information
117 * regarding rest of the contiguous physical block available
119 * Before allocating blocks via buddy cache we normalize the request
120 * blocks. This ensure we ask for more blocks that we needed. The extra
121 * blocks that we get after allocation is added to the respective prealloc
122 * list. In case of inode preallocation we follow a list of heuristics
123 * based on file size. This can be found in ext4_mb_normalize_request. If
124 * we are doing a group prealloc we try to normalize the request to
125 * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is
126 * 512 blocks. This can be tuned via
127 * /sys/fs/ext4/<partition/mb_group_prealloc. The value is represented in
128 * terms of number of blocks. If we have mounted the file system with -O
129 * stripe=<value> option the group prealloc request is normalized to the
130 * stripe value (sbi->s_stripe)
132 * The regular allocator(using the buddy cache) supports few tunables.
134 * /sys/fs/ext4/<partition>/mb_min_to_scan
135 * /sys/fs/ext4/<partition>/mb_max_to_scan
136 * /sys/fs/ext4/<partition>/mb_order2_req
138 * The regular allocator uses buddy scan only if the request len is power of
139 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
140 * value of s_mb_order2_reqs can be tuned via
141 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
142 * stripe size (sbi->s_stripe), we try to search for contigous block in
143 * stripe size. This should result in better allocation on RAID setups. If
144 * not, we search in the specific group using bitmap for best extents. The
145 * tunable min_to_scan and max_to_scan control the behaviour here.
146 * min_to_scan indicate how long the mballoc __must__ look for a best
147 * extent and max_to_scan indicates how long the mballoc __can__ look for a
148 * best extent in the found extents. Searching for the blocks starts with
149 * the group specified as the goal value in allocation context via
150 * ac_g_ex. Each group is first checked based on the criteria whether it
151 * can used for allocation. ext4_mb_good_group explains how the groups are
154 * Both the prealloc space are getting populated as above. So for the first
155 * request we will hit the buddy cache which will result in this prealloc
156 * space getting filled. The prealloc space is then later used for the
157 * subsequent request.
161 * mballoc operates on the following data:
163 * - in-core buddy (actually includes buddy and bitmap)
164 * - preallocation descriptors (PAs)
166 * there are two types of preallocations:
168 * assiged to specific inode and can be used for this inode only.
169 * it describes part of inode's space preallocated to specific
170 * physical blocks. any block from that preallocated can be used
171 * independent. the descriptor just tracks number of blocks left
172 * unused. so, before taking some block from descriptor, one must
173 * make sure corresponded logical block isn't allocated yet. this
174 * also means that freeing any block within descriptor's range
175 * must discard all preallocated blocks.
177 * assigned to specific locality group which does not translate to
178 * permanent set of inodes: inode can join and leave group. space
179 * from this type of preallocation can be used for any inode. thus
180 * it's consumed from the beginning to the end.
182 * relation between them can be expressed as:
183 * in-core buddy = on-disk bitmap + preallocation descriptors
185 * this mean blocks mballoc considers used are:
186 * - allocated blocks (persistent)
187 * - preallocated blocks (non-persistent)
189 * consistency in mballoc world means that at any time a block is either
190 * free or used in ALL structures. notice: "any time" should not be read
191 * literally -- time is discrete and delimited by locks.
193 * to keep it simple, we don't use block numbers, instead we count number of
194 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
196 * all operations can be expressed as:
197 * - init buddy: buddy = on-disk + PAs
198 * - new PA: buddy += N; PA = N
199 * - use inode PA: on-disk += N; PA -= N
200 * - discard inode PA buddy -= on-disk - PA; PA = 0
201 * - use locality group PA on-disk += N; PA -= N
202 * - discard locality group PA buddy -= PA; PA = 0
203 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
204 * is used in real operation because we can't know actual used
205 * bits from PA, only from on-disk bitmap
207 * if we follow this strict logic, then all operations above should be atomic.
208 * given some of them can block, we'd have to use something like semaphores
209 * killing performance on high-end SMP hardware. let's try to relax it using
210 * the following knowledge:
211 * 1) if buddy is referenced, it's already initialized
212 * 2) while block is used in buddy and the buddy is referenced,
213 * nobody can re-allocate that block
214 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
215 * bit set and PA claims same block, it's OK. IOW, one can set bit in
216 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
219 * so, now we're building a concurrency table:
222 * blocks for PA are allocated in the buddy, buddy must be referenced
223 * until PA is linked to allocation group to avoid concurrent buddy init
225 * we need to make sure that either on-disk bitmap or PA has uptodate data
226 * given (3) we care that PA-=N operation doesn't interfere with init
228 * the simplest way would be to have buddy initialized by the discard
229 * - use locality group PA
230 * again PA-=N must be serialized with init
231 * - discard locality group PA
232 * the simplest way would be to have buddy initialized by the discard
235 * i_data_sem serializes them
237 * discard process must wait until PA isn't used by another process
238 * - use locality group PA
239 * some mutex should serialize them
240 * - discard locality group PA
241 * discard process must wait until PA isn't used by another process
244 * i_data_sem or another mutex should serializes them
246 * discard process must wait until PA isn't used by another process
247 * - use locality group PA
248 * nothing wrong here -- they're different PAs covering different blocks
249 * - discard locality group PA
250 * discard process must wait until PA isn't used by another process
252 * now we're ready to make few consequences:
253 * - PA is referenced and while it is no discard is possible
254 * - PA is referenced until block isn't marked in on-disk bitmap
255 * - PA changes only after on-disk bitmap
256 * - discard must not compete with init. either init is done before
257 * any discard or they're serialized somehow
258 * - buddy init as sum of on-disk bitmap and PAs is done atomically
260 * a special case when we've used PA to emptiness. no need to modify buddy
261 * in this case, but we should care about concurrent init
266 * Logic in few words:
271 * mark bits in on-disk bitmap
274 * - use preallocation:
275 * find proper PA (per-inode or group)
277 * mark bits in on-disk bitmap
283 * mark bits in on-disk bitmap
286 * - discard preallocations in group:
288 * move them onto local list
289 * load on-disk bitmap
291 * remove PA from object (inode or locality group)
292 * mark free blocks in-core
294 * - discard inode's preallocations:
301 * - bitlock on a group (group)
302 * - object (inode/locality) (object)
313 * - release consumed pa:
318 * - generate in-core bitmap:
322 * - discard all for given object (inode, locality group):
327 * - discard all for given group:
334 static struct kmem_cache
*ext4_pspace_cachep
;
335 static struct kmem_cache
*ext4_ac_cachep
;
336 static struct kmem_cache
*ext4_free_ext_cachep
;
337 static void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
339 static void ext4_mb_generate_from_freelist(struct super_block
*sb
, void *bitmap
,
341 static void release_blocks_on_commit(journal_t
*journal
, transaction_t
*txn
);
345 static inline void *mb_correct_addr_and_bit(int *bit
, void *addr
)
347 #if BITS_PER_LONG == 64
348 *bit
+= ((unsigned long) addr
& 7UL) << 3;
349 addr
= (void *) ((unsigned long) addr
& ~7UL);
350 #elif BITS_PER_LONG == 32
351 *bit
+= ((unsigned long) addr
& 3UL) << 3;
352 addr
= (void *) ((unsigned long) addr
& ~3UL);
354 #error "how many bits you are?!"
359 static inline int mb_test_bit(int bit
, void *addr
)
362 * ext4_test_bit on architecture like powerpc
363 * needs unsigned long aligned address
365 addr
= mb_correct_addr_and_bit(&bit
, addr
);
366 return ext4_test_bit(bit
, addr
);
369 static inline void mb_set_bit(int bit
, void *addr
)
371 addr
= mb_correct_addr_and_bit(&bit
, addr
);
372 ext4_set_bit(bit
, addr
);
375 static inline void mb_set_bit_atomic(spinlock_t
*lock
, int bit
, void *addr
)
377 addr
= mb_correct_addr_and_bit(&bit
, addr
);
378 ext4_set_bit_atomic(lock
, bit
, addr
);
381 static inline void mb_clear_bit(int bit
, void *addr
)
383 addr
= mb_correct_addr_and_bit(&bit
, addr
);
384 ext4_clear_bit(bit
, addr
);
387 static inline void mb_clear_bit_atomic(spinlock_t
*lock
, int bit
, void *addr
)
389 addr
= mb_correct_addr_and_bit(&bit
, addr
);
390 ext4_clear_bit_atomic(lock
, bit
, addr
);
393 static inline int mb_find_next_zero_bit(void *addr
, int max
, int start
)
395 int fix
= 0, ret
, tmpmax
;
396 addr
= mb_correct_addr_and_bit(&fix
, addr
);
400 ret
= ext4_find_next_zero_bit(addr
, tmpmax
, start
) - fix
;
406 static inline int mb_find_next_bit(void *addr
, int max
, int start
)
408 int fix
= 0, ret
, tmpmax
;
409 addr
= mb_correct_addr_and_bit(&fix
, addr
);
413 ret
= ext4_find_next_bit(addr
, tmpmax
, start
) - fix
;
419 static void *mb_find_buddy(struct ext4_buddy
*e4b
, int order
, int *max
)
423 BUG_ON(EXT4_MB_BITMAP(e4b
) == EXT4_MB_BUDDY(e4b
));
426 if (order
> e4b
->bd_blkbits
+ 1) {
431 /* at order 0 we see each particular block */
432 *max
= 1 << (e4b
->bd_blkbits
+ 3);
434 return EXT4_MB_BITMAP(e4b
);
436 bb
= EXT4_MB_BUDDY(e4b
) + EXT4_SB(e4b
->bd_sb
)->s_mb_offsets
[order
];
437 *max
= EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[order
];
443 static void mb_free_blocks_double(struct inode
*inode
, struct ext4_buddy
*e4b
,
444 int first
, int count
)
447 struct super_block
*sb
= e4b
->bd_sb
;
449 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
451 BUG_ON(!ext4_is_group_locked(sb
, e4b
->bd_group
));
452 for (i
= 0; i
< count
; i
++) {
453 if (!mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
)) {
454 ext4_fsblk_t blocknr
;
455 blocknr
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
);
456 blocknr
+= first
+ i
;
458 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
459 ext4_grp_locked_error(sb
, e4b
->bd_group
,
460 __func__
, "double-free of inode"
461 " %lu's block %llu(bit %u in group %u)",
462 inode
? inode
->i_ino
: 0, blocknr
,
463 first
+ i
, e4b
->bd_group
);
465 mb_clear_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
469 static void mb_mark_used_double(struct ext4_buddy
*e4b
, int first
, int count
)
473 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
475 BUG_ON(!ext4_is_group_locked(e4b
->bd_sb
, e4b
->bd_group
));
476 for (i
= 0; i
< count
; i
++) {
477 BUG_ON(mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
));
478 mb_set_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
482 static void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
484 if (memcmp(e4b
->bd_info
->bb_bitmap
, bitmap
, e4b
->bd_sb
->s_blocksize
)) {
485 unsigned char *b1
, *b2
;
487 b1
= (unsigned char *) e4b
->bd_info
->bb_bitmap
;
488 b2
= (unsigned char *) bitmap
;
489 for (i
= 0; i
< e4b
->bd_sb
->s_blocksize
; i
++) {
490 if (b1
[i
] != b2
[i
]) {
491 printk(KERN_ERR
"corruption in group %u "
492 "at byte %u(%u): %x in copy != %x "
493 "on disk/prealloc\n",
494 e4b
->bd_group
, i
, i
* 8, b1
[i
], b2
[i
]);
502 static inline void mb_free_blocks_double(struct inode
*inode
,
503 struct ext4_buddy
*e4b
, int first
, int count
)
507 static inline void mb_mark_used_double(struct ext4_buddy
*e4b
,
508 int first
, int count
)
512 static inline void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
518 #ifdef AGGRESSIVE_CHECK
520 #define MB_CHECK_ASSERT(assert) \
524 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
525 function, file, line, # assert); \
530 static int __mb_check_buddy(struct ext4_buddy
*e4b
, char *file
,
531 const char *function
, int line
)
533 struct super_block
*sb
= e4b
->bd_sb
;
534 int order
= e4b
->bd_blkbits
+ 1;
541 struct ext4_group_info
*grp
;
544 struct list_head
*cur
;
549 static int mb_check_counter
;
550 if (mb_check_counter
++ % 100 != 0)
555 buddy
= mb_find_buddy(e4b
, order
, &max
);
556 MB_CHECK_ASSERT(buddy
);
557 buddy2
= mb_find_buddy(e4b
, order
- 1, &max2
);
558 MB_CHECK_ASSERT(buddy2
);
559 MB_CHECK_ASSERT(buddy
!= buddy2
);
560 MB_CHECK_ASSERT(max
* 2 == max2
);
563 for (i
= 0; i
< max
; i
++) {
565 if (mb_test_bit(i
, buddy
)) {
566 /* only single bit in buddy2 may be 1 */
567 if (!mb_test_bit(i
<< 1, buddy2
)) {
569 mb_test_bit((i
<<1)+1, buddy2
));
570 } else if (!mb_test_bit((i
<< 1) + 1, buddy2
)) {
572 mb_test_bit(i
<< 1, buddy2
));
577 /* both bits in buddy2 must be 0 */
578 MB_CHECK_ASSERT(mb_test_bit(i
<< 1, buddy2
));
579 MB_CHECK_ASSERT(mb_test_bit((i
<< 1) + 1, buddy2
));
581 for (j
= 0; j
< (1 << order
); j
++) {
582 k
= (i
* (1 << order
)) + j
;
584 !mb_test_bit(k
, EXT4_MB_BITMAP(e4b
)));
588 MB_CHECK_ASSERT(e4b
->bd_info
->bb_counters
[order
] == count
);
593 buddy
= mb_find_buddy(e4b
, 0, &max
);
594 for (i
= 0; i
< max
; i
++) {
595 if (!mb_test_bit(i
, buddy
)) {
596 MB_CHECK_ASSERT(i
>= e4b
->bd_info
->bb_first_free
);
604 /* check used bits only */
605 for (j
= 0; j
< e4b
->bd_blkbits
+ 1; j
++) {
606 buddy2
= mb_find_buddy(e4b
, j
, &max2
);
608 MB_CHECK_ASSERT(k
< max2
);
609 MB_CHECK_ASSERT(mb_test_bit(k
, buddy2
));
612 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b
->bd_info
));
613 MB_CHECK_ASSERT(e4b
->bd_info
->bb_fragments
== fragments
);
615 grp
= ext4_get_group_info(sb
, e4b
->bd_group
);
616 buddy
= mb_find_buddy(e4b
, 0, &max
);
617 list_for_each(cur
, &grp
->bb_prealloc_list
) {
618 ext4_group_t groupnr
;
619 struct ext4_prealloc_space
*pa
;
620 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
621 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &groupnr
, &k
);
622 MB_CHECK_ASSERT(groupnr
== e4b
->bd_group
);
623 for (i
= 0; i
< pa
->pa_len
; i
++)
624 MB_CHECK_ASSERT(mb_test_bit(k
+ i
, buddy
));
628 #undef MB_CHECK_ASSERT
629 #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
630 __FILE__, __func__, __LINE__)
632 #define mb_check_buddy(e4b)
635 /* FIXME!! need more doc */
636 static void ext4_mb_mark_free_simple(struct super_block
*sb
,
637 void *buddy
, unsigned first
, int len
,
638 struct ext4_group_info
*grp
)
640 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
643 unsigned short chunk
;
644 unsigned short border
;
646 BUG_ON(len
> EXT4_BLOCKS_PER_GROUP(sb
));
648 border
= 2 << sb
->s_blocksize_bits
;
651 /* find how many blocks can be covered since this position */
652 max
= ffs(first
| border
) - 1;
654 /* find how many blocks of power 2 we need to mark */
661 /* mark multiblock chunks only */
662 grp
->bb_counters
[min
]++;
664 mb_clear_bit(first
>> min
,
665 buddy
+ sbi
->s_mb_offsets
[min
]);
672 static void ext4_mb_generate_buddy(struct super_block
*sb
,
673 void *buddy
, void *bitmap
, ext4_group_t group
)
675 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
676 unsigned short max
= EXT4_BLOCKS_PER_GROUP(sb
);
677 unsigned short i
= 0;
678 unsigned short first
;
681 unsigned fragments
= 0;
682 unsigned long long period
= get_cycles();
684 /* initialize buddy from bitmap which is aggregation
685 * of on-disk bitmap and preallocations */
686 i
= mb_find_next_zero_bit(bitmap
, max
, 0);
687 grp
->bb_first_free
= i
;
691 i
= mb_find_next_bit(bitmap
, max
, i
);
695 ext4_mb_mark_free_simple(sb
, buddy
, first
, len
, grp
);
697 grp
->bb_counters
[0]++;
699 i
= mb_find_next_zero_bit(bitmap
, max
, i
);
701 grp
->bb_fragments
= fragments
;
703 if (free
!= grp
->bb_free
) {
704 ext4_grp_locked_error(sb
, group
, __func__
,
705 "EXT4-fs: group %u: %u blocks in bitmap, %u in gd",
706 group
, free
, grp
->bb_free
);
708 * If we intent to continue, we consider group descritor
709 * corrupt and update bb_free using bitmap value
714 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
, &(grp
->bb_state
));
716 period
= get_cycles() - period
;
717 spin_lock(&EXT4_SB(sb
)->s_bal_lock
);
718 EXT4_SB(sb
)->s_mb_buddies_generated
++;
719 EXT4_SB(sb
)->s_mb_generation_time
+= period
;
720 spin_unlock(&EXT4_SB(sb
)->s_bal_lock
);
723 /* The buddy information is attached the buddy cache inode
724 * for convenience. The information regarding each group
725 * is loaded via ext4_mb_load_buddy. The information involve
726 * block bitmap and buddy information. The information are
727 * stored in the inode as
730 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
733 * one block each for bitmap and buddy information.
734 * So for each group we take up 2 blocks. A page can
735 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
736 * So it can have information regarding groups_per_page which
737 * is blocks_per_page/2
740 static int ext4_mb_init_cache(struct page
*page
, char *incore
)
747 ext4_group_t first_group
;
749 struct super_block
*sb
;
750 struct buffer_head
*bhs
;
751 struct buffer_head
**bh
;
756 mb_debug("init page %lu\n", page
->index
);
758 inode
= page
->mapping
->host
;
760 blocksize
= 1 << inode
->i_blkbits
;
761 blocks_per_page
= PAGE_CACHE_SIZE
/ blocksize
;
763 groups_per_page
= blocks_per_page
>> 1;
764 if (groups_per_page
== 0)
767 /* allocate buffer_heads to read bitmaps */
768 if (groups_per_page
> 1) {
770 i
= sizeof(struct buffer_head
*) * groups_per_page
;
771 bh
= kzalloc(i
, GFP_NOFS
);
777 first_group
= page
->index
* blocks_per_page
/ 2;
779 /* read all groups the page covers into the cache */
780 for (i
= 0; i
< groups_per_page
; i
++) {
781 struct ext4_group_desc
*desc
;
783 if (first_group
+ i
>= EXT4_SB(sb
)->s_groups_count
)
787 desc
= ext4_get_group_desc(sb
, first_group
+ i
, NULL
);
792 bh
[i
] = sb_getblk(sb
, ext4_block_bitmap(sb
, desc
));
796 if (bitmap_uptodate(bh
[i
]))
800 if (bitmap_uptodate(bh
[i
])) {
801 unlock_buffer(bh
[i
]);
804 spin_lock(sb_bgl_lock(EXT4_SB(sb
), first_group
+ i
));
805 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
806 ext4_init_block_bitmap(sb
, bh
[i
],
807 first_group
+ i
, desc
);
808 set_bitmap_uptodate(bh
[i
]);
809 set_buffer_uptodate(bh
[i
]);
810 spin_unlock(sb_bgl_lock(EXT4_SB(sb
), first_group
+ i
));
811 unlock_buffer(bh
[i
]);
814 spin_unlock(sb_bgl_lock(EXT4_SB(sb
), first_group
+ i
));
815 if (buffer_uptodate(bh
[i
])) {
817 * if not uninit if bh is uptodate,
818 * bitmap is also uptodate
820 set_bitmap_uptodate(bh
[i
]);
821 unlock_buffer(bh
[i
]);
826 * submit the buffer_head for read. We can
827 * safely mark the bitmap as uptodate now.
828 * We do it here so the bitmap uptodate bit
829 * get set with buffer lock held.
831 set_bitmap_uptodate(bh
[i
]);
832 bh
[i
]->b_end_io
= end_buffer_read_sync
;
833 submit_bh(READ
, bh
[i
]);
834 mb_debug("read bitmap for group %u\n", first_group
+ i
);
837 /* wait for I/O completion */
838 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
839 wait_on_buffer(bh
[i
]);
842 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
843 if (!buffer_uptodate(bh
[i
]))
847 first_block
= page
->index
* blocks_per_page
;
849 memset(page_address(page
), 0xff, PAGE_CACHE_SIZE
);
850 for (i
= 0; i
< blocks_per_page
; i
++) {
852 struct ext4_group_info
*grinfo
;
854 group
= (first_block
+ i
) >> 1;
855 if (group
>= EXT4_SB(sb
)->s_groups_count
)
859 * data carry information regarding this
860 * particular group in the format specified
864 data
= page_address(page
) + (i
* blocksize
);
865 bitmap
= bh
[group
- first_group
]->b_data
;
868 * We place the buddy block and bitmap block
871 if ((first_block
+ i
) & 1) {
872 /* this is block of buddy */
873 BUG_ON(incore
== NULL
);
874 mb_debug("put buddy for group %u in page %lu/%x\n",
875 group
, page
->index
, i
* blocksize
);
876 grinfo
= ext4_get_group_info(sb
, group
);
877 grinfo
->bb_fragments
= 0;
878 memset(grinfo
->bb_counters
, 0,
879 sizeof(unsigned short)*(sb
->s_blocksize_bits
+2));
881 * incore got set to the group block bitmap below
883 ext4_lock_group(sb
, group
);
884 ext4_mb_generate_buddy(sb
, data
, incore
, group
);
885 ext4_unlock_group(sb
, group
);
888 /* this is block of bitmap */
889 BUG_ON(incore
!= NULL
);
890 mb_debug("put bitmap for group %u in page %lu/%x\n",
891 group
, page
->index
, i
* blocksize
);
893 /* see comments in ext4_mb_put_pa() */
894 ext4_lock_group(sb
, group
);
895 memcpy(data
, bitmap
, blocksize
);
897 /* mark all preallocated blks used in in-core bitmap */
898 ext4_mb_generate_from_pa(sb
, data
, group
);
899 ext4_mb_generate_from_freelist(sb
, data
, group
);
900 ext4_unlock_group(sb
, group
);
902 /* set incore so that the buddy information can be
903 * generated using this
908 SetPageUptodate(page
);
912 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
920 static noinline_for_stack
int
921 ext4_mb_load_buddy(struct super_block
*sb
, ext4_group_t group
,
922 struct ext4_buddy
*e4b
)
930 struct ext4_group_info
*grp
;
931 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
932 struct inode
*inode
= sbi
->s_buddy_cache
;
934 mb_debug("load group %u\n", group
);
936 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
937 grp
= ext4_get_group_info(sb
, group
);
939 e4b
->bd_blkbits
= sb
->s_blocksize_bits
;
940 e4b
->bd_info
= ext4_get_group_info(sb
, group
);
942 e4b
->bd_group
= group
;
943 e4b
->bd_buddy_page
= NULL
;
944 e4b
->bd_bitmap_page
= NULL
;
945 e4b
->alloc_semp
= &grp
->alloc_sem
;
947 /* Take the read lock on the group alloc
948 * sem. This would make sure a parallel
949 * ext4_mb_init_group happening on other
950 * groups mapped by the page is blocked
951 * till we are done with allocation
953 down_read(e4b
->alloc_semp
);
956 * the buddy cache inode stores the block bitmap
957 * and buddy information in consecutive blocks.
958 * So for each group we need two blocks.
961 pnum
= block
/ blocks_per_page
;
962 poff
= block
% blocks_per_page
;
964 /* we could use find_or_create_page(), but it locks page
965 * what we'd like to avoid in fast path ... */
966 page
= find_get_page(inode
->i_mapping
, pnum
);
967 if (page
== NULL
|| !PageUptodate(page
)) {
970 * drop the page reference and try
971 * to get the page with lock. If we
972 * are not uptodate that implies
973 * somebody just created the page but
974 * is yet to initialize the same. So
975 * wait for it to initialize.
977 page_cache_release(page
);
978 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
980 BUG_ON(page
->mapping
!= inode
->i_mapping
);
981 if (!PageUptodate(page
)) {
982 ret
= ext4_mb_init_cache(page
, NULL
);
987 mb_cmp_bitmaps(e4b
, page_address(page
) +
988 (poff
* sb
->s_blocksize
));
993 if (page
== NULL
|| !PageUptodate(page
)) {
997 e4b
->bd_bitmap_page
= page
;
998 e4b
->bd_bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
999 mark_page_accessed(page
);
1002 pnum
= block
/ blocks_per_page
;
1003 poff
= block
% blocks_per_page
;
1005 page
= find_get_page(inode
->i_mapping
, pnum
);
1006 if (page
== NULL
|| !PageUptodate(page
)) {
1008 page_cache_release(page
);
1009 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1011 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1012 if (!PageUptodate(page
)) {
1013 ret
= ext4_mb_init_cache(page
, e4b
->bd_bitmap
);
1022 if (page
== NULL
|| !PageUptodate(page
)) {
1026 e4b
->bd_buddy_page
= page
;
1027 e4b
->bd_buddy
= page_address(page
) + (poff
* sb
->s_blocksize
);
1028 mark_page_accessed(page
);
1030 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
1031 BUG_ON(e4b
->bd_buddy_page
== NULL
);
1036 if (e4b
->bd_bitmap_page
)
1037 page_cache_release(e4b
->bd_bitmap_page
);
1038 if (e4b
->bd_buddy_page
)
1039 page_cache_release(e4b
->bd_buddy_page
);
1040 e4b
->bd_buddy
= NULL
;
1041 e4b
->bd_bitmap
= NULL
;
1043 /* Done with the buddy cache */
1044 up_read(e4b
->alloc_semp
);
1048 static void ext4_mb_release_desc(struct ext4_buddy
*e4b
)
1050 if (e4b
->bd_bitmap_page
)
1051 page_cache_release(e4b
->bd_bitmap_page
);
1052 if (e4b
->bd_buddy_page
)
1053 page_cache_release(e4b
->bd_buddy_page
);
1054 /* Done with the buddy cache */
1055 if (e4b
->alloc_semp
)
1056 up_read(e4b
->alloc_semp
);
1060 static int mb_find_order_for_block(struct ext4_buddy
*e4b
, int block
)
1065 BUG_ON(EXT4_MB_BITMAP(e4b
) == EXT4_MB_BUDDY(e4b
));
1066 BUG_ON(block
>= (1 << (e4b
->bd_blkbits
+ 3)));
1068 bb
= EXT4_MB_BUDDY(e4b
);
1069 while (order
<= e4b
->bd_blkbits
+ 1) {
1071 if (!mb_test_bit(block
, bb
)) {
1072 /* this block is part of buddy of order 'order' */
1075 bb
+= 1 << (e4b
->bd_blkbits
- order
);
1081 static void mb_clear_bits(spinlock_t
*lock
, void *bm
, int cur
, int len
)
1087 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1088 /* fast path: clear whole word at once */
1089 addr
= bm
+ (cur
>> 3);
1095 mb_clear_bit_atomic(lock
, cur
, bm
);
1097 mb_clear_bit(cur
, bm
);
1102 static void mb_set_bits(spinlock_t
*lock
, void *bm
, int cur
, int len
)
1108 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1109 /* fast path: set whole word at once */
1110 addr
= bm
+ (cur
>> 3);
1116 mb_set_bit_atomic(lock
, cur
, bm
);
1118 mb_set_bit(cur
, bm
);
1123 static void mb_free_blocks(struct inode
*inode
, struct ext4_buddy
*e4b
,
1124 int first
, int count
)
1131 struct super_block
*sb
= e4b
->bd_sb
;
1133 BUG_ON(first
+ count
> (sb
->s_blocksize
<< 3));
1134 BUG_ON(!ext4_is_group_locked(sb
, e4b
->bd_group
));
1135 mb_check_buddy(e4b
);
1136 mb_free_blocks_double(inode
, e4b
, first
, count
);
1138 e4b
->bd_info
->bb_free
+= count
;
1139 if (first
< e4b
->bd_info
->bb_first_free
)
1140 e4b
->bd_info
->bb_first_free
= first
;
1142 /* let's maintain fragments counter */
1144 block
= !mb_test_bit(first
- 1, EXT4_MB_BITMAP(e4b
));
1145 if (first
+ count
< EXT4_SB(sb
)->s_mb_maxs
[0])
1146 max
= !mb_test_bit(first
+ count
, EXT4_MB_BITMAP(e4b
));
1148 e4b
->bd_info
->bb_fragments
--;
1149 else if (!block
&& !max
)
1150 e4b
->bd_info
->bb_fragments
++;
1152 /* let's maintain buddy itself */
1153 while (count
-- > 0) {
1157 if (!mb_test_bit(block
, EXT4_MB_BITMAP(e4b
))) {
1158 ext4_fsblk_t blocknr
;
1159 blocknr
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
);
1162 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
1163 ext4_grp_locked_error(sb
, e4b
->bd_group
,
1164 __func__
, "double-free of inode"
1165 " %lu's block %llu(bit %u in group %u)",
1166 inode
? inode
->i_ino
: 0, blocknr
, block
,
1169 mb_clear_bit(block
, EXT4_MB_BITMAP(e4b
));
1170 e4b
->bd_info
->bb_counters
[order
]++;
1172 /* start of the buddy */
1173 buddy
= mb_find_buddy(e4b
, order
, &max
);
1177 if (mb_test_bit(block
, buddy
) ||
1178 mb_test_bit(block
+ 1, buddy
))
1181 /* both the buddies are free, try to coalesce them */
1182 buddy2
= mb_find_buddy(e4b
, order
+ 1, &max
);
1188 /* for special purposes, we don't set
1189 * free bits in bitmap */
1190 mb_set_bit(block
, buddy
);
1191 mb_set_bit(block
+ 1, buddy
);
1193 e4b
->bd_info
->bb_counters
[order
]--;
1194 e4b
->bd_info
->bb_counters
[order
]--;
1198 e4b
->bd_info
->bb_counters
[order
]++;
1200 mb_clear_bit(block
, buddy2
);
1204 mb_check_buddy(e4b
);
1207 static int mb_find_extent(struct ext4_buddy
*e4b
, int order
, int block
,
1208 int needed
, struct ext4_free_extent
*ex
)
1215 BUG_ON(!ext4_is_group_locked(e4b
->bd_sb
, e4b
->bd_group
));
1218 buddy
= mb_find_buddy(e4b
, order
, &max
);
1219 BUG_ON(buddy
== NULL
);
1220 BUG_ON(block
>= max
);
1221 if (mb_test_bit(block
, buddy
)) {
1228 /* FIXME dorp order completely ? */
1229 if (likely(order
== 0)) {
1230 /* find actual order */
1231 order
= mb_find_order_for_block(e4b
, block
);
1232 block
= block
>> order
;
1235 ex
->fe_len
= 1 << order
;
1236 ex
->fe_start
= block
<< order
;
1237 ex
->fe_group
= e4b
->bd_group
;
1239 /* calc difference from given start */
1240 next
= next
- ex
->fe_start
;
1242 ex
->fe_start
+= next
;
1244 while (needed
> ex
->fe_len
&&
1245 (buddy
= mb_find_buddy(e4b
, order
, &max
))) {
1247 if (block
+ 1 >= max
)
1250 next
= (block
+ 1) * (1 << order
);
1251 if (mb_test_bit(next
, EXT4_MB_BITMAP(e4b
)))
1254 ord
= mb_find_order_for_block(e4b
, next
);
1257 block
= next
>> order
;
1258 ex
->fe_len
+= 1 << order
;
1261 BUG_ON(ex
->fe_start
+ ex
->fe_len
> (1 << (e4b
->bd_blkbits
+ 3)));
1265 static int mb_mark_used(struct ext4_buddy
*e4b
, struct ext4_free_extent
*ex
)
1271 int start
= ex
->fe_start
;
1272 int len
= ex
->fe_len
;
1277 BUG_ON(start
+ len
> (e4b
->bd_sb
->s_blocksize
<< 3));
1278 BUG_ON(e4b
->bd_group
!= ex
->fe_group
);
1279 BUG_ON(!ext4_is_group_locked(e4b
->bd_sb
, e4b
->bd_group
));
1280 mb_check_buddy(e4b
);
1281 mb_mark_used_double(e4b
, start
, len
);
1283 e4b
->bd_info
->bb_free
-= len
;
1284 if (e4b
->bd_info
->bb_first_free
== start
)
1285 e4b
->bd_info
->bb_first_free
+= len
;
1287 /* let's maintain fragments counter */
1289 mlen
= !mb_test_bit(start
- 1, EXT4_MB_BITMAP(e4b
));
1290 if (start
+ len
< EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[0])
1291 max
= !mb_test_bit(start
+ len
, EXT4_MB_BITMAP(e4b
));
1293 e4b
->bd_info
->bb_fragments
++;
1294 else if (!mlen
&& !max
)
1295 e4b
->bd_info
->bb_fragments
--;
1297 /* let's maintain buddy itself */
1299 ord
= mb_find_order_for_block(e4b
, start
);
1301 if (((start
>> ord
) << ord
) == start
&& len
>= (1 << ord
)) {
1302 /* the whole chunk may be allocated at once! */
1304 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1305 BUG_ON((start
>> ord
) >= max
);
1306 mb_set_bit(start
>> ord
, buddy
);
1307 e4b
->bd_info
->bb_counters
[ord
]--;
1314 /* store for history */
1316 ret
= len
| (ord
<< 16);
1318 /* we have to split large buddy */
1320 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1321 mb_set_bit(start
>> ord
, buddy
);
1322 e4b
->bd_info
->bb_counters
[ord
]--;
1325 cur
= (start
>> ord
) & ~1U;
1326 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1327 mb_clear_bit(cur
, buddy
);
1328 mb_clear_bit(cur
+ 1, buddy
);
1329 e4b
->bd_info
->bb_counters
[ord
]++;
1330 e4b
->bd_info
->bb_counters
[ord
]++;
1333 mb_set_bits(sb_bgl_lock(EXT4_SB(e4b
->bd_sb
), ex
->fe_group
),
1334 EXT4_MB_BITMAP(e4b
), ex
->fe_start
, len0
);
1335 mb_check_buddy(e4b
);
1341 * Must be called under group lock!
1343 static void ext4_mb_use_best_found(struct ext4_allocation_context
*ac
,
1344 struct ext4_buddy
*e4b
)
1346 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1349 BUG_ON(ac
->ac_b_ex
.fe_group
!= e4b
->bd_group
);
1350 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1352 ac
->ac_b_ex
.fe_len
= min(ac
->ac_b_ex
.fe_len
, ac
->ac_g_ex
.fe_len
);
1353 ac
->ac_b_ex
.fe_logical
= ac
->ac_g_ex
.fe_logical
;
1354 ret
= mb_mark_used(e4b
, &ac
->ac_b_ex
);
1356 /* preallocation can change ac_b_ex, thus we store actually
1357 * allocated blocks for history */
1358 ac
->ac_f_ex
= ac
->ac_b_ex
;
1360 ac
->ac_status
= AC_STATUS_FOUND
;
1361 ac
->ac_tail
= ret
& 0xffff;
1362 ac
->ac_buddy
= ret
>> 16;
1365 * take the page reference. We want the page to be pinned
1366 * so that we don't get a ext4_mb_init_cache_call for this
1367 * group until we update the bitmap. That would mean we
1368 * double allocate blocks. The reference is dropped
1369 * in ext4_mb_release_context
1371 ac
->ac_bitmap_page
= e4b
->bd_bitmap_page
;
1372 get_page(ac
->ac_bitmap_page
);
1373 ac
->ac_buddy_page
= e4b
->bd_buddy_page
;
1374 get_page(ac
->ac_buddy_page
);
1375 /* on allocation we use ac to track the held semaphore */
1376 ac
->alloc_semp
= e4b
->alloc_semp
;
1377 e4b
->alloc_semp
= NULL
;
1378 /* store last allocated for subsequent stream allocation */
1379 if ((ac
->ac_flags
& EXT4_MB_HINT_DATA
)) {
1380 spin_lock(&sbi
->s_md_lock
);
1381 sbi
->s_mb_last_group
= ac
->ac_f_ex
.fe_group
;
1382 sbi
->s_mb_last_start
= ac
->ac_f_ex
.fe_start
;
1383 spin_unlock(&sbi
->s_md_lock
);
1388 * regular allocator, for general purposes allocation
1391 static void ext4_mb_check_limits(struct ext4_allocation_context
*ac
,
1392 struct ext4_buddy
*e4b
,
1395 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1396 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1397 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1398 struct ext4_free_extent ex
;
1401 if (ac
->ac_status
== AC_STATUS_FOUND
)
1404 * We don't want to scan for a whole year
1406 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
&&
1407 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1408 ac
->ac_status
= AC_STATUS_BREAK
;
1413 * Haven't found good chunk so far, let's continue
1415 if (bex
->fe_len
< gex
->fe_len
)
1418 if ((finish_group
|| ac
->ac_found
> sbi
->s_mb_min_to_scan
)
1419 && bex
->fe_group
== e4b
->bd_group
) {
1420 /* recheck chunk's availability - we don't know
1421 * when it was found (within this lock-unlock
1423 max
= mb_find_extent(e4b
, 0, bex
->fe_start
, gex
->fe_len
, &ex
);
1424 if (max
>= gex
->fe_len
) {
1425 ext4_mb_use_best_found(ac
, e4b
);
1432 * The routine checks whether found extent is good enough. If it is,
1433 * then the extent gets marked used and flag is set to the context
1434 * to stop scanning. Otherwise, the extent is compared with the
1435 * previous found extent and if new one is better, then it's stored
1436 * in the context. Later, the best found extent will be used, if
1437 * mballoc can't find good enough extent.
1439 * FIXME: real allocation policy is to be designed yet!
1441 static void ext4_mb_measure_extent(struct ext4_allocation_context
*ac
,
1442 struct ext4_free_extent
*ex
,
1443 struct ext4_buddy
*e4b
)
1445 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1446 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1448 BUG_ON(ex
->fe_len
<= 0);
1449 BUG_ON(ex
->fe_len
> EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
1450 BUG_ON(ex
->fe_start
>= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
1451 BUG_ON(ac
->ac_status
!= AC_STATUS_CONTINUE
);
1456 * The special case - take what you catch first
1458 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1460 ext4_mb_use_best_found(ac
, e4b
);
1465 * Let's check whether the chuck is good enough
1467 if (ex
->fe_len
== gex
->fe_len
) {
1469 ext4_mb_use_best_found(ac
, e4b
);
1474 * If this is first found extent, just store it in the context
1476 if (bex
->fe_len
== 0) {
1482 * If new found extent is better, store it in the context
1484 if (bex
->fe_len
< gex
->fe_len
) {
1485 /* if the request isn't satisfied, any found extent
1486 * larger than previous best one is better */
1487 if (ex
->fe_len
> bex
->fe_len
)
1489 } else if (ex
->fe_len
> gex
->fe_len
) {
1490 /* if the request is satisfied, then we try to find
1491 * an extent that still satisfy the request, but is
1492 * smaller than previous one */
1493 if (ex
->fe_len
< bex
->fe_len
)
1497 ext4_mb_check_limits(ac
, e4b
, 0);
1500 static int ext4_mb_try_best_found(struct ext4_allocation_context
*ac
,
1501 struct ext4_buddy
*e4b
)
1503 struct ext4_free_extent ex
= ac
->ac_b_ex
;
1504 ext4_group_t group
= ex
.fe_group
;
1508 BUG_ON(ex
.fe_len
<= 0);
1509 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1513 ext4_lock_group(ac
->ac_sb
, group
);
1514 max
= mb_find_extent(e4b
, 0, ex
.fe_start
, ex
.fe_len
, &ex
);
1518 ext4_mb_use_best_found(ac
, e4b
);
1521 ext4_unlock_group(ac
->ac_sb
, group
);
1522 ext4_mb_release_desc(e4b
);
1527 static int ext4_mb_find_by_goal(struct ext4_allocation_context
*ac
,
1528 struct ext4_buddy
*e4b
)
1530 ext4_group_t group
= ac
->ac_g_ex
.fe_group
;
1533 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1534 struct ext4_super_block
*es
= sbi
->s_es
;
1535 struct ext4_free_extent ex
;
1537 if (!(ac
->ac_flags
& EXT4_MB_HINT_TRY_GOAL
))
1540 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1544 ext4_lock_group(ac
->ac_sb
, group
);
1545 max
= mb_find_extent(e4b
, 0, ac
->ac_g_ex
.fe_start
,
1546 ac
->ac_g_ex
.fe_len
, &ex
);
1548 if (max
>= ac
->ac_g_ex
.fe_len
&& ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
) {
1551 start
= (e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
)) +
1552 ex
.fe_start
+ le32_to_cpu(es
->s_first_data_block
);
1553 /* use do_div to get remainder (would be 64-bit modulo) */
1554 if (do_div(start
, sbi
->s_stripe
) == 0) {
1557 ext4_mb_use_best_found(ac
, e4b
);
1559 } else if (max
>= ac
->ac_g_ex
.fe_len
) {
1560 BUG_ON(ex
.fe_len
<= 0);
1561 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1562 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1565 ext4_mb_use_best_found(ac
, e4b
);
1566 } else if (max
> 0 && (ac
->ac_flags
& EXT4_MB_HINT_MERGE
)) {
1567 /* Sometimes, caller may want to merge even small
1568 * number of blocks to an existing extent */
1569 BUG_ON(ex
.fe_len
<= 0);
1570 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1571 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1574 ext4_mb_use_best_found(ac
, e4b
);
1576 ext4_unlock_group(ac
->ac_sb
, group
);
1577 ext4_mb_release_desc(e4b
);
1583 * The routine scans buddy structures (not bitmap!) from given order
1584 * to max order and tries to find big enough chunk to satisfy the req
1586 static void ext4_mb_simple_scan_group(struct ext4_allocation_context
*ac
,
1587 struct ext4_buddy
*e4b
)
1589 struct super_block
*sb
= ac
->ac_sb
;
1590 struct ext4_group_info
*grp
= e4b
->bd_info
;
1596 BUG_ON(ac
->ac_2order
<= 0);
1597 for (i
= ac
->ac_2order
; i
<= sb
->s_blocksize_bits
+ 1; i
++) {
1598 if (grp
->bb_counters
[i
] == 0)
1601 buddy
= mb_find_buddy(e4b
, i
, &max
);
1602 BUG_ON(buddy
== NULL
);
1604 k
= mb_find_next_zero_bit(buddy
, max
, 0);
1609 ac
->ac_b_ex
.fe_len
= 1 << i
;
1610 ac
->ac_b_ex
.fe_start
= k
<< i
;
1611 ac
->ac_b_ex
.fe_group
= e4b
->bd_group
;
1613 ext4_mb_use_best_found(ac
, e4b
);
1615 BUG_ON(ac
->ac_b_ex
.fe_len
!= ac
->ac_g_ex
.fe_len
);
1617 if (EXT4_SB(sb
)->s_mb_stats
)
1618 atomic_inc(&EXT4_SB(sb
)->s_bal_2orders
);
1625 * The routine scans the group and measures all found extents.
1626 * In order to optimize scanning, caller must pass number of
1627 * free blocks in the group, so the routine can know upper limit.
1629 static void ext4_mb_complex_scan_group(struct ext4_allocation_context
*ac
,
1630 struct ext4_buddy
*e4b
)
1632 struct super_block
*sb
= ac
->ac_sb
;
1633 void *bitmap
= EXT4_MB_BITMAP(e4b
);
1634 struct ext4_free_extent ex
;
1638 free
= e4b
->bd_info
->bb_free
;
1641 i
= e4b
->bd_info
->bb_first_free
;
1643 while (free
&& ac
->ac_status
== AC_STATUS_CONTINUE
) {
1644 i
= mb_find_next_zero_bit(bitmap
,
1645 EXT4_BLOCKS_PER_GROUP(sb
), i
);
1646 if (i
>= EXT4_BLOCKS_PER_GROUP(sb
)) {
1648 * IF we have corrupt bitmap, we won't find any
1649 * free blocks even though group info says we
1650 * we have free blocks
1652 ext4_grp_locked_error(sb
, e4b
->bd_group
,
1653 __func__
, "%d free blocks as per "
1654 "group info. But bitmap says 0",
1659 mb_find_extent(e4b
, 0, i
, ac
->ac_g_ex
.fe_len
, &ex
);
1660 BUG_ON(ex
.fe_len
<= 0);
1661 if (free
< ex
.fe_len
) {
1662 ext4_grp_locked_error(sb
, e4b
->bd_group
,
1663 __func__
, "%d free blocks as per "
1664 "group info. But got %d blocks",
1667 * The number of free blocks differs. This mostly
1668 * indicate that the bitmap is corrupt. So exit
1669 * without claiming the space.
1674 ext4_mb_measure_extent(ac
, &ex
, e4b
);
1680 ext4_mb_check_limits(ac
, e4b
, 1);
1684 * This is a special case for storages like raid5
1685 * we try to find stripe-aligned chunks for stripe-size requests
1686 * XXX should do so at least for multiples of stripe size as well
1688 static void ext4_mb_scan_aligned(struct ext4_allocation_context
*ac
,
1689 struct ext4_buddy
*e4b
)
1691 struct super_block
*sb
= ac
->ac_sb
;
1692 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1693 void *bitmap
= EXT4_MB_BITMAP(e4b
);
1694 struct ext4_free_extent ex
;
1695 ext4_fsblk_t first_group_block
;
1700 BUG_ON(sbi
->s_stripe
== 0);
1702 /* find first stripe-aligned block in group */
1703 first_group_block
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
)
1704 + le32_to_cpu(sbi
->s_es
->s_first_data_block
);
1705 a
= first_group_block
+ sbi
->s_stripe
- 1;
1706 do_div(a
, sbi
->s_stripe
);
1707 i
= (a
* sbi
->s_stripe
) - first_group_block
;
1709 while (i
< EXT4_BLOCKS_PER_GROUP(sb
)) {
1710 if (!mb_test_bit(i
, bitmap
)) {
1711 max
= mb_find_extent(e4b
, 0, i
, sbi
->s_stripe
, &ex
);
1712 if (max
>= sbi
->s_stripe
) {
1715 ext4_mb_use_best_found(ac
, e4b
);
1723 static int ext4_mb_good_group(struct ext4_allocation_context
*ac
,
1724 ext4_group_t group
, int cr
)
1726 unsigned free
, fragments
;
1728 int flex_size
= ext4_flex_bg_size(EXT4_SB(ac
->ac_sb
));
1729 struct ext4_group_desc
*desc
;
1730 struct ext4_group_info
*grp
= ext4_get_group_info(ac
->ac_sb
, group
);
1732 BUG_ON(cr
< 0 || cr
>= 4);
1733 BUG_ON(EXT4_MB_GRP_NEED_INIT(grp
));
1735 free
= grp
->bb_free
;
1736 fragments
= grp
->bb_fragments
;
1744 BUG_ON(ac
->ac_2order
== 0);
1745 /* If this group is uninitialized, skip it initially */
1746 desc
= ext4_get_group_desc(ac
->ac_sb
, group
, NULL
);
1747 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
))
1750 /* Avoid using the first bg of a flexgroup for data files */
1751 if ((ac
->ac_flags
& EXT4_MB_HINT_DATA
) &&
1752 (flex_size
>= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
) &&
1753 ((group
% flex_size
) == 0))
1756 bits
= ac
->ac_sb
->s_blocksize_bits
+ 1;
1757 for (i
= ac
->ac_2order
; i
<= bits
; i
++)
1758 if (grp
->bb_counters
[i
] > 0)
1762 if ((free
/ fragments
) >= ac
->ac_g_ex
.fe_len
)
1766 if (free
>= ac
->ac_g_ex
.fe_len
)
1779 * lock the group_info alloc_sem of all the groups
1780 * belonging to the same buddy cache page. This
1781 * make sure other parallel operation on the buddy
1782 * cache doesn't happen whild holding the buddy cache
1785 int ext4_mb_get_buddy_cache_lock(struct super_block
*sb
, ext4_group_t group
)
1789 int blocks_per_page
;
1790 int groups_per_page
;
1791 ext4_group_t first_group
;
1792 struct ext4_group_info
*grp
;
1794 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1796 * the buddy cache inode stores the block bitmap
1797 * and buddy information in consecutive blocks.
1798 * So for each group we need two blocks.
1801 pnum
= block
/ blocks_per_page
;
1802 first_group
= pnum
* blocks_per_page
/ 2;
1804 groups_per_page
= blocks_per_page
>> 1;
1805 if (groups_per_page
== 0)
1806 groups_per_page
= 1;
1807 /* read all groups the page covers into the cache */
1808 for (i
= 0; i
< groups_per_page
; i
++) {
1810 if ((first_group
+ i
) >= EXT4_SB(sb
)->s_groups_count
)
1812 grp
= ext4_get_group_info(sb
, first_group
+ i
);
1813 /* take all groups write allocation
1814 * semaphore. This make sure there is
1815 * no block allocation going on in any
1818 down_write_nested(&grp
->alloc_sem
, i
);
1823 void ext4_mb_put_buddy_cache_lock(struct super_block
*sb
,
1824 ext4_group_t group
, int locked_group
)
1828 int blocks_per_page
;
1829 ext4_group_t first_group
;
1830 struct ext4_group_info
*grp
;
1832 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1834 * the buddy cache inode stores the block bitmap
1835 * and buddy information in consecutive blocks.
1836 * So for each group we need two blocks.
1839 pnum
= block
/ blocks_per_page
;
1840 first_group
= pnum
* blocks_per_page
/ 2;
1841 /* release locks on all the groups */
1842 for (i
= 0; i
< locked_group
; i
++) {
1844 grp
= ext4_get_group_info(sb
, first_group
+ i
);
1845 /* take all groups write allocation
1846 * semaphore. This make sure there is
1847 * no block allocation going on in any
1850 up_write(&grp
->alloc_sem
);
1855 static int ext4_mb_init_group(struct super_block
*sb
, ext4_group_t group
)
1860 int blocks_per_page
;
1861 int block
, pnum
, poff
;
1862 int num_grp_locked
= 0;
1863 struct ext4_group_info
*this_grp
;
1864 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1865 struct inode
*inode
= sbi
->s_buddy_cache
;
1866 struct page
*page
= NULL
, *bitmap_page
= NULL
;
1868 mb_debug("init group %lu\n", group
);
1869 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1870 this_grp
= ext4_get_group_info(sb
, group
);
1872 * This ensures we don't add group
1873 * to this buddy cache via resize
1875 num_grp_locked
= ext4_mb_get_buddy_cache_lock(sb
, group
);
1876 if (!EXT4_MB_GRP_NEED_INIT(this_grp
)) {
1878 * somebody initialized the group
1879 * return without doing anything
1885 * the buddy cache inode stores the block bitmap
1886 * and buddy information in consecutive blocks.
1887 * So for each group we need two blocks.
1890 pnum
= block
/ blocks_per_page
;
1891 poff
= block
% blocks_per_page
;
1892 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1894 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1895 ret
= ext4_mb_init_cache(page
, NULL
);
1902 if (page
== NULL
|| !PageUptodate(page
)) {
1906 mark_page_accessed(page
);
1908 bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
1910 /* init buddy cache */
1912 pnum
= block
/ blocks_per_page
;
1913 poff
= block
% blocks_per_page
;
1914 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1915 if (page
== bitmap_page
) {
1917 * If both the bitmap and buddy are in
1918 * the same page we don't need to force
1923 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1924 ret
= ext4_mb_init_cache(page
, bitmap
);
1931 if (page
== NULL
|| !PageUptodate(page
)) {
1935 mark_page_accessed(page
);
1937 ext4_mb_put_buddy_cache_lock(sb
, group
, num_grp_locked
);
1939 page_cache_release(bitmap_page
);
1941 page_cache_release(page
);
1945 static noinline_for_stack
int
1946 ext4_mb_regular_allocator(struct ext4_allocation_context
*ac
)
1953 struct ext4_sb_info
*sbi
;
1954 struct super_block
*sb
;
1955 struct ext4_buddy e4b
;
1960 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1962 /* first, try the goal */
1963 err
= ext4_mb_find_by_goal(ac
, &e4b
);
1964 if (err
|| ac
->ac_status
== AC_STATUS_FOUND
)
1967 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
1971 * ac->ac2_order is set only if the fe_len is a power of 2
1972 * if ac2_order is set we also set criteria to 0 so that we
1973 * try exact allocation using buddy.
1975 i
= fls(ac
->ac_g_ex
.fe_len
);
1978 * We search using buddy data only if the order of the request
1979 * is greater than equal to the sbi_s_mb_order2_reqs
1980 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
1982 if (i
>= sbi
->s_mb_order2_reqs
) {
1984 * This should tell if fe_len is exactly power of 2
1986 if ((ac
->ac_g_ex
.fe_len
& (~(1 << (i
- 1)))) == 0)
1987 ac
->ac_2order
= i
- 1;
1990 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
1991 /* if stream allocation is enabled, use global goal */
1992 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
1993 isize
= i_size_read(ac
->ac_inode
) >> bsbits
;
1997 if (size
< sbi
->s_mb_stream_request
&&
1998 (ac
->ac_flags
& EXT4_MB_HINT_DATA
)) {
1999 /* TBD: may be hot point */
2000 spin_lock(&sbi
->s_md_lock
);
2001 ac
->ac_g_ex
.fe_group
= sbi
->s_mb_last_group
;
2002 ac
->ac_g_ex
.fe_start
= sbi
->s_mb_last_start
;
2003 spin_unlock(&sbi
->s_md_lock
);
2005 /* Let's just scan groups to find more-less suitable blocks */
2006 cr
= ac
->ac_2order
? 0 : 1;
2008 * cr == 0 try to get exact allocation,
2009 * cr == 3 try to get anything
2012 for (; cr
< 4 && ac
->ac_status
== AC_STATUS_CONTINUE
; cr
++) {
2013 ac
->ac_criteria
= cr
;
2015 * searching for the right group start
2016 * from the goal value specified
2018 group
= ac
->ac_g_ex
.fe_group
;
2020 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; group
++, i
++) {
2021 struct ext4_group_info
*grp
;
2022 struct ext4_group_desc
*desc
;
2024 if (group
== EXT4_SB(sb
)->s_groups_count
)
2027 /* quick check to skip empty groups */
2028 grp
= ext4_get_group_info(sb
, group
);
2029 if (grp
->bb_free
== 0)
2033 * if the group is already init we check whether it is
2034 * a good group and if not we don't load the buddy
2036 if (EXT4_MB_GRP_NEED_INIT(grp
)) {
2038 * we need full data about the group
2039 * to make a good selection
2041 err
= ext4_mb_init_group(sb
, group
);
2047 * If the particular group doesn't satisfy our
2048 * criteria we continue with the next group
2050 if (!ext4_mb_good_group(ac
, group
, cr
))
2053 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2057 ext4_lock_group(sb
, group
);
2058 if (!ext4_mb_good_group(ac
, group
, cr
)) {
2059 /* someone did allocation from this group */
2060 ext4_unlock_group(sb
, group
);
2061 ext4_mb_release_desc(&e4b
);
2065 ac
->ac_groups_scanned
++;
2066 desc
= ext4_get_group_desc(sb
, group
, NULL
);
2067 if (cr
== 0 || (desc
->bg_flags
&
2068 cpu_to_le16(EXT4_BG_BLOCK_UNINIT
) &&
2069 ac
->ac_2order
!= 0))
2070 ext4_mb_simple_scan_group(ac
, &e4b
);
2072 ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
)
2073 ext4_mb_scan_aligned(ac
, &e4b
);
2075 ext4_mb_complex_scan_group(ac
, &e4b
);
2077 ext4_unlock_group(sb
, group
);
2078 ext4_mb_release_desc(&e4b
);
2080 if (ac
->ac_status
!= AC_STATUS_CONTINUE
)
2085 if (ac
->ac_b_ex
.fe_len
> 0 && ac
->ac_status
!= AC_STATUS_FOUND
&&
2086 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
2088 * We've been searching too long. Let's try to allocate
2089 * the best chunk we've found so far
2092 ext4_mb_try_best_found(ac
, &e4b
);
2093 if (ac
->ac_status
!= AC_STATUS_FOUND
) {
2095 * Someone more lucky has already allocated it.
2096 * The only thing we can do is just take first
2098 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2100 ac
->ac_b_ex
.fe_group
= 0;
2101 ac
->ac_b_ex
.fe_start
= 0;
2102 ac
->ac_b_ex
.fe_len
= 0;
2103 ac
->ac_status
= AC_STATUS_CONTINUE
;
2104 ac
->ac_flags
|= EXT4_MB_HINT_FIRST
;
2106 atomic_inc(&sbi
->s_mb_lost_chunks
);
2114 #ifdef EXT4_MB_HISTORY
2115 struct ext4_mb_proc_session
{
2116 struct ext4_mb_history
*history
;
2117 struct super_block
*sb
;
2122 static void *ext4_mb_history_skip_empty(struct ext4_mb_proc_session
*s
,
2123 struct ext4_mb_history
*hs
,
2126 if (hs
== s
->history
+ s
->max
)
2128 if (!first
&& hs
== s
->history
+ s
->start
)
2130 while (hs
->orig
.fe_len
== 0) {
2132 if (hs
== s
->history
+ s
->max
)
2134 if (hs
== s
->history
+ s
->start
)
2140 static void *ext4_mb_seq_history_start(struct seq_file
*seq
, loff_t
*pos
)
2142 struct ext4_mb_proc_session
*s
= seq
->private;
2143 struct ext4_mb_history
*hs
;
2147 return SEQ_START_TOKEN
;
2148 hs
= ext4_mb_history_skip_empty(s
, s
->history
+ s
->start
, 1);
2151 while (--l
&& (hs
= ext4_mb_history_skip_empty(s
, ++hs
, 0)) != NULL
);
2155 static void *ext4_mb_seq_history_next(struct seq_file
*seq
, void *v
,
2158 struct ext4_mb_proc_session
*s
= seq
->private;
2159 struct ext4_mb_history
*hs
= v
;
2162 if (v
== SEQ_START_TOKEN
)
2163 return ext4_mb_history_skip_empty(s
, s
->history
+ s
->start
, 1);
2165 return ext4_mb_history_skip_empty(s
, ++hs
, 0);
2168 static int ext4_mb_seq_history_show(struct seq_file
*seq
, void *v
)
2170 char buf
[25], buf2
[25], buf3
[25], *fmt
;
2171 struct ext4_mb_history
*hs
= v
;
2173 if (v
== SEQ_START_TOKEN
) {
2174 seq_printf(seq
, "%-5s %-8s %-23s %-23s %-23s %-5s "
2175 "%-5s %-2s %-5s %-5s %-5s %-6s\n",
2176 "pid", "inode", "original", "goal", "result", "found",
2177 "grps", "cr", "flags", "merge", "tail", "broken");
2181 if (hs
->op
== EXT4_MB_HISTORY_ALLOC
) {
2182 fmt
= "%-5u %-8u %-23s %-23s %-23s %-5u %-5u %-2u "
2183 "%-5u %-5s %-5u %-6u\n";
2184 sprintf(buf2
, "%u/%d/%u@%u", hs
->result
.fe_group
,
2185 hs
->result
.fe_start
, hs
->result
.fe_len
,
2186 hs
->result
.fe_logical
);
2187 sprintf(buf
, "%u/%d/%u@%u", hs
->orig
.fe_group
,
2188 hs
->orig
.fe_start
, hs
->orig
.fe_len
,
2189 hs
->orig
.fe_logical
);
2190 sprintf(buf3
, "%u/%d/%u@%u", hs
->goal
.fe_group
,
2191 hs
->goal
.fe_start
, hs
->goal
.fe_len
,
2192 hs
->goal
.fe_logical
);
2193 seq_printf(seq
, fmt
, hs
->pid
, hs
->ino
, buf
, buf3
, buf2
,
2194 hs
->found
, hs
->groups
, hs
->cr
, hs
->flags
,
2195 hs
->merged
? "M" : "", hs
->tail
,
2196 hs
->buddy
? 1 << hs
->buddy
: 0);
2197 } else if (hs
->op
== EXT4_MB_HISTORY_PREALLOC
) {
2198 fmt
= "%-5u %-8u %-23s %-23s %-23s\n";
2199 sprintf(buf2
, "%u/%d/%u@%u", hs
->result
.fe_group
,
2200 hs
->result
.fe_start
, hs
->result
.fe_len
,
2201 hs
->result
.fe_logical
);
2202 sprintf(buf
, "%u/%d/%u@%u", hs
->orig
.fe_group
,
2203 hs
->orig
.fe_start
, hs
->orig
.fe_len
,
2204 hs
->orig
.fe_logical
);
2205 seq_printf(seq
, fmt
, hs
->pid
, hs
->ino
, buf
, "", buf2
);
2206 } else if (hs
->op
== EXT4_MB_HISTORY_DISCARD
) {
2207 sprintf(buf2
, "%u/%d/%u", hs
->result
.fe_group
,
2208 hs
->result
.fe_start
, hs
->result
.fe_len
);
2209 seq_printf(seq
, "%-5u %-8u %-23s discard\n",
2210 hs
->pid
, hs
->ino
, buf2
);
2211 } else if (hs
->op
== EXT4_MB_HISTORY_FREE
) {
2212 sprintf(buf2
, "%u/%d/%u", hs
->result
.fe_group
,
2213 hs
->result
.fe_start
, hs
->result
.fe_len
);
2214 seq_printf(seq
, "%-5u %-8u %-23s free\n",
2215 hs
->pid
, hs
->ino
, buf2
);
2220 static void ext4_mb_seq_history_stop(struct seq_file
*seq
, void *v
)
2224 static struct seq_operations ext4_mb_seq_history_ops
= {
2225 .start
= ext4_mb_seq_history_start
,
2226 .next
= ext4_mb_seq_history_next
,
2227 .stop
= ext4_mb_seq_history_stop
,
2228 .show
= ext4_mb_seq_history_show
,
2231 static int ext4_mb_seq_history_open(struct inode
*inode
, struct file
*file
)
2233 struct super_block
*sb
= PDE(inode
)->data
;
2234 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2235 struct ext4_mb_proc_session
*s
;
2239 if (unlikely(sbi
->s_mb_history
== NULL
))
2241 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
2245 size
= sizeof(struct ext4_mb_history
) * sbi
->s_mb_history_max
;
2246 s
->history
= kmalloc(size
, GFP_KERNEL
);
2247 if (s
->history
== NULL
) {
2252 spin_lock(&sbi
->s_mb_history_lock
);
2253 memcpy(s
->history
, sbi
->s_mb_history
, size
);
2254 s
->max
= sbi
->s_mb_history_max
;
2255 s
->start
= sbi
->s_mb_history_cur
% s
->max
;
2256 spin_unlock(&sbi
->s_mb_history_lock
);
2258 rc
= seq_open(file
, &ext4_mb_seq_history_ops
);
2260 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2270 static int ext4_mb_seq_history_release(struct inode
*inode
, struct file
*file
)
2272 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
2273 struct ext4_mb_proc_session
*s
= seq
->private;
2276 return seq_release(inode
, file
);
2279 static ssize_t
ext4_mb_seq_history_write(struct file
*file
,
2280 const char __user
*buffer
,
2281 size_t count
, loff_t
*ppos
)
2283 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
2284 struct ext4_mb_proc_session
*s
= seq
->private;
2285 struct super_block
*sb
= s
->sb
;
2289 if (count
>= sizeof(str
)) {
2290 printk(KERN_ERR
"EXT4-fs: %s string too long, max %u bytes\n",
2291 "mb_history", (int)sizeof(str
));
2295 if (copy_from_user(str
, buffer
, count
))
2298 value
= simple_strtol(str
, NULL
, 0);
2301 EXT4_SB(sb
)->s_mb_history_filter
= value
;
2306 static struct file_operations ext4_mb_seq_history_fops
= {
2307 .owner
= THIS_MODULE
,
2308 .open
= ext4_mb_seq_history_open
,
2310 .write
= ext4_mb_seq_history_write
,
2311 .llseek
= seq_lseek
,
2312 .release
= ext4_mb_seq_history_release
,
2315 static void *ext4_mb_seq_groups_start(struct seq_file
*seq
, loff_t
*pos
)
2317 struct super_block
*sb
= seq
->private;
2318 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2321 if (*pos
< 0 || *pos
>= sbi
->s_groups_count
)
2325 return (void *) ((unsigned long) group
);
2328 static void *ext4_mb_seq_groups_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2330 struct super_block
*sb
= seq
->private;
2331 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2335 if (*pos
< 0 || *pos
>= sbi
->s_groups_count
)
2338 return (void *) ((unsigned long) group
);
2341 static int ext4_mb_seq_groups_show(struct seq_file
*seq
, void *v
)
2343 struct super_block
*sb
= seq
->private;
2344 ext4_group_t group
= (ext4_group_t
) ((unsigned long) v
);
2347 struct ext4_buddy e4b
;
2349 struct ext4_group_info info
;
2350 unsigned short counters
[16];
2355 seq_printf(seq
, "#%-5s: %-5s %-5s %-5s "
2356 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2357 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2358 "group", "free", "frags", "first",
2359 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2360 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2362 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(sg
.info
.bb_counters
[0]) +
2363 sizeof(struct ext4_group_info
);
2364 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2366 seq_printf(seq
, "#%-5u: I/O error\n", group
);
2369 ext4_lock_group(sb
, group
);
2370 memcpy(&sg
, ext4_get_group_info(sb
, group
), i
);
2371 ext4_unlock_group(sb
, group
);
2372 ext4_mb_release_desc(&e4b
);
2374 seq_printf(seq
, "#%-5u: %-5u %-5u %-5u [", group
, sg
.info
.bb_free
,
2375 sg
.info
.bb_fragments
, sg
.info
.bb_first_free
);
2376 for (i
= 0; i
<= 13; i
++)
2377 seq_printf(seq
, " %-5u", i
<= sb
->s_blocksize_bits
+ 1 ?
2378 sg
.info
.bb_counters
[i
] : 0);
2379 seq_printf(seq
, " ]\n");
2384 static void ext4_mb_seq_groups_stop(struct seq_file
*seq
, void *v
)
2388 static struct seq_operations ext4_mb_seq_groups_ops
= {
2389 .start
= ext4_mb_seq_groups_start
,
2390 .next
= ext4_mb_seq_groups_next
,
2391 .stop
= ext4_mb_seq_groups_stop
,
2392 .show
= ext4_mb_seq_groups_show
,
2395 static int ext4_mb_seq_groups_open(struct inode
*inode
, struct file
*file
)
2397 struct super_block
*sb
= PDE(inode
)->data
;
2400 rc
= seq_open(file
, &ext4_mb_seq_groups_ops
);
2402 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2409 static struct file_operations ext4_mb_seq_groups_fops
= {
2410 .owner
= THIS_MODULE
,
2411 .open
= ext4_mb_seq_groups_open
,
2413 .llseek
= seq_lseek
,
2414 .release
= seq_release
,
2417 static void ext4_mb_history_release(struct super_block
*sb
)
2419 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2421 if (sbi
->s_proc
!= NULL
) {
2422 remove_proc_entry("mb_groups", sbi
->s_proc
);
2423 remove_proc_entry("mb_history", sbi
->s_proc
);
2425 kfree(sbi
->s_mb_history
);
2428 static void ext4_mb_history_init(struct super_block
*sb
)
2430 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2433 if (sbi
->s_proc
!= NULL
) {
2434 proc_create_data("mb_history", S_IRUGO
, sbi
->s_proc
,
2435 &ext4_mb_seq_history_fops
, sb
);
2436 proc_create_data("mb_groups", S_IRUGO
, sbi
->s_proc
,
2437 &ext4_mb_seq_groups_fops
, sb
);
2440 sbi
->s_mb_history_max
= 1000;
2441 sbi
->s_mb_history_cur
= 0;
2442 spin_lock_init(&sbi
->s_mb_history_lock
);
2443 i
= sbi
->s_mb_history_max
* sizeof(struct ext4_mb_history
);
2444 sbi
->s_mb_history
= kzalloc(i
, GFP_KERNEL
);
2445 /* if we can't allocate history, then we simple won't use it */
2448 static noinline_for_stack
void
2449 ext4_mb_store_history(struct ext4_allocation_context
*ac
)
2451 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
2452 struct ext4_mb_history h
;
2454 if (unlikely(sbi
->s_mb_history
== NULL
))
2457 if (!(ac
->ac_op
& sbi
->s_mb_history_filter
))
2461 h
.pid
= current
->pid
;
2462 h
.ino
= ac
->ac_inode
? ac
->ac_inode
->i_ino
: 0;
2463 h
.orig
= ac
->ac_o_ex
;
2464 h
.result
= ac
->ac_b_ex
;
2465 h
.flags
= ac
->ac_flags
;
2466 h
.found
= ac
->ac_found
;
2467 h
.groups
= ac
->ac_groups_scanned
;
2468 h
.cr
= ac
->ac_criteria
;
2469 h
.tail
= ac
->ac_tail
;
2470 h
.buddy
= ac
->ac_buddy
;
2472 if (ac
->ac_op
== EXT4_MB_HISTORY_ALLOC
) {
2473 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
2474 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
2476 h
.goal
= ac
->ac_g_ex
;
2477 h
.result
= ac
->ac_f_ex
;
2480 spin_lock(&sbi
->s_mb_history_lock
);
2481 memcpy(sbi
->s_mb_history
+ sbi
->s_mb_history_cur
, &h
, sizeof(h
));
2482 if (++sbi
->s_mb_history_cur
>= sbi
->s_mb_history_max
)
2483 sbi
->s_mb_history_cur
= 0;
2484 spin_unlock(&sbi
->s_mb_history_lock
);
2488 #define ext4_mb_history_release(sb)
2489 #define ext4_mb_history_init(sb)
2493 /* Create and initialize ext4_group_info data for the given group. */
2494 int ext4_mb_add_groupinfo(struct super_block
*sb
, ext4_group_t group
,
2495 struct ext4_group_desc
*desc
)
2499 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2500 struct ext4_group_info
**meta_group_info
;
2503 * First check if this group is the first of a reserved block.
2504 * If it's true, we have to allocate a new table of pointers
2505 * to ext4_group_info structures
2507 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0) {
2508 metalen
= sizeof(*meta_group_info
) <<
2509 EXT4_DESC_PER_BLOCK_BITS(sb
);
2510 meta_group_info
= kmalloc(metalen
, GFP_KERNEL
);
2511 if (meta_group_info
== NULL
) {
2512 printk(KERN_ERR
"EXT4-fs: can't allocate mem for a "
2514 goto exit_meta_group_info
;
2516 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)] =
2521 * calculate needed size. if change bb_counters size,
2522 * don't forget about ext4_mb_generate_buddy()
2524 len
= offsetof(typeof(**meta_group_info
),
2525 bb_counters
[sb
->s_blocksize_bits
+ 2]);
2528 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)];
2529 i
= group
& (EXT4_DESC_PER_BLOCK(sb
) - 1);
2531 meta_group_info
[i
] = kzalloc(len
, GFP_KERNEL
);
2532 if (meta_group_info
[i
] == NULL
) {
2533 printk(KERN_ERR
"EXT4-fs: can't allocate buddy mem\n");
2534 goto exit_group_info
;
2536 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
,
2537 &(meta_group_info
[i
]->bb_state
));
2540 * initialize bb_free to be able to skip
2541 * empty groups without initialization
2543 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2544 meta_group_info
[i
]->bb_free
=
2545 ext4_free_blocks_after_init(sb
, group
, desc
);
2547 meta_group_info
[i
]->bb_free
=
2548 ext4_free_blks_count(sb
, desc
);
2551 INIT_LIST_HEAD(&meta_group_info
[i
]->bb_prealloc_list
);
2552 init_rwsem(&meta_group_info
[i
]->alloc_sem
);
2553 meta_group_info
[i
]->bb_free_root
.rb_node
= NULL
;;
2557 struct buffer_head
*bh
;
2558 meta_group_info
[i
]->bb_bitmap
=
2559 kmalloc(sb
->s_blocksize
, GFP_KERNEL
);
2560 BUG_ON(meta_group_info
[i
]->bb_bitmap
== NULL
);
2561 bh
= ext4_read_block_bitmap(sb
, group
);
2563 memcpy(meta_group_info
[i
]->bb_bitmap
, bh
->b_data
,
2572 /* If a meta_group_info table has been allocated, release it now */
2573 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0)
2574 kfree(sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)]);
2575 exit_meta_group_info
:
2577 } /* ext4_mb_add_groupinfo */
2580 * Update an existing group.
2581 * This function is used for online resize
2583 void ext4_mb_update_group_info(struct ext4_group_info
*grp
, ext4_grpblk_t add
)
2585 grp
->bb_free
+= add
;
2588 static int ext4_mb_init_backend(struct super_block
*sb
)
2592 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2593 struct ext4_super_block
*es
= sbi
->s_es
;
2594 int num_meta_group_infos
;
2595 int num_meta_group_infos_max
;
2597 struct ext4_group_info
**meta_group_info
;
2598 struct ext4_group_desc
*desc
;
2600 /* This is the number of blocks used by GDT */
2601 num_meta_group_infos
= (sbi
->s_groups_count
+ EXT4_DESC_PER_BLOCK(sb
) -
2602 1) >> EXT4_DESC_PER_BLOCK_BITS(sb
);
2605 * This is the total number of blocks used by GDT including
2606 * the number of reserved blocks for GDT.
2607 * The s_group_info array is allocated with this value
2608 * to allow a clean online resize without a complex
2609 * manipulation of pointer.
2610 * The drawback is the unused memory when no resize
2611 * occurs but it's very low in terms of pages
2612 * (see comments below)
2613 * Need to handle this properly when META_BG resizing is allowed
2615 num_meta_group_infos_max
= num_meta_group_infos
+
2616 le16_to_cpu(es
->s_reserved_gdt_blocks
);
2619 * array_size is the size of s_group_info array. We round it
2620 * to the next power of two because this approximation is done
2621 * internally by kmalloc so we can have some more memory
2622 * for free here (e.g. may be used for META_BG resize).
2625 while (array_size
< sizeof(*sbi
->s_group_info
) *
2626 num_meta_group_infos_max
)
2627 array_size
= array_size
<< 1;
2628 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2629 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2630 * So a two level scheme suffices for now. */
2631 sbi
->s_group_info
= kmalloc(array_size
, GFP_KERNEL
);
2632 if (sbi
->s_group_info
== NULL
) {
2633 printk(KERN_ERR
"EXT4-fs: can't allocate buddy meta group\n");
2636 sbi
->s_buddy_cache
= new_inode(sb
);
2637 if (sbi
->s_buddy_cache
== NULL
) {
2638 printk(KERN_ERR
"EXT4-fs: can't get new inode\n");
2641 EXT4_I(sbi
->s_buddy_cache
)->i_disksize
= 0;
2643 metalen
= sizeof(*meta_group_info
) << EXT4_DESC_PER_BLOCK_BITS(sb
);
2644 for (i
= 0; i
< num_meta_group_infos
; i
++) {
2645 if ((i
+ 1) == num_meta_group_infos
)
2646 metalen
= sizeof(*meta_group_info
) *
2647 (sbi
->s_groups_count
-
2648 (i
<< EXT4_DESC_PER_BLOCK_BITS(sb
)));
2649 meta_group_info
= kmalloc(metalen
, GFP_KERNEL
);
2650 if (meta_group_info
== NULL
) {
2651 printk(KERN_ERR
"EXT4-fs: can't allocate mem for a "
2655 sbi
->s_group_info
[i
] = meta_group_info
;
2658 for (i
= 0; i
< sbi
->s_groups_count
; i
++) {
2659 desc
= ext4_get_group_desc(sb
, i
, NULL
);
2662 "EXT4-fs: can't read descriptor %u\n", i
);
2665 if (ext4_mb_add_groupinfo(sb
, i
, desc
) != 0)
2673 kfree(ext4_get_group_info(sb
, i
));
2674 i
= num_meta_group_infos
;
2677 kfree(sbi
->s_group_info
[i
]);
2678 iput(sbi
->s_buddy_cache
);
2680 kfree(sbi
->s_group_info
);
2684 int ext4_mb_init(struct super_block
*sb
, int needs_recovery
)
2686 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2692 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(unsigned short);
2694 sbi
->s_mb_offsets
= kmalloc(i
, GFP_KERNEL
);
2695 if (sbi
->s_mb_offsets
== NULL
) {
2699 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(unsigned int);
2700 sbi
->s_mb_maxs
= kmalloc(i
, GFP_KERNEL
);
2701 if (sbi
->s_mb_maxs
== NULL
) {
2702 kfree(sbi
->s_mb_offsets
);
2706 /* order 0 is regular bitmap */
2707 sbi
->s_mb_maxs
[0] = sb
->s_blocksize
<< 3;
2708 sbi
->s_mb_offsets
[0] = 0;
2712 max
= sb
->s_blocksize
<< 2;
2714 sbi
->s_mb_offsets
[i
] = offset
;
2715 sbi
->s_mb_maxs
[i
] = max
;
2716 offset
+= 1 << (sb
->s_blocksize_bits
- i
);
2719 } while (i
<= sb
->s_blocksize_bits
+ 1);
2721 /* init file for buddy data */
2722 ret
= ext4_mb_init_backend(sb
);
2724 kfree(sbi
->s_mb_offsets
);
2725 kfree(sbi
->s_mb_maxs
);
2729 spin_lock_init(&sbi
->s_md_lock
);
2730 spin_lock_init(&sbi
->s_bal_lock
);
2732 sbi
->s_mb_max_to_scan
= MB_DEFAULT_MAX_TO_SCAN
;
2733 sbi
->s_mb_min_to_scan
= MB_DEFAULT_MIN_TO_SCAN
;
2734 sbi
->s_mb_stats
= MB_DEFAULT_STATS
;
2735 sbi
->s_mb_stream_request
= MB_DEFAULT_STREAM_THRESHOLD
;
2736 sbi
->s_mb_order2_reqs
= MB_DEFAULT_ORDER2_REQS
;
2737 sbi
->s_mb_history_filter
= EXT4_MB_HISTORY_DEFAULT
;
2738 sbi
->s_mb_group_prealloc
= MB_DEFAULT_GROUP_PREALLOC
;
2740 sbi
->s_locality_groups
= alloc_percpu(struct ext4_locality_group
);
2741 if (sbi
->s_locality_groups
== NULL
) {
2742 kfree(sbi
->s_mb_offsets
);
2743 kfree(sbi
->s_mb_maxs
);
2746 for_each_possible_cpu(i
) {
2747 struct ext4_locality_group
*lg
;
2748 lg
= per_cpu_ptr(sbi
->s_locality_groups
, i
);
2749 mutex_init(&lg
->lg_mutex
);
2750 for (j
= 0; j
< PREALLOC_TB_SIZE
; j
++)
2751 INIT_LIST_HEAD(&lg
->lg_prealloc_list
[j
]);
2752 spin_lock_init(&lg
->lg_prealloc_lock
);
2755 ext4_mb_history_init(sb
);
2758 sbi
->s_journal
->j_commit_callback
= release_blocks_on_commit
;
2760 printk(KERN_INFO
"EXT4-fs: mballoc enabled\n");
2764 /* need to called with ext4 group lock (ext4_lock_group) */
2765 static void ext4_mb_cleanup_pa(struct ext4_group_info
*grp
)
2767 struct ext4_prealloc_space
*pa
;
2768 struct list_head
*cur
, *tmp
;
2771 list_for_each_safe(cur
, tmp
, &grp
->bb_prealloc_list
) {
2772 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
2773 list_del(&pa
->pa_group_list
);
2775 kmem_cache_free(ext4_pspace_cachep
, pa
);
2778 mb_debug("mballoc: %u PAs left\n", count
);
2782 int ext4_mb_release(struct super_block
*sb
)
2785 int num_meta_group_infos
;
2786 struct ext4_group_info
*grinfo
;
2787 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2789 if (sbi
->s_group_info
) {
2790 for (i
= 0; i
< sbi
->s_groups_count
; i
++) {
2791 grinfo
= ext4_get_group_info(sb
, i
);
2793 kfree(grinfo
->bb_bitmap
);
2795 ext4_lock_group(sb
, i
);
2796 ext4_mb_cleanup_pa(grinfo
);
2797 ext4_unlock_group(sb
, i
);
2800 num_meta_group_infos
= (sbi
->s_groups_count
+
2801 EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2802 EXT4_DESC_PER_BLOCK_BITS(sb
);
2803 for (i
= 0; i
< num_meta_group_infos
; i
++)
2804 kfree(sbi
->s_group_info
[i
]);
2805 kfree(sbi
->s_group_info
);
2807 kfree(sbi
->s_mb_offsets
);
2808 kfree(sbi
->s_mb_maxs
);
2809 if (sbi
->s_buddy_cache
)
2810 iput(sbi
->s_buddy_cache
);
2811 if (sbi
->s_mb_stats
) {
2813 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2814 atomic_read(&sbi
->s_bal_allocated
),
2815 atomic_read(&sbi
->s_bal_reqs
),
2816 atomic_read(&sbi
->s_bal_success
));
2818 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2819 "%u 2^N hits, %u breaks, %u lost\n",
2820 atomic_read(&sbi
->s_bal_ex_scanned
),
2821 atomic_read(&sbi
->s_bal_goals
),
2822 atomic_read(&sbi
->s_bal_2orders
),
2823 atomic_read(&sbi
->s_bal_breaks
),
2824 atomic_read(&sbi
->s_mb_lost_chunks
));
2826 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2827 sbi
->s_mb_buddies_generated
++,
2828 sbi
->s_mb_generation_time
);
2830 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2831 atomic_read(&sbi
->s_mb_preallocated
),
2832 atomic_read(&sbi
->s_mb_discarded
));
2835 free_percpu(sbi
->s_locality_groups
);
2836 ext4_mb_history_release(sb
);
2842 * This function is called by the jbd2 layer once the commit has finished,
2843 * so we know we can free the blocks that were released with that commit.
2845 static void release_blocks_on_commit(journal_t
*journal
, transaction_t
*txn
)
2847 struct super_block
*sb
= journal
->j_private
;
2848 struct ext4_buddy e4b
;
2849 struct ext4_group_info
*db
;
2850 int err
, count
= 0, count2
= 0;
2851 struct ext4_free_data
*entry
;
2852 ext4_fsblk_t discard_block
;
2853 struct list_head
*l
, *ltmp
;
2855 list_for_each_safe(l
, ltmp
, &txn
->t_private_list
) {
2856 entry
= list_entry(l
, struct ext4_free_data
, list
);
2858 mb_debug("gonna free %u blocks in group %u (0x%p):",
2859 entry
->count
, entry
->group
, entry
);
2861 err
= ext4_mb_load_buddy(sb
, entry
->group
, &e4b
);
2862 /* we expect to find existing buddy because it's pinned */
2866 /* there are blocks to put in buddy to make them really free */
2867 count
+= entry
->count
;
2869 ext4_lock_group(sb
, entry
->group
);
2870 /* Take it out of per group rb tree */
2871 rb_erase(&entry
->node
, &(db
->bb_free_root
));
2872 mb_free_blocks(NULL
, &e4b
, entry
->start_blk
, entry
->count
);
2874 if (!db
->bb_free_root
.rb_node
) {
2875 /* No more items in the per group rb tree
2876 * balance refcounts from ext4_mb_free_metadata()
2878 page_cache_release(e4b
.bd_buddy_page
);
2879 page_cache_release(e4b
.bd_bitmap_page
);
2881 ext4_unlock_group(sb
, entry
->group
);
2882 discard_block
= (ext4_fsblk_t
) entry
->group
* EXT4_BLOCKS_PER_GROUP(sb
)
2884 + le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
2885 trace_mark(ext4_discard_blocks
, "dev %s blk %llu count %u",
2886 sb
->s_id
, (unsigned long long) discard_block
,
2888 sb_issue_discard(sb
, discard_block
, entry
->count
);
2890 kmem_cache_free(ext4_free_ext_cachep
, entry
);
2891 ext4_mb_release_desc(&e4b
);
2894 mb_debug("freed %u blocks in %u structures\n", count
, count2
);
2897 int __init
init_ext4_mballoc(void)
2899 ext4_pspace_cachep
=
2900 kmem_cache_create("ext4_prealloc_space",
2901 sizeof(struct ext4_prealloc_space
),
2902 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2903 if (ext4_pspace_cachep
== NULL
)
2907 kmem_cache_create("ext4_alloc_context",
2908 sizeof(struct ext4_allocation_context
),
2909 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2910 if (ext4_ac_cachep
== NULL
) {
2911 kmem_cache_destroy(ext4_pspace_cachep
);
2915 ext4_free_ext_cachep
=
2916 kmem_cache_create("ext4_free_block_extents",
2917 sizeof(struct ext4_free_data
),
2918 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2919 if (ext4_free_ext_cachep
== NULL
) {
2920 kmem_cache_destroy(ext4_pspace_cachep
);
2921 kmem_cache_destroy(ext4_ac_cachep
);
2927 void exit_ext4_mballoc(void)
2929 /* XXX: synchronize_rcu(); */
2930 kmem_cache_destroy(ext4_pspace_cachep
);
2931 kmem_cache_destroy(ext4_ac_cachep
);
2932 kmem_cache_destroy(ext4_free_ext_cachep
);
2937 * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
2938 * Returns 0 if success or error code
2940 static noinline_for_stack
int
2941 ext4_mb_mark_diskspace_used(struct ext4_allocation_context
*ac
,
2942 handle_t
*handle
, unsigned int reserv_blks
)
2944 struct buffer_head
*bitmap_bh
= NULL
;
2945 struct ext4_super_block
*es
;
2946 struct ext4_group_desc
*gdp
;
2947 struct buffer_head
*gdp_bh
;
2948 struct ext4_sb_info
*sbi
;
2949 struct super_block
*sb
;
2953 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
2954 BUG_ON(ac
->ac_b_ex
.fe_len
<= 0);
2962 bitmap_bh
= ext4_read_block_bitmap(sb
, ac
->ac_b_ex
.fe_group
);
2966 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
2971 gdp
= ext4_get_group_desc(sb
, ac
->ac_b_ex
.fe_group
, &gdp_bh
);
2975 ext4_debug("using block group %u(%d)\n", ac
->ac_b_ex
.fe_group
,
2976 ext4_free_blks_count(sb
, gdp
));
2978 err
= ext4_journal_get_write_access(handle
, gdp_bh
);
2982 block
= ac
->ac_b_ex
.fe_group
* EXT4_BLOCKS_PER_GROUP(sb
)
2983 + ac
->ac_b_ex
.fe_start
2984 + le32_to_cpu(es
->s_first_data_block
);
2986 len
= ac
->ac_b_ex
.fe_len
;
2987 if (in_range(ext4_block_bitmap(sb
, gdp
), block
, len
) ||
2988 in_range(ext4_inode_bitmap(sb
, gdp
), block
, len
) ||
2989 in_range(block
, ext4_inode_table(sb
, gdp
),
2990 EXT4_SB(sb
)->s_itb_per_group
) ||
2991 in_range(block
+ len
- 1, ext4_inode_table(sb
, gdp
),
2992 EXT4_SB(sb
)->s_itb_per_group
)) {
2993 ext4_error(sb
, __func__
,
2994 "Allocating block %llu in system zone of %d group\n",
2995 block
, ac
->ac_b_ex
.fe_group
);
2996 /* File system mounted not to panic on error
2997 * Fix the bitmap and repeat the block allocation
2998 * We leak some of the blocks here.
3000 mb_set_bits(sb_bgl_lock(sbi
, ac
->ac_b_ex
.fe_group
),
3001 bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,
3002 ac
->ac_b_ex
.fe_len
);
3003 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
3008 #ifdef AGGRESSIVE_CHECK
3011 for (i
= 0; i
< ac
->ac_b_ex
.fe_len
; i
++) {
3012 BUG_ON(mb_test_bit(ac
->ac_b_ex
.fe_start
+ i
,
3013 bitmap_bh
->b_data
));
3017 spin_lock(sb_bgl_lock(sbi
, ac
->ac_b_ex
.fe_group
));
3018 mb_set_bits(NULL
, bitmap_bh
->b_data
,
3019 ac
->ac_b_ex
.fe_start
, ac
->ac_b_ex
.fe_len
);
3020 if (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
3021 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT
);
3022 ext4_free_blks_set(sb
, gdp
,
3023 ext4_free_blocks_after_init(sb
,
3024 ac
->ac_b_ex
.fe_group
, gdp
));
3026 len
= ext4_free_blks_count(sb
, gdp
) - ac
->ac_b_ex
.fe_len
;
3027 ext4_free_blks_set(sb
, gdp
, len
);
3028 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, ac
->ac_b_ex
.fe_group
, gdp
);
3029 spin_unlock(sb_bgl_lock(sbi
, ac
->ac_b_ex
.fe_group
));
3030 percpu_counter_sub(&sbi
->s_freeblocks_counter
, ac
->ac_b_ex
.fe_len
);
3032 * Now reduce the dirty block count also. Should not go negative
3034 if (!(ac
->ac_flags
& EXT4_MB_DELALLOC_RESERVED
))
3035 /* release all the reserved blocks if non delalloc */
3036 percpu_counter_sub(&sbi
->s_dirtyblocks_counter
, reserv_blks
);
3038 percpu_counter_sub(&sbi
->s_dirtyblocks_counter
,
3039 ac
->ac_b_ex
.fe_len
);
3040 /* convert reserved quota blocks to real quota blocks */
3041 vfs_dq_claim_block(ac
->ac_inode
, ac
->ac_b_ex
.fe_len
);
3044 if (sbi
->s_log_groups_per_flex
) {
3045 ext4_group_t flex_group
= ext4_flex_group(sbi
,
3046 ac
->ac_b_ex
.fe_group
);
3047 atomic_sub(ac
->ac_b_ex
.fe_len
,
3048 &sbi
->s_flex_groups
[flex_group
].free_blocks
);
3051 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
3054 err
= ext4_handle_dirty_metadata(handle
, NULL
, gdp_bh
);
3063 * here we normalize request for locality group
3064 * Group request are normalized to s_strip size if we set the same via mount
3065 * option. If not we set it to s_mb_group_prealloc which can be configured via
3066 * /sys/fs/ext4/<partition>/mb_group_prealloc
3068 * XXX: should we try to preallocate more than the group has now?
3070 static void ext4_mb_normalize_group_request(struct ext4_allocation_context
*ac
)
3072 struct super_block
*sb
= ac
->ac_sb
;
3073 struct ext4_locality_group
*lg
= ac
->ac_lg
;
3076 if (EXT4_SB(sb
)->s_stripe
)
3077 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_stripe
;
3079 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_mb_group_prealloc
;
3080 mb_debug("#%u: goal %u blocks for locality group\n",
3081 current
->pid
, ac
->ac_g_ex
.fe_len
);
3085 * Normalization means making request better in terms of
3086 * size and alignment
3088 static noinline_for_stack
void
3089 ext4_mb_normalize_request(struct ext4_allocation_context
*ac
,
3090 struct ext4_allocation_request
*ar
)
3094 loff_t size
, orig_size
, start_off
;
3095 ext4_lblk_t start
, orig_start
;
3096 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3097 struct ext4_prealloc_space
*pa
;
3099 /* do normalize only data requests, metadata requests
3100 do not need preallocation */
3101 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3104 /* sometime caller may want exact blocks */
3105 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
3108 /* caller may indicate that preallocation isn't
3109 * required (it's a tail, for example) */
3110 if (ac
->ac_flags
& EXT4_MB_HINT_NOPREALLOC
)
3113 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
) {
3114 ext4_mb_normalize_group_request(ac
);
3118 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
3120 /* first, let's learn actual file size
3121 * given current request is allocated */
3122 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
3123 size
= size
<< bsbits
;
3124 if (size
< i_size_read(ac
->ac_inode
))
3125 size
= i_size_read(ac
->ac_inode
);
3127 /* max size of free chunks */
3130 #define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3131 (req <= (size) || max <= (chunk_size))
3133 /* first, try to predict filesize */
3134 /* XXX: should this table be tunable? */
3136 if (size
<= 16 * 1024) {
3138 } else if (size
<= 32 * 1024) {
3140 } else if (size
<= 64 * 1024) {
3142 } else if (size
<= 128 * 1024) {
3144 } else if (size
<= 256 * 1024) {
3146 } else if (size
<= 512 * 1024) {
3148 } else if (size
<= 1024 * 1024) {
3150 } else if (NRL_CHECK_SIZE(size
, 4 * 1024 * 1024, max
, 2 * 1024)) {
3151 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3152 (21 - bsbits
)) << 21;
3153 size
= 2 * 1024 * 1024;
3154 } else if (NRL_CHECK_SIZE(size
, 8 * 1024 * 1024, max
, 4 * 1024)) {
3155 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3156 (22 - bsbits
)) << 22;
3157 size
= 4 * 1024 * 1024;
3158 } else if (NRL_CHECK_SIZE(ac
->ac_o_ex
.fe_len
,
3159 (8<<20)>>bsbits
, max
, 8 * 1024)) {
3160 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3161 (23 - bsbits
)) << 23;
3162 size
= 8 * 1024 * 1024;
3164 start_off
= (loff_t
)ac
->ac_o_ex
.fe_logical
<< bsbits
;
3165 size
= ac
->ac_o_ex
.fe_len
<< bsbits
;
3167 orig_size
= size
= size
>> bsbits
;
3168 orig_start
= start
= start_off
>> bsbits
;
3170 /* don't cover already allocated blocks in selected range */
3171 if (ar
->pleft
&& start
<= ar
->lleft
) {
3172 size
-= ar
->lleft
+ 1 - start
;
3173 start
= ar
->lleft
+ 1;
3175 if (ar
->pright
&& start
+ size
- 1 >= ar
->lright
)
3176 size
-= start
+ size
- ar
->lright
;
3180 /* check we don't cross already preallocated blocks */
3182 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3187 spin_lock(&pa
->pa_lock
);
3188 if (pa
->pa_deleted
) {
3189 spin_unlock(&pa
->pa_lock
);
3193 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
3195 /* PA must not overlap original request */
3196 BUG_ON(!(ac
->ac_o_ex
.fe_logical
>= pa_end
||
3197 ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
));
3199 /* skip PA normalized request doesn't overlap with */
3200 if (pa
->pa_lstart
>= end
) {
3201 spin_unlock(&pa
->pa_lock
);
3204 if (pa_end
<= start
) {
3205 spin_unlock(&pa
->pa_lock
);
3208 BUG_ON(pa
->pa_lstart
<= start
&& pa_end
>= end
);
3210 if (pa_end
<= ac
->ac_o_ex
.fe_logical
) {
3211 BUG_ON(pa_end
< start
);
3215 if (pa
->pa_lstart
> ac
->ac_o_ex
.fe_logical
) {
3216 BUG_ON(pa
->pa_lstart
> end
);
3217 end
= pa
->pa_lstart
;
3219 spin_unlock(&pa
->pa_lock
);
3224 /* XXX: extra loop to check we really don't overlap preallocations */
3226 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3228 spin_lock(&pa
->pa_lock
);
3229 if (pa
->pa_deleted
== 0) {
3230 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
3231 BUG_ON(!(start
>= pa_end
|| end
<= pa
->pa_lstart
));
3233 spin_unlock(&pa
->pa_lock
);
3237 if (start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3238 start
> ac
->ac_o_ex
.fe_logical
) {
3239 printk(KERN_ERR
"start %lu, size %lu, fe_logical %lu\n",
3240 (unsigned long) start
, (unsigned long) size
,
3241 (unsigned long) ac
->ac_o_ex
.fe_logical
);
3243 BUG_ON(start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3244 start
> ac
->ac_o_ex
.fe_logical
);
3245 BUG_ON(size
<= 0 || size
> EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
3247 /* now prepare goal request */
3249 /* XXX: is it better to align blocks WRT to logical
3250 * placement or satisfy big request as is */
3251 ac
->ac_g_ex
.fe_logical
= start
;
3252 ac
->ac_g_ex
.fe_len
= size
;
3254 /* define goal start in order to merge */
3255 if (ar
->pright
&& (ar
->lright
== (start
+ size
))) {
3256 /* merge to the right */
3257 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pright
- size
,
3258 &ac
->ac_f_ex
.fe_group
,
3259 &ac
->ac_f_ex
.fe_start
);
3260 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3262 if (ar
->pleft
&& (ar
->lleft
+ 1 == start
)) {
3263 /* merge to the left */
3264 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pleft
+ 1,
3265 &ac
->ac_f_ex
.fe_group
,
3266 &ac
->ac_f_ex
.fe_start
);
3267 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3270 mb_debug("goal: %u(was %u) blocks at %u\n", (unsigned) size
,
3271 (unsigned) orig_size
, (unsigned) start
);
3274 static void ext4_mb_collect_stats(struct ext4_allocation_context
*ac
)
3276 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3278 if (sbi
->s_mb_stats
&& ac
->ac_g_ex
.fe_len
> 1) {
3279 atomic_inc(&sbi
->s_bal_reqs
);
3280 atomic_add(ac
->ac_b_ex
.fe_len
, &sbi
->s_bal_allocated
);
3281 if (ac
->ac_o_ex
.fe_len
>= ac
->ac_g_ex
.fe_len
)
3282 atomic_inc(&sbi
->s_bal_success
);
3283 atomic_add(ac
->ac_found
, &sbi
->s_bal_ex_scanned
);
3284 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
3285 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
3286 atomic_inc(&sbi
->s_bal_goals
);
3287 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
)
3288 atomic_inc(&sbi
->s_bal_breaks
);
3291 ext4_mb_store_history(ac
);
3295 * use blocks preallocated to inode
3297 static void ext4_mb_use_inode_pa(struct ext4_allocation_context
*ac
,
3298 struct ext4_prealloc_space
*pa
)
3304 /* found preallocated blocks, use them */
3305 start
= pa
->pa_pstart
+ (ac
->ac_o_ex
.fe_logical
- pa
->pa_lstart
);
3306 end
= min(pa
->pa_pstart
+ pa
->pa_len
, start
+ ac
->ac_o_ex
.fe_len
);
3308 ext4_get_group_no_and_offset(ac
->ac_sb
, start
, &ac
->ac_b_ex
.fe_group
,
3309 &ac
->ac_b_ex
.fe_start
);
3310 ac
->ac_b_ex
.fe_len
= len
;
3311 ac
->ac_status
= AC_STATUS_FOUND
;
3314 BUG_ON(start
< pa
->pa_pstart
);
3315 BUG_ON(start
+ len
> pa
->pa_pstart
+ pa
->pa_len
);
3316 BUG_ON(pa
->pa_free
< len
);
3319 mb_debug("use %llu/%u from inode pa %p\n", start
, len
, pa
);
3323 * use blocks preallocated to locality group
3325 static void ext4_mb_use_group_pa(struct ext4_allocation_context
*ac
,
3326 struct ext4_prealloc_space
*pa
)
3328 unsigned int len
= ac
->ac_o_ex
.fe_len
;
3330 ext4_get_group_no_and_offset(ac
->ac_sb
, pa
->pa_pstart
,
3331 &ac
->ac_b_ex
.fe_group
,
3332 &ac
->ac_b_ex
.fe_start
);
3333 ac
->ac_b_ex
.fe_len
= len
;
3334 ac
->ac_status
= AC_STATUS_FOUND
;
3337 /* we don't correct pa_pstart or pa_plen here to avoid
3338 * possible race when the group is being loaded concurrently
3339 * instead we correct pa later, after blocks are marked
3340 * in on-disk bitmap -- see ext4_mb_release_context()
3341 * Other CPUs are prevented from allocating from this pa by lg_mutex
3343 mb_debug("use %u/%u from group pa %p\n", pa
->pa_lstart
-len
, len
, pa
);
3347 * Return the prealloc space that have minimal distance
3348 * from the goal block. @cpa is the prealloc
3349 * space that is having currently known minimal distance
3350 * from the goal block.
3352 static struct ext4_prealloc_space
*
3353 ext4_mb_check_group_pa(ext4_fsblk_t goal_block
,
3354 struct ext4_prealloc_space
*pa
,
3355 struct ext4_prealloc_space
*cpa
)
3357 ext4_fsblk_t cur_distance
, new_distance
;
3360 atomic_inc(&pa
->pa_count
);
3363 cur_distance
= abs(goal_block
- cpa
->pa_pstart
);
3364 new_distance
= abs(goal_block
- pa
->pa_pstart
);
3366 if (cur_distance
< new_distance
)
3369 /* drop the previous reference */
3370 atomic_dec(&cpa
->pa_count
);
3371 atomic_inc(&pa
->pa_count
);
3376 * search goal blocks in preallocated space
3378 static noinline_for_stack
int
3379 ext4_mb_use_preallocated(struct ext4_allocation_context
*ac
)
3382 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3383 struct ext4_locality_group
*lg
;
3384 struct ext4_prealloc_space
*pa
, *cpa
= NULL
;
3385 ext4_fsblk_t goal_block
;
3387 /* only data can be preallocated */
3388 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3391 /* first, try per-file preallocation */
3393 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3395 /* all fields in this condition don't change,
3396 * so we can skip locking for them */
3397 if (ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
||
3398 ac
->ac_o_ex
.fe_logical
>= pa
->pa_lstart
+ pa
->pa_len
)
3401 /* found preallocated blocks, use them */
3402 spin_lock(&pa
->pa_lock
);
3403 if (pa
->pa_deleted
== 0 && pa
->pa_free
) {
3404 atomic_inc(&pa
->pa_count
);
3405 ext4_mb_use_inode_pa(ac
, pa
);
3406 spin_unlock(&pa
->pa_lock
);
3407 ac
->ac_criteria
= 10;
3411 spin_unlock(&pa
->pa_lock
);
3415 /* can we use group allocation? */
3416 if (!(ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
))
3419 /* inode may have no locality group for some reason */
3423 order
= fls(ac
->ac_o_ex
.fe_len
) - 1;
3424 if (order
> PREALLOC_TB_SIZE
- 1)
3425 /* The max size of hash table is PREALLOC_TB_SIZE */
3426 order
= PREALLOC_TB_SIZE
- 1;
3428 goal_block
= ac
->ac_g_ex
.fe_group
* EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
) +
3429 ac
->ac_g_ex
.fe_start
+
3430 le32_to_cpu(EXT4_SB(ac
->ac_sb
)->s_es
->s_first_data_block
);
3432 * search for the prealloc space that is having
3433 * minimal distance from the goal block.
3435 for (i
= order
; i
< PREALLOC_TB_SIZE
; i
++) {
3437 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[i
],
3439 spin_lock(&pa
->pa_lock
);
3440 if (pa
->pa_deleted
== 0 &&
3441 pa
->pa_free
>= ac
->ac_o_ex
.fe_len
) {
3443 cpa
= ext4_mb_check_group_pa(goal_block
,
3446 spin_unlock(&pa
->pa_lock
);
3451 ext4_mb_use_group_pa(ac
, cpa
);
3452 ac
->ac_criteria
= 20;
3459 * the function goes through all block freed in the group
3460 * but not yet committed and marks them used in in-core bitmap.
3461 * buddy must be generated from this bitmap
3462 * Need to be called with ext4 group lock (ext4_lock_group)
3464 static void ext4_mb_generate_from_freelist(struct super_block
*sb
, void *bitmap
,
3468 struct ext4_group_info
*grp
;
3469 struct ext4_free_data
*entry
;
3471 grp
= ext4_get_group_info(sb
, group
);
3472 n
= rb_first(&(grp
->bb_free_root
));
3475 entry
= rb_entry(n
, struct ext4_free_data
, node
);
3476 mb_set_bits(sb_bgl_lock(EXT4_SB(sb
), group
),
3477 bitmap
, entry
->start_blk
,
3485 * the function goes through all preallocation in this group and marks them
3486 * used in in-core bitmap. buddy must be generated from this bitmap
3487 * Need to be called with ext4 group lock (ext4_lock_group)
3489 static void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
3492 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3493 struct ext4_prealloc_space
*pa
;
3494 struct list_head
*cur
;
3495 ext4_group_t groupnr
;
3496 ext4_grpblk_t start
;
3497 int preallocated
= 0;
3501 /* all form of preallocation discards first load group,
3502 * so the only competing code is preallocation use.
3503 * we don't need any locking here
3504 * notice we do NOT ignore preallocations with pa_deleted
3505 * otherwise we could leave used blocks available for
3506 * allocation in buddy when concurrent ext4_mb_put_pa()
3507 * is dropping preallocation
3509 list_for_each(cur
, &grp
->bb_prealloc_list
) {
3510 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
3511 spin_lock(&pa
->pa_lock
);
3512 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
3515 spin_unlock(&pa
->pa_lock
);
3516 if (unlikely(len
== 0))
3518 BUG_ON(groupnr
!= group
);
3519 mb_set_bits(sb_bgl_lock(EXT4_SB(sb
), group
),
3520 bitmap
, start
, len
);
3521 preallocated
+= len
;
3524 mb_debug("prellocated %u for group %u\n", preallocated
, group
);
3527 static void ext4_mb_pa_callback(struct rcu_head
*head
)
3529 struct ext4_prealloc_space
*pa
;
3530 pa
= container_of(head
, struct ext4_prealloc_space
, u
.pa_rcu
);
3531 kmem_cache_free(ext4_pspace_cachep
, pa
);
3535 * drops a reference to preallocated space descriptor
3536 * if this was the last reference and the space is consumed
3538 static void ext4_mb_put_pa(struct ext4_allocation_context
*ac
,
3539 struct super_block
*sb
, struct ext4_prealloc_space
*pa
)
3542 ext4_fsblk_t grp_blk
;
3544 if (!atomic_dec_and_test(&pa
->pa_count
) || pa
->pa_free
!= 0)
3547 /* in this short window concurrent discard can set pa_deleted */
3548 spin_lock(&pa
->pa_lock
);
3549 if (pa
->pa_deleted
== 1) {
3550 spin_unlock(&pa
->pa_lock
);
3555 spin_unlock(&pa
->pa_lock
);
3557 grp_blk
= pa
->pa_pstart
;
3559 * If doing group-based preallocation, pa_pstart may be in the
3560 * next group when pa is used up
3562 if (pa
->pa_type
== MB_GROUP_PA
)
3565 ext4_get_group_no_and_offset(sb
, grp_blk
, &grp
, NULL
);
3570 * P1 (buddy init) P2 (regular allocation)
3571 * find block B in PA
3572 * copy on-disk bitmap to buddy
3573 * mark B in on-disk bitmap
3574 * drop PA from group
3575 * mark all PAs in buddy
3577 * thus, P1 initializes buddy with B available. to prevent this
3578 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3581 ext4_lock_group(sb
, grp
);
3582 list_del(&pa
->pa_group_list
);
3583 ext4_unlock_group(sb
, grp
);
3585 spin_lock(pa
->pa_obj_lock
);
3586 list_del_rcu(&pa
->pa_inode_list
);
3587 spin_unlock(pa
->pa_obj_lock
);
3589 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3593 * creates new preallocated space for given inode
3595 static noinline_for_stack
int
3596 ext4_mb_new_inode_pa(struct ext4_allocation_context
*ac
)
3598 struct super_block
*sb
= ac
->ac_sb
;
3599 struct ext4_prealloc_space
*pa
;
3600 struct ext4_group_info
*grp
;
3601 struct ext4_inode_info
*ei
;
3603 /* preallocate only when found space is larger then requested */
3604 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3605 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3606 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3608 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3612 if (ac
->ac_b_ex
.fe_len
< ac
->ac_g_ex
.fe_len
) {
3618 /* we can't allocate as much as normalizer wants.
3619 * so, found space must get proper lstart
3620 * to cover original request */
3621 BUG_ON(ac
->ac_g_ex
.fe_logical
> ac
->ac_o_ex
.fe_logical
);
3622 BUG_ON(ac
->ac_g_ex
.fe_len
< ac
->ac_o_ex
.fe_len
);
3624 /* we're limited by original request in that
3625 * logical block must be covered any way
3626 * winl is window we can move our chunk within */
3627 winl
= ac
->ac_o_ex
.fe_logical
- ac
->ac_g_ex
.fe_logical
;
3629 /* also, we should cover whole original request */
3630 wins
= ac
->ac_b_ex
.fe_len
- ac
->ac_o_ex
.fe_len
;
3632 /* the smallest one defines real window */
3633 win
= min(winl
, wins
);
3635 offs
= ac
->ac_o_ex
.fe_logical
% ac
->ac_b_ex
.fe_len
;
3636 if (offs
&& offs
< win
)
3639 ac
->ac_b_ex
.fe_logical
= ac
->ac_o_ex
.fe_logical
- win
;
3640 BUG_ON(ac
->ac_o_ex
.fe_logical
< ac
->ac_b_ex
.fe_logical
);
3641 BUG_ON(ac
->ac_o_ex
.fe_len
> ac
->ac_b_ex
.fe_len
);
3644 /* preallocation can change ac_b_ex, thus we store actually
3645 * allocated blocks for history */
3646 ac
->ac_f_ex
= ac
->ac_b_ex
;
3648 pa
->pa_lstart
= ac
->ac_b_ex
.fe_logical
;
3649 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3650 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3651 pa
->pa_free
= pa
->pa_len
;
3652 atomic_set(&pa
->pa_count
, 1);
3653 spin_lock_init(&pa
->pa_lock
);
3654 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3655 INIT_LIST_HEAD(&pa
->pa_group_list
);
3657 pa
->pa_type
= MB_INODE_PA
;
3659 mb_debug("new inode pa %p: %llu/%u for %u\n", pa
,
3660 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3661 trace_mark(ext4_mb_new_inode_pa
,
3662 "dev %s ino %lu pstart %llu len %u lstart %u",
3663 sb
->s_id
, ac
->ac_inode
->i_ino
,
3664 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3666 ext4_mb_use_inode_pa(ac
, pa
);
3667 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3669 ei
= EXT4_I(ac
->ac_inode
);
3670 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3672 pa
->pa_obj_lock
= &ei
->i_prealloc_lock
;
3673 pa
->pa_inode
= ac
->ac_inode
;
3675 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3676 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3677 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3679 spin_lock(pa
->pa_obj_lock
);
3680 list_add_rcu(&pa
->pa_inode_list
, &ei
->i_prealloc_list
);
3681 spin_unlock(pa
->pa_obj_lock
);
3687 * creates new preallocated space for locality group inodes belongs to
3689 static noinline_for_stack
int
3690 ext4_mb_new_group_pa(struct ext4_allocation_context
*ac
)
3692 struct super_block
*sb
= ac
->ac_sb
;
3693 struct ext4_locality_group
*lg
;
3694 struct ext4_prealloc_space
*pa
;
3695 struct ext4_group_info
*grp
;
3697 /* preallocate only when found space is larger then requested */
3698 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3699 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3700 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3702 BUG_ON(ext4_pspace_cachep
== NULL
);
3703 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3707 /* preallocation can change ac_b_ex, thus we store actually
3708 * allocated blocks for history */
3709 ac
->ac_f_ex
= ac
->ac_b_ex
;
3711 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3712 pa
->pa_lstart
= pa
->pa_pstart
;
3713 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3714 pa
->pa_free
= pa
->pa_len
;
3715 atomic_set(&pa
->pa_count
, 1);
3716 spin_lock_init(&pa
->pa_lock
);
3717 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3718 INIT_LIST_HEAD(&pa
->pa_group_list
);
3720 pa
->pa_type
= MB_GROUP_PA
;
3722 mb_debug("new group pa %p: %llu/%u for %u\n", pa
,
3723 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3724 trace_mark(ext4_mb_new_group_pa
, "dev %s pstart %llu len %u lstart %u",
3725 sb
->s_id
, pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3727 ext4_mb_use_group_pa(ac
, pa
);
3728 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3730 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3734 pa
->pa_obj_lock
= &lg
->lg_prealloc_lock
;
3735 pa
->pa_inode
= NULL
;
3737 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3738 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3739 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3742 * We will later add the new pa to the right bucket
3743 * after updating the pa_free in ext4_mb_release_context
3748 static int ext4_mb_new_preallocation(struct ext4_allocation_context
*ac
)
3752 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
3753 err
= ext4_mb_new_group_pa(ac
);
3755 err
= ext4_mb_new_inode_pa(ac
);
3760 * finds all unused blocks in on-disk bitmap, frees them in
3761 * in-core bitmap and buddy.
3762 * @pa must be unlinked from inode and group lists, so that
3763 * nobody else can find/use it.
3764 * the caller MUST hold group/inode locks.
3765 * TODO: optimize the case when there are no in-core structures yet
3767 static noinline_for_stack
int
3768 ext4_mb_release_inode_pa(struct ext4_buddy
*e4b
, struct buffer_head
*bitmap_bh
,
3769 struct ext4_prealloc_space
*pa
,
3770 struct ext4_allocation_context
*ac
)
3772 struct super_block
*sb
= e4b
->bd_sb
;
3773 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3778 unsigned long long grp_blk_start
;
3783 BUG_ON(pa
->pa_deleted
== 0);
3784 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3785 grp_blk_start
= pa
->pa_pstart
- bit
;
3786 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3787 end
= bit
+ pa
->pa_len
;
3791 ac
->ac_inode
= pa
->pa_inode
;
3792 ac
->ac_op
= EXT4_MB_HISTORY_DISCARD
;
3796 bit
= mb_find_next_zero_bit(bitmap_bh
->b_data
, end
, bit
);
3799 next
= mb_find_next_bit(bitmap_bh
->b_data
, end
, bit
);
3800 start
= group
* EXT4_BLOCKS_PER_GROUP(sb
) + bit
+
3801 le32_to_cpu(sbi
->s_es
->s_first_data_block
);
3802 mb_debug(" free preallocated %u/%u in group %u\n",
3803 (unsigned) start
, (unsigned) next
- bit
,
3808 ac
->ac_b_ex
.fe_group
= group
;
3809 ac
->ac_b_ex
.fe_start
= bit
;
3810 ac
->ac_b_ex
.fe_len
= next
- bit
;
3811 ac
->ac_b_ex
.fe_logical
= 0;
3812 ext4_mb_store_history(ac
);
3815 trace_mark(ext4_mb_release_inode_pa
,
3816 "dev %s ino %lu block %llu count %u",
3817 sb
->s_id
, pa
->pa_inode
->i_ino
, grp_blk_start
+ bit
,
3819 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, next
- bit
);
3822 if (free
!= pa
->pa_free
) {
3823 printk(KERN_CRIT
"pa %p: logic %lu, phys. %lu, len %lu\n",
3824 pa
, (unsigned long) pa
->pa_lstart
,
3825 (unsigned long) pa
->pa_pstart
,
3826 (unsigned long) pa
->pa_len
);
3827 ext4_grp_locked_error(sb
, group
,
3828 __func__
, "free %u, pa_free %u",
3831 * pa is already deleted so we use the value obtained
3832 * from the bitmap and continue.
3835 atomic_add(free
, &sbi
->s_mb_discarded
);
3840 static noinline_for_stack
int
3841 ext4_mb_release_group_pa(struct ext4_buddy
*e4b
,
3842 struct ext4_prealloc_space
*pa
,
3843 struct ext4_allocation_context
*ac
)
3845 struct super_block
*sb
= e4b
->bd_sb
;
3850 ac
->ac_op
= EXT4_MB_HISTORY_DISCARD
;
3852 trace_mark(ext4_mb_release_group_pa
, "dev %s pstart %llu len %d",
3853 sb
->s_id
, pa
->pa_pstart
, pa
->pa_len
);
3854 BUG_ON(pa
->pa_deleted
== 0);
3855 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3856 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3857 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, pa
->pa_len
);
3858 atomic_add(pa
->pa_len
, &EXT4_SB(sb
)->s_mb_discarded
);
3862 ac
->ac_inode
= NULL
;
3863 ac
->ac_b_ex
.fe_group
= group
;
3864 ac
->ac_b_ex
.fe_start
= bit
;
3865 ac
->ac_b_ex
.fe_len
= pa
->pa_len
;
3866 ac
->ac_b_ex
.fe_logical
= 0;
3867 ext4_mb_store_history(ac
);
3874 * releases all preallocations in given group
3876 * first, we need to decide discard policy:
3877 * - when do we discard
3879 * - how many do we discard
3880 * 1) how many requested
3882 static noinline_for_stack
int
3883 ext4_mb_discard_group_preallocations(struct super_block
*sb
,
3884 ext4_group_t group
, int needed
)
3886 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3887 struct buffer_head
*bitmap_bh
= NULL
;
3888 struct ext4_prealloc_space
*pa
, *tmp
;
3889 struct ext4_allocation_context
*ac
;
3890 struct list_head list
;
3891 struct ext4_buddy e4b
;
3896 mb_debug("discard preallocation for group %u\n", group
);
3898 if (list_empty(&grp
->bb_prealloc_list
))
3901 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
3902 if (bitmap_bh
== NULL
) {
3903 ext4_error(sb
, __func__
, "Error in reading block "
3904 "bitmap for %u", group
);
3908 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
3910 ext4_error(sb
, __func__
, "Error in loading buddy "
3911 "information for %u", group
);
3917 needed
= EXT4_BLOCKS_PER_GROUP(sb
) + 1;
3919 INIT_LIST_HEAD(&list
);
3920 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
3922 ext4_lock_group(sb
, group
);
3923 list_for_each_entry_safe(pa
, tmp
,
3924 &grp
->bb_prealloc_list
, pa_group_list
) {
3925 spin_lock(&pa
->pa_lock
);
3926 if (atomic_read(&pa
->pa_count
)) {
3927 spin_unlock(&pa
->pa_lock
);
3931 if (pa
->pa_deleted
) {
3932 spin_unlock(&pa
->pa_lock
);
3936 /* seems this one can be freed ... */
3939 /* we can trust pa_free ... */
3940 free
+= pa
->pa_free
;
3942 spin_unlock(&pa
->pa_lock
);
3944 list_del(&pa
->pa_group_list
);
3945 list_add(&pa
->u
.pa_tmp_list
, &list
);
3948 /* if we still need more blocks and some PAs were used, try again */
3949 if (free
< needed
&& busy
) {
3951 ext4_unlock_group(sb
, group
);
3953 * Yield the CPU here so that we don't get soft lockup
3954 * in non preempt case.
3960 /* found anything to free? */
3961 if (list_empty(&list
)) {
3966 /* now free all selected PAs */
3967 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
3969 /* remove from object (inode or locality group) */
3970 spin_lock(pa
->pa_obj_lock
);
3971 list_del_rcu(&pa
->pa_inode_list
);
3972 spin_unlock(pa
->pa_obj_lock
);
3974 if (pa
->pa_type
== MB_GROUP_PA
)
3975 ext4_mb_release_group_pa(&e4b
, pa
, ac
);
3977 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
, ac
);
3979 list_del(&pa
->u
.pa_tmp_list
);
3980 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3984 ext4_unlock_group(sb
, group
);
3986 kmem_cache_free(ext4_ac_cachep
, ac
);
3987 ext4_mb_release_desc(&e4b
);
3993 * releases all non-used preallocated blocks for given inode
3995 * It's important to discard preallocations under i_data_sem
3996 * We don't want another block to be served from the prealloc
3997 * space when we are discarding the inode prealloc space.
3999 * FIXME!! Make sure it is valid at all the call sites
4001 void ext4_discard_preallocations(struct inode
*inode
)
4003 struct ext4_inode_info
*ei
= EXT4_I(inode
);
4004 struct super_block
*sb
= inode
->i_sb
;
4005 struct buffer_head
*bitmap_bh
= NULL
;
4006 struct ext4_prealloc_space
*pa
, *tmp
;
4007 struct ext4_allocation_context
*ac
;
4008 ext4_group_t group
= 0;
4009 struct list_head list
;
4010 struct ext4_buddy e4b
;
4013 if (!S_ISREG(inode
->i_mode
)) {
4014 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
4018 mb_debug("discard preallocation for inode %lu\n", inode
->i_ino
);
4019 trace_mark(ext4_discard_preallocations
, "dev %s ino %lu", sb
->s_id
,
4022 INIT_LIST_HEAD(&list
);
4024 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4026 /* first, collect all pa's in the inode */
4027 spin_lock(&ei
->i_prealloc_lock
);
4028 while (!list_empty(&ei
->i_prealloc_list
)) {
4029 pa
= list_entry(ei
->i_prealloc_list
.next
,
4030 struct ext4_prealloc_space
, pa_inode_list
);
4031 BUG_ON(pa
->pa_obj_lock
!= &ei
->i_prealloc_lock
);
4032 spin_lock(&pa
->pa_lock
);
4033 if (atomic_read(&pa
->pa_count
)) {
4034 /* this shouldn't happen often - nobody should
4035 * use preallocation while we're discarding it */
4036 spin_unlock(&pa
->pa_lock
);
4037 spin_unlock(&ei
->i_prealloc_lock
);
4038 printk(KERN_ERR
"uh-oh! used pa while discarding\n");
4040 schedule_timeout_uninterruptible(HZ
);
4044 if (pa
->pa_deleted
== 0) {
4046 spin_unlock(&pa
->pa_lock
);
4047 list_del_rcu(&pa
->pa_inode_list
);
4048 list_add(&pa
->u
.pa_tmp_list
, &list
);
4052 /* someone is deleting pa right now */
4053 spin_unlock(&pa
->pa_lock
);
4054 spin_unlock(&ei
->i_prealloc_lock
);
4056 /* we have to wait here because pa_deleted
4057 * doesn't mean pa is already unlinked from
4058 * the list. as we might be called from
4059 * ->clear_inode() the inode will get freed
4060 * and concurrent thread which is unlinking
4061 * pa from inode's list may access already
4062 * freed memory, bad-bad-bad */
4064 /* XXX: if this happens too often, we can
4065 * add a flag to force wait only in case
4066 * of ->clear_inode(), but not in case of
4067 * regular truncate */
4068 schedule_timeout_uninterruptible(HZ
);
4071 spin_unlock(&ei
->i_prealloc_lock
);
4073 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
4074 BUG_ON(pa
->pa_type
!= MB_INODE_PA
);
4075 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
4077 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
4079 ext4_error(sb
, __func__
, "Error in loading buddy "
4080 "information for %u", group
);
4084 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
4085 if (bitmap_bh
== NULL
) {
4086 ext4_error(sb
, __func__
, "Error in reading block "
4087 "bitmap for %u", group
);
4088 ext4_mb_release_desc(&e4b
);
4092 ext4_lock_group(sb
, group
);
4093 list_del(&pa
->pa_group_list
);
4094 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
, ac
);
4095 ext4_unlock_group(sb
, group
);
4097 ext4_mb_release_desc(&e4b
);
4100 list_del(&pa
->u
.pa_tmp_list
);
4101 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4104 kmem_cache_free(ext4_ac_cachep
, ac
);
4108 * finds all preallocated spaces and return blocks being freed to them
4109 * if preallocated space becomes full (no block is used from the space)
4110 * then the function frees space in buddy
4111 * XXX: at the moment, truncate (which is the only way to free blocks)
4112 * discards all preallocations
4114 static void ext4_mb_return_to_preallocation(struct inode
*inode
,
4115 struct ext4_buddy
*e4b
,
4116 sector_t block
, int count
)
4118 BUG_ON(!list_empty(&EXT4_I(inode
)->i_prealloc_list
));
4121 static void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
4123 struct super_block
*sb
= ac
->ac_sb
;
4126 printk(KERN_ERR
"EXT4-fs: Can't allocate:"
4127 " Allocation context details:\n");
4128 printk(KERN_ERR
"EXT4-fs: status %d flags %d\n",
4129 ac
->ac_status
, ac
->ac_flags
);
4130 printk(KERN_ERR
"EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
4131 "best %lu/%lu/%lu@%lu cr %d\n",
4132 (unsigned long)ac
->ac_o_ex
.fe_group
,
4133 (unsigned long)ac
->ac_o_ex
.fe_start
,
4134 (unsigned long)ac
->ac_o_ex
.fe_len
,
4135 (unsigned long)ac
->ac_o_ex
.fe_logical
,
4136 (unsigned long)ac
->ac_g_ex
.fe_group
,
4137 (unsigned long)ac
->ac_g_ex
.fe_start
,
4138 (unsigned long)ac
->ac_g_ex
.fe_len
,
4139 (unsigned long)ac
->ac_g_ex
.fe_logical
,
4140 (unsigned long)ac
->ac_b_ex
.fe_group
,
4141 (unsigned long)ac
->ac_b_ex
.fe_start
,
4142 (unsigned long)ac
->ac_b_ex
.fe_len
,
4143 (unsigned long)ac
->ac_b_ex
.fe_logical
,
4144 (int)ac
->ac_criteria
);
4145 printk(KERN_ERR
"EXT4-fs: %lu scanned, %d found\n", ac
->ac_ex_scanned
,
4147 printk(KERN_ERR
"EXT4-fs: groups: \n");
4148 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; i
++) {
4149 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, i
);
4150 struct ext4_prealloc_space
*pa
;
4151 ext4_grpblk_t start
;
4152 struct list_head
*cur
;
4153 ext4_lock_group(sb
, i
);
4154 list_for_each(cur
, &grp
->bb_prealloc_list
) {
4155 pa
= list_entry(cur
, struct ext4_prealloc_space
,
4157 spin_lock(&pa
->pa_lock
);
4158 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
4160 spin_unlock(&pa
->pa_lock
);
4161 printk(KERN_ERR
"PA:%lu:%d:%u \n", i
,
4164 ext4_unlock_group(sb
, i
);
4166 if (grp
->bb_free
== 0)
4168 printk(KERN_ERR
"%lu: %d/%d \n",
4169 i
, grp
->bb_free
, grp
->bb_fragments
);
4171 printk(KERN_ERR
"\n");
4174 static inline void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
4181 * We use locality group preallocation for small size file. The size of the
4182 * file is determined by the current size or the resulting size after
4183 * allocation which ever is larger
4185 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
4187 static void ext4_mb_group_or_file(struct ext4_allocation_context
*ac
)
4189 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
4190 int bsbits
= ac
->ac_sb
->s_blocksize_bits
;
4193 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
4196 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
4197 isize
= i_size_read(ac
->ac_inode
) >> bsbits
;
4198 size
= max(size
, isize
);
4200 /* don't use group allocation for large files */
4201 if (size
>= sbi
->s_mb_stream_request
)
4204 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
4207 BUG_ON(ac
->ac_lg
!= NULL
);
4209 * locality group prealloc space are per cpu. The reason for having
4210 * per cpu locality group is to reduce the contention between block
4211 * request from multiple CPUs.
4213 ac
->ac_lg
= per_cpu_ptr(sbi
->s_locality_groups
, raw_smp_processor_id());
4215 /* we're going to use group allocation */
4216 ac
->ac_flags
|= EXT4_MB_HINT_GROUP_ALLOC
;
4218 /* serialize all allocations in the group */
4219 mutex_lock(&ac
->ac_lg
->lg_mutex
);
4222 static noinline_for_stack
int
4223 ext4_mb_initialize_context(struct ext4_allocation_context
*ac
,
4224 struct ext4_allocation_request
*ar
)
4226 struct super_block
*sb
= ar
->inode
->i_sb
;
4227 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4228 struct ext4_super_block
*es
= sbi
->s_es
;
4232 ext4_grpblk_t block
;
4234 /* we can't allocate > group size */
4237 /* just a dirty hack to filter too big requests */
4238 if (len
>= EXT4_BLOCKS_PER_GROUP(sb
) - 10)
4239 len
= EXT4_BLOCKS_PER_GROUP(sb
) - 10;
4241 /* start searching from the goal */
4243 if (goal
< le32_to_cpu(es
->s_first_data_block
) ||
4244 goal
>= ext4_blocks_count(es
))
4245 goal
= le32_to_cpu(es
->s_first_data_block
);
4246 ext4_get_group_no_and_offset(sb
, goal
, &group
, &block
);
4248 /* set up allocation goals */
4249 ac
->ac_b_ex
.fe_logical
= ar
->logical
;
4250 ac
->ac_b_ex
.fe_group
= 0;
4251 ac
->ac_b_ex
.fe_start
= 0;
4252 ac
->ac_b_ex
.fe_len
= 0;
4253 ac
->ac_status
= AC_STATUS_CONTINUE
;
4254 ac
->ac_groups_scanned
= 0;
4255 ac
->ac_ex_scanned
= 0;
4258 ac
->ac_inode
= ar
->inode
;
4259 ac
->ac_o_ex
.fe_logical
= ar
->logical
;
4260 ac
->ac_o_ex
.fe_group
= group
;
4261 ac
->ac_o_ex
.fe_start
= block
;
4262 ac
->ac_o_ex
.fe_len
= len
;
4263 ac
->ac_g_ex
.fe_logical
= ar
->logical
;
4264 ac
->ac_g_ex
.fe_group
= group
;
4265 ac
->ac_g_ex
.fe_start
= block
;
4266 ac
->ac_g_ex
.fe_len
= len
;
4267 ac
->ac_f_ex
.fe_len
= 0;
4268 ac
->ac_flags
= ar
->flags
;
4270 ac
->ac_criteria
= 0;
4272 ac
->ac_bitmap_page
= NULL
;
4273 ac
->ac_buddy_page
= NULL
;
4274 ac
->alloc_semp
= NULL
;
4277 /* we have to define context: we'll we work with a file or
4278 * locality group. this is a policy, actually */
4279 ext4_mb_group_or_file(ac
);
4281 mb_debug("init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
4282 "left: %u/%u, right %u/%u to %swritable\n",
4283 (unsigned) ar
->len
, (unsigned) ar
->logical
,
4284 (unsigned) ar
->goal
, ac
->ac_flags
, ac
->ac_2order
,
4285 (unsigned) ar
->lleft
, (unsigned) ar
->pleft
,
4286 (unsigned) ar
->lright
, (unsigned) ar
->pright
,
4287 atomic_read(&ar
->inode
->i_writecount
) ? "" : "non-");
4292 static noinline_for_stack
void
4293 ext4_mb_discard_lg_preallocations(struct super_block
*sb
,
4294 struct ext4_locality_group
*lg
,
4295 int order
, int total_entries
)
4297 ext4_group_t group
= 0;
4298 struct ext4_buddy e4b
;
4299 struct list_head discard_list
;
4300 struct ext4_prealloc_space
*pa
, *tmp
;
4301 struct ext4_allocation_context
*ac
;
4303 mb_debug("discard locality group preallocation\n");
4305 INIT_LIST_HEAD(&discard_list
);
4306 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4308 spin_lock(&lg
->lg_prealloc_lock
);
4309 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[order
],
4311 spin_lock(&pa
->pa_lock
);
4312 if (atomic_read(&pa
->pa_count
)) {
4314 * This is the pa that we just used
4315 * for block allocation. So don't
4318 spin_unlock(&pa
->pa_lock
);
4321 if (pa
->pa_deleted
) {
4322 spin_unlock(&pa
->pa_lock
);
4325 /* only lg prealloc space */
4326 BUG_ON(pa
->pa_type
!= MB_GROUP_PA
);
4328 /* seems this one can be freed ... */
4330 spin_unlock(&pa
->pa_lock
);
4332 list_del_rcu(&pa
->pa_inode_list
);
4333 list_add(&pa
->u
.pa_tmp_list
, &discard_list
);
4336 if (total_entries
<= 5) {
4338 * we want to keep only 5 entries
4339 * allowing it to grow to 8. This
4340 * mak sure we don't call discard
4341 * soon for this list.
4346 spin_unlock(&lg
->lg_prealloc_lock
);
4348 list_for_each_entry_safe(pa
, tmp
, &discard_list
, u
.pa_tmp_list
) {
4350 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
4351 if (ext4_mb_load_buddy(sb
, group
, &e4b
)) {
4352 ext4_error(sb
, __func__
, "Error in loading buddy "
4353 "information for %u", group
);
4356 ext4_lock_group(sb
, group
);
4357 list_del(&pa
->pa_group_list
);
4358 ext4_mb_release_group_pa(&e4b
, pa
, ac
);
4359 ext4_unlock_group(sb
, group
);
4361 ext4_mb_release_desc(&e4b
);
4362 list_del(&pa
->u
.pa_tmp_list
);
4363 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4366 kmem_cache_free(ext4_ac_cachep
, ac
);
4370 * We have incremented pa_count. So it cannot be freed at this
4371 * point. Also we hold lg_mutex. So no parallel allocation is
4372 * possible from this lg. That means pa_free cannot be updated.
4374 * A parallel ext4_mb_discard_group_preallocations is possible.
4375 * which can cause the lg_prealloc_list to be updated.
4378 static void ext4_mb_add_n_trim(struct ext4_allocation_context
*ac
)
4380 int order
, added
= 0, lg_prealloc_count
= 1;
4381 struct super_block
*sb
= ac
->ac_sb
;
4382 struct ext4_locality_group
*lg
= ac
->ac_lg
;
4383 struct ext4_prealloc_space
*tmp_pa
, *pa
= ac
->ac_pa
;
4385 order
= fls(pa
->pa_free
) - 1;
4386 if (order
> PREALLOC_TB_SIZE
- 1)
4387 /* The max size of hash table is PREALLOC_TB_SIZE */
4388 order
= PREALLOC_TB_SIZE
- 1;
4389 /* Add the prealloc space to lg */
4391 list_for_each_entry_rcu(tmp_pa
, &lg
->lg_prealloc_list
[order
],
4393 spin_lock(&tmp_pa
->pa_lock
);
4394 if (tmp_pa
->pa_deleted
) {
4395 spin_unlock(&tmp_pa
->pa_lock
);
4398 if (!added
&& pa
->pa_free
< tmp_pa
->pa_free
) {
4399 /* Add to the tail of the previous entry */
4400 list_add_tail_rcu(&pa
->pa_inode_list
,
4401 &tmp_pa
->pa_inode_list
);
4404 * we want to count the total
4405 * number of entries in the list
4408 spin_unlock(&tmp_pa
->pa_lock
);
4409 lg_prealloc_count
++;
4412 list_add_tail_rcu(&pa
->pa_inode_list
,
4413 &lg
->lg_prealloc_list
[order
]);
4416 /* Now trim the list to be not more than 8 elements */
4417 if (lg_prealloc_count
> 8) {
4418 ext4_mb_discard_lg_preallocations(sb
, lg
,
4419 order
, lg_prealloc_count
);
4426 * release all resource we used in allocation
4428 static int ext4_mb_release_context(struct ext4_allocation_context
*ac
)
4430 struct ext4_prealloc_space
*pa
= ac
->ac_pa
;
4432 if (pa
->pa_type
== MB_GROUP_PA
) {
4433 /* see comment in ext4_mb_use_group_pa() */
4434 spin_lock(&pa
->pa_lock
);
4435 pa
->pa_pstart
+= ac
->ac_b_ex
.fe_len
;
4436 pa
->pa_lstart
+= ac
->ac_b_ex
.fe_len
;
4437 pa
->pa_free
-= ac
->ac_b_ex
.fe_len
;
4438 pa
->pa_len
-= ac
->ac_b_ex
.fe_len
;
4439 spin_unlock(&pa
->pa_lock
);
4443 up_read(ac
->alloc_semp
);
4446 * We want to add the pa to the right bucket.
4447 * Remove it from the list and while adding
4448 * make sure the list to which we are adding
4449 * doesn't grow big. We need to release
4450 * alloc_semp before calling ext4_mb_add_n_trim()
4452 if ((pa
->pa_type
== MB_GROUP_PA
) && likely(pa
->pa_free
)) {
4453 spin_lock(pa
->pa_obj_lock
);
4454 list_del_rcu(&pa
->pa_inode_list
);
4455 spin_unlock(pa
->pa_obj_lock
);
4456 ext4_mb_add_n_trim(ac
);
4458 ext4_mb_put_pa(ac
, ac
->ac_sb
, pa
);
4460 if (ac
->ac_bitmap_page
)
4461 page_cache_release(ac
->ac_bitmap_page
);
4462 if (ac
->ac_buddy_page
)
4463 page_cache_release(ac
->ac_buddy_page
);
4464 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
4465 mutex_unlock(&ac
->ac_lg
->lg_mutex
);
4466 ext4_mb_collect_stats(ac
);
4470 static int ext4_mb_discard_preallocations(struct super_block
*sb
, int needed
)
4476 trace_mark(ext4_mb_discard_preallocations
, "dev %s needed %d",
4478 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
&& needed
> 0; i
++) {
4479 ret
= ext4_mb_discard_group_preallocations(sb
, i
, needed
);
4488 * Main entry point into mballoc to allocate blocks
4489 * it tries to use preallocation first, then falls back
4490 * to usual allocation
4492 ext4_fsblk_t
ext4_mb_new_blocks(handle_t
*handle
,
4493 struct ext4_allocation_request
*ar
, int *errp
)
4496 struct ext4_allocation_context
*ac
= NULL
;
4497 struct ext4_sb_info
*sbi
;
4498 struct super_block
*sb
;
4499 ext4_fsblk_t block
= 0;
4500 unsigned int inquota
= 0;
4501 unsigned int reserv_blks
= 0;
4503 sb
= ar
->inode
->i_sb
;
4506 trace_mark(ext4_request_blocks
, "dev %s flags %u len %u ino %lu "
4507 "lblk %llu goal %llu lleft %llu lright %llu "
4508 "pleft %llu pright %llu ",
4509 sb
->s_id
, ar
->flags
, ar
->len
,
4510 ar
->inode
? ar
->inode
->i_ino
: 0,
4511 (unsigned long long) ar
->logical
,
4512 (unsigned long long) ar
->goal
,
4513 (unsigned long long) ar
->lleft
,
4514 (unsigned long long) ar
->lright
,
4515 (unsigned long long) ar
->pleft
,
4516 (unsigned long long) ar
->pright
);
4519 * For delayed allocation, we could skip the ENOSPC and
4520 * EDQUOT check, as blocks and quotas have been already
4521 * reserved when data being copied into pagecache.
4523 if (EXT4_I(ar
->inode
)->i_delalloc_reserved_flag
)
4524 ar
->flags
|= EXT4_MB_DELALLOC_RESERVED
;
4526 /* Without delayed allocation we need to verify
4527 * there is enough free blocks to do block allocation
4528 * and verify allocation doesn't exceed the quota limits.
4530 while (ar
->len
&& ext4_claim_free_blocks(sbi
, ar
->len
)) {
4531 /* let others to free the space */
4533 ar
->len
= ar
->len
>> 1;
4539 reserv_blks
= ar
->len
;
4540 while (ar
->len
&& vfs_dq_alloc_block(ar
->inode
, ar
->len
)) {
4541 ar
->flags
|= EXT4_MB_HINT_NOPREALLOC
;
4551 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4558 *errp
= ext4_mb_initialize_context(ac
, ar
);
4564 ac
->ac_op
= EXT4_MB_HISTORY_PREALLOC
;
4565 if (!ext4_mb_use_preallocated(ac
)) {
4566 ac
->ac_op
= EXT4_MB_HISTORY_ALLOC
;
4567 ext4_mb_normalize_request(ac
, ar
);
4569 /* allocate space in core */
4570 ext4_mb_regular_allocator(ac
);
4572 /* as we've just preallocated more space than
4573 * user requested orinally, we store allocated
4574 * space in a special descriptor */
4575 if (ac
->ac_status
== AC_STATUS_FOUND
&&
4576 ac
->ac_o_ex
.fe_len
< ac
->ac_b_ex
.fe_len
)
4577 ext4_mb_new_preallocation(ac
);
4579 if (likely(ac
->ac_status
== AC_STATUS_FOUND
)) {
4580 *errp
= ext4_mb_mark_diskspace_used(ac
, handle
, reserv_blks
);
4581 if (*errp
== -EAGAIN
) {
4583 * drop the reference that we took
4584 * in ext4_mb_use_best_found
4586 ext4_mb_release_context(ac
);
4587 ac
->ac_b_ex
.fe_group
= 0;
4588 ac
->ac_b_ex
.fe_start
= 0;
4589 ac
->ac_b_ex
.fe_len
= 0;
4590 ac
->ac_status
= AC_STATUS_CONTINUE
;
4593 ac
->ac_b_ex
.fe_len
= 0;
4595 ext4_mb_show_ac(ac
);
4597 block
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
4598 ar
->len
= ac
->ac_b_ex
.fe_len
;
4601 freed
= ext4_mb_discard_preallocations(sb
, ac
->ac_o_ex
.fe_len
);
4605 ac
->ac_b_ex
.fe_len
= 0;
4607 ext4_mb_show_ac(ac
);
4610 ext4_mb_release_context(ac
);
4613 kmem_cache_free(ext4_ac_cachep
, ac
);
4615 if (inquota
&& ar
->len
< inquota
)
4616 vfs_dq_free_block(ar
->inode
, inquota
- ar
->len
);
4619 if (!EXT4_I(ar
->inode
)->i_delalloc_reserved_flag
)
4620 /* release all the reserved blocks if non delalloc */
4621 percpu_counter_sub(&sbi
->s_dirtyblocks_counter
,
4625 trace_mark(ext4_allocate_blocks
,
4626 "dev %s block %llu flags %u len %u ino %lu "
4627 "logical %llu goal %llu lleft %llu lright %llu "
4628 "pleft %llu pright %llu ",
4629 sb
->s_id
, (unsigned long long) block
,
4630 ar
->flags
, ar
->len
, ar
->inode
? ar
->inode
->i_ino
: 0,
4631 (unsigned long long) ar
->logical
,
4632 (unsigned long long) ar
->goal
,
4633 (unsigned long long) ar
->lleft
,
4634 (unsigned long long) ar
->lright
,
4635 (unsigned long long) ar
->pleft
,
4636 (unsigned long long) ar
->pright
);
4642 * We can merge two free data extents only if the physical blocks
4643 * are contiguous, AND the extents were freed by the same transaction,
4644 * AND the blocks are associated with the same group.
4646 static int can_merge(struct ext4_free_data
*entry1
,
4647 struct ext4_free_data
*entry2
)
4649 if ((entry1
->t_tid
== entry2
->t_tid
) &&
4650 (entry1
->group
== entry2
->group
) &&
4651 ((entry1
->start_blk
+ entry1
->count
) == entry2
->start_blk
))
4656 static noinline_for_stack
int
4657 ext4_mb_free_metadata(handle_t
*handle
, struct ext4_buddy
*e4b
,
4658 struct ext4_free_data
*new_entry
)
4660 ext4_grpblk_t block
;
4661 struct ext4_free_data
*entry
;
4662 struct ext4_group_info
*db
= e4b
->bd_info
;
4663 struct super_block
*sb
= e4b
->bd_sb
;
4664 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4665 struct rb_node
**n
= &db
->bb_free_root
.rb_node
, *node
;
4666 struct rb_node
*parent
= NULL
, *new_node
;
4668 BUG_ON(!ext4_handle_valid(handle
));
4669 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
4670 BUG_ON(e4b
->bd_buddy_page
== NULL
);
4672 new_node
= &new_entry
->node
;
4673 block
= new_entry
->start_blk
;
4676 /* first free block exent. We need to
4677 protect buddy cache from being freed,
4678 * otherwise we'll refresh it from
4679 * on-disk bitmap and lose not-yet-available
4681 page_cache_get(e4b
->bd_buddy_page
);
4682 page_cache_get(e4b
->bd_bitmap_page
);
4686 entry
= rb_entry(parent
, struct ext4_free_data
, node
);
4687 if (block
< entry
->start_blk
)
4689 else if (block
>= (entry
->start_blk
+ entry
->count
))
4690 n
= &(*n
)->rb_right
;
4692 ext4_grp_locked_error(sb
, e4b
->bd_group
, __func__
,
4693 "Double free of blocks %d (%d %d)",
4694 block
, entry
->start_blk
, entry
->count
);
4699 rb_link_node(new_node
, parent
, n
);
4700 rb_insert_color(new_node
, &db
->bb_free_root
);
4702 /* Now try to see the extent can be merged to left and right */
4703 node
= rb_prev(new_node
);
4705 entry
= rb_entry(node
, struct ext4_free_data
, node
);
4706 if (can_merge(entry
, new_entry
)) {
4707 new_entry
->start_blk
= entry
->start_blk
;
4708 new_entry
->count
+= entry
->count
;
4709 rb_erase(node
, &(db
->bb_free_root
));
4710 spin_lock(&sbi
->s_md_lock
);
4711 list_del(&entry
->list
);
4712 spin_unlock(&sbi
->s_md_lock
);
4713 kmem_cache_free(ext4_free_ext_cachep
, entry
);
4717 node
= rb_next(new_node
);
4719 entry
= rb_entry(node
, struct ext4_free_data
, node
);
4720 if (can_merge(new_entry
, entry
)) {
4721 new_entry
->count
+= entry
->count
;
4722 rb_erase(node
, &(db
->bb_free_root
));
4723 spin_lock(&sbi
->s_md_lock
);
4724 list_del(&entry
->list
);
4725 spin_unlock(&sbi
->s_md_lock
);
4726 kmem_cache_free(ext4_free_ext_cachep
, entry
);
4729 /* Add the extent to transaction's private list */
4730 spin_lock(&sbi
->s_md_lock
);
4731 list_add(&new_entry
->list
, &handle
->h_transaction
->t_private_list
);
4732 spin_unlock(&sbi
->s_md_lock
);
4737 * Main entry point into mballoc to free blocks
4739 void ext4_mb_free_blocks(handle_t
*handle
, struct inode
*inode
,
4740 unsigned long block
, unsigned long count
,
4741 int metadata
, unsigned long *freed
)
4743 struct buffer_head
*bitmap_bh
= NULL
;
4744 struct super_block
*sb
= inode
->i_sb
;
4745 struct ext4_allocation_context
*ac
= NULL
;
4746 struct ext4_group_desc
*gdp
;
4747 struct ext4_super_block
*es
;
4748 unsigned int overflow
;
4750 struct buffer_head
*gd_bh
;
4751 ext4_group_t block_group
;
4752 struct ext4_sb_info
*sbi
;
4753 struct ext4_buddy e4b
;
4760 es
= EXT4_SB(sb
)->s_es
;
4761 if (block
< le32_to_cpu(es
->s_first_data_block
) ||
4762 block
+ count
< block
||
4763 block
+ count
> ext4_blocks_count(es
)) {
4764 ext4_error(sb
, __func__
,
4765 "Freeing blocks not in datazone - "
4766 "block = %lu, count = %lu", block
, count
);
4770 ext4_debug("freeing block %lu\n", block
);
4771 trace_mark(ext4_free_blocks
,
4772 "dev %s block %llu count %lu metadata %d ino %lu",
4773 sb
->s_id
, (unsigned long long) block
, count
, metadata
,
4774 inode
? inode
->i_ino
: 0);
4776 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4778 ac
->ac_op
= EXT4_MB_HISTORY_FREE
;
4779 ac
->ac_inode
= inode
;
4785 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
4788 * Check to see if we are freeing blocks across a group
4791 if (bit
+ count
> EXT4_BLOCKS_PER_GROUP(sb
)) {
4792 overflow
= bit
+ count
- EXT4_BLOCKS_PER_GROUP(sb
);
4795 bitmap_bh
= ext4_read_block_bitmap(sb
, block_group
);
4800 gdp
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
4806 if (in_range(ext4_block_bitmap(sb
, gdp
), block
, count
) ||
4807 in_range(ext4_inode_bitmap(sb
, gdp
), block
, count
) ||
4808 in_range(block
, ext4_inode_table(sb
, gdp
),
4809 EXT4_SB(sb
)->s_itb_per_group
) ||
4810 in_range(block
+ count
- 1, ext4_inode_table(sb
, gdp
),
4811 EXT4_SB(sb
)->s_itb_per_group
)) {
4813 ext4_error(sb
, __func__
,
4814 "Freeing blocks in system zone - "
4815 "Block = %lu, count = %lu", block
, count
);
4816 /* err = 0. ext4_std_error should be a no op */
4820 BUFFER_TRACE(bitmap_bh
, "getting write access");
4821 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
4826 * We are about to modify some metadata. Call the journal APIs
4827 * to unshare ->b_data if a currently-committing transaction is
4830 BUFFER_TRACE(gd_bh
, "get_write_access");
4831 err
= ext4_journal_get_write_access(handle
, gd_bh
);
4834 #ifdef AGGRESSIVE_CHECK
4837 for (i
= 0; i
< count
; i
++)
4838 BUG_ON(!mb_test_bit(bit
+ i
, bitmap_bh
->b_data
));
4842 ac
->ac_b_ex
.fe_group
= block_group
;
4843 ac
->ac_b_ex
.fe_start
= bit
;
4844 ac
->ac_b_ex
.fe_len
= count
;
4845 ext4_mb_store_history(ac
);
4848 err
= ext4_mb_load_buddy(sb
, block_group
, &e4b
);
4851 if (metadata
&& ext4_handle_valid(handle
)) {
4852 struct ext4_free_data
*new_entry
;
4854 * blocks being freed are metadata. these blocks shouldn't
4855 * be used until this transaction is committed
4857 new_entry
= kmem_cache_alloc(ext4_free_ext_cachep
, GFP_NOFS
);
4858 new_entry
->start_blk
= bit
;
4859 new_entry
->group
= block_group
;
4860 new_entry
->count
= count
;
4861 new_entry
->t_tid
= handle
->h_transaction
->t_tid
;
4862 ext4_lock_group(sb
, block_group
);
4863 mb_clear_bits(sb_bgl_lock(sbi
, block_group
), bitmap_bh
->b_data
,
4865 ext4_mb_free_metadata(handle
, &e4b
, new_entry
);
4866 ext4_unlock_group(sb
, block_group
);
4868 ext4_lock_group(sb
, block_group
);
4869 /* need to update group_info->bb_free and bitmap
4870 * with group lock held. generate_buddy look at
4871 * them with group lock_held
4873 mb_clear_bits(sb_bgl_lock(sbi
, block_group
), bitmap_bh
->b_data
,
4875 mb_free_blocks(inode
, &e4b
, bit
, count
);
4876 ext4_mb_return_to_preallocation(inode
, &e4b
, block
, count
);
4877 ext4_unlock_group(sb
, block_group
);
4880 spin_lock(sb_bgl_lock(sbi
, block_group
));
4881 ret
= ext4_free_blks_count(sb
, gdp
) + count
;
4882 ext4_free_blks_set(sb
, gdp
, ret
);
4883 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, block_group
, gdp
);
4884 spin_unlock(sb_bgl_lock(sbi
, block_group
));
4885 percpu_counter_add(&sbi
->s_freeblocks_counter
, count
);
4887 if (sbi
->s_log_groups_per_flex
) {
4888 ext4_group_t flex_group
= ext4_flex_group(sbi
, block_group
);
4889 atomic_add(count
, &sbi
->s_flex_groups
[flex_group
].free_blocks
);
4892 ext4_mb_release_desc(&e4b
);
4896 /* We dirtied the bitmap block */
4897 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
4898 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
4900 /* And the group descriptor block */
4901 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
4902 ret
= ext4_handle_dirty_metadata(handle
, NULL
, gd_bh
);
4906 if (overflow
&& !err
) {
4915 ext4_std_error(sb
, err
);
4917 kmem_cache_free(ext4_ac_cachep
, ac
);