PageRenderTime 79ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/md/raid5.c

https://bitbucket.org/ndreys/linux-sunxi
C | 6022 lines | 4565 code | 661 blank | 796 comment | 1110 complexity | 55ed2e6a439dbba1eab15e8ff1006358 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * raid5.c : Multiple Devices driver for Linux
  3. * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
  4. * Copyright (C) 1999, 2000 Ingo Molnar
  5. * Copyright (C) 2002, 2003 H. Peter Anvin
  6. *
  7. * RAID-4/5/6 management functions.
  8. * Thanks to Penguin Computing for making the RAID-6 development possible
  9. * by donating a test server!
  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. /*
  21. * BITMAP UNPLUGGING:
  22. *
  23. * The sequencing for updating the bitmap reliably is a little
  24. * subtle (and I got it wrong the first time) so it deserves some
  25. * explanation.
  26. *
  27. * We group bitmap updates into batches. Each batch has a number.
  28. * We may write out several batches at once, but that isn't very important.
  29. * conf->seq_write is the number of the last batch successfully written.
  30. * conf->seq_flush is the number of the last batch that was closed to
  31. * new additions.
  32. * When we discover that we will need to write to any block in a stripe
  33. * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
  34. * the number of the batch it will be in. This is seq_flush+1.
  35. * When we are ready to do a write, if that batch hasn't been written yet,
  36. * we plug the array and queue the stripe for later.
  37. * When an unplug happens, we increment bm_flush, thus closing the current
  38. * batch.
  39. * When we notice that bm_flush > bm_write, we write out all pending updates
  40. * to the bitmap, and advance bm_write to where bm_flush was.
  41. * This may occasionally write a bit out twice, but is sure never to
  42. * miss any bits.
  43. */
  44. #include <linux/blkdev.h>
  45. #include <linux/kthread.h>
  46. #include <linux/raid/pq.h>
  47. #include <linux/async_tx.h>
  48. #include <linux/async.h>
  49. #include <linux/seq_file.h>
  50. #include <linux/cpu.h>
  51. #include <linux/slab.h>
  52. #include "md.h"
  53. #include "raid5.h"
  54. #include "raid0.h"
  55. #include "bitmap.h"
  56. /*
  57. * Stripe cache
  58. */
  59. #define NR_STRIPES 256
  60. #define STRIPE_SIZE PAGE_SIZE
  61. #define STRIPE_SHIFT (PAGE_SHIFT - 9)
  62. #define STRIPE_SECTORS (STRIPE_SIZE>>9)
  63. #define IO_THRESHOLD 1
  64. #define BYPASS_THRESHOLD 1
  65. #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
  66. #define HASH_MASK (NR_HASH - 1)
  67. #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
  68. /* bio's attached to a stripe+device for I/O are linked together in bi_sector
  69. * order without overlap. There may be several bio's per stripe+device, and
  70. * a bio could span several devices.
  71. * When walking this list for a particular stripe+device, we must never proceed
  72. * beyond a bio that extends past this device, as the next bio might no longer
  73. * be valid.
  74. * This macro is used to determine the 'next' bio in the list, given the sector
  75. * of the current stripe+device
  76. */
  77. #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
  78. /*
  79. * The following can be used to debug the driver
  80. */
  81. #define RAID5_PARANOIA 1
  82. #if RAID5_PARANOIA && defined(CONFIG_SMP)
  83. # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
  84. #else
  85. # define CHECK_DEVLOCK()
  86. #endif
  87. #ifdef DEBUG
  88. #define inline
  89. #define __inline__
  90. #endif
  91. #define printk_rl(args...) ((void) (printk_ratelimit() && printk(args)))
  92. /*
  93. * We maintain a biased count of active stripes in the bottom 16 bits of
  94. * bi_phys_segments, and a count of processed stripes in the upper 16 bits
  95. */
  96. static inline int raid5_bi_phys_segments(struct bio *bio)
  97. {
  98. return bio->bi_phys_segments & 0xffff;
  99. }
  100. static inline int raid5_bi_hw_segments(struct bio *bio)
  101. {
  102. return (bio->bi_phys_segments >> 16) & 0xffff;
  103. }
  104. static inline int raid5_dec_bi_phys_segments(struct bio *bio)
  105. {
  106. --bio->bi_phys_segments;
  107. return raid5_bi_phys_segments(bio);
  108. }
  109. static inline int raid5_dec_bi_hw_segments(struct bio *bio)
  110. {
  111. unsigned short val = raid5_bi_hw_segments(bio);
  112. --val;
  113. bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
  114. return val;
  115. }
  116. static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
  117. {
  118. bio->bi_phys_segments = raid5_bi_phys_segments(bio) | (cnt << 16);
  119. }
  120. /* Find first data disk in a raid6 stripe */
  121. static inline int raid6_d0(struct stripe_head *sh)
  122. {
  123. if (sh->ddf_layout)
  124. /* ddf always start from first device */
  125. return 0;
  126. /* md starts just after Q block */
  127. if (sh->qd_idx == sh->disks - 1)
  128. return 0;
  129. else
  130. return sh->qd_idx + 1;
  131. }
  132. static inline int raid6_next_disk(int disk, int raid_disks)
  133. {
  134. disk++;
  135. return (disk < raid_disks) ? disk : 0;
  136. }
  137. /* When walking through the disks in a raid5, starting at raid6_d0,
  138. * We need to map each disk to a 'slot', where the data disks are slot
  139. * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
  140. * is raid_disks-1. This help does that mapping.
  141. */
  142. static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
  143. int *count, int syndrome_disks)
  144. {
  145. int slot = *count;
  146. if (sh->ddf_layout)
  147. (*count)++;
  148. if (idx == sh->pd_idx)
  149. return syndrome_disks;
  150. if (idx == sh->qd_idx)
  151. return syndrome_disks + 1;
  152. if (!sh->ddf_layout)
  153. (*count)++;
  154. return slot;
  155. }
  156. static void return_io(struct bio *return_bi)
  157. {
  158. struct bio *bi = return_bi;
  159. while (bi) {
  160. return_bi = bi->bi_next;
  161. bi->bi_next = NULL;
  162. bi->bi_size = 0;
  163. bio_endio(bi, 0);
  164. bi = return_bi;
  165. }
  166. }
  167. static void print_raid5_conf (raid5_conf_t *conf);
  168. static int stripe_operations_active(struct stripe_head *sh)
  169. {
  170. return sh->check_state || sh->reconstruct_state ||
  171. test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
  172. test_bit(STRIPE_COMPUTE_RUN, &sh->state);
  173. }
  174. static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
  175. {
  176. if (atomic_dec_and_test(&sh->count)) {
  177. BUG_ON(!list_empty(&sh->lru));
  178. BUG_ON(atomic_read(&conf->active_stripes)==0);
  179. if (test_bit(STRIPE_HANDLE, &sh->state)) {
  180. if (test_bit(STRIPE_DELAYED, &sh->state) &&
  181. !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  182. list_add_tail(&sh->lru, &conf->delayed_list);
  183. else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
  184. sh->bm_seq - conf->seq_write > 0)
  185. list_add_tail(&sh->lru, &conf->bitmap_list);
  186. else {
  187. clear_bit(STRIPE_DELAYED, &sh->state);
  188. clear_bit(STRIPE_BIT_DELAY, &sh->state);
  189. list_add_tail(&sh->lru, &conf->handle_list);
  190. }
  191. md_wakeup_thread(conf->mddev->thread);
  192. } else {
  193. BUG_ON(stripe_operations_active(sh));
  194. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  195. atomic_dec(&conf->preread_active_stripes);
  196. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  197. md_wakeup_thread(conf->mddev->thread);
  198. }
  199. atomic_dec(&conf->active_stripes);
  200. if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
  201. list_add_tail(&sh->lru, &conf->inactive_list);
  202. wake_up(&conf->wait_for_stripe);
  203. if (conf->retry_read_aligned)
  204. md_wakeup_thread(conf->mddev->thread);
  205. }
  206. }
  207. }
  208. }
  209. static void release_stripe(struct stripe_head *sh)
  210. {
  211. raid5_conf_t *conf = sh->raid_conf;
  212. unsigned long flags;
  213. spin_lock_irqsave(&conf->device_lock, flags);
  214. __release_stripe(conf, sh);
  215. spin_unlock_irqrestore(&conf->device_lock, flags);
  216. }
  217. static inline void remove_hash(struct stripe_head *sh)
  218. {
  219. pr_debug("remove_hash(), stripe %llu\n",
  220. (unsigned long long)sh->sector);
  221. hlist_del_init(&sh->hash);
  222. }
  223. static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
  224. {
  225. struct hlist_head *hp = stripe_hash(conf, sh->sector);
  226. pr_debug("insert_hash(), stripe %llu\n",
  227. (unsigned long long)sh->sector);
  228. CHECK_DEVLOCK();
  229. hlist_add_head(&sh->hash, hp);
  230. }
  231. /* find an idle stripe, make sure it is unhashed, and return it. */
  232. static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
  233. {
  234. struct stripe_head *sh = NULL;
  235. struct list_head *first;
  236. CHECK_DEVLOCK();
  237. if (list_empty(&conf->inactive_list))
  238. goto out;
  239. first = conf->inactive_list.next;
  240. sh = list_entry(first, struct stripe_head, lru);
  241. list_del_init(first);
  242. remove_hash(sh);
  243. atomic_inc(&conf->active_stripes);
  244. out:
  245. return sh;
  246. }
  247. static void shrink_buffers(struct stripe_head *sh)
  248. {
  249. struct page *p;
  250. int i;
  251. int num = sh->raid_conf->pool_size;
  252. for (i = 0; i < num ; i++) {
  253. p = sh->dev[i].page;
  254. if (!p)
  255. continue;
  256. sh->dev[i].page = NULL;
  257. put_page(p);
  258. }
  259. }
  260. static int grow_buffers(struct stripe_head *sh)
  261. {
  262. int i;
  263. int num = sh->raid_conf->pool_size;
  264. for (i = 0; i < num; i++) {
  265. struct page *page;
  266. if (!(page = alloc_page(GFP_KERNEL))) {
  267. return 1;
  268. }
  269. sh->dev[i].page = page;
  270. }
  271. return 0;
  272. }
  273. static void raid5_build_block(struct stripe_head *sh, int i, int previous);
  274. static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
  275. struct stripe_head *sh);
  276. static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
  277. {
  278. raid5_conf_t *conf = sh->raid_conf;
  279. int i;
  280. BUG_ON(atomic_read(&sh->count) != 0);
  281. BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
  282. BUG_ON(stripe_operations_active(sh));
  283. CHECK_DEVLOCK();
  284. pr_debug("init_stripe called, stripe %llu\n",
  285. (unsigned long long)sh->sector);
  286. remove_hash(sh);
  287. sh->generation = conf->generation - previous;
  288. sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
  289. sh->sector = sector;
  290. stripe_set_idx(sector, conf, previous, sh);
  291. sh->state = 0;
  292. for (i = sh->disks; i--; ) {
  293. struct r5dev *dev = &sh->dev[i];
  294. if (dev->toread || dev->read || dev->towrite || dev->written ||
  295. test_bit(R5_LOCKED, &dev->flags)) {
  296. printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
  297. (unsigned long long)sh->sector, i, dev->toread,
  298. dev->read, dev->towrite, dev->written,
  299. test_bit(R5_LOCKED, &dev->flags));
  300. BUG();
  301. }
  302. dev->flags = 0;
  303. raid5_build_block(sh, i, previous);
  304. }
  305. insert_hash(conf, sh);
  306. }
  307. static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector,
  308. short generation)
  309. {
  310. struct stripe_head *sh;
  311. struct hlist_node *hn;
  312. CHECK_DEVLOCK();
  313. pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
  314. hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
  315. if (sh->sector == sector && sh->generation == generation)
  316. return sh;
  317. pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
  318. return NULL;
  319. }
  320. /*
  321. * Need to check if array has failed when deciding whether to:
  322. * - start an array
  323. * - remove non-faulty devices
  324. * - add a spare
  325. * - allow a reshape
  326. * This determination is simple when no reshape is happening.
  327. * However if there is a reshape, we need to carefully check
  328. * both the before and after sections.
  329. * This is because some failed devices may only affect one
  330. * of the two sections, and some non-in_sync devices may
  331. * be insync in the section most affected by failed devices.
  332. */
  333. static int has_failed(raid5_conf_t *conf)
  334. {
  335. int degraded;
  336. int i;
  337. if (conf->mddev->reshape_position == MaxSector)
  338. return conf->mddev->degraded > conf->max_degraded;
  339. rcu_read_lock();
  340. degraded = 0;
  341. for (i = 0; i < conf->previous_raid_disks; i++) {
  342. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  343. if (!rdev || test_bit(Faulty, &rdev->flags))
  344. degraded++;
  345. else if (test_bit(In_sync, &rdev->flags))
  346. ;
  347. else
  348. /* not in-sync or faulty.
  349. * If the reshape increases the number of devices,
  350. * this is being recovered by the reshape, so
  351. * this 'previous' section is not in_sync.
  352. * If the number of devices is being reduced however,
  353. * the device can only be part of the array if
  354. * we are reverting a reshape, so this section will
  355. * be in-sync.
  356. */
  357. if (conf->raid_disks >= conf->previous_raid_disks)
  358. degraded++;
  359. }
  360. rcu_read_unlock();
  361. if (degraded > conf->max_degraded)
  362. return 1;
  363. rcu_read_lock();
  364. degraded = 0;
  365. for (i = 0; i < conf->raid_disks; i++) {
  366. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  367. if (!rdev || test_bit(Faulty, &rdev->flags))
  368. degraded++;
  369. else if (test_bit(In_sync, &rdev->flags))
  370. ;
  371. else
  372. /* not in-sync or faulty.
  373. * If reshape increases the number of devices, this
  374. * section has already been recovered, else it
  375. * almost certainly hasn't.
  376. */
  377. if (conf->raid_disks <= conf->previous_raid_disks)
  378. degraded++;
  379. }
  380. rcu_read_unlock();
  381. if (degraded > conf->max_degraded)
  382. return 1;
  383. return 0;
  384. }
  385. static struct stripe_head *
  386. get_active_stripe(raid5_conf_t *conf, sector_t sector,
  387. int previous, int noblock, int noquiesce)
  388. {
  389. struct stripe_head *sh;
  390. pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
  391. spin_lock_irq(&conf->device_lock);
  392. do {
  393. wait_event_lock_irq(conf->wait_for_stripe,
  394. conf->quiesce == 0 || noquiesce,
  395. conf->device_lock, /* nothing */);
  396. sh = __find_stripe(conf, sector, conf->generation - previous);
  397. if (!sh) {
  398. if (!conf->inactive_blocked)
  399. sh = get_free_stripe(conf);
  400. if (noblock && sh == NULL)
  401. break;
  402. if (!sh) {
  403. conf->inactive_blocked = 1;
  404. wait_event_lock_irq(conf->wait_for_stripe,
  405. !list_empty(&conf->inactive_list) &&
  406. (atomic_read(&conf->active_stripes)
  407. < (conf->max_nr_stripes *3/4)
  408. || !conf->inactive_blocked),
  409. conf->device_lock,
  410. );
  411. conf->inactive_blocked = 0;
  412. } else
  413. init_stripe(sh, sector, previous);
  414. } else {
  415. if (atomic_read(&sh->count)) {
  416. BUG_ON(!list_empty(&sh->lru)
  417. && !test_bit(STRIPE_EXPANDING, &sh->state));
  418. } else {
  419. if (!test_bit(STRIPE_HANDLE, &sh->state))
  420. atomic_inc(&conf->active_stripes);
  421. if (list_empty(&sh->lru) &&
  422. !test_bit(STRIPE_EXPANDING, &sh->state))
  423. BUG();
  424. list_del_init(&sh->lru);
  425. }
  426. }
  427. } while (sh == NULL);
  428. if (sh)
  429. atomic_inc(&sh->count);
  430. spin_unlock_irq(&conf->device_lock);
  431. return sh;
  432. }
  433. static void
  434. raid5_end_read_request(struct bio *bi, int error);
  435. static void
  436. raid5_end_write_request(struct bio *bi, int error);
  437. static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
  438. {
  439. raid5_conf_t *conf = sh->raid_conf;
  440. int i, disks = sh->disks;
  441. might_sleep();
  442. for (i = disks; i--; ) {
  443. int rw;
  444. struct bio *bi;
  445. mdk_rdev_t *rdev;
  446. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
  447. if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
  448. rw = WRITE_FUA;
  449. else
  450. rw = WRITE;
  451. } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  452. rw = READ;
  453. else
  454. continue;
  455. bi = &sh->dev[i].req;
  456. bi->bi_rw = rw;
  457. if (rw & WRITE)
  458. bi->bi_end_io = raid5_end_write_request;
  459. else
  460. bi->bi_end_io = raid5_end_read_request;
  461. rcu_read_lock();
  462. rdev = rcu_dereference(conf->disks[i].rdev);
  463. if (rdev && test_bit(Faulty, &rdev->flags))
  464. rdev = NULL;
  465. if (rdev)
  466. atomic_inc(&rdev->nr_pending);
  467. rcu_read_unlock();
  468. if (rdev) {
  469. if (s->syncing || s->expanding || s->expanded)
  470. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  471. set_bit(STRIPE_IO_STARTED, &sh->state);
  472. bi->bi_bdev = rdev->bdev;
  473. pr_debug("%s: for %llu schedule op %ld on disc %d\n",
  474. __func__, (unsigned long long)sh->sector,
  475. bi->bi_rw, i);
  476. atomic_inc(&sh->count);
  477. bi->bi_sector = sh->sector + rdev->data_offset;
  478. bi->bi_flags = 1 << BIO_UPTODATE;
  479. bi->bi_vcnt = 1;
  480. bi->bi_max_vecs = 1;
  481. bi->bi_idx = 0;
  482. bi->bi_io_vec = &sh->dev[i].vec;
  483. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  484. bi->bi_io_vec[0].bv_offset = 0;
  485. bi->bi_size = STRIPE_SIZE;
  486. bi->bi_next = NULL;
  487. if ((rw & WRITE) &&
  488. test_bit(R5_ReWrite, &sh->dev[i].flags))
  489. atomic_add(STRIPE_SECTORS,
  490. &rdev->corrected_errors);
  491. generic_make_request(bi);
  492. } else {
  493. if (rw & WRITE)
  494. set_bit(STRIPE_DEGRADED, &sh->state);
  495. pr_debug("skip op %ld on disc %d for sector %llu\n",
  496. bi->bi_rw, i, (unsigned long long)sh->sector);
  497. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  498. set_bit(STRIPE_HANDLE, &sh->state);
  499. }
  500. }
  501. }
  502. static struct dma_async_tx_descriptor *
  503. async_copy_data(int frombio, struct bio *bio, struct page *page,
  504. sector_t sector, struct dma_async_tx_descriptor *tx)
  505. {
  506. struct bio_vec *bvl;
  507. struct page *bio_page;
  508. int i;
  509. int page_offset;
  510. struct async_submit_ctl submit;
  511. enum async_tx_flags flags = 0;
  512. if (bio->bi_sector >= sector)
  513. page_offset = (signed)(bio->bi_sector - sector) * 512;
  514. else
  515. page_offset = (signed)(sector - bio->bi_sector) * -512;
  516. if (frombio)
  517. flags |= ASYNC_TX_FENCE;
  518. init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
  519. bio_for_each_segment(bvl, bio, i) {
  520. int len = bvl->bv_len;
  521. int clen;
  522. int b_offset = 0;
  523. if (page_offset < 0) {
  524. b_offset = -page_offset;
  525. page_offset += b_offset;
  526. len -= b_offset;
  527. }
  528. if (len > 0 && page_offset + len > STRIPE_SIZE)
  529. clen = STRIPE_SIZE - page_offset;
  530. else
  531. clen = len;
  532. if (clen > 0) {
  533. b_offset += bvl->bv_offset;
  534. bio_page = bvl->bv_page;
  535. if (frombio)
  536. tx = async_memcpy(page, bio_page, page_offset,
  537. b_offset, clen, &submit);
  538. else
  539. tx = async_memcpy(bio_page, page, b_offset,
  540. page_offset, clen, &submit);
  541. }
  542. /* chain the operations */
  543. submit.depend_tx = tx;
  544. if (clen < len) /* hit end of page */
  545. break;
  546. page_offset += len;
  547. }
  548. return tx;
  549. }
  550. static void ops_complete_biofill(void *stripe_head_ref)
  551. {
  552. struct stripe_head *sh = stripe_head_ref;
  553. struct bio *return_bi = NULL;
  554. raid5_conf_t *conf = sh->raid_conf;
  555. int i;
  556. pr_debug("%s: stripe %llu\n", __func__,
  557. (unsigned long long)sh->sector);
  558. /* clear completed biofills */
  559. spin_lock_irq(&conf->device_lock);
  560. for (i = sh->disks; i--; ) {
  561. struct r5dev *dev = &sh->dev[i];
  562. /* acknowledge completion of a biofill operation */
  563. /* and check if we need to reply to a read request,
  564. * new R5_Wantfill requests are held off until
  565. * !STRIPE_BIOFILL_RUN
  566. */
  567. if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
  568. struct bio *rbi, *rbi2;
  569. BUG_ON(!dev->read);
  570. rbi = dev->read;
  571. dev->read = NULL;
  572. while (rbi && rbi->bi_sector <
  573. dev->sector + STRIPE_SECTORS) {
  574. rbi2 = r5_next_bio(rbi, dev->sector);
  575. if (!raid5_dec_bi_phys_segments(rbi)) {
  576. rbi->bi_next = return_bi;
  577. return_bi = rbi;
  578. }
  579. rbi = rbi2;
  580. }
  581. }
  582. }
  583. spin_unlock_irq(&conf->device_lock);
  584. clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
  585. return_io(return_bi);
  586. set_bit(STRIPE_HANDLE, &sh->state);
  587. release_stripe(sh);
  588. }
  589. static void ops_run_biofill(struct stripe_head *sh)
  590. {
  591. struct dma_async_tx_descriptor *tx = NULL;
  592. raid5_conf_t *conf = sh->raid_conf;
  593. struct async_submit_ctl submit;
  594. int i;
  595. pr_debug("%s: stripe %llu\n", __func__,
  596. (unsigned long long)sh->sector);
  597. for (i = sh->disks; i--; ) {
  598. struct r5dev *dev = &sh->dev[i];
  599. if (test_bit(R5_Wantfill, &dev->flags)) {
  600. struct bio *rbi;
  601. spin_lock_irq(&conf->device_lock);
  602. dev->read = rbi = dev->toread;
  603. dev->toread = NULL;
  604. spin_unlock_irq(&conf->device_lock);
  605. while (rbi && rbi->bi_sector <
  606. dev->sector + STRIPE_SECTORS) {
  607. tx = async_copy_data(0, rbi, dev->page,
  608. dev->sector, tx);
  609. rbi = r5_next_bio(rbi, dev->sector);
  610. }
  611. }
  612. }
  613. atomic_inc(&sh->count);
  614. init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
  615. async_trigger_callback(&submit);
  616. }
  617. static void mark_target_uptodate(struct stripe_head *sh, int target)
  618. {
  619. struct r5dev *tgt;
  620. if (target < 0)
  621. return;
  622. tgt = &sh->dev[target];
  623. set_bit(R5_UPTODATE, &tgt->flags);
  624. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  625. clear_bit(R5_Wantcompute, &tgt->flags);
  626. }
  627. static void ops_complete_compute(void *stripe_head_ref)
  628. {
  629. struct stripe_head *sh = stripe_head_ref;
  630. pr_debug("%s: stripe %llu\n", __func__,
  631. (unsigned long long)sh->sector);
  632. /* mark the computed target(s) as uptodate */
  633. mark_target_uptodate(sh, sh->ops.target);
  634. mark_target_uptodate(sh, sh->ops.target2);
  635. clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
  636. if (sh->check_state == check_state_compute_run)
  637. sh->check_state = check_state_compute_result;
  638. set_bit(STRIPE_HANDLE, &sh->state);
  639. release_stripe(sh);
  640. }
  641. /* return a pointer to the address conversion region of the scribble buffer */
  642. static addr_conv_t *to_addr_conv(struct stripe_head *sh,
  643. struct raid5_percpu *percpu)
  644. {
  645. return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
  646. }
  647. static struct dma_async_tx_descriptor *
  648. ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
  649. {
  650. int disks = sh->disks;
  651. struct page **xor_srcs = percpu->scribble;
  652. int target = sh->ops.target;
  653. struct r5dev *tgt = &sh->dev[target];
  654. struct page *xor_dest = tgt->page;
  655. int count = 0;
  656. struct dma_async_tx_descriptor *tx;
  657. struct async_submit_ctl submit;
  658. int i;
  659. pr_debug("%s: stripe %llu block: %d\n",
  660. __func__, (unsigned long long)sh->sector, target);
  661. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  662. for (i = disks; i--; )
  663. if (i != target)
  664. xor_srcs[count++] = sh->dev[i].page;
  665. atomic_inc(&sh->count);
  666. init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
  667. ops_complete_compute, sh, to_addr_conv(sh, percpu));
  668. if (unlikely(count == 1))
  669. tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
  670. else
  671. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
  672. return tx;
  673. }
  674. /* set_syndrome_sources - populate source buffers for gen_syndrome
  675. * @srcs - (struct page *) array of size sh->disks
  676. * @sh - stripe_head to parse
  677. *
  678. * Populates srcs in proper layout order for the stripe and returns the
  679. * 'count' of sources to be used in a call to async_gen_syndrome. The P
  680. * destination buffer is recorded in srcs[count] and the Q destination
  681. * is recorded in srcs[count+1]].
  682. */
  683. static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
  684. {
  685. int disks = sh->disks;
  686. int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
  687. int d0_idx = raid6_d0(sh);
  688. int count;
  689. int i;
  690. for (i = 0; i < disks; i++)
  691. srcs[i] = NULL;
  692. count = 0;
  693. i = d0_idx;
  694. do {
  695. int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
  696. srcs[slot] = sh->dev[i].page;
  697. i = raid6_next_disk(i, disks);
  698. } while (i != d0_idx);
  699. return syndrome_disks;
  700. }
  701. static struct dma_async_tx_descriptor *
  702. ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
  703. {
  704. int disks = sh->disks;
  705. struct page **blocks = percpu->scribble;
  706. int target;
  707. int qd_idx = sh->qd_idx;
  708. struct dma_async_tx_descriptor *tx;
  709. struct async_submit_ctl submit;
  710. struct r5dev *tgt;
  711. struct page *dest;
  712. int i;
  713. int count;
  714. if (sh->ops.target < 0)
  715. target = sh->ops.target2;
  716. else if (sh->ops.target2 < 0)
  717. target = sh->ops.target;
  718. else
  719. /* we should only have one valid target */
  720. BUG();
  721. BUG_ON(target < 0);
  722. pr_debug("%s: stripe %llu block: %d\n",
  723. __func__, (unsigned long long)sh->sector, target);
  724. tgt = &sh->dev[target];
  725. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  726. dest = tgt->page;
  727. atomic_inc(&sh->count);
  728. if (target == qd_idx) {
  729. count = set_syndrome_sources(blocks, sh);
  730. blocks[count] = NULL; /* regenerating p is not necessary */
  731. BUG_ON(blocks[count+1] != dest); /* q should already be set */
  732. init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
  733. ops_complete_compute, sh,
  734. to_addr_conv(sh, percpu));
  735. tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
  736. } else {
  737. /* Compute any data- or p-drive using XOR */
  738. count = 0;
  739. for (i = disks; i-- ; ) {
  740. if (i == target || i == qd_idx)
  741. continue;
  742. blocks[count++] = sh->dev[i].page;
  743. }
  744. init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
  745. NULL, ops_complete_compute, sh,
  746. to_addr_conv(sh, percpu));
  747. tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
  748. }
  749. return tx;
  750. }
  751. static struct dma_async_tx_descriptor *
  752. ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
  753. {
  754. int i, count, disks = sh->disks;
  755. int syndrome_disks = sh->ddf_layout ? disks : disks-2;
  756. int d0_idx = raid6_d0(sh);
  757. int faila = -1, failb = -1;
  758. int target = sh->ops.target;
  759. int target2 = sh->ops.target2;
  760. struct r5dev *tgt = &sh->dev[target];
  761. struct r5dev *tgt2 = &sh->dev[target2];
  762. struct dma_async_tx_descriptor *tx;
  763. struct page **blocks = percpu->scribble;
  764. struct async_submit_ctl submit;
  765. pr_debug("%s: stripe %llu block1: %d block2: %d\n",
  766. __func__, (unsigned long long)sh->sector, target, target2);
  767. BUG_ON(target < 0 || target2 < 0);
  768. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  769. BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
  770. /* we need to open-code set_syndrome_sources to handle the
  771. * slot number conversion for 'faila' and 'failb'
  772. */
  773. for (i = 0; i < disks ; i++)
  774. blocks[i] = NULL;
  775. count = 0;
  776. i = d0_idx;
  777. do {
  778. int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
  779. blocks[slot] = sh->dev[i].page;
  780. if (i == target)
  781. faila = slot;
  782. if (i == target2)
  783. failb = slot;
  784. i = raid6_next_disk(i, disks);
  785. } while (i != d0_idx);
  786. BUG_ON(faila == failb);
  787. if (failb < faila)
  788. swap(faila, failb);
  789. pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
  790. __func__, (unsigned long long)sh->sector, faila, failb);
  791. atomic_inc(&sh->count);
  792. if (failb == syndrome_disks+1) {
  793. /* Q disk is one of the missing disks */
  794. if (faila == syndrome_disks) {
  795. /* Missing P+Q, just recompute */
  796. init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
  797. ops_complete_compute, sh,
  798. to_addr_conv(sh, percpu));
  799. return async_gen_syndrome(blocks, 0, syndrome_disks+2,
  800. STRIPE_SIZE, &submit);
  801. } else {
  802. struct page *dest;
  803. int data_target;
  804. int qd_idx = sh->qd_idx;
  805. /* Missing D+Q: recompute D from P, then recompute Q */
  806. if (target == qd_idx)
  807. data_target = target2;
  808. else
  809. data_target = target;
  810. count = 0;
  811. for (i = disks; i-- ; ) {
  812. if (i == data_target || i == qd_idx)
  813. continue;
  814. blocks[count++] = sh->dev[i].page;
  815. }
  816. dest = sh->dev[data_target].page;
  817. init_async_submit(&submit,
  818. ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
  819. NULL, NULL, NULL,
  820. to_addr_conv(sh, percpu));
  821. tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
  822. &submit);
  823. count = set_syndrome_sources(blocks, sh);
  824. init_async_submit(&submit, ASYNC_TX_FENCE, tx,
  825. ops_complete_compute, sh,
  826. to_addr_conv(sh, percpu));
  827. return async_gen_syndrome(blocks, 0, count+2,
  828. STRIPE_SIZE, &submit);
  829. }
  830. } else {
  831. init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
  832. ops_complete_compute, sh,
  833. to_addr_conv(sh, percpu));
  834. if (failb == syndrome_disks) {
  835. /* We're missing D+P. */
  836. return async_raid6_datap_recov(syndrome_disks+2,
  837. STRIPE_SIZE, faila,
  838. blocks, &submit);
  839. } else {
  840. /* We're missing D+D. */
  841. return async_raid6_2data_recov(syndrome_disks+2,
  842. STRIPE_SIZE, faila, failb,
  843. blocks, &submit);
  844. }
  845. }
  846. }
  847. static void ops_complete_prexor(void *stripe_head_ref)
  848. {
  849. struct stripe_head *sh = stripe_head_ref;
  850. pr_debug("%s: stripe %llu\n", __func__,
  851. (unsigned long long)sh->sector);
  852. }
  853. static struct dma_async_tx_descriptor *
  854. ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
  855. struct dma_async_tx_descriptor *tx)
  856. {
  857. int disks = sh->disks;
  858. struct page **xor_srcs = percpu->scribble;
  859. int count = 0, pd_idx = sh->pd_idx, i;
  860. struct async_submit_ctl submit;
  861. /* existing parity data subtracted */
  862. struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  863. pr_debug("%s: stripe %llu\n", __func__,
  864. (unsigned long long)sh->sector);
  865. for (i = disks; i--; ) {
  866. struct r5dev *dev = &sh->dev[i];
  867. /* Only process blocks that are known to be uptodate */
  868. if (test_bit(R5_Wantdrain, &dev->flags))
  869. xor_srcs[count++] = dev->page;
  870. }
  871. init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
  872. ops_complete_prexor, sh, to_addr_conv(sh, percpu));
  873. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
  874. return tx;
  875. }
  876. static struct dma_async_tx_descriptor *
  877. ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
  878. {
  879. int disks = sh->disks;
  880. int i;
  881. pr_debug("%s: stripe %llu\n", __func__,
  882. (unsigned long long)sh->sector);
  883. for (i = disks; i--; ) {
  884. struct r5dev *dev = &sh->dev[i];
  885. struct bio *chosen;
  886. if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
  887. struct bio *wbi;
  888. spin_lock(&sh->lock);
  889. chosen = dev->towrite;
  890. dev->towrite = NULL;
  891. BUG_ON(dev->written);
  892. wbi = dev->written = chosen;
  893. spin_unlock(&sh->lock);
  894. while (wbi && wbi->bi_sector <
  895. dev->sector + STRIPE_SECTORS) {
  896. if (wbi->bi_rw & REQ_FUA)
  897. set_bit(R5_WantFUA, &dev->flags);
  898. tx = async_copy_data(1, wbi, dev->page,
  899. dev->sector, tx);
  900. wbi = r5_next_bio(wbi, dev->sector);
  901. }
  902. }
  903. }
  904. return tx;
  905. }
  906. static void ops_complete_reconstruct(void *stripe_head_ref)
  907. {
  908. struct stripe_head *sh = stripe_head_ref;
  909. int disks = sh->disks;
  910. int pd_idx = sh->pd_idx;
  911. int qd_idx = sh->qd_idx;
  912. int i;
  913. bool fua = false;
  914. pr_debug("%s: stripe %llu\n", __func__,
  915. (unsigned long long)sh->sector);
  916. for (i = disks; i--; )
  917. fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
  918. for (i = disks; i--; ) {
  919. struct r5dev *dev = &sh->dev[i];
  920. if (dev->written || i == pd_idx || i == qd_idx) {
  921. set_bit(R5_UPTODATE, &dev->flags);
  922. if (fua)
  923. set_bit(R5_WantFUA, &dev->flags);
  924. }
  925. }
  926. if (sh->reconstruct_state == reconstruct_state_drain_run)
  927. sh->reconstruct_state = reconstruct_state_drain_result;
  928. else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
  929. sh->reconstruct_state = reconstruct_state_prexor_drain_result;
  930. else {
  931. BUG_ON(sh->reconstruct_state != reconstruct_state_run);
  932. sh->reconstruct_state = reconstruct_state_result;
  933. }
  934. set_bit(STRIPE_HANDLE, &sh->state);
  935. release_stripe(sh);
  936. }
  937. static void
  938. ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
  939. struct dma_async_tx_descriptor *tx)
  940. {
  941. int disks = sh->disks;
  942. struct page **xor_srcs = percpu->scribble;
  943. struct async_submit_ctl submit;
  944. int count = 0, pd_idx = sh->pd_idx, i;
  945. struct page *xor_dest;
  946. int prexor = 0;
  947. unsigned long flags;
  948. pr_debug("%s: stripe %llu\n", __func__,
  949. (unsigned long long)sh->sector);
  950. /* check if prexor is active which means only process blocks
  951. * that are part of a read-modify-write (written)
  952. */
  953. if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
  954. prexor = 1;
  955. xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  956. for (i = disks; i--; ) {
  957. struct r5dev *dev = &sh->dev[i];
  958. if (dev->written)
  959. xor_srcs[count++] = dev->page;
  960. }
  961. } else {
  962. xor_dest = sh->dev[pd_idx].page;
  963. for (i = disks; i--; ) {
  964. struct r5dev *dev = &sh->dev[i];
  965. if (i != pd_idx)
  966. xor_srcs[count++] = dev->page;
  967. }
  968. }
  969. /* 1/ if we prexor'd then the dest is reused as a source
  970. * 2/ if we did not prexor then we are redoing the parity
  971. * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
  972. * for the synchronous xor case
  973. */
  974. flags = ASYNC_TX_ACK |
  975. (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
  976. atomic_inc(&sh->count);
  977. init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
  978. to_addr_conv(sh, percpu));
  979. if (unlikely(count == 1))
  980. tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
  981. else
  982. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
  983. }
  984. static void
  985. ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
  986. struct dma_async_tx_descriptor *tx)
  987. {
  988. struct async_submit_ctl submit;
  989. struct page **blocks = percpu->scribble;
  990. int count;
  991. pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
  992. count = set_syndrome_sources(blocks, sh);
  993. atomic_inc(&sh->count);
  994. init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
  995. sh, to_addr_conv(sh, percpu));
  996. async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
  997. }
  998. static void ops_complete_check(void *stripe_head_ref)
  999. {
  1000. struct stripe_head *sh = stripe_head_ref;
  1001. pr_debug("%s: stripe %llu\n", __func__,
  1002. (unsigned long long)sh->sector);
  1003. sh->check_state = check_state_check_result;
  1004. set_bit(STRIPE_HANDLE, &sh->state);
  1005. release_stripe(sh);
  1006. }
  1007. static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
  1008. {
  1009. int disks = sh->disks;
  1010. int pd_idx = sh->pd_idx;
  1011. int qd_idx = sh->qd_idx;
  1012. struct page *xor_dest;
  1013. struct page **xor_srcs = percpu->scribble;
  1014. struct dma_async_tx_descriptor *tx;
  1015. struct async_submit_ctl submit;
  1016. int count;
  1017. int i;
  1018. pr_debug("%s: stripe %llu\n", __func__,
  1019. (unsigned long long)sh->sector);
  1020. count = 0;
  1021. xor_dest = sh->dev[pd_idx].page;
  1022. xor_srcs[count++] = xor_dest;
  1023. for (i = disks; i--; ) {
  1024. if (i == pd_idx || i == qd_idx)
  1025. continue;
  1026. xor_srcs[count++] = sh->dev[i].page;
  1027. }
  1028. init_async_submit(&submit, 0, NULL, NULL, NULL,
  1029. to_addr_conv(sh, percpu));
  1030. tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
  1031. &sh->ops.zero_sum_result, &submit);
  1032. atomic_inc(&sh->count);
  1033. init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
  1034. tx = async_trigger_callback(&submit);
  1035. }
  1036. static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
  1037. {
  1038. struct page **srcs = percpu->scribble;
  1039. struct async_submit_ctl submit;
  1040. int count;
  1041. pr_debug("%s: stripe %llu checkp: %d\n", __func__,
  1042. (unsigned long long)sh->sector, checkp);
  1043. count = set_syndrome_sources(srcs, sh);
  1044. if (!checkp)
  1045. srcs[count] = NULL;
  1046. atomic_inc(&sh->count);
  1047. init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
  1048. sh, to_addr_conv(sh, percpu));
  1049. async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
  1050. &sh->ops.zero_sum_result, percpu->spare_page, &submit);
  1051. }
  1052. static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
  1053. {
  1054. int overlap_clear = 0, i, disks = sh->disks;
  1055. struct dma_async_tx_descriptor *tx = NULL;
  1056. raid5_conf_t *conf = sh->raid_conf;
  1057. int level = conf->level;
  1058. struct raid5_percpu *percpu;
  1059. unsigned long cpu;
  1060. cpu = get_cpu();
  1061. percpu = per_cpu_ptr(conf->percpu, cpu);
  1062. if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
  1063. ops_run_biofill(sh);
  1064. overlap_clear++;
  1065. }
  1066. if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
  1067. if (level < 6)
  1068. tx = ops_run_compute5(sh, percpu);
  1069. else {
  1070. if (sh->ops.target2 < 0 || sh->ops.target < 0)
  1071. tx = ops_run_compute6_1(sh, percpu);
  1072. else
  1073. tx = ops_run_compute6_2(sh, percpu);
  1074. }
  1075. /* terminate the chain if reconstruct is not set to be run */
  1076. if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
  1077. async_tx_ack(tx);
  1078. }
  1079. if (test_bit(STRIPE_OP_PREXOR, &ops_request))
  1080. tx = ops_run_prexor(sh, percpu, tx);
  1081. if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
  1082. tx = ops_run_biodrain(sh, tx);
  1083. overlap_clear++;
  1084. }
  1085. if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
  1086. if (level < 6)
  1087. ops_run_reconstruct5(sh, percpu, tx);
  1088. else
  1089. ops_run_reconstruct6(sh, percpu, tx);
  1090. }
  1091. if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
  1092. if (sh->check_state == check_state_run)
  1093. ops_run_check_p(sh, percpu);
  1094. else if (sh->check_state == check_state_run_q)
  1095. ops_run_check_pq(sh, percpu, 0);
  1096. else if (sh->check_state == check_state_run_pq)
  1097. ops_run_check_pq(sh, percpu, 1);
  1098. else
  1099. BUG();
  1100. }
  1101. if (overlap_clear)
  1102. for (i = disks; i--; ) {
  1103. struct r5dev *dev = &sh->dev[i];
  1104. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  1105. wake_up(&sh->raid_conf->wait_for_overlap);
  1106. }
  1107. put_cpu();
  1108. }
  1109. #ifdef CONFIG_MULTICORE_RAID456
  1110. static void async_run_ops(void *param, async_cookie_t cookie)
  1111. {
  1112. struct stripe_head *sh = param;
  1113. unsigned long ops_request = sh->ops.request;
  1114. clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
  1115. wake_up(&sh->ops.wait_for_ops);
  1116. __raid_run_ops(sh, ops_request);
  1117. release_stripe(sh);
  1118. }
  1119. static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
  1120. {
  1121. /* since handle_stripe can be called outside of raid5d context
  1122. * we need to ensure sh->ops.request is de-staged before another
  1123. * request arrives
  1124. */
  1125. wait_event(sh->ops.wait_for_ops,
  1126. !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
  1127. sh->ops.request = ops_request;
  1128. atomic_inc(&sh->count);
  1129. async_schedule(async_run_ops, sh);
  1130. }
  1131. #else
  1132. #define raid_run_ops __raid_run_ops
  1133. #endif
  1134. static int grow_one_stripe(raid5_conf_t *conf)
  1135. {
  1136. struct stripe_head *sh;
  1137. sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
  1138. if (!sh)
  1139. return 0;
  1140. memset(sh, 0, sizeof(*sh) + (conf->pool_size-1)*sizeof(struct r5dev));
  1141. sh->raid_conf = conf;
  1142. spin_lock_init(&sh->lock);
  1143. #ifdef CONFIG_MULTICORE_RAID456
  1144. init_waitqueue_head(&sh->ops.wait_for_ops);
  1145. #endif
  1146. if (grow_buffers(sh)) {
  1147. shrink_buffers(sh);
  1148. kmem_cache_free(conf->slab_cache, sh);
  1149. return 0;
  1150. }
  1151. /* we just created an active stripe so... */
  1152. atomic_set(&sh->count, 1);
  1153. atomic_inc(&conf->active_stripes);
  1154. INIT_LIST_HEAD(&sh->lru);
  1155. release_stripe(sh);
  1156. return 1;
  1157. }
  1158. static int grow_stripes(raid5_conf_t *conf, int num)
  1159. {
  1160. struct kmem_cache *sc;
  1161. int devs = max(conf->raid_disks, conf->previous_raid_disks);
  1162. if (conf->mddev->gendisk)
  1163. sprintf(conf->cache_name[0],
  1164. "raid%d-%s", conf->level, mdname(conf->mddev));
  1165. else
  1166. sprintf(conf->cache_name[0],
  1167. "raid%d-%p", conf->level, conf->mddev);
  1168. sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
  1169. conf->active_name = 0;
  1170. sc = kmem_cache_create(conf->cache_name[conf->active_name],
  1171. sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
  1172. 0, 0, NULL);
  1173. if (!sc)
  1174. return 1;
  1175. conf->slab_cache = sc;
  1176. conf->pool_size = devs;
  1177. while (num--)
  1178. if (!grow_one_stripe(conf))
  1179. return 1;
  1180. return 0;
  1181. }
  1182. /**
  1183. * scribble_len - return the required size of the scribble region
  1184. * @num - total number of disks in the array
  1185. *
  1186. * The size must be enough to contain:
  1187. * 1/ a struct page pointer for each device in the array +2
  1188. * 2/ room to convert each entry in (1) to its corresponding dma
  1189. * (dma_map_page()) or page (page_address()) address.
  1190. *
  1191. * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
  1192. * calculate over all devices (not just the data blocks), using zeros in place
  1193. * of the P and Q blocks.
  1194. */
  1195. static size_t scribble_len(int num)
  1196. {
  1197. size_t len;
  1198. len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
  1199. return len;
  1200. }
  1201. static int resize_stripes(raid5_conf_t *conf, int newsize)
  1202. {
  1203. /* Make all the stripes able to hold 'newsize' devices.
  1204. * New slots in each stripe get 'page' set to a new page.
  1205. *
  1206. * This happens in stages:
  1207. * 1/ create a new kmem_cache and allocate the required number of
  1208. * stripe_heads.
  1209. * 2/ gather all the old stripe_heads and tranfer the pages across
  1210. * to the new stripe_heads. This will have the side effect of
  1211. * freezing the array as once all stripe_heads have been collected,
  1212. * no IO will be possible. Old stripe heads are freed once their
  1213. * pages have been transferred over, and the old kmem_cache is
  1214. * freed when all stripes are done.
  1215. * 3/ reallocate conf->disks to be suitable bigger. If this fails,
  1216. * we simple return a failre status - no need to clean anything up.
  1217. * 4/ allocate new pages for the new slots in the new stripe_heads.
  1218. * If this fails, we don't bother trying the shrink the
  1219. * stripe_heads down again, we just leave them as they are.
  1220. * As each stripe_head is processed the new one is released into
  1221. * active service.
  1222. *
  1223. * Once step2 is started, we cannot afford to wait for a write,
  1224. * so we use GFP_NOIO allocations.
  1225. */
  1226. struct stripe_head *osh, *nsh;
  1227. LIST_HEAD(newstripes);
  1228. struct disk_info *ndisks;
  1229. unsigned long cpu;
  1230. int err;
  1231. struct kmem_cache *sc;
  1232. int i;
  1233. if (newsize <= conf->pool_size)
  1234. return 0; /* never bother to shrink */
  1235. err = md_allow_write(conf->mddev);
  1236. if (err)
  1237. return err;
  1238. /* Step 1 */
  1239. sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
  1240. sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
  1241. 0, 0, NULL);
  1242. if (!sc)
  1243. return -ENOMEM;
  1244. for (i = conf->max_nr_stripes; i; i--) {
  1245. nsh = kmem_cache_alloc(sc, GFP_KERNEL);
  1246. if (!nsh)
  1247. break;
  1248. memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
  1249. nsh->raid_conf = conf;
  1250. spin_lock_init(&nsh->lock);
  1251. #ifdef CONFIG_MULTICORE_RAID456
  1252. init_waitqueue_head(&nsh->ops.wait_for_ops);
  1253. #endif
  1254. list_add(&nsh->lru, &newstripes);
  1255. }
  1256. if (i) {
  1257. /* didn't get enough, give up */
  1258. while (!list_empty(&newstripes)) {
  1259. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  1260. list_del(&nsh->lru);
  1261. kmem_cache_free(sc, nsh);
  1262. }
  1263. kmem_cache_destroy(sc);
  1264. return -ENOMEM;
  1265. }
  1266. /* Step 2 - Must use GFP_NOIO now.
  1267. * OK, we have enough stripes, start collecting inactive
  1268. * stripes and copying them over
  1269. */
  1270. list_for_each_entry(nsh, &newstripes, lru) {
  1271. spin_lock_irq(&conf->device_lock);
  1272. wait_event_lock_irq(conf->wait_for_stripe,
  1273. !list_empty(&conf->inactive_list),
  1274. conf->device_lock,
  1275. );
  1276. osh = get_free_stripe(conf);
  1277. spin_unlock_irq(&conf->device_lock);
  1278. atomic_set(&nsh->count, 1);
  1279. for(i=0; i<conf->pool_size; i++)
  1280. nsh->dev[i].page = osh->dev[i].page;
  1281. for( ; i<newsize; i++)
  1282. nsh->dev[i].page = NULL;
  1283. kmem_cache_free(conf->slab_cache, osh);
  1284. }
  1285. kmem_cache_destroy(conf->slab_cache);
  1286. /* Step 3.
  1287. * At this point, we are holding all the stripes so the array
  1288. * is completely stalled, so now is a good time to resize
  1289. * conf->disks and the scribble region
  1290. */
  1291. ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
  1292. if (ndisks) {
  1293. for (i=0; i<conf->raid_disks; i++)
  1294. ndisks[i] = conf->disks[i];
  1295. kfree(conf->disks);
  1296. conf->disks = ndisks;
  1297. } else
  1298. err = -ENOMEM;
  1299. get_online_cpus();
  1300. conf->scribble_len = scribble_len(newsize);
  1301. for_each_present_cpu(cpu) {
  1302. struct raid5_percpu *percpu;
  1303. void *scribble;
  1304. percpu = per_cpu_ptr(conf->percpu, cpu);
  1305. scribble = kmalloc(conf->scribble_len, GFP_NOIO);
  1306. if (scribble) {
  1307. kfree(percpu->scribble);
  1308. percpu->scribble = scribble;
  1309. } else {
  1310. err = -ENOMEM;
  1311. break;
  1312. }
  1313. }
  1314. put_online_cpus();
  1315. /* Step 4, return new stripes to service */
  1316. while(!list_empty(&newstripes)) {
  1317. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  1318. list_del_init(&nsh->lru);
  1319. for (i=conf->raid_disks; i < newsize; i++)
  1320. if (nsh->dev[i].page == NULL) {
  1321. struct page *p = alloc_page(GFP_NOIO);
  1322. nsh->dev[i].page = p;
  1323. if (!p)
  1324. err = -ENOMEM;
  1325. }
  1326. release_stripe(nsh);
  1327. }
  1328. /* critical section pass, GFP_NOIO no longer needed */
  1329. conf->slab_cache = sc;
  1330. conf->active_name = 1-conf->active_name;
  1331. conf->pool_size = newsize;
  1332. return err;
  1333. }
  1334. static int drop_one_stripe(raid5_conf_t *conf)
  1335. {
  1336. struct stripe_head *sh;
  1337. spin_lock_irq(&conf->device_lock);
  1338. sh = get_free_stripe(conf);
  1339. spin_unlock_irq(&conf->device_lock);
  1340. if (!sh)
  1341. return 0;
  1342. BUG_ON(atomic_read(&sh->count));
  1343. shrink_buffers(sh);
  1344. kmem_cache_free(conf->slab_cache, sh);
  1345. atomic_dec(&conf->active_stripes);
  1346. return 1;
  1347. }
  1348. static void shrink_stripes(raid5_conf_t *conf)
  1349. {
  1350. while (drop_one_stripe(conf))
  1351. ;
  1352. if (conf->slab_cache)
  1353. kmem_cache_destroy(conf->slab_cache);
  1354. conf->slab_cache = NULL;
  1355. }
  1356. static void raid5_end_read_request(struct bio * bi, int error)
  1357. {
  1358. struct stripe_head *sh = bi->bi_private;
  1359. raid5_conf_t *conf = sh->raid_conf;
  1360. int disks = sh->disks, i;
  1361. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  1362. char b[BDEVNAME_SIZE];
  1363. mdk_rdev_t *rdev;
  1364. for (i=0 ; i<disks; i++)
  1365. if (bi == &sh->dev[i].req)
  1366. break;
  1367. pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
  1368. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  1369. uptodate);
  1370. if (i == disks) {
  1371. BUG();
  1372. return;
  1373. }
  1374. if (uptodate) {
  1375. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  1376. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1377. rdev = conf->disks[i].rdev;
  1378. printk_rl(KERN_INFO "md/raid:%s: read error corrected"
  1379. " (%lu sectors at %llu on %s)\n",
  1380. mdname(conf->mddev), STRIPE_SECTORS,
  1381. (unsigned long long)(sh->sector
  1382. + rdev->data_offset),
  1383. bdevname(rdev->bdev, b));
  1384. clear_bit(R5_ReadError, &sh->dev[i].flags);
  1385. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  1386. }
  1387. if (atomic_read(&conf->disks[i].rdev->read_errors))
  1388. atomic_set(&conf->disks[i].rdev->read_errors, 0);
  1389. } else {
  1390. const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
  1391. int retry = 0;
  1392. rdev = conf->disks[i].rdev;
  1393. clear_bit(R5_UPTODATE, &sh->dev[i].flags);
  1394. atomic_inc(&rdev->read_errors);
  1395. if (conf->mddev->degraded >= conf->max_degraded)
  1396. printk_rl(KERN_WARNING
  1397. "md/raid:%s: read error not correctable "
  1398. "(sector %llu on %s).\n",
  1399. mdname(conf->mddev),
  1400. (unsigned long long)(sh->sector
  1401. + rdev->data_offset),
  1402. bdn);
  1403. else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
  1404. /* Oh, no!!! */
  1405. printk_rl(KERN_WARNING
  1406. "md/raid:%s: read error NOT corrected!! "
  1407. "(sector %llu on %s).\n",
  1408. mdname(conf->mddev),
  1409. (unsigned long long)(sh->sector
  1410. + rdev->data_offset),
  1411. bdn);
  1412. else if (atomic_read(&rdev->read_errors)
  1413. > conf->max_nr_stripes)
  1414. printk(KERN_WARNING
  1415. "md/raid:%s: Too many read errors, failing device %s.\n",
  1416. mdname(conf->mddev), bdn);
  1417. else
  1418. retry = 1;
  1419. if (retry)
  1420. set_bit(R5_ReadError, &sh->dev[i].flags);
  1421. else {
  1422. clear_bit(R5_ReadError, &sh->dev[i].flags);
  1423. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  1424. md_error(conf->mddev, rdev);
  1425. }
  1426. }
  1427. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  1428. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1429. set_bit(STRIPE_HANDLE, &sh->state);
  1430. release_stripe(sh);
  1431. }
  1432. static void raid5_end_write_request(struct bio *bi, int error)
  1433. {
  1434. struct stripe_head *sh = bi->bi_private;
  1435. raid5_conf_t *conf = sh->raid_conf;
  1436. int disks = sh->disks, i;
  1437. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  1438. for (i=0 ; i<disks; i++)
  1439. if (bi == &sh->dev[i].req)
  1440. break;
  1441. pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
  1442. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  1443. uptodate);
  1444. if (i == disks) {
  1445. BUG();
  1446. return;
  1447. }
  1448. if (!uptodate)
  1449. md_error(conf->mddev, conf->disks[i].rdev);
  1450. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  1451. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1452. set_bit(STRIPE_HANDLE, &sh->state);
  1453. release_stripe(sh);
  1454. }
  1455. static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
  1456. static void raid5_build_block(struct stripe_head *sh, int i, int previous)
  1457. {
  1458. struct r5dev *dev = &sh->dev[i];
  1459. bio_init(&dev->req);
  1460. dev->req.bi_io_vec = &dev->vec;
  1461. dev->req.bi_vcnt++;
  1462. dev->req.bi_max_vecs++;
  1463. dev->vec.bv_page = dev->page;
  1464. dev->vec.bv_len = STRIPE_SIZE;
  1465. dev->vec.bv_offset = 0;
  1466. dev->req.bi_sector = sh->sector;
  1467. dev->req.bi_private = sh;
  1468. dev->flags = 0;
  1469. dev->sector = compute_blocknr(sh, i, previous);
  1470. }
  1471. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  1472. {
  1473. char b[BDEVNAME_SIZE];
  1474. raid5_conf_t *conf = mddev->private;
  1475. pr_debug("raid456: error called\n");
  1476. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  1477. unsigned long flags;
  1478. spin_lock_irqsave(&conf->device_lock, flags);
  1479. mddev->degraded++;
  1480. spin_unlock_irqrestore(&conf->device_lock, flags);
  1481. /*
  1482. * if recovery was running, make sure it aborts.
  1483. */
  1484. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1485. }
  1486. set_bit(Faulty, &rdev->flags);
  1487. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1488. printk(KERN_ALERT
  1489. "md/raid:%s: Disk failure on %s, disabling device.\n"
  1490. "md/raid:%s: Operation continuing on %d devices.\n",
  1491. mdname(mddev),
  1492. bdevname(rdev->bdev, b),
  1493. mdname(mddev),
  1494. conf->raid_disks - mddev->degraded);
  1495. }
  1496. /*
  1497. * Input: a 'big' sector number,
  1498. * Output: index of the data and parity disk, and the sector # in them.
  1499. */
  1500. static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector,
  1501. int previous, int *dd_idx,
  1502. struct stripe_head *sh)
  1503. {
  1504. sector_t stripe, stripe2;
  1505. sector_t chunk_number;
  1506. unsigned int chunk_offset;
  1507. int pd_idx, qd_idx;
  1508. int ddf_layout = 0;
  1509. sector_t new_sector;
  1510. int algorithm = previous ? conf->prev_algo
  1511. : conf->algorithm;
  1512. int sectors_per_chunk = previous ? conf->prev_chunk_sectors
  1513. : conf->chunk_sectors;
  1514. int raid_disks = previous ? conf->previous_raid_disks
  1515. : conf->raid_disks;
  1516. int data_disks = raid_disks - conf->max_degraded;
  1517. /* First compute the information on this sector */
  1518. /*
  1519. * Compute the chunk number and the sector offset inside the chunk
  1520. */
  1521. chunk_offset = sector_div(r_sector, sectors_per_chunk);
  1522. chunk_number = r_sector;
  1523. /*
  1524. * Compute the stripe number
  1525. */
  1526. stripe = chunk_number;
  1527. *dd_idx = sector_div(stripe, data_disks);
  1528. stripe2 = stripe;
  1529. /*
  1530. * Select the parity disk based on the user selected algorithm.
  1531. */
  1532. pd_idx = qd_idx = ~0;
  1533. switch(conf->level) {
  1534. case 4:
  1535. pd_idx = data_disks;
  1536. break;
  1537. case 5:
  1538. switch (algorithm) {
  1539. case ALGORITHM_LEFT_ASYMMETRIC:
  1540. pd_idx = data_disks - sector_div(stripe2, raid_disks);
  1541. if (*dd_idx >= pd_idx)
  1542. (*dd_idx)++;
  1543. break;
  1544. case ALGORITHM_RIGHT_ASYMMETRIC:
  1545. pd_idx = sector_div(stripe2, raid_disks);
  1546. if (*dd_idx >= pd_idx)
  1547. (*dd_idx)++;
  1548. break;
  1549. case ALGORITHM_LEFT_SYMMETRIC:
  1550. pd_idx = data_disks - sector_div(stripe2, raid_disks);
  1551. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1552. break;
  1553. case ALGORITHM_RIGHT_SYMMETRIC:
  1554. pd_idx = sector_div(stripe2, raid_disks);
  1555. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1556. break;
  1557. case ALGORITHM_PARITY_0:
  1558. pd_idx = 0;
  1559. (*dd_idx)++;
  1560. break;
  1561. case ALGORITHM_PARITY_N:
  1562. pd_idx = data_disks;
  1563. break;
  1564. default:
  1565. BUG();
  1566. }
  1567. break;
  1568. case 6:
  1569. switch (algorithm) {
  1570. case ALGORITHM_LEFT_ASYMMETRIC:
  1571. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  1572. qd_idx = pd_idx + 1;
  1573. if (pd_idx == raid_disks-1) {
  1574. (*dd_idx)++; /* Q D D D P */
  1575. qd_idx = 0;
  1576. } else if (*dd_idx >= pd_idx)
  1577. (*dd_idx) += 2; /* D D P Q D */
  1578. break;
  1579. case ALGORITHM_RIGHT_ASYMMETRIC:
  1580. pd_idx = sector_div(stripe2, raid_disks);
  1581. qd_idx = pd_idx + 1;
  1582. if (pd_idx == raid_disks-1) {
  1583. (*dd_idx)++; /* Q D D D P */
  1584. qd_idx = 0;
  1585. } else if (*dd_idx >= pd_idx)
  1586. (*dd_idx) += 2; /* D D P Q D */
  1587. break;
  1588. case ALGORITHM_LEFT_SYMMETRIC:
  1589. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  1590. qd_idx = (pd_idx + 1) % raid_disks;
  1591. *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
  1592. break;
  1593. case ALGORITHM_RIGHT_SYMMETRIC:
  1594. pd_idx = sector_div(stripe2, raid_disks);
  1595. qd_idx = (pd_idx + 1) % raid_disks;
  1596. *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
  1597. break;
  1598. case ALGORITHM_PARITY_0:
  1599. pd_idx = 0;
  1600. qd_idx = 1;
  1601. (*dd_idx) += 2;
  1602. break;
  1603. case ALGORITHM_PARITY_N:
  1604. pd_idx = data_disks;
  1605. qd_idx = data_disks + 1;
  1606. break;
  1607. case ALGORITHM_ROTATING_ZERO_RESTART:
  1608. /* Exactly the same as RIGHT_ASYMMETRIC, but or
  1609. * of blocks for computing Q is different.
  1610. */
  1611. pd_idx = sector_div(stripe2, raid_disks);
  1612. qd_idx = pd_idx + 1;
  1613. if (pd_idx == raid_disks-1) {
  1614. (*dd_idx)++; /* Q D D D P */
  1615. qd_idx = 0;
  1616. } else if (*dd_idx >= pd_idx)
  1617. (*dd_idx) += 2; /* D D P Q D */
  1618. ddf_layout = 1;
  1619. break;
  1620. case ALGORITHM_ROTATING_N_RESTART:
  1621. /* Same a left_asymmetric, by first stripe is
  1622. * D D D P Q rather than
  1623. * Q D D D P
  1624. */
  1625. stripe2 += 1;
  1626. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  1627. qd_idx = pd_idx + 1;
  1628. if (pd_idx == raid_disks-1) {
  1629. (*dd_idx)++; /* Q D D D P */
  1630. qd_idx = 0;
  1631. } else if (*dd_idx >= pd_idx)
  1632. (*dd_idx) += 2; /* D D P Q D */
  1633. ddf_layout = 1;
  1634. break;
  1635. case ALGORITHM_ROTATING_N_CONTINUE:
  1636. /* Same as left_symmetric but Q is before P */
  1637. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  1638. qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
  1639. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1640. ddf_layout = 1;
  1641. break;
  1642. case ALGORITHM_LEFT_ASYMMETRIC_6:
  1643. /* RAID5 left_asymmetric, with Q on last device */
  1644. pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
  1645. if (*dd_idx >= pd_idx)
  1646. (*dd_idx)++;
  1647. qd_idx = raid_disks - 1;
  1648. break;
  1649. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  1650. pd_idx = sector_div(stripe2, raid_disks-1);
  1651. if (*dd_idx >= pd_idx)
  1652. (*dd_idx)++;
  1653. qd_idx = raid_disks - 1;
  1654. break;
  1655. case ALGORITHM_LEFT_SYMMETRIC_6:
  1656. pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
  1657. *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
  1658. qd_idx = raid_disks - 1;
  1659. break;
  1660. case ALGORITHM_RIGHT_SYMMETRIC_6:
  1661. pd_idx = sector_div(stripe2, raid_disks-1);
  1662. *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
  1663. qd_idx = raid_disks - 1;
  1664. break;
  1665. case ALGORITHM_PARITY_0_6:
  1666. pd_idx = 0;
  1667. (*dd_idx)++;
  1668. qd_idx = raid_disks - 1;
  1669. break;
  1670. default:
  1671. BUG();
  1672. }
  1673. break;
  1674. }
  1675. if (sh) {
  1676. sh->pd_idx = pd_idx;
  1677. sh->qd_idx = qd_idx;
  1678. sh->ddf_layout = ddf_layout;
  1679. }
  1680. /*
  1681. * Finally, compute the new sector number
  1682. */
  1683. new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
  1684. return new_sector;
  1685. }
  1686. static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
  1687. {
  1688. raid5_conf_t *conf = sh->raid_conf;
  1689. int raid_disks = sh->disks;
  1690. int data_disks = raid_disks - conf->max_degraded;
  1691. sector_t new_sector = sh->sector, check;
  1692. int sectors_per_chunk = previous ? conf->prev_chunk_sectors
  1693. : conf->chunk_sectors;
  1694. int algorithm = previous ? conf->prev_algo
  1695. : conf->algorithm;
  1696. sector_t stripe;
  1697. int chunk_offset;
  1698. sector_t chunk_number;
  1699. int dummy1, dd_idx = i;
  1700. sector_t r_sector;
  1701. struct stripe_head sh2;
  1702. chunk_offset = sector_div(new_sector, sectors_per_chunk);
  1703. stripe = new_sector;
  1704. if (i == sh->pd_idx)
  1705. return 0;
  1706. switch(conf->level) {
  1707. case 4: break;
  1708. case 5:
  1709. switch (algorithm) {
  1710. case ALGORITHM_LEFT_ASYMMETRIC:
  1711. case ALGORITHM_RIGHT_ASYMMETRIC:
  1712. if (i > sh->pd_idx)
  1713. i--;
  1714. break;
  1715. case ALGORITHM_LEFT_SYMMETRIC:
  1716. case ALGORITHM_RIGHT_SYMMETRIC:
  1717. if (i < sh->pd_idx)
  1718. i += raid_disks;
  1719. i -= (sh->pd_idx + 1);
  1720. break;
  1721. case ALGORITHM_PARITY_0:
  1722. i -= 1;
  1723. break;
  1724. case ALGORITHM_PARITY_N:
  1725. break;
  1726. default:
  1727. BUG();
  1728. }
  1729. break;
  1730. case 6:
  1731. if (i == sh->qd_idx)
  1732. return 0; /* It is the Q disk */
  1733. switch (algorithm) {
  1734. case ALGORITHM_LEFT_ASYMMETRIC:
  1735. case ALGORITHM_RIGHT_ASYMMETRIC:
  1736. case ALGORITHM_ROTATING_ZERO_RESTART:
  1737. case ALGORITHM_ROTATING_N_RESTART:
  1738. if (sh->pd_idx == raid_disks-1)
  1739. i--; /* Q D D D P */
  1740. else if (i > sh->pd_idx)
  1741. i -= 2; /* D D P Q D */
  1742. break;
  1743. case ALGORITHM_LEFT_SYMMETRIC:
  1744. case ALGORITHM_RIGHT_SYMMETRIC:
  1745. if (sh->pd_idx == raid_disks-1)
  1746. i--; /* Q D D D P */
  1747. else {
  1748. /* D D P Q D */
  1749. if (i < sh->pd_idx)
  1750. i += raid_disks;
  1751. i -= (sh->pd_idx + 2);
  1752. }
  1753. break;
  1754. case ALGORITHM_PARITY_0:
  1755. i -= 2;
  1756. break;
  1757. case ALGORITHM_PARITY_N:
  1758. break;
  1759. case ALGORITHM_ROTATING_N_CONTINUE:
  1760. /* Like left_symmetric, but P is before Q */
  1761. if (sh->pd_idx == 0)
  1762. i--; /* P D D D Q */
  1763. else {
  1764. /* D D Q P D */
  1765. if (i < sh->pd_idx)
  1766. i += raid_disks;
  1767. i -= (sh->pd_idx + 1);
  1768. }
  1769. break;
  1770. case ALGORITHM_LEFT_ASYMMETRIC_6:
  1771. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  1772. if (i > sh->pd_idx)
  1773. i--;
  1774. break;
  1775. case ALGORITHM_LEFT_SYMMETRIC_6:
  1776. case ALGORITHM_RIGHT_SYMMETRIC_6:
  1777. if (i < sh->pd_idx)
  1778. i += data_disks + 1;
  1779. i -= (sh->pd_idx + 1);
  1780. break;
  1781. case ALGORITHM_PARITY_0_6:
  1782. i -= 1;
  1783. break;
  1784. default:
  1785. BUG();
  1786. }
  1787. break;
  1788. }
  1789. chunk_number = stripe * data_disks + i;
  1790. r_sector = chunk_number * sectors_per_chunk + chunk_offset;
  1791. check = raid5_compute_sector(conf, r_sector,
  1792. previous, &dummy1, &sh2);
  1793. if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
  1794. || sh2.qd_idx != sh->qd_idx) {
  1795. printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
  1796. mdname(conf->mddev));
  1797. return 0;
  1798. }
  1799. return r_sector;
  1800. }
  1801. static void
  1802. schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
  1803. int rcw, int expand)
  1804. {
  1805. int i, pd_idx = sh->pd_idx, disks = sh->disks;
  1806. raid5_conf_t *conf = sh->raid_conf;
  1807. int level = conf->level;
  1808. if (rcw) {
  1809. /* if we are not expanding this is a proper write request, and
  1810. * there will be bios with new data to be drained into the
  1811. * stripe cache
  1812. */
  1813. if (!expand) {
  1814. sh->reconstruct_state = reconstruct_state_drain_run;
  1815. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  1816. } else
  1817. sh->reconstruct_state = reconstruct_state_run;
  1818. set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
  1819. for (i = disks; i--; ) {
  1820. struct r5dev *dev = &sh->dev[i];
  1821. if (dev->towrite) {
  1822. set_bit(R5_LOCKED, &dev->flags);
  1823. set_bit(R5_Wantdrain, &dev->flags);
  1824. if (!expand)
  1825. clear_bit(R5_UPTODATE, &dev->flags);
  1826. s->locked++;
  1827. }
  1828. }
  1829. if (s->locked + conf->max_degraded == disks)
  1830. if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
  1831. atomic_inc(&conf->pending_full_writes);
  1832. } else {
  1833. BUG_ON(level == 6);
  1834. BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
  1835. test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
  1836. sh->reconstruct_state = reconstruct_state_prexor_drain_run;
  1837. set_bit(STRIPE_OP_PREXOR, &s->ops_request);
  1838. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  1839. set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
  1840. for (i = disks; i--; ) {
  1841. struct r5dev *dev = &sh->dev[i];
  1842. if (i == pd_idx)
  1843. continue;
  1844. if (dev->towrite &&
  1845. (test_bit(R5_UPTODATE, &dev->flags) ||
  1846. test_bit(R5_Wantcompute, &dev->flags))) {
  1847. set_bit(R5_Wantdrain, &dev->flags);
  1848. set_bit(R5_LOCKED, &dev->flags);
  1849. clear_bit(R5_UPTODATE, &dev->flags);
  1850. s->locked++;
  1851. }
  1852. }
  1853. }
  1854. /* keep the parity disk(s) locked while asynchronous operations
  1855. * are in flight
  1856. */
  1857. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  1858. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  1859. s->locked++;
  1860. if (level == 6) {
  1861. int qd_idx = sh->qd_idx;
  1862. struct r5dev *dev = &sh->dev[qd_idx];
  1863. set_bit(R5_LOCKED, &dev->flags);
  1864. clear_bit(R5_UPTODATE, &dev->flags);
  1865. s->locked++;
  1866. }
  1867. pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
  1868. __func__, (unsigned long long)sh->sector,
  1869. s->locked, s->ops_request);
  1870. }
  1871. /*
  1872. * Each stripe/dev can have one or more bion attached.
  1873. * toread/towrite point to the first in a chain.
  1874. * The bi_next chain must be in order.
  1875. */
  1876. static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
  1877. {
  1878. struct bio **bip;
  1879. raid5_conf_t *conf = sh->raid_conf;
  1880. int firstwrite=0;
  1881. pr_debug("adding bh b#%llu to stripe s#%llu\n",
  1882. (unsigned long long)bi->bi_sector,
  1883. (unsigned long long)sh->sector);
  1884. spin_lock(&sh->lock);
  1885. spin_lock_irq(&conf->device_lock);
  1886. if (forwrite) {
  1887. bip = &sh->dev[dd_idx].towrite;
  1888. if (*bip == NULL && sh->dev[dd_idx].written == NULL)
  1889. firstwrite = 1;
  1890. } else
  1891. bip = &sh->dev[dd_idx].toread;
  1892. while (*bip && (*bip)->bi_sector < bi->bi_sector) {
  1893. if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
  1894. goto overlap;
  1895. bip = & (*bip)->bi_next;
  1896. }
  1897. if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
  1898. goto overlap;
  1899. BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
  1900. if (*bip)
  1901. bi->bi_next = *bip;
  1902. *bip = bi;
  1903. bi->bi_phys_segments++;
  1904. spin_unlock_irq(&conf->device_lock);
  1905. spin_unlock(&sh->lock);
  1906. pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
  1907. (unsigned long long)bi->bi_sector,
  1908. (unsigned long long)sh->sector, dd_idx);
  1909. if (conf->mddev->bitmap && firstwrite) {
  1910. bitmap_startwrite(conf->mddev->bitmap, sh->sector,
  1911. STRIPE_SECTORS, 0);
  1912. sh->bm_seq = conf->seq_flush+1;
  1913. set_bit(STRIPE_BIT_DELAY, &sh->state);
  1914. }
  1915. if (forwrite) {
  1916. /* check if page is covered */
  1917. sector_t sector = sh->dev[dd_idx].sector;
  1918. for (bi=sh->dev[dd_idx].towrite;
  1919. sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
  1920. bi && bi->bi_sector <= sector;
  1921. bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
  1922. if (bi->bi_sector + (bi->bi_size>>9) >= sector)
  1923. sector = bi->bi_sector + (bi->bi_size>>9);
  1924. }
  1925. if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
  1926. set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
  1927. }
  1928. return 1;
  1929. overlap:
  1930. set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
  1931. spin_unlock_irq(&conf->device_lock);
  1932. spin_unlock(&sh->lock);
  1933. return 0;
  1934. }
  1935. static void end_reshape(raid5_conf_t *conf);
  1936. static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
  1937. struct stripe_head *sh)
  1938. {
  1939. int sectors_per_chunk =
  1940. previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
  1941. int dd_idx;
  1942. int chunk_offset = sector_div(stripe, sectors_per_chunk);
  1943. int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
  1944. raid5_compute_sector(conf,
  1945. stripe * (disks - conf->max_degraded)
  1946. *sectors_per_chunk + chunk_offset,
  1947. previous,
  1948. &dd_idx, sh);
  1949. }
  1950. static void
  1951. handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
  1952. struct stripe_head_state *s, int disks,
  1953. struct bio **return_bi)
  1954. {
  1955. int i;
  1956. for (i = disks; i--; ) {
  1957. struct bio *bi;
  1958. int bitmap_end = 0;
  1959. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1960. mdk_rdev_t *rdev;
  1961. rcu_read_lock();
  1962. rdev = rcu_dereference(conf->disks[i].rdev);
  1963. if (rdev && test_bit(In_sync, &rdev->flags))
  1964. /* multiple read failures in one stripe */
  1965. md_error(conf->mddev, rdev);
  1966. rcu_read_unlock();
  1967. }
  1968. spin_lock_irq(&conf->device_lock);
  1969. /* fail all writes first */
  1970. bi = sh->dev[i].towrite;
  1971. sh->dev[i].towrite = NULL;
  1972. if (bi) {
  1973. s->to_write--;
  1974. bitmap_end = 1;
  1975. }
  1976. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1977. wake_up(&conf->wait_for_overlap);
  1978. while (bi && bi->bi_sector <
  1979. sh->dev[i].sector + STRIPE_SECTORS) {
  1980. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1981. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1982. if (!raid5_dec_bi_phys_segments(bi)) {
  1983. md_write_end(conf->mddev);
  1984. bi->bi_next = *return_bi;
  1985. *return_bi = bi;
  1986. }
  1987. bi = nextbi;
  1988. }
  1989. /* and fail all 'written' */
  1990. bi = sh->dev[i].written;
  1991. sh->dev[i].written = NULL;
  1992. if (bi) bitmap_end = 1;
  1993. while (bi && bi->bi_sector <
  1994. sh->dev[i].sector + STRIPE_SECTORS) {
  1995. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  1996. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1997. if (!raid5_dec_bi_phys_segments(bi)) {
  1998. md_write_end(conf->mddev);
  1999. bi->bi_next = *return_bi;
  2000. *return_bi = bi;
  2001. }
  2002. bi = bi2;
  2003. }
  2004. /* fail any reads if this device is non-operational and
  2005. * the data has not reached the cache yet.
  2006. */
  2007. if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
  2008. (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  2009. test_bit(R5_ReadError, &sh->dev[i].flags))) {
  2010. bi = sh->dev[i].toread;
  2011. sh->dev[i].toread = NULL;
  2012. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  2013. wake_up(&conf->wait_for_overlap);
  2014. if (bi) s->to_read--;
  2015. while (bi && bi->bi_sector <
  2016. sh->dev[i].sector + STRIPE_SECTORS) {
  2017. struct bio *nextbi =
  2018. r5_next_bio(bi, sh->dev[i].sector);
  2019. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  2020. if (!raid5_dec_bi_phys_segments(bi)) {
  2021. bi->bi_next = *return_bi;
  2022. *return_bi = bi;
  2023. }
  2024. bi = nextbi;
  2025. }
  2026. }
  2027. spin_unlock_irq(&conf->device_lock);
  2028. if (bitmap_end)
  2029. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  2030. STRIPE_SECTORS, 0, 0);
  2031. }
  2032. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  2033. if (atomic_dec_and_test(&conf->pending_full_writes))
  2034. md_wakeup_thread(conf->mddev->thread);
  2035. }
  2036. /* fetch_block5 - checks the given member device to see if its data needs
  2037. * to be read or computed to satisfy a request.
  2038. *
  2039. * Returns 1 when no more member devices need to be checked, otherwise returns
  2040. * 0 to tell the loop in handle_stripe_fill5 to continue
  2041. */
  2042. static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s,
  2043. int disk_idx, int disks)
  2044. {
  2045. struct r5dev *dev = &sh->dev[disk_idx];
  2046. struct r5dev *failed_dev = &sh->dev[s->failed_num];
  2047. /* is the data in this block needed, and can we get it? */
  2048. if (!test_bit(R5_LOCKED, &dev->flags) &&
  2049. !test_bit(R5_UPTODATE, &dev->flags) &&
  2050. (dev->toread ||
  2051. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  2052. s->syncing || s->expanding ||
  2053. (s->failed &&
  2054. (failed_dev->toread ||
  2055. (failed_dev->towrite &&
  2056. !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) {
  2057. /* We would like to get this block, possibly by computing it,
  2058. * otherwise read it if the backing disk is insync
  2059. */
  2060. if ((s->uptodate == disks - 1) &&
  2061. (s->failed && disk_idx == s->failed_num)) {
  2062. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2063. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2064. set_bit(R5_Wantcompute, &dev->flags);
  2065. sh->ops.target = disk_idx;
  2066. sh->ops.target2 = -1;
  2067. s->req_compute = 1;
  2068. /* Careful: from this point on 'uptodate' is in the eye
  2069. * of raid_run_ops which services 'compute' operations
  2070. * before writes. R5_Wantcompute flags a block that will
  2071. * be R5_UPTODATE by the time it is needed for a
  2072. * subsequent operation.
  2073. */
  2074. s->uptodate++;
  2075. return 1; /* uptodate + compute == disks */
  2076. } else if (test_bit(R5_Insync, &dev->flags)) {
  2077. set_bit(R5_LOCKED, &dev->flags);
  2078. set_bit(R5_Wantread, &dev->flags);
  2079. s->locked++;
  2080. pr_debug("Reading block %d (sync=%d)\n", disk_idx,
  2081. s->syncing);
  2082. }
  2083. }
  2084. return 0;
  2085. }
  2086. /**
  2087. * handle_stripe_fill5 - read or compute data to satisfy pending requests.
  2088. */
  2089. static void handle_stripe_fill5(struct stripe_head *sh,
  2090. struct stripe_head_state *s, int disks)
  2091. {
  2092. int i;
  2093. /* look for blocks to read/compute, skip this if a compute
  2094. * is already in flight, or if the stripe contents are in the
  2095. * midst of changing due to a write
  2096. */
  2097. if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
  2098. !sh->reconstruct_state)
  2099. for (i = disks; i--; )
  2100. if (fetch_block5(sh, s, i, disks))
  2101. break;
  2102. set_bit(STRIPE_HANDLE, &sh->state);
  2103. }
  2104. /* fetch_block6 - checks the given member device to see if its data needs
  2105. * to be read or computed to satisfy a request.
  2106. *
  2107. * Returns 1 when no more member devices need to be checked, otherwise returns
  2108. * 0 to tell the loop in handle_stripe_fill6 to continue
  2109. */
  2110. static int fetch_block6(struct stripe_head *sh, struct stripe_head_state *s,
  2111. struct r6_state *r6s, int disk_idx, int disks)
  2112. {
  2113. struct r5dev *dev = &sh->dev[disk_idx];
  2114. struct r5dev *fdev[2] = { &sh->dev[r6s->failed_num[0]],
  2115. &sh->dev[r6s->failed_num[1]] };
  2116. if (!test_bit(R5_LOCKED, &dev->flags) &&
  2117. !test_bit(R5_UPTODATE, &dev->flags) &&
  2118. (dev->toread ||
  2119. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  2120. s->syncing || s->expanding ||
  2121. (s->failed >= 1 &&
  2122. (fdev[0]->toread || s->to_write)) ||
  2123. (s->failed >= 2 &&
  2124. (fdev[1]->toread || s->to_write)))) {
  2125. /* we would like to get this block, possibly by computing it,
  2126. * otherwise read it if the backing disk is insync
  2127. */
  2128. BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
  2129. BUG_ON(test_bit(R5_Wantread, &dev->flags));
  2130. if ((s->uptodate == disks - 1) &&
  2131. (s->failed && (disk_idx == r6s->failed_num[0] ||
  2132. disk_idx == r6s->failed_num[1]))) {
  2133. /* have disk failed, and we're requested to fetch it;
  2134. * do compute it
  2135. */
  2136. pr_debug("Computing stripe %llu block %d\n",
  2137. (unsigned long long)sh->sector, disk_idx);
  2138. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2139. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2140. set_bit(R5_Wantcompute, &dev->flags);
  2141. sh->ops.target = disk_idx;
  2142. sh->ops.target2 = -1; /* no 2nd target */
  2143. s->req_compute = 1;
  2144. s->uptodate++;
  2145. return 1;
  2146. } else if (s->uptodate == disks-2 && s->failed >= 2) {
  2147. /* Computing 2-failure is *very* expensive; only
  2148. * do it if failed >= 2
  2149. */
  2150. int other;
  2151. for (other = disks; other--; ) {
  2152. if (other == disk_idx)
  2153. continue;
  2154. if (!test_bit(R5_UPTODATE,
  2155. &sh->dev[other].flags))
  2156. break;
  2157. }
  2158. BUG_ON(other < 0);
  2159. pr_debug("Computing stripe %llu blocks %d,%d\n",
  2160. (unsigned long long)sh->sector,
  2161. disk_idx, other);
  2162. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2163. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2164. set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
  2165. set_bit(R5_Wantcompute, &sh->dev[other].flags);
  2166. sh->ops.target = disk_idx;
  2167. sh->ops.target2 = other;
  2168. s->uptodate += 2;
  2169. s->req_compute = 1;
  2170. return 1;
  2171. } else if (test_bit(R5_Insync, &dev->flags)) {
  2172. set_bit(R5_LOCKED, &dev->flags);
  2173. set_bit(R5_Wantread, &dev->flags);
  2174. s->locked++;
  2175. pr_debug("Reading block %d (sync=%d)\n",
  2176. disk_idx, s->syncing);
  2177. }
  2178. }
  2179. return 0;
  2180. }
  2181. /**
  2182. * handle_stripe_fill6 - read or compute data to satisfy pending requests.
  2183. */
  2184. static void handle_stripe_fill6(struct stripe_head *sh,
  2185. struct stripe_head_state *s, struct r6_state *r6s,
  2186. int disks)
  2187. {
  2188. int i;
  2189. /* look for blocks to read/compute, skip this if a compute
  2190. * is already in flight, or if the stripe contents are in the
  2191. * midst of changing due to a write
  2192. */
  2193. if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
  2194. !sh->reconstruct_state)
  2195. for (i = disks; i--; )
  2196. if (fetch_block6(sh, s, r6s, i, disks))
  2197. break;
  2198. set_bit(STRIPE_HANDLE, &sh->state);
  2199. }
  2200. /* handle_stripe_clean_event
  2201. * any written block on an uptodate or failed drive can be returned.
  2202. * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
  2203. * never LOCKED, so we don't need to test 'failed' directly.
  2204. */
  2205. static void handle_stripe_clean_event(raid5_conf_t *conf,
  2206. struct stripe_head *sh, int disks, struct bio **return_bi)
  2207. {
  2208. int i;
  2209. struct r5dev *dev;
  2210. for (i = disks; i--; )
  2211. if (sh->dev[i].written) {
  2212. dev = &sh->dev[i];
  2213. if (!test_bit(R5_LOCKED, &dev->flags) &&
  2214. test_bit(R5_UPTODATE, &dev->flags)) {
  2215. /* We can return any write requests */
  2216. struct bio *wbi, *wbi2;
  2217. int bitmap_end = 0;
  2218. pr_debug("Return write for disc %d\n", i);
  2219. spin_lock_irq(&conf->device_lock);
  2220. wbi = dev->written;
  2221. dev->written = NULL;
  2222. while (wbi && wbi->bi_sector <
  2223. dev->sector + STRIPE_SECTORS) {
  2224. wbi2 = r5_next_bio(wbi, dev->sector);
  2225. if (!raid5_dec_bi_phys_segments(wbi)) {
  2226. md_write_end(conf->mddev);
  2227. wbi->bi_next = *return_bi;
  2228. *return_bi = wbi;
  2229. }
  2230. wbi = wbi2;
  2231. }
  2232. if (dev->towrite == NULL)
  2233. bitmap_end = 1;
  2234. spin_unlock_irq(&conf->device_lock);
  2235. if (bitmap_end)
  2236. bitmap_endwrite(conf->mddev->bitmap,
  2237. sh->sector,
  2238. STRIPE_SECTORS,
  2239. !test_bit(STRIPE_DEGRADED, &sh->state),
  2240. 0);
  2241. }
  2242. }
  2243. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  2244. if (atomic_dec_and_test(&conf->pending_full_writes))
  2245. md_wakeup_thread(conf->mddev->thread);
  2246. }
  2247. static void handle_stripe_dirtying5(raid5_conf_t *conf,
  2248. struct stripe_head *sh, struct stripe_head_state *s, int disks)
  2249. {
  2250. int rmw = 0, rcw = 0, i;
  2251. for (i = disks; i--; ) {
  2252. /* would I have to read this buffer for read_modify_write */
  2253. struct r5dev *dev = &sh->dev[i];
  2254. if ((dev->towrite || i == sh->pd_idx) &&
  2255. !test_bit(R5_LOCKED, &dev->flags) &&
  2256. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2257. test_bit(R5_Wantcompute, &dev->flags))) {
  2258. if (test_bit(R5_Insync, &dev->flags))
  2259. rmw++;
  2260. else
  2261. rmw += 2*disks; /* cannot read it */
  2262. }
  2263. /* Would I have to read this buffer for reconstruct_write */
  2264. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  2265. !test_bit(R5_LOCKED, &dev->flags) &&
  2266. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2267. test_bit(R5_Wantcompute, &dev->flags))) {
  2268. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  2269. else
  2270. rcw += 2*disks;
  2271. }
  2272. }
  2273. pr_debug("for sector %llu, rmw=%d rcw=%d\n",
  2274. (unsigned long long)sh->sector, rmw, rcw);
  2275. set_bit(STRIPE_HANDLE, &sh->state);
  2276. if (rmw < rcw && rmw > 0)
  2277. /* prefer read-modify-write, but need to get some data */
  2278. for (i = disks; i--; ) {
  2279. struct r5dev *dev = &sh->dev[i];
  2280. if ((dev->towrite || i == sh->pd_idx) &&
  2281. !test_bit(R5_LOCKED, &dev->flags) &&
  2282. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2283. test_bit(R5_Wantcompute, &dev->flags)) &&
  2284. test_bit(R5_Insync, &dev->flags)) {
  2285. if (
  2286. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2287. pr_debug("Read_old block "
  2288. "%d for r-m-w\n", i);
  2289. set_bit(R5_LOCKED, &dev->flags);
  2290. set_bit(R5_Wantread, &dev->flags);
  2291. s->locked++;
  2292. } else {
  2293. set_bit(STRIPE_DELAYED, &sh->state);
  2294. set_bit(STRIPE_HANDLE, &sh->state);
  2295. }
  2296. }
  2297. }
  2298. if (rcw <= rmw && rcw > 0)
  2299. /* want reconstruct write, but need to get some data */
  2300. for (i = disks; i--; ) {
  2301. struct r5dev *dev = &sh->dev[i];
  2302. if (!test_bit(R5_OVERWRITE, &dev->flags) &&
  2303. i != sh->pd_idx &&
  2304. !test_bit(R5_LOCKED, &dev->flags) &&
  2305. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2306. test_bit(R5_Wantcompute, &dev->flags)) &&
  2307. test_bit(R5_Insync, &dev->flags)) {
  2308. if (
  2309. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2310. pr_debug("Read_old block "
  2311. "%d for Reconstruct\n", i);
  2312. set_bit(R5_LOCKED, &dev->flags);
  2313. set_bit(R5_Wantread, &dev->flags);
  2314. s->locked++;
  2315. } else {
  2316. set_bit(STRIPE_DELAYED, &sh->state);
  2317. set_bit(STRIPE_HANDLE, &sh->state);
  2318. }
  2319. }
  2320. }
  2321. /* now if nothing is locked, and if we have enough data,
  2322. * we can start a write request
  2323. */
  2324. /* since handle_stripe can be called at any time we need to handle the
  2325. * case where a compute block operation has been submitted and then a
  2326. * subsequent call wants to start a write request. raid_run_ops only
  2327. * handles the case where compute block and reconstruct are requested
  2328. * simultaneously. If this is not the case then new writes need to be
  2329. * held off until the compute completes.
  2330. */
  2331. if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
  2332. (s->locked == 0 && (rcw == 0 || rmw == 0) &&
  2333. !test_bit(STRIPE_BIT_DELAY, &sh->state)))
  2334. schedule_reconstruction(sh, s, rcw == 0, 0);
  2335. }
  2336. static void handle_stripe_dirtying6(raid5_conf_t *conf,
  2337. struct stripe_head *sh, struct stripe_head_state *s,
  2338. struct r6_state *r6s, int disks)
  2339. {
  2340. int rcw = 0, pd_idx = sh->pd_idx, i;
  2341. int qd_idx = sh->qd_idx;
  2342. set_bit(STRIPE_HANDLE, &sh->state);
  2343. for (i = disks; i--; ) {
  2344. struct r5dev *dev = &sh->dev[i];
  2345. /* check if we haven't enough data */
  2346. if (!test_bit(R5_OVERWRITE, &dev->flags) &&
  2347. i != pd_idx && i != qd_idx &&
  2348. !test_bit(R5_LOCKED, &dev->flags) &&
  2349. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2350. test_bit(R5_Wantcompute, &dev->flags))) {
  2351. rcw++;
  2352. if (!test_bit(R5_Insync, &dev->flags))
  2353. continue; /* it's a failed drive */
  2354. if (
  2355. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2356. pr_debug("Read_old stripe %llu "
  2357. "block %d for Reconstruct\n",
  2358. (unsigned long long)sh->sector, i);
  2359. set_bit(R5_LOCKED, &dev->flags);
  2360. set_bit(R5_Wantread, &dev->flags);
  2361. s->locked++;
  2362. } else {
  2363. pr_debug("Request delayed stripe %llu "
  2364. "block %d for Reconstruct\n",
  2365. (unsigned long long)sh->sector, i);
  2366. set_bit(STRIPE_DELAYED, &sh->state);
  2367. set_bit(STRIPE_HANDLE, &sh->state);
  2368. }
  2369. }
  2370. }
  2371. /* now if nothing is locked, and if we have enough data, we can start a
  2372. * write request
  2373. */
  2374. if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
  2375. s->locked == 0 && rcw == 0 &&
  2376. !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
  2377. schedule_reconstruction(sh, s, 1, 0);
  2378. }
  2379. }
  2380. static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
  2381. struct stripe_head_state *s, int disks)
  2382. {
  2383. struct r5dev *dev = NULL;
  2384. set_bit(STRIPE_HANDLE, &sh->state);
  2385. switch (sh->check_state) {
  2386. case check_state_idle:
  2387. /* start a new check operation if there are no failures */
  2388. if (s->failed == 0) {
  2389. BUG_ON(s->uptodate != disks);
  2390. sh->check_state = check_state_run;
  2391. set_bit(STRIPE_OP_CHECK, &s->ops_request);
  2392. clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
  2393. s->uptodate--;
  2394. break;
  2395. }
  2396. dev = &sh->dev[s->failed_num];
  2397. /* fall through */
  2398. case check_state_compute_result:
  2399. sh->check_state = check_state_idle;
  2400. if (!dev)
  2401. dev = &sh->dev[sh->pd_idx];
  2402. /* check that a write has not made the stripe insync */
  2403. if (test_bit(STRIPE_INSYNC, &sh->state))
  2404. break;
  2405. /* either failed parity check, or recovery is happening */
  2406. BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
  2407. BUG_ON(s->uptodate != disks);
  2408. set_bit(R5_LOCKED, &dev->flags);
  2409. s->locked++;
  2410. set_bit(R5_Wantwrite, &dev->flags);
  2411. clear_bit(STRIPE_DEGRADED, &sh->state);
  2412. set_bit(STRIPE_INSYNC, &sh->state);
  2413. break;
  2414. case check_state_run:
  2415. break; /* we will be called again upon completion */
  2416. case check_state_check_result:
  2417. sh->check_state = check_state_idle;
  2418. /* if a failure occurred during the check operation, leave
  2419. * STRIPE_INSYNC not set and let the stripe be handled again
  2420. */
  2421. if (s->failed)
  2422. break;
  2423. /* handle a successful check operation, if parity is correct
  2424. * we are done. Otherwise update the mismatch count and repair
  2425. * parity if !MD_RECOVERY_CHECK
  2426. */
  2427. if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
  2428. /* parity is correct (on disc,
  2429. * not in buffer any more)
  2430. */
  2431. set_bit(STRIPE_INSYNC, &sh->state);
  2432. else {
  2433. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  2434. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2435. /* don't try to repair!! */
  2436. set_bit(STRIPE_INSYNC, &sh->state);
  2437. else {
  2438. sh->check_state = check_state_compute_run;
  2439. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2440. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2441. set_bit(R5_Wantcompute,
  2442. &sh->dev[sh->pd_idx].flags);
  2443. sh->ops.target = sh->pd_idx;
  2444. sh->ops.target2 = -1;
  2445. s->uptodate++;
  2446. }
  2447. }
  2448. break;
  2449. case check_state_compute_run:
  2450. break;
  2451. default:
  2452. printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
  2453. __func__, sh->check_state,
  2454. (unsigned long long) sh->sector);
  2455. BUG();
  2456. }
  2457. }
  2458. static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
  2459. struct stripe_head_state *s,
  2460. struct r6_state *r6s, int disks)
  2461. {
  2462. int pd_idx = sh->pd_idx;
  2463. int qd_idx = sh->qd_idx;
  2464. struct r5dev *dev;
  2465. set_bit(STRIPE_HANDLE, &sh->state);
  2466. BUG_ON(s->failed > 2);
  2467. /* Want to check and possibly repair P and Q.
  2468. * However there could be one 'failed' device, in which
  2469. * case we can only check one of them, possibly using the
  2470. * other to generate missing data
  2471. */
  2472. switch (sh->check_state) {
  2473. case check_state_idle:
  2474. /* start a new check operation if there are < 2 failures */
  2475. if (s->failed == r6s->q_failed) {
  2476. /* The only possible failed device holds Q, so it
  2477. * makes sense to check P (If anything else were failed,
  2478. * we would have used P to recreate it).
  2479. */
  2480. sh->check_state = check_state_run;
  2481. }
  2482. if (!r6s->q_failed && s->failed < 2) {
  2483. /* Q is not failed, and we didn't use it to generate
  2484. * anything, so it makes sense to check it
  2485. */
  2486. if (sh->check_state == check_state_run)
  2487. sh->check_state = check_state_run_pq;
  2488. else
  2489. sh->check_state = check_state_run_q;
  2490. }
  2491. /* discard potentially stale zero_sum_result */
  2492. sh->ops.zero_sum_result = 0;
  2493. if (sh->check_state == check_state_run) {
  2494. /* async_xor_zero_sum destroys the contents of P */
  2495. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  2496. s->uptodate--;
  2497. }
  2498. if (sh->check_state >= check_state_run &&
  2499. sh->check_state <= check_state_run_pq) {
  2500. /* async_syndrome_zero_sum preserves P and Q, so
  2501. * no need to mark them !uptodate here
  2502. */
  2503. set_bit(STRIPE_OP_CHECK, &s->ops_request);
  2504. break;
  2505. }
  2506. /* we have 2-disk failure */
  2507. BUG_ON(s->failed != 2);
  2508. /* fall through */
  2509. case check_state_compute_result:
  2510. sh->check_state = check_state_idle;
  2511. /* check that a write has not made the stripe insync */
  2512. if (test_bit(STRIPE_INSYNC, &sh->state))
  2513. break;
  2514. /* now write out any block on a failed drive,
  2515. * or P or Q if they were recomputed
  2516. */
  2517. BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
  2518. if (s->failed == 2) {
  2519. dev = &sh->dev[r6s->failed_num[1]];
  2520. s->locked++;
  2521. set_bit(R5_LOCKED, &dev->flags);
  2522. set_bit(R5_Wantwrite, &dev->flags);
  2523. }
  2524. if (s->failed >= 1) {
  2525. dev = &sh->dev[r6s->failed_num[0]];
  2526. s->locked++;
  2527. set_bit(R5_LOCKED, &dev->flags);
  2528. set_bit(R5_Wantwrite, &dev->flags);
  2529. }
  2530. if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
  2531. dev = &sh->dev[pd_idx];
  2532. s->locked++;
  2533. set_bit(R5_LOCKED, &dev->flags);
  2534. set_bit(R5_Wantwrite, &dev->flags);
  2535. }
  2536. if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
  2537. dev = &sh->dev[qd_idx];
  2538. s->locked++;
  2539. set_bit(R5_LOCKED, &dev->flags);
  2540. set_bit(R5_Wantwrite, &dev->flags);
  2541. }
  2542. clear_bit(STRIPE_DEGRADED, &sh->state);
  2543. set_bit(STRIPE_INSYNC, &sh->state);
  2544. break;
  2545. case check_state_run:
  2546. case check_state_run_q:
  2547. case check_state_run_pq:
  2548. break; /* we will be called again upon completion */
  2549. case check_state_check_result:
  2550. sh->check_state = check_state_idle;
  2551. /* handle a successful check operation, if parity is correct
  2552. * we are done. Otherwise update the mismatch count and repair
  2553. * parity if !MD_RECOVERY_CHECK
  2554. */
  2555. if (sh->ops.zero_sum_result == 0) {
  2556. /* both parities are correct */
  2557. if (!s->failed)
  2558. set_bit(STRIPE_INSYNC, &sh->state);
  2559. else {
  2560. /* in contrast to the raid5 case we can validate
  2561. * parity, but still have a failure to write
  2562. * back
  2563. */
  2564. sh->check_state = check_state_compute_result;
  2565. /* Returning at this point means that we may go
  2566. * off and bring p and/or q uptodate again so
  2567. * we make sure to check zero_sum_result again
  2568. * to verify if p or q need writeback
  2569. */
  2570. }
  2571. } else {
  2572. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  2573. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2574. /* don't try to repair!! */
  2575. set_bit(STRIPE_INSYNC, &sh->state);
  2576. else {
  2577. int *target = &sh->ops.target;
  2578. sh->ops.target = -1;
  2579. sh->ops.target2 = -1;
  2580. sh->check_state = check_state_compute_run;
  2581. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2582. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2583. if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
  2584. set_bit(R5_Wantcompute,
  2585. &sh->dev[pd_idx].flags);
  2586. *target = pd_idx;
  2587. target = &sh->ops.target2;
  2588. s->uptodate++;
  2589. }
  2590. if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
  2591. set_bit(R5_Wantcompute,
  2592. &sh->dev[qd_idx].flags);
  2593. *target = qd_idx;
  2594. s->uptodate++;
  2595. }
  2596. }
  2597. }
  2598. break;
  2599. case check_state_compute_run:
  2600. break;
  2601. default:
  2602. printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
  2603. __func__, sh->check_state,
  2604. (unsigned long long) sh->sector);
  2605. BUG();
  2606. }
  2607. }
  2608. static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
  2609. struct r6_state *r6s)
  2610. {
  2611. int i;
  2612. /* We have read all the blocks in this stripe and now we need to
  2613. * copy some of them into a target stripe for expand.
  2614. */
  2615. struct dma_async_tx_descriptor *tx = NULL;
  2616. clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2617. for (i = 0; i < sh->disks; i++)
  2618. if (i != sh->pd_idx && i != sh->qd_idx) {
  2619. int dd_idx, j;
  2620. struct stripe_head *sh2;
  2621. struct async_submit_ctl submit;
  2622. sector_t bn = compute_blocknr(sh, i, 1);
  2623. sector_t s = raid5_compute_sector(conf, bn, 0,
  2624. &dd_idx, NULL);
  2625. sh2 = get_active_stripe(conf, s, 0, 1, 1);
  2626. if (sh2 == NULL)
  2627. /* so far only the early blocks of this stripe
  2628. * have been requested. When later blocks
  2629. * get requested, we will try again
  2630. */
  2631. continue;
  2632. if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
  2633. test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
  2634. /* must have already done this block */
  2635. release_stripe(sh2);
  2636. continue;
  2637. }
  2638. /* place all the copies on one channel */
  2639. init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
  2640. tx = async_memcpy(sh2->dev[dd_idx].page,
  2641. sh->dev[i].page, 0, 0, STRIPE_SIZE,
  2642. &submit);
  2643. set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
  2644. set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
  2645. for (j = 0; j < conf->raid_disks; j++)
  2646. if (j != sh2->pd_idx &&
  2647. (!r6s || j != sh2->qd_idx) &&
  2648. !test_bit(R5_Expanded, &sh2->dev[j].flags))
  2649. break;
  2650. if (j == conf->raid_disks) {
  2651. set_bit(STRIPE_EXPAND_READY, &sh2->state);
  2652. set_bit(STRIPE_HANDLE, &sh2->state);
  2653. }
  2654. release_stripe(sh2);
  2655. }
  2656. /* done submitting copies, wait for them to complete */
  2657. if (tx) {
  2658. async_tx_ack(tx);
  2659. dma_wait_for_async_tx(tx);
  2660. }
  2661. }
  2662. /*
  2663. * handle_stripe - do things to a stripe.
  2664. *
  2665. * We lock the stripe and then examine the state of various bits
  2666. * to see what needs to be done.
  2667. * Possible results:
  2668. * return some read request which now have data
  2669. * return some write requests which are safely on disc
  2670. * schedule a read on some buffers
  2671. * schedule a write of some buffers
  2672. * return confirmation of parity correctness
  2673. *
  2674. * buffers are taken off read_list or write_list, and bh_cache buffers
  2675. * get BH_Lock set before the stripe lock is released.
  2676. *
  2677. */
  2678. static void handle_stripe5(struct stripe_head *sh)
  2679. {
  2680. raid5_conf_t *conf = sh->raid_conf;
  2681. int disks = sh->disks, i;
  2682. struct bio *return_bi = NULL;
  2683. struct stripe_head_state s;
  2684. struct r5dev *dev;
  2685. mdk_rdev_t *blocked_rdev = NULL;
  2686. int prexor;
  2687. int dec_preread_active = 0;
  2688. memset(&s, 0, sizeof(s));
  2689. pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
  2690. "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state,
  2691. atomic_read(&sh->count), sh->pd_idx, sh->check_state,
  2692. sh->reconstruct_state);
  2693. spin_lock(&sh->lock);
  2694. clear_bit(STRIPE_HANDLE, &sh->state);
  2695. clear_bit(STRIPE_DELAYED, &sh->state);
  2696. s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
  2697. s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2698. s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  2699. /* Now to look around and see what can be done */
  2700. rcu_read_lock();
  2701. for (i=disks; i--; ) {
  2702. mdk_rdev_t *rdev;
  2703. dev = &sh->dev[i];
  2704. pr_debug("check %d: state 0x%lx toread %p read %p write %p "
  2705. "written %p\n", i, dev->flags, dev->toread, dev->read,
  2706. dev->towrite, dev->written);
  2707. /* maybe we can request a biofill operation
  2708. *
  2709. * new wantfill requests are only permitted while
  2710. * ops_complete_biofill is guaranteed to be inactive
  2711. */
  2712. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
  2713. !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
  2714. set_bit(R5_Wantfill, &dev->flags);
  2715. /* now count some things */
  2716. if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
  2717. if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
  2718. if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
  2719. if (test_bit(R5_Wantfill, &dev->flags))
  2720. s.to_fill++;
  2721. else if (dev->toread)
  2722. s.to_read++;
  2723. if (dev->towrite) {
  2724. s.to_write++;
  2725. if (!test_bit(R5_OVERWRITE, &dev->flags))
  2726. s.non_overwrite++;
  2727. }
  2728. if (dev->written)
  2729. s.written++;
  2730. rdev = rcu_dereference(conf->disks[i].rdev);
  2731. if (blocked_rdev == NULL &&
  2732. rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  2733. blocked_rdev = rdev;
  2734. atomic_inc(&rdev->nr_pending);
  2735. }
  2736. clear_bit(R5_Insync, &dev->flags);
  2737. if (!rdev)
  2738. /* Not in-sync */;
  2739. else if (test_bit(In_sync, &rdev->flags))
  2740. set_bit(R5_Insync, &dev->flags);
  2741. else if (!test_bit(Faulty, &rdev->flags)) {
  2742. /* could be in-sync depending on recovery/reshape status */
  2743. if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
  2744. set_bit(R5_Insync, &dev->flags);
  2745. }
  2746. if (!test_bit(R5_Insync, &dev->flags)) {
  2747. /* The ReadError flag will just be confusing now */
  2748. clear_bit(R5_ReadError, &dev->flags);
  2749. clear_bit(R5_ReWrite, &dev->flags);
  2750. }
  2751. if (test_bit(R5_ReadError, &dev->flags))
  2752. clear_bit(R5_Insync, &dev->flags);
  2753. if (!test_bit(R5_Insync, &dev->flags)) {
  2754. s.failed++;
  2755. s.failed_num = i;
  2756. }
  2757. }
  2758. rcu_read_unlock();
  2759. if (unlikely(blocked_rdev)) {
  2760. if (s.syncing || s.expanding || s.expanded ||
  2761. s.to_write || s.written) {
  2762. set_bit(STRIPE_HANDLE, &sh->state);
  2763. goto unlock;
  2764. }
  2765. /* There is nothing for the blocked_rdev to block */
  2766. rdev_dec_pending(blocked_rdev, conf->mddev);
  2767. blocked_rdev = NULL;
  2768. }
  2769. if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
  2770. set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
  2771. set_bit(STRIPE_BIOFILL_RUN, &sh->state);
  2772. }
  2773. pr_debug("locked=%d uptodate=%d to_read=%d"
  2774. " to_write=%d failed=%d failed_num=%d\n",
  2775. s.locked, s.uptodate, s.to_read, s.to_write,
  2776. s.failed, s.failed_num);
  2777. /* check if the array has lost two devices and, if so, some requests might
  2778. * need to be failed
  2779. */
  2780. if (s.failed > 1) {
  2781. sh->check_state = 0;
  2782. sh->reconstruct_state = 0;
  2783. if (s.to_read+s.to_write+s.written)
  2784. handle_failed_stripe(conf, sh, &s, disks, &return_bi);
  2785. if (s.syncing) {
  2786. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  2787. clear_bit(STRIPE_SYNCING, &sh->state);
  2788. s.syncing = 0;
  2789. }
  2790. }
  2791. /* might be able to return some write requests if the parity block
  2792. * is safe, or on a failed drive
  2793. */
  2794. dev = &sh->dev[sh->pd_idx];
  2795. if ( s.written &&
  2796. ((test_bit(R5_Insync, &dev->flags) &&
  2797. !test_bit(R5_LOCKED, &dev->flags) &&
  2798. test_bit(R5_UPTODATE, &dev->flags)) ||
  2799. (s.failed == 1 && s.failed_num == sh->pd_idx)))
  2800. handle_stripe_clean_event(conf, sh, disks, &return_bi);
  2801. /* Now we might consider reading some blocks, either to check/generate
  2802. * parity, or to satisfy requests
  2803. * or to load a block that is being partially written.
  2804. */
  2805. if (s.to_read || s.non_overwrite ||
  2806. (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
  2807. handle_stripe_fill5(sh, &s, disks);
  2808. /* Now we check to see if any write operations have recently
  2809. * completed
  2810. */
  2811. prexor = 0;
  2812. if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
  2813. prexor = 1;
  2814. if (sh->reconstruct_state == reconstruct_state_drain_result ||
  2815. sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
  2816. sh->reconstruct_state = reconstruct_state_idle;
  2817. /* All the 'written' buffers and the parity block are ready to
  2818. * be written back to disk
  2819. */
  2820. BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
  2821. for (i = disks; i--; ) {
  2822. dev = &sh->dev[i];
  2823. if (test_bit(R5_LOCKED, &dev->flags) &&
  2824. (i == sh->pd_idx || dev->written)) {
  2825. pr_debug("Writing block %d\n", i);
  2826. set_bit(R5_Wantwrite, &dev->flags);
  2827. if (prexor)
  2828. continue;
  2829. if (!test_bit(R5_Insync, &dev->flags) ||
  2830. (i == sh->pd_idx && s.failed == 0))
  2831. set_bit(STRIPE_INSYNC, &sh->state);
  2832. }
  2833. }
  2834. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  2835. dec_preread_active = 1;
  2836. }
  2837. /* Now to consider new write requests and what else, if anything
  2838. * should be read. We do not handle new writes when:
  2839. * 1/ A 'write' operation (copy+xor) is already in flight.
  2840. * 2/ A 'check' operation is in flight, as it may clobber the parity
  2841. * block.
  2842. */
  2843. if (s.to_write && !sh->reconstruct_state && !sh->check_state)
  2844. handle_stripe_dirtying5(conf, sh, &s, disks);
  2845. /* maybe we need to check and possibly fix the parity for this stripe
  2846. * Any reads will already have been scheduled, so we just see if enough
  2847. * data is available. The parity check is held off while parity
  2848. * dependent operations are in flight.
  2849. */
  2850. if (sh->check_state ||
  2851. (s.syncing && s.locked == 0 &&
  2852. !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
  2853. !test_bit(STRIPE_INSYNC, &sh->state)))
  2854. handle_parity_checks5(conf, sh, &s, disks);
  2855. if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  2856. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  2857. clear_bit(STRIPE_SYNCING, &sh->state);
  2858. }
  2859. /* If the failed drive is just a ReadError, then we might need to progress
  2860. * the repair/check process
  2861. */
  2862. if (s.failed == 1 && !conf->mddev->ro &&
  2863. test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
  2864. && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
  2865. && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
  2866. ) {
  2867. dev = &sh->dev[s.failed_num];
  2868. if (!test_bit(R5_ReWrite, &dev->flags)) {
  2869. set_bit(R5_Wantwrite, &dev->flags);
  2870. set_bit(R5_ReWrite, &dev->flags);
  2871. set_bit(R5_LOCKED, &dev->flags);
  2872. s.locked++;
  2873. } else {
  2874. /* let's read it back */
  2875. set_bit(R5_Wantread, &dev->flags);
  2876. set_bit(R5_LOCKED, &dev->flags);
  2877. s.locked++;
  2878. }
  2879. }
  2880. /* Finish reconstruct operations initiated by the expansion process */
  2881. if (sh->reconstruct_state == reconstruct_state_result) {
  2882. struct stripe_head *sh2
  2883. = get_active_stripe(conf, sh->sector, 1, 1, 1);
  2884. if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
  2885. /* sh cannot be written until sh2 has been read.
  2886. * so arrange for sh to be delayed a little
  2887. */
  2888. set_bit(STRIPE_DELAYED, &sh->state);
  2889. set_bit(STRIPE_HANDLE, &sh->state);
  2890. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
  2891. &sh2->state))
  2892. atomic_inc(&conf->preread_active_stripes);
  2893. release_stripe(sh2);
  2894. goto unlock;
  2895. }
  2896. if (sh2)
  2897. release_stripe(sh2);
  2898. sh->reconstruct_state = reconstruct_state_idle;
  2899. clear_bit(STRIPE_EXPANDING, &sh->state);
  2900. for (i = conf->raid_disks; i--; ) {
  2901. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  2902. set_bit(R5_LOCKED, &sh->dev[i].flags);
  2903. s.locked++;
  2904. }
  2905. }
  2906. if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
  2907. !sh->reconstruct_state) {
  2908. /* Need to write out all blocks after computing parity */
  2909. sh->disks = conf->raid_disks;
  2910. stripe_set_idx(sh->sector, conf, 0, sh);
  2911. schedule_reconstruction(sh, &s, 1, 1);
  2912. } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
  2913. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  2914. atomic_dec(&conf->reshape_stripes);
  2915. wake_up(&conf->wait_for_overlap);
  2916. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  2917. }
  2918. if (s.expanding && s.locked == 0 &&
  2919. !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
  2920. handle_stripe_expansion(conf, sh, NULL);
  2921. unlock:
  2922. spin_unlock(&sh->lock);
  2923. /* wait for this device to become unblocked */
  2924. if (unlikely(blocked_rdev))
  2925. md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
  2926. if (s.ops_request)
  2927. raid_run_ops(sh, s.ops_request);
  2928. ops_run_io(sh, &s);
  2929. if (dec_preread_active) {
  2930. /* We delay this until after ops_run_io so that if make_request
  2931. * is waiting on a flush, it won't continue until the writes
  2932. * have actually been submitted.
  2933. */
  2934. atomic_dec(&conf->preread_active_stripes);
  2935. if (atomic_read(&conf->preread_active_stripes) <
  2936. IO_THRESHOLD)
  2937. md_wakeup_thread(conf->mddev->thread);
  2938. }
  2939. return_io(return_bi);
  2940. }
  2941. static void handle_stripe6(struct stripe_head *sh)
  2942. {
  2943. raid5_conf_t *conf = sh->raid_conf;
  2944. int disks = sh->disks;
  2945. struct bio *return_bi = NULL;
  2946. int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx;
  2947. struct stripe_head_state s;
  2948. struct r6_state r6s;
  2949. struct r5dev *dev, *pdev, *qdev;
  2950. mdk_rdev_t *blocked_rdev = NULL;
  2951. int dec_preread_active = 0;
  2952. pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
  2953. "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
  2954. (unsigned long long)sh->sector, sh->state,
  2955. atomic_read(&sh->count), pd_idx, qd_idx,
  2956. sh->check_state, sh->reconstruct_state);
  2957. memset(&s, 0, sizeof(s));
  2958. spin_lock(&sh->lock);
  2959. clear_bit(STRIPE_HANDLE, &sh->state);
  2960. clear_bit(STRIPE_DELAYED, &sh->state);
  2961. s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
  2962. s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2963. s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  2964. /* Now to look around and see what can be done */
  2965. rcu_read_lock();
  2966. for (i=disks; i--; ) {
  2967. mdk_rdev_t *rdev;
  2968. dev = &sh->dev[i];
  2969. pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
  2970. i, dev->flags, dev->toread, dev->towrite, dev->written);
  2971. /* maybe we can reply to a read
  2972. *
  2973. * new wantfill requests are only permitted while
  2974. * ops_complete_biofill is guaranteed to be inactive
  2975. */
  2976. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
  2977. !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
  2978. set_bit(R5_Wantfill, &dev->flags);
  2979. /* now count some things */
  2980. if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
  2981. if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
  2982. if (test_bit(R5_Wantcompute, &dev->flags)) {
  2983. s.compute++;
  2984. BUG_ON(s.compute > 2);
  2985. }
  2986. if (test_bit(R5_Wantfill, &dev->flags)) {
  2987. s.to_fill++;
  2988. } else if (dev->toread)
  2989. s.to_read++;
  2990. if (dev->towrite) {
  2991. s.to_write++;
  2992. if (!test_bit(R5_OVERWRITE, &dev->flags))
  2993. s.non_overwrite++;
  2994. }
  2995. if (dev->written)
  2996. s.written++;
  2997. rdev = rcu_dereference(conf->disks[i].rdev);
  2998. if (blocked_rdev == NULL &&
  2999. rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  3000. blocked_rdev = rdev;
  3001. atomic_inc(&rdev->nr_pending);
  3002. }
  3003. clear_bit(R5_Insync, &dev->flags);
  3004. if (!rdev)
  3005. /* Not in-sync */;
  3006. else if (test_bit(In_sync, &rdev->flags))
  3007. set_bit(R5_Insync, &dev->flags);
  3008. else if (!test_bit(Faulty, &rdev->flags)) {
  3009. /* in sync if before recovery_offset */
  3010. if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
  3011. set_bit(R5_Insync, &dev->flags);
  3012. }
  3013. if (!test_bit(R5_Insync, &dev->flags)) {
  3014. /* The ReadError flag will just be confusing now */
  3015. clear_bit(R5_ReadError, &dev->flags);
  3016. clear_bit(R5_ReWrite, &dev->flags);
  3017. }
  3018. if (test_bit(R5_ReadError, &dev->flags))
  3019. clear_bit(R5_Insync, &dev->flags);
  3020. if (!test_bit(R5_Insync, &dev->flags)) {
  3021. if (s.failed < 2)
  3022. r6s.failed_num[s.failed] = i;
  3023. s.failed++;
  3024. }
  3025. }
  3026. rcu_read_unlock();
  3027. if (unlikely(blocked_rdev)) {
  3028. if (s.syncing || s.expanding || s.expanded ||
  3029. s.to_write || s.written) {
  3030. set_bit(STRIPE_HANDLE, &sh->state);
  3031. goto unlock;
  3032. }
  3033. /* There is nothing for the blocked_rdev to block */
  3034. rdev_dec_pending(blocked_rdev, conf->mddev);
  3035. blocked_rdev = NULL;
  3036. }
  3037. if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
  3038. set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
  3039. set_bit(STRIPE_BIOFILL_RUN, &sh->state);
  3040. }
  3041. pr_debug("locked=%d uptodate=%d to_read=%d"
  3042. " to_write=%d failed=%d failed_num=%d,%d\n",
  3043. s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
  3044. r6s.failed_num[0], r6s.failed_num[1]);
  3045. /* check if the array has lost >2 devices and, if so, some requests
  3046. * might need to be failed
  3047. */
  3048. if (s.failed > 2) {
  3049. sh->check_state = 0;
  3050. sh->reconstruct_state = 0;
  3051. if (s.to_read+s.to_write+s.written)
  3052. handle_failed_stripe(conf, sh, &s, disks, &return_bi);
  3053. if (s.syncing) {
  3054. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  3055. clear_bit(STRIPE_SYNCING, &sh->state);
  3056. s.syncing = 0;
  3057. }
  3058. }
  3059. /*
  3060. * might be able to return some write requests if the parity blocks
  3061. * are safe, or on a failed drive
  3062. */
  3063. pdev = &sh->dev[pd_idx];
  3064. r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
  3065. || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
  3066. qdev = &sh->dev[qd_idx];
  3067. r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == qd_idx)
  3068. || (s.failed >= 2 && r6s.failed_num[1] == qd_idx);
  3069. if ( s.written &&
  3070. ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
  3071. && !test_bit(R5_LOCKED, &pdev->flags)
  3072. && test_bit(R5_UPTODATE, &pdev->flags)))) &&
  3073. ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
  3074. && !test_bit(R5_LOCKED, &qdev->flags)
  3075. && test_bit(R5_UPTODATE, &qdev->flags)))))
  3076. handle_stripe_clean_event(conf, sh, disks, &return_bi);
  3077. /* Now we might consider reading some blocks, either to check/generate
  3078. * parity, or to satisfy requests
  3079. * or to load a block that is being partially written.
  3080. */
  3081. if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
  3082. (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
  3083. handle_stripe_fill6(sh, &s, &r6s, disks);
  3084. /* Now we check to see if any write operations have recently
  3085. * completed
  3086. */
  3087. if (sh->reconstruct_state == reconstruct_state_drain_result) {
  3088. sh->reconstruct_state = reconstruct_state_idle;
  3089. /* All the 'written' buffers and the parity blocks are ready to
  3090. * be written back to disk
  3091. */
  3092. BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
  3093. BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags));
  3094. for (i = disks; i--; ) {
  3095. dev = &sh->dev[i];
  3096. if (test_bit(R5_LOCKED, &dev->flags) &&
  3097. (i == sh->pd_idx || i == qd_idx ||
  3098. dev->written)) {
  3099. pr_debug("Writing block %d\n", i);
  3100. BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
  3101. set_bit(R5_Wantwrite, &dev->flags);
  3102. if (!test_bit(R5_Insync, &dev->flags) ||
  3103. ((i == sh->pd_idx || i == qd_idx) &&
  3104. s.failed == 0))
  3105. set_bit(STRIPE_INSYNC, &sh->state);
  3106. }
  3107. }
  3108. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  3109. dec_preread_active = 1;
  3110. }
  3111. /* Now to consider new write requests and what else, if anything
  3112. * should be read. We do not handle new writes when:
  3113. * 1/ A 'write' operation (copy+gen_syndrome) is already in flight.
  3114. * 2/ A 'check' operation is in flight, as it may clobber the parity
  3115. * block.
  3116. */
  3117. if (s.to_write && !sh->reconstruct_state && !sh->check_state)
  3118. handle_stripe_dirtying6(conf, sh, &s, &r6s, disks);
  3119. /* maybe we need to check and possibly fix the parity for this stripe
  3120. * Any reads will already have been scheduled, so we just see if enough
  3121. * data is available. The parity check is held off while parity
  3122. * dependent operations are in flight.
  3123. */
  3124. if (sh->check_state ||
  3125. (s.syncing && s.locked == 0 &&
  3126. !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
  3127. !test_bit(STRIPE_INSYNC, &sh->state)))
  3128. handle_parity_checks6(conf, sh, &s, &r6s, disks);
  3129. if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  3130. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  3131. clear_bit(STRIPE_SYNCING, &sh->state);
  3132. }
  3133. /* If the failed drives are just a ReadError, then we might need
  3134. * to progress the repair/check process
  3135. */
  3136. if (s.failed <= 2 && !conf->mddev->ro)
  3137. for (i = 0; i < s.failed; i++) {
  3138. dev = &sh->dev[r6s.failed_num[i]];
  3139. if (test_bit(R5_ReadError, &dev->flags)
  3140. && !test_bit(R5_LOCKED, &dev->flags)
  3141. && test_bit(R5_UPTODATE, &dev->flags)
  3142. ) {
  3143. if (!test_bit(R5_ReWrite, &dev->flags)) {
  3144. set_bit(R5_Wantwrite, &dev->flags);
  3145. set_bit(R5_ReWrite, &dev->flags);
  3146. set_bit(R5_LOCKED, &dev->flags);
  3147. s.locked++;
  3148. } else {
  3149. /* let's read it back */
  3150. set_bit(R5_Wantread, &dev->flags);
  3151. set_bit(R5_LOCKED, &dev->flags);
  3152. s.locked++;
  3153. }
  3154. }
  3155. }
  3156. /* Finish reconstruct operations initiated by the expansion process */
  3157. if (sh->reconstruct_state == reconstruct_state_result) {
  3158. sh->reconstruct_state = reconstruct_state_idle;
  3159. clear_bit(STRIPE_EXPANDING, &sh->state);
  3160. for (i = conf->raid_disks; i--; ) {
  3161. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  3162. set_bit(R5_LOCKED, &sh->dev[i].flags);
  3163. s.locked++;
  3164. }
  3165. }
  3166. if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
  3167. !sh->reconstruct_state) {
  3168. struct stripe_head *sh2
  3169. = get_active_stripe(conf, sh->sector, 1, 1, 1);
  3170. if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
  3171. /* sh cannot be written until sh2 has been read.
  3172. * so arrange for sh to be delayed a little
  3173. */
  3174. set_bit(STRIPE_DELAYED, &sh->state);
  3175. set_bit(STRIPE_HANDLE, &sh->state);
  3176. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
  3177. &sh2->state))
  3178. atomic_inc(&conf->preread_active_stripes);
  3179. release_stripe(sh2);
  3180. goto unlock;
  3181. }
  3182. if (sh2)
  3183. release_stripe(sh2);
  3184. /* Need to write out all blocks after computing P&Q */
  3185. sh->disks = conf->raid_disks;
  3186. stripe_set_idx(sh->sector, conf, 0, sh);
  3187. schedule_reconstruction(sh, &s, 1, 1);
  3188. } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
  3189. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  3190. atomic_dec(&conf->reshape_stripes);
  3191. wake_up(&conf->wait_for_overlap);
  3192. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  3193. }
  3194. if (s.expanding && s.locked == 0 &&
  3195. !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
  3196. handle_stripe_expansion(conf, sh, &r6s);
  3197. unlock:
  3198. spin_unlock(&sh->lock);
  3199. /* wait for this device to become unblocked */
  3200. if (unlikely(blocked_rdev))
  3201. md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
  3202. if (s.ops_request)
  3203. raid_run_ops(sh, s.ops_request);
  3204. ops_run_io(sh, &s);
  3205. if (dec_preread_active) {
  3206. /* We delay this until after ops_run_io so that if make_request
  3207. * is waiting on a flush, it won't continue until the writes
  3208. * have actually been submitted.
  3209. */
  3210. atomic_dec(&conf->preread_active_stripes);
  3211. if (atomic_read(&conf->preread_active_stripes) <
  3212. IO_THRESHOLD)
  3213. md_wakeup_thread(conf->mddev->thread);
  3214. }
  3215. return_io(return_bi);
  3216. }
  3217. static void handle_stripe(struct stripe_head *sh)
  3218. {
  3219. if (sh->raid_conf->level == 6)
  3220. handle_stripe6(sh);
  3221. else
  3222. handle_stripe5(sh);
  3223. }
  3224. static void raid5_activate_delayed(raid5_conf_t *conf)
  3225. {
  3226. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
  3227. while (!list_empty(&conf->delayed_list)) {
  3228. struct list_head *l = conf->delayed_list.next;
  3229. struct stripe_head *sh;
  3230. sh = list_entry(l, struct stripe_head, lru);
  3231. list_del_init(l);
  3232. clear_bit(STRIPE_DELAYED, &sh->state);
  3233. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  3234. atomic_inc(&conf->preread_active_stripes);
  3235. list_add_tail(&sh->lru, &conf->hold_list);
  3236. }
  3237. }
  3238. }
  3239. static void activate_bit_delay(raid5_conf_t *conf)
  3240. {
  3241. /* device_lock is held */
  3242. struct list_head head;
  3243. list_add(&head, &conf->bitmap_list);
  3244. list_del_init(&conf->bitmap_list);
  3245. while (!list_empty(&head)) {
  3246. struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
  3247. list_del_init(&sh->lru);
  3248. atomic_inc(&sh->count);
  3249. __release_stripe(conf, sh);
  3250. }
  3251. }
  3252. int md_raid5_congested(mddev_t *mddev, int bits)
  3253. {
  3254. raid5_conf_t *conf = mddev->private;
  3255. /* No difference between reads and writes. Just check
  3256. * how busy the stripe_cache is
  3257. */
  3258. if (conf->inactive_blocked)
  3259. return 1;
  3260. if (conf->quiesce)
  3261. return 1;
  3262. if (list_empty_careful(&conf->inactive_list))
  3263. return 1;
  3264. return 0;
  3265. }
  3266. EXPORT_SYMBOL_GPL(md_raid5_congested);
  3267. static int raid5_congested(void *data, int bits)
  3268. {
  3269. mddev_t *mddev = data;
  3270. return mddev_congested(mddev, bits) ||
  3271. md_raid5_congested(mddev, bits);
  3272. }
  3273. /* We want read requests to align with chunks where possible,
  3274. * but write requests don't need to.
  3275. */
  3276. static int raid5_mergeable_bvec(struct request_queue *q,
  3277. struct bvec_merge_data *bvm,
  3278. struct bio_vec *biovec)
  3279. {
  3280. mddev_t *mddev = q->queuedata;
  3281. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  3282. int max;
  3283. unsigned int chunk_sectors = mddev->chunk_sectors;
  3284. unsigned int bio_sectors = bvm->bi_size >> 9;
  3285. if ((bvm->bi_rw & 1) == WRITE)
  3286. return biovec->bv_len; /* always allow writes to be mergeable */
  3287. if (mddev->new_chunk_sectors < mddev->chunk_sectors)
  3288. chunk_sectors = mddev->new_chunk_sectors;
  3289. max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
  3290. if (max < 0) max = 0;
  3291. if (max <= biovec->bv_len && bio_sectors == 0)
  3292. return biovec->bv_len;
  3293. else
  3294. return max;
  3295. }
  3296. static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
  3297. {
  3298. sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
  3299. unsigned int chunk_sectors = mddev->chunk_sectors;
  3300. unsigned int bio_sectors = bio->bi_size >> 9;
  3301. if (mddev->new_chunk_sectors < mddev->chunk_sectors)
  3302. chunk_sectors = mddev->new_chunk_sectors;
  3303. return chunk_sectors >=
  3304. ((sector & (chunk_sectors - 1)) + bio_sectors);
  3305. }
  3306. /*
  3307. * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
  3308. * later sampled by raid5d.
  3309. */
  3310. static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
  3311. {
  3312. unsigned long flags;
  3313. spin_lock_irqsave(&conf->device_lock, flags);
  3314. bi->bi_next = conf->retry_read_aligned_list;
  3315. conf->retry_read_aligned_list = bi;
  3316. spin_unlock_irqrestore(&conf->device_lock, flags);
  3317. md_wakeup_thread(conf->mddev->thread);
  3318. }
  3319. static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
  3320. {
  3321. struct bio *bi;
  3322. bi = conf->retry_read_aligned;
  3323. if (bi) {
  3324. conf->retry_read_aligned = NULL;
  3325. return bi;
  3326. }
  3327. bi = conf->retry_read_aligned_list;
  3328. if(bi) {
  3329. conf->retry_read_aligned_list = bi->bi_next;
  3330. bi->bi_next = NULL;
  3331. /*
  3332. * this sets the active strip count to 1 and the processed
  3333. * strip count to zero (upper 8 bits)
  3334. */
  3335. bi->bi_phys_segments = 1; /* biased count of active stripes */
  3336. }
  3337. return bi;
  3338. }
  3339. /*
  3340. * The "raid5_align_endio" should check if the read succeeded and if it
  3341. * did, call bio_endio on the original bio (having bio_put the new bio
  3342. * first).
  3343. * If the read failed..
  3344. */
  3345. static void raid5_align_endio(struct bio *bi, int error)
  3346. {
  3347. struct bio* raid_bi = bi->bi_private;
  3348. mddev_t *mddev;
  3349. raid5_conf_t *conf;
  3350. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  3351. mdk_rdev_t *rdev;
  3352. bio_put(bi);
  3353. rdev = (void*)raid_bi->bi_next;
  3354. raid_bi->bi_next = NULL;
  3355. mddev = rdev->mddev;
  3356. conf = mddev->private;
  3357. rdev_dec_pending(rdev, conf->mddev);
  3358. if (!error && uptodate) {
  3359. bio_endio(raid_bi, 0);
  3360. if (atomic_dec_and_test(&conf->active_aligned_reads))
  3361. wake_up(&conf->wait_for_stripe);
  3362. return;
  3363. }
  3364. pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
  3365. add_bio_to_retry(raid_bi, conf);
  3366. }
  3367. static int bio_fits_rdev(struct bio *bi)
  3368. {
  3369. struct request_queue *q = bdev_get_queue(bi->bi_bdev);
  3370. if ((bi->bi_size>>9) > queue_max_sectors(q))
  3371. return 0;
  3372. blk_recount_segments(q, bi);
  3373. if (bi->bi_phys_segments > queue_max_segments(q))
  3374. return 0;
  3375. if (q->merge_bvec_fn)
  3376. /* it's too hard to apply the merge_bvec_fn at this stage,
  3377. * just just give up
  3378. */
  3379. return 0;
  3380. return 1;
  3381. }
  3382. static int chunk_aligned_read(mddev_t *mddev, struct bio * raid_bio)
  3383. {
  3384. raid5_conf_t *conf = mddev->private;
  3385. int dd_idx;
  3386. struct bio* align_bi;
  3387. mdk_rdev_t *rdev;
  3388. if (!in_chunk_boundary(mddev, raid_bio)) {
  3389. pr_debug("chunk_aligned_read : non aligned\n");
  3390. return 0;
  3391. }
  3392. /*
  3393. * use bio_clone_mddev to make a copy of the bio
  3394. */
  3395. align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
  3396. if (!align_bi)
  3397. return 0;
  3398. /*
  3399. * set bi_end_io to a new function, and set bi_private to the
  3400. * original bio.
  3401. */
  3402. align_bi->bi_end_io = raid5_align_endio;
  3403. align_bi->bi_private = raid_bio;
  3404. /*
  3405. * compute position
  3406. */
  3407. align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
  3408. 0,
  3409. &dd_idx, NULL);
  3410. rcu_read_lock();
  3411. rdev = rcu_dereference(conf->disks[dd_idx].rdev);
  3412. if (rdev && test_bit(In_sync, &rdev->flags)) {
  3413. atomic_inc(&rdev->nr_pending);
  3414. rcu_read_unlock();
  3415. raid_bio->bi_next = (void*)rdev;
  3416. align_bi->bi_bdev = rdev->bdev;
  3417. align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
  3418. if (!bio_fits_rdev(align_bi)) {
  3419. /* too big in some way */
  3420. bio_put(align_bi);
  3421. rdev_dec_pending(rdev, mddev);
  3422. return 0;
  3423. }
  3424. /* No reshape active, so we can trust rdev->data_offset */
  3425. align_bi->bi_sector += rdev->data_offset;
  3426. spin_lock_irq(&conf->device_lock);
  3427. wait_event_lock_irq(conf->wait_for_stripe,
  3428. conf->quiesce == 0,
  3429. conf->device_lock, /* nothing */);
  3430. atomic_inc(&conf->active_aligned_reads);
  3431. spin_unlock_irq(&conf->device_lock);
  3432. generic_make_request(align_bi);
  3433. return 1;
  3434. } else {
  3435. rcu_read_unlock();
  3436. bio_put(align_bi);
  3437. return 0;
  3438. }
  3439. }
  3440. /* __get_priority_stripe - get the next stripe to process
  3441. *
  3442. * Full stripe writes are allowed to pass preread active stripes up until
  3443. * the bypass_threshold is exceeded. In general the bypass_count
  3444. * increments when the handle_list is handled before the hold_list; however, it
  3445. * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
  3446. * stripe with in flight i/o. The bypass_count will be reset when the
  3447. * head of the hold_list has changed, i.e. the head was promoted to the
  3448. * handle_list.
  3449. */
  3450. static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
  3451. {
  3452. struct stripe_head *sh;
  3453. pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
  3454. __func__,
  3455. list_empty(&conf->handle_list) ? "empty" : "busy",
  3456. list_empty(&conf->hold_list) ? "empty" : "busy",
  3457. atomic_read(&conf->pending_full_writes), conf->bypass_count);
  3458. if (!list_empty(&conf->handle_list)) {
  3459. sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
  3460. if (list_empty(&conf->hold_list))
  3461. conf->bypass_count = 0;
  3462. else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
  3463. if (conf->hold_list.next == conf->last_hold)
  3464. conf->bypass_count++;
  3465. else {
  3466. conf->last_hold = conf->hold_list.next;
  3467. conf->bypass_count -= conf->bypass_threshold;
  3468. if (conf->bypass_count < 0)
  3469. conf->bypass_count = 0;
  3470. }
  3471. }
  3472. } else if (!list_empty(&conf->hold_list) &&
  3473. ((conf->bypass_threshold &&
  3474. conf->bypass_count > conf->bypass_threshold) ||
  3475. atomic_read(&conf->pending_full_writes) == 0)) {
  3476. sh = list_entry(conf->hold_list.next,
  3477. typeof(*sh), lru);
  3478. conf->bypass_count -= conf->bypass_threshold;
  3479. if (conf->bypass_count < 0)
  3480. conf->bypass_count = 0;
  3481. } else
  3482. return NULL;
  3483. list_del_init(&sh->lru);
  3484. atomic_inc(&sh->count);
  3485. BUG_ON(atomic_read(&sh->count) != 1);
  3486. return sh;
  3487. }
  3488. static int make_request(mddev_t *mddev, struct bio * bi)
  3489. {
  3490. raid5_conf_t *conf = mddev->private;
  3491. int dd_idx;
  3492. sector_t new_sector;
  3493. sector_t logical_sector, last_sector;
  3494. struct stripe_head *sh;
  3495. const int rw = bio_data_dir(bi);
  3496. int remaining;
  3497. int plugged;
  3498. if (unlikely(bi->bi_rw & REQ_FLUSH)) {
  3499. md_flush_request(mddev, bi);
  3500. return 0;
  3501. }
  3502. md_write_start(mddev, bi);
  3503. if (rw == READ &&
  3504. mddev->reshape_position == MaxSector &&
  3505. chunk_aligned_read(mddev,bi))
  3506. return 0;
  3507. logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  3508. last_sector = bi->bi_sector + (bi->bi_size>>9);
  3509. bi->bi_next = NULL;
  3510. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  3511. plugged = mddev_check_plugged(mddev);
  3512. for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
  3513. DEFINE_WAIT(w);
  3514. int disks, data_disks;
  3515. int previous;
  3516. retry:
  3517. previous = 0;
  3518. disks = conf->raid_disks;
  3519. prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
  3520. if (unlikely(conf->reshape_progress != MaxSector)) {
  3521. /* spinlock is needed as reshape_progress may be
  3522. * 64bit on a 32bit platform, and so it might be
  3523. * possible to see a half-updated value
  3524. * Of course reshape_progress could change after
  3525. * the lock is dropped, so once we get a reference
  3526. * to the stripe that we think it is, we will have
  3527. * to check again.
  3528. */
  3529. spin_lock_irq(&conf->device_lock);
  3530. if (mddev->delta_disks < 0
  3531. ? logical_sector < conf->reshape_progress
  3532. : logical_sector >= conf->reshape_progress) {
  3533. disks = conf->previous_raid_disks;
  3534. previous = 1;
  3535. } else {
  3536. if (mddev->delta_disks < 0
  3537. ? logical_sector < conf->reshape_safe
  3538. : logical_sector >= conf->reshape_safe) {
  3539. spin_unlock_irq(&conf->device_lock);
  3540. schedule();
  3541. goto retry;
  3542. }
  3543. }
  3544. spin_unlock_irq(&conf->device_lock);
  3545. }
  3546. data_disks = disks - conf->max_degraded;
  3547. new_sector = raid5_compute_sector(conf, logical_sector,
  3548. previous,
  3549. &dd_idx, NULL);
  3550. pr_debug("raid456: make_request, sector %llu logical %llu\n",
  3551. (unsigned long long)new_sector,
  3552. (unsigned long long)logical_sector);
  3553. sh = get_active_stripe(conf, new_sector, previous,
  3554. (bi->bi_rw&RWA_MASK), 0);
  3555. if (sh) {
  3556. if (unlikely(previous)) {
  3557. /* expansion might have moved on while waiting for a
  3558. * stripe, so we must do the range check again.
  3559. * Expansion could still move past after this
  3560. * test, but as we are holding a reference to
  3561. * 'sh', we know that if that happens,
  3562. * STRIPE_EXPANDING will get set and the expansion
  3563. * won't proceed until we finish with the stripe.
  3564. */
  3565. int must_retry = 0;
  3566. spin_lock_irq(&conf->device_lock);
  3567. if (mddev->delta_disks < 0
  3568. ? logical_sector >= conf->reshape_progress
  3569. : logical_sector < conf->reshape_progress)
  3570. /* mismatch, need to try again */
  3571. must_retry = 1;
  3572. spin_unlock_irq(&conf->device_lock);
  3573. if (must_retry) {
  3574. release_stripe(sh);
  3575. schedule();
  3576. goto retry;
  3577. }
  3578. }
  3579. if (bio_data_dir(bi) == WRITE &&
  3580. logical_sector >= mddev->suspend_lo &&
  3581. logical_sector < mddev->suspend_hi) {
  3582. release_stripe(sh);
  3583. /* As the suspend_* range is controlled by
  3584. * userspace, we want an interruptible
  3585. * wait.
  3586. */
  3587. flush_signals(current);
  3588. prepare_to_wait(&conf->wait_for_overlap,
  3589. &w, TASK_INTERRUPTIBLE);
  3590. if (logical_sector >= mddev->suspend_lo &&
  3591. logical_sector < mddev->suspend_hi)
  3592. schedule();
  3593. goto retry;
  3594. }
  3595. if (test_bit(STRIPE_EXPANDING, &sh->state) ||
  3596. !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
  3597. /* Stripe is busy expanding or
  3598. * add failed due to overlap. Flush everything
  3599. * and wait a while
  3600. */
  3601. md_wakeup_thread(mddev->thread);
  3602. release_stripe(sh);
  3603. schedule();
  3604. goto retry;
  3605. }
  3606. finish_wait(&conf->wait_for_overlap, &w);
  3607. set_bit(STRIPE_HANDLE, &sh->state);
  3608. clear_bit(STRIPE_DELAYED, &sh->state);
  3609. if ((bi->bi_rw & REQ_SYNC) &&
  3610. !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  3611. atomic_inc(&conf->preread_active_stripes);
  3612. release_stripe(sh);
  3613. } else {
  3614. /* cannot get stripe for read-ahead, just give-up */
  3615. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  3616. finish_wait(&conf->wait_for_overlap, &w);
  3617. break;
  3618. }
  3619. }
  3620. if (!plugged)
  3621. md_wakeup_thread(mddev->thread);
  3622. spin_lock_irq(&conf->device_lock);
  3623. remaining = raid5_dec_bi_phys_segments(bi);
  3624. spin_unlock_irq(&conf->device_lock);
  3625. if (remaining == 0) {
  3626. if ( rw == WRITE )
  3627. md_write_end(mddev);
  3628. bio_endio(bi, 0);
  3629. }
  3630. return 0;
  3631. }
  3632. static sector_t raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks);
  3633. static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
  3634. {
  3635. /* reshaping is quite different to recovery/resync so it is
  3636. * handled quite separately ... here.
  3637. *
  3638. * On each call to sync_request, we gather one chunk worth of
  3639. * destination stripes and flag them as expanding.
  3640. * Then we find all the source stripes and request reads.
  3641. * As the reads complete, handle_stripe will copy the data
  3642. * into the destination stripe and release that stripe.
  3643. */
  3644. raid5_conf_t *conf = mddev->private;
  3645. struct stripe_head *sh;
  3646. sector_t first_sector, last_sector;
  3647. int raid_disks = conf->previous_raid_disks;
  3648. int data_disks = raid_disks - conf->max_degraded;
  3649. int new_data_disks = conf->raid_disks - conf->max_degraded;
  3650. int i;
  3651. int dd_idx;
  3652. sector_t writepos, readpos, safepos;
  3653. sector_t stripe_addr;
  3654. int reshape_sectors;
  3655. struct list_head stripes;
  3656. if (sector_nr == 0) {
  3657. /* If restarting in the middle, skip the initial sectors */
  3658. if (mddev->delta_disks < 0 &&
  3659. conf->reshape_progress < raid5_size(mddev, 0, 0)) {
  3660. sector_nr = raid5_size(mddev, 0, 0)
  3661. - conf->reshape_progress;
  3662. } else if (mddev->delta_disks >= 0 &&
  3663. conf->reshape_progress > 0)
  3664. sector_nr = conf->reshape_progress;
  3665. sector_div(sector_nr, new_data_disks);
  3666. if (sector_nr) {
  3667. mddev->curr_resync_completed = sector_nr;
  3668. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  3669. *skipped = 1;
  3670. return sector_nr;
  3671. }
  3672. }
  3673. /* We need to process a full chunk at a time.
  3674. * If old and new chunk sizes differ, we need to process the
  3675. * largest of these
  3676. */
  3677. if (mddev->new_chunk_sectors > mddev->chunk_sectors)
  3678. reshape_sectors = mddev->new_chunk_sectors;
  3679. else
  3680. reshape_sectors = mddev->chunk_sectors;
  3681. /* we update the metadata when there is more than 3Meg
  3682. * in the block range (that is rather arbitrary, should
  3683. * probably be time based) or when the data about to be
  3684. * copied would over-write the source of the data at
  3685. * the front of the range.
  3686. * i.e. one new_stripe along from reshape_progress new_maps
  3687. * to after where reshape_safe old_maps to
  3688. */
  3689. writepos = conf->reshape_progress;
  3690. sector_div(writepos, new_data_disks);
  3691. readpos = conf->reshape_progress;
  3692. sector_div(readpos, data_disks);
  3693. safepos = conf->reshape_safe;
  3694. sector_div(safepos, data_disks);
  3695. if (mddev->delta_disks < 0) {
  3696. writepos -= min_t(sector_t, reshape_sectors, writepos);
  3697. readpos += reshape_sectors;
  3698. safepos += reshape_sectors;
  3699. } else {
  3700. writepos += reshape_sectors;
  3701. readpos -= min_t(sector_t, reshape_sectors, readpos);
  3702. safepos -= min_t(sector_t, reshape_sectors, safepos);
  3703. }
  3704. /* 'writepos' is the most advanced device address we might write.
  3705. * 'readpos' is the least advanced device address we might read.
  3706. * 'safepos' is the least address recorded in the metadata as having
  3707. * been reshaped.
  3708. * If 'readpos' is behind 'writepos', then there is no way that we can
  3709. * ensure safety in the face of a crash - that must be done by userspace
  3710. * making a backup of the data. So in that case there is no particular
  3711. * rush to update metadata.
  3712. * Otherwise if 'safepos' is behind 'writepos', then we really need to
  3713. * update the metadata to advance 'safepos' to match 'readpos' so that
  3714. * we can be safe in the event of a crash.
  3715. * So we insist on updating metadata if safepos is behind writepos and
  3716. * readpos is beyond writepos.
  3717. * In any case, update the metadata every 10 seconds.
  3718. * Maybe that number should be configurable, but I'm not sure it is
  3719. * worth it.... maybe it could be a multiple of safemode_delay???
  3720. */
  3721. if ((mddev->delta_disks < 0
  3722. ? (safepos > writepos && readpos < writepos)
  3723. : (safepos < writepos && readpos > writepos)) ||
  3724. time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
  3725. /* Cannot proceed until we've updated the superblock... */
  3726. wait_event(conf->wait_for_overlap,
  3727. atomic_read(&conf->reshape_stripes)==0);
  3728. mddev->reshape_position = conf->reshape_progress;
  3729. mddev->curr_resync_completed = sector_nr;
  3730. conf->reshape_checkpoint = jiffies;
  3731. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3732. md_wakeup_thread(mddev->thread);
  3733. wait_event(mddev->sb_wait, mddev->flags == 0 ||
  3734. kthread_should_stop());
  3735. spin_lock_irq(&conf->device_lock);
  3736. conf->reshape_safe = mddev->reshape_position;
  3737. spin_unlock_irq(&conf->device_lock);
  3738. wake_up(&conf->wait_for_overlap);
  3739. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  3740. }
  3741. if (mddev->delta_disks < 0) {
  3742. BUG_ON(conf->reshape_progress == 0);
  3743. stripe_addr = writepos;
  3744. BUG_ON((mddev->dev_sectors &
  3745. ~((sector_t)reshape_sectors - 1))
  3746. - reshape_sectors - stripe_addr
  3747. != sector_nr);
  3748. } else {
  3749. BUG_ON(writepos != sector_nr + reshape_sectors);
  3750. stripe_addr = sector_nr;
  3751. }
  3752. INIT_LIST_HEAD(&stripes);
  3753. for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
  3754. int j;
  3755. int skipped_disk = 0;
  3756. sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
  3757. set_bit(STRIPE_EXPANDING, &sh->state);
  3758. atomic_inc(&conf->reshape_stripes);
  3759. /* If any of this stripe is beyond the end of the old
  3760. * array, then we need to zero those blocks
  3761. */
  3762. for (j=sh->disks; j--;) {
  3763. sector_t s;
  3764. if (j == sh->pd_idx)
  3765. continue;
  3766. if (conf->level == 6 &&
  3767. j == sh->qd_idx)
  3768. continue;
  3769. s = compute_blocknr(sh, j, 0);
  3770. if (s < raid5_size(mddev, 0, 0)) {
  3771. skipped_disk = 1;
  3772. continue;
  3773. }
  3774. memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
  3775. set_bit(R5_Expanded, &sh->dev[j].flags);
  3776. set_bit(R5_UPTODATE, &sh->dev[j].flags);
  3777. }
  3778. if (!skipped_disk) {
  3779. set_bit(STRIPE_EXPAND_READY, &sh->state);
  3780. set_bit(STRIPE_HANDLE, &sh->state);
  3781. }
  3782. list_add(&sh->lru, &stripes);
  3783. }
  3784. spin_lock_irq(&conf->device_lock);
  3785. if (mddev->delta_disks < 0)
  3786. conf->reshape_progress -= reshape_sectors * new_data_disks;
  3787. else
  3788. conf->reshape_progress += reshape_sectors * new_data_disks;
  3789. spin_unlock_irq(&conf->device_lock);
  3790. /* Ok, those stripe are ready. We can start scheduling
  3791. * reads on the source stripes.
  3792. * The source stripes are determined by mapping the first and last
  3793. * block on the destination stripes.
  3794. */
  3795. first_sector =
  3796. raid5_compute_sector(conf, stripe_addr*(new_data_disks),
  3797. 1, &dd_idx, NULL);
  3798. last_sector =
  3799. raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
  3800. * new_data_disks - 1),
  3801. 1, &dd_idx, NULL);
  3802. if (last_sector >= mddev->dev_sectors)
  3803. last_sector = mddev->dev_sectors - 1;
  3804. while (first_sector <= last_sector) {
  3805. sh = get_active_stripe(conf, first_sector, 1, 0, 1);
  3806. set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  3807. set_bit(STRIPE_HANDLE, &sh->state);
  3808. release_stripe(sh);
  3809. first_sector += STRIPE_SECTORS;
  3810. }
  3811. /* Now that the sources are clearly marked, we can release
  3812. * the destination stripes
  3813. */
  3814. while (!list_empty(&stripes)) {
  3815. sh = list_entry(stripes.next, struct stripe_head, lru);
  3816. list_del_init(&sh->lru);
  3817. release_stripe(sh);
  3818. }
  3819. /* If this takes us to the resync_max point where we have to pause,
  3820. * then we need to write out the superblock.
  3821. */
  3822. sector_nr += reshape_sectors;
  3823. if ((sector_nr - mddev->curr_resync_completed) * 2
  3824. >= mddev->resync_max - mddev->curr_resync_completed) {
  3825. /* Cannot proceed until we've updated the superblock... */
  3826. wait_event(conf->wait_for_overlap,
  3827. atomic_read(&conf->reshape_stripes) == 0);
  3828. mddev->reshape_position = conf->reshape_progress;
  3829. mddev->curr_resync_completed = sector_nr;
  3830. conf->reshape_checkpoint = jiffies;
  3831. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3832. md_wakeup_thread(mddev->thread);
  3833. wait_event(mddev->sb_wait,
  3834. !test_bit(MD_CHANGE_DEVS, &mddev->flags)
  3835. || kthread_should_stop());
  3836. spin_lock_irq(&conf->device_lock);
  3837. conf->reshape_safe = mddev->reshape_position;
  3838. spin_unlock_irq(&conf->device_lock);
  3839. wake_up(&conf->wait_for_overlap);
  3840. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  3841. }
  3842. return reshape_sectors;
  3843. }
  3844. /* FIXME go_faster isn't used */
  3845. static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
  3846. {
  3847. raid5_conf_t *conf = mddev->private;
  3848. struct stripe_head *sh;
  3849. sector_t max_sector = mddev->dev_sectors;
  3850. sector_t sync_blocks;
  3851. int still_degraded = 0;
  3852. int i;
  3853. if (sector_nr >= max_sector) {
  3854. /* just being told to finish up .. nothing much to do */
  3855. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  3856. end_reshape(conf);
  3857. return 0;
  3858. }
  3859. if (mddev->curr_resync < max_sector) /* aborted */
  3860. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  3861. &sync_blocks, 1);
  3862. else /* completed sync */
  3863. conf->fullsync = 0;
  3864. bitmap_close_sync(mddev->bitmap);
  3865. return 0;
  3866. }
  3867. /* Allow raid5_quiesce to complete */
  3868. wait_event(conf->wait_for_overlap, conf->quiesce != 2);
  3869. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  3870. return reshape_request(mddev, sector_nr, skipped);
  3871. /* No need to check resync_max as we never do more than one
  3872. * stripe, and as resync_max will always be on a chunk boundary,
  3873. * if the check in md_do_sync didn't fire, there is no chance
  3874. * of overstepping resync_max here
  3875. */
  3876. /* if there is too many failed drives and we are trying
  3877. * to resync, then assert that we are finished, because there is
  3878. * nothing we can do.
  3879. */
  3880. if (mddev->degraded >= conf->max_degraded &&
  3881. test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  3882. sector_t rv = mddev->dev_sectors - sector_nr;
  3883. *skipped = 1;
  3884. return rv;
  3885. }
  3886. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  3887. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  3888. !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
  3889. /* we can skip this block, and probably more */
  3890. sync_blocks /= STRIPE_SECTORS;
  3891. *skipped = 1;
  3892. return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
  3893. }
  3894. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  3895. sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
  3896. if (sh == NULL) {
  3897. sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
  3898. /* make sure we don't swamp the stripe cache if someone else
  3899. * is trying to get access
  3900. */
  3901. schedule_timeout_uninterruptible(1);
  3902. }
  3903. /* Need to check if array will still be degraded after recovery/resync
  3904. * We don't need to check the 'failed' flag as when that gets set,
  3905. * recovery aborts.
  3906. */
  3907. for (i = 0; i < conf->raid_disks; i++)
  3908. if (conf->disks[i].rdev == NULL)
  3909. still_degraded = 1;
  3910. bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
  3911. spin_lock(&sh->lock);
  3912. set_bit(STRIPE_SYNCING, &sh->state);
  3913. clear_bit(STRIPE_INSYNC, &sh->state);
  3914. spin_unlock(&sh->lock);
  3915. handle_stripe(sh);
  3916. release_stripe(sh);
  3917. return STRIPE_SECTORS;
  3918. }
  3919. static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
  3920. {
  3921. /* We may not be able to submit a whole bio at once as there
  3922. * may not be enough stripe_heads available.
  3923. * We cannot pre-allocate enough stripe_heads as we may need
  3924. * more than exist in the cache (if we allow ever large chunks).
  3925. * So we do one stripe head at a time and record in
  3926. * ->bi_hw_segments how many have been done.
  3927. *
  3928. * We *know* that this entire raid_bio is in one chunk, so
  3929. * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
  3930. */
  3931. struct stripe_head *sh;
  3932. int dd_idx;
  3933. sector_t sector, logical_sector, last_sector;
  3934. int scnt = 0;
  3935. int remaining;
  3936. int handled = 0;
  3937. logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  3938. sector = raid5_compute_sector(conf, logical_sector,
  3939. 0, &dd_idx, NULL);
  3940. last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
  3941. for (; logical_sector < last_sector;
  3942. logical_sector += STRIPE_SECTORS,
  3943. sector += STRIPE_SECTORS,
  3944. scnt++) {
  3945. if (scnt < raid5_bi_hw_segments(raid_bio))
  3946. /* already done this stripe */
  3947. continue;
  3948. sh = get_active_stripe(conf, sector, 0, 1, 0);
  3949. if (!sh) {
  3950. /* failed to get a stripe - must wait */
  3951. raid5_set_bi_hw_segments(raid_bio, scnt);
  3952. conf->retry_read_aligned = raid_bio;
  3953. return handled;
  3954. }
  3955. set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
  3956. if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
  3957. release_stripe(sh);
  3958. raid5_set_bi_hw_segments(raid_bio, scnt);
  3959. conf->retry_read_aligned = raid_bio;
  3960. return handled;
  3961. }
  3962. handle_stripe(sh);
  3963. release_stripe(sh);
  3964. handled++;
  3965. }
  3966. spin_lock_irq(&conf->device_lock);
  3967. remaining = raid5_dec_bi_phys_segments(raid_bio);
  3968. spin_unlock_irq(&conf->device_lock);
  3969. if (remaining == 0)
  3970. bio_endio(raid_bio, 0);
  3971. if (atomic_dec_and_test(&conf->active_aligned_reads))
  3972. wake_up(&conf->wait_for_stripe);
  3973. return handled;
  3974. }
  3975. /*
  3976. * This is our raid5 kernel thread.
  3977. *
  3978. * We scan the hash table for stripes which can be handled now.
  3979. * During the scan, completed stripes are saved for us by the interrupt
  3980. * handler, so that they will not have to wait for our next wakeup.
  3981. */
  3982. static void raid5d(mddev_t *mddev)
  3983. {
  3984. struct stripe_head *sh;
  3985. raid5_conf_t *conf = mddev->private;
  3986. int handled;
  3987. struct blk_plug plug;
  3988. pr_debug("+++ raid5d active\n");
  3989. md_check_recovery(mddev);
  3990. blk_start_plug(&plug);
  3991. handled = 0;
  3992. spin_lock_irq(&conf->device_lock);
  3993. while (1) {
  3994. struct bio *bio;
  3995. if (atomic_read(&mddev->plug_cnt) == 0 &&
  3996. !list_empty(&conf->bitmap_list)) {
  3997. /* Now is a good time to flush some bitmap updates */
  3998. conf->seq_flush++;
  3999. spin_unlock_irq(&conf->device_lock);
  4000. bitmap_unplug(mddev->bitmap);
  4001. spin_lock_irq(&conf->device_lock);
  4002. conf->seq_write = conf->seq_flush;
  4003. activate_bit_delay(conf);
  4004. }
  4005. if (atomic_read(&mddev->plug_cnt) == 0)
  4006. raid5_activate_delayed(conf);
  4007. while ((bio = remove_bio_from_retry(conf))) {
  4008. int ok;
  4009. spin_unlock_irq(&conf->device_lock);
  4010. ok = retry_aligned_read(conf, bio);
  4011. spin_lock_irq(&conf->device_lock);
  4012. if (!ok)
  4013. break;
  4014. handled++;
  4015. }
  4016. sh = __get_priority_stripe(conf);
  4017. if (!sh)
  4018. break;
  4019. spin_unlock_irq(&conf->device_lock);
  4020. handled++;
  4021. handle_stripe(sh);
  4022. release_stripe(sh);
  4023. cond_resched();
  4024. spin_lock_irq(&conf->device_lock);
  4025. }
  4026. pr_debug("%d stripes handled\n", handled);
  4027. spin_unlock_irq(&conf->device_lock);
  4028. async_tx_issue_pending_all();
  4029. blk_finish_plug(&plug);
  4030. pr_debug("--- raid5d inactive\n");
  4031. }
  4032. static ssize_t
  4033. raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
  4034. {
  4035. raid5_conf_t *conf = mddev->private;
  4036. if (conf)
  4037. return sprintf(page, "%d\n", conf->max_nr_stripes);
  4038. else
  4039. return 0;
  4040. }
  4041. int
  4042. raid5_set_cache_size(mddev_t *mddev, int size)
  4043. {
  4044. raid5_conf_t *conf = mddev->private;
  4045. int err;
  4046. if (size <= 16 || size > 32768)
  4047. return -EINVAL;
  4048. while (size < conf->max_nr_stripes) {
  4049. if (drop_one_stripe(conf))
  4050. conf->max_nr_stripes--;
  4051. else
  4052. break;
  4053. }
  4054. err = md_allow_write(mddev);
  4055. if (err)
  4056. return err;
  4057. while (size > conf->max_nr_stripes) {
  4058. if (grow_one_stripe(conf))
  4059. conf->max_nr_stripes++;
  4060. else break;
  4061. }
  4062. return 0;
  4063. }
  4064. EXPORT_SYMBOL(raid5_set_cache_size);
  4065. static ssize_t
  4066. raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
  4067. {
  4068. raid5_conf_t *conf = mddev->private;
  4069. unsigned long new;
  4070. int err;
  4071. if (len >= PAGE_SIZE)
  4072. return -EINVAL;
  4073. if (!conf)
  4074. return -ENODEV;
  4075. if (strict_strtoul(page, 10, &new))
  4076. return -EINVAL;
  4077. err = raid5_set_cache_size(mddev, new);
  4078. if (err)
  4079. return err;
  4080. return len;
  4081. }
  4082. static struct md_sysfs_entry
  4083. raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
  4084. raid5_show_stripe_cache_size,
  4085. raid5_store_stripe_cache_size);
  4086. static ssize_t
  4087. raid5_show_preread_threshold(mddev_t *mddev, char *page)
  4088. {
  4089. raid5_conf_t *conf = mddev->private;
  4090. if (conf)
  4091. return sprintf(page, "%d\n", conf->bypass_threshold);
  4092. else
  4093. return 0;
  4094. }
  4095. static ssize_t
  4096. raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
  4097. {
  4098. raid5_conf_t *conf = mddev->private;
  4099. unsigned long new;
  4100. if (len >= PAGE_SIZE)
  4101. return -EINVAL;
  4102. if (!conf)
  4103. return -ENODEV;
  4104. if (strict_strtoul(page, 10, &new))
  4105. return -EINVAL;
  4106. if (new > conf->max_nr_stripes)
  4107. return -EINVAL;
  4108. conf->bypass_threshold = new;
  4109. return len;
  4110. }
  4111. static struct md_sysfs_entry
  4112. raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
  4113. S_IRUGO | S_IWUSR,
  4114. raid5_show_preread_threshold,
  4115. raid5_store_preread_threshold);
  4116. static ssize_t
  4117. stripe_cache_active_show(mddev_t *mddev, char *page)
  4118. {
  4119. raid5_conf_t *conf = mddev->private;
  4120. if (conf)
  4121. return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
  4122. else
  4123. return 0;
  4124. }
  4125. static struct md_sysfs_entry
  4126. raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
  4127. static struct attribute *raid5_attrs[] = {
  4128. &raid5_stripecache_size.attr,
  4129. &raid5_stripecache_active.attr,
  4130. &raid5_preread_bypass_threshold.attr,
  4131. NULL,
  4132. };
  4133. static struct attribute_group raid5_attrs_group = {
  4134. .name = NULL,
  4135. .attrs = raid5_attrs,
  4136. };
  4137. static sector_t
  4138. raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks)
  4139. {
  4140. raid5_conf_t *conf = mddev->private;
  4141. if (!sectors)
  4142. sectors = mddev->dev_sectors;
  4143. if (!raid_disks)
  4144. /* size is defined by the smallest of previous and new size */
  4145. raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
  4146. sectors &= ~((sector_t)mddev->chunk_sectors - 1);
  4147. sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
  4148. return sectors * (raid_disks - conf->max_degraded);
  4149. }
  4150. static void raid5_free_percpu(raid5_conf_t *conf)
  4151. {
  4152. struct raid5_percpu *percpu;
  4153. unsigned long cpu;
  4154. if (!conf->percpu)
  4155. return;
  4156. get_online_cpus();
  4157. for_each_possible_cpu(cpu) {
  4158. percpu = per_cpu_ptr(conf->percpu, cpu);
  4159. safe_put_page(percpu->spare_page);
  4160. kfree(percpu->scribble);
  4161. }
  4162. #ifdef CONFIG_HOTPLUG_CPU
  4163. unregister_cpu_notifier(&conf->cpu_notify);
  4164. #endif
  4165. put_online_cpus();
  4166. free_percpu(conf->percpu);
  4167. }
  4168. static void free_conf(raid5_conf_t *conf)
  4169. {
  4170. shrink_stripes(conf);
  4171. raid5_free_percpu(conf);
  4172. kfree(conf->disks);
  4173. kfree(conf->stripe_hashtbl);
  4174. kfree(conf);
  4175. }
  4176. #ifdef CONFIG_HOTPLUG_CPU
  4177. static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
  4178. void *hcpu)
  4179. {
  4180. raid5_conf_t *conf = container_of(nfb, raid5_conf_t, cpu_notify);
  4181. long cpu = (long)hcpu;
  4182. struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
  4183. switch (action) {
  4184. case CPU_UP_PREPARE:
  4185. case CPU_UP_PREPARE_FROZEN:
  4186. if (conf->level == 6 && !percpu->spare_page)
  4187. percpu->spare_page = alloc_page(GFP_KERNEL);
  4188. if (!percpu->scribble)
  4189. percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
  4190. if (!percpu->scribble ||
  4191. (conf->level == 6 && !percpu->spare_page)) {
  4192. safe_put_page(percpu->spare_page);
  4193. kfree(percpu->scribble);
  4194. pr_err("%s: failed memory allocation for cpu%ld\n",
  4195. __func__, cpu);
  4196. return notifier_from_errno(-ENOMEM);
  4197. }
  4198. break;
  4199. case CPU_DEAD:
  4200. case CPU_DEAD_FROZEN:
  4201. safe_put_page(percpu->spare_page);
  4202. kfree(percpu->scribble);
  4203. percpu->spare_page = NULL;
  4204. percpu->scribble = NULL;
  4205. break;
  4206. default:
  4207. break;
  4208. }
  4209. return NOTIFY_OK;
  4210. }
  4211. #endif
  4212. static int raid5_alloc_percpu(raid5_conf_t *conf)
  4213. {
  4214. unsigned long cpu;
  4215. struct page *spare_page;
  4216. struct raid5_percpu __percpu *allcpus;
  4217. void *scribble;
  4218. int err;
  4219. allcpus = alloc_percpu(struct raid5_percpu);
  4220. if (!allcpus)
  4221. return -ENOMEM;
  4222. conf->percpu = allcpus;
  4223. get_online_cpus();
  4224. err = 0;
  4225. for_each_present_cpu(cpu) {
  4226. if (conf->level == 6) {
  4227. spare_page = alloc_page(GFP_KERNEL);
  4228. if (!spare_page) {
  4229. err = -ENOMEM;
  4230. break;
  4231. }
  4232. per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
  4233. }
  4234. scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
  4235. if (!scribble) {
  4236. err = -ENOMEM;
  4237. break;
  4238. }
  4239. per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
  4240. }
  4241. #ifdef CONFIG_HOTPLUG_CPU
  4242. conf->cpu_notify.notifier_call = raid456_cpu_notify;
  4243. conf->cpu_notify.priority = 0;
  4244. if (err == 0)
  4245. err = register_cpu_notifier(&conf->cpu_notify);
  4246. #endif
  4247. put_online_cpus();
  4248. return err;
  4249. }
  4250. static raid5_conf_t *setup_conf(mddev_t *mddev)
  4251. {
  4252. raid5_conf_t *conf;
  4253. int raid_disk, memory, max_disks;
  4254. mdk_rdev_t *rdev;
  4255. struct disk_info *disk;
  4256. if (mddev->new_level != 5
  4257. && mddev->new_level != 4
  4258. && mddev->new_level != 6) {
  4259. printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
  4260. mdname(mddev), mddev->new_level);
  4261. return ERR_PTR(-EIO);
  4262. }
  4263. if ((mddev->new_level == 5
  4264. && !algorithm_valid_raid5(mddev->new_layout)) ||
  4265. (mddev->new_level == 6
  4266. && !algorithm_valid_raid6(mddev->new_layout))) {
  4267. printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
  4268. mdname(mddev), mddev->new_layout);
  4269. return ERR_PTR(-EIO);
  4270. }
  4271. if (mddev->new_level == 6 && mddev->raid_disks < 4) {
  4272. printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
  4273. mdname(mddev), mddev->raid_disks);
  4274. return ERR_PTR(-EINVAL);
  4275. }
  4276. if (!mddev->new_chunk_sectors ||
  4277. (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
  4278. !is_power_of_2(mddev->new_chunk_sectors)) {
  4279. printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
  4280. mdname(mddev), mddev->new_chunk_sectors << 9);
  4281. return ERR_PTR(-EINVAL);
  4282. }
  4283. conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL);
  4284. if (conf == NULL)
  4285. goto abort;
  4286. spin_lock_init(&conf->device_lock);
  4287. init_waitqueue_head(&conf->wait_for_stripe);
  4288. init_waitqueue_head(&conf->wait_for_overlap);
  4289. INIT_LIST_HEAD(&conf->handle_list);
  4290. INIT_LIST_HEAD(&conf->hold_list);
  4291. INIT_LIST_HEAD(&conf->delayed_list);
  4292. INIT_LIST_HEAD(&conf->bitmap_list);
  4293. INIT_LIST_HEAD(&conf->inactive_list);
  4294. atomic_set(&conf->active_stripes, 0);
  4295. atomic_set(&conf->preread_active_stripes, 0);
  4296. atomic_set(&conf->active_aligned_reads, 0);
  4297. conf->bypass_threshold = BYPASS_THRESHOLD;
  4298. conf->raid_disks = mddev->raid_disks;
  4299. if (mddev->reshape_position == MaxSector)
  4300. conf->previous_raid_disks = mddev->raid_disks;
  4301. else
  4302. conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
  4303. max_disks = max(conf->raid_disks, conf->previous_raid_disks);
  4304. conf->scribble_len = scribble_len(max_disks);
  4305. conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
  4306. GFP_KERNEL);
  4307. if (!conf->disks)
  4308. goto abort;
  4309. conf->mddev = mddev;
  4310. if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
  4311. goto abort;
  4312. conf->level = mddev->new_level;
  4313. if (raid5_alloc_percpu(conf) != 0)
  4314. goto abort;
  4315. pr_debug("raid456: run(%s) called.\n", mdname(mddev));
  4316. list_for_each_entry(rdev, &mddev->disks, same_set) {
  4317. raid_disk = rdev->raid_disk;
  4318. if (raid_disk >= max_disks
  4319. || raid_disk < 0)
  4320. continue;
  4321. disk = conf->disks + raid_disk;
  4322. disk->rdev = rdev;
  4323. if (test_bit(In_sync, &rdev->flags)) {
  4324. char b[BDEVNAME_SIZE];
  4325. printk(KERN_INFO "md/raid:%s: device %s operational as raid"
  4326. " disk %d\n",
  4327. mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
  4328. } else if (rdev->saved_raid_disk != raid_disk)
  4329. /* Cannot rely on bitmap to complete recovery */
  4330. conf->fullsync = 1;
  4331. }
  4332. conf->chunk_sectors = mddev->new_chunk_sectors;
  4333. conf->level = mddev->new_level;
  4334. if (conf->level == 6)
  4335. conf->max_degraded = 2;
  4336. else
  4337. conf->max_degraded = 1;
  4338. conf->algorithm = mddev->new_layout;
  4339. conf->max_nr_stripes = NR_STRIPES;
  4340. conf->reshape_progress = mddev->reshape_position;
  4341. if (conf->reshape_progress != MaxSector) {
  4342. conf->prev_chunk_sectors = mddev->chunk_sectors;
  4343. conf->prev_algo = mddev->layout;
  4344. }
  4345. memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
  4346. max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
  4347. if (grow_stripes(conf, conf->max_nr_stripes)) {
  4348. printk(KERN_ERR
  4349. "md/raid:%s: couldn't allocate %dkB for buffers\n",
  4350. mdname(mddev), memory);
  4351. goto abort;
  4352. } else
  4353. printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
  4354. mdname(mddev), memory);
  4355. conf->thread = md_register_thread(raid5d, mddev, NULL);
  4356. if (!conf->thread) {
  4357. printk(KERN_ERR
  4358. "md/raid:%s: couldn't allocate thread.\n",
  4359. mdname(mddev));
  4360. goto abort;
  4361. }
  4362. return conf;
  4363. abort:
  4364. if (conf) {
  4365. free_conf(conf);
  4366. return ERR_PTR(-EIO);
  4367. } else
  4368. return ERR_PTR(-ENOMEM);
  4369. }
  4370. static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
  4371. {
  4372. switch (algo) {
  4373. case ALGORITHM_PARITY_0:
  4374. if (raid_disk < max_degraded)
  4375. return 1;
  4376. break;
  4377. case ALGORITHM_PARITY_N:
  4378. if (raid_disk >= raid_disks - max_degraded)
  4379. return 1;
  4380. break;
  4381. case ALGORITHM_PARITY_0_6:
  4382. if (raid_disk == 0 ||
  4383. raid_disk == raid_disks - 1)
  4384. return 1;
  4385. break;
  4386. case ALGORITHM_LEFT_ASYMMETRIC_6:
  4387. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  4388. case ALGORITHM_LEFT_SYMMETRIC_6:
  4389. case ALGORITHM_RIGHT_SYMMETRIC_6:
  4390. if (raid_disk == raid_disks - 1)
  4391. return 1;
  4392. }
  4393. return 0;
  4394. }
  4395. static int run(mddev_t *mddev)
  4396. {
  4397. raid5_conf_t *conf;
  4398. int working_disks = 0;
  4399. int dirty_parity_disks = 0;
  4400. mdk_rdev_t *rdev;
  4401. sector_t reshape_offset = 0;
  4402. if (mddev->recovery_cp != MaxSector)
  4403. printk(KERN_NOTICE "md/raid:%s: not clean"
  4404. " -- starting background reconstruction\n",
  4405. mdname(mddev));
  4406. if (mddev->reshape_position != MaxSector) {
  4407. /* Check that we can continue the reshape.
  4408. * Currently only disks can change, it must
  4409. * increase, and we must be past the point where
  4410. * a stripe over-writes itself
  4411. */
  4412. sector_t here_new, here_old;
  4413. int old_disks;
  4414. int max_degraded = (mddev->level == 6 ? 2 : 1);
  4415. if (mddev->new_level != mddev->level) {
  4416. printk(KERN_ERR "md/raid:%s: unsupported reshape "
  4417. "required - aborting.\n",
  4418. mdname(mddev));
  4419. return -EINVAL;
  4420. }
  4421. old_disks = mddev->raid_disks - mddev->delta_disks;
  4422. /* reshape_position must be on a new-stripe boundary, and one
  4423. * further up in new geometry must map after here in old
  4424. * geometry.
  4425. */
  4426. here_new = mddev->reshape_position;
  4427. if (sector_div(here_new, mddev->new_chunk_sectors *
  4428. (mddev->raid_disks - max_degraded))) {
  4429. printk(KERN_ERR "md/raid:%s: reshape_position not "
  4430. "on a stripe boundary\n", mdname(mddev));
  4431. return -EINVAL;
  4432. }
  4433. reshape_offset = here_new * mddev->new_chunk_sectors;
  4434. /* here_new is the stripe we will write to */
  4435. here_old = mddev->reshape_position;
  4436. sector_div(here_old, mddev->chunk_sectors *
  4437. (old_disks-max_degraded));
  4438. /* here_old is the first stripe that we might need to read
  4439. * from */
  4440. if (mddev->delta_disks == 0) {
  4441. /* We cannot be sure it is safe to start an in-place
  4442. * reshape. It is only safe if user-space if monitoring
  4443. * and taking constant backups.
  4444. * mdadm always starts a situation like this in
  4445. * readonly mode so it can take control before
  4446. * allowing any writes. So just check for that.
  4447. */
  4448. if ((here_new * mddev->new_chunk_sectors !=
  4449. here_old * mddev->chunk_sectors) ||
  4450. mddev->ro == 0) {
  4451. printk(KERN_ERR "md/raid:%s: in-place reshape must be started"
  4452. " in read-only mode - aborting\n",
  4453. mdname(mddev));
  4454. return -EINVAL;
  4455. }
  4456. } else if (mddev->delta_disks < 0
  4457. ? (here_new * mddev->new_chunk_sectors <=
  4458. here_old * mddev->chunk_sectors)
  4459. : (here_new * mddev->new_chunk_sectors >=
  4460. here_old * mddev->chunk_sectors)) {
  4461. /* Reading from the same stripe as writing to - bad */
  4462. printk(KERN_ERR "md/raid:%s: reshape_position too early for "
  4463. "auto-recovery - aborting.\n",
  4464. mdname(mddev));
  4465. return -EINVAL;
  4466. }
  4467. printk(KERN_INFO "md/raid:%s: reshape will continue\n",
  4468. mdname(mddev));
  4469. /* OK, we should be able to continue; */
  4470. } else {
  4471. BUG_ON(mddev->level != mddev->new_level);
  4472. BUG_ON(mddev->layout != mddev->new_layout);
  4473. BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
  4474. BUG_ON(mddev->delta_disks != 0);
  4475. }
  4476. if (mddev->private == NULL)
  4477. conf = setup_conf(mddev);
  4478. else
  4479. conf = mddev->private;
  4480. if (IS_ERR(conf))
  4481. return PTR_ERR(conf);
  4482. mddev->thread = conf->thread;
  4483. conf->thread = NULL;
  4484. mddev->private = conf;
  4485. /*
  4486. * 0 for a fully functional array, 1 or 2 for a degraded array.
  4487. */
  4488. list_for_each_entry(rdev, &mddev->disks, same_set) {
  4489. if (rdev->raid_disk < 0)
  4490. continue;
  4491. if (test_bit(In_sync, &rdev->flags)) {
  4492. working_disks++;
  4493. continue;
  4494. }
  4495. /* This disc is not fully in-sync. However if it
  4496. * just stored parity (beyond the recovery_offset),
  4497. * when we don't need to be concerned about the
  4498. * array being dirty.
  4499. * When reshape goes 'backwards', we never have
  4500. * partially completed devices, so we only need
  4501. * to worry about reshape going forwards.
  4502. */
  4503. /* Hack because v0.91 doesn't store recovery_offset properly. */
  4504. if (mddev->major_version == 0 &&
  4505. mddev->minor_version > 90)
  4506. rdev->recovery_offset = reshape_offset;
  4507. if (rdev->recovery_offset < reshape_offset) {
  4508. /* We need to check old and new layout */
  4509. if (!only_parity(rdev->raid_disk,
  4510. conf->algorithm,
  4511. conf->raid_disks,
  4512. conf->max_degraded))
  4513. continue;
  4514. }
  4515. if (!only_parity(rdev->raid_disk,
  4516. conf->prev_algo,
  4517. conf->previous_raid_disks,
  4518. conf->max_degraded))
  4519. continue;
  4520. dirty_parity_disks++;
  4521. }
  4522. mddev->degraded = (max(conf->raid_disks, conf->previous_raid_disks)
  4523. - working_disks);
  4524. if (has_failed(conf)) {
  4525. printk(KERN_ERR "md/raid:%s: not enough operational devices"
  4526. " (%d/%d failed)\n",
  4527. mdname(mddev), mddev->degraded, conf->raid_disks);
  4528. goto abort;
  4529. }
  4530. /* device size must be a multiple of chunk size */
  4531. mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
  4532. mddev->resync_max_sectors = mddev->dev_sectors;
  4533. if (mddev->degraded > dirty_parity_disks &&
  4534. mddev->recovery_cp != MaxSector) {
  4535. if (mddev->ok_start_degraded)
  4536. printk(KERN_WARNING
  4537. "md/raid:%s: starting dirty degraded array"
  4538. " - data corruption possible.\n",
  4539. mdname(mddev));
  4540. else {
  4541. printk(KERN_ERR
  4542. "md/raid:%s: cannot start dirty degraded array.\n",
  4543. mdname(mddev));
  4544. goto abort;
  4545. }
  4546. }
  4547. if (mddev->degraded == 0)
  4548. printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
  4549. " devices, algorithm %d\n", mdname(mddev), conf->level,
  4550. mddev->raid_disks-mddev->degraded, mddev->raid_disks,
  4551. mddev->new_layout);
  4552. else
  4553. printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
  4554. " out of %d devices, algorithm %d\n",
  4555. mdname(mddev), conf->level,
  4556. mddev->raid_disks - mddev->degraded,
  4557. mddev->raid_disks, mddev->new_layout);
  4558. print_raid5_conf(conf);
  4559. if (conf->reshape_progress != MaxSector) {
  4560. conf->reshape_safe = conf->reshape_progress;
  4561. atomic_set(&conf->reshape_stripes, 0);
  4562. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  4563. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  4564. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  4565. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  4566. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  4567. "reshape");
  4568. }
  4569. /* Ok, everything is just fine now */
  4570. if (mddev->to_remove == &raid5_attrs_group)
  4571. mddev->to_remove = NULL;
  4572. else if (mddev->kobj.sd &&
  4573. sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
  4574. printk(KERN_WARNING
  4575. "raid5: failed to create sysfs attributes for %s\n",
  4576. mdname(mddev));
  4577. md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
  4578. if (mddev->queue) {
  4579. int chunk_size;
  4580. /* read-ahead size must cover two whole stripes, which
  4581. * is 2 * (datadisks) * chunksize where 'n' is the
  4582. * number of raid devices
  4583. */
  4584. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  4585. int stripe = data_disks *
  4586. ((mddev->chunk_sectors << 9) / PAGE_SIZE);
  4587. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  4588. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  4589. blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
  4590. mddev->queue->backing_dev_info.congested_data = mddev;
  4591. mddev->queue->backing_dev_info.congested_fn = raid5_congested;
  4592. chunk_size = mddev->chunk_sectors << 9;
  4593. blk_queue_io_min(mddev->queue, chunk_size);
  4594. blk_queue_io_opt(mddev->queue, chunk_size *
  4595. (conf->raid_disks - conf->max_degraded));
  4596. list_for_each_entry(rdev, &mddev->disks, same_set)
  4597. disk_stack_limits(mddev->gendisk, rdev->bdev,
  4598. rdev->data_offset << 9);
  4599. }
  4600. return 0;
  4601. abort:
  4602. md_unregister_thread(&mddev->thread);
  4603. if (conf) {
  4604. print_raid5_conf(conf);
  4605. free_conf(conf);
  4606. }
  4607. mddev->private = NULL;
  4608. printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
  4609. return -EIO;
  4610. }
  4611. static int stop(mddev_t *mddev)
  4612. {
  4613. raid5_conf_t *conf = mddev->private;
  4614. md_unregister_thread(&mddev->thread);
  4615. if (mddev->queue)
  4616. mddev->queue->backing_dev_info.congested_fn = NULL;
  4617. free_conf(conf);
  4618. mddev->private = NULL;
  4619. mddev->to_remove = &raid5_attrs_group;
  4620. return 0;
  4621. }
  4622. #ifdef DEBUG
  4623. static void print_sh(struct seq_file *seq, struct stripe_head *sh)
  4624. {
  4625. int i;
  4626. seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
  4627. (unsigned long long)sh->sector, sh->pd_idx, sh->state);
  4628. seq_printf(seq, "sh %llu, count %d.\n",
  4629. (unsigned long long)sh->sector, atomic_read(&sh->count));
  4630. seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
  4631. for (i = 0; i < sh->disks; i++) {
  4632. seq_printf(seq, "(cache%d: %p %ld) ",
  4633. i, sh->dev[i].page, sh->dev[i].flags);
  4634. }
  4635. seq_printf(seq, "\n");
  4636. }
  4637. static void printall(struct seq_file *seq, raid5_conf_t *conf)
  4638. {
  4639. struct stripe_head *sh;
  4640. struct hlist_node *hn;
  4641. int i;
  4642. spin_lock_irq(&conf->device_lock);
  4643. for (i = 0; i < NR_HASH; i++) {
  4644. hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
  4645. if (sh->raid_conf != conf)
  4646. continue;
  4647. print_sh(seq, sh);
  4648. }
  4649. }
  4650. spin_unlock_irq(&conf->device_lock);
  4651. }
  4652. #endif
  4653. static void status(struct seq_file *seq, mddev_t *mddev)
  4654. {
  4655. raid5_conf_t *conf = mddev->private;
  4656. int i;
  4657. seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
  4658. mddev->chunk_sectors / 2, mddev->layout);
  4659. seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
  4660. for (i = 0; i < conf->raid_disks; i++)
  4661. seq_printf (seq, "%s",
  4662. conf->disks[i].rdev &&
  4663. test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
  4664. seq_printf (seq, "]");
  4665. #ifdef DEBUG
  4666. seq_printf (seq, "\n");
  4667. printall(seq, conf);
  4668. #endif
  4669. }
  4670. static void print_raid5_conf (raid5_conf_t *conf)
  4671. {
  4672. int i;
  4673. struct disk_info *tmp;
  4674. printk(KERN_DEBUG "RAID conf printout:\n");
  4675. if (!conf) {
  4676. printk("(conf==NULL)\n");
  4677. return;
  4678. }
  4679. printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
  4680. conf->raid_disks,
  4681. conf->raid_disks - conf->mddev->degraded);
  4682. for (i = 0; i < conf->raid_disks; i++) {
  4683. char b[BDEVNAME_SIZE];
  4684. tmp = conf->disks + i;
  4685. if (tmp->rdev)
  4686. printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
  4687. i, !test_bit(Faulty, &tmp->rdev->flags),
  4688. bdevname(tmp->rdev->bdev, b));
  4689. }
  4690. }
  4691. static int raid5_spare_active(mddev_t *mddev)
  4692. {
  4693. int i;
  4694. raid5_conf_t *conf = mddev->private;
  4695. struct disk_info *tmp;
  4696. int count = 0;
  4697. unsigned long flags;
  4698. for (i = 0; i < conf->raid_disks; i++) {
  4699. tmp = conf->disks + i;
  4700. if (tmp->rdev
  4701. && tmp->rdev->recovery_offset == MaxSector
  4702. && !test_bit(Faulty, &tmp->rdev->flags)
  4703. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  4704. count++;
  4705. sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
  4706. }
  4707. }
  4708. spin_lock_irqsave(&conf->device_lock, flags);
  4709. mddev->degraded -= count;
  4710. spin_unlock_irqrestore(&conf->device_lock, flags);
  4711. print_raid5_conf(conf);
  4712. return count;
  4713. }
  4714. static int raid5_remove_disk(mddev_t *mddev, int number)
  4715. {
  4716. raid5_conf_t *conf = mddev->private;
  4717. int err = 0;
  4718. mdk_rdev_t *rdev;
  4719. struct disk_info *p = conf->disks + number;
  4720. print_raid5_conf(conf);
  4721. rdev = p->rdev;
  4722. if (rdev) {
  4723. if (number >= conf->raid_disks &&
  4724. conf->reshape_progress == MaxSector)
  4725. clear_bit(In_sync, &rdev->flags);
  4726. if (test_bit(In_sync, &rdev->flags) ||
  4727. atomic_read(&rdev->nr_pending)) {
  4728. err = -EBUSY;
  4729. goto abort;
  4730. }
  4731. /* Only remove non-faulty devices if recovery
  4732. * isn't possible.
  4733. */
  4734. if (!test_bit(Faulty, &rdev->flags) &&
  4735. !has_failed(conf) &&
  4736. number < conf->raid_disks) {
  4737. err = -EBUSY;
  4738. goto abort;
  4739. }
  4740. p->rdev = NULL;
  4741. synchronize_rcu();
  4742. if (atomic_read(&rdev->nr_pending)) {
  4743. /* lost the race, try later */
  4744. err = -EBUSY;
  4745. p->rdev = rdev;
  4746. }
  4747. }
  4748. abort:
  4749. print_raid5_conf(conf);
  4750. return err;
  4751. }
  4752. static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  4753. {
  4754. raid5_conf_t *conf = mddev->private;
  4755. int err = -EEXIST;
  4756. int disk;
  4757. struct disk_info *p;
  4758. int first = 0;
  4759. int last = conf->raid_disks - 1;
  4760. if (has_failed(conf))
  4761. /* no point adding a device */
  4762. return -EINVAL;
  4763. if (rdev->raid_disk >= 0)
  4764. first = last = rdev->raid_disk;
  4765. /*
  4766. * find the disk ... but prefer rdev->saved_raid_disk
  4767. * if possible.
  4768. */
  4769. if (rdev->saved_raid_disk >= 0 &&
  4770. rdev->saved_raid_disk >= first &&
  4771. conf->disks[rdev->saved_raid_disk].rdev == NULL)
  4772. disk = rdev->saved_raid_disk;
  4773. else
  4774. disk = first;
  4775. for ( ; disk <= last ; disk++)
  4776. if ((p=conf->disks + disk)->rdev == NULL) {
  4777. clear_bit(In_sync, &rdev->flags);
  4778. rdev->raid_disk = disk;
  4779. err = 0;
  4780. if (rdev->saved_raid_disk != disk)
  4781. conf->fullsync = 1;
  4782. rcu_assign_pointer(p->rdev, rdev);
  4783. break;
  4784. }
  4785. print_raid5_conf(conf);
  4786. return err;
  4787. }
  4788. static int raid5_resize(mddev_t *mddev, sector_t sectors)
  4789. {
  4790. /* no resync is happening, and there is enough space
  4791. * on all devices, so we can resize.
  4792. * We need to make sure resync covers any new space.
  4793. * If the array is shrinking we should possibly wait until
  4794. * any io in the removed space completes, but it hardly seems
  4795. * worth it.
  4796. */
  4797. sectors &= ~((sector_t)mddev->chunk_sectors - 1);
  4798. md_set_array_sectors(mddev, raid5_size(mddev, sectors,
  4799. mddev->raid_disks));
  4800. if (mddev->array_sectors >
  4801. raid5_size(mddev, sectors, mddev->raid_disks))
  4802. return -EINVAL;
  4803. set_capacity(mddev->gendisk, mddev->array_sectors);
  4804. revalidate_disk(mddev->gendisk);
  4805. if (sectors > mddev->dev_sectors &&
  4806. mddev->recovery_cp > mddev->dev_sectors) {
  4807. mddev->recovery_cp = mddev->dev_sectors;
  4808. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  4809. }
  4810. mddev->dev_sectors = sectors;
  4811. mddev->resync_max_sectors = sectors;
  4812. return 0;
  4813. }
  4814. static int check_stripe_cache(mddev_t *mddev)
  4815. {
  4816. /* Can only proceed if there are plenty of stripe_heads.
  4817. * We need a minimum of one full stripe,, and for sensible progress
  4818. * it is best to have about 4 times that.
  4819. * If we require 4 times, then the default 256 4K stripe_heads will
  4820. * allow for chunk sizes up to 256K, which is probably OK.
  4821. * If the chunk size is greater, user-space should request more
  4822. * stripe_heads first.
  4823. */
  4824. raid5_conf_t *conf = mddev->private;
  4825. if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
  4826. > conf->max_nr_stripes ||
  4827. ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
  4828. > conf->max_nr_stripes) {
  4829. printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
  4830. mdname(mddev),
  4831. ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
  4832. / STRIPE_SIZE)*4);
  4833. return 0;
  4834. }
  4835. return 1;
  4836. }
  4837. static int check_reshape(mddev_t *mddev)
  4838. {
  4839. raid5_conf_t *conf = mddev->private;
  4840. if (mddev->delta_disks == 0 &&
  4841. mddev->new_layout == mddev->layout &&
  4842. mddev->new_chunk_sectors == mddev->chunk_sectors)
  4843. return 0; /* nothing to do */
  4844. if (mddev->bitmap)
  4845. /* Cannot grow a bitmap yet */
  4846. return -EBUSY;
  4847. if (has_failed(conf))
  4848. return -EINVAL;
  4849. if (mddev->delta_disks < 0) {
  4850. /* We might be able to shrink, but the devices must
  4851. * be made bigger first.
  4852. * For raid6, 4 is the minimum size.
  4853. * Otherwise 2 is the minimum
  4854. */
  4855. int min = 2;
  4856. if (mddev->level == 6)
  4857. min = 4;
  4858. if (mddev->raid_disks + mddev->delta_disks < min)
  4859. return -EINVAL;
  4860. }
  4861. if (!check_stripe_cache(mddev))
  4862. return -ENOSPC;
  4863. return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
  4864. }
  4865. static int raid5_start_reshape(mddev_t *mddev)
  4866. {
  4867. raid5_conf_t *conf = mddev->private;
  4868. mdk_rdev_t *rdev;
  4869. int spares = 0;
  4870. unsigned long flags;
  4871. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  4872. return -EBUSY;
  4873. if (!check_stripe_cache(mddev))
  4874. return -ENOSPC;
  4875. list_for_each_entry(rdev, &mddev->disks, same_set)
  4876. if (!test_bit(In_sync, &rdev->flags)
  4877. && !test_bit(Faulty, &rdev->flags))
  4878. spares++;
  4879. if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
  4880. /* Not enough devices even to make a degraded array
  4881. * of that size
  4882. */
  4883. return -EINVAL;
  4884. /* Refuse to reduce size of the array. Any reductions in
  4885. * array size must be through explicit setting of array_size
  4886. * attribute.
  4887. */
  4888. if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
  4889. < mddev->array_sectors) {
  4890. printk(KERN_ERR "md/raid:%s: array size must be reduced "
  4891. "before number of disks\n", mdname(mddev));
  4892. return -EINVAL;
  4893. }
  4894. atomic_set(&conf->reshape_stripes, 0);
  4895. spin_lock_irq(&conf->device_lock);
  4896. conf->previous_raid_disks = conf->raid_disks;
  4897. conf->raid_disks += mddev->delta_disks;
  4898. conf->prev_chunk_sectors = conf->chunk_sectors;
  4899. conf->chunk_sectors = mddev->new_chunk_sectors;
  4900. conf->prev_algo = conf->algorithm;
  4901. conf->algorithm = mddev->new_layout;
  4902. if (mddev->delta_disks < 0)
  4903. conf->reshape_progress = raid5_size(mddev, 0, 0);
  4904. else
  4905. conf->reshape_progress = 0;
  4906. conf->reshape_safe = conf->reshape_progress;
  4907. conf->generation++;
  4908. spin_unlock_irq(&conf->device_lock);
  4909. /* Add some new drives, as many as will fit.
  4910. * We know there are enough to make the newly sized array work.
  4911. * Don't add devices if we are reducing the number of
  4912. * devices in the array. This is because it is not possible
  4913. * to correctly record the "partially reconstructed" state of
  4914. * such devices during the reshape and confusion could result.
  4915. */
  4916. if (mddev->delta_disks >= 0) {
  4917. int added_devices = 0;
  4918. list_for_each_entry(rdev, &mddev->disks, same_set)
  4919. if (rdev->raid_disk < 0 &&
  4920. !test_bit(Faulty, &rdev->flags)) {
  4921. if (raid5_add_disk(mddev, rdev) == 0) {
  4922. char nm[20];
  4923. if (rdev->raid_disk
  4924. >= conf->previous_raid_disks) {
  4925. set_bit(In_sync, &rdev->flags);
  4926. added_devices++;
  4927. } else
  4928. rdev->recovery_offset = 0;
  4929. sprintf(nm, "rd%d", rdev->raid_disk);
  4930. if (sysfs_create_link(&mddev->kobj,
  4931. &rdev->kobj, nm))
  4932. /* Failure here is OK */;
  4933. }
  4934. } else if (rdev->raid_disk >= conf->previous_raid_disks
  4935. && !test_bit(Faulty, &rdev->flags)) {
  4936. /* This is a spare that was manually added */
  4937. set_bit(In_sync, &rdev->flags);
  4938. added_devices++;
  4939. }
  4940. /* When a reshape changes the number of devices,
  4941. * ->degraded is measured against the larger of the
  4942. * pre and post number of devices.
  4943. */
  4944. spin_lock_irqsave(&conf->device_lock, flags);
  4945. mddev->degraded += (conf->raid_disks - conf->previous_raid_disks)
  4946. - added_devices;
  4947. spin_unlock_irqrestore(&conf->device_lock, flags);
  4948. }
  4949. mddev->raid_disks = conf->raid_disks;
  4950. mddev->reshape_position = conf->reshape_progress;
  4951. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  4952. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  4953. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  4954. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  4955. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  4956. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  4957. "reshape");
  4958. if (!mddev->sync_thread) {
  4959. mddev->recovery = 0;
  4960. spin_lock_irq(&conf->device_lock);
  4961. mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
  4962. conf->reshape_progress = MaxSector;
  4963. spin_unlock_irq(&conf->device_lock);
  4964. return -EAGAIN;
  4965. }
  4966. conf->reshape_checkpoint = jiffies;
  4967. md_wakeup_thread(mddev->sync_thread);
  4968. md_new_event(mddev);
  4969. return 0;
  4970. }
  4971. /* This is called from the reshape thread and should make any
  4972. * changes needed in 'conf'
  4973. */
  4974. static void end_reshape(raid5_conf_t *conf)
  4975. {
  4976. if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
  4977. spin_lock_irq(&conf->device_lock);
  4978. conf->previous_raid_disks = conf->raid_disks;
  4979. conf->reshape_progress = MaxSector;
  4980. spin_unlock_irq(&conf->device_lock);
  4981. wake_up(&conf->wait_for_overlap);
  4982. /* read-ahead size must cover two whole stripes, which is
  4983. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  4984. */
  4985. if (conf->mddev->queue) {
  4986. int data_disks = conf->raid_disks - conf->max_degraded;
  4987. int stripe = data_disks * ((conf->chunk_sectors << 9)
  4988. / PAGE_SIZE);
  4989. if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  4990. conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  4991. }
  4992. }
  4993. }
  4994. /* This is called from the raid5d thread with mddev_lock held.
  4995. * It makes config changes to the device.
  4996. */
  4997. static void raid5_finish_reshape(mddev_t *mddev)
  4998. {
  4999. raid5_conf_t *conf = mddev->private;
  5000. if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
  5001. if (mddev->delta_disks > 0) {
  5002. md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
  5003. set_capacity(mddev->gendisk, mddev->array_sectors);
  5004. revalidate_disk(mddev->gendisk);
  5005. } else {
  5006. int d;
  5007. mddev->degraded = conf->raid_disks;
  5008. for (d = 0; d < conf->raid_disks ; d++)
  5009. if (conf->disks[d].rdev &&
  5010. test_bit(In_sync,
  5011. &conf->disks[d].rdev->flags))
  5012. mddev->degraded--;
  5013. for (d = conf->raid_disks ;
  5014. d < conf->raid_disks - mddev->delta_disks;
  5015. d++) {
  5016. mdk_rdev_t *rdev = conf->disks[d].rdev;
  5017. if (rdev && raid5_remove_disk(mddev, d) == 0) {
  5018. char nm[20];
  5019. sprintf(nm, "rd%d", rdev->raid_disk);
  5020. sysfs_remove_link(&mddev->kobj, nm);
  5021. rdev->raid_disk = -1;
  5022. }
  5023. }
  5024. }
  5025. mddev->layout = conf->algorithm;
  5026. mddev->chunk_sectors = conf->chunk_sectors;
  5027. mddev->reshape_position = MaxSector;
  5028. mddev->delta_disks = 0;
  5029. }
  5030. }
  5031. static void raid5_quiesce(mddev_t *mddev, int state)
  5032. {
  5033. raid5_conf_t *conf = mddev->private;
  5034. switch(state) {
  5035. case 2: /* resume for a suspend */
  5036. wake_up(&conf->wait_for_overlap);
  5037. break;
  5038. case 1: /* stop all writes */
  5039. spin_lock_irq(&conf->device_lock);
  5040. /* '2' tells resync/reshape to pause so that all
  5041. * active stripes can drain
  5042. */
  5043. conf->quiesce = 2;
  5044. wait_event_lock_irq(conf->wait_for_stripe,
  5045. atomic_read(&conf->active_stripes) == 0 &&
  5046. atomic_read(&conf->active_aligned_reads) == 0,
  5047. conf->device_lock, /* nothing */);
  5048. conf->quiesce = 1;
  5049. spin_unlock_irq(&conf->device_lock);
  5050. /* allow reshape to continue */
  5051. wake_up(&conf->wait_for_overlap);
  5052. break;
  5053. case 0: /* re-enable writes */
  5054. spin_lock_irq(&conf->device_lock);
  5055. conf->quiesce = 0;
  5056. wake_up(&conf->wait_for_stripe);
  5057. wake_up(&conf->wait_for_overlap);
  5058. spin_unlock_irq(&conf->device_lock);
  5059. break;
  5060. }
  5061. }
  5062. static void *raid45_takeover_raid0(mddev_t *mddev, int level)
  5063. {
  5064. struct raid0_private_data *raid0_priv = mddev->private;
  5065. sector_t sectors;
  5066. /* for raid0 takeover only one zone is supported */
  5067. if (raid0_priv->nr_strip_zones > 1) {
  5068. printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
  5069. mdname(mddev));
  5070. return ERR_PTR(-EINVAL);
  5071. }
  5072. sectors = raid0_priv->strip_zone[0].zone_end;
  5073. sector_div(sectors, raid0_priv->strip_zone[0].nb_dev);
  5074. mddev->dev_sectors = sectors;
  5075. mddev->new_level = level;
  5076. mddev->new_layout = ALGORITHM_PARITY_N;
  5077. mddev->new_chunk_sectors = mddev->chunk_sectors;
  5078. mddev->raid_disks += 1;
  5079. mddev->delta_disks = 1;
  5080. /* make sure it will be not marked as dirty */
  5081. mddev->recovery_cp = MaxSector;
  5082. return setup_conf(mddev);
  5083. }
  5084. static void *raid5_takeover_raid1(mddev_t *mddev)
  5085. {
  5086. int chunksect;
  5087. if (mddev->raid_disks != 2 ||
  5088. mddev->degraded > 1)
  5089. return ERR_PTR(-EINVAL);
  5090. /* Should check if there are write-behind devices? */
  5091. chunksect = 64*2; /* 64K by default */
  5092. /* The array must be an exact multiple of chunksize */
  5093. while (chunksect && (mddev->array_sectors & (chunksect-1)))
  5094. chunksect >>= 1;
  5095. if ((chunksect<<9) < STRIPE_SIZE)
  5096. /* array size does not allow a suitable chunk size */
  5097. return ERR_PTR(-EINVAL);
  5098. mddev->new_level = 5;
  5099. mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
  5100. mddev->new_chunk_sectors = chunksect;
  5101. return setup_conf(mddev);
  5102. }
  5103. static void *raid5_takeover_raid6(mddev_t *mddev)
  5104. {
  5105. int new_layout;
  5106. switch (mddev->layout) {
  5107. case ALGORITHM_LEFT_ASYMMETRIC_6:
  5108. new_layout = ALGORITHM_LEFT_ASYMMETRIC;
  5109. break;
  5110. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  5111. new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
  5112. break;
  5113. case ALGORITHM_LEFT_SYMMETRIC_6:
  5114. new_layout = ALGORITHM_LEFT_SYMMETRIC;
  5115. break;
  5116. case ALGORITHM_RIGHT_SYMMETRIC_6:
  5117. new_layout = ALGORITHM_RIGHT_SYMMETRIC;
  5118. break;
  5119. case ALGORITHM_PARITY_0_6:
  5120. new_layout = ALGORITHM_PARITY_0;
  5121. break;
  5122. case ALGORITHM_PARITY_N:
  5123. new_layout = ALGORITHM_PARITY_N;
  5124. break;
  5125. default:
  5126. return ERR_PTR(-EINVAL);
  5127. }
  5128. mddev->new_level = 5;
  5129. mddev->new_layout = new_layout;
  5130. mddev->delta_disks = -1;
  5131. mddev->raid_disks -= 1;
  5132. return setup_conf(mddev);
  5133. }
  5134. static int raid5_check_reshape(mddev_t *mddev)
  5135. {
  5136. /* For a 2-drive array, the layout and chunk size can be changed
  5137. * immediately as not restriping is needed.
  5138. * For larger arrays we record the new value - after validation
  5139. * to be used by a reshape pass.
  5140. */
  5141. raid5_conf_t *conf = mddev->private;
  5142. int new_chunk = mddev->new_chunk_sectors;
  5143. if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
  5144. return -EINVAL;
  5145. if (new_chunk > 0) {
  5146. if (!is_power_of_2(new_chunk))
  5147. return -EINVAL;
  5148. if (new_chunk < (PAGE_SIZE>>9))
  5149. return -EINVAL;
  5150. if (mddev->array_sectors & (new_chunk-1))
  5151. /* not factor of array size */
  5152. return -EINVAL;
  5153. }
  5154. /* They look valid */
  5155. if (mddev->raid_disks == 2) {
  5156. /* can make the change immediately */
  5157. if (mddev->new_layout >= 0) {
  5158. conf->algorithm = mddev->new_layout;
  5159. mddev->layout = mddev->new_layout;
  5160. }
  5161. if (new_chunk > 0) {
  5162. conf->chunk_sectors = new_chunk ;
  5163. mddev->chunk_sectors = new_chunk;
  5164. }
  5165. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  5166. md_wakeup_thread(mddev->thread);
  5167. }
  5168. return check_reshape(mddev);
  5169. }
  5170. static int raid6_check_reshape(mddev_t *mddev)
  5171. {
  5172. int new_chunk = mddev->new_chunk_sectors;
  5173. if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
  5174. return -EINVAL;
  5175. if (new_chunk > 0) {
  5176. if (!is_power_of_2(new_chunk))
  5177. return -EINVAL;
  5178. if (new_chunk < (PAGE_SIZE >> 9))
  5179. return -EINVAL;
  5180. if (mddev->array_sectors & (new_chunk-1))
  5181. /* not factor of array size */
  5182. return -EINVAL;
  5183. }
  5184. /* They look valid */
  5185. return check_reshape(mddev);
  5186. }
  5187. static void *raid5_takeover(mddev_t *mddev)
  5188. {
  5189. /* raid5 can take over:
  5190. * raid0 - if there is only one strip zone - make it a raid4 layout
  5191. * raid1 - if there are two drives. We need to know the chunk size
  5192. * raid4 - trivial - just use a raid4 layout.
  5193. * raid6 - Providing it is a *_6 layout
  5194. */
  5195. if (mddev->level == 0)
  5196. return raid45_takeover_raid0(mddev, 5);
  5197. if (mddev->level == 1)
  5198. return raid5_takeover_raid1(mddev);
  5199. if (mddev->level == 4) {
  5200. mddev->new_layout = ALGORITHM_PARITY_N;
  5201. mddev->new_level = 5;
  5202. return setup_conf(mddev);
  5203. }
  5204. if (mddev->level == 6)
  5205. return raid5_takeover_raid6(mddev);
  5206. return ERR_PTR(-EINVAL);
  5207. }
  5208. static void *raid4_takeover(mddev_t *mddev)
  5209. {
  5210. /* raid4 can take over:
  5211. * raid0 - if there is only one strip zone
  5212. * raid5 - if layout is right
  5213. */
  5214. if (mddev->level == 0)
  5215. return raid45_takeover_raid0(mddev, 4);
  5216. if (mddev->level == 5 &&
  5217. mddev->layout == ALGORITHM_PARITY_N) {
  5218. mddev->new_layout = 0;
  5219. mddev->new_level = 4;
  5220. return setup_conf(mddev);
  5221. }
  5222. return ERR_PTR(-EINVAL);
  5223. }
  5224. static struct mdk_personality raid5_personality;
  5225. static void *raid6_takeover(mddev_t *mddev)
  5226. {
  5227. /* Currently can only take over a raid5. We map the
  5228. * personality to an equivalent raid6 personality
  5229. * with the Q block at the end.
  5230. */
  5231. int new_layout;
  5232. if (mddev->pers != &raid5_personality)
  5233. return ERR_PTR(-EINVAL);
  5234. if (mddev->degraded > 1)
  5235. return ERR_PTR(-EINVAL);
  5236. if (mddev->raid_disks > 253)
  5237. return ERR_PTR(-EINVAL);
  5238. if (mddev->raid_disks < 3)
  5239. return ERR_PTR(-EINVAL);
  5240. switch (mddev->layout) {
  5241. case ALGORITHM_LEFT_ASYMMETRIC:
  5242. new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
  5243. break;
  5244. case ALGORITHM_RIGHT_ASYMMETRIC:
  5245. new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
  5246. break;
  5247. case ALGORITHM_LEFT_SYMMETRIC:
  5248. new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
  5249. break;
  5250. case ALGORITHM_RIGHT_SYMMETRIC:
  5251. new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
  5252. break;
  5253. case ALGORITHM_PARITY_0:
  5254. new_layout = ALGORITHM_PARITY_0_6;
  5255. break;
  5256. case ALGORITHM_PARITY_N:
  5257. new_layout = ALGORITHM_PARITY_N;
  5258. break;
  5259. default:
  5260. return ERR_PTR(-EINVAL);
  5261. }
  5262. mddev->new_level = 6;
  5263. mddev->new_layout = new_layout;
  5264. mddev->delta_disks = 1;
  5265. mddev->raid_disks += 1;
  5266. return setup_conf(mddev);
  5267. }
  5268. static struct mdk_personality raid6_personality =
  5269. {
  5270. .name = "raid6",
  5271. .level = 6,
  5272. .owner = THIS_MODULE,
  5273. .make_request = make_request,
  5274. .run = run,
  5275. .stop = stop,
  5276. .status = status,
  5277. .error_handler = error,
  5278. .hot_add_disk = raid5_add_disk,
  5279. .hot_remove_disk= raid5_remove_disk,
  5280. .spare_active = raid5_spare_active,
  5281. .sync_request = sync_request,
  5282. .resize = raid5_resize,
  5283. .size = raid5_size,
  5284. .check_reshape = raid6_check_reshape,
  5285. .start_reshape = raid5_start_reshape,
  5286. .finish_reshape = raid5_finish_reshape,
  5287. .quiesce = raid5_quiesce,
  5288. .takeover = raid6_takeover,
  5289. };
  5290. static struct mdk_personality raid5_personality =
  5291. {
  5292. .name = "raid5",
  5293. .level = 5,
  5294. .owner = THIS_MODULE,
  5295. .make_request = make_request,
  5296. .run = run,
  5297. .stop = stop,
  5298. .status = status,
  5299. .error_handler = error,
  5300. .hot_add_disk = raid5_add_disk,
  5301. .hot_remove_disk= raid5_remove_disk,
  5302. .spare_active = raid5_spare_active,
  5303. .sync_request = sync_request,
  5304. .resize = raid5_resize,
  5305. .size = raid5_size,
  5306. .check_reshape = raid5_check_reshape,
  5307. .start_reshape = raid5_start_reshape,
  5308. .finish_reshape = raid5_finish_reshape,
  5309. .quiesce = raid5_quiesce,
  5310. .takeover = raid5_takeover,
  5311. };
  5312. static struct mdk_personality raid4_personality =
  5313. {
  5314. .name = "raid4",
  5315. .level = 4,
  5316. .owner = THIS_MODULE,
  5317. .make_request = make_request,
  5318. .run = run,
  5319. .stop = stop,
  5320. .status = status,
  5321. .error_handler = error,
  5322. .hot_add_disk = raid5_add_disk,
  5323. .hot_remove_disk= raid5_remove_disk,
  5324. .spare_active = raid5_spare_active,
  5325. .sync_request = sync_request,
  5326. .resize = raid5_resize,
  5327. .size = raid5_size,
  5328. .check_reshape = raid5_check_reshape,
  5329. .start_reshape = raid5_start_reshape,
  5330. .finish_reshape = raid5_finish_reshape,
  5331. .quiesce = raid5_quiesce,
  5332. .takeover = raid4_takeover,
  5333. };
  5334. static int __init raid5_init(void)
  5335. {
  5336. register_md_personality(&raid6_personality);
  5337. register_md_personality(&raid5_personality);
  5338. register_md_personality(&raid4_personality);
  5339. return 0;
  5340. }
  5341. static void raid5_exit(void)
  5342. {
  5343. unregister_md_personality(&raid6_personality);
  5344. unregister_md_personality(&raid5_personality);
  5345. unregister_md_personality(&raid4_personality);
  5346. }
  5347. module_init(raid5_init);
  5348. module_exit(raid5_exit);
  5349. MODULE_LICENSE("GPL");
  5350. MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
  5351. MODULE_ALIAS("md-personality-4"); /* RAID5 */
  5352. MODULE_ALIAS("md-raid5");
  5353. MODULE_ALIAS("md-raid4");
  5354. MODULE_ALIAS("md-level-5");
  5355. MODULE_ALIAS("md-level-4");
  5356. MODULE_ALIAS("md-personality-8"); /* RAID6 */
  5357. MODULE_ALIAS("md-raid6");
  5358. MODULE_ALIAS("md-level-6");
  5359. /* This used to be two separate modules, they were: */
  5360. MODULE_ALIAS("raid5");
  5361. MODULE_ALIAS("raid6");