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