ordered-data.h 6.3 KB

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