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