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