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