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