raid1.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _RAID1_H
  3. #define _RAID1_H
  4. /*
  5. * each barrier unit size is 64MB fow now
  6. * note: it must be larger than RESYNC_DEPTH
  7. */
  8. #define BARRIER_UNIT_SECTOR_BITS 17
  9. #define BARRIER_UNIT_SECTOR_SIZE (1<<17)
  10. /*
  11. * In struct r1conf, the following members are related to I/O barrier
  12. * buckets,
  13. * atomic_t *nr_pending;
  14. * atomic_t *nr_waiting;
  15. * atomic_t *nr_queued;
  16. * atomic_t *barrier;
  17. * Each of them points to array of atomic_t variables, each array is
  18. * designed to have BARRIER_BUCKETS_NR elements and occupy a single
  19. * memory page. The data width of atomic_t variables is 4 bytes, equal
  20. * to 1<<(ilog2(sizeof(atomic_t))), BARRIER_BUCKETS_NR_BITS is defined
  21. * as (PAGE_SHIFT - ilog2(sizeof(int))) to make sure an array of
  22. * atomic_t variables with BARRIER_BUCKETS_NR elements just exactly
  23. * occupies a single memory page.
  24. */
  25. #define BARRIER_BUCKETS_NR_BITS (PAGE_SHIFT - ilog2(sizeof(atomic_t)))
  26. #define BARRIER_BUCKETS_NR (1<<BARRIER_BUCKETS_NR_BITS)
  27. struct raid1_info {
  28. struct md_rdev *rdev;
  29. sector_t head_position;
  30. /* When choose the best device for a read (read_balance())
  31. * we try to keep sequential reads one the same device
  32. */
  33. sector_t next_seq_sect;
  34. sector_t seq_start;
  35. };
  36. /*
  37. * memory pools need a pointer to the mddev, so they can force an unplug
  38. * when memory is tight, and a count of the number of drives that the
  39. * pool was allocated for, so they know how much to allocate and free.
  40. * mddev->raid_disks cannot be used, as it can change while a pool is active
  41. * These two datums are stored in a kmalloced struct.
  42. * The 'raid_disks' here is twice the raid_disks in r1conf.
  43. * This allows space for each 'real' device can have a replacement in the
  44. * second half of the array.
  45. */
  46. struct pool_info {
  47. struct mddev *mddev;
  48. int raid_disks;
  49. };
  50. struct r1conf {
  51. struct mddev *mddev;
  52. struct raid1_info *mirrors; /* twice 'raid_disks' to
  53. * allow for replacements.
  54. */
  55. int raid_disks;
  56. spinlock_t device_lock;
  57. /* list of 'struct r1bio' that need to be processed by raid1d,
  58. * whether to retry a read, writeout a resync or recovery
  59. * block, or anything else.
  60. */
  61. struct list_head retry_list;
  62. /* A separate list of r1bio which just need raid_end_bio_io called.
  63. * This mustn't happen for writes which had any errors if the superblock
  64. * needs to be written.
  65. */
  66. struct list_head bio_end_io_list;
  67. /* queue pending writes to be submitted on unplug */
  68. struct bio_list pending_bio_list;
  69. int pending_count;
  70. /* for use when syncing mirrors:
  71. * We don't allow both normal IO and resync/recovery IO at
  72. * the same time - resync/recovery can only happen when there
  73. * is no other IO. So when either is active, the other has to wait.
  74. * See more details description in raid1.c near raise_barrier().
  75. */
  76. wait_queue_head_t wait_barrier;
  77. spinlock_t resync_lock;
  78. atomic_t nr_sync_pending;
  79. atomic_t *nr_pending;
  80. atomic_t *nr_waiting;
  81. atomic_t *nr_queued;
  82. atomic_t *barrier;
  83. int array_frozen;
  84. /* Set to 1 if a full sync is needed, (fresh device added).
  85. * Cleared when a sync completes.
  86. */
  87. int fullsync;
  88. /* When the same as mddev->recovery_disabled we don't allow
  89. * recovery to be attempted as we expect a read error.
  90. */
  91. int recovery_disabled;
  92. /* poolinfo contains information about the content of the
  93. * mempools - it changes when the array grows or shrinks
  94. */
  95. struct pool_info *poolinfo;
  96. mempool_t *r1bio_pool;
  97. mempool_t *r1buf_pool;
  98. struct bio_set *bio_split;
  99. /* temporary buffer to synchronous IO when attempting to repair
  100. * a read error.
  101. */
  102. struct page *tmppage;
  103. /* When taking over an array from a different personality, we store
  104. * the new thread here until we fully activate the array.
  105. */
  106. struct md_thread *thread;
  107. /* Keep track of cluster resync window to send to other
  108. * nodes.
  109. */
  110. sector_t cluster_sync_low;
  111. sector_t cluster_sync_high;
  112. };
  113. /*
  114. * this is our 'private' RAID1 bio.
  115. *
  116. * it contains information about what kind of IO operations were started
  117. * for this RAID1 operation, and about their status:
  118. */
  119. struct r1bio {
  120. atomic_t remaining; /* 'have we finished' count,
  121. * used from IRQ handlers
  122. */
  123. atomic_t behind_remaining; /* number of write-behind ios remaining
  124. * in this BehindIO request
  125. */
  126. sector_t sector;
  127. int sectors;
  128. unsigned long state;
  129. struct mddev *mddev;
  130. /*
  131. * original bio going to /dev/mdx
  132. */
  133. struct bio *master_bio;
  134. /*
  135. * if the IO is in READ direction, then this is where we read
  136. */
  137. int read_disk;
  138. struct list_head retry_list;
  139. /*
  140. * When R1BIO_BehindIO is set, we store pages for write behind
  141. * in behind_master_bio.
  142. */
  143. struct bio *behind_master_bio;
  144. /*
  145. * if the IO is in WRITE direction, then multiple bios are used.
  146. * We choose the number when they are allocated.
  147. */
  148. struct bio *bios[0];
  149. /* DO NOT PUT ANY NEW FIELDS HERE - bios array is contiguously alloced*/
  150. };
  151. /* bits for r1bio.state */
  152. enum r1bio_state {
  153. R1BIO_Uptodate,
  154. R1BIO_IsSync,
  155. R1BIO_Degraded,
  156. R1BIO_BehindIO,
  157. /* Set ReadError on bios that experience a readerror so that
  158. * raid1d knows what to do with them.
  159. */
  160. R1BIO_ReadError,
  161. /* For write-behind requests, we call bi_end_io when
  162. * the last non-write-behind device completes, providing
  163. * any write was successful. Otherwise we call when
  164. * any write-behind write succeeds, otherwise we call
  165. * with failure when last write completes (and all failed).
  166. * Record that bi_end_io was called with this flag...
  167. */
  168. R1BIO_Returned,
  169. /* If a write for this request means we can clear some
  170. * known-bad-block records, we set this flag
  171. */
  172. R1BIO_MadeGood,
  173. R1BIO_WriteError,
  174. R1BIO_FailFast,
  175. };
  176. static inline int sector_to_idx(sector_t sector)
  177. {
  178. return hash_long(sector >> BARRIER_UNIT_SECTOR_BITS,
  179. BARRIER_BUCKETS_NR_BITS);
  180. }
  181. #endif