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