raid1-10.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Maximum size of each resync request */
  3. #define RESYNC_BLOCK_SIZE (64*1024)
  4. #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
  5. /*
  6. * Number of guaranteed raid bios in case of extreme VM load:
  7. */
  8. #define NR_RAID_BIOS 256
  9. /* when we get a read error on a read-only array, we redirect to another
  10. * device without failing the first device, or trying to over-write to
  11. * correct the read error. To keep track of bad blocks on a per-bio
  12. * level, we store IO_BLOCKED in the appropriate 'bios' pointer
  13. */
  14. #define IO_BLOCKED ((struct bio *)1)
  15. /* When we successfully write to a known bad-block, we need to remove the
  16. * bad-block marking which must be done from process context. So we record
  17. * the success by setting devs[n].bio to IO_MADE_GOOD
  18. */
  19. #define IO_MADE_GOOD ((struct bio *)2)
  20. #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
  21. #define MAX_PLUG_BIO 32
  22. /* for managing resync I/O pages */
  23. struct resync_pages {
  24. void *raid_bio;
  25. struct page *pages[RESYNC_PAGES];
  26. };
  27. struct raid1_plug_cb {
  28. struct blk_plug_cb cb;
  29. struct bio_list pending;
  30. unsigned int count;
  31. };
  32. static void rbio_pool_free(void *rbio, void *data)
  33. {
  34. kfree(rbio);
  35. }
  36. static inline int resync_alloc_pages(struct resync_pages *rp,
  37. gfp_t gfp_flags)
  38. {
  39. int i;
  40. for (i = 0; i < RESYNC_PAGES; i++) {
  41. rp->pages[i] = alloc_page(gfp_flags);
  42. if (!rp->pages[i])
  43. goto out_free;
  44. }
  45. return 0;
  46. out_free:
  47. while (--i >= 0)
  48. put_page(rp->pages[i]);
  49. return -ENOMEM;
  50. }
  51. static inline void resync_free_pages(struct resync_pages *rp)
  52. {
  53. int i;
  54. for (i = 0; i < RESYNC_PAGES; i++)
  55. put_page(rp->pages[i]);
  56. }
  57. static inline void resync_get_all_pages(struct resync_pages *rp)
  58. {
  59. int i;
  60. for (i = 0; i < RESYNC_PAGES; i++)
  61. get_page(rp->pages[i]);
  62. }
  63. static inline struct page *resync_fetch_page(struct resync_pages *rp,
  64. unsigned idx)
  65. {
  66. if (WARN_ON_ONCE(idx >= RESYNC_PAGES))
  67. return NULL;
  68. return rp->pages[idx];
  69. }
  70. /*
  71. * 'strct resync_pages' stores actual pages used for doing the resync
  72. * IO, and it is per-bio, so make .bi_private points to it.
  73. */
  74. static inline struct resync_pages *get_resync_pages(struct bio *bio)
  75. {
  76. return bio->bi_private;
  77. }
  78. /* generally called after bio_reset() for reseting bvec */
  79. static void md_bio_reset_resync_pages(struct bio *bio, struct resync_pages *rp,
  80. int size)
  81. {
  82. int idx = 0;
  83. /* initialize bvec table again */
  84. do {
  85. struct page *page = resync_fetch_page(rp, idx);
  86. int len = min_t(int, size, PAGE_SIZE);
  87. if (WARN_ON(!bio_add_page(bio, page, len, 0))) {
  88. bio->bi_status = BLK_STS_RESOURCE;
  89. bio_endio(bio);
  90. return;
  91. }
  92. size -= len;
  93. } while (idx++ < RESYNC_PAGES && size > 0);
  94. }
  95. static inline void raid1_submit_write(struct bio *bio)
  96. {
  97. struct md_rdev *rdev = (void *)bio->bi_bdev;
  98. bio->bi_next = NULL;
  99. bio_set_dev(bio, rdev->bdev);
  100. if (test_bit(Faulty, &rdev->flags))
  101. bio_io_error(bio);
  102. else if (unlikely(bio_op(bio) == REQ_OP_DISCARD &&
  103. !bdev_max_discard_sectors(bio->bi_bdev)))
  104. /* Just ignore it */
  105. bio_endio(bio);
  106. else
  107. submit_bio_noacct(bio);
  108. }
  109. static inline bool raid1_add_bio_to_plug(struct mddev *mddev, struct bio *bio,
  110. blk_plug_cb_fn unplug, int copies)
  111. {
  112. struct raid1_plug_cb *plug = NULL;
  113. struct blk_plug_cb *cb;
  114. /*
  115. * If bitmap is not enabled, it's safe to submit the io directly, and
  116. * this can get optimal performance.
  117. */
  118. if (!mddev->bitmap_ops->enabled(mddev)) {
  119. raid1_submit_write(bio);
  120. return true;
  121. }
  122. cb = blk_check_plugged(unplug, mddev, sizeof(*plug));
  123. if (!cb)
  124. return false;
  125. plug = container_of(cb, struct raid1_plug_cb, cb);
  126. bio_list_add(&plug->pending, bio);
  127. if (++plug->count / MAX_PLUG_BIO >= copies) {
  128. list_del(&cb->list);
  129. cb->callback(cb, false);
  130. }
  131. return true;
  132. }
  133. /*
  134. * current->bio_list will be set under submit_bio() context, in this case bitmap
  135. * io will be added to the list and wait for current io submission to finish,
  136. * while current io submission must wait for bitmap io to be done. In order to
  137. * avoid such deadlock, submit bitmap io asynchronously.
  138. */
  139. static inline void raid1_prepare_flush_writes(struct mddev *mddev)
  140. {
  141. mddev->bitmap_ops->unplug(mddev, current->bio_list == NULL);
  142. }
  143. /*
  144. * Used by fix_read_error() to decay the per rdev read_errors.
  145. * We halve the read error count for every hour that has elapsed
  146. * since the last recorded read error.
  147. */
  148. static inline void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
  149. {
  150. long cur_time_mon;
  151. unsigned long hours_since_last;
  152. unsigned int read_errors = atomic_read(&rdev->read_errors);
  153. cur_time_mon = ktime_get_seconds();
  154. if (rdev->last_read_error == 0) {
  155. /* first time we've seen a read error */
  156. rdev->last_read_error = cur_time_mon;
  157. return;
  158. }
  159. hours_since_last = (long)(cur_time_mon -
  160. rdev->last_read_error) / 3600;
  161. rdev->last_read_error = cur_time_mon;
  162. /*
  163. * if hours_since_last is > the number of bits in read_errors
  164. * just set read errors to 0. We do this to avoid
  165. * overflowing the shift of read_errors by hours_since_last.
  166. */
  167. if (hours_since_last >= 8 * sizeof(read_errors))
  168. atomic_set(&rdev->read_errors, 0);
  169. else
  170. atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
  171. }
  172. static inline bool exceed_read_errors(struct mddev *mddev, struct md_rdev *rdev)
  173. {
  174. int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
  175. int read_errors;
  176. check_decay_read_errors(mddev, rdev);
  177. read_errors = atomic_inc_return(&rdev->read_errors);
  178. if (read_errors > max_read_errors) {
  179. pr_notice("md/"RAID_1_10_NAME":%s: %pg: Raid device exceeded read_error threshold [cur %d:max %d]\n",
  180. mdname(mddev), rdev->bdev, read_errors, max_read_errors);
  181. pr_notice("md/"RAID_1_10_NAME":%s: %pg: Failing raid device\n",
  182. mdname(mddev), rdev->bdev);
  183. md_error(mddev, rdev);
  184. return true;
  185. }
  186. return false;
  187. }
  188. /**
  189. * raid1_check_read_range() - check a given read range for bad blocks,
  190. * available read length is returned;
  191. * @rdev: the rdev to read;
  192. * @this_sector: read position;
  193. * @len: read length;
  194. *
  195. * helper function for read_balance()
  196. *
  197. * 1) If there are no bad blocks in the range, @len is returned;
  198. * 2) If the range are all bad blocks, 0 is returned;
  199. * 3) If there are partial bad blocks:
  200. * - If the bad block range starts after @this_sector, the length of first
  201. * good region is returned;
  202. * - If the bad block range starts before @this_sector, 0 is returned and
  203. * the @len is updated to the offset into the region before we get to the
  204. * good blocks;
  205. */
  206. static inline int raid1_check_read_range(struct md_rdev *rdev,
  207. sector_t this_sector, int *len)
  208. {
  209. sector_t first_bad;
  210. int bad_sectors;
  211. /* no bad block overlap */
  212. if (!is_badblock(rdev, this_sector, *len, &first_bad, &bad_sectors))
  213. return *len;
  214. /*
  215. * bad block range starts offset into our range so we can return the
  216. * number of sectors before the bad blocks start.
  217. */
  218. if (first_bad > this_sector)
  219. return first_bad - this_sector;
  220. /* read range is fully consumed by bad blocks. */
  221. if (this_sector + *len <= first_bad + bad_sectors)
  222. return 0;
  223. /*
  224. * final case, bad block range starts before or at the start of our
  225. * range but does not cover our entire range so we still return 0 but
  226. * update the length with the number of sectors before we get to the
  227. * good ones.
  228. */
  229. *len = first_bad + bad_sectors - this_sector;
  230. return 0;
  231. }
  232. /*
  233. * Check if read should choose the first rdev.
  234. *
  235. * Balance on the whole device if no resync is going on (recovery is ok) or
  236. * below the resync window. Otherwise, take the first readable disk.
  237. */
  238. static inline bool raid1_should_read_first(struct mddev *mddev,
  239. sector_t this_sector, int len)
  240. {
  241. if ((mddev->recovery_cp < this_sector + len))
  242. return true;
  243. if (mddev_is_clustered(mddev) &&
  244. md_cluster_ops->area_resyncing(mddev, READ, this_sector,
  245. this_sector + len))
  246. return true;
  247. return false;
  248. }
  249. /*
  250. * bio with REQ_RAHEAD or REQ_NOWAIT can fail at anytime, before such IO is
  251. * submitted to the underlying disks, hence don't record badblocks or retry
  252. * in this case.
  253. */
  254. static inline bool raid1_should_handle_error(struct bio *bio)
  255. {
  256. return !(bio->bi_opf & (REQ_RAHEAD | REQ_NOWAIT));
  257. }