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