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