ordered-data.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. u32 sums[];
  23. };
  24. /*
  25. * bits for the flags field:
  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. #define BTRFS_ORDERED_IO_DONE 0 /* set when all the pages are written */
  36. #define BTRFS_ORDERED_COMPLETE 1 /* set when removed from the tree */
  37. #define BTRFS_ORDERED_NOCOW 2 /* set when we want to write in place */
  38. #define BTRFS_ORDERED_COMPRESSED 3 /* writing a zlib compressed extent */
  39. #define BTRFS_ORDERED_PREALLOC 4 /* set when writing to preallocated extent */
  40. #define BTRFS_ORDERED_DIRECT 5 /* set when we're doing DIO with this extent */
  41. #define BTRFS_ORDERED_IOERR 6 /* We had an io error when writing this out */
  42. #define BTRFS_ORDERED_UPDATED_ISIZE 7 /* indicates whether this ordered extent
  43. * has done its due diligence in updating
  44. * the isize. */
  45. #define BTRFS_ORDERED_LOGGED_CSUM 8 /* We've logged the csums on this ordered
  46. ordered extent */
  47. #define BTRFS_ORDERED_TRUNCATED 9 /* Set when we have to truncate an extent */
  48. #define BTRFS_ORDERED_LOGGED 10 /* Set when we've waited on this ordered extent
  49. * in the logging code. */
  50. #define BTRFS_ORDERED_PENDING 11 /* We are waiting for this ordered extent to
  51. * complete in the current transaction. */
  52. #define BTRFS_ORDERED_REGULAR 12 /* Regular IO for COW */
  53. struct btrfs_ordered_extent {
  54. /* logical offset in the file */
  55. u64 file_offset;
  56. /* disk byte number */
  57. u64 start;
  58. /* ram length of the extent in bytes */
  59. u64 len;
  60. /* extent length on disk */
  61. u64 disk_len;
  62. /* number of bytes that still need writing */
  63. u64 bytes_left;
  64. /*
  65. * the end of the ordered extent which is behind it but
  66. * didn't update disk_i_size. Please see the comment of
  67. * btrfs_ordered_update_i_size();
  68. */
  69. u64 outstanding_isize;
  70. /*
  71. * If we get truncated we need to adjust the file extent we enter for
  72. * this ordered extent so that we do not expose stale data.
  73. */
  74. u64 truncated_len;
  75. /* flags (described above) */
  76. unsigned long flags;
  77. /* compression algorithm */
  78. int compress_type;
  79. /* reference count */
  80. refcount_t refs;
  81. /* the inode we belong to */
  82. struct inode *inode;
  83. /* list of checksums for insertion when the extent io is done */
  84. struct list_head list;
  85. /* If we need to wait on this to be done */
  86. struct list_head log_list;
  87. /* If the transaction needs to wait on this ordered extent */
  88. struct list_head trans_list;
  89. /* used to wait for the BTRFS_ORDERED_COMPLETE bit */
  90. wait_queue_head_t wait;
  91. /* our friendly rbtree entry */
  92. struct rb_node rb_node;
  93. /* a per root list of all the pending ordered extents */
  94. struct list_head root_extent_list;
  95. struct btrfs_work work;
  96. struct completion completion;
  97. struct btrfs_work flush_work;
  98. struct list_head work_list;
  99. };
  100. /*
  101. * calculates the total size you need to allocate for an ordered sum
  102. * structure spanning 'bytes' in the file
  103. */
  104. static inline int btrfs_ordered_sum_size(struct btrfs_fs_info *fs_info,
  105. unsigned long bytes)
  106. {
  107. int num_sectors = (int)DIV_ROUND_UP(bytes, fs_info->sectorsize);
  108. int csum_size = btrfs_super_csum_size(fs_info->super_copy);
  109. return sizeof(struct btrfs_ordered_sum) + num_sectors * csum_size;
  110. }
  111. static inline void
  112. btrfs_ordered_inode_tree_init(struct btrfs_ordered_inode_tree *t)
  113. {
  114. spin_lock_init(&t->lock);
  115. t->tree = RB_ROOT;
  116. t->last = NULL;
  117. }
  118. void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry);
  119. void btrfs_remove_ordered_extent(struct inode *inode,
  120. struct btrfs_ordered_extent *entry);
  121. int btrfs_dec_test_ordered_pending(struct inode *inode,
  122. struct btrfs_ordered_extent **cached,
  123. u64 file_offset, u64 io_size, int uptodate);
  124. int btrfs_dec_test_first_ordered_pending(struct inode *inode,
  125. struct btrfs_ordered_extent **cached,
  126. u64 *file_offset, u64 io_size,
  127. int uptodate);
  128. int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
  129. u64 start, u64 len, u64 disk_len, int type);
  130. int btrfs_add_ordered_extent_dio(struct inode *inode, u64 file_offset,
  131. u64 start, u64 len, u64 disk_len, int type);
  132. int btrfs_add_ordered_extent_compress(struct inode *inode, u64 file_offset,
  133. u64 start, u64 len, u64 disk_len,
  134. int type, int compress_type);
  135. void btrfs_add_ordered_sum(struct inode *inode,
  136. struct btrfs_ordered_extent *entry,
  137. struct btrfs_ordered_sum *sum);
  138. struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
  139. u64 file_offset);
  140. void btrfs_start_ordered_extent(struct inode *inode,
  141. struct btrfs_ordered_extent *entry, int wait);
  142. int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len);
  143. struct btrfs_ordered_extent *
  144. btrfs_lookup_first_ordered_extent(struct inode * inode, u64 file_offset);
  145. struct btrfs_ordered_extent *btrfs_lookup_ordered_range(
  146. struct btrfs_inode *inode,
  147. u64 file_offset,
  148. u64 len);
  149. bool btrfs_have_ordered_extents_in_range(struct inode *inode,
  150. u64 file_offset,
  151. u64 len);
  152. int btrfs_ordered_update_i_size(struct inode *inode, u64 offset,
  153. struct btrfs_ordered_extent *ordered);
  154. int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr,
  155. u32 *sum, int len);
  156. u64 btrfs_wait_ordered_extents(struct btrfs_root *root, u64 nr,
  157. const u64 range_start, const u64 range_len);
  158. u64 btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, u64 nr,
  159. const u64 range_start, const u64 range_len);
  160. void btrfs_get_logged_extents(struct btrfs_inode *inode,
  161. struct list_head *logged_list,
  162. const loff_t start,
  163. const loff_t end);
  164. void btrfs_put_logged_extents(struct list_head *logged_list);
  165. void btrfs_submit_logged_extents(struct list_head *logged_list,
  166. struct btrfs_root *log);
  167. void btrfs_wait_logged_extents(struct btrfs_trans_handle *trans,
  168. struct btrfs_root *log, u64 transid);
  169. void btrfs_free_logged_extents(struct btrfs_root *log, u64 transid);
  170. int __init ordered_data_init(void);
  171. void __cold ordered_data_exit(void);
  172. #endif