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