ordered-data.h 6.4 KB

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