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