xref: /openbmc/linux/fs/ext4/mballoc.h (revision fdd9a009)
1f5166768STheodore Ts'o // SPDX-License-Identifier: GPL-2.0
28f6e39a7SMingming Cao /*
38f6e39a7SMingming Cao  *  fs/ext4/mballoc.h
48f6e39a7SMingming Cao  *
58f6e39a7SMingming Cao  *  Written by: Alex Tomas <alex@clusterfs.com>
68f6e39a7SMingming Cao  *
78f6e39a7SMingming Cao  */
88f6e39a7SMingming Cao #ifndef _EXT4_MBALLOC_H
98f6e39a7SMingming Cao #define _EXT4_MBALLOC_H
108f6e39a7SMingming Cao 
118f6e39a7SMingming Cao #include <linux/time.h>
128f6e39a7SMingming Cao #include <linux/fs.h>
138f6e39a7SMingming Cao #include <linux/namei.h>
148f6e39a7SMingming Cao #include <linux/quotaops.h>
158f6e39a7SMingming Cao #include <linux/buffer_head.h>
168f6e39a7SMingming Cao #include <linux/module.h>
178f6e39a7SMingming Cao #include <linux/swap.h>
188f6e39a7SMingming Cao #include <linux/proc_fs.h>
198f6e39a7SMingming Cao #include <linux/pagemap.h>
208f6e39a7SMingming Cao #include <linux/seq_file.h>
218a0aba73STheodore Ts'o #include <linux/blkdev.h>
22920313a7SAneesh Kumar K.V #include <linux/mutex.h>
238f6e39a7SMingming Cao #include "ext4_jbd2.h"
248f6e39a7SMingming Cao #include "ext4.h"
258f6e39a7SMingming Cao 
268f6e39a7SMingming Cao /*
27d3df1453SRitesh Harjani  * mb_debug() dynamic printk msgs could be used to debug mballoc code.
288f6e39a7SMingming Cao  */
296ba495e9STheodore Ts'o #ifdef CONFIG_EXT4_DEBUG
30d3df1453SRitesh Harjani #define mb_debug(sb, fmt, ...)						\
31d3df1453SRitesh Harjani 	pr_debug("[%s/%d] EXT4-fs (%s): (%s, %d): %s: " fmt,		\
32d3df1453SRitesh Harjani 		current->comm, task_pid_nr(current), sb->s_id,		\
33d3df1453SRitesh Harjani 	       __FILE__, __LINE__, __func__, ##__VA_ARGS__)
348f6e39a7SMingming Cao #else
35d3df1453SRitesh Harjani #define mb_debug(sb, fmt, ...)	no_printk(fmt, ##__VA_ARGS__)
368f6e39a7SMingming Cao #endif
378f6e39a7SMingming Cao 
388f6e39a7SMingming Cao #define EXT4_MB_HISTORY_ALLOC		1	/* allocation */
398f6e39a7SMingming Cao #define EXT4_MB_HISTORY_PREALLOC	2	/* preallocated blocks used */
408f6e39a7SMingming Cao 
418f6e39a7SMingming Cao /*
428f6e39a7SMingming Cao  * How long mballoc can look for a best extent (in found extents)
438f6e39a7SMingming Cao  */
448f6e39a7SMingming Cao #define MB_DEFAULT_MAX_TO_SCAN		200
458f6e39a7SMingming Cao 
468f6e39a7SMingming Cao /*
478f6e39a7SMingming Cao  * How long mballoc must look for a best extent
488f6e39a7SMingming Cao  */
498f6e39a7SMingming Cao #define MB_DEFAULT_MIN_TO_SCAN		10
508f6e39a7SMingming Cao 
518f6e39a7SMingming Cao /*
525730cce3SRitesh Harjani  * with 's_mb_stats' allocator will collect stats that will be
538f6e39a7SMingming Cao  * shown at umount. The collecting costs though!
548f6e39a7SMingming Cao  */
5590576c0bSTheodore Ts'o #define MB_DEFAULT_STATS		0
568f6e39a7SMingming Cao 
578f6e39a7SMingming Cao /*
588f6e39a7SMingming Cao  * files smaller than MB_DEFAULT_STREAM_THRESHOLD are served
598f6e39a7SMingming Cao  * by the stream allocator, which purpose is to pack requests
608f6e39a7SMingming Cao  * as close each to other as possible to produce smooth I/O traffic
618f6e39a7SMingming Cao  * We use locality group prealloc space for stream request.
623088e5a5SBhaskar Chowdhury  * We can tune the same via /proc/fs/ext4/<partition>/stream_req
638f6e39a7SMingming Cao  */
648f6e39a7SMingming Cao #define MB_DEFAULT_STREAM_THRESHOLD	16	/* 64K */
658f6e39a7SMingming Cao 
668f6e39a7SMingming Cao /*
678f6e39a7SMingming Cao  * for which requests use 2^N search using buddies
688f6e39a7SMingming Cao  */
698f6e39a7SMingming Cao #define MB_DEFAULT_ORDER2_REQS		2
708f6e39a7SMingming Cao 
718f6e39a7SMingming Cao /*
728f6e39a7SMingming Cao  * default group prealloc size 512 blocks
738f6e39a7SMingming Cao  */
748f6e39a7SMingming Cao #define MB_DEFAULT_GROUP_PREALLOC	512
758f6e39a7SMingming Cao 
7627bc446eSbrookxu /*
77196e402aSHarshad Shirwadkar  * Number of groups to search linearly before performing group scanning
78196e402aSHarshad Shirwadkar  * optimization.
79196e402aSHarshad Shirwadkar  */
80196e402aSHarshad Shirwadkar #define MB_DEFAULT_LINEAR_LIMIT		4
81196e402aSHarshad Shirwadkar 
82196e402aSHarshad Shirwadkar /*
83196e402aSHarshad Shirwadkar  * Minimum number of groups that should be present in the file system to perform
84196e402aSHarshad Shirwadkar  * group scanning optimizations.
85196e402aSHarshad Shirwadkar  */
86196e402aSHarshad Shirwadkar #define MB_DEFAULT_LINEAR_SCAN_THRESHOLD	16
87196e402aSHarshad Shirwadkar 
88196e402aSHarshad Shirwadkar /*
894b68f6dfSHarshad Shirwadkar  * Number of valid buddy orders
904b68f6dfSHarshad Shirwadkar  */
914b68f6dfSHarshad Shirwadkar #define MB_NUM_ORDERS(sb)		((sb)->s_blocksize_bits + 2)
924b68f6dfSHarshad Shirwadkar 
93c894058dSAneesh Kumar K.V struct ext4_free_data {
94a0154344SDaeho Jeong 	/* this links the free block information from sb_info */
95a0154344SDaeho Jeong 	struct list_head		efd_list;
9618aadd47SBobi Jam 
9718aadd47SBobi Jam 	/* this links the free block information from group_info */
9818aadd47SBobi Jam 	struct rb_node			efd_node;
99c894058dSAneesh Kumar K.V 
100c894058dSAneesh Kumar K.V 	/* group which free block extent belongs */
10118aadd47SBobi Jam 	ext4_group_t			efd_group;
102c894058dSAneesh Kumar K.V 
103c894058dSAneesh Kumar K.V 	/* free block extent */
10418aadd47SBobi Jam 	ext4_grpblk_t			efd_start_cluster;
10518aadd47SBobi Jam 	ext4_grpblk_t			efd_count;
106c894058dSAneesh Kumar K.V 
107c894058dSAneesh Kumar K.V 	/* transaction which freed this extent */
10818aadd47SBobi Jam 	tid_t				efd_tid;
1098f6e39a7SMingming Cao };
1108f6e39a7SMingming Cao 
1118f6e39a7SMingming Cao struct ext4_prealloc_space {
112a8e38fd3SOjaswin Mujoo 	union {
11338727786SOjaswin Mujoo 		struct rb_node	inode_node;		/* for inode PA rbtree */
114a8e38fd3SOjaswin Mujoo 		struct list_head	lg_list;	/* for lg PAs */
115a8e38fd3SOjaswin Mujoo 	} pa_node;
1168f6e39a7SMingming Cao 	struct list_head	pa_group_list;
1178f6e39a7SMingming Cao 	union {
1188f6e39a7SMingming Cao 		struct list_head pa_tmp_list;
1198f6e39a7SMingming Cao 		struct rcu_head	pa_rcu;
1208f6e39a7SMingming Cao 	} u;
1218f6e39a7SMingming Cao 	spinlock_t		pa_lock;
1228f6e39a7SMingming Cao 	atomic_t		pa_count;
1238f6e39a7SMingming Cao 	unsigned		pa_deleted;
1248f6e39a7SMingming Cao 	ext4_fsblk_t		pa_pstart;	/* phys. block */
1258f6e39a7SMingming Cao 	ext4_lblk_t		pa_lstart;	/* log. block */
126a36b4498SEric Sandeen 	ext4_grpblk_t		pa_len;		/* len of preallocated chunk */
127a36b4498SEric Sandeen 	ext4_grpblk_t		pa_free;	/* how many blocks are free */
128cc0fb9adSAneesh Kumar K.V 	unsigned short		pa_type;	/* pa type. inode or group */
129a8e38fd3SOjaswin Mujoo 	union {
13038727786SOjaswin Mujoo 		rwlock_t		*inode_lock;	/* locks the rbtree holding this PA */
131a8e38fd3SOjaswin Mujoo 		spinlock_t		*lg_lock;	/* locks the lg list holding this PA */
132a8e38fd3SOjaswin Mujoo 	} pa_node_lock;
13338727786SOjaswin Mujoo 	struct inode		*pa_inode;	/* used to get the inode during group discard */
1348f6e39a7SMingming Cao };
1358f6e39a7SMingming Cao 
136cc0fb9adSAneesh Kumar K.V enum {
137cc0fb9adSAneesh Kumar K.V 	MB_INODE_PA = 0,
138cc0fb9adSAneesh Kumar K.V 	MB_GROUP_PA = 1
139cc0fb9adSAneesh Kumar K.V };
1408f6e39a7SMingming Cao 
1418f6e39a7SMingming Cao struct ext4_free_extent {
1428f6e39a7SMingming Cao 	ext4_lblk_t fe_logical;
14353accfa9STheodore Ts'o 	ext4_grpblk_t fe_start;	/* In cluster units */
1448f6e39a7SMingming Cao 	ext4_group_t fe_group;
14553accfa9STheodore Ts'o 	ext4_grpblk_t fe_len;	/* In cluster units */
1468f6e39a7SMingming Cao };
1478f6e39a7SMingming Cao 
1488f6e39a7SMingming Cao /*
1498f6e39a7SMingming Cao  * Locality group:
1508f6e39a7SMingming Cao  *   we try to group all related changes together
1518f6e39a7SMingming Cao  *   so that writeback can flush/allocate them together as well
1526be2ded1SAneesh Kumar K.V  *   Size of lg_prealloc_list hash is determined by MB_DEFAULT_GROUP_PREALLOC
1536be2ded1SAneesh Kumar K.V  *   (512). We store prealloc space into the hash based on the pa_free blocks
1546be2ded1SAneesh Kumar K.V  *   order value.ie, fls(pa_free)-1;
1558f6e39a7SMingming Cao  */
1566be2ded1SAneesh Kumar K.V #define PREALLOC_TB_SIZE 10
1578f6e39a7SMingming Cao struct ext4_locality_group {
1588f6e39a7SMingming Cao 	/* for allocator */
1596be2ded1SAneesh Kumar K.V 	/* to serialize allocates */
1606be2ded1SAneesh Kumar K.V 	struct mutex		lg_mutex;
1616be2ded1SAneesh Kumar K.V 	/* list of preallocations */
1626be2ded1SAneesh Kumar K.V 	struct list_head	lg_prealloc_list[PREALLOC_TB_SIZE];
1638f6e39a7SMingming Cao 	spinlock_t		lg_prealloc_lock;
1648f6e39a7SMingming Cao };
1658f6e39a7SMingming Cao 
1668f6e39a7SMingming Cao struct ext4_allocation_context {
1678f6e39a7SMingming Cao 	struct inode *ac_inode;
1688f6e39a7SMingming Cao 	struct super_block *ac_sb;
1698f6e39a7SMingming Cao 
1708f6e39a7SMingming Cao 	/* original request */
1718f6e39a7SMingming Cao 	struct ext4_free_extent ac_o_ex;
1728f6e39a7SMingming Cao 
17358696f3aSColy Li 	/* goal request (normalized ac_o_ex) */
1748f6e39a7SMingming Cao 	struct ext4_free_extent ac_g_ex;
1758f6e39a7SMingming Cao 
1768f6e39a7SMingming Cao 	/* the best found extent */
1778f6e39a7SMingming Cao 	struct ext4_free_extent ac_b_ex;
1788f6e39a7SMingming Cao 
179ff3fc173SRobin Dong 	/* copy of the best found extent taken before preallocation efforts */
1808f6e39a7SMingming Cao 	struct ext4_free_extent ac_f_ex;
1818f6e39a7SMingming Cao 
182196e402aSHarshad Shirwadkar 	__u32 ac_groups_considered;
183196e402aSHarshad Shirwadkar 	__u32 ac_flags;		/* allocation hints */
1848f6e39a7SMingming Cao 	__u16 ac_groups_scanned;
185196e402aSHarshad Shirwadkar 	__u16 ac_groups_linear_remaining;
1868f6e39a7SMingming Cao 	__u16 ac_found;
187*fdd9a009SOjaswin Mujoo 	__u16 ac_cX_found[EXT4_MB_NUM_CRS];
1888f6e39a7SMingming Cao 	__u16 ac_tail;
1898f6e39a7SMingming Cao 	__u16 ac_buddy;
1908f6e39a7SMingming Cao 	__u8 ac_status;
1918f6e39a7SMingming Cao 	__u8 ac_criteria;
1928f6e39a7SMingming Cao 	__u8 ac_2order;		/* if request is to allocate 2^N blocks and
1938f6e39a7SMingming Cao 				 * N > 0, the field stores N, otherwise 0 */
1948f6e39a7SMingming Cao 	__u8 ac_op;		/* operation, for history only */
1958f6e39a7SMingming Cao 	struct page *ac_bitmap_page;
1968f6e39a7SMingming Cao 	struct page *ac_buddy_page;
1978f6e39a7SMingming Cao 	struct ext4_prealloc_space *ac_pa;
1988f6e39a7SMingming Cao 	struct ext4_locality_group *ac_lg;
1998f6e39a7SMingming Cao };
2008f6e39a7SMingming Cao 
2018f6e39a7SMingming Cao #define AC_STATUS_CONTINUE	1
2028f6e39a7SMingming Cao #define AC_STATUS_FOUND		2
2038f6e39a7SMingming Cao #define AC_STATUS_BREAK		3
2048f6e39a7SMingming Cao 
2058f6e39a7SMingming Cao struct ext4_buddy {
2068f6e39a7SMingming Cao 	struct page *bd_buddy_page;
2078f6e39a7SMingming Cao 	void *bd_buddy;
2088f6e39a7SMingming Cao 	struct page *bd_bitmap_page;
2098f6e39a7SMingming Cao 	void *bd_bitmap;
2108f6e39a7SMingming Cao 	struct ext4_group_info *bd_info;
2118f6e39a7SMingming Cao 	struct super_block *bd_sb;
2128f6e39a7SMingming Cao 	__u16 bd_blkbits;
2138f6e39a7SMingming Cao 	ext4_group_t bd_group;
2148f6e39a7SMingming Cao };
2158f6e39a7SMingming Cao 
216c3a326a6SAneesh Kumar K.V static inline ext4_fsblk_t ext4_grp_offs_to_block(struct super_block *sb,
2178f6e39a7SMingming Cao 					struct ext4_free_extent *fex)
2188f6e39a7SMingming Cao {
2193212a80aSTheodore Ts'o 	return ext4_group_first_block_no(sb, fex->fe_group) +
2203212a80aSTheodore Ts'o 		(fex->fe_start << EXT4_SB(sb)->s_cluster_bits);
2218f6e39a7SMingming Cao }
2220c9ec4beSDarrick J. Wong 
2230c9ec4beSDarrick J. Wong typedef int (*ext4_mballoc_query_range_fn)(
2240c9ec4beSDarrick J. Wong 	struct super_block		*sb,
2250c9ec4beSDarrick J. Wong 	ext4_group_t			agno,
2260c9ec4beSDarrick J. Wong 	ext4_grpblk_t			start,
2270c9ec4beSDarrick J. Wong 	ext4_grpblk_t			len,
2280c9ec4beSDarrick J. Wong 	void				*priv);
2290c9ec4beSDarrick J. Wong 
2300c9ec4beSDarrick J. Wong int
2310c9ec4beSDarrick J. Wong ext4_mballoc_query_range(
2320c9ec4beSDarrick J. Wong 	struct super_block		*sb,
2330c9ec4beSDarrick J. Wong 	ext4_group_t			agno,
2340c9ec4beSDarrick J. Wong 	ext4_grpblk_t			start,
2350c9ec4beSDarrick J. Wong 	ext4_grpblk_t			end,
2360c9ec4beSDarrick J. Wong 	ext4_mballoc_query_range_fn	formatter,
2370c9ec4beSDarrick J. Wong 	void				*priv);
2380c9ec4beSDarrick J. Wong 
2398f6e39a7SMingming Cao #endif
240