Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 1 | /* |
| 2 | * fs/ext4/mballoc.h |
| 3 | * |
| 4 | * Written by: Alex Tomas <alex@clusterfs.com> |
| 5 | * |
| 6 | */ |
| 7 | #ifndef _EXT4_MBALLOC_H |
| 8 | #define _EXT4_MBALLOC_H |
| 9 | |
| 10 | #include <linux/time.h> |
| 11 | #include <linux/fs.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> |
Theodore Ts'o | 8a0aba7 | 2008-10-16 10:06:27 -0400 | [diff] [blame] | 21 | #include <linux/blkdev.h> |
| 22 | #include <linux/marker.h> |
Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 23 | #include "ext4_jbd2.h" |
| 24 | #include "ext4.h" |
| 25 | #include "group.h" |
| 26 | |
| 27 | /* |
| 28 | * with AGGRESSIVE_CHECK allocator runs consistency checks over |
| 29 | * structures. these checks slow things down a lot |
| 30 | */ |
| 31 | #define AGGRESSIVE_CHECK__ |
| 32 | |
| 33 | /* |
| 34 | * with DOUBLE_CHECK defined mballoc creates persistent in-core |
| 35 | * bitmaps, maintains and uses them to check for double allocations |
| 36 | */ |
| 37 | #define DOUBLE_CHECK__ |
| 38 | |
| 39 | /* |
| 40 | */ |
| 41 | #define MB_DEBUG__ |
| 42 | #ifdef MB_DEBUG |
| 43 | #define mb_debug(fmt, a...) printk(fmt, ##a) |
| 44 | #else |
| 45 | #define mb_debug(fmt, a...) |
| 46 | #endif |
| 47 | |
| 48 | /* |
| 49 | * with EXT4_MB_HISTORY mballoc stores last N allocations in memory |
| 50 | * and you can monitor it in /proc/fs/ext4/<dev>/mb_history |
| 51 | */ |
| 52 | #define EXT4_MB_HISTORY |
| 53 | #define EXT4_MB_HISTORY_ALLOC 1 /* allocation */ |
| 54 | #define EXT4_MB_HISTORY_PREALLOC 2 /* preallocated blocks used */ |
| 55 | #define EXT4_MB_HISTORY_DISCARD 4 /* preallocation discarded */ |
| 56 | #define EXT4_MB_HISTORY_FREE 8 /* free */ |
| 57 | |
| 58 | #define EXT4_MB_HISTORY_DEFAULT (EXT4_MB_HISTORY_ALLOC | \ |
| 59 | EXT4_MB_HISTORY_PREALLOC) |
| 60 | |
| 61 | /* |
| 62 | * How long mballoc can look for a best extent (in found extents) |
| 63 | */ |
| 64 | #define MB_DEFAULT_MAX_TO_SCAN 200 |
| 65 | |
| 66 | /* |
| 67 | * How long mballoc must look for a best extent |
| 68 | */ |
| 69 | #define MB_DEFAULT_MIN_TO_SCAN 10 |
| 70 | |
| 71 | /* |
| 72 | * How many groups mballoc will scan looking for the best chunk |
| 73 | */ |
| 74 | #define MB_DEFAULT_MAX_GROUPS_TO_SCAN 5 |
| 75 | |
| 76 | /* |
| 77 | * with 'ext4_mb_stats' allocator will collect stats that will be |
| 78 | * shown at umount. The collecting costs though! |
| 79 | */ |
| 80 | #define MB_DEFAULT_STATS 1 |
| 81 | |
| 82 | /* |
| 83 | * files smaller than MB_DEFAULT_STREAM_THRESHOLD are served |
| 84 | * by the stream allocator, which purpose is to pack requests |
| 85 | * as close each to other as possible to produce smooth I/O traffic |
| 86 | * We use locality group prealloc space for stream request. |
| 87 | * We can tune the same via /proc/fs/ext4/<parition>/stream_req |
| 88 | */ |
| 89 | #define MB_DEFAULT_STREAM_THRESHOLD 16 /* 64K */ |
| 90 | |
| 91 | /* |
| 92 | * for which requests use 2^N search using buddies |
| 93 | */ |
| 94 | #define MB_DEFAULT_ORDER2_REQS 2 |
| 95 | |
| 96 | /* |
| 97 | * default group prealloc size 512 blocks |
| 98 | */ |
| 99 | #define MB_DEFAULT_GROUP_PREALLOC 512 |
| 100 | |
| 101 | static struct kmem_cache *ext4_pspace_cachep; |
| 102 | static struct kmem_cache *ext4_ac_cachep; |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 103 | static struct kmem_cache *ext4_free_ext_cachep; |
Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 104 | |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 105 | struct ext4_free_data { |
| 106 | /* this links the free block information from group_info */ |
| 107 | struct rb_node node; |
Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 108 | |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 109 | /* this links the free block information from ext4_sb_info */ |
Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 110 | struct list_head list; |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 111 | |
| 112 | /* group which free block extent belongs */ |
| 113 | ext4_group_t group; |
| 114 | |
| 115 | /* free block extent */ |
| 116 | ext4_grpblk_t start_blk; |
| 117 | ext4_grpblk_t count; |
| 118 | |
| 119 | /* transaction which freed this extent */ |
| 120 | tid_t t_tid; |
Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | struct ext4_group_info { |
| 124 | unsigned long bb_state; |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 125 | struct rb_root bb_free_root; |
Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 126 | unsigned short bb_first_free; |
| 127 | unsigned short bb_free; |
| 128 | unsigned short bb_fragments; |
| 129 | struct list_head bb_prealloc_list; |
| 130 | #ifdef DOUBLE_CHECK |
| 131 | void *bb_bitmap; |
| 132 | #endif |
| 133 | unsigned short bb_counters[]; |
| 134 | }; |
| 135 | |
| 136 | #define EXT4_GROUP_INFO_NEED_INIT_BIT 0 |
| 137 | #define EXT4_GROUP_INFO_LOCKED_BIT 1 |
| 138 | |
| 139 | #define EXT4_MB_GRP_NEED_INIT(grp) \ |
| 140 | (test_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &((grp)->bb_state))) |
| 141 | |
| 142 | |
| 143 | struct ext4_prealloc_space { |
| 144 | struct list_head pa_inode_list; |
| 145 | struct list_head pa_group_list; |
| 146 | union { |
| 147 | struct list_head pa_tmp_list; |
| 148 | struct rcu_head pa_rcu; |
| 149 | } u; |
| 150 | spinlock_t pa_lock; |
| 151 | atomic_t pa_count; |
| 152 | unsigned pa_deleted; |
| 153 | ext4_fsblk_t pa_pstart; /* phys. block */ |
| 154 | ext4_lblk_t pa_lstart; /* log. block */ |
| 155 | unsigned short pa_len; /* len of preallocated chunk */ |
| 156 | unsigned short pa_free; /* how many blocks are free */ |
| 157 | unsigned short pa_linear; /* consumed in one direction |
| 158 | * strictly, for grp prealloc */ |
| 159 | spinlock_t *pa_obj_lock; |
| 160 | struct inode *pa_inode; /* hack, for history only */ |
| 161 | }; |
| 162 | |
| 163 | |
| 164 | struct ext4_free_extent { |
| 165 | ext4_lblk_t fe_logical; |
| 166 | ext4_grpblk_t fe_start; |
| 167 | ext4_group_t fe_group; |
| 168 | int fe_len; |
| 169 | }; |
| 170 | |
| 171 | /* |
| 172 | * Locality group: |
| 173 | * we try to group all related changes together |
| 174 | * so that writeback can flush/allocate them together as well |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 175 | * Size of lg_prealloc_list hash is determined by MB_DEFAULT_GROUP_PREALLOC |
| 176 | * (512). We store prealloc space into the hash based on the pa_free blocks |
| 177 | * order value.ie, fls(pa_free)-1; |
Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 178 | */ |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 179 | #define PREALLOC_TB_SIZE 10 |
Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 180 | struct ext4_locality_group { |
| 181 | /* for allocator */ |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 182 | /* to serialize allocates */ |
| 183 | struct mutex lg_mutex; |
| 184 | /* list of preallocations */ |
| 185 | struct list_head lg_prealloc_list[PREALLOC_TB_SIZE]; |
Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 186 | spinlock_t lg_prealloc_lock; |
| 187 | }; |
| 188 | |
| 189 | struct ext4_allocation_context { |
| 190 | struct inode *ac_inode; |
| 191 | struct super_block *ac_sb; |
| 192 | |
| 193 | /* original request */ |
| 194 | struct ext4_free_extent ac_o_ex; |
| 195 | |
| 196 | /* goal request (after normalization) */ |
| 197 | struct ext4_free_extent ac_g_ex; |
| 198 | |
| 199 | /* the best found extent */ |
| 200 | struct ext4_free_extent ac_b_ex; |
| 201 | |
| 202 | /* copy of the bext found extent taken before preallocation efforts */ |
| 203 | struct ext4_free_extent ac_f_ex; |
| 204 | |
| 205 | /* number of iterations done. we have to track to limit searching */ |
| 206 | unsigned long ac_ex_scanned; |
| 207 | __u16 ac_groups_scanned; |
| 208 | __u16 ac_found; |
| 209 | __u16 ac_tail; |
| 210 | __u16 ac_buddy; |
| 211 | __u16 ac_flags; /* allocation hints */ |
| 212 | __u8 ac_status; |
| 213 | __u8 ac_criteria; |
| 214 | __u8 ac_repeats; |
| 215 | __u8 ac_2order; /* if request is to allocate 2^N blocks and |
| 216 | * N > 0, the field stores N, otherwise 0 */ |
| 217 | __u8 ac_op; /* operation, for history only */ |
| 218 | struct page *ac_bitmap_page; |
| 219 | struct page *ac_buddy_page; |
| 220 | struct ext4_prealloc_space *ac_pa; |
| 221 | struct ext4_locality_group *ac_lg; |
| 222 | }; |
| 223 | |
| 224 | #define AC_STATUS_CONTINUE 1 |
| 225 | #define AC_STATUS_FOUND 2 |
| 226 | #define AC_STATUS_BREAK 3 |
| 227 | |
| 228 | struct ext4_mb_history { |
| 229 | struct ext4_free_extent orig; /* orig allocation */ |
| 230 | struct ext4_free_extent goal; /* goal allocation */ |
| 231 | struct ext4_free_extent result; /* result allocation */ |
| 232 | unsigned pid; |
| 233 | unsigned ino; |
| 234 | __u16 found; /* how many extents have been found */ |
| 235 | __u16 groups; /* how many groups have been scanned */ |
| 236 | __u16 tail; /* what tail broke some buddy */ |
| 237 | __u16 buddy; /* buddy the tail ^^^ broke */ |
| 238 | __u16 flags; |
| 239 | __u8 cr:3; /* which phase the result extent was found at */ |
| 240 | __u8 op:4; |
| 241 | __u8 merged:1; |
| 242 | }; |
| 243 | |
| 244 | struct ext4_buddy { |
| 245 | struct page *bd_buddy_page; |
| 246 | void *bd_buddy; |
| 247 | struct page *bd_bitmap_page; |
| 248 | void *bd_bitmap; |
| 249 | struct ext4_group_info *bd_info; |
| 250 | struct super_block *bd_sb; |
| 251 | __u16 bd_blkbits; |
| 252 | ext4_group_t bd_group; |
| 253 | }; |
| 254 | #define EXT4_MB_BITMAP(e4b) ((e4b)->bd_bitmap) |
| 255 | #define EXT4_MB_BUDDY(e4b) ((e4b)->bd_buddy) |
| 256 | |
| 257 | #ifndef EXT4_MB_HISTORY |
| 258 | static inline void ext4_mb_store_history(struct ext4_allocation_context *ac) |
| 259 | { |
| 260 | return; |
| 261 | } |
| 262 | #else |
| 263 | static void ext4_mb_store_history(struct ext4_allocation_context *ac); |
| 264 | #endif |
| 265 | |
| 266 | #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1) |
| 267 | |
Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 268 | struct buffer_head *read_block_bitmap(struct super_block *, ext4_group_t); |
| 269 | |
| 270 | static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap, |
| 271 | ext4_group_t group); |
Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 272 | static void ext4_mb_return_to_preallocation(struct inode *inode, |
| 273 | struct ext4_buddy *e4b, sector_t block, |
| 274 | int count); |
| 275 | static void ext4_mb_put_pa(struct ext4_allocation_context *, |
| 276 | struct super_block *, struct ext4_prealloc_space *pa); |
| 277 | static int ext4_mb_init_per_dev_proc(struct super_block *sb); |
| 278 | static int ext4_mb_destroy_per_dev_proc(struct super_block *sb); |
Theodore Ts'o | 3e624fc | 2008-10-16 20:00:24 -0400 | [diff] [blame] | 279 | static void release_blocks_on_commit(journal_t *journal, transaction_t *txn); |
Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 280 | |
| 281 | |
| 282 | static inline void ext4_lock_group(struct super_block *sb, ext4_group_t group) |
| 283 | { |
| 284 | struct ext4_group_info *grinfo = ext4_get_group_info(sb, group); |
| 285 | |
| 286 | bit_spin_lock(EXT4_GROUP_INFO_LOCKED_BIT, &(grinfo->bb_state)); |
| 287 | } |
| 288 | |
| 289 | static inline void ext4_unlock_group(struct super_block *sb, |
| 290 | ext4_group_t group) |
| 291 | { |
| 292 | struct ext4_group_info *grinfo = ext4_get_group_info(sb, group); |
| 293 | |
| 294 | bit_spin_unlock(EXT4_GROUP_INFO_LOCKED_BIT, &(grinfo->bb_state)); |
| 295 | } |
| 296 | |
| 297 | static inline int ext4_is_group_locked(struct super_block *sb, |
| 298 | ext4_group_t group) |
| 299 | { |
| 300 | struct ext4_group_info *grinfo = ext4_get_group_info(sb, group); |
| 301 | |
| 302 | return bit_spin_is_locked(EXT4_GROUP_INFO_LOCKED_BIT, |
| 303 | &(grinfo->bb_state)); |
| 304 | } |
| 305 | |
| 306 | static ext4_fsblk_t ext4_grp_offs_to_block(struct super_block *sb, |
| 307 | struct ext4_free_extent *fex) |
| 308 | { |
| 309 | ext4_fsblk_t block; |
| 310 | |
| 311 | block = (ext4_fsblk_t) fex->fe_group * EXT4_BLOCKS_PER_GROUP(sb) |
| 312 | + fex->fe_start |
| 313 | + le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block); |
| 314 | return block; |
| 315 | } |
| 316 | #endif |