ordered-data.h 6.5 KB

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