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 group
50 * preallocation or inode preallocation depending on the size file. The
51 * size of the file could be the resulting file size we would have after
52 * allocation or the current file size which ever is larger. If the size is
53 * less that sbi->s_mb_stream_request we select the group
54 * preallocation. The default value of s_mb_stream_request is 16
55 * blocks. This can also be tuned via
56 * /proc/fs/ext4/<partition>/stream_req. The value is represented in terms
57 * of number of blocks.
59 * The main motivation for having small file use group preallocation is to
60 * ensure that we have small file closer in the disk.
62 * First stage the allocator looks at the inode prealloc list
63 * ext4_inode_info->i_prealloc_list contain list of prealloc spaces for
64 * this particular inode. The inode prealloc space is represented as:
66 * pa_lstart -> the logical start block for this prealloc space
67 * pa_pstart -> the physical start block for this prealloc space
68 * pa_len -> lenght for this prealloc space
69 * pa_free -> free space available in this prealloc space
71 * The inode preallocation space is used looking at the _logical_ start
72 * block. If only the logical file block falls within the range of prealloc
73 * space we will consume the particular prealloc space. This make sure that
74 * that the we have contiguous physical blocks representing the file blocks
76 * The important thing to be noted in case of inode prealloc space is that
77 * we don't modify the values associated to inode prealloc space except
80 * If we are not able to find blocks in the inode prealloc space and if we
81 * have the group allocation flag set then we look at the locality group
82 * prealloc space. These are per CPU prealloc list repreasented as
84 * ext4_sb_info.s_locality_groups[smp_processor_id()]
86 * The reason for having a per cpu locality group is to reduce the contention
87 * between CPUs. It is possible to get scheduled at this point.
89 * The locality group prealloc space is used looking at whether we have
90 * enough free space (pa_free) withing the prealloc space.
92 * If we can't allocate blocks via inode prealloc or/and locality group
93 * prealloc then we look at the buddy cache. The buddy cache is represented
94 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
95 * mapped to the buddy and bitmap information regarding different
96 * groups. The buddy information is attached to buddy cache inode so that
97 * we can access them through the page cache. The information regarding
98 * each group is loaded via ext4_mb_load_buddy. The information involve
99 * block bitmap and buddy information. The information are stored in the
103 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
106 * one block each for bitmap and buddy information. So for each group we
107 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
108 * blocksize) blocks. So it can have information regarding groups_per_page
109 * which is blocks_per_page/2
111 * The buddy cache inode is not stored on disk. The inode is thrown
112 * away when the filesystem is unmounted.
114 * We look for count number of blocks in the buddy cache. If we were able
115 * to locate that many free blocks we return with additional information
116 * regarding rest of the contiguous physical block available
118 * Before allocating blocks via buddy cache we normalize the request
119 * blocks. This ensure we ask for more blocks that we needed. The extra
120 * blocks that we get after allocation is added to the respective prealloc
121 * list. In case of inode preallocation we follow a list of heuristics
122 * based on file size. This can be found in ext4_mb_normalize_request. If
123 * we are doing a group prealloc we try to normalize the request to
124 * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is set to
125 * 512 blocks. This can be tuned via
126 * /proc/fs/ext4/<partition/group_prealloc. The value is represented in
127 * terms of number of blocks. If we have mounted the file system with -O
128 * stripe=<value> option the group prealloc request is normalized to the
129 * stripe value (sbi->s_stripe)
131 * The regular allocator(using the buddy cache) support few tunables.
133 * /proc/fs/ext4/<partition>/min_to_scan
134 * /proc/fs/ext4/<partition>/max_to_scan
135 * /proc/fs/ext4/<partition>/order2_req
137 * The regular allocator use buddy scan only if the request len is power of
138 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
139 * value of s_mb_order2_reqs can be tuned via
140 * /proc/fs/ext4/<partition>/order2_req. If the request len is equal to
141 * stripe size (sbi->s_stripe), we try to search for contigous block in
142 * stripe size. This should result in better allocation on RAID setup. If
143 * not we search in the specific group using bitmap for best extents. The
144 * tunable min_to_scan and max_to_scan controll the behaviour here.
145 * min_to_scan indicate how long the mballoc __must__ look for a best
146 * extent and max_to_scanindicate how long the mballoc __can__ look for a
147 * best extent in the found extents. Searching for the blocks starts with
148 * the group specified as the goal value in allocation context via
149 * ac_g_ex. Each group is first checked based on the criteria whether it
150 * can used for allocation. ext4_mb_good_group explains how the groups are
153 * Both the prealloc space are getting populated as above. So for the first
154 * request we will hit the buddy cache which will result in this prealloc
155 * space getting filled. The prealloc space is then later used for the
156 * subsequent request.
160 * mballoc operates on the following data:
162 * - in-core buddy (actually includes buddy and bitmap)
163 * - preallocation descriptors (PAs)
165 * there are two types of preallocations:
167 * assiged to specific inode and can be used for this inode only.
168 * it describes part of inode's space preallocated to specific
169 * physical blocks. any block from that preallocated can be used
170 * independent. the descriptor just tracks number of blocks left
171 * unused. so, before taking some block from descriptor, one must
172 * make sure corresponded logical block isn't allocated yet. this
173 * also means that freeing any block within descriptor's range
174 * must discard all preallocated blocks.
176 * assigned to specific locality group which does not translate to
177 * permanent set of inodes: inode can join and leave group. space
178 * from this type of preallocation can be used for any inode. thus
179 * it's consumed from the beginning to the end.
181 * relation between them can be expressed as:
182 * in-core buddy = on-disk bitmap + preallocation descriptors
184 * this mean blocks mballoc considers used are:
185 * - allocated blocks (persistent)
186 * - preallocated blocks (non-persistent)
188 * consistency in mballoc world means that at any time a block is either
189 * free or used in ALL structures. notice: "any time" should not be read
190 * literally -- time is discrete and delimited by locks.
192 * to keep it simple, we don't use block numbers, instead we count number of
193 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
195 * all operations can be expressed as:
196 * - init buddy: buddy = on-disk + PAs
197 * - new PA: buddy += N; PA = N
198 * - use inode PA: on-disk += N; PA -= N
199 * - discard inode PA buddy -= on-disk - PA; PA = 0
200 * - use locality group PA on-disk += N; PA -= N
201 * - discard locality group PA buddy -= PA; PA = 0
202 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
203 * is used in real operation because we can't know actual used
204 * bits from PA, only from on-disk bitmap
206 * if we follow this strict logic, then all operations above should be atomic.
207 * given some of them can block, we'd have to use something like semaphores
208 * killing performance on high-end SMP hardware. let's try to relax it using
209 * the following knowledge:
210 * 1) if buddy is referenced, it's already initialized
211 * 2) while block is used in buddy and the buddy is referenced,
212 * nobody can re-allocate that block
213 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
214 * bit set and PA claims same block, it's OK. IOW, one can set bit in
215 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
218 * so, now we're building a concurrency table:
221 * blocks for PA are allocated in the buddy, buddy must be referenced
222 * until PA is linked to allocation group to avoid concurrent buddy init
224 * we need to make sure that either on-disk bitmap or PA has uptodate data
225 * given (3) we care that PA-=N operation doesn't interfere with init
227 * the simplest way would be to have buddy initialized by the discard
228 * - use locality group PA
229 * again PA-=N must be serialized with init
230 * - discard locality group PA
231 * the simplest way would be to have buddy initialized by the discard
234 * i_data_sem serializes them
236 * discard process must wait until PA isn't used by another process
237 * - use locality group PA
238 * some mutex should serialize them
239 * - discard locality group PA
240 * discard process must wait until PA isn't used by another process
243 * i_data_sem or another mutex should serializes them
245 * discard process must wait until PA isn't used by another process
246 * - use locality group PA
247 * nothing wrong here -- they're different PAs covering different blocks
248 * - discard locality group PA
249 * discard process must wait until PA isn't used by another process
251 * now we're ready to make few consequences:
252 * - PA is referenced and while it is no discard is possible
253 * - PA is referenced until block isn't marked in on-disk bitmap
254 * - PA changes only after on-disk bitmap
255 * - discard must not compete with init. either init is done before
256 * any discard or they're serialized somehow
257 * - buddy init as sum of on-disk bitmap and PAs is done atomically
259 * a special case when we've used PA to emptiness. no need to modify buddy
260 * in this case, but we should care about concurrent init
265 * Logic in few words:
270 * mark bits in on-disk bitmap
273 * - use preallocation:
274 * find proper PA (per-inode or group)
276 * mark bits in on-disk bitmap
282 * mark bits in on-disk bitmap
285 * - discard preallocations in group:
287 * move them onto local list
288 * load on-disk bitmap
290 * remove PA from object (inode or locality group)
291 * mark free blocks in-core
293 * - discard inode's preallocations:
300 * - bitlock on a group (group)
301 * - object (inode/locality) (object)
312 * - release consumed pa:
317 * - generate in-core bitmap:
321 * - discard all for given object (inode, locality group):
326 * - discard all for given group:
333 static struct kmem_cache
*ext4_pspace_cachep
;
334 static struct kmem_cache
*ext4_ac_cachep
;
335 static struct kmem_cache
*ext4_free_ext_cachep
;
336 static void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
338 static int ext4_mb_init_per_dev_proc(struct super_block
*sb
);
339 static int ext4_mb_destroy_per_dev_proc(struct super_block
*sb
);
340 static void release_blocks_on_commit(journal_t
*journal
, transaction_t
*txn
);
344 static inline void *mb_correct_addr_and_bit(int *bit
, void *addr
)
346 #if BITS_PER_LONG == 64
347 *bit
+= ((unsigned long) addr
& 7UL) << 3;
348 addr
= (void *) ((unsigned long) addr
& ~7UL);
349 #elif BITS_PER_LONG == 32
350 *bit
+= ((unsigned long) addr
& 3UL) << 3;
351 addr
= (void *) ((unsigned long) addr
& ~3UL);
353 #error "how many bits you are?!"
358 static inline int mb_test_bit(int bit
, void *addr
)
361 * ext4_test_bit on architecture like powerpc
362 * needs unsigned long aligned address
364 addr
= mb_correct_addr_and_bit(&bit
, addr
);
365 return ext4_test_bit(bit
, addr
);
368 static inline void mb_set_bit(int bit
, void *addr
)
370 addr
= mb_correct_addr_and_bit(&bit
, addr
);
371 ext4_set_bit(bit
, addr
);
374 static inline void mb_set_bit_atomic(spinlock_t
*lock
, int bit
, void *addr
)
376 addr
= mb_correct_addr_and_bit(&bit
, addr
);
377 ext4_set_bit_atomic(lock
, bit
, addr
);
380 static inline void mb_clear_bit(int bit
, void *addr
)
382 addr
= mb_correct_addr_and_bit(&bit
, addr
);
383 ext4_clear_bit(bit
, addr
);
386 static inline void mb_clear_bit_atomic(spinlock_t
*lock
, int bit
, void *addr
)
388 addr
= mb_correct_addr_and_bit(&bit
, addr
);
389 ext4_clear_bit_atomic(lock
, bit
, addr
);
392 static inline int mb_find_next_zero_bit(void *addr
, int max
, int start
)
394 int fix
= 0, ret
, tmpmax
;
395 addr
= mb_correct_addr_and_bit(&fix
, addr
);
399 ret
= ext4_find_next_zero_bit(addr
, tmpmax
, start
) - fix
;
405 static inline int mb_find_next_bit(void *addr
, int max
, int start
)
407 int fix
= 0, ret
, tmpmax
;
408 addr
= mb_correct_addr_and_bit(&fix
, addr
);
412 ret
= ext4_find_next_bit(addr
, tmpmax
, start
) - fix
;
418 static void *mb_find_buddy(struct ext4_buddy
*e4b
, int order
, int *max
)
422 BUG_ON(EXT4_MB_BITMAP(e4b
) == EXT4_MB_BUDDY(e4b
));
425 if (order
> e4b
->bd_blkbits
+ 1) {
430 /* at order 0 we see each particular block */
431 *max
= 1 << (e4b
->bd_blkbits
+ 3);
433 return EXT4_MB_BITMAP(e4b
);
435 bb
= EXT4_MB_BUDDY(e4b
) + EXT4_SB(e4b
->bd_sb
)->s_mb_offsets
[order
];
436 *max
= EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[order
];
442 static void mb_free_blocks_double(struct inode
*inode
, struct ext4_buddy
*e4b
,
443 int first
, int count
)
446 struct super_block
*sb
= e4b
->bd_sb
;
448 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
450 BUG_ON(!ext4_is_group_locked(sb
, e4b
->bd_group
));
451 for (i
= 0; i
< count
; i
++) {
452 if (!mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
)) {
453 ext4_fsblk_t blocknr
;
454 blocknr
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
);
455 blocknr
+= first
+ i
;
457 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
459 ext4_error(sb
, __func__
, "double-free of inode"
460 " %lu's block %llu(bit %u in group %u)",
461 inode
? inode
->i_ino
: 0, blocknr
,
462 first
+ i
, e4b
->bd_group
);
464 mb_clear_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
468 static void mb_mark_used_double(struct ext4_buddy
*e4b
, int first
, int count
)
472 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
474 BUG_ON(!ext4_is_group_locked(e4b
->bd_sb
, e4b
->bd_group
));
475 for (i
= 0; i
< count
; i
++) {
476 BUG_ON(mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
));
477 mb_set_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
481 static void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
483 if (memcmp(e4b
->bd_info
->bb_bitmap
, bitmap
, e4b
->bd_sb
->s_blocksize
)) {
484 unsigned char *b1
, *b2
;
486 b1
= (unsigned char *) e4b
->bd_info
->bb_bitmap
;
487 b2
= (unsigned char *) bitmap
;
488 for (i
= 0; i
< e4b
->bd_sb
->s_blocksize
; i
++) {
489 if (b1
[i
] != b2
[i
]) {
490 printk(KERN_ERR
"corruption in group %u "
491 "at byte %u(%u): %x in copy != %x "
492 "on disk/prealloc\n",
493 e4b
->bd_group
, i
, i
* 8, b1
[i
], b2
[i
]);
501 static inline void mb_free_blocks_double(struct inode
*inode
,
502 struct ext4_buddy
*e4b
, int first
, int count
)
506 static inline void mb_mark_used_double(struct ext4_buddy
*e4b
,
507 int first
, int count
)
511 static inline void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
517 #ifdef AGGRESSIVE_CHECK
519 #define MB_CHECK_ASSERT(assert) \
523 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
524 function, file, line, # assert); \
529 static int __mb_check_buddy(struct ext4_buddy
*e4b
, char *file
,
530 const char *function
, int line
)
532 struct super_block
*sb
= e4b
->bd_sb
;
533 int order
= e4b
->bd_blkbits
+ 1;
540 struct ext4_group_info
*grp
;
543 struct list_head
*cur
;
548 static int mb_check_counter
;
549 if (mb_check_counter
++ % 100 != 0)
554 buddy
= mb_find_buddy(e4b
, order
, &max
);
555 MB_CHECK_ASSERT(buddy
);
556 buddy2
= mb_find_buddy(e4b
, order
- 1, &max2
);
557 MB_CHECK_ASSERT(buddy2
);
558 MB_CHECK_ASSERT(buddy
!= buddy2
);
559 MB_CHECK_ASSERT(max
* 2 == max2
);
562 for (i
= 0; i
< max
; i
++) {
564 if (mb_test_bit(i
, buddy
)) {
565 /* only single bit in buddy2 may be 1 */
566 if (!mb_test_bit(i
<< 1, buddy2
)) {
568 mb_test_bit((i
<<1)+1, buddy2
));
569 } else if (!mb_test_bit((i
<< 1) + 1, buddy2
)) {
571 mb_test_bit(i
<< 1, buddy2
));
576 /* both bits in buddy2 must be 0 */
577 MB_CHECK_ASSERT(mb_test_bit(i
<< 1, buddy2
));
578 MB_CHECK_ASSERT(mb_test_bit((i
<< 1) + 1, buddy2
));
580 for (j
= 0; j
< (1 << order
); j
++) {
581 k
= (i
* (1 << order
)) + j
;
583 !mb_test_bit(k
, EXT4_MB_BITMAP(e4b
)));
587 MB_CHECK_ASSERT(e4b
->bd_info
->bb_counters
[order
] == count
);
592 buddy
= mb_find_buddy(e4b
, 0, &max
);
593 for (i
= 0; i
< max
; i
++) {
594 if (!mb_test_bit(i
, buddy
)) {
595 MB_CHECK_ASSERT(i
>= e4b
->bd_info
->bb_first_free
);
603 /* check used bits only */
604 for (j
= 0; j
< e4b
->bd_blkbits
+ 1; j
++) {
605 buddy2
= mb_find_buddy(e4b
, j
, &max2
);
607 MB_CHECK_ASSERT(k
< max2
);
608 MB_CHECK_ASSERT(mb_test_bit(k
, buddy2
));
611 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b
->bd_info
));
612 MB_CHECK_ASSERT(e4b
->bd_info
->bb_fragments
== fragments
);
614 grp
= ext4_get_group_info(sb
, e4b
->bd_group
);
615 buddy
= mb_find_buddy(e4b
, 0, &max
);
616 list_for_each(cur
, &grp
->bb_prealloc_list
) {
617 ext4_group_t groupnr
;
618 struct ext4_prealloc_space
*pa
;
619 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
620 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &groupnr
, &k
);
621 MB_CHECK_ASSERT(groupnr
== e4b
->bd_group
);
622 for (i
= 0; i
< pa
->pa_len
; i
++)
623 MB_CHECK_ASSERT(mb_test_bit(k
+ i
, buddy
));
627 #undef MB_CHECK_ASSERT
628 #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
629 __FILE__, __func__, __LINE__)
631 #define mb_check_buddy(e4b)
634 /* FIXME!! need more doc */
635 static void ext4_mb_mark_free_simple(struct super_block
*sb
,
636 void *buddy
, unsigned first
, int len
,
637 struct ext4_group_info
*grp
)
639 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
642 unsigned short chunk
;
643 unsigned short border
;
645 BUG_ON(len
> EXT4_BLOCKS_PER_GROUP(sb
));
647 border
= 2 << sb
->s_blocksize_bits
;
650 /* find how many blocks can be covered since this position */
651 max
= ffs(first
| border
) - 1;
653 /* find how many blocks of power 2 we need to mark */
660 /* mark multiblock chunks only */
661 grp
->bb_counters
[min
]++;
663 mb_clear_bit(first
>> min
,
664 buddy
+ sbi
->s_mb_offsets
[min
]);
671 static void ext4_mb_generate_buddy(struct super_block
*sb
,
672 void *buddy
, void *bitmap
, ext4_group_t group
)
674 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
675 unsigned short max
= EXT4_BLOCKS_PER_GROUP(sb
);
676 unsigned short i
= 0;
677 unsigned short first
;
680 unsigned fragments
= 0;
681 unsigned long long period
= get_cycles();
683 /* initialize buddy from bitmap which is aggregation
684 * of on-disk bitmap and preallocations */
685 i
= mb_find_next_zero_bit(bitmap
, max
, 0);
686 grp
->bb_first_free
= i
;
690 i
= mb_find_next_bit(bitmap
, max
, i
);
694 ext4_mb_mark_free_simple(sb
, buddy
, first
, len
, grp
);
696 grp
->bb_counters
[0]++;
698 i
= mb_find_next_zero_bit(bitmap
, max
, i
);
700 grp
->bb_fragments
= fragments
;
702 if (free
!= grp
->bb_free
) {
703 ext4_error(sb
, __func__
,
704 "EXT4-fs: group %u: %u blocks in bitmap, %u in gd",
705 group
, free
, grp
->bb_free
);
707 * If we intent to continue, we consider group descritor
708 * corrupt and update bb_free using bitmap value
713 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
, &(grp
->bb_state
));
715 period
= get_cycles() - period
;
716 spin_lock(&EXT4_SB(sb
)->s_bal_lock
);
717 EXT4_SB(sb
)->s_mb_buddies_generated
++;
718 EXT4_SB(sb
)->s_mb_generation_time
+= period
;
719 spin_unlock(&EXT4_SB(sb
)->s_bal_lock
);
722 /* The buddy information is attached the buddy cache inode
723 * for convenience. The information regarding each group
724 * is loaded via ext4_mb_load_buddy. The information involve
725 * block bitmap and buddy information. The information are
726 * stored in the inode as
729 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
732 * one block each for bitmap and buddy information.
733 * So for each group we take up 2 blocks. A page can
734 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
735 * So it can have information regarding groups_per_page which
736 * is blocks_per_page/2
739 static int ext4_mb_init_cache(struct page
*page
, char *incore
)
746 ext4_group_t first_group
;
748 struct super_block
*sb
;
749 struct buffer_head
*bhs
;
750 struct buffer_head
**bh
;
755 mb_debug("init page %lu\n", page
->index
);
757 inode
= page
->mapping
->host
;
759 blocksize
= 1 << inode
->i_blkbits
;
760 blocks_per_page
= PAGE_CACHE_SIZE
/ blocksize
;
762 groups_per_page
= blocks_per_page
>> 1;
763 if (groups_per_page
== 0)
766 /* allocate buffer_heads to read bitmaps */
767 if (groups_per_page
> 1) {
769 i
= sizeof(struct buffer_head
*) * groups_per_page
;
770 bh
= kzalloc(i
, GFP_NOFS
);
776 first_group
= page
->index
* blocks_per_page
/ 2;
778 /* read all groups the page covers into the cache */
779 for (i
= 0; i
< groups_per_page
; i
++) {
780 struct ext4_group_desc
*desc
;
782 if (first_group
+ i
>= EXT4_SB(sb
)->s_groups_count
)
786 desc
= ext4_get_group_desc(sb
, first_group
+ i
, NULL
);
791 bh
[i
] = sb_getblk(sb
, ext4_block_bitmap(sb
, desc
));
795 if (buffer_uptodate(bh
[i
]) &&
796 !(desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)))
800 spin_lock(sb_bgl_lock(EXT4_SB(sb
), first_group
+ i
));
801 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
802 ext4_init_block_bitmap(sb
, bh
[i
],
803 first_group
+ i
, desc
);
804 set_buffer_uptodate(bh
[i
]);
805 unlock_buffer(bh
[i
]);
806 spin_unlock(sb_bgl_lock(EXT4_SB(sb
), first_group
+ i
));
809 spin_unlock(sb_bgl_lock(EXT4_SB(sb
), first_group
+ i
));
811 bh
[i
]->b_end_io
= end_buffer_read_sync
;
812 submit_bh(READ
, bh
[i
]);
813 mb_debug("read bitmap for group %u\n", first_group
+ i
);
816 /* wait for I/O completion */
817 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
818 wait_on_buffer(bh
[i
]);
821 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
822 if (!buffer_uptodate(bh
[i
]))
826 first_block
= page
->index
* blocks_per_page
;
827 for (i
= 0; i
< blocks_per_page
; i
++) {
829 struct ext4_group_info
*grinfo
;
831 group
= (first_block
+ i
) >> 1;
832 if (group
>= EXT4_SB(sb
)->s_groups_count
)
836 * data carry information regarding this
837 * particular group in the format specified
841 data
= page_address(page
) + (i
* blocksize
);
842 bitmap
= bh
[group
- first_group
]->b_data
;
845 * We place the buddy block and bitmap block
848 if ((first_block
+ i
) & 1) {
849 /* this is block of buddy */
850 BUG_ON(incore
== NULL
);
851 mb_debug("put buddy for group %u in page %lu/%x\n",
852 group
, page
->index
, i
* blocksize
);
853 memset(data
, 0xff, blocksize
);
854 grinfo
= ext4_get_group_info(sb
, group
);
855 grinfo
->bb_fragments
= 0;
856 memset(grinfo
->bb_counters
, 0,
857 sizeof(unsigned short)*(sb
->s_blocksize_bits
+2));
859 * incore got set to the group block bitmap below
861 ext4_mb_generate_buddy(sb
, data
, incore
, group
);
864 /* this is block of bitmap */
865 BUG_ON(incore
!= NULL
);
866 mb_debug("put bitmap for group %u in page %lu/%x\n",
867 group
, page
->index
, i
* blocksize
);
869 /* see comments in ext4_mb_put_pa() */
870 ext4_lock_group(sb
, group
);
871 memcpy(data
, bitmap
, blocksize
);
873 /* mark all preallocated blks used in in-core bitmap */
874 ext4_mb_generate_from_pa(sb
, data
, group
);
875 ext4_unlock_group(sb
, group
);
877 /* set incore so that the buddy information can be
878 * generated using this
883 SetPageUptodate(page
);
887 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
895 static noinline_for_stack
int
896 ext4_mb_load_buddy(struct super_block
*sb
, ext4_group_t group
,
897 struct ext4_buddy
*e4b
)
905 struct ext4_group_info
*grp
;
906 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
907 struct inode
*inode
= sbi
->s_buddy_cache
;
909 mb_debug("load group %u\n", group
);
911 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
912 grp
= ext4_get_group_info(sb
, group
);
914 e4b
->bd_blkbits
= sb
->s_blocksize_bits
;
915 e4b
->bd_info
= ext4_get_group_info(sb
, group
);
917 e4b
->bd_group
= group
;
918 e4b
->bd_buddy_page
= NULL
;
919 e4b
->bd_bitmap_page
= NULL
;
920 e4b
->alloc_semp
= &grp
->alloc_sem
;
922 /* Take the read lock on the group alloc
923 * sem. This would make sure a parallel
924 * ext4_mb_init_group happening on other
925 * groups mapped by the page is blocked
926 * till we are done with allocation
928 down_read(e4b
->alloc_semp
);
931 * the buddy cache inode stores the block bitmap
932 * and buddy information in consecutive blocks.
933 * So for each group we need two blocks.
936 pnum
= block
/ blocks_per_page
;
937 poff
= block
% blocks_per_page
;
939 /* we could use find_or_create_page(), but it locks page
940 * what we'd like to avoid in fast path ... */
941 page
= find_get_page(inode
->i_mapping
, pnum
);
942 if (page
== NULL
|| !PageUptodate(page
)) {
945 * drop the page reference and try
946 * to get the page with lock. If we
947 * are not uptodate that implies
948 * somebody just created the page but
949 * is yet to initialize the same. So
950 * wait for it to initialize.
952 page_cache_release(page
);
953 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
955 BUG_ON(page
->mapping
!= inode
->i_mapping
);
956 if (!PageUptodate(page
)) {
957 ret
= ext4_mb_init_cache(page
, NULL
);
962 mb_cmp_bitmaps(e4b
, page_address(page
) +
963 (poff
* sb
->s_blocksize
));
968 if (page
== NULL
|| !PageUptodate(page
)) {
972 e4b
->bd_bitmap_page
= page
;
973 e4b
->bd_bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
974 mark_page_accessed(page
);
977 pnum
= block
/ blocks_per_page
;
978 poff
= block
% blocks_per_page
;
980 page
= find_get_page(inode
->i_mapping
, pnum
);
981 if (page
== NULL
|| !PageUptodate(page
)) {
983 page_cache_release(page
);
984 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
986 BUG_ON(page
->mapping
!= inode
->i_mapping
);
987 if (!PageUptodate(page
)) {
988 ret
= ext4_mb_init_cache(page
, e4b
->bd_bitmap
);
997 if (page
== NULL
|| !PageUptodate(page
)) {
1001 e4b
->bd_buddy_page
= page
;
1002 e4b
->bd_buddy
= page_address(page
) + (poff
* sb
->s_blocksize
);
1003 mark_page_accessed(page
);
1005 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
1006 BUG_ON(e4b
->bd_buddy_page
== NULL
);
1011 if (e4b
->bd_bitmap_page
)
1012 page_cache_release(e4b
->bd_bitmap_page
);
1013 if (e4b
->bd_buddy_page
)
1014 page_cache_release(e4b
->bd_buddy_page
);
1015 e4b
->bd_buddy
= NULL
;
1016 e4b
->bd_bitmap
= NULL
;
1018 /* Done with the buddy cache */
1019 up_read(e4b
->alloc_semp
);
1023 static void ext4_mb_release_desc(struct ext4_buddy
*e4b
)
1025 if (e4b
->bd_bitmap_page
)
1026 page_cache_release(e4b
->bd_bitmap_page
);
1027 if (e4b
->bd_buddy_page
)
1028 page_cache_release(e4b
->bd_buddy_page
);
1029 /* Done with the buddy cache */
1030 up_read(e4b
->alloc_semp
);
1034 static int mb_find_order_for_block(struct ext4_buddy
*e4b
, int block
)
1039 BUG_ON(EXT4_MB_BITMAP(e4b
) == EXT4_MB_BUDDY(e4b
));
1040 BUG_ON(block
>= (1 << (e4b
->bd_blkbits
+ 3)));
1042 bb
= EXT4_MB_BUDDY(e4b
);
1043 while (order
<= e4b
->bd_blkbits
+ 1) {
1045 if (!mb_test_bit(block
, bb
)) {
1046 /* this block is part of buddy of order 'order' */
1049 bb
+= 1 << (e4b
->bd_blkbits
- order
);
1055 static void mb_clear_bits(spinlock_t
*lock
, void *bm
, int cur
, int len
)
1061 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1062 /* fast path: clear whole word at once */
1063 addr
= bm
+ (cur
>> 3);
1068 mb_clear_bit_atomic(lock
, cur
, bm
);
1073 static void mb_set_bits(spinlock_t
*lock
, void *bm
, int cur
, int len
)
1079 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1080 /* fast path: set whole word at once */
1081 addr
= bm
+ (cur
>> 3);
1086 mb_set_bit_atomic(lock
, cur
, bm
);
1091 static void mb_free_blocks(struct inode
*inode
, struct ext4_buddy
*e4b
,
1092 int first
, int count
)
1101 struct super_block
*sb
= e4b
->bd_sb
;
1103 BUG_ON(first
+ count
> (sb
->s_blocksize
<< 3));
1104 BUG_ON(!ext4_is_group_locked(sb
, e4b
->bd_group
));
1105 mb_check_buddy(e4b
);
1106 mb_free_blocks_double(inode
, e4b
, first
, count
);
1108 e4b
->bd_info
->bb_free
+= count
;
1109 if (first
< e4b
->bd_info
->bb_first_free
)
1110 e4b
->bd_info
->bb_first_free
= first
;
1112 /* let's maintain fragments counter */
1114 block
= !mb_test_bit(first
- 1, EXT4_MB_BITMAP(e4b
));
1115 if (first
+ count
< EXT4_SB(sb
)->s_mb_maxs
[0])
1116 max
= !mb_test_bit(first
+ count
, EXT4_MB_BITMAP(e4b
));
1118 e4b
->bd_info
->bb_fragments
--;
1119 else if (!block
&& !max
)
1120 e4b
->bd_info
->bb_fragments
++;
1122 /* let's maintain buddy itself */
1123 while (count
-- > 0) {
1127 if (!mb_test_bit(block
, EXT4_MB_BITMAP(e4b
))) {
1128 ext4_fsblk_t blocknr
;
1129 blocknr
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
);
1132 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
1133 ext4_unlock_group(sb
, e4b
->bd_group
);
1134 ext4_error(sb
, __func__
, "double-free of inode"
1135 " %lu's block %llu(bit %u in group %u)",
1136 inode
? inode
->i_ino
: 0, blocknr
, block
,
1138 ext4_lock_group(sb
, e4b
->bd_group
);
1140 mb_clear_bit(block
, EXT4_MB_BITMAP(e4b
));
1141 e4b
->bd_info
->bb_counters
[order
]++;
1143 /* start of the buddy */
1144 buddy
= mb_find_buddy(e4b
, order
, &max
);
1148 if (mb_test_bit(block
, buddy
) ||
1149 mb_test_bit(block
+ 1, buddy
))
1152 /* both the buddies are free, try to coalesce them */
1153 buddy2
= mb_find_buddy(e4b
, order
+ 1, &max
);
1159 /* for special purposes, we don't set
1160 * free bits in bitmap */
1161 mb_set_bit(block
, buddy
);
1162 mb_set_bit(block
+ 1, buddy
);
1164 e4b
->bd_info
->bb_counters
[order
]--;
1165 e4b
->bd_info
->bb_counters
[order
]--;
1169 e4b
->bd_info
->bb_counters
[order
]++;
1171 mb_clear_bit(block
, buddy2
);
1175 mb_check_buddy(e4b
);
1178 static int mb_find_extent(struct ext4_buddy
*e4b
, int order
, int block
,
1179 int needed
, struct ext4_free_extent
*ex
)
1186 BUG_ON(!ext4_is_group_locked(e4b
->bd_sb
, e4b
->bd_group
));
1189 buddy
= mb_find_buddy(e4b
, order
, &max
);
1190 BUG_ON(buddy
== NULL
);
1191 BUG_ON(block
>= max
);
1192 if (mb_test_bit(block
, buddy
)) {
1199 /* FIXME dorp order completely ? */
1200 if (likely(order
== 0)) {
1201 /* find actual order */
1202 order
= mb_find_order_for_block(e4b
, block
);
1203 block
= block
>> order
;
1206 ex
->fe_len
= 1 << order
;
1207 ex
->fe_start
= block
<< order
;
1208 ex
->fe_group
= e4b
->bd_group
;
1210 /* calc difference from given start */
1211 next
= next
- ex
->fe_start
;
1213 ex
->fe_start
+= next
;
1215 while (needed
> ex
->fe_len
&&
1216 (buddy
= mb_find_buddy(e4b
, order
, &max
))) {
1218 if (block
+ 1 >= max
)
1221 next
= (block
+ 1) * (1 << order
);
1222 if (mb_test_bit(next
, EXT4_MB_BITMAP(e4b
)))
1225 ord
= mb_find_order_for_block(e4b
, next
);
1228 block
= next
>> order
;
1229 ex
->fe_len
+= 1 << order
;
1232 BUG_ON(ex
->fe_start
+ ex
->fe_len
> (1 << (e4b
->bd_blkbits
+ 3)));
1236 static int mb_mark_used(struct ext4_buddy
*e4b
, struct ext4_free_extent
*ex
)
1242 int start
= ex
->fe_start
;
1243 int len
= ex
->fe_len
;
1248 BUG_ON(start
+ len
> (e4b
->bd_sb
->s_blocksize
<< 3));
1249 BUG_ON(e4b
->bd_group
!= ex
->fe_group
);
1250 BUG_ON(!ext4_is_group_locked(e4b
->bd_sb
, e4b
->bd_group
));
1251 mb_check_buddy(e4b
);
1252 mb_mark_used_double(e4b
, start
, len
);
1254 e4b
->bd_info
->bb_free
-= len
;
1255 if (e4b
->bd_info
->bb_first_free
== start
)
1256 e4b
->bd_info
->bb_first_free
+= len
;
1258 /* let's maintain fragments counter */
1260 mlen
= !mb_test_bit(start
- 1, EXT4_MB_BITMAP(e4b
));
1261 if (start
+ len
< EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[0])
1262 max
= !mb_test_bit(start
+ len
, EXT4_MB_BITMAP(e4b
));
1264 e4b
->bd_info
->bb_fragments
++;
1265 else if (!mlen
&& !max
)
1266 e4b
->bd_info
->bb_fragments
--;
1268 /* let's maintain buddy itself */
1270 ord
= mb_find_order_for_block(e4b
, start
);
1272 if (((start
>> ord
) << ord
) == start
&& len
>= (1 << ord
)) {
1273 /* the whole chunk may be allocated at once! */
1275 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1276 BUG_ON((start
>> ord
) >= max
);
1277 mb_set_bit(start
>> ord
, buddy
);
1278 e4b
->bd_info
->bb_counters
[ord
]--;
1285 /* store for history */
1287 ret
= len
| (ord
<< 16);
1289 /* we have to split large buddy */
1291 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1292 mb_set_bit(start
>> ord
, buddy
);
1293 e4b
->bd_info
->bb_counters
[ord
]--;
1296 cur
= (start
>> ord
) & ~1U;
1297 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1298 mb_clear_bit(cur
, buddy
);
1299 mb_clear_bit(cur
+ 1, buddy
);
1300 e4b
->bd_info
->bb_counters
[ord
]++;
1301 e4b
->bd_info
->bb_counters
[ord
]++;
1304 mb_set_bits(sb_bgl_lock(EXT4_SB(e4b
->bd_sb
), ex
->fe_group
),
1305 EXT4_MB_BITMAP(e4b
), ex
->fe_start
, len0
);
1306 mb_check_buddy(e4b
);
1312 * Must be called under group lock!
1314 static void ext4_mb_use_best_found(struct ext4_allocation_context
*ac
,
1315 struct ext4_buddy
*e4b
)
1317 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1320 BUG_ON(ac
->ac_b_ex
.fe_group
!= e4b
->bd_group
);
1321 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1323 ac
->ac_b_ex
.fe_len
= min(ac
->ac_b_ex
.fe_len
, ac
->ac_g_ex
.fe_len
);
1324 ac
->ac_b_ex
.fe_logical
= ac
->ac_g_ex
.fe_logical
;
1325 ret
= mb_mark_used(e4b
, &ac
->ac_b_ex
);
1327 /* preallocation can change ac_b_ex, thus we store actually
1328 * allocated blocks for history */
1329 ac
->ac_f_ex
= ac
->ac_b_ex
;
1331 ac
->ac_status
= AC_STATUS_FOUND
;
1332 ac
->ac_tail
= ret
& 0xffff;
1333 ac
->ac_buddy
= ret
>> 16;
1336 * take the page reference. We want the page to be pinned
1337 * so that we don't get a ext4_mb_init_cache_call for this
1338 * group until we update the bitmap. That would mean we
1339 * double allocate blocks. The reference is dropped
1340 * in ext4_mb_release_context
1342 ac
->ac_bitmap_page
= e4b
->bd_bitmap_page
;
1343 get_page(ac
->ac_bitmap_page
);
1344 ac
->ac_buddy_page
= e4b
->bd_buddy_page
;
1345 get_page(ac
->ac_buddy_page
);
1347 /* store last allocated for subsequent stream allocation */
1348 if ((ac
->ac_flags
& EXT4_MB_HINT_DATA
)) {
1349 spin_lock(&sbi
->s_md_lock
);
1350 sbi
->s_mb_last_group
= ac
->ac_f_ex
.fe_group
;
1351 sbi
->s_mb_last_start
= ac
->ac_f_ex
.fe_start
;
1352 spin_unlock(&sbi
->s_md_lock
);
1357 * regular allocator, for general purposes allocation
1360 static void ext4_mb_check_limits(struct ext4_allocation_context
*ac
,
1361 struct ext4_buddy
*e4b
,
1364 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1365 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1366 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1367 struct ext4_free_extent ex
;
1370 if (ac
->ac_status
== AC_STATUS_FOUND
)
1373 * We don't want to scan for a whole year
1375 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
&&
1376 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1377 ac
->ac_status
= AC_STATUS_BREAK
;
1382 * Haven't found good chunk so far, let's continue
1384 if (bex
->fe_len
< gex
->fe_len
)
1387 if ((finish_group
|| ac
->ac_found
> sbi
->s_mb_min_to_scan
)
1388 && bex
->fe_group
== e4b
->bd_group
) {
1389 /* recheck chunk's availability - we don't know
1390 * when it was found (within this lock-unlock
1392 max
= mb_find_extent(e4b
, 0, bex
->fe_start
, gex
->fe_len
, &ex
);
1393 if (max
>= gex
->fe_len
) {
1394 ext4_mb_use_best_found(ac
, e4b
);
1401 * The routine checks whether found extent is good enough. If it is,
1402 * then the extent gets marked used and flag is set to the context
1403 * to stop scanning. Otherwise, the extent is compared with the
1404 * previous found extent and if new one is better, then it's stored
1405 * in the context. Later, the best found extent will be used, if
1406 * mballoc can't find good enough extent.
1408 * FIXME: real allocation policy is to be designed yet!
1410 static void ext4_mb_measure_extent(struct ext4_allocation_context
*ac
,
1411 struct ext4_free_extent
*ex
,
1412 struct ext4_buddy
*e4b
)
1414 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1415 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1417 BUG_ON(ex
->fe_len
<= 0);
1418 BUG_ON(ex
->fe_len
>= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
1419 BUG_ON(ex
->fe_start
>= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
1420 BUG_ON(ac
->ac_status
!= AC_STATUS_CONTINUE
);
1425 * The special case - take what you catch first
1427 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1429 ext4_mb_use_best_found(ac
, e4b
);
1434 * Let's check whether the chuck is good enough
1436 if (ex
->fe_len
== gex
->fe_len
) {
1438 ext4_mb_use_best_found(ac
, e4b
);
1443 * If this is first found extent, just store it in the context
1445 if (bex
->fe_len
== 0) {
1451 * If new found extent is better, store it in the context
1453 if (bex
->fe_len
< gex
->fe_len
) {
1454 /* if the request isn't satisfied, any found extent
1455 * larger than previous best one is better */
1456 if (ex
->fe_len
> bex
->fe_len
)
1458 } else if (ex
->fe_len
> gex
->fe_len
) {
1459 /* if the request is satisfied, then we try to find
1460 * an extent that still satisfy the request, but is
1461 * smaller than previous one */
1462 if (ex
->fe_len
< bex
->fe_len
)
1466 ext4_mb_check_limits(ac
, e4b
, 0);
1469 static int ext4_mb_try_best_found(struct ext4_allocation_context
*ac
,
1470 struct ext4_buddy
*e4b
)
1472 struct ext4_free_extent ex
= ac
->ac_b_ex
;
1473 ext4_group_t group
= ex
.fe_group
;
1477 BUG_ON(ex
.fe_len
<= 0);
1478 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1482 ext4_lock_group(ac
->ac_sb
, group
);
1483 max
= mb_find_extent(e4b
, 0, ex
.fe_start
, ex
.fe_len
, &ex
);
1487 ext4_mb_use_best_found(ac
, e4b
);
1490 ext4_unlock_group(ac
->ac_sb
, group
);
1491 ext4_mb_release_desc(e4b
);
1496 static int ext4_mb_find_by_goal(struct ext4_allocation_context
*ac
,
1497 struct ext4_buddy
*e4b
)
1499 ext4_group_t group
= ac
->ac_g_ex
.fe_group
;
1502 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1503 struct ext4_super_block
*es
= sbi
->s_es
;
1504 struct ext4_free_extent ex
;
1506 if (!(ac
->ac_flags
& EXT4_MB_HINT_TRY_GOAL
))
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, ac
->ac_g_ex
.fe_start
,
1515 ac
->ac_g_ex
.fe_len
, &ex
);
1517 if (max
>= ac
->ac_g_ex
.fe_len
&& ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
) {
1520 start
= (e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
)) +
1521 ex
.fe_start
+ le32_to_cpu(es
->s_first_data_block
);
1522 /* use do_div to get remainder (would be 64-bit modulo) */
1523 if (do_div(start
, sbi
->s_stripe
) == 0) {
1526 ext4_mb_use_best_found(ac
, e4b
);
1528 } else if (max
>= ac
->ac_g_ex
.fe_len
) {
1529 BUG_ON(ex
.fe_len
<= 0);
1530 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1531 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1534 ext4_mb_use_best_found(ac
, e4b
);
1535 } else if (max
> 0 && (ac
->ac_flags
& EXT4_MB_HINT_MERGE
)) {
1536 /* Sometimes, caller may want to merge even small
1537 * number of blocks to an existing extent */
1538 BUG_ON(ex
.fe_len
<= 0);
1539 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1540 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1543 ext4_mb_use_best_found(ac
, e4b
);
1545 ext4_unlock_group(ac
->ac_sb
, group
);
1546 ext4_mb_release_desc(e4b
);
1552 * The routine scans buddy structures (not bitmap!) from given order
1553 * to max order and tries to find big enough chunk to satisfy the req
1555 static void ext4_mb_simple_scan_group(struct ext4_allocation_context
*ac
,
1556 struct ext4_buddy
*e4b
)
1558 struct super_block
*sb
= ac
->ac_sb
;
1559 struct ext4_group_info
*grp
= e4b
->bd_info
;
1565 BUG_ON(ac
->ac_2order
<= 0);
1566 for (i
= ac
->ac_2order
; i
<= sb
->s_blocksize_bits
+ 1; i
++) {
1567 if (grp
->bb_counters
[i
] == 0)
1570 buddy
= mb_find_buddy(e4b
, i
, &max
);
1571 BUG_ON(buddy
== NULL
);
1573 k
= mb_find_next_zero_bit(buddy
, max
, 0);
1578 ac
->ac_b_ex
.fe_len
= 1 << i
;
1579 ac
->ac_b_ex
.fe_start
= k
<< i
;
1580 ac
->ac_b_ex
.fe_group
= e4b
->bd_group
;
1582 ext4_mb_use_best_found(ac
, e4b
);
1584 BUG_ON(ac
->ac_b_ex
.fe_len
!= ac
->ac_g_ex
.fe_len
);
1586 if (EXT4_SB(sb
)->s_mb_stats
)
1587 atomic_inc(&EXT4_SB(sb
)->s_bal_2orders
);
1594 * The routine scans the group and measures all found extents.
1595 * In order to optimize scanning, caller must pass number of
1596 * free blocks in the group, so the routine can know upper limit.
1598 static void ext4_mb_complex_scan_group(struct ext4_allocation_context
*ac
,
1599 struct ext4_buddy
*e4b
)
1601 struct super_block
*sb
= ac
->ac_sb
;
1602 void *bitmap
= EXT4_MB_BITMAP(e4b
);
1603 struct ext4_free_extent ex
;
1607 free
= e4b
->bd_info
->bb_free
;
1610 i
= e4b
->bd_info
->bb_first_free
;
1612 while (free
&& ac
->ac_status
== AC_STATUS_CONTINUE
) {
1613 i
= mb_find_next_zero_bit(bitmap
,
1614 EXT4_BLOCKS_PER_GROUP(sb
), i
);
1615 if (i
>= EXT4_BLOCKS_PER_GROUP(sb
)) {
1617 * IF we have corrupt bitmap, we won't find any
1618 * free blocks even though group info says we
1619 * we have free blocks
1621 ext4_error(sb
, __func__
, "%d free blocks as per "
1622 "group info. But bitmap says 0",
1627 mb_find_extent(e4b
, 0, i
, ac
->ac_g_ex
.fe_len
, &ex
);
1628 BUG_ON(ex
.fe_len
<= 0);
1629 if (free
< ex
.fe_len
) {
1630 ext4_error(sb
, __func__
, "%d free blocks as per "
1631 "group info. But got %d blocks",
1634 * The number of free blocks differs. This mostly
1635 * indicate that the bitmap is corrupt. So exit
1636 * without claiming the space.
1641 ext4_mb_measure_extent(ac
, &ex
, e4b
);
1647 ext4_mb_check_limits(ac
, e4b
, 1);
1651 * This is a special case for storages like raid5
1652 * we try to find stripe-aligned chunks for stripe-size requests
1653 * XXX should do so at least for multiples of stripe size as well
1655 static void ext4_mb_scan_aligned(struct ext4_allocation_context
*ac
,
1656 struct ext4_buddy
*e4b
)
1658 struct super_block
*sb
= ac
->ac_sb
;
1659 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1660 void *bitmap
= EXT4_MB_BITMAP(e4b
);
1661 struct ext4_free_extent ex
;
1662 ext4_fsblk_t first_group_block
;
1667 BUG_ON(sbi
->s_stripe
== 0);
1669 /* find first stripe-aligned block in group */
1670 first_group_block
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
)
1671 + le32_to_cpu(sbi
->s_es
->s_first_data_block
);
1672 a
= first_group_block
+ sbi
->s_stripe
- 1;
1673 do_div(a
, sbi
->s_stripe
);
1674 i
= (a
* sbi
->s_stripe
) - first_group_block
;
1676 while (i
< EXT4_BLOCKS_PER_GROUP(sb
)) {
1677 if (!mb_test_bit(i
, bitmap
)) {
1678 max
= mb_find_extent(e4b
, 0, i
, sbi
->s_stripe
, &ex
);
1679 if (max
>= sbi
->s_stripe
) {
1682 ext4_mb_use_best_found(ac
, e4b
);
1690 static int ext4_mb_good_group(struct ext4_allocation_context
*ac
,
1691 ext4_group_t group
, int cr
)
1693 unsigned free
, fragments
;
1695 struct ext4_group_desc
*desc
;
1696 struct ext4_group_info
*grp
= ext4_get_group_info(ac
->ac_sb
, group
);
1698 BUG_ON(cr
< 0 || cr
>= 4);
1699 BUG_ON(EXT4_MB_GRP_NEED_INIT(grp
));
1701 free
= grp
->bb_free
;
1702 fragments
= grp
->bb_fragments
;
1710 BUG_ON(ac
->ac_2order
== 0);
1711 /* If this group is uninitialized, skip it initially */
1712 desc
= ext4_get_group_desc(ac
->ac_sb
, group
, NULL
);
1713 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
))
1716 bits
= ac
->ac_sb
->s_blocksize_bits
+ 1;
1717 for (i
= ac
->ac_2order
; i
<= bits
; i
++)
1718 if (grp
->bb_counters
[i
] > 0)
1722 if ((free
/ fragments
) >= ac
->ac_g_ex
.fe_len
)
1726 if (free
>= ac
->ac_g_ex
.fe_len
)
1739 * lock the group_info alloc_sem of all the groups
1740 * belonging to the same buddy cache page. This
1741 * make sure other parallel operation on the buddy
1742 * cache doesn't happen whild holding the buddy cache
1745 int ext4_mb_get_buddy_cache_lock(struct super_block
*sb
, ext4_group_t group
)
1749 int blocks_per_page
;
1750 int groups_per_page
;
1751 ext4_group_t first_group
;
1752 struct ext4_group_info
*grp
;
1754 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1756 * the buddy cache inode stores the block bitmap
1757 * and buddy information in consecutive blocks.
1758 * So for each group we need two blocks.
1761 pnum
= block
/ blocks_per_page
;
1762 first_group
= pnum
* blocks_per_page
/ 2;
1764 groups_per_page
= blocks_per_page
>> 1;
1765 if (groups_per_page
== 0)
1766 groups_per_page
= 1;
1767 /* read all groups the page covers into the cache */
1768 for (i
= 0; i
< groups_per_page
; i
++) {
1770 if ((first_group
+ i
) >= EXT4_SB(sb
)->s_groups_count
)
1772 grp
= ext4_get_group_info(sb
, first_group
+ i
);
1773 /* take all groups write allocation
1774 * semaphore. This make sure there is
1775 * no block allocation going on in any
1778 down_write(&grp
->alloc_sem
);
1783 void ext4_mb_put_buddy_cache_lock(struct super_block
*sb
,
1784 ext4_group_t group
, int locked_group
)
1788 int blocks_per_page
;
1789 ext4_group_t first_group
;
1790 struct ext4_group_info
*grp
;
1792 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1794 * the buddy cache inode stores the block bitmap
1795 * and buddy information in consecutive blocks.
1796 * So for each group we need two blocks.
1799 pnum
= block
/ blocks_per_page
;
1800 first_group
= pnum
* blocks_per_page
/ 2;
1801 /* release locks on all the groups */
1802 for (i
= 0; i
< locked_group
; i
++) {
1804 grp
= ext4_get_group_info(sb
, first_group
+ i
);
1805 /* take all groups write allocation
1806 * semaphore. This make sure there is
1807 * no block allocation going on in any
1810 up_write(&grp
->alloc_sem
);
1815 static int ext4_mb_init_group(struct super_block
*sb
, ext4_group_t group
)
1820 int blocks_per_page
;
1821 int block
, pnum
, poff
;
1822 int num_grp_locked
= 0;
1823 struct ext4_group_info
*this_grp
;
1824 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1825 struct inode
*inode
= sbi
->s_buddy_cache
;
1826 struct page
*page
= NULL
, *bitmap_page
= NULL
;
1828 mb_debug("init group %lu\n", group
);
1829 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1830 this_grp
= ext4_get_group_info(sb
, group
);
1832 * This ensures we don't add group
1833 * to this buddy cache via resize
1835 num_grp_locked
= ext4_mb_get_buddy_cache_lock(sb
, group
);
1836 if (!EXT4_MB_GRP_NEED_INIT(this_grp
)) {
1838 * somebody initialized the group
1839 * return without doing anything
1845 * the buddy cache inode stores the block bitmap
1846 * and buddy information in consecutive blocks.
1847 * So for each group we need two blocks.
1850 pnum
= block
/ blocks_per_page
;
1851 poff
= block
% blocks_per_page
;
1852 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1854 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1855 ret
= ext4_mb_init_cache(page
, NULL
);
1862 if (page
== NULL
|| !PageUptodate(page
)) {
1866 mark_page_accessed(page
);
1868 bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
1870 /* init buddy cache */
1872 pnum
= block
/ blocks_per_page
;
1873 poff
= block
% blocks_per_page
;
1874 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1875 if (page
== bitmap_page
) {
1877 * If both the bitmap and buddy are in
1878 * the same page we don't need to force
1883 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1884 ret
= ext4_mb_init_cache(page
, bitmap
);
1891 if (page
== NULL
|| !PageUptodate(page
)) {
1895 mark_page_accessed(page
);
1897 ext4_mb_put_buddy_cache_lock(sb
, group
, num_grp_locked
);
1899 page_cache_release(bitmap_page
);
1901 page_cache_release(page
);
1905 static noinline_for_stack
int
1906 ext4_mb_regular_allocator(struct ext4_allocation_context
*ac
)
1913 struct ext4_sb_info
*sbi
;
1914 struct super_block
*sb
;
1915 struct ext4_buddy e4b
;
1920 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1922 /* first, try the goal */
1923 err
= ext4_mb_find_by_goal(ac
, &e4b
);
1924 if (err
|| ac
->ac_status
== AC_STATUS_FOUND
)
1927 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
1931 * ac->ac2_order is set only if the fe_len is a power of 2
1932 * if ac2_order is set we also set criteria to 0 so that we
1933 * try exact allocation using buddy.
1935 i
= fls(ac
->ac_g_ex
.fe_len
);
1938 * We search using buddy data only if the order of the request
1939 * is greater than equal to the sbi_s_mb_order2_reqs
1940 * You can tune it via /proc/fs/ext4/<partition>/order2_req
1942 if (i
>= sbi
->s_mb_order2_reqs
) {
1944 * This should tell if fe_len is exactly power of 2
1946 if ((ac
->ac_g_ex
.fe_len
& (~(1 << (i
- 1)))) == 0)
1947 ac
->ac_2order
= i
- 1;
1950 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
1951 /* if stream allocation is enabled, use global goal */
1952 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
1953 isize
= i_size_read(ac
->ac_inode
) >> bsbits
;
1957 if (size
< sbi
->s_mb_stream_request
&&
1958 (ac
->ac_flags
& EXT4_MB_HINT_DATA
)) {
1959 /* TBD: may be hot point */
1960 spin_lock(&sbi
->s_md_lock
);
1961 ac
->ac_g_ex
.fe_group
= sbi
->s_mb_last_group
;
1962 ac
->ac_g_ex
.fe_start
= sbi
->s_mb_last_start
;
1963 spin_unlock(&sbi
->s_md_lock
);
1965 /* Let's just scan groups to find more-less suitable blocks */
1966 cr
= ac
->ac_2order
? 0 : 1;
1968 * cr == 0 try to get exact allocation,
1969 * cr == 3 try to get anything
1972 for (; cr
< 4 && ac
->ac_status
== AC_STATUS_CONTINUE
; cr
++) {
1973 ac
->ac_criteria
= cr
;
1975 * searching for the right group start
1976 * from the goal value specified
1978 group
= ac
->ac_g_ex
.fe_group
;
1980 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; group
++, i
++) {
1981 struct ext4_group_info
*grp
;
1982 struct ext4_group_desc
*desc
;
1984 if (group
== EXT4_SB(sb
)->s_groups_count
)
1987 /* quick check to skip empty groups */
1988 grp
= ext4_get_group_info(sb
, group
);
1989 if (grp
->bb_free
== 0)
1993 * if the group is already init we check whether it is
1994 * a good group and if not we don't load the buddy
1996 if (EXT4_MB_GRP_NEED_INIT(grp
)) {
1998 * we need full data about the group
1999 * to make a good selection
2001 err
= ext4_mb_init_group(sb
, group
);
2007 * If the particular group doesn't satisfy our
2008 * criteria we continue with the next group
2010 if (!ext4_mb_good_group(ac
, group
, cr
))
2013 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2017 ext4_lock_group(sb
, group
);
2018 if (!ext4_mb_good_group(ac
, group
, cr
)) {
2019 /* someone did allocation from this group */
2020 ext4_unlock_group(sb
, group
);
2021 ext4_mb_release_desc(&e4b
);
2025 ac
->ac_groups_scanned
++;
2026 desc
= ext4_get_group_desc(sb
, group
, NULL
);
2027 if (cr
== 0 || (desc
->bg_flags
&
2028 cpu_to_le16(EXT4_BG_BLOCK_UNINIT
) &&
2029 ac
->ac_2order
!= 0))
2030 ext4_mb_simple_scan_group(ac
, &e4b
);
2032 ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
)
2033 ext4_mb_scan_aligned(ac
, &e4b
);
2035 ext4_mb_complex_scan_group(ac
, &e4b
);
2037 ext4_unlock_group(sb
, group
);
2038 ext4_mb_release_desc(&e4b
);
2040 if (ac
->ac_status
!= AC_STATUS_CONTINUE
)
2045 if (ac
->ac_b_ex
.fe_len
> 0 && ac
->ac_status
!= AC_STATUS_FOUND
&&
2046 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
2048 * We've been searching too long. Let's try to allocate
2049 * the best chunk we've found so far
2052 ext4_mb_try_best_found(ac
, &e4b
);
2053 if (ac
->ac_status
!= AC_STATUS_FOUND
) {
2055 * Someone more lucky has already allocated it.
2056 * The only thing we can do is just take first
2058 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2060 ac
->ac_b_ex
.fe_group
= 0;
2061 ac
->ac_b_ex
.fe_start
= 0;
2062 ac
->ac_b_ex
.fe_len
= 0;
2063 ac
->ac_status
= AC_STATUS_CONTINUE
;
2064 ac
->ac_flags
|= EXT4_MB_HINT_FIRST
;
2066 atomic_inc(&sbi
->s_mb_lost_chunks
);
2074 #ifdef EXT4_MB_HISTORY
2075 struct ext4_mb_proc_session
{
2076 struct ext4_mb_history
*history
;
2077 struct super_block
*sb
;
2082 static void *ext4_mb_history_skip_empty(struct ext4_mb_proc_session
*s
,
2083 struct ext4_mb_history
*hs
,
2086 if (hs
== s
->history
+ s
->max
)
2088 if (!first
&& hs
== s
->history
+ s
->start
)
2090 while (hs
->orig
.fe_len
== 0) {
2092 if (hs
== s
->history
+ s
->max
)
2094 if (hs
== s
->history
+ s
->start
)
2100 static void *ext4_mb_seq_history_start(struct seq_file
*seq
, loff_t
*pos
)
2102 struct ext4_mb_proc_session
*s
= seq
->private;
2103 struct ext4_mb_history
*hs
;
2107 return SEQ_START_TOKEN
;
2108 hs
= ext4_mb_history_skip_empty(s
, s
->history
+ s
->start
, 1);
2111 while (--l
&& (hs
= ext4_mb_history_skip_empty(s
, ++hs
, 0)) != NULL
);
2115 static void *ext4_mb_seq_history_next(struct seq_file
*seq
, void *v
,
2118 struct ext4_mb_proc_session
*s
= seq
->private;
2119 struct ext4_mb_history
*hs
= v
;
2122 if (v
== SEQ_START_TOKEN
)
2123 return ext4_mb_history_skip_empty(s
, s
->history
+ s
->start
, 1);
2125 return ext4_mb_history_skip_empty(s
, ++hs
, 0);
2128 static int ext4_mb_seq_history_show(struct seq_file
*seq
, void *v
)
2130 char buf
[25], buf2
[25], buf3
[25], *fmt
;
2131 struct ext4_mb_history
*hs
= v
;
2133 if (v
== SEQ_START_TOKEN
) {
2134 seq_printf(seq
, "%-5s %-8s %-23s %-23s %-23s %-5s "
2135 "%-5s %-2s %-5s %-5s %-5s %-6s\n",
2136 "pid", "inode", "original", "goal", "result", "found",
2137 "grps", "cr", "flags", "merge", "tail", "broken");
2141 if (hs
->op
== EXT4_MB_HISTORY_ALLOC
) {
2142 fmt
= "%-5u %-8u %-23s %-23s %-23s %-5u %-5u %-2u "
2143 "%-5u %-5s %-5u %-6u\n";
2144 sprintf(buf2
, "%u/%d/%u@%u", hs
->result
.fe_group
,
2145 hs
->result
.fe_start
, hs
->result
.fe_len
,
2146 hs
->result
.fe_logical
);
2147 sprintf(buf
, "%u/%d/%u@%u", hs
->orig
.fe_group
,
2148 hs
->orig
.fe_start
, hs
->orig
.fe_len
,
2149 hs
->orig
.fe_logical
);
2150 sprintf(buf3
, "%u/%d/%u@%u", hs
->goal
.fe_group
,
2151 hs
->goal
.fe_start
, hs
->goal
.fe_len
,
2152 hs
->goal
.fe_logical
);
2153 seq_printf(seq
, fmt
, hs
->pid
, hs
->ino
, buf
, buf3
, buf2
,
2154 hs
->found
, hs
->groups
, hs
->cr
, hs
->flags
,
2155 hs
->merged
? "M" : "", hs
->tail
,
2156 hs
->buddy
? 1 << hs
->buddy
: 0);
2157 } else if (hs
->op
== EXT4_MB_HISTORY_PREALLOC
) {
2158 fmt
= "%-5u %-8u %-23s %-23s %-23s\n";
2159 sprintf(buf2
, "%u/%d/%u@%u", hs
->result
.fe_group
,
2160 hs
->result
.fe_start
, hs
->result
.fe_len
,
2161 hs
->result
.fe_logical
);
2162 sprintf(buf
, "%u/%d/%u@%u", hs
->orig
.fe_group
,
2163 hs
->orig
.fe_start
, hs
->orig
.fe_len
,
2164 hs
->orig
.fe_logical
);
2165 seq_printf(seq
, fmt
, hs
->pid
, hs
->ino
, buf
, "", buf2
);
2166 } else if (hs
->op
== EXT4_MB_HISTORY_DISCARD
) {
2167 sprintf(buf2
, "%u/%d/%u", hs
->result
.fe_group
,
2168 hs
->result
.fe_start
, hs
->result
.fe_len
);
2169 seq_printf(seq
, "%-5u %-8u %-23s discard\n",
2170 hs
->pid
, hs
->ino
, buf2
);
2171 } else if (hs
->op
== EXT4_MB_HISTORY_FREE
) {
2172 sprintf(buf2
, "%u/%d/%u", hs
->result
.fe_group
,
2173 hs
->result
.fe_start
, hs
->result
.fe_len
);
2174 seq_printf(seq
, "%-5u %-8u %-23s free\n",
2175 hs
->pid
, hs
->ino
, buf2
);
2180 static void ext4_mb_seq_history_stop(struct seq_file
*seq
, void *v
)
2184 static struct seq_operations ext4_mb_seq_history_ops
= {
2185 .start
= ext4_mb_seq_history_start
,
2186 .next
= ext4_mb_seq_history_next
,
2187 .stop
= ext4_mb_seq_history_stop
,
2188 .show
= ext4_mb_seq_history_show
,
2191 static int ext4_mb_seq_history_open(struct inode
*inode
, struct file
*file
)
2193 struct super_block
*sb
= PDE(inode
)->data
;
2194 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2195 struct ext4_mb_proc_session
*s
;
2199 if (unlikely(sbi
->s_mb_history
== NULL
))
2201 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
2205 size
= sizeof(struct ext4_mb_history
) * sbi
->s_mb_history_max
;
2206 s
->history
= kmalloc(size
, GFP_KERNEL
);
2207 if (s
->history
== NULL
) {
2212 spin_lock(&sbi
->s_mb_history_lock
);
2213 memcpy(s
->history
, sbi
->s_mb_history
, size
);
2214 s
->max
= sbi
->s_mb_history_max
;
2215 s
->start
= sbi
->s_mb_history_cur
% s
->max
;
2216 spin_unlock(&sbi
->s_mb_history_lock
);
2218 rc
= seq_open(file
, &ext4_mb_seq_history_ops
);
2220 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2230 static int ext4_mb_seq_history_release(struct inode
*inode
, struct file
*file
)
2232 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
2233 struct ext4_mb_proc_session
*s
= seq
->private;
2236 return seq_release(inode
, file
);
2239 static ssize_t
ext4_mb_seq_history_write(struct file
*file
,
2240 const char __user
*buffer
,
2241 size_t count
, loff_t
*ppos
)
2243 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
2244 struct ext4_mb_proc_session
*s
= seq
->private;
2245 struct super_block
*sb
= s
->sb
;
2249 if (count
>= sizeof(str
)) {
2250 printk(KERN_ERR
"EXT4-fs: %s string too long, max %u bytes\n",
2251 "mb_history", (int)sizeof(str
));
2255 if (copy_from_user(str
, buffer
, count
))
2258 value
= simple_strtol(str
, NULL
, 0);
2261 EXT4_SB(sb
)->s_mb_history_filter
= value
;
2266 static struct file_operations ext4_mb_seq_history_fops
= {
2267 .owner
= THIS_MODULE
,
2268 .open
= ext4_mb_seq_history_open
,
2270 .write
= ext4_mb_seq_history_write
,
2271 .llseek
= seq_lseek
,
2272 .release
= ext4_mb_seq_history_release
,
2275 static void *ext4_mb_seq_groups_start(struct seq_file
*seq
, loff_t
*pos
)
2277 struct super_block
*sb
= seq
->private;
2278 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2281 if (*pos
< 0 || *pos
>= sbi
->s_groups_count
)
2285 return (void *) ((unsigned long) group
);
2288 static void *ext4_mb_seq_groups_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2290 struct super_block
*sb
= seq
->private;
2291 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2295 if (*pos
< 0 || *pos
>= sbi
->s_groups_count
)
2298 return (void *) ((unsigned long) group
);
2301 static int ext4_mb_seq_groups_show(struct seq_file
*seq
, void *v
)
2303 struct super_block
*sb
= seq
->private;
2304 ext4_group_t group
= (ext4_group_t
) ((unsigned long) v
);
2307 struct ext4_buddy e4b
;
2309 struct ext4_group_info info
;
2310 unsigned short counters
[16];
2315 seq_printf(seq
, "#%-5s: %-5s %-5s %-5s "
2316 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2317 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2318 "group", "free", "frags", "first",
2319 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2320 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2322 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(sg
.info
.bb_counters
[0]) +
2323 sizeof(struct ext4_group_info
);
2324 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2326 seq_printf(seq
, "#%-5u: I/O error\n", group
);
2329 ext4_lock_group(sb
, group
);
2330 memcpy(&sg
, ext4_get_group_info(sb
, group
), i
);
2331 ext4_unlock_group(sb
, group
);
2332 ext4_mb_release_desc(&e4b
);
2334 seq_printf(seq
, "#%-5u: %-5u %-5u %-5u [", group
, sg
.info
.bb_free
,
2335 sg
.info
.bb_fragments
, sg
.info
.bb_first_free
);
2336 for (i
= 0; i
<= 13; i
++)
2337 seq_printf(seq
, " %-5u", i
<= sb
->s_blocksize_bits
+ 1 ?
2338 sg
.info
.bb_counters
[i
] : 0);
2339 seq_printf(seq
, " ]\n");
2344 static void ext4_mb_seq_groups_stop(struct seq_file
*seq
, void *v
)
2348 static struct seq_operations ext4_mb_seq_groups_ops
= {
2349 .start
= ext4_mb_seq_groups_start
,
2350 .next
= ext4_mb_seq_groups_next
,
2351 .stop
= ext4_mb_seq_groups_stop
,
2352 .show
= ext4_mb_seq_groups_show
,
2355 static int ext4_mb_seq_groups_open(struct inode
*inode
, struct file
*file
)
2357 struct super_block
*sb
= PDE(inode
)->data
;
2360 rc
= seq_open(file
, &ext4_mb_seq_groups_ops
);
2362 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2369 static struct file_operations ext4_mb_seq_groups_fops
= {
2370 .owner
= THIS_MODULE
,
2371 .open
= ext4_mb_seq_groups_open
,
2373 .llseek
= seq_lseek
,
2374 .release
= seq_release
,
2377 static void ext4_mb_history_release(struct super_block
*sb
)
2379 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2381 if (sbi
->s_proc
!= NULL
) {
2382 remove_proc_entry("mb_groups", sbi
->s_proc
);
2383 remove_proc_entry("mb_history", sbi
->s_proc
);
2385 kfree(sbi
->s_mb_history
);
2388 static void ext4_mb_history_init(struct super_block
*sb
)
2390 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2393 if (sbi
->s_proc
!= NULL
) {
2394 proc_create_data("mb_history", S_IRUGO
, sbi
->s_proc
,
2395 &ext4_mb_seq_history_fops
, sb
);
2396 proc_create_data("mb_groups", S_IRUGO
, sbi
->s_proc
,
2397 &ext4_mb_seq_groups_fops
, sb
);
2400 sbi
->s_mb_history_max
= 1000;
2401 sbi
->s_mb_history_cur
= 0;
2402 spin_lock_init(&sbi
->s_mb_history_lock
);
2403 i
= sbi
->s_mb_history_max
* sizeof(struct ext4_mb_history
);
2404 sbi
->s_mb_history
= kzalloc(i
, GFP_KERNEL
);
2405 /* if we can't allocate history, then we simple won't use it */
2408 static noinline_for_stack
void
2409 ext4_mb_store_history(struct ext4_allocation_context
*ac
)
2411 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
2412 struct ext4_mb_history h
;
2414 if (unlikely(sbi
->s_mb_history
== NULL
))
2417 if (!(ac
->ac_op
& sbi
->s_mb_history_filter
))
2421 h
.pid
= current
->pid
;
2422 h
.ino
= ac
->ac_inode
? ac
->ac_inode
->i_ino
: 0;
2423 h
.orig
= ac
->ac_o_ex
;
2424 h
.result
= ac
->ac_b_ex
;
2425 h
.flags
= ac
->ac_flags
;
2426 h
.found
= ac
->ac_found
;
2427 h
.groups
= ac
->ac_groups_scanned
;
2428 h
.cr
= ac
->ac_criteria
;
2429 h
.tail
= ac
->ac_tail
;
2430 h
.buddy
= ac
->ac_buddy
;
2432 if (ac
->ac_op
== EXT4_MB_HISTORY_ALLOC
) {
2433 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
2434 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
2436 h
.goal
= ac
->ac_g_ex
;
2437 h
.result
= ac
->ac_f_ex
;
2440 spin_lock(&sbi
->s_mb_history_lock
);
2441 memcpy(sbi
->s_mb_history
+ sbi
->s_mb_history_cur
, &h
, sizeof(h
));
2442 if (++sbi
->s_mb_history_cur
>= sbi
->s_mb_history_max
)
2443 sbi
->s_mb_history_cur
= 0;
2444 spin_unlock(&sbi
->s_mb_history_lock
);
2448 #define ext4_mb_history_release(sb)
2449 #define ext4_mb_history_init(sb)
2453 /* Create and initialize ext4_group_info data for the given group. */
2454 int ext4_mb_add_groupinfo(struct super_block
*sb
, ext4_group_t group
,
2455 struct ext4_group_desc
*desc
)
2459 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2460 struct ext4_group_info
**meta_group_info
;
2463 * First check if this group is the first of a reserved block.
2464 * If it's true, we have to allocate a new table of pointers
2465 * to ext4_group_info structures
2467 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0) {
2468 metalen
= sizeof(*meta_group_info
) <<
2469 EXT4_DESC_PER_BLOCK_BITS(sb
);
2470 meta_group_info
= kmalloc(metalen
, GFP_KERNEL
);
2471 if (meta_group_info
== NULL
) {
2472 printk(KERN_ERR
"EXT4-fs: can't allocate mem for a "
2474 goto exit_meta_group_info
;
2476 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)] =
2481 * calculate needed size. if change bb_counters size,
2482 * don't forget about ext4_mb_generate_buddy()
2484 len
= offsetof(typeof(**meta_group_info
),
2485 bb_counters
[sb
->s_blocksize_bits
+ 2]);
2488 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)];
2489 i
= group
& (EXT4_DESC_PER_BLOCK(sb
) - 1);
2491 meta_group_info
[i
] = kzalloc(len
, GFP_KERNEL
);
2492 if (meta_group_info
[i
] == NULL
) {
2493 printk(KERN_ERR
"EXT4-fs: can't allocate buddy mem\n");
2494 goto exit_group_info
;
2496 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
,
2497 &(meta_group_info
[i
]->bb_state
));
2500 * initialize bb_free to be able to skip
2501 * empty groups without initialization
2503 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2504 meta_group_info
[i
]->bb_free
=
2505 ext4_free_blocks_after_init(sb
, group
, desc
);
2507 meta_group_info
[i
]->bb_free
=
2508 le16_to_cpu(desc
->bg_free_blocks_count
);
2511 INIT_LIST_HEAD(&meta_group_info
[i
]->bb_prealloc_list
);
2512 init_rwsem(&meta_group_info
[i
]->alloc_sem
);
2513 meta_group_info
[i
]->bb_free_root
.rb_node
= NULL
;;
2517 struct buffer_head
*bh
;
2518 meta_group_info
[i
]->bb_bitmap
=
2519 kmalloc(sb
->s_blocksize
, GFP_KERNEL
);
2520 BUG_ON(meta_group_info
[i
]->bb_bitmap
== NULL
);
2521 bh
= ext4_read_block_bitmap(sb
, group
);
2523 memcpy(meta_group_info
[i
]->bb_bitmap
, bh
->b_data
,
2532 /* If a meta_group_info table has been allocated, release it now */
2533 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0)
2534 kfree(sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)]);
2535 exit_meta_group_info
:
2537 } /* ext4_mb_add_groupinfo */
2540 * Update an existing group.
2541 * This function is used for online resize
2543 void ext4_mb_update_group_info(struct ext4_group_info
*grp
, ext4_grpblk_t add
)
2545 grp
->bb_free
+= add
;
2548 static int ext4_mb_init_backend(struct super_block
*sb
)
2552 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2553 struct ext4_super_block
*es
= sbi
->s_es
;
2554 int num_meta_group_infos
;
2555 int num_meta_group_infos_max
;
2557 struct ext4_group_info
**meta_group_info
;
2558 struct ext4_group_desc
*desc
;
2560 /* This is the number of blocks used by GDT */
2561 num_meta_group_infos
= (sbi
->s_groups_count
+ EXT4_DESC_PER_BLOCK(sb
) -
2562 1) >> EXT4_DESC_PER_BLOCK_BITS(sb
);
2565 * This is the total number of blocks used by GDT including
2566 * the number of reserved blocks for GDT.
2567 * The s_group_info array is allocated with this value
2568 * to allow a clean online resize without a complex
2569 * manipulation of pointer.
2570 * The drawback is the unused memory when no resize
2571 * occurs but it's very low in terms of pages
2572 * (see comments below)
2573 * Need to handle this properly when META_BG resizing is allowed
2575 num_meta_group_infos_max
= num_meta_group_infos
+
2576 le16_to_cpu(es
->s_reserved_gdt_blocks
);
2579 * array_size is the size of s_group_info array. We round it
2580 * to the next power of two because this approximation is done
2581 * internally by kmalloc so we can have some more memory
2582 * for free here (e.g. may be used for META_BG resize).
2585 while (array_size
< sizeof(*sbi
->s_group_info
) *
2586 num_meta_group_infos_max
)
2587 array_size
= array_size
<< 1;
2588 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2589 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2590 * So a two level scheme suffices for now. */
2591 sbi
->s_group_info
= kmalloc(array_size
, GFP_KERNEL
);
2592 if (sbi
->s_group_info
== NULL
) {
2593 printk(KERN_ERR
"EXT4-fs: can't allocate buddy meta group\n");
2596 sbi
->s_buddy_cache
= new_inode(sb
);
2597 if (sbi
->s_buddy_cache
== NULL
) {
2598 printk(KERN_ERR
"EXT4-fs: can't get new inode\n");
2601 EXT4_I(sbi
->s_buddy_cache
)->i_disksize
= 0;
2603 metalen
= sizeof(*meta_group_info
) << EXT4_DESC_PER_BLOCK_BITS(sb
);
2604 for (i
= 0; i
< num_meta_group_infos
; i
++) {
2605 if ((i
+ 1) == num_meta_group_infos
)
2606 metalen
= sizeof(*meta_group_info
) *
2607 (sbi
->s_groups_count
-
2608 (i
<< EXT4_DESC_PER_BLOCK_BITS(sb
)));
2609 meta_group_info
= kmalloc(metalen
, GFP_KERNEL
);
2610 if (meta_group_info
== NULL
) {
2611 printk(KERN_ERR
"EXT4-fs: can't allocate mem for a "
2615 sbi
->s_group_info
[i
] = meta_group_info
;
2618 for (i
= 0; i
< sbi
->s_groups_count
; i
++) {
2619 desc
= ext4_get_group_desc(sb
, i
, NULL
);
2622 "EXT4-fs: can't read descriptor %u\n", i
);
2625 if (ext4_mb_add_groupinfo(sb
, i
, desc
) != 0)
2633 kfree(ext4_get_group_info(sb
, i
));
2634 i
= num_meta_group_infos
;
2637 kfree(sbi
->s_group_info
[i
]);
2638 iput(sbi
->s_buddy_cache
);
2640 kfree(sbi
->s_group_info
);
2644 int ext4_mb_init(struct super_block
*sb
, int needs_recovery
)
2646 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2652 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(unsigned short);
2654 sbi
->s_mb_offsets
= kmalloc(i
, GFP_KERNEL
);
2655 if (sbi
->s_mb_offsets
== NULL
) {
2659 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(unsigned int);
2660 sbi
->s_mb_maxs
= kmalloc(i
, GFP_KERNEL
);
2661 if (sbi
->s_mb_maxs
== NULL
) {
2662 kfree(sbi
->s_mb_maxs
);
2666 /* order 0 is regular bitmap */
2667 sbi
->s_mb_maxs
[0] = sb
->s_blocksize
<< 3;
2668 sbi
->s_mb_offsets
[0] = 0;
2672 max
= sb
->s_blocksize
<< 2;
2674 sbi
->s_mb_offsets
[i
] = offset
;
2675 sbi
->s_mb_maxs
[i
] = max
;
2676 offset
+= 1 << (sb
->s_blocksize_bits
- i
);
2679 } while (i
<= sb
->s_blocksize_bits
+ 1);
2681 /* init file for buddy data */
2682 ret
= ext4_mb_init_backend(sb
);
2684 kfree(sbi
->s_mb_offsets
);
2685 kfree(sbi
->s_mb_maxs
);
2689 spin_lock_init(&sbi
->s_md_lock
);
2690 spin_lock_init(&sbi
->s_bal_lock
);
2692 sbi
->s_mb_max_to_scan
= MB_DEFAULT_MAX_TO_SCAN
;
2693 sbi
->s_mb_min_to_scan
= MB_DEFAULT_MIN_TO_SCAN
;
2694 sbi
->s_mb_stats
= MB_DEFAULT_STATS
;
2695 sbi
->s_mb_stream_request
= MB_DEFAULT_STREAM_THRESHOLD
;
2696 sbi
->s_mb_order2_reqs
= MB_DEFAULT_ORDER2_REQS
;
2697 sbi
->s_mb_history_filter
= EXT4_MB_HISTORY_DEFAULT
;
2698 sbi
->s_mb_group_prealloc
= MB_DEFAULT_GROUP_PREALLOC
;
2700 sbi
->s_locality_groups
= alloc_percpu(struct ext4_locality_group
);
2701 if (sbi
->s_locality_groups
== NULL
) {
2702 kfree(sbi
->s_mb_offsets
);
2703 kfree(sbi
->s_mb_maxs
);
2706 for_each_possible_cpu(i
) {
2707 struct ext4_locality_group
*lg
;
2708 lg
= per_cpu_ptr(sbi
->s_locality_groups
, i
);
2709 mutex_init(&lg
->lg_mutex
);
2710 for (j
= 0; j
< PREALLOC_TB_SIZE
; j
++)
2711 INIT_LIST_HEAD(&lg
->lg_prealloc_list
[j
]);
2712 spin_lock_init(&lg
->lg_prealloc_lock
);
2715 ext4_mb_init_per_dev_proc(sb
);
2716 ext4_mb_history_init(sb
);
2719 sbi
->s_journal
->j_commit_callback
= release_blocks_on_commit
;
2721 printk(KERN_INFO
"EXT4-fs: mballoc enabled\n");
2725 /* need to called with ext4 group lock (ext4_lock_group) */
2726 static void ext4_mb_cleanup_pa(struct ext4_group_info
*grp
)
2728 struct ext4_prealloc_space
*pa
;
2729 struct list_head
*cur
, *tmp
;
2732 list_for_each_safe(cur
, tmp
, &grp
->bb_prealloc_list
) {
2733 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
2734 list_del(&pa
->pa_group_list
);
2736 kmem_cache_free(ext4_pspace_cachep
, pa
);
2739 mb_debug("mballoc: %u PAs left\n", count
);
2743 int ext4_mb_release(struct super_block
*sb
)
2746 int num_meta_group_infos
;
2747 struct ext4_group_info
*grinfo
;
2748 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2750 if (sbi
->s_group_info
) {
2751 for (i
= 0; i
< sbi
->s_groups_count
; i
++) {
2752 grinfo
= ext4_get_group_info(sb
, i
);
2754 kfree(grinfo
->bb_bitmap
);
2756 ext4_lock_group(sb
, i
);
2757 ext4_mb_cleanup_pa(grinfo
);
2758 ext4_unlock_group(sb
, i
);
2761 num_meta_group_infos
= (sbi
->s_groups_count
+
2762 EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2763 EXT4_DESC_PER_BLOCK_BITS(sb
);
2764 for (i
= 0; i
< num_meta_group_infos
; i
++)
2765 kfree(sbi
->s_group_info
[i
]);
2766 kfree(sbi
->s_group_info
);
2768 kfree(sbi
->s_mb_offsets
);
2769 kfree(sbi
->s_mb_maxs
);
2770 if (sbi
->s_buddy_cache
)
2771 iput(sbi
->s_buddy_cache
);
2772 if (sbi
->s_mb_stats
) {
2774 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2775 atomic_read(&sbi
->s_bal_allocated
),
2776 atomic_read(&sbi
->s_bal_reqs
),
2777 atomic_read(&sbi
->s_bal_success
));
2779 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2780 "%u 2^N hits, %u breaks, %u lost\n",
2781 atomic_read(&sbi
->s_bal_ex_scanned
),
2782 atomic_read(&sbi
->s_bal_goals
),
2783 atomic_read(&sbi
->s_bal_2orders
),
2784 atomic_read(&sbi
->s_bal_breaks
),
2785 atomic_read(&sbi
->s_mb_lost_chunks
));
2787 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2788 sbi
->s_mb_buddies_generated
++,
2789 sbi
->s_mb_generation_time
);
2791 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2792 atomic_read(&sbi
->s_mb_preallocated
),
2793 atomic_read(&sbi
->s_mb_discarded
));
2796 free_percpu(sbi
->s_locality_groups
);
2797 ext4_mb_history_release(sb
);
2798 ext4_mb_destroy_per_dev_proc(sb
);
2804 * This function is called by the jbd2 layer once the commit has finished,
2805 * so we know we can free the blocks that were released with that commit.
2807 static void release_blocks_on_commit(journal_t
*journal
, transaction_t
*txn
)
2809 struct super_block
*sb
= journal
->j_private
;
2810 struct ext4_buddy e4b
;
2811 struct ext4_group_info
*db
;
2812 int err
, count
= 0, count2
= 0;
2813 struct ext4_free_data
*entry
;
2814 ext4_fsblk_t discard_block
;
2815 struct list_head
*l
, *ltmp
;
2817 list_for_each_safe(l
, ltmp
, &txn
->t_private_list
) {
2818 entry
= list_entry(l
, struct ext4_free_data
, list
);
2820 mb_debug("gonna free %u blocks in group %u (0x%p):",
2821 entry
->count
, entry
->group
, entry
);
2823 err
= ext4_mb_load_buddy(sb
, entry
->group
, &e4b
);
2824 /* we expect to find existing buddy because it's pinned */
2828 /* there are blocks to put in buddy to make them really free */
2829 count
+= entry
->count
;
2831 ext4_lock_group(sb
, entry
->group
);
2832 /* Take it out of per group rb tree */
2833 rb_erase(&entry
->node
, &(db
->bb_free_root
));
2834 mb_free_blocks(NULL
, &e4b
, entry
->start_blk
, entry
->count
);
2836 if (!db
->bb_free_root
.rb_node
) {
2837 /* No more items in the per group rb tree
2838 * balance refcounts from ext4_mb_free_metadata()
2840 page_cache_release(e4b
.bd_buddy_page
);
2841 page_cache_release(e4b
.bd_bitmap_page
);
2843 ext4_unlock_group(sb
, entry
->group
);
2844 discard_block
= (ext4_fsblk_t
) entry
->group
* EXT4_BLOCKS_PER_GROUP(sb
)
2846 + le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
2847 trace_mark(ext4_discard_blocks
, "dev %s blk %llu count %u", sb
->s_id
,
2848 (unsigned long long) discard_block
, entry
->count
);
2849 sb_issue_discard(sb
, discard_block
, entry
->count
);
2851 kmem_cache_free(ext4_free_ext_cachep
, entry
);
2852 ext4_mb_release_desc(&e4b
);
2855 mb_debug("freed %u blocks in %u structures\n", count
, count2
);
2858 #define EXT4_MB_STATS_NAME "stats"
2859 #define EXT4_MB_MAX_TO_SCAN_NAME "max_to_scan"
2860 #define EXT4_MB_MIN_TO_SCAN_NAME "min_to_scan"
2861 #define EXT4_MB_ORDER2_REQ "order2_req"
2862 #define EXT4_MB_STREAM_REQ "stream_req"
2863 #define EXT4_MB_GROUP_PREALLOC "group_prealloc"
2865 static int ext4_mb_init_per_dev_proc(struct super_block
*sb
)
2867 #ifdef CONFIG_PROC_FS
2868 mode_t mode
= S_IFREG
| S_IRUGO
| S_IWUSR
;
2869 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2870 struct proc_dir_entry
*proc
;
2872 if (sbi
->s_proc
== NULL
)
2875 EXT4_PROC_HANDLER(EXT4_MB_STATS_NAME
, mb_stats
);
2876 EXT4_PROC_HANDLER(EXT4_MB_MAX_TO_SCAN_NAME
, mb_max_to_scan
);
2877 EXT4_PROC_HANDLER(EXT4_MB_MIN_TO_SCAN_NAME
, mb_min_to_scan
);
2878 EXT4_PROC_HANDLER(EXT4_MB_ORDER2_REQ
, mb_order2_reqs
);
2879 EXT4_PROC_HANDLER(EXT4_MB_STREAM_REQ
, mb_stream_request
);
2880 EXT4_PROC_HANDLER(EXT4_MB_GROUP_PREALLOC
, mb_group_prealloc
);
2884 remove_proc_entry(EXT4_MB_GROUP_PREALLOC
, sbi
->s_proc
);
2885 remove_proc_entry(EXT4_MB_STREAM_REQ
, sbi
->s_proc
);
2886 remove_proc_entry(EXT4_MB_ORDER2_REQ
, sbi
->s_proc
);
2887 remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME
, sbi
->s_proc
);
2888 remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME
, sbi
->s_proc
);
2889 remove_proc_entry(EXT4_MB_STATS_NAME
, sbi
->s_proc
);
2896 static int ext4_mb_destroy_per_dev_proc(struct super_block
*sb
)
2898 #ifdef CONFIG_PROC_FS
2899 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2901 if (sbi
->s_proc
== NULL
)
2904 remove_proc_entry(EXT4_MB_GROUP_PREALLOC
, sbi
->s_proc
);
2905 remove_proc_entry(EXT4_MB_STREAM_REQ
, sbi
->s_proc
);
2906 remove_proc_entry(EXT4_MB_ORDER2_REQ
, sbi
->s_proc
);
2907 remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME
, sbi
->s_proc
);
2908 remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME
, sbi
->s_proc
);
2909 remove_proc_entry(EXT4_MB_STATS_NAME
, sbi
->s_proc
);
2914 int __init
init_ext4_mballoc(void)
2916 ext4_pspace_cachep
=
2917 kmem_cache_create("ext4_prealloc_space",
2918 sizeof(struct ext4_prealloc_space
),
2919 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2920 if (ext4_pspace_cachep
== NULL
)
2924 kmem_cache_create("ext4_alloc_context",
2925 sizeof(struct ext4_allocation_context
),
2926 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2927 if (ext4_ac_cachep
== NULL
) {
2928 kmem_cache_destroy(ext4_pspace_cachep
);
2932 ext4_free_ext_cachep
=
2933 kmem_cache_create("ext4_free_block_extents",
2934 sizeof(struct ext4_free_data
),
2935 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2936 if (ext4_free_ext_cachep
== NULL
) {
2937 kmem_cache_destroy(ext4_pspace_cachep
);
2938 kmem_cache_destroy(ext4_ac_cachep
);
2944 void exit_ext4_mballoc(void)
2946 /* XXX: synchronize_rcu(); */
2947 kmem_cache_destroy(ext4_pspace_cachep
);
2948 kmem_cache_destroy(ext4_ac_cachep
);
2949 kmem_cache_destroy(ext4_free_ext_cachep
);
2954 * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
2955 * Returns 0 if success or error code
2957 static noinline_for_stack
int
2958 ext4_mb_mark_diskspace_used(struct ext4_allocation_context
*ac
,
2959 handle_t
*handle
, unsigned int reserv_blks
)
2961 struct buffer_head
*bitmap_bh
= NULL
;
2962 struct ext4_super_block
*es
;
2963 struct ext4_group_desc
*gdp
;
2964 struct buffer_head
*gdp_bh
;
2965 struct ext4_sb_info
*sbi
;
2966 struct super_block
*sb
;
2970 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
2971 BUG_ON(ac
->ac_b_ex
.fe_len
<= 0);
2979 bitmap_bh
= ext4_read_block_bitmap(sb
, ac
->ac_b_ex
.fe_group
);
2983 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
2988 gdp
= ext4_get_group_desc(sb
, ac
->ac_b_ex
.fe_group
, &gdp_bh
);
2992 ext4_debug("using block group %u(%d)\n", ac
->ac_b_ex
.fe_group
,
2993 gdp
->bg_free_blocks_count
);
2995 err
= ext4_journal_get_write_access(handle
, gdp_bh
);
2999 block
= ac
->ac_b_ex
.fe_group
* EXT4_BLOCKS_PER_GROUP(sb
)
3000 + ac
->ac_b_ex
.fe_start
3001 + le32_to_cpu(es
->s_first_data_block
);
3003 len
= ac
->ac_b_ex
.fe_len
;
3004 if (in_range(ext4_block_bitmap(sb
, gdp
), block
, len
) ||
3005 in_range(ext4_inode_bitmap(sb
, gdp
), block
, len
) ||
3006 in_range(block
, ext4_inode_table(sb
, gdp
),
3007 EXT4_SB(sb
)->s_itb_per_group
) ||
3008 in_range(block
+ len
- 1, ext4_inode_table(sb
, gdp
),
3009 EXT4_SB(sb
)->s_itb_per_group
)) {
3010 ext4_error(sb
, __func__
,
3011 "Allocating block in system zone - block = %llu",
3013 /* File system mounted not to panic on error
3014 * Fix the bitmap and repeat the block allocation
3015 * We leak some of the blocks here.
3017 mb_set_bits(sb_bgl_lock(sbi
, ac
->ac_b_ex
.fe_group
),
3018 bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,
3019 ac
->ac_b_ex
.fe_len
);
3020 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
3025 #ifdef AGGRESSIVE_CHECK
3028 for (i
= 0; i
< ac
->ac_b_ex
.fe_len
; i
++) {
3029 BUG_ON(mb_test_bit(ac
->ac_b_ex
.fe_start
+ i
,
3030 bitmap_bh
->b_data
));
3034 mb_set_bits(sb_bgl_lock(sbi
, ac
->ac_b_ex
.fe_group
), bitmap_bh
->b_data
,
3035 ac
->ac_b_ex
.fe_start
, ac
->ac_b_ex
.fe_len
);
3037 spin_lock(sb_bgl_lock(sbi
, ac
->ac_b_ex
.fe_group
));
3038 if (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
3039 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT
);
3040 gdp
->bg_free_blocks_count
=
3041 cpu_to_le16(ext4_free_blocks_after_init(sb
,
3042 ac
->ac_b_ex
.fe_group
,
3045 le16_add_cpu(&gdp
->bg_free_blocks_count
, -ac
->ac_b_ex
.fe_len
);
3046 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, ac
->ac_b_ex
.fe_group
, gdp
);
3047 spin_unlock(sb_bgl_lock(sbi
, ac
->ac_b_ex
.fe_group
));
3048 percpu_counter_sub(&sbi
->s_freeblocks_counter
, ac
->ac_b_ex
.fe_len
);
3050 * Now reduce the dirty block count also. Should not go negative
3052 if (!(ac
->ac_flags
& EXT4_MB_DELALLOC_RESERVED
))
3053 /* release all the reserved blocks if non delalloc */
3054 percpu_counter_sub(&sbi
->s_dirtyblocks_counter
, reserv_blks
);
3056 percpu_counter_sub(&sbi
->s_dirtyblocks_counter
,
3057 ac
->ac_b_ex
.fe_len
);
3059 if (sbi
->s_log_groups_per_flex
) {
3060 ext4_group_t flex_group
= ext4_flex_group(sbi
,
3061 ac
->ac_b_ex
.fe_group
);
3062 spin_lock(sb_bgl_lock(sbi
, flex_group
));
3063 sbi
->s_flex_groups
[flex_group
].free_blocks
-= ac
->ac_b_ex
.fe_len
;
3064 spin_unlock(sb_bgl_lock(sbi
, flex_group
));
3067 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
3070 err
= ext4_handle_dirty_metadata(handle
, NULL
, gdp_bh
);
3079 * here we normalize request for locality group
3080 * Group request are normalized to s_strip size if we set the same via mount
3081 * option. If not we set it to s_mb_group_prealloc which can be configured via
3082 * /proc/fs/ext4/<partition>/group_prealloc
3084 * XXX: should we try to preallocate more than the group has now?
3086 static void ext4_mb_normalize_group_request(struct ext4_allocation_context
*ac
)
3088 struct super_block
*sb
= ac
->ac_sb
;
3089 struct ext4_locality_group
*lg
= ac
->ac_lg
;
3092 if (EXT4_SB(sb
)->s_stripe
)
3093 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_stripe
;
3095 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_mb_group_prealloc
;
3096 mb_debug("#%u: goal %u blocks for locality group\n",
3097 current
->pid
, ac
->ac_g_ex
.fe_len
);
3101 * Normalization means making request better in terms of
3102 * size and alignment
3104 static noinline_for_stack
void
3105 ext4_mb_normalize_request(struct ext4_allocation_context
*ac
,
3106 struct ext4_allocation_request
*ar
)
3110 loff_t size
, orig_size
, start_off
;
3111 ext4_lblk_t start
, orig_start
;
3112 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3113 struct ext4_prealloc_space
*pa
;
3115 /* do normalize only data requests, metadata requests
3116 do not need preallocation */
3117 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3120 /* sometime caller may want exact blocks */
3121 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
3124 /* caller may indicate that preallocation isn't
3125 * required (it's a tail, for example) */
3126 if (ac
->ac_flags
& EXT4_MB_HINT_NOPREALLOC
)
3129 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
) {
3130 ext4_mb_normalize_group_request(ac
);
3134 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
3136 /* first, let's learn actual file size
3137 * given current request is allocated */
3138 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
3139 size
= size
<< bsbits
;
3140 if (size
< i_size_read(ac
->ac_inode
))
3141 size
= i_size_read(ac
->ac_inode
);
3143 /* max size of free chunks */
3146 #define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3147 (req <= (size) || max <= (chunk_size))
3149 /* first, try to predict filesize */
3150 /* XXX: should this table be tunable? */
3152 if (size
<= 16 * 1024) {
3154 } else if (size
<= 32 * 1024) {
3156 } else if (size
<= 64 * 1024) {
3158 } else if (size
<= 128 * 1024) {
3160 } else if (size
<= 256 * 1024) {
3162 } else if (size
<= 512 * 1024) {
3164 } else if (size
<= 1024 * 1024) {
3166 } else if (NRL_CHECK_SIZE(size
, 4 * 1024 * 1024, max
, 2 * 1024)) {
3167 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3168 (21 - bsbits
)) << 21;
3169 size
= 2 * 1024 * 1024;
3170 } else if (NRL_CHECK_SIZE(size
, 8 * 1024 * 1024, max
, 4 * 1024)) {
3171 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3172 (22 - bsbits
)) << 22;
3173 size
= 4 * 1024 * 1024;
3174 } else if (NRL_CHECK_SIZE(ac
->ac_o_ex
.fe_len
,
3175 (8<<20)>>bsbits
, max
, 8 * 1024)) {
3176 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3177 (23 - bsbits
)) << 23;
3178 size
= 8 * 1024 * 1024;
3180 start_off
= (loff_t
)ac
->ac_o_ex
.fe_logical
<< bsbits
;
3181 size
= ac
->ac_o_ex
.fe_len
<< bsbits
;
3183 orig_size
= size
= size
>> bsbits
;
3184 orig_start
= start
= start_off
>> bsbits
;
3186 /* don't cover already allocated blocks in selected range */
3187 if (ar
->pleft
&& start
<= ar
->lleft
) {
3188 size
-= ar
->lleft
+ 1 - start
;
3189 start
= ar
->lleft
+ 1;
3191 if (ar
->pright
&& start
+ size
- 1 >= ar
->lright
)
3192 size
-= start
+ size
- ar
->lright
;
3196 /* check we don't cross already preallocated blocks */
3198 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3203 spin_lock(&pa
->pa_lock
);
3204 if (pa
->pa_deleted
) {
3205 spin_unlock(&pa
->pa_lock
);
3209 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
3211 /* PA must not overlap original request */
3212 BUG_ON(!(ac
->ac_o_ex
.fe_logical
>= pa_end
||
3213 ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
));
3215 /* skip PA normalized request doesn't overlap with */
3216 if (pa
->pa_lstart
>= end
) {
3217 spin_unlock(&pa
->pa_lock
);
3220 if (pa_end
<= start
) {
3221 spin_unlock(&pa
->pa_lock
);
3224 BUG_ON(pa
->pa_lstart
<= start
&& pa_end
>= end
);
3226 if (pa_end
<= ac
->ac_o_ex
.fe_logical
) {
3227 BUG_ON(pa_end
< start
);
3231 if (pa
->pa_lstart
> ac
->ac_o_ex
.fe_logical
) {
3232 BUG_ON(pa
->pa_lstart
> end
);
3233 end
= pa
->pa_lstart
;
3235 spin_unlock(&pa
->pa_lock
);
3240 /* XXX: extra loop to check we really don't overlap preallocations */
3242 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3244 spin_lock(&pa
->pa_lock
);
3245 if (pa
->pa_deleted
== 0) {
3246 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
3247 BUG_ON(!(start
>= pa_end
|| end
<= pa
->pa_lstart
));
3249 spin_unlock(&pa
->pa_lock
);
3253 if (start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3254 start
> ac
->ac_o_ex
.fe_logical
) {
3255 printk(KERN_ERR
"start %lu, size %lu, fe_logical %lu\n",
3256 (unsigned long) start
, (unsigned long) size
,
3257 (unsigned long) ac
->ac_o_ex
.fe_logical
);
3259 BUG_ON(start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3260 start
> ac
->ac_o_ex
.fe_logical
);
3261 BUG_ON(size
<= 0 || size
>= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
3263 /* now prepare goal request */
3265 /* XXX: is it better to align blocks WRT to logical
3266 * placement or satisfy big request as is */
3267 ac
->ac_g_ex
.fe_logical
= start
;
3268 ac
->ac_g_ex
.fe_len
= size
;
3270 /* define goal start in order to merge */
3271 if (ar
->pright
&& (ar
->lright
== (start
+ size
))) {
3272 /* merge to the right */
3273 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pright
- size
,
3274 &ac
->ac_f_ex
.fe_group
,
3275 &ac
->ac_f_ex
.fe_start
);
3276 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3278 if (ar
->pleft
&& (ar
->lleft
+ 1 == start
)) {
3279 /* merge to the left */
3280 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pleft
+ 1,
3281 &ac
->ac_f_ex
.fe_group
,
3282 &ac
->ac_f_ex
.fe_start
);
3283 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3286 mb_debug("goal: %u(was %u) blocks at %u\n", (unsigned) size
,
3287 (unsigned) orig_size
, (unsigned) start
);
3290 static void ext4_mb_collect_stats(struct ext4_allocation_context
*ac
)
3292 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3294 if (sbi
->s_mb_stats
&& ac
->ac_g_ex
.fe_len
> 1) {
3295 atomic_inc(&sbi
->s_bal_reqs
);
3296 atomic_add(ac
->ac_b_ex
.fe_len
, &sbi
->s_bal_allocated
);
3297 if (ac
->ac_o_ex
.fe_len
>= ac
->ac_g_ex
.fe_len
)
3298 atomic_inc(&sbi
->s_bal_success
);
3299 atomic_add(ac
->ac_found
, &sbi
->s_bal_ex_scanned
);
3300 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
3301 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
3302 atomic_inc(&sbi
->s_bal_goals
);
3303 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
)
3304 atomic_inc(&sbi
->s_bal_breaks
);
3307 ext4_mb_store_history(ac
);
3311 * use blocks preallocated to inode
3313 static void ext4_mb_use_inode_pa(struct ext4_allocation_context
*ac
,
3314 struct ext4_prealloc_space
*pa
)
3320 /* found preallocated blocks, use them */
3321 start
= pa
->pa_pstart
+ (ac
->ac_o_ex
.fe_logical
- pa
->pa_lstart
);
3322 end
= min(pa
->pa_pstart
+ pa
->pa_len
, start
+ ac
->ac_o_ex
.fe_len
);
3324 ext4_get_group_no_and_offset(ac
->ac_sb
, start
, &ac
->ac_b_ex
.fe_group
,
3325 &ac
->ac_b_ex
.fe_start
);
3326 ac
->ac_b_ex
.fe_len
= len
;
3327 ac
->ac_status
= AC_STATUS_FOUND
;
3330 BUG_ON(start
< pa
->pa_pstart
);
3331 BUG_ON(start
+ len
> pa
->pa_pstart
+ pa
->pa_len
);
3332 BUG_ON(pa
->pa_free
< len
);
3335 mb_debug("use %llu/%u from inode pa %p\n", start
, len
, pa
);
3339 * use blocks preallocated to locality group
3341 static void ext4_mb_use_group_pa(struct ext4_allocation_context
*ac
,
3342 struct ext4_prealloc_space
*pa
)
3344 unsigned int len
= ac
->ac_o_ex
.fe_len
;
3346 ext4_get_group_no_and_offset(ac
->ac_sb
, pa
->pa_pstart
,
3347 &ac
->ac_b_ex
.fe_group
,
3348 &ac
->ac_b_ex
.fe_start
);
3349 ac
->ac_b_ex
.fe_len
= len
;
3350 ac
->ac_status
= AC_STATUS_FOUND
;
3353 /* we don't correct pa_pstart or pa_plen here to avoid
3354 * possible race when the group is being loaded concurrently
3355 * instead we correct pa later, after blocks are marked
3356 * in on-disk bitmap -- see ext4_mb_release_context()
3357 * Other CPUs are prevented from allocating from this pa by lg_mutex
3359 mb_debug("use %u/%u from group pa %p\n", pa
->pa_lstart
-len
, len
, pa
);
3363 * Return the prealloc space that have minimal distance
3364 * from the goal block. @cpa is the prealloc
3365 * space that is having currently known minimal distance
3366 * from the goal block.
3368 static struct ext4_prealloc_space
*
3369 ext4_mb_check_group_pa(ext4_fsblk_t goal_block
,
3370 struct ext4_prealloc_space
*pa
,
3371 struct ext4_prealloc_space
*cpa
)
3373 ext4_fsblk_t cur_distance
, new_distance
;
3376 atomic_inc(&pa
->pa_count
);
3379 cur_distance
= abs(goal_block
- cpa
->pa_pstart
);
3380 new_distance
= abs(goal_block
- pa
->pa_pstart
);
3382 if (cur_distance
< new_distance
)
3385 /* drop the previous reference */
3386 atomic_dec(&cpa
->pa_count
);
3387 atomic_inc(&pa
->pa_count
);
3392 * search goal blocks in preallocated space
3394 static noinline_for_stack
int
3395 ext4_mb_use_preallocated(struct ext4_allocation_context
*ac
)
3398 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3399 struct ext4_locality_group
*lg
;
3400 struct ext4_prealloc_space
*pa
, *cpa
= NULL
;
3401 ext4_fsblk_t goal_block
;
3403 /* only data can be preallocated */
3404 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3407 /* first, try per-file preallocation */
3409 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3411 /* all fields in this condition don't change,
3412 * so we can skip locking for them */
3413 if (ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
||
3414 ac
->ac_o_ex
.fe_logical
>= pa
->pa_lstart
+ pa
->pa_len
)
3417 /* found preallocated blocks, use them */
3418 spin_lock(&pa
->pa_lock
);
3419 if (pa
->pa_deleted
== 0 && pa
->pa_free
) {
3420 atomic_inc(&pa
->pa_count
);
3421 ext4_mb_use_inode_pa(ac
, pa
);
3422 spin_unlock(&pa
->pa_lock
);
3423 ac
->ac_criteria
= 10;
3427 spin_unlock(&pa
->pa_lock
);
3431 /* can we use group allocation? */
3432 if (!(ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
))
3435 /* inode may have no locality group for some reason */
3439 order
= fls(ac
->ac_o_ex
.fe_len
) - 1;
3440 if (order
> PREALLOC_TB_SIZE
- 1)
3441 /* The max size of hash table is PREALLOC_TB_SIZE */
3442 order
= PREALLOC_TB_SIZE
- 1;
3444 goal_block
= ac
->ac_g_ex
.fe_group
* EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
) +
3445 ac
->ac_g_ex
.fe_start
+
3446 le32_to_cpu(EXT4_SB(ac
->ac_sb
)->s_es
->s_first_data_block
);
3448 * search for the prealloc space that is having
3449 * minimal distance from the goal block.
3451 for (i
= order
; i
< PREALLOC_TB_SIZE
; i
++) {
3453 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[i
],
3455 spin_lock(&pa
->pa_lock
);
3456 if (pa
->pa_deleted
== 0 &&
3457 pa
->pa_free
>= ac
->ac_o_ex
.fe_len
) {
3459 cpa
= ext4_mb_check_group_pa(goal_block
,
3462 spin_unlock(&pa
->pa_lock
);
3467 ext4_mb_use_group_pa(ac
, cpa
);
3468 ac
->ac_criteria
= 20;
3475 * the function goes through all preallocation in this group and marks them
3476 * used in in-core bitmap. buddy must be generated from this bitmap
3477 * Need to be called with ext4 group lock (ext4_lock_group)
3479 static void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
3482 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3483 struct ext4_prealloc_space
*pa
;
3484 struct list_head
*cur
;
3485 ext4_group_t groupnr
;
3486 ext4_grpblk_t start
;
3487 int preallocated
= 0;
3491 /* all form of preallocation discards first load group,
3492 * so the only competing code is preallocation use.
3493 * we don't need any locking here
3494 * notice we do NOT ignore preallocations with pa_deleted
3495 * otherwise we could leave used blocks available for
3496 * allocation in buddy when concurrent ext4_mb_put_pa()
3497 * is dropping preallocation
3499 list_for_each(cur
, &grp
->bb_prealloc_list
) {
3500 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
3501 spin_lock(&pa
->pa_lock
);
3502 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
3505 spin_unlock(&pa
->pa_lock
);
3506 if (unlikely(len
== 0))
3508 BUG_ON(groupnr
!= group
);
3509 mb_set_bits(sb_bgl_lock(EXT4_SB(sb
), group
),
3510 bitmap
, start
, len
);
3511 preallocated
+= len
;
3514 mb_debug("prellocated %u for group %u\n", preallocated
, group
);
3517 static void ext4_mb_pa_callback(struct rcu_head
*head
)
3519 struct ext4_prealloc_space
*pa
;
3520 pa
= container_of(head
, struct ext4_prealloc_space
, u
.pa_rcu
);
3521 kmem_cache_free(ext4_pspace_cachep
, pa
);
3525 * drops a reference to preallocated space descriptor
3526 * if this was the last reference and the space is consumed
3528 static void ext4_mb_put_pa(struct ext4_allocation_context
*ac
,
3529 struct super_block
*sb
, struct ext4_prealloc_space
*pa
)
3533 if (!atomic_dec_and_test(&pa
->pa_count
) || pa
->pa_free
!= 0)
3536 /* in this short window concurrent discard can set pa_deleted */
3537 spin_lock(&pa
->pa_lock
);
3538 if (pa
->pa_deleted
== 1) {
3539 spin_unlock(&pa
->pa_lock
);
3544 spin_unlock(&pa
->pa_lock
);
3546 /* -1 is to protect from crossing allocation group */
3547 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
- 1, &grp
, NULL
);
3552 * P1 (buddy init) P2 (regular allocation)
3553 * find block B in PA
3554 * copy on-disk bitmap to buddy
3555 * mark B in on-disk bitmap
3556 * drop PA from group
3557 * mark all PAs in buddy
3559 * thus, P1 initializes buddy with B available. to prevent this
3560 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3563 ext4_lock_group(sb
, grp
);
3564 list_del(&pa
->pa_group_list
);
3565 ext4_unlock_group(sb
, grp
);
3567 spin_lock(pa
->pa_obj_lock
);
3568 list_del_rcu(&pa
->pa_inode_list
);
3569 spin_unlock(pa
->pa_obj_lock
);
3571 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3575 * creates new preallocated space for given inode
3577 static noinline_for_stack
int
3578 ext4_mb_new_inode_pa(struct ext4_allocation_context
*ac
)
3580 struct super_block
*sb
= ac
->ac_sb
;
3581 struct ext4_prealloc_space
*pa
;
3582 struct ext4_group_info
*grp
;
3583 struct ext4_inode_info
*ei
;
3585 /* preallocate only when found space is larger then requested */
3586 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3587 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3588 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3590 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3594 if (ac
->ac_b_ex
.fe_len
< ac
->ac_g_ex
.fe_len
) {
3600 /* we can't allocate as much as normalizer wants.
3601 * so, found space must get proper lstart
3602 * to cover original request */
3603 BUG_ON(ac
->ac_g_ex
.fe_logical
> ac
->ac_o_ex
.fe_logical
);
3604 BUG_ON(ac
->ac_g_ex
.fe_len
< ac
->ac_o_ex
.fe_len
);
3606 /* we're limited by original request in that
3607 * logical block must be covered any way
3608 * winl is window we can move our chunk within */
3609 winl
= ac
->ac_o_ex
.fe_logical
- ac
->ac_g_ex
.fe_logical
;
3611 /* also, we should cover whole original request */
3612 wins
= ac
->ac_b_ex
.fe_len
- ac
->ac_o_ex
.fe_len
;
3614 /* the smallest one defines real window */
3615 win
= min(winl
, wins
);
3617 offs
= ac
->ac_o_ex
.fe_logical
% ac
->ac_b_ex
.fe_len
;
3618 if (offs
&& offs
< win
)
3621 ac
->ac_b_ex
.fe_logical
= ac
->ac_o_ex
.fe_logical
- win
;
3622 BUG_ON(ac
->ac_o_ex
.fe_logical
< ac
->ac_b_ex
.fe_logical
);
3623 BUG_ON(ac
->ac_o_ex
.fe_len
> ac
->ac_b_ex
.fe_len
);
3626 /* preallocation can change ac_b_ex, thus we store actually
3627 * allocated blocks for history */
3628 ac
->ac_f_ex
= ac
->ac_b_ex
;
3630 pa
->pa_lstart
= ac
->ac_b_ex
.fe_logical
;
3631 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3632 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3633 pa
->pa_free
= pa
->pa_len
;
3634 atomic_set(&pa
->pa_count
, 1);
3635 spin_lock_init(&pa
->pa_lock
);
3639 mb_debug("new inode pa %p: %llu/%u for %u\n", pa
,
3640 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3642 ext4_mb_use_inode_pa(ac
, pa
);
3643 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3645 ei
= EXT4_I(ac
->ac_inode
);
3646 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3648 pa
->pa_obj_lock
= &ei
->i_prealloc_lock
;
3649 pa
->pa_inode
= ac
->ac_inode
;
3651 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3652 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3653 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3655 spin_lock(pa
->pa_obj_lock
);
3656 list_add_rcu(&pa
->pa_inode_list
, &ei
->i_prealloc_list
);
3657 spin_unlock(pa
->pa_obj_lock
);
3663 * creates new preallocated space for locality group inodes belongs to
3665 static noinline_for_stack
int
3666 ext4_mb_new_group_pa(struct ext4_allocation_context
*ac
)
3668 struct super_block
*sb
= ac
->ac_sb
;
3669 struct ext4_locality_group
*lg
;
3670 struct ext4_prealloc_space
*pa
;
3671 struct ext4_group_info
*grp
;
3673 /* preallocate only when found space is larger then requested */
3674 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3675 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3676 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3678 BUG_ON(ext4_pspace_cachep
== NULL
);
3679 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3683 /* preallocation can change ac_b_ex, thus we store actually
3684 * allocated blocks for history */
3685 ac
->ac_f_ex
= ac
->ac_b_ex
;
3687 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3688 pa
->pa_lstart
= pa
->pa_pstart
;
3689 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3690 pa
->pa_free
= pa
->pa_len
;
3691 atomic_set(&pa
->pa_count
, 1);
3692 spin_lock_init(&pa
->pa_lock
);
3693 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3697 mb_debug("new group pa %p: %llu/%u for %u\n", pa
,
3698 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3700 ext4_mb_use_group_pa(ac
, pa
);
3701 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3703 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3707 pa
->pa_obj_lock
= &lg
->lg_prealloc_lock
;
3708 pa
->pa_inode
= NULL
;
3710 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3711 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3712 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3715 * We will later add the new pa to the right bucket
3716 * after updating the pa_free in ext4_mb_release_context
3721 static int ext4_mb_new_preallocation(struct ext4_allocation_context
*ac
)
3725 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
3726 err
= ext4_mb_new_group_pa(ac
);
3728 err
= ext4_mb_new_inode_pa(ac
);
3733 * finds all unused blocks in on-disk bitmap, frees them in
3734 * in-core bitmap and buddy.
3735 * @pa must be unlinked from inode and group lists, so that
3736 * nobody else can find/use it.
3737 * the caller MUST hold group/inode locks.
3738 * TODO: optimize the case when there are no in-core structures yet
3740 static noinline_for_stack
int
3741 ext4_mb_release_inode_pa(struct ext4_buddy
*e4b
, struct buffer_head
*bitmap_bh
,
3742 struct ext4_prealloc_space
*pa
,
3743 struct ext4_allocation_context
*ac
)
3745 struct super_block
*sb
= e4b
->bd_sb
;
3746 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3755 BUG_ON(pa
->pa_deleted
== 0);
3756 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3757 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3758 end
= bit
+ pa
->pa_len
;
3762 ac
->ac_inode
= pa
->pa_inode
;
3763 ac
->ac_op
= EXT4_MB_HISTORY_DISCARD
;
3767 bit
= mb_find_next_zero_bit(bitmap_bh
->b_data
, end
, bit
);
3770 next
= mb_find_next_bit(bitmap_bh
->b_data
, end
, bit
);
3771 start
= group
* EXT4_BLOCKS_PER_GROUP(sb
) + bit
+
3772 le32_to_cpu(sbi
->s_es
->s_first_data_block
);
3773 mb_debug(" free preallocated %u/%u in group %u\n",
3774 (unsigned) start
, (unsigned) next
- bit
,
3779 ac
->ac_b_ex
.fe_group
= group
;
3780 ac
->ac_b_ex
.fe_start
= bit
;
3781 ac
->ac_b_ex
.fe_len
= next
- bit
;
3782 ac
->ac_b_ex
.fe_logical
= 0;
3783 ext4_mb_store_history(ac
);
3786 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, next
- bit
);
3789 if (free
!= pa
->pa_free
) {
3790 printk(KERN_CRIT
"pa %p: logic %lu, phys. %lu, len %lu\n",
3791 pa
, (unsigned long) pa
->pa_lstart
,
3792 (unsigned long) pa
->pa_pstart
,
3793 (unsigned long) pa
->pa_len
);
3794 ext4_error(sb
, __func__
, "free %u, pa_free %u",
3797 * pa is already deleted so we use the value obtained
3798 * from the bitmap and continue.
3801 atomic_add(free
, &sbi
->s_mb_discarded
);
3806 static noinline_for_stack
int
3807 ext4_mb_release_group_pa(struct ext4_buddy
*e4b
,
3808 struct ext4_prealloc_space
*pa
,
3809 struct ext4_allocation_context
*ac
)
3811 struct super_block
*sb
= e4b
->bd_sb
;
3816 ac
->ac_op
= EXT4_MB_HISTORY_DISCARD
;
3818 BUG_ON(pa
->pa_deleted
== 0);
3819 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3820 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3821 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, pa
->pa_len
);
3822 atomic_add(pa
->pa_len
, &EXT4_SB(sb
)->s_mb_discarded
);
3826 ac
->ac_inode
= NULL
;
3827 ac
->ac_b_ex
.fe_group
= group
;
3828 ac
->ac_b_ex
.fe_start
= bit
;
3829 ac
->ac_b_ex
.fe_len
= pa
->pa_len
;
3830 ac
->ac_b_ex
.fe_logical
= 0;
3831 ext4_mb_store_history(ac
);
3838 * releases all preallocations in given group
3840 * first, we need to decide discard policy:
3841 * - when do we discard
3843 * - how many do we discard
3844 * 1) how many requested
3846 static noinline_for_stack
int
3847 ext4_mb_discard_group_preallocations(struct super_block
*sb
,
3848 ext4_group_t group
, int needed
)
3850 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3851 struct buffer_head
*bitmap_bh
= NULL
;
3852 struct ext4_prealloc_space
*pa
, *tmp
;
3853 struct ext4_allocation_context
*ac
;
3854 struct list_head list
;
3855 struct ext4_buddy e4b
;
3860 mb_debug("discard preallocation for group %u\n", group
);
3862 if (list_empty(&grp
->bb_prealloc_list
))
3865 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
3866 if (bitmap_bh
== NULL
) {
3867 ext4_error(sb
, __func__
, "Error in reading block "
3868 "bitmap for %u", group
);
3872 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
3874 ext4_error(sb
, __func__
, "Error in loading buddy "
3875 "information for %u", group
);
3881 needed
= EXT4_BLOCKS_PER_GROUP(sb
) + 1;
3883 INIT_LIST_HEAD(&list
);
3884 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
3886 ext4_lock_group(sb
, group
);
3887 list_for_each_entry_safe(pa
, tmp
,
3888 &grp
->bb_prealloc_list
, pa_group_list
) {
3889 spin_lock(&pa
->pa_lock
);
3890 if (atomic_read(&pa
->pa_count
)) {
3891 spin_unlock(&pa
->pa_lock
);
3895 if (pa
->pa_deleted
) {
3896 spin_unlock(&pa
->pa_lock
);
3900 /* seems this one can be freed ... */
3903 /* we can trust pa_free ... */
3904 free
+= pa
->pa_free
;
3906 spin_unlock(&pa
->pa_lock
);
3908 list_del(&pa
->pa_group_list
);
3909 list_add(&pa
->u
.pa_tmp_list
, &list
);
3912 /* if we still need more blocks and some PAs were used, try again */
3913 if (free
< needed
&& busy
) {
3915 ext4_unlock_group(sb
, group
);
3917 * Yield the CPU here so that we don't get soft lockup
3918 * in non preempt case.
3924 /* found anything to free? */
3925 if (list_empty(&list
)) {
3930 /* now free all selected PAs */
3931 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
3933 /* remove from object (inode or locality group) */
3934 spin_lock(pa
->pa_obj_lock
);
3935 list_del_rcu(&pa
->pa_inode_list
);
3936 spin_unlock(pa
->pa_obj_lock
);
3939 ext4_mb_release_group_pa(&e4b
, pa
, ac
);
3941 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
, ac
);
3943 list_del(&pa
->u
.pa_tmp_list
);
3944 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3948 ext4_unlock_group(sb
, group
);
3950 kmem_cache_free(ext4_ac_cachep
, ac
);
3951 ext4_mb_release_desc(&e4b
);
3957 * releases all non-used preallocated blocks for given inode
3959 * It's important to discard preallocations under i_data_sem
3960 * We don't want another block to be served from the prealloc
3961 * space when we are discarding the inode prealloc space.
3963 * FIXME!! Make sure it is valid at all the call sites
3965 void ext4_discard_preallocations(struct inode
*inode
)
3967 struct ext4_inode_info
*ei
= EXT4_I(inode
);
3968 struct super_block
*sb
= inode
->i_sb
;
3969 struct buffer_head
*bitmap_bh
= NULL
;
3970 struct ext4_prealloc_space
*pa
, *tmp
;
3971 struct ext4_allocation_context
*ac
;
3972 ext4_group_t group
= 0;
3973 struct list_head list
;
3974 struct ext4_buddy e4b
;
3977 if (!S_ISREG(inode
->i_mode
)) {
3978 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3982 mb_debug("discard preallocation for inode %lu\n", inode
->i_ino
);
3984 INIT_LIST_HEAD(&list
);
3986 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
3988 /* first, collect all pa's in the inode */
3989 spin_lock(&ei
->i_prealloc_lock
);
3990 while (!list_empty(&ei
->i_prealloc_list
)) {
3991 pa
= list_entry(ei
->i_prealloc_list
.next
,
3992 struct ext4_prealloc_space
, pa_inode_list
);
3993 BUG_ON(pa
->pa_obj_lock
!= &ei
->i_prealloc_lock
);
3994 spin_lock(&pa
->pa_lock
);
3995 if (atomic_read(&pa
->pa_count
)) {
3996 /* this shouldn't happen often - nobody should
3997 * use preallocation while we're discarding it */
3998 spin_unlock(&pa
->pa_lock
);
3999 spin_unlock(&ei
->i_prealloc_lock
);
4000 printk(KERN_ERR
"uh-oh! used pa while discarding\n");
4002 schedule_timeout_uninterruptible(HZ
);
4006 if (pa
->pa_deleted
== 0) {
4008 spin_unlock(&pa
->pa_lock
);
4009 list_del_rcu(&pa
->pa_inode_list
);
4010 list_add(&pa
->u
.pa_tmp_list
, &list
);
4014 /* someone is deleting pa right now */
4015 spin_unlock(&pa
->pa_lock
);
4016 spin_unlock(&ei
->i_prealloc_lock
);
4018 /* we have to wait here because pa_deleted
4019 * doesn't mean pa is already unlinked from
4020 * the list. as we might be called from
4021 * ->clear_inode() the inode will get freed
4022 * and concurrent thread which is unlinking
4023 * pa from inode's list may access already
4024 * freed memory, bad-bad-bad */
4026 /* XXX: if this happens too often, we can
4027 * add a flag to force wait only in case
4028 * of ->clear_inode(), but not in case of
4029 * regular truncate */
4030 schedule_timeout_uninterruptible(HZ
);
4033 spin_unlock(&ei
->i_prealloc_lock
);
4035 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
4036 BUG_ON(pa
->pa_linear
!= 0);
4037 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
4039 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
4041 ext4_error(sb
, __func__
, "Error in loading buddy "
4042 "information for %u", group
);
4046 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
4047 if (bitmap_bh
== NULL
) {
4048 ext4_error(sb
, __func__
, "Error in reading block "
4049 "bitmap for %u", group
);
4050 ext4_mb_release_desc(&e4b
);
4054 ext4_lock_group(sb
, group
);
4055 list_del(&pa
->pa_group_list
);
4056 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
, ac
);
4057 ext4_unlock_group(sb
, group
);
4059 ext4_mb_release_desc(&e4b
);
4062 list_del(&pa
->u
.pa_tmp_list
);
4063 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4066 kmem_cache_free(ext4_ac_cachep
, ac
);
4070 * finds all preallocated spaces and return blocks being freed to them
4071 * if preallocated space becomes full (no block is used from the space)
4072 * then the function frees space in buddy
4073 * XXX: at the moment, truncate (which is the only way to free blocks)
4074 * discards all preallocations
4076 static void ext4_mb_return_to_preallocation(struct inode
*inode
,
4077 struct ext4_buddy
*e4b
,
4078 sector_t block
, int count
)
4080 BUG_ON(!list_empty(&EXT4_I(inode
)->i_prealloc_list
));
4083 static void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
4085 struct super_block
*sb
= ac
->ac_sb
;
4088 printk(KERN_ERR
"EXT4-fs: Can't allocate:"
4089 " Allocation context details:\n");
4090 printk(KERN_ERR
"EXT4-fs: status %d flags %d\n",
4091 ac
->ac_status
, ac
->ac_flags
);
4092 printk(KERN_ERR
"EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
4093 "best %lu/%lu/%lu@%lu cr %d\n",
4094 (unsigned long)ac
->ac_o_ex
.fe_group
,
4095 (unsigned long)ac
->ac_o_ex
.fe_start
,
4096 (unsigned long)ac
->ac_o_ex
.fe_len
,
4097 (unsigned long)ac
->ac_o_ex
.fe_logical
,
4098 (unsigned long)ac
->ac_g_ex
.fe_group
,
4099 (unsigned long)ac
->ac_g_ex
.fe_start
,
4100 (unsigned long)ac
->ac_g_ex
.fe_len
,
4101 (unsigned long)ac
->ac_g_ex
.fe_logical
,
4102 (unsigned long)ac
->ac_b_ex
.fe_group
,
4103 (unsigned long)ac
->ac_b_ex
.fe_start
,
4104 (unsigned long)ac
->ac_b_ex
.fe_len
,
4105 (unsigned long)ac
->ac_b_ex
.fe_logical
,
4106 (int)ac
->ac_criteria
);
4107 printk(KERN_ERR
"EXT4-fs: %lu scanned, %d found\n", ac
->ac_ex_scanned
,
4109 printk(KERN_ERR
"EXT4-fs: groups: \n");
4110 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; i
++) {
4111 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, i
);
4112 struct ext4_prealloc_space
*pa
;
4113 ext4_grpblk_t start
;
4114 struct list_head
*cur
;
4115 ext4_lock_group(sb
, i
);
4116 list_for_each(cur
, &grp
->bb_prealloc_list
) {
4117 pa
= list_entry(cur
, struct ext4_prealloc_space
,
4119 spin_lock(&pa
->pa_lock
);
4120 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
4122 spin_unlock(&pa
->pa_lock
);
4123 printk(KERN_ERR
"PA:%lu:%d:%u \n", i
,
4126 ext4_unlock_group(sb
, i
);
4128 if (grp
->bb_free
== 0)
4130 printk(KERN_ERR
"%lu: %d/%d \n",
4131 i
, grp
->bb_free
, grp
->bb_fragments
);
4133 printk(KERN_ERR
"\n");
4136 static inline void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
4143 * We use locality group preallocation for small size file. The size of the
4144 * file is determined by the current size or the resulting size after
4145 * allocation which ever is larger
4147 * One can tune this size via /proc/fs/ext4/<partition>/stream_req
4149 static void ext4_mb_group_or_file(struct ext4_allocation_context
*ac
)
4151 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
4152 int bsbits
= ac
->ac_sb
->s_blocksize_bits
;
4155 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
4158 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
4159 isize
= i_size_read(ac
->ac_inode
) >> bsbits
;
4160 size
= max(size
, isize
);
4162 /* don't use group allocation for large files */
4163 if (size
>= sbi
->s_mb_stream_request
)
4166 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
4169 BUG_ON(ac
->ac_lg
!= NULL
);
4171 * locality group prealloc space are per cpu. The reason for having
4172 * per cpu locality group is to reduce the contention between block
4173 * request from multiple CPUs.
4175 ac
->ac_lg
= per_cpu_ptr(sbi
->s_locality_groups
, raw_smp_processor_id());
4177 /* we're going to use group allocation */
4178 ac
->ac_flags
|= EXT4_MB_HINT_GROUP_ALLOC
;
4180 /* serialize all allocations in the group */
4181 mutex_lock(&ac
->ac_lg
->lg_mutex
);
4184 static noinline_for_stack
int
4185 ext4_mb_initialize_context(struct ext4_allocation_context
*ac
,
4186 struct ext4_allocation_request
*ar
)
4188 struct super_block
*sb
= ar
->inode
->i_sb
;
4189 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4190 struct ext4_super_block
*es
= sbi
->s_es
;
4194 ext4_grpblk_t block
;
4196 /* we can't allocate > group size */
4199 /* just a dirty hack to filter too big requests */
4200 if (len
>= EXT4_BLOCKS_PER_GROUP(sb
) - 10)
4201 len
= EXT4_BLOCKS_PER_GROUP(sb
) - 10;
4203 /* start searching from the goal */
4205 if (goal
< le32_to_cpu(es
->s_first_data_block
) ||
4206 goal
>= ext4_blocks_count(es
))
4207 goal
= le32_to_cpu(es
->s_first_data_block
);
4208 ext4_get_group_no_and_offset(sb
, goal
, &group
, &block
);
4210 /* set up allocation goals */
4211 ac
->ac_b_ex
.fe_logical
= ar
->logical
;
4212 ac
->ac_b_ex
.fe_group
= 0;
4213 ac
->ac_b_ex
.fe_start
= 0;
4214 ac
->ac_b_ex
.fe_len
= 0;
4215 ac
->ac_status
= AC_STATUS_CONTINUE
;
4216 ac
->ac_groups_scanned
= 0;
4217 ac
->ac_ex_scanned
= 0;
4220 ac
->ac_inode
= ar
->inode
;
4221 ac
->ac_o_ex
.fe_logical
= ar
->logical
;
4222 ac
->ac_o_ex
.fe_group
= group
;
4223 ac
->ac_o_ex
.fe_start
= block
;
4224 ac
->ac_o_ex
.fe_len
= len
;
4225 ac
->ac_g_ex
.fe_logical
= ar
->logical
;
4226 ac
->ac_g_ex
.fe_group
= group
;
4227 ac
->ac_g_ex
.fe_start
= block
;
4228 ac
->ac_g_ex
.fe_len
= len
;
4229 ac
->ac_f_ex
.fe_len
= 0;
4230 ac
->ac_flags
= ar
->flags
;
4232 ac
->ac_criteria
= 0;
4234 ac
->ac_bitmap_page
= NULL
;
4235 ac
->ac_buddy_page
= NULL
;
4238 /* we have to define context: we'll we work with a file or
4239 * locality group. this is a policy, actually */
4240 ext4_mb_group_or_file(ac
);
4242 mb_debug("init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
4243 "left: %u/%u, right %u/%u to %swritable\n",
4244 (unsigned) ar
->len
, (unsigned) ar
->logical
,
4245 (unsigned) ar
->goal
, ac
->ac_flags
, ac
->ac_2order
,
4246 (unsigned) ar
->lleft
, (unsigned) ar
->pleft
,
4247 (unsigned) ar
->lright
, (unsigned) ar
->pright
,
4248 atomic_read(&ar
->inode
->i_writecount
) ? "" : "non-");
4253 static noinline_for_stack
void
4254 ext4_mb_discard_lg_preallocations(struct super_block
*sb
,
4255 struct ext4_locality_group
*lg
,
4256 int order
, int total_entries
)
4258 ext4_group_t group
= 0;
4259 struct ext4_buddy e4b
;
4260 struct list_head discard_list
;
4261 struct ext4_prealloc_space
*pa
, *tmp
;
4262 struct ext4_allocation_context
*ac
;
4264 mb_debug("discard locality group preallocation\n");
4266 INIT_LIST_HEAD(&discard_list
);
4267 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4269 spin_lock(&lg
->lg_prealloc_lock
);
4270 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[order
],
4272 spin_lock(&pa
->pa_lock
);
4273 if (atomic_read(&pa
->pa_count
)) {
4275 * This is the pa that we just used
4276 * for block allocation. So don't
4279 spin_unlock(&pa
->pa_lock
);
4282 if (pa
->pa_deleted
) {
4283 spin_unlock(&pa
->pa_lock
);
4286 /* only lg prealloc space */
4287 BUG_ON(!pa
->pa_linear
);
4289 /* seems this one can be freed ... */
4291 spin_unlock(&pa
->pa_lock
);
4293 list_del_rcu(&pa
->pa_inode_list
);
4294 list_add(&pa
->u
.pa_tmp_list
, &discard_list
);
4297 if (total_entries
<= 5) {
4299 * we want to keep only 5 entries
4300 * allowing it to grow to 8. This
4301 * mak sure we don't call discard
4302 * soon for this list.
4307 spin_unlock(&lg
->lg_prealloc_lock
);
4309 list_for_each_entry_safe(pa
, tmp
, &discard_list
, u
.pa_tmp_list
) {
4311 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
4312 if (ext4_mb_load_buddy(sb
, group
, &e4b
)) {
4313 ext4_error(sb
, __func__
, "Error in loading buddy "
4314 "information for %u", group
);
4317 ext4_lock_group(sb
, group
);
4318 list_del(&pa
->pa_group_list
);
4319 ext4_mb_release_group_pa(&e4b
, pa
, ac
);
4320 ext4_unlock_group(sb
, group
);
4322 ext4_mb_release_desc(&e4b
);
4323 list_del(&pa
->u
.pa_tmp_list
);
4324 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4327 kmem_cache_free(ext4_ac_cachep
, ac
);
4331 * We have incremented pa_count. So it cannot be freed at this
4332 * point. Also we hold lg_mutex. So no parallel allocation is
4333 * possible from this lg. That means pa_free cannot be updated.
4335 * A parallel ext4_mb_discard_group_preallocations is possible.
4336 * which can cause the lg_prealloc_list to be updated.
4339 static void ext4_mb_add_n_trim(struct ext4_allocation_context
*ac
)
4341 int order
, added
= 0, lg_prealloc_count
= 1;
4342 struct super_block
*sb
= ac
->ac_sb
;
4343 struct ext4_locality_group
*lg
= ac
->ac_lg
;
4344 struct ext4_prealloc_space
*tmp_pa
, *pa
= ac
->ac_pa
;
4346 order
= fls(pa
->pa_free
) - 1;
4347 if (order
> PREALLOC_TB_SIZE
- 1)
4348 /* The max size of hash table is PREALLOC_TB_SIZE */
4349 order
= PREALLOC_TB_SIZE
- 1;
4350 /* Add the prealloc space to lg */
4352 list_for_each_entry_rcu(tmp_pa
, &lg
->lg_prealloc_list
[order
],
4354 spin_lock(&tmp_pa
->pa_lock
);
4355 if (tmp_pa
->pa_deleted
) {
4356 spin_unlock(&pa
->pa_lock
);
4359 if (!added
&& pa
->pa_free
< tmp_pa
->pa_free
) {
4360 /* Add to the tail of the previous entry */
4361 list_add_tail_rcu(&pa
->pa_inode_list
,
4362 &tmp_pa
->pa_inode_list
);
4365 * we want to count the total
4366 * number of entries in the list
4369 spin_unlock(&tmp_pa
->pa_lock
);
4370 lg_prealloc_count
++;
4373 list_add_tail_rcu(&pa
->pa_inode_list
,
4374 &lg
->lg_prealloc_list
[order
]);
4377 /* Now trim the list to be not more than 8 elements */
4378 if (lg_prealloc_count
> 8) {
4379 ext4_mb_discard_lg_preallocations(sb
, lg
,
4380 order
, lg_prealloc_count
);
4387 * release all resource we used in allocation
4389 static int ext4_mb_release_context(struct ext4_allocation_context
*ac
)
4391 struct ext4_prealloc_space
*pa
= ac
->ac_pa
;
4393 if (pa
->pa_linear
) {
4394 /* see comment in ext4_mb_use_group_pa() */
4395 spin_lock(&pa
->pa_lock
);
4396 pa
->pa_pstart
+= ac
->ac_b_ex
.fe_len
;
4397 pa
->pa_lstart
+= ac
->ac_b_ex
.fe_len
;
4398 pa
->pa_free
-= ac
->ac_b_ex
.fe_len
;
4399 pa
->pa_len
-= ac
->ac_b_ex
.fe_len
;
4400 spin_unlock(&pa
->pa_lock
);
4402 * We want to add the pa to the right bucket.
4403 * Remove it from the list and while adding
4404 * make sure the list to which we are adding
4407 if (likely(pa
->pa_free
)) {
4408 spin_lock(pa
->pa_obj_lock
);
4409 list_del_rcu(&pa
->pa_inode_list
);
4410 spin_unlock(pa
->pa_obj_lock
);
4411 ext4_mb_add_n_trim(ac
);
4414 ext4_mb_put_pa(ac
, ac
->ac_sb
, pa
);
4416 if (ac
->ac_bitmap_page
)
4417 page_cache_release(ac
->ac_bitmap_page
);
4418 if (ac
->ac_buddy_page
)
4419 page_cache_release(ac
->ac_buddy_page
);
4420 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
4421 mutex_unlock(&ac
->ac_lg
->lg_mutex
);
4422 ext4_mb_collect_stats(ac
);
4426 static int ext4_mb_discard_preallocations(struct super_block
*sb
, int needed
)
4432 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
&& needed
> 0; i
++) {
4433 ret
= ext4_mb_discard_group_preallocations(sb
, i
, needed
);
4442 * Main entry point into mballoc to allocate blocks
4443 * it tries to use preallocation first, then falls back
4444 * to usual allocation
4446 ext4_fsblk_t
ext4_mb_new_blocks(handle_t
*handle
,
4447 struct ext4_allocation_request
*ar
, int *errp
)
4450 struct ext4_allocation_context
*ac
= NULL
;
4451 struct ext4_sb_info
*sbi
;
4452 struct super_block
*sb
;
4453 ext4_fsblk_t block
= 0;
4454 unsigned int inquota
;
4455 unsigned int reserv_blks
= 0;
4457 sb
= ar
->inode
->i_sb
;
4460 if (!EXT4_I(ar
->inode
)->i_delalloc_reserved_flag
) {
4462 * With delalloc we already reserved the blocks
4464 while (ar
->len
&& ext4_claim_free_blocks(sbi
, ar
->len
)) {
4465 /* let others to free the space */
4467 ar
->len
= ar
->len
>> 1;
4473 reserv_blks
= ar
->len
;
4475 while (ar
->len
&& DQUOT_ALLOC_BLOCK(ar
->inode
, ar
->len
)) {
4476 ar
->flags
|= EXT4_MB_HINT_NOPREALLOC
;
4485 if (EXT4_I(ar
->inode
)->i_delalloc_reserved_flag
)
4486 ar
->flags
|= EXT4_MB_DELALLOC_RESERVED
;
4488 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4495 *errp
= ext4_mb_initialize_context(ac
, ar
);
4501 ac
->ac_op
= EXT4_MB_HISTORY_PREALLOC
;
4502 if (!ext4_mb_use_preallocated(ac
)) {
4503 ac
->ac_op
= EXT4_MB_HISTORY_ALLOC
;
4504 ext4_mb_normalize_request(ac
, ar
);
4506 /* allocate space in core */
4507 ext4_mb_regular_allocator(ac
);
4509 /* as we've just preallocated more space than
4510 * user requested orinally, we store allocated
4511 * space in a special descriptor */
4512 if (ac
->ac_status
== AC_STATUS_FOUND
&&
4513 ac
->ac_o_ex
.fe_len
< ac
->ac_b_ex
.fe_len
)
4514 ext4_mb_new_preallocation(ac
);
4517 if (likely(ac
->ac_status
== AC_STATUS_FOUND
)) {
4518 *errp
= ext4_mb_mark_diskspace_used(ac
, handle
, reserv_blks
);
4519 if (*errp
== -EAGAIN
) {
4520 ac
->ac_b_ex
.fe_group
= 0;
4521 ac
->ac_b_ex
.fe_start
= 0;
4522 ac
->ac_b_ex
.fe_len
= 0;
4523 ac
->ac_status
= AC_STATUS_CONTINUE
;
4526 ac
->ac_b_ex
.fe_len
= 0;
4528 ext4_mb_show_ac(ac
);
4530 block
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
4531 ar
->len
= ac
->ac_b_ex
.fe_len
;
4534 freed
= ext4_mb_discard_preallocations(sb
, ac
->ac_o_ex
.fe_len
);
4538 ac
->ac_b_ex
.fe_len
= 0;
4540 ext4_mb_show_ac(ac
);
4543 ext4_mb_release_context(ac
);
4546 kmem_cache_free(ext4_ac_cachep
, ac
);
4548 if (ar
->len
< inquota
)
4549 DQUOT_FREE_BLOCK(ar
->inode
, inquota
- ar
->len
);
4555 * We can merge two free data extents only if the physical blocks
4556 * are contiguous, AND the extents were freed by the same transaction,
4557 * AND the blocks are associated with the same group.
4559 static int can_merge(struct ext4_free_data
*entry1
,
4560 struct ext4_free_data
*entry2
)
4562 if ((entry1
->t_tid
== entry2
->t_tid
) &&
4563 (entry1
->group
== entry2
->group
) &&
4564 ((entry1
->start_blk
+ entry1
->count
) == entry2
->start_blk
))
4569 static noinline_for_stack
int
4570 ext4_mb_free_metadata(handle_t
*handle
, struct ext4_buddy
*e4b
,
4571 ext4_group_t group
, ext4_grpblk_t block
, int count
)
4573 struct ext4_group_info
*db
= e4b
->bd_info
;
4574 struct super_block
*sb
= e4b
->bd_sb
;
4575 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4576 struct ext4_free_data
*entry
, *new_entry
;
4577 struct rb_node
**n
= &db
->bb_free_root
.rb_node
, *node
;
4578 struct rb_node
*parent
= NULL
, *new_node
;
4580 BUG_ON(!ext4_handle_valid(handle
));
4581 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
4582 BUG_ON(e4b
->bd_buddy_page
== NULL
);
4584 new_entry
= kmem_cache_alloc(ext4_free_ext_cachep
, GFP_NOFS
);
4585 new_entry
->start_blk
= block
;
4586 new_entry
->group
= group
;
4587 new_entry
->count
= count
;
4588 new_entry
->t_tid
= handle
->h_transaction
->t_tid
;
4589 new_node
= &new_entry
->node
;
4591 ext4_lock_group(sb
, group
);
4593 /* first free block exent. We need to
4594 protect buddy cache from being freed,
4595 * otherwise we'll refresh it from
4596 * on-disk bitmap and lose not-yet-available
4598 page_cache_get(e4b
->bd_buddy_page
);
4599 page_cache_get(e4b
->bd_bitmap_page
);
4603 entry
= rb_entry(parent
, struct ext4_free_data
, node
);
4604 if (block
< entry
->start_blk
)
4606 else if (block
>= (entry
->start_blk
+ entry
->count
))
4607 n
= &(*n
)->rb_right
;
4609 ext4_unlock_group(sb
, group
);
4610 ext4_error(sb
, __func__
,
4611 "Double free of blocks %d (%d %d)",
4612 block
, entry
->start_blk
, entry
->count
);
4617 rb_link_node(new_node
, parent
, n
);
4618 rb_insert_color(new_node
, &db
->bb_free_root
);
4620 /* Now try to see the extent can be merged to left and right */
4621 node
= rb_prev(new_node
);
4623 entry
= rb_entry(node
, struct ext4_free_data
, node
);
4624 if (can_merge(entry
, new_entry
)) {
4625 new_entry
->start_blk
= entry
->start_blk
;
4626 new_entry
->count
+= entry
->count
;
4627 rb_erase(node
, &(db
->bb_free_root
));
4628 spin_lock(&sbi
->s_md_lock
);
4629 list_del(&entry
->list
);
4630 spin_unlock(&sbi
->s_md_lock
);
4631 kmem_cache_free(ext4_free_ext_cachep
, entry
);
4635 node
= rb_next(new_node
);
4637 entry
= rb_entry(node
, struct ext4_free_data
, node
);
4638 if (can_merge(new_entry
, entry
)) {
4639 new_entry
->count
+= entry
->count
;
4640 rb_erase(node
, &(db
->bb_free_root
));
4641 spin_lock(&sbi
->s_md_lock
);
4642 list_del(&entry
->list
);
4643 spin_unlock(&sbi
->s_md_lock
);
4644 kmem_cache_free(ext4_free_ext_cachep
, entry
);
4647 /* Add the extent to transaction's private list */
4648 spin_lock(&sbi
->s_md_lock
);
4649 list_add(&new_entry
->list
, &handle
->h_transaction
->t_private_list
);
4650 spin_unlock(&sbi
->s_md_lock
);
4651 ext4_unlock_group(sb
, group
);
4656 * Main entry point into mballoc to free blocks
4658 void ext4_mb_free_blocks(handle_t
*handle
, struct inode
*inode
,
4659 unsigned long block
, unsigned long count
,
4660 int metadata
, unsigned long *freed
)
4662 struct buffer_head
*bitmap_bh
= NULL
;
4663 struct super_block
*sb
= inode
->i_sb
;
4664 struct ext4_allocation_context
*ac
= NULL
;
4665 struct ext4_group_desc
*gdp
;
4666 struct ext4_super_block
*es
;
4667 unsigned int overflow
;
4669 struct buffer_head
*gd_bh
;
4670 ext4_group_t block_group
;
4671 struct ext4_sb_info
*sbi
;
4672 struct ext4_buddy e4b
;
4679 es
= EXT4_SB(sb
)->s_es
;
4680 if (block
< le32_to_cpu(es
->s_first_data_block
) ||
4681 block
+ count
< block
||
4682 block
+ count
> ext4_blocks_count(es
)) {
4683 ext4_error(sb
, __func__
,
4684 "Freeing blocks not in datazone - "
4685 "block = %lu, count = %lu", block
, count
);
4689 ext4_debug("freeing block %lu\n", block
);
4691 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4693 ac
->ac_op
= EXT4_MB_HISTORY_FREE
;
4694 ac
->ac_inode
= inode
;
4700 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
4703 * Check to see if we are freeing blocks across a group
4706 if (bit
+ count
> EXT4_BLOCKS_PER_GROUP(sb
)) {
4707 overflow
= bit
+ count
- EXT4_BLOCKS_PER_GROUP(sb
);
4710 bitmap_bh
= ext4_read_block_bitmap(sb
, block_group
);
4715 gdp
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
4721 if (in_range(ext4_block_bitmap(sb
, gdp
), block
, count
) ||
4722 in_range(ext4_inode_bitmap(sb
, gdp
), block
, count
) ||
4723 in_range(block
, ext4_inode_table(sb
, gdp
),
4724 EXT4_SB(sb
)->s_itb_per_group
) ||
4725 in_range(block
+ count
- 1, ext4_inode_table(sb
, gdp
),
4726 EXT4_SB(sb
)->s_itb_per_group
)) {
4728 ext4_error(sb
, __func__
,
4729 "Freeing blocks in system zone - "
4730 "Block = %lu, count = %lu", block
, count
);
4731 /* err = 0. ext4_std_error should be a no op */
4735 BUFFER_TRACE(bitmap_bh
, "getting write access");
4736 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
4741 * We are about to modify some metadata. Call the journal APIs
4742 * to unshare ->b_data if a currently-committing transaction is
4745 BUFFER_TRACE(gd_bh
, "get_write_access");
4746 err
= ext4_journal_get_write_access(handle
, gd_bh
);
4749 #ifdef AGGRESSIVE_CHECK
4752 for (i
= 0; i
< count
; i
++)
4753 BUG_ON(!mb_test_bit(bit
+ i
, bitmap_bh
->b_data
));
4756 mb_clear_bits(sb_bgl_lock(sbi
, block_group
), bitmap_bh
->b_data
,
4759 /* We dirtied the bitmap block */
4760 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
4761 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
4766 ac
->ac_b_ex
.fe_group
= block_group
;
4767 ac
->ac_b_ex
.fe_start
= bit
;
4768 ac
->ac_b_ex
.fe_len
= count
;
4769 ext4_mb_store_history(ac
);
4772 err
= ext4_mb_load_buddy(sb
, block_group
, &e4b
);
4775 if (metadata
&& ext4_handle_valid(handle
)) {
4776 /* blocks being freed are metadata. these blocks shouldn't
4777 * be used until this transaction is committed */
4778 ext4_mb_free_metadata(handle
, &e4b
, block_group
, bit
, count
);
4780 ext4_lock_group(sb
, block_group
);
4781 mb_free_blocks(inode
, &e4b
, bit
, count
);
4782 ext4_mb_return_to_preallocation(inode
, &e4b
, block
, count
);
4783 ext4_unlock_group(sb
, block_group
);
4786 spin_lock(sb_bgl_lock(sbi
, block_group
));
4787 le16_add_cpu(&gdp
->bg_free_blocks_count
, count
);
4788 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, block_group
, gdp
);
4789 spin_unlock(sb_bgl_lock(sbi
, block_group
));
4790 percpu_counter_add(&sbi
->s_freeblocks_counter
, count
);
4792 if (sbi
->s_log_groups_per_flex
) {
4793 ext4_group_t flex_group
= ext4_flex_group(sbi
, block_group
);
4794 spin_lock(sb_bgl_lock(sbi
, flex_group
));
4795 sbi
->s_flex_groups
[flex_group
].free_blocks
+= count
;
4796 spin_unlock(sb_bgl_lock(sbi
, flex_group
));
4799 ext4_mb_release_desc(&e4b
);
4803 /* And the group descriptor block */
4804 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
4805 ret
= ext4_handle_dirty_metadata(handle
, NULL
, gd_bh
);
4809 if (overflow
&& !err
) {
4818 ext4_std_error(sb
, err
);
4820 kmem_cache_free(ext4_ac_cachep
, ac
);