mballoc.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 "ext4_jbd2.h"
  23. #include "ext4.h"
  24. #include "group.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. static struct kmem_cache *ext4_pspace_cachep;
  88. static struct kmem_cache *ext4_ac_cachep;
  89. static struct kmem_cache *ext4_free_ext_cachep;
  90. struct ext4_free_data {
  91. /* this links the free block information from group_info */
  92. struct rb_node node;
  93. /* this links the free block information from ext4_sb_info */
  94. struct list_head list;
  95. /* group which free block extent belongs */
  96. ext4_group_t group;
  97. /* free block extent */
  98. ext4_grpblk_t start_blk;
  99. ext4_grpblk_t count;
  100. /* transaction which freed this extent */
  101. tid_t t_tid;
  102. };
  103. struct ext4_group_info {
  104. unsigned long bb_state;
  105. struct rb_root bb_free_root;
  106. unsigned short bb_first_free;
  107. unsigned short bb_free;
  108. unsigned short bb_fragments;
  109. struct list_head bb_prealloc_list;
  110. #ifdef DOUBLE_CHECK
  111. void *bb_bitmap;
  112. #endif
  113. unsigned short bb_counters[];
  114. };
  115. #define EXT4_GROUP_INFO_NEED_INIT_BIT 0
  116. #define EXT4_GROUP_INFO_LOCKED_BIT 1
  117. #define EXT4_MB_GRP_NEED_INIT(grp) \
  118. (test_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &((grp)->bb_state)))
  119. struct ext4_prealloc_space {
  120. struct list_head pa_inode_list;
  121. struct list_head pa_group_list;
  122. union {
  123. struct list_head pa_tmp_list;
  124. struct rcu_head pa_rcu;
  125. } u;
  126. spinlock_t pa_lock;
  127. atomic_t pa_count;
  128. unsigned pa_deleted;
  129. ext4_fsblk_t pa_pstart; /* phys. block */
  130. ext4_lblk_t pa_lstart; /* log. block */
  131. unsigned short pa_len; /* len of preallocated chunk */
  132. unsigned short pa_free; /* how many blocks are free */
  133. unsigned short pa_linear; /* consumed in one direction
  134. * strictly, for grp prealloc */
  135. spinlock_t *pa_obj_lock;
  136. struct inode *pa_inode; /* hack, for history only */
  137. };
  138. struct ext4_free_extent {
  139. ext4_lblk_t fe_logical;
  140. ext4_grpblk_t fe_start;
  141. ext4_group_t fe_group;
  142. int fe_len;
  143. };
  144. /*
  145. * Locality group:
  146. * we try to group all related changes together
  147. * so that writeback can flush/allocate them together as well
  148. * Size of lg_prealloc_list hash is determined by MB_DEFAULT_GROUP_PREALLOC
  149. * (512). We store prealloc space into the hash based on the pa_free blocks
  150. * order value.ie, fls(pa_free)-1;
  151. */
  152. #define PREALLOC_TB_SIZE 10
  153. struct ext4_locality_group {
  154. /* for allocator */
  155. /* to serialize allocates */
  156. struct mutex lg_mutex;
  157. /* list of preallocations */
  158. struct list_head lg_prealloc_list[PREALLOC_TB_SIZE];
  159. spinlock_t lg_prealloc_lock;
  160. };
  161. struct ext4_allocation_context {
  162. struct inode *ac_inode;
  163. struct super_block *ac_sb;
  164. /* original request */
  165. struct ext4_free_extent ac_o_ex;
  166. /* goal request (after normalization) */
  167. struct ext4_free_extent ac_g_ex;
  168. /* the best found extent */
  169. struct ext4_free_extent ac_b_ex;
  170. /* copy of the bext found extent taken before preallocation efforts */
  171. struct ext4_free_extent ac_f_ex;
  172. /* number of iterations done. we have to track to limit searching */
  173. unsigned long ac_ex_scanned;
  174. __u16 ac_groups_scanned;
  175. __u16 ac_found;
  176. __u16 ac_tail;
  177. __u16 ac_buddy;
  178. __u16 ac_flags; /* allocation hints */
  179. __u8 ac_status;
  180. __u8 ac_criteria;
  181. __u8 ac_repeats;
  182. __u8 ac_2order; /* if request is to allocate 2^N blocks and
  183. * N > 0, the field stores N, otherwise 0 */
  184. __u8 ac_op; /* operation, for history only */
  185. struct page *ac_bitmap_page;
  186. struct page *ac_buddy_page;
  187. struct ext4_prealloc_space *ac_pa;
  188. struct ext4_locality_group *ac_lg;
  189. };
  190. #define AC_STATUS_CONTINUE 1
  191. #define AC_STATUS_FOUND 2
  192. #define AC_STATUS_BREAK 3
  193. struct ext4_mb_history {
  194. struct ext4_free_extent orig; /* orig allocation */
  195. struct ext4_free_extent goal; /* goal allocation */
  196. struct ext4_free_extent result; /* result allocation */
  197. unsigned pid;
  198. unsigned ino;
  199. __u16 found; /* how many extents have been found */
  200. __u16 groups; /* how many groups have been scanned */
  201. __u16 tail; /* what tail broke some buddy */
  202. __u16 buddy; /* buddy the tail ^^^ broke */
  203. __u16 flags;
  204. __u8 cr:3; /* which phase the result extent was found at */
  205. __u8 op:4;
  206. __u8 merged:1;
  207. };
  208. struct ext4_buddy {
  209. struct page *bd_buddy_page;
  210. void *bd_buddy;
  211. struct page *bd_bitmap_page;
  212. void *bd_bitmap;
  213. struct ext4_group_info *bd_info;
  214. struct super_block *bd_sb;
  215. __u16 bd_blkbits;
  216. ext4_group_t bd_group;
  217. };
  218. #define EXT4_MB_BITMAP(e4b) ((e4b)->bd_bitmap)
  219. #define EXT4_MB_BUDDY(e4b) ((e4b)->bd_buddy)
  220. #ifndef EXT4_MB_HISTORY
  221. static inline void ext4_mb_store_history(struct ext4_allocation_context *ac)
  222. {
  223. return;
  224. }
  225. #else
  226. static void ext4_mb_store_history(struct ext4_allocation_context *ac);
  227. #endif
  228. #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
  229. struct buffer_head *read_block_bitmap(struct super_block *, ext4_group_t);
  230. static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
  231. ext4_group_t group);
  232. static void ext4_mb_return_to_preallocation(struct inode *inode,
  233. struct ext4_buddy *e4b, sector_t block,
  234. int count);
  235. static void ext4_mb_put_pa(struct ext4_allocation_context *,
  236. struct super_block *, struct ext4_prealloc_space *pa);
  237. static int ext4_mb_init_per_dev_proc(struct super_block *sb);
  238. static int ext4_mb_destroy_per_dev_proc(struct super_block *sb);
  239. static void release_blocks_on_commit(journal_t *journal, transaction_t *txn);
  240. static inline void ext4_lock_group(struct super_block *sb, ext4_group_t group)
  241. {
  242. struct ext4_group_info *grinfo = ext4_get_group_info(sb, group);
  243. bit_spin_lock(EXT4_GROUP_INFO_LOCKED_BIT, &(grinfo->bb_state));
  244. }
  245. static inline void ext4_unlock_group(struct super_block *sb,
  246. ext4_group_t group)
  247. {
  248. struct ext4_group_info *grinfo = ext4_get_group_info(sb, group);
  249. bit_spin_unlock(EXT4_GROUP_INFO_LOCKED_BIT, &(grinfo->bb_state));
  250. }
  251. static inline int ext4_is_group_locked(struct super_block *sb,
  252. ext4_group_t group)
  253. {
  254. struct ext4_group_info *grinfo = ext4_get_group_info(sb, group);
  255. return bit_spin_is_locked(EXT4_GROUP_INFO_LOCKED_BIT,
  256. &(grinfo->bb_state));
  257. }
  258. static ext4_fsblk_t ext4_grp_offs_to_block(struct super_block *sb,
  259. struct ext4_free_extent *fex)
  260. {
  261. ext4_fsblk_t block;
  262. block = (ext4_fsblk_t) fex->fe_group * EXT4_BLOCKS_PER_GROUP(sb)
  263. + fex->fe_start
  264. + le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
  265. return block;
  266. }
  267. #endif