mballoc.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 'ext4_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/<parition>/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. * maximum length of inode prealloc list
  68. */
  69. #define MB_DEFAULT_MAX_INODE_PREALLOC 512
  70. struct ext4_free_data {
  71. /* this links the free block information from sb_info */
  72. struct list_head efd_list;
  73. /* this links the free block information from group_info */
  74. struct rb_node efd_node;
  75. /* group which free block extent belongs */
  76. ext4_group_t efd_group;
  77. /* free block extent */
  78. ext4_grpblk_t efd_start_cluster;
  79. ext4_grpblk_t efd_count;
  80. /* transaction which freed this extent */
  81. tid_t efd_tid;
  82. };
  83. struct ext4_prealloc_space {
  84. struct list_head pa_inode_list;
  85. struct list_head pa_group_list;
  86. union {
  87. struct list_head pa_tmp_list;
  88. struct rcu_head pa_rcu;
  89. } u;
  90. spinlock_t pa_lock;
  91. atomic_t pa_count;
  92. unsigned pa_deleted;
  93. ext4_fsblk_t pa_pstart; /* phys. block */
  94. ext4_lblk_t pa_lstart; /* log. block */
  95. ext4_grpblk_t pa_len; /* len of preallocated chunk */
  96. ext4_grpblk_t pa_free; /* how many blocks are free */
  97. unsigned short pa_type; /* pa type. inode or group */
  98. spinlock_t *pa_obj_lock;
  99. struct inode *pa_inode; /* hack, for history only */
  100. };
  101. enum {
  102. MB_INODE_PA = 0,
  103. MB_GROUP_PA = 1
  104. };
  105. struct ext4_free_extent {
  106. ext4_lblk_t fe_logical;
  107. ext4_grpblk_t fe_start; /* In cluster units */
  108. ext4_group_t fe_group;
  109. ext4_grpblk_t fe_len; /* In cluster units */
  110. };
  111. /*
  112. * Locality group:
  113. * we try to group all related changes together
  114. * so that writeback can flush/allocate them together as well
  115. * Size of lg_prealloc_list hash is determined by MB_DEFAULT_GROUP_PREALLOC
  116. * (512). We store prealloc space into the hash based on the pa_free blocks
  117. * order value.ie, fls(pa_free)-1;
  118. */
  119. #define PREALLOC_TB_SIZE 10
  120. struct ext4_locality_group {
  121. /* for allocator */
  122. /* to serialize allocates */
  123. struct mutex lg_mutex;
  124. /* list of preallocations */
  125. struct list_head lg_prealloc_list[PREALLOC_TB_SIZE];
  126. spinlock_t lg_prealloc_lock;
  127. };
  128. struct ext4_allocation_context {
  129. struct inode *ac_inode;
  130. struct super_block *ac_sb;
  131. /* original request */
  132. struct ext4_free_extent ac_o_ex;
  133. /* goal request (normalized ac_o_ex) */
  134. struct ext4_free_extent ac_g_ex;
  135. /* the best found extent */
  136. struct ext4_free_extent ac_b_ex;
  137. /* copy of the best found extent taken before preallocation efforts */
  138. struct ext4_free_extent ac_f_ex;
  139. __u16 ac_groups_scanned;
  140. __u16 ac_found;
  141. __u16 ac_tail;
  142. __u16 ac_buddy;
  143. __u16 ac_flags; /* allocation hints */
  144. __u8 ac_status;
  145. __u8 ac_criteria;
  146. __u8 ac_2order; /* if request is to allocate 2^N blocks and
  147. * N > 0, the field stores N, otherwise 0 */
  148. __u8 ac_op; /* operation, for history only */
  149. struct page *ac_bitmap_page;
  150. struct page *ac_buddy_page;
  151. struct ext4_prealloc_space *ac_pa;
  152. struct ext4_locality_group *ac_lg;
  153. };
  154. #define AC_STATUS_CONTINUE 1
  155. #define AC_STATUS_FOUND 2
  156. #define AC_STATUS_BREAK 3
  157. struct ext4_buddy {
  158. struct page *bd_buddy_page;
  159. void *bd_buddy;
  160. struct page *bd_bitmap_page;
  161. void *bd_bitmap;
  162. struct ext4_group_info *bd_info;
  163. struct super_block *bd_sb;
  164. __u16 bd_blkbits;
  165. ext4_group_t bd_group;
  166. };
  167. static inline ext4_fsblk_t ext4_grp_offs_to_block(struct super_block *sb,
  168. struct ext4_free_extent *fex)
  169. {
  170. return ext4_group_first_block_no(sb, fex->fe_group) +
  171. (fex->fe_start << EXT4_SB(sb)->s_cluster_bits);
  172. }
  173. typedef int (*ext4_mballoc_query_range_fn)(
  174. struct super_block *sb,
  175. ext4_group_t agno,
  176. ext4_grpblk_t start,
  177. ext4_grpblk_t len,
  178. void *priv);
  179. int
  180. ext4_mballoc_query_range(
  181. struct super_block *sb,
  182. ext4_group_t agno,
  183. ext4_grpblk_t start,
  184. ext4_grpblk_t end,
  185. ext4_mballoc_query_range_fn formatter,
  186. void *priv);
  187. #endif