PageRenderTime 33ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/md/raid10.c

https://github.com/mturquette/linux
C | 1752 lines | 1190 code | 172 blank | 390 comment | 236 complexity | be3daff5c92afcc342f88c2a0cd76729 MD5 | raw file
  1. /*
  2. * raid10.c : Multiple Devices driver for Linux
  3. *
  4. * Copyright (C) 2000-2004 Neil Brown
  5. *
  6. * RAID-10 support for md.
  7. *
  8. * Base on code in raid1.c. See raid1.c for further copyright information.
  9. *
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * (for example /usr/src/linux/COPYING); if not, write to the Free
  18. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/slab.h>
  21. #include <linux/delay.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/module.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/ratelimit.h>
  26. #include <linux/kthread.h>
  27. #include "md.h"
  28. #include "raid10.h"
  29. #include "raid0.h"
  30. #include "bitmap.h"
  31. /*
  32. * RAID10 provides a combination of RAID0 and RAID1 functionality.
  33. * The layout of data is defined by
  34. * chunk_size
  35. * raid_disks
  36. * near_copies (stored in low byte of layout)
  37. * far_copies (stored in second byte of layout)
  38. * far_offset (stored in bit 16 of layout )
  39. * use_far_sets (stored in bit 17 of layout )
  40. * use_far_sets_bugfixed (stored in bit 18 of layout )
  41. *
  42. * The data to be stored is divided into chunks using chunksize. Each device
  43. * is divided into far_copies sections. In each section, chunks are laid out
  44. * in a style similar to raid0, but near_copies copies of each chunk is stored
  45. * (each on a different drive). The starting device for each section is offset
  46. * near_copies from the starting device of the previous section. Thus there
  47. * are (near_copies * far_copies) of each chunk, and each is on a different
  48. * drive. near_copies and far_copies must be at least one, and their product
  49. * is at most raid_disks.
  50. *
  51. * If far_offset is true, then the far_copies are handled a bit differently.
  52. * The copies are still in different stripes, but instead of being very far
  53. * apart on disk, there are adjacent stripes.
  54. *
  55. * The far and offset algorithms are handled slightly differently if
  56. * 'use_far_sets' is true. In this case, the array's devices are grouped into
  57. * sets that are (near_copies * far_copies) in size. The far copied stripes
  58. * are still shifted by 'near_copies' devices, but this shifting stays confined
  59. * to the set rather than the entire array. This is done to improve the number
  60. * of device combinations that can fail without causing the array to fail.
  61. * Example 'far' algorithm w/o 'use_far_sets' (each letter represents a chunk
  62. * on a device):
  63. * A B C D A B C D E
  64. * ... ...
  65. * D A B C E A B C D
  66. * Example 'far' algorithm w/ 'use_far_sets' enabled (sets illustrated w/ []'s):
  67. * [A B] [C D] [A B] [C D E]
  68. * |...| |...| |...| | ... |
  69. * [B A] [D C] [B A] [E C D]
  70. */
  71. /*
  72. * Number of guaranteed r10bios in case of extreme VM load:
  73. */
  74. #define NR_RAID10_BIOS 256
  75. /* when we get a read error on a read-only array, we redirect to another
  76. * device without failing the first device, or trying to over-write to
  77. * correct the read error. To keep track of bad blocks on a per-bio
  78. * level, we store IO_BLOCKED in the appropriate 'bios' pointer
  79. */
  80. #define IO_BLOCKED ((struct bio *)1)
  81. /* When we successfully write to a known bad-block, we need to remove the
  82. * bad-block marking which must be done from process context. So we record
  83. * the success by setting devs[n].bio to IO_MADE_GOOD
  84. */
  85. #define IO_MADE_GOOD ((struct bio *)2)
  86. #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
  87. /* When there are this many requests queued to be written by
  88. * the raid10 thread, we become 'congested' to provide back-pressure
  89. * for writeback.
  90. */
  91. static int max_queued_requests = 1024;
  92. static void allow_barrier(struct r10conf *conf);
  93. static void lower_barrier(struct r10conf *conf);
  94. static int _enough(struct r10conf *conf, int previous, int ignore);
  95. static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
  96. int *skipped);
  97. static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio);
  98. static void end_reshape_write(struct bio *bio);
  99. static void end_reshape(struct r10conf *conf);
  100. static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
  101. {
  102. struct r10conf *conf = data;
  103. int size = offsetof(struct r10bio, devs[conf->copies]);
  104. /* allocate a r10bio with room for raid_disks entries in the
  105. * bios array */
  106. return kzalloc(size, gfp_flags);
  107. }
  108. static void r10bio_pool_free(void *r10_bio, void *data)
  109. {
  110. kfree(r10_bio);
  111. }
  112. /* Maximum size of each resync request */
  113. #define RESYNC_BLOCK_SIZE (64*1024)
  114. #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
  115. /* amount of memory to reserve for resync requests */
  116. #define RESYNC_WINDOW (1024*1024)
  117. /* maximum number of concurrent requests, memory permitting */
  118. #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
  119. /*
  120. * When performing a resync, we need to read and compare, so
  121. * we need as many pages are there are copies.
  122. * When performing a recovery, we need 2 bios, one for read,
  123. * one for write (we recover only one drive per r10buf)
  124. *
  125. */
  126. static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
  127. {
  128. struct r10conf *conf = data;
  129. struct page *page;
  130. struct r10bio *r10_bio;
  131. struct bio *bio;
  132. int i, j;
  133. int nalloc;
  134. r10_bio = r10bio_pool_alloc(gfp_flags, conf);
  135. if (!r10_bio)
  136. return NULL;
  137. if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
  138. test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
  139. nalloc = conf->copies; /* resync */
  140. else
  141. nalloc = 2; /* recovery */
  142. /*
  143. * Allocate bios.
  144. */
  145. for (j = nalloc ; j-- ; ) {
  146. bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
  147. if (!bio)
  148. goto out_free_bio;
  149. r10_bio->devs[j].bio = bio;
  150. if (!conf->have_replacement)
  151. continue;
  152. bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
  153. if (!bio)
  154. goto out_free_bio;
  155. r10_bio->devs[j].repl_bio = bio;
  156. }
  157. /*
  158. * Allocate RESYNC_PAGES data pages and attach them
  159. * where needed.
  160. */
  161. for (j = 0 ; j < nalloc; j++) {
  162. struct bio *rbio = r10_bio->devs[j].repl_bio;
  163. bio = r10_bio->devs[j].bio;
  164. for (i = 0; i < RESYNC_PAGES; i++) {
  165. if (j > 0 && !test_bit(MD_RECOVERY_SYNC,
  166. &conf->mddev->recovery)) {
  167. /* we can share bv_page's during recovery
  168. * and reshape */
  169. struct bio *rbio = r10_bio->devs[0].bio;
  170. page = rbio->bi_io_vec[i].bv_page;
  171. get_page(page);
  172. } else
  173. page = alloc_page(gfp_flags);
  174. if (unlikely(!page))
  175. goto out_free_pages;
  176. bio->bi_io_vec[i].bv_page = page;
  177. if (rbio)
  178. rbio->bi_io_vec[i].bv_page = page;
  179. }
  180. }
  181. return r10_bio;
  182. out_free_pages:
  183. for ( ; i > 0 ; i--)
  184. safe_put_page(bio->bi_io_vec[i-1].bv_page);
  185. while (j--)
  186. for (i = 0; i < RESYNC_PAGES ; i++)
  187. safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
  188. j = 0;
  189. out_free_bio:
  190. for ( ; j < nalloc; j++) {
  191. if (r10_bio->devs[j].bio)
  192. bio_put(r10_bio->devs[j].bio);
  193. if (r10_bio->devs[j].repl_bio)
  194. bio_put(r10_bio->devs[j].repl_bio);
  195. }
  196. r10bio_pool_free(r10_bio, conf);
  197. return NULL;
  198. }
  199. static void r10buf_pool_free(void *__r10_bio, void *data)
  200. {
  201. int i;
  202. struct r10conf *conf = data;
  203. struct r10bio *r10bio = __r10_bio;
  204. int j;
  205. for (j=0; j < conf->copies; j++) {
  206. struct bio *bio = r10bio->devs[j].bio;
  207. if (bio) {
  208. for (i = 0; i < RESYNC_PAGES; i++) {
  209. safe_put_page(bio->bi_io_vec[i].bv_page);
  210. bio->bi_io_vec[i].bv_page = NULL;
  211. }
  212. bio_put(bio);
  213. }
  214. bio = r10bio->devs[j].repl_bio;
  215. if (bio)
  216. bio_put(bio);
  217. }
  218. r10bio_pool_free(r10bio, conf);
  219. }
  220. static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
  221. {
  222. int i;
  223. for (i = 0; i < conf->copies; i++) {
  224. struct bio **bio = & r10_bio->devs[i].bio;
  225. if (!BIO_SPECIAL(*bio))
  226. bio_put(*bio);
  227. *bio = NULL;
  228. bio = &r10_bio->devs[i].repl_bio;
  229. if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
  230. bio_put(*bio);
  231. *bio = NULL;
  232. }
  233. }
  234. static void free_r10bio(struct r10bio *r10_bio)
  235. {
  236. struct r10conf *conf = r10_bio->mddev->private;
  237. put_all_bios(conf, r10_bio);
  238. mempool_free(r10_bio, conf->r10bio_pool);
  239. }
  240. static void put_buf(struct r10bio *r10_bio)
  241. {
  242. struct r10conf *conf = r10_bio->mddev->private;
  243. mempool_free(r10_bio, conf->r10buf_pool);
  244. lower_barrier(conf);
  245. }
  246. static void reschedule_retry(struct r10bio *r10_bio)
  247. {
  248. unsigned long flags;
  249. struct mddev *mddev = r10_bio->mddev;
  250. struct r10conf *conf = mddev->private;
  251. spin_lock_irqsave(&conf->device_lock, flags);
  252. list_add(&r10_bio->retry_list, &conf->retry_list);
  253. conf->nr_queued ++;
  254. spin_unlock_irqrestore(&conf->device_lock, flags);
  255. /* wake up frozen array... */
  256. wake_up(&conf->wait_barrier);
  257. md_wakeup_thread(mddev->thread);
  258. }
  259. /*
  260. * raid_end_bio_io() is called when we have finished servicing a mirrored
  261. * operation and are ready to return a success/failure code to the buffer
  262. * cache layer.
  263. */
  264. static void raid_end_bio_io(struct r10bio *r10_bio)
  265. {
  266. struct bio *bio = r10_bio->master_bio;
  267. int done;
  268. struct r10conf *conf = r10_bio->mddev->private;
  269. if (bio->bi_phys_segments) {
  270. unsigned long flags;
  271. spin_lock_irqsave(&conf->device_lock, flags);
  272. bio->bi_phys_segments--;
  273. done = (bio->bi_phys_segments == 0);
  274. spin_unlock_irqrestore(&conf->device_lock, flags);
  275. } else
  276. done = 1;
  277. if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
  278. bio->bi_error = -EIO;
  279. if (done) {
  280. bio_endio(bio);
  281. /*
  282. * Wake up any possible resync thread that waits for the device
  283. * to go idle.
  284. */
  285. allow_barrier(conf);
  286. }
  287. free_r10bio(r10_bio);
  288. }
  289. /*
  290. * Update disk head position estimator based on IRQ completion info.
  291. */
  292. static inline void update_head_pos(int slot, struct r10bio *r10_bio)
  293. {
  294. struct r10conf *conf = r10_bio->mddev->private;
  295. conf->mirrors[r10_bio->devs[slot].devnum].head_position =
  296. r10_bio->devs[slot].addr + (r10_bio->sectors);
  297. }
  298. /*
  299. * Find the disk number which triggered given bio
  300. */
  301. static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
  302. struct bio *bio, int *slotp, int *replp)
  303. {
  304. int slot;
  305. int repl = 0;
  306. for (slot = 0; slot < conf->copies; slot++) {
  307. if (r10_bio->devs[slot].bio == bio)
  308. break;
  309. if (r10_bio->devs[slot].repl_bio == bio) {
  310. repl = 1;
  311. break;
  312. }
  313. }
  314. BUG_ON(slot == conf->copies);
  315. update_head_pos(slot, r10_bio);
  316. if (slotp)
  317. *slotp = slot;
  318. if (replp)
  319. *replp = repl;
  320. return r10_bio->devs[slot].devnum;
  321. }
  322. static void raid10_end_read_request(struct bio *bio)
  323. {
  324. int uptodate = !bio->bi_error;
  325. struct r10bio *r10_bio = bio->bi_private;
  326. int slot, dev;
  327. struct md_rdev *rdev;
  328. struct r10conf *conf = r10_bio->mddev->private;
  329. slot = r10_bio->read_slot;
  330. dev = r10_bio->devs[slot].devnum;
  331. rdev = r10_bio->devs[slot].rdev;
  332. /*
  333. * this branch is our 'one mirror IO has finished' event handler:
  334. */
  335. update_head_pos(slot, r10_bio);
  336. if (uptodate) {
  337. /*
  338. * Set R10BIO_Uptodate in our master bio, so that
  339. * we will return a good error code to the higher
  340. * levels even if IO on some other mirrored buffer fails.
  341. *
  342. * The 'master' represents the composite IO operation to
  343. * user-side. So if something waits for IO, then it will
  344. * wait for the 'master' bio.
  345. */
  346. set_bit(R10BIO_Uptodate, &r10_bio->state);
  347. } else {
  348. /* If all other devices that store this block have
  349. * failed, we want to return the error upwards rather
  350. * than fail the last device. Here we redefine
  351. * "uptodate" to mean "Don't want to retry"
  352. */
  353. if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state),
  354. rdev->raid_disk))
  355. uptodate = 1;
  356. }
  357. if (uptodate) {
  358. raid_end_bio_io(r10_bio);
  359. rdev_dec_pending(rdev, conf->mddev);
  360. } else {
  361. /*
  362. * oops, read error - keep the refcount on the rdev
  363. */
  364. char b[BDEVNAME_SIZE];
  365. printk_ratelimited(KERN_ERR
  366. "md/raid10:%s: %s: rescheduling sector %llu\n",
  367. mdname(conf->mddev),
  368. bdevname(rdev->bdev, b),
  369. (unsigned long long)r10_bio->sector);
  370. set_bit(R10BIO_ReadError, &r10_bio->state);
  371. reschedule_retry(r10_bio);
  372. }
  373. }
  374. static void close_write(struct r10bio *r10_bio)
  375. {
  376. /* clear the bitmap if all writes complete successfully */
  377. bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
  378. r10_bio->sectors,
  379. !test_bit(R10BIO_Degraded, &r10_bio->state),
  380. 0);
  381. md_write_end(r10_bio->mddev);
  382. }
  383. static void one_write_done(struct r10bio *r10_bio)
  384. {
  385. if (atomic_dec_and_test(&r10_bio->remaining)) {
  386. if (test_bit(R10BIO_WriteError, &r10_bio->state))
  387. reschedule_retry(r10_bio);
  388. else {
  389. close_write(r10_bio);
  390. if (test_bit(R10BIO_MadeGood, &r10_bio->state))
  391. reschedule_retry(r10_bio);
  392. else
  393. raid_end_bio_io(r10_bio);
  394. }
  395. }
  396. }
  397. static void raid10_end_write_request(struct bio *bio)
  398. {
  399. struct r10bio *r10_bio = bio->bi_private;
  400. int dev;
  401. int dec_rdev = 1;
  402. struct r10conf *conf = r10_bio->mddev->private;
  403. int slot, repl;
  404. struct md_rdev *rdev = NULL;
  405. dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
  406. if (repl)
  407. rdev = conf->mirrors[dev].replacement;
  408. if (!rdev) {
  409. smp_rmb();
  410. repl = 0;
  411. rdev = conf->mirrors[dev].rdev;
  412. }
  413. /*
  414. * this branch is our 'one mirror IO has finished' event handler:
  415. */
  416. if (bio->bi_error) {
  417. if (repl)
  418. /* Never record new bad blocks to replacement,
  419. * just fail it.
  420. */
  421. md_error(rdev->mddev, rdev);
  422. else {
  423. set_bit(WriteErrorSeen, &rdev->flags);
  424. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  425. set_bit(MD_RECOVERY_NEEDED,
  426. &rdev->mddev->recovery);
  427. set_bit(R10BIO_WriteError, &r10_bio->state);
  428. dec_rdev = 0;
  429. }
  430. } else {
  431. /*
  432. * Set R10BIO_Uptodate in our master bio, so that
  433. * we will return a good error code for to the higher
  434. * levels even if IO on some other mirrored buffer fails.
  435. *
  436. * The 'master' represents the composite IO operation to
  437. * user-side. So if something waits for IO, then it will
  438. * wait for the 'master' bio.
  439. */
  440. sector_t first_bad;
  441. int bad_sectors;
  442. /*
  443. * Do not set R10BIO_Uptodate if the current device is
  444. * rebuilding or Faulty. This is because we cannot use
  445. * such device for properly reading the data back (we could
  446. * potentially use it, if the current write would have felt
  447. * before rdev->recovery_offset, but for simplicity we don't
  448. * check this here.
  449. */
  450. if (test_bit(In_sync, &rdev->flags) &&
  451. !test_bit(Faulty, &rdev->flags))
  452. set_bit(R10BIO_Uptodate, &r10_bio->state);
  453. /* Maybe we can clear some bad blocks. */
  454. if (is_badblock(rdev,
  455. r10_bio->devs[slot].addr,
  456. r10_bio->sectors,
  457. &first_bad, &bad_sectors)) {
  458. bio_put(bio);
  459. if (repl)
  460. r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
  461. else
  462. r10_bio->devs[slot].bio = IO_MADE_GOOD;
  463. dec_rdev = 0;
  464. set_bit(R10BIO_MadeGood, &r10_bio->state);
  465. }
  466. }
  467. /*
  468. *
  469. * Let's see if all mirrored write operations have finished
  470. * already.
  471. */
  472. one_write_done(r10_bio);
  473. if (dec_rdev)
  474. rdev_dec_pending(rdev, conf->mddev);
  475. }
  476. /*
  477. * RAID10 layout manager
  478. * As well as the chunksize and raid_disks count, there are two
  479. * parameters: near_copies and far_copies.
  480. * near_copies * far_copies must be <= raid_disks.
  481. * Normally one of these will be 1.
  482. * If both are 1, we get raid0.
  483. * If near_copies == raid_disks, we get raid1.
  484. *
  485. * Chunks are laid out in raid0 style with near_copies copies of the
  486. * first chunk, followed by near_copies copies of the next chunk and
  487. * so on.
  488. * If far_copies > 1, then after 1/far_copies of the array has been assigned
  489. * as described above, we start again with a device offset of near_copies.
  490. * So we effectively have another copy of the whole array further down all
  491. * the drives, but with blocks on different drives.
  492. * With this layout, and block is never stored twice on the one device.
  493. *
  494. * raid10_find_phys finds the sector offset of a given virtual sector
  495. * on each device that it is on.
  496. *
  497. * raid10_find_virt does the reverse mapping, from a device and a
  498. * sector offset to a virtual address
  499. */
  500. static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
  501. {
  502. int n,f;
  503. sector_t sector;
  504. sector_t chunk;
  505. sector_t stripe;
  506. int dev;
  507. int slot = 0;
  508. int last_far_set_start, last_far_set_size;
  509. last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
  510. last_far_set_start *= geo->far_set_size;
  511. last_far_set_size = geo->far_set_size;
  512. last_far_set_size += (geo->raid_disks % geo->far_set_size);
  513. /* now calculate first sector/dev */
  514. chunk = r10bio->sector >> geo->chunk_shift;
  515. sector = r10bio->sector & geo->chunk_mask;
  516. chunk *= geo->near_copies;
  517. stripe = chunk;
  518. dev = sector_div(stripe, geo->raid_disks);
  519. if (geo->far_offset)
  520. stripe *= geo->far_copies;
  521. sector += stripe << geo->chunk_shift;
  522. /* and calculate all the others */
  523. for (n = 0; n < geo->near_copies; n++) {
  524. int d = dev;
  525. int set;
  526. sector_t s = sector;
  527. r10bio->devs[slot].devnum = d;
  528. r10bio->devs[slot].addr = s;
  529. slot++;
  530. for (f = 1; f < geo->far_copies; f++) {
  531. set = d / geo->far_set_size;
  532. d += geo->near_copies;
  533. if ((geo->raid_disks % geo->far_set_size) &&
  534. (d > last_far_set_start)) {
  535. d -= last_far_set_start;
  536. d %= last_far_set_size;
  537. d += last_far_set_start;
  538. } else {
  539. d %= geo->far_set_size;
  540. d += geo->far_set_size * set;
  541. }
  542. s += geo->stride;
  543. r10bio->devs[slot].devnum = d;
  544. r10bio->devs[slot].addr = s;
  545. slot++;
  546. }
  547. dev++;
  548. if (dev >= geo->raid_disks) {
  549. dev = 0;
  550. sector += (geo->chunk_mask + 1);
  551. }
  552. }
  553. }
  554. static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
  555. {
  556. struct geom *geo = &conf->geo;
  557. if (conf->reshape_progress != MaxSector &&
  558. ((r10bio->sector >= conf->reshape_progress) !=
  559. conf->mddev->reshape_backwards)) {
  560. set_bit(R10BIO_Previous, &r10bio->state);
  561. geo = &conf->prev;
  562. } else
  563. clear_bit(R10BIO_Previous, &r10bio->state);
  564. __raid10_find_phys(geo, r10bio);
  565. }
  566. static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
  567. {
  568. sector_t offset, chunk, vchunk;
  569. /* Never use conf->prev as this is only called during resync
  570. * or recovery, so reshape isn't happening
  571. */
  572. struct geom *geo = &conf->geo;
  573. int far_set_start = (dev / geo->far_set_size) * geo->far_set_size;
  574. int far_set_size = geo->far_set_size;
  575. int last_far_set_start;
  576. if (geo->raid_disks % geo->far_set_size) {
  577. last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
  578. last_far_set_start *= geo->far_set_size;
  579. if (dev >= last_far_set_start) {
  580. far_set_size = geo->far_set_size;
  581. far_set_size += (geo->raid_disks % geo->far_set_size);
  582. far_set_start = last_far_set_start;
  583. }
  584. }
  585. offset = sector & geo->chunk_mask;
  586. if (geo->far_offset) {
  587. int fc;
  588. chunk = sector >> geo->chunk_shift;
  589. fc = sector_div(chunk, geo->far_copies);
  590. dev -= fc * geo->near_copies;
  591. if (dev < far_set_start)
  592. dev += far_set_size;
  593. } else {
  594. while (sector >= geo->stride) {
  595. sector -= geo->stride;
  596. if (dev < (geo->near_copies + far_set_start))
  597. dev += far_set_size - geo->near_copies;
  598. else
  599. dev -= geo->near_copies;
  600. }
  601. chunk = sector >> geo->chunk_shift;
  602. }
  603. vchunk = chunk * geo->raid_disks + dev;
  604. sector_div(vchunk, geo->near_copies);
  605. return (vchunk << geo->chunk_shift) + offset;
  606. }
  607. /*
  608. * This routine returns the disk from which the requested read should
  609. * be done. There is a per-array 'next expected sequential IO' sector
  610. * number - if this matches on the next IO then we use the last disk.
  611. * There is also a per-disk 'last know head position' sector that is
  612. * maintained from IRQ contexts, both the normal and the resync IO
  613. * completion handlers update this position correctly. If there is no
  614. * perfect sequential match then we pick the disk whose head is closest.
  615. *
  616. * If there are 2 mirrors in the same 2 devices, performance degrades
  617. * because position is mirror, not device based.
  618. *
  619. * The rdev for the device selected will have nr_pending incremented.
  620. */
  621. /*
  622. * FIXME: possibly should rethink readbalancing and do it differently
  623. * depending on near_copies / far_copies geometry.
  624. */
  625. static struct md_rdev *read_balance(struct r10conf *conf,
  626. struct r10bio *r10_bio,
  627. int *max_sectors)
  628. {
  629. const sector_t this_sector = r10_bio->sector;
  630. int disk, slot;
  631. int sectors = r10_bio->sectors;
  632. int best_good_sectors;
  633. sector_t new_distance, best_dist;
  634. struct md_rdev *best_rdev, *rdev = NULL;
  635. int do_balance;
  636. int best_slot;
  637. struct geom *geo = &conf->geo;
  638. raid10_find_phys(conf, r10_bio);
  639. rcu_read_lock();
  640. retry:
  641. sectors = r10_bio->sectors;
  642. best_slot = -1;
  643. best_rdev = NULL;
  644. best_dist = MaxSector;
  645. best_good_sectors = 0;
  646. do_balance = 1;
  647. /*
  648. * Check if we can balance. We can balance on the whole
  649. * device if no resync is going on (recovery is ok), or below
  650. * the resync window. We take the first readable disk when
  651. * above the resync window.
  652. */
  653. if (conf->mddev->recovery_cp < MaxSector
  654. && (this_sector + sectors >= conf->next_resync))
  655. do_balance = 0;
  656. for (slot = 0; slot < conf->copies ; slot++) {
  657. sector_t first_bad;
  658. int bad_sectors;
  659. sector_t dev_sector;
  660. if (r10_bio->devs[slot].bio == IO_BLOCKED)
  661. continue;
  662. disk = r10_bio->devs[slot].devnum;
  663. rdev = rcu_dereference(conf->mirrors[disk].replacement);
  664. if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
  665. r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
  666. rdev = rcu_dereference(conf->mirrors[disk].rdev);
  667. if (rdev == NULL ||
  668. test_bit(Faulty, &rdev->flags))
  669. continue;
  670. if (!test_bit(In_sync, &rdev->flags) &&
  671. r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
  672. continue;
  673. dev_sector = r10_bio->devs[slot].addr;
  674. if (is_badblock(rdev, dev_sector, sectors,
  675. &first_bad, &bad_sectors)) {
  676. if (best_dist < MaxSector)
  677. /* Already have a better slot */
  678. continue;
  679. if (first_bad <= dev_sector) {
  680. /* Cannot read here. If this is the
  681. * 'primary' device, then we must not read
  682. * beyond 'bad_sectors' from another device.
  683. */
  684. bad_sectors -= (dev_sector - first_bad);
  685. if (!do_balance && sectors > bad_sectors)
  686. sectors = bad_sectors;
  687. if (best_good_sectors > sectors)
  688. best_good_sectors = sectors;
  689. } else {
  690. sector_t good_sectors =
  691. first_bad - dev_sector;
  692. if (good_sectors > best_good_sectors) {
  693. best_good_sectors = good_sectors;
  694. best_slot = slot;
  695. best_rdev = rdev;
  696. }
  697. if (!do_balance)
  698. /* Must read from here */
  699. break;
  700. }
  701. continue;
  702. } else
  703. best_good_sectors = sectors;
  704. if (!do_balance)
  705. break;
  706. /* This optimisation is debatable, and completely destroys
  707. * sequential read speed for 'far copies' arrays. So only
  708. * keep it for 'near' arrays, and review those later.
  709. */
  710. if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending))
  711. break;
  712. /* for far > 1 always use the lowest address */
  713. if (geo->far_copies > 1)
  714. new_distance = r10_bio->devs[slot].addr;
  715. else
  716. new_distance = abs(r10_bio->devs[slot].addr -
  717. conf->mirrors[disk].head_position);
  718. if (new_distance < best_dist) {
  719. best_dist = new_distance;
  720. best_slot = slot;
  721. best_rdev = rdev;
  722. }
  723. }
  724. if (slot >= conf->copies) {
  725. slot = best_slot;
  726. rdev = best_rdev;
  727. }
  728. if (slot >= 0) {
  729. atomic_inc(&rdev->nr_pending);
  730. if (test_bit(Faulty, &rdev->flags)) {
  731. /* Cannot risk returning a device that failed
  732. * before we inc'ed nr_pending
  733. */
  734. rdev_dec_pending(rdev, conf->mddev);
  735. goto retry;
  736. }
  737. r10_bio->read_slot = slot;
  738. } else
  739. rdev = NULL;
  740. rcu_read_unlock();
  741. *max_sectors = best_good_sectors;
  742. return rdev;
  743. }
  744. static int raid10_congested(struct mddev *mddev, int bits)
  745. {
  746. struct r10conf *conf = mddev->private;
  747. int i, ret = 0;
  748. if ((bits & (1 << WB_async_congested)) &&
  749. conf->pending_count >= max_queued_requests)
  750. return 1;
  751. rcu_read_lock();
  752. for (i = 0;
  753. (i < conf->geo.raid_disks || i < conf->prev.raid_disks)
  754. && ret == 0;
  755. i++) {
  756. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  757. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  758. struct request_queue *q = bdev_get_queue(rdev->bdev);
  759. ret |= bdi_congested(&q->backing_dev_info, bits);
  760. }
  761. }
  762. rcu_read_unlock();
  763. return ret;
  764. }
  765. static void flush_pending_writes(struct r10conf *conf)
  766. {
  767. /* Any writes that have been queued but are awaiting
  768. * bitmap updates get flushed here.
  769. */
  770. spin_lock_irq(&conf->device_lock);
  771. if (conf->pending_bio_list.head) {
  772. struct bio *bio;
  773. bio = bio_list_get(&conf->pending_bio_list);
  774. conf->pending_count = 0;
  775. spin_unlock_irq(&conf->device_lock);
  776. /* flush any pending bitmap writes to disk
  777. * before proceeding w/ I/O */
  778. bitmap_unplug(conf->mddev->bitmap);
  779. wake_up(&conf->wait_barrier);
  780. while (bio) { /* submit pending writes */
  781. struct bio *next = bio->bi_next;
  782. bio->bi_next = NULL;
  783. if (unlikely((bio->bi_rw & REQ_DISCARD) &&
  784. !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
  785. /* Just ignore it */
  786. bio_endio(bio);
  787. else
  788. generic_make_request(bio);
  789. bio = next;
  790. }
  791. } else
  792. spin_unlock_irq(&conf->device_lock);
  793. }
  794. /* Barriers....
  795. * Sometimes we need to suspend IO while we do something else,
  796. * either some resync/recovery, or reconfigure the array.
  797. * To do this we raise a 'barrier'.
  798. * The 'barrier' is a counter that can be raised multiple times
  799. * to count how many activities are happening which preclude
  800. * normal IO.
  801. * We can only raise the barrier if there is no pending IO.
  802. * i.e. if nr_pending == 0.
  803. * We choose only to raise the barrier if no-one is waiting for the
  804. * barrier to go down. This means that as soon as an IO request
  805. * is ready, no other operations which require a barrier will start
  806. * until the IO request has had a chance.
  807. *
  808. * So: regular IO calls 'wait_barrier'. When that returns there
  809. * is no backgroup IO happening, It must arrange to call
  810. * allow_barrier when it has finished its IO.
  811. * backgroup IO calls must call raise_barrier. Once that returns
  812. * there is no normal IO happeing. It must arrange to call
  813. * lower_barrier when the particular background IO completes.
  814. */
  815. static void raise_barrier(struct r10conf *conf, int force)
  816. {
  817. BUG_ON(force && !conf->barrier);
  818. spin_lock_irq(&conf->resync_lock);
  819. /* Wait until no block IO is waiting (unless 'force') */
  820. wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
  821. conf->resync_lock);
  822. /* block any new IO from starting */
  823. conf->barrier++;
  824. /* Now wait for all pending IO to complete */
  825. wait_event_lock_irq(conf->wait_barrier,
  826. !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
  827. conf->resync_lock);
  828. spin_unlock_irq(&conf->resync_lock);
  829. }
  830. static void lower_barrier(struct r10conf *conf)
  831. {
  832. unsigned long flags;
  833. spin_lock_irqsave(&conf->resync_lock, flags);
  834. conf->barrier--;
  835. spin_unlock_irqrestore(&conf->resync_lock, flags);
  836. wake_up(&conf->wait_barrier);
  837. }
  838. static void wait_barrier(struct r10conf *conf)
  839. {
  840. spin_lock_irq(&conf->resync_lock);
  841. if (conf->barrier) {
  842. conf->nr_waiting++;
  843. /* Wait for the barrier to drop.
  844. * However if there are already pending
  845. * requests (preventing the barrier from
  846. * rising completely), and the
  847. * pre-process bio queue isn't empty,
  848. * then don't wait, as we need to empty
  849. * that queue to get the nr_pending
  850. * count down.
  851. */
  852. wait_event_lock_irq(conf->wait_barrier,
  853. !conf->barrier ||
  854. (conf->nr_pending &&
  855. current->bio_list &&
  856. !bio_list_empty(current->bio_list)),
  857. conf->resync_lock);
  858. conf->nr_waiting--;
  859. }
  860. conf->nr_pending++;
  861. spin_unlock_irq(&conf->resync_lock);
  862. }
  863. static void allow_barrier(struct r10conf *conf)
  864. {
  865. unsigned long flags;
  866. spin_lock_irqsave(&conf->resync_lock, flags);
  867. conf->nr_pending--;
  868. spin_unlock_irqrestore(&conf->resync_lock, flags);
  869. wake_up(&conf->wait_barrier);
  870. }
  871. static void freeze_array(struct r10conf *conf, int extra)
  872. {
  873. /* stop syncio and normal IO and wait for everything to
  874. * go quiet.
  875. * We increment barrier and nr_waiting, and then
  876. * wait until nr_pending match nr_queued+extra
  877. * This is called in the context of one normal IO request
  878. * that has failed. Thus any sync request that might be pending
  879. * will be blocked by nr_pending, and we need to wait for
  880. * pending IO requests to complete or be queued for re-try.
  881. * Thus the number queued (nr_queued) plus this request (extra)
  882. * must match the number of pending IOs (nr_pending) before
  883. * we continue.
  884. */
  885. spin_lock_irq(&conf->resync_lock);
  886. conf->barrier++;
  887. conf->nr_waiting++;
  888. wait_event_lock_irq_cmd(conf->wait_barrier,
  889. conf->nr_pending == conf->nr_queued+extra,
  890. conf->resync_lock,
  891. flush_pending_writes(conf));
  892. spin_unlock_irq(&conf->resync_lock);
  893. }
  894. static void unfreeze_array(struct r10conf *conf)
  895. {
  896. /* reverse the effect of the freeze */
  897. spin_lock_irq(&conf->resync_lock);
  898. conf->barrier--;
  899. conf->nr_waiting--;
  900. wake_up(&conf->wait_barrier);
  901. spin_unlock_irq(&conf->resync_lock);
  902. }
  903. static sector_t choose_data_offset(struct r10bio *r10_bio,
  904. struct md_rdev *rdev)
  905. {
  906. if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
  907. test_bit(R10BIO_Previous, &r10_bio->state))
  908. return rdev->data_offset;
  909. else
  910. return rdev->new_data_offset;
  911. }
  912. struct raid10_plug_cb {
  913. struct blk_plug_cb cb;
  914. struct bio_list pending;
  915. int pending_cnt;
  916. };
  917. static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
  918. {
  919. struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb,
  920. cb);
  921. struct mddev *mddev = plug->cb.data;
  922. struct r10conf *conf = mddev->private;
  923. struct bio *bio;
  924. if (from_schedule || current->bio_list) {
  925. spin_lock_irq(&conf->device_lock);
  926. bio_list_merge(&conf->pending_bio_list, &plug->pending);
  927. conf->pending_count += plug->pending_cnt;
  928. spin_unlock_irq(&conf->device_lock);
  929. wake_up(&conf->wait_barrier);
  930. md_wakeup_thread(mddev->thread);
  931. kfree(plug);
  932. return;
  933. }
  934. /* we aren't scheduling, so we can do the write-out directly. */
  935. bio = bio_list_get(&plug->pending);
  936. bitmap_unplug(mddev->bitmap);
  937. wake_up(&conf->wait_barrier);
  938. while (bio) { /* submit pending writes */
  939. struct bio *next = bio->bi_next;
  940. bio->bi_next = NULL;
  941. if (unlikely((bio->bi_rw & REQ_DISCARD) &&
  942. !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
  943. /* Just ignore it */
  944. bio_endio(bio);
  945. else
  946. generic_make_request(bio);
  947. bio = next;
  948. }
  949. kfree(plug);
  950. }
  951. static void __make_request(struct mddev *mddev, struct bio *bio)
  952. {
  953. struct r10conf *conf = mddev->private;
  954. struct r10bio *r10_bio;
  955. struct bio *read_bio;
  956. int i;
  957. const int rw = bio_data_dir(bio);
  958. const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
  959. const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
  960. const unsigned long do_discard = (bio->bi_rw
  961. & (REQ_DISCARD | REQ_SECURE));
  962. const unsigned long do_same = (bio->bi_rw & REQ_WRITE_SAME);
  963. unsigned long flags;
  964. struct md_rdev *blocked_rdev;
  965. struct blk_plug_cb *cb;
  966. struct raid10_plug_cb *plug = NULL;
  967. int sectors_handled;
  968. int max_sectors;
  969. int sectors;
  970. /*
  971. * Register the new request and wait if the reconstruction
  972. * thread has put up a bar for new requests.
  973. * Continue immediately if no resync is active currently.
  974. */
  975. wait_barrier(conf);
  976. sectors = bio_sectors(bio);
  977. while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
  978. bio->bi_iter.bi_sector < conf->reshape_progress &&
  979. bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
  980. /* IO spans the reshape position. Need to wait for
  981. * reshape to pass
  982. */
  983. allow_barrier(conf);
  984. wait_event(conf->wait_barrier,
  985. conf->reshape_progress <= bio->bi_iter.bi_sector ||
  986. conf->reshape_progress >= bio->bi_iter.bi_sector +
  987. sectors);
  988. wait_barrier(conf);
  989. }
  990. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
  991. bio_data_dir(bio) == WRITE &&
  992. (mddev->reshape_backwards
  993. ? (bio->bi_iter.bi_sector < conf->reshape_safe &&
  994. bio->bi_iter.bi_sector + sectors > conf->reshape_progress)
  995. : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe &&
  996. bio->bi_iter.bi_sector < conf->reshape_progress))) {
  997. /* Need to update reshape_position in metadata */
  998. mddev->reshape_position = conf->reshape_progress;
  999. set_mask_bits(&mddev->flags, 0,
  1000. BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_PENDING));
  1001. md_wakeup_thread(mddev->thread);
  1002. wait_event(mddev->sb_wait,
  1003. !test_bit(MD_CHANGE_PENDING, &mddev->flags));
  1004. conf->reshape_safe = mddev->reshape_position;
  1005. }
  1006. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  1007. r10_bio->master_bio = bio;
  1008. r10_bio->sectors = sectors;
  1009. r10_bio->mddev = mddev;
  1010. r10_bio->sector = bio->bi_iter.bi_sector;
  1011. r10_bio->state = 0;
  1012. /* We might need to issue multiple reads to different
  1013. * devices if there are bad blocks around, so we keep
  1014. * track of the number of reads in bio->bi_phys_segments.
  1015. * If this is 0, there is only one r10_bio and no locking
  1016. * will be needed when the request completes. If it is
  1017. * non-zero, then it is the number of not-completed requests.
  1018. */
  1019. bio->bi_phys_segments = 0;
  1020. bio_clear_flag(bio, BIO_SEG_VALID);
  1021. if (rw == READ) {
  1022. /*
  1023. * read balancing logic:
  1024. */
  1025. struct md_rdev *rdev;
  1026. int slot;
  1027. read_again:
  1028. rdev = read_balance(conf, r10_bio, &max_sectors);
  1029. if (!rdev) {
  1030. raid_end_bio_io(r10_bio);
  1031. return;
  1032. }
  1033. slot = r10_bio->read_slot;
  1034. read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  1035. bio_trim(read_bio, r10_bio->sector - bio->bi_iter.bi_sector,
  1036. max_sectors);
  1037. r10_bio->devs[slot].bio = read_bio;
  1038. r10_bio->devs[slot].rdev = rdev;
  1039. read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr +
  1040. choose_data_offset(r10_bio, rdev);
  1041. read_bio->bi_bdev = rdev->bdev;
  1042. read_bio->bi_end_io = raid10_end_read_request;
  1043. read_bio->bi_rw = READ | do_sync;
  1044. read_bio->bi_private = r10_bio;
  1045. if (max_sectors < r10_bio->sectors) {
  1046. /* Could not read all from this device, so we will
  1047. * need another r10_bio.
  1048. */
  1049. sectors_handled = (r10_bio->sector + max_sectors
  1050. - bio->bi_iter.bi_sector);
  1051. r10_bio->sectors = max_sectors;
  1052. spin_lock_irq(&conf->device_lock);
  1053. if (bio->bi_phys_segments == 0)
  1054. bio->bi_phys_segments = 2;
  1055. else
  1056. bio->bi_phys_segments++;
  1057. spin_unlock_irq(&conf->device_lock);
  1058. /* Cannot call generic_make_request directly
  1059. * as that will be queued in __generic_make_request
  1060. * and subsequent mempool_alloc might block
  1061. * waiting for it. so hand bio over to raid10d.
  1062. */
  1063. reschedule_retry(r10_bio);
  1064. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  1065. r10_bio->master_bio = bio;
  1066. r10_bio->sectors = bio_sectors(bio) - sectors_handled;
  1067. r10_bio->state = 0;
  1068. r10_bio->mddev = mddev;
  1069. r10_bio->sector = bio->bi_iter.bi_sector +
  1070. sectors_handled;
  1071. goto read_again;
  1072. } else
  1073. generic_make_request(read_bio);
  1074. return;
  1075. }
  1076. /*
  1077. * WRITE:
  1078. */
  1079. if (conf->pending_count >= max_queued_requests) {
  1080. md_wakeup_thread(mddev->thread);
  1081. wait_event(conf->wait_barrier,
  1082. conf->pending_count < max_queued_requests);
  1083. }
  1084. /* first select target devices under rcu_lock and
  1085. * inc refcount on their rdev. Record them by setting
  1086. * bios[x] to bio
  1087. * If there are known/acknowledged bad blocks on any device
  1088. * on which we have seen a write error, we want to avoid
  1089. * writing to those blocks. This potentially requires several
  1090. * writes to write around the bad blocks. Each set of writes
  1091. * gets its own r10_bio with a set of bios attached. The number
  1092. * of r10_bios is recored in bio->bi_phys_segments just as with
  1093. * the read case.
  1094. */
  1095. r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
  1096. raid10_find_phys(conf, r10_bio);
  1097. retry_write:
  1098. blocked_rdev = NULL;
  1099. rcu_read_lock();
  1100. max_sectors = r10_bio->sectors;
  1101. for (i = 0; i < conf->copies; i++) {
  1102. int d = r10_bio->devs[i].devnum;
  1103. struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
  1104. struct md_rdev *rrdev = rcu_dereference(
  1105. conf->mirrors[d].replacement);
  1106. if (rdev == rrdev)
  1107. rrdev = NULL;
  1108. if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  1109. atomic_inc(&rdev->nr_pending);
  1110. blocked_rdev = rdev;
  1111. break;
  1112. }
  1113. if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
  1114. atomic_inc(&rrdev->nr_pending);
  1115. blocked_rdev = rrdev;
  1116. break;
  1117. }
  1118. if (rdev && (test_bit(Faulty, &rdev->flags)))
  1119. rdev = NULL;
  1120. if (rrdev && (test_bit(Faulty, &rrdev->flags)))
  1121. rrdev = NULL;
  1122. r10_bio->devs[i].bio = NULL;
  1123. r10_bio->devs[i].repl_bio = NULL;
  1124. if (!rdev && !rrdev) {
  1125. set_bit(R10BIO_Degraded, &r10_bio->state);
  1126. continue;
  1127. }
  1128. if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
  1129. sector_t first_bad;
  1130. sector_t dev_sector = r10_bio->devs[i].addr;
  1131. int bad_sectors;
  1132. int is_bad;
  1133. is_bad = is_badblock(rdev, dev_sector,
  1134. max_sectors,
  1135. &first_bad, &bad_sectors);
  1136. if (is_bad < 0) {
  1137. /* Mustn't write here until the bad block
  1138. * is acknowledged
  1139. */
  1140. atomic_inc(&rdev->nr_pending);
  1141. set_bit(BlockedBadBlocks, &rdev->flags);
  1142. blocked_rdev = rdev;
  1143. break;
  1144. }
  1145. if (is_bad && first_bad <= dev_sector) {
  1146. /* Cannot write here at all */
  1147. bad_sectors -= (dev_sector - first_bad);
  1148. if (bad_sectors < max_sectors)
  1149. /* Mustn't write more than bad_sectors
  1150. * to other devices yet
  1151. */
  1152. max_sectors = bad_sectors;
  1153. /* We don't set R10BIO_Degraded as that
  1154. * only applies if the disk is missing,
  1155. * so it might be re-added, and we want to
  1156. * know to recover this chunk.
  1157. * In this case the device is here, and the
  1158. * fact that this chunk is not in-sync is
  1159. * recorded in the bad block log.
  1160. */
  1161. continue;
  1162. }
  1163. if (is_bad) {
  1164. int good_sectors = first_bad - dev_sector;
  1165. if (good_sectors < max_sectors)
  1166. max_sectors = good_sectors;
  1167. }
  1168. }
  1169. if (rdev) {
  1170. r10_bio->devs[i].bio = bio;
  1171. atomic_inc(&rdev->nr_pending);
  1172. }
  1173. if (rrdev) {
  1174. r10_bio->devs[i].repl_bio = bio;
  1175. atomic_inc(&rrdev->nr_pending);
  1176. }
  1177. }
  1178. rcu_read_unlock();
  1179. if (unlikely(blocked_rdev)) {
  1180. /* Have to wait for this device to get unblocked, then retry */
  1181. int j;
  1182. int d;
  1183. for (j = 0; j < i; j++) {
  1184. if (r10_bio->devs[j].bio) {
  1185. d = r10_bio->devs[j].devnum;
  1186. rdev_dec_pending(conf->mirrors[d].rdev, mddev);
  1187. }
  1188. if (r10_bio->devs[j].repl_bio) {
  1189. struct md_rdev *rdev;
  1190. d = r10_bio->devs[j].devnum;
  1191. rdev = conf->mirrors[d].replacement;
  1192. if (!rdev) {
  1193. /* Race with remove_disk */
  1194. smp_mb();
  1195. rdev = conf->mirrors[d].rdev;
  1196. }
  1197. rdev_dec_pending(rdev, mddev);
  1198. }
  1199. }
  1200. allow_barrier(conf);
  1201. md_wait_for_blocked_rdev(blocked_rdev, mddev);
  1202. wait_barrier(conf);
  1203. goto retry_write;
  1204. }
  1205. if (max_sectors < r10_bio->sectors) {
  1206. /* We are splitting this into multiple parts, so
  1207. * we need to prepare for allocating another r10_bio.
  1208. */
  1209. r10_bio->sectors = max_sectors;
  1210. spin_lock_irq(&conf->device_lock);
  1211. if (bio->bi_phys_segments == 0)
  1212. bio->bi_phys_segments = 2;
  1213. else
  1214. bio->bi_phys_segments++;
  1215. spin_unlock_irq(&conf->device_lock);
  1216. }
  1217. sectors_handled = r10_bio->sector + max_sectors -
  1218. bio->bi_iter.bi_sector;
  1219. atomic_set(&r10_bio->remaining, 1);
  1220. bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
  1221. for (i = 0; i < conf->copies; i++) {
  1222. struct bio *mbio;
  1223. int d = r10_bio->devs[i].devnum;
  1224. if (r10_bio->devs[i].bio) {
  1225. struct md_rdev *rdev = conf->mirrors[d].rdev;
  1226. mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  1227. bio_trim(mbio, r10_bio->sector - bio->bi_iter.bi_sector,
  1228. max_sectors);
  1229. r10_bio->devs[i].bio = mbio;
  1230. mbio->bi_iter.bi_sector = (r10_bio->devs[i].addr+
  1231. choose_data_offset(r10_bio,
  1232. rdev));
  1233. mbio->bi_bdev = rdev->bdev;
  1234. mbio->bi_end_io = raid10_end_write_request;
  1235. mbio->bi_rw =
  1236. WRITE | do_sync | do_fua | do_discard | do_same;
  1237. mbio->bi_private = r10_bio;
  1238. atomic_inc(&r10_bio->remaining);
  1239. cb = blk_check_plugged(raid10_unplug, mddev,
  1240. sizeof(*plug));
  1241. if (cb)
  1242. plug = container_of(cb, struct raid10_plug_cb,
  1243. cb);
  1244. else
  1245. plug = NULL;
  1246. spin_lock_irqsave(&conf->device_lock, flags);
  1247. if (plug) {
  1248. bio_list_add(&plug->pending, mbio);
  1249. plug->pending_cnt++;
  1250. } else {
  1251. bio_list_add(&conf->pending_bio_list, mbio);
  1252. conf->pending_count++;
  1253. }
  1254. spin_unlock_irqrestore(&conf->device_lock, flags);
  1255. if (!plug)
  1256. md_wakeup_thread(mddev->thread);
  1257. }
  1258. if (r10_bio->devs[i].repl_bio) {
  1259. struct md_rdev *rdev = conf->mirrors[d].replacement;
  1260. if (rdev == NULL) {
  1261. /* Replacement just got moved to main 'rdev' */
  1262. smp_mb();
  1263. rdev = conf->mirrors[d].rdev;
  1264. }
  1265. mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  1266. bio_trim(mbio, r10_bio->sector - bio->bi_iter.bi_sector,
  1267. max_sectors);
  1268. r10_bio->devs[i].repl_bio = mbio;
  1269. mbio->bi_iter.bi_sector = (r10_bio->devs[i].addr +
  1270. choose_data_offset(
  1271. r10_bio, rdev));
  1272. mbio->bi_bdev = rdev->bdev;
  1273. mbio->bi_end_io = raid10_end_write_request;
  1274. mbio->bi_rw =
  1275. WRITE | do_sync | do_fua | do_discard | do_same;
  1276. mbio->bi_private = r10_bio;
  1277. atomic_inc(&r10_bio->remaining);
  1278. spin_lock_irqsave(&conf->device_lock, flags);
  1279. bio_list_add(&conf->pending_bio_list, mbio);
  1280. conf->pending_count++;
  1281. spin_unlock_irqrestore(&conf->device_lock, flags);
  1282. if (!mddev_check_plugged(mddev))
  1283. md_wakeup_thread(mddev->thread);
  1284. }
  1285. }
  1286. /* Don't remove the bias on 'remaining' (one_write_done) until
  1287. * after checking if we need to go around again.
  1288. */
  1289. if (sectors_handled < bio_sectors(bio)) {
  1290. one_write_done(r10_bio);
  1291. /* We need another r10_bio. It has already been counted
  1292. * in bio->bi_phys_segments.
  1293. */
  1294. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  1295. r10_bio->master_bio = bio;
  1296. r10_bio->sectors = bio_sectors(bio) - sectors_handled;
  1297. r10_bio->mddev = mddev;
  1298. r10_bio->sector = bio->bi_iter.bi_sector + sectors_handled;
  1299. r10_bio->state = 0;
  1300. goto retry_write;
  1301. }
  1302. one_write_done(r10_bio);
  1303. }
  1304. static void raid10_make_request(struct mddev *mddev, struct bio *bio)
  1305. {
  1306. struct r10conf *conf = mddev->private;
  1307. sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
  1308. int chunk_sects = chunk_mask + 1;
  1309. struct bio *split;
  1310. if (unlikely(bio->bi_rw & REQ_FLUSH)) {
  1311. md_flush_request(mddev, bio);
  1312. return;
  1313. }
  1314. md_write_start(mddev, bio);
  1315. do {
  1316. /*
  1317. * If this request crosses a chunk boundary, we need to split
  1318. * it.
  1319. */
  1320. if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
  1321. bio_sectors(bio) > chunk_sects
  1322. && (conf->geo.near_copies < conf->geo.raid_disks
  1323. || conf->prev.near_copies <
  1324. conf->prev.raid_disks))) {
  1325. split = bio_split(bio, chunk_sects -
  1326. (bio->bi_iter.bi_sector &
  1327. (chunk_sects - 1)),
  1328. GFP_NOIO, fs_bio_set);
  1329. bio_chain(split, bio);
  1330. } else {
  1331. split = bio;
  1332. }
  1333. __make_request(mddev, split);
  1334. } while (split != bio);
  1335. /* In case raid10d snuck in to freeze_array */
  1336. wake_up(&conf->wait_barrier);
  1337. }
  1338. static void raid10_status(struct seq_file *seq, struct mddev *mddev)
  1339. {
  1340. struct r10conf *conf = mddev->private;
  1341. int i;
  1342. if (conf->geo.near_copies < conf->geo.raid_disks)
  1343. seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
  1344. if (conf->geo.near_copies > 1)
  1345. seq_printf(seq, " %d near-copies", conf->geo.near_copies);
  1346. if (conf->geo.far_copies > 1) {
  1347. if (conf->geo.far_offset)
  1348. seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
  1349. else
  1350. seq_printf(seq, " %d far-copies", conf->geo.far_copies);
  1351. if (conf->geo.far_set_size != conf->geo.raid_disks)
  1352. seq_printf(seq, " %d devices per set", conf->geo.far_set_size);
  1353. }
  1354. seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
  1355. conf->geo.raid_disks - mddev->degraded);
  1356. for (i = 0; i < conf->geo.raid_disks; i++)
  1357. seq_printf(seq, "%s",
  1358. conf->mirrors[i].rdev &&
  1359. test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
  1360. seq_printf(seq, "]");
  1361. }
  1362. /* check if there are enough drives for
  1363. * every block to appear on atleast one.
  1364. * Don't consider the device numbered 'ignore'
  1365. * as we might be about to remove it.
  1366. */
  1367. static int _enough(struct r10conf *conf, int previous, int ignore)
  1368. {
  1369. int first = 0;
  1370. int has_enough = 0;
  1371. int disks, ncopies;
  1372. if (previous) {
  1373. disks = conf->prev.raid_disks;
  1374. ncopies = conf->prev.near_copies;
  1375. } else {
  1376. disks = conf->geo.raid_disks;
  1377. ncopies = conf->geo.near_copies;
  1378. }
  1379. rcu_read_lock();
  1380. do {
  1381. int n = conf->copies;
  1382. int cnt = 0;
  1383. int this = first;
  1384. while (n--) {
  1385. struct md_rdev *rdev;
  1386. if (this != ignore &&
  1387. (rdev = rcu_dereference(conf->mirrors[this].rdev)) &&
  1388. test_bit(In_sync, &rdev->flags))
  1389. cnt++;
  1390. this = (this+1) % disks;
  1391. }
  1392. if (cnt == 0)
  1393. goto out;
  1394. first = (first + ncopies) % disks;
  1395. } while (first != 0);
  1396. has_enough = 1;
  1397. out:
  1398. rcu_read_unlock();
  1399. return has_enough;
  1400. }
  1401. static int enough(struct r10conf *conf, int ignore)
  1402. {
  1403. /* when calling 'enough', both 'prev' and 'geo' must
  1404. * be stable.
  1405. * This is ensured if ->reconfig_mutex or ->device_lock
  1406. * is held.
  1407. */
  1408. return _enough(conf, 0, ignore) &&
  1409. _enough(conf, 1, ignore);
  1410. }
  1411. static void raid10_error(struct mddev *mddev, struct md_rdev *rdev)
  1412. {
  1413. char b[BDEVNAME_SIZE];
  1414. struct r10conf *conf = mddev->private;
  1415. unsigned long flags;
  1416. /*
  1417. * If it is not operational, then we have already marked it as dead
  1418. * else if it is the last working disks, ignore the error, let the
  1419. * next level up know.
  1420. * else mark the drive as failed
  1421. */
  1422. spin_lock_irqsave(&conf->device_lock, flags);
  1423. if (test_bit(In_sync, &rdev->flags)
  1424. && !enough(conf, rdev->raid_disk)) {
  1425. /*
  1426. * Don't fail the drive, just return an IO error.
  1427. */
  1428. spin_unlock_irqrestore(&conf->device_lock, flags);
  1429. return;
  1430. }
  1431. if (test_and_clear_bit(In_sync, &rdev->flags))
  1432. mddev->degraded++;
  1433. /*
  1434. * If recovery is running, make sure it aborts.
  1435. */
  1436. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1437. set_bit(Blocked, &rdev->flags);
  1438. set_bit(Faulty, &rdev->flags);
  1439. set_mask_bits(&mddev->flags, 0,
  1440. BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_PENDING));
  1441. spin_unlock_irqrestore(&conf->device_lock, flags);
  1442. printk(KERN_ALERT
  1443. "md/raid10:%s: Disk failure on %s, disabling device.\n"
  1444. "md/raid10:%s: Operation continuing on %d devices.\n",
  1445. mdname(mddev), bdevname(rdev->bdev, b),
  1446. mdname(mddev), conf->geo.raid_disks - mddev->degraded);
  1447. }
  1448. static void print_conf(struct r10conf *conf)
  1449. {
  1450. int i;
  1451. struct raid10_info *tmp;
  1452. printk(KERN_DEBUG "RAID10 conf printout:\n");
  1453. if (!conf) {
  1454. printk(KERN_DEBUG "(!conf)\n");
  1455. return;
  1456. }
  1457. printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
  1458. conf->geo.raid_disks);
  1459. for (i = 0; i < conf->geo.raid_disks; i++) {
  1460. char b[BDEVNAME_SIZE];
  1461. tmp = conf->mirrors + i;
  1462. if (tmp->rdev)
  1463. printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
  1464. i, !test_bit(In_sync, &tmp->rdev->flags),
  1465. !test_bit(Faulty, &tmp->rdev->flags),
  1466. bdevname(tmp->rdev->bdev,b));
  1467. }
  1468. }
  1469. static void close_sync(struct r10conf *conf)
  1470. {
  1471. wait_barrier(conf);
  1472. allow_barrier(conf);
  1473. mempool_destroy(conf->r10buf_pool);
  1474. conf->r10buf_pool = NULL;
  1475. }
  1476. static int raid10_spare_active(struct mddev *mddev)
  1477. {
  1478. int i;
  1479. struct r10conf *conf = mddev->private;
  1480. struct raid10_info *tmp;
  1481. int count = 0;
  1482. unsigned long flags;
  1483. /*
  1484. * Find all non-in_sync disks within the RAID10 configuration
  1485. * and mark them in_sync
  1486. */
  1487. for (i = 0; i < conf->geo.raid_disks; i++) {
  1488. tmp = conf->mirrors + i;
  1489. if (tmp->replacement
  1490. && tmp->replacement->recovery_offset == MaxSector
  1491. && !test_bit(Faulty, &tmp->replacement->flags)
  1492. && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
  1493. /* Replacement has just become active */
  1494. if (!tmp->rdev
  1495. || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
  1496. count++;
  1497. if (tmp->rdev) {
  1498. /* Replaced device not technically faulty,
  1499. * but we need to be sure it gets removed
  1500. * and never re-added.
  1501. */
  1502. set_bit(Faulty, &tmp->rdev->flags);
  1503. sysfs_notify_dirent_safe(
  1504. tmp->rdev->sysfs_state);
  1505. }
  1506. sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
  1507. } else if (tmp->rdev
  1508. && tmp->rdev->recovery_offset == MaxSector
  1509. && !test_bit(Faulty, &tmp->rdev->flags)
  1510. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  1511. count++;
  1512. sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
  1513. }
  1514. }
  1515. spin_lock_irqsave(&conf->device_lock, flags);
  1516. mddev->degraded -= count;
  1517. spin_unlock_irqrestore(&conf->device_lock, flags);
  1518. print_conf(conf);
  1519. return count;
  1520. }
  1521. static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
  1522. {
  1523. struct r10conf *conf = mddev->private;
  1524. int err = -EEXIST;
  1525. int mirror;
  1526. int first = 0;
  1527. int last = conf->geo.raid_disks - 1;
  1528. if (mddev->recovery_cp < MaxSector)
  1529. /* only hot-add to in-sync arrays, as recovery is
  1530. * very different from resync
  1531. */
  1532. return -EBUSY;
  1533. if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
  1534. return -EINVAL;
  1535. if (md_integrity_add_rdev(rdev, mddev))
  1536. return -ENXIO;
  1537. if (rdev->raid_disk >= 0)
  1538. first = last = rdev->raid_disk;
  1539. if (rdev->saved_raid_disk >= first &&
  1540. conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
  1541. mirror = rdev->saved_raid_disk;
  1542. else
  1543. mirror = first;
  1544. for ( ; mirror <= last ; mirror++) {
  1545. struct raid10_info *p = &conf->mirrors[mirror];
  1546. if (p->recovery_disabled == mddev->recovery_disabled)
  1547. continue;
  1548. if (p->rdev) {
  1549. if (!test_bit(WantReplacement, &p->rdev->flags) ||
  1550. p->replacement != NULL)
  1551. continue;
  1552. clear_bit(In_sync, &rdev->flags);
  1553. set_bit(Replacement, &rdev->flags);
  1554. rdev->raid_disk = mirror;
  1555. err = 0;
  1556. if (mddev->gendisk)
  1557. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1558. rdev->data_offset << 9);
  1559. conf->fullsync = 1;
  1560. rcu_assign_pointer(p->replacement, rdev);
  1561. break;
  1562. }
  1563. if (mddev->gendisk)
  1564. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1565. rdev->data_offset << 9);
  1566. p->head_position = 0;
  1567. p->recovery_disabled = mddev->recovery_disabled - 1;
  1568. rdev->raid_disk = mirror;
  1569. err = 0;
  1570. if (rdev->saved_raid_disk != mirror)
  1571. conf->fullsync = 1;
  1572. rcu_assign_pointer(p->rdev, rdev);
  1573. break;
  1574. }
  1575. if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
  1576. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
  1577. print_conf(conf);
  1578. return err;
  1579. }
  1580. static int raid10_remove_disk(struct mddev *mddev, struct md_rdev