mballoc.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. struct ext4_free_data {
  89. /* this links the free block information from group_info */
  90. struct rb_node node;
  91. /* this links the free block information from ext4_sb_info */
  92. struct list_head list;
  93. /* group which free block extent belongs */
  94. ext4_group_t group;
  95. /* free block extent */
  96. ext4_grpblk_t start_blk;
  97. ext4_grpblk_t count;
  98. /* transaction which freed this extent */
  99. tid_t t_tid;
  100. };
  101. struct ext4_prealloc_space {
  102. struct list_head pa_inode_list;
  103. struct list_head pa_group_list;
  104. union {
  105. struct list_head pa_tmp_list;
  106. struct rcu_head pa_rcu;
  107. } u;
  108. spinlock_t pa_lock;
  109. atomic_t pa_count;
  110. unsigned pa_deleted;
  111. ext4_fsblk_t pa_pstart; /* phys. block */
  112. ext4_lblk_t pa_lstart; /* log. block */
  113. unsigned short pa_len; /* len of preallocated chunk */
  114. unsigned short pa_free; /* how many blocks are free */
  115. unsigned short pa_type; /* pa type. inode or group */
  116. spinlock_t *pa_obj_lock;
  117. struct inode *pa_inode; /* hack, for history only */
  118. };
  119. enum {
  120. MB_INODE_PA = 0,
  121. MB_GROUP_PA = 1
  122. };
  123. struct ext4_free_extent {
  124. ext4_lblk_t fe_logical;
  125. ext4_grpblk_t fe_start;
  126. ext4_group_t fe_group;
  127. int fe_len;
  128. };
  129. /*
  130. * Locality group:
  131. * we try to group all related changes together
  132. * so that writeback can flush/allocate them together as well
  133. * Size of lg_prealloc_list hash is determined by MB_DEFAULT_GROUP_PREALLOC
  134. * (512). We store prealloc space into the hash based on the pa_free blocks
  135. * order value.ie, fls(pa_free)-1;
  136. */
  137. #define PREALLOC_TB_SIZE 10
  138. struct ext4_locality_group {
  139. /* for allocator */
  140. /* to serialize allocates */
  141. struct mutex lg_mutex;
  142. /* list of preallocations */
  143. struct list_head lg_prealloc_list[PREALLOC_TB_SIZE];
  144. spinlock_t lg_prealloc_lock;
  145. };
  146. struct ext4_allocation_context {
  147. struct inode *ac_inode;
  148. struct super_block *ac_sb;
  149. /* original request */
  150. struct ext4_free_extent ac_o_ex;
  151. /* goal request (after normalization) */
  152. struct ext4_free_extent ac_g_ex;
  153. /* the best found extent */
  154. struct ext4_free_extent ac_b_ex;
  155. /* copy of the bext found extent taken before preallocation efforts */
  156. struct ext4_free_extent ac_f_ex;
  157. /* number of iterations done. we have to track to limit searching */
  158. unsigned long ac_ex_scanned;
  159. __u16 ac_groups_scanned;
  160. __u16 ac_found;
  161. __u16 ac_tail;
  162. __u16 ac_buddy;
  163. __u16 ac_flags; /* allocation hints */
  164. __u8 ac_status;
  165. __u8 ac_criteria;
  166. __u8 ac_repeats;
  167. __u8 ac_2order; /* if request is to allocate 2^N blocks and
  168. * N > 0, the field stores N, otherwise 0 */
  169. __u8 ac_op; /* operation, for history only */
  170. struct page *ac_bitmap_page;
  171. struct page *ac_buddy_page;
  172. /*
  173. * pointer to the held semaphore upon successful
  174. * block allocation
  175. */
  176. struct rw_semaphore *alloc_semp;
  177. struct ext4_prealloc_space *ac_pa;
  178. struct ext4_locality_group *ac_lg;
  179. };
  180. #define AC_STATUS_CONTINUE 1
  181. #define AC_STATUS_FOUND 2
  182. #define AC_STATUS_BREAK 3
  183. struct ext4_mb_history {
  184. struct ext4_free_extent orig; /* orig allocation */
  185. struct ext4_free_extent goal; /* goal allocation */
  186. struct ext4_free_extent result; /* result allocation */
  187. unsigned pid;
  188. unsigned ino;
  189. __u16 found; /* how many extents have been found */
  190. __u16 groups; /* how many groups have been scanned */
  191. __u16 tail; /* what tail broke some buddy */
  192. __u16 buddy; /* buddy the tail ^^^ broke */
  193. __u16 flags;
  194. __u8 cr:3; /* which phase the result extent was found at */
  195. __u8 op:4;
  196. __u8 merged:1;
  197. };
  198. struct ext4_buddy {
  199. struct page *bd_buddy_page;
  200. void *bd_buddy;
  201. struct page *bd_bitmap_page;
  202. void *bd_bitmap;
  203. struct ext4_group_info *bd_info;
  204. struct super_block *bd_sb;
  205. __u16 bd_blkbits;
  206. ext4_group_t bd_group;
  207. struct rw_semaphore *alloc_semp;
  208. };
  209. #define EXT4_MB_BITMAP(e4b) ((e4b)->bd_bitmap)
  210. #define EXT4_MB_BUDDY(e4b) ((e4b)->bd_buddy)
  211. #ifndef EXT4_MB_HISTORY
  212. static inline void ext4_mb_store_history(struct ext4_allocation_context *ac)
  213. {
  214. return;
  215. }
  216. #endif
  217. #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
  218. static inline ext4_fsblk_t ext4_grp_offs_to_block(struct super_block *sb,
  219. struct ext4_free_extent *fex)
  220. {
  221. ext4_fsblk_t block;
  222. block = (ext4_fsblk_t) fex->fe_group * EXT4_BLOCKS_PER_GROUP(sb)
  223. + fex->fe_start
  224. + le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
  225. return block;
  226. }
  227. #endif