| 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> | 
| Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 23 | #include <linux/mutex.h> | 
| Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 24 | #include "ext4_jbd2.h" | 
|  | 25 | #include "ext4.h" | 
|  | 26 | #include "group.h" | 
|  | 27 |  | 
|  | 28 | /* | 
|  | 29 | * with AGGRESSIVE_CHECK allocator runs consistency checks over | 
|  | 30 | * structures. these checks slow things down a lot | 
|  | 31 | */ | 
|  | 32 | #define AGGRESSIVE_CHECK__ | 
|  | 33 |  | 
|  | 34 | /* | 
|  | 35 | * with DOUBLE_CHECK defined mballoc creates persistent in-core | 
|  | 36 | * bitmaps, maintains and uses them to check for double allocations | 
|  | 37 | */ | 
|  | 38 | #define DOUBLE_CHECK__ | 
|  | 39 |  | 
|  | 40 | /* | 
|  | 41 | */ | 
|  | 42 | #define MB_DEBUG__ | 
|  | 43 | #ifdef MB_DEBUG | 
|  | 44 | #define mb_debug(fmt, a...)	printk(fmt, ##a) | 
|  | 45 | #else | 
|  | 46 | #define mb_debug(fmt, a...) | 
|  | 47 | #endif | 
|  | 48 |  | 
|  | 49 | /* | 
|  | 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 | 
|  | 52 | */ | 
|  | 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 */ | 
|  | 58 |  | 
|  | 59 | #define EXT4_MB_HISTORY_DEFAULT		(EXT4_MB_HISTORY_ALLOC | \ | 
|  | 60 | EXT4_MB_HISTORY_PREALLOC) | 
|  | 61 |  | 
|  | 62 | /* | 
|  | 63 | * How long mballoc can look for a best extent (in found extents) | 
|  | 64 | */ | 
|  | 65 | #define MB_DEFAULT_MAX_TO_SCAN		200 | 
|  | 66 |  | 
|  | 67 | /* | 
|  | 68 | * How long mballoc must look for a best extent | 
|  | 69 | */ | 
|  | 70 | #define MB_DEFAULT_MIN_TO_SCAN		10 | 
|  | 71 |  | 
|  | 72 | /* | 
|  | 73 | * How many groups mballoc will scan looking for the best chunk | 
|  | 74 | */ | 
|  | 75 | #define MB_DEFAULT_MAX_GROUPS_TO_SCAN	5 | 
|  | 76 |  | 
|  | 77 | /* | 
|  | 78 | * with 'ext4_mb_stats' allocator will collect stats that will be | 
|  | 79 | * shown at umount. The collecting costs though! | 
|  | 80 | */ | 
|  | 81 | #define MB_DEFAULT_STATS		1 | 
|  | 82 |  | 
|  | 83 | /* | 
|  | 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 | 
|  | 89 | */ | 
|  | 90 | #define MB_DEFAULT_STREAM_THRESHOLD	16	/* 64K */ | 
|  | 91 |  | 
|  | 92 | /* | 
|  | 93 | * for which requests use 2^N search using buddies | 
|  | 94 | */ | 
|  | 95 | #define MB_DEFAULT_ORDER2_REQS		2 | 
|  | 96 |  | 
|  | 97 | /* | 
|  | 98 | * default group prealloc size 512 blocks | 
|  | 99 | */ | 
|  | 100 | #define MB_DEFAULT_GROUP_PREALLOC	512 | 
|  | 101 |  | 
| Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 102 |  | 
| Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 103 | struct ext4_free_data { | 
|  | 104 | /* this links the free block information from group_info */ | 
|  | 105 | struct rb_node node; | 
| Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 106 |  | 
| Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 107 | /* this links the free block information from ext4_sb_info */ | 
| Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 108 | struct list_head list; | 
| Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 109 |  | 
|  | 110 | /* group which free block extent belongs */ | 
|  | 111 | ext4_group_t group; | 
|  | 112 |  | 
|  | 113 | /* free block extent */ | 
|  | 114 | ext4_grpblk_t start_blk; | 
|  | 115 | ext4_grpblk_t count; | 
|  | 116 |  | 
|  | 117 | /* transaction which freed this extent */ | 
|  | 118 | tid_t	t_tid; | 
| Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 119 | }; | 
|  | 120 |  | 
| Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 121 | struct ext4_prealloc_space { | 
|  | 122 | struct list_head	pa_inode_list; | 
|  | 123 | struct list_head	pa_group_list; | 
|  | 124 | union { | 
|  | 125 | struct list_head pa_tmp_list; | 
|  | 126 | struct rcu_head	pa_rcu; | 
|  | 127 | } u; | 
|  | 128 | spinlock_t		pa_lock; | 
|  | 129 | atomic_t		pa_count; | 
|  | 130 | unsigned		pa_deleted; | 
|  | 131 | ext4_fsblk_t		pa_pstart;	/* phys. block */ | 
|  | 132 | ext4_lblk_t		pa_lstart;	/* log. block */ | 
|  | 133 | unsigned short		pa_len;		/* len of preallocated chunk */ | 
|  | 134 | unsigned short		pa_free;	/* how many blocks are free */ | 
|  | 135 | unsigned short		pa_linear;	/* consumed in one direction | 
|  | 136 | * strictly, for grp prealloc */ | 
|  | 137 | spinlock_t		*pa_obj_lock; | 
|  | 138 | struct inode		*pa_inode;	/* hack, for history only */ | 
|  | 139 | }; | 
|  | 140 |  | 
|  | 141 |  | 
|  | 142 | struct ext4_free_extent { | 
|  | 143 | ext4_lblk_t fe_logical; | 
|  | 144 | ext4_grpblk_t fe_start; | 
|  | 145 | ext4_group_t fe_group; | 
|  | 146 | int fe_len; | 
|  | 147 | }; | 
|  | 148 |  | 
|  | 149 | /* | 
|  | 150 | * Locality group: | 
|  | 151 | *   we try to group all related changes together | 
|  | 152 | *   so that writeback can flush/allocate them together as well | 
| Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 153 | *   Size of lg_prealloc_list hash is determined by MB_DEFAULT_GROUP_PREALLOC | 
|  | 154 | *   (512). We store prealloc space into the hash based on the pa_free blocks | 
|  | 155 | *   order value.ie, fls(pa_free)-1; | 
| Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 156 | */ | 
| Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 157 | #define PREALLOC_TB_SIZE 10 | 
| Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 158 | struct ext4_locality_group { | 
|  | 159 | /* for allocator */ | 
| Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 160 | /* to serialize allocates */ | 
|  | 161 | struct mutex		lg_mutex; | 
|  | 162 | /* list of preallocations */ | 
|  | 163 | struct list_head	lg_prealloc_list[PREALLOC_TB_SIZE]; | 
| Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 164 | spinlock_t		lg_prealloc_lock; | 
|  | 165 | }; | 
|  | 166 |  | 
|  | 167 | struct ext4_allocation_context { | 
|  | 168 | struct inode *ac_inode; | 
|  | 169 | struct super_block *ac_sb; | 
|  | 170 |  | 
|  | 171 | /* original request */ | 
|  | 172 | struct ext4_free_extent ac_o_ex; | 
|  | 173 |  | 
|  | 174 | /* goal request (after normalization) */ | 
|  | 175 | struct ext4_free_extent ac_g_ex; | 
|  | 176 |  | 
|  | 177 | /* the best found extent */ | 
|  | 178 | struct ext4_free_extent ac_b_ex; | 
|  | 179 |  | 
|  | 180 | /* copy of the bext found extent taken before preallocation efforts */ | 
|  | 181 | struct ext4_free_extent ac_f_ex; | 
|  | 182 |  | 
|  | 183 | /* number of iterations done. we have to track to limit searching */ | 
|  | 184 | unsigned long ac_ex_scanned; | 
|  | 185 | __u16 ac_groups_scanned; | 
|  | 186 | __u16 ac_found; | 
|  | 187 | __u16 ac_tail; | 
|  | 188 | __u16 ac_buddy; | 
|  | 189 | __u16 ac_flags;		/* allocation hints */ | 
|  | 190 | __u8 ac_status; | 
|  | 191 | __u8 ac_criteria; | 
|  | 192 | __u8 ac_repeats; | 
|  | 193 | __u8 ac_2order;		/* if request is to allocate 2^N blocks and | 
|  | 194 | * N > 0, the field stores N, otherwise 0 */ | 
|  | 195 | __u8 ac_op;		/* operation, for history only */ | 
|  | 196 | struct page *ac_bitmap_page; | 
|  | 197 | struct page *ac_buddy_page; | 
| Aneesh Kumar K.V | 8556e8f | 2009-01-05 21:46:55 -0500 | [diff] [blame] | 198 | /* | 
|  | 199 | * pointer to the held semaphore upon successful | 
|  | 200 | * block allocation | 
|  | 201 | */ | 
|  | 202 | struct rw_semaphore *alloc_semp; | 
| Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 203 | struct ext4_prealloc_space *ac_pa; | 
|  | 204 | struct ext4_locality_group *ac_lg; | 
|  | 205 | }; | 
|  | 206 |  | 
|  | 207 | #define AC_STATUS_CONTINUE	1 | 
|  | 208 | #define AC_STATUS_FOUND		2 | 
|  | 209 | #define AC_STATUS_BREAK		3 | 
|  | 210 |  | 
|  | 211 | struct ext4_mb_history { | 
|  | 212 | struct ext4_free_extent orig;	/* orig allocation */ | 
|  | 213 | struct ext4_free_extent goal;	/* goal allocation */ | 
|  | 214 | struct ext4_free_extent result;	/* result allocation */ | 
|  | 215 | unsigned pid; | 
|  | 216 | unsigned ino; | 
|  | 217 | __u16 found;	/* how many extents have been found */ | 
|  | 218 | __u16 groups;	/* how many groups have been scanned */ | 
|  | 219 | __u16 tail;	/* what tail broke some buddy */ | 
|  | 220 | __u16 buddy;	/* buddy the tail ^^^ broke */ | 
|  | 221 | __u16 flags; | 
|  | 222 | __u8 cr:3;	/* which phase the result extent was found at */ | 
|  | 223 | __u8 op:4; | 
|  | 224 | __u8 merged:1; | 
|  | 225 | }; | 
|  | 226 |  | 
|  | 227 | struct ext4_buddy { | 
|  | 228 | struct page *bd_buddy_page; | 
|  | 229 | void *bd_buddy; | 
|  | 230 | struct page *bd_bitmap_page; | 
|  | 231 | void *bd_bitmap; | 
|  | 232 | struct ext4_group_info *bd_info; | 
|  | 233 | struct super_block *bd_sb; | 
|  | 234 | __u16 bd_blkbits; | 
|  | 235 | ext4_group_t bd_group; | 
| Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 236 | struct rw_semaphore *alloc_semp; | 
| Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 237 | }; | 
|  | 238 | #define EXT4_MB_BITMAP(e4b)	((e4b)->bd_bitmap) | 
|  | 239 | #define EXT4_MB_BUDDY(e4b)	((e4b)->bd_buddy) | 
|  | 240 |  | 
|  | 241 | #ifndef EXT4_MB_HISTORY | 
|  | 242 | static inline void ext4_mb_store_history(struct ext4_allocation_context *ac) | 
|  | 243 | { | 
|  | 244 | return; | 
|  | 245 | } | 
| Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 246 | #endif | 
|  | 247 |  | 
|  | 248 | #define in_range(b, first, len)	((b) >= (first) && (b) <= (first) + (len) - 1) | 
|  | 249 |  | 
| Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 250 | struct buffer_head *read_block_bitmap(struct super_block *, ext4_group_t); | 
| Aneesh Kumar K.V | c3a326a | 2008-11-25 15:11:52 -0500 | [diff] [blame] | 251 | static inline ext4_fsblk_t ext4_grp_offs_to_block(struct super_block *sb, | 
| Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 252 | struct ext4_free_extent *fex) | 
|  | 253 | { | 
|  | 254 | ext4_fsblk_t block; | 
|  | 255 |  | 
|  | 256 | block = (ext4_fsblk_t) fex->fe_group * EXT4_BLOCKS_PER_GROUP(sb) | 
|  | 257 | + fex->fe_start | 
|  | 258 | + le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block); | 
|  | 259 | return block; | 
|  | 260 | } | 
|  | 261 | #endif |