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