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