xref: /openbmc/linux/fs/ext4/mballoc.h (revision e8e0929d)
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>
21 #include <linux/blkdev.h>
22 #include <linux/mutex.h>
23 #include "ext4_jbd2.h"
24 #include "ext4.h"
25 
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  */
40 #ifdef CONFIG_EXT4_DEBUG
41 extern 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)
51 #else
52 #define mb_debug(n, fmt, a...)
53 #endif
54 
55 #define EXT4_MB_HISTORY_ALLOC		1	/* allocation */
56 #define EXT4_MB_HISTORY_PREALLOC	2	/* preallocated blocks used */
57 
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  */
77 #define MB_DEFAULT_STATS		0
78 
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 
98 
99 struct ext4_free_data {
100 	/* this links the free block information from group_info */
101 	struct rb_node node;
102 
103 	/* this links the free block information from ext4_sb_info */
104 	struct list_head list;
105 
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;
115 };
116 
117 struct 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 */
129 	ext4_grpblk_t		pa_len;		/* len of preallocated chunk */
130 	ext4_grpblk_t		pa_free;	/* how many blocks are free */
131 	unsigned short		pa_type;	/* pa type. inode or group */
132 	spinlock_t		*pa_obj_lock;
133 	struct inode		*pa_inode;	/* hack, for history only */
134 };
135 
136 enum {
137 	MB_INODE_PA = 0,
138 	MB_GROUP_PA = 1
139 };
140 
141 struct ext4_free_extent {
142 	ext4_lblk_t fe_logical;
143 	ext4_grpblk_t fe_start;
144 	ext4_group_t fe_group;
145 	ext4_grpblk_t fe_len;
146 };
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
152  *   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;
155  */
156 #define PREALLOC_TB_SIZE 10
157 struct ext4_locality_group {
158 	/* for allocator */
159 	/* to serialize allocates */
160 	struct mutex		lg_mutex;
161 	/* list of preallocations */
162 	struct list_head	lg_prealloc_list[PREALLOC_TB_SIZE];
163 	spinlock_t		lg_prealloc_lock;
164 };
165 
166 struct 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;
197 	/*
198 	 * pointer to the held semaphore upon successful
199 	 * block allocation
200 	 */
201 	struct rw_semaphore *alloc_semp;
202 	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 
210 struct 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;
219 	struct rw_semaphore *alloc_semp;
220 };
221 #define EXT4_MB_BITMAP(e4b)	((e4b)->bd_bitmap)
222 #define EXT4_MB_BUDDY(e4b)	((e4b)->bd_buddy)
223 
224 #define in_range(b, first, len)	((b) >= (first) && (b) <= (first) + (len) - 1)
225 
226 static inline ext4_fsblk_t ext4_grp_offs_to_block(struct super_block *sb,
227 					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
237