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 <linux/blkdev.h>
22 #include <linux/marker.h>
23 #include <linux/mutex.h>
24 #include "ext4_jbd2.h"
29 * with AGGRESSIVE_CHECK allocator runs consistency checks over
30 * structures. these checks slow things down a lot
32 #define AGGRESSIVE_CHECK__
35 * with DOUBLE_CHECK defined mballoc creates persistent in-core
36 * bitmaps, maintains and uses them to check for double allocations
38 #define DOUBLE_CHECK__
44 #define mb_debug(fmt, a...) printk(fmt, ##a)
46 #define mb_debug(fmt, a...)
50 * with EXT4_MB_HISTORY mballoc stores last N allocations in memory
51 * and you can monitor it in /proc/fs/ext4/<dev>/mb_history
53 #define EXT4_MB_HISTORY
54 #define EXT4_MB_HISTORY_ALLOC 1 /* allocation */
55 #define EXT4_MB_HISTORY_PREALLOC 2 /* preallocated blocks used */
56 #define EXT4_MB_HISTORY_DISCARD 4 /* preallocation discarded */
57 #define EXT4_MB_HISTORY_FREE 8 /* free */
59 #define EXT4_MB_HISTORY_DEFAULT (EXT4_MB_HISTORY_ALLOC | \
60 EXT4_MB_HISTORY_PREALLOC)
63 * How long mballoc can look for a best extent (in found extents)
65 #define MB_DEFAULT_MAX_TO_SCAN 200
68 * How long mballoc must look for a best extent
70 #define MB_DEFAULT_MIN_TO_SCAN 10
73 * How many groups mballoc will scan looking for the best chunk
75 #define MB_DEFAULT_MAX_GROUPS_TO_SCAN 5
78 * with 'ext4_mb_stats' allocator will collect stats that will be
79 * shown at umount. The collecting costs though!
81 #define MB_DEFAULT_STATS 1
84 * files smaller than MB_DEFAULT_STREAM_THRESHOLD are served
85 * by the stream allocator, which purpose is to pack requests
86 * as close each to other as possible to produce smooth I/O traffic
87 * We use locality group prealloc space for stream request.
88 * We can tune the same via /proc/fs/ext4/<parition>/stream_req
90 #define MB_DEFAULT_STREAM_THRESHOLD 16 /* 64K */
93 * for which requests use 2^N search using buddies
95 #define MB_DEFAULT_ORDER2_REQS 2
98 * default group prealloc size 512 blocks
100 #define MB_DEFAULT_GROUP_PREALLOC 512
103 struct ext4_free_data
{
104 /* this links the free block information from group_info */
107 /* this links the free block information from ext4_sb_info */
108 struct list_head list
;
110 /* group which free block extent belongs */
113 /* free block extent */
114 ext4_grpblk_t start_blk
;
117 /* transaction which freed this extent */
121 struct ext4_group_info
{
122 unsigned long bb_state
;
123 struct rb_root bb_free_root
;
124 unsigned short bb_first_free
;
125 unsigned short bb_free
;
126 unsigned short bb_fragments
;
127 struct list_head bb_prealloc_list
;
131 struct rw_semaphore alloc_sem
;
132 unsigned short bb_counters
[];
135 #define EXT4_GROUP_INFO_NEED_INIT_BIT 0
136 #define EXT4_GROUP_INFO_LOCKED_BIT 1
138 #define EXT4_MB_GRP_NEED_INIT(grp) \
139 (test_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &((grp)->bb_state)))
142 struct ext4_prealloc_space
{
143 struct list_head pa_inode_list
;
144 struct list_head pa_group_list
;
146 struct list_head pa_tmp_list
;
147 struct rcu_head pa_rcu
;
152 ext4_fsblk_t pa_pstart
; /* phys. block */
153 ext4_lblk_t pa_lstart
; /* log. block */
154 unsigned short pa_len
; /* len of preallocated chunk */
155 unsigned short pa_free
; /* how many blocks are free */
156 unsigned short pa_linear
; /* consumed in one direction
157 * strictly, for grp prealloc */
158 spinlock_t
*pa_obj_lock
;
159 struct inode
*pa_inode
; /* hack, for history only */
163 struct ext4_free_extent
{
164 ext4_lblk_t fe_logical
;
165 ext4_grpblk_t fe_start
;
166 ext4_group_t fe_group
;
172 * we try to group all related changes together
173 * so that writeback can flush/allocate them together as well
174 * Size of lg_prealloc_list hash is determined by MB_DEFAULT_GROUP_PREALLOC
175 * (512). We store prealloc space into the hash based on the pa_free blocks
176 * order value.ie, fls(pa_free)-1;
178 #define PREALLOC_TB_SIZE 10
179 struct ext4_locality_group
{
181 /* to serialize allocates */
182 struct mutex lg_mutex
;
183 /* list of preallocations */
184 struct list_head lg_prealloc_list
[PREALLOC_TB_SIZE
];
185 spinlock_t lg_prealloc_lock
;
188 struct ext4_allocation_context
{
189 struct inode
*ac_inode
;
190 struct super_block
*ac_sb
;
192 /* original request */
193 struct ext4_free_extent ac_o_ex
;
195 /* goal request (after normalization) */
196 struct ext4_free_extent ac_g_ex
;
198 /* the best found extent */
199 struct ext4_free_extent ac_b_ex
;
201 /* copy of the bext found extent taken before preallocation efforts */
202 struct ext4_free_extent ac_f_ex
;
204 /* number of iterations done. we have to track to limit searching */
205 unsigned long ac_ex_scanned
;
206 __u16 ac_groups_scanned
;
210 __u16 ac_flags
; /* allocation hints */
214 __u8 ac_2order
; /* if request is to allocate 2^N blocks and
215 * N > 0, the field stores N, otherwise 0 */
216 __u8 ac_op
; /* operation, for history only */
217 struct page
*ac_bitmap_page
;
218 struct page
*ac_buddy_page
;
220 * pointer to the held semaphore upon successful
223 struct rw_semaphore
*alloc_semp
;
224 struct ext4_prealloc_space
*ac_pa
;
225 struct ext4_locality_group
*ac_lg
;
228 #define AC_STATUS_CONTINUE 1
229 #define AC_STATUS_FOUND 2
230 #define AC_STATUS_BREAK 3
232 struct ext4_mb_history
{
233 struct ext4_free_extent orig
; /* orig allocation */
234 struct ext4_free_extent goal
; /* goal allocation */
235 struct ext4_free_extent result
; /* result allocation */
238 __u16 found
; /* how many extents have been found */
239 __u16 groups
; /* how many groups have been scanned */
240 __u16 tail
; /* what tail broke some buddy */
241 __u16 buddy
; /* buddy the tail ^^^ broke */
243 __u8 cr
:3; /* which phase the result extent was found at */
249 struct page
*bd_buddy_page
;
251 struct page
*bd_bitmap_page
;
253 struct ext4_group_info
*bd_info
;
254 struct super_block
*bd_sb
;
256 ext4_group_t bd_group
;
257 struct rw_semaphore
*alloc_semp
;
259 #define EXT4_MB_BITMAP(e4b) ((e4b)->bd_bitmap)
260 #define EXT4_MB_BUDDY(e4b) ((e4b)->bd_buddy)
262 #ifndef EXT4_MB_HISTORY
263 static inline void ext4_mb_store_history(struct ext4_allocation_context
*ac
)
269 #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
271 struct buffer_head
*read_block_bitmap(struct super_block
*, ext4_group_t
);
274 static inline void ext4_lock_group(struct super_block
*sb
, ext4_group_t group
)
276 struct ext4_group_info
*grinfo
= ext4_get_group_info(sb
, group
);
278 bit_spin_lock(EXT4_GROUP_INFO_LOCKED_BIT
, &(grinfo
->bb_state
));
281 static inline void ext4_unlock_group(struct super_block
*sb
,
284 struct ext4_group_info
*grinfo
= ext4_get_group_info(sb
, group
);
286 bit_spin_unlock(EXT4_GROUP_INFO_LOCKED_BIT
, &(grinfo
->bb_state
));
289 static inline int ext4_is_group_locked(struct super_block
*sb
,
292 struct ext4_group_info
*grinfo
= ext4_get_group_info(sb
, group
);
294 return bit_spin_is_locked(EXT4_GROUP_INFO_LOCKED_BIT
,
295 &(grinfo
->bb_state
));
298 static inline ext4_fsblk_t
ext4_grp_offs_to_block(struct super_block
*sb
,
299 struct ext4_free_extent
*fex
)
303 block
= (ext4_fsblk_t
) fex
->fe_group
* EXT4_BLOCKS_PER_GROUP(sb
)
305 + le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);