ordered-data.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. struct mutex mutex;
  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. u64 offset;
  34. u32 sum;
  35. };
  36. struct btrfs_ordered_sum {
  37. u64 file_offset;
  38. u64 len;
  39. struct list_head list;
  40. /* last field is a variable length array of btrfs_sector_sums */
  41. struct btrfs_sector_sum sums;
  42. };
  43. /*
  44. * bits for the flags field:
  45. *
  46. * BTRFS_ORDERED_IO_DONE is set when all of the blocks are written.
  47. * It is used to make sure metadata is inserted into the tree only once
  48. * per extent.
  49. *
  50. * BTRFS_ORDERED_COMPLETE is set when the extent is removed from the
  51. * rbtree, just before waking any waiters. It is used to indicate the
  52. * IO is done and any metadata is inserted into the tree.
  53. */
  54. #define BTRFS_ORDERED_IO_DONE 0 /* set when all the pages are written */
  55. #define BTRFS_ORDERED_COMPLETE 1 /* set when removed from the tree */
  56. struct btrfs_ordered_extent {
  57. /* logical offset in the file */
  58. u64 file_offset;
  59. /* disk byte number */
  60. u64 start;
  61. /* length of the extent in bytes */
  62. u64 len;
  63. /* flags (described above) */
  64. unsigned long flags;
  65. /* reference count */
  66. atomic_t refs;
  67. /* list of checksums for insertion when the extent io is done */
  68. struct list_head list;
  69. /* used to wait for the BTRFS_ORDERED_COMPLETE bit */
  70. wait_queue_head_t wait;
  71. /* our friendly rbtree entry */
  72. struct rb_node rb_node;
  73. };
  74. /*
  75. * calculates the total size you need to allocate for an ordered sum
  76. * structure spanning 'bytes' in the file
  77. */
  78. static inline int btrfs_ordered_sum_size(struct btrfs_root *root, u64 bytes)
  79. {
  80. unsigned long num_sectors = (bytes + root->sectorsize - 1) /
  81. root->sectorsize;
  82. return sizeof(struct btrfs_ordered_sum) +
  83. num_sectors * sizeof(struct btrfs_sector_sum);
  84. }
  85. static inline void
  86. btrfs_ordered_inode_tree_init(struct btrfs_ordered_inode_tree *t)
  87. {
  88. mutex_init(&t->mutex);
  89. t->tree.rb_node = NULL;
  90. t->last = NULL;
  91. }
  92. int btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry);
  93. int btrfs_remove_ordered_extent(struct inode *inode,
  94. struct btrfs_ordered_extent *entry);
  95. int btrfs_dec_test_ordered_pending(struct inode *inode,
  96. u64 file_offset, u64 io_size);
  97. int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
  98. u64 start, u64 len);
  99. int btrfs_add_ordered_sum(struct inode *inode, struct btrfs_ordered_sum *sum);
  100. struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
  101. u64 file_offset);
  102. void btrfs_start_ordered_extent(struct inode *inode,
  103. struct btrfs_ordered_extent *entry, int wait);
  104. void btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len);
  105. struct btrfs_ordered_extent *
  106. btrfs_lookup_first_ordered_extent(struct inode * inode, u64 file_offset);
  107. int btrfs_ordered_update_i_size(struct inode *inode,
  108. struct btrfs_ordered_extent *ordered);
  109. int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u32 *sum);
  110. #endif