raid56.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2012 Fusion-io All rights reserved.
  4. * Copyright (C) 2012 Intel Corp. All rights reserved.
  5. */
  6. #ifndef BTRFS_RAID56_H
  7. #define BTRFS_RAID56_H
  8. #include <linux/types.h>
  9. #include <linux/list.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/bio.h>
  12. #include <linux/refcount.h>
  13. #include <linux/workqueue.h>
  14. #include "volumes.h"
  15. struct page;
  16. struct sector_ptr;
  17. struct btrfs_fs_info;
  18. enum btrfs_rbio_ops {
  19. BTRFS_RBIO_WRITE,
  20. BTRFS_RBIO_READ_REBUILD,
  21. BTRFS_RBIO_PARITY_SCRUB,
  22. };
  23. struct btrfs_raid_bio {
  24. struct btrfs_io_context *bioc;
  25. /*
  26. * While we're doing RMW on a stripe we put it into a hash table so we
  27. * can lock the stripe and merge more rbios into it.
  28. */
  29. struct list_head hash_list;
  30. /* LRU list for the stripe cache */
  31. struct list_head stripe_cache;
  32. /* For scheduling work in the helper threads */
  33. struct work_struct work;
  34. /*
  35. * bio_list and bio_list_lock are used to add more bios into the stripe
  36. * in hopes of avoiding the full RMW
  37. */
  38. struct bio_list bio_list;
  39. spinlock_t bio_list_lock;
  40. /*
  41. * Also protected by the bio_list_lock, the plug list is used by the
  42. * plugging code to collect partial bios while plugged. The stripe
  43. * locking code also uses it to hand off the stripe lock to the next
  44. * pending IO.
  45. */
  46. struct list_head plug_list;
  47. /* Flags that tell us if it is safe to merge with this bio. */
  48. unsigned long flags;
  49. /*
  50. * Set if we're doing a parity rebuild for a read from higher up, which
  51. * is handled differently from a parity rebuild as part of RMW.
  52. */
  53. enum btrfs_rbio_ops operation;
  54. /* How many pages there are for the full stripe including P/Q */
  55. u16 nr_pages;
  56. /* How many sectors there are for the full stripe including P/Q */
  57. u16 nr_sectors;
  58. /* Number of data stripes (no p/q) */
  59. u8 nr_data;
  60. /* Number of all stripes (including P/Q) */
  61. u8 real_stripes;
  62. /* How many pages there are for each stripe */
  63. u8 stripe_npages;
  64. /* How many sectors there are for each stripe */
  65. u8 stripe_nsectors;
  66. /* Stripe number that we're scrubbing */
  67. u8 scrubp;
  68. /*
  69. * Size of all the bios in the bio_list. This helps us decide if the
  70. * rbio maps to a full stripe or not.
  71. */
  72. int bio_list_bytes;
  73. refcount_t refs;
  74. atomic_t stripes_pending;
  75. wait_queue_head_t io_wait;
  76. /* Bitmap to record which horizontal stripe has data */
  77. unsigned long dbitmap;
  78. /* Allocated with stripe_nsectors-many bits for finish_*() calls */
  79. unsigned long finish_pbitmap;
  80. /*
  81. * These are two arrays of pointers. We allocate the rbio big enough
  82. * to hold them both and setup their locations when the rbio is
  83. * allocated.
  84. */
  85. /*
  86. * Pointers to pages that we allocated for reading/writing stripes
  87. * directly from the disk (including P/Q).
  88. */
  89. struct page **stripe_pages;
  90. /* Pointers to the sectors in the bio_list, for faster lookup */
  91. struct sector_ptr *bio_sectors;
  92. /*
  93. * For subpage support, we need to map each sector to above
  94. * stripe_pages.
  95. */
  96. struct sector_ptr *stripe_sectors;
  97. /* Allocated with real_stripes-many pointers for finish_*() calls */
  98. void **finish_pointers;
  99. /*
  100. * The bitmap recording where IO errors happened.
  101. * Each bit is corresponding to one sector in either bio_sectors[] or
  102. * stripe_sectors[] array.
  103. *
  104. * The reason we don't use another bit in sector_ptr is, we have two
  105. * arrays of sectors, and a lot of IO can use sectors in both arrays.
  106. * Thus making it much harder to iterate.
  107. */
  108. unsigned long *error_bitmap;
  109. /*
  110. * Checksum buffer if the rbio is for data. The buffer should cover
  111. * all data sectors (excluding P/Q sectors).
  112. */
  113. u8 *csum_buf;
  114. /*
  115. * Each bit represents if the corresponding sector has data csum found.
  116. * Should only cover data sectors (excluding P/Q sectors).
  117. */
  118. unsigned long *csum_bitmap;
  119. };
  120. /*
  121. * For trace event usage only. Records useful debug info for each bio submitted
  122. * by RAID56 to each physical device.
  123. *
  124. * No matter signed or not, (-1) is always the one indicating we can not grab
  125. * the proper stripe number.
  126. */
  127. struct raid56_bio_trace_info {
  128. u64 devid;
  129. /* The offset inside the stripe. (<= STRIPE_LEN) */
  130. u32 offset;
  131. /*
  132. * Stripe number.
  133. * 0 is the first data stripe, and nr_data for P stripe,
  134. * nr_data + 1 for Q stripe.
  135. * >= real_stripes for
  136. */
  137. u8 stripe_nr;
  138. };
  139. static inline int nr_data_stripes(const struct btrfs_chunk_map *map)
  140. {
  141. return map->num_stripes - btrfs_nr_parity_stripes(map->type);
  142. }
  143. static inline int nr_bioc_data_stripes(const struct btrfs_io_context *bioc)
  144. {
  145. return bioc->num_stripes - btrfs_nr_parity_stripes(bioc->map_type);
  146. }
  147. #define RAID5_P_STRIPE ((u64)-2)
  148. #define RAID6_Q_STRIPE ((u64)-1)
  149. #define is_parity_stripe(x) (((x) == RAID5_P_STRIPE) || \
  150. ((x) == RAID6_Q_STRIPE))
  151. struct btrfs_device;
  152. void raid56_parity_recover(struct bio *bio, struct btrfs_io_context *bioc,
  153. int mirror_num);
  154. void raid56_parity_write(struct bio *bio, struct btrfs_io_context *bioc);
  155. struct btrfs_raid_bio *raid56_parity_alloc_scrub_rbio(struct bio *bio,
  156. struct btrfs_io_context *bioc,
  157. struct btrfs_device *scrub_dev,
  158. unsigned long *dbitmap, int stripe_nsectors);
  159. void raid56_parity_submit_scrub_rbio(struct btrfs_raid_bio *rbio);
  160. void raid56_parity_cache_data_pages(struct btrfs_raid_bio *rbio,
  161. struct page **data_pages, u64 data_logical);
  162. int btrfs_alloc_stripe_hash_table(struct btrfs_fs_info *info);
  163. void btrfs_free_stripe_hash_table(struct btrfs_fs_info *info);
  164. #endif