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