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 buddy][ group 0 bitmap] [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:
334 static inline void *mb_correct_addr_and_bit(int *bit
, void *addr
)
336 #if BITS_PER_LONG == 64
337 *bit
+= ((unsigned long) addr
& 7UL) << 3;
338 addr
= (void *) ((unsigned long) addr
& ~7UL);
339 #elif BITS_PER_LONG == 32
340 *bit
+= ((unsigned long) addr
& 3UL) << 3;
341 addr
= (void *) ((unsigned long) addr
& ~3UL);
343 #error "how many bits you are?!"
348 static inline int mb_test_bit(int bit
, void *addr
)
351 * ext4_test_bit on architecture like powerpc
352 * needs unsigned long aligned address
354 addr
= mb_correct_addr_and_bit(&bit
, addr
);
355 return ext4_test_bit(bit
, addr
);
358 static inline void mb_set_bit(int bit
, void *addr
)
360 addr
= mb_correct_addr_and_bit(&bit
, addr
);
361 ext4_set_bit(bit
, addr
);
364 static inline void mb_set_bit_atomic(spinlock_t
*lock
, int bit
, void *addr
)
366 addr
= mb_correct_addr_and_bit(&bit
, addr
);
367 ext4_set_bit_atomic(lock
, bit
, addr
);
370 static inline void mb_clear_bit(int bit
, void *addr
)
372 addr
= mb_correct_addr_and_bit(&bit
, addr
);
373 ext4_clear_bit(bit
, addr
);
376 static inline void mb_clear_bit_atomic(spinlock_t
*lock
, int bit
, void *addr
)
378 addr
= mb_correct_addr_and_bit(&bit
, addr
);
379 ext4_clear_bit_atomic(lock
, bit
, addr
);
382 static inline int mb_find_next_zero_bit(void *addr
, int max
, int start
)
384 int fix
= 0, ret
, tmpmax
;
385 addr
= mb_correct_addr_and_bit(&fix
, addr
);
389 ret
= ext4_find_next_zero_bit(addr
, tmpmax
, start
) - fix
;
395 static inline int mb_find_next_bit(void *addr
, int max
, int start
)
397 int fix
= 0, ret
, tmpmax
;
398 addr
= mb_correct_addr_and_bit(&fix
, addr
);
402 ret
= ext4_find_next_bit(addr
, tmpmax
, start
) - fix
;
408 static void *mb_find_buddy(struct ext4_buddy
*e4b
, int order
, int *max
)
412 BUG_ON(EXT4_MB_BITMAP(e4b
) == EXT4_MB_BUDDY(e4b
));
415 if (order
> e4b
->bd_blkbits
+ 1) {
420 /* at order 0 we see each particular block */
421 *max
= 1 << (e4b
->bd_blkbits
+ 3);
423 return EXT4_MB_BITMAP(e4b
);
425 bb
= EXT4_MB_BUDDY(e4b
) + EXT4_SB(e4b
->bd_sb
)->s_mb_offsets
[order
];
426 *max
= EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[order
];
432 static void mb_free_blocks_double(struct inode
*inode
, struct ext4_buddy
*e4b
,
433 int first
, int count
)
436 struct super_block
*sb
= e4b
->bd_sb
;
438 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
440 BUG_ON(!ext4_is_group_locked(sb
, e4b
->bd_group
));
441 for (i
= 0; i
< count
; i
++) {
442 if (!mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
)) {
443 ext4_fsblk_t blocknr
;
444 blocknr
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
);
445 blocknr
+= first
+ i
;
447 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
449 ext4_error(sb
, __func__
, "double-free of inode"
450 " %lu's block %llu(bit %u in group %lu)\n",
451 inode
? inode
->i_ino
: 0, blocknr
,
452 first
+ i
, e4b
->bd_group
);
454 mb_clear_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
458 static void mb_mark_used_double(struct ext4_buddy
*e4b
, int first
, int count
)
462 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
464 BUG_ON(!ext4_is_group_locked(e4b
->bd_sb
, e4b
->bd_group
));
465 for (i
= 0; i
< count
; i
++) {
466 BUG_ON(mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
));
467 mb_set_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
471 static void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
473 if (memcmp(e4b
->bd_info
->bb_bitmap
, bitmap
, e4b
->bd_sb
->s_blocksize
)) {
474 unsigned char *b1
, *b2
;
476 b1
= (unsigned char *) e4b
->bd_info
->bb_bitmap
;
477 b2
= (unsigned char *) bitmap
;
478 for (i
= 0; i
< e4b
->bd_sb
->s_blocksize
; i
++) {
479 if (b1
[i
] != b2
[i
]) {
480 printk("corruption in group %lu at byte %u(%u):"
481 " %x in copy != %x on disk/prealloc\n",
482 e4b
->bd_group
, i
, i
* 8, b1
[i
], b2
[i
]);
490 static inline void mb_free_blocks_double(struct inode
*inode
,
491 struct ext4_buddy
*e4b
, int first
, int count
)
495 static inline void mb_mark_used_double(struct ext4_buddy
*e4b
,
496 int first
, int count
)
500 static inline void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
506 #ifdef AGGRESSIVE_CHECK
508 #define MB_CHECK_ASSERT(assert) \
512 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
513 function, file, line, # assert); \
518 static int __mb_check_buddy(struct ext4_buddy
*e4b
, char *file
,
519 const char *function
, int line
)
521 struct super_block
*sb
= e4b
->bd_sb
;
522 int order
= e4b
->bd_blkbits
+ 1;
529 struct ext4_group_info
*grp
;
532 struct list_head
*cur
;
536 if (!test_opt(sb
, MBALLOC
))
540 static int mb_check_counter
;
541 if (mb_check_counter
++ % 100 != 0)
546 buddy
= mb_find_buddy(e4b
, order
, &max
);
547 MB_CHECK_ASSERT(buddy
);
548 buddy2
= mb_find_buddy(e4b
, order
- 1, &max2
);
549 MB_CHECK_ASSERT(buddy2
);
550 MB_CHECK_ASSERT(buddy
!= buddy2
);
551 MB_CHECK_ASSERT(max
* 2 == max2
);
554 for (i
= 0; i
< max
; i
++) {
556 if (mb_test_bit(i
, buddy
)) {
557 /* only single bit in buddy2 may be 1 */
558 if (!mb_test_bit(i
<< 1, buddy2
)) {
560 mb_test_bit((i
<<1)+1, buddy2
));
561 } else if (!mb_test_bit((i
<< 1) + 1, buddy2
)) {
563 mb_test_bit(i
<< 1, buddy2
));
568 /* both bits in buddy2 must be 0 */
569 MB_CHECK_ASSERT(mb_test_bit(i
<< 1, buddy2
));
570 MB_CHECK_ASSERT(mb_test_bit((i
<< 1) + 1, buddy2
));
572 for (j
= 0; j
< (1 << order
); j
++) {
573 k
= (i
* (1 << order
)) + j
;
575 !mb_test_bit(k
, EXT4_MB_BITMAP(e4b
)));
579 MB_CHECK_ASSERT(e4b
->bd_info
->bb_counters
[order
] == count
);
584 buddy
= mb_find_buddy(e4b
, 0, &max
);
585 for (i
= 0; i
< max
; i
++) {
586 if (!mb_test_bit(i
, buddy
)) {
587 MB_CHECK_ASSERT(i
>= e4b
->bd_info
->bb_first_free
);
595 /* check used bits only */
596 for (j
= 0; j
< e4b
->bd_blkbits
+ 1; j
++) {
597 buddy2
= mb_find_buddy(e4b
, j
, &max2
);
599 MB_CHECK_ASSERT(k
< max2
);
600 MB_CHECK_ASSERT(mb_test_bit(k
, buddy2
));
603 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b
->bd_info
));
604 MB_CHECK_ASSERT(e4b
->bd_info
->bb_fragments
== fragments
);
606 grp
= ext4_get_group_info(sb
, e4b
->bd_group
);
607 buddy
= mb_find_buddy(e4b
, 0, &max
);
608 list_for_each(cur
, &grp
->bb_prealloc_list
) {
609 ext4_group_t groupnr
;
610 struct ext4_prealloc_space
*pa
;
611 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
612 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &groupnr
, &k
);
613 MB_CHECK_ASSERT(groupnr
== e4b
->bd_group
);
614 for (i
= 0; i
< pa
->pa_len
; i
++)
615 MB_CHECK_ASSERT(mb_test_bit(k
+ i
, buddy
));
619 #undef MB_CHECK_ASSERT
620 #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
621 __FILE__, __func__, __LINE__)
623 #define mb_check_buddy(e4b)
626 /* FIXME!! need more doc */
627 static void ext4_mb_mark_free_simple(struct super_block
*sb
,
628 void *buddy
, unsigned first
, int len
,
629 struct ext4_group_info
*grp
)
631 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
634 unsigned short chunk
;
635 unsigned short border
;
637 BUG_ON(len
> EXT4_BLOCKS_PER_GROUP(sb
));
639 border
= 2 << sb
->s_blocksize_bits
;
642 /* find how many blocks can be covered since this position */
643 max
= ffs(first
| border
) - 1;
645 /* find how many blocks of power 2 we need to mark */
652 /* mark multiblock chunks only */
653 grp
->bb_counters
[min
]++;
655 mb_clear_bit(first
>> min
,
656 buddy
+ sbi
->s_mb_offsets
[min
]);
663 static void ext4_mb_generate_buddy(struct super_block
*sb
,
664 void *buddy
, void *bitmap
, ext4_group_t group
)
666 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
667 unsigned short max
= EXT4_BLOCKS_PER_GROUP(sb
);
668 unsigned short i
= 0;
669 unsigned short first
;
672 unsigned fragments
= 0;
673 unsigned long long period
= get_cycles();
675 /* initialize buddy from bitmap which is aggregation
676 * of on-disk bitmap and preallocations */
677 i
= mb_find_next_zero_bit(bitmap
, max
, 0);
678 grp
->bb_first_free
= i
;
682 i
= mb_find_next_bit(bitmap
, max
, i
);
686 ext4_mb_mark_free_simple(sb
, buddy
, first
, len
, grp
);
688 grp
->bb_counters
[0]++;
690 i
= mb_find_next_zero_bit(bitmap
, max
, i
);
692 grp
->bb_fragments
= fragments
;
694 if (free
!= grp
->bb_free
) {
695 ext4_error(sb
, __func__
,
696 "EXT4-fs: group %lu: %u blocks in bitmap, %u in gd\n",
697 group
, free
, grp
->bb_free
);
699 * If we intent to continue, we consider group descritor
700 * corrupt and update bb_free using bitmap value
705 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
, &(grp
->bb_state
));
707 period
= get_cycles() - period
;
708 spin_lock(&EXT4_SB(sb
)->s_bal_lock
);
709 EXT4_SB(sb
)->s_mb_buddies_generated
++;
710 EXT4_SB(sb
)->s_mb_generation_time
+= period
;
711 spin_unlock(&EXT4_SB(sb
)->s_bal_lock
);
714 /* The buddy information is attached the buddy cache inode
715 * for convenience. The information regarding each group
716 * is loaded via ext4_mb_load_buddy. The information involve
717 * block bitmap and buddy information. The information are
718 * stored in the inode as
721 * [ group 0 buddy][ group 0 bitmap] [group 1][ group 1]...
724 * one block each for bitmap and buddy information.
725 * So for each group we take up 2 blocks. A page can
726 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
727 * So it can have information regarding groups_per_page which
728 * is blocks_per_page/2
731 static int ext4_mb_init_cache(struct page
*page
, char *incore
)
738 ext4_group_t first_group
;
740 struct super_block
*sb
;
741 struct buffer_head
*bhs
;
742 struct buffer_head
**bh
;
747 mb_debug("init page %lu\n", page
->index
);
749 inode
= page
->mapping
->host
;
751 blocksize
= 1 << inode
->i_blkbits
;
752 blocks_per_page
= PAGE_CACHE_SIZE
/ blocksize
;
754 groups_per_page
= blocks_per_page
>> 1;
755 if (groups_per_page
== 0)
758 /* allocate buffer_heads to read bitmaps */
759 if (groups_per_page
> 1) {
761 i
= sizeof(struct buffer_head
*) * groups_per_page
;
762 bh
= kzalloc(i
, GFP_NOFS
);
768 first_group
= page
->index
* blocks_per_page
/ 2;
770 /* read all groups the page covers into the cache */
771 for (i
= 0; i
< groups_per_page
; i
++) {
772 struct ext4_group_desc
*desc
;
774 if (first_group
+ i
>= EXT4_SB(sb
)->s_groups_count
)
778 desc
= ext4_get_group_desc(sb
, first_group
+ i
, NULL
);
783 bh
[i
] = sb_getblk(sb
, ext4_block_bitmap(sb
, desc
));
787 if (bh_uptodate_or_lock(bh
[i
]))
790 spin_lock(sb_bgl_lock(EXT4_SB(sb
), first_group
+ i
));
791 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
792 ext4_init_block_bitmap(sb
, bh
[i
],
793 first_group
+ i
, desc
);
794 set_buffer_uptodate(bh
[i
]);
795 unlock_buffer(bh
[i
]);
796 spin_unlock(sb_bgl_lock(EXT4_SB(sb
), first_group
+ i
));
799 spin_unlock(sb_bgl_lock(EXT4_SB(sb
), first_group
+ i
));
801 bh
[i
]->b_end_io
= end_buffer_read_sync
;
802 submit_bh(READ
, bh
[i
]);
803 mb_debug("read bitmap for group %lu\n", first_group
+ i
);
806 /* wait for I/O completion */
807 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
808 wait_on_buffer(bh
[i
]);
811 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
812 if (!buffer_uptodate(bh
[i
]))
816 first_block
= page
->index
* blocks_per_page
;
817 for (i
= 0; i
< blocks_per_page
; i
++) {
819 struct ext4_group_info
*grinfo
;
821 group
= (first_block
+ i
) >> 1;
822 if (group
>= EXT4_SB(sb
)->s_groups_count
)
826 * data carry information regarding this
827 * particular group in the format specified
831 data
= page_address(page
) + (i
* blocksize
);
832 bitmap
= bh
[group
- first_group
]->b_data
;
835 * We place the buddy block and bitmap block
838 if ((first_block
+ i
) & 1) {
839 /* this is block of buddy */
840 BUG_ON(incore
== NULL
);
841 mb_debug("put buddy for group %u in page %lu/%x\n",
842 group
, page
->index
, i
* blocksize
);
843 memset(data
, 0xff, blocksize
);
844 grinfo
= ext4_get_group_info(sb
, group
);
845 grinfo
->bb_fragments
= 0;
846 memset(grinfo
->bb_counters
, 0,
847 sizeof(unsigned short)*(sb
->s_blocksize_bits
+2));
849 * incore got set to the group block bitmap below
851 ext4_mb_generate_buddy(sb
, data
, incore
, group
);
854 /* this is block of bitmap */
855 BUG_ON(incore
!= NULL
);
856 mb_debug("put bitmap for group %u in page %lu/%x\n",
857 group
, page
->index
, i
* blocksize
);
859 /* see comments in ext4_mb_put_pa() */
860 ext4_lock_group(sb
, group
);
861 memcpy(data
, bitmap
, blocksize
);
863 /* mark all preallocated blks used in in-core bitmap */
864 ext4_mb_generate_from_pa(sb
, data
, group
);
865 ext4_unlock_group(sb
, group
);
867 /* set incore so that the buddy information can be
868 * generated using this
873 SetPageUptodate(page
);
877 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
885 static noinline_for_stack
int
886 ext4_mb_load_buddy(struct super_block
*sb
, ext4_group_t group
,
887 struct ext4_buddy
*e4b
)
889 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
890 struct inode
*inode
= sbi
->s_buddy_cache
;
898 mb_debug("load group %lu\n", group
);
900 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
902 e4b
->bd_blkbits
= sb
->s_blocksize_bits
;
903 e4b
->bd_info
= ext4_get_group_info(sb
, group
);
905 e4b
->bd_group
= group
;
906 e4b
->bd_buddy_page
= NULL
;
907 e4b
->bd_bitmap_page
= NULL
;
910 * the buddy cache inode stores the block bitmap
911 * and buddy information in consecutive blocks.
912 * So for each group we need two blocks.
915 pnum
= block
/ blocks_per_page
;
916 poff
= block
% blocks_per_page
;
918 /* we could use find_or_create_page(), but it locks page
919 * what we'd like to avoid in fast path ... */
920 page
= find_get_page(inode
->i_mapping
, pnum
);
921 if (page
== NULL
|| !PageUptodate(page
)) {
923 page_cache_release(page
);
924 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
926 BUG_ON(page
->mapping
!= inode
->i_mapping
);
927 if (!PageUptodate(page
)) {
928 ret
= ext4_mb_init_cache(page
, NULL
);
933 mb_cmp_bitmaps(e4b
, page_address(page
) +
934 (poff
* sb
->s_blocksize
));
939 if (page
== NULL
|| !PageUptodate(page
)) {
943 e4b
->bd_bitmap_page
= page
;
944 e4b
->bd_bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
945 mark_page_accessed(page
);
948 pnum
= block
/ blocks_per_page
;
949 poff
= block
% blocks_per_page
;
951 page
= find_get_page(inode
->i_mapping
, pnum
);
952 if (page
== NULL
|| !PageUptodate(page
)) {
954 page_cache_release(page
);
955 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
957 BUG_ON(page
->mapping
!= inode
->i_mapping
);
958 if (!PageUptodate(page
)) {
959 ret
= ext4_mb_init_cache(page
, e4b
->bd_bitmap
);
968 if (page
== NULL
|| !PageUptodate(page
)) {
972 e4b
->bd_buddy_page
= page
;
973 e4b
->bd_buddy
= page_address(page
) + (poff
* sb
->s_blocksize
);
974 mark_page_accessed(page
);
976 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
977 BUG_ON(e4b
->bd_buddy_page
== NULL
);
982 if (e4b
->bd_bitmap_page
)
983 page_cache_release(e4b
->bd_bitmap_page
);
984 if (e4b
->bd_buddy_page
)
985 page_cache_release(e4b
->bd_buddy_page
);
986 e4b
->bd_buddy
= NULL
;
987 e4b
->bd_bitmap
= NULL
;
991 static void ext4_mb_release_desc(struct ext4_buddy
*e4b
)
993 if (e4b
->bd_bitmap_page
)
994 page_cache_release(e4b
->bd_bitmap_page
);
995 if (e4b
->bd_buddy_page
)
996 page_cache_release(e4b
->bd_buddy_page
);
1000 static int mb_find_order_for_block(struct ext4_buddy
*e4b
, int block
)
1005 BUG_ON(EXT4_MB_BITMAP(e4b
) == EXT4_MB_BUDDY(e4b
));
1006 BUG_ON(block
>= (1 << (e4b
->bd_blkbits
+ 3)));
1008 bb
= EXT4_MB_BUDDY(e4b
);
1009 while (order
<= e4b
->bd_blkbits
+ 1) {
1011 if (!mb_test_bit(block
, bb
)) {
1012 /* this block is part of buddy of order 'order' */
1015 bb
+= 1 << (e4b
->bd_blkbits
- order
);
1021 static void mb_clear_bits(spinlock_t
*lock
, void *bm
, int cur
, int len
)
1027 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1028 /* fast path: clear whole word at once */
1029 addr
= bm
+ (cur
>> 3);
1034 mb_clear_bit_atomic(lock
, cur
, bm
);
1039 static void mb_set_bits(spinlock_t
*lock
, void *bm
, int cur
, int len
)
1045 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1046 /* fast path: set whole word at once */
1047 addr
= bm
+ (cur
>> 3);
1052 mb_set_bit_atomic(lock
, cur
, bm
);
1057 static void mb_free_blocks(struct inode
*inode
, struct ext4_buddy
*e4b
,
1058 int first
, int count
)
1065 struct super_block
*sb
= e4b
->bd_sb
;
1067 BUG_ON(first
+ count
> (sb
->s_blocksize
<< 3));
1068 BUG_ON(!ext4_is_group_locked(sb
, e4b
->bd_group
));
1069 mb_check_buddy(e4b
);
1070 mb_free_blocks_double(inode
, e4b
, first
, count
);
1072 e4b
->bd_info
->bb_free
+= count
;
1073 if (first
< e4b
->bd_info
->bb_first_free
)
1074 e4b
->bd_info
->bb_first_free
= first
;
1076 /* let's maintain fragments counter */
1078 block
= !mb_test_bit(first
- 1, EXT4_MB_BITMAP(e4b
));
1079 if (first
+ count
< EXT4_SB(sb
)->s_mb_maxs
[0])
1080 max
= !mb_test_bit(first
+ count
, EXT4_MB_BITMAP(e4b
));
1082 e4b
->bd_info
->bb_fragments
--;
1083 else if (!block
&& !max
)
1084 e4b
->bd_info
->bb_fragments
++;
1086 /* let's maintain buddy itself */
1087 while (count
-- > 0) {
1091 if (!mb_test_bit(block
, EXT4_MB_BITMAP(e4b
))) {
1092 ext4_fsblk_t blocknr
;
1093 blocknr
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
);
1096 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
1097 ext4_unlock_group(sb
, e4b
->bd_group
);
1098 ext4_error(sb
, __func__
, "double-free of inode"
1099 " %lu's block %llu(bit %u in group %lu)\n",
1100 inode
? inode
->i_ino
: 0, blocknr
, block
,
1102 ext4_lock_group(sb
, e4b
->bd_group
);
1104 mb_clear_bit(block
, EXT4_MB_BITMAP(e4b
));
1105 e4b
->bd_info
->bb_counters
[order
]++;
1107 /* start of the buddy */
1108 buddy
= mb_find_buddy(e4b
, order
, &max
);
1112 if (mb_test_bit(block
, buddy
) ||
1113 mb_test_bit(block
+ 1, buddy
))
1116 /* both the buddies are free, try to coalesce them */
1117 buddy2
= mb_find_buddy(e4b
, order
+ 1, &max
);
1123 /* for special purposes, we don't set
1124 * free bits in bitmap */
1125 mb_set_bit(block
, buddy
);
1126 mb_set_bit(block
+ 1, buddy
);
1128 e4b
->bd_info
->bb_counters
[order
]--;
1129 e4b
->bd_info
->bb_counters
[order
]--;
1133 e4b
->bd_info
->bb_counters
[order
]++;
1135 mb_clear_bit(block
, buddy2
);
1139 mb_check_buddy(e4b
);
1142 static int mb_find_extent(struct ext4_buddy
*e4b
, int order
, int block
,
1143 int needed
, struct ext4_free_extent
*ex
)
1150 BUG_ON(!ext4_is_group_locked(e4b
->bd_sb
, e4b
->bd_group
));
1153 buddy
= mb_find_buddy(e4b
, order
, &max
);
1154 BUG_ON(buddy
== NULL
);
1155 BUG_ON(block
>= max
);
1156 if (mb_test_bit(block
, buddy
)) {
1163 /* FIXME dorp order completely ? */
1164 if (likely(order
== 0)) {
1165 /* find actual order */
1166 order
= mb_find_order_for_block(e4b
, block
);
1167 block
= block
>> order
;
1170 ex
->fe_len
= 1 << order
;
1171 ex
->fe_start
= block
<< order
;
1172 ex
->fe_group
= e4b
->bd_group
;
1174 /* calc difference from given start */
1175 next
= next
- ex
->fe_start
;
1177 ex
->fe_start
+= next
;
1179 while (needed
> ex
->fe_len
&&
1180 (buddy
= mb_find_buddy(e4b
, order
, &max
))) {
1182 if (block
+ 1 >= max
)
1185 next
= (block
+ 1) * (1 << order
);
1186 if (mb_test_bit(next
, EXT4_MB_BITMAP(e4b
)))
1189 ord
= mb_find_order_for_block(e4b
, next
);
1192 block
= next
>> order
;
1193 ex
->fe_len
+= 1 << order
;
1196 BUG_ON(ex
->fe_start
+ ex
->fe_len
> (1 << (e4b
->bd_blkbits
+ 3)));
1200 static int mb_mark_used(struct ext4_buddy
*e4b
, struct ext4_free_extent
*ex
)
1206 int start
= ex
->fe_start
;
1207 int len
= ex
->fe_len
;
1212 BUG_ON(start
+ len
> (e4b
->bd_sb
->s_blocksize
<< 3));
1213 BUG_ON(e4b
->bd_group
!= ex
->fe_group
);
1214 BUG_ON(!ext4_is_group_locked(e4b
->bd_sb
, e4b
->bd_group
));
1215 mb_check_buddy(e4b
);
1216 mb_mark_used_double(e4b
, start
, len
);
1218 e4b
->bd_info
->bb_free
-= len
;
1219 if (e4b
->bd_info
->bb_first_free
== start
)
1220 e4b
->bd_info
->bb_first_free
+= len
;
1222 /* let's maintain fragments counter */
1224 mlen
= !mb_test_bit(start
- 1, EXT4_MB_BITMAP(e4b
));
1225 if (start
+ len
< EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[0])
1226 max
= !mb_test_bit(start
+ len
, EXT4_MB_BITMAP(e4b
));
1228 e4b
->bd_info
->bb_fragments
++;
1229 else if (!mlen
&& !max
)
1230 e4b
->bd_info
->bb_fragments
--;
1232 /* let's maintain buddy itself */
1234 ord
= mb_find_order_for_block(e4b
, start
);
1236 if (((start
>> ord
) << ord
) == start
&& len
>= (1 << ord
)) {
1237 /* the whole chunk may be allocated at once! */
1239 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1240 BUG_ON((start
>> ord
) >= max
);
1241 mb_set_bit(start
>> ord
, buddy
);
1242 e4b
->bd_info
->bb_counters
[ord
]--;
1249 /* store for history */
1251 ret
= len
| (ord
<< 16);
1253 /* we have to split large buddy */
1255 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1256 mb_set_bit(start
>> ord
, buddy
);
1257 e4b
->bd_info
->bb_counters
[ord
]--;
1260 cur
= (start
>> ord
) & ~1U;
1261 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1262 mb_clear_bit(cur
, buddy
);
1263 mb_clear_bit(cur
+ 1, buddy
);
1264 e4b
->bd_info
->bb_counters
[ord
]++;
1265 e4b
->bd_info
->bb_counters
[ord
]++;
1268 mb_set_bits(sb_bgl_lock(EXT4_SB(e4b
->bd_sb
), ex
->fe_group
),
1269 EXT4_MB_BITMAP(e4b
), ex
->fe_start
, len0
);
1270 mb_check_buddy(e4b
);
1276 * Must be called under group lock!
1278 static void ext4_mb_use_best_found(struct ext4_allocation_context
*ac
,
1279 struct ext4_buddy
*e4b
)
1281 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1284 BUG_ON(ac
->ac_b_ex
.fe_group
!= e4b
->bd_group
);
1285 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1287 ac
->ac_b_ex
.fe_len
= min(ac
->ac_b_ex
.fe_len
, ac
->ac_g_ex
.fe_len
);
1288 ac
->ac_b_ex
.fe_logical
= ac
->ac_g_ex
.fe_logical
;
1289 ret
= mb_mark_used(e4b
, &ac
->ac_b_ex
);
1291 /* preallocation can change ac_b_ex, thus we store actually
1292 * allocated blocks for history */
1293 ac
->ac_f_ex
= ac
->ac_b_ex
;
1295 ac
->ac_status
= AC_STATUS_FOUND
;
1296 ac
->ac_tail
= ret
& 0xffff;
1297 ac
->ac_buddy
= ret
>> 16;
1299 /* XXXXXXX: SUCH A HORRIBLE **CK */
1301 ac
->ac_bitmap_page
= e4b
->bd_bitmap_page
;
1302 get_page(ac
->ac_bitmap_page
);
1303 ac
->ac_buddy_page
= e4b
->bd_buddy_page
;
1304 get_page(ac
->ac_buddy_page
);
1306 /* store last allocated for subsequent stream allocation */
1307 if ((ac
->ac_flags
& EXT4_MB_HINT_DATA
)) {
1308 spin_lock(&sbi
->s_md_lock
);
1309 sbi
->s_mb_last_group
= ac
->ac_f_ex
.fe_group
;
1310 sbi
->s_mb_last_start
= ac
->ac_f_ex
.fe_start
;
1311 spin_unlock(&sbi
->s_md_lock
);
1316 * regular allocator, for general purposes allocation
1319 static void ext4_mb_check_limits(struct ext4_allocation_context
*ac
,
1320 struct ext4_buddy
*e4b
,
1323 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1324 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1325 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1326 struct ext4_free_extent ex
;
1330 * We don't want to scan for a whole year
1332 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
&&
1333 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1334 ac
->ac_status
= AC_STATUS_BREAK
;
1339 * Haven't found good chunk so far, let's continue
1341 if (bex
->fe_len
< gex
->fe_len
)
1344 if ((finish_group
|| ac
->ac_found
> sbi
->s_mb_min_to_scan
)
1345 && bex
->fe_group
== e4b
->bd_group
) {
1346 /* recheck chunk's availability - we don't know
1347 * when it was found (within this lock-unlock
1349 max
= mb_find_extent(e4b
, 0, bex
->fe_start
, gex
->fe_len
, &ex
);
1350 if (max
>= gex
->fe_len
) {
1351 ext4_mb_use_best_found(ac
, e4b
);
1358 * The routine checks whether found extent is good enough. If it is,
1359 * then the extent gets marked used and flag is set to the context
1360 * to stop scanning. Otherwise, the extent is compared with the
1361 * previous found extent and if new one is better, then it's stored
1362 * in the context. Later, the best found extent will be used, if
1363 * mballoc can't find good enough extent.
1365 * FIXME: real allocation policy is to be designed yet!
1367 static void ext4_mb_measure_extent(struct ext4_allocation_context
*ac
,
1368 struct ext4_free_extent
*ex
,
1369 struct ext4_buddy
*e4b
)
1371 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1372 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1374 BUG_ON(ex
->fe_len
<= 0);
1375 BUG_ON(ex
->fe_len
>= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
1376 BUG_ON(ex
->fe_start
>= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
1377 BUG_ON(ac
->ac_status
!= AC_STATUS_CONTINUE
);
1382 * The special case - take what you catch first
1384 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1386 ext4_mb_use_best_found(ac
, e4b
);
1391 * Let's check whether the chuck is good enough
1393 if (ex
->fe_len
== gex
->fe_len
) {
1395 ext4_mb_use_best_found(ac
, e4b
);
1400 * If this is first found extent, just store it in the context
1402 if (bex
->fe_len
== 0) {
1408 * If new found extent is better, store it in the context
1410 if (bex
->fe_len
< gex
->fe_len
) {
1411 /* if the request isn't satisfied, any found extent
1412 * larger than previous best one is better */
1413 if (ex
->fe_len
> bex
->fe_len
)
1415 } else if (ex
->fe_len
> gex
->fe_len
) {
1416 /* if the request is satisfied, then we try to find
1417 * an extent that still satisfy the request, but is
1418 * smaller than previous one */
1419 if (ex
->fe_len
< bex
->fe_len
)
1423 ext4_mb_check_limits(ac
, e4b
, 0);
1426 static int ext4_mb_try_best_found(struct ext4_allocation_context
*ac
,
1427 struct ext4_buddy
*e4b
)
1429 struct ext4_free_extent ex
= ac
->ac_b_ex
;
1430 ext4_group_t group
= ex
.fe_group
;
1434 BUG_ON(ex
.fe_len
<= 0);
1435 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1439 ext4_lock_group(ac
->ac_sb
, group
);
1440 max
= mb_find_extent(e4b
, 0, ex
.fe_start
, ex
.fe_len
, &ex
);
1444 ext4_mb_use_best_found(ac
, e4b
);
1447 ext4_unlock_group(ac
->ac_sb
, group
);
1448 ext4_mb_release_desc(e4b
);
1453 static int ext4_mb_find_by_goal(struct ext4_allocation_context
*ac
,
1454 struct ext4_buddy
*e4b
)
1456 ext4_group_t group
= ac
->ac_g_ex
.fe_group
;
1459 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1460 struct ext4_super_block
*es
= sbi
->s_es
;
1461 struct ext4_free_extent ex
;
1463 if (!(ac
->ac_flags
& EXT4_MB_HINT_TRY_GOAL
))
1466 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1470 ext4_lock_group(ac
->ac_sb
, group
);
1471 max
= mb_find_extent(e4b
, 0, ac
->ac_g_ex
.fe_start
,
1472 ac
->ac_g_ex
.fe_len
, &ex
);
1474 if (max
>= ac
->ac_g_ex
.fe_len
&& ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
) {
1477 start
= (e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
)) +
1478 ex
.fe_start
+ le32_to_cpu(es
->s_first_data_block
);
1479 /* use do_div to get remainder (would be 64-bit modulo) */
1480 if (do_div(start
, sbi
->s_stripe
) == 0) {
1483 ext4_mb_use_best_found(ac
, e4b
);
1485 } else if (max
>= ac
->ac_g_ex
.fe_len
) {
1486 BUG_ON(ex
.fe_len
<= 0);
1487 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1488 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1491 ext4_mb_use_best_found(ac
, e4b
);
1492 } else if (max
> 0 && (ac
->ac_flags
& EXT4_MB_HINT_MERGE
)) {
1493 /* Sometimes, caller may want to merge even small
1494 * number of blocks to an existing extent */
1495 BUG_ON(ex
.fe_len
<= 0);
1496 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1497 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1500 ext4_mb_use_best_found(ac
, e4b
);
1502 ext4_unlock_group(ac
->ac_sb
, group
);
1503 ext4_mb_release_desc(e4b
);
1509 * The routine scans buddy structures (not bitmap!) from given order
1510 * to max order and tries to find big enough chunk to satisfy the req
1512 static void ext4_mb_simple_scan_group(struct ext4_allocation_context
*ac
,
1513 struct ext4_buddy
*e4b
)
1515 struct super_block
*sb
= ac
->ac_sb
;
1516 struct ext4_group_info
*grp
= e4b
->bd_info
;
1522 BUG_ON(ac
->ac_2order
<= 0);
1523 for (i
= ac
->ac_2order
; i
<= sb
->s_blocksize_bits
+ 1; i
++) {
1524 if (grp
->bb_counters
[i
] == 0)
1527 buddy
= mb_find_buddy(e4b
, i
, &max
);
1528 BUG_ON(buddy
== NULL
);
1530 k
= mb_find_next_zero_bit(buddy
, max
, 0);
1535 ac
->ac_b_ex
.fe_len
= 1 << i
;
1536 ac
->ac_b_ex
.fe_start
= k
<< i
;
1537 ac
->ac_b_ex
.fe_group
= e4b
->bd_group
;
1539 ext4_mb_use_best_found(ac
, e4b
);
1541 BUG_ON(ac
->ac_b_ex
.fe_len
!= ac
->ac_g_ex
.fe_len
);
1543 if (EXT4_SB(sb
)->s_mb_stats
)
1544 atomic_inc(&EXT4_SB(sb
)->s_bal_2orders
);
1551 * The routine scans the group and measures all found extents.
1552 * In order to optimize scanning, caller must pass number of
1553 * free blocks in the group, so the routine can know upper limit.
1555 static void ext4_mb_complex_scan_group(struct ext4_allocation_context
*ac
,
1556 struct ext4_buddy
*e4b
)
1558 struct super_block
*sb
= ac
->ac_sb
;
1559 void *bitmap
= EXT4_MB_BITMAP(e4b
);
1560 struct ext4_free_extent ex
;
1564 free
= e4b
->bd_info
->bb_free
;
1567 i
= e4b
->bd_info
->bb_first_free
;
1569 while (free
&& ac
->ac_status
== AC_STATUS_CONTINUE
) {
1570 i
= mb_find_next_zero_bit(bitmap
,
1571 EXT4_BLOCKS_PER_GROUP(sb
), i
);
1572 if (i
>= EXT4_BLOCKS_PER_GROUP(sb
)) {
1574 * IF we have corrupt bitmap, we won't find any
1575 * free blocks even though group info says we
1576 * we have free blocks
1578 ext4_error(sb
, __func__
, "%d free blocks as per "
1579 "group info. But bitmap says 0\n",
1584 mb_find_extent(e4b
, 0, i
, ac
->ac_g_ex
.fe_len
, &ex
);
1585 BUG_ON(ex
.fe_len
<= 0);
1586 if (free
< ex
.fe_len
) {
1587 ext4_error(sb
, __func__
, "%d free blocks as per "
1588 "group info. But got %d blocks\n",
1591 * The number of free blocks differs. This mostly
1592 * indicate that the bitmap is corrupt. So exit
1593 * without claiming the space.
1598 ext4_mb_measure_extent(ac
, &ex
, e4b
);
1604 ext4_mb_check_limits(ac
, e4b
, 1);
1608 * This is a special case for storages like raid5
1609 * we try to find stripe-aligned chunks for stripe-size requests
1610 * XXX should do so at least for multiples of stripe size as well
1612 static void ext4_mb_scan_aligned(struct ext4_allocation_context
*ac
,
1613 struct ext4_buddy
*e4b
)
1615 struct super_block
*sb
= ac
->ac_sb
;
1616 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1617 void *bitmap
= EXT4_MB_BITMAP(e4b
);
1618 struct ext4_free_extent ex
;
1619 ext4_fsblk_t first_group_block
;
1624 BUG_ON(sbi
->s_stripe
== 0);
1626 /* find first stripe-aligned block in group */
1627 first_group_block
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
)
1628 + le32_to_cpu(sbi
->s_es
->s_first_data_block
);
1629 a
= first_group_block
+ sbi
->s_stripe
- 1;
1630 do_div(a
, sbi
->s_stripe
);
1631 i
= (a
* sbi
->s_stripe
) - first_group_block
;
1633 while (i
< EXT4_BLOCKS_PER_GROUP(sb
)) {
1634 if (!mb_test_bit(i
, bitmap
)) {
1635 max
= mb_find_extent(e4b
, 0, i
, sbi
->s_stripe
, &ex
);
1636 if (max
>= sbi
->s_stripe
) {
1639 ext4_mb_use_best_found(ac
, e4b
);
1647 static int ext4_mb_good_group(struct ext4_allocation_context
*ac
,
1648 ext4_group_t group
, int cr
)
1650 unsigned free
, fragments
;
1652 struct ext4_group_desc
*desc
;
1653 struct ext4_group_info
*grp
= ext4_get_group_info(ac
->ac_sb
, group
);
1655 BUG_ON(cr
< 0 || cr
>= 4);
1656 BUG_ON(EXT4_MB_GRP_NEED_INIT(grp
));
1658 free
= grp
->bb_free
;
1659 fragments
= grp
->bb_fragments
;
1667 BUG_ON(ac
->ac_2order
== 0);
1668 /* If this group is uninitialized, skip it initially */
1669 desc
= ext4_get_group_desc(ac
->ac_sb
, group
, NULL
);
1670 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
))
1673 bits
= ac
->ac_sb
->s_blocksize_bits
+ 1;
1674 for (i
= ac
->ac_2order
; i
<= bits
; i
++)
1675 if (grp
->bb_counters
[i
] > 0)
1679 if ((free
/ fragments
) >= ac
->ac_g_ex
.fe_len
)
1683 if (free
>= ac
->ac_g_ex
.fe_len
)
1695 static noinline_for_stack
int
1696 ext4_mb_regular_allocator(struct ext4_allocation_context
*ac
)
1703 struct ext4_sb_info
*sbi
;
1704 struct super_block
*sb
;
1705 struct ext4_buddy e4b
;
1710 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1712 /* first, try the goal */
1713 err
= ext4_mb_find_by_goal(ac
, &e4b
);
1714 if (err
|| ac
->ac_status
== AC_STATUS_FOUND
)
1717 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
1721 * ac->ac2_order is set only if the fe_len is a power of 2
1722 * if ac2_order is set we also set criteria to 0 so that we
1723 * try exact allocation using buddy.
1725 i
= fls(ac
->ac_g_ex
.fe_len
);
1728 * We search using buddy data only if the order of the request
1729 * is greater than equal to the sbi_s_mb_order2_reqs
1730 * You can tune it via /proc/fs/ext4/<partition>/order2_req
1732 if (i
>= sbi
->s_mb_order2_reqs
) {
1734 * This should tell if fe_len is exactly power of 2
1736 if ((ac
->ac_g_ex
.fe_len
& (~(1 << (i
- 1)))) == 0)
1737 ac
->ac_2order
= i
- 1;
1740 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
1741 /* if stream allocation is enabled, use global goal */
1742 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
1743 isize
= i_size_read(ac
->ac_inode
) >> bsbits
;
1747 if (size
< sbi
->s_mb_stream_request
&&
1748 (ac
->ac_flags
& EXT4_MB_HINT_DATA
)) {
1749 /* TBD: may be hot point */
1750 spin_lock(&sbi
->s_md_lock
);
1751 ac
->ac_g_ex
.fe_group
= sbi
->s_mb_last_group
;
1752 ac
->ac_g_ex
.fe_start
= sbi
->s_mb_last_start
;
1753 spin_unlock(&sbi
->s_md_lock
);
1755 /* Let's just scan groups to find more-less suitable blocks */
1756 cr
= ac
->ac_2order
? 0 : 1;
1758 * cr == 0 try to get exact allocation,
1759 * cr == 3 try to get anything
1762 for (; cr
< 4 && ac
->ac_status
== AC_STATUS_CONTINUE
; cr
++) {
1763 ac
->ac_criteria
= cr
;
1765 * searching for the right group start
1766 * from the goal value specified
1768 group
= ac
->ac_g_ex
.fe_group
;
1770 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; group
++, i
++) {
1771 struct ext4_group_info
*grp
;
1772 struct ext4_group_desc
*desc
;
1774 if (group
== EXT4_SB(sb
)->s_groups_count
)
1777 /* quick check to skip empty groups */
1778 grp
= ext4_get_group_info(ac
->ac_sb
, group
);
1779 if (grp
->bb_free
== 0)
1783 * if the group is already init we check whether it is
1784 * a good group and if not we don't load the buddy
1786 if (EXT4_MB_GRP_NEED_INIT(grp
)) {
1788 * we need full data about the group
1789 * to make a good selection
1791 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
1794 ext4_mb_release_desc(&e4b
);
1798 * If the particular group doesn't satisfy our
1799 * criteria we continue with the next group
1801 if (!ext4_mb_good_group(ac
, group
, cr
))
1804 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
1808 ext4_lock_group(sb
, group
);
1809 if (!ext4_mb_good_group(ac
, group
, cr
)) {
1810 /* someone did allocation from this group */
1811 ext4_unlock_group(sb
, group
);
1812 ext4_mb_release_desc(&e4b
);
1816 ac
->ac_groups_scanned
++;
1817 desc
= ext4_get_group_desc(sb
, group
, NULL
);
1818 if (cr
== 0 || (desc
->bg_flags
&
1819 cpu_to_le16(EXT4_BG_BLOCK_UNINIT
) &&
1820 ac
->ac_2order
!= 0))
1821 ext4_mb_simple_scan_group(ac
, &e4b
);
1823 ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
)
1824 ext4_mb_scan_aligned(ac
, &e4b
);
1826 ext4_mb_complex_scan_group(ac
, &e4b
);
1828 ext4_unlock_group(sb
, group
);
1829 ext4_mb_release_desc(&e4b
);
1831 if (ac
->ac_status
!= AC_STATUS_CONTINUE
)
1836 if (ac
->ac_b_ex
.fe_len
> 0 && ac
->ac_status
!= AC_STATUS_FOUND
&&
1837 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1839 * We've been searching too long. Let's try to allocate
1840 * the best chunk we've found so far
1843 ext4_mb_try_best_found(ac
, &e4b
);
1844 if (ac
->ac_status
!= AC_STATUS_FOUND
) {
1846 * Someone more lucky has already allocated it.
1847 * The only thing we can do is just take first
1849 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
1851 ac
->ac_b_ex
.fe_group
= 0;
1852 ac
->ac_b_ex
.fe_start
= 0;
1853 ac
->ac_b_ex
.fe_len
= 0;
1854 ac
->ac_status
= AC_STATUS_CONTINUE
;
1855 ac
->ac_flags
|= EXT4_MB_HINT_FIRST
;
1857 atomic_inc(&sbi
->s_mb_lost_chunks
);
1865 #ifdef EXT4_MB_HISTORY
1866 struct ext4_mb_proc_session
{
1867 struct ext4_mb_history
*history
;
1868 struct super_block
*sb
;
1873 static void *ext4_mb_history_skip_empty(struct ext4_mb_proc_session
*s
,
1874 struct ext4_mb_history
*hs
,
1877 if (hs
== s
->history
+ s
->max
)
1879 if (!first
&& hs
== s
->history
+ s
->start
)
1881 while (hs
->orig
.fe_len
== 0) {
1883 if (hs
== s
->history
+ s
->max
)
1885 if (hs
== s
->history
+ s
->start
)
1891 static void *ext4_mb_seq_history_start(struct seq_file
*seq
, loff_t
*pos
)
1893 struct ext4_mb_proc_session
*s
= seq
->private;
1894 struct ext4_mb_history
*hs
;
1898 return SEQ_START_TOKEN
;
1899 hs
= ext4_mb_history_skip_empty(s
, s
->history
+ s
->start
, 1);
1902 while (--l
&& (hs
= ext4_mb_history_skip_empty(s
, ++hs
, 0)) != NULL
);
1906 static void *ext4_mb_seq_history_next(struct seq_file
*seq
, void *v
,
1909 struct ext4_mb_proc_session
*s
= seq
->private;
1910 struct ext4_mb_history
*hs
= v
;
1913 if (v
== SEQ_START_TOKEN
)
1914 return ext4_mb_history_skip_empty(s
, s
->history
+ s
->start
, 1);
1916 return ext4_mb_history_skip_empty(s
, ++hs
, 0);
1919 static int ext4_mb_seq_history_show(struct seq_file
*seq
, void *v
)
1921 char buf
[25], buf2
[25], buf3
[25], *fmt
;
1922 struct ext4_mb_history
*hs
= v
;
1924 if (v
== SEQ_START_TOKEN
) {
1925 seq_printf(seq
, "%-5s %-8s %-23s %-23s %-23s %-5s "
1926 "%-5s %-2s %-5s %-5s %-5s %-6s\n",
1927 "pid", "inode", "original", "goal", "result", "found",
1928 "grps", "cr", "flags", "merge", "tail", "broken");
1932 if (hs
->op
== EXT4_MB_HISTORY_ALLOC
) {
1933 fmt
= "%-5u %-8u %-23s %-23s %-23s %-5u %-5u %-2u "
1934 "%-5u %-5s %-5u %-6u\n";
1935 sprintf(buf2
, "%lu/%d/%u@%u", hs
->result
.fe_group
,
1936 hs
->result
.fe_start
, hs
->result
.fe_len
,
1937 hs
->result
.fe_logical
);
1938 sprintf(buf
, "%lu/%d/%u@%u", hs
->orig
.fe_group
,
1939 hs
->orig
.fe_start
, hs
->orig
.fe_len
,
1940 hs
->orig
.fe_logical
);
1941 sprintf(buf3
, "%lu/%d/%u@%u", hs
->goal
.fe_group
,
1942 hs
->goal
.fe_start
, hs
->goal
.fe_len
,
1943 hs
->goal
.fe_logical
);
1944 seq_printf(seq
, fmt
, hs
->pid
, hs
->ino
, buf
, buf3
, buf2
,
1945 hs
->found
, hs
->groups
, hs
->cr
, hs
->flags
,
1946 hs
->merged
? "M" : "", hs
->tail
,
1947 hs
->buddy
? 1 << hs
->buddy
: 0);
1948 } else if (hs
->op
== EXT4_MB_HISTORY_PREALLOC
) {
1949 fmt
= "%-5u %-8u %-23s %-23s %-23s\n";
1950 sprintf(buf2
, "%lu/%d/%u@%u", hs
->result
.fe_group
,
1951 hs
->result
.fe_start
, hs
->result
.fe_len
,
1952 hs
->result
.fe_logical
);
1953 sprintf(buf
, "%lu/%d/%u@%u", hs
->orig
.fe_group
,
1954 hs
->orig
.fe_start
, hs
->orig
.fe_len
,
1955 hs
->orig
.fe_logical
);
1956 seq_printf(seq
, fmt
, hs
->pid
, hs
->ino
, buf
, "", buf2
);
1957 } else if (hs
->op
== EXT4_MB_HISTORY_DISCARD
) {
1958 sprintf(buf2
, "%lu/%d/%u", hs
->result
.fe_group
,
1959 hs
->result
.fe_start
, hs
->result
.fe_len
);
1960 seq_printf(seq
, "%-5u %-8u %-23s discard\n",
1961 hs
->pid
, hs
->ino
, buf2
);
1962 } else if (hs
->op
== EXT4_MB_HISTORY_FREE
) {
1963 sprintf(buf2
, "%lu/%d/%u", hs
->result
.fe_group
,
1964 hs
->result
.fe_start
, hs
->result
.fe_len
);
1965 seq_printf(seq
, "%-5u %-8u %-23s free\n",
1966 hs
->pid
, hs
->ino
, buf2
);
1971 static void ext4_mb_seq_history_stop(struct seq_file
*seq
, void *v
)
1975 static struct seq_operations ext4_mb_seq_history_ops
= {
1976 .start
= ext4_mb_seq_history_start
,
1977 .next
= ext4_mb_seq_history_next
,
1978 .stop
= ext4_mb_seq_history_stop
,
1979 .show
= ext4_mb_seq_history_show
,
1982 static int ext4_mb_seq_history_open(struct inode
*inode
, struct file
*file
)
1984 struct super_block
*sb
= PDE(inode
)->data
;
1985 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1986 struct ext4_mb_proc_session
*s
;
1990 if (unlikely(sbi
->s_mb_history
== NULL
))
1992 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1996 size
= sizeof(struct ext4_mb_history
) * sbi
->s_mb_history_max
;
1997 s
->history
= kmalloc(size
, GFP_KERNEL
);
1998 if (s
->history
== NULL
) {
2003 spin_lock(&sbi
->s_mb_history_lock
);
2004 memcpy(s
->history
, sbi
->s_mb_history
, size
);
2005 s
->max
= sbi
->s_mb_history_max
;
2006 s
->start
= sbi
->s_mb_history_cur
% s
->max
;
2007 spin_unlock(&sbi
->s_mb_history_lock
);
2009 rc
= seq_open(file
, &ext4_mb_seq_history_ops
);
2011 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2021 static int ext4_mb_seq_history_release(struct inode
*inode
, struct file
*file
)
2023 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
2024 struct ext4_mb_proc_session
*s
= seq
->private;
2027 return seq_release(inode
, file
);
2030 static ssize_t
ext4_mb_seq_history_write(struct file
*file
,
2031 const char __user
*buffer
,
2032 size_t count
, loff_t
*ppos
)
2034 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
2035 struct ext4_mb_proc_session
*s
= seq
->private;
2036 struct super_block
*sb
= s
->sb
;
2040 if (count
>= sizeof(str
)) {
2041 printk(KERN_ERR
"EXT4-fs: %s string too long, max %u bytes\n",
2042 "mb_history", (int)sizeof(str
));
2046 if (copy_from_user(str
, buffer
, count
))
2049 value
= simple_strtol(str
, NULL
, 0);
2052 EXT4_SB(sb
)->s_mb_history_filter
= value
;
2057 static struct file_operations ext4_mb_seq_history_fops
= {
2058 .owner
= THIS_MODULE
,
2059 .open
= ext4_mb_seq_history_open
,
2061 .write
= ext4_mb_seq_history_write
,
2062 .llseek
= seq_lseek
,
2063 .release
= ext4_mb_seq_history_release
,
2066 static void *ext4_mb_seq_groups_start(struct seq_file
*seq
, loff_t
*pos
)
2068 struct super_block
*sb
= seq
->private;
2069 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2072 if (*pos
< 0 || *pos
>= sbi
->s_groups_count
)
2076 return (void *) group
;
2079 static void *ext4_mb_seq_groups_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2081 struct super_block
*sb
= seq
->private;
2082 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2086 if (*pos
< 0 || *pos
>= sbi
->s_groups_count
)
2089 return (void *) group
;;
2092 static int ext4_mb_seq_groups_show(struct seq_file
*seq
, void *v
)
2094 struct super_block
*sb
= seq
->private;
2095 long group
= (long) v
;
2098 struct ext4_buddy e4b
;
2100 struct ext4_group_info info
;
2101 unsigned short counters
[16];
2106 seq_printf(seq
, "#%-5s: %-5s %-5s %-5s "
2107 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2108 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2109 "group", "free", "frags", "first",
2110 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2111 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2113 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(sg
.info
.bb_counters
[0]) +
2114 sizeof(struct ext4_group_info
);
2115 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2117 seq_printf(seq
, "#%-5lu: I/O error\n", group
);
2120 ext4_lock_group(sb
, group
);
2121 memcpy(&sg
, ext4_get_group_info(sb
, group
), i
);
2122 ext4_unlock_group(sb
, group
);
2123 ext4_mb_release_desc(&e4b
);
2125 seq_printf(seq
, "#%-5lu: %-5u %-5u %-5u [", group
, sg
.info
.bb_free
,
2126 sg
.info
.bb_fragments
, sg
.info
.bb_first_free
);
2127 for (i
= 0; i
<= 13; i
++)
2128 seq_printf(seq
, " %-5u", i
<= sb
->s_blocksize_bits
+ 1 ?
2129 sg
.info
.bb_counters
[i
] : 0);
2130 seq_printf(seq
, " ]\n");
2135 static void ext4_mb_seq_groups_stop(struct seq_file
*seq
, void *v
)
2139 static struct seq_operations ext4_mb_seq_groups_ops
= {
2140 .start
= ext4_mb_seq_groups_start
,
2141 .next
= ext4_mb_seq_groups_next
,
2142 .stop
= ext4_mb_seq_groups_stop
,
2143 .show
= ext4_mb_seq_groups_show
,
2146 static int ext4_mb_seq_groups_open(struct inode
*inode
, struct file
*file
)
2148 struct super_block
*sb
= PDE(inode
)->data
;
2151 rc
= seq_open(file
, &ext4_mb_seq_groups_ops
);
2153 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2160 static struct file_operations ext4_mb_seq_groups_fops
= {
2161 .owner
= THIS_MODULE
,
2162 .open
= ext4_mb_seq_groups_open
,
2164 .llseek
= seq_lseek
,
2165 .release
= seq_release
,
2168 static void ext4_mb_history_release(struct super_block
*sb
)
2170 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2172 remove_proc_entry("mb_groups", sbi
->s_mb_proc
);
2173 remove_proc_entry("mb_history", sbi
->s_mb_proc
);
2175 kfree(sbi
->s_mb_history
);
2178 static void ext4_mb_history_init(struct super_block
*sb
)
2180 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2183 if (sbi
->s_mb_proc
!= NULL
) {
2184 proc_create_data("mb_history", S_IRUGO
, sbi
->s_mb_proc
,
2185 &ext4_mb_seq_history_fops
, sb
);
2186 proc_create_data("mb_groups", S_IRUGO
, sbi
->s_mb_proc
,
2187 &ext4_mb_seq_groups_fops
, sb
);
2190 sbi
->s_mb_history_max
= 1000;
2191 sbi
->s_mb_history_cur
= 0;
2192 spin_lock_init(&sbi
->s_mb_history_lock
);
2193 i
= sbi
->s_mb_history_max
* sizeof(struct ext4_mb_history
);
2194 sbi
->s_mb_history
= kzalloc(i
, GFP_KERNEL
);
2195 /* if we can't allocate history, then we simple won't use it */
2198 static noinline_for_stack
void
2199 ext4_mb_store_history(struct ext4_allocation_context
*ac
)
2201 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
2202 struct ext4_mb_history h
;
2204 if (unlikely(sbi
->s_mb_history
== NULL
))
2207 if (!(ac
->ac_op
& sbi
->s_mb_history_filter
))
2211 h
.pid
= current
->pid
;
2212 h
.ino
= ac
->ac_inode
? ac
->ac_inode
->i_ino
: 0;
2213 h
.orig
= ac
->ac_o_ex
;
2214 h
.result
= ac
->ac_b_ex
;
2215 h
.flags
= ac
->ac_flags
;
2216 h
.found
= ac
->ac_found
;
2217 h
.groups
= ac
->ac_groups_scanned
;
2218 h
.cr
= ac
->ac_criteria
;
2219 h
.tail
= ac
->ac_tail
;
2220 h
.buddy
= ac
->ac_buddy
;
2222 if (ac
->ac_op
== EXT4_MB_HISTORY_ALLOC
) {
2223 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
2224 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
2226 h
.goal
= ac
->ac_g_ex
;
2227 h
.result
= ac
->ac_f_ex
;
2230 spin_lock(&sbi
->s_mb_history_lock
);
2231 memcpy(sbi
->s_mb_history
+ sbi
->s_mb_history_cur
, &h
, sizeof(h
));
2232 if (++sbi
->s_mb_history_cur
>= sbi
->s_mb_history_max
)
2233 sbi
->s_mb_history_cur
= 0;
2234 spin_unlock(&sbi
->s_mb_history_lock
);
2238 #define ext4_mb_history_release(sb)
2239 #define ext4_mb_history_init(sb)
2243 /* Create and initialize ext4_group_info data for the given group. */
2244 int ext4_mb_add_groupinfo(struct super_block
*sb
, ext4_group_t group
,
2245 struct ext4_group_desc
*desc
)
2249 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2250 struct ext4_group_info
**meta_group_info
;
2253 * First check if this group is the first of a reserved block.
2254 * If it's true, we have to allocate a new table of pointers
2255 * to ext4_group_info structures
2257 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0) {
2258 metalen
= sizeof(*meta_group_info
) <<
2259 EXT4_DESC_PER_BLOCK_BITS(sb
);
2260 meta_group_info
= kmalloc(metalen
, GFP_KERNEL
);
2261 if (meta_group_info
== NULL
) {
2262 printk(KERN_ERR
"EXT4-fs: can't allocate mem for a "
2264 goto exit_meta_group_info
;
2266 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)] =
2271 * calculate needed size. if change bb_counters size,
2272 * don't forget about ext4_mb_generate_buddy()
2274 len
= offsetof(typeof(**meta_group_info
),
2275 bb_counters
[sb
->s_blocksize_bits
+ 2]);
2278 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)];
2279 i
= group
& (EXT4_DESC_PER_BLOCK(sb
) - 1);
2281 meta_group_info
[i
] = kzalloc(len
, GFP_KERNEL
);
2282 if (meta_group_info
[i
] == NULL
) {
2283 printk(KERN_ERR
"EXT4-fs: can't allocate buddy mem\n");
2284 goto exit_group_info
;
2286 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
,
2287 &(meta_group_info
[i
]->bb_state
));
2290 * initialize bb_free to be able to skip
2291 * empty groups without initialization
2293 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2294 meta_group_info
[i
]->bb_free
=
2295 ext4_free_blocks_after_init(sb
, group
, desc
);
2297 meta_group_info
[i
]->bb_free
=
2298 le16_to_cpu(desc
->bg_free_blocks_count
);
2301 INIT_LIST_HEAD(&meta_group_info
[i
]->bb_prealloc_list
);
2305 struct buffer_head
*bh
;
2306 meta_group_info
[i
]->bb_bitmap
=
2307 kmalloc(sb
->s_blocksize
, GFP_KERNEL
);
2308 BUG_ON(meta_group_info
[i
]->bb_bitmap
== NULL
);
2309 bh
= ext4_read_block_bitmap(sb
, group
);
2311 memcpy(meta_group_info
[i
]->bb_bitmap
, bh
->b_data
,
2320 /* If a meta_group_info table has been allocated, release it now */
2321 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0)
2322 kfree(sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)]);
2323 exit_meta_group_info
:
2325 } /* ext4_mb_add_groupinfo */
2328 * Add a group to the existing groups.
2329 * This function is used for online resize
2331 int ext4_mb_add_more_groupinfo(struct super_block
*sb
, ext4_group_t group
,
2332 struct ext4_group_desc
*desc
)
2334 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2335 struct inode
*inode
= sbi
->s_buddy_cache
;
2336 int blocks_per_page
;
2342 /* Add group based on group descriptor*/
2343 err
= ext4_mb_add_groupinfo(sb
, group
, desc
);
2348 * Cache pages containing dynamic mb_alloc datas (buddy and bitmap
2349 * datas) are set not up to date so that they will be re-initilaized
2350 * during the next call to ext4_mb_load_buddy
2353 /* Set buddy page as not up to date */
2354 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
2356 pnum
= block
/ blocks_per_page
;
2357 page
= find_get_page(inode
->i_mapping
, pnum
);
2359 ClearPageUptodate(page
);
2360 page_cache_release(page
);
2363 /* Set bitmap page as not up to date */
2365 pnum
= block
/ blocks_per_page
;
2366 page
= find_get_page(inode
->i_mapping
, pnum
);
2368 ClearPageUptodate(page
);
2369 page_cache_release(page
);
2376 * Update an existing group.
2377 * This function is used for online resize
2379 void ext4_mb_update_group_info(struct ext4_group_info
*grp
, ext4_grpblk_t add
)
2381 grp
->bb_free
+= add
;
2384 static int ext4_mb_init_backend(struct super_block
*sb
)
2388 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2389 struct ext4_super_block
*es
= sbi
->s_es
;
2390 int num_meta_group_infos
;
2391 int num_meta_group_infos_max
;
2393 struct ext4_group_info
**meta_group_info
;
2394 struct ext4_group_desc
*desc
;
2396 /* This is the number of blocks used by GDT */
2397 num_meta_group_infos
= (sbi
->s_groups_count
+ EXT4_DESC_PER_BLOCK(sb
) -
2398 1) >> EXT4_DESC_PER_BLOCK_BITS(sb
);
2401 * This is the total number of blocks used by GDT including
2402 * the number of reserved blocks for GDT.
2403 * The s_group_info array is allocated with this value
2404 * to allow a clean online resize without a complex
2405 * manipulation of pointer.
2406 * The drawback is the unused memory when no resize
2407 * occurs but it's very low in terms of pages
2408 * (see comments below)
2409 * Need to handle this properly when META_BG resizing is allowed
2411 num_meta_group_infos_max
= num_meta_group_infos
+
2412 le16_to_cpu(es
->s_reserved_gdt_blocks
);
2415 * array_size is the size of s_group_info array. We round it
2416 * to the next power of two because this approximation is done
2417 * internally by kmalloc so we can have some more memory
2418 * for free here (e.g. may be used for META_BG resize).
2421 while (array_size
< sizeof(*sbi
->s_group_info
) *
2422 num_meta_group_infos_max
)
2423 array_size
= array_size
<< 1;
2424 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2425 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2426 * So a two level scheme suffices for now. */
2427 sbi
->s_group_info
= kmalloc(array_size
, GFP_KERNEL
);
2428 if (sbi
->s_group_info
== NULL
) {
2429 printk(KERN_ERR
"EXT4-fs: can't allocate buddy meta group\n");
2432 sbi
->s_buddy_cache
= new_inode(sb
);
2433 if (sbi
->s_buddy_cache
== NULL
) {
2434 printk(KERN_ERR
"EXT4-fs: can't get new inode\n");
2437 EXT4_I(sbi
->s_buddy_cache
)->i_disksize
= 0;
2439 metalen
= sizeof(*meta_group_info
) << EXT4_DESC_PER_BLOCK_BITS(sb
);
2440 for (i
= 0; i
< num_meta_group_infos
; i
++) {
2441 if ((i
+ 1) == num_meta_group_infos
)
2442 metalen
= sizeof(*meta_group_info
) *
2443 (sbi
->s_groups_count
-
2444 (i
<< EXT4_DESC_PER_BLOCK_BITS(sb
)));
2445 meta_group_info
= kmalloc(metalen
, GFP_KERNEL
);
2446 if (meta_group_info
== NULL
) {
2447 printk(KERN_ERR
"EXT4-fs: can't allocate mem for a "
2451 sbi
->s_group_info
[i
] = meta_group_info
;
2454 for (i
= 0; i
< sbi
->s_groups_count
; i
++) {
2455 desc
= ext4_get_group_desc(sb
, i
, NULL
);
2458 "EXT4-fs: can't read descriptor %lu\n", i
);
2461 if (ext4_mb_add_groupinfo(sb
, i
, desc
) != 0)
2469 kfree(ext4_get_group_info(sb
, i
));
2470 i
= num_meta_group_infos
;
2473 kfree(sbi
->s_group_info
[i
]);
2474 iput(sbi
->s_buddy_cache
);
2476 kfree(sbi
->s_group_info
);
2480 int ext4_mb_init(struct super_block
*sb
, int needs_recovery
)
2482 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2488 if (!test_opt(sb
, MBALLOC
))
2491 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(unsigned short);
2493 sbi
->s_mb_offsets
= kmalloc(i
, GFP_KERNEL
);
2494 if (sbi
->s_mb_offsets
== NULL
) {
2495 clear_opt(sbi
->s_mount_opt
, MBALLOC
);
2498 sbi
->s_mb_maxs
= kmalloc(i
, GFP_KERNEL
);
2499 if (sbi
->s_mb_maxs
== NULL
) {
2500 clear_opt(sbi
->s_mount_opt
, MBALLOC
);
2501 kfree(sbi
->s_mb_maxs
);
2505 /* order 0 is regular bitmap */
2506 sbi
->s_mb_maxs
[0] = sb
->s_blocksize
<< 3;
2507 sbi
->s_mb_offsets
[0] = 0;
2511 max
= sb
->s_blocksize
<< 2;
2513 sbi
->s_mb_offsets
[i
] = offset
;
2514 sbi
->s_mb_maxs
[i
] = max
;
2515 offset
+= 1 << (sb
->s_blocksize_bits
- i
);
2518 } while (i
<= sb
->s_blocksize_bits
+ 1);
2520 /* init file for buddy data */
2521 ret
= ext4_mb_init_backend(sb
);
2523 clear_opt(sbi
->s_mount_opt
, MBALLOC
);
2524 kfree(sbi
->s_mb_offsets
);
2525 kfree(sbi
->s_mb_maxs
);
2529 spin_lock_init(&sbi
->s_md_lock
);
2530 INIT_LIST_HEAD(&sbi
->s_active_transaction
);
2531 INIT_LIST_HEAD(&sbi
->s_closed_transaction
);
2532 INIT_LIST_HEAD(&sbi
->s_committed_transaction
);
2533 spin_lock_init(&sbi
->s_bal_lock
);
2535 sbi
->s_mb_max_to_scan
= MB_DEFAULT_MAX_TO_SCAN
;
2536 sbi
->s_mb_min_to_scan
= MB_DEFAULT_MIN_TO_SCAN
;
2537 sbi
->s_mb_stats
= MB_DEFAULT_STATS
;
2538 sbi
->s_mb_stream_request
= MB_DEFAULT_STREAM_THRESHOLD
;
2539 sbi
->s_mb_order2_reqs
= MB_DEFAULT_ORDER2_REQS
;
2540 sbi
->s_mb_history_filter
= EXT4_MB_HISTORY_DEFAULT
;
2541 sbi
->s_mb_group_prealloc
= MB_DEFAULT_GROUP_PREALLOC
;
2543 i
= sizeof(struct ext4_locality_group
) * nr_cpu_ids
;
2544 sbi
->s_locality_groups
= kmalloc(i
, GFP_KERNEL
);
2545 if (sbi
->s_locality_groups
== NULL
) {
2546 clear_opt(sbi
->s_mount_opt
, MBALLOC
);
2547 kfree(sbi
->s_mb_offsets
);
2548 kfree(sbi
->s_mb_maxs
);
2551 for (i
= 0; i
< nr_cpu_ids
; i
++) {
2552 struct ext4_locality_group
*lg
;
2553 lg
= &sbi
->s_locality_groups
[i
];
2554 mutex_init(&lg
->lg_mutex
);
2555 for (j
= 0; j
< PREALLOC_TB_SIZE
; j
++)
2556 INIT_LIST_HEAD(&lg
->lg_prealloc_list
[j
]);
2557 spin_lock_init(&lg
->lg_prealloc_lock
);
2560 ext4_mb_init_per_dev_proc(sb
);
2561 ext4_mb_history_init(sb
);
2563 printk("EXT4-fs: mballoc enabled\n");
2567 /* need to called with ext4 group lock (ext4_lock_group) */
2568 static void ext4_mb_cleanup_pa(struct ext4_group_info
*grp
)
2570 struct ext4_prealloc_space
*pa
;
2571 struct list_head
*cur
, *tmp
;
2574 list_for_each_safe(cur
, tmp
, &grp
->bb_prealloc_list
) {
2575 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
2576 list_del(&pa
->pa_group_list
);
2581 mb_debug("mballoc: %u PAs left\n", count
);
2585 int ext4_mb_release(struct super_block
*sb
)
2588 int num_meta_group_infos
;
2589 struct ext4_group_info
*grinfo
;
2590 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2592 if (!test_opt(sb
, MBALLOC
))
2595 /* release freed, non-committed blocks */
2596 spin_lock(&sbi
->s_md_lock
);
2597 list_splice_init(&sbi
->s_closed_transaction
,
2598 &sbi
->s_committed_transaction
);
2599 list_splice_init(&sbi
->s_active_transaction
,
2600 &sbi
->s_committed_transaction
);
2601 spin_unlock(&sbi
->s_md_lock
);
2602 ext4_mb_free_committed_blocks(sb
);
2604 if (sbi
->s_group_info
) {
2605 for (i
= 0; i
< sbi
->s_groups_count
; i
++) {
2606 grinfo
= ext4_get_group_info(sb
, i
);
2608 kfree(grinfo
->bb_bitmap
);
2610 ext4_lock_group(sb
, i
);
2611 ext4_mb_cleanup_pa(grinfo
);
2612 ext4_unlock_group(sb
, i
);
2615 num_meta_group_infos
= (sbi
->s_groups_count
+
2616 EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2617 EXT4_DESC_PER_BLOCK_BITS(sb
);
2618 for (i
= 0; i
< num_meta_group_infos
; i
++)
2619 kfree(sbi
->s_group_info
[i
]);
2620 kfree(sbi
->s_group_info
);
2622 kfree(sbi
->s_mb_offsets
);
2623 kfree(sbi
->s_mb_maxs
);
2624 if (sbi
->s_buddy_cache
)
2625 iput(sbi
->s_buddy_cache
);
2626 if (sbi
->s_mb_stats
) {
2628 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2629 atomic_read(&sbi
->s_bal_allocated
),
2630 atomic_read(&sbi
->s_bal_reqs
),
2631 atomic_read(&sbi
->s_bal_success
));
2633 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2634 "%u 2^N hits, %u breaks, %u lost\n",
2635 atomic_read(&sbi
->s_bal_ex_scanned
),
2636 atomic_read(&sbi
->s_bal_goals
),
2637 atomic_read(&sbi
->s_bal_2orders
),
2638 atomic_read(&sbi
->s_bal_breaks
),
2639 atomic_read(&sbi
->s_mb_lost_chunks
));
2641 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2642 sbi
->s_mb_buddies_generated
++,
2643 sbi
->s_mb_generation_time
);
2645 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2646 atomic_read(&sbi
->s_mb_preallocated
),
2647 atomic_read(&sbi
->s_mb_discarded
));
2650 kfree(sbi
->s_locality_groups
);
2652 ext4_mb_history_release(sb
);
2653 ext4_mb_destroy_per_dev_proc(sb
);
2658 static noinline_for_stack
void
2659 ext4_mb_free_committed_blocks(struct super_block
*sb
)
2661 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2666 struct ext4_free_metadata
*md
;
2667 struct ext4_buddy e4b
;
2669 if (list_empty(&sbi
->s_committed_transaction
))
2672 /* there is committed blocks to be freed yet */
2674 /* get next array of blocks */
2676 spin_lock(&sbi
->s_md_lock
);
2677 if (!list_empty(&sbi
->s_committed_transaction
)) {
2678 md
= list_entry(sbi
->s_committed_transaction
.next
,
2679 struct ext4_free_metadata
, list
);
2680 list_del(&md
->list
);
2682 spin_unlock(&sbi
->s_md_lock
);
2687 mb_debug("gonna free %u blocks in group %lu (0x%p):",
2688 md
->num
, md
->group
, md
);
2690 err
= ext4_mb_load_buddy(sb
, md
->group
, &e4b
);
2691 /* we expect to find existing buddy because it's pinned */
2694 /* there are blocks to put in buddy to make them really free */
2697 ext4_lock_group(sb
, md
->group
);
2698 for (i
= 0; i
< md
->num
; i
++) {
2699 mb_debug(" %u", md
->blocks
[i
]);
2700 mb_free_blocks(NULL
, &e4b
, md
->blocks
[i
], 1);
2703 ext4_unlock_group(sb
, md
->group
);
2705 /* balance refcounts from ext4_mb_free_metadata() */
2706 page_cache_release(e4b
.bd_buddy_page
);
2707 page_cache_release(e4b
.bd_bitmap_page
);
2710 ext4_mb_release_desc(&e4b
);
2714 mb_debug("freed %u blocks in %u structures\n", count
, count2
);
2717 #define EXT4_MB_STATS_NAME "stats"
2718 #define EXT4_MB_MAX_TO_SCAN_NAME "max_to_scan"
2719 #define EXT4_MB_MIN_TO_SCAN_NAME "min_to_scan"
2720 #define EXT4_MB_ORDER2_REQ "order2_req"
2721 #define EXT4_MB_STREAM_REQ "stream_req"
2722 #define EXT4_MB_GROUP_PREALLOC "group_prealloc"
2726 #define MB_PROC_FOPS(name) \
2727 static int ext4_mb_##name##_proc_show(struct seq_file *m, void *v) \
2729 struct ext4_sb_info *sbi = m->private; \
2731 seq_printf(m, "%ld\n", sbi->s_mb_##name); \
2735 static int ext4_mb_##name##_proc_open(struct inode *inode, struct file *file)\
2737 return single_open(file, ext4_mb_##name##_proc_show, PDE(inode)->data);\
2740 static ssize_t ext4_mb_##name##_proc_write(struct file *file, \
2741 const char __user *buf, size_t cnt, loff_t *ppos) \
2743 struct ext4_sb_info *sbi = PDE(file->f_path.dentry->d_inode)->data;\
2746 if (cnt >= sizeof(str)) \
2748 if (copy_from_user(str, buf, cnt)) \
2750 value = simple_strtol(str, NULL, 0); \
2753 sbi->s_mb_##name = value; \
2757 static const struct file_operations ext4_mb_##name##_proc_fops = { \
2758 .owner = THIS_MODULE, \
2759 .open = ext4_mb_##name##_proc_open, \
2761 .llseek = seq_lseek, \
2762 .release = single_release, \
2763 .write = ext4_mb_##name##_proc_write, \
2766 MB_PROC_FOPS(stats
);
2767 MB_PROC_FOPS(max_to_scan
);
2768 MB_PROC_FOPS(min_to_scan
);
2769 MB_PROC_FOPS(order2_reqs
);
2770 MB_PROC_FOPS(stream_request
);
2771 MB_PROC_FOPS(group_prealloc
);
2773 #define MB_PROC_HANDLER(name, var) \
2775 proc = proc_create_data(name, mode, sbi->s_mb_proc, \
2776 &ext4_mb_##var##_proc_fops, sbi); \
2777 if (proc == NULL) { \
2778 printk(KERN_ERR "EXT4-fs: can't to create %s\n", name); \
2783 static int ext4_mb_init_per_dev_proc(struct super_block
*sb
)
2785 mode_t mode
= S_IFREG
| S_IRUGO
| S_IWUSR
;
2786 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2787 struct proc_dir_entry
*proc
;
2790 if (proc_root_ext4
== NULL
) {
2791 sbi
->s_mb_proc
= NULL
;
2794 bdevname(sb
->s_bdev
, devname
);
2795 sbi
->s_mb_proc
= proc_mkdir(devname
, proc_root_ext4
);
2797 MB_PROC_HANDLER(EXT4_MB_STATS_NAME
, stats
);
2798 MB_PROC_HANDLER(EXT4_MB_MAX_TO_SCAN_NAME
, max_to_scan
);
2799 MB_PROC_HANDLER(EXT4_MB_MIN_TO_SCAN_NAME
, min_to_scan
);
2800 MB_PROC_HANDLER(EXT4_MB_ORDER2_REQ
, order2_reqs
);
2801 MB_PROC_HANDLER(EXT4_MB_STREAM_REQ
, stream_request
);
2802 MB_PROC_HANDLER(EXT4_MB_GROUP_PREALLOC
, group_prealloc
);
2807 printk(KERN_ERR
"EXT4-fs: Unable to create %s\n", devname
);
2808 remove_proc_entry(EXT4_MB_GROUP_PREALLOC
, sbi
->s_mb_proc
);
2809 remove_proc_entry(EXT4_MB_STREAM_REQ
, sbi
->s_mb_proc
);
2810 remove_proc_entry(EXT4_MB_ORDER2_REQ
, sbi
->s_mb_proc
);
2811 remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME
, sbi
->s_mb_proc
);
2812 remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME
, sbi
->s_mb_proc
);
2813 remove_proc_entry(EXT4_MB_STATS_NAME
, sbi
->s_mb_proc
);
2814 remove_proc_entry(devname
, proc_root_ext4
);
2815 sbi
->s_mb_proc
= NULL
;
2820 static int ext4_mb_destroy_per_dev_proc(struct super_block
*sb
)
2822 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2825 if (sbi
->s_mb_proc
== NULL
)
2828 bdevname(sb
->s_bdev
, devname
);
2829 remove_proc_entry(EXT4_MB_GROUP_PREALLOC
, sbi
->s_mb_proc
);
2830 remove_proc_entry(EXT4_MB_STREAM_REQ
, sbi
->s_mb_proc
);
2831 remove_proc_entry(EXT4_MB_ORDER2_REQ
, sbi
->s_mb_proc
);
2832 remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME
, sbi
->s_mb_proc
);
2833 remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME
, sbi
->s_mb_proc
);
2834 remove_proc_entry(EXT4_MB_STATS_NAME
, sbi
->s_mb_proc
);
2835 remove_proc_entry(devname
, proc_root_ext4
);
2840 int __init
init_ext4_mballoc(void)
2842 ext4_pspace_cachep
=
2843 kmem_cache_create("ext4_prealloc_space",
2844 sizeof(struct ext4_prealloc_space
),
2845 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2846 if (ext4_pspace_cachep
== NULL
)
2850 kmem_cache_create("ext4_alloc_context",
2851 sizeof(struct ext4_allocation_context
),
2852 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2853 if (ext4_ac_cachep
== NULL
) {
2854 kmem_cache_destroy(ext4_pspace_cachep
);
2857 #ifdef CONFIG_PROC_FS
2858 proc_root_ext4
= proc_mkdir("fs/ext4", NULL
);
2859 if (proc_root_ext4
== NULL
)
2860 printk(KERN_ERR
"EXT4-fs: Unable to create fs/ext4\n");
2865 void exit_ext4_mballoc(void)
2867 /* XXX: synchronize_rcu(); */
2868 kmem_cache_destroy(ext4_pspace_cachep
);
2869 kmem_cache_destroy(ext4_ac_cachep
);
2870 #ifdef CONFIG_PROC_FS
2871 remove_proc_entry("fs/ext4", NULL
);
2877 * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
2878 * Returns 0 if success or error code
2880 static noinline_for_stack
int
2881 ext4_mb_mark_diskspace_used(struct ext4_allocation_context
*ac
,
2884 struct buffer_head
*bitmap_bh
= NULL
;
2885 struct ext4_super_block
*es
;
2886 struct ext4_group_desc
*gdp
;
2887 struct buffer_head
*gdp_bh
;
2888 struct ext4_sb_info
*sbi
;
2889 struct super_block
*sb
;
2893 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
2894 BUG_ON(ac
->ac_b_ex
.fe_len
<= 0);
2902 bitmap_bh
= ext4_read_block_bitmap(sb
, ac
->ac_b_ex
.fe_group
);
2906 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
2911 gdp
= ext4_get_group_desc(sb
, ac
->ac_b_ex
.fe_group
, &gdp_bh
);
2915 ext4_debug("using block group %lu(%d)\n", ac
->ac_b_ex
.fe_group
,
2916 gdp
->bg_free_blocks_count
);
2918 err
= ext4_journal_get_write_access(handle
, gdp_bh
);
2922 block
= ac
->ac_b_ex
.fe_group
* EXT4_BLOCKS_PER_GROUP(sb
)
2923 + ac
->ac_b_ex
.fe_start
2924 + le32_to_cpu(es
->s_first_data_block
);
2926 len
= ac
->ac_b_ex
.fe_len
;
2927 if (in_range(ext4_block_bitmap(sb
, gdp
), block
, len
) ||
2928 in_range(ext4_inode_bitmap(sb
, gdp
), block
, len
) ||
2929 in_range(block
, ext4_inode_table(sb
, gdp
),
2930 EXT4_SB(sb
)->s_itb_per_group
) ||
2931 in_range(block
+ len
- 1, ext4_inode_table(sb
, gdp
),
2932 EXT4_SB(sb
)->s_itb_per_group
)) {
2933 ext4_error(sb
, __func__
,
2934 "Allocating block in system zone - block = %llu",
2936 /* File system mounted not to panic on error
2937 * Fix the bitmap and repeat the block allocation
2938 * We leak some of the blocks here.
2940 mb_set_bits(sb_bgl_lock(sbi
, ac
->ac_b_ex
.fe_group
),
2941 bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,
2942 ac
->ac_b_ex
.fe_len
);
2943 err
= ext4_journal_dirty_metadata(handle
, bitmap_bh
);
2948 #ifdef AGGRESSIVE_CHECK
2951 for (i
= 0; i
< ac
->ac_b_ex
.fe_len
; i
++) {
2952 BUG_ON(mb_test_bit(ac
->ac_b_ex
.fe_start
+ i
,
2953 bitmap_bh
->b_data
));
2957 mb_set_bits(sb_bgl_lock(sbi
, ac
->ac_b_ex
.fe_group
), bitmap_bh
->b_data
,
2958 ac
->ac_b_ex
.fe_start
, ac
->ac_b_ex
.fe_len
);
2960 spin_lock(sb_bgl_lock(sbi
, ac
->ac_b_ex
.fe_group
));
2961 if (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2962 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT
);
2963 gdp
->bg_free_blocks_count
=
2964 cpu_to_le16(ext4_free_blocks_after_init(sb
,
2965 ac
->ac_b_ex
.fe_group
,
2968 le16_add_cpu(&gdp
->bg_free_blocks_count
, -ac
->ac_b_ex
.fe_len
);
2969 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, ac
->ac_b_ex
.fe_group
, gdp
);
2970 spin_unlock(sb_bgl_lock(sbi
, ac
->ac_b_ex
.fe_group
));
2973 * free blocks account has already be reduced/reserved
2974 * at write_begin() time for delayed allocation
2975 * do not double accounting
2977 if (!(ac
->ac_flags
& EXT4_MB_DELALLOC_RESERVED
))
2978 percpu_counter_sub(&sbi
->s_freeblocks_counter
,
2979 ac
->ac_b_ex
.fe_len
);
2981 if (sbi
->s_log_groups_per_flex
) {
2982 ext4_group_t flex_group
= ext4_flex_group(sbi
,
2983 ac
->ac_b_ex
.fe_group
);
2984 spin_lock(sb_bgl_lock(sbi
, flex_group
));
2985 sbi
->s_flex_groups
[flex_group
].free_blocks
-= ac
->ac_b_ex
.fe_len
;
2986 spin_unlock(sb_bgl_lock(sbi
, flex_group
));
2989 err
= ext4_journal_dirty_metadata(handle
, bitmap_bh
);
2992 err
= ext4_journal_dirty_metadata(handle
, gdp_bh
);
3001 * here we normalize request for locality group
3002 * Group request are normalized to s_strip size if we set the same via mount
3003 * option. If not we set it to s_mb_group_prealloc which can be configured via
3004 * /proc/fs/ext4/<partition>/group_prealloc
3006 * XXX: should we try to preallocate more than the group has now?
3008 static void ext4_mb_normalize_group_request(struct ext4_allocation_context
*ac
)
3010 struct super_block
*sb
= ac
->ac_sb
;
3011 struct ext4_locality_group
*lg
= ac
->ac_lg
;
3014 if (EXT4_SB(sb
)->s_stripe
)
3015 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_stripe
;
3017 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_mb_group_prealloc
;
3018 mb_debug("#%u: goal %u blocks for locality group\n",
3019 current
->pid
, ac
->ac_g_ex
.fe_len
);
3023 * Normalization means making request better in terms of
3024 * size and alignment
3026 static noinline_for_stack
void
3027 ext4_mb_normalize_request(struct ext4_allocation_context
*ac
,
3028 struct ext4_allocation_request
*ar
)
3032 loff_t size
, orig_size
, start_off
;
3033 ext4_lblk_t start
, orig_start
;
3034 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3035 struct ext4_prealloc_space
*pa
;
3037 /* do normalize only data requests, metadata requests
3038 do not need preallocation */
3039 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3042 /* sometime caller may want exact blocks */
3043 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
3046 /* caller may indicate that preallocation isn't
3047 * required (it's a tail, for example) */
3048 if (ac
->ac_flags
& EXT4_MB_HINT_NOPREALLOC
)
3051 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
) {
3052 ext4_mb_normalize_group_request(ac
);
3056 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
3058 /* first, let's learn actual file size
3059 * given current request is allocated */
3060 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
3061 size
= size
<< bsbits
;
3062 if (size
< i_size_read(ac
->ac_inode
))
3063 size
= i_size_read(ac
->ac_inode
);
3065 /* max size of free chunks */
3068 #define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3069 (req <= (size) || max <= (chunk_size))
3071 /* first, try to predict filesize */
3072 /* XXX: should this table be tunable? */
3074 if (size
<= 16 * 1024) {
3076 } else if (size
<= 32 * 1024) {
3078 } else if (size
<= 64 * 1024) {
3080 } else if (size
<= 128 * 1024) {
3082 } else if (size
<= 256 * 1024) {
3084 } else if (size
<= 512 * 1024) {
3086 } else if (size
<= 1024 * 1024) {
3088 } else if (NRL_CHECK_SIZE(size
, 4 * 1024 * 1024, max
, 2 * 1024)) {
3089 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3090 (21 - bsbits
)) << 21;
3091 size
= 2 * 1024 * 1024;
3092 } else if (NRL_CHECK_SIZE(size
, 8 * 1024 * 1024, max
, 4 * 1024)) {
3093 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3094 (22 - bsbits
)) << 22;
3095 size
= 4 * 1024 * 1024;
3096 } else if (NRL_CHECK_SIZE(ac
->ac_o_ex
.fe_len
,
3097 (8<<20)>>bsbits
, max
, 8 * 1024)) {
3098 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3099 (23 - bsbits
)) << 23;
3100 size
= 8 * 1024 * 1024;
3102 start_off
= (loff_t
)ac
->ac_o_ex
.fe_logical
<< bsbits
;
3103 size
= ac
->ac_o_ex
.fe_len
<< bsbits
;
3105 orig_size
= size
= size
>> bsbits
;
3106 orig_start
= start
= start_off
>> bsbits
;
3108 /* don't cover already allocated blocks in selected range */
3109 if (ar
->pleft
&& start
<= ar
->lleft
) {
3110 size
-= ar
->lleft
+ 1 - start
;
3111 start
= ar
->lleft
+ 1;
3113 if (ar
->pright
&& start
+ size
- 1 >= ar
->lright
)
3114 size
-= start
+ size
- ar
->lright
;
3118 /* check we don't cross already preallocated blocks */
3120 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3121 unsigned long pa_end
;
3125 spin_lock(&pa
->pa_lock
);
3126 if (pa
->pa_deleted
) {
3127 spin_unlock(&pa
->pa_lock
);
3131 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
3133 /* PA must not overlap original request */
3134 BUG_ON(!(ac
->ac_o_ex
.fe_logical
>= pa_end
||
3135 ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
));
3137 /* skip PA normalized request doesn't overlap with */
3138 if (pa
->pa_lstart
>= end
) {
3139 spin_unlock(&pa
->pa_lock
);
3142 if (pa_end
<= start
) {
3143 spin_unlock(&pa
->pa_lock
);
3146 BUG_ON(pa
->pa_lstart
<= start
&& pa_end
>= end
);
3148 if (pa_end
<= ac
->ac_o_ex
.fe_logical
) {
3149 BUG_ON(pa_end
< start
);
3153 if (pa
->pa_lstart
> ac
->ac_o_ex
.fe_logical
) {
3154 BUG_ON(pa
->pa_lstart
> end
);
3155 end
= pa
->pa_lstart
;
3157 spin_unlock(&pa
->pa_lock
);
3162 /* XXX: extra loop to check we really don't overlap preallocations */
3164 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3165 unsigned long pa_end
;
3166 spin_lock(&pa
->pa_lock
);
3167 if (pa
->pa_deleted
== 0) {
3168 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
3169 BUG_ON(!(start
>= pa_end
|| end
<= pa
->pa_lstart
));
3171 spin_unlock(&pa
->pa_lock
);
3175 if (start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3176 start
> ac
->ac_o_ex
.fe_logical
) {
3177 printk(KERN_ERR
"start %lu, size %lu, fe_logical %lu\n",
3178 (unsigned long) start
, (unsigned long) size
,
3179 (unsigned long) ac
->ac_o_ex
.fe_logical
);
3181 BUG_ON(start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3182 start
> ac
->ac_o_ex
.fe_logical
);
3183 BUG_ON(size
<= 0 || size
>= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
3185 /* now prepare goal request */
3187 /* XXX: is it better to align blocks WRT to logical
3188 * placement or satisfy big request as is */
3189 ac
->ac_g_ex
.fe_logical
= start
;
3190 ac
->ac_g_ex
.fe_len
= size
;
3192 /* define goal start in order to merge */
3193 if (ar
->pright
&& (ar
->lright
== (start
+ size
))) {
3194 /* merge to the right */
3195 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pright
- size
,
3196 &ac
->ac_f_ex
.fe_group
,
3197 &ac
->ac_f_ex
.fe_start
);
3198 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3200 if (ar
->pleft
&& (ar
->lleft
+ 1 == start
)) {
3201 /* merge to the left */
3202 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pleft
+ 1,
3203 &ac
->ac_f_ex
.fe_group
,
3204 &ac
->ac_f_ex
.fe_start
);
3205 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3208 mb_debug("goal: %u(was %u) blocks at %u\n", (unsigned) size
,
3209 (unsigned) orig_size
, (unsigned) start
);
3212 static void ext4_mb_collect_stats(struct ext4_allocation_context
*ac
)
3214 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3216 if (sbi
->s_mb_stats
&& ac
->ac_g_ex
.fe_len
> 1) {
3217 atomic_inc(&sbi
->s_bal_reqs
);
3218 atomic_add(ac
->ac_b_ex
.fe_len
, &sbi
->s_bal_allocated
);
3219 if (ac
->ac_o_ex
.fe_len
>= ac
->ac_g_ex
.fe_len
)
3220 atomic_inc(&sbi
->s_bal_success
);
3221 atomic_add(ac
->ac_found
, &sbi
->s_bal_ex_scanned
);
3222 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
3223 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
3224 atomic_inc(&sbi
->s_bal_goals
);
3225 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
)
3226 atomic_inc(&sbi
->s_bal_breaks
);
3229 ext4_mb_store_history(ac
);
3233 * use blocks preallocated to inode
3235 static void ext4_mb_use_inode_pa(struct ext4_allocation_context
*ac
,
3236 struct ext4_prealloc_space
*pa
)
3242 /* found preallocated blocks, use them */
3243 start
= pa
->pa_pstart
+ (ac
->ac_o_ex
.fe_logical
- pa
->pa_lstart
);
3244 end
= min(pa
->pa_pstart
+ pa
->pa_len
, start
+ ac
->ac_o_ex
.fe_len
);
3246 ext4_get_group_no_and_offset(ac
->ac_sb
, start
, &ac
->ac_b_ex
.fe_group
,
3247 &ac
->ac_b_ex
.fe_start
);
3248 ac
->ac_b_ex
.fe_len
= len
;
3249 ac
->ac_status
= AC_STATUS_FOUND
;
3252 BUG_ON(start
< pa
->pa_pstart
);
3253 BUG_ON(start
+ len
> pa
->pa_pstart
+ pa
->pa_len
);
3254 BUG_ON(pa
->pa_free
< len
);
3257 mb_debug("use %llu/%u from inode pa %p\n", start
, len
, pa
);
3261 * use blocks preallocated to locality group
3263 static void ext4_mb_use_group_pa(struct ext4_allocation_context
*ac
,
3264 struct ext4_prealloc_space
*pa
)
3266 unsigned int len
= ac
->ac_o_ex
.fe_len
;
3268 ext4_get_group_no_and_offset(ac
->ac_sb
, pa
->pa_pstart
,
3269 &ac
->ac_b_ex
.fe_group
,
3270 &ac
->ac_b_ex
.fe_start
);
3271 ac
->ac_b_ex
.fe_len
= len
;
3272 ac
->ac_status
= AC_STATUS_FOUND
;
3275 /* we don't correct pa_pstart or pa_plen here to avoid
3276 * possible race when the group is being loaded concurrently
3277 * instead we correct pa later, after blocks are marked
3278 * in on-disk bitmap -- see ext4_mb_release_context()
3279 * Other CPUs are prevented from allocating from this pa by lg_mutex
3281 mb_debug("use %u/%u from group pa %p\n", pa
->pa_lstart
-len
, len
, pa
);
3285 * search goal blocks in preallocated space
3287 static noinline_for_stack
int
3288 ext4_mb_use_preallocated(struct ext4_allocation_context
*ac
)
3291 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3292 struct ext4_locality_group
*lg
;
3293 struct ext4_prealloc_space
*pa
;
3295 /* only data can be preallocated */
3296 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3299 /* first, try per-file preallocation */
3301 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3303 /* all fields in this condition don't change,
3304 * so we can skip locking for them */
3305 if (ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
||
3306 ac
->ac_o_ex
.fe_logical
>= pa
->pa_lstart
+ pa
->pa_len
)
3309 /* found preallocated blocks, use them */
3310 spin_lock(&pa
->pa_lock
);
3311 if (pa
->pa_deleted
== 0 && pa
->pa_free
) {
3312 atomic_inc(&pa
->pa_count
);
3313 ext4_mb_use_inode_pa(ac
, pa
);
3314 spin_unlock(&pa
->pa_lock
);
3315 ac
->ac_criteria
= 10;
3319 spin_unlock(&pa
->pa_lock
);
3323 /* can we use group allocation? */
3324 if (!(ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
))
3327 /* inode may have no locality group for some reason */
3331 order
= fls(ac
->ac_o_ex
.fe_len
) - 1;
3332 if (order
> PREALLOC_TB_SIZE
- 1)
3333 /* The max size of hash table is PREALLOC_TB_SIZE */
3334 order
= PREALLOC_TB_SIZE
- 1;
3336 for (i
= order
; i
< PREALLOC_TB_SIZE
; i
++) {
3338 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[i
],
3340 spin_lock(&pa
->pa_lock
);
3341 if (pa
->pa_deleted
== 0 &&
3342 pa
->pa_free
>= ac
->ac_o_ex
.fe_len
) {
3343 atomic_inc(&pa
->pa_count
);
3344 ext4_mb_use_group_pa(ac
, pa
);
3345 spin_unlock(&pa
->pa_lock
);
3346 ac
->ac_criteria
= 20;
3350 spin_unlock(&pa
->pa_lock
);
3358 * the function goes through all preallocation in this group and marks them
3359 * used in in-core bitmap. buddy must be generated from this bitmap
3360 * Need to be called with ext4 group lock (ext4_lock_group)
3362 static void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
3365 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3366 struct ext4_prealloc_space
*pa
;
3367 struct list_head
*cur
;
3368 ext4_group_t groupnr
;
3369 ext4_grpblk_t start
;
3370 int preallocated
= 0;
3374 /* all form of preallocation discards first load group,
3375 * so the only competing code is preallocation use.
3376 * we don't need any locking here
3377 * notice we do NOT ignore preallocations with pa_deleted
3378 * otherwise we could leave used blocks available for
3379 * allocation in buddy when concurrent ext4_mb_put_pa()
3380 * is dropping preallocation
3382 list_for_each(cur
, &grp
->bb_prealloc_list
) {
3383 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
3384 spin_lock(&pa
->pa_lock
);
3385 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
3388 spin_unlock(&pa
->pa_lock
);
3389 if (unlikely(len
== 0))
3391 BUG_ON(groupnr
!= group
);
3392 mb_set_bits(sb_bgl_lock(EXT4_SB(sb
), group
),
3393 bitmap
, start
, len
);
3394 preallocated
+= len
;
3397 mb_debug("prellocated %u for group %lu\n", preallocated
, group
);
3400 static void ext4_mb_pa_callback(struct rcu_head
*head
)
3402 struct ext4_prealloc_space
*pa
;
3403 pa
= container_of(head
, struct ext4_prealloc_space
, u
.pa_rcu
);
3404 kmem_cache_free(ext4_pspace_cachep
, pa
);
3408 * drops a reference to preallocated space descriptor
3409 * if this was the last reference and the space is consumed
3411 static void ext4_mb_put_pa(struct ext4_allocation_context
*ac
,
3412 struct super_block
*sb
, struct ext4_prealloc_space
*pa
)
3416 if (!atomic_dec_and_test(&pa
->pa_count
) || pa
->pa_free
!= 0)
3419 /* in this short window concurrent discard can set pa_deleted */
3420 spin_lock(&pa
->pa_lock
);
3421 if (pa
->pa_deleted
== 1) {
3422 spin_unlock(&pa
->pa_lock
);
3427 spin_unlock(&pa
->pa_lock
);
3429 /* -1 is to protect from crossing allocation group */
3430 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
- 1, &grp
, NULL
);
3435 * P1 (buddy init) P2 (regular allocation)
3436 * find block B in PA
3437 * copy on-disk bitmap to buddy
3438 * mark B in on-disk bitmap
3439 * drop PA from group
3440 * mark all PAs in buddy
3442 * thus, P1 initializes buddy with B available. to prevent this
3443 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3446 ext4_lock_group(sb
, grp
);
3447 list_del(&pa
->pa_group_list
);
3448 ext4_unlock_group(sb
, grp
);
3450 spin_lock(pa
->pa_obj_lock
);
3451 list_del_rcu(&pa
->pa_inode_list
);
3452 spin_unlock(pa
->pa_obj_lock
);
3454 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3458 * creates new preallocated space for given inode
3460 static noinline_for_stack
int
3461 ext4_mb_new_inode_pa(struct ext4_allocation_context
*ac
)
3463 struct super_block
*sb
= ac
->ac_sb
;
3464 struct ext4_prealloc_space
*pa
;
3465 struct ext4_group_info
*grp
;
3466 struct ext4_inode_info
*ei
;
3468 /* preallocate only when found space is larger then requested */
3469 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3470 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3471 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3473 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3477 if (ac
->ac_b_ex
.fe_len
< ac
->ac_g_ex
.fe_len
) {
3483 /* we can't allocate as much as normalizer wants.
3484 * so, found space must get proper lstart
3485 * to cover original request */
3486 BUG_ON(ac
->ac_g_ex
.fe_logical
> ac
->ac_o_ex
.fe_logical
);
3487 BUG_ON(ac
->ac_g_ex
.fe_len
< ac
->ac_o_ex
.fe_len
);
3489 /* we're limited by original request in that
3490 * logical block must be covered any way
3491 * winl is window we can move our chunk within */
3492 winl
= ac
->ac_o_ex
.fe_logical
- ac
->ac_g_ex
.fe_logical
;
3494 /* also, we should cover whole original request */
3495 wins
= ac
->ac_b_ex
.fe_len
- ac
->ac_o_ex
.fe_len
;
3497 /* the smallest one defines real window */
3498 win
= min(winl
, wins
);
3500 offs
= ac
->ac_o_ex
.fe_logical
% ac
->ac_b_ex
.fe_len
;
3501 if (offs
&& offs
< win
)
3504 ac
->ac_b_ex
.fe_logical
= ac
->ac_o_ex
.fe_logical
- win
;
3505 BUG_ON(ac
->ac_o_ex
.fe_logical
< ac
->ac_b_ex
.fe_logical
);
3506 BUG_ON(ac
->ac_o_ex
.fe_len
> ac
->ac_b_ex
.fe_len
);
3509 /* preallocation can change ac_b_ex, thus we store actually
3510 * allocated blocks for history */
3511 ac
->ac_f_ex
= ac
->ac_b_ex
;
3513 pa
->pa_lstart
= ac
->ac_b_ex
.fe_logical
;
3514 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3515 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3516 pa
->pa_free
= pa
->pa_len
;
3517 atomic_set(&pa
->pa_count
, 1);
3518 spin_lock_init(&pa
->pa_lock
);
3522 mb_debug("new inode pa %p: %llu/%u for %u\n", pa
,
3523 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3525 ext4_mb_use_inode_pa(ac
, pa
);
3526 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3528 ei
= EXT4_I(ac
->ac_inode
);
3529 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3531 pa
->pa_obj_lock
= &ei
->i_prealloc_lock
;
3532 pa
->pa_inode
= ac
->ac_inode
;
3534 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3535 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3536 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3538 spin_lock(pa
->pa_obj_lock
);
3539 list_add_rcu(&pa
->pa_inode_list
, &ei
->i_prealloc_list
);
3540 spin_unlock(pa
->pa_obj_lock
);
3546 * creates new preallocated space for locality group inodes belongs to
3548 static noinline_for_stack
int
3549 ext4_mb_new_group_pa(struct ext4_allocation_context
*ac
)
3551 struct super_block
*sb
= ac
->ac_sb
;
3552 struct ext4_locality_group
*lg
;
3553 struct ext4_prealloc_space
*pa
;
3554 struct ext4_group_info
*grp
;
3556 /* preallocate only when found space is larger then requested */
3557 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3558 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3559 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3561 BUG_ON(ext4_pspace_cachep
== NULL
);
3562 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3566 /* preallocation can change ac_b_ex, thus we store actually
3567 * allocated blocks for history */
3568 ac
->ac_f_ex
= ac
->ac_b_ex
;
3570 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3571 pa
->pa_lstart
= pa
->pa_pstart
;
3572 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3573 pa
->pa_free
= pa
->pa_len
;
3574 atomic_set(&pa
->pa_count
, 1);
3575 spin_lock_init(&pa
->pa_lock
);
3576 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3580 mb_debug("new group pa %p: %llu/%u for %u\n", pa
,
3581 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3583 ext4_mb_use_group_pa(ac
, pa
);
3584 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3586 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3590 pa
->pa_obj_lock
= &lg
->lg_prealloc_lock
;
3591 pa
->pa_inode
= NULL
;
3593 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3594 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3595 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3598 * We will later add the new pa to the right bucket
3599 * after updating the pa_free in ext4_mb_release_context
3604 static int ext4_mb_new_preallocation(struct ext4_allocation_context
*ac
)
3608 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
3609 err
= ext4_mb_new_group_pa(ac
);
3611 err
= ext4_mb_new_inode_pa(ac
);
3616 * finds all unused blocks in on-disk bitmap, frees them in
3617 * in-core bitmap and buddy.
3618 * @pa must be unlinked from inode and group lists, so that
3619 * nobody else can find/use it.
3620 * the caller MUST hold group/inode locks.
3621 * TODO: optimize the case when there are no in-core structures yet
3623 static noinline_for_stack
int
3624 ext4_mb_release_inode_pa(struct ext4_buddy
*e4b
, struct buffer_head
*bitmap_bh
,
3625 struct ext4_prealloc_space
*pa
,
3626 struct ext4_allocation_context
*ac
)
3628 struct super_block
*sb
= e4b
->bd_sb
;
3629 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3638 BUG_ON(pa
->pa_deleted
== 0);
3639 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3640 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3641 end
= bit
+ pa
->pa_len
;
3645 ac
->ac_inode
= pa
->pa_inode
;
3646 ac
->ac_op
= EXT4_MB_HISTORY_DISCARD
;
3650 bit
= mb_find_next_zero_bit(bitmap_bh
->b_data
, end
, bit
);
3653 next
= mb_find_next_bit(bitmap_bh
->b_data
, end
, bit
);
3654 start
= group
* EXT4_BLOCKS_PER_GROUP(sb
) + bit
+
3655 le32_to_cpu(sbi
->s_es
->s_first_data_block
);
3656 mb_debug(" free preallocated %u/%u in group %u\n",
3657 (unsigned) start
, (unsigned) next
- bit
,
3662 ac
->ac_b_ex
.fe_group
= group
;
3663 ac
->ac_b_ex
.fe_start
= bit
;
3664 ac
->ac_b_ex
.fe_len
= next
- bit
;
3665 ac
->ac_b_ex
.fe_logical
= 0;
3666 ext4_mb_store_history(ac
);
3669 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, next
- bit
);
3672 if (free
!= pa
->pa_free
) {
3673 printk(KERN_CRIT
"pa %p: logic %lu, phys. %lu, len %lu\n",
3674 pa
, (unsigned long) pa
->pa_lstart
,
3675 (unsigned long) pa
->pa_pstart
,
3676 (unsigned long) pa
->pa_len
);
3677 ext4_error(sb
, __func__
, "free %u, pa_free %u\n",
3680 * pa is already deleted so we use the value obtained
3681 * from the bitmap and continue.
3684 atomic_add(free
, &sbi
->s_mb_discarded
);
3689 static noinline_for_stack
int
3690 ext4_mb_release_group_pa(struct ext4_buddy
*e4b
,
3691 struct ext4_prealloc_space
*pa
,
3692 struct ext4_allocation_context
*ac
)
3694 struct super_block
*sb
= e4b
->bd_sb
;
3699 ac
->ac_op
= EXT4_MB_HISTORY_DISCARD
;
3701 BUG_ON(pa
->pa_deleted
== 0);
3702 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3703 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3704 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, pa
->pa_len
);
3705 atomic_add(pa
->pa_len
, &EXT4_SB(sb
)->s_mb_discarded
);
3709 ac
->ac_inode
= NULL
;
3710 ac
->ac_b_ex
.fe_group
= group
;
3711 ac
->ac_b_ex
.fe_start
= bit
;
3712 ac
->ac_b_ex
.fe_len
= pa
->pa_len
;
3713 ac
->ac_b_ex
.fe_logical
= 0;
3714 ext4_mb_store_history(ac
);
3721 * releases all preallocations in given group
3723 * first, we need to decide discard policy:
3724 * - when do we discard
3726 * - how many do we discard
3727 * 1) how many requested
3729 static noinline_for_stack
int
3730 ext4_mb_discard_group_preallocations(struct super_block
*sb
,
3731 ext4_group_t group
, int needed
)
3733 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3734 struct buffer_head
*bitmap_bh
= NULL
;
3735 struct ext4_prealloc_space
*pa
, *tmp
;
3736 struct ext4_allocation_context
*ac
;
3737 struct list_head list
;
3738 struct ext4_buddy e4b
;
3743 mb_debug("discard preallocation for group %lu\n", group
);
3745 if (list_empty(&grp
->bb_prealloc_list
))
3748 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
3749 if (bitmap_bh
== NULL
) {
3750 ext4_error(sb
, __func__
, "Error in reading block "
3751 "bitmap for %lu\n", group
);
3755 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
3757 ext4_error(sb
, __func__
, "Error in loading buddy "
3758 "information for %lu\n", group
);
3764 needed
= EXT4_BLOCKS_PER_GROUP(sb
) + 1;
3766 INIT_LIST_HEAD(&list
);
3767 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
3769 ext4_lock_group(sb
, group
);
3770 list_for_each_entry_safe(pa
, tmp
,
3771 &grp
->bb_prealloc_list
, pa_group_list
) {
3772 spin_lock(&pa
->pa_lock
);
3773 if (atomic_read(&pa
->pa_count
)) {
3774 spin_unlock(&pa
->pa_lock
);
3778 if (pa
->pa_deleted
) {
3779 spin_unlock(&pa
->pa_lock
);
3783 /* seems this one can be freed ... */
3786 /* we can trust pa_free ... */
3787 free
+= pa
->pa_free
;
3789 spin_unlock(&pa
->pa_lock
);
3791 list_del(&pa
->pa_group_list
);
3792 list_add(&pa
->u
.pa_tmp_list
, &list
);
3795 /* if we still need more blocks and some PAs were used, try again */
3796 if (free
< needed
&& busy
) {
3798 ext4_unlock_group(sb
, group
);
3800 * Yield the CPU here so that we don't get soft lockup
3801 * in non preempt case.
3807 /* found anything to free? */
3808 if (list_empty(&list
)) {
3813 /* now free all selected PAs */
3814 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
3816 /* remove from object (inode or locality group) */
3817 spin_lock(pa
->pa_obj_lock
);
3818 list_del_rcu(&pa
->pa_inode_list
);
3819 spin_unlock(pa
->pa_obj_lock
);
3822 ext4_mb_release_group_pa(&e4b
, pa
, ac
);
3824 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
, ac
);
3826 list_del(&pa
->u
.pa_tmp_list
);
3827 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3831 ext4_unlock_group(sb
, group
);
3833 kmem_cache_free(ext4_ac_cachep
, ac
);
3834 ext4_mb_release_desc(&e4b
);
3840 * releases all non-used preallocated blocks for given inode
3842 * It's important to discard preallocations under i_data_sem
3843 * We don't want another block to be served from the prealloc
3844 * space when we are discarding the inode prealloc space.
3846 * FIXME!! Make sure it is valid at all the call sites
3848 void ext4_mb_discard_inode_preallocations(struct inode
*inode
)
3850 struct ext4_inode_info
*ei
= EXT4_I(inode
);
3851 struct super_block
*sb
= inode
->i_sb
;
3852 struct buffer_head
*bitmap_bh
= NULL
;
3853 struct ext4_prealloc_space
*pa
, *tmp
;
3854 struct ext4_allocation_context
*ac
;
3855 ext4_group_t group
= 0;
3856 struct list_head list
;
3857 struct ext4_buddy e4b
;
3860 if (!test_opt(sb
, MBALLOC
) || !S_ISREG(inode
->i_mode
)) {
3861 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3865 mb_debug("discard preallocation for inode %lu\n", inode
->i_ino
);
3867 INIT_LIST_HEAD(&list
);
3869 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
3871 /* first, collect all pa's in the inode */
3872 spin_lock(&ei
->i_prealloc_lock
);
3873 while (!list_empty(&ei
->i_prealloc_list
)) {
3874 pa
= list_entry(ei
->i_prealloc_list
.next
,
3875 struct ext4_prealloc_space
, pa_inode_list
);
3876 BUG_ON(pa
->pa_obj_lock
!= &ei
->i_prealloc_lock
);
3877 spin_lock(&pa
->pa_lock
);
3878 if (atomic_read(&pa
->pa_count
)) {
3879 /* this shouldn't happen often - nobody should
3880 * use preallocation while we're discarding it */
3881 spin_unlock(&pa
->pa_lock
);
3882 spin_unlock(&ei
->i_prealloc_lock
);
3883 printk(KERN_ERR
"uh-oh! used pa while discarding\n");
3885 schedule_timeout_uninterruptible(HZ
);
3889 if (pa
->pa_deleted
== 0) {
3891 spin_unlock(&pa
->pa_lock
);
3892 list_del_rcu(&pa
->pa_inode_list
);
3893 list_add(&pa
->u
.pa_tmp_list
, &list
);
3897 /* someone is deleting pa right now */
3898 spin_unlock(&pa
->pa_lock
);
3899 spin_unlock(&ei
->i_prealloc_lock
);
3901 /* we have to wait here because pa_deleted
3902 * doesn't mean pa is already unlinked from
3903 * the list. as we might be called from
3904 * ->clear_inode() the inode will get freed
3905 * and concurrent thread which is unlinking
3906 * pa from inode's list may access already
3907 * freed memory, bad-bad-bad */
3909 /* XXX: if this happens too often, we can
3910 * add a flag to force wait only in case
3911 * of ->clear_inode(), but not in case of
3912 * regular truncate */
3913 schedule_timeout_uninterruptible(HZ
);
3916 spin_unlock(&ei
->i_prealloc_lock
);
3918 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
3919 BUG_ON(pa
->pa_linear
!= 0);
3920 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
3922 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
3924 ext4_error(sb
, __func__
, "Error in loading buddy "
3925 "information for %lu\n", group
);
3929 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
3930 if (bitmap_bh
== NULL
) {
3931 ext4_error(sb
, __func__
, "Error in reading block "
3932 "bitmap for %lu\n", group
);
3933 ext4_mb_release_desc(&e4b
);
3937 ext4_lock_group(sb
, group
);
3938 list_del(&pa
->pa_group_list
);
3939 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
, ac
);
3940 ext4_unlock_group(sb
, group
);
3942 ext4_mb_release_desc(&e4b
);
3945 list_del(&pa
->u
.pa_tmp_list
);
3946 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3949 kmem_cache_free(ext4_ac_cachep
, ac
);
3953 * finds all preallocated spaces and return blocks being freed to them
3954 * if preallocated space becomes full (no block is used from the space)
3955 * then the function frees space in buddy
3956 * XXX: at the moment, truncate (which is the only way to free blocks)
3957 * discards all preallocations
3959 static void ext4_mb_return_to_preallocation(struct inode
*inode
,
3960 struct ext4_buddy
*e4b
,
3961 sector_t block
, int count
)
3963 BUG_ON(!list_empty(&EXT4_I(inode
)->i_prealloc_list
));
3966 static void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
3968 struct super_block
*sb
= ac
->ac_sb
;
3971 printk(KERN_ERR
"EXT4-fs: Can't allocate:"
3972 " Allocation context details:\n");
3973 printk(KERN_ERR
"EXT4-fs: status %d flags %d\n",
3974 ac
->ac_status
, ac
->ac_flags
);
3975 printk(KERN_ERR
"EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
3976 "best %lu/%lu/%lu@%lu cr %d\n",
3977 (unsigned long)ac
->ac_o_ex
.fe_group
,
3978 (unsigned long)ac
->ac_o_ex
.fe_start
,
3979 (unsigned long)ac
->ac_o_ex
.fe_len
,
3980 (unsigned long)ac
->ac_o_ex
.fe_logical
,
3981 (unsigned long)ac
->ac_g_ex
.fe_group
,
3982 (unsigned long)ac
->ac_g_ex
.fe_start
,
3983 (unsigned long)ac
->ac_g_ex
.fe_len
,
3984 (unsigned long)ac
->ac_g_ex
.fe_logical
,
3985 (unsigned long)ac
->ac_b_ex
.fe_group
,
3986 (unsigned long)ac
->ac_b_ex
.fe_start
,
3987 (unsigned long)ac
->ac_b_ex
.fe_len
,
3988 (unsigned long)ac
->ac_b_ex
.fe_logical
,
3989 (int)ac
->ac_criteria
);
3990 printk(KERN_ERR
"EXT4-fs: %lu scanned, %d found\n", ac
->ac_ex_scanned
,
3992 printk(KERN_ERR
"EXT4-fs: groups: \n");
3993 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; i
++) {
3994 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, i
);
3995 struct ext4_prealloc_space
*pa
;
3996 ext4_grpblk_t start
;
3997 struct list_head
*cur
;
3998 ext4_lock_group(sb
, i
);
3999 list_for_each(cur
, &grp
->bb_prealloc_list
) {
4000 pa
= list_entry(cur
, struct ext4_prealloc_space
,
4002 spin_lock(&pa
->pa_lock
);
4003 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
4005 spin_unlock(&pa
->pa_lock
);
4006 printk(KERN_ERR
"PA:%lu:%d:%u \n", i
,
4009 ext4_unlock_group(sb
, i
);
4011 if (grp
->bb_free
== 0)
4013 printk(KERN_ERR
"%lu: %d/%d \n",
4014 i
, grp
->bb_free
, grp
->bb_fragments
);
4016 printk(KERN_ERR
"\n");
4019 static inline void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
4026 * We use locality group preallocation for small size file. The size of the
4027 * file is determined by the current size or the resulting size after
4028 * allocation which ever is larger
4030 * One can tune this size via /proc/fs/ext4/<partition>/stream_req
4032 static void ext4_mb_group_or_file(struct ext4_allocation_context
*ac
)
4034 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
4035 int bsbits
= ac
->ac_sb
->s_blocksize_bits
;
4038 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
4041 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
4042 isize
= i_size_read(ac
->ac_inode
) >> bsbits
;
4043 size
= max(size
, isize
);
4045 /* don't use group allocation for large files */
4046 if (size
>= sbi
->s_mb_stream_request
)
4049 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
4052 BUG_ON(ac
->ac_lg
!= NULL
);
4054 * locality group prealloc space are per cpu. The reason for having
4055 * per cpu locality group is to reduce the contention between block
4056 * request from multiple CPUs.
4058 ac
->ac_lg
= &sbi
->s_locality_groups
[get_cpu()];
4061 /* we're going to use group allocation */
4062 ac
->ac_flags
|= EXT4_MB_HINT_GROUP_ALLOC
;
4064 /* serialize all allocations in the group */
4065 mutex_lock(&ac
->ac_lg
->lg_mutex
);
4068 static noinline_for_stack
int
4069 ext4_mb_initialize_context(struct ext4_allocation_context
*ac
,
4070 struct ext4_allocation_request
*ar
)
4072 struct super_block
*sb
= ar
->inode
->i_sb
;
4073 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4074 struct ext4_super_block
*es
= sbi
->s_es
;
4078 ext4_grpblk_t block
;
4080 /* we can't allocate > group size */
4083 /* just a dirty hack to filter too big requests */
4084 if (len
>= EXT4_BLOCKS_PER_GROUP(sb
) - 10)
4085 len
= EXT4_BLOCKS_PER_GROUP(sb
) - 10;
4087 /* start searching from the goal */
4089 if (goal
< le32_to_cpu(es
->s_first_data_block
) ||
4090 goal
>= ext4_blocks_count(es
))
4091 goal
= le32_to_cpu(es
->s_first_data_block
);
4092 ext4_get_group_no_and_offset(sb
, goal
, &group
, &block
);
4094 /* set up allocation goals */
4095 ac
->ac_b_ex
.fe_logical
= ar
->logical
;
4096 ac
->ac_b_ex
.fe_group
= 0;
4097 ac
->ac_b_ex
.fe_start
= 0;
4098 ac
->ac_b_ex
.fe_len
= 0;
4099 ac
->ac_status
= AC_STATUS_CONTINUE
;
4100 ac
->ac_groups_scanned
= 0;
4101 ac
->ac_ex_scanned
= 0;
4104 ac
->ac_inode
= ar
->inode
;
4105 ac
->ac_o_ex
.fe_logical
= ar
->logical
;
4106 ac
->ac_o_ex
.fe_group
= group
;
4107 ac
->ac_o_ex
.fe_start
= block
;
4108 ac
->ac_o_ex
.fe_len
= len
;
4109 ac
->ac_g_ex
.fe_logical
= ar
->logical
;
4110 ac
->ac_g_ex
.fe_group
= group
;
4111 ac
->ac_g_ex
.fe_start
= block
;
4112 ac
->ac_g_ex
.fe_len
= len
;
4113 ac
->ac_f_ex
.fe_len
= 0;
4114 ac
->ac_flags
= ar
->flags
;
4116 ac
->ac_criteria
= 0;
4118 ac
->ac_bitmap_page
= NULL
;
4119 ac
->ac_buddy_page
= NULL
;
4122 /* we have to define context: we'll we work with a file or
4123 * locality group. this is a policy, actually */
4124 ext4_mb_group_or_file(ac
);
4126 mb_debug("init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
4127 "left: %u/%u, right %u/%u to %swritable\n",
4128 (unsigned) ar
->len
, (unsigned) ar
->logical
,
4129 (unsigned) ar
->goal
, ac
->ac_flags
, ac
->ac_2order
,
4130 (unsigned) ar
->lleft
, (unsigned) ar
->pleft
,
4131 (unsigned) ar
->lright
, (unsigned) ar
->pright
,
4132 atomic_read(&ar
->inode
->i_writecount
) ? "" : "non-");
4137 static noinline_for_stack
void
4138 ext4_mb_discard_lg_preallocations(struct super_block
*sb
,
4139 struct ext4_locality_group
*lg
,
4140 int order
, int total_entries
)
4142 ext4_group_t group
= 0;
4143 struct ext4_buddy e4b
;
4144 struct list_head discard_list
;
4145 struct ext4_prealloc_space
*pa
, *tmp
;
4146 struct ext4_allocation_context
*ac
;
4148 mb_debug("discard locality group preallocation\n");
4150 INIT_LIST_HEAD(&discard_list
);
4151 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4153 spin_lock(&lg
->lg_prealloc_lock
);
4154 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[order
],
4156 spin_lock(&pa
->pa_lock
);
4157 if (atomic_read(&pa
->pa_count
)) {
4159 * This is the pa that we just used
4160 * for block allocation. So don't
4163 spin_unlock(&pa
->pa_lock
);
4166 if (pa
->pa_deleted
) {
4167 spin_unlock(&pa
->pa_lock
);
4170 /* only lg prealloc space */
4171 BUG_ON(!pa
->pa_linear
);
4173 /* seems this one can be freed ... */
4175 spin_unlock(&pa
->pa_lock
);
4177 list_del_rcu(&pa
->pa_inode_list
);
4178 list_add(&pa
->u
.pa_tmp_list
, &discard_list
);
4181 if (total_entries
<= 5) {
4183 * we want to keep only 5 entries
4184 * allowing it to grow to 8. This
4185 * mak sure we don't call discard
4186 * soon for this list.
4191 spin_unlock(&lg
->lg_prealloc_lock
);
4193 list_for_each_entry_safe(pa
, tmp
, &discard_list
, u
.pa_tmp_list
) {
4195 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
4196 if (ext4_mb_load_buddy(sb
, group
, &e4b
)) {
4197 ext4_error(sb
, __func__
, "Error in loading buddy "
4198 "information for %lu\n", group
);
4201 ext4_lock_group(sb
, group
);
4202 list_del(&pa
->pa_group_list
);
4203 ext4_mb_release_group_pa(&e4b
, pa
, ac
);
4204 ext4_unlock_group(sb
, group
);
4206 ext4_mb_release_desc(&e4b
);
4207 list_del(&pa
->u
.pa_tmp_list
);
4208 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4211 kmem_cache_free(ext4_ac_cachep
, ac
);
4215 * We have incremented pa_count. So it cannot be freed at this
4216 * point. Also we hold lg_mutex. So no parallel allocation is
4217 * possible from this lg. That means pa_free cannot be updated.
4219 * A parallel ext4_mb_discard_group_preallocations is possible.
4220 * which can cause the lg_prealloc_list to be updated.
4223 static void ext4_mb_add_n_trim(struct ext4_allocation_context
*ac
)
4225 int order
, added
= 0, lg_prealloc_count
= 1;
4226 struct super_block
*sb
= ac
->ac_sb
;
4227 struct ext4_locality_group
*lg
= ac
->ac_lg
;
4228 struct ext4_prealloc_space
*tmp_pa
, *pa
= ac
->ac_pa
;
4230 order
= fls(pa
->pa_free
) - 1;
4231 if (order
> PREALLOC_TB_SIZE
- 1)
4232 /* The max size of hash table is PREALLOC_TB_SIZE */
4233 order
= PREALLOC_TB_SIZE
- 1;
4234 /* Add the prealloc space to lg */
4236 list_for_each_entry_rcu(tmp_pa
, &lg
->lg_prealloc_list
[order
],
4238 spin_lock(&tmp_pa
->pa_lock
);
4239 if (tmp_pa
->pa_deleted
) {
4240 spin_unlock(&pa
->pa_lock
);
4243 if (!added
&& pa
->pa_free
< tmp_pa
->pa_free
) {
4244 /* Add to the tail of the previous entry */
4245 list_add_tail_rcu(&pa
->pa_inode_list
,
4246 &tmp_pa
->pa_inode_list
);
4249 * we want to count the total
4250 * number of entries in the list
4253 spin_unlock(&tmp_pa
->pa_lock
);
4254 lg_prealloc_count
++;
4257 list_add_tail_rcu(&pa
->pa_inode_list
,
4258 &lg
->lg_prealloc_list
[order
]);
4261 /* Now trim the list to be not more than 8 elements */
4262 if (lg_prealloc_count
> 8) {
4263 ext4_mb_discard_lg_preallocations(sb
, lg
,
4264 order
, lg_prealloc_count
);
4271 * release all resource we used in allocation
4273 static int ext4_mb_release_context(struct ext4_allocation_context
*ac
)
4275 struct ext4_prealloc_space
*pa
= ac
->ac_pa
;
4277 if (pa
->pa_linear
) {
4278 /* see comment in ext4_mb_use_group_pa() */
4279 spin_lock(&pa
->pa_lock
);
4280 pa
->pa_pstart
+= ac
->ac_b_ex
.fe_len
;
4281 pa
->pa_lstart
+= ac
->ac_b_ex
.fe_len
;
4282 pa
->pa_free
-= ac
->ac_b_ex
.fe_len
;
4283 pa
->pa_len
-= ac
->ac_b_ex
.fe_len
;
4284 spin_unlock(&pa
->pa_lock
);
4286 * We want to add the pa to the right bucket.
4287 * Remove it from the list and while adding
4288 * make sure the list to which we are adding
4291 if (likely(pa
->pa_free
)) {
4292 spin_lock(pa
->pa_obj_lock
);
4293 list_del_rcu(&pa
->pa_inode_list
);
4294 spin_unlock(pa
->pa_obj_lock
);
4295 ext4_mb_add_n_trim(ac
);
4298 ext4_mb_put_pa(ac
, ac
->ac_sb
, pa
);
4300 if (ac
->ac_bitmap_page
)
4301 page_cache_release(ac
->ac_bitmap_page
);
4302 if (ac
->ac_buddy_page
)
4303 page_cache_release(ac
->ac_buddy_page
);
4304 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
4305 mutex_unlock(&ac
->ac_lg
->lg_mutex
);
4306 ext4_mb_collect_stats(ac
);
4310 static int ext4_mb_discard_preallocations(struct super_block
*sb
, int needed
)
4316 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
&& needed
> 0; i
++) {
4317 ret
= ext4_mb_discard_group_preallocations(sb
, i
, needed
);
4326 * Main entry point into mballoc to allocate blocks
4327 * it tries to use preallocation first, then falls back
4328 * to usual allocation
4330 ext4_fsblk_t
ext4_mb_new_blocks(handle_t
*handle
,
4331 struct ext4_allocation_request
*ar
, int *errp
)
4333 struct ext4_allocation_context
*ac
= NULL
;
4334 struct ext4_sb_info
*sbi
;
4335 struct super_block
*sb
;
4336 ext4_fsblk_t block
= 0;
4340 sb
= ar
->inode
->i_sb
;
4343 if (!test_opt(sb
, MBALLOC
)) {
4344 block
= ext4_old_new_blocks(handle
, ar
->inode
, ar
->goal
,
4348 if (!EXT4_I(ar
->inode
)->i_delalloc_reserved_flag
) {
4350 * With delalloc we already reserved the blocks
4352 ar
->len
= ext4_has_free_blocks(sbi
, ar
->len
);
4360 while (ar
->len
&& DQUOT_ALLOC_BLOCK(ar
->inode
, ar
->len
)) {
4361 ar
->flags
|= EXT4_MB_HINT_NOPREALLOC
;
4370 if (EXT4_I(ar
->inode
)->i_delalloc_reserved_flag
)
4371 ar
->flags
|= EXT4_MB_DELALLOC_RESERVED
;
4373 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4380 ext4_mb_poll_new_transaction(sb
, handle
);
4382 *errp
= ext4_mb_initialize_context(ac
, ar
);
4388 ac
->ac_op
= EXT4_MB_HISTORY_PREALLOC
;
4389 if (!ext4_mb_use_preallocated(ac
)) {
4390 ac
->ac_op
= EXT4_MB_HISTORY_ALLOC
;
4391 ext4_mb_normalize_request(ac
, ar
);
4393 /* allocate space in core */
4394 ext4_mb_regular_allocator(ac
);
4396 /* as we've just preallocated more space than
4397 * user requested orinally, we store allocated
4398 * space in a special descriptor */
4399 if (ac
->ac_status
== AC_STATUS_FOUND
&&
4400 ac
->ac_o_ex
.fe_len
< ac
->ac_b_ex
.fe_len
)
4401 ext4_mb_new_preallocation(ac
);
4404 if (likely(ac
->ac_status
== AC_STATUS_FOUND
)) {
4405 *errp
= ext4_mb_mark_diskspace_used(ac
, handle
);
4406 if (*errp
== -EAGAIN
) {
4407 ac
->ac_b_ex
.fe_group
= 0;
4408 ac
->ac_b_ex
.fe_start
= 0;
4409 ac
->ac_b_ex
.fe_len
= 0;
4410 ac
->ac_status
= AC_STATUS_CONTINUE
;
4413 ac
->ac_b_ex
.fe_len
= 0;
4415 ext4_mb_show_ac(ac
);
4417 block
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
4418 ar
->len
= ac
->ac_b_ex
.fe_len
;
4421 freed
= ext4_mb_discard_preallocations(sb
, ac
->ac_o_ex
.fe_len
);
4425 ac
->ac_b_ex
.fe_len
= 0;
4427 ext4_mb_show_ac(ac
);
4430 ext4_mb_release_context(ac
);
4433 kmem_cache_free(ext4_ac_cachep
, ac
);
4435 if (ar
->len
< inquota
)
4436 DQUOT_FREE_BLOCK(ar
->inode
, inquota
- ar
->len
);
4440 static void ext4_mb_poll_new_transaction(struct super_block
*sb
,
4443 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4445 if (sbi
->s_last_transaction
== handle
->h_transaction
->t_tid
)
4448 /* new transaction! time to close last one and free blocks for
4449 * committed transaction. we know that only transaction can be
4450 * active, so previos transaction can be being logged and we
4451 * know that transaction before previous is known to be already
4452 * logged. this means that now we may free blocks freed in all
4453 * transactions before previous one. hope I'm clear enough ... */
4455 spin_lock(&sbi
->s_md_lock
);
4456 if (sbi
->s_last_transaction
!= handle
->h_transaction
->t_tid
) {
4457 mb_debug("new transaction %lu, old %lu\n",
4458 (unsigned long) handle
->h_transaction
->t_tid
,
4459 (unsigned long) sbi
->s_last_transaction
);
4460 list_splice_init(&sbi
->s_closed_transaction
,
4461 &sbi
->s_committed_transaction
);
4462 list_splice_init(&sbi
->s_active_transaction
,
4463 &sbi
->s_closed_transaction
);
4464 sbi
->s_last_transaction
= handle
->h_transaction
->t_tid
;
4466 spin_unlock(&sbi
->s_md_lock
);
4468 ext4_mb_free_committed_blocks(sb
);
4471 static noinline_for_stack
int
4472 ext4_mb_free_metadata(handle_t
*handle
, struct ext4_buddy
*e4b
,
4473 ext4_group_t group
, ext4_grpblk_t block
, int count
)
4475 struct ext4_group_info
*db
= e4b
->bd_info
;
4476 struct super_block
*sb
= e4b
->bd_sb
;
4477 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4478 struct ext4_free_metadata
*md
;
4481 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
4482 BUG_ON(e4b
->bd_buddy_page
== NULL
);
4484 ext4_lock_group(sb
, group
);
4485 for (i
= 0; i
< count
; i
++) {
4487 if (md
&& db
->bb_tid
!= handle
->h_transaction
->t_tid
) {
4488 db
->bb_md_cur
= NULL
;
4493 ext4_unlock_group(sb
, group
);
4494 md
= kmalloc(sizeof(*md
), GFP_NOFS
);
4500 ext4_lock_group(sb
, group
);
4501 if (db
->bb_md_cur
== NULL
) {
4502 spin_lock(&sbi
->s_md_lock
);
4503 list_add(&md
->list
, &sbi
->s_active_transaction
);
4504 spin_unlock(&sbi
->s_md_lock
);
4505 /* protect buddy cache from being freed,
4506 * otherwise we'll refresh it from
4507 * on-disk bitmap and lose not-yet-available
4509 page_cache_get(e4b
->bd_buddy_page
);
4510 page_cache_get(e4b
->bd_bitmap_page
);
4512 db
->bb_tid
= handle
->h_transaction
->t_tid
;
4513 mb_debug("new md 0x%p for group %lu\n",
4521 BUG_ON(md
->num
>= EXT4_BB_MAX_BLOCKS
);
4522 md
->blocks
[md
->num
] = block
+ i
;
4524 if (md
->num
== EXT4_BB_MAX_BLOCKS
) {
4525 /* no more space, put full container on a sb's list */
4526 db
->bb_md_cur
= NULL
;
4529 ext4_unlock_group(sb
, group
);
4534 * Main entry point into mballoc to free blocks
4536 void ext4_mb_free_blocks(handle_t
*handle
, struct inode
*inode
,
4537 unsigned long block
, unsigned long count
,
4538 int metadata
, unsigned long *freed
)
4540 struct buffer_head
*bitmap_bh
= NULL
;
4541 struct super_block
*sb
= inode
->i_sb
;
4542 struct ext4_allocation_context
*ac
= NULL
;
4543 struct ext4_group_desc
*gdp
;
4544 struct ext4_super_block
*es
;
4545 unsigned long overflow
;
4547 struct buffer_head
*gd_bh
;
4548 ext4_group_t block_group
;
4549 struct ext4_sb_info
*sbi
;
4550 struct ext4_buddy e4b
;
4556 ext4_mb_poll_new_transaction(sb
, handle
);
4559 es
= EXT4_SB(sb
)->s_es
;
4560 if (block
< le32_to_cpu(es
->s_first_data_block
) ||
4561 block
+ count
< block
||
4562 block
+ count
> ext4_blocks_count(es
)) {
4563 ext4_error(sb
, __func__
,
4564 "Freeing blocks not in datazone - "
4565 "block = %lu, count = %lu", block
, count
);
4569 ext4_debug("freeing block %lu\n", block
);
4571 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4573 ac
->ac_op
= EXT4_MB_HISTORY_FREE
;
4574 ac
->ac_inode
= inode
;
4580 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
4583 * Check to see if we are freeing blocks across a group
4586 if (bit
+ count
> EXT4_BLOCKS_PER_GROUP(sb
)) {
4587 overflow
= bit
+ count
- EXT4_BLOCKS_PER_GROUP(sb
);
4590 bitmap_bh
= ext4_read_block_bitmap(sb
, block_group
);
4595 gdp
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
4601 if (in_range(ext4_block_bitmap(sb
, gdp
), block
, count
) ||
4602 in_range(ext4_inode_bitmap(sb
, gdp
), block
, count
) ||
4603 in_range(block
, ext4_inode_table(sb
, gdp
),
4604 EXT4_SB(sb
)->s_itb_per_group
) ||
4605 in_range(block
+ count
- 1, ext4_inode_table(sb
, gdp
),
4606 EXT4_SB(sb
)->s_itb_per_group
)) {
4608 ext4_error(sb
, __func__
,
4609 "Freeing blocks in system zone - "
4610 "Block = %lu, count = %lu", block
, count
);
4611 /* err = 0. ext4_std_error should be a no op */
4615 BUFFER_TRACE(bitmap_bh
, "getting write access");
4616 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
4621 * We are about to modify some metadata. Call the journal APIs
4622 * to unshare ->b_data if a currently-committing transaction is
4625 BUFFER_TRACE(gd_bh
, "get_write_access");
4626 err
= ext4_journal_get_write_access(handle
, gd_bh
);
4630 err
= ext4_mb_load_buddy(sb
, block_group
, &e4b
);
4634 #ifdef AGGRESSIVE_CHECK
4637 for (i
= 0; i
< count
; i
++)
4638 BUG_ON(!mb_test_bit(bit
+ i
, bitmap_bh
->b_data
));
4641 mb_clear_bits(sb_bgl_lock(sbi
, block_group
), bitmap_bh
->b_data
,
4644 /* We dirtied the bitmap block */
4645 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
4646 err
= ext4_journal_dirty_metadata(handle
, bitmap_bh
);
4649 ac
->ac_b_ex
.fe_group
= block_group
;
4650 ac
->ac_b_ex
.fe_start
= bit
;
4651 ac
->ac_b_ex
.fe_len
= count
;
4652 ext4_mb_store_history(ac
);
4656 /* blocks being freed are metadata. these blocks shouldn't
4657 * be used until this transaction is committed */
4658 ext4_mb_free_metadata(handle
, &e4b
, block_group
, bit
, count
);
4660 ext4_lock_group(sb
, block_group
);
4661 mb_free_blocks(inode
, &e4b
, bit
, count
);
4662 ext4_mb_return_to_preallocation(inode
, &e4b
, block
, count
);
4663 ext4_unlock_group(sb
, block_group
);
4666 spin_lock(sb_bgl_lock(sbi
, block_group
));
4667 le16_add_cpu(&gdp
->bg_free_blocks_count
, count
);
4668 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, block_group
, gdp
);
4669 spin_unlock(sb_bgl_lock(sbi
, block_group
));
4670 percpu_counter_add(&sbi
->s_freeblocks_counter
, count
);
4672 if (sbi
->s_log_groups_per_flex
) {
4673 ext4_group_t flex_group
= ext4_flex_group(sbi
, block_group
);
4674 spin_lock(sb_bgl_lock(sbi
, flex_group
));
4675 sbi
->s_flex_groups
[flex_group
].free_blocks
+= count
;
4676 spin_unlock(sb_bgl_lock(sbi
, flex_group
));
4679 ext4_mb_release_desc(&e4b
);
4683 /* And the group descriptor block */
4684 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
4685 ret
= ext4_journal_dirty_metadata(handle
, gd_bh
);
4689 if (overflow
&& !err
) {
4698 ext4_std_error(sb
, err
);
4700 kmem_cache_free(ext4_ac_cachep
, ac
);