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