mballoc.h 6.8 KB

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