mballoc.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 "ext4_jbd2.h"
  21. #include "ext4.h"
  22. #include "group.h"
  23. /*
  24. * with AGGRESSIVE_CHECK allocator runs consistency checks over
  25. * structures. these checks slow things down a lot
  26. */
  27. #define AGGRESSIVE_CHECK__
  28. /*
  29. * with DOUBLE_CHECK defined mballoc creates persistent in-core
  30. * bitmaps, maintains and uses them to check for double allocations
  31. */
  32. #define DOUBLE_CHECK__
  33. /*
  34. */
  35. #define MB_DEBUG__
  36. #ifdef MB_DEBUG
  37. #define mb_debug(fmt, a...) printk(fmt, ##a)
  38. #else
  39. #define mb_debug(fmt, a...)
  40. #endif
  41. /*
  42. * with EXT4_MB_HISTORY mballoc stores last N allocations in memory
  43. * and you can monitor it in /proc/fs/ext4/<dev>/mb_history
  44. */
  45. #define EXT4_MB_HISTORY
  46. #define EXT4_MB_HISTORY_ALLOC 1 /* allocation */
  47. #define EXT4_MB_HISTORY_PREALLOC 2 /* preallocated blocks used */
  48. #define EXT4_MB_HISTORY_DISCARD 4 /* preallocation discarded */
  49. #define EXT4_MB_HISTORY_FREE 8 /* free */
  50. #define EXT4_MB_HISTORY_DEFAULT (EXT4_MB_HISTORY_ALLOC | \
  51. EXT4_MB_HISTORY_PREALLOC)
  52. /*
  53. * How long mballoc can look for a best extent (in found extents)
  54. */
  55. #define MB_DEFAULT_MAX_TO_SCAN 200
  56. /*
  57. * How long mballoc must look for a best extent
  58. */
  59. #define MB_DEFAULT_MIN_TO_SCAN 10
  60. /*
  61. * How many groups mballoc will scan looking for the best chunk
  62. */
  63. #define MB_DEFAULT_MAX_GROUPS_TO_SCAN 5
  64. /*
  65. * with 'ext4_mb_stats' allocator will collect stats that will be
  66. * shown at umount. The collecting costs though!
  67. */
  68. #define MB_DEFAULT_STATS 1
  69. /*
  70. * files smaller than MB_DEFAULT_STREAM_THRESHOLD are served
  71. * by the stream allocator, which purpose is to pack requests
  72. * as close each to other as possible to produce smooth I/O traffic
  73. * We use locality group prealloc space for stream request.
  74. * We can tune the same via /proc/fs/ext4/<parition>/stream_req
  75. */
  76. #define MB_DEFAULT_STREAM_THRESHOLD 16 /* 64K */
  77. /*
  78. * for which requests use 2^N search using buddies
  79. */
  80. #define MB_DEFAULT_ORDER2_REQS 2
  81. /*
  82. * default group prealloc size 512 blocks
  83. */
  84. #define MB_DEFAULT_GROUP_PREALLOC 512
  85. static struct kmem_cache *ext4_pspace_cachep;
  86. static struct kmem_cache *ext4_ac_cachep;
  87. #ifdef EXT4_BB_MAX_BLOCKS
  88. #undef EXT4_BB_MAX_BLOCKS
  89. #endif
  90. #define EXT4_BB_MAX_BLOCKS 30
  91. struct ext4_free_metadata {
  92. ext4_group_t group;
  93. unsigned short num;
  94. ext4_grpblk_t blocks[EXT4_BB_MAX_BLOCKS];
  95. struct list_head list;
  96. };
  97. struct ext4_group_info {
  98. unsigned long bb_state;
  99. unsigned long bb_tid;
  100. struct ext4_free_metadata *bb_md_cur;
  101. unsigned short bb_first_free;
  102. unsigned short bb_free;
  103. unsigned short bb_fragments;
  104. struct list_head bb_prealloc_list;
  105. #ifdef DOUBLE_CHECK
  106. void *bb_bitmap;
  107. #endif
  108. unsigned short bb_counters[];
  109. };
  110. #define EXT4_GROUP_INFO_NEED_INIT_BIT 0
  111. #define EXT4_GROUP_INFO_LOCKED_BIT 1
  112. #define EXT4_MB_GRP_NEED_INIT(grp) \
  113. (test_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &((grp)->bb_state)))
  114. struct ext4_prealloc_space {
  115. struct list_head pa_inode_list;
  116. struct list_head pa_group_list;
  117. union {
  118. struct list_head pa_tmp_list;
  119. struct rcu_head pa_rcu;
  120. } u;
  121. spinlock_t pa_lock;
  122. atomic_t pa_count;
  123. unsigned pa_deleted;
  124. ext4_fsblk_t pa_pstart; /* phys. block */
  125. ext4_lblk_t pa_lstart; /* log. block */
  126. unsigned short pa_len; /* len of preallocated chunk */
  127. unsigned short pa_free; /* how many blocks are free */
  128. unsigned short pa_linear; /* consumed in one direction
  129. * strictly, for grp prealloc */
  130. spinlock_t *pa_obj_lock;
  131. struct inode *pa_inode; /* hack, for history only */
  132. };
  133. struct ext4_free_extent {
  134. ext4_lblk_t fe_logical;
  135. ext4_grpblk_t fe_start;
  136. ext4_group_t fe_group;
  137. int fe_len;
  138. };
  139. /*
  140. * Locality group:
  141. * we try to group all related changes together
  142. * so that writeback can flush/allocate them together as well
  143. */
  144. struct ext4_locality_group {
  145. /* for allocator */
  146. struct mutex lg_mutex; /* to serialize allocates */
  147. struct list_head lg_prealloc_list;/* list of preallocations */
  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 (after normalization) */
  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 bext found extent taken before preallocation efforts */
  160. struct ext4_free_extent ac_f_ex;
  161. /* number of iterations done. we have to track to limit searching */
  162. unsigned long ac_ex_scanned;
  163. __u16 ac_groups_scanned;
  164. __u16 ac_found;
  165. __u16 ac_tail;
  166. __u16 ac_buddy;
  167. __u16 ac_flags; /* allocation hints */
  168. __u8 ac_status;
  169. __u8 ac_criteria;
  170. __u8 ac_repeats;
  171. __u8 ac_2order; /* if request is to allocate 2^N blocks and
  172. * N > 0, the field stores N, otherwise 0 */
  173. __u8 ac_op; /* operation, for history only */
  174. struct page *ac_bitmap_page;
  175. struct page *ac_buddy_page;
  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. };
  207. #define EXT4_MB_BITMAP(e4b) ((e4b)->bd_bitmap)
  208. #define EXT4_MB_BUDDY(e4b) ((e4b)->bd_buddy)
  209. #ifndef EXT4_MB_HISTORY
  210. static inline void ext4_mb_store_history(struct ext4_allocation_context *ac)
  211. {
  212. return;
  213. }
  214. #else
  215. static void ext4_mb_store_history(struct ext4_allocation_context *ac);
  216. #endif
  217. #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
  218. static struct proc_dir_entry *proc_root_ext4;
  219. struct buffer_head *read_block_bitmap(struct super_block *, ext4_group_t);
  220. static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
  221. ext4_group_t group);
  222. static void ext4_mb_poll_new_transaction(struct super_block *, handle_t *);
  223. static void ext4_mb_free_committed_blocks(struct super_block *);
  224. static void ext4_mb_return_to_preallocation(struct inode *inode,
  225. struct ext4_buddy *e4b, sector_t block,
  226. int count);
  227. static void ext4_mb_put_pa(struct ext4_allocation_context *,
  228. struct super_block *, struct ext4_prealloc_space *pa);
  229. static int ext4_mb_init_per_dev_proc(struct super_block *sb);
  230. static int ext4_mb_destroy_per_dev_proc(struct super_block *sb);
  231. static inline void ext4_lock_group(struct super_block *sb, ext4_group_t group)
  232. {
  233. struct ext4_group_info *grinfo = ext4_get_group_info(sb, group);
  234. bit_spin_lock(EXT4_GROUP_INFO_LOCKED_BIT, &(grinfo->bb_state));
  235. }
  236. static inline void ext4_unlock_group(struct super_block *sb,
  237. ext4_group_t group)
  238. {
  239. struct ext4_group_info *grinfo = ext4_get_group_info(sb, group);
  240. bit_spin_unlock(EXT4_GROUP_INFO_LOCKED_BIT, &(grinfo->bb_state));
  241. }
  242. static inline int ext4_is_group_locked(struct super_block *sb,
  243. ext4_group_t group)
  244. {
  245. struct ext4_group_info *grinfo = ext4_get_group_info(sb, group);
  246. return bit_spin_is_locked(EXT4_GROUP_INFO_LOCKED_BIT,
  247. &(grinfo->bb_state));
  248. }
  249. static ext4_fsblk_t ext4_grp_offs_to_block(struct super_block *sb,
  250. struct ext4_free_extent *fex)
  251. {
  252. ext4_fsblk_t block;
  253. block = (ext4_fsblk_t) fex->fe_group * EXT4_BLOCKS_PER_GROUP(sb)
  254. + fex->fe_start
  255. + le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
  256. return block;
  257. }
  258. #endif