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