extent_io.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. #ifndef __EXTENTIO__
  2. #define __EXTENTIO__
  3. #include <linux/rbtree.h>
  4. /* bits for the extent state */
  5. #define EXTENT_DIRTY 1
  6. #define EXTENT_WRITEBACK (1 << 1)
  7. #define EXTENT_UPTODATE (1 << 2)
  8. #define EXTENT_LOCKED (1 << 3)
  9. #define EXTENT_NEW (1 << 4)
  10. #define EXTENT_DELALLOC (1 << 5)
  11. #define EXTENT_DEFRAG (1 << 6)
  12. #define EXTENT_DEFRAG_DONE (1 << 7)
  13. #define EXTENT_BUFFER_FILLED (1 << 8)
  14. #define EXTENT_BOUNDARY (1 << 9)
  15. #define EXTENT_NODATASUM (1 << 10)
  16. #define EXTENT_DO_ACCOUNTING (1 << 11)
  17. #define EXTENT_FIRST_DELALLOC (1 << 12)
  18. #define EXTENT_NEED_WAIT (1 << 13)
  19. #define EXTENT_DAMAGED (1 << 14)
  20. #define EXTENT_IOBITS (EXTENT_LOCKED | EXTENT_WRITEBACK)
  21. #define EXTENT_CTLBITS (EXTENT_DO_ACCOUNTING | EXTENT_FIRST_DELALLOC)
  22. /*
  23. * flags for bio submission. The high bits indicate the compression
  24. * type for this bio
  25. */
  26. #define EXTENT_BIO_COMPRESSED 1
  27. #define EXTENT_BIO_FLAG_SHIFT 16
  28. /* these are bit numbers for test/set bit */
  29. #define EXTENT_BUFFER_UPTODATE 0
  30. #define EXTENT_BUFFER_BLOCKING 1
  31. #define EXTENT_BUFFER_DIRTY 2
  32. #define EXTENT_BUFFER_CORRUPT 3
  33. #define EXTENT_BUFFER_READAHEAD 4 /* this got triggered by readahead */
  34. #define EXTENT_BUFFER_TREE_REF 5
  35. #define EXTENT_BUFFER_STALE 6
  36. #define EXTENT_BUFFER_WRITEBACK 7
  37. #define EXTENT_BUFFER_IOERR 8
  38. /* these are flags for extent_clear_unlock_delalloc */
  39. #define EXTENT_CLEAR_UNLOCK_PAGE 0x1
  40. #define EXTENT_CLEAR_UNLOCK 0x2
  41. #define EXTENT_CLEAR_DELALLOC 0x4
  42. #define EXTENT_CLEAR_DIRTY 0x8
  43. #define EXTENT_SET_WRITEBACK 0x10
  44. #define EXTENT_END_WRITEBACK 0x20
  45. #define EXTENT_SET_PRIVATE2 0x40
  46. #define EXTENT_CLEAR_ACCOUNTING 0x80
  47. /*
  48. * page->private values. Every page that is controlled by the extent
  49. * map has page->private set to one.
  50. */
  51. #define EXTENT_PAGE_PRIVATE 1
  52. #define EXTENT_PAGE_PRIVATE_FIRST_PAGE 3
  53. struct extent_state;
  54. struct btrfs_root;
  55. typedef int (extent_submit_bio_hook_t)(struct inode *inode, int rw,
  56. struct bio *bio, int mirror_num,
  57. unsigned long bio_flags, u64 bio_offset);
  58. struct extent_io_ops {
  59. int (*fill_delalloc)(struct inode *inode, struct page *locked_page,
  60. u64 start, u64 end, int *page_started,
  61. unsigned long *nr_written);
  62. int (*writepage_start_hook)(struct page *page, u64 start, u64 end);
  63. int (*writepage_io_hook)(struct page *page, u64 start, u64 end);
  64. extent_submit_bio_hook_t *submit_bio_hook;
  65. int (*merge_bio_hook)(struct page *page, unsigned long offset,
  66. size_t size, struct bio *bio,
  67. unsigned long bio_flags);
  68. int (*readpage_io_hook)(struct page *page, u64 start, u64 end);
  69. int (*readpage_io_failed_hook)(struct page *page, int failed_mirror);
  70. int (*writepage_io_failed_hook)(struct bio *bio, struct page *page,
  71. u64 start, u64 end,
  72. struct extent_state *state);
  73. int (*readpage_end_io_hook)(struct page *page, u64 start, u64 end,
  74. struct extent_state *state, int mirror);
  75. int (*writepage_end_io_hook)(struct page *page, u64 start, u64 end,
  76. struct extent_state *state, int uptodate);
  77. void (*set_bit_hook)(struct inode *inode, struct extent_state *state,
  78. int *bits);
  79. void (*clear_bit_hook)(struct inode *inode, struct extent_state *state,
  80. int *bits);
  81. void (*merge_extent_hook)(struct inode *inode,
  82. struct extent_state *new,
  83. struct extent_state *other);
  84. void (*split_extent_hook)(struct inode *inode,
  85. struct extent_state *orig, u64 split);
  86. int (*write_cache_pages_lock_hook)(struct page *page, void *data,
  87. void (*flush_fn)(void *));
  88. };
  89. struct extent_io_tree {
  90. struct rb_root state;
  91. struct radix_tree_root buffer;
  92. struct address_space *mapping;
  93. u64 dirty_bytes;
  94. int track_uptodate;
  95. spinlock_t lock;
  96. spinlock_t buffer_lock;
  97. struct extent_io_ops *ops;
  98. };
  99. struct extent_state {
  100. u64 start;
  101. u64 end; /* inclusive */
  102. struct rb_node rb_node;
  103. /* ADD NEW ELEMENTS AFTER THIS */
  104. struct extent_io_tree *tree;
  105. wait_queue_head_t wq;
  106. atomic_t refs;
  107. unsigned long state;
  108. /* for use by the FS */
  109. u64 private;
  110. struct list_head leak_list;
  111. };
  112. #define INLINE_EXTENT_BUFFER_PAGES 16
  113. #define MAX_INLINE_EXTENT_BUFFER_SIZE (INLINE_EXTENT_BUFFER_PAGES * PAGE_CACHE_SIZE)
  114. struct extent_buffer {
  115. u64 start;
  116. unsigned long len;
  117. unsigned long map_start;
  118. unsigned long map_len;
  119. unsigned long bflags;
  120. struct extent_io_tree *tree;
  121. spinlock_t refs_lock;
  122. atomic_t refs;
  123. atomic_t io_pages;
  124. int read_mirror;
  125. struct list_head leak_list;
  126. struct rcu_head rcu_head;
  127. pid_t lock_owner;
  128. /* count of read lock holders on the extent buffer */
  129. atomic_t write_locks;
  130. atomic_t read_locks;
  131. atomic_t blocking_writers;
  132. atomic_t blocking_readers;
  133. atomic_t spinning_readers;
  134. atomic_t spinning_writers;
  135. int lock_nested;
  136. /* protects write locks */
  137. rwlock_t lock;
  138. /* readers use lock_wq while they wait for the write
  139. * lock holders to unlock
  140. */
  141. wait_queue_head_t write_lock_wq;
  142. /* writers use read_lock_wq while they wait for readers
  143. * to unlock
  144. */
  145. wait_queue_head_t read_lock_wq;
  146. wait_queue_head_t lock_wq;
  147. struct page *inline_pages[INLINE_EXTENT_BUFFER_PAGES];
  148. struct page **pages;
  149. };
  150. static inline void extent_set_compress_type(unsigned long *bio_flags,
  151. int compress_type)
  152. {
  153. *bio_flags |= compress_type << EXTENT_BIO_FLAG_SHIFT;
  154. }
  155. static inline int extent_compress_type(unsigned long bio_flags)
  156. {
  157. return bio_flags >> EXTENT_BIO_FLAG_SHIFT;
  158. }
  159. struct extent_map_tree;
  160. typedef struct extent_map *(get_extent_t)(struct inode *inode,
  161. struct page *page,
  162. size_t pg_offset,
  163. u64 start, u64 len,
  164. int create);
  165. void extent_io_tree_init(struct extent_io_tree *tree,
  166. struct address_space *mapping);
  167. int try_release_extent_mapping(struct extent_map_tree *map,
  168. struct extent_io_tree *tree, struct page *page,
  169. gfp_t mask);
  170. int try_release_extent_buffer(struct page *page, gfp_t mask);
  171. int try_release_extent_state(struct extent_map_tree *map,
  172. struct extent_io_tree *tree, struct page *page,
  173. gfp_t mask);
  174. int lock_extent(struct extent_io_tree *tree, u64 start, u64 end);
  175. int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  176. int bits, struct extent_state **cached);
  177. int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end);
  178. int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
  179. struct extent_state **cached, gfp_t mask);
  180. int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end);
  181. int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
  182. get_extent_t *get_extent, int mirror_num);
  183. int __init extent_io_init(void);
  184. void extent_io_exit(void);
  185. u64 count_range_bits(struct extent_io_tree *tree,
  186. u64 *start, u64 search_end,
  187. u64 max_bytes, unsigned long bits, int contig);
  188. void free_extent_state(struct extent_state *state);
  189. int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
  190. int bits, int filled, struct extent_state *cached_state);
  191. int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  192. int bits, gfp_t mask);
  193. int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  194. int bits, int wake, int delete, struct extent_state **cached,
  195. gfp_t mask);
  196. int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  197. int bits, gfp_t mask);
  198. int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  199. int bits, u64 *failed_start,
  200. struct extent_state **cached_state, gfp_t mask);
  201. int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
  202. struct extent_state **cached_state, gfp_t mask);
  203. int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
  204. gfp_t mask);
  205. int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
  206. gfp_t mask);
  207. int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
  208. gfp_t mask);
  209. int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  210. int bits, int clear_bits, gfp_t mask);
  211. int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
  212. struct extent_state **cached_state, gfp_t mask);
  213. int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
  214. u64 *start_ret, u64 *end_ret, int bits);
  215. struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
  216. u64 start, int bits);
  217. int extent_invalidatepage(struct extent_io_tree *tree,
  218. struct page *page, unsigned long offset);
  219. int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
  220. get_extent_t *get_extent,
  221. struct writeback_control *wbc);
  222. int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
  223. u64 start, u64 end, get_extent_t *get_extent,
  224. int mode);
  225. int extent_writepages(struct extent_io_tree *tree,
  226. struct address_space *mapping,
  227. get_extent_t *get_extent,
  228. struct writeback_control *wbc);
  229. int btree_write_cache_pages(struct address_space *mapping,
  230. struct writeback_control *wbc);
  231. int extent_readpages(struct extent_io_tree *tree,
  232. struct address_space *mapping,
  233. struct list_head *pages, unsigned nr_pages,
  234. get_extent_t get_extent);
  235. int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  236. __u64 start, __u64 len, get_extent_t *get_extent);
  237. int set_state_private(struct extent_io_tree *tree, u64 start, u64 private);
  238. int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private);
  239. void set_page_extent_mapped(struct page *page);
  240. struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
  241. u64 start, unsigned long len);
  242. struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
  243. u64 start, unsigned long len);
  244. void free_extent_buffer(struct extent_buffer *eb);
  245. void free_extent_buffer_stale(struct extent_buffer *eb);
  246. #define WAIT_NONE 0
  247. #define WAIT_COMPLETE 1
  248. #define WAIT_PAGE_LOCK 2
  249. int read_extent_buffer_pages(struct extent_io_tree *tree,
  250. struct extent_buffer *eb, u64 start, int wait,
  251. get_extent_t *get_extent, int mirror_num);
  252. unsigned long num_extent_pages(u64 start, u64 len);
  253. struct page *extent_buffer_page(struct extent_buffer *eb, unsigned long i);
  254. static inline void extent_buffer_get(struct extent_buffer *eb)
  255. {
  256. atomic_inc(&eb->refs);
  257. }
  258. int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
  259. unsigned long start,
  260. unsigned long len);
  261. void read_extent_buffer(struct extent_buffer *eb, void *dst,
  262. unsigned long start,
  263. unsigned long len);
  264. void write_extent_buffer(struct extent_buffer *eb, const void *src,
  265. unsigned long start, unsigned long len);
  266. void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
  267. unsigned long dst_offset, unsigned long src_offset,
  268. unsigned long len);
  269. void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  270. unsigned long src_offset, unsigned long len);
  271. void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  272. unsigned long src_offset, unsigned long len);
  273. void memset_extent_buffer(struct extent_buffer *eb, char c,
  274. unsigned long start, unsigned long len);
  275. void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits);
  276. void clear_extent_buffer_dirty(struct extent_buffer *eb);
  277. int set_extent_buffer_dirty(struct extent_buffer *eb);
  278. int set_extent_buffer_uptodate(struct extent_buffer *eb);
  279. int clear_extent_buffer_uptodate(struct extent_buffer *eb);
  280. int extent_buffer_uptodate(struct extent_buffer *eb);
  281. int map_private_extent_buffer(struct extent_buffer *eb, unsigned long offset,
  282. unsigned long min_len, char **map,
  283. unsigned long *map_start,
  284. unsigned long *map_len);
  285. int extent_range_uptodate(struct extent_io_tree *tree,
  286. u64 start, u64 end);
  287. int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end);
  288. int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end);
  289. int extent_clear_unlock_delalloc(struct inode *inode,
  290. struct extent_io_tree *tree,
  291. u64 start, u64 end, struct page *locked_page,
  292. unsigned long op);
  293. struct bio *
  294. btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
  295. gfp_t gfp_flags);
  296. struct btrfs_mapping_tree;
  297. int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
  298. u64 length, u64 logical, struct page *page,
  299. int mirror_num);
  300. int end_extent_writepage(struct page *page, int err, u64 start, u64 end);
  301. int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
  302. int mirror_num);
  303. #endif