blob: 56938532b4ce258e210178d2406e187eec5ef8cc [file] [log] [blame]
Theodore Ts'of5166762017-12-17 22:00:59 -05001// SPDX-License-Identifier: GPL-2.0
Mingming Cao8f6e39a2008-04-29 22:01:31 -04002/*
3 * fs/ext4/mballoc.h
4 *
5 * Written by: Alex Tomas <alex@clusterfs.com>
6 *
7 */
8#ifndef _EXT4_MBALLOC_H
9#define _EXT4_MBALLOC_H
10
11#include <linux/time.h>
12#include <linux/fs.h>
13#include <linux/namei.h>
14#include <linux/quotaops.h>
15#include <linux/buffer_head.h>
16#include <linux/module.h>
17#include <linux/swap.h>
18#include <linux/proc_fs.h>
19#include <linux/pagemap.h>
20#include <linux/seq_file.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/*
Ritesh Harjanid3df1452020-05-10 11:54:54 +053027 * mb_debug() dynamic printk msgs could be used to debug mballoc code.
Mingming Cao8f6e39a2008-04-29 22:01:31 -040028 */
Theodore Ts'o6ba495e2009-09-18 13:38:55 -040029#ifdef CONFIG_EXT4_DEBUG
Ritesh Harjanid3df1452020-05-10 11:54:54 +053030#define mb_debug(sb, fmt, ...) \
31 pr_debug("[%s/%d] EXT4-fs (%s): (%s, %d): %s: " fmt, \
32 current->comm, task_pid_nr(current), sb->s_id, \
33 __FILE__, __LINE__, __func__, ##__VA_ARGS__)
Mingming Cao8f6e39a2008-04-29 22:01:31 -040034#else
Ritesh Harjanid3df1452020-05-10 11:54:54 +053035#define mb_debug(sb, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
Mingming Cao8f6e39a2008-04-29 22:01:31 -040036#endif
37
Mingming Cao8f6e39a2008-04-29 22:01:31 -040038#define EXT4_MB_HISTORY_ALLOC 1 /* allocation */
39#define EXT4_MB_HISTORY_PREALLOC 2 /* preallocated blocks used */
Mingming Cao8f6e39a2008-04-29 22:01:31 -040040
41/*
42 * How long mballoc can look for a best extent (in found extents)
43 */
44#define MB_DEFAULT_MAX_TO_SCAN 200
45
46/*
47 * How long mballoc must look for a best extent
48 */
49#define MB_DEFAULT_MIN_TO_SCAN 10
50
51/*
Ritesh Harjani5730cce2023-05-30 18:03:41 +053052 * with 's_mb_stats' allocator will collect stats that will be
Mingming Cao8f6e39a2008-04-29 22:01:31 -040053 * shown at umount. The collecting costs though!
54 */
Theodore Ts'o90576c02009-09-29 15:51:30 -040055#define MB_DEFAULT_STATS 0
Mingming Cao8f6e39a2008-04-29 22:01:31 -040056
57/*
58 * files smaller than MB_DEFAULT_STREAM_THRESHOLD are served
59 * by the stream allocator, which purpose is to pack requests
60 * as close each to other as possible to produce smooth I/O traffic
61 * We use locality group prealloc space for stream request.
Bhaskar Chowdhury3088e5a2021-03-27 16:00:05 +053062 * We can tune the same via /proc/fs/ext4/<partition>/stream_req
Mingming Cao8f6e39a2008-04-29 22:01:31 -040063 */
64#define MB_DEFAULT_STREAM_THRESHOLD 16 /* 64K */
65
66/*
67 * for which requests use 2^N search using buddies
68 */
69#define MB_DEFAULT_ORDER2_REQS 2
70
71/*
72 * default group prealloc size 512 blocks
73 */
74#define MB_DEFAULT_GROUP_PREALLOC 512
75
brookxu27bc4462020-08-17 15:36:15 +080076/*
Harshad Shirwadkar196e4022021-04-01 10:21:27 -070077 * Number of groups to search linearly before performing group scanning
78 * optimization.
79 */
80#define MB_DEFAULT_LINEAR_LIMIT 4
81
82/*
83 * Minimum number of groups that should be present in the file system to perform
84 * group scanning optimizations.
85 */
86#define MB_DEFAULT_LINEAR_SCAN_THRESHOLD 16
87
88/*
Ojaswin Mujoof52f3d22023-05-30 18:03:50 +053089 * The maximum order upto which CR_BEST_AVAIL_LEN can trim a particular
90 * allocation request. Example, if we have an order 7 request and max trim order
91 * of 3, we can trim this request upto order 4.
Ojaswin Mujoo7e170922023-05-30 18:03:49 +053092 */
Ojaswin Mujoof52f3d22023-05-30 18:03:50 +053093#define MB_DEFAULT_BEST_AVAIL_TRIM_ORDER 3
Ojaswin Mujoo7e170922023-05-30 18:03:49 +053094
95/*
Harshad Shirwadkar4b68f6d2021-04-01 10:21:26 -070096 * Number of valid buddy orders
97 */
98#define MB_NUM_ORDERS(sb) ((sb)->s_blocksize_bits + 2)
99
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -0400100struct ext4_free_data {
Daeho Jeonga0154342017-06-22 23:54:33 -0400101 /* this links the free block information from sb_info */
102 struct list_head efd_list;
Bobi Jam18aadd42012-02-20 17:53:02 -0500103
104 /* this links the free block information from group_info */
105 struct rb_node efd_node;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -0400106
107 /* group which free block extent belongs */
Bobi Jam18aadd42012-02-20 17:53:02 -0500108 ext4_group_t efd_group;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -0400109
110 /* free block extent */
Bobi Jam18aadd42012-02-20 17:53:02 -0500111 ext4_grpblk_t efd_start_cluster;
112 ext4_grpblk_t efd_count;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -0400113
114 /* transaction which freed this extent */
Bobi Jam18aadd42012-02-20 17:53:02 -0500115 tid_t efd_tid;
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400116};
117
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400118struct ext4_prealloc_space {
Ojaswin Mujooa8e38fd2023-03-25 13:43:40 +0530119 union {
Ojaswin Mujoo38727782023-03-25 13:43:41 +0530120 struct rb_node inode_node; /* for inode PA rbtree */
Ojaswin Mujooa8e38fd2023-03-25 13:43:40 +0530121 struct list_head lg_list; /* for lg PAs */
122 } pa_node;
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400123 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 */
Eric Sandeena36b4492009-08-25 22:36:45 -0400133 ext4_grpblk_t pa_len; /* len of preallocated chunk */
134 ext4_grpblk_t pa_free; /* how many blocks are free */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -0400135 unsigned short pa_type; /* pa type. inode or group */
Ojaswin Mujooa8e38fd2023-03-25 13:43:40 +0530136 union {
Ojaswin Mujoo38727782023-03-25 13:43:41 +0530137 rwlock_t *inode_lock; /* locks the rbtree holding this PA */
Ojaswin Mujooa8e38fd2023-03-25 13:43:40 +0530138 spinlock_t *lg_lock; /* locks the lg list holding this PA */
139 } pa_node_lock;
Ojaswin Mujoo38727782023-03-25 13:43:41 +0530140 struct inode *pa_inode; /* used to get the inode during group discard */
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400141};
142
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -0400143enum {
144 MB_INODE_PA = 0,
145 MB_GROUP_PA = 1
146};
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400147
148struct ext4_free_extent {
149 ext4_lblk_t fe_logical;
Theodore Ts'o53accfa2011-09-09 18:48:51 -0400150 ext4_grpblk_t fe_start; /* In cluster units */
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400151 ext4_group_t fe_group;
Theodore Ts'o53accfa2011-09-09 18:48:51 -0400152 ext4_grpblk_t fe_len; /* In cluster units */
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400153};
154
155/*
156 * Locality group:
157 * we try to group all related changes together
158 * so that writeback can flush/allocate them together as well
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -0400159 * Size of lg_prealloc_list hash is determined by MB_DEFAULT_GROUP_PREALLOC
160 * (512). We store prealloc space into the hash based on the pa_free blocks
161 * order value.ie, fls(pa_free)-1;
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400162 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -0400163#define PREALLOC_TB_SIZE 10
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400164struct ext4_locality_group {
165 /* for allocator */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -0400166 /* to serialize allocates */
167 struct mutex lg_mutex;
168 /* list of preallocations */
169 struct list_head lg_prealloc_list[PREALLOC_TB_SIZE];
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400170 spinlock_t lg_prealloc_lock;
171};
172
173struct ext4_allocation_context {
174 struct inode *ac_inode;
175 struct super_block *ac_sb;
176
177 /* original request */
178 struct ext4_free_extent ac_o_ex;
179
Coly Li58696f32011-02-24 14:10:00 -0500180 /* goal request (normalized ac_o_ex) */
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400181 struct ext4_free_extent ac_g_ex;
182
183 /* the best found extent */
184 struct ext4_free_extent ac_b_ex;
185
Robin Dongff3fc172011-10-31 18:55:50 -0400186 /* copy of the best found extent taken before preallocation efforts */
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400187 struct ext4_free_extent ac_f_ex;
188
Ojaswin Mujoo7e170922023-05-30 18:03:49 +0530189 /*
190 * goal len can change in CR1.5, so save the original len. This is
191 * used while adjusting the PA window and for accounting.
192 */
193 ext4_grpblk_t ac_orig_goal_len;
194
Harshad Shirwadkar196e4022021-04-01 10:21:27 -0700195 __u32 ac_flags; /* allocation hints */
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400196 __u16 ac_groups_scanned;
Harshad Shirwadkar196e4022021-04-01 10:21:27 -0700197 __u16 ac_groups_linear_remaining;
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400198 __u16 ac_found;
Ojaswin Mujoofdd9a002023-05-30 18:03:43 +0530199 __u16 ac_cX_found[EXT4_MB_NUM_CRS];
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400200 __u16 ac_tail;
201 __u16 ac_buddy;
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400202 __u8 ac_status;
203 __u8 ac_criteria;
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400204 __u8 ac_2order; /* if request is to allocate 2^N blocks and
205 * N > 0, the field stores N, otherwise 0 */
206 __u8 ac_op; /* operation, for history only */
207 struct page *ac_bitmap_page;
208 struct page *ac_buddy_page;
209 struct ext4_prealloc_space *ac_pa;
210 struct ext4_locality_group *ac_lg;
211};
212
213#define AC_STATUS_CONTINUE 1
214#define AC_STATUS_FOUND 2
215#define AC_STATUS_BREAK 3
216
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400217struct ext4_buddy {
218 struct page *bd_buddy_page;
219 void *bd_buddy;
220 struct page *bd_bitmap_page;
221 void *bd_bitmap;
222 struct ext4_group_info *bd_info;
223 struct super_block *bd_sb;
224 __u16 bd_blkbits;
225 ext4_group_t bd_group;
226};
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400227
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500228static inline ext4_fsblk_t ext4_grp_offs_to_block(struct super_block *sb,
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400229 struct ext4_free_extent *fex)
230{
Theodore Ts'o3212a802011-09-09 18:46:51 -0400231 return ext4_group_first_block_no(sb, fex->fe_group) +
232 (fex->fe_start << EXT4_SB(sb)->s_cluster_bits);
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400233}
Darrick J. Wong0c9ec4b2017-04-30 00:36:53 -0400234
Baokun Li43bbddc2023-07-24 20:10:57 +0800235static inline loff_t extent_logical_end(struct ext4_sb_info *sbi,
236 struct ext4_free_extent *fex)
237{
238 /* Use loff_t to avoid end exceeding ext4_lblk_t max. */
239 return (loff_t)fex->fe_logical + EXT4_C2B(sbi, fex->fe_len);
240}
241
242static inline loff_t pa_logical_end(struct ext4_sb_info *sbi,
243 struct ext4_prealloc_space *pa)
244{
245 /* Use loff_t to avoid end exceeding ext4_lblk_t max. */
246 return (loff_t)pa->pa_lstart + EXT4_C2B(sbi, pa->pa_len);
247}
248
Darrick J. Wong0c9ec4b2017-04-30 00:36:53 -0400249typedef int (*ext4_mballoc_query_range_fn)(
250 struct super_block *sb,
251 ext4_group_t agno,
252 ext4_grpblk_t start,
253 ext4_grpblk_t len,
254 void *priv);
255
256int
257ext4_mballoc_query_range(
258 struct super_block *sb,
259 ext4_group_t agno,
260 ext4_grpblk_t start,
261 ext4_grpblk_t end,
262 ext4_mballoc_query_range_fn formatter,
263 void *priv);
264
Mingming Cao8f6e39a2008-04-29 22:01:31 -0400265#endif