disable some mediatekl custom warnings
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / md / raid5.c
1 /*
2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
5 * Copyright (C) 2002, 2003 H. Peter Anvin
6 *
7 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 /*
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 <trace/events/block.h>
57
58 #include "md.h"
59 #include "raid5.h"
60 #include "raid0.h"
61 #include "bitmap.h"
62
63 /*
64 * Stripe cache
65 */
66
67 #define NR_STRIPES 256
68 #define STRIPE_SIZE PAGE_SIZE
69 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
70 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
71 #define IO_THRESHOLD 1
72 #define BYPASS_THRESHOLD 1
73 #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
74 #define HASH_MASK (NR_HASH - 1)
75
76 static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
77 {
78 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
79 return &conf->stripe_hashtbl[hash];
80 }
81
82 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
83 * order without overlap. There may be several bio's per stripe+device, and
84 * a bio could span several devices.
85 * When walking this list for a particular stripe+device, we must never proceed
86 * beyond a bio that extends past this device, as the next bio might no longer
87 * be valid.
88 * This function is used to determine the 'next' bio in the list, given the sector
89 * of the current stripe+device
90 */
91 static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
92 {
93 int sectors = bio_sectors(bio);
94 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
95 return bio->bi_next;
96 else
97 return NULL;
98 }
99
100 /*
101 * We maintain a biased count of active stripes in the bottom 16 bits of
102 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
103 */
104 static inline int raid5_bi_processed_stripes(struct bio *bio)
105 {
106 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
107 return (atomic_read(segments) >> 16) & 0xffff;
108 }
109
110 static inline int raid5_dec_bi_active_stripes(struct bio *bio)
111 {
112 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
113 return atomic_sub_return(1, segments) & 0xffff;
114 }
115
116 static inline void raid5_inc_bi_active_stripes(struct bio *bio)
117 {
118 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
119 atomic_inc(segments);
120 }
121
122 static inline void raid5_set_bi_processed_stripes(struct bio *bio,
123 unsigned int cnt)
124 {
125 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
126 int old, new;
127
128 do {
129 old = atomic_read(segments);
130 new = (old & 0xffff) | (cnt << 16);
131 } while (atomic_cmpxchg(segments, old, new) != old);
132 }
133
134 static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
135 {
136 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
137 atomic_set(segments, cnt);
138 }
139
140 /* Find first data disk in a raid6 stripe */
141 static inline int raid6_d0(struct stripe_head *sh)
142 {
143 if (sh->ddf_layout)
144 /* ddf always start from first device */
145 return 0;
146 /* md starts just after Q block */
147 if (sh->qd_idx == sh->disks - 1)
148 return 0;
149 else
150 return sh->qd_idx + 1;
151 }
152 static inline int raid6_next_disk(int disk, int raid_disks)
153 {
154 disk++;
155 return (disk < raid_disks) ? disk : 0;
156 }
157
158 /* When walking through the disks in a raid5, starting at raid6_d0,
159 * We need to map each disk to a 'slot', where the data disks are slot
160 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
161 * is raid_disks-1. This help does that mapping.
162 */
163 static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
164 int *count, int syndrome_disks)
165 {
166 int slot = *count;
167
168 if (sh->ddf_layout)
169 (*count)++;
170 if (idx == sh->pd_idx)
171 return syndrome_disks;
172 if (idx == sh->qd_idx)
173 return syndrome_disks + 1;
174 if (!sh->ddf_layout)
175 (*count)++;
176 return slot;
177 }
178
179 static void return_io(struct bio *return_bi)
180 {
181 struct bio *bi = return_bi;
182 while (bi) {
183
184 return_bi = bi->bi_next;
185 bi->bi_next = NULL;
186 bi->bi_size = 0;
187 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
188 bi, 0);
189 bio_endio(bi, 0);
190 bi = return_bi;
191 }
192 }
193
194 static void print_raid5_conf (struct r5conf *conf);
195
196 static int stripe_operations_active(struct stripe_head *sh)
197 {
198 return sh->check_state || sh->reconstruct_state ||
199 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
200 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
201 }
202
203 static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh)
204 {
205 BUG_ON(!list_empty(&sh->lru));
206 BUG_ON(atomic_read(&conf->active_stripes)==0);
207 if (test_bit(STRIPE_HANDLE, &sh->state)) {
208 if (test_bit(STRIPE_DELAYED, &sh->state) &&
209 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
210 list_add_tail(&sh->lru, &conf->delayed_list);
211 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
212 sh->bm_seq - conf->seq_write > 0)
213 list_add_tail(&sh->lru, &conf->bitmap_list);
214 else {
215 clear_bit(STRIPE_DELAYED, &sh->state);
216 clear_bit(STRIPE_BIT_DELAY, &sh->state);
217 list_add_tail(&sh->lru, &conf->handle_list);
218 }
219 md_wakeup_thread(conf->mddev->thread);
220 } else {
221 BUG_ON(stripe_operations_active(sh));
222 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
223 if (atomic_dec_return(&conf->preread_active_stripes)
224 < IO_THRESHOLD)
225 md_wakeup_thread(conf->mddev->thread);
226 atomic_dec(&conf->active_stripes);
227 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
228 list_add_tail(&sh->lru, &conf->inactive_list);
229 wake_up(&conf->wait_for_stripe);
230 if (conf->retry_read_aligned)
231 md_wakeup_thread(conf->mddev->thread);
232 }
233 }
234 }
235
236 static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
237 {
238 if (atomic_dec_and_test(&sh->count))
239 do_release_stripe(conf, sh);
240 }
241
242 static void release_stripe(struct stripe_head *sh)
243 {
244 struct r5conf *conf = sh->raid_conf;
245 unsigned long flags;
246
247 local_irq_save(flags);
248 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
249 do_release_stripe(conf, sh);
250 spin_unlock(&conf->device_lock);
251 }
252 local_irq_restore(flags);
253 }
254
255 static inline void remove_hash(struct stripe_head *sh)
256 {
257 pr_debug("remove_hash(), stripe %llu\n",
258 (unsigned long long)sh->sector);
259
260 hlist_del_init(&sh->hash);
261 }
262
263 static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
264 {
265 struct hlist_head *hp = stripe_hash(conf, sh->sector);
266
267 pr_debug("insert_hash(), stripe %llu\n",
268 (unsigned long long)sh->sector);
269
270 hlist_add_head(&sh->hash, hp);
271 }
272
273
274 /* find an idle stripe, make sure it is unhashed, and return it. */
275 static struct stripe_head *get_free_stripe(struct r5conf *conf)
276 {
277 struct stripe_head *sh = NULL;
278 struct list_head *first;
279
280 if (list_empty(&conf->inactive_list))
281 goto out;
282 first = conf->inactive_list.next;
283 sh = list_entry(first, struct stripe_head, lru);
284 list_del_init(first);
285 remove_hash(sh);
286 atomic_inc(&conf->active_stripes);
287 out:
288 return sh;
289 }
290
291 static void shrink_buffers(struct stripe_head *sh)
292 {
293 struct page *p;
294 int i;
295 int num = sh->raid_conf->pool_size;
296
297 for (i = 0; i < num ; i++) {
298 p = sh->dev[i].page;
299 if (!p)
300 continue;
301 sh->dev[i].page = NULL;
302 put_page(p);
303 }
304 }
305
306 static int grow_buffers(struct stripe_head *sh)
307 {
308 int i;
309 int num = sh->raid_conf->pool_size;
310
311 for (i = 0; i < num; i++) {
312 struct page *page;
313
314 if (!(page = alloc_page(GFP_KERNEL))) {
315 return 1;
316 }
317 sh->dev[i].page = page;
318 }
319 return 0;
320 }
321
322 static void raid5_build_block(struct stripe_head *sh, int i, int previous);
323 static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
324 struct stripe_head *sh);
325
326 static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
327 {
328 struct r5conf *conf = sh->raid_conf;
329 int i;
330
331 BUG_ON(atomic_read(&sh->count) != 0);
332 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
333 BUG_ON(stripe_operations_active(sh));
334
335 pr_debug("init_stripe called, stripe %llu\n",
336 (unsigned long long)sh->sector);
337
338 remove_hash(sh);
339
340 sh->generation = conf->generation - previous;
341 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
342 sh->sector = sector;
343 stripe_set_idx(sector, conf, previous, sh);
344 sh->state = 0;
345
346
347 for (i = sh->disks; i--; ) {
348 struct r5dev *dev = &sh->dev[i];
349
350 if (dev->toread || dev->read || dev->towrite || dev->written ||
351 test_bit(R5_LOCKED, &dev->flags)) {
352 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
353 (unsigned long long)sh->sector, i, dev->toread,
354 dev->read, dev->towrite, dev->written,
355 test_bit(R5_LOCKED, &dev->flags));
356 WARN_ON(1);
357 }
358 dev->flags = 0;
359 raid5_build_block(sh, i, previous);
360 }
361 insert_hash(conf, sh);
362 }
363
364 static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
365 short generation)
366 {
367 struct stripe_head *sh;
368
369 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
370 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
371 if (sh->sector == sector && sh->generation == generation)
372 return sh;
373 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
374 return NULL;
375 }
376
377 /*
378 * Need to check if array has failed when deciding whether to:
379 * - start an array
380 * - remove non-faulty devices
381 * - add a spare
382 * - allow a reshape
383 * This determination is simple when no reshape is happening.
384 * However if there is a reshape, we need to carefully check
385 * both the before and after sections.
386 * This is because some failed devices may only affect one
387 * of the two sections, and some non-in_sync devices may
388 * be insync in the section most affected by failed devices.
389 */
390 static int calc_degraded(struct r5conf *conf)
391 {
392 int degraded, degraded2;
393 int i;
394
395 rcu_read_lock();
396 degraded = 0;
397 for (i = 0; i < conf->previous_raid_disks; i++) {
398 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
399 if (rdev && test_bit(Faulty, &rdev->flags))
400 rdev = rcu_dereference(conf->disks[i].replacement);
401 if (!rdev || test_bit(Faulty, &rdev->flags))
402 degraded++;
403 else if (test_bit(In_sync, &rdev->flags))
404 ;
405 else
406 /* not in-sync or faulty.
407 * If the reshape increases the number of devices,
408 * this is being recovered by the reshape, so
409 * this 'previous' section is not in_sync.
410 * If the number of devices is being reduced however,
411 * the device can only be part of the array if
412 * we are reverting a reshape, so this section will
413 * be in-sync.
414 */
415 if (conf->raid_disks >= conf->previous_raid_disks)
416 degraded++;
417 }
418 rcu_read_unlock();
419 if (conf->raid_disks == conf->previous_raid_disks)
420 return degraded;
421 rcu_read_lock();
422 degraded2 = 0;
423 for (i = 0; i < conf->raid_disks; i++) {
424 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
425 if (rdev && test_bit(Faulty, &rdev->flags))
426 rdev = rcu_dereference(conf->disks[i].replacement);
427 if (!rdev || test_bit(Faulty, &rdev->flags))
428 degraded2++;
429 else if (test_bit(In_sync, &rdev->flags))
430 ;
431 else
432 /* not in-sync or faulty.
433 * If reshape increases the number of devices, this
434 * section has already been recovered, else it
435 * almost certainly hasn't.
436 */
437 if (conf->raid_disks <= conf->previous_raid_disks)
438 degraded2++;
439 }
440 rcu_read_unlock();
441 if (degraded2 > degraded)
442 return degraded2;
443 return degraded;
444 }
445
446 static int has_failed(struct r5conf *conf)
447 {
448 int degraded;
449
450 if (conf->mddev->reshape_position == MaxSector)
451 return conf->mddev->degraded > conf->max_degraded;
452
453 degraded = calc_degraded(conf);
454 if (degraded > conf->max_degraded)
455 return 1;
456 return 0;
457 }
458
459 static struct stripe_head *
460 get_active_stripe(struct r5conf *conf, sector_t sector,
461 int previous, int noblock, int noquiesce)
462 {
463 struct stripe_head *sh;
464
465 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
466
467 spin_lock_irq(&conf->device_lock);
468
469 do {
470 wait_event_lock_irq(conf->wait_for_stripe,
471 conf->quiesce == 0 || noquiesce,
472 conf->device_lock);
473 sh = __find_stripe(conf, sector, conf->generation - previous);
474 if (!sh) {
475 if (!conf->inactive_blocked)
476 sh = get_free_stripe(conf);
477 if (noblock && sh == NULL)
478 break;
479 if (!sh) {
480 conf->inactive_blocked = 1;
481 wait_event_lock_irq(conf->wait_for_stripe,
482 !list_empty(&conf->inactive_list) &&
483 (atomic_read(&conf->active_stripes)
484 < (conf->max_nr_stripes *3/4)
485 || !conf->inactive_blocked),
486 conf->device_lock);
487 conf->inactive_blocked = 0;
488 } else
489 init_stripe(sh, sector, previous);
490 } else {
491 if (atomic_read(&sh->count)) {
492 BUG_ON(!list_empty(&sh->lru)
493 && !test_bit(STRIPE_EXPANDING, &sh->state)
494 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state));
495 } else {
496 if (!test_bit(STRIPE_HANDLE, &sh->state))
497 atomic_inc(&conf->active_stripes);
498 if (list_empty(&sh->lru) &&
499 !test_bit(STRIPE_EXPANDING, &sh->state))
500 BUG();
501 list_del_init(&sh->lru);
502 }
503 }
504 } while (sh == NULL);
505
506 if (sh)
507 atomic_inc(&sh->count);
508
509 spin_unlock_irq(&conf->device_lock);
510 return sh;
511 }
512
513 /* Determine if 'data_offset' or 'new_data_offset' should be used
514 * in this stripe_head.
515 */
516 static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
517 {
518 sector_t progress = conf->reshape_progress;
519 /* Need a memory barrier to make sure we see the value
520 * of conf->generation, or ->data_offset that was set before
521 * reshape_progress was updated.
522 */
523 smp_rmb();
524 if (progress == MaxSector)
525 return 0;
526 if (sh->generation == conf->generation - 1)
527 return 0;
528 /* We are in a reshape, and this is a new-generation stripe,
529 * so use new_data_offset.
530 */
531 return 1;
532 }
533
534 static void
535 raid5_end_read_request(struct bio *bi, int error);
536 static void
537 raid5_end_write_request(struct bio *bi, int error);
538
539 static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
540 {
541 struct r5conf *conf = sh->raid_conf;
542 int i, disks = sh->disks;
543
544 might_sleep();
545
546 for (i = disks; i--; ) {
547 int rw;
548 int replace_only = 0;
549 struct bio *bi, *rbi;
550 struct md_rdev *rdev, *rrdev = NULL;
551 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
552 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
553 rw = WRITE_FUA;
554 else
555 rw = WRITE;
556 if (test_bit(R5_Discard, &sh->dev[i].flags))
557 rw |= REQ_DISCARD;
558 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
559 rw = READ;
560 else if (test_and_clear_bit(R5_WantReplace,
561 &sh->dev[i].flags)) {
562 rw = WRITE;
563 replace_only = 1;
564 } else
565 continue;
566 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
567 rw |= REQ_SYNC;
568
569 bi = &sh->dev[i].req;
570 rbi = &sh->dev[i].rreq; /* For writing to replacement */
571
572 rcu_read_lock();
573 rrdev = rcu_dereference(conf->disks[i].replacement);
574 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
575 rdev = rcu_dereference(conf->disks[i].rdev);
576 if (!rdev) {
577 rdev = rrdev;
578 rrdev = NULL;
579 }
580 if (rw & WRITE) {
581 if (replace_only)
582 rdev = NULL;
583 if (rdev == rrdev)
584 /* We raced and saw duplicates */
585 rrdev = NULL;
586 } else {
587 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
588 rdev = rrdev;
589 rrdev = NULL;
590 }
591
592 if (rdev && test_bit(Faulty, &rdev->flags))
593 rdev = NULL;
594 if (rdev)
595 atomic_inc(&rdev->nr_pending);
596 if (rrdev && test_bit(Faulty, &rrdev->flags))
597 rrdev = NULL;
598 if (rrdev)
599 atomic_inc(&rrdev->nr_pending);
600 rcu_read_unlock();
601
602 /* We have already checked bad blocks for reads. Now
603 * need to check for writes. We never accept write errors
604 * on the replacement, so we don't to check rrdev.
605 */
606 while ((rw & WRITE) && rdev &&
607 test_bit(WriteErrorSeen, &rdev->flags)) {
608 sector_t first_bad;
609 int bad_sectors;
610 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
611 &first_bad, &bad_sectors);
612 if (!bad)
613 break;
614
615 if (bad < 0) {
616 set_bit(BlockedBadBlocks, &rdev->flags);
617 if (!conf->mddev->external &&
618 conf->mddev->flags) {
619 /* It is very unlikely, but we might
620 * still need to write out the
621 * bad block log - better give it
622 * a chance*/
623 md_check_recovery(conf->mddev);
624 }
625 /*
626 * Because md_wait_for_blocked_rdev
627 * will dec nr_pending, we must
628 * increment it first.
629 */
630 atomic_inc(&rdev->nr_pending);
631 md_wait_for_blocked_rdev(rdev, conf->mddev);
632 } else {
633 /* Acknowledged bad block - skip the write */
634 rdev_dec_pending(rdev, conf->mddev);
635 rdev = NULL;
636 }
637 }
638
639 if (rdev) {
640 if (s->syncing || s->expanding || s->expanded
641 || s->replacing)
642 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
643
644 set_bit(STRIPE_IO_STARTED, &sh->state);
645
646 bio_reset(bi);
647 bi->bi_bdev = rdev->bdev;
648 bi->bi_rw = rw;
649 bi->bi_end_io = (rw & WRITE)
650 ? raid5_end_write_request
651 : raid5_end_read_request;
652 bi->bi_private = sh;
653
654 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
655 __func__, (unsigned long long)sh->sector,
656 bi->bi_rw, i);
657 atomic_inc(&sh->count);
658 if (use_new_offset(conf, sh))
659 bi->bi_sector = (sh->sector
660 + rdev->new_data_offset);
661 else
662 bi->bi_sector = (sh->sector
663 + rdev->data_offset);
664 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
665 bi->bi_rw |= REQ_FLUSH;
666
667 bi->bi_vcnt = 1;
668 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
669 bi->bi_io_vec[0].bv_offset = 0;
670 bi->bi_size = STRIPE_SIZE;
671 /*
672 * If this is discard request, set bi_vcnt 0. We don't
673 * want to confuse SCSI because SCSI will replace payload
674 */
675 if (rw & REQ_DISCARD)
676 bi->bi_vcnt = 0;
677 if (rrdev)
678 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
679
680 if (conf->mddev->gendisk)
681 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
682 bi, disk_devt(conf->mddev->gendisk),
683 sh->dev[i].sector);
684 generic_make_request(bi);
685 }
686 if (rrdev) {
687 if (s->syncing || s->expanding || s->expanded
688 || s->replacing)
689 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
690
691 set_bit(STRIPE_IO_STARTED, &sh->state);
692
693 bio_reset(rbi);
694 rbi->bi_bdev = rrdev->bdev;
695 rbi->bi_rw = rw;
696 BUG_ON(!(rw & WRITE));
697 rbi->bi_end_io = raid5_end_write_request;
698 rbi->bi_private = sh;
699
700 pr_debug("%s: for %llu schedule op %ld on "
701 "replacement disc %d\n",
702 __func__, (unsigned long long)sh->sector,
703 rbi->bi_rw, i);
704 atomic_inc(&sh->count);
705 if (use_new_offset(conf, sh))
706 rbi->bi_sector = (sh->sector
707 + rrdev->new_data_offset);
708 else
709 rbi->bi_sector = (sh->sector
710 + rrdev->data_offset);
711 rbi->bi_vcnt = 1;
712 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
713 rbi->bi_io_vec[0].bv_offset = 0;
714 rbi->bi_size = STRIPE_SIZE;
715 /*
716 * If this is discard request, set bi_vcnt 0. We don't
717 * want to confuse SCSI because SCSI will replace payload
718 */
719 if (rw & REQ_DISCARD)
720 rbi->bi_vcnt = 0;
721 if (conf->mddev->gendisk)
722 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
723 rbi, disk_devt(conf->mddev->gendisk),
724 sh->dev[i].sector);
725 generic_make_request(rbi);
726 }
727 if (!rdev && !rrdev) {
728 if (rw & WRITE)
729 set_bit(STRIPE_DEGRADED, &sh->state);
730 pr_debug("skip op %ld on disc %d for sector %llu\n",
731 bi->bi_rw, i, (unsigned long long)sh->sector);
732 clear_bit(R5_LOCKED, &sh->dev[i].flags);
733 set_bit(STRIPE_HANDLE, &sh->state);
734 }
735 }
736 }
737
738 static struct dma_async_tx_descriptor *
739 async_copy_data(int frombio, struct bio *bio, struct page *page,
740 sector_t sector, struct dma_async_tx_descriptor *tx)
741 {
742 struct bio_vec *bvl;
743 struct page *bio_page;
744 int i;
745 int page_offset;
746 struct async_submit_ctl submit;
747 enum async_tx_flags flags = 0;
748
749 if (bio->bi_sector >= sector)
750 page_offset = (signed)(bio->bi_sector - sector) * 512;
751 else
752 page_offset = (signed)(sector - bio->bi_sector) * -512;
753
754 if (frombio)
755 flags |= ASYNC_TX_FENCE;
756 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
757
758 bio_for_each_segment(bvl, bio, i) {
759 int len = bvl->bv_len;
760 int clen;
761 int b_offset = 0;
762
763 if (page_offset < 0) {
764 b_offset = -page_offset;
765 page_offset += b_offset;
766 len -= b_offset;
767 }
768
769 if (len > 0 && page_offset + len > STRIPE_SIZE)
770 clen = STRIPE_SIZE - page_offset;
771 else
772 clen = len;
773
774 if (clen > 0) {
775 b_offset += bvl->bv_offset;
776 bio_page = bvl->bv_page;
777 if (frombio)
778 tx = async_memcpy(page, bio_page, page_offset,
779 b_offset, clen, &submit);
780 else
781 tx = async_memcpy(bio_page, page, b_offset,
782 page_offset, clen, &submit);
783 }
784 /* chain the operations */
785 submit.depend_tx = tx;
786
787 if (clen < len) /* hit end of page */
788 break;
789 page_offset += len;
790 }
791
792 return tx;
793 }
794
795 static void ops_complete_biofill(void *stripe_head_ref)
796 {
797 struct stripe_head *sh = stripe_head_ref;
798 struct bio *return_bi = NULL;
799 int i;
800
801 pr_debug("%s: stripe %llu\n", __func__,
802 (unsigned long long)sh->sector);
803
804 /* clear completed biofills */
805 for (i = sh->disks; i--; ) {
806 struct r5dev *dev = &sh->dev[i];
807
808 /* acknowledge completion of a biofill operation */
809 /* and check if we need to reply to a read request,
810 * new R5_Wantfill requests are held off until
811 * !STRIPE_BIOFILL_RUN
812 */
813 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
814 struct bio *rbi, *rbi2;
815
816 BUG_ON(!dev->read);
817 rbi = dev->read;
818 dev->read = NULL;
819 while (rbi && rbi->bi_sector <
820 dev->sector + STRIPE_SECTORS) {
821 rbi2 = r5_next_bio(rbi, dev->sector);
822 if (!raid5_dec_bi_active_stripes(rbi)) {
823 rbi->bi_next = return_bi;
824 return_bi = rbi;
825 }
826 rbi = rbi2;
827 }
828 }
829 }
830 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
831
832 return_io(return_bi);
833
834 set_bit(STRIPE_HANDLE, &sh->state);
835 release_stripe(sh);
836 }
837
838 static void ops_run_biofill(struct stripe_head *sh)
839 {
840 struct dma_async_tx_descriptor *tx = NULL;
841 struct async_submit_ctl submit;
842 int i;
843
844 pr_debug("%s: stripe %llu\n", __func__,
845 (unsigned long long)sh->sector);
846
847 for (i = sh->disks; i--; ) {
848 struct r5dev *dev = &sh->dev[i];
849 if (test_bit(R5_Wantfill, &dev->flags)) {
850 struct bio *rbi;
851 spin_lock_irq(&sh->stripe_lock);
852 dev->read = rbi = dev->toread;
853 dev->toread = NULL;
854 spin_unlock_irq(&sh->stripe_lock);
855 while (rbi && rbi->bi_sector <
856 dev->sector + STRIPE_SECTORS) {
857 tx = async_copy_data(0, rbi, dev->page,
858 dev->sector, tx);
859 rbi = r5_next_bio(rbi, dev->sector);
860 }
861 }
862 }
863
864 atomic_inc(&sh->count);
865 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
866 async_trigger_callback(&submit);
867 }
868
869 static void mark_target_uptodate(struct stripe_head *sh, int target)
870 {
871 struct r5dev *tgt;
872
873 if (target < 0)
874 return;
875
876 tgt = &sh->dev[target];
877 set_bit(R5_UPTODATE, &tgt->flags);
878 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
879 clear_bit(R5_Wantcompute, &tgt->flags);
880 }
881
882 static void ops_complete_compute(void *stripe_head_ref)
883 {
884 struct stripe_head *sh = stripe_head_ref;
885
886 pr_debug("%s: stripe %llu\n", __func__,
887 (unsigned long long)sh->sector);
888
889 /* mark the computed target(s) as uptodate */
890 mark_target_uptodate(sh, sh->ops.target);
891 mark_target_uptodate(sh, sh->ops.target2);
892
893 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
894 if (sh->check_state == check_state_compute_run)
895 sh->check_state = check_state_compute_result;
896 set_bit(STRIPE_HANDLE, &sh->state);
897 release_stripe(sh);
898 }
899
900 /* return a pointer to the address conversion region of the scribble buffer */
901 static addr_conv_t *to_addr_conv(struct stripe_head *sh,
902 struct raid5_percpu *percpu)
903 {
904 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
905 }
906
907 static struct dma_async_tx_descriptor *
908 ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
909 {
910 int disks = sh->disks;
911 struct page **xor_srcs = percpu->scribble;
912 int target = sh->ops.target;
913 struct r5dev *tgt = &sh->dev[target];
914 struct page *xor_dest = tgt->page;
915 int count = 0;
916 struct dma_async_tx_descriptor *tx;
917 struct async_submit_ctl submit;
918 int i;
919
920 pr_debug("%s: stripe %llu block: %d\n",
921 __func__, (unsigned long long)sh->sector, target);
922 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
923
924 for (i = disks; i--; )
925 if (i != target)
926 xor_srcs[count++] = sh->dev[i].page;
927
928 atomic_inc(&sh->count);
929
930 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
931 ops_complete_compute, sh, to_addr_conv(sh, percpu));
932 if (unlikely(count == 1))
933 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
934 else
935 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
936
937 return tx;
938 }
939
940 /* set_syndrome_sources - populate source buffers for gen_syndrome
941 * @srcs - (struct page *) array of size sh->disks
942 * @sh - stripe_head to parse
943 *
944 * Populates srcs in proper layout order for the stripe and returns the
945 * 'count' of sources to be used in a call to async_gen_syndrome. The P
946 * destination buffer is recorded in srcs[count] and the Q destination
947 * is recorded in srcs[count+1]].
948 */
949 static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
950 {
951 int disks = sh->disks;
952 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
953 int d0_idx = raid6_d0(sh);
954 int count;
955 int i;
956
957 for (i = 0; i < disks; i++)
958 srcs[i] = NULL;
959
960 count = 0;
961 i = d0_idx;
962 do {
963 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
964
965 srcs[slot] = sh->dev[i].page;
966 i = raid6_next_disk(i, disks);
967 } while (i != d0_idx);
968
969 return syndrome_disks;
970 }
971
972 static struct dma_async_tx_descriptor *
973 ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
974 {
975 int disks = sh->disks;
976 struct page **blocks = percpu->scribble;
977 int target;
978 int qd_idx = sh->qd_idx;
979 struct dma_async_tx_descriptor *tx;
980 struct async_submit_ctl submit;
981 struct r5dev *tgt;
982 struct page *dest;
983 int i;
984 int count;
985
986 if (sh->ops.target < 0)
987 target = sh->ops.target2;
988 else if (sh->ops.target2 < 0)
989 target = sh->ops.target;
990 else
991 /* we should only have one valid target */
992 BUG();
993 BUG_ON(target < 0);
994 pr_debug("%s: stripe %llu block: %d\n",
995 __func__, (unsigned long long)sh->sector, target);
996
997 tgt = &sh->dev[target];
998 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
999 dest = tgt->page;
1000
1001 atomic_inc(&sh->count);
1002
1003 if (target == qd_idx) {
1004 count = set_syndrome_sources(blocks, sh);
1005 blocks[count] = NULL; /* regenerating p is not necessary */
1006 BUG_ON(blocks[count+1] != dest); /* q should already be set */
1007 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1008 ops_complete_compute, sh,
1009 to_addr_conv(sh, percpu));
1010 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1011 } else {
1012 /* Compute any data- or p-drive using XOR */
1013 count = 0;
1014 for (i = disks; i-- ; ) {
1015 if (i == target || i == qd_idx)
1016 continue;
1017 blocks[count++] = sh->dev[i].page;
1018 }
1019
1020 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1021 NULL, ops_complete_compute, sh,
1022 to_addr_conv(sh, percpu));
1023 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1024 }
1025
1026 return tx;
1027 }
1028
1029 static struct dma_async_tx_descriptor *
1030 ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1031 {
1032 int i, count, disks = sh->disks;
1033 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1034 int d0_idx = raid6_d0(sh);
1035 int faila = -1, failb = -1;
1036 int target = sh->ops.target;
1037 int target2 = sh->ops.target2;
1038 struct r5dev *tgt = &sh->dev[target];
1039 struct r5dev *tgt2 = &sh->dev[target2];
1040 struct dma_async_tx_descriptor *tx;
1041 struct page **blocks = percpu->scribble;
1042 struct async_submit_ctl submit;
1043
1044 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1045 __func__, (unsigned long long)sh->sector, target, target2);
1046 BUG_ON(target < 0 || target2 < 0);
1047 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1048 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1049
1050 /* we need to open-code set_syndrome_sources to handle the
1051 * slot number conversion for 'faila' and 'failb'
1052 */
1053 for (i = 0; i < disks ; i++)
1054 blocks[i] = NULL;
1055 count = 0;
1056 i = d0_idx;
1057 do {
1058 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1059
1060 blocks[slot] = sh->dev[i].page;
1061
1062 if (i == target)
1063 faila = slot;
1064 if (i == target2)
1065 failb = slot;
1066 i = raid6_next_disk(i, disks);
1067 } while (i != d0_idx);
1068
1069 BUG_ON(faila == failb);
1070 if (failb < faila)
1071 swap(faila, failb);
1072 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1073 __func__, (unsigned long long)sh->sector, faila, failb);
1074
1075 atomic_inc(&sh->count);
1076
1077 if (failb == syndrome_disks+1) {
1078 /* Q disk is one of the missing disks */
1079 if (faila == syndrome_disks) {
1080 /* Missing P+Q, just recompute */
1081 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1082 ops_complete_compute, sh,
1083 to_addr_conv(sh, percpu));
1084 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
1085 STRIPE_SIZE, &submit);
1086 } else {
1087 struct page *dest;
1088 int data_target;
1089 int qd_idx = sh->qd_idx;
1090
1091 /* Missing D+Q: recompute D from P, then recompute Q */
1092 if (target == qd_idx)
1093 data_target = target2;
1094 else
1095 data_target = target;
1096
1097 count = 0;
1098 for (i = disks; i-- ; ) {
1099 if (i == data_target || i == qd_idx)
1100 continue;
1101 blocks[count++] = sh->dev[i].page;
1102 }
1103 dest = sh->dev[data_target].page;
1104 init_async_submit(&submit,
1105 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1106 NULL, NULL, NULL,
1107 to_addr_conv(sh, percpu));
1108 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1109 &submit);
1110
1111 count = set_syndrome_sources(blocks, sh);
1112 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1113 ops_complete_compute, sh,
1114 to_addr_conv(sh, percpu));
1115 return async_gen_syndrome(blocks, 0, count+2,
1116 STRIPE_SIZE, &submit);
1117 }
1118 } else {
1119 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1120 ops_complete_compute, sh,
1121 to_addr_conv(sh, percpu));
1122 if (failb == syndrome_disks) {
1123 /* We're missing D+P. */
1124 return async_raid6_datap_recov(syndrome_disks+2,
1125 STRIPE_SIZE, faila,
1126 blocks, &submit);
1127 } else {
1128 /* We're missing D+D. */
1129 return async_raid6_2data_recov(syndrome_disks+2,
1130 STRIPE_SIZE, faila, failb,
1131 blocks, &submit);
1132 }
1133 }
1134 }
1135
1136
1137 static void ops_complete_prexor(void *stripe_head_ref)
1138 {
1139 struct stripe_head *sh = stripe_head_ref;
1140
1141 pr_debug("%s: stripe %llu\n", __func__,
1142 (unsigned long long)sh->sector);
1143 }
1144
1145 static struct dma_async_tx_descriptor *
1146 ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1147 struct dma_async_tx_descriptor *tx)
1148 {
1149 int disks = sh->disks;
1150 struct page **xor_srcs = percpu->scribble;
1151 int count = 0, pd_idx = sh->pd_idx, i;
1152 struct async_submit_ctl submit;
1153
1154 /* existing parity data subtracted */
1155 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1156
1157 pr_debug("%s: stripe %llu\n", __func__,
1158 (unsigned long long)sh->sector);
1159
1160 for (i = disks; i--; ) {
1161 struct r5dev *dev = &sh->dev[i];
1162 /* Only process blocks that are known to be uptodate */
1163 if (test_bit(R5_Wantdrain, &dev->flags))
1164 xor_srcs[count++] = dev->page;
1165 }
1166
1167 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
1168 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
1169 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
1170
1171 return tx;
1172 }
1173
1174 static struct dma_async_tx_descriptor *
1175 ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
1176 {
1177 int disks = sh->disks;
1178 int i;
1179
1180 pr_debug("%s: stripe %llu\n", __func__,
1181 (unsigned long long)sh->sector);
1182
1183 for (i = disks; i--; ) {
1184 struct r5dev *dev = &sh->dev[i];
1185 struct bio *chosen;
1186
1187 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
1188 struct bio *wbi;
1189
1190 spin_lock_irq(&sh->stripe_lock);
1191 chosen = dev->towrite;
1192 dev->towrite = NULL;
1193 BUG_ON(dev->written);
1194 wbi = dev->written = chosen;
1195 spin_unlock_irq(&sh->stripe_lock);
1196
1197 while (wbi && wbi->bi_sector <
1198 dev->sector + STRIPE_SECTORS) {
1199 if (wbi->bi_rw & REQ_FUA)
1200 set_bit(R5_WantFUA, &dev->flags);
1201 if (wbi->bi_rw & REQ_SYNC)
1202 set_bit(R5_SyncIO, &dev->flags);
1203 if (wbi->bi_rw & REQ_DISCARD)
1204 set_bit(R5_Discard, &dev->flags);
1205 else
1206 tx = async_copy_data(1, wbi, dev->page,
1207 dev->sector, tx);
1208 wbi = r5_next_bio(wbi, dev->sector);
1209 }
1210 }
1211 }
1212
1213 return tx;
1214 }
1215
1216 static void ops_complete_reconstruct(void *stripe_head_ref)
1217 {
1218 struct stripe_head *sh = stripe_head_ref;
1219 int disks = sh->disks;
1220 int pd_idx = sh->pd_idx;
1221 int qd_idx = sh->qd_idx;
1222 int i;
1223 bool fua = false, sync = false, discard = false;
1224
1225 pr_debug("%s: stripe %llu\n", __func__,
1226 (unsigned long long)sh->sector);
1227
1228 for (i = disks; i--; ) {
1229 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1230 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
1231 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
1232 }
1233
1234 for (i = disks; i--; ) {
1235 struct r5dev *dev = &sh->dev[i];
1236
1237 if (dev->written || i == pd_idx || i == qd_idx) {
1238 if (!discard)
1239 set_bit(R5_UPTODATE, &dev->flags);
1240 if (fua)
1241 set_bit(R5_WantFUA, &dev->flags);
1242 if (sync)
1243 set_bit(R5_SyncIO, &dev->flags);
1244 }
1245 }
1246
1247 if (sh->reconstruct_state == reconstruct_state_drain_run)
1248 sh->reconstruct_state = reconstruct_state_drain_result;
1249 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1250 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1251 else {
1252 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1253 sh->reconstruct_state = reconstruct_state_result;
1254 }
1255
1256 set_bit(STRIPE_HANDLE, &sh->state);
1257 release_stripe(sh);
1258 }
1259
1260 static void
1261 ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1262 struct dma_async_tx_descriptor *tx)
1263 {
1264 int disks = sh->disks;
1265 struct page **xor_srcs = percpu->scribble;
1266 struct async_submit_ctl submit;
1267 int count = 0, pd_idx = sh->pd_idx, i;
1268 struct page *xor_dest;
1269 int prexor = 0;
1270 unsigned long flags;
1271
1272 pr_debug("%s: stripe %llu\n", __func__,
1273 (unsigned long long)sh->sector);
1274
1275 for (i = 0; i < sh->disks; i++) {
1276 if (pd_idx == i)
1277 continue;
1278 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1279 break;
1280 }
1281 if (i >= sh->disks) {
1282 atomic_inc(&sh->count);
1283 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1284 ops_complete_reconstruct(sh);
1285 return;
1286 }
1287 /* check if prexor is active which means only process blocks
1288 * that are part of a read-modify-write (written)
1289 */
1290 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1291 prexor = 1;
1292 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1293 for (i = disks; i--; ) {
1294 struct r5dev *dev = &sh->dev[i];
1295 if (dev->written)
1296 xor_srcs[count++] = dev->page;
1297 }
1298 } else {
1299 xor_dest = sh->dev[pd_idx].page;
1300 for (i = disks; i--; ) {
1301 struct r5dev *dev = &sh->dev[i];
1302 if (i != pd_idx)
1303 xor_srcs[count++] = dev->page;
1304 }
1305 }
1306
1307 /* 1/ if we prexor'd then the dest is reused as a source
1308 * 2/ if we did not prexor then we are redoing the parity
1309 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1310 * for the synchronous xor case
1311 */
1312 flags = ASYNC_TX_ACK |
1313 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1314
1315 atomic_inc(&sh->count);
1316
1317 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
1318 to_addr_conv(sh, percpu));
1319 if (unlikely(count == 1))
1320 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1321 else
1322 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
1323 }
1324
1325 static void
1326 ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1327 struct dma_async_tx_descriptor *tx)
1328 {
1329 struct async_submit_ctl submit;
1330 struct page **blocks = percpu->scribble;
1331 int count, i;
1332
1333 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1334
1335 for (i = 0; i < sh->disks; i++) {
1336 if (sh->pd_idx == i || sh->qd_idx == i)
1337 continue;
1338 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1339 break;
1340 }
1341 if (i >= sh->disks) {
1342 atomic_inc(&sh->count);
1343 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1344 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1345 ops_complete_reconstruct(sh);
1346 return;
1347 }
1348
1349 count = set_syndrome_sources(blocks, sh);
1350
1351 atomic_inc(&sh->count);
1352
1353 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1354 sh, to_addr_conv(sh, percpu));
1355 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1356 }
1357
1358 static void ops_complete_check(void *stripe_head_ref)
1359 {
1360 struct stripe_head *sh = stripe_head_ref;
1361
1362 pr_debug("%s: stripe %llu\n", __func__,
1363 (unsigned long long)sh->sector);
1364
1365 sh->check_state = check_state_check_result;
1366 set_bit(STRIPE_HANDLE, &sh->state);
1367 release_stripe(sh);
1368 }
1369
1370 static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
1371 {
1372 int disks = sh->disks;
1373 int pd_idx = sh->pd_idx;
1374 int qd_idx = sh->qd_idx;
1375 struct page *xor_dest;
1376 struct page **xor_srcs = percpu->scribble;
1377 struct dma_async_tx_descriptor *tx;
1378 struct async_submit_ctl submit;
1379 int count;
1380 int i;
1381
1382 pr_debug("%s: stripe %llu\n", __func__,
1383 (unsigned long long)sh->sector);
1384
1385 count = 0;
1386 xor_dest = sh->dev[pd_idx].page;
1387 xor_srcs[count++] = xor_dest;
1388 for (i = disks; i--; ) {
1389 if (i == pd_idx || i == qd_idx)
1390 continue;
1391 xor_srcs[count++] = sh->dev[i].page;
1392 }
1393
1394 init_async_submit(&submit, 0, NULL, NULL, NULL,
1395 to_addr_conv(sh, percpu));
1396 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
1397 &sh->ops.zero_sum_result, &submit);
1398
1399 atomic_inc(&sh->count);
1400 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1401 tx = async_trigger_callback(&submit);
1402 }
1403
1404 static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1405 {
1406 struct page **srcs = percpu->scribble;
1407 struct async_submit_ctl submit;
1408 int count;
1409
1410 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1411 (unsigned long long)sh->sector, checkp);
1412
1413 count = set_syndrome_sources(srcs, sh);
1414 if (!checkp)
1415 srcs[count] = NULL;
1416
1417 atomic_inc(&sh->count);
1418 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1419 sh, to_addr_conv(sh, percpu));
1420 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1421 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1422 }
1423
1424 static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1425 {
1426 int overlap_clear = 0, i, disks = sh->disks;
1427 struct dma_async_tx_descriptor *tx = NULL;
1428 struct r5conf *conf = sh->raid_conf;
1429 int level = conf->level;
1430 struct raid5_percpu *percpu;
1431 unsigned long cpu;
1432
1433 cpu = get_cpu();
1434 percpu = per_cpu_ptr(conf->percpu, cpu);
1435 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
1436 ops_run_biofill(sh);
1437 overlap_clear++;
1438 }
1439
1440 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
1441 if (level < 6)
1442 tx = ops_run_compute5(sh, percpu);
1443 else {
1444 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1445 tx = ops_run_compute6_1(sh, percpu);
1446 else
1447 tx = ops_run_compute6_2(sh, percpu);
1448 }
1449 /* terminate the chain if reconstruct is not set to be run */
1450 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
1451 async_tx_ack(tx);
1452 }
1453
1454 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
1455 tx = ops_run_prexor(sh, percpu, tx);
1456
1457 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
1458 tx = ops_run_biodrain(sh, tx);
1459 overlap_clear++;
1460 }
1461
1462 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1463 if (level < 6)
1464 ops_run_reconstruct5(sh, percpu, tx);
1465 else
1466 ops_run_reconstruct6(sh, percpu, tx);
1467 }
1468
1469 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1470 if (sh->check_state == check_state_run)
1471 ops_run_check_p(sh, percpu);
1472 else if (sh->check_state == check_state_run_q)
1473 ops_run_check_pq(sh, percpu, 0);
1474 else if (sh->check_state == check_state_run_pq)
1475 ops_run_check_pq(sh, percpu, 1);
1476 else
1477 BUG();
1478 }
1479
1480 if (overlap_clear)
1481 for (i = disks; i--; ) {
1482 struct r5dev *dev = &sh->dev[i];
1483 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1484 wake_up(&sh->raid_conf->wait_for_overlap);
1485 }
1486 put_cpu();
1487 }
1488
1489 static int grow_one_stripe(struct r5conf *conf)
1490 {
1491 struct stripe_head *sh;
1492 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
1493 if (!sh)
1494 return 0;
1495
1496 sh->raid_conf = conf;
1497
1498 spin_lock_init(&sh->stripe_lock);
1499
1500 if (grow_buffers(sh)) {
1501 shrink_buffers(sh);
1502 kmem_cache_free(conf->slab_cache, sh);
1503 return 0;
1504 }
1505 /* we just created an active stripe so... */
1506 atomic_set(&sh->count, 1);
1507 atomic_inc(&conf->active_stripes);
1508 INIT_LIST_HEAD(&sh->lru);
1509 release_stripe(sh);
1510 return 1;
1511 }
1512
1513 static int grow_stripes(struct r5conf *conf, int num)
1514 {
1515 struct kmem_cache *sc;
1516 int devs = max(conf->raid_disks, conf->previous_raid_disks);
1517
1518 if (conf->mddev->gendisk)
1519 sprintf(conf->cache_name[0],
1520 "raid%d-%s", conf->level, mdname(conf->mddev));
1521 else
1522 sprintf(conf->cache_name[0],
1523 "raid%d-%p", conf->level, conf->mddev);
1524 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1525
1526 conf->active_name = 0;
1527 sc = kmem_cache_create(conf->cache_name[conf->active_name],
1528 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
1529 0, 0, NULL);
1530 if (!sc)
1531 return 1;
1532 conf->slab_cache = sc;
1533 conf->pool_size = devs;
1534 while (num--)
1535 if (!grow_one_stripe(conf))
1536 return 1;
1537 return 0;
1538 }
1539
1540 /**
1541 * scribble_len - return the required size of the scribble region
1542 * @num - total number of disks in the array
1543 *
1544 * The size must be enough to contain:
1545 * 1/ a struct page pointer for each device in the array +2
1546 * 2/ room to convert each entry in (1) to its corresponding dma
1547 * (dma_map_page()) or page (page_address()) address.
1548 *
1549 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1550 * calculate over all devices (not just the data blocks), using zeros in place
1551 * of the P and Q blocks.
1552 */
1553 static size_t scribble_len(int num)
1554 {
1555 size_t len;
1556
1557 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1558
1559 return len;
1560 }
1561
1562 static int resize_stripes(struct r5conf *conf, int newsize)
1563 {
1564 /* Make all the stripes able to hold 'newsize' devices.
1565 * New slots in each stripe get 'page' set to a new page.
1566 *
1567 * This happens in stages:
1568 * 1/ create a new kmem_cache and allocate the required number of
1569 * stripe_heads.
1570 * 2/ gather all the old stripe_heads and transfer the pages across
1571 * to the new stripe_heads. This will have the side effect of
1572 * freezing the array as once all stripe_heads have been collected,
1573 * no IO will be possible. Old stripe heads are freed once their
1574 * pages have been transferred over, and the old kmem_cache is
1575 * freed when all stripes are done.
1576 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1577 * we simple return a failre status - no need to clean anything up.
1578 * 4/ allocate new pages for the new slots in the new stripe_heads.
1579 * If this fails, we don't bother trying the shrink the
1580 * stripe_heads down again, we just leave them as they are.
1581 * As each stripe_head is processed the new one is released into
1582 * active service.
1583 *
1584 * Once step2 is started, we cannot afford to wait for a write,
1585 * so we use GFP_NOIO allocations.
1586 */
1587 struct stripe_head *osh, *nsh;
1588 LIST_HEAD(newstripes);
1589 struct disk_info *ndisks;
1590 unsigned long cpu;
1591 int err;
1592 struct kmem_cache *sc;
1593 int i;
1594
1595 if (newsize <= conf->pool_size)
1596 return 0; /* never bother to shrink */
1597
1598 err = md_allow_write(conf->mddev);
1599 if (err)
1600 return err;
1601
1602 /* Step 1 */
1603 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1604 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
1605 0, 0, NULL);
1606 if (!sc)
1607 return -ENOMEM;
1608
1609 for (i = conf->max_nr_stripes; i; i--) {
1610 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
1611 if (!nsh)
1612 break;
1613
1614 nsh->raid_conf = conf;
1615 spin_lock_init(&nsh->stripe_lock);
1616
1617 list_add(&nsh->lru, &newstripes);
1618 }
1619 if (i) {
1620 /* didn't get enough, give up */
1621 while (!list_empty(&newstripes)) {
1622 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1623 list_del(&nsh->lru);
1624 kmem_cache_free(sc, nsh);
1625 }
1626 kmem_cache_destroy(sc);
1627 return -ENOMEM;
1628 }
1629 /* Step 2 - Must use GFP_NOIO now.
1630 * OK, we have enough stripes, start collecting inactive
1631 * stripes and copying them over
1632 */
1633 list_for_each_entry(nsh, &newstripes, lru) {
1634 spin_lock_irq(&conf->device_lock);
1635 wait_event_lock_irq(conf->wait_for_stripe,
1636 !list_empty(&conf->inactive_list),
1637 conf->device_lock);
1638 osh = get_free_stripe(conf);
1639 spin_unlock_irq(&conf->device_lock);
1640 atomic_set(&nsh->count, 1);
1641 for(i=0; i<conf->pool_size; i++)
1642 nsh->dev[i].page = osh->dev[i].page;
1643 for( ; i<newsize; i++)
1644 nsh->dev[i].page = NULL;
1645 kmem_cache_free(conf->slab_cache, osh);
1646 }
1647 kmem_cache_destroy(conf->slab_cache);
1648
1649 /* Step 3.
1650 * At this point, we are holding all the stripes so the array
1651 * is completely stalled, so now is a good time to resize
1652 * conf->disks and the scribble region
1653 */
1654 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1655 if (ndisks) {
1656 for (i=0; i<conf->raid_disks; i++)
1657 ndisks[i] = conf->disks[i];
1658 kfree(conf->disks);
1659 conf->disks = ndisks;
1660 } else
1661 err = -ENOMEM;
1662
1663 get_online_cpus();
1664 conf->scribble_len = scribble_len(newsize);
1665 for_each_present_cpu(cpu) {
1666 struct raid5_percpu *percpu;
1667 void *scribble;
1668
1669 percpu = per_cpu_ptr(conf->percpu, cpu);
1670 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1671
1672 if (scribble) {
1673 kfree(percpu->scribble);
1674 percpu->scribble = scribble;
1675 } else {
1676 err = -ENOMEM;
1677 break;
1678 }
1679 }
1680 put_online_cpus();
1681
1682 /* Step 4, return new stripes to service */
1683 while(!list_empty(&newstripes)) {
1684 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1685 list_del_init(&nsh->lru);
1686
1687 for (i=conf->raid_disks; i < newsize; i++)
1688 if (nsh->dev[i].page == NULL) {
1689 struct page *p = alloc_page(GFP_NOIO);
1690 nsh->dev[i].page = p;
1691 if (!p)
1692 err = -ENOMEM;
1693 }
1694 release_stripe(nsh);
1695 }
1696 /* critical section pass, GFP_NOIO no longer needed */
1697
1698 conf->slab_cache = sc;
1699 conf->active_name = 1-conf->active_name;
1700 conf->pool_size = newsize;
1701 return err;
1702 }
1703
1704 static int drop_one_stripe(struct r5conf *conf)
1705 {
1706 struct stripe_head *sh;
1707
1708 spin_lock_irq(&conf->device_lock);
1709 sh = get_free_stripe(conf);
1710 spin_unlock_irq(&conf->device_lock);
1711 if (!sh)
1712 return 0;
1713 BUG_ON(atomic_read(&sh->count));
1714 shrink_buffers(sh);
1715 kmem_cache_free(conf->slab_cache, sh);
1716 atomic_dec(&conf->active_stripes);
1717 return 1;
1718 }
1719
1720 static void shrink_stripes(struct r5conf *conf)
1721 {
1722 while (drop_one_stripe(conf))
1723 ;
1724
1725 if (conf->slab_cache)
1726 kmem_cache_destroy(conf->slab_cache);
1727 conf->slab_cache = NULL;
1728 }
1729
1730 static void raid5_end_read_request(struct bio * bi, int error)
1731 {
1732 struct stripe_head *sh = bi->bi_private;
1733 struct r5conf *conf = sh->raid_conf;
1734 int disks = sh->disks, i;
1735 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1736 char b[BDEVNAME_SIZE];
1737 struct md_rdev *rdev = NULL;
1738 sector_t s;
1739
1740 for (i=0 ; i<disks; i++)
1741 if (bi == &sh->dev[i].req)
1742 break;
1743
1744 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1745 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1746 uptodate);
1747 if (i == disks) {
1748 BUG();
1749 return;
1750 }
1751 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1752 /* If replacement finished while this request was outstanding,
1753 * 'replacement' might be NULL already.
1754 * In that case it moved down to 'rdev'.
1755 * rdev is not removed until all requests are finished.
1756 */
1757 rdev = conf->disks[i].replacement;
1758 if (!rdev)
1759 rdev = conf->disks[i].rdev;
1760
1761 if (use_new_offset(conf, sh))
1762 s = sh->sector + rdev->new_data_offset;
1763 else
1764 s = sh->sector + rdev->data_offset;
1765 if (uptodate) {
1766 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1767 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1768 /* Note that this cannot happen on a
1769 * replacement device. We just fail those on
1770 * any error
1771 */
1772 printk_ratelimited(
1773 KERN_INFO
1774 "md/raid:%s: read error corrected"
1775 " (%lu sectors at %llu on %s)\n",
1776 mdname(conf->mddev), STRIPE_SECTORS,
1777 (unsigned long long)s,
1778 bdevname(rdev->bdev, b));
1779 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
1780 clear_bit(R5_ReadError, &sh->dev[i].flags);
1781 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1782 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1783 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1784
1785 if (atomic_read(&rdev->read_errors))
1786 atomic_set(&rdev->read_errors, 0);
1787 } else {
1788 const char *bdn = bdevname(rdev->bdev, b);
1789 int retry = 0;
1790 int set_bad = 0;
1791
1792 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
1793 atomic_inc(&rdev->read_errors);
1794 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1795 printk_ratelimited(
1796 KERN_WARNING
1797 "md/raid:%s: read error on replacement device "
1798 "(sector %llu on %s).\n",
1799 mdname(conf->mddev),
1800 (unsigned long long)s,
1801 bdn);
1802 else if (conf->mddev->degraded >= conf->max_degraded) {
1803 set_bad = 1;
1804 printk_ratelimited(
1805 KERN_WARNING
1806 "md/raid:%s: read error not correctable "
1807 "(sector %llu on %s).\n",
1808 mdname(conf->mddev),
1809 (unsigned long long)s,
1810 bdn);
1811 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
1812 /* Oh, no!!! */
1813 set_bad = 1;
1814 printk_ratelimited(
1815 KERN_WARNING
1816 "md/raid:%s: read error NOT corrected!! "
1817 "(sector %llu on %s).\n",
1818 mdname(conf->mddev),
1819 (unsigned long long)s,
1820 bdn);
1821 } else if (atomic_read(&rdev->read_errors)
1822 > conf->max_nr_stripes)
1823 printk(KERN_WARNING
1824 "md/raid:%s: Too many read errors, failing device %s.\n",
1825 mdname(conf->mddev), bdn);
1826 else
1827 retry = 1;
1828 if (retry)
1829 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1830 set_bit(R5_ReadError, &sh->dev[i].flags);
1831 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1832 } else
1833 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1834 else {
1835 clear_bit(R5_ReadError, &sh->dev[i].flags);
1836 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1837 if (!(set_bad
1838 && test_bit(In_sync, &rdev->flags)
1839 && rdev_set_badblocks(
1840 rdev, sh->sector, STRIPE_SECTORS, 0)))
1841 md_error(conf->mddev, rdev);
1842 }
1843 }
1844 rdev_dec_pending(rdev, conf->mddev);
1845 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1846 set_bit(STRIPE_HANDLE, &sh->state);
1847 release_stripe(sh);
1848 }
1849
1850 static void raid5_end_write_request(struct bio *bi, int error)
1851 {
1852 struct stripe_head *sh = bi->bi_private;
1853 struct r5conf *conf = sh->raid_conf;
1854 int disks = sh->disks, i;
1855 struct md_rdev *uninitialized_var(rdev);
1856 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1857 sector_t first_bad;
1858 int bad_sectors;
1859 int replacement = 0;
1860
1861 for (i = 0 ; i < disks; i++) {
1862 if (bi == &sh->dev[i].req) {
1863 rdev = conf->disks[i].rdev;
1864 break;
1865 }
1866 if (bi == &sh->dev[i].rreq) {
1867 rdev = conf->disks[i].replacement;
1868 if (rdev)
1869 replacement = 1;
1870 else
1871 /* rdev was removed and 'replacement'
1872 * replaced it. rdev is not removed
1873 * until all requests are finished.
1874 */
1875 rdev = conf->disks[i].rdev;
1876 break;
1877 }
1878 }
1879 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
1880 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1881 uptodate);
1882 if (i == disks) {
1883 BUG();
1884 return;
1885 }
1886
1887 if (replacement) {
1888 if (!uptodate)
1889 md_error(conf->mddev, rdev);
1890 else if (is_badblock(rdev, sh->sector,
1891 STRIPE_SECTORS,
1892 &first_bad, &bad_sectors))
1893 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1894 } else {
1895 if (!uptodate) {
1896 set_bit(STRIPE_DEGRADED, &sh->state);
1897 set_bit(WriteErrorSeen, &rdev->flags);
1898 set_bit(R5_WriteError, &sh->dev[i].flags);
1899 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1900 set_bit(MD_RECOVERY_NEEDED,
1901 &rdev->mddev->recovery);
1902 } else if (is_badblock(rdev, sh->sector,
1903 STRIPE_SECTORS,
1904 &first_bad, &bad_sectors)) {
1905 set_bit(R5_MadeGood, &sh->dev[i].flags);
1906 if (test_bit(R5_ReadError, &sh->dev[i].flags))
1907 /* That was a successful write so make
1908 * sure it looks like we already did
1909 * a re-write.
1910 */
1911 set_bit(R5_ReWrite, &sh->dev[i].flags);
1912 }
1913 }
1914 rdev_dec_pending(rdev, conf->mddev);
1915
1916 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1917 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1918 set_bit(STRIPE_HANDLE, &sh->state);
1919 release_stripe(sh);
1920 }
1921
1922 static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
1923
1924 static void raid5_build_block(struct stripe_head *sh, int i, int previous)
1925 {
1926 struct r5dev *dev = &sh->dev[i];
1927
1928 bio_init(&dev->req);
1929 dev->req.bi_io_vec = &dev->vec;
1930 dev->req.bi_vcnt++;
1931 dev->req.bi_max_vecs++;
1932 dev->req.bi_private = sh;
1933 dev->vec.bv_page = dev->page;
1934
1935 bio_init(&dev->rreq);
1936 dev->rreq.bi_io_vec = &dev->rvec;
1937 dev->rreq.bi_vcnt++;
1938 dev->rreq.bi_max_vecs++;
1939 dev->rreq.bi_private = sh;
1940 dev->rvec.bv_page = dev->page;
1941
1942 dev->flags = 0;
1943 dev->sector = compute_blocknr(sh, i, previous);
1944 }
1945
1946 static void error(struct mddev *mddev, struct md_rdev *rdev)
1947 {
1948 char b[BDEVNAME_SIZE];
1949 struct r5conf *conf = mddev->private;
1950 unsigned long flags;
1951 pr_debug("raid456: error called\n");
1952
1953 spin_lock_irqsave(&conf->device_lock, flags);
1954 clear_bit(In_sync, &rdev->flags);
1955 mddev->degraded = calc_degraded(conf);
1956 spin_unlock_irqrestore(&conf->device_lock, flags);
1957 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1958
1959 set_bit(Blocked, &rdev->flags);
1960 set_bit(Faulty, &rdev->flags);
1961 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1962 printk(KERN_ALERT
1963 "md/raid:%s: Disk failure on %s, disabling device.\n"
1964 "md/raid:%s: Operation continuing on %d devices.\n",
1965 mdname(mddev),
1966 bdevname(rdev->bdev, b),
1967 mdname(mddev),
1968 conf->raid_disks - mddev->degraded);
1969 }
1970
1971 /*
1972 * Input: a 'big' sector number,
1973 * Output: index of the data and parity disk, and the sector # in them.
1974 */
1975 static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
1976 int previous, int *dd_idx,
1977 struct stripe_head *sh)
1978 {
1979 sector_t stripe, stripe2;
1980 sector_t chunk_number;
1981 unsigned int chunk_offset;
1982 int pd_idx, qd_idx;
1983 int ddf_layout = 0;
1984 sector_t new_sector;
1985 int algorithm = previous ? conf->prev_algo
1986 : conf->algorithm;
1987 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1988 : conf->chunk_sectors;
1989 int raid_disks = previous ? conf->previous_raid_disks
1990 : conf->raid_disks;
1991 int data_disks = raid_disks - conf->max_degraded;
1992
1993 /* First compute the information on this sector */
1994
1995 /*
1996 * Compute the chunk number and the sector offset inside the chunk
1997 */
1998 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1999 chunk_number = r_sector;
2000
2001 /*
2002 * Compute the stripe number
2003 */
2004 stripe = chunk_number;
2005 *dd_idx = sector_div(stripe, data_disks);
2006 stripe2 = stripe;
2007 /*
2008 * Select the parity disk based on the user selected algorithm.
2009 */
2010 pd_idx = qd_idx = -1;
2011 switch(conf->level) {
2012 case 4:
2013 pd_idx = data_disks;
2014 break;
2015 case 5:
2016 switch (algorithm) {
2017 case ALGORITHM_LEFT_ASYMMETRIC:
2018 pd_idx = data_disks - sector_div(stripe2, raid_disks);
2019 if (*dd_idx >= pd_idx)
2020 (*dd_idx)++;
2021 break;
2022 case ALGORITHM_RIGHT_ASYMMETRIC:
2023 pd_idx = sector_div(stripe2, raid_disks);
2024 if (*dd_idx >= pd_idx)
2025 (*dd_idx)++;
2026 break;
2027 case ALGORITHM_LEFT_SYMMETRIC:
2028 pd_idx = data_disks - sector_div(stripe2, raid_disks);
2029 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
2030 break;
2031 case ALGORITHM_RIGHT_SYMMETRIC:
2032 pd_idx = sector_div(stripe2, raid_disks);
2033 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
2034 break;
2035 case ALGORITHM_PARITY_0:
2036 pd_idx = 0;
2037 (*dd_idx)++;
2038 break;
2039 case ALGORITHM_PARITY_N:
2040 pd_idx = data_disks;
2041 break;
2042 default:
2043 BUG();
2044 }
2045 break;
2046 case 6:
2047
2048 switch (algorithm) {
2049 case ALGORITHM_LEFT_ASYMMETRIC:
2050 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
2051 qd_idx = pd_idx + 1;
2052 if (pd_idx == raid_disks-1) {
2053 (*dd_idx)++; /* Q D D D P */
2054 qd_idx = 0;
2055 } else if (*dd_idx >= pd_idx)
2056 (*dd_idx) += 2; /* D D P Q D */
2057 break;
2058 case ALGORITHM_RIGHT_ASYMMETRIC:
2059 pd_idx = sector_div(stripe2, raid_disks);
2060 qd_idx = pd_idx + 1;
2061 if (pd_idx == raid_disks-1) {
2062 (*dd_idx)++; /* Q D D D P */
2063 qd_idx = 0;
2064 } else if (*dd_idx >= pd_idx)
2065 (*dd_idx) += 2; /* D D P Q D */
2066 break;
2067 case ALGORITHM_LEFT_SYMMETRIC:
2068 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
2069 qd_idx = (pd_idx + 1) % raid_disks;
2070 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
2071 break;
2072 case ALGORITHM_RIGHT_SYMMETRIC:
2073 pd_idx = sector_div(stripe2, raid_disks);
2074 qd_idx = (pd_idx + 1) % raid_disks;
2075 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
2076 break;
2077
2078 case ALGORITHM_PARITY_0:
2079 pd_idx = 0;
2080 qd_idx = 1;
2081 (*dd_idx) += 2;
2082 break;
2083 case ALGORITHM_PARITY_N:
2084 pd_idx = data_disks;
2085 qd_idx = data_disks + 1;
2086 break;
2087
2088 case ALGORITHM_ROTATING_ZERO_RESTART:
2089 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2090 * of blocks for computing Q is different.
2091 */
2092 pd_idx = sector_div(stripe2, raid_disks);
2093 qd_idx = pd_idx + 1;
2094 if (pd_idx == raid_disks-1) {
2095 (*dd_idx)++; /* Q D D D P */
2096 qd_idx = 0;
2097 } else if (*dd_idx >= pd_idx)
2098 (*dd_idx) += 2; /* D D P Q D */
2099 ddf_layout = 1;
2100 break;
2101
2102 case ALGORITHM_ROTATING_N_RESTART:
2103 /* Same a left_asymmetric, by first stripe is
2104 * D D D P Q rather than
2105 * Q D D D P
2106 */
2107 stripe2 += 1;
2108 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
2109 qd_idx = pd_idx + 1;
2110 if (pd_idx == raid_disks-1) {
2111 (*dd_idx)++; /* Q D D D P */
2112 qd_idx = 0;
2113 } else if (*dd_idx >= pd_idx)
2114 (*dd_idx) += 2; /* D D P Q D */
2115 ddf_layout = 1;
2116 break;
2117
2118 case ALGORITHM_ROTATING_N_CONTINUE:
2119 /* Same as left_symmetric but Q is before P */
2120 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
2121 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2122 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
2123 ddf_layout = 1;
2124 break;
2125
2126 case ALGORITHM_LEFT_ASYMMETRIC_6:
2127 /* RAID5 left_asymmetric, with Q on last device */
2128 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
2129 if (*dd_idx >= pd_idx)
2130 (*dd_idx)++;
2131 qd_idx = raid_disks - 1;
2132 break;
2133
2134 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2135 pd_idx = sector_div(stripe2, raid_disks-1);
2136 if (*dd_idx >= pd_idx)
2137 (*dd_idx)++;
2138 qd_idx = raid_disks - 1;
2139 break;
2140
2141 case ALGORITHM_LEFT_SYMMETRIC_6:
2142 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
2143 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2144 qd_idx = raid_disks - 1;
2145 break;
2146
2147 case ALGORITHM_RIGHT_SYMMETRIC_6:
2148 pd_idx = sector_div(stripe2, raid_disks-1);
2149 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2150 qd_idx = raid_disks - 1;
2151 break;
2152
2153 case ALGORITHM_PARITY_0_6:
2154 pd_idx = 0;
2155 (*dd_idx)++;
2156 qd_idx = raid_disks - 1;
2157 break;
2158
2159 default:
2160 BUG();
2161 }
2162 break;
2163 }
2164
2165 if (sh) {
2166 sh->pd_idx = pd_idx;
2167 sh->qd_idx = qd_idx;
2168 sh->ddf_layout = ddf_layout;
2169 }
2170 /*
2171 * Finally, compute the new sector number
2172 */
2173 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2174 return new_sector;
2175 }
2176
2177
2178 static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
2179 {
2180 struct r5conf *conf = sh->raid_conf;
2181 int raid_disks = sh->disks;
2182 int data_disks = raid_disks - conf->max_degraded;
2183 sector_t new_sector = sh->sector, check;
2184 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2185 : conf->chunk_sectors;
2186 int algorithm = previous ? conf->prev_algo
2187 : conf->algorithm;
2188 sector_t stripe;
2189 int chunk_offset;
2190 sector_t chunk_number;
2191 int dummy1, dd_idx = i;
2192 sector_t r_sector;
2193 struct stripe_head sh2;
2194
2195
2196 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2197 stripe = new_sector;
2198
2199 if (i == sh->pd_idx)
2200 return 0;
2201 switch(conf->level) {
2202 case 4: break;
2203 case 5:
2204 switch (algorithm) {
2205 case ALGORITHM_LEFT_ASYMMETRIC:
2206 case ALGORITHM_RIGHT_ASYMMETRIC:
2207 if (i > sh->pd_idx)
2208 i--;
2209 break;
2210 case ALGORITHM_LEFT_SYMMETRIC:
2211 case ALGORITHM_RIGHT_SYMMETRIC:
2212 if (i < sh->pd_idx)
2213 i += raid_disks;
2214 i -= (sh->pd_idx + 1);
2215 break;
2216 case ALGORITHM_PARITY_0:
2217 i -= 1;
2218 break;
2219 case ALGORITHM_PARITY_N:
2220 break;
2221 default:
2222 BUG();
2223 }
2224 break;
2225 case 6:
2226 if (i == sh->qd_idx)
2227 return 0; /* It is the Q disk */
2228 switch (algorithm) {
2229 case ALGORITHM_LEFT_ASYMMETRIC:
2230 case ALGORITHM_RIGHT_ASYMMETRIC:
2231 case ALGORITHM_ROTATING_ZERO_RESTART:
2232 case ALGORITHM_ROTATING_N_RESTART:
2233 if (sh->pd_idx == raid_disks-1)
2234 i--; /* Q D D D P */
2235 else if (i > sh->pd_idx)
2236 i -= 2; /* D D P Q D */
2237 break;
2238 case ALGORITHM_LEFT_SYMMETRIC:
2239 case ALGORITHM_RIGHT_SYMMETRIC:
2240 if (sh->pd_idx == raid_disks-1)
2241 i--; /* Q D D D P */
2242 else {
2243 /* D D P Q D */
2244 if (i < sh->pd_idx)
2245 i += raid_disks;
2246 i -= (sh->pd_idx + 2);
2247 }
2248 break;
2249 case ALGORITHM_PARITY_0:
2250 i -= 2;
2251 break;
2252 case ALGORITHM_PARITY_N:
2253 break;
2254 case ALGORITHM_ROTATING_N_CONTINUE:
2255 /* Like left_symmetric, but P is before Q */
2256 if (sh->pd_idx == 0)
2257 i--; /* P D D D Q */
2258 else {
2259 /* D D Q P D */
2260 if (i < sh->pd_idx)
2261 i += raid_disks;
2262 i -= (sh->pd_idx + 1);
2263 }
2264 break;
2265 case ALGORITHM_LEFT_ASYMMETRIC_6:
2266 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2267 if (i > sh->pd_idx)
2268 i--;
2269 break;
2270 case ALGORITHM_LEFT_SYMMETRIC_6:
2271 case ALGORITHM_RIGHT_SYMMETRIC_6:
2272 if (i < sh->pd_idx)
2273 i += data_disks + 1;
2274 i -= (sh->pd_idx + 1);
2275 break;
2276 case ALGORITHM_PARITY_0_6:
2277 i -= 1;
2278 break;
2279 default:
2280 BUG();
2281 }
2282 break;
2283 }
2284
2285 chunk_number = stripe * data_disks + i;
2286 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
2287
2288 check = raid5_compute_sector(conf, r_sector,
2289 previous, &dummy1, &sh2);
2290 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2291 || sh2.qd_idx != sh->qd_idx) {
2292 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2293 mdname(conf->mddev));
2294 return 0;
2295 }
2296 return r_sector;
2297 }
2298
2299
2300 static void
2301 schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
2302 int rcw, int expand)
2303 {
2304 int i, pd_idx = sh->pd_idx, disks = sh->disks;
2305 struct r5conf *conf = sh->raid_conf;
2306 int level = conf->level;
2307
2308 if (rcw) {
2309
2310 for (i = disks; i--; ) {
2311 struct r5dev *dev = &sh->dev[i];
2312
2313 if (dev->towrite) {
2314 set_bit(R5_LOCKED, &dev->flags);
2315 set_bit(R5_Wantdrain, &dev->flags);
2316 if (!expand)
2317 clear_bit(R5_UPTODATE, &dev->flags);
2318 s->locked++;
2319 }
2320 }
2321 /* if we are not expanding this is a proper write request, and
2322 * there will be bios with new data to be drained into the
2323 * stripe cache
2324 */
2325 if (!expand) {
2326 if (!s->locked)
2327 /* False alarm, nothing to do */
2328 return;
2329 sh->reconstruct_state = reconstruct_state_drain_run;
2330 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2331 } else
2332 sh->reconstruct_state = reconstruct_state_run;
2333
2334 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2335
2336 if (s->locked + conf->max_degraded == disks)
2337 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
2338 atomic_inc(&conf->pending_full_writes);
2339 } else {
2340 BUG_ON(level == 6);
2341 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2342 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2343
2344 for (i = disks; i--; ) {
2345 struct r5dev *dev = &sh->dev[i];
2346 if (i == pd_idx)
2347 continue;
2348
2349 if (dev->towrite &&
2350 (test_bit(R5_UPTODATE, &dev->flags) ||
2351 test_bit(R5_Wantcompute, &dev->flags))) {
2352 set_bit(R5_Wantdrain, &dev->flags);
2353 set_bit(R5_LOCKED, &dev->flags);
2354 clear_bit(R5_UPTODATE, &dev->flags);
2355 s->locked++;
2356 }
2357 }
2358 if (!s->locked)
2359 /* False alarm - nothing to do */
2360 return;
2361 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
2362 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2363 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2364 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2365 }
2366
2367 /* keep the parity disk(s) locked while asynchronous operations
2368 * are in flight
2369 */
2370 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2371 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2372 s->locked++;
2373
2374 if (level == 6) {
2375 int qd_idx = sh->qd_idx;
2376 struct r5dev *dev = &sh->dev[qd_idx];
2377
2378 set_bit(R5_LOCKED, &dev->flags);
2379 clear_bit(R5_UPTODATE, &dev->flags);
2380 s->locked++;
2381 }
2382
2383 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
2384 __func__, (unsigned long long)sh->sector,
2385 s->locked, s->ops_request);
2386 }
2387
2388 /*
2389 * Each stripe/dev can have one or more bion attached.
2390 * toread/towrite point to the first in a chain.
2391 * The bi_next chain must be in order.
2392 */
2393 static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2394 {
2395 struct bio **bip;
2396 struct r5conf *conf = sh->raid_conf;
2397 int firstwrite=0;
2398
2399 pr_debug("adding bi b#%llu to stripe s#%llu\n",
2400 (unsigned long long)bi->bi_sector,
2401 (unsigned long long)sh->sector);
2402
2403 /*
2404 * If several bio share a stripe. The bio bi_phys_segments acts as a
2405 * reference count to avoid race. The reference count should already be
2406 * increased before this function is called (for example, in
2407 * make_request()), so other bio sharing this stripe will not free the
2408 * stripe. If a stripe is owned by one stripe, the stripe lock will
2409 * protect it.
2410 */
2411 spin_lock_irq(&sh->stripe_lock);
2412 if (forwrite) {
2413 bip = &sh->dev[dd_idx].towrite;
2414 if (*bip == NULL)
2415 firstwrite = 1;
2416 } else
2417 bip = &sh->dev[dd_idx].toread;
2418 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2419 if (bio_end_sector(*bip) > bi->bi_sector)
2420 goto overlap;
2421 bip = & (*bip)->bi_next;
2422 }
2423 if (*bip && (*bip)->bi_sector < bio_end_sector(bi))
2424 goto overlap;
2425
2426 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
2427 if (*bip)
2428 bi->bi_next = *bip;
2429 *bip = bi;
2430 raid5_inc_bi_active_stripes(bi);
2431
2432 if (forwrite) {
2433 /* check if page is covered */
2434 sector_t sector = sh->dev[dd_idx].sector;
2435 for (bi=sh->dev[dd_idx].towrite;
2436 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2437 bi && bi->bi_sector <= sector;
2438 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2439 if (bio_end_sector(bi) >= sector)
2440 sector = bio_end_sector(bi);
2441 }
2442 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2443 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2444 }
2445
2446 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2447 (unsigned long long)(*bip)->bi_sector,
2448 (unsigned long long)sh->sector, dd_idx);
2449 spin_unlock_irq(&sh->stripe_lock);
2450
2451 if (conf->mddev->bitmap && firstwrite) {
2452 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2453 STRIPE_SECTORS, 0);
2454 sh->bm_seq = conf->seq_flush+1;
2455 set_bit(STRIPE_BIT_DELAY, &sh->state);
2456 }
2457 return 1;
2458
2459 overlap:
2460 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2461 spin_unlock_irq(&sh->stripe_lock);
2462 return 0;
2463 }
2464
2465 static void end_reshape(struct r5conf *conf);
2466
2467 static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
2468 struct stripe_head *sh)
2469 {
2470 int sectors_per_chunk =
2471 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
2472 int dd_idx;
2473 int chunk_offset = sector_div(stripe, sectors_per_chunk);
2474 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
2475
2476 raid5_compute_sector(conf,
2477 stripe * (disks - conf->max_degraded)
2478 *sectors_per_chunk + chunk_offset,
2479 previous,
2480 &dd_idx, sh);
2481 }
2482
2483 static void
2484 handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
2485 struct stripe_head_state *s, int disks,
2486 struct bio **return_bi)
2487 {
2488 int i;
2489 for (i = disks; i--; ) {
2490 struct bio *bi;
2491 int bitmap_end = 0;
2492
2493 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2494 struct md_rdev *rdev;
2495 rcu_read_lock();
2496 rdev = rcu_dereference(conf->disks[i].rdev);
2497 if (rdev && test_bit(In_sync, &rdev->flags))
2498 atomic_inc(&rdev->nr_pending);
2499 else
2500 rdev = NULL;
2501 rcu_read_unlock();
2502 if (rdev) {
2503 if (!rdev_set_badblocks(
2504 rdev,
2505 sh->sector,
2506 STRIPE_SECTORS, 0))
2507 md_error(conf->mddev, rdev);
2508 rdev_dec_pending(rdev, conf->mddev);
2509 }
2510 }
2511 spin_lock_irq(&sh->stripe_lock);
2512 /* fail all writes first */
2513 bi = sh->dev[i].towrite;
2514 sh->dev[i].towrite = NULL;
2515 spin_unlock_irq(&sh->stripe_lock);
2516 if (bi)
2517 bitmap_end = 1;
2518
2519 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2520 wake_up(&conf->wait_for_overlap);
2521
2522 while (bi && bi->bi_sector <
2523 sh->dev[i].sector + STRIPE_SECTORS) {
2524 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2525 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2526 if (!raid5_dec_bi_active_stripes(bi)) {
2527 md_write_end(conf->mddev);
2528 bi->bi_next = *return_bi;
2529 *return_bi = bi;
2530 }
2531 bi = nextbi;
2532 }
2533 if (bitmap_end)
2534 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2535 STRIPE_SECTORS, 0, 0);
2536 bitmap_end = 0;
2537 /* and fail all 'written' */
2538 bi = sh->dev[i].written;
2539 sh->dev[i].written = NULL;
2540 if (bi) bitmap_end = 1;
2541 while (bi && bi->bi_sector <
2542 sh->dev[i].sector + STRIPE_SECTORS) {
2543 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2544 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2545 if (!raid5_dec_bi_active_stripes(bi)) {
2546 md_write_end(conf->mddev);
2547 bi->bi_next = *return_bi;
2548 *return_bi = bi;
2549 }
2550 bi = bi2;
2551 }
2552
2553 /* fail any reads if this device is non-operational and
2554 * the data has not reached the cache yet.
2555 */
2556 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2557 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2558 test_bit(R5_ReadError, &sh->dev[i].flags))) {
2559 spin_lock_irq(&sh->stripe_lock);
2560 bi = sh->dev[i].toread;
2561 sh->dev[i].toread = NULL;
2562 spin_unlock_irq(&sh->stripe_lock);
2563 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2564 wake_up(&conf->wait_for_overlap);
2565 while (bi && bi->bi_sector <
2566 sh->dev[i].sector + STRIPE_SECTORS) {
2567 struct bio *nextbi =
2568 r5_next_bio(bi, sh->dev[i].sector);
2569 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2570 if (!raid5_dec_bi_active_stripes(bi)) {
2571 bi->bi_next = *return_bi;
2572 *return_bi = bi;
2573 }
2574 bi = nextbi;
2575 }
2576 }
2577 if (bitmap_end)
2578 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2579 STRIPE_SECTORS, 0, 0);
2580 /* If we were in the middle of a write the parity block might
2581 * still be locked - so just clear all R5_LOCKED flags
2582 */
2583 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2584 }
2585
2586 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2587 if (atomic_dec_and_test(&conf->pending_full_writes))
2588 md_wakeup_thread(conf->mddev->thread);
2589 }
2590
2591 static void
2592 handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
2593 struct stripe_head_state *s)
2594 {
2595 int abort = 0;
2596 int i;
2597
2598 clear_bit(STRIPE_SYNCING, &sh->state);
2599 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
2600 wake_up(&conf->wait_for_overlap);
2601 s->syncing = 0;
2602 s->replacing = 0;
2603 /* There is nothing more to do for sync/check/repair.
2604 * Don't even need to abort as that is handled elsewhere
2605 * if needed, and not always wanted e.g. if there is a known
2606 * bad block here.
2607 * For recover/replace we need to record a bad block on all
2608 * non-sync devices, or abort the recovery
2609 */
2610 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2611 /* During recovery devices cannot be removed, so
2612 * locking and refcounting of rdevs is not needed
2613 */
2614 for (i = 0; i < conf->raid_disks; i++) {
2615 struct md_rdev *rdev = conf->disks[i].rdev;
2616 if (rdev
2617 && !test_bit(Faulty, &rdev->flags)
2618 && !test_bit(In_sync, &rdev->flags)
2619 && !rdev_set_badblocks(rdev, sh->sector,
2620 STRIPE_SECTORS, 0))
2621 abort = 1;
2622 rdev = conf->disks[i].replacement;
2623 if (rdev
2624 && !test_bit(Faulty, &rdev->flags)
2625 && !test_bit(In_sync, &rdev->flags)
2626 && !rdev_set_badblocks(rdev, sh->sector,
2627 STRIPE_SECTORS, 0))
2628 abort = 1;
2629 }
2630 if (abort)
2631 conf->recovery_disabled =
2632 conf->mddev->recovery_disabled;
2633 }
2634 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
2635 }
2636
2637 static int want_replace(struct stripe_head *sh, int disk_idx)
2638 {
2639 struct md_rdev *rdev;
2640 int rv = 0;
2641 /* Doing recovery so rcu locking not required */
2642 rdev = sh->raid_conf->disks[disk_idx].replacement;
2643 if (rdev
2644 && !test_bit(Faulty, &rdev->flags)
2645 && !test_bit(In_sync, &rdev->flags)
2646 && (rdev->recovery_offset <= sh->sector
2647 || rdev->mddev->recovery_cp <= sh->sector))
2648 rv = 1;
2649
2650 return rv;
2651 }
2652
2653 /* fetch_block - checks the given member device to see if its data needs
2654 * to be read or computed to satisfy a request.
2655 *
2656 * Returns 1 when no more member devices need to be checked, otherwise returns
2657 * 0 to tell the loop in handle_stripe_fill to continue
2658 */
2659 static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2660 int disk_idx, int disks)
2661 {
2662 struct r5dev *dev = &sh->dev[disk_idx];
2663 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2664 &sh->dev[s->failed_num[1]] };
2665
2666 /* is the data in this block needed, and can we get it? */