raid1.h 6.3 KB

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