mballoc.h 8.4 KB

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