ordered-data.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #ifndef __BTRFS_ORDERED_DATA__
  19. #define __BTRFS_ORDERED_DATA__
  20. /* one of these per inode */
  21. struct btrfs_ordered_inode_tree {
  22. spinlock_t lock;
  23. struct rb_root tree;
  24. struct rb_node *last;
  25. };
  26. struct btrfs_ordered_sum {
  27. /* bytenr is the start of this extent on disk */
  28. u64 bytenr;
  29. /*
  30. * this is the length in bytes covered by the sums array below.
  31. */
  32. int len;
  33. struct list_head list;
  34. /* last field is a variable length array of csums */
  35. u32 sums[];
  36. };
  37. /*
  38. * bits for the flags field:
  39. *
  40. * BTRFS_ORDERED_IO_DONE is set when all of the blocks are written.
  41. * It is used to make sure metadata is inserted into the tree only once
  42. * per extent.
  43. *
  44. * BTRFS_ORDERED_COMPLETE is set when the extent is removed from the
  45. * rbtree, just before waking any waiters. It is used to indicate the
  46. * IO is done and any metadata is inserted into the tree.
  47. */
  48. #define BTRFS_ORDERED_IO_DONE 0 /* set when all the pages are written */
  49. #define BTRFS_ORDERED_COMPLETE 1 /* set when removed from the tree */
  50. #define BTRFS_ORDERED_NOCOW 2 /* set when we want to write in place */
  51. #define BTRFS_ORDERED_COMPRESSED 3 /* writing a zlib compressed extent */
  52. #define BTRFS_ORDERED_PREALLOC 4 /* set when writing to prealloced extent */
  53. #define BTRFS_ORDERED_DIRECT 5 /* set when we're doing DIO with this extent */
  54. #define BTRFS_ORDERED_IOERR 6 /* We had an io error when writing this out */
  55. #define BTRFS_ORDERED_UPDATED_ISIZE 7 /* indicates whether this ordered extent
  56. * has done its due diligence in updating
  57. * the isize. */
  58. #define BTRFS_ORDERED_LOGGED_CSUM 8 /* We've logged the csums on this ordered
  59. ordered extent */
  60. #define BTRFS_ORDERED_TRUNCATED 9 /* Set when we have to truncate an extent */
  61. #define BTRFS_ORDERED_LOGGED 10 /* Set when we've waited on this ordered extent
  62. * in the logging code. */
  63. #define BTRFS_ORDERED_PENDING 11 /* We are waiting for this ordered extent to
  64. * complete in the current transaction. */
  65. struct btrfs_ordered_extent {
  66. /* logical offset in the file */
  67. u64 file_offset;
  68. /* disk byte number */
  69. u64 start;
  70. /* ram length of the extent in bytes */
  71. u64 len;
  72. /* extent length on disk */
  73. u64 disk_len;
  74. /* number of bytes that still need writing */
  75. u64 bytes_left;
  76. /*
  77. * the end of the ordered extent which is behind it but
  78. * didn't update disk_i_size. Please see the comment of
  79. * btrfs_ordered_update_i_size();
  80. */
  81. u64 outstanding_isize;
  82. /*
  83. * If we get truncated we need to adjust the file extent we enter for
  84. * this ordered extent so that we do not expose stale data.
  85. */
  86. u64 truncated_len;
  87. /* flags (described above) */
  88. unsigned long flags;
  89. /* compression algorithm */
  90. int compress_type;
  91. /* reference count */
  92. atomic_t refs;
  93. /* the inode we belong to */
  94. struct inode *inode;
  95. /* list of checksums for insertion when the extent io is done */
  96. struct list_head list;
  97. /* If we need to wait on this to be done */
  98. struct list_head log_list;
  99. /* If the transaction needs to wait on this ordered extent */
  100. struct list_head trans_list;
  101. /* used to wait for the BTRFS_ORDERED_COMPLETE bit */
  102. wait_queue_head_t wait;
  103. /* our friendly rbtree entry */
  104. struct rb_node rb_node;
  105. /* a per root list of all the pending ordered extents */
  106. struct list_head root_extent_list;
  107. struct btrfs_work work;
  108. struct completion completion;
  109. struct btrfs_work flush_work;
  110. struct list_head work_list;
  111. };
  112. /*
  113. * calculates the total size you need to allocate for an ordered sum
  114. * structure spanning 'bytes' in the file
  115. */
  116. static inline int btrfs_ordered_sum_size(struct btrfs_root *root,
  117. unsigned long bytes)
  118. {
  119. int num_sectors = (int)DIV_ROUND_UP(bytes, root->sectorsize);
  120. return sizeof(struct btrfs_ordered_sum) + num_sectors * sizeof(u32);
  121. }
  122. static inline void
  123. btrfs_ordered_inode_tree_init(struct btrfs_ordered_inode_tree *t)
  124. {
  125. spin_lock_init(&t->lock);
  126. t->tree = RB_ROOT;
  127. t->last = NULL;
  128. }
  129. void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry);
  130. void btrfs_remove_ordered_extent(struct inode *inode,
  131. struct btrfs_ordered_extent *entry);
  132. int btrfs_dec_test_ordered_pending(struct inode *inode,
  133. struct btrfs_ordered_extent **cached,
  134. u64 file_offset, u64 io_size, int uptodate);
  135. int btrfs_dec_test_first_ordered_pending(struct inode *inode,
  136. struct btrfs_ordered_extent **cached,
  137. u64 *file_offset, u64 io_size,
  138. int uptodate);
  139. int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
  140. u64 start, u64 len, u64 disk_len, int type);
  141. int btrfs_add_ordered_extent_dio(struct inode *inode, u64 file_offset,
  142. u64 start, u64 len, u64 disk_len, int type);
  143. int btrfs_add_ordered_extent_compress(struct inode *inode, u64 file_offset,
  144. u64 start, u64 len, u64 disk_len,
  145. int type, int compress_type);
  146. void btrfs_add_ordered_sum(struct inode *inode,
  147. struct btrfs_ordered_extent *entry,
  148. struct btrfs_ordered_sum *sum);
  149. struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
  150. u64 file_offset);
  151. void btrfs_start_ordered_extent(struct inode *inode,
  152. struct btrfs_ordered_extent *entry, int wait);
  153. int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len);
  154. struct btrfs_ordered_extent *
  155. btrfs_lookup_first_ordered_extent(struct inode * inode, u64 file_offset);
  156. struct btrfs_ordered_extent *btrfs_lookup_ordered_range(struct inode *inode,
  157. u64 file_offset,
  158. u64 len);
  159. bool btrfs_have_ordered_extents_in_range(struct inode *inode,
  160. u64 file_offset,
  161. u64 len);
  162. int btrfs_ordered_update_i_size(struct inode *inode, u64 offset,
  163. struct btrfs_ordered_extent *ordered);
  164. int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr,
  165. u32 *sum, int len);
  166. int btrfs_wait_ordered_extents(struct btrfs_root *root, int nr);
  167. void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, int nr);
  168. void btrfs_get_logged_extents(struct inode *inode,
  169. struct list_head *logged_list,
  170. const loff_t start,
  171. const loff_t end);
  172. void btrfs_put_logged_extents(struct list_head *logged_list);
  173. void btrfs_submit_logged_extents(struct list_head *logged_list,
  174. struct btrfs_root *log);
  175. void btrfs_wait_logged_extents(struct btrfs_trans_handle *trans,
  176. struct btrfs_root *log, u64 transid);
  177. void btrfs_free_logged_extents(struct btrfs_root *log, u64 transid);
  178. int __init ordered_data_init(void);
  179. void ordered_data_exit(void);
  180. #endif