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