blob: 0ca811061bc72eca18b46ca025b660f32ce87138 [file] [log] [blame]
Mingming Cao8f6e39a2008-04-29 22:01:31 -04001/*
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'o8a0aba72008-10-16 10:06:27 -040021#include <linux/blkdev.h>
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -050022#include <linux/mutex.h>
Mingming Cao8f6e39a2008-04-29 22:01:31 -040023#include "ext4_jbd2.h"
24#include "ext4.h"
Mingming Cao8f6e39a2008-04-29 22:01:31 -040025
26/*
27 * with AGGRESSIVE_CHECK allocator runs consistency checks over
28 * structures. these checks slow things down a lot
29 */
30#define AGGRESSIVE_CHECK__
31
32/*
33 * with DOUBLE_CHECK defined mballoc creates persistent in-core
34 * bitmaps, maintains and uses them to check for double allocations
35 */
36#define DOUBLE_CHECK__
37
38/*
39 */
Theodore Ts'o6ba495e2009-09-18 13:38:55 -040040#ifdef CONFIG_EXT4_DEBUG
41extern u8 mb_enable_debug;
42
43#define mb_debug(n, fmt, a...) \
44 do { \
45 if ((n) <= mb_enable_debug) { \
46 printk(KERN_DEBUG "(%s, %d): %s: ", \
47 __FILE__, __LINE__, __func__); \
48 printk(fmt, ## a); \
49 } \
50 } while (0)
Mingming Cao8f6e39a2008-04-29 22:01:31 -040051#else
Theodore Ts'o6ba495e2009-09-18 13:38:55 -040052#define mb_debug(n, fmt, a...)
Mingming Cao8f6e39a2008-04-29 22:01:31 -040053#endif
54
Mingming Cao8f6e39a2008-04-29 22:01:31 -040055#define EXT4_MB_HISTORY_ALLOC 1 /* allocation */
56#define EXT4_MB_HISTORY_PREALLOC 2 /* preallocated blocks used */
Mingming Cao8f6e39a2008-04-29 22:01:31 -040057
58/*
59 * How long mballoc can look for a best extent (in found extents)
60 */
61#define MB_DEFAULT_MAX_TO_SCAN 200
62
63/*
64 * How long mballoc must look for a best extent
65 */
66#define MB_DEFAULT_MIN_TO_SCAN 10
67
68/*
69 * How many groups mballoc will scan looking for the best chunk
70 */
71#define MB_DEFAULT_MAX_GROUPS_TO_SCAN 5
72
73/*
74 * with 'ext4_mb_stats' allocator will collect stats that will be
75 * shown at umount. The collecting costs though!
76 */
Theodore Ts'o90576c02009-09-29 15:51:30 -040077#define MB_DEFAULT_STATS 0
Mingming Cao8f6e39a2008-04-29 22:01:31 -040078
79/*
80 * files smaller than MB_DEFAULT_STREAM_THRESHOLD are served
81 * by the stream allocator, which purpose is to pack requests
82 * as close each to other as possible to produce smooth I/O traffic
83 * We use locality group prealloc space for stream request.
84 * We can tune the same via /proc/fs/ext4/<parition>/stream_req
85 */
86#define MB_DEFAULT_STREAM_THRESHOLD 16 /* 64K */
87
88/*
89 * for which requests use 2^N search using buddies
90 */
91#define MB_DEFAULT_ORDER2_REQS 2
92
93/*
94 * default group prealloc size 512 blocks
95 */
96#define MB_DEFAULT_GROUP_PREALLOC 512
97
Mingming Cao8f6e39a2008-04-29 22:01:31 -040098
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -040099struct ext4_free_data {
100 /* this links the free block information from group_info */
101 struct rb_node node;
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400102
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -0400103 /* this links the free block information from ext4_sb_info */
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400104 struct list_head list;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -0400105
106 /* group which free block extent belongs */
107 ext4_group_t group;
108
109 /* free block extent */
110 ext4_grpblk_t start_blk;
111 ext4_grpblk_t count;
112
113 /* transaction which freed this extent */
114 tid_t t_tid;
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400115};
116
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400117struct ext4_prealloc_space {
118 struct list_head pa_inode_list;
119 struct list_head pa_group_list;
120 union {
121 struct list_head pa_tmp_list;
122 struct rcu_head pa_rcu;
123 } u;
124 spinlock_t pa_lock;
125 atomic_t pa_count;
126 unsigned pa_deleted;
127 ext4_fsblk_t pa_pstart; /* phys. block */
128 ext4_lblk_t pa_lstart; /* log. block */
Eric Sandeena36b4492009-08-25 22:36:45 -0400129 ext4_grpblk_t pa_len; /* len of preallocated chunk */
130 ext4_grpblk_t pa_free; /* how many blocks are free */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -0400131 unsigned short pa_type; /* pa type. inode or group */
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400132 spinlock_t *pa_obj_lock;
133 struct inode *pa_inode; /* hack, for history only */
134};
135
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -0400136enum {
137 MB_INODE_PA = 0,
138 MB_GROUP_PA = 1
139};
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400140
141struct ext4_free_extent {
142 ext4_lblk_t fe_logical;
143 ext4_grpblk_t fe_start;
144 ext4_group_t fe_group;
Eric Sandeena36b4492009-08-25 22:36:45 -0400145 ext4_grpblk_t fe_len;
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400146};
147
148/*
149 * Locality group:
150 * we try to group all related changes together
151 * so that writeback can flush/allocate them together as well
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -0400152 * Size of lg_prealloc_list hash is determined by MB_DEFAULT_GROUP_PREALLOC
153 * (512). We store prealloc space into the hash based on the pa_free blocks
154 * order value.ie, fls(pa_free)-1;
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400155 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -0400156#define PREALLOC_TB_SIZE 10
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400157struct ext4_locality_group {
158 /* for allocator */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -0400159 /* to serialize allocates */
160 struct mutex lg_mutex;
161 /* list of preallocations */
162 struct list_head lg_prealloc_list[PREALLOC_TB_SIZE];
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400163 spinlock_t lg_prealloc_lock;
164};
165
166struct ext4_allocation_context {
167 struct inode *ac_inode;
168 struct super_block *ac_sb;
169
170 /* original request */
171 struct ext4_free_extent ac_o_ex;
172
173 /* goal request (after normalization) */
174 struct ext4_free_extent ac_g_ex;
175
176 /* the best found extent */
177 struct ext4_free_extent ac_b_ex;
178
179 /* copy of the bext found extent taken before preallocation efforts */
180 struct ext4_free_extent ac_f_ex;
181
182 /* number of iterations done. we have to track to limit searching */
183 unsigned long ac_ex_scanned;
184 __u16 ac_groups_scanned;
185 __u16 ac_found;
186 __u16 ac_tail;
187 __u16 ac_buddy;
188 __u16 ac_flags; /* allocation hints */
189 __u8 ac_status;
190 __u8 ac_criteria;
191 __u8 ac_repeats;
192 __u8 ac_2order; /* if request is to allocate 2^N blocks and
193 * N > 0, the field stores N, otherwise 0 */
194 __u8 ac_op; /* operation, for history only */
195 struct page *ac_bitmap_page;
196 struct page *ac_buddy_page;
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -0500197 /*
198 * pointer to the held semaphore upon successful
199 * block allocation
200 */
201 struct rw_semaphore *alloc_semp;
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400202 struct ext4_prealloc_space *ac_pa;
203 struct ext4_locality_group *ac_lg;
204};
205
206#define AC_STATUS_CONTINUE 1
207#define AC_STATUS_FOUND 2
208#define AC_STATUS_BREAK 3
209
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400210struct ext4_buddy {
211 struct page *bd_buddy_page;
212 void *bd_buddy;
213 struct page *bd_bitmap_page;
214 void *bd_bitmap;
215 struct ext4_group_info *bd_info;
216 struct super_block *bd_sb;
217 __u16 bd_blkbits;
218 ext4_group_t bd_group;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -0500219 struct rw_semaphore *alloc_semp;
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400220};
221#define EXT4_MB_BITMAP(e4b) ((e4b)->bd_bitmap)
222#define EXT4_MB_BUDDY(e4b) ((e4b)->bd_buddy)
223
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400224#define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
225
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500226static inline ext4_fsblk_t ext4_grp_offs_to_block(struct super_block *sb,
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400227 struct ext4_free_extent *fex)
228{
229 ext4_fsblk_t block;
230
231 block = (ext4_fsblk_t) fex->fe_group * EXT4_BLOCKS_PER_GROUP(sb)
232 + fex->fe_start
233 + le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
234 return block;
235}
236#endif