ordered-data.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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_TRUNCATED 8 /* Set when we have to truncate an extent */
  46. #define BTRFS_ORDERED_PENDING 9 /* We are waiting for this ordered extent to
  47. * complete in the current transaction. */
  48. #define BTRFS_ORDERED_REGULAR 10 /* Regular IO for COW */
  49. struct btrfs_ordered_extent {
  50. /* logical offset in the file */
  51. u64 file_offset;
  52. /* disk byte number */
  53. u64 start;
  54. /* ram length of the extent in bytes */
  55. u64 len;
  56. /* extent length on disk */
  57. u64 disk_len;
  58. /* number of bytes that still need writing */
  59. u64 bytes_left;
  60. /*
  61. * the end of the ordered extent which is behind it but
  62. * didn't update disk_i_size. Please see the comment of
  63. * btrfs_ordered_update_i_size();
  64. */
  65. u64 outstanding_isize;
  66. /*
  67. * If we get truncated we need to adjust the file extent we enter for
  68. * this ordered extent so that we do not expose stale data.
  69. */
  70. u64 truncated_len;
  71. /* flags (described above) */
  72. unsigned long flags;
  73. /* compression algorithm */
  74. int compress_type;
  75. /* reference count */
  76. refcount_t refs;
  77. /* the inode we belong to */
  78. struct inode *inode;
  79. /* list of checksums for insertion when the extent io is done */
  80. struct list_head list;
  81. /* If we need to wait on this to be done */
  82. struct list_head log_list;
  83. /* If the transaction needs to wait on this ordered extent */
  84. struct list_head trans_list;
  85. /* used to wait for the BTRFS_ORDERED_COMPLETE bit */
  86. wait_queue_head_t wait;
  87. /* our friendly rbtree entry */
  88. struct rb_node rb_node;
  89. /* a per root list of all the pending ordered extents */
  90. struct list_head root_extent_list;
  91. struct btrfs_work work;
  92. struct completion completion;
  93. struct btrfs_work flush_work;
  94. struct list_head work_list;
  95. };
  96. /*
  97. * calculates the total size you need to allocate for an ordered sum
  98. * structure spanning 'bytes' in the file
  99. */
  100. static inline int btrfs_ordered_sum_size(struct btrfs_fs_info *fs_info,
  101. unsigned long bytes)
  102. {
  103. int num_sectors = (int)DIV_ROUND_UP(bytes, fs_info->sectorsize);
  104. int csum_size = btrfs_super_csum_size(fs_info->super_copy);
  105. return sizeof(struct btrfs_ordered_sum) + num_sectors * csum_size;
  106. }
  107. static inline void
  108. btrfs_ordered_inode_tree_init(struct btrfs_ordered_inode_tree *t)
  109. {
  110. spin_lock_init(&t->lock);
  111. t->tree = RB_ROOT;
  112. t->last = NULL;
  113. }
  114. void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry);
  115. void btrfs_remove_ordered_extent(struct inode *inode,
  116. struct btrfs_ordered_extent *entry);
  117. int btrfs_dec_test_ordered_pending(struct inode *inode,
  118. struct btrfs_ordered_extent **cached,
  119. u64 file_offset, u64 io_size, int uptodate);
  120. int btrfs_dec_test_first_ordered_pending(struct inode *inode,
  121. struct btrfs_ordered_extent **cached,
  122. u64 *file_offset, u64 io_size,
  123. int uptodate);
  124. int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
  125. u64 start, u64 len, u64 disk_len, int type);
  126. int btrfs_add_ordered_extent_dio(struct inode *inode, u64 file_offset,
  127. u64 start, u64 len, u64 disk_len, int type);
  128. int btrfs_add_ordered_extent_compress(struct inode *inode, u64 file_offset,
  129. u64 start, u64 len, u64 disk_len,
  130. int type, int compress_type);
  131. void btrfs_add_ordered_sum(struct inode *inode,
  132. struct btrfs_ordered_extent *entry,
  133. struct btrfs_ordered_sum *sum);
  134. struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
  135. u64 file_offset);
  136. void btrfs_start_ordered_extent(struct inode *inode,
  137. struct btrfs_ordered_extent *entry, int wait);
  138. int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len);
  139. struct btrfs_ordered_extent *
  140. btrfs_lookup_first_ordered_extent(struct inode * inode, u64 file_offset);
  141. struct btrfs_ordered_extent *btrfs_lookup_ordered_range(
  142. struct btrfs_inode *inode,
  143. u64 file_offset,
  144. u64 len);
  145. int btrfs_ordered_update_i_size(struct inode *inode, u64 offset,
  146. struct btrfs_ordered_extent *ordered);
  147. int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr,
  148. u32 *sum, int len);
  149. u64 btrfs_wait_ordered_extents(struct btrfs_root *root, u64 nr,
  150. const u64 range_start, const u64 range_len);
  151. u64 btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, u64 nr,
  152. const u64 range_start, const u64 range_len);
  153. int __init ordered_data_init(void);
  154. void __cold ordered_data_exit(void);
  155. #endif