ordered-data.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. struct btrfs_ordered_extent {
  62. /* logical offset in the file */
  63. u64 file_offset;
  64. /* disk byte number */
  65. u64 start;
  66. /* ram length of the extent in bytes */
  67. u64 len;
  68. /* extent length on disk */
  69. u64 disk_len;
  70. /* number of bytes that still need writing */
  71. u64 bytes_left;
  72. /* number of bytes that still need csumming */
  73. u64 csum_bytes_left;
  74. /*
  75. * the end of the ordered extent which is behind it but
  76. * didn't update disk_i_size. Please see the comment of
  77. * btrfs_ordered_update_i_size();
  78. */
  79. u64 outstanding_isize;
  80. /*
  81. * If we get truncated we need to adjust the file extent we enter for
  82. * this ordered extent so that we do not expose stale data.
  83. */
  84. u64 truncated_len;
  85. /* flags (described above) */
  86. unsigned long flags;
  87. /* compression algorithm */
  88. int compress_type;
  89. /* reference count */
  90. atomic_t refs;
  91. /* the inode we belong to */
  92. struct inode *inode;
  93. /* list of checksums for insertion when the extent io is done */
  94. struct list_head list;
  95. /* If we need to wait on this to be done */
  96. struct list_head log_list;
  97. /* used to wait for the BTRFS_ORDERED_COMPLETE bit */
  98. wait_queue_head_t wait;
  99. /* our friendly rbtree entry */
  100. struct rb_node rb_node;
  101. /* a per root list of all the pending ordered extents */
  102. struct list_head root_extent_list;
  103. struct btrfs_work work;
  104. struct completion completion;
  105. struct btrfs_work flush_work;
  106. struct list_head work_list;
  107. };
  108. /*
  109. * calculates the total size you need to allocate for an ordered sum
  110. * structure spanning 'bytes' in the file
  111. */
  112. static inline int btrfs_ordered_sum_size(struct btrfs_root *root,
  113. unsigned long bytes)
  114. {
  115. int num_sectors = (int)DIV_ROUND_UP(bytes, root->sectorsize);
  116. return sizeof(struct btrfs_ordered_sum) + num_sectors * sizeof(u32);
  117. }
  118. static inline void
  119. btrfs_ordered_inode_tree_init(struct btrfs_ordered_inode_tree *t)
  120. {
  121. spin_lock_init(&t->lock);
  122. t->tree = RB_ROOT;
  123. t->last = NULL;
  124. }
  125. void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry);
  126. void btrfs_remove_ordered_extent(struct inode *inode,
  127. struct btrfs_ordered_extent *entry);
  128. int btrfs_dec_test_ordered_pending(struct inode *inode,
  129. struct btrfs_ordered_extent **cached,
  130. u64 file_offset, u64 io_size, int uptodate);
  131. int btrfs_dec_test_first_ordered_pending(struct inode *inode,
  132. struct btrfs_ordered_extent **cached,
  133. u64 *file_offset, u64 io_size,
  134. int uptodate);
  135. int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
  136. u64 start, u64 len, u64 disk_len, int type);
  137. int btrfs_add_ordered_extent_dio(struct inode *inode, u64 file_offset,
  138. u64 start, u64 len, u64 disk_len, int type);
  139. int btrfs_add_ordered_extent_compress(struct inode *inode, u64 file_offset,
  140. u64 start, u64 len, u64 disk_len,
  141. int type, int compress_type);
  142. void btrfs_add_ordered_sum(struct inode *inode,
  143. struct btrfs_ordered_extent *entry,
  144. struct btrfs_ordered_sum *sum);
  145. struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
  146. u64 file_offset);
  147. void btrfs_start_ordered_extent(struct inode *inode,
  148. struct btrfs_ordered_extent *entry, int wait);
  149. void btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len);
  150. struct btrfs_ordered_extent *
  151. btrfs_lookup_first_ordered_extent(struct inode * inode, u64 file_offset);
  152. struct btrfs_ordered_extent *btrfs_lookup_ordered_range(struct inode *inode,
  153. u64 file_offset,
  154. u64 len);
  155. int btrfs_ordered_update_i_size(struct inode *inode, u64 offset,
  156. struct btrfs_ordered_extent *ordered);
  157. int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr,
  158. u32 *sum, int len);
  159. int btrfs_run_ordered_operations(struct btrfs_trans_handle *trans,
  160. struct btrfs_root *root, int wait);
  161. void btrfs_add_ordered_operation(struct btrfs_trans_handle *trans,
  162. struct btrfs_root *root,
  163. struct inode *inode);
  164. void btrfs_wait_ordered_extents(struct btrfs_root *root);
  165. void btrfs_wait_all_ordered_extents(struct btrfs_fs_info *fs_info);
  166. void btrfs_get_logged_extents(struct btrfs_root *log, struct inode *inode);
  167. void btrfs_wait_logged_extents(struct btrfs_root *log, u64 transid);
  168. void btrfs_free_logged_extents(struct btrfs_root *log, u64 transid);
  169. int __init ordered_data_init(void);
  170. void ordered_data_exit(void);
  171. #endif