ordered-data.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2007 Oracle. All rights reserved.
  4. */
  5. #ifndef BTRFS_ORDERED_DATA_H
  6. #define BTRFS_ORDERED_DATA_H
  7. #include <linux/types.h>
  8. #include <linux/list.h>
  9. #include <linux/refcount.h>
  10. #include <linux/completion.h>
  11. #include <linux/rbtree.h>
  12. #include <linux/wait.h>
  13. #include "async-thread.h"
  14. struct inode;
  15. struct page;
  16. struct extent_state;
  17. struct btrfs_block_group;
  18. struct btrfs_inode;
  19. struct btrfs_root;
  20. struct btrfs_fs_info;
  21. struct btrfs_ordered_sum {
  22. /*
  23. * Logical start address and length for of the blocks covered by
  24. * the sums array.
  25. */
  26. u64 logical;
  27. u32 len;
  28. struct list_head list;
  29. /* last field is a variable length array of csums */
  30. u8 sums[];
  31. };
  32. /*
  33. * Bits for btrfs_ordered_extent::flags.
  34. *
  35. * BTRFS_ORDERED_IO_DONE is set when all of the blocks are written.
  36. * It is used to make sure metadata is inserted into the tree only once
  37. * per extent.
  38. *
  39. * BTRFS_ORDERED_COMPLETE is set when the extent is removed from the
  40. * rbtree, just before waking any waiters. It is used to indicate the
  41. * IO is done and any metadata is inserted into the tree.
  42. */
  43. enum {
  44. /*
  45. * Different types for ordered extents, one and only one of the 4 types
  46. * need to be set when creating ordered extent.
  47. *
  48. * REGULAR: For regular non-compressed COW write
  49. * NOCOW: For NOCOW write into existing non-hole extent
  50. * PREALLOC: For NOCOW write into preallocated extent
  51. * COMPRESSED: For compressed COW write
  52. */
  53. BTRFS_ORDERED_REGULAR,
  54. BTRFS_ORDERED_NOCOW,
  55. BTRFS_ORDERED_PREALLOC,
  56. BTRFS_ORDERED_COMPRESSED,
  57. /*
  58. * Extra bit for direct io, can only be set for
  59. * REGULAR/NOCOW/PREALLOC. No direct io for compressed extent.
  60. */
  61. BTRFS_ORDERED_DIRECT,
  62. /* Extra status bits for ordered extents */
  63. /* set when all the pages are written */
  64. BTRFS_ORDERED_IO_DONE,
  65. /* set when removed from the tree */
  66. BTRFS_ORDERED_COMPLETE,
  67. /* We had an io error when writing this out */
  68. BTRFS_ORDERED_IOERR,
  69. /* Set when we have to truncate an extent */
  70. BTRFS_ORDERED_TRUNCATED,
  71. /* Used during fsync to track already logged extents */
  72. BTRFS_ORDERED_LOGGED,
  73. /* We have already logged all the csums of the ordered extent */
  74. BTRFS_ORDERED_LOGGED_CSUM,
  75. /* We wait for this extent to complete in the current transaction */
  76. BTRFS_ORDERED_PENDING,
  77. /* BTRFS_IOC_ENCODED_WRITE */
  78. BTRFS_ORDERED_ENCODED,
  79. };
  80. /* BTRFS_ORDERED_* flags that specify the type of the extent. */
  81. #define BTRFS_ORDERED_TYPE_FLAGS ((1UL << BTRFS_ORDERED_REGULAR) | \
  82. (1UL << BTRFS_ORDERED_NOCOW) | \
  83. (1UL << BTRFS_ORDERED_PREALLOC) | \
  84. (1UL << BTRFS_ORDERED_COMPRESSED) | \
  85. (1UL << BTRFS_ORDERED_DIRECT) | \
  86. (1UL << BTRFS_ORDERED_ENCODED))
  87. struct btrfs_ordered_extent {
  88. /* logical offset in the file */
  89. u64 file_offset;
  90. /*
  91. * These fields directly correspond to the same fields in
  92. * btrfs_file_extent_item.
  93. */
  94. u64 num_bytes;
  95. u64 ram_bytes;
  96. u64 disk_bytenr;
  97. u64 disk_num_bytes;
  98. u64 offset;
  99. /* number of bytes that still need writing */
  100. u64 bytes_left;
  101. /*
  102. * If we get truncated we need to adjust the file extent we enter for
  103. * this ordered extent so that we do not expose stale data.
  104. */
  105. u64 truncated_len;
  106. /* flags (described above) */
  107. unsigned long flags;
  108. /* compression algorithm */
  109. int compress_type;
  110. /* Qgroup reserved space */
  111. int qgroup_rsv;
  112. /* reference count */
  113. refcount_t refs;
  114. /* the inode we belong to */
  115. struct btrfs_inode *inode;
  116. /* list of checksums for insertion when the extent io is done */
  117. struct list_head list;
  118. /* used for fast fsyncs */
  119. struct list_head log_list;
  120. /* used to wait for the BTRFS_ORDERED_COMPLETE bit */
  121. wait_queue_head_t wait;
  122. /* our friendly rbtree entry */
  123. struct rb_node rb_node;
  124. /* a per root list of all the pending ordered extents */
  125. struct list_head root_extent_list;
  126. struct btrfs_work work;
  127. struct completion completion;
  128. struct btrfs_work flush_work;
  129. struct list_head work_list;
  130. struct list_head bioc_list;
  131. };
  132. int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent);
  133. int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
  134. void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry);
  135. void btrfs_remove_ordered_extent(struct btrfs_inode *btrfs_inode,
  136. struct btrfs_ordered_extent *entry);
  137. void btrfs_finish_ordered_extent(struct btrfs_ordered_extent *ordered,
  138. struct folio *folio, u64 file_offset, u64 len,
  139. bool uptodate);
  140. void btrfs_mark_ordered_io_finished(struct btrfs_inode *inode,
  141. struct folio *folio, u64 file_offset,
  142. u64 num_bytes, bool uptodate);
  143. bool btrfs_dec_test_ordered_pending(struct btrfs_inode *inode,
  144. struct btrfs_ordered_extent **cached,
  145. u64 file_offset, u64 io_size);
  146. /*
  147. * This represents details about the target file extent item of a write operation.
  148. */
  149. struct btrfs_file_extent {
  150. u64 disk_bytenr;
  151. u64 disk_num_bytes;
  152. u64 num_bytes;
  153. u64 ram_bytes;
  154. u64 offset;
  155. u8 compression;
  156. };
  157. struct btrfs_ordered_extent *btrfs_alloc_ordered_extent(
  158. struct btrfs_inode *inode, u64 file_offset,
  159. const struct btrfs_file_extent *file_extent, unsigned long flags);
  160. void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry,
  161. struct btrfs_ordered_sum *sum);
  162. struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode,
  163. u64 file_offset);
  164. void btrfs_start_ordered_extent_nowriteback(struct btrfs_ordered_extent *entry,
  165. u64 nowriteback_start, u32 nowriteback_len);
  166. static inline void btrfs_start_ordered_extent(struct btrfs_ordered_extent *entry)
  167. {
  168. return btrfs_start_ordered_extent_nowriteback(entry, 0, 0);
  169. }
  170. int btrfs_wait_ordered_range(struct btrfs_inode *inode, u64 start, u64 len);
  171. struct btrfs_ordered_extent *
  172. btrfs_lookup_first_ordered_extent(struct btrfs_inode *inode, u64 file_offset);
  173. struct btrfs_ordered_extent *btrfs_lookup_first_ordered_range(
  174. struct btrfs_inode *inode, u64 file_offset, u64 len);
  175. struct btrfs_ordered_extent *btrfs_lookup_ordered_range(
  176. struct btrfs_inode *inode,
  177. u64 file_offset,
  178. u64 len);
  179. void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode,
  180. struct list_head *list);
  181. u64 btrfs_wait_ordered_extents(struct btrfs_root *root, u64 nr,
  182. const struct btrfs_block_group *bg);
  183. void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, u64 nr,
  184. const struct btrfs_block_group *bg);
  185. void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start,
  186. u64 end,
  187. struct extent_state **cached_state);
  188. bool btrfs_try_lock_ordered_range(struct btrfs_inode *inode, u64 start, u64 end,
  189. struct extent_state **cached_state);
  190. struct btrfs_ordered_extent *btrfs_split_ordered_extent(
  191. struct btrfs_ordered_extent *ordered, u64 len);
  192. void btrfs_mark_ordered_extent_error(struct btrfs_ordered_extent *ordered);
  193. int __init ordered_data_init(void);
  194. void __cold ordered_data_exit(void);
  195. #endif