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