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