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