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