PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/md/raid10.c

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