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 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
791 ext4_init_block_bitmap(sb
, bh
[i
],
792 first_group
+ i
, desc
);
793 set_buffer_uptodate(bh
[i
]);
794 unlock_buffer(bh
[i
]);
798 bh
[i
]->b_end_io
= end_buffer_read_sync
;
799 submit_bh(READ
, bh
[i
]);
800 mb_debug("read bitmap for group %lu\n", first_group
+ i
);
803 /* wait for I/O completion */
804 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
805 wait_on_buffer(bh
[i
]);
808 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
809 if (!buffer_uptodate(bh
[i
]))
813 first_block
= page
->index
* blocks_per_page
;
814 for (i
= 0; i
< blocks_per_page
; i
++) {
816 struct ext4_group_info
*grinfo
;
818 group
= (first_block
+ i
) >> 1;
819 if (group
>= EXT4_SB(sb
)->s_groups_count
)
823 * data carry information regarding this
824 * particular group in the format specified
828 data
= page_address(page
) + (i
* blocksize
);
829 bitmap
= bh
[group
- first_group
]->b_data
;
832 * We place the buddy block and bitmap block
835 if ((first_block
+ i
) & 1) {
836 /* this is block of buddy */
837 BUG_ON(incore
== NULL
);
838 mb_debug("put buddy for group %u in page %lu/%x\n",
839 group
, page
->index
, i
* blocksize
);
840 memset(data
, 0xff, blocksize
);
841 grinfo
= ext4_get_group_info(sb
, group
);
842 grinfo
->bb_fragments
= 0;
843 memset(grinfo
->bb_counters
, 0,
844 sizeof(unsigned short)*(sb
->s_blocksize_bits
+2));
846 * incore got set to the group block bitmap below
848 ext4_mb_generate_buddy(sb
, data
, incore
, group
);
851 /* this is block of bitmap */
852 BUG_ON(incore
!= NULL
);
853 mb_debug("put bitmap for group %u in page %lu/%x\n",
854 group
, page
->index
, i
* blocksize
);
856 /* see comments in ext4_mb_put_pa() */
857 ext4_lock_group(sb
, group
);
858 memcpy(data
, bitmap
, blocksize
);
860 /* mark all preallocated blks used in in-core bitmap */
861 ext4_mb_generate_from_pa(sb
, data
, group
);
862 ext4_unlock_group(sb
, group
);
864 /* set incore so that the buddy information can be
865 * generated using this
870 SetPageUptodate(page
);
874 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
882 static noinline_for_stack
int
883 ext4_mb_load_buddy(struct super_block
*sb
, ext4_group_t group
,
884 struct ext4_buddy
*e4b
)
886 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
887 struct inode
*inode
= sbi
->s_buddy_cache
;
895 mb_debug("load group %lu\n", group
);
897 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
899 e4b
->bd_blkbits
= sb
->s_blocksize_bits
;
900 e4b
->bd_info
= ext4_get_group_info(sb
, group
);
902 e4b
->bd_group
= group
;
903 e4b
->bd_buddy_page
= NULL
;
904 e4b
->bd_bitmap_page
= NULL
;
907 * the buddy cache inode stores the block bitmap
908 * and buddy information in consecutive blocks.
909 * So for each group we need two blocks.
912 pnum
= block
/ blocks_per_page
;
913 poff
= block
% blocks_per_page
;
915 /* we could use find_or_create_page(), but it locks page
916 * what we'd like to avoid in fast path ... */
917 page
= find_get_page(inode
->i_mapping
, pnum
);
918 if (page
== NULL
|| !PageUptodate(page
)) {
920 page_cache_release(page
);
921 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
923 BUG_ON(page
->mapping
!= inode
->i_mapping
);
924 if (!PageUptodate(page
)) {
925 ret
= ext4_mb_init_cache(page
, NULL
);
930 mb_cmp_bitmaps(e4b
, page_address(page
) +
931 (poff
* sb
->s_blocksize
));
936 if (page
== NULL
|| !PageUptodate(page
)) {
940 e4b
->bd_bitmap_page
= page
;
941 e4b
->bd_bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
942 mark_page_accessed(page
);
945 pnum
= block
/ blocks_per_page
;
946 poff
= block
% blocks_per_page
;
948 page
= find_get_page(inode
->i_mapping
, pnum
);
949 if (page
== NULL
|| !PageUptodate(page
)) {
951 page_cache_release(page
);
952 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
954 BUG_ON(page
->mapping
!= inode
->i_mapping
);
955 if (!PageUptodate(page
)) {
956 ret
= ext4_mb_init_cache(page
, e4b
->bd_bitmap
);
965 if (page
== NULL
|| !PageUptodate(page
)) {
969 e4b
->bd_buddy_page
= page
;
970 e4b
->bd_buddy
= page_address(page
) + (poff
* sb
->s_blocksize
);
971 mark_page_accessed(page
);
973 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
974 BUG_ON(e4b
->bd_buddy_page
== NULL
);
979 if (e4b
->bd_bitmap_page
)
980 page_cache_release(e4b
->bd_bitmap_page
);
981 if (e4b
->bd_buddy_page
)
982 page_cache_release(e4b
->bd_buddy_page
);
983 e4b
->bd_buddy
= NULL
;
984 e4b
->bd_bitmap
= NULL
;
988 static void ext4_mb_release_desc(struct ext4_buddy
*e4b
)
990 if (e4b
->bd_bitmap_page
)
991 page_cache_release(e4b
->bd_bitmap_page
);
992 if (e4b
->bd_buddy_page
)
993 page_cache_release(e4b
->bd_buddy_page
);
997 static int mb_find_order_for_block(struct ext4_buddy
*e4b
, int block
)
1002 BUG_ON(EXT4_MB_BITMAP(e4b
) == EXT4_MB_BUDDY(e4b
));
1003 BUG_ON(block
>= (1 << (e4b
->bd_blkbits
+ 3)));
1005 bb
= EXT4_MB_BUDDY(e4b
);
1006 while (order
<= e4b
->bd_blkbits
+ 1) {
1008 if (!mb_test_bit(block
, bb
)) {
1009 /* this block is part of buddy of order 'order' */
1012 bb
+= 1 << (e4b
->bd_blkbits
- order
);
1018 static void mb_clear_bits(spinlock_t
*lock
, void *bm
, int cur
, int len
)
1024 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1025 /* fast path: clear whole word at once */
1026 addr
= bm
+ (cur
>> 3);
1031 mb_clear_bit_atomic(lock
, cur
, bm
);
1036 static void mb_set_bits(spinlock_t
*lock
, void *bm
, int cur
, int len
)
1042 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1043 /* fast path: set whole word at once */
1044 addr
= bm
+ (cur
>> 3);
1049 mb_set_bit_atomic(lock
, cur
, bm
);
1054 static void mb_free_blocks(struct inode
*inode
, struct ext4_buddy
*e4b
,
1055 int first
, int count
)
1062 struct super_block
*sb
= e4b
->bd_sb
;
1064 BUG_ON(first
+ count
> (sb
->s_blocksize
<< 3));
1065 BUG_ON(!ext4_is_group_locked(sb
, e4b
->bd_group
));
1066 mb_check_buddy(e4b
);
1067 mb_free_blocks_double(inode
, e4b
, first
, count
);
1069 e4b
->bd_info
->bb_free
+= count
;
1070 if (first
< e4b
->bd_info
->bb_first_free
)
1071 e4b
->bd_info
->bb_first_free
= first
;
1073 /* let's maintain fragments counter */
1075 block
= !mb_test_bit(first
- 1, EXT4_MB_BITMAP(e4b
));
1076 if (first
+ count
< EXT4_SB(sb
)->s_mb_maxs
[0])
1077 max
= !mb_test_bit(first
+ count
, EXT4_MB_BITMAP(e4b
));
1079 e4b
->bd_info
->bb_fragments
--;
1080 else if (!block
&& !max
)
1081 e4b
->bd_info
->bb_fragments
++;
1083 /* let's maintain buddy itself */
1084 while (count
-- > 0) {
1088 if (!mb_test_bit(block
, EXT4_MB_BITMAP(e4b
))) {
1089 ext4_fsblk_t blocknr
;
1090 blocknr
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
);
1093 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
1094 ext4_unlock_group(sb
, e4b
->bd_group
);
1095 ext4_error(sb
, __func__
, "double-free of inode"
1096 " %lu's block %llu(bit %u in group %lu)\n",
1097 inode
? inode
->i_ino
: 0, blocknr
, block
,
1099 ext4_lock_group(sb
, e4b
->bd_group
);
1101 mb_clear_bit(block
, EXT4_MB_BITMAP(e4b
));
1102 e4b
->bd_info
->bb_counters
[order
]++;
1104 /* start of the buddy */
1105 buddy
= mb_find_buddy(e4b
, order
, &max
);
1109 if (mb_test_bit(block
, buddy
) ||
1110 mb_test_bit(block
+ 1, buddy
))
1113 /* both the buddies are free, try to coalesce them */
1114 buddy2
= mb_find_buddy(e4b
, order
+ 1, &max
);
1120 /* for special purposes, we don't set
1121 * free bits in bitmap */
1122 mb_set_bit(block
, buddy
);
1123 mb_set_bit(block
+ 1, buddy
);
1125 e4b
->bd_info
->bb_counters
[order
]--;
1126 e4b
->bd_info
->bb_counters
[order
]--;
1130 e4b
->bd_info
->bb_counters
[order
]++;
1132 mb_clear_bit(block
, buddy2
);
1136 mb_check_buddy(e4b
);
1139 static int mb_find_extent(struct ext4_buddy
*e4b
, int order
, int block
,
1140 int needed
, struct ext4_free_extent
*ex
)
1147 BUG_ON(!ext4_is_group_locked(e4b
->bd_sb
, e4b
->bd_group
));
1150 buddy
= mb_find_buddy(e4b
, order
, &max
);
1151 BUG_ON(buddy
== NULL
);
1152 BUG_ON(block
>= max
);
1153 if (mb_test_bit(block
, buddy
)) {
1160 /* FIXME dorp order completely ? */
1161 if (likely(order
== 0)) {
1162 /* find actual order */
1163 order
= mb_find_order_for_block(e4b
, block
);
1164 block
= block
>> order
;
1167 ex
->fe_len
= 1 << order
;
1168 ex
->fe_start
= block
<< order
;
1169 ex
->fe_group
= e4b
->bd_group
;
1171 /* calc difference from given start */
1172 next
= next
- ex
->fe_start
;
1174 ex
->fe_start
+= next
;
1176 while (needed
> ex
->fe_len
&&
1177 (buddy
= mb_find_buddy(e4b
, order
, &max
))) {
1179 if (block
+ 1 >= max
)
1182 next
= (block
+ 1) * (1 << order
);
1183 if (mb_test_bit(next
, EXT4_MB_BITMAP(e4b
)))
1186 ord
= mb_find_order_for_block(e4b
, next
);
1189 block
= next
>> order
;
1190 ex
->fe_len
+= 1 << order
;
1193 BUG_ON(ex
->fe_start
+ ex
->fe_len
> (1 << (e4b
->bd_blkbits
+ 3)));
1197 static int mb_mark_used(struct ext4_buddy
*e4b
, struct ext4_free_extent
*ex
)
1203 int start
= ex
->fe_start
;
1204 int len
= ex
->fe_len
;
1209 BUG_ON(start
+ len
> (e4b
->bd_sb
->s_blocksize
<< 3));
1210 BUG_ON(e4b
->bd_group
!= ex
->fe_group
);
1211 BUG_ON(!ext4_is_group_locked(e4b
->bd_sb
, e4b
->bd_group
));
1212 mb_check_buddy(e4b
);
1213 mb_mark_used_double(e4b
, start
, len
);
1215 e4b
->bd_info
->bb_free
-= len
;
1216 if (e4b
->bd_info
->bb_first_free
== start
)
1217 e4b
->bd_info
->bb_first_free
+= len
;
1219 /* let's maintain fragments counter */
1221 mlen
= !mb_test_bit(start
- 1, EXT4_MB_BITMAP(e4b
));
1222 if (start
+ len
< EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[0])
1223 max
= !mb_test_bit(start
+ len
, EXT4_MB_BITMAP(e4b
));
1225 e4b
->bd_info
->bb_fragments
++;
1226 else if (!mlen
&& !max
)
1227 e4b
->bd_info
->bb_fragments
--;
1229 /* let's maintain buddy itself */
1231 ord
= mb_find_order_for_block(e4b
, start
);
1233 if (((start
>> ord
) << ord
) == start
&& len
>= (1 << ord
)) {
1234 /* the whole chunk may be allocated at once! */
1236 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1237 BUG_ON((start
>> ord
) >= max
);
1238 mb_set_bit(start
>> ord
, buddy
);
1239 e4b
->bd_info
->bb_counters
[ord
]--;
1246 /* store for history */
1248 ret
= len
| (ord
<< 16);
1250 /* we have to split large buddy */
1252 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1253 mb_set_bit(start
>> ord
, buddy
);
1254 e4b
->bd_info
->bb_counters
[ord
]--;
1257 cur
= (start
>> ord
) & ~1U;
1258 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1259 mb_clear_bit(cur
, buddy
);
1260 mb_clear_bit(cur
+ 1, buddy
);
1261 e4b
->bd_info
->bb_counters
[ord
]++;
1262 e4b
->bd_info
->bb_counters
[ord
]++;
1265 mb_set_bits(sb_bgl_lock(EXT4_SB(e4b
->bd_sb
), ex
->fe_group
),
1266 EXT4_MB_BITMAP(e4b
), ex
->fe_start
, len0
);
1267 mb_check_buddy(e4b
);
1273 * Must be called under group lock!
1275 static void ext4_mb_use_best_found(struct ext4_allocation_context
*ac
,
1276 struct ext4_buddy
*e4b
)
1278 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1281 BUG_ON(ac
->ac_b_ex
.fe_group
!= e4b
->bd_group
);
1282 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1284 ac
->ac_b_ex
.fe_len
= min(ac
->ac_b_ex
.fe_len
, ac
->ac_g_ex
.fe_len
);
1285 ac
->ac_b_ex
.fe_logical
= ac
->ac_g_ex
.fe_logical
;
1286 ret
= mb_mark_used(e4b
, &ac
->ac_b_ex
);
1288 /* preallocation can change ac_b_ex, thus we store actually
1289 * allocated blocks for history */
1290 ac
->ac_f_ex
= ac
->ac_b_ex
;
1292 ac
->ac_status
= AC_STATUS_FOUND
;
1293 ac
->ac_tail
= ret
& 0xffff;
1294 ac
->ac_buddy
= ret
>> 16;
1296 /* XXXXXXX: SUCH A HORRIBLE **CK */
1298 ac
->ac_bitmap_page
= e4b
->bd_bitmap_page
;
1299 get_page(ac
->ac_bitmap_page
);
1300 ac
->ac_buddy_page
= e4b
->bd_buddy_page
;
1301 get_page(ac
->ac_buddy_page
);
1303 /* store last allocated for subsequent stream allocation */
1304 if ((ac
->ac_flags
& EXT4_MB_HINT_DATA
)) {
1305 spin_lock(&sbi
->s_md_lock
);
1306 sbi
->s_mb_last_group
= ac
->ac_f_ex
.fe_group
;
1307 sbi
->s_mb_last_start
= ac
->ac_f_ex
.fe_start
;
1308 spin_unlock(&sbi
->s_md_lock
);
1313 * regular allocator, for general purposes allocation
1316 static void ext4_mb_check_limits(struct ext4_allocation_context
*ac
,
1317 struct ext4_buddy
*e4b
,
1320 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1321 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1322 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1323 struct ext4_free_extent ex
;
1327 * We don't want to scan for a whole year
1329 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
&&
1330 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1331 ac
->ac_status
= AC_STATUS_BREAK
;
1336 * Haven't found good chunk so far, let's continue
1338 if (bex
->fe_len
< gex
->fe_len
)
1341 if ((finish_group
|| ac
->ac_found
> sbi
->s_mb_min_to_scan
)
1342 && bex
->fe_group
== e4b
->bd_group
) {
1343 /* recheck chunk's availability - we don't know
1344 * when it was found (within this lock-unlock
1346 max
= mb_find_extent(e4b
, 0, bex
->fe_start
, gex
->fe_len
, &ex
);
1347 if (max
>= gex
->fe_len
) {
1348 ext4_mb_use_best_found(ac
, e4b
);
1355 * The routine checks whether found extent is good enough. If it is,
1356 * then the extent gets marked used and flag is set to the context
1357 * to stop scanning. Otherwise, the extent is compared with the
1358 * previous found extent and if new one is better, then it's stored
1359 * in the context. Later, the best found extent will be used, if
1360 * mballoc can't find good enough extent.
1362 * FIXME: real allocation policy is to be designed yet!
1364 static void ext4_mb_measure_extent(struct ext4_allocation_context
*ac
,
1365 struct ext4_free_extent
*ex
,
1366 struct ext4_buddy
*e4b
)
1368 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1369 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1371 BUG_ON(ex
->fe_len
<= 0);
1372 BUG_ON(ex
->fe_len
>= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
1373 BUG_ON(ex
->fe_start
>= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
1374 BUG_ON(ac
->ac_status
!= AC_STATUS_CONTINUE
);
1379 * The special case - take what you catch first
1381 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1383 ext4_mb_use_best_found(ac
, e4b
);
1388 * Let's check whether the chuck is good enough
1390 if (ex
->fe_len
== gex
->fe_len
) {
1392 ext4_mb_use_best_found(ac
, e4b
);
1397 * If this is first found extent, just store it in the context
1399 if (bex
->fe_len
== 0) {
1405 * If new found extent is better, store it in the context
1407 if (bex
->fe_len
< gex
->fe_len
) {
1408 /* if the request isn't satisfied, any found extent
1409 * larger than previous best one is better */
1410 if (ex
->fe_len
> bex
->fe_len
)
1412 } else if (ex
->fe_len
> gex
->fe_len
) {
1413 /* if the request is satisfied, then we try to find
1414 * an extent that still satisfy the request, but is
1415 * smaller than previous one */
1416 if (ex
->fe_len
< bex
->fe_len
)
1420 ext4_mb_check_limits(ac
, e4b
, 0);
1423 static int ext4_mb_try_best_found(struct ext4_allocation_context
*ac
,
1424 struct ext4_buddy
*e4b
)
1426 struct ext4_free_extent ex
= ac
->ac_b_ex
;
1427 ext4_group_t group
= ex
.fe_group
;
1431 BUG_ON(ex
.fe_len
<= 0);
1432 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1436 ext4_lock_group(ac
->ac_sb
, group
);
1437 max
= mb_find_extent(e4b
, 0, ex
.fe_start
, ex
.fe_len
, &ex
);
1441 ext4_mb_use_best_found(ac
, e4b
);
1444 ext4_unlock_group(ac
->ac_sb
, group
);
1445 ext4_mb_release_desc(e4b
);
1450 static int ext4_mb_find_by_goal(struct ext4_allocation_context
*ac
,
1451 struct ext4_buddy
*e4b
)
1453 ext4_group_t group
= ac
->ac_g_ex
.fe_group
;
1456 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1457 struct ext4_super_block
*es
= sbi
->s_es
;
1458 struct ext4_free_extent ex
;
1460 if (!(ac
->ac_flags
& EXT4_MB_HINT_TRY_GOAL
))
1463 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1467 ext4_lock_group(ac
->ac_sb
, group
);
1468 max
= mb_find_extent(e4b
, 0, ac
->ac_g_ex
.fe_start
,
1469 ac
->ac_g_ex
.fe_len
, &ex
);
1471 if (max
>= ac
->ac_g_ex
.fe_len
&& ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
) {
1474 start
= (e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
)) +
1475 ex
.fe_start
+ le32_to_cpu(es
->s_first_data_block
);
1476 /* use do_div to get remainder (would be 64-bit modulo) */
1477 if (do_div(start
, sbi
->s_stripe
) == 0) {
1480 ext4_mb_use_best_found(ac
, e4b
);
1482 } else if (max
>= ac
->ac_g_ex
.fe_len
) {
1483 BUG_ON(ex
.fe_len
<= 0);
1484 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1485 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1488 ext4_mb_use_best_found(ac
, e4b
);
1489 } else if (max
> 0 && (ac
->ac_flags
& EXT4_MB_HINT_MERGE
)) {
1490 /* Sometimes, caller may want to merge even small
1491 * number of blocks to an existing extent */
1492 BUG_ON(ex
.fe_len
<= 0);
1493 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1494 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1497 ext4_mb_use_best_found(ac
, e4b
);
1499 ext4_unlock_group(ac
->ac_sb
, group
);
1500 ext4_mb_release_desc(e4b
);
1506 * The routine scans buddy structures (not bitmap!) from given order
1507 * to max order and tries to find big enough chunk to satisfy the req
1509 static void ext4_mb_simple_scan_group(struct ext4_allocation_context
*ac
,
1510 struct ext4_buddy
*e4b
)
1512 struct super_block
*sb
= ac
->ac_sb
;
1513 struct ext4_group_info
*grp
= e4b
->bd_info
;
1519 BUG_ON(ac
->ac_2order
<= 0);
1520 for (i
= ac
->ac_2order
; i
<= sb
->s_blocksize_bits
+ 1; i
++) {
1521 if (grp
->bb_counters
[i
] == 0)
1524 buddy
= mb_find_buddy(e4b
, i
, &max
);
1525 BUG_ON(buddy
== NULL
);
1527 k
= mb_find_next_zero_bit(buddy
, max
, 0);
1532 ac
->ac_b_ex
.fe_len
= 1 << i
;
1533 ac
->ac_b_ex
.fe_start
= k
<< i
;
1534 ac
->ac_b_ex
.fe_group
= e4b
->bd_group
;
1536 ext4_mb_use_best_found(ac
, e4b
);
1538 BUG_ON(ac
->ac_b_ex
.fe_len
!= ac
->ac_g_ex
.fe_len
);
1540 if (EXT4_SB(sb
)->s_mb_stats
)
1541 atomic_inc(&EXT4_SB(sb
)->s_bal_2orders
);
1548 * The routine scans the group and measures all found extents.
1549 * In order to optimize scanning, caller must pass number of
1550 * free blocks in the group, so the routine can know upper limit.
1552 static void ext4_mb_complex_scan_group(struct ext4_allocation_context
*ac
,
1553 struct ext4_buddy
*e4b
)
1555 struct super_block
*sb
= ac
->ac_sb
;
1556 void *bitmap
= EXT4_MB_BITMAP(e4b
);
1557 struct ext4_free_extent ex
;
1561 free
= e4b
->bd_info
->bb_free
;
1564 i
= e4b
->bd_info
->bb_first_free
;
1566 while (free
&& ac
->ac_status
== AC_STATUS_CONTINUE
) {
1567 i
= mb_find_next_zero_bit(bitmap
,
1568 EXT4_BLOCKS_PER_GROUP(sb
), i
);
1569 if (i
>= EXT4_BLOCKS_PER_GROUP(sb
)) {
1571 * IF we have corrupt bitmap, we won't find any
1572 * free blocks even though group info says we
1573 * we have free blocks
1575 ext4_error(sb
, __func__
, "%d free blocks as per "
1576 "group info. But bitmap says 0\n",
1581 mb_find_extent(e4b
, 0, i
, ac
->ac_g_ex
.fe_len
, &ex
);
1582 BUG_ON(ex
.fe_len
<= 0);
1583 if (free
< ex
.fe_len
) {
1584 ext4_error(sb
, __func__
, "%d free blocks as per "
1585 "group info. But got %d blocks\n",
1588 * The number of free blocks differs. This mostly
1589 * indicate that the bitmap is corrupt. So exit
1590 * without claiming the space.
1595 ext4_mb_measure_extent(ac
, &ex
, e4b
);
1601 ext4_mb_check_limits(ac
, e4b
, 1);
1605 * This is a special case for storages like raid5
1606 * we try to find stripe-aligned chunks for stripe-size requests
1607 * XXX should do so at least for multiples of stripe size as well
1609 static void ext4_mb_scan_aligned(struct ext4_allocation_context
*ac
,
1610 struct ext4_buddy
*e4b
)
1612 struct super_block
*sb
= ac
->ac_sb
;
1613 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1614 void *bitmap
= EXT4_MB_BITMAP(e4b
);
1615 struct ext4_free_extent ex
;
1616 ext4_fsblk_t first_group_block
;
1621 BUG_ON(sbi
->s_stripe
== 0);
1623 /* find first stripe-aligned block in group */
1624 first_group_block
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
)
1625 + le32_to_cpu(sbi
->s_es
->s_first_data_block
);
1626 a
= first_group_block
+ sbi
->s_stripe
- 1;
1627 do_div(a
, sbi
->s_stripe
);
1628 i
= (a
* sbi
->s_stripe
) - first_group_block
;
1630 while (i
< EXT4_BLOCKS_PER_GROUP(sb
)) {
1631 if (!mb_test_bit(i
, bitmap
)) {
1632 max
= mb_find_extent(e4b
, 0, i
, sbi
->s_stripe
, &ex
);
1633 if (max
>= sbi
->s_stripe
) {
1636 ext4_mb_use_best_found(ac
, e4b
);
1644 static int ext4_mb_good_group(struct ext4_allocation_context
*ac
,
1645 ext4_group_t group
, int cr
)
1647 unsigned free
, fragments
;
1649 struct ext4_group_desc
*desc
;
1650 struct ext4_group_info
*grp
= ext4_get_group_info(ac
->ac_sb
, group
);
1652 BUG_ON(cr
< 0 || cr
>= 4);
1653 BUG_ON(EXT4_MB_GRP_NEED_INIT(grp
));
1655 free
= grp
->bb_free
;
1656 fragments
= grp
->bb_fragments
;
1664 BUG_ON(ac
->ac_2order
== 0);
1665 /* If this group is uninitialized, skip it initially */
1666 desc
= ext4_get_group_desc(ac
->ac_sb
, group
, NULL
);
1667 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
))
1670 bits
= ac
->ac_sb
->s_blocksize_bits
+ 1;
1671 for (i
= ac
->ac_2order
; i
<= bits
; i
++)
1672 if (grp
->bb_counters
[i
] > 0)
1676 if ((free
/ fragments
) >= ac
->ac_g_ex
.fe_len
)
1680 if (free
>= ac
->ac_g_ex
.fe_len
)
1692 static noinline_for_stack
int
1693 ext4_mb_regular_allocator(struct ext4_allocation_context
*ac
)
1700 struct ext4_sb_info
*sbi
;
1701 struct super_block
*sb
;
1702 struct ext4_buddy e4b
;
1707 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1709 /* first, try the goal */
1710 err
= ext4_mb_find_by_goal(ac
, &e4b
);
1711 if (err
|| ac
->ac_status
== AC_STATUS_FOUND
)
1714 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
1718 * ac->ac2_order is set only if the fe_len is a power of 2
1719 * if ac2_order is set we also set criteria to 0 so that we
1720 * try exact allocation using buddy.
1722 i
= fls(ac
->ac_g_ex
.fe_len
);
1725 * We search using buddy data only if the order of the request
1726 * is greater than equal to the sbi_s_mb_order2_reqs
1727 * You can tune it via /proc/fs/ext4/<partition>/order2_req
1729 if (i
>= sbi
->s_mb_order2_reqs
) {
1731 * This should tell if fe_len is exactly power of 2
1733 if ((ac
->ac_g_ex
.fe_len
& (~(1 << (i
- 1)))) == 0)
1734 ac
->ac_2order
= i
- 1;
1737 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
1738 /* if stream allocation is enabled, use global goal */
1739 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
1740 isize
= i_size_read(ac
->ac_inode
) >> bsbits
;
1744 if (size
< sbi
->s_mb_stream_request
&&
1745 (ac
->ac_flags
& EXT4_MB_HINT_DATA
)) {
1746 /* TBD: may be hot point */
1747 spin_lock(&sbi
->s_md_lock
);
1748 ac
->ac_g_ex
.fe_group
= sbi
->s_mb_last_group
;
1749 ac
->ac_g_ex
.fe_start
= sbi
->s_mb_last_start
;
1750 spin_unlock(&sbi
->s_md_lock
);
1752 /* Let's just scan groups to find more-less suitable blocks */
1753 cr
= ac
->ac_2order
? 0 : 1;
1755 * cr == 0 try to get exact allocation,
1756 * cr == 3 try to get anything
1759 for (; cr
< 4 && ac
->ac_status
== AC_STATUS_CONTINUE
; cr
++) {
1760 ac
->ac_criteria
= cr
;
1762 * searching for the right group start
1763 * from the goal value specified
1765 group
= ac
->ac_g_ex
.fe_group
;
1767 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; group
++, i
++) {
1768 struct ext4_group_info
*grp
;
1769 struct ext4_group_desc
*desc
;
1771 if (group
== EXT4_SB(sb
)->s_groups_count
)
1774 /* quick check to skip empty groups */
1775 grp
= ext4_get_group_info(ac
->ac_sb
, group
);
1776 if (grp
->bb_free
== 0)
1780 * if the group is already init we check whether it is
1781 * a good group and if not we don't load the buddy
1783 if (EXT4_MB_GRP_NEED_INIT(grp
)) {
1785 * we need full data about the group
1786 * to make a good selection
1788 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
1791 ext4_mb_release_desc(&e4b
);
1795 * If the particular group doesn't satisfy our
1796 * criteria we continue with the next group
1798 if (!ext4_mb_good_group(ac
, group
, cr
))
1801 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
1805 ext4_lock_group(sb
, group
);
1806 if (!ext4_mb_good_group(ac
, group
, cr
)) {
1807 /* someone did allocation from this group */
1808 ext4_unlock_group(sb
, group
);
1809 ext4_mb_release_desc(&e4b
);
1813 ac
->ac_groups_scanned
++;
1814 desc
= ext4_get_group_desc(sb
, group
, NULL
);
1815 if (cr
== 0 || (desc
->bg_flags
&
1816 cpu_to_le16(EXT4_BG_BLOCK_UNINIT
) &&
1817 ac
->ac_2order
!= 0))
1818 ext4_mb_simple_scan_group(ac
, &e4b
);
1820 ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
)
1821 ext4_mb_scan_aligned(ac
, &e4b
);
1823 ext4_mb_complex_scan_group(ac
, &e4b
);
1825 ext4_unlock_group(sb
, group
);
1826 ext4_mb_release_desc(&e4b
);
1828 if (ac
->ac_status
!= AC_STATUS_CONTINUE
)
1833 if (ac
->ac_b_ex
.fe_len
> 0 && ac
->ac_status
!= AC_STATUS_FOUND
&&
1834 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1836 * We've been searching too long. Let's try to allocate
1837 * the best chunk we've found so far
1840 ext4_mb_try_best_found(ac
, &e4b
);
1841 if (ac
->ac_status
!= AC_STATUS_FOUND
) {
1843 * Someone more lucky has already allocated it.
1844 * The only thing we can do is just take first
1846 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
1848 ac
->ac_b_ex
.fe_group
= 0;
1849 ac
->ac_b_ex
.fe_start
= 0;
1850 ac
->ac_b_ex
.fe_len
= 0;
1851 ac
->ac_status
= AC_STATUS_CONTINUE
;
1852 ac
->ac_flags
|= EXT4_MB_HINT_FIRST
;
1854 atomic_inc(&sbi
->s_mb_lost_chunks
);
1862 #ifdef EXT4_MB_HISTORY
1863 struct ext4_mb_proc_session
{
1864 struct ext4_mb_history
*history
;
1865 struct super_block
*sb
;
1870 static void *ext4_mb_history_skip_empty(struct ext4_mb_proc_session
*s
,
1871 struct ext4_mb_history
*hs
,
1874 if (hs
== s
->history
+ s
->max
)
1876 if (!first
&& hs
== s
->history
+ s
->start
)
1878 while (hs
->orig
.fe_len
== 0) {
1880 if (hs
== s
->history
+ s
->max
)
1882 if (hs
== s
->history
+ s
->start
)
1888 static void *ext4_mb_seq_history_start(struct seq_file
*seq
, loff_t
*pos
)
1890 struct ext4_mb_proc_session
*s
= seq
->private;
1891 struct ext4_mb_history
*hs
;
1895 return SEQ_START_TOKEN
;
1896 hs
= ext4_mb_history_skip_empty(s
, s
->history
+ s
->start
, 1);
1899 while (--l
&& (hs
= ext4_mb_history_skip_empty(s
, ++hs
, 0)) != NULL
);
1903 static void *ext4_mb_seq_history_next(struct seq_file
*seq
, void *v
,
1906 struct ext4_mb_proc_session
*s
= seq
->private;
1907 struct ext4_mb_history
*hs
= v
;
1910 if (v
== SEQ_START_TOKEN
)
1911 return ext4_mb_history_skip_empty(s
, s
->history
+ s
->start
, 1);
1913 return ext4_mb_history_skip_empty(s
, ++hs
, 0);
1916 static int ext4_mb_seq_history_show(struct seq_file
*seq
, void *v
)
1918 char buf
[25], buf2
[25], buf3
[25], *fmt
;
1919 struct ext4_mb_history
*hs
= v
;
1921 if (v
== SEQ_START_TOKEN
) {
1922 seq_printf(seq
, "%-5s %-8s %-23s %-23s %-23s %-5s "
1923 "%-5s %-2s %-5s %-5s %-5s %-6s\n",
1924 "pid", "inode", "original", "goal", "result", "found",
1925 "grps", "cr", "flags", "merge", "tail", "broken");
1929 if (hs
->op
== EXT4_MB_HISTORY_ALLOC
) {
1930 fmt
= "%-5u %-8u %-23s %-23s %-23s %-5u %-5u %-2u "
1931 "%-5u %-5s %-5u %-6u\n";
1932 sprintf(buf2
, "%lu/%d/%u@%u", hs
->result
.fe_group
,
1933 hs
->result
.fe_start
, hs
->result
.fe_len
,
1934 hs
->result
.fe_logical
);
1935 sprintf(buf
, "%lu/%d/%u@%u", hs
->orig
.fe_group
,
1936 hs
->orig
.fe_start
, hs
->orig
.fe_len
,
1937 hs
->orig
.fe_logical
);
1938 sprintf(buf3
, "%lu/%d/%u@%u", hs
->goal
.fe_group
,
1939 hs
->goal
.fe_start
, hs
->goal
.fe_len
,
1940 hs
->goal
.fe_logical
);
1941 seq_printf(seq
, fmt
, hs
->pid
, hs
->ino
, buf
, buf3
, buf2
,
1942 hs
->found
, hs
->groups
, hs
->cr
, hs
->flags
,
1943 hs
->merged
? "M" : "", hs
->tail
,
1944 hs
->buddy
? 1 << hs
->buddy
: 0);
1945 } else if (hs
->op
== EXT4_MB_HISTORY_PREALLOC
) {
1946 fmt
= "%-5u %-8u %-23s %-23s %-23s\n";
1947 sprintf(buf2
, "%lu/%d/%u@%u", hs
->result
.fe_group
,
1948 hs
->result
.fe_start
, hs
->result
.fe_len
,
1949 hs
->result
.fe_logical
);
1950 sprintf(buf
, "%lu/%d/%u@%u", hs
->orig
.fe_group
,
1951 hs
->orig
.fe_start
, hs
->orig
.fe_len
,
1952 hs
->orig
.fe_logical
);
1953 seq_printf(seq
, fmt
, hs
->pid
, hs
->ino
, buf
, "", buf2
);
1954 } else if (hs
->op
== EXT4_MB_HISTORY_DISCARD
) {
1955 sprintf(buf2
, "%lu/%d/%u", hs
->result
.fe_group
,
1956 hs
->result
.fe_start
, hs
->result
.fe_len
);
1957 seq_printf(seq
, "%-5u %-8u %-23s discard\n",
1958 hs
->pid
, hs
->ino
, buf2
);
1959 } else if (hs
->op
== EXT4_MB_HISTORY_FREE
) {
1960 sprintf(buf2
, "%lu/%d/%u", hs
->result
.fe_group
,
1961 hs
->result
.fe_start
, hs
->result
.fe_len
);
1962 seq_printf(seq
, "%-5u %-8u %-23s free\n",
1963 hs
->pid
, hs
->ino
, buf2
);
1968 static void ext4_mb_seq_history_stop(struct seq_file
*seq
, void *v
)
1972 static struct seq_operations ext4_mb_seq_history_ops
= {
1973 .start
= ext4_mb_seq_history_start
,
1974 .next
= ext4_mb_seq_history_next
,
1975 .stop
= ext4_mb_seq_history_stop
,
1976 .show
= ext4_mb_seq_history_show
,
1979 static int ext4_mb_seq_history_open(struct inode
*inode
, struct file
*file
)
1981 struct super_block
*sb
= PDE(inode
)->data
;
1982 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1983 struct ext4_mb_proc_session
*s
;
1987 if (unlikely(sbi
->s_mb_history
== NULL
))
1989 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1993 size
= sizeof(struct ext4_mb_history
) * sbi
->s_mb_history_max
;
1994 s
->history
= kmalloc(size
, GFP_KERNEL
);
1995 if (s
->history
== NULL
) {
2000 spin_lock(&sbi
->s_mb_history_lock
);
2001 memcpy(s
->history
, sbi
->s_mb_history
, size
);
2002 s
->max
= sbi
->s_mb_history_max
;
2003 s
->start
= sbi
->s_mb_history_cur
% s
->max
;
2004 spin_unlock(&sbi
->s_mb_history_lock
);
2006 rc
= seq_open(file
, &ext4_mb_seq_history_ops
);
2008 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2018 static int ext4_mb_seq_history_release(struct inode
*inode
, struct file
*file
)
2020 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
2021 struct ext4_mb_proc_session
*s
= seq
->private;
2024 return seq_release(inode
, file
);
2027 static ssize_t
ext4_mb_seq_history_write(struct file
*file
,
2028 const char __user
*buffer
,
2029 size_t count
, loff_t
*ppos
)
2031 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
2032 struct ext4_mb_proc_session
*s
= seq
->private;
2033 struct super_block
*sb
= s
->sb
;
2037 if (count
>= sizeof(str
)) {
2038 printk(KERN_ERR
"EXT4-fs: %s string too long, max %u bytes\n",
2039 "mb_history", (int)sizeof(str
));
2043 if (copy_from_user(str
, buffer
, count
))
2046 value
= simple_strtol(str
, NULL
, 0);
2049 EXT4_SB(sb
)->s_mb_history_filter
= value
;
2054 static struct file_operations ext4_mb_seq_history_fops
= {
2055 .owner
= THIS_MODULE
,
2056 .open
= ext4_mb_seq_history_open
,
2058 .write
= ext4_mb_seq_history_write
,
2059 .llseek
= seq_lseek
,
2060 .release
= ext4_mb_seq_history_release
,
2063 static void *ext4_mb_seq_groups_start(struct seq_file
*seq
, loff_t
*pos
)
2065 struct super_block
*sb
= seq
->private;
2066 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2069 if (*pos
< 0 || *pos
>= sbi
->s_groups_count
)
2073 return (void *) group
;
2076 static void *ext4_mb_seq_groups_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2078 struct super_block
*sb
= seq
->private;
2079 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2083 if (*pos
< 0 || *pos
>= sbi
->s_groups_count
)
2086 return (void *) group
;;
2089 static int ext4_mb_seq_groups_show(struct seq_file
*seq
, void *v
)
2091 struct super_block
*sb
= seq
->private;
2092 long group
= (long) v
;
2095 struct ext4_buddy e4b
;
2097 struct ext4_group_info info
;
2098 unsigned short counters
[16];
2103 seq_printf(seq
, "#%-5s: %-5s %-5s %-5s "
2104 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2105 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2106 "group", "free", "frags", "first",
2107 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2108 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2110 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(sg
.info
.bb_counters
[0]) +
2111 sizeof(struct ext4_group_info
);
2112 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2114 seq_printf(seq
, "#%-5lu: I/O error\n", group
);
2117 ext4_lock_group(sb
, group
);
2118 memcpy(&sg
, ext4_get_group_info(sb
, group
), i
);
2119 ext4_unlock_group(sb
, group
);
2120 ext4_mb_release_desc(&e4b
);
2122 seq_printf(seq
, "#%-5lu: %-5u %-5u %-5u [", group
, sg
.info
.bb_free
,
2123 sg
.info
.bb_fragments
, sg
.info
.bb_first_free
);
2124 for (i
= 0; i
<= 13; i
++)
2125 seq_printf(seq
, " %-5u", i
<= sb
->s_blocksize_bits
+ 1 ?
2126 sg
.info
.bb_counters
[i
] : 0);
2127 seq_printf(seq
, " ]\n");
2132 static void ext4_mb_seq_groups_stop(struct seq_file
*seq
, void *v
)
2136 static struct seq_operations ext4_mb_seq_groups_ops
= {
2137 .start
= ext4_mb_seq_groups_start
,
2138 .next
= ext4_mb_seq_groups_next
,
2139 .stop
= ext4_mb_seq_groups_stop
,
2140 .show
= ext4_mb_seq_groups_show
,
2143 static int ext4_mb_seq_groups_open(struct inode
*inode
, struct file
*file
)
2145 struct super_block
*sb
= PDE(inode
)->data
;
2148 rc
= seq_open(file
, &ext4_mb_seq_groups_ops
);
2150 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2157 static struct file_operations ext4_mb_seq_groups_fops
= {
2158 .owner
= THIS_MODULE
,
2159 .open
= ext4_mb_seq_groups_open
,
2161 .llseek
= seq_lseek
,
2162 .release
= seq_release
,
2165 static void ext4_mb_history_release(struct super_block
*sb
)
2167 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2169 remove_proc_entry("mb_groups", sbi
->s_mb_proc
);
2170 remove_proc_entry("mb_history", sbi
->s_mb_proc
);
2172 kfree(sbi
->s_mb_history
);
2175 static void ext4_mb_history_init(struct super_block
*sb
)
2177 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2180 if (sbi
->s_mb_proc
!= NULL
) {
2181 proc_create_data("mb_history", S_IRUGO
, sbi
->s_mb_proc
,
2182 &ext4_mb_seq_history_fops
, sb
);
2183 proc_create_data("mb_groups", S_IRUGO
, sbi
->s_mb_proc
,
2184 &ext4_mb_seq_groups_fops
, sb
);
2187 sbi
->s_mb_history_max
= 1000;
2188 sbi
->s_mb_history_cur
= 0;
2189 spin_lock_init(&sbi
->s_mb_history_lock
);
2190 i
= sbi
->s_mb_history_max
* sizeof(struct ext4_mb_history
);
2191 sbi
->s_mb_history
= kzalloc(i
, GFP_KERNEL
);
2192 /* if we can't allocate history, then we simple won't use it */
2195 static noinline_for_stack
void
2196 ext4_mb_store_history(struct ext4_allocation_context
*ac
)
2198 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
2199 struct ext4_mb_history h
;
2201 if (unlikely(sbi
->s_mb_history
== NULL
))
2204 if (!(ac
->ac_op
& sbi
->s_mb_history_filter
))
2208 h
.pid
= current
->pid
;
2209 h
.ino
= ac
->ac_inode
? ac
->ac_inode
->i_ino
: 0;
2210 h
.orig
= ac
->ac_o_ex
;
2211 h
.result
= ac
->ac_b_ex
;
2212 h
.flags
= ac
->ac_flags
;
2213 h
.found
= ac
->ac_found
;
2214 h
.groups
= ac
->ac_groups_scanned
;
2215 h
.cr
= ac
->ac_criteria
;
2216 h
.tail
= ac
->ac_tail
;
2217 h
.buddy
= ac
->ac_buddy
;
2219 if (ac
->ac_op
== EXT4_MB_HISTORY_ALLOC
) {
2220 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
2221 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
2223 h
.goal
= ac
->ac_g_ex
;
2224 h
.result
= ac
->ac_f_ex
;
2227 spin_lock(&sbi
->s_mb_history_lock
);
2228 memcpy(sbi
->s_mb_history
+ sbi
->s_mb_history_cur
, &h
, sizeof(h
));
2229 if (++sbi
->s_mb_history_cur
>= sbi
->s_mb_history_max
)
2230 sbi
->s_mb_history_cur
= 0;
2231 spin_unlock(&sbi
->s_mb_history_lock
);
2235 #define ext4_mb_history_release(sb)
2236 #define ext4_mb_history_init(sb)
2239 static int ext4_mb_init_backend(struct super_block
*sb
)
2242 int j
, len
, metalen
;
2243 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2244 int num_meta_group_infos
=
2245 (sbi
->s_groups_count
+ EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2246 EXT4_DESC_PER_BLOCK_BITS(sb
);
2247 struct ext4_group_info
**meta_group_info
;
2249 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2250 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2251 * So a two level scheme suffices for now. */
2252 sbi
->s_group_info
= kmalloc(sizeof(*sbi
->s_group_info
) *
2253 num_meta_group_infos
, GFP_KERNEL
);
2254 if (sbi
->s_group_info
== NULL
) {
2255 printk(KERN_ERR
"EXT4-fs: can't allocate buddy meta group\n");
2258 sbi
->s_buddy_cache
= new_inode(sb
);
2259 if (sbi
->s_buddy_cache
== NULL
) {
2260 printk(KERN_ERR
"EXT4-fs: can't get new inode\n");
2263 EXT4_I(sbi
->s_buddy_cache
)->i_disksize
= 0;
2265 metalen
= sizeof(*meta_group_info
) << EXT4_DESC_PER_BLOCK_BITS(sb
);
2266 for (i
= 0; i
< num_meta_group_infos
; i
++) {
2267 if ((i
+ 1) == num_meta_group_infos
)
2268 metalen
= sizeof(*meta_group_info
) *
2269 (sbi
->s_groups_count
-
2270 (i
<< EXT4_DESC_PER_BLOCK_BITS(sb
)));
2271 meta_group_info
= kmalloc(metalen
, GFP_KERNEL
);
2272 if (meta_group_info
== NULL
) {
2273 printk(KERN_ERR
"EXT4-fs: can't allocate mem for a "
2277 sbi
->s_group_info
[i
] = meta_group_info
;
2281 * calculate needed size. if change bb_counters size,
2282 * don't forget about ext4_mb_generate_buddy()
2284 len
= sizeof(struct ext4_group_info
);
2285 len
+= sizeof(unsigned short) * (sb
->s_blocksize_bits
+ 2);
2286 for (i
= 0; i
< sbi
->s_groups_count
; i
++) {
2287 struct ext4_group_desc
*desc
;
2290 sbi
->s_group_info
[i
>> EXT4_DESC_PER_BLOCK_BITS(sb
)];
2291 j
= i
& (EXT4_DESC_PER_BLOCK(sb
) - 1);
2293 meta_group_info
[j
] = kzalloc(len
, GFP_KERNEL
);
2294 if (meta_group_info
[j
] == NULL
) {
2295 printk(KERN_ERR
"EXT4-fs: can't allocate buddy mem\n");
2298 desc
= ext4_get_group_desc(sb
, i
, NULL
);
2301 "EXT4-fs: can't read descriptor %lu\n", i
);
2305 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
,
2306 &(meta_group_info
[j
]->bb_state
));
2309 * initialize bb_free to be able to skip
2310 * empty groups without initialization
2312 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2313 meta_group_info
[j
]->bb_free
=
2314 ext4_free_blocks_after_init(sb
, i
, desc
);
2316 meta_group_info
[j
]->bb_free
=
2317 le16_to_cpu(desc
->bg_free_blocks_count
);
2320 INIT_LIST_HEAD(&meta_group_info
[j
]->bb_prealloc_list
);
2324 struct buffer_head
*bh
;
2325 meta_group_info
[j
]->bb_bitmap
=
2326 kmalloc(sb
->s_blocksize
, GFP_KERNEL
);
2327 BUG_ON(meta_group_info
[j
]->bb_bitmap
== NULL
);
2328 bh
= ext4_read_block_bitmap(sb
, i
);
2330 memcpy(meta_group_info
[j
]->bb_bitmap
, bh
->b_data
,
2342 kfree(ext4_get_group_info(sb
, i
));
2343 i
= num_meta_group_infos
;
2346 kfree(sbi
->s_group_info
[i
]);
2347 iput(sbi
->s_buddy_cache
);
2349 kfree(sbi
->s_group_info
);
2353 int ext4_mb_init(struct super_block
*sb
, int needs_recovery
)
2355 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2361 if (!test_opt(sb
, MBALLOC
))
2364 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(unsigned short);
2366 sbi
->s_mb_offsets
= kmalloc(i
, GFP_KERNEL
);
2367 if (sbi
->s_mb_offsets
== NULL
) {
2368 clear_opt(sbi
->s_mount_opt
, MBALLOC
);
2371 sbi
->s_mb_maxs
= kmalloc(i
, GFP_KERNEL
);
2372 if (sbi
->s_mb_maxs
== NULL
) {
2373 clear_opt(sbi
->s_mount_opt
, MBALLOC
);
2374 kfree(sbi
->s_mb_maxs
);
2378 /* order 0 is regular bitmap */
2379 sbi
->s_mb_maxs
[0] = sb
->s_blocksize
<< 3;
2380 sbi
->s_mb_offsets
[0] = 0;
2384 max
= sb
->s_blocksize
<< 2;
2386 sbi
->s_mb_offsets
[i
] = offset
;
2387 sbi
->s_mb_maxs
[i
] = max
;
2388 offset
+= 1 << (sb
->s_blocksize_bits
- i
);
2391 } while (i
<= sb
->s_blocksize_bits
+ 1);
2393 /* init file for buddy data */
2394 ret
= ext4_mb_init_backend(sb
);
2396 clear_opt(sbi
->s_mount_opt
, MBALLOC
);
2397 kfree(sbi
->s_mb_offsets
);
2398 kfree(sbi
->s_mb_maxs
);
2402 spin_lock_init(&sbi
->s_md_lock
);
2403 INIT_LIST_HEAD(&sbi
->s_active_transaction
);
2404 INIT_LIST_HEAD(&sbi
->s_closed_transaction
);
2405 INIT_LIST_HEAD(&sbi
->s_committed_transaction
);
2406 spin_lock_init(&sbi
->s_bal_lock
);
2408 sbi
->s_mb_max_to_scan
= MB_DEFAULT_MAX_TO_SCAN
;
2409 sbi
->s_mb_min_to_scan
= MB_DEFAULT_MIN_TO_SCAN
;
2410 sbi
->s_mb_stats
= MB_DEFAULT_STATS
;
2411 sbi
->s_mb_stream_request
= MB_DEFAULT_STREAM_THRESHOLD
;
2412 sbi
->s_mb_order2_reqs
= MB_DEFAULT_ORDER2_REQS
;
2413 sbi
->s_mb_history_filter
= EXT4_MB_HISTORY_DEFAULT
;
2414 sbi
->s_mb_group_prealloc
= MB_DEFAULT_GROUP_PREALLOC
;
2416 i
= sizeof(struct ext4_locality_group
) * NR_CPUS
;
2417 sbi
->s_locality_groups
= kmalloc(i
, GFP_KERNEL
);
2418 if (sbi
->s_locality_groups
== NULL
) {
2419 clear_opt(sbi
->s_mount_opt
, MBALLOC
);
2420 kfree(sbi
->s_mb_offsets
);
2421 kfree(sbi
->s_mb_maxs
);
2424 for (i
= 0; i
< NR_CPUS
; i
++) {
2425 struct ext4_locality_group
*lg
;
2426 lg
= &sbi
->s_locality_groups
[i
];
2427 mutex_init(&lg
->lg_mutex
);
2428 INIT_LIST_HEAD(&lg
->lg_prealloc_list
);
2429 spin_lock_init(&lg
->lg_prealloc_lock
);
2432 ext4_mb_init_per_dev_proc(sb
);
2433 ext4_mb_history_init(sb
);
2435 printk("EXT4-fs: mballoc enabled\n");
2439 /* need to called with ext4 group lock (ext4_lock_group) */
2440 static void ext4_mb_cleanup_pa(struct ext4_group_info
*grp
)
2442 struct ext4_prealloc_space
*pa
;
2443 struct list_head
*cur
, *tmp
;
2446 list_for_each_safe(cur
, tmp
, &grp
->bb_prealloc_list
) {
2447 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
2448 list_del(&pa
->pa_group_list
);
2453 mb_debug("mballoc: %u PAs left\n", count
);
2457 int ext4_mb_release(struct super_block
*sb
)
2460 int num_meta_group_infos
;
2461 struct ext4_group_info
*grinfo
;
2462 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2464 if (!test_opt(sb
, MBALLOC
))
2467 /* release freed, non-committed blocks */
2468 spin_lock(&sbi
->s_md_lock
);
2469 list_splice_init(&sbi
->s_closed_transaction
,
2470 &sbi
->s_committed_transaction
);
2471 list_splice_init(&sbi
->s_active_transaction
,
2472 &sbi
->s_committed_transaction
);
2473 spin_unlock(&sbi
->s_md_lock
);
2474 ext4_mb_free_committed_blocks(sb
);
2476 if (sbi
->s_group_info
) {
2477 for (i
= 0; i
< sbi
->s_groups_count
; i
++) {
2478 grinfo
= ext4_get_group_info(sb
, i
);
2480 kfree(grinfo
->bb_bitmap
);
2482 ext4_lock_group(sb
, i
);
2483 ext4_mb_cleanup_pa(grinfo
);
2484 ext4_unlock_group(sb
, i
);
2487 num_meta_group_infos
= (sbi
->s_groups_count
+
2488 EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2489 EXT4_DESC_PER_BLOCK_BITS(sb
);
2490 for (i
= 0; i
< num_meta_group_infos
; i
++)
2491 kfree(sbi
->s_group_info
[i
]);
2492 kfree(sbi
->s_group_info
);
2494 kfree(sbi
->s_mb_offsets
);
2495 kfree(sbi
->s_mb_maxs
);
2496 if (sbi
->s_buddy_cache
)
2497 iput(sbi
->s_buddy_cache
);
2498 if (sbi
->s_mb_stats
) {
2500 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2501 atomic_read(&sbi
->s_bal_allocated
),
2502 atomic_read(&sbi
->s_bal_reqs
),
2503 atomic_read(&sbi
->s_bal_success
));
2505 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2506 "%u 2^N hits, %u breaks, %u lost\n",
2507 atomic_read(&sbi
->s_bal_ex_scanned
),
2508 atomic_read(&sbi
->s_bal_goals
),
2509 atomic_read(&sbi
->s_bal_2orders
),
2510 atomic_read(&sbi
->s_bal_breaks
),
2511 atomic_read(&sbi
->s_mb_lost_chunks
));
2513 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2514 sbi
->s_mb_buddies_generated
++,
2515 sbi
->s_mb_generation_time
);
2517 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2518 atomic_read(&sbi
->s_mb_preallocated
),
2519 atomic_read(&sbi
->s_mb_discarded
));
2522 kfree(sbi
->s_locality_groups
);
2524 ext4_mb_history_release(sb
);
2525 ext4_mb_destroy_per_dev_proc(sb
);
2530 static noinline_for_stack
void
2531 ext4_mb_free_committed_blocks(struct super_block
*sb
)
2533 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2538 struct ext4_free_metadata
*md
;
2539 struct ext4_buddy e4b
;
2541 if (list_empty(&sbi
->s_committed_transaction
))
2544 /* there is committed blocks to be freed yet */
2546 /* get next array of blocks */
2548 spin_lock(&sbi
->s_md_lock
);
2549 if (!list_empty(&sbi
->s_committed_transaction
)) {
2550 md
= list_entry(sbi
->s_committed_transaction
.next
,
2551 struct ext4_free_metadata
, list
);
2552 list_del(&md
->list
);
2554 spin_unlock(&sbi
->s_md_lock
);
2559 mb_debug("gonna free %u blocks in group %lu (0x%p):",
2560 md
->num
, md
->group
, md
);
2562 err
= ext4_mb_load_buddy(sb
, md
->group
, &e4b
);
2563 /* we expect to find existing buddy because it's pinned */
2566 /* there are blocks to put in buddy to make them really free */
2569 ext4_lock_group(sb
, md
->group
);
2570 for (i
= 0; i
< md
->num
; i
++) {
2571 mb_debug(" %u", md
->blocks
[i
]);
2572 mb_free_blocks(NULL
, &e4b
, md
->blocks
[i
], 1);
2575 ext4_unlock_group(sb
, md
->group
);
2577 /* balance refcounts from ext4_mb_free_metadata() */
2578 page_cache_release(e4b
.bd_buddy_page
);
2579 page_cache_release(e4b
.bd_bitmap_page
);
2582 ext4_mb_release_desc(&e4b
);
2586 mb_debug("freed %u blocks in %u structures\n", count
, count2
);
2589 #define EXT4_MB_STATS_NAME "stats"
2590 #define EXT4_MB_MAX_TO_SCAN_NAME "max_to_scan"
2591 #define EXT4_MB_MIN_TO_SCAN_NAME "min_to_scan"
2592 #define EXT4_MB_ORDER2_REQ "order2_req"
2593 #define EXT4_MB_STREAM_REQ "stream_req"
2594 #define EXT4_MB_GROUP_PREALLOC "group_prealloc"
2598 #define MB_PROC_FOPS(name) \
2599 static int ext4_mb_##name##_proc_show(struct seq_file *m, void *v) \
2601 struct ext4_sb_info *sbi = m->private; \
2603 seq_printf(m, "%ld\n", sbi->s_mb_##name); \
2607 static int ext4_mb_##name##_proc_open(struct inode *inode, struct file *file)\
2609 return single_open(file, ext4_mb_##name##_proc_show, PDE(inode)->data);\
2612 static ssize_t ext4_mb_##name##_proc_write(struct file *file, \
2613 const char __user *buf, size_t cnt, loff_t *ppos) \
2615 struct ext4_sb_info *sbi = PDE(file->f_path.dentry->d_inode)->data;\
2618 if (cnt >= sizeof(str)) \
2620 if (copy_from_user(str, buf, cnt)) \
2622 value = simple_strtol(str, NULL, 0); \
2625 sbi->s_mb_##name = value; \
2629 static const struct file_operations ext4_mb_##name##_proc_fops = { \
2630 .owner = THIS_MODULE, \
2631 .open = ext4_mb_##name##_proc_open, \
2633 .llseek = seq_lseek, \
2634 .release = single_release, \
2635 .write = ext4_mb_##name##_proc_write, \
2638 MB_PROC_FOPS(stats
);
2639 MB_PROC_FOPS(max_to_scan
);
2640 MB_PROC_FOPS(min_to_scan
);
2641 MB_PROC_FOPS(order2_reqs
);
2642 MB_PROC_FOPS(stream_request
);
2643 MB_PROC_FOPS(group_prealloc
);
2645 #define MB_PROC_HANDLER(name, var) \
2647 proc = proc_create_data(name, mode, sbi->s_mb_proc, \
2648 &ext4_mb_##var##_proc_fops, sbi); \
2649 if (proc == NULL) { \
2650 printk(KERN_ERR "EXT4-fs: can't to create %s\n", name); \
2655 static int ext4_mb_init_per_dev_proc(struct super_block
*sb
)
2657 mode_t mode
= S_IFREG
| S_IRUGO
| S_IWUSR
;
2658 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2659 struct proc_dir_entry
*proc
;
2662 if (proc_root_ext4
== NULL
) {
2663 sbi
->s_mb_proc
= NULL
;
2666 bdevname(sb
->s_bdev
, devname
);
2667 sbi
->s_mb_proc
= proc_mkdir(devname
, proc_root_ext4
);
2669 MB_PROC_HANDLER(EXT4_MB_STATS_NAME
, stats
);
2670 MB_PROC_HANDLER(EXT4_MB_MAX_TO_SCAN_NAME
, max_to_scan
);
2671 MB_PROC_HANDLER(EXT4_MB_MIN_TO_SCAN_NAME
, min_to_scan
);
2672 MB_PROC_HANDLER(EXT4_MB_ORDER2_REQ
, order2_reqs
);
2673 MB_PROC_HANDLER(EXT4_MB_STREAM_REQ
, stream_request
);
2674 MB_PROC_HANDLER(EXT4_MB_GROUP_PREALLOC
, group_prealloc
);
2679 printk(KERN_ERR
"EXT4-fs: Unable to create %s\n", devname
);
2680 remove_proc_entry(EXT4_MB_GROUP_PREALLOC
, sbi
->s_mb_proc
);
2681 remove_proc_entry(EXT4_MB_STREAM_REQ
, sbi
->s_mb_proc
);
2682 remove_proc_entry(EXT4_MB_ORDER2_REQ
, sbi
->s_mb_proc
);
2683 remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME
, sbi
->s_mb_proc
);
2684 remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME
, sbi
->s_mb_proc
);
2685 remove_proc_entry(EXT4_MB_STATS_NAME
, sbi
->s_mb_proc
);
2686 remove_proc_entry(devname
, proc_root_ext4
);
2687 sbi
->s_mb_proc
= NULL
;
2692 static int ext4_mb_destroy_per_dev_proc(struct super_block
*sb
)
2694 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2697 if (sbi
->s_mb_proc
== NULL
)
2700 bdevname(sb
->s_bdev
, devname
);
2701 remove_proc_entry(EXT4_MB_GROUP_PREALLOC
, sbi
->s_mb_proc
);
2702 remove_proc_entry(EXT4_MB_STREAM_REQ
, sbi
->s_mb_proc
);
2703 remove_proc_entry(EXT4_MB_ORDER2_REQ
, sbi
->s_mb_proc
);
2704 remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME
, sbi
->s_mb_proc
);
2705 remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME
, sbi
->s_mb_proc
);
2706 remove_proc_entry(EXT4_MB_STATS_NAME
, sbi
->s_mb_proc
);
2707 remove_proc_entry(devname
, proc_root_ext4
);
2712 int __init
init_ext4_mballoc(void)
2714 ext4_pspace_cachep
=
2715 kmem_cache_create("ext4_prealloc_space",
2716 sizeof(struct ext4_prealloc_space
),
2717 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2718 if (ext4_pspace_cachep
== NULL
)
2722 kmem_cache_create("ext4_alloc_context",
2723 sizeof(struct ext4_allocation_context
),
2724 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2725 if (ext4_ac_cachep
== NULL
) {
2726 kmem_cache_destroy(ext4_pspace_cachep
);
2729 #ifdef CONFIG_PROC_FS
2730 proc_root_ext4
= proc_mkdir("fs/ext4", NULL
);
2731 if (proc_root_ext4
== NULL
)
2732 printk(KERN_ERR
"EXT4-fs: Unable to create fs/ext4\n");
2737 void exit_ext4_mballoc(void)
2739 /* XXX: synchronize_rcu(); */
2740 kmem_cache_destroy(ext4_pspace_cachep
);
2741 kmem_cache_destroy(ext4_ac_cachep
);
2742 #ifdef CONFIG_PROC_FS
2743 remove_proc_entry("fs/ext4", NULL
);
2749 * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
2750 * Returns 0 if success or error code
2752 static noinline_for_stack
int
2753 ext4_mb_mark_diskspace_used(struct ext4_allocation_context
*ac
,
2756 struct buffer_head
*bitmap_bh
= NULL
;
2757 struct ext4_super_block
*es
;
2758 struct ext4_group_desc
*gdp
;
2759 struct buffer_head
*gdp_bh
;
2760 struct ext4_sb_info
*sbi
;
2761 struct super_block
*sb
;
2765 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
2766 BUG_ON(ac
->ac_b_ex
.fe_len
<= 0);
2774 bitmap_bh
= ext4_read_block_bitmap(sb
, ac
->ac_b_ex
.fe_group
);
2778 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
2783 gdp
= ext4_get_group_desc(sb
, ac
->ac_b_ex
.fe_group
, &gdp_bh
);
2787 ext4_debug("using block group %lu(%d)\n", ac
->ac_b_ex
.fe_group
,
2788 gdp
->bg_free_blocks_count
);
2790 err
= ext4_journal_get_write_access(handle
, gdp_bh
);
2794 block
= ac
->ac_b_ex
.fe_group
* EXT4_BLOCKS_PER_GROUP(sb
)
2795 + ac
->ac_b_ex
.fe_start
2796 + le32_to_cpu(es
->s_first_data_block
);
2798 len
= ac
->ac_b_ex
.fe_len
;
2799 if (in_range(ext4_block_bitmap(sb
, gdp
), block
, len
) ||
2800 in_range(ext4_inode_bitmap(sb
, gdp
), block
, len
) ||
2801 in_range(block
, ext4_inode_table(sb
, gdp
),
2802 EXT4_SB(sb
)->s_itb_per_group
) ||
2803 in_range(block
+ len
- 1, ext4_inode_table(sb
, gdp
),
2804 EXT4_SB(sb
)->s_itb_per_group
)) {
2805 ext4_error(sb
, __func__
,
2806 "Allocating block in system zone - block = %llu",
2808 /* File system mounted not to panic on error
2809 * Fix the bitmap and repeat the block allocation
2810 * We leak some of the blocks here.
2812 mb_set_bits(sb_bgl_lock(sbi
, ac
->ac_b_ex
.fe_group
),
2813 bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,
2814 ac
->ac_b_ex
.fe_len
);
2815 err
= ext4_journal_dirty_metadata(handle
, bitmap_bh
);
2820 #ifdef AGGRESSIVE_CHECK
2823 for (i
= 0; i
< ac
->ac_b_ex
.fe_len
; i
++) {
2824 BUG_ON(mb_test_bit(ac
->ac_b_ex
.fe_start
+ i
,
2825 bitmap_bh
->b_data
));
2829 mb_set_bits(sb_bgl_lock(sbi
, ac
->ac_b_ex
.fe_group
), bitmap_bh
->b_data
,
2830 ac
->ac_b_ex
.fe_start
, ac
->ac_b_ex
.fe_len
);
2832 spin_lock(sb_bgl_lock(sbi
, ac
->ac_b_ex
.fe_group
));
2833 if (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2834 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT
);
2835 gdp
->bg_free_blocks_count
=
2836 cpu_to_le16(ext4_free_blocks_after_init(sb
,
2837 ac
->ac_b_ex
.fe_group
,
2840 le16_add_cpu(&gdp
->bg_free_blocks_count
, -ac
->ac_b_ex
.fe_len
);
2841 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, ac
->ac_b_ex
.fe_group
, gdp
);
2842 spin_unlock(sb_bgl_lock(sbi
, ac
->ac_b_ex
.fe_group
));
2843 percpu_counter_sub(&sbi
->s_freeblocks_counter
, ac
->ac_b_ex
.fe_len
);
2845 err
= ext4_journal_dirty_metadata(handle
, bitmap_bh
);
2848 err
= ext4_journal_dirty_metadata(handle
, gdp_bh
);
2857 * here we normalize request for locality group
2858 * Group request are normalized to s_strip size if we set the same via mount
2859 * option. If not we set it to s_mb_group_prealloc which can be configured via
2860 * /proc/fs/ext4/<partition>/group_prealloc
2862 * XXX: should we try to preallocate more than the group has now?
2864 static void ext4_mb_normalize_group_request(struct ext4_allocation_context
*ac
)
2866 struct super_block
*sb
= ac
->ac_sb
;
2867 struct ext4_locality_group
*lg
= ac
->ac_lg
;
2870 if (EXT4_SB(sb
)->s_stripe
)
2871 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_stripe
;
2873 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_mb_group_prealloc
;
2874 mb_debug("#%u: goal %u blocks for locality group\n",
2875 current
->pid
, ac
->ac_g_ex
.fe_len
);
2879 * Normalization means making request better in terms of
2880 * size and alignment
2882 static noinline_for_stack
void
2883 ext4_mb_normalize_request(struct ext4_allocation_context
*ac
,
2884 struct ext4_allocation_request
*ar
)
2888 loff_t size
, orig_size
, start_off
;
2889 ext4_lblk_t start
, orig_start
;
2890 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
2891 struct ext4_prealloc_space
*pa
;
2893 /* do normalize only data requests, metadata requests
2894 do not need preallocation */
2895 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
2898 /* sometime caller may want exact blocks */
2899 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
2902 /* caller may indicate that preallocation isn't
2903 * required (it's a tail, for example) */
2904 if (ac
->ac_flags
& EXT4_MB_HINT_NOPREALLOC
)
2907 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
) {
2908 ext4_mb_normalize_group_request(ac
);
2912 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
2914 /* first, let's learn actual file size
2915 * given current request is allocated */
2916 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
2917 size
= size
<< bsbits
;
2918 if (size
< i_size_read(ac
->ac_inode
))
2919 size
= i_size_read(ac
->ac_inode
);
2921 /* max size of free chunks */
2924 #define NRL_CHECK_SIZE(req, size, max, chunk_size) \
2925 (req <= (size) || max <= (chunk_size))
2927 /* first, try to predict filesize */
2928 /* XXX: should this table be tunable? */
2930 if (size
<= 16 * 1024) {
2932 } else if (size
<= 32 * 1024) {
2934 } else if (size
<= 64 * 1024) {
2936 } else if (size
<= 128 * 1024) {
2938 } else if (size
<= 256 * 1024) {
2940 } else if (size
<= 512 * 1024) {
2942 } else if (size
<= 1024 * 1024) {
2944 } else if (NRL_CHECK_SIZE(size
, 4 * 1024 * 1024, max
, 2 * 1024)) {
2945 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
2946 (21 - bsbits
)) << 21;
2947 size
= 2 * 1024 * 1024;
2948 } else if (NRL_CHECK_SIZE(size
, 8 * 1024 * 1024, max
, 4 * 1024)) {
2949 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
2950 (22 - bsbits
)) << 22;
2951 size
= 4 * 1024 * 1024;
2952 } else if (NRL_CHECK_SIZE(ac
->ac_o_ex
.fe_len
,
2953 (8<<20)>>bsbits
, max
, 8 * 1024)) {
2954 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
2955 (23 - bsbits
)) << 23;
2956 size
= 8 * 1024 * 1024;
2958 start_off
= (loff_t
)ac
->ac_o_ex
.fe_logical
<< bsbits
;
2959 size
= ac
->ac_o_ex
.fe_len
<< bsbits
;
2961 orig_size
= size
= size
>> bsbits
;
2962 orig_start
= start
= start_off
>> bsbits
;
2964 /* don't cover already allocated blocks in selected range */
2965 if (ar
->pleft
&& start
<= ar
->lleft
) {
2966 size
-= ar
->lleft
+ 1 - start
;
2967 start
= ar
->lleft
+ 1;
2969 if (ar
->pright
&& start
+ size
- 1 >= ar
->lright
)
2970 size
-= start
+ size
- ar
->lright
;
2974 /* check we don't cross already preallocated blocks */
2976 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
2977 unsigned long pa_end
;
2981 spin_lock(&pa
->pa_lock
);
2982 if (pa
->pa_deleted
) {
2983 spin_unlock(&pa
->pa_lock
);
2987 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
2989 /* PA must not overlap original request */
2990 BUG_ON(!(ac
->ac_o_ex
.fe_logical
>= pa_end
||
2991 ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
));
2993 /* skip PA normalized request doesn't overlap with */
2994 if (pa
->pa_lstart
>= end
) {
2995 spin_unlock(&pa
->pa_lock
);
2998 if (pa_end
<= start
) {
2999 spin_unlock(&pa
->pa_lock
);
3002 BUG_ON(pa
->pa_lstart
<= start
&& pa_end
>= end
);
3004 if (pa_end
<= ac
->ac_o_ex
.fe_logical
) {
3005 BUG_ON(pa_end
< start
);
3009 if (pa
->pa_lstart
> ac
->ac_o_ex
.fe_logical
) {
3010 BUG_ON(pa
->pa_lstart
> end
);
3011 end
= pa
->pa_lstart
;
3013 spin_unlock(&pa
->pa_lock
);
3018 /* XXX: extra loop to check we really don't overlap preallocations */
3020 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3021 unsigned long pa_end
;
3022 spin_lock(&pa
->pa_lock
);
3023 if (pa
->pa_deleted
== 0) {
3024 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
3025 BUG_ON(!(start
>= pa_end
|| end
<= pa
->pa_lstart
));
3027 spin_unlock(&pa
->pa_lock
);
3031 if (start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3032 start
> ac
->ac_o_ex
.fe_logical
) {
3033 printk(KERN_ERR
"start %lu, size %lu, fe_logical %lu\n",
3034 (unsigned long) start
, (unsigned long) size
,
3035 (unsigned long) ac
->ac_o_ex
.fe_logical
);
3037 BUG_ON(start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3038 start
> ac
->ac_o_ex
.fe_logical
);
3039 BUG_ON(size
<= 0 || size
>= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
3041 /* now prepare goal request */
3043 /* XXX: is it better to align blocks WRT to logical
3044 * placement or satisfy big request as is */
3045 ac
->ac_g_ex
.fe_logical
= start
;
3046 ac
->ac_g_ex
.fe_len
= size
;
3048 /* define goal start in order to merge */
3049 if (ar
->pright
&& (ar
->lright
== (start
+ size
))) {
3050 /* merge to the right */
3051 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pright
- size
,
3052 &ac
->ac_f_ex
.fe_group
,
3053 &ac
->ac_f_ex
.fe_start
);
3054 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3056 if (ar
->pleft
&& (ar
->lleft
+ 1 == start
)) {
3057 /* merge to the left */
3058 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pleft
+ 1,
3059 &ac
->ac_f_ex
.fe_group
,
3060 &ac
->ac_f_ex
.fe_start
);
3061 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3064 mb_debug("goal: %u(was %u) blocks at %u\n", (unsigned) size
,
3065 (unsigned) orig_size
, (unsigned) start
);
3068 static void ext4_mb_collect_stats(struct ext4_allocation_context
*ac
)
3070 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3072 if (sbi
->s_mb_stats
&& ac
->ac_g_ex
.fe_len
> 1) {
3073 atomic_inc(&sbi
->s_bal_reqs
);
3074 atomic_add(ac
->ac_b_ex
.fe_len
, &sbi
->s_bal_allocated
);
3075 if (ac
->ac_o_ex
.fe_len
>= ac
->ac_g_ex
.fe_len
)
3076 atomic_inc(&sbi
->s_bal_success
);
3077 atomic_add(ac
->ac_found
, &sbi
->s_bal_ex_scanned
);
3078 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
3079 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
3080 atomic_inc(&sbi
->s_bal_goals
);
3081 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
)
3082 atomic_inc(&sbi
->s_bal_breaks
);
3085 ext4_mb_store_history(ac
);
3089 * use blocks preallocated to inode
3091 static void ext4_mb_use_inode_pa(struct ext4_allocation_context
*ac
,
3092 struct ext4_prealloc_space
*pa
)
3098 /* found preallocated blocks, use them */
3099 start
= pa
->pa_pstart
+ (ac
->ac_o_ex
.fe_logical
- pa
->pa_lstart
);
3100 end
= min(pa
->pa_pstart
+ pa
->pa_len
, start
+ ac
->ac_o_ex
.fe_len
);
3102 ext4_get_group_no_and_offset(ac
->ac_sb
, start
, &ac
->ac_b_ex
.fe_group
,
3103 &ac
->ac_b_ex
.fe_start
);
3104 ac
->ac_b_ex
.fe_len
= len
;
3105 ac
->ac_status
= AC_STATUS_FOUND
;
3108 BUG_ON(start
< pa
->pa_pstart
);
3109 BUG_ON(start
+ len
> pa
->pa_pstart
+ pa
->pa_len
);
3110 BUG_ON(pa
->pa_free
< len
);
3113 mb_debug("use %llu/%u from inode pa %p\n", start
, len
, pa
);
3117 * use blocks preallocated to locality group
3119 static void ext4_mb_use_group_pa(struct ext4_allocation_context
*ac
,
3120 struct ext4_prealloc_space
*pa
)
3122 unsigned int len
= ac
->ac_o_ex
.fe_len
;
3123 ext4_get_group_no_and_offset(ac
->ac_sb
, pa
->pa_pstart
,
3124 &ac
->ac_b_ex
.fe_group
,
3125 &ac
->ac_b_ex
.fe_start
);
3126 ac
->ac_b_ex
.fe_len
= len
;
3127 ac
->ac_status
= AC_STATUS_FOUND
;
3130 /* we don't correct pa_pstart or pa_plen here to avoid
3131 * possible race when the group is being loaded concurrently
3132 * instead we correct pa later, after blocks are marked
3133 * in on-disk bitmap -- see ext4_mb_release_context()
3134 * Other CPUs are prevented from allocating from this pa by lg_mutex
3136 mb_debug("use %u/%u from group pa %p\n", pa
->pa_lstart
-len
, len
, pa
);
3140 * search goal blocks in preallocated space
3142 static noinline_for_stack
int
3143 ext4_mb_use_preallocated(struct ext4_allocation_context
*ac
)
3145 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3146 struct ext4_locality_group
*lg
;
3147 struct ext4_prealloc_space
*pa
;
3149 /* only data can be preallocated */
3150 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3153 /* first, try per-file preallocation */
3155 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3157 /* all fields in this condition don't change,
3158 * so we can skip locking for them */
3159 if (ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
||
3160 ac
->ac_o_ex
.fe_logical
>= pa
->pa_lstart
+ pa
->pa_len
)
3163 /* found preallocated blocks, use them */
3164 spin_lock(&pa
->pa_lock
);
3165 if (pa
->pa_deleted
== 0 && pa
->pa_free
) {
3166 atomic_inc(&pa
->pa_count
);
3167 ext4_mb_use_inode_pa(ac
, pa
);
3168 spin_unlock(&pa
->pa_lock
);
3169 ac
->ac_criteria
= 10;
3173 spin_unlock(&pa
->pa_lock
);
3177 /* can we use group allocation? */
3178 if (!(ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
))
3181 /* inode may have no locality group for some reason */
3187 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
, pa_inode_list
) {
3188 spin_lock(&pa
->pa_lock
);
3189 if (pa
->pa_deleted
== 0 && pa
->pa_free
>= ac
->ac_o_ex
.fe_len
) {
3190 atomic_inc(&pa
->pa_count
);
3191 ext4_mb_use_group_pa(ac
, pa
);
3192 spin_unlock(&pa
->pa_lock
);
3193 ac
->ac_criteria
= 20;
3197 spin_unlock(&pa
->pa_lock
);
3205 * the function goes through all preallocation in this group and marks them
3206 * used in in-core bitmap. buddy must be generated from this bitmap
3207 * Need to be called with ext4 group lock (ext4_lock_group)
3209 static void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
3212 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3213 struct ext4_prealloc_space
*pa
;
3214 struct list_head
*cur
;
3215 ext4_group_t groupnr
;
3216 ext4_grpblk_t start
;
3217 int preallocated
= 0;
3221 /* all form of preallocation discards first load group,
3222 * so the only competing code is preallocation use.
3223 * we don't need any locking here
3224 * notice we do NOT ignore preallocations with pa_deleted
3225 * otherwise we could leave used blocks available for
3226 * allocation in buddy when concurrent ext4_mb_put_pa()
3227 * is dropping preallocation
3229 list_for_each(cur
, &grp
->bb_prealloc_list
) {
3230 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
3231 spin_lock(&pa
->pa_lock
);
3232 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
3235 spin_unlock(&pa
->pa_lock
);
3236 if (unlikely(len
== 0))
3238 BUG_ON(groupnr
!= group
);
3239 mb_set_bits(sb_bgl_lock(EXT4_SB(sb
), group
),
3240 bitmap
, start
, len
);
3241 preallocated
+= len
;
3244 mb_debug("prellocated %u for group %lu\n", preallocated
, group
);
3247 static void ext4_mb_pa_callback(struct rcu_head
*head
)
3249 struct ext4_prealloc_space
*pa
;
3250 pa
= container_of(head
, struct ext4_prealloc_space
, u
.pa_rcu
);
3251 kmem_cache_free(ext4_pspace_cachep
, pa
);
3255 * drops a reference to preallocated space descriptor
3256 * if this was the last reference and the space is consumed
3258 static void ext4_mb_put_pa(struct ext4_allocation_context
*ac
,
3259 struct super_block
*sb
, struct ext4_prealloc_space
*pa
)
3263 if (!atomic_dec_and_test(&pa
->pa_count
) || pa
->pa_free
!= 0)
3266 /* in this short window concurrent discard can set pa_deleted */
3267 spin_lock(&pa
->pa_lock
);
3268 if (pa
->pa_deleted
== 1) {
3269 spin_unlock(&pa
->pa_lock
);
3274 spin_unlock(&pa
->pa_lock
);
3276 /* -1 is to protect from crossing allocation group */
3277 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
- 1, &grp
, NULL
);
3282 * P1 (buddy init) P2 (regular allocation)
3283 * find block B in PA
3284 * copy on-disk bitmap to buddy
3285 * mark B in on-disk bitmap
3286 * drop PA from group
3287 * mark all PAs in buddy
3289 * thus, P1 initializes buddy with B available. to prevent this
3290 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3293 ext4_lock_group(sb
, grp
);
3294 list_del(&pa
->pa_group_list
);
3295 ext4_unlock_group(sb
, grp
);
3297 spin_lock(pa
->pa_obj_lock
);
3298 list_del_rcu(&pa
->pa_inode_list
);
3299 spin_unlock(pa
->pa_obj_lock
);
3301 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3305 * creates new preallocated space for given inode
3307 static noinline_for_stack
int
3308 ext4_mb_new_inode_pa(struct ext4_allocation_context
*ac
)
3310 struct super_block
*sb
= ac
->ac_sb
;
3311 struct ext4_prealloc_space
*pa
;
3312 struct ext4_group_info
*grp
;
3313 struct ext4_inode_info
*ei
;
3315 /* preallocate only when found space is larger then requested */
3316 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3317 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3318 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3320 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3324 if (ac
->ac_b_ex
.fe_len
< ac
->ac_g_ex
.fe_len
) {
3330 /* we can't allocate as much as normalizer wants.
3331 * so, found space must get proper lstart
3332 * to cover original request */
3333 BUG_ON(ac
->ac_g_ex
.fe_logical
> ac
->ac_o_ex
.fe_logical
);
3334 BUG_ON(ac
->ac_g_ex
.fe_len
< ac
->ac_o_ex
.fe_len
);
3336 /* we're limited by original request in that
3337 * logical block must be covered any way
3338 * winl is window we can move our chunk within */
3339 winl
= ac
->ac_o_ex
.fe_logical
- ac
->ac_g_ex
.fe_logical
;
3341 /* also, we should cover whole original request */
3342 wins
= ac
->ac_b_ex
.fe_len
- ac
->ac_o_ex
.fe_len
;
3344 /* the smallest one defines real window */
3345 win
= min(winl
, wins
);
3347 offs
= ac
->ac_o_ex
.fe_logical
% ac
->ac_b_ex
.fe_len
;
3348 if (offs
&& offs
< win
)
3351 ac
->ac_b_ex
.fe_logical
= ac
->ac_o_ex
.fe_logical
- win
;
3352 BUG_ON(ac
->ac_o_ex
.fe_logical
< ac
->ac_b_ex
.fe_logical
);
3353 BUG_ON(ac
->ac_o_ex
.fe_len
> ac
->ac_b_ex
.fe_len
);
3356 /* preallocation can change ac_b_ex, thus we store actually
3357 * allocated blocks for history */
3358 ac
->ac_f_ex
= ac
->ac_b_ex
;
3360 pa
->pa_lstart
= ac
->ac_b_ex
.fe_logical
;
3361 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3362 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3363 pa
->pa_free
= pa
->pa_len
;
3364 atomic_set(&pa
->pa_count
, 1);
3365 spin_lock_init(&pa
->pa_lock
);
3369 mb_debug("new inode pa %p: %llu/%u for %u\n", pa
,
3370 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3372 ext4_mb_use_inode_pa(ac
, pa
);
3373 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3375 ei
= EXT4_I(ac
->ac_inode
);
3376 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3378 pa
->pa_obj_lock
= &ei
->i_prealloc_lock
;
3379 pa
->pa_inode
= ac
->ac_inode
;
3381 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3382 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3383 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3385 spin_lock(pa
->pa_obj_lock
);
3386 list_add_rcu(&pa
->pa_inode_list
, &ei
->i_prealloc_list
);
3387 spin_unlock(pa
->pa_obj_lock
);
3393 * creates new preallocated space for locality group inodes belongs to
3395 static noinline_for_stack
int
3396 ext4_mb_new_group_pa(struct ext4_allocation_context
*ac
)
3398 struct super_block
*sb
= ac
->ac_sb
;
3399 struct ext4_locality_group
*lg
;
3400 struct ext4_prealloc_space
*pa
;
3401 struct ext4_group_info
*grp
;
3403 /* preallocate only when found space is larger then requested */
3404 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3405 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3406 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3408 BUG_ON(ext4_pspace_cachep
== NULL
);
3409 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3413 /* preallocation can change ac_b_ex, thus we store actually
3414 * allocated blocks for history */
3415 ac
->ac_f_ex
= ac
->ac_b_ex
;
3417 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3418 pa
->pa_lstart
= pa
->pa_pstart
;
3419 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3420 pa
->pa_free
= pa
->pa_len
;
3421 atomic_set(&pa
->pa_count
, 1);
3422 spin_lock_init(&pa
->pa_lock
);
3426 mb_debug("new group pa %p: %llu/%u for %u\n", pa
,
3427 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3429 ext4_mb_use_group_pa(ac
, pa
);
3430 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3432 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3436 pa
->pa_obj_lock
= &lg
->lg_prealloc_lock
;
3437 pa
->pa_inode
= NULL
;
3439 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3440 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3441 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3443 spin_lock(pa
->pa_obj_lock
);
3444 list_add_tail_rcu(&pa
->pa_inode_list
, &lg
->lg_prealloc_list
);
3445 spin_unlock(pa
->pa_obj_lock
);
3450 static int ext4_mb_new_preallocation(struct ext4_allocation_context
*ac
)
3454 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
3455 err
= ext4_mb_new_group_pa(ac
);
3457 err
= ext4_mb_new_inode_pa(ac
);
3462 * finds all unused blocks in on-disk bitmap, frees them in
3463 * in-core bitmap and buddy.
3464 * @pa must be unlinked from inode and group lists, so that
3465 * nobody else can find/use it.
3466 * the caller MUST hold group/inode locks.
3467 * TODO: optimize the case when there are no in-core structures yet
3469 static noinline_for_stack
int
3470 ext4_mb_release_inode_pa(struct ext4_buddy
*e4b
, struct buffer_head
*bitmap_bh
,
3471 struct ext4_prealloc_space
*pa
,
3472 struct ext4_allocation_context
*ac
)
3474 struct super_block
*sb
= e4b
->bd_sb
;
3475 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3484 BUG_ON(pa
->pa_deleted
== 0);
3485 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3486 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3487 end
= bit
+ pa
->pa_len
;
3491 ac
->ac_inode
= pa
->pa_inode
;
3492 ac
->ac_op
= EXT4_MB_HISTORY_DISCARD
;
3496 bit
= mb_find_next_zero_bit(bitmap_bh
->b_data
, end
, bit
);
3499 next
= mb_find_next_bit(bitmap_bh
->b_data
, end
, bit
);
3500 start
= group
* EXT4_BLOCKS_PER_GROUP(sb
) + bit
+
3501 le32_to_cpu(sbi
->s_es
->s_first_data_block
);
3502 mb_debug(" free preallocated %u/%u in group %u\n",
3503 (unsigned) start
, (unsigned) next
- bit
,
3508 ac
->ac_b_ex
.fe_group
= group
;
3509 ac
->ac_b_ex
.fe_start
= bit
;
3510 ac
->ac_b_ex
.fe_len
= next
- bit
;
3511 ac
->ac_b_ex
.fe_logical
= 0;
3512 ext4_mb_store_history(ac
);
3515 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, next
- bit
);
3518 if (free
!= pa
->pa_free
) {
3519 printk(KERN_CRIT
"pa %p: logic %lu, phys. %lu, len %lu\n",
3520 pa
, (unsigned long) pa
->pa_lstart
,
3521 (unsigned long) pa
->pa_pstart
,
3522 (unsigned long) pa
->pa_len
);
3523 ext4_error(sb
, __func__
, "free %u, pa_free %u\n",
3526 * pa is already deleted so we use the value obtained
3527 * from the bitmap and continue.
3530 atomic_add(free
, &sbi
->s_mb_discarded
);
3535 static noinline_for_stack
int
3536 ext4_mb_release_group_pa(struct ext4_buddy
*e4b
,
3537 struct ext4_prealloc_space
*pa
,
3538 struct ext4_allocation_context
*ac
)
3540 struct super_block
*sb
= e4b
->bd_sb
;
3545 ac
->ac_op
= EXT4_MB_HISTORY_DISCARD
;
3547 BUG_ON(pa
->pa_deleted
== 0);
3548 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3549 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3550 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, pa
->pa_len
);
3551 atomic_add(pa
->pa_len
, &EXT4_SB(sb
)->s_mb_discarded
);
3555 ac
->ac_inode
= NULL
;
3556 ac
->ac_b_ex
.fe_group
= group
;
3557 ac
->ac_b_ex
.fe_start
= bit
;
3558 ac
->ac_b_ex
.fe_len
= pa
->pa_len
;
3559 ac
->ac_b_ex
.fe_logical
= 0;
3560 ext4_mb_store_history(ac
);
3567 * releases all preallocations in given group
3569 * first, we need to decide discard policy:
3570 * - when do we discard
3572 * - how many do we discard
3573 * 1) how many requested
3575 static noinline_for_stack
int
3576 ext4_mb_discard_group_preallocations(struct super_block
*sb
,
3577 ext4_group_t group
, int needed
)
3579 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3580 struct buffer_head
*bitmap_bh
= NULL
;
3581 struct ext4_prealloc_space
*pa
, *tmp
;
3582 struct ext4_allocation_context
*ac
;
3583 struct list_head list
;
3584 struct ext4_buddy e4b
;
3589 mb_debug("discard preallocation for group %lu\n", group
);
3591 if (list_empty(&grp
->bb_prealloc_list
))
3594 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
3595 if (bitmap_bh
== NULL
) {
3596 /* error handling here */
3597 ext4_mb_release_desc(&e4b
);
3598 BUG_ON(bitmap_bh
== NULL
);
3601 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
3602 BUG_ON(err
!= 0); /* error handling here */
3605 needed
= EXT4_BLOCKS_PER_GROUP(sb
) + 1;
3607 grp
= ext4_get_group_info(sb
, group
);
3608 INIT_LIST_HEAD(&list
);
3610 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
3612 ext4_lock_group(sb
, group
);
3613 list_for_each_entry_safe(pa
, tmp
,
3614 &grp
->bb_prealloc_list
, pa_group_list
) {
3615 spin_lock(&pa
->pa_lock
);
3616 if (atomic_read(&pa
->pa_count
)) {
3617 spin_unlock(&pa
->pa_lock
);
3621 if (pa
->pa_deleted
) {
3622 spin_unlock(&pa
->pa_lock
);
3626 /* seems this one can be freed ... */
3629 /* we can trust pa_free ... */
3630 free
+= pa
->pa_free
;
3632 spin_unlock(&pa
->pa_lock
);
3634 list_del(&pa
->pa_group_list
);
3635 list_add(&pa
->u
.pa_tmp_list
, &list
);
3638 /* if we still need more blocks and some PAs were used, try again */
3639 if (free
< needed
&& busy
) {
3641 ext4_unlock_group(sb
, group
);
3643 * Yield the CPU here so that we don't get soft lockup
3644 * in non preempt case.
3650 /* found anything to free? */
3651 if (list_empty(&list
)) {
3656 /* now free all selected PAs */
3657 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
3659 /* remove from object (inode or locality group) */
3660 spin_lock(pa
->pa_obj_lock
);
3661 list_del_rcu(&pa
->pa_inode_list
);
3662 spin_unlock(pa
->pa_obj_lock
);
3665 ext4_mb_release_group_pa(&e4b
, pa
, ac
);
3667 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
, ac
);
3669 list_del(&pa
->u
.pa_tmp_list
);
3670 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3674 ext4_unlock_group(sb
, group
);
3676 kmem_cache_free(ext4_ac_cachep
, ac
);
3677 ext4_mb_release_desc(&e4b
);
3683 * releases all non-used preallocated blocks for given inode
3685 * It's important to discard preallocations under i_data_sem
3686 * We don't want another block to be served from the prealloc
3687 * space when we are discarding the inode prealloc space.
3689 * FIXME!! Make sure it is valid at all the call sites
3691 void ext4_mb_discard_inode_preallocations(struct inode
*inode
)
3693 struct ext4_inode_info
*ei
= EXT4_I(inode
);
3694 struct super_block
*sb
= inode
->i_sb
;
3695 struct buffer_head
*bitmap_bh
= NULL
;
3696 struct ext4_prealloc_space
*pa
, *tmp
;
3697 struct ext4_allocation_context
*ac
;
3698 ext4_group_t group
= 0;
3699 struct list_head list
;
3700 struct ext4_buddy e4b
;
3703 if (!test_opt(sb
, MBALLOC
) || !S_ISREG(inode
->i_mode
)) {
3704 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3708 mb_debug("discard preallocation for inode %lu\n", inode
->i_ino
);
3710 INIT_LIST_HEAD(&list
);
3712 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
3714 /* first, collect all pa's in the inode */
3715 spin_lock(&ei
->i_prealloc_lock
);
3716 while (!list_empty(&ei
->i_prealloc_list
)) {
3717 pa
= list_entry(ei
->i_prealloc_list
.next
,
3718 struct ext4_prealloc_space
, pa_inode_list
);
3719 BUG_ON(pa
->pa_obj_lock
!= &ei
->i_prealloc_lock
);
3720 spin_lock(&pa
->pa_lock
);
3721 if (atomic_read(&pa
->pa_count
)) {
3722 /* this shouldn't happen often - nobody should
3723 * use preallocation while we're discarding it */
3724 spin_unlock(&pa
->pa_lock
);
3725 spin_unlock(&ei
->i_prealloc_lock
);
3726 printk(KERN_ERR
"uh-oh! used pa while discarding\n");
3728 schedule_timeout_uninterruptible(HZ
);
3732 if (pa
->pa_deleted
== 0) {
3734 spin_unlock(&pa
->pa_lock
);
3735 list_del_rcu(&pa
->pa_inode_list
);
3736 list_add(&pa
->u
.pa_tmp_list
, &list
);
3740 /* someone is deleting pa right now */
3741 spin_unlock(&pa
->pa_lock
);
3742 spin_unlock(&ei
->i_prealloc_lock
);
3744 /* we have to wait here because pa_deleted
3745 * doesn't mean pa is already unlinked from
3746 * the list. as we might be called from
3747 * ->clear_inode() the inode will get freed
3748 * and concurrent thread which is unlinking
3749 * pa from inode's list may access already
3750 * freed memory, bad-bad-bad */
3752 /* XXX: if this happens too often, we can
3753 * add a flag to force wait only in case
3754 * of ->clear_inode(), but not in case of
3755 * regular truncate */
3756 schedule_timeout_uninterruptible(HZ
);
3759 spin_unlock(&ei
->i_prealloc_lock
);
3761 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
3762 BUG_ON(pa
->pa_linear
!= 0);
3763 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
3765 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
3766 BUG_ON(err
!= 0); /* error handling here */
3768 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
3769 if (bitmap_bh
== NULL
) {
3770 /* error handling here */
3771 ext4_mb_release_desc(&e4b
);
3772 BUG_ON(bitmap_bh
== NULL
);
3775 ext4_lock_group(sb
, group
);
3776 list_del(&pa
->pa_group_list
);
3777 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
, ac
);
3778 ext4_unlock_group(sb
, group
);
3780 ext4_mb_release_desc(&e4b
);
3783 list_del(&pa
->u
.pa_tmp_list
);
3784 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3787 kmem_cache_free(ext4_ac_cachep
, ac
);
3791 * finds all preallocated spaces and return blocks being freed to them
3792 * if preallocated space becomes full (no block is used from the space)
3793 * then the function frees space in buddy
3794 * XXX: at the moment, truncate (which is the only way to free blocks)
3795 * discards all preallocations
3797 static void ext4_mb_return_to_preallocation(struct inode
*inode
,
3798 struct ext4_buddy
*e4b
,
3799 sector_t block
, int count
)
3801 BUG_ON(!list_empty(&EXT4_I(inode
)->i_prealloc_list
));
3804 static void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
3806 struct super_block
*sb
= ac
->ac_sb
;
3809 printk(KERN_ERR
"EXT4-fs: Can't allocate:"
3810 " Allocation context details:\n");
3811 printk(KERN_ERR
"EXT4-fs: status %d flags %d\n",
3812 ac
->ac_status
, ac
->ac_flags
);
3813 printk(KERN_ERR
"EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
3814 "best %lu/%lu/%lu@%lu cr %d\n",
3815 (unsigned long)ac
->ac_o_ex
.fe_group
,
3816 (unsigned long)ac
->ac_o_ex
.fe_start
,
3817 (unsigned long)ac
->ac_o_ex
.fe_len
,
3818 (unsigned long)ac
->ac_o_ex
.fe_logical
,
3819 (unsigned long)ac
->ac_g_ex
.fe_group
,
3820 (unsigned long)ac
->ac_g_ex
.fe_start
,
3821 (unsigned long)ac
->ac_g_ex
.fe_len
,
3822 (unsigned long)ac
->ac_g_ex
.fe_logical
,
3823 (unsigned long)ac
->ac_b_ex
.fe_group
,
3824 (unsigned long)ac
->ac_b_ex
.fe_start
,
3825 (unsigned long)ac
->ac_b_ex
.fe_len
,
3826 (unsigned long)ac
->ac_b_ex
.fe_logical
,
3827 (int)ac
->ac_criteria
);
3828 printk(KERN_ERR
"EXT4-fs: %lu scanned, %d found\n", ac
->ac_ex_scanned
,
3830 printk(KERN_ERR
"EXT4-fs: groups: \n");
3831 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; i
++) {
3832 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, i
);
3833 struct ext4_prealloc_space
*pa
;
3834 ext4_grpblk_t start
;
3835 struct list_head
*cur
;
3836 ext4_lock_group(sb
, i
);
3837 list_for_each(cur
, &grp
->bb_prealloc_list
) {
3838 pa
= list_entry(cur
, struct ext4_prealloc_space
,
3840 spin_lock(&pa
->pa_lock
);
3841 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
3843 spin_unlock(&pa
->pa_lock
);
3844 printk(KERN_ERR
"PA:%lu:%d:%u \n", i
,
3847 ext4_unlock_group(sb
, i
);
3849 if (grp
->bb_free
== 0)
3851 printk(KERN_ERR
"%lu: %d/%d \n",
3852 i
, grp
->bb_free
, grp
->bb_fragments
);
3854 printk(KERN_ERR
"\n");
3857 static inline void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
3864 * We use locality group preallocation for small size file. The size of the
3865 * file is determined by the current size or the resulting size after
3866 * allocation which ever is larger
3868 * One can tune this size via /proc/fs/ext4/<partition>/stream_req
3870 static void ext4_mb_group_or_file(struct ext4_allocation_context
*ac
)
3872 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3873 int bsbits
= ac
->ac_sb
->s_blocksize_bits
;
3876 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3879 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
3880 isize
= i_size_read(ac
->ac_inode
) >> bsbits
;
3881 size
= max(size
, isize
);
3883 /* don't use group allocation for large files */
3884 if (size
>= sbi
->s_mb_stream_request
)
3887 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
3890 BUG_ON(ac
->ac_lg
!= NULL
);
3892 * locality group prealloc space are per cpu. The reason for having
3893 * per cpu locality group is to reduce the contention between block
3894 * request from multiple CPUs.
3896 ac
->ac_lg
= &sbi
->s_locality_groups
[get_cpu()];
3899 /* we're going to use group allocation */
3900 ac
->ac_flags
|= EXT4_MB_HINT_GROUP_ALLOC
;
3902 /* serialize all allocations in the group */
3903 mutex_lock(&ac
->ac_lg
->lg_mutex
);
3906 static noinline_for_stack
int
3907 ext4_mb_initialize_context(struct ext4_allocation_context
*ac
,
3908 struct ext4_allocation_request
*ar
)
3910 struct super_block
*sb
= ar
->inode
->i_sb
;
3911 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3912 struct ext4_super_block
*es
= sbi
->s_es
;
3916 ext4_grpblk_t block
;
3918 /* we can't allocate > group size */
3921 /* just a dirty hack to filter too big requests */
3922 if (len
>= EXT4_BLOCKS_PER_GROUP(sb
) - 10)
3923 len
= EXT4_BLOCKS_PER_GROUP(sb
) - 10;
3925 /* start searching from the goal */
3927 if (goal
< le32_to_cpu(es
->s_first_data_block
) ||
3928 goal
>= ext4_blocks_count(es
))
3929 goal
= le32_to_cpu(es
->s_first_data_block
);
3930 ext4_get_group_no_and_offset(sb
, goal
, &group
, &block
);
3932 /* set up allocation goals */
3933 ac
->ac_b_ex
.fe_logical
= ar
->logical
;
3934 ac
->ac_b_ex
.fe_group
= 0;
3935 ac
->ac_b_ex
.fe_start
= 0;
3936 ac
->ac_b_ex
.fe_len
= 0;
3937 ac
->ac_status
= AC_STATUS_CONTINUE
;
3938 ac
->ac_groups_scanned
= 0;
3939 ac
->ac_ex_scanned
= 0;
3942 ac
->ac_inode
= ar
->inode
;
3943 ac
->ac_o_ex
.fe_logical
= ar
->logical
;
3944 ac
->ac_o_ex
.fe_group
= group
;
3945 ac
->ac_o_ex
.fe_start
= block
;
3946 ac
->ac_o_ex
.fe_len
= len
;
3947 ac
->ac_g_ex
.fe_logical
= ar
->logical
;
3948 ac
->ac_g_ex
.fe_group
= group
;
3949 ac
->ac_g_ex
.fe_start
= block
;
3950 ac
->ac_g_ex
.fe_len
= len
;
3951 ac
->ac_f_ex
.fe_len
= 0;
3952 ac
->ac_flags
= ar
->flags
;
3954 ac
->ac_criteria
= 0;
3956 ac
->ac_bitmap_page
= NULL
;
3957 ac
->ac_buddy_page
= NULL
;
3960 /* we have to define context: we'll we work with a file or
3961 * locality group. this is a policy, actually */
3962 ext4_mb_group_or_file(ac
);
3964 mb_debug("init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
3965 "left: %u/%u, right %u/%u to %swritable\n",
3966 (unsigned) ar
->len
, (unsigned) ar
->logical
,
3967 (unsigned) ar
->goal
, ac
->ac_flags
, ac
->ac_2order
,
3968 (unsigned) ar
->lleft
, (unsigned) ar
->pleft
,
3969 (unsigned) ar
->lright
, (unsigned) ar
->pright
,
3970 atomic_read(&ar
->inode
->i_writecount
) ? "" : "non-");
3976 * release all resource we used in allocation
3978 static int ext4_mb_release_context(struct ext4_allocation_context
*ac
)
3981 if (ac
->ac_pa
->pa_linear
) {
3982 /* see comment in ext4_mb_use_group_pa() */
3983 spin_lock(&ac
->ac_pa
->pa_lock
);
3984 ac
->ac_pa
->pa_pstart
+= ac
->ac_b_ex
.fe_len
;
3985 ac
->ac_pa
->pa_lstart
+= ac
->ac_b_ex
.fe_len
;
3986 ac
->ac_pa
->pa_free
-= ac
->ac_b_ex
.fe_len
;
3987 ac
->ac_pa
->pa_len
-= ac
->ac_b_ex
.fe_len
;
3988 spin_unlock(&ac
->ac_pa
->pa_lock
);
3990 ext4_mb_put_pa(ac
, ac
->ac_sb
, ac
->ac_pa
);
3992 if (ac
->ac_bitmap_page
)
3993 page_cache_release(ac
->ac_bitmap_page
);
3994 if (ac
->ac_buddy_page
)
3995 page_cache_release(ac
->ac_buddy_page
);
3996 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
3997 mutex_unlock(&ac
->ac_lg
->lg_mutex
);
3998 ext4_mb_collect_stats(ac
);
4002 static int ext4_mb_discard_preallocations(struct super_block
*sb
, int needed
)
4008 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
&& needed
> 0; i
++) {
4009 ret
= ext4_mb_discard_group_preallocations(sb
, i
, needed
);
4018 * Main entry point into mballoc to allocate blocks
4019 * it tries to use preallocation first, then falls back
4020 * to usual allocation
4022 ext4_fsblk_t
ext4_mb_new_blocks(handle_t
*handle
,
4023 struct ext4_allocation_request
*ar
, int *errp
)
4025 struct ext4_allocation_context
*ac
= NULL
;
4026 struct ext4_sb_info
*sbi
;
4027 struct super_block
*sb
;
4028 ext4_fsblk_t block
= 0;
4032 sb
= ar
->inode
->i_sb
;
4035 if (!test_opt(sb
, MBALLOC
)) {
4036 block
= ext4_new_blocks_old(handle
, ar
->inode
, ar
->goal
,
4041 while (ar
->len
&& DQUOT_ALLOC_BLOCK(ar
->inode
, ar
->len
)) {
4042 ar
->flags
|= EXT4_MB_HINT_NOPREALLOC
;
4051 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4057 ext4_mb_poll_new_transaction(sb
, handle
);
4059 *errp
= ext4_mb_initialize_context(ac
, ar
);
4065 ac
->ac_op
= EXT4_MB_HISTORY_PREALLOC
;
4066 if (!ext4_mb_use_preallocated(ac
)) {
4068 ac
->ac_op
= EXT4_MB_HISTORY_ALLOC
;
4069 ext4_mb_normalize_request(ac
, ar
);
4071 /* allocate space in core */
4072 ext4_mb_regular_allocator(ac
);
4074 /* as we've just preallocated more space than
4075 * user requested orinally, we store allocated
4076 * space in a special descriptor */
4077 if (ac
->ac_status
== AC_STATUS_FOUND
&&
4078 ac
->ac_o_ex
.fe_len
< ac
->ac_b_ex
.fe_len
)
4079 ext4_mb_new_preallocation(ac
);
4082 if (likely(ac
->ac_status
== AC_STATUS_FOUND
)) {
4083 *errp
= ext4_mb_mark_diskspace_used(ac
, handle
);
4084 if (*errp
== -EAGAIN
) {
4085 ac
->ac_b_ex
.fe_group
= 0;
4086 ac
->ac_b_ex
.fe_start
= 0;
4087 ac
->ac_b_ex
.fe_len
= 0;
4088 ac
->ac_status
= AC_STATUS_CONTINUE
;
4091 ac
->ac_b_ex
.fe_len
= 0;
4093 ext4_mb_show_ac(ac
);
4095 block
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
4096 ar
->len
= ac
->ac_b_ex
.fe_len
;
4099 freed
= ext4_mb_discard_preallocations(sb
, ac
->ac_o_ex
.fe_len
);
4103 ac
->ac_b_ex
.fe_len
= 0;
4105 ext4_mb_show_ac(ac
);
4108 ext4_mb_release_context(ac
);
4111 if (ar
->len
< inquota
)
4112 DQUOT_FREE_BLOCK(ar
->inode
, inquota
- ar
->len
);
4114 kmem_cache_free(ext4_ac_cachep
, ac
);
4117 static void ext4_mb_poll_new_transaction(struct super_block
*sb
,
4120 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4122 if (sbi
->s_last_transaction
== handle
->h_transaction
->t_tid
)
4125 /* new transaction! time to close last one and free blocks for
4126 * committed transaction. we know that only transaction can be
4127 * active, so previos transaction can be being logged and we
4128 * know that transaction before previous is known to be already
4129 * logged. this means that now we may free blocks freed in all
4130 * transactions before previous one. hope I'm clear enough ... */
4132 spin_lock(&sbi
->s_md_lock
);
4133 if (sbi
->s_last_transaction
!= handle
->h_transaction
->t_tid
) {
4134 mb_debug("new transaction %lu, old %lu\n",
4135 (unsigned long) handle
->h_transaction
->t_tid
,
4136 (unsigned long) sbi
->s_last_transaction
);
4137 list_splice_init(&sbi
->s_closed_transaction
,
4138 &sbi
->s_committed_transaction
);
4139 list_splice_init(&sbi
->s_active_transaction
,
4140 &sbi
->s_closed_transaction
);
4141 sbi
->s_last_transaction
= handle
->h_transaction
->t_tid
;
4143 spin_unlock(&sbi
->s_md_lock
);
4145 ext4_mb_free_committed_blocks(sb
);
4148 static noinline_for_stack
int
4149 ext4_mb_free_metadata(handle_t
*handle
, struct ext4_buddy
*e4b
,
4150 ext4_group_t group
, ext4_grpblk_t block
, int count
)
4152 struct ext4_group_info
*db
= e4b
->bd_info
;
4153 struct super_block
*sb
= e4b
->bd_sb
;
4154 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4155 struct ext4_free_metadata
*md
;
4158 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
4159 BUG_ON(e4b
->bd_buddy_page
== NULL
);
4161 ext4_lock_group(sb
, group
);
4162 for (i
= 0; i
< count
; i
++) {
4164 if (md
&& db
->bb_tid
!= handle
->h_transaction
->t_tid
) {
4165 db
->bb_md_cur
= NULL
;
4170 ext4_unlock_group(sb
, group
);
4171 md
= kmalloc(sizeof(*md
), GFP_NOFS
);
4177 ext4_lock_group(sb
, group
);
4178 if (db
->bb_md_cur
== NULL
) {
4179 spin_lock(&sbi
->s_md_lock
);
4180 list_add(&md
->list
, &sbi
->s_active_transaction
);
4181 spin_unlock(&sbi
->s_md_lock
);
4182 /* protect buddy cache from being freed,
4183 * otherwise we'll refresh it from
4184 * on-disk bitmap and lose not-yet-available
4186 page_cache_get(e4b
->bd_buddy_page
);
4187 page_cache_get(e4b
->bd_bitmap_page
);
4189 db
->bb_tid
= handle
->h_transaction
->t_tid
;
4190 mb_debug("new md 0x%p for group %lu\n",
4198 BUG_ON(md
->num
>= EXT4_BB_MAX_BLOCKS
);
4199 md
->blocks
[md
->num
] = block
+ i
;
4201 if (md
->num
== EXT4_BB_MAX_BLOCKS
) {
4202 /* no more space, put full container on a sb's list */
4203 db
->bb_md_cur
= NULL
;
4206 ext4_unlock_group(sb
, group
);
4211 * Main entry point into mballoc to free blocks
4213 void ext4_mb_free_blocks(handle_t
*handle
, struct inode
*inode
,
4214 unsigned long block
, unsigned long count
,
4215 int metadata
, unsigned long *freed
)
4217 struct buffer_head
*bitmap_bh
= NULL
;
4218 struct super_block
*sb
= inode
->i_sb
;
4219 struct ext4_allocation_context
*ac
= NULL
;
4220 struct ext4_group_desc
*gdp
;
4221 struct ext4_super_block
*es
;
4222 unsigned long overflow
;
4224 struct buffer_head
*gd_bh
;
4225 ext4_group_t block_group
;
4226 struct ext4_sb_info
*sbi
;
4227 struct ext4_buddy e4b
;
4233 ext4_mb_poll_new_transaction(sb
, handle
);
4236 es
= EXT4_SB(sb
)->s_es
;
4237 if (block
< le32_to_cpu(es
->s_first_data_block
) ||
4238 block
+ count
< block
||
4239 block
+ count
> ext4_blocks_count(es
)) {
4240 ext4_error(sb
, __func__
,
4241 "Freeing blocks not in datazone - "
4242 "block = %lu, count = %lu", block
, count
);
4246 ext4_debug("freeing block %lu\n", block
);
4248 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4250 ac
->ac_op
= EXT4_MB_HISTORY_FREE
;
4251 ac
->ac_inode
= inode
;
4257 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
4260 * Check to see if we are freeing blocks across a group
4263 if (bit
+ count
> EXT4_BLOCKS_PER_GROUP(sb
)) {
4264 overflow
= bit
+ count
- EXT4_BLOCKS_PER_GROUP(sb
);
4267 bitmap_bh
= ext4_read_block_bitmap(sb
, block_group
);
4270 gdp
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
4274 if (in_range(ext4_block_bitmap(sb
, gdp
), block
, count
) ||
4275 in_range(ext4_inode_bitmap(sb
, gdp
), block
, count
) ||
4276 in_range(block
, ext4_inode_table(sb
, gdp
),
4277 EXT4_SB(sb
)->s_itb_per_group
) ||
4278 in_range(block
+ count
- 1, ext4_inode_table(sb
, gdp
),
4279 EXT4_SB(sb
)->s_itb_per_group
)) {
4281 ext4_error(sb
, __func__
,
4282 "Freeing blocks in system zone - "
4283 "Block = %lu, count = %lu", block
, count
);
4284 /* err = 0. ext4_std_error should be a no op */
4288 BUFFER_TRACE(bitmap_bh
, "getting write access");
4289 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
4294 * We are about to modify some metadata. Call the journal APIs
4295 * to unshare ->b_data if a currently-committing transaction is
4298 BUFFER_TRACE(gd_bh
, "get_write_access");
4299 err
= ext4_journal_get_write_access(handle
, gd_bh
);
4303 err
= ext4_mb_load_buddy(sb
, block_group
, &e4b
);
4307 #ifdef AGGRESSIVE_CHECK
4310 for (i
= 0; i
< count
; i
++)
4311 BUG_ON(!mb_test_bit(bit
+ i
, bitmap_bh
->b_data
));
4314 mb_clear_bits(sb_bgl_lock(sbi
, block_group
), bitmap_bh
->b_data
,
4317 /* We dirtied the bitmap block */
4318 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
4319 err
= ext4_journal_dirty_metadata(handle
, bitmap_bh
);
4322 ac
->ac_b_ex
.fe_group
= block_group
;
4323 ac
->ac_b_ex
.fe_start
= bit
;
4324 ac
->ac_b_ex
.fe_len
= count
;
4325 ext4_mb_store_history(ac
);
4329 /* blocks being freed are metadata. these blocks shouldn't
4330 * be used until this transaction is committed */
4331 ext4_mb_free_metadata(handle
, &e4b
, block_group
, bit
, count
);
4333 ext4_lock_group(sb
, block_group
);
4334 mb_free_blocks(inode
, &e4b
, bit
, count
);
4335 ext4_mb_return_to_preallocation(inode
, &e4b
, block
, count
);
4336 ext4_unlock_group(sb
, block_group
);
4339 spin_lock(sb_bgl_lock(sbi
, block_group
));
4340 le16_add_cpu(&gdp
->bg_free_blocks_count
, count
);
4341 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, block_group
, gdp
);
4342 spin_unlock(sb_bgl_lock(sbi
, block_group
));
4343 percpu_counter_add(&sbi
->s_freeblocks_counter
, count
);
4345 ext4_mb_release_desc(&e4b
);
4349 /* And the group descriptor block */
4350 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
4351 ret
= ext4_journal_dirty_metadata(handle
, gd_bh
);
4355 if (overflow
&& !err
) {
4364 ext4_std_error(sb
, err
);
4366 kmem_cache_free(ext4_ac_cachep
, ac
);