mballoc.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  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. #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/blkdev.h>
  21. #include <linux/mutex.h>
  22. #include "ext4_jbd2.h"
  23. #include "ext4.h"
  24. /*
  25. * mb_debug() dynamic printk msgs could be used to debug mballoc code.
  26. */
  27. #ifdef CONFIG_EXT4_DEBUG
  28. #define mb_debug(sb, fmt, ...) \
  29. pr_debug("[%s/%d] EXT4-fs (%s): (%s, %d): %s: " fmt, \
  30. current->comm, task_pid_nr(current), sb->s_id, \
  31. __FILE__, __LINE__, __func__, ##__VA_ARGS__)
  32. #else
  33. #define mb_debug(sb, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
  34. #endif
  35. #define EXT4_MB_HISTORY_ALLOC 1 /* allocation */
  36. #define EXT4_MB_HISTORY_PREALLOC 2 /* preallocated blocks used */
  37. /*
  38. * How long mballoc can look for a best extent (in found extents)
  39. */
  40. #define MB_DEFAULT_MAX_TO_SCAN 200
  41. /*
  42. * How long mballoc must look for a best extent
  43. */
  44. #define MB_DEFAULT_MIN_TO_SCAN 10
  45. /*
  46. * with 's_mb_stats' allocator will collect stats that will be
  47. * shown at umount. The collecting costs though!
  48. */
  49. #define MB_DEFAULT_STATS 0
  50. /*
  51. * files smaller than MB_DEFAULT_STREAM_THRESHOLD are served
  52. * by the stream allocator, which purpose is to pack requests
  53. * as close each to other as possible to produce smooth I/O traffic
  54. * We use locality group prealloc space for stream request.
  55. * We can tune the same via /proc/fs/ext4/<partition>/stream_req
  56. */
  57. #define MB_DEFAULT_STREAM_THRESHOLD 16 /* 64K */
  58. /*
  59. * for which requests use 2^N search using buddies
  60. */
  61. #define MB_DEFAULT_ORDER2_REQS 2
  62. /*
  63. * default group prealloc size 512 blocks
  64. */
  65. #define MB_DEFAULT_GROUP_PREALLOC 512
  66. /*
  67. * Number of groups to search linearly before performing group scanning
  68. * optimization.
  69. */
  70. #define MB_DEFAULT_LINEAR_LIMIT 4
  71. /*
  72. * Minimum number of groups that should be present in the file system to perform
  73. * group scanning optimizations.
  74. */
  75. #define MB_DEFAULT_LINEAR_SCAN_THRESHOLD 16
  76. /*
  77. * The maximum order upto which CR_BEST_AVAIL_LEN can trim a particular
  78. * allocation request. Example, if we have an order 7 request and max trim order
  79. * of 3, we can trim this request upto order 4.
  80. */
  81. #define MB_DEFAULT_BEST_AVAIL_TRIM_ORDER 3
  82. /*
  83. * Number of valid buddy orders
  84. */
  85. #define MB_NUM_ORDERS(sb) ((sb)->s_blocksize_bits + 2)
  86. struct ext4_free_data {
  87. /* this links the free block information from sb_info */
  88. struct list_head efd_list;
  89. /* this links the free block information from group_info */
  90. struct rb_node efd_node;
  91. /* group which free block extent belongs */
  92. ext4_group_t efd_group;
  93. /* free block extent */
  94. ext4_grpblk_t efd_start_cluster;
  95. ext4_grpblk_t efd_count;
  96. /* transaction which freed this extent */
  97. tid_t efd_tid;
  98. };
  99. struct ext4_prealloc_space {
  100. union {
  101. struct rb_node inode_node; /* for inode PA rbtree */
  102. struct list_head lg_list; /* for lg PAs */
  103. } pa_node;
  104. struct list_head pa_group_list;
  105. union {
  106. struct list_head pa_tmp_list;
  107. struct rcu_head pa_rcu;
  108. } u;
  109. spinlock_t pa_lock;
  110. atomic_t pa_count;
  111. unsigned pa_deleted;
  112. ext4_fsblk_t pa_pstart; /* phys. block */
  113. ext4_lblk_t pa_lstart; /* log. block */
  114. ext4_grpblk_t pa_len; /* len of preallocated chunk */
  115. ext4_grpblk_t pa_free; /* how many blocks are free */
  116. unsigned short pa_type; /* pa type. inode or group */
  117. union {
  118. rwlock_t *inode_lock; /* locks the rbtree holding this PA */
  119. spinlock_t *lg_lock; /* locks the lg list holding this PA */
  120. } pa_node_lock;
  121. struct inode *pa_inode; /* used to get the inode during group discard */
  122. };
  123. enum {
  124. MB_INODE_PA = 0,
  125. MB_GROUP_PA = 1
  126. };
  127. struct ext4_free_extent {
  128. ext4_lblk_t fe_logical;
  129. ext4_grpblk_t fe_start; /* In cluster units */
  130. ext4_group_t fe_group;
  131. ext4_grpblk_t fe_len; /* In cluster units */
  132. };
  133. /*
  134. * Locality group:
  135. * we try to group all related changes together
  136. * so that writeback can flush/allocate them together as well
  137. * Size of lg_prealloc_list hash is determined by MB_DEFAULT_GROUP_PREALLOC
  138. * (512). We store prealloc space into the hash based on the pa_free blocks
  139. * order value.ie, fls(pa_free)-1;
  140. */
  141. #define PREALLOC_TB_SIZE 10
  142. struct ext4_locality_group {
  143. /* for allocator */
  144. /* to serialize allocates */
  145. struct mutex lg_mutex;
  146. /* list of preallocations */
  147. struct list_head lg_prealloc_list[PREALLOC_TB_SIZE];
  148. spinlock_t lg_prealloc_lock;
  149. };
  150. struct ext4_allocation_context {
  151. struct inode *ac_inode;
  152. struct super_block *ac_sb;
  153. /* original request */
  154. struct ext4_free_extent ac_o_ex;
  155. /* goal request (normalized ac_o_ex) */
  156. struct ext4_free_extent ac_g_ex;
  157. /* the best found extent */
  158. struct ext4_free_extent ac_b_ex;
  159. /* copy of the best found extent taken before preallocation efforts */
  160. struct ext4_free_extent ac_f_ex;
  161. /*
  162. * goal len can change in CR_BEST_AVAIL_LEN, so save the original len.
  163. * This is used while adjusting the PA window and for accounting.
  164. */
  165. ext4_grpblk_t ac_orig_goal_len;
  166. __u32 ac_flags; /* allocation hints */
  167. __u32 ac_groups_linear_remaining;
  168. __u16 ac_groups_scanned;
  169. __u16 ac_found;
  170. __u16 ac_cX_found[EXT4_MB_NUM_CRS];
  171. __u16 ac_tail;
  172. __u16 ac_buddy;
  173. __u8 ac_status;
  174. __u8 ac_criteria;
  175. __u8 ac_2order; /* if request is to allocate 2^N blocks and
  176. * N > 0, the field stores N, otherwise 0 */
  177. __u8 ac_op; /* operation, for history only */
  178. struct folio *ac_bitmap_folio;
  179. struct folio *ac_buddy_folio;
  180. struct ext4_prealloc_space *ac_pa;
  181. struct ext4_locality_group *ac_lg;
  182. };
  183. #define AC_STATUS_CONTINUE 1
  184. #define AC_STATUS_FOUND 2
  185. #define AC_STATUS_BREAK 3
  186. struct ext4_buddy {
  187. struct folio *bd_buddy_folio;
  188. void *bd_buddy;
  189. struct folio *bd_bitmap_folio;
  190. void *bd_bitmap;
  191. struct ext4_group_info *bd_info;
  192. struct super_block *bd_sb;
  193. __u16 bd_blkbits;
  194. ext4_group_t bd_group;
  195. };
  196. static inline ext4_fsblk_t ext4_grp_offs_to_block(struct super_block *sb,
  197. struct ext4_free_extent *fex)
  198. {
  199. return ext4_group_first_block_no(sb, fex->fe_group) +
  200. (fex->fe_start << EXT4_SB(sb)->s_cluster_bits);
  201. }
  202. static inline loff_t extent_logical_end(struct ext4_sb_info *sbi,
  203. struct ext4_free_extent *fex)
  204. {
  205. /* Use loff_t to avoid end exceeding ext4_lblk_t max. */
  206. return (loff_t)fex->fe_logical + EXT4_C2B(sbi, fex->fe_len);
  207. }
  208. static inline loff_t pa_logical_end(struct ext4_sb_info *sbi,
  209. struct ext4_prealloc_space *pa)
  210. {
  211. /* Use loff_t to avoid end exceeding ext4_lblk_t max. */
  212. return (loff_t)pa->pa_lstart + EXT4_C2B(sbi, pa->pa_len);
  213. }
  214. typedef int (*ext4_mballoc_query_range_fn)(
  215. struct super_block *sb,
  216. ext4_group_t agno,
  217. ext4_grpblk_t start,
  218. ext4_grpblk_t len,
  219. void *priv);
  220. int
  221. ext4_mballoc_query_range(
  222. struct super_block *sb,
  223. ext4_group_t agno,
  224. ext4_grpblk_t start,
  225. ext4_grpblk_t end,
  226. ext4_mballoc_query_range_fn meta_formatter,
  227. ext4_mballoc_query_range_fn formatter,
  228. void *priv);
  229. #endif