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
24 #include <linux/time.h>
26 #include <linux/namei.h>
27 #include <linux/ext4_jbd2.h>
28 #include <linux/ext4_fs.h>
29 #include <linux/quotaops.h>
30 #include <linux/buffer_head.h>
31 #include <linux/module.h>
32 #include <linux/swap.h>
33 #include <linux/proc_fs.h>
34 #include <linux/pagemap.h>
35 #include <linux/seq_file.h>
36 #include <linux/version.h>
41 * - test ext4_ext_search_left() and ext4_ext_search_right()
42 * - search for metadata in few groups
45 * - normalization should take into account whether file is still open
46 * - discard preallocations if no free space left (policy?)
47 * - don't normalize tails
49 * - reservation for superuser
52 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
53 * - track min/max extents in each group for better group selection
54 * - mb_mark_used() may allocate chunk right after splitting buddy
55 * - tree of groups sorted by number of free blocks
60 * The allocation request involve request for multiple number of blocks
61 * near to the goal(block) value specified.
63 * During initialization phase of the allocator we decide to use the group
64 * preallocation or inode preallocation depending on the size file. The
65 * size of the file could be the resulting file size we would have after
66 * allocation or the current file size which ever is larger. If the size is
67 * less that sbi->s_mb_stream_request we select the group
68 * preallocation. The default value of s_mb_stream_request is 16
69 * blocks. This can also be tuned via
70 * /proc/fs/ext4/<partition>/stream_req. The value is represented in terms
71 * of number of blocks.
73 * The main motivation for having small file use group preallocation is to
74 * ensure that we have small file closer in the disk.
76 * First stage the allocator looks at the inode prealloc list
77 * ext4_inode_info->i_prealloc_list contain list of prealloc spaces for
78 * this particular inode. The inode prealloc space is represented as:
80 * pa_lstart -> the logical start block for this prealloc space
81 * pa_pstart -> the physical start block for this prealloc space
82 * pa_len -> lenght for this prealloc space
83 * pa_free -> free space available in this prealloc space
85 * The inode preallocation space is used looking at the _logical_ start
86 * block. If only the logical file block falls within the range of prealloc
87 * space we will consume the particular prealloc space. This make sure that
88 * that the we have contiguous physical blocks representing the file blocks
90 * The important thing to be noted in case of inode prealloc space is that
91 * we don't modify the values associated to inode prealloc space except
94 * If we are not able to find blocks in the inode prealloc space and if we
95 * have the group allocation flag set then we look at the locality group
96 * prealloc space. These are per CPU prealloc list repreasented as
98 * ext4_sb_info.s_locality_groups[smp_processor_id()]
100 * The reason for having a per cpu locality group is to reduce the contention
101 * between CPUs. It is possible to get scheduled at this point.
103 * The locality group prealloc space is used looking at whether we have
104 * enough free space (pa_free) withing the prealloc space.
106 * If we can't allocate blocks via inode prealloc or/and locality group
107 * prealloc then we look at the buddy cache. The buddy cache is represented
108 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
109 * mapped to the buddy and bitmap information regarding different
110 * groups. The buddy information is attached to buddy cache inode so that
111 * we can access them through the page cache. The information regarding
112 * each group is loaded via ext4_mb_load_buddy. The information involve
113 * block bitmap and buddy information. The information are stored in the
117 * [ group 0 buddy][ group 0 bitmap] [group 1][ group 1]...
120 * one block each for bitmap and buddy information. So for each group we
121 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
122 * blocksize) blocks. So it can have information regarding groups_per_page
123 * which is blocks_per_page/2
125 * The buddy cache inode is not stored on disk. The inode is thrown
126 * away when the filesystem is unmounted.
128 * We look for count number of blocks in the buddy cache. If we were able
129 * to locate that many free blocks we return with additional information
130 * regarding rest of the contiguous physical block available
132 * Before allocating blocks via buddy cache we normalize the request
133 * blocks. This ensure we ask for more blocks that we needed. The extra
134 * blocks that we get after allocation is added to the respective prealloc
135 * list. In case of inode preallocation we follow a list of heuristics
136 * based on file size. This can be found in ext4_mb_normalize_request. If
137 * we are doing a group prealloc we try to normalize the request to
138 * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is set to
139 * 512 blocks. This can be tuned via
140 * /proc/fs/ext4/<partition/group_prealloc. The value is represented in
141 * terms of number of blocks. If we have mounted the file system with -O
142 * stripe=<value> option the group prealloc request is normalized to the
143 * stripe value (sbi->s_stripe)
145 * The regular allocator(using the buddy cache) support few tunables.
147 * /proc/fs/ext4/<partition>/min_to_scan
148 * /proc/fs/ext4/<partition>/max_to_scan
149 * /proc/fs/ext4/<partition>/order2_req
151 * The regular allocator use buddy scan only if the request len is power of
152 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
153 * value of s_mb_order2_reqs can be tuned via
154 * /proc/fs/ext4/<partition>/order2_req. If the request len is equal to
155 * stripe size (sbi->s_stripe), we try to search for contigous block in
156 * stripe size. This should result in better allocation on RAID setup. If
157 * not we search in the specific group using bitmap for best extents. The
158 * tunable min_to_scan and max_to_scan controll the behaviour here.
159 * min_to_scan indicate how long the mballoc __must__ look for a best
160 * extent and max_to_scanindicate how long the mballoc __can__ look for a
161 * best extent in the found extents. Searching for the blocks starts with
162 * the group specified as the goal value in allocation context via
163 * ac_g_ex. Each group is first checked based on the criteria whether it
164 * can used for allocation. ext4_mb_good_group explains how the groups are
167 * Both the prealloc space are getting populated as above. So for the first
168 * request we will hit the buddy cache which will result in this prealloc
169 * space getting filled. The prealloc space is then later used for the
170 * subsequent request.
174 * mballoc operates on the following data:
176 * - in-core buddy (actually includes buddy and bitmap)
177 * - preallocation descriptors (PAs)
179 * there are two types of preallocations:
181 * assiged to specific inode and can be used for this inode only.
182 * it describes part of inode's space preallocated to specific
183 * physical blocks. any block from that preallocated can be used
184 * independent. the descriptor just tracks number of blocks left
185 * unused. so, before taking some block from descriptor, one must
186 * make sure corresponded logical block isn't allocated yet. this
187 * also means that freeing any block within descriptor's range
188 * must discard all preallocated blocks.
190 * assigned to specific locality group which does not translate to
191 * permanent set of inodes: inode can join and leave group. space
192 * from this type of preallocation can be used for any inode. thus
193 * it's consumed from the beginning to the end.
195 * relation between them can be expressed as:
196 * in-core buddy = on-disk bitmap + preallocation descriptors
198 * this mean blocks mballoc considers used are:
199 * - allocated blocks (persistent)
200 * - preallocated blocks (non-persistent)
202 * consistency in mballoc world means that at any time a block is either
203 * free or used in ALL structures. notice: "any time" should not be read
204 * literally -- time is discrete and delimited by locks.
206 * to keep it simple, we don't use block numbers, instead we count number of
207 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
209 * all operations can be expressed as:
210 * - init buddy: buddy = on-disk + PAs
211 * - new PA: buddy += N; PA = N
212 * - use inode PA: on-disk += N; PA -= N
213 * - discard inode PA buddy -= on-disk - PA; PA = 0
214 * - use locality group PA on-disk += N; PA -= N
215 * - discard locality group PA buddy -= PA; PA = 0
216 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
217 * is used in real operation because we can't know actual used
218 * bits from PA, only from on-disk bitmap
220 * if we follow this strict logic, then all operations above should be atomic.
221 * given some of them can block, we'd have to use something like semaphores
222 * killing performance on high-end SMP hardware. let's try to relax it using
223 * the following knowledge:
224 * 1) if buddy is referenced, it's already initialized
225 * 2) while block is used in buddy and the buddy is referenced,
226 * nobody can re-allocate that block
227 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
228 * bit set and PA claims same block, it's OK. IOW, one can set bit in
229 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
232 * so, now we're building a concurrency table:
235 * blocks for PA are allocated in the buddy, buddy must be referenced
236 * until PA is linked to allocation group to avoid concurrent buddy init
238 * we need to make sure that either on-disk bitmap or PA has uptodate data
239 * given (3) we care that PA-=N operation doesn't interfere with init
241 * the simplest way would be to have buddy initialized by the discard
242 * - use locality group PA
243 * again PA-=N must be serialized with init
244 * - discard locality group PA
245 * the simplest way would be to have buddy initialized by the discard
248 * i_data_sem serializes them
250 * discard process must wait until PA isn't used by another process
251 * - use locality group PA
252 * some mutex should serialize them
253 * - discard locality group PA
254 * discard process must wait until PA isn't used by another process
257 * i_data_sem or another mutex should serializes them
259 * discard process must wait until PA isn't used by another process
260 * - use locality group PA
261 * nothing wrong here -- they're different PAs covering different blocks
262 * - discard locality group PA
263 * discard process must wait until PA isn't used by another process
265 * now we're ready to make few consequences:
266 * - PA is referenced and while it is no discard is possible
267 * - PA is referenced until block isn't marked in on-disk bitmap
268 * - PA changes only after on-disk bitmap
269 * - discard must not compete with init. either init is done before
270 * any discard or they're serialized somehow
271 * - buddy init as sum of on-disk bitmap and PAs is done atomically
273 * a special case when we've used PA to emptiness. no need to modify buddy
274 * in this case, but we should care about concurrent init
279 * Logic in few words:
284 * mark bits in on-disk bitmap
287 * - use preallocation:
288 * find proper PA (per-inode or group)
290 * mark bits in on-disk bitmap
296 * mark bits in on-disk bitmap
299 * - discard preallocations in group:
301 * move them onto local list
302 * load on-disk bitmap
304 * remove PA from object (inode or locality group)
305 * mark free blocks in-core
307 * - discard inode's preallocations:
314 * - bitlock on a group (group)
315 * - object (inode/locality) (object)
326 * - release consumed pa:
331 * - generate in-core bitmap:
335 * - discard all for given object (inode, locality group):
340 * - discard all for given group:
349 * with AGGRESSIVE_CHECK allocator runs consistency checks over
350 * structures. these checks slow things down a lot
352 #define AGGRESSIVE_CHECK__
355 * with DOUBLE_CHECK defined mballoc creates persistent in-core
356 * bitmaps, maintains and uses them to check for double allocations
358 #define DOUBLE_CHECK__
364 #define mb_debug(fmt, a...) printk(fmt, ##a)
366 #define mb_debug(fmt, a...)
370 * with EXT4_MB_HISTORY mballoc stores last N allocations in memory
371 * and you can monitor it in /proc/fs/ext4/<dev>/mb_history
373 #define EXT4_MB_HISTORY
374 #define EXT4_MB_HISTORY_ALLOC 1 /* allocation */
375 #define EXT4_MB_HISTORY_PREALLOC 2 /* preallocated blocks used */
376 #define EXT4_MB_HISTORY_DISCARD 4 /* preallocation discarded */
377 #define EXT4_MB_HISTORY_FREE 8 /* free */
379 #define EXT4_MB_HISTORY_DEFAULT (EXT4_MB_HISTORY_ALLOC | \
380 EXT4_MB_HISTORY_PREALLOC)
383 * How long mballoc can look for a best extent (in found extents)
385 #define MB_DEFAULT_MAX_TO_SCAN 200
388 * How long mballoc must look for a best extent
390 #define MB_DEFAULT_MIN_TO_SCAN 10
393 * How many groups mballoc will scan looking for the best chunk
395 #define MB_DEFAULT_MAX_GROUPS_TO_SCAN 5
398 * with 'ext4_mb_stats' allocator will collect stats that will be
399 * shown at umount. The collecting costs though!
401 #define MB_DEFAULT_STATS 1
404 * files smaller than MB_DEFAULT_STREAM_THRESHOLD are served
405 * by the stream allocator, which purpose is to pack requests
406 * as close each to other as possible to produce smooth I/O traffic
407 * We use locality group prealloc space for stream request.
408 * We can tune the same via /proc/fs/ext4/<parition>/stream_req
410 #define MB_DEFAULT_STREAM_THRESHOLD 16 /* 64K */
413 * for which requests use 2^N search using buddies
415 #define MB_DEFAULT_ORDER2_REQS 2
418 * default group prealloc size 512 blocks
420 #define MB_DEFAULT_GROUP_PREALLOC 512
422 static struct kmem_cache *ext4_pspace_cachep;
423 static struct kmem_cache *ext4_ac_cachep;
425 #ifdef EXT4_BB_MAX_BLOCKS
426 #undef EXT4_BB_MAX_BLOCKS
428 #define EXT4_BB_MAX_BLOCKS 30
430 struct ext4_free_metadata {
433 ext4_grpblk_t blocks[EXT4_BB_MAX_BLOCKS];
434 struct list_head list;
437 struct ext4_group_info {
438 unsigned long bb_state;
439 unsigned long bb_tid;
440 struct ext4_free_metadata *bb_md_cur;
441 unsigned short bb_first_free;
442 unsigned short bb_free;
443 unsigned short bb_fragments;
444 struct list_head bb_prealloc_list;
448 unsigned short bb_counters[];
451 #define EXT4_GROUP_INFO_NEED_INIT_BIT 0
452 #define EXT4_GROUP_INFO_LOCKED_BIT 1
454 #define EXT4_MB_GRP_NEED_INIT(grp) \
455 (test_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &((grp)->bb_state)))
458 struct ext4_prealloc_space {
459 struct list_head pa_inode_list;
460 struct list_head pa_group_list;
462 struct list_head pa_tmp_list;
463 struct rcu_head pa_rcu;
468 ext4_fsblk_t pa_pstart; /* phys. block */
469 ext4_lblk_t pa_lstart; /* log. block */
470 unsigned short pa_len; /* len of preallocated chunk */
471 unsigned short pa_free; /* how many blocks are free */
472 unsigned short pa_linear; /* consumed in one direction
473 * strictly, for grp prealloc */
474 spinlock_t *pa_obj_lock;
475 struct inode *pa_inode; /* hack, for history only */
479 struct ext4_free_extent {
480 ext4_lblk_t fe_logical;
481 ext4_grpblk_t fe_start;
482 ext4_group_t fe_group;
488 * we try to group all related changes together
489 * so that writeback can flush/allocate them together as well
491 struct ext4_locality_group {
493 struct mutex lg_mutex; /* to serialize allocates */
494 struct list_head lg_prealloc_list;/* list of preallocations */
495 spinlock_t lg_prealloc_lock;
498 struct ext4_allocation_context {
499 struct inode *ac_inode;
500 struct super_block *ac_sb;
502 /* original request */
503 struct ext4_free_extent ac_o_ex;
505 /* goal request (after normalization) */
506 struct ext4_free_extent ac_g_ex;
508 /* the best found extent */
509 struct ext4_free_extent ac_b_ex;
511 /* copy of the bext found extent taken before preallocation efforts */
512 struct ext4_free_extent ac_f_ex;
514 /* number of iterations done. we have to track to limit searching */
515 unsigned long ac_ex_scanned;
516 __u16 ac_groups_scanned;
520 __u16 ac_flags; /* allocation hints */
524 __u8 ac_2order; /* if request is to allocate 2^N blocks and
525 * N > 0, the field stores N, otherwise 0 */
526 __u8 ac_op; /* operation, for history only */
527 struct page *ac_bitmap_page;
528 struct page *ac_buddy_page;
529 struct ext4_prealloc_space *ac_pa;
530 struct ext4_locality_group *ac_lg;
533 #define AC_STATUS_CONTINUE 1
534 #define AC_STATUS_FOUND 2
535 #define AC_STATUS_BREAK 3
537 struct ext4_mb_history {
538 struct ext4_free_extent orig; /* orig allocation */
539 struct ext4_free_extent goal; /* goal allocation */
540 struct ext4_free_extent result; /* result allocation */
543 __u16 found; /* how many extents have been found */
544 __u16 groups; /* how many groups have been scanned */
545 __u16 tail; /* what tail broke some buddy */
546 __u16 buddy; /* buddy the tail ^^^ broke */
548 __u8 cr:3; /* which phase the result extent was found at */
554 struct page *bd_buddy_page;
556 struct page *bd_bitmap_page;
558 struct ext4_group_info *bd_info;
559 struct super_block *bd_sb;
561 ext4_group_t bd_group;
563 #define EXT4_MB_BITMAP(e4b) ((e4b)->bd_bitmap)
564 #define EXT4_MB_BUDDY(e4b) ((e4b)->bd_buddy)
566 #ifndef EXT4_MB_HISTORY
567 static inline void ext4_mb_store_history(struct ext4_allocation_context *ac)
572 static void ext4_mb_store_history(struct ext4_allocation_context *ac);
575 #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
577 static struct proc_dir_entry *proc_root_ext4;
578 struct buffer_head *read_block_bitmap(struct super_block *, ext4_group_t);
579 ext4_fsblk_t ext4_new_blocks_old(handle_t *handle, struct inode *inode,
580 ext4_fsblk_t goal, unsigned long *count, int *errp);
582 static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
584 static void ext4_mb_poll_new_transaction(struct super_block *, handle_t *);
585 static void ext4_mb_free_committed_blocks(struct super_block *);
586 static void ext4_mb_return_to_preallocation(struct inode *inode,
587 struct ext4_buddy *e4b, sector_t block,
589 static void ext4_mb_put_pa(struct ext4_allocation_context *,
590 struct super_block *, struct ext4_prealloc_space *pa);
591 static int ext4_mb_init_per_dev_proc(struct super_block *sb);
592 static int ext4_mb_destroy_per_dev_proc(struct super_block *sb);
595 static inline void ext4_lock_group(struct super_block *sb, ext4_group_t group)
597 struct ext4_group_info *grinfo = ext4_get_group_info(sb, group);
599 bit_spin_lock(EXT4_GROUP_INFO_LOCKED_BIT, &(grinfo->bb_state));
602 static inline void ext4_unlock_group(struct super_block *sb,
605 struct ext4_group_info *grinfo = ext4_get_group_info(sb, group);
607 bit_spin_unlock(EXT4_GROUP_INFO_LOCKED_BIT, &(grinfo->bb_state));
610 static inline int ext4_is_group_locked(struct super_block *sb,
613 struct ext4_group_info *grinfo = ext4_get_group_info(sb, group);
615 return bit_spin_is_locked(EXT4_GROUP_INFO_LOCKED_BIT,
616 &(grinfo->bb_state));
619 static ext4_fsblk_t ext4_grp_offs_to_block(struct super_block *sb,
620 struct ext4_free_extent *fex)
624 block = (ext4_fsblk_t) fex->fe_group * EXT4_BLOCKS_PER_GROUP(sb)
626 + le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
630 static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
632 #if BITS_PER_LONG == 64
633 *bit += ((unsigned long) addr & 7UL) << 3;
634 addr = (void *) ((unsigned long) addr & ~7UL);
635 #elif BITS_PER_LONG == 32
636 *bit += ((unsigned long) addr & 3UL) << 3;
637 addr = (void *) ((unsigned long) addr & ~3UL);
639 #error "how many bits you are?!"
644 static inline int mb_test_bit(int bit, void *addr)
647 * ext4_test_bit on architecture like powerpc
648 * needs unsigned long aligned address
650 addr = mb_correct_addr_and_bit(&bit, addr);
651 return ext4_test_bit(bit, addr);
654 static inline void mb_set_bit(int bit, void *addr)
656 addr = mb_correct_addr_and_bit(&bit, addr);
657 ext4_set_bit(bit, addr);
660 static inline void mb_set_bit_atomic(spinlock_t *lock, int bit, void *addr)
662 addr = mb_correct_addr_and_bit(&bit, addr);
663 ext4_set_bit_atomic(lock, bit, addr);
666 static inline void mb_clear_bit(int bit, void *addr)
668 addr = mb_correct_addr_and_bit(&bit, addr);
669 ext4_clear_bit(bit, addr);
672 static inline void mb_clear_bit_atomic(spinlock_t *lock, int bit, void *addr)
674 addr = mb_correct_addr_and_bit(&bit, addr);
675 ext4_clear_bit_atomic(lock, bit, addr);
678 static inline int mb_find_next_zero_bit(void *addr, int max, int start)
681 addr = mb_correct_addr_and_bit(&fix, addr);
685 return ext4_find_next_zero_bit(addr, max, start) - fix;
688 static inline int mb_find_next_bit(void *addr, int max, int start)
691 addr = mb_correct_addr_and_bit(&fix, addr);
695 return ext4_find_next_bit(addr, max, start) - fix;
698 static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
702 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
705 if (order > e4b->bd_blkbits + 1) {
710 /* at order 0 we see each particular block */
711 *max = 1 << (e4b->bd_blkbits + 3);
713 return EXT4_MB_BITMAP(e4b);
715 bb = EXT4_MB_BUDDY(e4b) + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
716 *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
722 static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
723 int first, int count)
726 struct super_block *sb = e4b->bd_sb;
728 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
730 BUG_ON(!ext4_is_group_locked(sb, e4b->bd_group));
731 for (i = 0; i < count; i++) {
732 if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
733 ext4_fsblk_t blocknr;
734 blocknr = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb);
735 blocknr += first + i;
737 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
739 ext4_error(sb, __FUNCTION__, "double-free of inode"
740 " %lu's block %llu(bit %u in group %lu)\n",
741 inode ? inode->i_ino : 0, blocknr,
742 first + i, e4b->bd_group);
744 mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
748 static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
752 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
754 BUG_ON(!ext4_is_group_locked(e4b->bd_sb, e4b->bd_group));
755 for (i = 0; i < count; i++) {
756 BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
757 mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
761 static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
763 if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
764 unsigned char *b1, *b2;
766 b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
767 b2 = (unsigned char *) bitmap;
768 for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
769 if (b1[i] != b2[i]) {
770 printk("corruption in group %lu at byte %u(%u):"
771 " %x in copy != %x on disk/prealloc\n",
772 e4b->bd_group, i, i * 8, b1[i], b2[i]);
780 static inline void mb_free_blocks_double(struct inode *inode,
781 struct ext4_buddy *e4b, int first, int count)
785 static inline void mb_mark_used_double(struct ext4_buddy *e4b,
786 int first, int count)
790 static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
796 #ifdef AGGRESSIVE_CHECK
798 #define MB_CHECK_ASSERT(assert) \
802 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
803 function, file, line, # assert); \
808 static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
809 const char *function, int line)
811 struct super_block *sb = e4b->bd_sb;
812 int order = e4b->bd_blkbits + 1;
819 struct ext4_group_info *grp;
822 struct list_head *cur;
826 if (!test_opt(sb, MBALLOC))
830 static int mb_check_counter;
831 if (mb_check_counter++ % 100 != 0)
836 buddy = mb_find_buddy(e4b, order, &max);
837 MB_CHECK_ASSERT(buddy);
838 buddy2 = mb_find_buddy(e4b, order - 1, &max2);
839 MB_CHECK_ASSERT(buddy2);
840 MB_CHECK_ASSERT(buddy != buddy2);
841 MB_CHECK_ASSERT(max * 2 == max2);
844 for (i = 0; i < max; i++) {
846 if (mb_test_bit(i, buddy)) {
847 /* only single bit in buddy2 may be 1 */
848 if (!mb_test_bit(i << 1, buddy2)) {
850 mb_test_bit((i<<1)+1, buddy2));
851 } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
853 mb_test_bit(i << 1, buddy2));
858 /* both bits in buddy2 must be 0 */
859 MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
860 MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
862 for (j = 0; j < (1 << order); j++) {
863 k = (i * (1 << order)) + j;
865 !mb_test_bit(k, EXT4_MB_BITMAP(e4b)));
869 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
874 buddy = mb_find_buddy(e4b, 0, &max);
875 for (i = 0; i < max; i++) {
876 if (!mb_test_bit(i, buddy)) {
877 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
885 /* check used bits only */
886 for (j = 0; j < e4b->bd_blkbits + 1; j++) {
887 buddy2 = mb_find_buddy(e4b, j, &max2);
889 MB_CHECK_ASSERT(k < max2);
890 MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
893 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
894 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
896 grp = ext4_get_group_info(sb, e4b->bd_group);
897 buddy = mb_find_buddy(e4b, 0, &max);
898 list_for_each(cur, &grp->bb_prealloc_list) {
899 ext4_group_t groupnr;
900 struct ext4_prealloc_space *pa;
901 pa = list_entry(cur, struct ext4_prealloc_space, group_list);
902 ext4_get_group_no_and_offset(sb, pa->pstart, &groupnr, &k);
903 MB_CHECK_ASSERT(groupnr == e4b->bd_group);
904 for (i = 0; i < pa->len; i++)
905 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
909 #undef MB_CHECK_ASSERT
910 #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
911 __FILE__, __FUNCTION__, __LINE__)
913 #define mb_check_buddy(e4b)
916 /* FIXME!! need more doc */
917 static void ext4_mb_mark_free_simple(struct super_block *sb,
918 void *buddy, unsigned first, int len,
919 struct ext4_group_info *grp)
921 struct ext4_sb_info *sbi = EXT4_SB(sb);
924 unsigned short chunk;
925 unsigned short border;
927 BUG_ON(len > EXT4_BLOCKS_PER_GROUP(sb));
929 border = 2 << sb->s_blocksize_bits;
932 /* find how many blocks can be covered since this position */
933 max = ffs(first | border) - 1;
935 /* find how many blocks of power 2 we need to mark */
942 /* mark multiblock chunks only */
943 grp->bb_counters[min]++;
945 mb_clear_bit(first >> min,
946 buddy + sbi->s_mb_offsets[min]);
953 static void ext4_mb_generate_buddy(struct super_block *sb,
954 void *buddy, void *bitmap, ext4_group_t group)
956 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
957 unsigned short max = EXT4_BLOCKS_PER_GROUP(sb);
958 unsigned short i = 0;
959 unsigned short first;
962 unsigned fragments = 0;
963 unsigned long long period = get_cycles();
965 /* initialize buddy from bitmap which is aggregation
966 * of on-disk bitmap and preallocations */
967 i = mb_find_next_zero_bit(bitmap, max, 0);
968 grp->bb_first_free = i;
972 i = mb_find_next_bit(bitmap, max, i);
976 ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
978 grp->bb_counters[0]++;
980 i = mb_find_next_zero_bit(bitmap, max, i);
982 grp->bb_fragments = fragments;
984 if (free != grp->bb_free) {
985 ext4_error(sb, __FUNCTION__,
986 "EXT4-fs: group %lu: %u blocks in bitmap, %u in gd\n",
987 group, free, grp->bb_free);
989 * If we intent to continue, we consider group descritor
990 * corrupt and update bb_free using bitmap value
995 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
997 period = get_cycles() - period;
998 spin_lock(&EXT4_SB(sb)->s_bal_lock);
999 EXT4_SB(sb)->s_mb_buddies_generated++;
1000 EXT4_SB(sb)->s_mb_generation_time += period;
1001 spin_unlock(&EXT4_SB(sb)->s_bal_lock);
1004 /* The buddy information is attached the buddy cache inode
1005 * for convenience. The information regarding each group
1006 * is loaded via ext4_mb_load_buddy. The information involve
1007 * block bitmap and buddy information. The information are
1008 * stored in the inode as
1011 * [ group 0 buddy][ group 0 bitmap] [group 1][ group 1]...
1014 * one block each for bitmap and buddy information.
1015 * So for each group we take up 2 blocks. A page can
1016 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
1017 * So it can have information regarding groups_per_page which
1018 * is blocks_per_page/2
1021 static int ext4_mb_init_cache(struct page *page, char *incore)
1024 int blocks_per_page;
1025 int groups_per_page;
1028 ext4_group_t first_group;
1030 struct super_block *sb;
1031 struct buffer_head *bhs;
1032 struct buffer_head **bh;
1033 struct inode *inode;
1037 mb_debug("init page %lu\n", page->index);
1039 inode = page->mapping->host;
1041 blocksize = 1 << inode->i_blkbits;
1042 blocks_per_page = PAGE_CACHE_SIZE / blocksize;
1044 groups_per_page = blocks_per_page >> 1;
1045 if (groups_per_page == 0)
1046 groups_per_page = 1;
1048 /* allocate buffer_heads to read bitmaps */
1049 if (groups_per_page > 1) {
1051 i = sizeof(struct buffer_head *) * groups_per_page;
1052 bh = kzalloc(i, GFP_NOFS);
1058 first_group = page->index * blocks_per_page / 2;
1060 /* read all groups the page covers into the cache */
1061 for (i = 0; i < groups_per_page; i++) {
1062 struct ext4_group_desc *desc;
1064 if (first_group + i >= EXT4_SB(sb)->s_groups_count)
1068 desc = ext4_get_group_desc(sb, first_group + i, NULL);
1073 bh[i] = sb_getblk(sb, ext4_block_bitmap(sb, desc));
1077 if (bh_uptodate_or_lock(bh[i]))
1080 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
1081 ext4_init_block_bitmap(sb, bh[i],
1082 first_group + i, desc);
1083 set_buffer_uptodate(bh[i]);
1084 unlock_buffer(bh[i]);
1088 bh[i]->b_end_io = end_buffer_read_sync;
1089 submit_bh(READ, bh[i]);
1090 mb_debug("read bitmap for group %lu\n", first_group + i);
1093 /* wait for I/O completion */
1094 for (i = 0; i < groups_per_page && bh[i]; i++)
1095 wait_on_buffer(bh[i]);
1098 for (i = 0; i < groups_per_page && bh[i]; i++)
1099 if (!buffer_uptodate(bh[i]))
1102 first_block = page->index * blocks_per_page;
1103 for (i = 0; i < blocks_per_page; i++) {
1105 struct ext4_group_info *grinfo;
1107 group = (first_block + i) >> 1;
1108 if (group >= EXT4_SB(sb)->s_groups_count)
1112 * data carry information regarding this
1113 * particular group in the format specified
1117 data = page_address(page) + (i * blocksize);
1118 bitmap = bh[group - first_group]->b_data;
1121 * We place the buddy block and bitmap block
1124 if ((first_block + i) & 1) {
1125 /* this is block of buddy */
1126 BUG_ON(incore == NULL);
1127 mb_debug("put buddy for group %u in page %lu/%x\n",
1128 group, page->index, i * blocksize);
1129 memset(data, 0xff, blocksize);
1130 grinfo = ext4_get_group_info(sb, group);
1131 grinfo->bb_fragments = 0;
1132 memset(grinfo->bb_counters, 0,
1133 sizeof(unsigned short)*(sb->s_blocksize_bits+2));
1135 * incore got set to the group block bitmap below
1137 ext4_mb_generate_buddy(sb, data, incore, group);
1140 /* this is block of bitmap */
1141 BUG_ON(incore != NULL);
1142 mb_debug("put bitmap for group %u in page %lu/%x\n",
1143 group, page->index, i * blocksize);
1145 /* see comments in ext4_mb_put_pa() */
1146 ext4_lock_group(sb, group);
1147 memcpy(data, bitmap, blocksize);
1149 /* mark all preallocated blks used in in-core bitmap */
1150 ext4_mb_generate_from_pa(sb, data, group);
1151 ext4_unlock_group(sb, group);
1153 /* set incore so that the buddy information can be
1154 * generated using this
1159 SetPageUptodate(page);
1163 for (i = 0; i < groups_per_page && bh[i]; i++)
1171 static int ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
1172 struct ext4_buddy *e4b)
1174 struct ext4_sb_info *sbi = EXT4_SB(sb);
1175 struct inode *inode = sbi->s_buddy_cache;
1176 int blocks_per_page;
1182 mb_debug("load group %lu\n", group);
1184 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1186 e4b->bd_blkbits = sb->s_blocksize_bits;
1187 e4b->bd_info = ext4_get_group_info(sb, group);
1189 e4b->bd_group = group;
1190 e4b->bd_buddy_page = NULL;
1191 e4b->bd_bitmap_page = NULL;
1194 * the buddy cache inode stores the block bitmap
1195 * and buddy information in consecutive blocks.
1196 * So for each group we need two blocks.
1199 pnum = block / blocks_per_page;
1200 poff = block % blocks_per_page;
1202 /* we could use find_or_create_page(), but it locks page
1203 * what we'd like to avoid in fast path ... */
1204 page = find_get_page(inode->i_mapping, pnum);
1205 if (page == NULL || !PageUptodate(page)) {
1207 page_cache_release(page);
1208 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1210 BUG_ON(page->mapping != inode->i_mapping);
1211 if (!PageUptodate(page)) {
1212 ext4_mb_init_cache(page, NULL);
1213 mb_cmp_bitmaps(e4b, page_address(page) +
1214 (poff * sb->s_blocksize));
1219 if (page == NULL || !PageUptodate(page))
1221 e4b->bd_bitmap_page = page;
1222 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1223 mark_page_accessed(page);
1226 pnum = block / blocks_per_page;
1227 poff = block % blocks_per_page;
1229 page = find_get_page(inode->i_mapping, pnum);
1230 if (page == NULL || !PageUptodate(page)) {
1232 page_cache_release(page);
1233 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1235 BUG_ON(page->mapping != inode->i_mapping);
1236 if (!PageUptodate(page))
1237 ext4_mb_init_cache(page, e4b->bd_bitmap);
1242 if (page == NULL || !PageUptodate(page))
1244 e4b->bd_buddy_page = page;
1245 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1246 mark_page_accessed(page);
1248 BUG_ON(e4b->bd_bitmap_page == NULL);
1249 BUG_ON(e4b->bd_buddy_page == NULL);
1254 if (e4b->bd_bitmap_page)
1255 page_cache_release(e4b->bd_bitmap_page);
1256 if (e4b->bd_buddy_page)
1257 page_cache_release(e4b->bd_buddy_page);
1258 e4b->bd_buddy = NULL;
1259 e4b->bd_bitmap = NULL;
1263 static void ext4_mb_release_desc(struct ext4_buddy *e4b)
1265 if (e4b->bd_bitmap_page)
1266 page_cache_release(e4b->bd_bitmap_page);
1267 if (e4b->bd_buddy_page)
1268 page_cache_release(e4b->bd_buddy_page);
1272 static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1277 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
1278 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1280 bb = EXT4_MB_BUDDY(e4b);
1281 while (order <= e4b->bd_blkbits + 1) {
1283 if (!mb_test_bit(block, bb)) {
1284 /* this block is part of buddy of order 'order' */
1287 bb += 1 << (e4b->bd_blkbits - order);
1293 static void mb_clear_bits(spinlock_t *lock, void *bm, int cur, int len)
1299 if ((cur & 31) == 0 && (len - cur) >= 32) {
1300 /* fast path: clear whole word at once */
1301 addr = bm + (cur >> 3);
1306 mb_clear_bit_atomic(lock, cur, bm);
1311 static void mb_set_bits(spinlock_t *lock, void *bm, int cur, int len)
1317 if ((cur & 31) == 0 && (len - cur) >= 32) {
1318 /* fast path: set whole word at once */
1319 addr = bm + (cur >> 3);
1324 mb_set_bit_atomic(lock, cur, bm);
1329 static int mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
1330 int first, int count)
1337 struct super_block *sb = e4b->bd_sb;
1339 BUG_ON(first + count > (sb->s_blocksize << 3));
1340 BUG_ON(!ext4_is_group_locked(sb, e4b->bd_group));
1341 mb_check_buddy(e4b);
1342 mb_free_blocks_double(inode, e4b, first, count);
1344 e4b->bd_info->bb_free += count;
1345 if (first < e4b->bd_info->bb_first_free)
1346 e4b->bd_info->bb_first_free = first;
1348 /* let's maintain fragments counter */
1350 block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
1351 if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
1352 max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
1354 e4b->bd_info->bb_fragments--;
1355 else if (!block && !max)
1356 e4b->bd_info->bb_fragments++;
1358 /* let's maintain buddy itself */
1359 while (count-- > 0) {
1363 if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
1364 ext4_fsblk_t blocknr;
1365 blocknr = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb);
1368 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
1370 ext4_error(sb, __FUNCTION__, "double-free of inode"
1371 " %lu's block %llu(bit %u in group %lu)\n",
1372 inode ? inode->i_ino : 0, blocknr, block,
1375 mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
1376 e4b->bd_info->bb_counters[order]++;
1378 /* start of the buddy */
1379 buddy = mb_find_buddy(e4b, order, &max);
1383 if (mb_test_bit(block, buddy) ||
1384 mb_test_bit(block + 1, buddy))
1387 /* both the buddies are free, try to coalesce them */
1388 buddy2 = mb_find_buddy(e4b, order + 1, &max);
1394 /* for special purposes, we don't set
1395 * free bits in bitmap */
1396 mb_set_bit(block, buddy);
1397 mb_set_bit(block + 1, buddy);
1399 e4b->bd_info->bb_counters[order]--;
1400 e4b->bd_info->bb_counters[order]--;
1404 e4b->bd_info->bb_counters[order]++;
1406 mb_clear_bit(block, buddy2);
1410 mb_check_buddy(e4b);
1415 static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
1416 int needed, struct ext4_free_extent *ex)
1423 BUG_ON(!ext4_is_group_locked(e4b->bd_sb, e4b->bd_group));
1426 buddy = mb_find_buddy(e4b, order, &max);
1427 BUG_ON(buddy == NULL);
1428 BUG_ON(block >= max);
1429 if (mb_test_bit(block, buddy)) {
1436 /* FIXME dorp order completely ? */
1437 if (likely(order == 0)) {
1438 /* find actual order */
1439 order = mb_find_order_for_block(e4b, block);
1440 block = block >> order;
1443 ex->fe_len = 1 << order;
1444 ex->fe_start = block << order;
1445 ex->fe_group = e4b->bd_group;
1447 /* calc difference from given start */
1448 next = next - ex->fe_start;
1450 ex->fe_start += next;
1452 while (needed > ex->fe_len &&
1453 (buddy = mb_find_buddy(e4b, order, &max))) {
1455 if (block + 1 >= max)
1458 next = (block + 1) * (1 << order);
1459 if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
1462 ord = mb_find_order_for_block(e4b, next);
1465 block = next >> order;
1466 ex->fe_len += 1 << order;
1469 BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
1473 static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1479 int start = ex->fe_start;
1480 int len = ex->fe_len;
1485 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1486 BUG_ON(e4b->bd_group != ex->fe_group);
1487 BUG_ON(!ext4_is_group_locked(e4b->bd_sb, e4b->bd_group));
1488 mb_check_buddy(e4b);
1489 mb_mark_used_double(e4b, start, len);
1491 e4b->bd_info->bb_free -= len;
1492 if (e4b->bd_info->bb_first_free == start)
1493 e4b->bd_info->bb_first_free += len;
1495 /* let's maintain fragments counter */
1497 mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
1498 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
1499 max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
1501 e4b->bd_info->bb_fragments++;
1502 else if (!mlen && !max)
1503 e4b->bd_info->bb_fragments--;
1505 /* let's maintain buddy itself */
1507 ord = mb_find_order_for_block(e4b, start);
1509 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1510 /* the whole chunk may be allocated at once! */
1512 buddy = mb_find_buddy(e4b, ord, &max);
1513 BUG_ON((start >> ord) >= max);
1514 mb_set_bit(start >> ord, buddy);
1515 e4b->bd_info->bb_counters[ord]--;
1522 /* store for history */
1524 ret = len | (ord << 16);
1526 /* we have to split large buddy */
1528 buddy = mb_find_buddy(e4b, ord, &max);
1529 mb_set_bit(start >> ord, buddy);
1530 e4b->bd_info->bb_counters[ord]--;
1533 cur = (start >> ord) & ~1U;
1534 buddy = mb_find_buddy(e4b, ord, &max);
1535 mb_clear_bit(cur, buddy);
1536 mb_clear_bit(cur + 1, buddy);
1537 e4b->bd_info->bb_counters[ord]++;
1538 e4b->bd_info->bb_counters[ord]++;
1541 mb_set_bits(sb_bgl_lock(EXT4_SB(e4b->bd_sb), ex->fe_group),
1542 EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
1543 mb_check_buddy(e4b);
1549 * Must be called under group lock!
1551 static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1552 struct ext4_buddy *e4b)
1554 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1557 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1558 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1560 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1561 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1562 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1564 /* preallocation can change ac_b_ex, thus we store actually
1565 * allocated blocks for history */
1566 ac->ac_f_ex = ac->ac_b_ex;
1568 ac->ac_status = AC_STATUS_FOUND;
1569 ac->ac_tail = ret & 0xffff;
1570 ac->ac_buddy = ret >> 16;
1572 /* XXXXXXX: SUCH A HORRIBLE **CK */
1574 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1575 get_page(ac->ac_bitmap_page);
1576 ac->ac_buddy_page = e4b->bd_buddy_page;
1577 get_page(ac->ac_buddy_page);
1579 /* store last allocated for subsequent stream allocation */
1580 if ((ac->ac_flags & EXT4_MB_HINT_DATA)) {
1581 spin_lock(&sbi->s_md_lock);
1582 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1583 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1584 spin_unlock(&sbi->s_md_lock);
1589 * regular allocator, for general purposes allocation
1592 static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1593 struct ext4_buddy *e4b,
1596 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1597 struct ext4_free_extent *bex = &ac->ac_b_ex;
1598 struct ext4_free_extent *gex = &ac->ac_g_ex;
1599 struct ext4_free_extent ex;
1603 * We don't want to scan for a whole year
1605 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1606 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1607 ac->ac_status = AC_STATUS_BREAK;
1612 * Haven't found good chunk so far, let's continue
1614 if (bex->fe_len < gex->fe_len)
1617 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1618 && bex->fe_group == e4b->bd_group) {
1619 /* recheck chunk's availability - we don't know
1620 * when it was found (within this lock-unlock
1622 max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
1623 if (max >= gex->fe_len) {
1624 ext4_mb_use_best_found(ac, e4b);
1631 * The routine checks whether found extent is good enough. If it is,
1632 * then the extent gets marked used and flag is set to the context
1633 * to stop scanning. Otherwise, the extent is compared with the
1634 * previous found extent and if new one is better, then it's stored
1635 * in the context. Later, the best found extent will be used, if
1636 * mballoc can't find good enough extent.
1638 * FIXME: real allocation policy is to be designed yet!
1640 static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1641 struct ext4_free_extent *ex,
1642 struct ext4_buddy *e4b)
1644 struct ext4_free_extent *bex = &ac->ac_b_ex;
1645 struct ext4_free_extent *gex = &ac->ac_g_ex;
1647 BUG_ON(ex->fe_len <= 0);
1648 BUG_ON(ex->fe_len >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
1649 BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
1650 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1655 * The special case - take what you catch first
1657 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1659 ext4_mb_use_best_found(ac, e4b);
1664 * Let's check whether the chuck is good enough
1666 if (ex->fe_len == gex->fe_len) {
1668 ext4_mb_use_best_found(ac, e4b);
1673 * If this is first found extent, just store it in the context
1675 if (bex->fe_len == 0) {
1681 * If new found extent is better, store it in the context
1683 if (bex->fe_len < gex->fe_len) {
1684 /* if the request isn't satisfied, any found extent
1685 * larger than previous best one is better */
1686 if (ex->fe_len > bex->fe_len)
1688 } else if (ex->fe_len > gex->fe_len) {
1689 /* if the request is satisfied, then we try to find
1690 * an extent that still satisfy the request, but is
1691 * smaller than previous one */
1692 if (ex->fe_len < bex->fe_len)
1696 ext4_mb_check_limits(ac, e4b, 0);
1699 static int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
1700 struct ext4_buddy *e4b)
1702 struct ext4_free_extent ex = ac->ac_b_ex;
1703 ext4_group_t group = ex.fe_group;
1707 BUG_ON(ex.fe_len <= 0);
1708 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1712 ext4_lock_group(ac->ac_sb, group);
1713 max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
1717 ext4_mb_use_best_found(ac, e4b);
1720 ext4_unlock_group(ac->ac_sb, group);
1721 ext4_mb_release_desc(e4b);
1726 static int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
1727 struct ext4_buddy *e4b)
1729 ext4_group_t group = ac->ac_g_ex.fe_group;
1732 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1733 struct ext4_super_block *es = sbi->s_es;
1734 struct ext4_free_extent ex;
1736 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1739 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1743 ext4_lock_group(ac->ac_sb, group);
1744 max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
1745 ac->ac_g_ex.fe_len, &ex);
1747 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1750 start = (e4b->bd_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb)) +
1751 ex.fe_start + le32_to_cpu(es->s_first_data_block);
1752 /* use do_div to get remainder (would be 64-bit modulo) */
1753 if (do_div(start, sbi->s_stripe) == 0) {
1756 ext4_mb_use_best_found(ac, e4b);
1758 } else if (max >= ac->ac_g_ex.fe_len) {
1759 BUG_ON(ex.fe_len <= 0);
1760 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1761 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1764 ext4_mb_use_best_found(ac, e4b);
1765 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1766 /* Sometimes, caller may want to merge even small
1767 * number of blocks to an existing extent */
1768 BUG_ON(ex.fe_len <= 0);
1769 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1770 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1773 ext4_mb_use_best_found(ac, e4b);
1775 ext4_unlock_group(ac->ac_sb, group);
1776 ext4_mb_release_desc(e4b);
1782 * The routine scans buddy structures (not bitmap!) from given order
1783 * to max order and tries to find big enough chunk to satisfy the req
1785 static void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
1786 struct ext4_buddy *e4b)
1788 struct super_block *sb = ac->ac_sb;
1789 struct ext4_group_info *grp = e4b->bd_info;
1795 BUG_ON(ac->ac_2order <= 0);
1796 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1797 if (grp->bb_counters[i] == 0)
1800 buddy = mb_find_buddy(e4b, i, &max);
1801 BUG_ON(buddy == NULL);
1803 k = mb_find_next_zero_bit(buddy, max, 0);
1808 ac->ac_b_ex.fe_len = 1 << i;
1809 ac->ac_b_ex.fe_start = k << i;
1810 ac->ac_b_ex.fe_group = e4b->bd_group;
1812 ext4_mb_use_best_found(ac, e4b);
1814 BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1816 if (EXT4_SB(sb)->s_mb_stats)
1817 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1824 * The routine scans the group and measures all found extents.
1825 * In order to optimize scanning, caller must pass number of
1826 * free blocks in the group, so the routine can know upper limit.
1828 static void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
1829 struct ext4_buddy *e4b)
1831 struct super_block *sb = ac->ac_sb;
1832 void *bitmap = EXT4_MB_BITMAP(e4b);
1833 struct ext4_free_extent ex;
1837 free = e4b->bd_info->bb_free;
1840 i = e4b->bd_info->bb_first_free;
1842 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
1843 i = mb_find_next_zero_bit(bitmap,
1844 EXT4_BLOCKS_PER_GROUP(sb), i);
1845 if (i >= EXT4_BLOCKS_PER_GROUP(sb)) {
1847 * IF we have corrupt bitmap, we won't find any
1848 * free blocks even though group info says we
1849 * we have free blocks
1851 ext4_error(sb, __FUNCTION__, "%d free blocks as per "
1852 "group info. But bitmap says 0\n",
1857 mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
1858 BUG_ON(ex.fe_len <= 0);
1859 if (free < ex.fe_len) {
1860 ext4_error(sb, __FUNCTION__, "%d free blocks as per "
1861 "group info. But got %d blocks\n",
1864 * The number of free blocks differs. This mostly
1865 * indicate that the bitmap is corrupt. So exit
1866 * without claiming the space.
1871 ext4_mb_measure_extent(ac, &ex, e4b);
1877 ext4_mb_check_limits(ac, e4b, 1);
1881 * This is a special case for storages like raid5
1882 * we try to find stripe-aligned chunks for stripe-size requests
1883 * XXX should do so at least for multiples of stripe size as well
1885 static void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
1886 struct ext4_buddy *e4b)
1888 struct super_block *sb = ac->ac_sb;
1889 struct ext4_sb_info *sbi = EXT4_SB(sb);
1890 void *bitmap = EXT4_MB_BITMAP(e4b);
1891 struct ext4_free_extent ex;
1892 ext4_fsblk_t first_group_block;
1897 BUG_ON(sbi->s_stripe == 0);
1899 /* find first stripe-aligned block in group */
1900 first_group_block = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb)
1901 + le32_to_cpu(sbi->s_es->s_first_data_block);
1902 a = first_group_block + sbi->s_stripe - 1;
1903 do_div(a, sbi->s_stripe);
1904 i = (a * sbi->s_stripe) - first_group_block;
1906 while (i < EXT4_BLOCKS_PER_GROUP(sb)) {
1907 if (!mb_test_bit(i, bitmap)) {
1908 max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex);
1909 if (max >= sbi->s_stripe) {
1912 ext4_mb_use_best_found(ac, e4b);
1920 static int ext4_mb_good_group(struct ext4_allocation_context *ac,
1921 ext4_group_t group, int cr)
1923 unsigned free, fragments;
1925 struct ext4_group_desc *desc;
1926 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
1928 BUG_ON(cr < 0 || cr >= 4);
1929 BUG_ON(EXT4_MB_GRP_NEED_INIT(grp));
1931 free = grp->bb_free;
1932 fragments = grp->bb_fragments;
1940 BUG_ON(ac->ac_2order == 0);
1941 /* If this group is uninitialized, skip it initially */
1942 desc = ext4_get_group_desc(ac->ac_sb, group, NULL);
1943 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))
1946 bits = ac->ac_sb->s_blocksize_bits + 1;
1947 for (i = ac->ac_2order; i <= bits; i++)
1948 if (grp->bb_counters[i] > 0)
1952 if ((free / fragments) >= ac->ac_g_ex.fe_len)
1956 if (free >= ac->ac_g_ex.fe_len)
1968 static int ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
1975 struct ext4_sb_info *sbi;
1976 struct super_block *sb;
1977 struct ext4_buddy e4b;
1982 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1984 /* first, try the goal */
1985 err = ext4_mb_find_by_goal(ac, &e4b);
1986 if (err || ac->ac_status == AC_STATUS_FOUND)
1989 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
1993 * ac->ac2_order is set only if the fe_len is a power of 2
1994 * if ac2_order is set we also set criteria to 0 so that we
1995 * try exact allocation using buddy.
1997 i = fls(ac->ac_g_ex.fe_len);
2000 * We search using buddy data only if the order of the request
2001 * is greater than equal to the sbi_s_mb_order2_reqs
2002 * You can tune it via /proc/fs/ext4/<partition>/order2_req
2004 if (i >= sbi->s_mb_order2_reqs) {
2006 * This should tell if fe_len is exactly power of 2
2008 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
2009 ac->ac_2order = i - 1;
2012 bsbits = ac->ac_sb->s_blocksize_bits;
2013 /* if stream allocation is enabled, use global goal */
2014 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
2015 isize = i_size_read(ac->ac_inode) >> bsbits;
2019 if (size < sbi->s_mb_stream_request &&
2020 (ac->ac_flags & EXT4_MB_HINT_DATA)) {
2021 /* TBD: may be hot point */
2022 spin_lock(&sbi->s_md_lock);
2023 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2024 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2025 spin_unlock(&sbi->s_md_lock);
2028 /* searching for the right group start from the goal value specified */
2029 group = ac->ac_g_ex.fe_group;
2031 /* Let's just scan groups to find more-less suitable blocks */
2032 cr = ac->ac_2order ? 0 : 1;
2034 * cr == 0 try to get exact allocation,
2035 * cr == 3 try to get anything
2038 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2039 ac->ac_criteria = cr;
2040 for (i = 0; i < EXT4_SB(sb)->s_groups_count; group++, i++) {
2041 struct ext4_group_info *grp;
2042 struct ext4_group_desc *desc;
2044 if (group == EXT4_SB(sb)->s_groups_count)
2047 /* quick check to skip empty groups */
2048 grp = ext4_get_group_info(ac->ac_sb, group);
2049 if (grp->bb_free == 0)
2053 * if the group is already init we check whether it is
2054 * a good group and if not we don't load the buddy
2056 if (EXT4_MB_GRP_NEED_INIT(grp)) {
2058 * we need full data about the group
2059 * to make a good selection
2061 err = ext4_mb_load_buddy(sb, group, &e4b);
2064 ext4_mb_release_desc(&e4b);
2068 * If the particular group doesn't satisfy our
2069 * criteria we continue with the next group
2071 if (!ext4_mb_good_group(ac, group, cr))
2074 err = ext4_mb_load_buddy(sb, group, &e4b);
2078 ext4_lock_group(sb, group);
2079 if (!ext4_mb_good_group(ac, group, cr)) {
2080 /* someone did allocation from this group */
2081 ext4_unlock_group(sb, group);
2082 ext4_mb_release_desc(&e4b);
2086 ac->ac_groups_scanned++;
2087 desc = ext4_get_group_desc(sb, group, NULL);
2088 if (cr == 0 || (desc->bg_flags &
2089 cpu_to_le16(EXT4_BG_BLOCK_UNINIT) &&
2090 ac->ac_2order != 0))
2091 ext4_mb_simple_scan_group(ac, &e4b);
2093 ac->ac_g_ex.fe_len == sbi->s_stripe)
2094 ext4_mb_scan_aligned(ac, &e4b);
2096 ext4_mb_complex_scan_group(ac, &e4b);
2098 ext4_unlock_group(sb, group);
2099 ext4_mb_release_desc(&e4b);
2101 if (ac->ac_status != AC_STATUS_CONTINUE)
2106 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2107 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2109 * We've been searching too long. Let's try to allocate
2110 * the best chunk we've found so far
2113 ext4_mb_try_best_found(ac, &e4b);
2114 if (ac->ac_status != AC_STATUS_FOUND) {
2116 * Someone more lucky has already allocated it.
2117 * The only thing we can do is just take first
2119 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2121 ac->ac_b_ex.fe_group = 0;
2122 ac->ac_b_ex.fe_start = 0;
2123 ac->ac_b_ex.fe_len = 0;
2124 ac->ac_status = AC_STATUS_CONTINUE;
2125 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2127 atomic_inc(&sbi->s_mb_lost_chunks);
2135 #ifdef EXT4_MB_HISTORY
2136 struct ext4_mb_proc_session {
2137 struct ext4_mb_history *history;
2138 struct super_block *sb;
2143 static void *ext4_mb_history_skip_empty(struct ext4_mb_proc_session *s,
2144 struct ext4_mb_history *hs,
2147 if (hs == s->history + s->max)
2149 if (!first && hs == s->history + s->start)
2151 while (hs->orig.fe_len == 0) {
2153 if (hs == s->history + s->max)
2155 if (hs == s->history + s->start)
2161 static void *ext4_mb_seq_history_start(struct seq_file *seq, loff_t *pos)
2163 struct ext4_mb_proc_session *s = seq->private;
2164 struct ext4_mb_history *hs;
2168 return SEQ_START_TOKEN;
2169 hs = ext4_mb_history_skip_empty(s, s->history + s->start, 1);
2172 while (--l && (hs = ext4_mb_history_skip_empty(s, ++hs, 0)) != NULL);
2176 static void *ext4_mb_seq_history_next(struct seq_file *seq, void *v,
2179 struct ext4_mb_proc_session *s = seq->private;
2180 struct ext4_mb_history *hs = v;
2183 if (v == SEQ_START_TOKEN)
2184 return ext4_mb_history_skip_empty(s, s->history + s->start, 1);
2186 return ext4_mb_history_skip_empty(s, ++hs, 0);
2189 static int ext4_mb_seq_history_show(struct seq_file *seq, void *v)
2191 char buf[25], buf2[25], buf3[25], *fmt;
2192 struct ext4_mb_history *hs = v;
2194 if (v == SEQ_START_TOKEN) {
2195 seq_printf(seq, "%-5s %-8s %-23s %-23s %-23s %-5s "
2196 "%-5s %-2s %-5s %-5s %-5s %-6s\n",
2197 "pid", "inode", "original", "goal", "result", "found",
2198 "grps", "cr", "flags", "merge", "tail", "broken");
2202 if (hs->op == EXT4_MB_HISTORY_ALLOC) {
2203 fmt = "%-5u %-8u %-23s %-23s %-23s %-5u %-5u %-2u "
2204 "%-5u %-5s %-5u %-6u\n";
2205 sprintf(buf2, "%lu/%d/%u@%u", hs->result.fe_group,
2206 hs->result.fe_start, hs->result.fe_len,
2207 hs->result.fe_logical);
2208 sprintf(buf, "%lu/%d/%u@%u", hs->orig.fe_group,
2209 hs->orig.fe_start, hs->orig.fe_len,
2210 hs->orig.fe_logical);
2211 sprintf(buf3, "%lu/%d/%u@%u", hs->goal.fe_group,
2212 hs->goal.fe_start, hs->goal.fe_len,
2213 hs->goal.fe_logical);
2214 seq_printf(seq, fmt, hs->pid, hs->ino, buf, buf3, buf2,
2215 hs->found, hs->groups, hs->cr, hs->flags,
2216 hs->merged ? "M" : "", hs->tail,
2217 hs->buddy ? 1 << hs->buddy : 0);
2218 } else if (hs->op == EXT4_MB_HISTORY_PREALLOC) {
2219 fmt = "%-5u %-8u %-23s %-23s %-23s\n";
2220 sprintf(buf2, "%lu/%d/%u@%u", hs->result.fe_group,
2221 hs->result.fe_start, hs->result.fe_len,
2222 hs->result.fe_logical);
2223 sprintf(buf, "%lu/%d/%u@%u", hs->orig.fe_group,
2224 hs->orig.fe_start, hs->orig.fe_len,
2225 hs->orig.fe_logical);
2226 seq_printf(seq, fmt, hs->pid, hs->ino, buf, "", buf2);
2227 } else if (hs->op == EXT4_MB_HISTORY_DISCARD) {
2228 sprintf(buf2, "%lu/%d/%u", hs->result.fe_group,
2229 hs->result.fe_start, hs->result.fe_len);
2230 seq_printf(seq, "%-5u %-8u %-23s discard\n",
2231 hs->pid, hs->ino, buf2);
2232 } else if (hs->op == EXT4_MB_HISTORY_FREE) {
2233 sprintf(buf2, "%lu/%d/%u", hs->result.fe_group,
2234 hs->result.fe_start, hs->result.fe_len);
2235 seq_printf(seq, "%-5u %-8u %-23s free\n",
2236 hs->pid, hs->ino, buf2);
2241 static void ext4_mb_seq_history_stop(struct seq_file *seq, void *v)
2245 static struct seq_operations ext4_mb_seq_history_ops = {
2246 .start = ext4_mb_seq_history_start,
2247 .next = ext4_mb_seq_history_next,
2248 .stop = ext4_mb_seq_history_stop,
2249 .show = ext4_mb_seq_history_show,
2252 static int ext4_mb_seq_history_open(struct inode *inode, struct file *file)
2254 struct super_block *sb = PDE(inode)->data;
2255 struct ext4_sb_info *sbi = EXT4_SB(sb);
2256 struct ext4_mb_proc_session *s;
2260 s = kmalloc(sizeof(*s), GFP_KERNEL);
2264 size = sizeof(struct ext4_mb_history) * sbi->s_mb_history_max;
2265 s->history = kmalloc(size, GFP_KERNEL);
2266 if (s->history == NULL) {
2271 spin_lock(&sbi->s_mb_history_lock);
2272 memcpy(s->history, sbi->s_mb_history, size);
2273 s->max = sbi->s_mb_history_max;
2274 s->start = sbi->s_mb_history_cur % s->max;
2275 spin_unlock(&sbi->s_mb_history_lock);
2277 rc = seq_open(file, &ext4_mb_seq_history_ops);
2279 struct seq_file *m = (struct seq_file *)file->private_data;
2289 static int ext4_mb_seq_history_release(struct inode *inode, struct file *file)
2291 struct seq_file *seq = (struct seq_file *)file->private_data;
2292 struct ext4_mb_proc_session *s = seq->private;
2295 return seq_release(inode, file);
2298 static ssize_t ext4_mb_seq_history_write(struct file *file,
2299 const char __user *buffer,
2300 size_t count, loff_t *ppos)
2302 struct seq_file *seq = (struct seq_file *)file->private_data;
2303 struct ext4_mb_proc_session *s = seq->private;
2304 struct super_block *sb = s->sb;
2308 if (count >= sizeof(str)) {
2309 printk(KERN_ERR "EXT4-fs: %s string too long, max %u bytes\n",
2310 "mb_history", (int)sizeof(str));
2314 if (copy_from_user(str, buffer, count))
2317 value = simple_strtol(str, NULL, 0);
2320 EXT4_SB(sb)->s_mb_history_filter = value;
2325 static struct file_operations ext4_mb_seq_history_fops = {
2326 .owner = THIS_MODULE,
2327 .open = ext4_mb_seq_history_open,
2329 .write = ext4_mb_seq_history_write,
2330 .llseek = seq_lseek,
2331 .release = ext4_mb_seq_history_release,
2334 static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2336 struct super_block *sb = seq->private;
2337 struct ext4_sb_info *sbi = EXT4_SB(sb);
2340 if (*pos < 0 || *pos >= sbi->s_groups_count)
2344 return (void *) group;
2347 static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2349 struct super_block *sb = seq->private;
2350 struct ext4_sb_info *sbi = EXT4_SB(sb);
2354 if (*pos < 0 || *pos >= sbi->s_groups_count)
2357 return (void *) group;;
2360 static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2362 struct super_block *sb = seq->private;
2363 long group = (long) v;
2366 struct ext4_buddy e4b;
2368 struct ext4_group_info info;
2369 unsigned short counters[16];
2374 seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2375 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2376 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2377 "group", "free", "frags", "first",
2378 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2379 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2381 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2382 sizeof(struct ext4_group_info);
2383 err = ext4_mb_load_buddy(sb, group, &e4b);
2385 seq_printf(seq, "#%-5lu: I/O error\n", group);
2388 ext4_lock_group(sb, group);
2389 memcpy(&sg, ext4_get_group_info(sb, group), i);
2390 ext4_unlock_group(sb, group);
2391 ext4_mb_release_desc(&e4b);
2393 seq_printf(seq, "#%-5lu: %-5u %-5u %-5u [", group, sg.info.bb_free,
2394 sg.info.bb_fragments, sg.info.bb_first_free);
2395 for (i = 0; i <= 13; i++)
2396 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2397 sg.info.bb_counters[i] : 0);
2398 seq_printf(seq, " ]\n");
2403 static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2407 static struct seq_operations ext4_mb_seq_groups_ops = {
2408 .start = ext4_mb_seq_groups_start,
2409 .next = ext4_mb_seq_groups_next,
2410 .stop = ext4_mb_seq_groups_stop,
2411 .show = ext4_mb_seq_groups_show,
2414 static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2416 struct super_block *sb = PDE(inode)->data;
2419 rc = seq_open(file, &ext4_mb_seq_groups_ops);
2421 struct seq_file *m = (struct seq_file *)file->private_data;
2428 static struct file_operations ext4_mb_seq_groups_fops = {
2429 .owner = THIS_MODULE,
2430 .open = ext4_mb_seq_groups_open,
2432 .llseek = seq_lseek,
2433 .release = seq_release,
2436 static void ext4_mb_history_release(struct super_block *sb)
2438 struct ext4_sb_info *sbi = EXT4_SB(sb);
2440 remove_proc_entry("mb_groups", sbi->s_mb_proc);
2441 remove_proc_entry("mb_history", sbi->s_mb_proc);
2443 kfree(sbi->s_mb_history);
2446 static void ext4_mb_history_init(struct super_block *sb)
2448 struct ext4_sb_info *sbi = EXT4_SB(sb);
2451 if (sbi->s_mb_proc != NULL) {
2452 proc_create_data("mb_history", S_IRUGO, sbi->s_mb_proc,
2453 &ext4_mb_seq_history_fops, sb);
2454 proc_create_data("mb_groups", S_IRUGO, sbi->s_mb_proc,
2455 &ext4_mb_seq_groups_fops, sb);
2458 sbi->s_mb_history_max = 1000;
2459 sbi->s_mb_history_cur = 0;
2460 spin_lock_init(&sbi->s_mb_history_lock);
2461 i = sbi->s_mb_history_max * sizeof(struct ext4_mb_history);
2462 sbi->s_mb_history = kmalloc(i, GFP_KERNEL);
2463 if (likely(sbi->s_mb_history != NULL))
2464 memset(sbi->s_mb_history, 0, i);
2465 /* if we can't allocate history, then we simple won't use it */
2468 static void ext4_mb_store_history(struct ext4_allocation_context *ac)
2470 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
2471 struct ext4_mb_history h;
2473 if (unlikely(sbi->s_mb_history == NULL))
2476 if (!(ac->ac_op & sbi->s_mb_history_filter))
2480 h.pid = current->pid;
2481 h.ino = ac->ac_inode ? ac->ac_inode->i_ino : 0;
2482 h.orig = ac->ac_o_ex;
2483 h.result = ac->ac_b_ex;
2484 h.flags = ac->ac_flags;
2485 h.found = ac->ac_found;
2486 h.groups = ac->ac_groups_scanned;
2487 h.cr = ac->ac_criteria;
2488 h.tail = ac->ac_tail;
2489 h.buddy = ac->ac_buddy;
2491 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC) {
2492 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
2493 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
2495 h.goal = ac->ac_g_ex;
2496 h.result = ac->ac_f_ex;
2499 spin_lock(&sbi->s_mb_history_lock);
2500 memcpy(sbi->s_mb_history + sbi->s_mb_history_cur, &h, sizeof(h));
2501 if (++sbi->s_mb_history_cur >= sbi->s_mb_history_max)
2502 sbi->s_mb_history_cur = 0;
2503 spin_unlock(&sbi->s_mb_history_lock);
2507 #define ext4_mb_history_release(sb)
2508 #define ext4_mb_history_init(sb)
2511 static int ext4_mb_init_backend(struct super_block *sb)
2514 int j, len, metalen;
2515 struct ext4_sb_info *sbi = EXT4_SB(sb);
2516 int num_meta_group_infos =
2517 (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) >>
2518 EXT4_DESC_PER_BLOCK_BITS(sb);
2519 struct ext4_group_info **meta_group_info;
2521 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2522 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2523 * So a two level scheme suffices for now. */
2524 sbi->s_group_info = kmalloc(sizeof(*sbi->s_group_info) *
2525 num_meta_group_infos, GFP_KERNEL);
2526 if (sbi->s_group_info == NULL) {
2527 printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n");
2530 sbi->s_buddy_cache = new_inode(sb);
2531 if (sbi->s_buddy_cache == NULL) {
2532 printk(KERN_ERR "EXT4-fs: can't get new inode\n");
2535 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
2537 metalen = sizeof(*meta_group_info) << EXT4_DESC_PER_BLOCK_BITS(sb);
2538 for (i = 0; i < num_meta_group_infos; i++) {
2539 if ((i + 1) == num_meta_group_infos)
2540 metalen = sizeof(*meta_group_info) *
2541 (sbi->s_groups_count -
2542 (i << EXT4_DESC_PER_BLOCK_BITS(sb)));
2543 meta_group_info = kmalloc(metalen, GFP_KERNEL);
2544 if (meta_group_info == NULL) {
2545 printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
2549 sbi->s_group_info[i] = meta_group_info;
2553 * calculate needed size. if change bb_counters size,
2554 * don't forget about ext4_mb_generate_buddy()
2556 len = sizeof(struct ext4_group_info);
2557 len += sizeof(unsigned short) * (sb->s_blocksize_bits + 2);
2558 for (i = 0; i < sbi->s_groups_count; i++) {
2559 struct ext4_group_desc *desc;
2562 sbi->s_group_info[i >> EXT4_DESC_PER_BLOCK_BITS(sb)];
2563 j = i & (EXT4_DESC_PER_BLOCK(sb) - 1);
2565 meta_group_info[j] = kzalloc(len, GFP_KERNEL);
2566 if (meta_group_info[j] == NULL) {
2567 printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n");
2571 desc = ext4_get_group_desc(sb, i, NULL);
2574 "EXT4-fs: can't read descriptor %lu\n", i);
2577 memset(meta_group_info[j], 0, len);
2578 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2579 &(meta_group_info[j]->bb_state));
2582 * initialize bb_free to be able to skip
2583 * empty groups without initialization
2585 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2586 meta_group_info[j]->bb_free =
2587 ext4_free_blocks_after_init(sb, i, desc);
2589 meta_group_info[j]->bb_free =
2590 le16_to_cpu(desc->bg_free_blocks_count);
2593 INIT_LIST_HEAD(&meta_group_info[j]->bb_prealloc_list);
2597 struct buffer_head *bh;
2598 meta_group_info[j]->bb_bitmap =
2599 kmalloc(sb->s_blocksize, GFP_KERNEL);
2600 BUG_ON(meta_group_info[j]->bb_bitmap == NULL);
2601 bh = read_block_bitmap(sb, i);
2603 memcpy(meta_group_info[j]->bb_bitmap, bh->b_data,
2615 kfree(ext4_get_group_info(sb, i));
2618 i = num_meta_group_infos;
2621 kfree(sbi->s_group_info[i]);
2622 iput(sbi->s_buddy_cache);
2624 kfree(sbi->s_group_info);
2628 int ext4_mb_init(struct super_block *sb, int needs_recovery)
2630 struct ext4_sb_info *sbi = EXT4_SB(sb);
2635 if (!test_opt(sb, MBALLOC))
2638 i = (sb->s_blocksize_bits + 2) * sizeof(unsigned short);
2640 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2641 if (sbi->s_mb_offsets == NULL) {
2642 clear_opt(sbi->s_mount_opt, MBALLOC);
2645 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2646 if (sbi->s_mb_maxs == NULL) {
2647 clear_opt(sbi->s_mount_opt, MBALLOC);
2648 kfree(sbi->s_mb_maxs);
2652 /* order 0 is regular bitmap */
2653 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2654 sbi->s_mb_offsets[0] = 0;
2658 max = sb->s_blocksize << 2;
2660 sbi->s_mb_offsets[i] = offset;
2661 sbi->s_mb_maxs[i] = max;
2662 offset += 1 << (sb->s_blocksize_bits - i);
2665 } while (i <= sb->s_blocksize_bits + 1);
2667 /* init file for buddy data */
2668 i = ext4_mb_init_backend(sb);
2670 clear_opt(sbi->s_mount_opt, MBALLOC);
2671 kfree(sbi->s_mb_offsets);
2672 kfree(sbi->s_mb_maxs);
2676 spin_lock_init(&sbi->s_md_lock);
2677 INIT_LIST_HEAD(&sbi->s_active_transaction);
2678 INIT_LIST_HEAD(&sbi->s_closed_transaction);
2679 INIT_LIST_HEAD(&sbi->s_committed_transaction);
2680 spin_lock_init(&sbi->s_bal_lock);
2682 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2683 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2684 sbi->s_mb_stats = MB_DEFAULT_STATS;
2685 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2686 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
2687 sbi->s_mb_history_filter = EXT4_MB_HISTORY_DEFAULT;
2688 sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
2690 i = sizeof(struct ext4_locality_group) * NR_CPUS;
2691 sbi->s_locality_groups = kmalloc(i, GFP_KERNEL);
2692 if (sbi->s_locality_groups == NULL) {
2693 clear_opt(sbi->s_mount_opt, MBALLOC);
2694 kfree(sbi->s_mb_offsets);
2695 kfree(sbi->s_mb_maxs);
2698 for (i = 0; i < NR_CPUS; i++) {
2699 struct ext4_locality_group *lg;
2700 lg = &sbi->s_locality_groups[i];
2701 mutex_init(&lg->lg_mutex);
2702 INIT_LIST_HEAD(&lg->lg_prealloc_list);
2703 spin_lock_init(&lg->lg_prealloc_lock);
2706 ext4_mb_init_per_dev_proc(sb);
2707 ext4_mb_history_init(sb);
2709 printk("EXT4-fs: mballoc enabled\n");
2713 /* need to called with ext4 group lock (ext4_lock_group) */
2714 static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2716 struct ext4_prealloc_space *pa;
2717 struct list_head *cur, *tmp;
2720 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2721 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2722 list_del(&pa->pa_group_list);
2727 mb_debug("mballoc: %u PAs left\n", count);
2731 int ext4_mb_release(struct super_block *sb)
2734 int num_meta_group_infos;
2735 struct ext4_group_info *grinfo;
2736 struct ext4_sb_info *sbi = EXT4_SB(sb);
2738 if (!test_opt(sb, MBALLOC))
2741 /* release freed, non-committed blocks */
2742 spin_lock(&sbi->s_md_lock);
2743 list_splice_init(&sbi->s_closed_transaction,
2744 &sbi->s_committed_transaction);
2745 list_splice_init(&sbi->s_active_transaction,
2746 &sbi->s_committed_transaction);
2747 spin_unlock(&sbi->s_md_lock);
2748 ext4_mb_free_committed_blocks(sb);
2750 if (sbi->s_group_info) {
2751 for (i = 0; i < sbi->s_groups_count; i++) {
2752 grinfo = ext4_get_group_info(sb, i);
2754 kfree(grinfo->bb_bitmap);
2756 ext4_lock_group(sb, i);
2757 ext4_mb_cleanup_pa(grinfo);
2758 ext4_unlock_group(sb, i);
2761 num_meta_group_infos = (sbi->s_groups_count +
2762 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2763 EXT4_DESC_PER_BLOCK_BITS(sb);
2764 for (i = 0; i < num_meta_group_infos; i++)
2765 kfree(sbi->s_group_info[i]);
2766 kfree(sbi->s_group_info);
2768 kfree(sbi->s_mb_offsets);
2769 kfree(sbi->s_mb_maxs);
2770 if (sbi->s_buddy_cache)
2771 iput(sbi->s_buddy_cache);
2772 if (sbi->s_mb_stats) {
2774 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2775 atomic_read(&sbi->s_bal_allocated),
2776 atomic_read(&sbi->s_bal_reqs),
2777 atomic_read(&sbi->s_bal_success));
2779 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2780 "%u 2^N hits, %u breaks, %u lost\n",
2781 atomic_read(&sbi->s_bal_ex_scanned),
2782 atomic_read(&sbi->s_bal_goals),
2783 atomic_read(&sbi->s_bal_2orders),
2784 atomic_read(&sbi->s_bal_breaks),
2785 atomic_read(&sbi->s_mb_lost_chunks));
2787 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2788 sbi->s_mb_buddies_generated++,
2789 sbi->s_mb_generation_time);
2791 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2792 atomic_read(&sbi->s_mb_preallocated),
2793 atomic_read(&sbi->s_mb_discarded));
2796 kfree(sbi->s_locality_groups);
2798 ext4_mb_history_release(sb);
2799 ext4_mb_destroy_per_dev_proc(sb);
2804 static void ext4_mb_free_committed_blocks(struct super_block *sb)
2806 struct ext4_sb_info *sbi = EXT4_SB(sb);
2811 struct ext4_free_metadata *md;
2812 struct ext4_buddy e4b;
2814 if (list_empty(&sbi->s_committed_transaction))
2817 /* there is committed blocks to be freed yet */
2819 /* get next array of blocks */
2821 spin_lock(&sbi->s_md_lock);
2822 if (!list_empty(&sbi->s_committed_transaction)) {
2823 md = list_entry(sbi->s_committed_transaction.next,
2824 struct ext4_free_metadata, list);
2825 list_del(&md->list);
2827 spin_unlock(&sbi->s_md_lock);
2832 mb_debug("gonna free %u blocks in group %lu (0x%p):",
2833 md->num, md->group, md);
2835 err = ext4_mb_load_buddy(sb, md->group, &e4b);
2836 /* we expect to find existing buddy because it's pinned */
2839 /* there are blocks to put in buddy to make them really free */
2842 ext4_lock_group(sb, md->group);
2843 for (i = 0; i < md->num; i++) {
2844 mb_debug(" %u", md->blocks[i]);
2845 err = mb_free_blocks(NULL, &e4b, md->blocks[i], 1);
2849 ext4_unlock_group(sb, md->group);
2851 /* balance refcounts from ext4_mb_free_metadata() */
2852 page_cache_release(e4b.bd_buddy_page);
2853 page_cache_release(e4b.bd_bitmap_page);
2856 ext4_mb_release_desc(&e4b);
2860 mb_debug("freed %u blocks in %u structures\n", count, count2);
2863 #define EXT4_MB_STATS_NAME "stats"
2864 #define EXT4_MB_MAX_TO_SCAN_NAME "max_to_scan"
2865 #define EXT4_MB_MIN_TO_SCAN_NAME "min_to_scan"
2866 #define EXT4_MB_ORDER2_REQ "order2_req"
2867 #define EXT4_MB_STREAM_REQ "stream_req"
2868 #define EXT4_MB_GROUP_PREALLOC "group_prealloc"
2872 #define MB_PROC_VALUE_READ(name) \
2873 static int ext4_mb_read_##name(char *page, char **start, \
2874 off_t off, int count, int *eof, void *data) \
2876 struct ext4_sb_info *sbi = data; \
2881 len = sprintf(page, "%ld\n", sbi->s_mb_##name); \
2886 #define MB_PROC_VALUE_WRITE(name) \
2887 static int ext4_mb_write_##name(struct file *file, \
2888 const char __user *buf, unsigned long cnt, void *data) \
2890 struct ext4_sb_info *sbi = data; \
2893 if (cnt >= sizeof(str)) \
2895 if (copy_from_user(str, buf, cnt)) \
2897 value = simple_strtol(str, NULL, 0); \
2900 sbi->s_mb_##name = value; \
2904 MB_PROC_VALUE_READ(stats);
2905 MB_PROC_VALUE_WRITE(stats);
2906 MB_PROC_VALUE_READ(max_to_scan);
2907 MB_PROC_VALUE_WRITE(max_to_scan);
2908 MB_PROC_VALUE_READ(min_to_scan);
2909 MB_PROC_VALUE_WRITE(min_to_scan);
2910 MB_PROC_VALUE_READ(order2_reqs);
2911 MB_PROC_VALUE_WRITE(order2_reqs);
2912 MB_PROC_VALUE_READ(stream_request);
2913 MB_PROC_VALUE_WRITE(stream_request);
2914 MB_PROC_VALUE_READ(group_prealloc);
2915 MB_PROC_VALUE_WRITE(group_prealloc);
2917 #define MB_PROC_HANDLER(name, var) \
2919 proc = create_proc_entry(name, mode, sbi->s_mb_proc); \
2920 if (proc == NULL) { \
2921 printk(KERN_ERR "EXT4-fs: can't to create %s\n", name); \
2925 proc->read_proc = ext4_mb_read_##var ; \
2926 proc->write_proc = ext4_mb_write_##var; \
2929 static int ext4_mb_init_per_dev_proc(struct super_block *sb)
2931 mode_t mode = S_IFREG | S_IRUGO | S_IWUSR;
2932 struct ext4_sb_info *sbi = EXT4_SB(sb);
2933 struct proc_dir_entry *proc;
2936 snprintf(devname, sizeof(devname) - 1, "%s",
2937 bdevname(sb->s_bdev, devname));
2938 sbi->s_mb_proc = proc_mkdir(devname, proc_root_ext4);
2940 MB_PROC_HANDLER(EXT4_MB_STATS_NAME, stats);
2941 MB_PROC_HANDLER(EXT4_MB_MAX_TO_SCAN_NAME, max_to_scan);
2942 MB_PROC_HANDLER(EXT4_MB_MIN_TO_SCAN_NAME, min_to_scan);
2943 MB_PROC_HANDLER(EXT4_MB_ORDER2_REQ, order2_reqs);
2944 MB_PROC_HANDLER(EXT4_MB_STREAM_REQ, stream_request);
2945 MB_PROC_HANDLER(EXT4_MB_GROUP_PREALLOC, group_prealloc);
2950 printk(KERN_ERR "EXT4-fs: Unable to create %s\n", devname);
2951 remove_proc_entry(EXT4_MB_GROUP_PREALLOC, sbi->s_mb_proc);
2952 remove_proc_entry(EXT4_MB_STREAM_REQ, sbi->s_mb_proc);
2953 remove_proc_entry(EXT4_MB_ORDER2_REQ, sbi->s_mb_proc);
2954 remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME, sbi->s_mb_proc);
2955 remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME, sbi->s_mb_proc);
2956 remove_proc_entry(EXT4_MB_STATS_NAME, sbi->s_mb_proc);
2957 remove_proc_entry(devname, proc_root_ext4);
2958 sbi->s_mb_proc = NULL;
2963 static int ext4_mb_destroy_per_dev_proc(struct super_block *sb)
2965 struct ext4_sb_info *sbi = EXT4_SB(sb);
2968 if (sbi->s_mb_proc == NULL)
2971 snprintf(devname, sizeof(devname) - 1, "%s",
2972 bdevname(sb->s_bdev, devname));
2973 remove_proc_entry(EXT4_MB_GROUP_PREALLOC, sbi->s_mb_proc);
2974 remove_proc_entry(EXT4_MB_STREAM_REQ, sbi->s_mb_proc);
2975 remove_proc_entry(EXT4_MB_ORDER2_REQ, sbi->s_mb_proc);
2976 remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME, sbi->s_mb_proc);
2977 remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME, sbi->s_mb_proc);
2978 remove_proc_entry(EXT4_MB_STATS_NAME, sbi->s_mb_proc);
2979 remove_proc_entry(devname, proc_root_ext4);
2984 int __init init_ext4_mballoc(void)
2986 ext4_pspace_cachep =
2987 kmem_cache_create("ext4_prealloc_space",
2988 sizeof(struct ext4_prealloc_space),
2989 0, SLAB_RECLAIM_ACCOUNT, NULL);
2990 if (ext4_pspace_cachep == NULL)
2994 kmem_cache_create("ext4_alloc_context",
2995 sizeof(struct ext4_allocation_context),
2996 0, SLAB_RECLAIM_ACCOUNT, NULL);
2997 if (ext4_ac_cachep == NULL) {
2998 kmem_cache_destroy(ext4_pspace_cachep);
3001 #ifdef CONFIG_PROC_FS
3002 proc_root_ext4 = proc_mkdir("fs/ext4", NULL);
3003 if (proc_root_ext4 == NULL)
3004 printk(KERN_ERR "EXT4-fs: Unable to create fs/ext4\n");
3009 void exit_ext4_mballoc(void)
3011 /* XXX: synchronize_rcu(); */
3012 kmem_cache_destroy(ext4_pspace_cachep);
3013 kmem_cache_destroy(ext4_ac_cachep);
3014 #ifdef CONFIG_PROC_FS
3015 remove_proc_entry("fs/ext4", NULL);
3021 * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
3022 * Returns 0 if success or error code
3024 static int ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
3027 struct buffer_head *bitmap_bh = NULL;
3028 struct ext4_super_block *es;
3029 struct ext4_group_desc *gdp;
3030 struct buffer_head *gdp_bh;
3031 struct ext4_sb_info *sbi;
3032 struct super_block *sb;
3036 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3037 BUG_ON(ac->ac_b_ex.fe_len <= 0);
3043 ext4_debug("using block group %lu(%d)\n", ac->ac_b_ex.fe_group,
3044 gdp->bg_free_blocks_count);
3047 bitmap_bh = read_block_bitmap(sb, ac->ac_b_ex.fe_group);
3051 err = ext4_journal_get_write_access(handle, bitmap_bh);
3056 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
3060 err = ext4_journal_get_write_access(handle, gdp_bh);
3064 block = ac->ac_b_ex.fe_group * EXT4_BLOCKS_PER_GROUP(sb)
3065 + ac->ac_b_ex.fe_start
3066 + le32_to_cpu(es->s_first_data_block);
3068 if (block == ext4_block_bitmap(sb, gdp) ||
3069 block == ext4_inode_bitmap(sb, gdp) ||
3070 in_range(block, ext4_inode_table(sb, gdp),
3071 EXT4_SB(sb)->s_itb_per_group)) {
3073 ext4_error(sb, __FUNCTION__,
3074 "Allocating block in system zone - block = %llu",
3077 #ifdef AGGRESSIVE_CHECK
3080 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
3081 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
3082 bitmap_bh->b_data));
3086 mb_set_bits(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group), bitmap_bh->b_data,
3087 ac->ac_b_ex.fe_start, ac->ac_b_ex.fe_len);
3089 spin_lock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
3090 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
3091 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
3092 gdp->bg_free_blocks_count =
3093 cpu_to_le16(ext4_free_blocks_after_init(sb,
3094 ac->ac_b_ex.fe_group,
3097 gdp->bg_free_blocks_count =
3098 cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)
3099 - ac->ac_b_ex.fe_len);
3100 gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
3101 spin_unlock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
3102 percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
3104 err = ext4_journal_dirty_metadata(handle, bitmap_bh);
3107 err = ext4_journal_dirty_metadata(handle, gdp_bh);
3116 * here we normalize request for locality group
3117 * Group request are normalized to s_strip size if we set the same via mount
3118 * option. If not we set it to s_mb_group_prealloc which can be configured via
3119 * /proc/fs/ext4/<partition>/group_prealloc
3121 * XXX: should we try to preallocate more than the group has now?
3123 static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
3125 struct super_block *sb = ac->ac_sb;
3126 struct ext4_locality_group *lg = ac->ac_lg;
3129 if (EXT4_SB(sb)->s_stripe)
3130 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
3132 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
3133 mb_debug("#%u: goal %lu blocks for locality group\n",
3134 current->pid, ac->ac_g_ex.fe_len);
3138 * Normalization means making request better in terms of
3139 * size and alignment
3141 static void ext4_mb_normalize_request(struct ext4_allocation_context *ac,
3142 struct ext4_allocation_request *ar)
3146 struct list_head *cur;
3147 loff_t size, orig_size, start_off;
3148 ext4_lblk_t start, orig_start;
3149 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3151 /* do normalize only data requests, metadata requests
3152 do not need preallocation */
3153 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3156 /* sometime caller may want exact blocks */
3157 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3160 /* caller may indicate that preallocation isn't
3161 * required (it's a tail, for example) */
3162 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
3165 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
3166 ext4_mb_normalize_group_request(ac);
3170 bsbits = ac->ac_sb->s_blocksize_bits;
3172 /* first, let's learn actual file size
3173 * given current request is allocated */
3174 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
3175 size = size << bsbits;
3176 if (size < i_size_read(ac->ac_inode))
3177 size = i_size_read(ac->ac_inode);
3179 /* max available blocks in a free group */
3180 max = EXT4_BLOCKS_PER_GROUP(ac->ac_sb) - 1 - 1 -
3181 EXT4_SB(ac->ac_sb)->s_itb_per_group;
3183 #define NRL_CHECK_SIZE(req, size, max,bits) \
3184 (req <= (size) || max <= ((size) >> bits))
3186 /* first, try to predict filesize */
3187 /* XXX: should this table be tunable? */
3189 if (size <= 16 * 1024) {
3191 } else if (size <= 32 * 1024) {
3193 } else if (size <= 64 * 1024) {
3195 } else if (size <= 128 * 1024) {
3197 } else if (size <= 256 * 1024) {
3199 } else if (size <= 512 * 1024) {
3201 } else if (size <= 1024 * 1024) {
3203 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, bsbits)) {
3204 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3205 (20 - bsbits)) << 20;
3207 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, bsbits)) {
3208 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3209 (22 - bsbits)) << 22;
3210 size = 4 * 1024 * 1024;
3211 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
3212 (8<<20)>>bsbits, max, bsbits)) {
3213 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3214 (23 - bsbits)) << 23;
3215 size = 8 * 1024 * 1024;
3217 start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
3218 size = ac->ac_o_ex.fe_len << bsbits;
3220 orig_size = size = size >> bsbits;
3221 orig_start = start = start_off >> bsbits;
3223 /* don't cover already allocated blocks in selected range */
3224 if (ar->pleft && start <= ar->lleft) {
3225 size -= ar->lleft + 1 - start;
3226 start = ar->lleft + 1;
3228 if (ar->pright && start + size - 1 >= ar->lright)
3229 size -= start + size - ar->lright;
3233 /* check we don't cross already preallocated blocks */
3235 list_for_each_rcu(cur, &ei->i_prealloc_list) {
3236 struct ext4_prealloc_space *pa;
3237 unsigned long pa_end;
3239 pa = list_entry(cur, struct ext4_prealloc_space, pa_inode_list);
3243 spin_lock(&pa->pa_lock);
3244 if (pa->pa_deleted) {
3245 spin_unlock(&pa->pa_lock);
3249 pa_end = pa->pa_lstart + pa->pa_len;
3251 /* PA must not overlap original request */
3252 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
3253 ac->ac_o_ex.fe_logical < pa->pa_lstart));
3255 /* skip PA normalized request doesn't overlap with */
3256 if (pa->pa_lstart >= end) {
3257 spin_unlock(&pa->pa_lock);
3260 if (pa_end <= start) {
3261 spin_unlock(&pa->pa_lock);
3264 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
3266 if (pa_end <= ac->ac_o_ex.fe_logical) {
3267 BUG_ON(pa_end < start);
3271 if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
3272 BUG_ON(pa->pa_lstart > end);
3273 end = pa->pa_lstart;
3275 spin_unlock(&pa->pa_lock);
3280 /* XXX: extra loop to check we really don't overlap preallocations */
3282 list_for_each_rcu(cur, &ei->i_prealloc_list) {
3283 struct ext4_prealloc_space *pa;
3284 unsigned long pa_end;
3285 pa = list_entry(cur, struct ext4_prealloc_space, pa_inode_list);
3286 spin_lock(&pa->pa_lock);
3287 if (pa->pa_deleted == 0) {
3288 pa_end = pa->pa_lstart + pa->pa_len;
3289 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3291 spin_unlock(&pa->pa_lock);
3295 if (start + size <= ac->ac_o_ex.fe_logical &&
3296 start > ac->ac_o_ex.fe_logical) {
3297 printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n",
3298 (unsigned long) start, (unsigned long) size,
3299 (unsigned long) ac->ac_o_ex.fe_logical);
3301 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
3302 start > ac->ac_o_ex.fe_logical);
3303 BUG_ON(size <= 0 || size >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
3305 /* now prepare goal request */
3307 /* XXX: is it better to align blocks WRT to logical
3308 * placement or satisfy big request as is */
3309 ac->ac_g_ex.fe_logical = start;
3310 ac->ac_g_ex.fe_len = size;
3312 /* define goal start in order to merge */
3313 if (ar->pright && (ar->lright == (start + size))) {
3314 /* merge to the right */
3315 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3316 &ac->ac_f_ex.fe_group,
3317 &ac->ac_f_ex.fe_start);
3318 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3320 if (ar->pleft && (ar->lleft + 1 == start)) {
3321 /* merge to the left */
3322 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3323 &ac->ac_f_ex.fe_group,
3324 &ac->ac_f_ex.fe_start);
3325 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3328 mb_debug("goal: %u(was %u) blocks at %u\n", (unsigned) size,
3329 (unsigned) orig_size, (unsigned) start);
3332 static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3334 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3336 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3337 atomic_inc(&sbi->s_bal_reqs);
3338 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
3339 if (ac->ac_o_ex.fe_len >= ac->ac_g_ex.fe_len)
3340 atomic_inc(&sbi->s_bal_success);
3341 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3342 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3343 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3344 atomic_inc(&sbi->s_bal_goals);
3345 if (ac->ac_found > sbi->s_mb_max_to_scan)
3346 atomic_inc(&sbi->s_bal_breaks);
3349 ext4_mb_store_history(ac);
3353 * use blocks preallocated to inode
3355 static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3356 struct ext4_prealloc_space *pa)
3362 /* found preallocated blocks, use them */
3363 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
3364 end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
3366 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3367 &ac->ac_b_ex.fe_start);
3368 ac->ac_b_ex.fe_len = len;
3369 ac->ac_status = AC_STATUS_FOUND;
3372 BUG_ON(start < pa->pa_pstart);
3373 BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
3374 BUG_ON(pa->pa_free < len);
3377 mb_debug("use %llu/%lu from inode pa %p\n", start, len, pa);
3381 * use blocks preallocated to locality group
3383 static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3384 struct ext4_prealloc_space *pa)
3386 unsigned len = ac->ac_o_ex.fe_len;
3388 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3389 &ac->ac_b_ex.fe_group,
3390 &ac->ac_b_ex.fe_start);
3391 ac->ac_b_ex.fe_len = len;
3392 ac->ac_status = AC_STATUS_FOUND;
3395 /* we don't correct pa_pstart or pa_plen here to avoid
3396 * possible race when the group is being loaded concurrently
3397 * instead we correct pa later, after blocks are marked
3398 * in on-disk bitmap -- see ext4_mb_release_context()
3399 * Other CPUs are prevented from allocating from this pa by lg_mutex
3401 mb_debug("use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
3405 * search goal blocks in preallocated space
3407 static int ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
3409 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3410 struct ext4_locality_group *lg;
3411 struct ext4_prealloc_space *pa;
3412 struct list_head *cur;
3414 /* only data can be preallocated */
3415 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3418 /* first, try per-file preallocation */
3420 list_for_each_rcu(cur, &ei->i_prealloc_list) {
3421 pa = list_entry(cur, struct ext4_prealloc_space, pa_inode_list);
3423 /* all fields in this condition don't change,
3424 * so we can skip locking for them */
3425 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
3426 ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
3429 /* found preallocated blocks, use them */
3430 spin_lock(&pa->pa_lock);
3431 if (pa->pa_deleted == 0 && pa->pa_free) {
3432 atomic_inc(&pa->pa_count);
3433 ext4_mb_use_inode_pa(ac, pa);
3434 spin_unlock(&pa->pa_lock);
3435 ac->ac_criteria = 10;
3439 spin_unlock(&pa->pa_lock);
3443 /* can we use group allocation? */
3444 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3447 /* inode may have no locality group for some reason */
3453 list_for_each_rcu(cur, &lg->lg_prealloc_list) {
3454 pa = list_entry(cur, struct ext4_prealloc_space, pa_inode_list);
3455 spin_lock(&pa->pa_lock);
3456 if (pa->pa_deleted == 0 && pa->pa_free >= ac->ac_o_ex.fe_len) {
3457 atomic_inc(&pa->pa_count);
3458 ext4_mb_use_group_pa(ac, pa);
3459 spin_unlock(&pa->pa_lock);
3460 ac->ac_criteria = 20;
3464 spin_unlock(&pa->pa_lock);
3472 * the function goes through all preallocation in this group and marks them
3473 * used in in-core bitmap. buddy must be generated from this bitmap
3474 * Need to be called with ext4 group lock (ext4_lock_group)
3476 static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
3479 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3480 struct ext4_prealloc_space *pa;
3481 struct list_head *cur;
3482 ext4_group_t groupnr;
3483 ext4_grpblk_t start;
3484 int preallocated = 0;
3488 /* all form of preallocation discards first load group,
3489 * so the only competing code is preallocation use.
3490 * we don't need any locking here
3491 * notice we do NOT ignore preallocations with pa_deleted
3492 * otherwise we could leave used blocks available for
3493 * allocation in buddy when concurrent ext4_mb_put_pa()
3494 * is dropping preallocation
3496 list_for_each(cur, &grp->bb_prealloc_list) {
3497 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3498 spin_lock(&pa->pa_lock);
3499 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3502 spin_unlock(&pa->pa_lock);
3503 if (unlikely(len == 0))
3505 BUG_ON(groupnr != group);
3506 mb_set_bits(sb_bgl_lock(EXT4_SB(sb), group),
3507 bitmap, start, len);
3508 preallocated += len;
3511 mb_debug("prellocated %u for group %lu\n", preallocated, group);
3514 static void ext4_mb_pa_callback(struct rcu_head *head)
3516 struct ext4_prealloc_space *pa;
3517 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
3518 kmem_cache_free(ext4_pspace_cachep, pa);
3522 * drops a reference to preallocated space descriptor
3523 * if this was the last reference and the space is consumed
3525 static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3526 struct super_block *sb, struct ext4_prealloc_space *pa)
3530 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3533 /* in this short window concurrent discard can set pa_deleted */
3534 spin_lock(&pa->pa_lock);
3535 if (pa->pa_deleted == 1) {
3536 spin_unlock(&pa->pa_lock);
3541 spin_unlock(&pa->pa_lock);
3543 /* -1 is to protect from crossing allocation group */
3544 ext4_get_group_no_and_offset(sb, pa->pa_pstart - 1, &grp, NULL);
3549 * P1 (buddy init) P2 (regular allocation)
3550 * find block B in PA
3551 * copy on-disk bitmap to buddy
3552 * mark B in on-disk bitmap
3553 * drop PA from group
3554 * mark all PAs in buddy
3556 * thus, P1 initializes buddy with B available. to prevent this
3557 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3560 ext4_lock_group(sb, grp);
3561 list_del(&pa->pa_group_list);
3562 ext4_unlock_group(sb, grp);
3564 spin_lock(pa->pa_obj_lock);
3565 list_del_rcu(&pa->pa_inode_list);
3566 spin_unlock(pa->pa_obj_lock);
3568 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3572 * creates new preallocated space for given inode
3574 static int ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
3576 struct super_block *sb = ac->ac_sb;
3577 struct ext4_prealloc_space *pa;
3578 struct ext4_group_info *grp;
3579 struct ext4_inode_info *ei;
3581 /* preallocate only when found space is larger then requested */
3582 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3583 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3584 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3586 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3590 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3596 /* we can't allocate as much as normalizer wants.
3597 * so, found space must get proper lstart
3598 * to cover original request */
3599 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3600 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3602 /* we're limited by original request in that
3603 * logical block must be covered any way
3604 * winl is window we can move our chunk within */
3605 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3607 /* also, we should cover whole original request */
3608 wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
3610 /* the smallest one defines real window */
3611 win = min(winl, wins);
3613 offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
3614 if (offs && offs < win)
3617 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
3618 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3619 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3622 /* preallocation can change ac_b_ex, thus we store actually
3623 * allocated blocks for history */
3624 ac->ac_f_ex = ac->ac_b_ex;
3626 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3627 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3628 pa->pa_len = ac->ac_b_ex.fe_len;
3629 pa->pa_free = pa->pa_len;
3630 atomic_set(&pa->pa_count, 1);
3631 spin_lock_init(&pa->pa_lock);
3635 mb_debug("new inode pa %p: %llu/%u for %u\n", pa,
3636 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3638 ext4_mb_use_inode_pa(ac, pa);
3639 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3641 ei = EXT4_I(ac->ac_inode);
3642 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3644 pa->pa_obj_lock = &ei->i_prealloc_lock;
3645 pa->pa_inode = ac->ac_inode;
3647 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3648 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3649 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3651 spin_lock(pa->pa_obj_lock);
3652 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3653 spin_unlock(pa->pa_obj_lock);
3659 * creates new preallocated space for locality group inodes belongs to
3661 static int ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
3663 struct super_block *sb = ac->ac_sb;
3664 struct ext4_locality_group *lg;
3665 struct ext4_prealloc_space *pa;
3666 struct ext4_group_info *grp;
3668 /* preallocate only when found space is larger then requested */
3669 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3670 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3671 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3673 BUG_ON(ext4_pspace_cachep == NULL);
3674 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3678 /* preallocation can change ac_b_ex, thus we store actually
3679 * allocated blocks for history */
3680 ac->ac_f_ex = ac->ac_b_ex;
3682 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3683 pa->pa_lstart = pa->pa_pstart;
3684 pa->pa_len = ac->ac_b_ex.fe_len;
3685 pa->pa_free = pa->pa_len;
3686 atomic_set(&pa->pa_count, 1);
3687 spin_lock_init(&pa->pa_lock);
3691 mb_debug("new group pa %p: %llu/%u for %u\n", pa,
3692 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3694 ext4_mb_use_group_pa(ac, pa);
3695 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3697 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3701 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3702 pa->pa_inode = NULL;
3704 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3705 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3706 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3708 spin_lock(pa->pa_obj_lock);
3709 list_add_tail_rcu(&pa->pa_inode_list, &lg->lg_prealloc_list);
3710 spin_unlock(pa->pa_obj_lock);
3715 static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3719 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3720 err = ext4_mb_new_group_pa(ac);
3722 err = ext4_mb_new_inode_pa(ac);
3727 * finds all unused blocks in on-disk bitmap, frees them in
3728 * in-core bitmap and buddy.
3729 * @pa must be unlinked from inode and group lists, so that
3730 * nobody else can find/use it.
3731 * the caller MUST hold group/inode locks.
3732 * TODO: optimize the case when there are no in-core structures yet
3734 static int ext4_mb_release_inode_pa(struct ext4_buddy *e4b,
3735 struct buffer_head *bitmap_bh,
3736 struct ext4_prealloc_space *pa)
3738 struct ext4_allocation_context *ac;
3739 struct super_block *sb = e4b->bd_sb;
3740 struct ext4_sb_info *sbi = EXT4_SB(sb);
3749 BUG_ON(pa->pa_deleted == 0);
3750 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3751 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3752 end = bit + pa->pa_len;
3754 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
3758 ac->ac_inode = pa->pa_inode;
3759 ac->ac_op = EXT4_MB_HISTORY_DISCARD;
3763 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
3766 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
3769 start = group * EXT4_BLOCKS_PER_GROUP(sb) + bit +
3770 le32_to_cpu(sbi->s_es->s_first_data_block);
3771 mb_debug(" free preallocated %u/%u in group %u\n",
3772 (unsigned) start, (unsigned) next - bit,
3777 ac->ac_b_ex.fe_group = group;
3778 ac->ac_b_ex.fe_start = bit;
3779 ac->ac_b_ex.fe_len = next - bit;
3780 ac->ac_b_ex.fe_logical = 0;
3781 ext4_mb_store_history(ac);
3784 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3787 if (free != pa->pa_free) {
3788 printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n",
3789 pa, (unsigned long) pa->pa_lstart,
3790 (unsigned long) pa->pa_pstart,
3791 (unsigned long) pa->pa_len);
3792 ext4_error(sb, __FUNCTION__, "free %u, pa_free %u\n",
3795 * pa is already deleted so we use the value obtained
3796 * from the bitmap and continue.
3799 atomic_add(free, &sbi->s_mb_discarded);
3801 kmem_cache_free(ext4_ac_cachep, ac);
3806 static int ext4_mb_release_group_pa(struct ext4_buddy *e4b,
3807 struct ext4_prealloc_space *pa)
3809 struct ext4_allocation_context *ac;
3810 struct super_block *sb = e4b->bd_sb;
3814 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
3817 ac->ac_op = EXT4_MB_HISTORY_DISCARD;
3819 BUG_ON(pa->pa_deleted == 0);
3820 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3821 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3822 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3823 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
3827 ac->ac_inode = NULL;
3828 ac->ac_b_ex.fe_group = group;
3829 ac->ac_b_ex.fe_start = bit;
3830 ac->ac_b_ex.fe_len = pa->pa_len;
3831 ac->ac_b_ex.fe_logical = 0;
3832 ext4_mb_store_history(ac);
3833 kmem_cache_free(ext4_ac_cachep, ac);
3840 * releases all preallocations in given group
3842 * first, we need to decide discard policy:
3843 * - when do we discard
3845 * - how many do we discard
3846 * 1) how many requested
3848 static int ext4_mb_discard_group_preallocations(struct super_block *sb,
3849 ext4_group_t group, int needed)
3851 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3852 struct buffer_head *bitmap_bh = NULL;
3853 struct ext4_prealloc_space *pa, *tmp;
3854 struct list_head list;
3855 struct ext4_buddy e4b;
3860 mb_debug("discard preallocation for group %lu\n", group);
3862 if (list_empty(&grp->bb_prealloc_list))
3865 bitmap_bh = read_block_bitmap(sb, group);
3866 if (bitmap_bh == NULL) {
3867 /* error handling here */
3868 ext4_mb_release_desc(&e4b);
3869 BUG_ON(bitmap_bh == NULL);
3872 err = ext4_mb_load_buddy(sb, group, &e4b);
3873 BUG_ON(err != 0); /* error handling here */
3876 needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
3878 grp = ext4_get_group_info(sb, group);
3879 INIT_LIST_HEAD(&list);
3882 ext4_lock_group(sb, group);
3883 list_for_each_entry_safe(pa, tmp,
3884 &grp->bb_prealloc_list, pa_group_list) {
3885 spin_lock(&pa->pa_lock);
3886 if (atomic_read(&pa->pa_count)) {
3887 spin_unlock(&pa->pa_lock);
3891 if (pa->pa_deleted) {
3892 spin_unlock(&pa->pa_lock);
3896 /* seems this one can be freed ... */
3899 /* we can trust pa_free ... */
3900 free += pa->pa_free;
3902 spin_unlock(&pa->pa_lock);
3904 list_del(&pa->pa_group_list);
3905 list_add(&pa->u.pa_tmp_list, &list);
3908 /* if we still need more blocks and some PAs were used, try again */
3909 if (free < needed && busy) {
3911 ext4_unlock_group(sb, group);
3913 * Yield the CPU here so that we don't get soft lockup
3914 * in non preempt case.
3920 /* found anything to free? */
3921 if (list_empty(&list)) {
3926 /* now free all selected PAs */
3927 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3929 /* remove from object (inode or locality group) */
3930 spin_lock(pa->pa_obj_lock);
3931 list_del_rcu(&pa->pa_inode_list);
3932 spin_unlock(pa->pa_obj_lock);
3935 ext4_mb_release_group_pa(&e4b, pa);
3937 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
3939 list_del(&pa->u.pa_tmp_list);
3940 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3944 ext4_unlock_group(sb, group);
3945 ext4_mb_release_desc(&e4b);
3951 * releases all non-used preallocated blocks for given inode
3953 * It's important to discard preallocations under i_data_sem
3954 * We don't want another block to be served from the prealloc
3955 * space when we are discarding the inode prealloc space.
3957 * FIXME!! Make sure it is valid at all the call sites
3959 void ext4_mb_discard_inode_preallocations(struct inode *inode)
3961 struct ext4_inode_info *ei = EXT4_I(inode);
3962 struct super_block *sb = inode->i_sb;
3963 struct buffer_head *bitmap_bh = NULL;
3964 struct ext4_prealloc_space *pa, *tmp;
3965 ext4_group_t group = 0;
3966 struct list_head list;
3967 struct ext4_buddy e4b;
3970 if (!test_opt(sb, MBALLOC) || !S_ISREG(inode->i_mode)) {
3971 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3975 mb_debug("discard preallocation for inode %lu\n", inode->i_ino);
3977 INIT_LIST_HEAD(&list);
3980 /* first, collect all pa's in the inode */
3981 spin_lock(&ei->i_prealloc_lock);
3982 while (!list_empty(&ei->i_prealloc_list)) {
3983 pa = list_entry(ei->i_prealloc_list.next,
3984 struct ext4_prealloc_space, pa_inode_list);
3985 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
3986 spin_lock(&pa->pa_lock);
3987 if (atomic_read(&pa->pa_count)) {
3988 /* this shouldn't happen often - nobody should
3989 * use preallocation while we're discarding it */
3990 spin_unlock(&pa->pa_lock);
3991 spin_unlock(&ei->i_prealloc_lock);
3992 printk(KERN_ERR "uh-oh! used pa while discarding\n");
3994 schedule_timeout_uninterruptible(HZ);
3998 if (pa->pa_deleted == 0) {
4000 spin_unlock(&pa->pa_lock);
4001 list_del_rcu(&pa->pa_inode_list);
4002 list_add(&pa->u.pa_tmp_list, &list);
4006 /* someone is deleting pa right now */
4007 spin_unlock(&pa->pa_lock);
4008 spin_unlock(&ei->i_prealloc_lock);
4010 /* we have to wait here because pa_deleted
4011 * doesn't mean pa is already unlinked from
4012 * the list. as we might be called from
4013 * ->clear_inode() the inode will get freed
4014 * and concurrent thread which is unlinking
4015 * pa from inode's list may access already
4016 * freed memory, bad-bad-bad */
4018 /* XXX: if this happens too often, we can
4019 * add a flag to force wait only in case
4020 * of ->clear_inode(), but not in case of
4021 * regular truncate */
4022 schedule_timeout_uninterruptible(HZ);
4025 spin_unlock(&ei->i_prealloc_lock);
4027 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
4028 BUG_ON(pa->pa_linear != 0);
4029 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4031 err = ext4_mb_load_buddy(sb, group, &e4b);
4032 BUG_ON(err != 0); /* error handling here */
4034 bitmap_bh = read_block_bitmap(sb, group);
4035 if (bitmap_bh == NULL) {
4036 /* error handling here */
4037 ext4_mb_release_desc(&e4b);
4038 BUG_ON(bitmap_bh == NULL);
4041 ext4_lock_group(sb, group);
4042 list_del(&pa->pa_group_list);
4043 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
4044 ext4_unlock_group(sb, group);
4046 ext4_mb_release_desc(&e4b);
4049 list_del(&pa->u.pa_tmp_list);
4050 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4055 * finds all preallocated spaces and return blocks being freed to them
4056 * if preallocated space becomes full (no block is used from the space)
4057 * then the function frees space in buddy
4058 * XXX: at the moment, truncate (which is the only way to free blocks)
4059 * discards all preallocations
4061 static void ext4_mb_return_to_preallocation(struct inode *inode,
4062 struct ext4_buddy *e4b,
4063 sector_t block, int count)
4065 BUG_ON(!list_empty(&EXT4_I(inode)->i_prealloc_list));
4068 static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4070 struct super_block *sb = ac->ac_sb;
4073 printk(KERN_ERR "EXT4-fs: Can't allocate:"
4074 " Allocation context details:\n");
4075 printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
4076 ac->ac_status, ac->ac_flags);
4077 printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
4078 "best %lu/%lu/%lu@%lu cr %d\n",
4079 (unsigned long)ac->ac_o_ex.fe_group,
4080 (unsigned long)ac->ac_o_ex.fe_start,
4081 (unsigned long)ac->ac_o_ex.fe_len,
4082 (unsigned long)ac->ac_o_ex.fe_logical,
4083 (unsigned long)ac->ac_g_ex.fe_group,
4084 (unsigned long)ac->ac_g_ex.fe_start,
4085 (unsigned long)ac->ac_g_ex.fe_len,
4086 (unsigned long)ac->ac_g_ex.fe_logical,
4087 (unsigned long)ac->ac_b_ex.fe_group,
4088 (unsigned long)ac->ac_b_ex.fe_start,
4089 (unsigned long)ac->ac_b_ex.fe_len,
4090 (unsigned long)ac->ac_b_ex.fe_logical,
4091 (int)ac->ac_criteria);
4092 printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
4094 printk(KERN_ERR "EXT4-fs: groups: \n");
4095 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
4096 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
4097 struct ext4_prealloc_space *pa;
4098 ext4_grpblk_t start;
4099 struct list_head *cur;
4100 ext4_lock_group(sb, i);
4101 list_for_each(cur, &grp->bb_prealloc_list) {
4102 pa = list_entry(cur, struct ext4_prealloc_space,
4104 spin_lock(&pa->pa_lock);
4105 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
4107 spin_unlock(&pa->pa_lock);
4108 printk(KERN_ERR "PA:%lu:%d:%u \n", i,
4111 ext4_lock_group(sb, i);
4113 if (grp->bb_free == 0)
4115 printk(KERN_ERR "%lu: %d/%d \n",
4116 i, grp->bb_free, grp->bb_fragments);
4118 printk(KERN_ERR "\n");
4121 static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4128 * We use locality group preallocation for small size file. The size of the
4129 * file is determined by the current size or the resulting size after
4130 * allocation which ever is larger
4132 * One can tune this size via /proc/fs/ext4/<partition>/stream_req
4134 static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
4136 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
4137 int bsbits = ac->ac_sb->s_blocksize_bits;
4140 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
4143 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
4144 isize = i_size_read(ac->ac_inode) >> bsbits;
4145 size = max(size, isize);
4147 /* don't use group allocation for large files */
4148 if (size >= sbi->s_mb_stream_request)
4151 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
4154 BUG_ON(ac->ac_lg != NULL);
4156 * locality group prealloc space are per cpu. The reason for having
4157 * per cpu locality group is to reduce the contention between block
4158 * request from multiple CPUs.
4160 ac->ac_lg = &sbi->s_locality_groups[get_cpu()];
4163 /* we're going to use group allocation */
4164 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
4166 /* serialize all allocations in the group */
4167 mutex_lock(&ac->ac_lg->lg_mutex);
4170 static int ext4_mb_initialize_context(struct ext4_allocation_context *ac,
4171 struct ext4_allocation_request *ar)
4173 struct super_block *sb = ar->inode->i_sb;
4174 struct ext4_sb_info *sbi = EXT4_SB(sb);
4175 struct ext4_super_block *es = sbi->s_es;
4179 ext4_grpblk_t block;
4181 /* we can't allocate > group size */
4184 /* just a dirty hack to filter too big requests */
4185 if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
4186 len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
4188 /* start searching from the goal */
4190 if (goal < le32_to_cpu(es->s_first_data_block) ||
4191 goal >= ext4_blocks_count(es))
4192 goal = le32_to_cpu(es->s_first_data_block);
4193 ext4_get_group_no_and_offset(sb, goal, &group, &block);
4195 /* set up allocation goals */
4196 ac->ac_b_ex.fe_logical = ar->logical;
4197 ac->ac_b_ex.fe_group = 0;
4198 ac->ac_b_ex.fe_start = 0;
4199 ac->ac_b_ex.fe_len = 0;
4200 ac->ac_status = AC_STATUS_CONTINUE;
4201 ac->ac_groups_scanned = 0;
4202 ac->ac_ex_scanned = 0;
4205 ac->ac_inode = ar->inode;
4206 ac->ac_o_ex.fe_logical = ar->logical;
4207 ac->ac_o_ex.fe_group = group;
4208 ac->ac_o_ex.fe_start = block;
4209 ac->ac_o_ex.fe_len = len;
4210 ac->ac_g_ex.fe_logical = ar->logical;
4211 ac->ac_g_ex.fe_group = group;
4212 ac->ac_g_ex.fe_start = block;
4213 ac->ac_g_ex.fe_len = len;
4214 ac->ac_f_ex.fe_len = 0;
4215 ac->ac_flags = ar->flags;
4217 ac->ac_criteria = 0;
4219 ac->ac_bitmap_page = NULL;
4220 ac->ac_buddy_page = NULL;
4223 /* we have to define context: we'll we work with a file or
4224 * locality group. this is a policy, actually */
4225 ext4_mb_group_or_file(ac);
4227 mb_debug("init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
4228 "left: %u/%u, right %u/%u to %swritable\n",
4229 (unsigned) ar->len, (unsigned) ar->logical,
4230 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4231 (unsigned) ar->lleft, (unsigned) ar->pleft,
4232 (unsigned) ar->lright, (unsigned) ar->pright,
4233 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4239 * release all resource we used in allocation
4241 static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4244 if (ac->ac_pa->pa_linear) {
4245 /* see comment in ext4_mb_use_group_pa() */
4246 spin_lock(&ac->ac_pa->pa_lock);
4247 ac->ac_pa->pa_pstart += ac->ac_b_ex.fe_len;
4248 ac->ac_pa->pa_lstart += ac->ac_b_ex.fe_len;
4249 ac->ac_pa->pa_free -= ac->ac_b_ex.fe_len;
4250 ac->ac_pa->pa_len -= ac->ac_b_ex.fe_len;
4251 spin_unlock(&ac->ac_pa->pa_lock);
4253 ext4_mb_put_pa(ac, ac->ac_sb, ac->ac_pa);
4255 if (ac->ac_bitmap_page)
4256 page_cache_release(ac->ac_bitmap_page);
4257 if (ac->ac_buddy_page)
4258 page_cache_release(ac->ac_buddy_page);
4259 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4260 mutex_unlock(&ac->ac_lg->lg_mutex);
4261 ext4_mb_collect_stats(ac);
4265 static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4271 for (i = 0; i < EXT4_SB(sb)->s_groups_count && needed > 0; i++) {
4272 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4281 * Main entry point into mballoc to allocate blocks
4282 * it tries to use preallocation first, then falls back
4283 * to usual allocation
4285 ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
4286 struct ext4_allocation_request *ar, int *errp)
4288 struct ext4_allocation_context *ac = NULL;
4289 struct ext4_sb_info *sbi;
4290 struct super_block *sb;
4291 ext4_fsblk_t block = 0;
4295 sb = ar->inode->i_sb;
4298 if (!test_opt(sb, MBALLOC)) {
4299 block = ext4_new_blocks_old(handle, ar->inode, ar->goal,
4304 while (ar->len && DQUOT_ALLOC_BLOCK(ar->inode, ar->len)) {
4305 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4314 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4320 ext4_mb_poll_new_transaction(sb, handle);
4322 *errp = ext4_mb_initialize_context(ac, ar);
4328 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4329 if (!ext4_mb_use_preallocated(ac)) {
4331 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4332 ext4_mb_normalize_request(ac, ar);
4335 /* allocate space in core */
4336 ext4_mb_regular_allocator(ac);
4338 /* as we've just preallocated more space than
4339 * user requested orinally, we store allocated
4340 * space in a special descriptor */
4341 if (ac->ac_status == AC_STATUS_FOUND &&
4342 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4343 ext4_mb_new_preallocation(ac);
4346 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
4347 ext4_mb_mark_diskspace_used(ac, handle);
4349 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4350 ar->len = ac->ac_b_ex.fe_len;
4352 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
4356 ac->ac_b_ex.fe_len = 0;
4358 ext4_mb_show_ac(ac);
4361 ext4_mb_release_context(ac);
4364 if (ar->len < inquota)
4365 DQUOT_FREE_BLOCK(ar->inode, inquota - ar->len);
4367 kmem_cache_free(ext4_ac_cachep, ac);
4370 static void ext4_mb_poll_new_transaction(struct super_block *sb,
4373 struct ext4_sb_info *sbi = EXT4_SB(sb);
4375 if (sbi->s_last_transaction == handle->h_transaction->t_tid)
4378 /* new transaction! time to close last one and free blocks for
4379 * committed transaction. we know that only transaction can be
4380 * active, so previos transaction can be being logged and we
4381 * know that transaction before previous is known to be already
4382 * logged. this means that now we may free blocks freed in all
4383 * transactions before previous one. hope I'm clear enough ... */
4385 spin_lock(&sbi->s_md_lock);
4386 if (sbi->s_last_transaction != handle->h_transaction->t_tid) {
4387 mb_debug("new transaction %lu, old %lu\n",
4388 (unsigned long) handle->h_transaction->t_tid,
4389 (unsigned long) sbi->s_last_transaction);
4390 list_splice_init(&sbi->s_closed_transaction,
4391 &sbi->s_committed_transaction);
4392 list_splice_init(&sbi->s_active_transaction,
4393 &sbi->s_closed_transaction);
4394 sbi->s_last_transaction = handle->h_transaction->t_tid;
4396 spin_unlock(&sbi->s_md_lock);
4398 ext4_mb_free_committed_blocks(sb);
4401 static int ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
4402 ext4_group_t group, ext4_grpblk_t block, int count)
4404 struct ext4_group_info *db = e4b->bd_info;
4405 struct super_block *sb = e4b->bd_sb;
4406 struct ext4_sb_info *sbi = EXT4_SB(sb);
4407 struct ext4_free_metadata *md;
4410 BUG_ON(e4b->bd_bitmap_page == NULL);
4411 BUG_ON(e4b->bd_buddy_page == NULL);
4413 ext4_lock_group(sb, group);
4414 for (i = 0; i < count; i++) {
4416 if (md && db->bb_tid != handle->h_transaction->t_tid) {
4417 db->bb_md_cur = NULL;
4422 ext4_unlock_group(sb, group);
4423 md = kmalloc(sizeof(*md), GFP_NOFS);
4429 ext4_lock_group(sb, group);
4430 if (db->bb_md_cur == NULL) {
4431 spin_lock(&sbi->s_md_lock);
4432 list_add(&md->list, &sbi->s_active_transaction);
4433 spin_unlock(&sbi->s_md_lock);
4434 /* protect buddy cache from being freed,
4435 * otherwise we'll refresh it from
4436 * on-disk bitmap and lose not-yet-available
4438 page_cache_get(e4b->bd_buddy_page);
4439 page_cache_get(e4b->bd_bitmap_page);
4441 db->bb_tid = handle->h_transaction->t_tid;
4442 mb_debug("new md 0x%p for group %lu\n",
4450 BUG_ON(md->num >= EXT4_BB_MAX_BLOCKS);
4451 md->blocks[md->num] = block + i;
4453 if (md->num == EXT4_BB_MAX_BLOCKS) {
4454 /* no more space, put full container on a sb's list */
4455 db->bb_md_cur = NULL;
4458 ext4_unlock_group(sb, group);
4463 * Main entry point into mballoc to free blocks
4465 void ext4_mb_free_blocks(handle_t *handle, struct inode *inode,
4466 unsigned long block, unsigned long count,
4467 int metadata, unsigned long *freed)
4469 struct buffer_head *bitmap_bh = NULL;
4470 struct super_block *sb = inode->i_sb;
4471 struct ext4_allocation_context *ac = NULL;
4472 struct ext4_group_desc *gdp;
4473 struct ext4_super_block *es;
4474 unsigned long overflow;
4476 struct buffer_head *gd_bh;
4477 ext4_group_t block_group;
4478 struct ext4_sb_info *sbi;
4479 struct ext4_buddy e4b;
4485 ext4_mb_poll_new_transaction(sb, handle);
4488 es = EXT4_SB(sb)->s_es;
4489 if (block < le32_to_cpu(es->s_first_data_block) ||
4490 block + count < block ||
4491 block + count > ext4_blocks_count(es)) {
4492 ext4_error(sb, __FUNCTION__,
4493 "Freeing blocks not in datazone - "
4494 "block = %lu, count = %lu", block, count);
4498 ext4_debug("freeing block %lu\n", block);
4500 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4502 ac->ac_op = EXT4_MB_HISTORY_FREE;
4503 ac->ac_inode = inode;
4509 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4512 * Check to see if we are freeing blocks across a group
4515 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4516 overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
4519 bitmap_bh = read_block_bitmap(sb, block_group);
4522 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
4526 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4527 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4528 in_range(block, ext4_inode_table(sb, gdp),
4529 EXT4_SB(sb)->s_itb_per_group) ||
4530 in_range(block + count - 1, ext4_inode_table(sb, gdp),
4531 EXT4_SB(sb)->s_itb_per_group)) {
4533 ext4_error(sb, __FUNCTION__,
4534 "Freeing blocks in system zone - "
4535 "Block = %lu, count = %lu", block, count);
4538 BUFFER_TRACE(bitmap_bh, "getting write access");
4539 err = ext4_journal_get_write_access(handle, bitmap_bh);
4544 * We are about to modify some metadata. Call the journal APIs
4545 * to unshare ->b_data if a currently-committing transaction is
4548 BUFFER_TRACE(gd_bh, "get_write_access");
4549 err = ext4_journal_get_write_access(handle, gd_bh);
4553 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4557 #ifdef AGGRESSIVE_CHECK
4560 for (i = 0; i < count; i++)
4561 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4564 mb_clear_bits(sb_bgl_lock(sbi, block_group), bitmap_bh->b_data,
4567 /* We dirtied the bitmap block */
4568 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4569 err = ext4_journal_dirty_metadata(handle, bitmap_bh);
4572 ac->ac_b_ex.fe_group = block_group;
4573 ac->ac_b_ex.fe_start = bit;
4574 ac->ac_b_ex.fe_len = count;
4575 ext4_mb_store_history(ac);
4579 /* blocks being freed are metadata. these blocks shouldn't
4580 * be used until this transaction is committed */
4581 ext4_mb_free_metadata(handle, &e4b, block_group, bit, count);
4583 ext4_lock_group(sb, block_group);
4584 err = mb_free_blocks(inode, &e4b, bit, count);
4585 ext4_mb_return_to_preallocation(inode, &e4b, block, count);
4586 ext4_unlock_group(sb, block_group);
4590 spin_lock(sb_bgl_lock(sbi, block_group));
4591 gdp->bg_free_blocks_count =
4592 cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) + count);
4593 gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
4594 spin_unlock(sb_bgl_lock(sbi, block_group));
4595 percpu_counter_add(&sbi->s_freeblocks_counter, count);
4597 ext4_mb_release_desc(&e4b);
4601 /* And the group descriptor block */
4602 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
4603 ret = ext4_journal_dirty_metadata(handle, gd_bh);
4607 if (overflow && !err) {
4616 ext4_std_error(sb, err);
4618 kmem_cache_free(ext4_ac_cachep, ac);