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