raid1.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. struct r1conf {
  48. struct mddev *mddev;
  49. struct raid1_info *mirrors; /* twice 'raid_disks' to
  50. * allow for replacements.
  51. */
  52. int raid_disks;
  53. int nonrot_disks;
  54. spinlock_t device_lock;
  55. /* list of 'struct r1bio' that need to be processed by raid1d,
  56. * whether to retry a read, writeout a resync or recovery
  57. * block, or anything else.
  58. */
  59. struct list_head retry_list;
  60. /* A separate list of r1bio which just need raid_end_bio_io called.
  61. * This mustn't happen for writes which had any errors if the superblock
  62. * needs to be written.
  63. */
  64. struct list_head bio_end_io_list;
  65. /* queue pending writes to be submitted on unplug */
  66. struct bio_list pending_bio_list;
  67. /* for use when syncing mirrors:
  68. * We don't allow both normal IO and resync/recovery IO at
  69. * the same time - resync/recovery can only happen when there
  70. * is no other IO. So when either is active, the other has to wait.
  71. * See more details description in raid1.c near raise_barrier().
  72. */
  73. wait_queue_head_t wait_barrier;
  74. spinlock_t resync_lock;
  75. atomic_t nr_sync_pending;
  76. atomic_t *nr_pending;
  77. atomic_t *nr_waiting;
  78. atomic_t *nr_queued;
  79. atomic_t *barrier;
  80. int array_frozen;
  81. /* Set to 1 if a full sync is needed, (fresh device added).
  82. * Cleared when a sync completes.
  83. */
  84. int fullsync;
  85. mempool_t *r1bio_pool;
  86. mempool_t r1buf_pool;
  87. struct bio_set bio_split;
  88. /* temporary buffer to synchronous IO when attempting to repair
  89. * a read error.
  90. */
  91. struct page *tmppage;
  92. /* When taking over an array from a different personality, we store
  93. * the new thread here until we fully activate the array.
  94. */
  95. struct md_thread __rcu *thread;
  96. /* Keep track of cluster resync window to send to other
  97. * nodes.
  98. */
  99. sector_t cluster_sync_low;
  100. sector_t cluster_sync_high;
  101. };
  102. /*
  103. * this is our 'private' RAID1 bio.
  104. *
  105. * it contains information about what kind of IO operations were started
  106. * for this RAID1 operation, and about their status:
  107. */
  108. struct r1bio {
  109. atomic_t remaining; /* 'have we finished' count,
  110. * used from IRQ handlers
  111. */
  112. atomic_t behind_remaining; /* number of write-behind ios remaining
  113. * in this BehindIO request
  114. */
  115. sector_t sector;
  116. int sectors;
  117. unsigned long state;
  118. struct mddev *mddev;
  119. /*
  120. * original bio going to /dev/mdx
  121. */
  122. struct bio *master_bio;
  123. /*
  124. * if the IO is in READ direction, then this is where we read
  125. */
  126. int read_disk;
  127. struct list_head retry_list;
  128. /*
  129. * When R1BIO_BehindIO is set, we store pages for write behind
  130. * in behind_master_bio.
  131. */
  132. struct bio *behind_master_bio;
  133. /*
  134. * if the IO is in WRITE direction, then multiple bios are used.
  135. * We choose the number when they are allocated.
  136. */
  137. struct bio *bios[];
  138. /* DO NOT PUT ANY NEW FIELDS HERE - bios array is contiguously alloced*/
  139. };
  140. /* bits for r1bio.state */
  141. enum r1bio_state {
  142. R1BIO_Uptodate,
  143. R1BIO_IsSync,
  144. R1BIO_BehindIO,
  145. /* Set ReadError on bios that experience a readerror so that
  146. * raid1d knows what to do with them.
  147. */
  148. R1BIO_ReadError,
  149. /* For write-behind requests, we call bi_end_io when
  150. * the last non-write-behind device completes, providing
  151. * any write was successful. Otherwise we call when
  152. * any write-behind write succeeds, otherwise we call
  153. * with failure when last write completes (and all failed).
  154. *
  155. * And for bio_split errors, record that bi_end_io was called
  156. * with this flag...
  157. */
  158. R1BIO_Returned,
  159. /* If a write for this request means we can clear some
  160. * known-bad-block records, we set this flag
  161. */
  162. R1BIO_MadeGood,
  163. R1BIO_WriteError,
  164. R1BIO_FailFast,
  165. };
  166. static inline int sector_to_idx(sector_t sector)
  167. {
  168. return hash_long(sector >> BARRIER_UNIT_SECTOR_BITS,
  169. BARRIER_BUCKETS_NR_BITS);
  170. }
  171. #endif