mballoc.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * fs/ext4/mballoc.h
  3. *
  4. * Written by: Alex Tomas <alex@clusterfs.com>
  5. *
  6. */
  7. #ifndef _EXT4_MBALLOC_H
  8. #define _EXT4_MBALLOC_H
  9. #include <linux/time.h>
  10. #include <linux/fs.h>
  11. #include <linux/namei.h>
  12. #include <linux/quotaops.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/module.h>
  15. #include <linux/swap.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/version.h>
  20. #include <linux/blkdev.h>
  21. #include <linux/marker.h>
  22. #include <linux/mutex.h>
  23. #include "ext4_jbd2.h"
  24. #include "ext4.h"
  25. /*
  26. * with AGGRESSIVE_CHECK allocator runs consistency checks over
  27. * structures. these checks slow things down a lot
  28. */
  29. #define AGGRESSIVE_CHECK__
  30. /*
  31. * with DOUBLE_CHECK defined mballoc creates persistent in-core
  32. * bitmaps, maintains and uses them to check for double allocations
  33. */
  34. #define DOUBLE_CHECK__
  35. /*
  36. */
  37. #define MB_DEBUG__
  38. #ifdef MB_DEBUG
  39. #define mb_debug(fmt, a...) printk(fmt, ##a)
  40. #else
  41. #define mb_debug(fmt, a...)
  42. #endif
  43. /*
  44. * with EXT4_MB_HISTORY mballoc stores last N allocations in memory
  45. * and you can monitor it in /proc/fs/ext4/<dev>/mb_history
  46. */
  47. #define EXT4_MB_HISTORY
  48. #define EXT4_MB_HISTORY_ALLOC 1 /* allocation */
  49. #define EXT4_MB_HISTORY_PREALLOC 2 /* preallocated blocks used */
  50. #define EXT4_MB_HISTORY_DISCARD 4 /* preallocation discarded */
  51. #define EXT4_MB_HISTORY_FREE 8 /* free */
  52. #define EXT4_MB_HISTORY_DEFAULT (EXT4_MB_HISTORY_ALLOC | \
  53. EXT4_MB_HISTORY_PREALLOC)
  54. /*
  55. * How long mballoc can look for a best extent (in found extents)
  56. */
  57. #define MB_DEFAULT_MAX_TO_SCAN 200
  58. /*
  59. * How long mballoc must look for a best extent
  60. */
  61. #define MB_DEFAULT_MIN_TO_SCAN 10
  62. /*
  63. * How many groups mballoc will scan looking for the best chunk
  64. */
  65. #define MB_DEFAULT_MAX_GROUPS_TO_SCAN 5
  66. /*
  67. * with 'ext4_mb_stats' allocator will collect stats that will be
  68. * shown at umount. The collecting costs though!
  69. */
  70. #define MB_DEFAULT_STATS 1
  71. /*
  72. * files smaller than MB_DEFAULT_STREAM_THRESHOLD are served
  73. * by the stream allocator, which purpose is to pack requests
  74. * as close each to other as possible to produce smooth I/O traffic
  75. * We use locality group prealloc space for stream request.
  76. * We can tune the same via /proc/fs/ext4/<parition>/stream_req
  77. */
  78. #define MB_DEFAULT_STREAM_THRESHOLD 16 /* 64K */
  79. /*
  80. * for which requests use 2^N search using buddies
  81. */
  82. #define MB_DEFAULT_ORDER2_REQS 2
  83. /*
  84. * default group prealloc size 512 blocks
  85. */
  86. #define MB_DEFAULT_GROUP_PREALLOC 512
  87. struct ext4_free_data {
  88. /* this links the free block information from group_info */
  89. struct rb_node node;
  90. /* this links the free block information from ext4_sb_info */
  91. struct list_head list;
  92. /* group which free block extent belongs */
  93. ext4_group_t group;
  94. /* free block extent */
  95. ext4_grpblk_t start_blk;
  96. ext4_grpblk_t count;
  97. /* transaction which freed this extent */
  98. tid_t t_tid;
  99. };
  100. struct ext4_prealloc_space {
  101. struct list_head pa_inode_list;
  102. struct list_head pa_group_list;
  103. union {
  104. struct list_head pa_tmp_list;
  105. struct rcu_head pa_rcu;
  106. } u;
  107. spinlock_t pa_lock;
  108. atomic_t pa_count;
  109. unsigned pa_deleted;
  110. ext4_fsblk_t pa_pstart; /* phys. block */
  111. ext4_lblk_t pa_lstart; /* log. block */
  112. unsigned short pa_len; /* len of preallocated chunk */
  113. unsigned short pa_free; /* how many blocks are free */
  114. unsigned short pa_type; /* pa type. inode or group */
  115. spinlock_t *pa_obj_lock;
  116. struct inode *pa_inode; /* hack, for history only */
  117. };
  118. enum {
  119. MB_INODE_PA = 0,
  120. MB_GROUP_PA = 1
  121. };
  122. struct ext4_free_extent {
  123. ext4_lblk_t fe_logical;
  124. ext4_grpblk_t fe_start;
  125. ext4_group_t fe_group;
  126. int fe_len;
  127. };
  128. /*
  129. * Locality group:
  130. * we try to group all related changes together
  131. * so that writeback can flush/allocate them together as well
  132. * Size of lg_prealloc_list hash is determined by MB_DEFAULT_GROUP_PREALLOC
  133. * (512). We store prealloc space into the hash based on the pa_free blocks
  134. * order value.ie, fls(pa_free)-1;
  135. */
  136. #define PREALLOC_TB_SIZE 10
  137. struct ext4_locality_group {
  138. /* for allocator */
  139. /* to serialize allocates */
  140. struct mutex lg_mutex;
  141. /* list of preallocations */
  142. struct list_head lg_prealloc_list[PREALLOC_TB_SIZE];
  143. spinlock_t lg_prealloc_lock;
  144. };
  145. struct ext4_allocation_context {
  146. struct inode *ac_inode;
  147. struct super_block *ac_sb;
  148. /* original request */
  149. struct ext4_free_extent ac_o_ex;
  150. /* goal request (after normalization) */
  151. struct ext4_free_extent ac_g_ex;
  152. /* the best found extent */
  153. struct ext4_free_extent ac_b_ex;
  154. /* copy of the bext found extent taken before preallocation efforts */
  155. struct ext4_free_extent ac_f_ex;
  156. /* number of iterations done. we have to track to limit searching */
  157. unsigned long ac_ex_scanned;
  158. __u16 ac_groups_scanned;
  159. __u16 ac_found;
  160. __u16 ac_tail;
  161. __u16 ac_buddy;
  162. __u16 ac_flags; /* allocation hints */
  163. __u8 ac_status;
  164. __u8 ac_criteria;
  165. __u8 ac_repeats;
  166. __u8 ac_2order; /* if request is to allocate 2^N blocks and
  167. * N > 0, the field stores N, otherwise 0 */
  168. __u8 ac_op; /* operation, for history only */
  169. struct page *ac_bitmap_page;
  170. struct page *ac_buddy_page;
  171. /*
  172. * pointer to the held semaphore upon successful
  173. * block allocation
  174. */
  175. struct rw_semaphore *alloc_semp;
  176. struct ext4_prealloc_space *ac_pa;
  177. struct ext4_locality_group *ac_lg;
  178. };
  179. #define AC_STATUS_CONTINUE 1
  180. #define AC_STATUS_FOUND 2
  181. #define AC_STATUS_BREAK 3
  182. struct ext4_mb_history {
  183. struct ext4_free_extent orig; /* orig allocation */
  184. struct ext4_free_extent goal; /* goal allocation */
  185. struct ext4_free_extent result; /* result allocation */
  186. unsigned pid;
  187. unsigned ino;
  188. __u16 found; /* how many extents have been found */
  189. __u16 groups; /* how many groups have been scanned */
  190. __u16 tail; /* what tail broke some buddy */
  191. __u16 buddy; /* buddy the tail ^^^ broke */
  192. __u16 flags;
  193. __u8 cr:3; /* which phase the result extent was found at */
  194. __u8 op:4;
  195. __u8 merged:1;
  196. };
  197. struct ext4_buddy {
  198. struct page *bd_buddy_page;
  199. void *bd_buddy;
  200. struct page *bd_bitmap_page;
  201. void *bd_bitmap;
  202. struct ext4_group_info *bd_info;
  203. struct super_block *bd_sb;
  204. __u16 bd_blkbits;
  205. ext4_group_t bd_group;
  206. struct rw_semaphore *alloc_semp;
  207. };
  208. #define EXT4_MB_BITMAP(e4b) ((e4b)->bd_bitmap)
  209. #define EXT4_MB_BUDDY(e4b) ((e4b)->bd_buddy)
  210. #ifndef EXT4_MB_HISTORY
  211. static inline void ext4_mb_store_history(struct ext4_allocation_context *ac)
  212. {
  213. return;
  214. }
  215. #endif
  216. #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
  217. static inline ext4_fsblk_t ext4_grp_offs_to_block(struct super_block *sb,
  218. struct ext4_free_extent *fex)
  219. {
  220. ext4_fsblk_t block;
  221. block = (ext4_fsblk_t) fex->fe_group * EXT4_BLOCKS_PER_GROUP(sb)
  222. + fex->fe_start
  223. + le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
  224. return block;
  225. }
  226. #endif