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 /* 89*f52f3d2bSOjaswin Mujoo * The maximum order upto which CR_BEST_AVAIL_LEN can trim a particular 90*f52f3d2bSOjaswin Mujoo * allocation request. Example, if we have an order 7 request and max trim order 91*f52f3d2bSOjaswin Mujoo * of 3, we can trim this request upto order 4. 927e170922SOjaswin Mujoo */ 93*f52f3d2bSOjaswin Mujoo #define MB_DEFAULT_BEST_AVAIL_TRIM_ORDER 3 947e170922SOjaswin Mujoo 957e170922SOjaswin Mujoo /* 964b68f6dfSHarshad Shirwadkar * Number of valid buddy orders 974b68f6dfSHarshad Shirwadkar */ 984b68f6dfSHarshad Shirwadkar #define MB_NUM_ORDERS(sb) ((sb)->s_blocksize_bits + 2) 994b68f6dfSHarshad Shirwadkar 100c894058dSAneesh Kumar K.V struct ext4_free_data { 101a0154344SDaeho Jeong /* this links the free block information from sb_info */ 102a0154344SDaeho Jeong struct list_head efd_list; 10318aadd47SBobi Jam 10418aadd47SBobi Jam /* this links the free block information from group_info */ 10518aadd47SBobi Jam struct rb_node efd_node; 106c894058dSAneesh Kumar K.V 107c894058dSAneesh Kumar K.V /* group which free block extent belongs */ 10818aadd47SBobi Jam ext4_group_t efd_group; 109c894058dSAneesh Kumar K.V 110c894058dSAneesh Kumar K.V /* free block extent */ 11118aadd47SBobi Jam ext4_grpblk_t efd_start_cluster; 11218aadd47SBobi Jam ext4_grpblk_t efd_count; 113c894058dSAneesh Kumar K.V 114c894058dSAneesh Kumar K.V /* transaction which freed this extent */ 11518aadd47SBobi Jam tid_t efd_tid; 1168f6e39a7SMingming Cao }; 1178f6e39a7SMingming Cao 1188f6e39a7SMingming Cao struct ext4_prealloc_space { 119a8e38fd3SOjaswin Mujoo union { 12038727786SOjaswin Mujoo struct rb_node inode_node; /* for inode PA rbtree */ 121a8e38fd3SOjaswin Mujoo struct list_head lg_list; /* for lg PAs */ 122a8e38fd3SOjaswin Mujoo } pa_node; 1238f6e39a7SMingming Cao struct list_head pa_group_list; 1248f6e39a7SMingming Cao union { 1258f6e39a7SMingming Cao struct list_head pa_tmp_list; 1268f6e39a7SMingming Cao struct rcu_head pa_rcu; 1278f6e39a7SMingming Cao } u; 1288f6e39a7SMingming Cao spinlock_t pa_lock; 1298f6e39a7SMingming Cao atomic_t pa_count; 1308f6e39a7SMingming Cao unsigned pa_deleted; 1318f6e39a7SMingming Cao ext4_fsblk_t pa_pstart; /* phys. block */ 1328f6e39a7SMingming Cao ext4_lblk_t pa_lstart; /* log. block */ 133a36b4498SEric Sandeen ext4_grpblk_t pa_len; /* len of preallocated chunk */ 134a36b4498SEric Sandeen ext4_grpblk_t pa_free; /* how many blocks are free */ 135cc0fb9adSAneesh Kumar K.V unsigned short pa_type; /* pa type. inode or group */ 136a8e38fd3SOjaswin Mujoo union { 13738727786SOjaswin Mujoo rwlock_t *inode_lock; /* locks the rbtree holding this PA */ 138a8e38fd3SOjaswin Mujoo spinlock_t *lg_lock; /* locks the lg list holding this PA */ 139a8e38fd3SOjaswin Mujoo } pa_node_lock; 14038727786SOjaswin Mujoo struct inode *pa_inode; /* used to get the inode during group discard */ 1418f6e39a7SMingming Cao }; 1428f6e39a7SMingming Cao 143cc0fb9adSAneesh Kumar K.V enum { 144cc0fb9adSAneesh Kumar K.V MB_INODE_PA = 0, 145cc0fb9adSAneesh Kumar K.V MB_GROUP_PA = 1 146cc0fb9adSAneesh Kumar K.V }; 1478f6e39a7SMingming Cao 1488f6e39a7SMingming Cao struct ext4_free_extent { 1498f6e39a7SMingming Cao ext4_lblk_t fe_logical; 15053accfa9STheodore Ts'o ext4_grpblk_t fe_start; /* In cluster units */ 1518f6e39a7SMingming Cao ext4_group_t fe_group; 15253accfa9STheodore Ts'o ext4_grpblk_t fe_len; /* In cluster units */ 1538f6e39a7SMingming Cao }; 1548f6e39a7SMingming Cao 1558f6e39a7SMingming Cao /* 1568f6e39a7SMingming Cao * Locality group: 1578f6e39a7SMingming Cao * we try to group all related changes together 1588f6e39a7SMingming Cao * so that writeback can flush/allocate them together as well 1596be2ded1SAneesh Kumar K.V * Size of lg_prealloc_list hash is determined by MB_DEFAULT_GROUP_PREALLOC 1606be2ded1SAneesh Kumar K.V * (512). We store prealloc space into the hash based on the pa_free blocks 1616be2ded1SAneesh Kumar K.V * order value.ie, fls(pa_free)-1; 1628f6e39a7SMingming Cao */ 1636be2ded1SAneesh Kumar K.V #define PREALLOC_TB_SIZE 10 1648f6e39a7SMingming Cao struct ext4_locality_group { 1658f6e39a7SMingming Cao /* for allocator */ 1666be2ded1SAneesh Kumar K.V /* to serialize allocates */ 1676be2ded1SAneesh Kumar K.V struct mutex lg_mutex; 1686be2ded1SAneesh Kumar K.V /* list of preallocations */ 1696be2ded1SAneesh Kumar K.V struct list_head lg_prealloc_list[PREALLOC_TB_SIZE]; 1708f6e39a7SMingming Cao spinlock_t lg_prealloc_lock; 1718f6e39a7SMingming Cao }; 1728f6e39a7SMingming Cao 1738f6e39a7SMingming Cao struct ext4_allocation_context { 1748f6e39a7SMingming Cao struct inode *ac_inode; 1758f6e39a7SMingming Cao struct super_block *ac_sb; 1768f6e39a7SMingming Cao 1778f6e39a7SMingming Cao /* original request */ 1788f6e39a7SMingming Cao struct ext4_free_extent ac_o_ex; 1798f6e39a7SMingming Cao 18058696f3aSColy Li /* goal request (normalized ac_o_ex) */ 1818f6e39a7SMingming Cao struct ext4_free_extent ac_g_ex; 1828f6e39a7SMingming Cao 1838f6e39a7SMingming Cao /* the best found extent */ 1848f6e39a7SMingming Cao struct ext4_free_extent ac_b_ex; 1858f6e39a7SMingming Cao 186ff3fc173SRobin Dong /* copy of the best found extent taken before preallocation efforts */ 1878f6e39a7SMingming Cao struct ext4_free_extent ac_f_ex; 1888f6e39a7SMingming Cao 1897e170922SOjaswin Mujoo /* 1907e170922SOjaswin Mujoo * goal len can change in CR1.5, so save the original len. This is 1917e170922SOjaswin Mujoo * used while adjusting the PA window and for accounting. 1927e170922SOjaswin Mujoo */ 1937e170922SOjaswin Mujoo ext4_grpblk_t ac_orig_goal_len; 1947e170922SOjaswin Mujoo 195196e402aSHarshad Shirwadkar __u32 ac_groups_considered; 196196e402aSHarshad Shirwadkar __u32 ac_flags; /* allocation hints */ 1978f6e39a7SMingming Cao __u16 ac_groups_scanned; 198196e402aSHarshad Shirwadkar __u16 ac_groups_linear_remaining; 1998f6e39a7SMingming Cao __u16 ac_found; 200fdd9a009SOjaswin Mujoo __u16 ac_cX_found[EXT4_MB_NUM_CRS]; 2018f6e39a7SMingming Cao __u16 ac_tail; 2028f6e39a7SMingming Cao __u16 ac_buddy; 2038f6e39a7SMingming Cao __u8 ac_status; 2048f6e39a7SMingming Cao __u8 ac_criteria; 2058f6e39a7SMingming Cao __u8 ac_2order; /* if request is to allocate 2^N blocks and 2068f6e39a7SMingming Cao * N > 0, the field stores N, otherwise 0 */ 2078f6e39a7SMingming Cao __u8 ac_op; /* operation, for history only */ 2088f6e39a7SMingming Cao struct page *ac_bitmap_page; 2098f6e39a7SMingming Cao struct page *ac_buddy_page; 2108f6e39a7SMingming Cao struct ext4_prealloc_space *ac_pa; 2118f6e39a7SMingming Cao struct ext4_locality_group *ac_lg; 2128f6e39a7SMingming Cao }; 2138f6e39a7SMingming Cao 2148f6e39a7SMingming Cao #define AC_STATUS_CONTINUE 1 2158f6e39a7SMingming Cao #define AC_STATUS_FOUND 2 2168f6e39a7SMingming Cao #define AC_STATUS_BREAK 3 2178f6e39a7SMingming Cao 2188f6e39a7SMingming Cao struct ext4_buddy { 2198f6e39a7SMingming Cao struct page *bd_buddy_page; 2208f6e39a7SMingming Cao void *bd_buddy; 2218f6e39a7SMingming Cao struct page *bd_bitmap_page; 2228f6e39a7SMingming Cao void *bd_bitmap; 2238f6e39a7SMingming Cao struct ext4_group_info *bd_info; 2248f6e39a7SMingming Cao struct super_block *bd_sb; 2258f6e39a7SMingming Cao __u16 bd_blkbits; 2268f6e39a7SMingming Cao ext4_group_t bd_group; 2278f6e39a7SMingming Cao }; 2288f6e39a7SMingming Cao 229c3a326a6SAneesh Kumar K.V static inline ext4_fsblk_t ext4_grp_offs_to_block(struct super_block *sb, 2308f6e39a7SMingming Cao struct ext4_free_extent *fex) 2318f6e39a7SMingming Cao { 2323212a80aSTheodore Ts'o return ext4_group_first_block_no(sb, fex->fe_group) + 2333212a80aSTheodore Ts'o (fex->fe_start << EXT4_SB(sb)->s_cluster_bits); 2348f6e39a7SMingming Cao } 2350c9ec4beSDarrick J. Wong 2360c9ec4beSDarrick J. Wong typedef int (*ext4_mballoc_query_range_fn)( 2370c9ec4beSDarrick J. Wong struct super_block *sb, 2380c9ec4beSDarrick J. Wong ext4_group_t agno, 2390c9ec4beSDarrick J. Wong ext4_grpblk_t start, 2400c9ec4beSDarrick J. Wong ext4_grpblk_t len, 2410c9ec4beSDarrick J. Wong void *priv); 2420c9ec4beSDarrick J. Wong 2430c9ec4beSDarrick J. Wong int 2440c9ec4beSDarrick J. Wong ext4_mballoc_query_range( 2450c9ec4beSDarrick J. Wong struct super_block *sb, 2460c9ec4beSDarrick J. Wong ext4_group_t agno, 2470c9ec4beSDarrick J. Wong ext4_grpblk_t start, 2480c9ec4beSDarrick J. Wong ext4_grpblk_t end, 2490c9ec4beSDarrick J. Wong ext4_mballoc_query_range_fn formatter, 2500c9ec4beSDarrick J. Wong void *priv); 2510c9ec4beSDarrick J. Wong 2528f6e39a7SMingming Cao #endif 253