ordered-data.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. /*
  27. * these are used to collect checksums done just before bios submission.
  28. * They are attached via a list into the ordered extent, and
  29. * checksum items are inserted into the tree after all the blocks in
  30. * the ordered extent are on disk
  31. */
  32. struct btrfs_sector_sum {
  33. /* bytenr on disk */
  34. u64 bytenr;
  35. u32 sum;
  36. };
  37. struct btrfs_ordered_sum {
  38. /* bytenr is the start of this extent on disk */
  39. u64 bytenr;
  40. /*
  41. * this is the length in bytes covered by the sums array below.
  42. */
  43. unsigned long len;
  44. struct list_head list;
  45. /* last field is a variable length array of btrfs_sector_sums */
  46. struct btrfs_sector_sum sums[];
  47. };
  48. /*
  49. * bits for the flags field:
  50. *
  51. * BTRFS_ORDERED_IO_DONE is set when all of the blocks are written.
  52. * It is used to make sure metadata is inserted into the tree only once
  53. * per extent.
  54. *
  55. * BTRFS_ORDERED_COMPLETE is set when the extent is removed from the
  56. * rbtree, just before waking any waiters. It is used to indicate the
  57. * IO is done and any metadata is inserted into the tree.
  58. */
  59. #define BTRFS_ORDERED_IO_DONE 0 /* set when all the pages are written */
  60. #define BTRFS_ORDERED_COMPLETE 1 /* set when removed from the tree */
  61. #define BTRFS_ORDERED_NOCOW 2 /* set when we want to write in place */
  62. #define BTRFS_ORDERED_COMPRESSED 3 /* writing a zlib compressed extent */
  63. #define BTRFS_ORDERED_PREALLOC 4 /* set when writing to prealloced extent */
  64. #define BTRFS_ORDERED_DIRECT 5 /* set when we're doing DIO with this extent */
  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. /* flags (described above) */
  77. unsigned long flags;
  78. /* compression algorithm */
  79. int compress_type;
  80. /* reference count */
  81. atomic_t refs;
  82. /* the inode we belong to */
  83. struct inode *inode;
  84. /* list of checksums for insertion when the extent io is done */
  85. struct list_head list;
  86. /* used to wait for the BTRFS_ORDERED_COMPLETE bit */
  87. wait_queue_head_t wait;
  88. /* our friendly rbtree entry */
  89. struct rb_node rb_node;
  90. /* a per root list of all the pending ordered extents */
  91. struct list_head root_extent_list;
  92. };
  93. /*
  94. * calculates the total size you need to allocate for an ordered sum
  95. * structure spanning 'bytes' in the file
  96. */
  97. static inline int btrfs_ordered_sum_size(struct btrfs_root *root,
  98. unsigned long bytes)
  99. {
  100. unsigned long num_sectors = (bytes + root->sectorsize - 1) /
  101. root->sectorsize;
  102. num_sectors++;
  103. return sizeof(struct btrfs_ordered_sum) +
  104. num_sectors * sizeof(struct btrfs_sector_sum);
  105. }
  106. static inline void
  107. btrfs_ordered_inode_tree_init(struct btrfs_ordered_inode_tree *t)
  108. {
  109. spin_lock_init(&t->lock);
  110. t->tree = RB_ROOT;
  111. t->last = NULL;
  112. }
  113. void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry);
  114. void btrfs_remove_ordered_extent(struct inode *inode,
  115. struct btrfs_ordered_extent *entry);
  116. int btrfs_dec_test_ordered_pending(struct inode *inode,
  117. struct btrfs_ordered_extent **cached,
  118. u64 file_offset, u64 io_size);
  119. int btrfs_dec_test_first_ordered_pending(struct inode *inode,
  120. struct btrfs_ordered_extent **cached,
  121. u64 *file_offset, u64 io_size);
  122. int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
  123. u64 start, u64 len, u64 disk_len, int type);
  124. int btrfs_add_ordered_extent_dio(struct inode *inode, u64 file_offset,
  125. u64 start, u64 len, u64 disk_len, int type);
  126. int btrfs_add_ordered_extent_compress(struct inode *inode, u64 file_offset,
  127. u64 start, u64 len, u64 disk_len,
  128. int type, int compress_type);
  129. void btrfs_add_ordered_sum(struct inode *inode,
  130. struct btrfs_ordered_extent *entry,
  131. struct btrfs_ordered_sum *sum);
  132. struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
  133. u64 file_offset);
  134. void btrfs_start_ordered_extent(struct inode *inode,
  135. struct btrfs_ordered_extent *entry, int wait);
  136. void btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len);
  137. struct btrfs_ordered_extent *
  138. btrfs_lookup_first_ordered_extent(struct inode * inode, u64 file_offset);
  139. struct btrfs_ordered_extent *btrfs_lookup_ordered_range(struct inode *inode,
  140. u64 file_offset,
  141. u64 len);
  142. int btrfs_ordered_update_i_size(struct inode *inode, u64 offset,
  143. struct btrfs_ordered_extent *ordered);
  144. int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr, u32 *sum);
  145. void btrfs_run_ordered_operations(struct btrfs_root *root, int wait);
  146. void btrfs_add_ordered_operation(struct btrfs_trans_handle *trans,
  147. struct btrfs_root *root,
  148. struct inode *inode);
  149. void btrfs_wait_ordered_extents(struct btrfs_root *root,
  150. int nocow_only, int delay_iput);
  151. #endif