4 * Written by: Alex Tomas <alex@clusterfs.com>
7 #ifndef _EXT4_MBALLOC_H
8 #define _EXT4_MBALLOC_H
10 #include <linux/time.h>
12 #include <linux/namei.h>
13 #include <linux/quotaops.h>
14 #include <linux/buffer_head.h>
15 #include <linux/module.h>
16 #include <linux/swap.h>
17 #include <linux/proc_fs.h>
18 #include <linux/pagemap.h>
19 #include <linux/seq_file.h>
20 #include <linux/version.h>
21 #include "ext4_jbd2.h"
26 * with AGGRESSIVE_CHECK allocator runs consistency checks over
27 * structures. these checks slow things down a lot
29 #define AGGRESSIVE_CHECK__
32 * with DOUBLE_CHECK defined mballoc creates persistent in-core
33 * bitmaps, maintains and uses them to check for double allocations
35 #define DOUBLE_CHECK__
41 #define mb_debug(fmt, a...) printk(fmt, ##a)
43 #define mb_debug(fmt, a...)
47 * with EXT4_MB_HISTORY mballoc stores last N allocations in memory
48 * and you can monitor it in /proc/fs/ext4/<dev>/mb_history
50 #define EXT4_MB_HISTORY
51 #define EXT4_MB_HISTORY_ALLOC 1 /* allocation */
52 #define EXT4_MB_HISTORY_PREALLOC 2 /* preallocated blocks used */
53 #define EXT4_MB_HISTORY_DISCARD 4 /* preallocation discarded */
54 #define EXT4_MB_HISTORY_FREE 8 /* free */
56 #define EXT4_MB_HISTORY_DEFAULT (EXT4_MB_HISTORY_ALLOC | \
57 EXT4_MB_HISTORY_PREALLOC)
60 * How long mballoc can look for a best extent (in found extents)
62 #define MB_DEFAULT_MAX_TO_SCAN 200
65 * How long mballoc must look for a best extent
67 #define MB_DEFAULT_MIN_TO_SCAN 10
70 * How many groups mballoc will scan looking for the best chunk
72 #define MB_DEFAULT_MAX_GROUPS_TO_SCAN 5
75 * with 'ext4_mb_stats' allocator will collect stats that will be
76 * shown at umount. The collecting costs though!
78 #define MB_DEFAULT_STATS 1
81 * files smaller than MB_DEFAULT_STREAM_THRESHOLD are served
82 * by the stream allocator, which purpose is to pack requests
83 * as close each to other as possible to produce smooth I/O traffic
84 * We use locality group prealloc space for stream request.
85 * We can tune the same via /proc/fs/ext4/<parition>/stream_req
87 #define MB_DEFAULT_STREAM_THRESHOLD 16 /* 64K */
90 * for which requests use 2^N search using buddies
92 #define MB_DEFAULT_ORDER2_REQS 2
95 * default group prealloc size 512 blocks
97 #define MB_DEFAULT_GROUP_PREALLOC 512
99 static struct kmem_cache *ext4_pspace_cachep;
100 static struct kmem_cache *ext4_ac_cachep;
102 #ifdef EXT4_BB_MAX_BLOCKS
103 #undef EXT4_BB_MAX_BLOCKS
105 #define EXT4_BB_MAX_BLOCKS 30
107 struct ext4_free_metadata {
110 ext4_grpblk_t blocks[EXT4_BB_MAX_BLOCKS];
111 struct list_head list;
114 struct ext4_group_info {
115 unsigned long bb_state;
116 unsigned long bb_tid;
117 struct ext4_free_metadata *bb_md_cur;
118 unsigned short bb_first_free;
119 unsigned short bb_free;
120 unsigned short bb_fragments;
121 struct list_head bb_prealloc_list;
125 unsigned short bb_counters[];
128 #define EXT4_GROUP_INFO_NEED_INIT_BIT 0
129 #define EXT4_GROUP_INFO_LOCKED_BIT 1
131 #define EXT4_MB_GRP_NEED_INIT(grp) \
132 (test_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &((grp)->bb_state)))
135 struct ext4_prealloc_space {
136 struct list_head pa_inode_list;
137 struct list_head pa_group_list;
139 struct list_head pa_tmp_list;
140 struct rcu_head pa_rcu;
145 ext4_fsblk_t pa_pstart; /* phys. block */
146 ext4_lblk_t pa_lstart; /* log. block */
147 unsigned short pa_len; /* len of preallocated chunk */
148 unsigned short pa_free; /* how many blocks are free */
149 unsigned short pa_linear; /* consumed in one direction
150 * strictly, for grp prealloc */
151 spinlock_t *pa_obj_lock;
152 struct inode *pa_inode; /* hack, for history only */
156 struct ext4_free_extent {
157 ext4_lblk_t fe_logical;
158 ext4_grpblk_t fe_start;
159 ext4_group_t fe_group;
165 * we try to group all related changes together
166 * so that writeback can flush/allocate them together as well
168 struct ext4_locality_group {
170 struct mutex lg_mutex; /* to serialize allocates */
171 struct list_head lg_prealloc_list;/* list of preallocations */
172 spinlock_t lg_prealloc_lock;
175 struct ext4_allocation_context {
176 struct inode *ac_inode;
177 struct super_block *ac_sb;
179 /* original request */
180 struct ext4_free_extent ac_o_ex;
182 /* goal request (after normalization) */
183 struct ext4_free_extent ac_g_ex;
185 /* the best found extent */
186 struct ext4_free_extent ac_b_ex;
188 /* copy of the bext found extent taken before preallocation efforts */
189 struct ext4_free_extent ac_f_ex;
191 /* number of iterations done. we have to track to limit searching */
192 unsigned long ac_ex_scanned;
193 __u16 ac_groups_scanned;
197 __u16 ac_flags; /* allocation hints */
201 __u8 ac_2order; /* if request is to allocate 2^N blocks and
202 * N > 0, the field stores N, otherwise 0 */
203 __u8 ac_op; /* operation, for history only */
204 struct page *ac_bitmap_page;
205 struct page *ac_buddy_page;
206 struct ext4_prealloc_space *ac_pa;
207 struct ext4_locality_group *ac_lg;
210 #define AC_STATUS_CONTINUE 1
211 #define AC_STATUS_FOUND 2
212 #define AC_STATUS_BREAK 3
214 struct ext4_mb_history {
215 struct ext4_free_extent orig; /* orig allocation */
216 struct ext4_free_extent goal; /* goal allocation */
217 struct ext4_free_extent result; /* result allocation */
220 __u16 found; /* how many extents have been found */
221 __u16 groups; /* how many groups have been scanned */
222 __u16 tail; /* what tail broke some buddy */
223 __u16 buddy; /* buddy the tail ^^^ broke */
225 __u8 cr:3; /* which phase the result extent was found at */
231 struct page *bd_buddy_page;
233 struct page *bd_bitmap_page;
235 struct ext4_group_info *bd_info;
236 struct super_block *bd_sb;
238 ext4_group_t bd_group;
240 #define EXT4_MB_BITMAP(e4b) ((e4b)->bd_bitmap)
241 #define EXT4_MB_BUDDY(e4b) ((e4b)->bd_buddy)
243 #ifndef EXT4_MB_HISTORY
244 static inline void ext4_mb_store_history(struct ext4_allocation_context *ac)
249 static void ext4_mb_store_history(struct ext4_allocation_context *ac);
252 #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
254 static struct proc_dir_entry *proc_root_ext4;
255 struct buffer_head *read_block_bitmap(struct super_block *, ext4_group_t);
257 static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
259 static void ext4_mb_poll_new_transaction(struct super_block *, handle_t *);
260 static void ext4_mb_free_committed_blocks(struct super_block *);
261 static void ext4_mb_return_to_preallocation(struct inode *inode,
262 struct ext4_buddy *e4b, sector_t block,
264 static void ext4_mb_put_pa(struct ext4_allocation_context *,
265 struct super_block *, struct ext4_prealloc_space *pa);
266 static int ext4_mb_init_per_dev_proc(struct super_block *sb);
267 static int ext4_mb_destroy_per_dev_proc(struct super_block *sb);
270 static inline void ext4_lock_group(struct super_block *sb, ext4_group_t group)
272 struct ext4_group_info *grinfo = ext4_get_group_info(sb, group);
274 bit_spin_lock(EXT4_GROUP_INFO_LOCKED_BIT, &(grinfo->bb_state));
277 static inline void ext4_unlock_group(struct super_block *sb,
280 struct ext4_group_info *grinfo = ext4_get_group_info(sb, group);
282 bit_spin_unlock(EXT4_GROUP_INFO_LOCKED_BIT, &(grinfo->bb_state));
285 static inline int ext4_is_group_locked(struct super_block *sb,
288 struct ext4_group_info *grinfo = ext4_get_group_info(sb, group);
290 return bit_spin_is_locked(EXT4_GROUP_INFO_LOCKED_BIT,
291 &(grinfo->bb_state));
294 static ext4_fsblk_t ext4_grp_offs_to_block(struct super_block *sb,
295 struct ext4_free_extent *fex)
299 block = (ext4_fsblk_t) fex->fe_group * EXT4_BLOCKS_PER_GROUP(sb)
301 + le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);