1 /*
2 * raid10.c : Multiple Devices driver for Linux
3 *
4 * Copyright (C) 2000-2004 Neil Brown
5 *
6 * RAID-10 support for md.
7 *
8 * Base on code in raid1.c. See raid1.c for further copyright information.
9 *
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 */
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/blkdev.h>
24 #include <linux/module.h>
25 #include <linux/seq_file.h>
26 #include <linux/ratelimit.h>
27 #include <linux/kthread.h>
28 #include "md.h"
29 #include "raid10.h"
30 #include "raid0.h"
31 #include "bitmap.h"
33 /*
34 * RAID10 provides a combination of RAID0 and RAID1 functionality.
35 * The layout of data is defined by
36 * chunk_size
37 * raid_disks
38 * near_copies (stored in low byte of layout)
39 * far_copies (stored in second byte of layout)
40 * far_offset (stored in bit 16 of layout )
41 *
42 * The data to be stored is divided into chunks using chunksize.
43 * Each device is divided into far_copies sections.
44 * In each section, chunks are laid out in a style similar to raid0, but
45 * near_copies copies of each chunk is stored (each on a different drive).
46 * The starting device for each section is offset near_copies from the starting
47 * device of the previous section.
48 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
49 * drive.
50 * near_copies and far_copies must be at least one, and their product is at most
51 * raid_disks.
52 *
53 * If far_offset is true, then the far_copies are handled a bit differently.
54 * The copies are still in different stripes, but instead of be very far apart
55 * on disk, there are adjacent stripes.
56 */
58 /*
59 * Number of guaranteed r10bios in case of extreme VM load:
60 */
61 #define NR_RAID10_BIOS 256
63 /* when we get a read error on a read-only array, we redirect to another
64 * device without failing the first device, or trying to over-write to
65 * correct the read error. To keep track of bad blocks on a per-bio
66 * level, we store IO_BLOCKED in the appropriate 'bios' pointer
67 */
68 #define IO_BLOCKED ((struct bio *)1)
69 /* When we successfully write to a known bad-block, we need to remove the
70 * bad-block marking which must be done from process context. So we record
71 * the success by setting devs[n].bio to IO_MADE_GOOD
72 */
73 #define IO_MADE_GOOD ((struct bio *)2)
75 #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
77 /* When there are this many requests queued to be written by
78 * the raid10 thread, we become 'congested' to provide back-pressure
79 * for writeback.
80 */
81 static int max_queued_requests = 1024;
83 static void allow_barrier(struct r10conf *conf);
84 static void lower_barrier(struct r10conf *conf);
85 static int enough(struct r10conf *conf, int ignore);
86 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
87 int *skipped);
88 static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio);
89 static void end_reshape_write(struct bio *bio, int error);
90 static void end_reshape(struct r10conf *conf);
92 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
93 {
94 struct r10conf *conf = data;
95 int size = offsetof(struct r10bio, devs[conf->copies]);
97 /* allocate a r10bio with room for raid_disks entries in the
98 * bios array */
99 return kzalloc(size, gfp_flags);
100 }
102 static void r10bio_pool_free(void *r10_bio, void *data)
103 {
104 kfree(r10_bio);
105 }
107 /* Maximum size of each resync request */
108 #define RESYNC_BLOCK_SIZE (64*1024)
109 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
110 /* amount of memory to reserve for resync requests */
111 #define RESYNC_WINDOW (1024*1024)
112 /* maximum number of concurrent requests, memory permitting */
113 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
115 /*
116 * When performing a resync, we need to read and compare, so
117 * we need as many pages are there are copies.
118 * When performing a recovery, we need 2 bios, one for read,
119 * one for write (we recover only one drive per r10buf)
120 *
121 */
122 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
123 {
124 struct r10conf *conf = data;
125 struct page *page;
126 struct r10bio *r10_bio;
127 struct bio *bio;
128 int i, j;
129 int nalloc;
131 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
132 if (!r10_bio)
133 return NULL;
135 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
136 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
137 nalloc = conf->copies; /* resync */
138 else
139 nalloc = 2; /* recovery */
141 /*
142 * Allocate bios.
143 */
144 for (j = nalloc ; j-- ; ) {
145 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
146 if (!bio)
147 goto out_free_bio;
148 r10_bio->devs[j].bio = bio;
149 if (!conf->have_replacement)
150 continue;
151 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
152 if (!bio)
153 goto out_free_bio;
154 r10_bio->devs[j].repl_bio = bio;
155 }
156 /*
157 * Allocate RESYNC_PAGES data pages and attach them
158 * where needed.
159 */
160 for (j = 0 ; j < nalloc; j++) {
161 struct bio *rbio = r10_bio->devs[j].repl_bio;
162 bio = r10_bio->devs[j].bio;
163 for (i = 0; i < RESYNC_PAGES; i++) {
164 if (j > 0 && !test_bit(MD_RECOVERY_SYNC,
165 &conf->mddev->recovery)) {
166 /* we can share bv_page's during recovery
167 * and reshape */
168 struct bio *rbio = r10_bio->devs[0].bio;
169 page = rbio->bi_io_vec[i].bv_page;
170 get_page(page);
171 } else
172 page = alloc_page(gfp_flags);
173 if (unlikely(!page))
174 goto out_free_pages;
176 bio->bi_io_vec[i].bv_page = page;
177 if (rbio)
178 rbio->bi_io_vec[i].bv_page = page;
179 }
180 }
182 return r10_bio;
184 out_free_pages:
185 for ( ; i > 0 ; i--)
186 safe_put_page(bio->bi_io_vec[i-1].bv_page);
187 while (j--)
188 for (i = 0; i < RESYNC_PAGES ; i++)
189 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
190 j = 0;
191 out_free_bio:
192 for ( ; j < nalloc; j++) {
193 if (r10_bio->devs[j].bio)
194 bio_put(r10_bio->devs[j].bio);
195 if (r10_bio->devs[j].repl_bio)
196 bio_put(r10_bio->devs[j].repl_bio);
197 }
198 r10bio_pool_free(r10_bio, conf);
199 return NULL;
200 }
202 static void r10buf_pool_free(void *__r10_bio, void *data)
203 {
204 int i;
205 struct r10conf *conf = data;
206 struct r10bio *r10bio = __r10_bio;
207 int j;
209 for (j=0; j < conf->copies; j++) {
210 struct bio *bio = r10bio->devs[j].bio;
211 if (bio) {
212 for (i = 0; i < RESYNC_PAGES; i++) {
213 safe_put_page(bio->bi_io_vec[i].bv_page);
214 bio->bi_io_vec[i].bv_page = NULL;
215 }
216 bio_put(bio);
217 }
218 bio = r10bio->devs[j].repl_bio;
219 if (bio)
220 bio_put(bio);
221 }
222 r10bio_pool_free(r10bio, conf);
223 }
225 static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
226 {
227 int i;
229 for (i = 0; i < conf->copies; i++) {
230 struct bio **bio = & r10_bio->devs[i].bio;
231 if (!BIO_SPECIAL(*bio))
232 bio_put(*bio);
233 *bio = NULL;
234 bio = &r10_bio->devs[i].repl_bio;
235 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
236 bio_put(*bio);
237 *bio = NULL;
238 }
239 }
241 static void free_r10bio(struct r10bio *r10_bio)
242 {
243 struct r10conf *conf = r10_bio->mddev->private;
245 put_all_bios(conf, r10_bio);
246 mempool_free(r10_bio, conf->r10bio_pool);
247 }
249 static void put_buf(struct r10bio *r10_bio)
250 {
251 struct r10conf *conf = r10_bio->mddev->private;
253 mempool_free(r10_bio, conf->r10buf_pool);
255 lower_barrier(conf);
256 }
258 static void reschedule_retry(struct r10bio *r10_bio)
259 {
260 unsigned long flags;
261 struct mddev *mddev = r10_bio->mddev;
262 struct r10conf *conf = mddev->private;
264 spin_lock_irqsave(&conf->device_lock, flags);
265 list_add(&r10_bio->retry_list, &conf->retry_list);
266 conf->nr_queued ++;
267 spin_unlock_irqrestore(&conf->device_lock, flags);
269 /* wake up frozen array... */
270 wake_up(&conf->wait_barrier);
272 md_wakeup_thread(mddev->thread);
273 }
275 /*
276 * raid_end_bio_io() is called when we have finished servicing a mirrored
277 * operation and are ready to return a success/failure code to the buffer
278 * cache layer.
279 */
280 static void raid_end_bio_io(struct r10bio *r10_bio)
281 {
282 struct bio *bio = r10_bio->master_bio;
283 int done;
284 struct r10conf *conf = r10_bio->mddev->private;
286 if (bio->bi_phys_segments) {
287 unsigned long flags;
288 spin_lock_irqsave(&conf->device_lock, flags);
289 bio->bi_phys_segments--;
290 done = (bio->bi_phys_segments == 0);
291 spin_unlock_irqrestore(&conf->device_lock, flags);
292 } else
293 done = 1;
294 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
295 clear_bit(BIO_UPTODATE, &bio->bi_flags);
296 if (done) {
297 bio_endio(bio, 0);
298 /*
299 * Wake up any possible resync thread that waits for the device
300 * to go idle.
301 */
302 allow_barrier(conf);
303 }
304 free_r10bio(r10_bio);
305 }
307 /*
308 * Update disk head position estimator based on IRQ completion info.
309 */
310 static inline void update_head_pos(int slot, struct r10bio *r10_bio)
311 {
312 struct r10conf *conf = r10_bio->mddev->private;
314 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
315 r10_bio->devs[slot].addr + (r10_bio->sectors);
316 }
318 /*
319 * Find the disk number which triggered given bio
320 */
321 static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
322 struct bio *bio, int *slotp, int *replp)
323 {
324 int slot;
325 int repl = 0;
327 for (slot = 0; slot < conf->copies; slot++) {
328 if (r10_bio->devs[slot].bio == bio)
329 break;
330 if (r10_bio->devs[slot].repl_bio == bio) {
331 repl = 1;
332 break;
333 }
334 }
336 BUG_ON(slot == conf->copies);
337 update_head_pos(slot, r10_bio);
339 if (slotp)
340 *slotp = slot;
341 if (replp)
342 *replp = repl;
343 return r10_bio->devs[slot].devnum;
344 }
346 static void raid10_end_read_request(struct bio *bio, int error)
347 {
348 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
349 struct r10bio *r10_bio = bio->bi_private;
350 int slot, dev;
351 struct md_rdev *rdev;
352 struct r10conf *conf = r10_bio->mddev->private;
355 slot = r10_bio->read_slot;
356 dev = r10_bio->devs[slot].devnum;
357 rdev = r10_bio->devs[slot].rdev;
358 /*
359 * this branch is our 'one mirror IO has finished' event handler:
360 */
361 update_head_pos(slot, r10_bio);
363 if (uptodate) {
364 /*
365 * Set R10BIO_Uptodate in our master bio, so that
366 * we will return a good error code to the higher
367 * levels even if IO on some other mirrored buffer fails.
368 *
369 * The 'master' represents the composite IO operation to
370 * user-side. So if something waits for IO, then it will
371 * wait for the 'master' bio.
372 */
373 set_bit(R10BIO_Uptodate, &r10_bio->state);
374 } else {
375 /* If all other devices that store this block have
376 * failed, we want to return the error upwards rather
377 * than fail the last device. Here we redefine
378 * "uptodate" to mean "Don't want to retry"
379 */
380 unsigned long flags;
381 spin_lock_irqsave(&conf->device_lock, flags);
382 if (!enough(conf, rdev->raid_disk))
383 uptodate = 1;
384 spin_unlock_irqrestore(&conf->device_lock, flags);
385 }
386 if (uptodate) {
387 raid_end_bio_io(r10_bio);
388 rdev_dec_pending(rdev, conf->mddev);
389 } else {
390 /*
391 * oops, read error - keep the refcount on the rdev
392 */
393 char b[BDEVNAME_SIZE];
394 printk_ratelimited(KERN_ERR
395 "md/raid10:%s: %s: rescheduling sector %llu\n",
396 mdname(conf->mddev),
397 bdevname(rdev->bdev, b),
398 (unsigned long long)r10_bio->sector);
399 set_bit(R10BIO_ReadError, &r10_bio->state);
400 reschedule_retry(r10_bio);
401 }
402 }
404 static void close_write(struct r10bio *r10_bio)
405 {
406 /* clear the bitmap if all writes complete successfully */
407 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
408 r10_bio->sectors,
409 !test_bit(R10BIO_Degraded, &r10_bio->state),
410 0);
411 md_write_end(r10_bio->mddev);
412 }
414 static void one_write_done(struct r10bio *r10_bio)
415 {
416 if (atomic_dec_and_test(&r10_bio->remaining)) {
417 if (test_bit(R10BIO_WriteError, &r10_bio->state))
418 reschedule_retry(r10_bio);
419 else {
420 close_write(r10_bio);
421 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
422 reschedule_retry(r10_bio);
423 else
424 raid_end_bio_io(r10_bio);
425 }
426 }
427 }
429 static void raid10_end_write_request(struct bio *bio, int error)
430 {
431 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
432 struct r10bio *r10_bio = bio->bi_private;
433 int dev;
434 int dec_rdev = 1;
435 struct r10conf *conf = r10_bio->mddev->private;
436 int slot, repl;
437 struct md_rdev *rdev = NULL;
439 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
441 if (repl)
442 rdev = conf->mirrors[dev].replacement;
443 if (!rdev) {
444 smp_rmb();
445 repl = 0;
446 rdev = conf->mirrors[dev].rdev;
447 }
448 /*
449 * this branch is our 'one mirror IO has finished' event handler:
450 */
451 if (!uptodate) {
452 if (repl)
453 /* Never record new bad blocks to replacement,
454 * just fail it.
455 */
456 md_error(rdev->mddev, rdev);
457 else {
458 set_bit(WriteErrorSeen, &rdev->flags);
459 if (!test_and_set_bit(WantReplacement, &rdev->flags))
460 set_bit(MD_RECOVERY_NEEDED,
461 &rdev->mddev->recovery);
462 set_bit(R10BIO_WriteError, &r10_bio->state);
463 dec_rdev = 0;
464 }
465 } else {
466 /*
467 * Set R10BIO_Uptodate in our master bio, so that
468 * we will return a good error code for to the higher
469 * levels even if IO on some other mirrored buffer fails.
470 *
471 * The 'master' represents the composite IO operation to
472 * user-side. So if something waits for IO, then it will
473 * wait for the 'master' bio.
474 */
475 sector_t first_bad;
476 int bad_sectors;
478 set_bit(R10BIO_Uptodate, &r10_bio->state);
480 /* Maybe we can clear some bad blocks. */
481 if (is_badblock(rdev,
482 r10_bio->devs[slot].addr,
483 r10_bio->sectors,
484 &first_bad, &bad_sectors)) {
485 bio_put(bio);
486 if (repl)
487 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
488 else
489 r10_bio->devs[slot].bio = IO_MADE_GOOD;
490 dec_rdev = 0;
491 set_bit(R10BIO_MadeGood, &r10_bio->state);
492 }
493 }
495 /*
496 *
497 * Let's see if all mirrored write operations have finished
498 * already.
499 */
500 one_write_done(r10_bio);
501 if (dec_rdev)
502 rdev_dec_pending(rdev, conf->mddev);
503 }
505 /*
506 * RAID10 layout manager
507 * As well as the chunksize and raid_disks count, there are two
508 * parameters: near_copies and far_copies.
509 * near_copies * far_copies must be <= raid_disks.
510 * Normally one of these will be 1.
511 * If both are 1, we get raid0.
512 * If near_copies == raid_disks, we get raid1.
513 *
514 * Chunks are laid out in raid0 style with near_copies copies of the
515 * first chunk, followed by near_copies copies of the next chunk and
516 * so on.
517 * If far_copies > 1, then after 1/far_copies of the array has been assigned
518 * as described above, we start again with a device offset of near_copies.
519 * So we effectively have another copy of the whole array further down all
520 * the drives, but with blocks on different drives.
521 * With this layout, and block is never stored twice on the one device.
522 *
523 * raid10_find_phys finds the sector offset of a given virtual sector
524 * on each device that it is on.
525 *
526 * raid10_find_virt does the reverse mapping, from a device and a
527 * sector offset to a virtual address
528 */
530 static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
531 {
532 int n,f;
533 sector_t sector;
534 sector_t chunk;
535 sector_t stripe;
536 int dev;
537 int slot = 0;
539 /* now calculate first sector/dev */
540 chunk = r10bio->sector >> geo->chunk_shift;
541 sector = r10bio->sector & geo->chunk_mask;
543 chunk *= geo->near_copies;
544 stripe = chunk;
545 dev = sector_div(stripe, geo->raid_disks);
546 if (geo->far_offset)
547 stripe *= geo->far_copies;
549 sector += stripe << geo->chunk_shift;
551 /* and calculate all the others */
552 for (n = 0; n < geo->near_copies; n++) {
553 int d = dev;
554 sector_t s = sector;
555 r10bio->devs[slot].addr = sector;
556 r10bio->devs[slot].devnum = d;
557 slot++;
559 for (f = 1; f < geo->far_copies; f++) {
560 d += geo->near_copies;
561 if (d >= geo->raid_disks)
562 d -= geo->raid_disks;
563 s += geo->stride;
564 r10bio->devs[slot].devnum = d;
565 r10bio->devs[slot].addr = s;
566 slot++;
567 }
568 dev++;
569 if (dev >= geo->raid_disks) {
570 dev = 0;
571 sector += (geo->chunk_mask + 1);
572 }
573 }
574 }
576 static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
577 {
578 struct geom *geo = &conf->geo;
580 if (conf->reshape_progress != MaxSector &&
581 ((r10bio->sector >= conf->reshape_progress) !=
582 conf->mddev->reshape_backwards)) {
583 set_bit(R10BIO_Previous, &r10bio->state);
584 geo = &conf->prev;
585 } else
586 clear_bit(R10BIO_Previous, &r10bio->state);
588 __raid10_find_phys(geo, r10bio);
589 }
591 static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
592 {
593 sector_t offset, chunk, vchunk;
594 /* Never use conf->prev as this is only called during resync
595 * or recovery, so reshape isn't happening
596 */
597 struct geom *geo = &conf->geo;
599 offset = sector & geo->chunk_mask;
600 if (geo->far_offset) {
601 int fc;
602 chunk = sector >> geo->chunk_shift;
603 fc = sector_div(chunk, geo->far_copies);
604 dev -= fc * geo->near_copies;
605 if (dev < 0)
606 dev += geo->raid_disks;
607 } else {
608 while (sector >= geo->stride) {
609 sector -= geo->stride;
610 if (dev < geo->near_copies)
611 dev += geo->raid_disks - geo->near_copies;
612 else
613 dev -= geo->near_copies;
614 }
615 chunk = sector >> geo->chunk_shift;
616 }
617 vchunk = chunk * geo->raid_disks + dev;
618 sector_div(vchunk, geo->near_copies);
619 return (vchunk << geo->chunk_shift) + offset;
620 }
622 /**
623 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
624 * @q: request queue
625 * @bvm: properties of new bio
626 * @biovec: the request that could be merged to it.
627 *
628 * Return amount of bytes we can accept at this offset
629 * This requires checking for end-of-chunk if near_copies != raid_disks,
630 * and for subordinate merge_bvec_fns if merge_check_needed.
631 */
632 static int raid10_mergeable_bvec(struct request_queue *q,
633 struct bvec_merge_data *bvm,
634 struct bio_vec *biovec)
635 {
636 struct mddev *mddev = q->queuedata;
637 struct r10conf *conf = mddev->private;
638 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
639 int max;
640 unsigned int chunk_sectors;
641 unsigned int bio_sectors = bvm->bi_size >> 9;
642 struct geom *geo = &conf->geo;
644 chunk_sectors = (conf->geo.chunk_mask & conf->prev.chunk_mask) + 1;
645 if (conf->reshape_progress != MaxSector &&
646 ((sector >= conf->reshape_progress) !=
647 conf->mddev->reshape_backwards))
648 geo = &conf->prev;
650 if (geo->near_copies < geo->raid_disks) {
651 max = (chunk_sectors - ((sector & (chunk_sectors - 1))
652 + bio_sectors)) << 9;
653 if (max < 0)
654 /* bio_add cannot handle a negative return */
655 max = 0;
656 if (max <= biovec->bv_len && bio_sectors == 0)
657 return biovec->bv_len;
658 } else
659 max = biovec->bv_len;
661 if (mddev->merge_check_needed) {
662 struct {
663 struct r10bio r10_bio;
664 struct r10dev devs[conf->copies];
665 } on_stack;
666 struct r10bio *r10_bio = &on_stack.r10_bio;
667 int s;
668 if (conf->reshape_progress != MaxSector) {
669 /* Cannot give any guidance during reshape */
670 if (max <= biovec->bv_len && bio_sectors == 0)
671 return biovec->bv_len;
672 return 0;
673 }
674 r10_bio->sector = sector;
675 raid10_find_phys(conf, r10_bio);
676 rcu_read_lock();
677 for (s = 0; s < conf->copies; s++) {
678 int disk = r10_bio->devs[s].devnum;
679 struct md_rdev *rdev = rcu_dereference(
680 conf->mirrors[disk].rdev);
681 if (rdev && !test_bit(Faulty, &rdev->flags)) {
682 struct request_queue *q =
683 bdev_get_queue(rdev->bdev);
684 if (q->merge_bvec_fn) {
685 bvm->bi_sector = r10_bio->devs[s].addr
686 + rdev->data_offset;
687 bvm->bi_bdev = rdev->bdev;
688 max = min(max, q->merge_bvec_fn(
689 q, bvm, biovec));
690 }
691 }
692 rdev = rcu_dereference(conf->mirrors[disk].replacement);
693 if (rdev && !test_bit(Faulty, &rdev->flags)) {
694 struct request_queue *q =
695 bdev_get_queue(rdev->bdev);
696 if (q->merge_bvec_fn) {
697 bvm->bi_sector = r10_bio->devs[s].addr
698 + rdev->data_offset;
699 bvm->bi_bdev = rdev->bdev;
700 max = min(max, q->merge_bvec_fn(
701 q, bvm, biovec));
702 }
703 }
704 }
705 rcu_read_unlock();
706 }
707 return max;
708 }
710 /*
711 * This routine returns the disk from which the requested read should
712 * be done. There is a per-array 'next expected sequential IO' sector
713 * number - if this matches on the next IO then we use the last disk.
714 * There is also a per-disk 'last know head position' sector that is
715 * maintained from IRQ contexts, both the normal and the resync IO
716 * completion handlers update this position correctly. If there is no
717 * perfect sequential match then we pick the disk whose head is closest.
718 *
719 * If there are 2 mirrors in the same 2 devices, performance degrades
720 * because position is mirror, not device based.
721 *
722 * The rdev for the device selected will have nr_pending incremented.
723 */
725 /*
726 * FIXME: possibly should rethink readbalancing and do it differently
727 * depending on near_copies / far_copies geometry.
728 */
729 static struct md_rdev *read_balance(struct r10conf *conf,
730 struct r10bio *r10_bio,
731 int *max_sectors)
732 {
733 const sector_t this_sector = r10_bio->sector;
734 int disk, slot;
735 int sectors = r10_bio->sectors;
736 int best_good_sectors;
737 sector_t new_distance, best_dist;
738 struct md_rdev *best_rdev, *rdev = NULL;
739 int do_balance;
740 int best_slot;
741 struct geom *geo = &conf->geo;
743 raid10_find_phys(conf, r10_bio);
744 rcu_read_lock();
745 retry:
746 sectors = r10_bio->sectors;
747 best_slot = -1;
748 best_rdev = NULL;
749 best_dist = MaxSector;
750 best_good_sectors = 0;
751 do_balance = 1;
752 /*
753 * Check if we can balance. We can balance on the whole
754 * device if no resync is going on (recovery is ok), or below
755 * the resync window. We take the first readable disk when
756 * above the resync window.
757 */
758 if (conf->mddev->recovery_cp < MaxSector
759 && (this_sector + sectors >= conf->next_resync))
760 do_balance = 0;
762 for (slot = 0; slot < conf->copies ; slot++) {
763 sector_t first_bad;
764 int bad_sectors;
765 sector_t dev_sector;
767 if (r10_bio->devs[slot].bio == IO_BLOCKED)
768 continue;
769 disk = r10_bio->devs[slot].devnum;
770 rdev = rcu_dereference(conf->mirrors[disk].replacement);
771 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
772 test_bit(Unmerged, &rdev->flags) ||
773 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
774 rdev = rcu_dereference(conf->mirrors[disk].rdev);
775 if (rdev == NULL ||
776 test_bit(Faulty, &rdev->flags) ||
777 test_bit(Unmerged, &rdev->flags))
778 continue;
779 if (!test_bit(In_sync, &rdev->flags) &&
780 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
781 continue;
783 dev_sector = r10_bio->devs[slot].addr;
784 if (is_badblock(rdev, dev_sector, sectors,
785 &first_bad, &bad_sectors)) {
786 if (best_dist < MaxSector)
787 /* Already have a better slot */
788 continue;
789 if (first_bad <= dev_sector) {
790 /* Cannot read here. If this is the
791 * 'primary' device, then we must not read
792 * beyond 'bad_sectors' from another device.
793 */
794 bad_sectors -= (dev_sector - first_bad);
795 if (!do_balance && sectors > bad_sectors)
796 sectors = bad_sectors;
797 if (best_good_sectors > sectors)
798 best_good_sectors = sectors;
799 } else {
800 sector_t good_sectors =
801 first_bad - dev_sector;
802 if (good_sectors > best_good_sectors) {
803 best_good_sectors = good_sectors;
804 best_slot = slot;
805 best_rdev = rdev;
806 }
807 if (!do_balance)
808 /* Must read from here */
809 break;
810 }
811 continue;
812 } else
813 best_good_sectors = sectors;
815 if (!do_balance)
816 break;
818 /* This optimisation is debatable, and completely destroys
819 * sequential read speed for 'far copies' arrays. So only
820 * keep it for 'near' arrays, and review those later.
821 */
822 if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending))
823 break;
825 /* for far > 1 always use the lowest address */
826 if (geo->far_copies > 1)
827 new_distance = r10_bio->devs[slot].addr;
828 else
829 new_distance = abs(r10_bio->devs[slot].addr -
830 conf->mirrors[disk].head_position);
831 if (new_distance < best_dist) {
832 best_dist = new_distance;
833 best_slot = slot;
834 best_rdev = rdev;
835 }
836 }
837 if (slot >= conf->copies) {
838 slot = best_slot;
839 rdev = best_rdev;
840 }
842 if (slot >= 0) {
843 atomic_inc(&rdev->nr_pending);
844 if (test_bit(Faulty, &rdev->flags)) {
845 /* Cannot risk returning a device that failed
846 * before we inc'ed nr_pending
847 */
848 rdev_dec_pending(rdev, conf->mddev);
849 goto retry;
850 }
851 r10_bio->read_slot = slot;
852 } else
853 rdev = NULL;
854 rcu_read_unlock();
855 *max_sectors = best_good_sectors;
857 return rdev;
858 }
860 int md_raid10_congested(struct mddev *mddev, int bits)
861 {
862 struct r10conf *conf = mddev->private;
863 int i, ret = 0;
865 if ((bits & (1 << BDI_async_congested)) &&
866 conf->pending_count >= max_queued_requests)
867 return 1;
869 rcu_read_lock();
870 for (i = 0;
871 (i < conf->geo.raid_disks || i < conf->prev.raid_disks)
872 && ret == 0;
873 i++) {
874 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
875 if (rdev && !test_bit(Faulty, &rdev->flags)) {
876 struct request_queue *q = bdev_get_queue(rdev->bdev);
878 ret |= bdi_congested(&q->backing_dev_info, bits);
879 }
880 }
881 rcu_read_unlock();
882 return ret;
883 }
884 EXPORT_SYMBOL_GPL(md_raid10_congested);
886 static int raid10_congested(void *data, int bits)
887 {
888 struct mddev *mddev = data;
890 return mddev_congested(mddev, bits) ||
891 md_raid10_congested(mddev, bits);
892 }
894 static void flush_pending_writes(struct r10conf *conf)
895 {
896 /* Any writes that have been queued but are awaiting
897 * bitmap updates get flushed here.
898 */
899 spin_lock_irq(&conf->device_lock);
901 if (conf->pending_bio_list.head) {
902 struct bio *bio;
903 bio = bio_list_get(&conf->pending_bio_list);
904 conf->pending_count = 0;
905 spin_unlock_irq(&conf->device_lock);
906 /* flush any pending bitmap writes to disk
907 * before proceeding w/ I/O */
908 bitmap_unplug(conf->mddev->bitmap);
909 wake_up(&conf->wait_barrier);
911 while (bio) { /* submit pending writes */
912 struct bio *next = bio->bi_next;
913 bio->bi_next = NULL;
914 if (unlikely((bio->bi_rw & REQ_DISCARD) &&
915 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
916 /* Just ignore it */
917 bio_endio(bio, 0);
918 else
919 generic_make_request(bio);
920 bio = next;
921 }
922 } else
923 spin_unlock_irq(&conf->device_lock);
924 }
926 /* Barriers....
927 * Sometimes we need to suspend IO while we do something else,
928 * either some resync/recovery, or reconfigure the array.
929 * To do this we raise a 'barrier'.
930 * The 'barrier' is a counter that can be raised multiple times
931 * to count how many activities are happening which preclude
932 * normal IO.
933 * We can only raise the barrier if there is no pending IO.
934 * i.e. if nr_pending == 0.
935 * We choose only to raise the barrier if no-one is waiting for the
936 * barrier to go down. This means that as soon as an IO request
937 * is ready, no other operations which require a barrier will start
938 * until the IO request has had a chance.
939 *
940 * So: regular IO calls 'wait_barrier'. When that returns there
941 * is no backgroup IO happening, It must arrange to call
942 * allow_barrier when it has finished its IO.
943 * backgroup IO calls must call raise_barrier. Once that returns
944 * there is no normal IO happeing. It must arrange to call
945 * lower_barrier when the particular background IO completes.
946 */
948 static void raise_barrier(struct r10conf *conf, int force)
949 {
950 BUG_ON(force && !conf->barrier);
951 spin_lock_irq(&conf->resync_lock);
953 /* Wait until no block IO is waiting (unless 'force') */
954 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
955 conf->resync_lock);
957 /* block any new IO from starting */
958 conf->barrier++;
960 /* Now wait for all pending IO to complete */
961 wait_event_lock_irq(conf->wait_barrier,
962 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
963 conf->resync_lock);
965 spin_unlock_irq(&conf->resync_lock);
966 }
968 static void lower_barrier(struct r10conf *conf)
969 {
970 unsigned long flags;
971 spin_lock_irqsave(&conf->resync_lock, flags);
972 conf->barrier--;
973 spin_unlock_irqrestore(&conf->resync_lock, flags);
974 wake_up(&conf->wait_barrier);
975 }
977 static void wait_barrier(struct r10conf *conf)
978 {
979 spin_lock_irq(&conf->resync_lock);
980 if (conf->barrier) {
981 conf->nr_waiting++;
982 /* Wait for the barrier to drop.
983 * However if there are already pending
984 * requests (preventing the barrier from
985 * rising completely), and the
986 * pre-process bio queue isn't empty,
987 * then don't wait, as we need to empty
988 * that queue to get the nr_pending
989 * count down.
990 */
991 wait_event_lock_irq(conf->wait_barrier,
992 !conf->barrier ||
993 (conf->nr_pending &&
994 current->bio_list &&
995 !bio_list_empty(current->bio_list)),
996 conf->resync_lock);
997 conf->nr_waiting--;
998 }
999 conf->nr_pending++;
1000 spin_unlock_irq(&conf->resync_lock);
1001 }
1003 static void allow_barrier(struct r10conf *conf)
1004 {
1005 unsigned long flags;
1006 spin_lock_irqsave(&conf->resync_lock, flags);
1007 conf->nr_pending--;
1008 spin_unlock_irqrestore(&conf->resync_lock, flags);
1009 wake_up(&conf->wait_barrier);
1010 }
1012 static void freeze_array(struct r10conf *conf)
1013 {
1014 /* stop syncio and normal IO and wait for everything to
1015 * go quiet.
1016 * We increment barrier and nr_waiting, and then
1017 * wait until nr_pending match nr_queued+1
1018 * This is called in the context of one normal IO request
1019 * that has failed. Thus any sync request that might be pending
1020 * will be blocked by nr_pending, and we need to wait for
1021 * pending IO requests to complete or be queued for re-try.
1022 * Thus the number queued (nr_queued) plus this request (1)
1023 * must match the number of pending IOs (nr_pending) before
1024 * we continue.
1025 */
1026 spin_lock_irq(&conf->resync_lock);
1027 conf->barrier++;
1028 conf->nr_waiting++;
1029 wait_event_lock_irq_cmd(conf->wait_barrier,
1030 conf->nr_pending == conf->nr_queued+1,
1031 conf->resync_lock,
1032 flush_pending_writes(conf));
1034 spin_unlock_irq(&conf->resync_lock);
1035 }
1037 static void unfreeze_array(struct r10conf *conf)
1038 {
1039 /* reverse the effect of the freeze */
1040 spin_lock_irq(&conf->resync_lock);
1041 conf->barrier--;
1042 conf->nr_waiting--;
1043 wake_up(&conf->wait_barrier);
1044 spin_unlock_irq(&conf->resync_lock);
1045 }
1047 static sector_t choose_data_offset(struct r10bio *r10_bio,
1048 struct md_rdev *rdev)
1049 {
1050 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1051 test_bit(R10BIO_Previous, &r10_bio->state))
1052 return rdev->data_offset;
1053 else
1054 return rdev->new_data_offset;
1055 }
1057 struct raid10_plug_cb {
1058 struct blk_plug_cb cb;
1059 struct bio_list pending;
1060 int pending_cnt;
1061 };
1063 static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1064 {
1065 struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb,
1066 cb);
1067 struct mddev *mddev = plug->cb.data;
1068 struct r10conf *conf = mddev->private;
1069 struct bio *bio;
1071 if (from_schedule || current->bio_list) {
1072 spin_lock_irq(&conf->device_lock);
1073 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1074 conf->pending_count += plug->pending_cnt;
1075 spin_unlock_irq(&conf->device_lock);
1076 wake_up(&conf->wait_barrier);
1077 md_wakeup_thread(mddev->thread);
1078 kfree(plug);
1079 return;
1080 }
1082 /* we aren't scheduling, so we can do the write-out directly. */
1083 bio = bio_list_get(&plug->pending);
1084 bitmap_unplug(mddev->bitmap);
1085 wake_up(&conf->wait_barrier);
1087 while (bio) { /* submit pending writes */
1088 struct bio *next = bio->bi_next;
1089 bio->bi_next = NULL;
1090 if (unlikely((bio->bi_rw & REQ_DISCARD) &&
1091 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
1092 /* Just ignore it */
1093 bio_endio(bio, 0);
1094 else
1095 generic_make_request(bio);
1096 bio = next;
1097 }
1098 kfree(plug);
1099 }
1101 static void make_request(struct mddev *mddev, struct bio * bio)
1102 {
1103 struct r10conf *conf = mddev->private;
1104 struct r10bio *r10_bio;
1105 struct bio *read_bio;
1106 int i;
1107 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
1108 int chunk_sects = chunk_mask + 1;
1109 const int rw = bio_data_dir(bio);
1110 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
1111 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
1112 const unsigned long do_discard = (bio->bi_rw
1113 & (REQ_DISCARD | REQ_SECURE));
1114 const unsigned long do_same = (bio->bi_rw & REQ_WRITE_SAME);
1115 unsigned long flags;
1116 struct md_rdev *blocked_rdev;
1117 struct blk_plug_cb *cb;
1118 struct raid10_plug_cb *plug = NULL;
1119 int sectors_handled;
1120 int max_sectors;
1121 int sectors;
1123 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
1124 md_flush_request(mddev, bio);
1125 return;
1126 }
1128 /* If this request crosses a chunk boundary, we need to
1129 * split it. This will only happen for 1 PAGE (or less) requests.
1130 */
1131 if (unlikely((bio->bi_sector & chunk_mask) + (bio->bi_size >> 9)
1132 > chunk_sects
1133 && (conf->geo.near_copies < conf->geo.raid_disks
1134 || conf->prev.near_copies < conf->prev.raid_disks))) {
1135 struct bio_pair *bp;
1136 /* Sanity check -- queue functions should prevent this happening */
1137 if ((bio->bi_vcnt != 1 && bio->bi_vcnt != 0) ||
1138 bio->bi_idx != 0)
1139 goto bad_map;
1140 /* This is a one page bio that upper layers
1141 * refuse to split for us, so we need to split it.
1142 */
1143 bp = bio_split(bio,
1144 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
1146 /* Each of these 'make_request' calls will call 'wait_barrier'.
1147 * If the first succeeds but the second blocks due to the resync
1148 * thread raising the barrier, we will deadlock because the
1149 * IO to the underlying device will be queued in generic_make_request
1150 * and will never complete, so will never reduce nr_pending.
1151 * So increment nr_waiting here so no new raise_barriers will
1152 * succeed, and so the second wait_barrier cannot block.
1153 */
1154 spin_lock_irq(&conf->resync_lock);
1155 conf->nr_waiting++;
1156 spin_unlock_irq(&conf->resync_lock);
1158 make_request(mddev, &bp->bio1);
1159 make_request(mddev, &bp->bio2);
1161 spin_lock_irq(&conf->resync_lock);
1162 conf->nr_waiting--;
1163 wake_up(&conf->wait_barrier);
1164 spin_unlock_irq(&conf->resync_lock);
1166 bio_pair_release(bp);
1167 return;
1168 bad_map:
1169 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
1170 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
1171 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
1173 bio_io_error(bio);
1174 return;
1175 }
1177 md_write_start(mddev, bio);
1179 /*
1180 * Register the new request and wait if the reconstruction
1181 * thread has put up a bar for new requests.
1182 * Continue immediately if no resync is active currently.
1183 */
1184 wait_barrier(conf);
1186 sectors = bio->bi_size >> 9;
1187 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1188 bio->bi_sector < conf->reshape_progress &&
1189 bio->bi_sector + sectors > conf->reshape_progress) {
1190 /* IO spans the reshape position. Need to wait for
1191 * reshape to pass
1192 */
1193 allow_barrier(conf);
1194 wait_event(conf->wait_barrier,
1195 conf->reshape_progress <= bio->bi_sector ||
1196 conf->reshape_progress >= bio->bi_sector + sectors);
1197 wait_barrier(conf);
1198 }
1199 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1200 bio_data_dir(bio) == WRITE &&
1201 (mddev->reshape_backwards
1202 ? (bio->bi_sector < conf->reshape_safe &&
1203 bio->bi_sector + sectors > conf->reshape_progress)
1204 : (bio->bi_sector + sectors > conf->reshape_safe &&
1205 bio->bi_sector < conf->reshape_progress))) {
1206 /* Need to update reshape_position in metadata */
1207 mddev->reshape_position = conf->reshape_progress;
1208 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1209 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1210 md_wakeup_thread(mddev->thread);
1211 wait_event(mddev->sb_wait,
1212 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
1214 conf->reshape_safe = mddev->reshape_position;
1215 }
1217 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1219 r10_bio->master_bio = bio;
1220 r10_bio->sectors = sectors;
1222 r10_bio->mddev = mddev;
1223 r10_bio->sector = bio->bi_sector;
1224 r10_bio->state = 0;
1226 /* We might need to issue multiple reads to different
1227 * devices if there are bad blocks around, so we keep
1228 * track of the number of reads in bio->bi_phys_segments.
1229 * If this is 0, there is only one r10_bio and no locking
1230 * will be needed when the request completes. If it is
1231 * non-zero, then it is the number of not-completed requests.
1232 */
1233 bio->bi_phys_segments = 0;
1234 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
1236 if (rw == READ) {
1237 /*
1238 * read balancing logic:
1239 */
1240 struct md_rdev *rdev;
1241 int slot;
1243 read_again:
1244 rdev = read_balance(conf, r10_bio, &max_sectors);
1245 if (!rdev) {
1246 raid_end_bio_io(r10_bio);
1247 return;
1248 }
1249 slot = r10_bio->read_slot;
1251 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1252 md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
1253 max_sectors);
1255 r10_bio->devs[slot].bio = read_bio;
1256 r10_bio->devs[slot].rdev = rdev;
1258 read_bio->bi_sector = r10_bio->devs[slot].addr +
1259 choose_data_offset(r10_bio, rdev);
1260 read_bio->bi_bdev = rdev->bdev;
1261 read_bio->bi_end_io = raid10_end_read_request;
1262 read_bio->bi_rw = READ | do_sync;
1263 read_bio->bi_private = r10_bio;
1265 if (max_sectors < r10_bio->sectors) {
1266 /* Could not read all from this device, so we will
1267 * need another r10_bio.
1268 */
1269 sectors_handled = (r10_bio->sectors + max_sectors
1270 - bio->bi_sector);
1271 r10_bio->sectors = max_sectors;
1272 spin_lock_irq(&conf->device_lock);
1273 if (bio->bi_phys_segments == 0)
1274 bio->bi_phys_segments = 2;
1275 else
1276 bio->bi_phys_segments++;
1277 spin_unlock(&conf->device_lock);
1278 /* Cannot call generic_make_request directly
1279 * as that will be queued in __generic_make_request
1280 * and subsequent mempool_alloc might block
1281 * waiting for it. so hand bio over to raid10d.
1282 */
1283 reschedule_retry(r10_bio);
1285 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1287 r10_bio->master_bio = bio;
1288 r10_bio->sectors = ((bio->bi_size >> 9)
1289 - sectors_handled);
1290 r10_bio->state = 0;
1291 r10_bio->mddev = mddev;
1292 r10_bio->sector = bio->bi_sector + sectors_handled;
1293 goto read_again;
1294 } else
1295 generic_make_request(read_bio);
1296 return;
1297 }
1299 /*
1300 * WRITE:
1301 */
1302 if (conf->pending_count >= max_queued_requests) {
1303 md_wakeup_thread(mddev->thread);
1304 wait_event(conf->wait_barrier,
1305 conf->pending_count < max_queued_requests);
1306 }
1307 /* first select target devices under rcu_lock and
1308 * inc refcount on their rdev. Record them by setting
1309 * bios[x] to bio
1310 * If there are known/acknowledged bad blocks on any device
1311 * on which we have seen a write error, we want to avoid
1312 * writing to those blocks. This potentially requires several
1313 * writes to write around the bad blocks. Each set of writes
1314 * gets its own r10_bio with a set of bios attached. The number
1315 * of r10_bios is recored in bio->bi_phys_segments just as with
1316 * the read case.
1317 */
1319 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
1320 raid10_find_phys(conf, r10_bio);
1321 retry_write:
1322 blocked_rdev = NULL;
1323 rcu_read_lock();
1324 max_sectors = r10_bio->sectors;
1326 for (i = 0; i < conf->copies; i++) {
1327 int d = r10_bio->devs[i].devnum;
1328 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
1329 struct md_rdev *rrdev = rcu_dereference(
1330 conf->mirrors[d].replacement);
1331 if (rdev == rrdev)
1332 rrdev = NULL;
1333 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1334 atomic_inc(&rdev->nr_pending);
1335 blocked_rdev = rdev;
1336 break;
1337 }
1338 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1339 atomic_inc(&rrdev->nr_pending);
1340 blocked_rdev = rrdev;
1341 break;
1342 }
1343 if (rdev && (test_bit(Faulty, &rdev->flags)
1344 || test_bit(Unmerged, &rdev->flags)))
1345 rdev = NULL;
1346 if (rrdev && (test_bit(Faulty, &rrdev->flags)
1347 || test_bit(Unmerged, &rrdev->flags)))
1348 rrdev = NULL;
1350 r10_bio->devs[i].bio = NULL;
1351 r10_bio->devs[i].repl_bio = NULL;
1353 if (!rdev && !rrdev) {
1354 set_bit(R10BIO_Degraded, &r10_bio->state);
1355 continue;
1356 }
1357 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
1358 sector_t first_bad;
1359 sector_t dev_sector = r10_bio->devs[i].addr;
1360 int bad_sectors;
1361 int is_bad;
1363 is_bad = is_badblock(rdev, dev_sector,
1364 max_sectors,
1365 &first_bad, &bad_sectors);
1366 if (is_bad < 0) {
1367 /* Mustn't write here until the bad block
1368 * is acknowledged
1369 */
1370 atomic_inc(&rdev->nr_pending);
1371 set_bit(BlockedBadBlocks, &rdev->flags);
1372 blocked_rdev = rdev;
1373 break;
1374 }
1375 if (is_bad && first_bad <= dev_sector) {
1376 /* Cannot write here at all */
1377 bad_sectors -= (dev_sector - first_bad);
1378 if (bad_sectors < max_sectors)
1379 /* Mustn't write more than bad_sectors
1380 * to other devices yet
1381 */
1382 max_sectors = bad_sectors;
1383 /* We don't set R10BIO_Degraded as that
1384 * only applies if the disk is missing,
1385 * so it might be re-added, and we want to
1386 * know to recover this chunk.
1387 * In this case the device is here, and the
1388 * fact that this chunk is not in-sync is
1389 * recorded in the bad block log.
1390 */
1391 continue;
1392 }
1393 if (is_bad) {
1394 int good_sectors = first_bad - dev_sector;
1395 if (good_sectors < max_sectors)
1396 max_sectors = good_sectors;
1397 }
1398 }
1399 if (rdev) {
1400 r10_bio->devs[i].bio = bio;
1401 atomic_inc(&rdev->nr_pending);
1402 }
1403 if (rrdev) {
1404 r10_bio->devs[i].repl_bio = bio;
1405 atomic_inc(&rrdev->nr_pending);
1406 }
1407 }
1408 rcu_read_unlock();
1410 if (unlikely(blocked_rdev)) {
1411 /* Have to wait for this device to get unblocked, then retry */
1412 int j;
1413 int d;
1415 for (j = 0; j < i; j++) {
1416 if (r10_bio->devs[j].bio) {
1417 d = r10_bio->devs[j].devnum;
1418 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1419 }
1420 if (r10_bio->devs[j].repl_bio) {
1421 struct md_rdev *rdev;
1422 d = r10_bio->devs[j].devnum;
1423 rdev = conf->mirrors[d].replacement;
1424 if (!rdev) {
1425 /* Race with remove_disk */
1426 smp_mb();
1427 rdev = conf->mirrors[d].rdev;
1428 }
1429 rdev_dec_pending(rdev, mddev);
1430 }
1431 }
1432 allow_barrier(conf);
1433 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1434 wait_barrier(conf);
1435 goto retry_write;
1436 }
1438 if (max_sectors < r10_bio->sectors) {
1439 /* We are splitting this into multiple parts, so
1440 * we need to prepare for allocating another r10_bio.
1441 */
1442 r10_bio->sectors = max_sectors;
1443 spin_lock_irq(&conf->device_lock);
1444 if (bio->bi_phys_segments == 0)
1445 bio->bi_phys_segments = 2;
1446 else
1447 bio->bi_phys_segments++;
1448 spin_unlock_irq(&conf->device_lock);
1449 }
1450 sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
1452 atomic_set(&r10_bio->remaining, 1);
1453 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
1455 for (i = 0; i < conf->copies; i++) {
1456 struct bio *mbio;
1457 int d = r10_bio->devs[i].devnum;
1458 if (r10_bio->devs[i].bio) {
1459 struct md_rdev *rdev = conf->mirrors[d].rdev;
1460 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1461 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1462 max_sectors);
1463 r10_bio->devs[i].bio = mbio;
1465 mbio->bi_sector = (r10_bio->devs[i].addr+
1466 choose_data_offset(r10_bio,
1467 rdev));
1468 mbio->bi_bdev = rdev->bdev;
1469 mbio->bi_end_io = raid10_end_write_request;
1470 mbio->bi_rw =
1471 WRITE | do_sync | do_fua | do_discard | do_same;
1472 mbio->bi_private = r10_bio;
1474 atomic_inc(&r10_bio->remaining);
1476 cb = blk_check_plugged(raid10_unplug, mddev,
1477 sizeof(*plug));
1478 if (cb)
1479 plug = container_of(cb, struct raid10_plug_cb,
1480 cb);
1481 else
1482 plug = NULL;
1483 spin_lock_irqsave(&conf->device_lock, flags);
1484 if (plug) {
1485 bio_list_add(&plug->pending, mbio);
1486 plug->pending_cnt++;
1487 } else {
1488 bio_list_add(&conf->pending_bio_list, mbio);
1489 conf->pending_count++;
1490 }
1491 spin_unlock_irqrestore(&conf->device_lock, flags);
1492 if (!plug)
1493 md_wakeup_thread(mddev->thread);
1494 }
1496 if (r10_bio->devs[i].repl_bio) {
1497 struct md_rdev *rdev = conf->mirrors[d].replacement;
1498 if (rdev == NULL) {
1499 /* Replacement just got moved to main 'rdev' */
1500 smp_mb();
1501 rdev = conf->mirrors[d].rdev;
1502 }
1503 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1504 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1505 max_sectors);
1506 r10_bio->devs[i].repl_bio = mbio;
1508 mbio->bi_sector = (r10_bio->devs[i].addr +
1509 choose_data_offset(
1510 r10_bio, rdev));
1511 mbio->bi_bdev = rdev->bdev;
1512 mbio->bi_end_io = raid10_end_write_request;
1513 mbio->bi_rw =
1514 WRITE | do_sync | do_fua | do_discard | do_same;
1515 mbio->bi_private = r10_bio;
1517 atomic_inc(&r10_bio->remaining);
1518 spin_lock_irqsave(&conf->device_lock, flags);
1519 bio_list_add(&conf->pending_bio_list, mbio);
1520 conf->pending_count++;
1521 spin_unlock_irqrestore(&conf->device_lock, flags);
1522 if (!mddev_check_plugged(mddev))
1523 md_wakeup_thread(mddev->thread);
1524 }
1525 }
1527 /* Don't remove the bias on 'remaining' (one_write_done) until
1528 * after checking if we need to go around again.
1529 */
1531 if (sectors_handled < (bio->bi_size >> 9)) {
1532 one_write_done(r10_bio);
1533 /* We need another r10_bio. It has already been counted
1534 * in bio->bi_phys_segments.
1535 */
1536 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1538 r10_bio->master_bio = bio;
1539 r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1541 r10_bio->mddev = mddev;
1542 r10_bio->sector = bio->bi_sector + sectors_handled;
1543 r10_bio->state = 0;
1544 goto retry_write;
1545 }
1546 one_write_done(r10_bio);
1548 /* In case raid10d snuck in to freeze_array */
1549 wake_up(&conf->wait_barrier);
1550 }
1552 static void status(struct seq_file *seq, struct mddev *mddev)
1553 {
1554 struct r10conf *conf = mddev->private;
1555 int i;
1557 if (conf->geo.near_copies < conf->geo.raid_disks)
1558 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
1559 if (conf->geo.near_copies > 1)
1560 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1561 if (conf->geo.far_copies > 1) {
1562 if (conf->geo.far_offset)
1563 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
1564 else
1565 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
1566 }
1567 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1568 conf->geo.raid_disks - mddev->degraded);
1569 for (i = 0; i < conf->geo.raid_disks; i++)
1570 seq_printf(seq, "%s",
1571 conf->mirrors[i].rdev &&
1572 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
1573 seq_printf(seq, "]");
1574 }
1576 /* check if there are enough drives for
1577 * every block to appear on atleast one.
1578 * Don't consider the device numbered 'ignore'
1579 * as we might be about to remove it.
1580 */
1581 static int _enough(struct r10conf *conf, struct geom *geo, int ignore)
1582 {
1583 int first = 0;
1585 do {
1586 int n = conf->copies;
1587 int cnt = 0;
1588 int this = first;
1589 while (n--) {
1590 if (conf->mirrors[this].rdev &&
1591 this != ignore)
1592 cnt++;
1593 this = (this+1) % geo->raid_disks;
1594 }
1595 if (cnt == 0)
1596 return 0;
1597 first = (first + geo->near_copies) % geo->raid_disks;
1598 } while (first != 0);
1599 return 1;
1600 }
1602 static int enough(struct r10conf *conf, int ignore)
1603 {
1604 return _enough(conf, &conf->geo, ignore) &&
1605 _enough(conf, &conf->prev, ignore);
1606 }
1608 static void error(struct mddev *mddev, struct md_rdev *rdev)
1609 {
1610 char b[BDEVNAME_SIZE];
1611 struct r10conf *conf = mddev->private;
1613 /*
1614 * If it is not operational, then we have already marked it as dead
1615 * else if it is the last working disks, ignore the error, let the
1616 * next level up know.
1617 * else mark the drive as failed
1618 */
1619 if (test_bit(In_sync, &rdev->flags)
1620 && !enough(conf, rdev->raid_disk))
1621 /*
1622 * Don't fail the drive, just return an IO error.
1623 */
1624 return;
1625 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1626 unsigned long flags;
1627 spin_lock_irqsave(&conf->device_lock, flags);
1628 mddev->degraded++;
1629 spin_unlock_irqrestore(&conf->device_lock, flags);
1630 /*
1631 * if recovery is running, make sure it aborts.
1632 */
1633 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1634 }
1635 set_bit(Blocked, &rdev->flags);
1636 set_bit(Faulty, &rdev->flags);
1637 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1638 printk(KERN_ALERT
1639 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1640 "md/raid10:%s: Operation continuing on %d devices.\n",
1641 mdname(mddev), bdevname(rdev->bdev, b),
1642 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
1643 }
1645 static void print_conf(struct r10conf *conf)
1646 {
1647 int i;
1648 struct raid10_info *tmp;
1650 printk(KERN_DEBUG "RAID10 conf printout:\n");
1651 if (!conf) {
1652 printk(KERN_DEBUG "(!conf)\n");
1653 return;
1654 }
1655 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
1656 conf->geo.raid_disks);
1658 for (i = 0; i < conf->geo.raid_disks; i++) {
1659 char b[BDEVNAME_SIZE];
1660 tmp = conf->mirrors + i;
1661 if (tmp->rdev)
1662 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
1663 i, !test_bit(In_sync, &tmp->rdev->flags),
1664 !test_bit(Faulty, &tmp->rdev->flags),
1665 bdevname(tmp->rdev->bdev,b));
1666 }
1667 }
1669 static void close_sync(struct r10conf *conf)
1670 {
1671 wait_barrier(conf);
1672 allow_barrier(conf);
1674 mempool_destroy(conf->r10buf_pool);
1675 conf->r10buf_pool = NULL;
1676 }
1678 static int raid10_spare_active(struct mddev *mddev)
1679 {
1680 int i;
1681 struct r10conf *conf = mddev->private;
1682 struct raid10_info *tmp;
1683 int count = 0;
1684 unsigned long flags;
1686 /*
1687 * Find all non-in_sync disks within the RAID10 configuration
1688 * and mark them in_sync
1689 */
1690 for (i = 0; i < conf->geo.raid_disks; i++) {
1691 tmp = conf->mirrors + i;
1692 if (tmp->replacement
1693 && tmp->replacement->recovery_offset == MaxSector
1694 && !test_bit(Faulty, &tmp->replacement->flags)
1695 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1696 /* Replacement has just become active */
1697 if (!tmp->rdev
1698 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1699 count++;
1700 if (tmp->rdev) {
1701 /* Replaced device not technically faulty,
1702 * but we need to be sure it gets removed
1703 * and never re-added.
1704 */
1705 set_bit(Faulty, &tmp->rdev->flags);
1706 sysfs_notify_dirent_safe(
1707 tmp->rdev->sysfs_state);
1708 }
1709 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1710 } else if (tmp->rdev
1711 && !test_bit(Faulty, &tmp->rdev->flags)
1712 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1713 count++;
1714 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
1715 }
1716 }
1717 spin_lock_irqsave(&conf->device_lock, flags);
1718 mddev->degraded -= count;
1719 spin_unlock_irqrestore(&conf->device_lock, flags);
1721 print_conf(conf);
1722 return count;
1723 }
1726 static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1727 {
1728 struct r10conf *conf = mddev->private;
1729 int err = -EEXIST;
1730 int mirror;
1731 int first = 0;
1732 int last = conf->geo.raid_disks - 1;
1733 struct request_queue *q = bdev_get_queue(rdev->bdev);
1735 if (mddev->recovery_cp < MaxSector)
1736 /* only hot-add to in-sync arrays, as recovery is
1737 * very different from resync
1738 */
1739 return -EBUSY;
1740 if (rdev->saved_raid_disk < 0 && !_enough(conf, &conf->prev, -1))
1741 return -EINVAL;
1743 if (rdev->raid_disk >= 0)
1744 first = last = rdev->raid_disk;
1746 if (q->merge_bvec_fn) {
1747 set_bit(Unmerged, &rdev->flags);
1748 mddev->merge_check_needed = 1;
1749 }
1751 if (rdev->saved_raid_disk >= first &&
1752 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1753 mirror = rdev->saved_raid_disk;
1754 else
1755 mirror = first;
1756 for ( ; mirror <= last ; mirror++) {
1757 struct raid10_info *p = &conf->mirrors[mirror];
1758 if (p->recovery_disabled == mddev->recovery_disabled)
1759 continue;
1760 if (p->rdev) {
1761 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1762 p->replacement != NULL)
1763 continue;
1764 clear_bit(In_sync, &rdev->flags);
1765 set_bit(Replacement, &rdev->flags);
1766 rdev->raid_disk = mirror;
1767 err = 0;
1768 disk_stack_limits(mddev->gendisk, rdev->bdev,
1769 rdev->data_offset << 9);
1770 conf->fullsync = 1;
1771 rcu_assign_pointer(p->replacement, rdev);
1772 break;
1773 }
1775 disk_stack_limits(mddev->gendisk, rdev->bdev,
1776 rdev->data_offset << 9);
1778 p->head_position = 0;
1779 p->recovery_disabled = mddev->recovery_disabled - 1;
1780 rdev->raid_disk = mirror;
1781 err = 0;
1782 if (rdev->saved_raid_disk != mirror)
1783 conf->fullsync = 1;
1784 rcu_assign_pointer(p->rdev, rdev);
1785 break;
1786 }
1787 if (err == 0 && test_bit(Unmerged, &rdev->flags)) {
1788 /* Some requests might not have seen this new
1789 * merge_bvec_fn. We must wait for them to complete
1790 * before merging the device fully.
1791 * First we make sure any code which has tested
1792 * our function has submitted the request, then
1793 * we wait for all outstanding requests to complete.
1794 */
1795 synchronize_sched();
1796 raise_barrier(conf, 0);
1797 lower_barrier(conf);
1798 clear_bit(Unmerged, &rdev->flags);
1799 }
1800 md_integrity_add_rdev(rdev, mddev);
1801 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
1802 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
1804 print_conf(conf);
1805 return err;
1806 }
1808 static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1809 {
1810 struct r10conf *conf = mddev->private;
1811 int err = 0;
1812 int number = rdev->raid_disk;
1813 struct md_rdev **rdevp;
1814 struct raid10_info *p = conf->mirrors + number;
1816 print_conf(conf);
1817 if (rdev == p->rdev)
1818 rdevp = &p->rdev;
1819 else if (rdev == p->replacement)
1820 rdevp = &p->replacement;
1821 else
1822 return 0;
1824 if (test_bit(In_sync, &rdev->flags) ||
1825 atomic_read(&rdev->nr_pending)) {
1826 err = -EBUSY;
1827 goto abort;
1828 }
1829 /* Only remove faulty devices if recovery
1830 * is not possible.
1831 */
1832 if (!test_bit(Faulty, &rdev->flags) &&
1833 mddev->recovery_disabled != p->recovery_disabled &&
1834 (!p->replacement || p->replacement == rdev) &&
1835 number < conf->geo.raid_disks &&
1836 enough(conf, -1)) {
1837 err = -EBUSY;
1838 goto abort;
1839 }
1840 *rdevp = NULL;
1841 synchronize_rcu();
1842 if (atomic_read(&rdev->nr_pending)) {
1843 /* lost the race, try later */
1844 err = -EBUSY;
1845 *rdevp = rdev;
1846 goto abort;
1847 } else if (p->replacement) {
1848 /* We must have just cleared 'rdev' */
1849 p->rdev = p->replacement;
1850 clear_bit(Replacement, &p->replacement->flags);
1851 smp_mb(); /* Make sure other CPUs may see both as identical
1852 * but will never see neither -- if they are careful.
1853 */
1854 p->replacement = NULL;
1855 clear_bit(WantReplacement, &rdev->flags);
1856 } else
1857 /* We might have just remove the Replacement as faulty
1858 * Clear the flag just in case
1859 */
1860 clear_bit(WantReplacement, &rdev->flags);
1862 err = md_integrity_register(mddev);
1864 abort:
1866 print_conf(conf);
1867 return err;
1868 }
1871 static void end_sync_read(struct bio *bio, int error)
1872 {
1873 struct r10bio *r10_bio = bio->bi_private;
1874 struct r10conf *conf = r10_bio->mddev->private;
1875 int d;
1877 if (bio == r10_bio->master_bio) {
1878 /* this is a reshape read */
1879 d = r10_bio->read_slot; /* really the read dev */
1880 } else
1881 d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
1883 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1884 set_bit(R10BIO_Uptodate, &r10_bio->state);
1885 else
1886 /* The write handler will notice the lack of
1887 * R10BIO_Uptodate and record any errors etc
1888 */
1889 atomic_add(r10_bio->sectors,
1890 &conf->mirrors[d].rdev->corrected_errors);
1892 /* for reconstruct, we always reschedule after a read.
1893 * for resync, only after all reads
1894 */
1895 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1896 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1897 atomic_dec_and_test(&r10_bio->remaining)) {
1898 /* we have read all the blocks,
1899 * do the comparison in process context in raid10d
1900 */
1901 reschedule_retry(r10_bio);
1902 }
1903 }
1905 static void end_sync_request(struct r10bio *r10_bio)
1906 {
1907 struct mddev *mddev = r10_bio->mddev;
1909 while (atomic_dec_and_test(&r10_bio->remaining)) {
1910 if (r10_bio->master_bio == NULL) {
1911 /* the primary of several recovery bios */
1912 sector_t s = r10_bio->sectors;
1913 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1914 test_bit(R10BIO_WriteError, &r10_bio->state))
1915 reschedule_retry(r10_bio);
1916 else
1917 put_buf(r10_bio);
1918 md_done_sync(mddev, s, 1);
1919 break;
1920 } else {
1921 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
1922 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1923 test_bit(R10BIO_WriteError, &r10_bio->state))
1924 reschedule_retry(r10_bio);
1925 else
1926 put_buf(r10_bio);
1927 r10_bio = r10_bio2;
1928 }
1929 }
1930 }
1932 static void end_sync_write(struct bio *bio, int error)
1933 {
1934 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1935 struct r10bio *r10_bio = bio->bi_private;
1936 struct mddev *mddev = r10_bio->mddev;
1937 struct r10conf *conf = mddev->private;
1938 int d;
1939 sector_t first_bad;
1940 int bad_sectors;
1941 int slot;
1942 int repl;
1943 struct md_rdev *rdev = NULL;
1945 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1946 if (repl)
1947 rdev = conf->mirrors[d].replacement;
1948 else
1949 rdev = conf->mirrors[d].rdev;
1951 if (!uptodate) {
1952 if (repl)
1953 md_error(mddev, rdev);
1954 else {
1955 set_bit(WriteErrorSeen, &rdev->flags);
1956 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1957 set_bit(MD_RECOVERY_NEEDED,
1958 &rdev->mddev->recovery);
1959 set_bit(R10BIO_WriteError, &r10_bio->state);
1960 }
1961 } else if (is_badblock(rdev,
1962 r10_bio->devs[slot].addr,
1963 r10_bio->sectors,
1964 &first_bad, &bad_sectors))
1965 set_bit(R10BIO_MadeGood, &r10_bio->state);
1967 rdev_dec_pending(rdev, mddev);
1969 end_sync_request(r10_bio);
1970 }
1972 /*
1973 * Note: sync and recover and handled very differently for raid10
1974 * This code is for resync.
1975 * For resync, we read through virtual addresses and read all blocks.
1976 * If there is any error, we schedule a write. The lowest numbered
1977 * drive is authoritative.
1978 * However requests come for physical address, so we need to map.
1979 * For every physical address there are raid_disks/copies virtual addresses,
1980 * which is always are least one, but is not necessarly an integer.
1981 * This means that a physical address can span multiple chunks, so we may
1982 * have to submit multiple io requests for a single sync request.
1983 */
1984 /*
1985 * We check if all blocks are in-sync and only write to blocks that
1986 * aren't in sync
1987 */
1988 static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
1989 {
1990 struct r10conf *conf = mddev->private;
1991 int i, first;
1992 struct bio *tbio, *fbio;
1993 int vcnt;
1995 atomic_set(&r10_bio->remaining, 1);
1997 /* find the first device with a block */
1998 for (i=0; i<conf->copies; i++)
1999 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
2000 break;
2002 if (i == conf->copies)
2003 goto done;
2005 first = i;
2006 fbio = r10_bio->devs[i].bio;
2008 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
2009 /* now find blocks with errors */
2010 for (i=0 ; i < conf->copies ; i++) {
2011 int j, d;
2013 tbio = r10_bio->devs[i].bio;
2015 if (tbio->bi_end_io != end_sync_read)
2016 continue;
2017 if (i == first)
2018 continue;
2019 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
2020 /* We know that the bi_io_vec layout is the same for
2021 * both 'first' and 'i', so we just compare them.
2022 * All vec entries are PAGE_SIZE;
2023 */
2024 for (j = 0; j < vcnt; j++)
2025 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
2026 page_address(tbio->bi_io_vec[j].bv_page),
2027 fbio->bi_io_vec[j].bv_len))
2028 break;
2029 if (j == vcnt)
2030 continue;
2031 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
2032 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2033 /* Don't fix anything. */
2034 continue;
2035 }
2036 /* Ok, we need to write this bio, either to correct an
2037 * inconsistency or to correct an unreadable block.
2038 * First we need to fixup bv_offset, bv_len and
2039 * bi_vecs, as the read request might have corrupted these
2040 */
2041 tbio->bi_vcnt = vcnt;
2042 tbio->bi_size = r10_bio->sectors << 9;
2043 tbio->bi_idx = 0;
2044 tbio->bi_phys_segments = 0;
2045 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
2046 tbio->bi_flags |= 1 << BIO_UPTODATE;
2047 tbio->bi_next = NULL;
2048 tbio->bi_rw = WRITE;
2049 tbio->bi_private = r10_bio;
2050 tbio->bi_sector = r10_bio->devs[i].addr;
2052 for (j=0; j < vcnt ; j++) {
2053 tbio->bi_io_vec[j].bv_offset = 0;
2054 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
2056 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
2057 page_address(fbio->bi_io_vec[j].bv_page),
2058 PAGE_SIZE);
2059 }
2060 tbio->bi_end_io = end_sync_write;
2062 d = r10_bio->devs[i].devnum;
2063 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2064 atomic_inc(&r10_bio->remaining);
2065 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
2067 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
2068 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
2069 generic_make_request(tbio);
2070 }
2072 /* Now write out to any replacement devices
2073 * that are active
2074 */
2075 for (i = 0; i < conf->copies; i++) {
2076 int j, d;
2078 tbio = r10_bio->devs[i].repl_bio;
2079 if (!tbio || !tbio->bi_end_io)
2080 continue;
2081 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2082 && r10_bio->devs[i].bio != fbio)
2083 for (j = 0; j < vcnt; j++)
2084 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
2085 page_address(fbio->bi_io_vec[j].bv_page),
2086 PAGE_SIZE);
2087 d = r10_bio->devs[i].devnum;
2088 atomic_inc(&r10_bio->remaining);
2089 md_sync_acct(conf->mirrors[d].replacement->bdev,
2090 tbio->bi_size >> 9);
2091 generic_make_request(tbio);
2092 }
2094 done:
2095 if (atomic_dec_and_test(&r10_bio->remaining)) {
2096 md_done_sync(mddev, r10_bio->sectors, 1);
2097 put_buf(r10_bio);
2098 }
2099 }
2101 /*
2102 * Now for the recovery code.
2103 * Recovery happens across physical sectors.
2104 * We recover all non-is_sync drives by finding the virtual address of
2105 * each, and then choose a working drive that also has that virt address.
2106 * There is a separate r10_bio for each non-in_sync drive.
2107 * Only the first two slots are in use. The first for reading,
2108 * The second for writing.
2109 *
2110 */
2111 static void fix_recovery_read_error(struct r10bio *r10_bio)
2112 {
2113 /* We got a read error during recovery.
2114 * We repeat the read in smaller page-sized sections.
2115 * If a read succeeds, write it to the new device or record
2116 * a bad block if we cannot.
2117 * If a read fails, record a bad block on both old and
2118 * new devices.
2119 */
2120 struct mddev *mddev = r10_bio->mddev;
2121 struct r10conf *conf = mddev->private;
2122 struct bio *bio = r10_bio->devs[0].bio;
2123 sector_t sect = 0;
2124 int sectors = r10_bio->sectors;
2125 int idx = 0;
2126 int dr = r10_bio->devs[0].devnum;
2127 int dw = r10_bio->devs[1].devnum;
2129 while (sectors) {
2130 int s = sectors;
2131 struct md_rdev *rdev;
2132 sector_t addr;
2133 int ok;
2135 if (s > (PAGE_SIZE>>9))
2136 s = PAGE_SIZE >> 9;
2138 rdev = conf->mirrors[dr].rdev;
2139 addr = r10_bio->devs[0].addr + sect,
2140 ok = sync_page_io(rdev,
2141 addr,
2142 s << 9,
2143 bio->bi_io_vec[idx].bv_page,
2144 READ, false);
2145 if (ok) {
2146 rdev = conf->mirrors[dw].rdev;
2147 addr = r10_bio->devs[1].addr + sect;
2148 ok = sync_page_io(rdev,
2149 addr,
2150 s << 9,
2151 bio->bi_io_vec[idx].bv_page,
2152 WRITE, false);
2153 if (!ok) {
2154 set_bit(WriteErrorSeen, &rdev->flags);
2155 if (!test_and_set_bit(WantReplacement,
2156 &rdev->flags))
2157 set_bit(MD_RECOVERY_NEEDED,
2158 &rdev->mddev->recovery);
2159 }
2160 }
2161 if (!ok) {
2162 /* We don't worry if we cannot set a bad block -
2163 * it really is bad so there is no loss in not
2164 * recording it yet
2165 */
2166 rdev_set_badblocks(rdev, addr, s, 0);
2168 if (rdev != conf->mirrors[dw].rdev) {
2169 /* need bad block on destination too */
2170 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
2171 addr = r10_bio->devs[1].addr + sect;
2172 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2173 if (!ok) {
2174 /* just abort the recovery */
2175 printk(KERN_NOTICE
2176 "md/raid10:%s: recovery aborted"
2177 " due to read error\n",
2178 mdname(mddev));
2180 conf->mirrors[dw].recovery_disabled
2181 = mddev->recovery_disabled;
2182 set_bit(MD_RECOVERY_INTR,
2183 &mddev->recovery);
2184 break;
2185 }
2186 }
2187 }
2189 sectors -= s;
2190 sect += s;
2191 idx++;
2192 }
2193 }
2195 static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
2196 {
2197 struct r10conf *conf = mddev->private;
2198 int d;
2199 struct bio *wbio, *wbio2;
2201 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2202 fix_recovery_read_error(r10_bio);
2203 end_sync_request(r10_bio);
2204 return;
2205 }
2207 /*
2208 * share the pages with the first bio
2209 * and submit the write request
2210 */
2211 d = r10_bio->devs[1].devnum;
2212 wbio = r10_bio->devs[1].bio;
2213 wbio2 = r10_bio->devs[1].repl_bio;
2214 if (wbio->bi_end_io) {
2215 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2216 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
2217 generic_make_request(wbio);
2218 }
2219 if (wbio2 && wbio2->bi_end_io) {
2220 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2221 md_sync_acct(conf->mirrors[d].replacement->bdev,
2222 wbio2->bi_size >> 9);
2223 generic_make_request(wbio2);
2224 }
2225 }
2228 /*
2229 * Used by fix_read_error() to decay the per rdev read_errors.
2230 * We halve the read error count for every hour that has elapsed
2231 * since the last recorded read error.
2232 *
2233 */
2234 static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
2235 {
2236 struct timespec cur_time_mon;
2237 unsigned long hours_since_last;
2238 unsigned int read_errors = atomic_read(&rdev->read_errors);
2240 ktime_get_ts(&cur_time_mon);
2242 if (rdev->last_read_error.tv_sec == 0 &&
2243 rdev->last_read_error.tv_nsec == 0) {
2244 /* first time we've seen a read error */
2245 rdev->last_read_error = cur_time_mon;
2246 return;
2247 }
2249 hours_since_last = (cur_time_mon.tv_sec -
2250 rdev->last_read_error.tv_sec) / 3600;
2252 rdev->last_read_error = cur_time_mon;
2254 /*
2255 * if hours_since_last is > the number of bits in read_errors
2256 * just set read errors to 0. We do this to avoid
2257 * overflowing the shift of read_errors by hours_since_last.
2258 */
2259 if (hours_since_last >= 8 * sizeof(read_errors))
2260 atomic_set(&rdev->read_errors, 0);
2261 else
2262 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2263 }
2265 static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
2266 int sectors, struct page *page, int rw)
2267 {
2268 sector_t first_bad;
2269 int bad_sectors;
2271 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2272 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2273 return -1;
2274 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
2275 /* success */
2276 return 1;
2277 if (rw == WRITE) {
2278 set_bit(WriteErrorSeen, &rdev->flags);
2279 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2280 set_bit(MD_RECOVERY_NEEDED,
2281 &rdev->mddev->recovery);
2282 }
2283 /* need to record an error - either for the block or the device */
2284 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2285 md_error(rdev->mddev, rdev);
2286 return 0;
2287 }
2289 /*
2290 * This is a kernel thread which:
2291 *
2292 * 1. Retries failed read operations on working mirrors.
2293 * 2. Updates the raid superblock when problems encounter.
2294 * 3. Performs writes following reads for array synchronising.
2295 */
2297 static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
2298 {
2299 int sect = 0; /* Offset from r10_bio->sector */
2300 int sectors = r10_bio->sectors;
2301 struct md_rdev*rdev;
2302 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
2303 int d = r10_bio->devs[r10_bio->read_slot].devnum;
2305 /* still own a reference to this rdev, so it cannot
2306 * have been cleared recently.
2307 */
2308 rdev = conf->mirrors[d].rdev;
2310 if (test_bit(Faulty, &rdev->flags))
2311 /* drive has already been failed, just ignore any
2312 more fix_read_error() attempts */
2313 return;
2315 check_decay_read_errors(mddev, rdev);
2316 atomic_inc(&rdev->read_errors);
2317 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2318 char b[BDEVNAME_SIZE];
2319 bdevname(rdev->bdev, b);
2321 printk(KERN_NOTICE
2322 "md/raid10:%s: %s: Raid device exceeded "
2323 "read_error threshold [cur %d:max %d]\n",
2324 mdname(mddev), b,
2325 atomic_read(&rdev->read_errors), max_read_errors);
2326 printk(KERN_NOTICE
2327 "md/raid10:%s: %s: Failing raid device\n",
2328 mdname(mddev), b);
2329 md_error(mddev, conf->mirrors[d].rdev);
2330 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
2331 return;
2332 }
2334 while(sectors) {
2335 int s = sectors;
2336 int sl = r10_bio->read_slot;
2337 int success = 0;
2338 int start;
2340 if (s > (PAGE_SIZE>>9))
2341 s = PAGE_SIZE >> 9;
2343 rcu_read_lock();
2344 do {
2345 sector_t first_bad;
2346 int bad_sectors;
2348 d = r10_bio->devs[sl].devnum;
2349 rdev = rcu_dereference(conf->mirrors[d].rdev);
2350 if (rdev &&
2351 !test_bit(Unmerged, &rdev->flags) &&
2352 test_bit(In_sync, &rdev->flags) &&
2353 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2354 &first_bad, &bad_sectors) == 0) {
2355 atomic_inc(&rdev->nr_pending);
2356 rcu_read_unlock();
2357 success = sync_page_io(rdev,
2358 r10_bio->devs[sl].addr +
2359 sect,
2360 s<<9,
2361 conf->tmppage, READ, false);
2362 rdev_dec_pending(rdev, mddev);
2363 rcu_read_lock();
2364 if (success)
2365 break;
2366 }
2367 sl++;
2368 if (sl == conf->copies)
2369 sl = 0;
2370 } while (!success && sl != r10_bio->read_slot);
2371 rcu_read_unlock();
2373 if (!success) {
2374 /* Cannot read from anywhere, just mark the block
2375 * as bad on the first device to discourage future
2376 * reads.
2377 */
2378 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
2379 rdev = conf->mirrors[dn].rdev;
2381 if (!rdev_set_badblocks(
2382 rdev,
2383 r10_bio->devs[r10_bio->read_slot].addr
2384 + sect,
2385 s, 0)) {
2386 md_error(mddev, rdev);
2387 r10_bio->devs[r10_bio->read_slot].bio
2388 = IO_BLOCKED;
2389 }
2390 break;
2391 }
2393 start = sl;
2394 /* write it back and re-read */
2395 rcu_read_lock();
2396 while (sl != r10_bio->read_slot) {
2397 char b[BDEVNAME_SIZE];
2399 if (sl==0)
2400 sl = conf->copies;
2401 sl--;
2402 d = r10_bio->devs[sl].devnum;
2403 rdev = rcu_dereference(conf->mirrors[d].rdev);
2404 if (!rdev ||
2405 test_bit(Unmerged, &rdev->flags) ||
2406 !test_bit(In_sync, &rdev->flags))
2407 continue;
2409 atomic_inc(&rdev->nr_pending);
2410 rcu_read_unlock();
2411 if (r10_sync_page_io(rdev,
2412 r10_bio->devs[sl].addr +
2413 sect,
2414 s, conf->tmppage, WRITE)
2415 == 0) {
2416 /* Well, this device is dead */
2417 printk(KERN_NOTICE
2418 "md/raid10:%s: read correction "
2419 "write failed"
2420 " (%d sectors at %llu on %s)\n",
2421 mdname(mddev), s,
2422 (unsigned long long)(
2423 sect +
2424 choose_data_offset(r10_bio,
2425 rdev)),
2426 bdevname(rdev->bdev, b));
2427 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2428 "drive\n",
2429 mdname(mddev),
2430 bdevname(rdev->bdev, b));
2431 }
2432 rdev_dec_pending(rdev, mddev);
2433 rcu_read_lock();
2434 }
2435 sl = start;
2436 while (sl != r10_bio->read_slot) {
2437 char b[BDEVNAME_SIZE];
2439 if (sl==0)
2440 sl = conf->copies;
2441 sl--;
2442 d = r10_bio->devs[sl].devnum;
2443 rdev = rcu_dereference(conf->mirrors[d].rdev);
2444 if (!rdev ||
2445 !test_bit(In_sync, &rdev->flags))
2446 continue;
2448 atomic_inc(&rdev->nr_pending);
2449 rcu_read_unlock();
2450 switch (r10_sync_page_io(rdev,
2451 r10_bio->devs[sl].addr +
2452 sect,
2453 s, conf->tmppage,
2454 READ)) {
2455 case 0:
2456 /* Well, this device is dead */
2457 printk(KERN_NOTICE
2458 "md/raid10:%s: unable to read back "
2459 "corrected sectors"
2460 " (%d sectors at %llu on %s)\n",
2461 mdname(mddev), s,
2462 (unsigned long long)(
2463 sect +
2464 choose_data_offset(r10_bio, rdev)),
2465 bdevname(rdev->bdev, b));
2466 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2467 "drive\n",
2468 mdname(mddev),
2469 bdevname(rdev->bdev, b));
2470 break;
2471 case 1:
2472 printk(KERN_INFO
2473 "md/raid10:%s: read error corrected"
2474 " (%d sectors at %llu on %s)\n",
2475 mdname(mddev), s,
2476 (unsigned long long)(
2477 sect +
2478 choose_data_offset(r10_bio, rdev)),
2479 bdevname(rdev->bdev, b));
2480 atomic_add(s, &rdev->corrected_errors);
2481 }
2483 rdev_dec_pending(rdev, mddev);
2484 rcu_read_lock();
2485 }
2486 rcu_read_unlock();
2488 sectors -= s;
2489 sect += s;
2490 }
2491 }
2493 static void bi_complete(struct bio *bio, int error)
2494 {
2495 complete((struct completion *)bio->bi_private);
2496 }
2498 static int submit_bio_wait(int rw, struct bio *bio)
2499 {
2500 struct completion event;
2501 rw |= REQ_SYNC;
2503 init_completion(&event);
2504 bio->bi_private = &event;
2505 bio->bi_end_io = bi_complete;
2506 submit_bio(rw, bio);
2507 wait_for_completion(&event);
2509 return test_bit(BIO_UPTODATE, &bio->bi_flags);
2510 }
2512 static int narrow_write_error(struct r10bio *r10_bio, int i)
2513 {
2514 struct bio *bio = r10_bio->master_bio;
2515 struct mddev *mddev = r10_bio->mddev;
2516 struct r10conf *conf = mddev->private;
2517 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
2518 /* bio has the data to be written to slot 'i' where
2519 * we just recently had a write error.
2520 * We repeatedly clone the bio and trim down to one block,
2521 * then try the write. Where the write fails we record
2522 * a bad block.
2523 * It is conceivable that the bio doesn't exactly align with
2524 * blocks. We must handle this.
2525 *
2526 * We currently own a reference to the rdev.
2527 */
2529 int block_sectors;
2530 sector_t sector;
2531 int sectors;
2532 int sect_to_write = r10_bio->sectors;
2533 int ok = 1;
2535 if (rdev->badblocks.shift < 0)
2536 return 0;
2538 block_sectors = 1 << rdev->badblocks.shift;
2539 sector = r10_bio->sector;
2540 sectors = ((r10_bio->sector + block_sectors)
2541 & ~(sector_t)(block_sectors - 1))
2542 - sector;
2544 while (sect_to_write) {
2545 struct bio *wbio;
2546 if (sectors > sect_to_write)
2547 sectors = sect_to_write;
2548 /* Write at 'sector' for 'sectors' */
2549 wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
2550 md_trim_bio(wbio, sector - bio->bi_sector, sectors);
2551 wbio->bi_sector = (r10_bio->devs[i].addr+
2552 choose_data_offset(r10_bio, rdev) +
2553 (sector - r10_bio->sector));
2554 wbio->bi_bdev = rdev->bdev;
2555 if (submit_bio_wait(WRITE, wbio) == 0)
2556 /* Failure! */
2557 ok = rdev_set_badblocks(rdev, sector,
2558 sectors, 0)
2559 && ok;
2561 bio_put(wbio);
2562 sect_to_write -= sectors;
2563 sector += sectors;
2564 sectors = block_sectors;
2565 }
2566 return ok;
2567 }
2569 static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
2570 {
2571 int slot = r10_bio->read_slot;
2572 struct bio *bio;
2573 struct r10conf *conf = mddev->private;
2574 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
2575 char b[BDEVNAME_SIZE];
2576 unsigned long do_sync;
2577 int max_sectors;
2579 /* we got a read error. Maybe the drive is bad. Maybe just
2580 * the block and we can fix it.
2581 * We freeze all other IO, and try reading the block from
2582 * other devices. When we find one, we re-write
2583 * and check it that fixes the read error.
2584 * This is all done synchronously while the array is
2585 * frozen.
2586 */
2587 bio = r10_bio->devs[slot].bio;
2588 bdevname(bio->bi_bdev, b);
2589 bio_put(bio);
2590 r10_bio->devs[slot].bio = NULL;
2592 if (mddev->ro == 0) {
2593 freeze_array(conf);
2594 fix_read_error(conf, mddev, r10_bio);
2595 unfreeze_array(conf);
2596 } else
2597 r10_bio->devs[slot].bio = IO_BLOCKED;
2599 rdev_dec_pending(rdev, mddev);
2601 read_more:
2602 rdev = read_balance(conf, r10_bio, &max_sectors);
2603 if (rdev == NULL) {
2604 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
2605 " read error for block %llu\n",
2606 mdname(mddev), b,
2607 (unsigned long long)r10_bio->sector);
2608 raid_end_bio_io(r10_bio);
2609 return;
2610 }
2612 do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
2613 slot = r10_bio->read_slot;
2614 printk_ratelimited(
2615 KERN_ERR
2616 "md/raid10:%s: %s: redirecting "
2617 "sector %llu to another mirror\n",
2618 mdname(mddev),
2619 bdevname(rdev->bdev, b),
2620 (unsigned long long)r10_bio->sector);
2621 bio = bio_clone_mddev(r10_bio->master_bio,
2622 GFP_NOIO, mddev);
2623 md_trim_bio(bio,
2624 r10_bio->sector - bio->bi_sector,
2625 max_sectors);
2626 r10_bio->devs[slot].bio = bio;
2627 r10_bio->devs[slot].rdev = rdev;
2628 bio->bi_sector = r10_bio->devs[slot].addr
2629 + choose_data_offset(r10_bio, rdev);
2630 bio->bi_bdev = rdev->bdev;
2631 bio->bi_rw = READ | do_sync;
2632 bio->bi_private = r10_bio;
2633 bio->bi_end_io = raid10_end_read_request;
2634 if (max_sectors < r10_bio->sectors) {
2635 /* Drat - have to split this up more */
2636 struct bio *mbio = r10_bio->master_bio;
2637 int sectors_handled =
2638 r10_bio->sector + max_sectors
2639 - mbio->bi_sector;
2640 r10_bio->sectors = max_sectors;
2641 spin_lock_irq(&conf->device_lock);
2642 if (mbio->bi_phys_segments == 0)
2643 mbio->bi_phys_segments = 2;
2644 else
2645 mbio->bi_phys_segments++;
2646 spin_unlock_irq(&conf->device_lock);
2647 generic_make_request(bio);
2649 r10_bio = mempool_alloc(conf->r10bio_pool,
2650 GFP_NOIO);
2651 r10_bio->master_bio = mbio;
2652 r10_bio->sectors = (mbio->bi_size >> 9)
2653 - sectors_handled;
2654 r10_bio->state = 0;
2655 set_bit(R10BIO_ReadError,
2656 &r10_bio->state);
2657 r10_bio->mddev = mddev;
2658 r10_bio->sector = mbio->bi_sector
2659 + sectors_handled;
2661 goto read_more;
2662 } else
2663 generic_make_request(bio);
2664 }
2666 static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
2667 {
2668 /* Some sort of write request has finished and it
2669 * succeeded in writing where we thought there was a
2670 * bad block. So forget the bad block.
2671 * Or possibly if failed and we need to record
2672 * a bad block.
2673 */
2674 int m;
2675 struct md_rdev *rdev;
2677 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2678 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
2679 for (m = 0; m < conf->copies; m++) {
2680 int dev = r10_bio->devs[m].devnum;
2681 rdev = conf->mirrors[dev].rdev;
2682 if (r10_bio->devs[m].bio == NULL)
2683 continue;
2684 if (test_bit(BIO_UPTODATE,
2685 &r10_bio->devs[m].bio->bi_flags)) {
2686 rdev_clear_badblocks(
2687 rdev,
2688 r10_bio->devs[m].addr,
2689 r10_bio->sectors, 0);
2690 } else {
2691 if (!rdev_set_badblocks(
2692 rdev,
2693 r10_bio->devs[m].addr,
2694 r10_bio->sectors, 0))
2695 md_error(conf->mddev, rdev);
2696 }
2697 rdev = conf->mirrors[dev].replacement;
2698 if (r10_bio->devs[m].repl_bio == NULL)
2699 continue;
2700 if (test_bit(BIO_UPTODATE,
2701 &r10_bio->devs[m].repl_bio->bi_flags)) {
2702 rdev_clear_badblocks(
2703 rdev,
2704 r10_bio->devs[m].addr,
2705 r10_bio->sectors, 0);
2706 } else {
2707 if (!rdev_set_badblocks(
2708 rdev,
2709 r10_bio->devs[m].addr,
2710 r10_bio->sectors, 0))
2711 md_error(conf->mddev, rdev);
2712 }
2713 }
2714 put_buf(r10_bio);
2715 } else {
2716 for (m = 0; m < conf->copies; m++) {
2717 int dev = r10_bio->devs[m].devnum;
2718 struct bio *bio = r10_bio->devs[m].bio;
2719 rdev = conf->mirrors[dev].rdev;
2720 if (bio == IO_MADE_GOOD) {
2721 rdev_clear_badblocks(
2722 rdev,
2723 r10_bio->devs[m].addr,
2724 r10_bio->sectors, 0);
2725 rdev_dec_pending(rdev, conf->mddev);
2726 } else if (bio != NULL &&
2727 !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2728 if (!narrow_write_error(r10_bio, m)) {
2729 md_error(conf->mddev, rdev);
2730 set_bit(R10BIO_Degraded,
2731 &r10_bio->state);
2732 }
2733 rdev_dec_pending(rdev, conf->mddev);
2734 }
2735 bio = r10_bio->devs[m].repl_bio;
2736 rdev = conf->mirrors[dev].replacement;
2737 if (rdev && bio == IO_MADE_GOOD) {
2738 rdev_clear_badblocks(
2739 rdev,
2740 r10_bio->devs[m].addr,
2741 r10_bio->sectors, 0);
2742 rdev_dec_pending(rdev, conf->mddev);
2743 }
2744 }
2745 if (test_bit(R10BIO_WriteError,
2746 &r10_bio->state))
2747 close_write(r10_bio);
2748 raid_end_bio_io(r10_bio);
2749 }
2750 }
2752 static void raid10d(struct md_thread *thread)
2753 {
2754 struct mddev *mddev = thread->mddev;
2755 struct r10bio *r10_bio;
2756 unsigned long flags;
2757 struct r10conf *conf = mddev->private;
2758 struct list_head *head = &conf->retry_list;
2759 struct blk_plug plug;
2761 md_check_recovery(mddev);
2763 blk_start_plug(&plug);
2764 for (;;) {
2766 flush_pending_writes(conf);
2768 spin_lock_irqsave(&conf->device_lock, flags);
2769 if (list_empty(head)) {
2770 spin_unlock_irqrestore(&conf->device_lock, flags);
2771 break;
2772 }
2773 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
2774 list_del(head->prev);
2775 conf->nr_queued--;
2776 spin_unlock_irqrestore(&conf->device_lock, flags);
2778 mddev = r10_bio->mddev;
2779 conf = mddev->private;
2780 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2781 test_bit(R10BIO_WriteError, &r10_bio->state))
2782 handle_write_completed(conf, r10_bio);
2783 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
2784 reshape_request_write(mddev, r10_bio);
2785 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
2786 sync_request_write(mddev, r10_bio);
2787 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
2788 recovery_request_write(mddev, r10_bio);
2789 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
2790 handle_read_error(mddev, r10_bio);
2791 else {
2792 /* just a partial read to be scheduled from a
2793 * separate context
2794 */
2795 int slot = r10_bio->read_slot;
2796 generic_make_request(r10_bio->devs[slot].bio);
2797 }
2799 cond_resched();
2800 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2801 md_check_recovery(mddev);
2802 }
2803 blk_finish_plug(&plug);
2804 }
2807 static int init_resync(struct r10conf *conf)
2808 {
2809 int buffs;
2810 int i;
2812 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
2813 BUG_ON(conf->r10buf_pool);
2814 conf->have_replacement = 0;
2815 for (i = 0; i < conf->geo.raid_disks; i++)
2816 if (conf->mirrors[i].replacement)
2817 conf->have_replacement = 1;
2818 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2819 if (!conf->r10buf_pool)
2820 return -ENOMEM;
2821 conf->next_resync = 0;
2822 return 0;
2823 }
2825 /*
2826 * perform a "sync" on one "block"
2827 *
2828 * We need to make sure that no normal I/O request - particularly write
2829 * requests - conflict with active sync requests.
2830 *
2831 * This is achieved by tracking pending requests and a 'barrier' concept
2832 * that can be installed to exclude normal IO requests.
2833 *
2834 * Resync and recovery are handled very differently.
2835 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2836 *
2837 * For resync, we iterate over virtual addresses, read all copies,
2838 * and update if there are differences. If only one copy is live,
2839 * skip it.
2840 * For recovery, we iterate over physical addresses, read a good
2841 * value for each non-in_sync drive, and over-write.
2842 *
2843 * So, for recovery we may have several outstanding complex requests for a
2844 * given address, one for each out-of-sync device. We model this by allocating
2845 * a number of r10_bio structures, one for each out-of-sync device.
2846 * As we setup these structures, we collect all bio's together into a list
2847 * which we then process collectively to add pages, and then process again
2848 * to pass to generic_make_request.
2849 *
2850 * The r10_bio structures are linked using a borrowed master_bio pointer.
2851 * This link is counted in ->remaining. When the r10_bio that points to NULL
2852 * has its remaining count decremented to 0, the whole complex operation
2853 * is complete.
2854 *
2855 */
2857 static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
2858 int *skipped, int go_faster)
2859 {
2860 struct r10conf *conf = mddev->private;
2861 struct r10bio *r10_bio;
2862 struct bio *biolist = NULL, *bio;
2863 sector_t max_sector, nr_sectors;
2864 int i;
2865 int max_sync;
2866 sector_t sync_blocks;
2867 sector_t sectors_skipped = 0;
2868 int chunks_skipped = 0;
2869 sector_t chunk_mask = conf->geo.chunk_mask;
2871 if (!conf->r10buf_pool)
2872 if (init_resync(conf))
2873 return 0;
2875 skipped:
2876 max_sector = mddev->dev_sectors;
2877 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
2878 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2879 max_sector = mddev->resync_max_sectors;
2880 if (sector_nr >= max_sector) {
2881 /* If we aborted, we need to abort the
2882 * sync on the 'current' bitmap chucks (there can
2883 * be several when recovering multiple devices).
2884 * as we may have started syncing it but not finished.
2885 * We can find the current address in
2886 * mddev->curr_resync, but for recovery,
2887 * we need to convert that to several
2888 * virtual addresses.
2889 */
2890 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
2891 end_reshape(conf);
2892 return 0;
2893 }
2895 if (mddev->curr_resync < max_sector) { /* aborted */
2896 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2897 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2898 &sync_blocks, 1);
2899 else for (i = 0; i < conf->geo.raid_disks; i++) {
2900 sector_t sect =
2901 raid10_find_virt(conf, mddev->curr_resync, i);
2902 bitmap_end_sync(mddev->bitmap, sect,
2903 &sync_blocks, 1);
2904 }
2905 } else {
2906 /* completed sync */
2907 if ((!mddev->bitmap || conf->fullsync)
2908 && conf->have_replacement
2909 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2910 /* Completed a full sync so the replacements
2911 * are now fully recovered.
2912 */
2913 for (i = 0; i < conf->geo.raid_disks; i++)
2914 if (conf->mirrors[i].replacement)
2915 conf->mirrors[i].replacement
2916 ->recovery_offset
2917 = MaxSector;
2918 }
2919 conf->fullsync = 0;
2920 }
2921 bitmap_close_sync(mddev->bitmap);
2922 close_sync(conf);
2923 *skipped = 1;
2924 return sectors_skipped;
2925 }
2927 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2928 return reshape_request(mddev, sector_nr, skipped);
2930 if (chunks_skipped >= conf->geo.raid_disks) {
2931 /* if there has been nothing to do on any drive,
2932 * then there is nothing to do at all..
2933 */
2934 *skipped = 1;
2935 return (max_sector - sector_nr) + sectors_skipped;
2936 }
2938 if (max_sector > mddev->resync_max)
2939 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2941 /* make sure whole request will fit in a chunk - if chunks
2942 * are meaningful
2943 */
2944 if (conf->geo.near_copies < conf->geo.raid_disks &&
2945 max_sector > (sector_nr | chunk_mask))
2946 max_sector = (sector_nr | chunk_mask) + 1;
2947 /*
2948 * If there is non-resync activity waiting for us then
2949 * put in a delay to throttle resync.
2950 */
2951 if (!go_faster && conf->nr_waiting)
2952 msleep_interruptible(1000);
2954 /* Again, very different code for resync and recovery.
2955 * Both must result in an r10bio with a list of bios that
2956 * have bi_end_io, bi_sector, bi_bdev set,
2957 * and bi_private set to the r10bio.
2958 * For recovery, we may actually create several r10bios
2959 * with 2 bios in each, that correspond to the bios in the main one.
2960 * In this case, the subordinate r10bios link back through a
2961 * borrowed master_bio pointer, and the counter in the master
2962 * includes a ref from each subordinate.
2963 */
2964 /* First, we decide what to do and set ->bi_end_io
2965 * To end_sync_read if we want to read, and
2966 * end_sync_write if we will want to write.
2967 */
2969 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
2970 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2971 /* recovery... the complicated one */
2972 int j;
2973 r10_bio = NULL;
2975 for (i = 0 ; i < conf->geo.raid_disks; i++) {
2976 int still_degraded;
2977 struct r10bio *rb2;
2978 sector_t sect;
2979 int must_sync;
2980 int any_working;
2981 struct raid10_info *mirror = &conf->mirrors[i];
2983 if ((mirror->rdev == NULL ||
2984 test_bit(In_sync, &mirror->rdev->flags))
2985 &&
2986 (mirror->replacement == NULL ||
2987 test_bit(Faulty,
2988 &mirror->replacement->flags)))
2989 continue;
2991 still_degraded = 0;
2992 /* want to reconstruct this device */
2993 rb2 = r10_bio;
2994 sect = raid10_find_virt(conf, sector_nr, i);
2995 if (sect >= mddev->resync_max_sectors) {
2996 /* last stripe is not complete - don't
2997 * try to recover this sector.
2998 */
2999 continue;
3000 }
3001 /* Unless we are doing a full sync, or a replacement
3002 * we only need to recover the block if it is set in
3003 * the bitmap
3004 */
3005 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3006 &sync_blocks, 1);
3007 if (sync_blocks < max_sync)
3008 max_sync = sync_blocks;
3009 if (!must_sync &&
3010 mirror->replacement == NULL &&
3011 !conf->fullsync) {
3012 /* yep, skip the sync_blocks here, but don't assume
3013 * that there will never be anything to do here
3014 */
3015 chunks_skipped = -1;
3016 continue;
3017 }
3019 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
3020 raise_barrier(conf, rb2 != NULL);
3021 atomic_set(&r10_bio->remaining, 0);
3023 r10_bio->master_bio = (struct bio*)rb2;
3024 if (rb2)
3025 atomic_inc(&rb2->remaining);
3026 r10_bio->mddev = mddev;
3027 set_bit(R10BIO_IsRecover, &r10_bio->state);
3028 r10_bio->sector = sect;
3030 raid10_find_phys(conf, r10_bio);
3032 /* Need to check if the array will still be
3033 * degraded
3034 */
3035 for (j = 0; j < conf->geo.raid_disks; j++)
3036 if (conf->mirrors[j].rdev == NULL ||
3037 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
3038 still_degraded = 1;
3039 break;
3040 }
3042 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3043 &sync_blocks, still_degraded);
3045 any_working = 0;
3046 for (j=0; j<conf->copies;j++) {
3047 int k;
3048 int d = r10_bio->devs[j].devnum;
3049 sector_t from_addr, to_addr;
3050 struct md_rdev *rdev;
3051 sector_t sector, first_bad;
3052 int bad_sectors;
3053 if (!conf->mirrors[d].rdev ||
3054 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
3055 continue;
3056 /* This is where we read from */
3057 any_working = 1;
3058 rdev = conf->mirrors[d].rdev;
3059 sector = r10_bio->devs[j].addr;
3061 if (is_badblock(rdev, sector, max_sync,
3062 &first_bad, &bad_sectors)) {
3063 if (first_bad > sector)
3064 max_sync = first_bad - sector;
3065 else {
3066 bad_sectors -= (sector
3067 - first_bad);
3068 if (max_sync > bad_sectors)
3069 max_sync = bad_sectors;
3070 continue;
3071 }
3072 }
3073 bio = r10_bio->devs[0].bio;
3074 bio->bi_next = biolist;
3075 biolist = bio;
3076 bio->bi_private = r10_bio;
3077 bio->bi_end_io = end_sync_read;
3078 bio->bi_rw = READ;
3079 from_addr = r10_bio->devs[j].addr;
3080 bio->bi_sector = from_addr + rdev->data_offset;
3081 bio->bi_bdev = rdev->bdev;
3082 atomic_inc(&rdev->nr_pending);
3083 /* and we write to 'i' (if not in_sync) */
3085 for (k=0; k<conf->copies; k++)
3086 if (r10_bio->devs[k].devnum == i)
3087 break;
3088 BUG_ON(k == conf->copies);
3089 to_addr = r10_bio->devs[k].addr;
3090 r10_bio->devs[0].devnum = d;
3091 r10_bio->devs[0].addr = from_addr;
3092 r10_bio->devs[1].devnum = i;
3093 r10_bio->devs[1].addr = to_addr;
3095 rdev = mirror->rdev;
3096 if (!test_bit(In_sync, &rdev->flags)) {
3097 bio = r10_bio->devs[1].bio;
3098 bio->bi_next = biolist;
3099 biolist = bio;
3100 bio->bi_private = r10_bio;
3101 bio->bi_end_io = end_sync_write;
3102 bio->bi_rw = WRITE;
3103 bio->bi_sector = to_addr
3104 + rdev->data_offset;
3105 bio->bi_bdev = rdev->bdev;
3106 atomic_inc(&r10_bio->remaining);
3107 } else
3108 r10_bio->devs[1].bio->bi_end_io = NULL;
3110 /* and maybe write to replacement */
3111 bio = r10_bio->devs[1].repl_bio;
3112 if (bio)
3113 bio->bi_end_io = NULL;
3114 rdev = mirror->replacement;
3115 /* Note: if rdev != NULL, then bio
3116 * cannot be NULL as r10buf_pool_alloc will
3117 * have allocated it.
3118 * So the second test here is pointless.
3119 * But it keeps semantic-checkers happy, and
3120 * this comment keeps human reviewers
3121 * happy.
3122 */
3123 if (rdev == NULL || bio == NULL ||
3124 test_bit(Faulty, &rdev->flags))
3125 break;
3126 bio->bi_next = biolist;
3127 biolist = bio;
3128 bio->bi_private = r10_bio;
3129 bio->bi_end_io = end_sync_write;
3130 bio->bi_rw = WRITE;
3131 bio->bi_sector = to_addr + rdev->data_offset;
3132 bio->bi_bdev = rdev->bdev;
3133 atomic_inc(&r10_bio->remaining);
3134 break;
3135 }
3136 if (j == conf->copies) {
3137 /* Cannot recover, so abort the recovery or
3138 * record a bad block */
3139 put_buf(r10_bio);
3140 if (rb2)
3141 atomic_dec(&rb2->remaining);
3142 r10_bio = rb2;
3143 if (any_working) {
3144 /* problem is that there are bad blocks
3145 * on other device(s)
3146 */
3147 int k;
3148 for (k = 0; k < conf->copies; k++)
3149 if (r10_bio->devs[k].devnum == i)
3150 break;
3151 if (!test_bit(In_sync,
3152 &mirror->rdev->flags)
3153 && !rdev_set_badblocks(
3154 mirror->rdev,
3155 r10_bio->devs[k].addr,
3156 max_sync, 0))
3157 any_working = 0;
3158 if (mirror->replacement &&
3159 !rdev_set_badblocks(
3160 mirror->replacement,
3161 r10_bio->devs[k].addr,
3162 max_sync, 0))
3163 any_working = 0;
3164 }
3165 if (!any_working) {
3166 if (!test_and_set_bit(MD_RECOVERY_INTR,
3167 &mddev->recovery))
3168 printk(KERN_INFO "md/raid10:%s: insufficient "
3169 "working devices for recovery.\n",
3170 mdname(mddev));
3171 mirror->recovery_disabled
3172 = mddev->recovery_disabled;
3173 }
3174 break;
3175 }
3176 }
3177 if (biolist == NULL) {
3178 while (r10_bio) {
3179 struct r10bio *rb2 = r10_bio;
3180 r10_bio = (struct r10bio*) rb2->master_bio;
3181 rb2->master_bio = NULL;
3182 put_buf(rb2);
3183 }
3184 goto giveup;
3185 }
3186 } else {
3187 /* resync. Schedule a read for every block at this virt offset */
3188 int count = 0;
3190 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
3192 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
3193 &sync_blocks, mddev->degraded) &&
3194 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3195 &mddev->recovery)) {
3196 /* We can skip this block */
3197 *skipped = 1;
3198 return sync_blocks + sectors_skipped;
3199 }
3200 if (sync_blocks < max_sync)
3201 max_sync = sync_blocks;
3202 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
3204 r10_bio->mddev = mddev;
3205 atomic_set(&r10_bio->remaining, 0);
3206 raise_barrier(conf, 0);
3207 conf->next_resync = sector_nr;
3209 r10_bio->master_bio = NULL;
3210 r10_bio->sector = sector_nr;
3211 set_bit(R10BIO_IsSync, &r10_bio->state);
3212 raid10_find_phys(conf, r10_bio);
3213 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
3215 for (i = 0; i < conf->copies; i++) {
3216 int d = r10_bio->devs[i].devnum;
3217 sector_t first_bad, sector;
3218 int bad_sectors;
3220 if (r10_bio->devs[i].repl_bio)
3221 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3223 bio = r10_bio->devs[i].bio;
3224 bio->bi_end_io = NULL;
3225 clear_bit(BIO_UPTODATE, &bio->bi_flags);
3226 if (conf->mirrors[d].rdev == NULL ||
3227 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
3228 continue;
3229 sector = r10_bio->devs[i].addr;
3230 if (is_badblock(conf->mirrors[d].rdev,
3231 sector, max_sync,
3232 &first_bad, &bad_sectors)) {
3233 if (first_bad > sector)
3234 max_sync = first_bad - sector;
3235 else {
3236 bad_sectors -= (sector - first_bad);
3237 if (max_sync > bad_sectors)
3238 max_sync = bad_sectors;
3239 continue;
3240 }
3241 }
3242 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
3243 atomic_inc(&r10_bio->remaining);
3244 bio->bi_next = biolist;
3245 biolist = bio;
3246 bio->bi_private = r10_bio;
3247 bio->bi_end_io = end_sync_read;
3248 bio->bi_rw = READ;
3249 bio->bi_sector = sector +
3250 conf->mirrors[d].rdev->data_offset;
3251 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
3252 count++;
3254 if (conf->mirrors[d].replacement == NULL ||
3255 test_bit(Faulty,
3256 &conf->mirrors[d].replacement->flags))
3257 continue;
3259 /* Need to set up for writing to the replacement */
3260 bio = r10_bio->devs[i].repl_bio;
3261 clear_bit(BIO_UPTODATE, &bio->bi_flags);
3263 sector = r10_bio->devs[i].addr;
3264 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
3265 bio->bi_next = biolist;
3266 biolist = bio;
3267 bio->bi_private = r10_bio;
3268 bio->bi_end_io = end_sync_write;
3269 bio->bi_rw = WRITE;
3270 bio->bi_sector = sector +
3271 conf->mirrors[d].replacement->data_offset;
3272 bio->bi_bdev = conf->mirrors[d].replacement->bdev;
3273 count++;
3274 }
3276 if (count < 2) {
3277 for (i=0; i<conf->copies; i++) {
3278 int d = r10_bio->devs[i].devnum;
3279 if (r10_bio->devs[i].bio->bi_end_io)
3280 rdev_dec_pending(conf->mirrors[d].rdev,
3281 mddev);
3282 if (r10_bio->devs[i].repl_bio &&
3283 r10_bio->devs[i].repl_bio->bi_end_io)
3284 rdev_dec_pending(
3285 conf->mirrors[d].replacement,
3286 mddev);
3287 }
3288 put_buf(r10_bio);
3289 biolist = NULL;
3290 goto giveup;
3291 }
3292 }
3294 for (bio = biolist; bio ; bio=bio->bi_next) {
3296 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
3297 if (bio->bi_end_io)
3298 bio->bi_flags |= 1 << BIO_UPTODATE;
3299 bio->bi_vcnt = 0;
3300 bio->bi_idx = 0;
3301 bio->bi_phys_segments = 0;
3302 bio->bi_size = 0;
3303 }
3305 nr_sectors = 0;
3306 if (sector_nr + max_sync < max_sector)
3307 max_sector = sector_nr + max_sync;
3308 do {
3309 struct page *page;
3310 int len = PAGE_SIZE;
3311 if (sector_nr + (len>>9) > max_sector)
3312 len = (max_sector - sector_nr) << 9;
3313 if (len == 0)
3314 break;
3315 for (bio= biolist ; bio ; bio=bio->bi_next) {
3316 struct bio *bio2;
3317 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
3318 if (bio_add_page(bio, page, len, 0))
3319 continue;
3321 /* stop here */
3322 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
3323 for (bio2 = biolist;
3324 bio2 && bio2 != bio;
3325 bio2 = bio2->bi_next) {
3326 /* remove last page from this bio */
3327 bio2->bi_vcnt--;
3328 bio2->bi_size -= len;
3329 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
3330 }
3331 goto bio_full;
3332 }
3333 nr_sectors += len>>9;
3334 sector_nr += len>>9;
3335 } while (biolist->bi_vcnt < RESYNC_PAGES);
3336 bio_full:
3337 r10_bio->sectors = nr_sectors;
3339 while (biolist) {
3340 bio = biolist;
3341 biolist = biolist->bi_next;
3343 bio->bi_next = NULL;
3344 r10_bio = bio->bi_private;
3345 r10_bio->sectors = nr_sectors;
3347 if (bio->bi_end_io == end_sync_read) {
3348 md_sync_acct(bio->bi_bdev, nr_sectors);
3349 generic_make_request(bio);
3350 }
3351 }
3353 if (sectors_skipped)
3354 /* pretend they weren't skipped, it makes
3355 * no important difference in this case
3356 */
3357 md_done_sync(mddev, sectors_skipped, 1);
3359 return sectors_skipped + nr_sectors;
3360 giveup:
3361 /* There is nowhere to write, so all non-sync
3362 * drives must be failed or in resync, all drives
3363 * have a bad block, so try the next chunk...
3364 */
3365 if (sector_nr + max_sync < max_sector)
3366 max_sector = sector_nr + max_sync;
3368 sectors_skipped += (max_sector - sector_nr);
3369 chunks_skipped ++;
3370 sector_nr = max_sector;
3371 goto skipped;
3372 }
3374 static sector_t
3375 raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
3376 {
3377 sector_t size;
3378 struct r10conf *conf = mddev->private;
3380 if (!raid_disks)
3381 raid_disks = min(conf->geo.raid_disks,
3382 conf->prev.raid_disks);
3383 if (!sectors)
3384 sectors = conf->dev_sectors;
3386 size = sectors >> conf->geo.chunk_shift;
3387 sector_div(size, conf->geo.far_copies);
3388 size = size * raid_disks;
3389 sector_div(size, conf->geo.near_copies);
3391 return size << conf->geo.chunk_shift;
3392 }
3394 static void calc_sectors(struct r10conf *conf, sector_t size)
3395 {
3396 /* Calculate the number of sectors-per-device that will
3397 * actually be used, and set conf->dev_sectors and
3398 * conf->stride
3399 */
3401 size = size >> conf->geo.chunk_shift;
3402 sector_div(size, conf->geo.far_copies);
3403 size = size * conf->geo.raid_disks;
3404 sector_div(size, conf->geo.near_copies);
3405 /* 'size' is now the number of chunks in the array */
3406 /* calculate "used chunks per device" */
3407 size = size * conf->copies;
3409 /* We need to round up when dividing by raid_disks to
3410 * get the stride size.
3411 */
3412 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
3414 conf->dev_sectors = size << conf->geo.chunk_shift;
3416 if (conf->geo.far_offset)
3417 conf->geo.stride = 1 << conf->geo.chunk_shift;
3418 else {
3419 sector_div(size, conf->geo.far_copies);
3420 conf->geo.stride = size << conf->geo.chunk_shift;
3421 }
3422 }
3424 enum geo_type {geo_new, geo_old, geo_start};
3425 static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3426 {
3427 int nc, fc, fo;
3428 int layout, chunk, disks;
3429 switch (new) {
3430 case geo_old:
3431 layout = mddev->layout;
3432 chunk = mddev->chunk_sectors;
3433 disks = mddev->raid_disks - mddev->delta_disks;
3434 break;
3435 case geo_new:
3436 layout = mddev->new_layout;
3437 chunk = mddev->new_chunk_sectors;
3438 disks = mddev->raid_disks;
3439 break;
3440 default: /* avoid 'may be unused' warnings */
3441 case geo_start: /* new when starting reshape - raid_disks not
3442 * updated yet. */
3443 layout = mddev->new_layout;
3444 chunk = mddev->new_chunk_sectors;
3445 disks = mddev->raid_disks + mddev->delta_disks;
3446 break;
3447 }
3448 if (layout >> 17)
3449 return -1;
3450 if (chunk < (PAGE_SIZE >> 9) ||
3451 !is_power_of_2(chunk))
3452 return -2;
3453 nc = layout & 255;
3454 fc = (layout >> 8) & 255;
3455 fo = layout & (1<<16);
3456 geo->raid_disks = disks;
3457 geo->near_copies = nc;
3458 geo->far_copies = fc;
3459 geo->far_offset = fo;
3460 geo->chunk_mask = chunk - 1;
3461 geo->chunk_shift = ffz(~chunk);
3462 return nc*fc;
3463 }
3465 static struct r10conf *setup_conf(struct mddev *mddev)
3466 {
3467 struct r10conf *conf = NULL;
3468 int err = -EINVAL;
3469 struct geom geo;
3470 int copies;
3472 copies = setup_geo(&geo, mddev, geo_new);
3474 if (copies == -2) {
3475 printk(KERN_ERR "md/raid10:%s: chunk size must be "
3476 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
3477 mdname(mddev), PAGE_SIZE);
3478 goto out;
3479 }
3481 if (copies < 2 || copies > mddev->raid_disks) {
3482 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3483 mdname(mddev), mddev->new_layout);
3484 goto out;
3485 }
3487 err = -ENOMEM;
3488 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
3489 if (!conf)
3490 goto out;
3492 /* FIXME calc properly */
3493 conf->mirrors = kzalloc(sizeof(struct raid10_info)*(mddev->raid_disks +
3494 max(0,mddev->delta_disks)),
3495 GFP_KERNEL);
3496 if (!conf->mirrors)
3497 goto out;
3499 conf->tmppage = alloc_page(GFP_KERNEL);
3500 if (!conf->tmppage)
3501 goto out;
3503 conf->geo = geo;
3504 conf->copies = copies;
3505 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
3506 r10bio_pool_free, conf);
3507 if (!conf->r10bio_pool)
3508 goto out;
3510 calc_sectors(conf, mddev->dev_sectors);
3511 if (mddev->reshape_position == MaxSector) {
3512 conf->prev = conf->geo;
3513 conf->reshape_progress = MaxSector;
3514 } else {
3515 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
3516 err = -EINVAL;
3517 goto out;
3518 }
3519 conf->reshape_progress = mddev->reshape_position;
3520 if (conf->prev.far_offset)
3521 conf->prev.stride = 1 << conf->prev.chunk_shift;
3522 else
3523 /* far_copies must be 1 */
3524 conf->prev.stride = conf->dev_sectors;
3525 }
3526 spin_lock_init(&conf->device_lock);
3527 INIT_LIST_HEAD(&conf->retry_list);
3529 spin_lock_init(&conf->resync_lock);
3530 init_waitqueue_head(&conf->wait_barrier);
3532 conf->thread = md_register_thread(raid10d, mddev, "raid10");
3533 if (!conf->thread)
3534 goto out;
3536 conf->mddev = mddev;
3537 return conf;
3539 out:
3540 if (err == -ENOMEM)
3541 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
3542 mdname(mddev));
3543 if (conf) {
3544 if (conf->r10bio_pool)
3545 mempool_destroy(conf->r10bio_pool);
3546 kfree(conf->mirrors);
3547 safe_put_page(conf->tmppage);
3548 kfree(conf);
3549 }
3550 return ERR_PTR(err);
3551 }
3553 static int run(struct mddev *mddev)
3554 {
3555 struct r10conf *conf;
3556 int i, disk_idx, chunk_size;
3557 struct raid10_info *disk;
3558 struct md_rdev *rdev;
3559 sector_t size;
3560 sector_t min_offset_diff = 0;
3561 int first = 1;
3562 bool discard_supported = false;
3564 if (mddev->private == NULL) {
3565 conf = setup_conf(mddev);
3566 if (IS_ERR(conf))
3567 return PTR_ERR(conf);
3568 mddev->private = conf;
3569 }
3570 conf = mddev->private;
3571 if (!conf)
3572 goto out;
3574 mddev->thread = conf->thread;
3575 conf->thread = NULL;
3577 chunk_size = mddev->chunk_sectors << 9;
3578 if (mddev->queue) {
3579 blk_queue_max_discard_sectors(mddev->queue,
3580 mddev->chunk_sectors);
3581 blk_queue_max_write_same_sectors(mddev->queue,
3582 mddev->chunk_sectors);
3583 blk_queue_io_min(mddev->queue, chunk_size);
3584 if (conf->geo.raid_disks % conf->geo.near_copies)
3585 blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
3586 else
3587 blk_queue_io_opt(mddev->queue, chunk_size *
3588 (conf->geo.raid_disks / conf->geo.near_copies));
3589 }
3591 rdev_for_each(rdev, mddev) {
3592 long long diff;
3593 struct request_queue *q;
3595 disk_idx = rdev->raid_disk;
3596 if (disk_idx < 0)
3597 continue;
3598 if (disk_idx >= conf->geo.raid_disks &&
3599 disk_idx >= conf->prev.raid_disks)
3600 continue;
3601 disk = conf->mirrors + disk_idx;
3603 if (test_bit(Replacement, &rdev->flags)) {
3604 if (disk->replacement)
3605 goto out_free_conf;
3606 disk->replacement = rdev;
3607 } else {
3608 if (disk->rdev)
3609 goto out_free_conf;
3610 disk->rdev = rdev;
3611 }
3612 q = bdev_get_queue(rdev->bdev);
3613 if (q->merge_bvec_fn)
3614 mddev->merge_check_needed = 1;
3615 diff = (rdev->new_data_offset - rdev->data_offset);
3616 if (!mddev->reshape_backwards)
3617 diff = -diff;
3618 if (diff < 0)
3619 diff = 0;
3620 if (first || diff < min_offset_diff)
3621 min_offset_diff = diff;
3623 if (mddev->gendisk)
3624 disk_stack_limits(mddev->gendisk, rdev->bdev,
3625 rdev->data_offset << 9);
3627 disk->head_position = 0;
3629 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3630 discard_supported = true;
3631 }
3633 if (mddev->queue) {
3634 if (discard_supported)
3635 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
3636 mddev->queue);
3637 else
3638 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
3639 mddev->queue);
3640 }
3641 /* need to check that every block has at least one working mirror */
3642 if (!enough(conf, -1)) {
3643 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
3644 mdname(mddev));
3645 goto out_free_conf;
3646 }
3648 if (conf->reshape_progress != MaxSector) {
3649 /* must ensure that shape change is supported */
3650 if (conf->geo.far_copies != 1 &&
3651 conf->geo.far_offset == 0)
3652 goto out_free_conf;
3653 if (conf->prev.far_copies != 1 &&
3654 conf->geo.far_offset == 0)
3655 goto out_free_conf;
3656 }
3658 mddev->degraded = 0;
3659 for (i = 0;
3660 i < conf->geo.raid_disks
3661 || i < conf->prev.raid_disks;
3662 i++) {
3664 disk = conf->mirrors + i;
3666 if (!disk->rdev && disk->replacement) {
3667 /* The replacement is all we have - use it */
3668 disk->rdev = disk->replacement;
3669 disk->replacement = NULL;
3670 clear_bit(Replacement, &disk->rdev->flags);
3671 }
3673 if (!disk->rdev ||
3674 !test_bit(In_sync, &disk->rdev->flags)) {
3675 disk->head_position = 0;
3676 mddev->degraded++;
3677 if (disk->rdev)
3678 conf->fullsync = 1;
3679 }
3680 disk->recovery_disabled = mddev->recovery_disabled - 1;
3681 }
3683 if (mddev->recovery_cp != MaxSector)
3684 printk(KERN_NOTICE "md/raid10:%s: not clean"
3685 " -- starting background reconstruction\n",
3686 mdname(mddev));
3687 printk(KERN_INFO
3688 "md/raid10:%s: active with %d out of %d devices\n",
3689 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
3690 conf->geo.raid_disks);
3691 /*
3692 * Ok, everything is just fine now
3693 */
3694 mddev->dev_sectors = conf->dev_sectors;
3695 size = raid10_size(mddev, 0, 0);
3696 md_set_array_sectors(mddev, size);
3697 mddev->resync_max_sectors = size;
3699 if (mddev->queue) {
3700 int stripe = conf->geo.raid_disks *
3701 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
3702 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
3703 mddev->queue->backing_dev_info.congested_data = mddev;
3705 /* Calculate max read-ahead size.
3706 * We need to readahead at least twice a whole stripe....
3707 * maybe...
3708 */
3709 stripe /= conf->geo.near_copies;
3710 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3711 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
3712 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
3713 }
3716 if (md_integrity_register(mddev))
3717 goto out_free_conf;
3719 if (conf->reshape_progress != MaxSector) {
3720 unsigned long before_length, after_length;
3722 before_length = ((1 << conf->prev.chunk_shift) *
3723 conf->prev.far_copies);
3724 after_length = ((1 << conf->geo.chunk_shift) *
3725 conf->geo.far_copies);
3727 if (max(before_length, after_length) > min_offset_diff) {
3728 /* This cannot work */
3729 printk("md/raid10: offset difference not enough to continue reshape\n");
3730 goto out_free_conf;
3731 }
3732 conf->offset_diff = min_offset_diff;
3734 conf->reshape_safe = conf->reshape_progress;
3735 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3736 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3737 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3738 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3739 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3740 "reshape");
3741 }
3743 return 0;
3745 out_free_conf:
3746 md_unregister_thread(&mddev->thread);
3747 if (conf->r10bio_pool)
3748 mempool_destroy(conf->r10bio_pool);
3749 safe_put_page(conf->tmppage);
3750 kfree(conf->mirrors);
3751 kfree(conf);
3752 mddev->private = NULL;
3753 out:
3754 return -EIO;
3755 }
3757 static int stop(struct mddev *mddev)
3758 {
3759 struct r10conf *conf = mddev->private;
3761 raise_barrier(conf, 0);
3762 lower_barrier(conf);
3764 md_unregister_thread(&mddev->thread);
3765 if (mddev->queue)
3766 /* the unplug fn references 'conf'*/
3767 blk_sync_queue(mddev->queue);
3769 if (conf->r10bio_pool)
3770 mempool_destroy(conf->r10bio_pool);
3771 kfree(conf->mirrors);
3772 kfree(conf);
3773 mddev->private = NULL;
3774 return 0;
3775 }
3777 static void raid10_quiesce(struct mddev *mddev, int state)
3778 {
3779 struct r10conf *conf = mddev->private;
3781 switch(state) {
3782 case 1:
3783 raise_barrier(conf, 0);
3784 break;
3785 case 0:
3786 lower_barrier(conf);
3787 break;
3788 }
3789 }
3791 static int raid10_resize(struct mddev *mddev, sector_t sectors)
3792 {
3793 /* Resize of 'far' arrays is not supported.
3794 * For 'near' and 'offset' arrays we can set the
3795 * number of sectors used to be an appropriate multiple
3796 * of the chunk size.
3797 * For 'offset', this is far_copies*chunksize.
3798 * For 'near' the multiplier is the LCM of
3799 * near_copies and raid_disks.
3800 * So if far_copies > 1 && !far_offset, fail.
3801 * Else find LCM(raid_disks, near_copy)*far_copies and
3802 * multiply by chunk_size. Then round to this number.
3803 * This is mostly done by raid10_size()
3804 */
3805 struct r10conf *conf = mddev->private;
3806 sector_t oldsize, size;
3808 if (mddev->reshape_position != MaxSector)
3809 return -EBUSY;
3811 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
3812 return -EINVAL;
3814 oldsize = raid10_size(mddev, 0, 0);
3815 size = raid10_size(mddev, sectors, 0);
3816 if (mddev->external_size &&
3817 mddev->array_sectors > size)
3818 return -EINVAL;
3819 if (mddev->bitmap) {
3820 int ret = bitmap_resize(mddev->bitmap, size, 0, 0);
3821 if (ret)
3822 return ret;
3823 }
3824 md_set_array_sectors(mddev, size);
3825 set_capacity(mddev->gendisk, mddev->array_sectors);
3826 revalidate_disk(mddev->gendisk);
3827 if (sectors > mddev->dev_sectors &&
3828 mddev->recovery_cp > oldsize) {
3829 mddev->recovery_cp = oldsize;
3830 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3831 }
3832 calc_sectors(conf, sectors);
3833 mddev->dev_sectors = conf->dev_sectors;
3834 mddev->resync_max_sectors = size;
3835 return 0;
3836 }
3838 static void *raid10_takeover_raid0(struct mddev *mddev)
3839 {
3840 struct md_rdev *rdev;
3841 struct r10conf *conf;
3843 if (mddev->degraded > 0) {
3844 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
3845 mdname(mddev));
3846 return ERR_PTR(-EINVAL);
3847 }
3849 /* Set new parameters */
3850 mddev->new_level = 10;
3851 /* new layout: far_copies = 1, near_copies = 2 */
3852 mddev->new_layout = (1<<8) + 2;
3853 mddev->new_chunk_sectors = mddev->chunk_sectors;
3854 mddev->delta_disks = mddev->raid_disks;
3855 mddev->raid_disks *= 2;
3856 /* make sure it will be not marked as dirty */
3857 mddev->recovery_cp = MaxSector;
3859 conf = setup_conf(mddev);
3860 if (!IS_ERR(conf)) {
3861 rdev_for_each(rdev, mddev)
3862 if (rdev->raid_disk >= 0)
3863 rdev->new_raid_disk = rdev->raid_disk * 2;
3864 conf->barrier = 1;
3865 }
3867 return conf;
3868 }
3870 static void *raid10_takeover(struct mddev *mddev)
3871 {
3872 struct r0conf *raid0_conf;
3874 /* raid10 can take over:
3875 * raid0 - providing it has only two drives
3876 */
3877 if (mddev->level == 0) {
3878 /* for raid0 takeover only one zone is supported */
3879 raid0_conf = mddev->private;
3880 if (raid0_conf->nr_strip_zones > 1) {
3881 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
3882 " with more than one zone.\n",
3883 mdname(mddev));
3884 return ERR_PTR(-EINVAL);
3885 }
3886 return raid10_takeover_raid0(mddev);
3887 }
3888 return ERR_PTR(-EINVAL);
3889 }
3891 static int raid10_check_reshape(struct mddev *mddev)
3892 {
3893 /* Called when there is a request to change
3894 * - layout (to ->new_layout)
3895 * - chunk size (to ->new_chunk_sectors)
3896 * - raid_disks (by delta_disks)
3897 * or when trying to restart a reshape that was ongoing.
3898 *
3899 * We need to validate the request and possibly allocate
3900 * space if that might be an issue later.
3901 *
3902 * Currently we reject any reshape of a 'far' mode array,
3903 * allow chunk size to change if new is generally acceptable,
3904 * allow raid_disks to increase, and allow
3905 * a switch between 'near' mode and 'offset' mode.
3906 */
3907 struct r10conf *conf = mddev->private;
3908 struct geom geo;
3910 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
3911 return -EINVAL;
3913 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
3914 /* mustn't change number of copies */
3915 return -EINVAL;
3916 if (geo.far_copies > 1 && !geo.far_offset)
3917 /* Cannot switch to 'far' mode */
3918 return -EINVAL;
3920 if (mddev->array_sectors & geo.chunk_mask)
3921 /* not factor of array size */
3922 return -EINVAL;
3924 if (!enough(conf, -1))
3925 return -EINVAL;
3927 kfree(conf->mirrors_new);
3928 conf->mirrors_new = NULL;
3929 if (mddev->delta_disks > 0) {
3930 /* allocate new 'mirrors' list */
3931 conf->mirrors_new = kzalloc(
3932 sizeof(struct raid10_info)
3933 *(mddev->raid_disks +
3934 mddev->delta_disks),
3935 GFP_KERNEL);
3936 if (!conf->mirrors_new)
3937 return -ENOMEM;
3938 }
3939 return 0;
3940 }
3942 /*
3943 * Need to check if array has failed when deciding whether to:
3944 * - start an array
3945 * - remove non-faulty devices
3946 * - add a spare
3947 * - allow a reshape
3948 * This determination is simple when no reshape is happening.
3949 * However if there is a reshape, we need to carefully check
3950 * both the before and after sections.
3951 * This is because some failed devices may only affect one
3952 * of the two sections, and some non-in_sync devices may
3953 * be insync in the section most affected by failed devices.
3954 */
3955 static int calc_degraded(struct r10conf *conf)
3956 {
3957 int degraded, degraded2;
3958 int i;
3960 rcu_read_lock();
3961 degraded = 0;
3962 /* 'prev' section first */
3963 for (i = 0; i < conf->prev.raid_disks; i++) {
3964 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
3965 if (!rdev || test_bit(Faulty, &rdev->flags))
3966 degraded++;
3967 else if (!test_bit(In_sync, &rdev->flags))
3968 /* When we can reduce the number of devices in
3969 * an array, this might not contribute to
3970 * 'degraded'. It does now.
3971 */
3972 degraded++;
3973 }
3974 rcu_read_unlock();
3975 if (conf->geo.raid_disks == conf->prev.raid_disks)
3976 return degraded;
3977 rcu_read_lock();
3978 degraded2 = 0;
3979 for (i = 0; i < conf->geo.raid_disks; i++) {
3980 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
3981 if (!rdev || test_bit(Faulty, &rdev->flags))
3982 degraded2++;
3983 else if (!test_bit(In_sync, &rdev->flags)) {
3984 /* If reshape is increasing the number of devices,
3985 * this section has already been recovered, so
3986 * it doesn't contribute to degraded.
3987 * else it does.
3988 */
3989 if (conf->geo.raid_disks <= conf->prev.raid_disks)
3990 degraded2++;
3991 }
3992 }
3993 rcu_read_unlock();
3994 if (degraded2 > degraded)
3995 return degraded2;
3996 return degraded;
3997 }
3999 static int raid10_start_reshape(struct mddev *mddev)
4000 {
4001 /* A 'reshape' has been requested. This commits
4002 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4003 * This also checks if there are enough spares and adds them
4004 * to the array.
4005 * We currently require enough spares to make the final
4006 * array non-degraded. We also require that the difference
4007 * between old and new data_offset - on each device - is
4008 * enough that we never risk over-writing.
4009 */
4011 unsigned long before_length, after_length;
4012 sector_t min_offset_diff = 0;
4013 int first = 1;
4014 struct geom new;
4015 struct r10conf *conf = mddev->private;
4016 struct md_rdev *rdev;
4017 int spares = 0;
4018 int ret;
4020 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4021 return -EBUSY;
4023 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4024 return -EINVAL;
4026 before_length = ((1 << conf->prev.chunk_shift) *
4027 conf->prev.far_copies);
4028 after_length = ((1 << conf->geo.chunk_shift) *
4029 conf->geo.far_copies);
4031 rdev_for_each(rdev, mddev) {
4032 if (!test_bit(In_sync, &rdev->flags)
4033 && !test_bit(Faulty, &rdev->flags))
4034 spares++;
4035 if (rdev->raid_disk >= 0) {
4036 long long diff = (rdev->new_data_offset
4037 - rdev->data_offset);
4038 if (!mddev->reshape_backwards)
4039 diff = -diff;
4040 if (diff < 0)
4041 diff = 0;
4042 if (first || diff < min_offset_diff)
4043 min_offset_diff = diff;
4044 }
4045 }
4047 if (max(before_length, after_length) > min_offset_diff)
4048 return -EINVAL;
4050 if (spares < mddev->delta_disks)
4051 return -EINVAL;
4053 conf->offset_diff = min_offset_diff;
4054 spin_lock_irq(&conf->device_lock);
4055 if (conf->mirrors_new) {
4056 memcpy(conf->mirrors_new, conf->mirrors,
4057 sizeof(struct raid10_info)*conf->prev.raid_disks);
4058 smp_mb();
4059 kfree(conf->mirrors_old); /* FIXME and elsewhere */
4060 conf->mirrors_old = conf->mirrors;
4061 conf->mirrors = conf->mirrors_new;
4062 conf->mirrors_new = NULL;
4063 }
4064 setup_geo(&conf->geo, mddev, geo_start);
4065 smp_mb();
4066 if (mddev->reshape_backwards) {
4067 sector_t size = raid10_size(mddev, 0, 0);
4068 if (size < mddev->array_sectors) {
4069 spin_unlock_irq(&conf->device_lock);
4070 printk(KERN_ERR "md/raid10:%s: array size must be reduce before number of disks\n",
4071 mdname(mddev));
4072 return -EINVAL;
4073 }
4074 mddev->resync_max_sectors = size;
4075 conf->reshape_progress = size;
4076 } else
4077 conf->reshape_progress = 0;
4078 spin_unlock_irq(&conf->device_lock);
4080 if (mddev->delta_disks && mddev->bitmap) {
4081 ret = bitmap_resize(mddev->bitmap,
4082 raid10_size(mddev, 0,
4083 conf->geo.raid_disks),
4084 0, 0);
4085 if (ret)
4086 goto abort;
4087 }
4088 if (mddev->delta_disks > 0) {
4089 rdev_for_each(rdev, mddev)
4090 if (rdev->raid_disk < 0 &&
4091 !test_bit(Faulty, &rdev->flags)) {
4092 if (raid10_add_disk(mddev, rdev) == 0) {
4093 if (rdev->raid_disk >=
4094 conf->prev.raid_disks)
4095 set_bit(In_sync, &rdev->flags);
4096 else
4097 rdev->recovery_offset = 0;
4099 if (sysfs_link_rdev(mddev, rdev))
4100 /* Failure here is OK */;
4101 }
4102 } else if (rdev->raid_disk >= conf->prev.raid_disks
4103 && !test_bit(Faulty, &rdev->flags)) {
4104 /* This is a spare that was manually added */
4105 set_bit(In_sync, &rdev->flags);
4106 }
4107 }
4108 /* When a reshape changes the number of devices,
4109 * ->degraded is measured against the larger of the
4110 * pre and post numbers.
4111 */
4112 spin_lock_irq(&conf->device_lock);
4113 mddev->degraded = calc_degraded(conf);
4114 spin_unlock_irq(&conf->device_lock);
4115 mddev->raid_disks = conf->geo.raid_disks;
4116 mddev->reshape_position = conf->reshape_progress;
4117 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4119 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4120 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4121 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4122 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4124 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4125 "reshape");
4126 if (!mddev->sync_thread) {
4127 ret = -EAGAIN;
4128 goto abort;
4129 }
4130 conf->reshape_checkpoint = jiffies;
4131 md_wakeup_thread(mddev->sync_thread);
4132 md_new_event(mddev);
4133 return 0;
4135 abort:
4136 mddev->recovery = 0;
4137 spin_lock_irq(&conf->device_lock);
4138 conf->geo = conf->prev;
4139 mddev->raid_disks = conf->geo.raid_disks;
4140 rdev_for_each(rdev, mddev)
4141 rdev->new_data_offset = rdev->data_offset;
4142 smp_wmb();
4143 conf->reshape_progress = MaxSector;
4144 mddev->reshape_position = MaxSector;
4145 spin_unlock_irq(&conf->device_lock);
4146 return ret;
4147 }
4149 /* Calculate the last device-address that could contain
4150 * any block from the chunk that includes the array-address 's'
4151 * and report the next address.
4152 * i.e. the address returned will be chunk-aligned and after
4153 * any data that is in the chunk containing 's'.
4154 */
4155 static sector_t last_dev_address(sector_t s, struct geom *geo)
4156 {
4157 s = (s | geo->chunk_mask) + 1;
4158 s >>= geo->chunk_shift;
4159 s *= geo->near_copies;
4160 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4161 s *= geo->far_copies;
4162 s <<= geo->chunk_shift;
4163 return s;
4164 }
4166 /* Calculate the first device-address that could contain
4167 * any block from the chunk that includes the array-address 's'.
4168 * This too will be the start of a chunk
4169 */
4170 static sector_t first_dev_address(sector_t s, struct geom *geo)
4171 {
4172 s >>= geo->chunk_shift;
4173 s *= geo->near_copies;
4174 sector_div(s, geo->raid_disks);
4175 s *= geo->far_copies;
4176 s <<= geo->chunk_shift;
4177 return s;
4178 }
4180 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4181 int *skipped)
4182 {
4183 /* We simply copy at most one chunk (smallest of old and new)
4184 * at a time, possibly less if that exceeds RESYNC_PAGES,
4185 * or we hit a bad block or something.
4186 * This might mean we pause for normal IO in the middle of
4187 * a chunk, but that is not a problem was mddev->reshape_position
4188 * can record any location.
4189 *
4190 * If we will want to write to a location that isn't
4191 * yet recorded as 'safe' (i.e. in metadata on disk) then
4192 * we need to flush all reshape requests and update the metadata.
4193 *
4194 * When reshaping forwards (e.g. to more devices), we interpret
4195 * 'safe' as the earliest block which might not have been copied
4196 * down yet. We divide this by previous stripe size and multiply
4197 * by previous stripe length to get lowest device offset that we
4198 * cannot write to yet.
4199 * We interpret 'sector_nr' as an address that we want to write to.
4200 * From this we use last_device_address() to find where we might
4201 * write to, and first_device_address on the 'safe' position.
4202 * If this 'next' write position is after the 'safe' position,
4203 * we must update the metadata to increase the 'safe' position.
4204 *
4205 * When reshaping backwards, we round in the opposite direction
4206 * and perform the reverse test: next write position must not be
4207 * less than current safe position.
4208 *
4209 * In all this the minimum difference in data offsets
4210 * (conf->offset_diff - always positive) allows a bit of slack,
4211 * so next can be after 'safe', but not by more than offset_disk
4212 *
4213 * We need to prepare all the bios here before we start any IO
4214 * to ensure the size we choose is acceptable to all devices.
4215 * The means one for each copy for write-out and an extra one for
4216 * read-in.
4217 * We store the read-in bio in ->master_bio and the others in
4218 * ->devs[x].bio and ->devs[x].repl_bio.
4219 */
4220 struct r10conf *conf = mddev->private;
4221 struct r10bio *r10_bio;
4222 sector_t next, safe, last;
4223 int max_sectors;
4224 int nr_sectors;
4225 int s;
4226 struct md_rdev *rdev;
4227 int need_flush = 0;
4228 struct bio *blist;
4229 struct bio *bio, *read_bio;
4230 int sectors_done = 0;
4232 if (sector_nr == 0) {
4233 /* If restarting in the middle, skip the initial sectors */
4234 if (mddev->reshape_backwards &&
4235 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4236 sector_nr = (raid10_size(mddev, 0, 0)
4237 - conf->reshape_progress);
4238 } else if (!mddev->reshape_backwards &&
4239 conf->reshape_progress > 0)
4240 sector_nr = conf->reshape_progress;
4241 if (sector_nr) {
4242 mddev->curr_resync_completed = sector_nr;
4243 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4244 *skipped = 1;
4245 return sector_nr;
4246 }
4247 }
4249 /* We don't use sector_nr to track where we are up to
4250 * as that doesn't work well for ->reshape_backwards.
4251 * So just use ->reshape_progress.
4252 */
4253 if (mddev->reshape_backwards) {
4254 /* 'next' is the earliest device address that we might
4255 * write to for this chunk in the new layout
4256 */
4257 next = first_dev_address(conf->reshape_progress - 1,
4258 &conf->geo);
4260 /* 'safe' is the last device address that we might read from
4261 * in the old layout after a restart
4262 */
4263 safe = last_dev_address(conf->reshape_safe - 1,
4264 &conf->prev);
4266 if (next + conf->offset_diff < safe)
4267 need_flush = 1;
4269 last = conf->reshape_progress - 1;
4270 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4271 & conf->prev.chunk_mask);
4272 if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
4273 sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
4274 } else {
4275 /* 'next' is after the last device address that we
4276 * might write to for this chunk in the new layout
4277 */
4278 next = last_dev_address(conf->reshape_progress, &conf->geo);
4280 /* 'safe' is the earliest device address that we might
4281 * read from in the old layout after a restart
4282 */
4283 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4285 /* Need to update metadata if 'next' might be beyond 'safe'
4286 * as that would possibly corrupt data
4287 */
4288 if (next > safe + conf->offset_diff)
4289 need_flush = 1;
4291 sector_nr = conf->reshape_progress;
4292 last = sector_nr | (conf->geo.chunk_mask
4293 & conf->prev.chunk_mask);
4295 if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
4296 last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
4297 }
4299 if (need_flush ||
4300 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4301 /* Need to update reshape_position in metadata */
4302 wait_barrier(conf);
4303 mddev->reshape_position = conf->reshape_progress;
4304 if (mddev->reshape_backwards)
4305 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4306 - conf->reshape_progress;
4307 else
4308 mddev->curr_resync_completed = conf->reshape_progress;
4309 conf->reshape_checkpoint = jiffies;
4310 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4311 md_wakeup_thread(mddev->thread);
4312 wait_event(mddev->sb_wait, mddev->flags == 0 ||
4313 kthread_should_stop());
4314 conf->reshape_safe = mddev->reshape_position;
4315 allow_barrier(conf);
4316 }
4318 read_more:
4319 /* Now schedule reads for blocks from sector_nr to last */
4320 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
4321 raise_barrier(conf, sectors_done != 0);
4322 atomic_set(&r10_bio->remaining, 0);
4323 r10_bio->mddev = mddev;
4324 r10_bio->sector = sector_nr;
4325 set_bit(R10BIO_IsReshape, &r10_bio->state);
4326 r10_bio->sectors = last - sector_nr + 1;
4327 rdev = read_balance(conf, r10_bio, &max_sectors);
4328 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4330 if (!rdev) {
4331 /* Cannot read from here, so need to record bad blocks
4332 * on all the target devices.
4333 */
4334 // FIXME
4335 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4336 return sectors_done;
4337 }
4339 read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
4341 read_bio->bi_bdev = rdev->bdev;
4342 read_bio->bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
4343 + rdev->data_offset);
4344 read_bio->bi_private = r10_bio;
4345 read_bio->bi_end_io = end_sync_read;
4346 read_bio->bi_rw = READ;
4347 read_bio->bi_flags &= ~(BIO_POOL_MASK - 1);
4348 read_bio->bi_flags |= 1 << BIO_UPTODATE;
4349 read_bio->bi_vcnt = 0;
4350 read_bio->bi_idx = 0;
4351 read_bio->bi_size = 0;
4352 r10_bio->master_bio = read_bio;
4353 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4355 /* Now find the locations in the new layout */
4356 __raid10_find_phys(&conf->geo, r10_bio);
4358 blist = read_bio;
4359 read_bio->bi_next = NULL;
4361 for (s = 0; s < conf->copies*2; s++) {
4362 struct bio *b;
4363 int d = r10_bio->devs[s/2].devnum;
4364 struct md_rdev *rdev2;
4365 if (s&1) {
4366 rdev2 = conf->mirrors[d].replacement;
4367 b = r10_bio->devs[s/2].repl_bio;
4368 } else {
4369 rdev2 = conf->mirrors[d].rdev;
4370 b = r10_bio->devs[s/2].bio;
4371 }
4372 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4373 continue;
4374 b->bi_bdev = rdev2->bdev;
4375 b->bi_sector = r10_bio->devs[s/2].addr + rdev2->new_data_offset;
4376 b->bi_private = r10_bio;
4377 b->bi_end_io = end_reshape_write;
4378 b->bi_rw = WRITE;
4379 b->bi_flags &= ~(BIO_POOL_MASK - 1);
4380 b->bi_flags |= 1 << BIO_UPTODATE;
4381 b->bi_next = blist;
4382 b->bi_vcnt = 0;
4383 b->bi_idx = 0;
4384 b->bi_size = 0;
4385 blist = b;
4386 }
4388 /* Now add as many pages as possible to all of these bios. */
4390 nr_sectors = 0;
4391 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
4392 struct page *page = r10_bio->devs[0].bio->bi_io_vec[s/(PAGE_SIZE>>9)].bv_page;
4393 int len = (max_sectors - s) << 9;
4394 if (len > PAGE_SIZE)
4395 len = PAGE_SIZE;
4396 for (bio = blist; bio ; bio = bio->bi_next) {
4397 struct bio *bio2;
4398 if (bio_add_page(bio, page, len, 0))
4399 continue;
4401 /* Didn't fit, must stop */
4402 for (bio2 = blist;
4403 bio2 && bio2 != bio;
4404 bio2 = bio2->bi_next) {
4405 /* Remove last page from this bio */
4406 bio2->bi_vcnt--;
4407 bio2->bi_size -= len;
4408 bio2->bi_flags &= ~(1<<BIO_SEG_VALID);
4409 }
4410 goto bio_full;
4411 }
4412 sector_nr += len >> 9;
4413 nr_sectors += len >> 9;
4414 }
4415 bio_full:
4416 r10_bio->sectors = nr_sectors;
4418 /* Now submit the read */
4419 md_sync_acct(read_bio->bi_bdev, r10_bio->sectors);
4420 atomic_inc(&r10_bio->remaining);
4421 read_bio->bi_next = NULL;
4422 generic_make_request(read_bio);
4423 sector_nr += nr_sectors;
4424 sectors_done += nr_sectors;
4425 if (sector_nr <= last)
4426 goto read_more;
4428 /* Now that we have done the whole section we can
4429 * update reshape_progress
4430 */
4431 if (mddev->reshape_backwards)
4432 conf->reshape_progress -= sectors_done;
4433 else
4434 conf->reshape_progress += sectors_done;
4436 return sectors_done;
4437 }
4439 static void end_reshape_request(struct r10bio *r10_bio);
4440 static int handle_reshape_read_error(struct mddev *mddev,
4441 struct r10bio *r10_bio);
4442 static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
4443 {
4444 /* Reshape read completed. Hopefully we have a block
4445 * to write out.
4446 * If we got a read error then we do sync 1-page reads from
4447 * elsewhere until we find the data - or give up.
4448 */
4449 struct r10conf *conf = mddev->private;
4450 int s;
4452 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
4453 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
4454 /* Reshape has been aborted */
4455 md_done_sync(mddev, r10_bio->sectors, 0);
4456 return;
4457 }
4459 /* We definitely have the data in the pages, schedule the
4460 * writes.
4461 */
4462 atomic_set(&r10_bio->remaining, 1);
4463 for (s = 0; s < conf->copies*2; s++) {
4464 struct bio *b;
4465 int d = r10_bio->devs[s/2].devnum;
4466 struct md_rdev *rdev;
4467 if (s&1) {
4468 rdev = conf->mirrors[d].replacement;
4469 b = r10_bio->devs[s/2].repl_bio;
4470 } else {
4471 rdev = conf->mirrors[d].rdev;
4472 b = r10_bio->devs[s/2].bio;
4473 }
4474 if (!rdev || test_bit(Faulty, &rdev->flags))
4475 continue;
4476 atomic_inc(&rdev->nr_pending);
4477 md_sync_acct(b->bi_bdev, r10_bio->sectors);
4478 atomic_inc(&r10_bio->remaining);
4479 b->bi_next = NULL;
4480 generic_make_request(b);
4481 }
4482 end_reshape_request(r10_bio);
4483 }
4485 static void end_reshape(struct r10conf *conf)
4486 {
4487 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
4488 return;
4490 spin_lock_irq(&conf->device_lock);
4491 conf->prev = conf->geo;
4492 md_finish_reshape(conf->mddev);
4493 smp_wmb();
4494 conf->reshape_progress = MaxSector;
4495 spin_unlock_irq(&conf->device_lock);
4497 /* read-ahead size must cover two whole stripes, which is
4498 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4499 */
4500 if (conf->mddev->queue) {
4501 int stripe = conf->geo.raid_disks *
4502 ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
4503 stripe /= conf->geo.near_copies;
4504 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4505 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4506 }
4507 conf->fullsync = 0;
4508 }
4511 static int handle_reshape_read_error(struct mddev *mddev,
4512 struct r10bio *r10_bio)
4513 {
4514 /* Use sync reads to get the blocks from somewhere else */
4515 int sectors = r10_bio->sectors;
4516 struct r10conf *conf = mddev->private;
4517 struct {
4518 struct r10bio r10_bio;
4519 struct r10dev devs[conf->copies];
4520 } on_stack;
4521 struct r10bio *r10b = &on_stack.r10_bio;
4522 int slot = 0;
4523 int idx = 0;
4524 struct bio_vec *bvec = r10_bio->master_bio->bi_io_vec;
4526 r10b->sector = r10_bio->sector;
4527 __raid10_find_phys(&conf->prev, r10b);
4529 while (sectors) {
4530 int s = sectors;
4531 int success = 0;
4532 int first_slot = slot;
4534 if (s > (PAGE_SIZE >> 9))
4535 s = PAGE_SIZE >> 9;
4537 while (!success) {
4538 int d = r10b->devs[slot].devnum;
4539 struct md_rdev *rdev = conf->mirrors[d].rdev;
4540 sector_t addr;
4541 if (rdev == NULL ||
4542 test_bit(Faulty, &rdev->flags) ||
4543 !test_bit(In_sync, &rdev->flags))
4544 goto failed;
4546 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
4547 success = sync_page_io(rdev,
4548 addr,
4549 s << 9,
4550 bvec[idx].bv_page,
4551 READ, false);
4552 if (success)
4553 break;
4554 failed:
4555 slot++;
4556 if (slot >= conf->copies)
4557 slot = 0;
4558 if (slot == first_slot)
4559 break;
4560 }
4561 if (!success) {
4562 /* couldn't read this block, must give up */
4563 set_bit(MD_RECOVERY_INTR,
4564 &mddev->recovery);
4565 return -EIO;
4566 }
4567 sectors -= s;
4568 idx++;
4569 }
4570 return 0;
4571 }
4573 static void end_reshape_write(struct bio *bio, int error)
4574 {
4575 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
4576 struct r10bio *r10_bio = bio->bi_private;
4577 struct mddev *mddev = r10_bio->mddev;
4578 struct r10conf *conf = mddev->private;
4579 int d;
4580 int slot;
4581 int repl;
4582 struct md_rdev *rdev = NULL;
4584 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
4585 if (repl)
4586 rdev = conf->mirrors[d].replacement;
4587 if (!rdev) {
4588 smp_mb();
4589 rdev = conf->mirrors[d].rdev;
4590 }
4592 if (!uptodate) {
4593 /* FIXME should record badblock */
4594 md_error(mddev, rdev);
4595 }
4597 rdev_dec_pending(rdev, mddev);
4598 end_reshape_request(r10_bio);
4599 }
4601 static void end_reshape_request(struct r10bio *r10_bio)
4602 {
4603 if (!atomic_dec_and_test(&r10_bio->remaining))
4604 return;
4605 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
4606 bio_put(r10_bio->master_bio);
4607 put_buf(r10_bio);
4608 }
4610 static void raid10_finish_reshape(struct mddev *mddev)
4611 {
4612 struct r10conf *conf = mddev->private;
4614 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4615 return;
4617 if (mddev->delta_disks > 0) {
4618 sector_t size = raid10_size(mddev, 0, 0);
4619 md_set_array_sectors(mddev, size);
4620 if (mddev->recovery_cp > mddev->resync_max_sectors) {
4621 mddev->recovery_cp = mddev->resync_max_sectors;
4622 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4623 }
4624 mddev->resync_max_sectors = size;
4625 set_capacity(mddev->gendisk, mddev->array_sectors);
4626 revalidate_disk(mddev->gendisk);
4627 } else {
4628 int d;
4629 for (d = conf->geo.raid_disks ;
4630 d < conf->geo.raid_disks - mddev->delta_disks;
4631 d++) {
4632 struct md_rdev *rdev = conf->mirrors[d].rdev;
4633 if (rdev)
4634 clear_bit(In_sync, &rdev->flags);
4635 rdev = conf->mirrors[d].replacement;
4636 if (rdev)
4637 clear_bit(In_sync, &rdev->flags);
4638 }
4639 }
4640 mddev->layout = mddev->new_layout;
4641 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
4642 mddev->reshape_position = MaxSector;
4643 mddev->delta_disks = 0;
4644 mddev->reshape_backwards = 0;
4645 }
4647 static struct md_personality raid10_personality =
4648 {
4649 .name = "raid10",
4650 .level = 10,
4651 .owner = THIS_MODULE,
4652 .make_request = make_request,
4653 .run = run,
4654 .stop = stop,
4655 .status = status,
4656 .error_handler = error,
4657 .hot_add_disk = raid10_add_disk,
4658 .hot_remove_disk= raid10_remove_disk,
4659 .spare_active = raid10_spare_active,
4660 .sync_request = sync_request,
4661 .quiesce = raid10_quiesce,
4662 .size = raid10_size,
4663 .resize = raid10_resize,
4664 .takeover = raid10_takeover,
4665 .check_reshape = raid10_check_reshape,
4666 .start_reshape = raid10_start_reshape,
4667 .finish_reshape = raid10_finish_reshape,
4668 };
4670 static int __init raid_init(void)
4671 {
4672 return register_md_personality(&raid10_personality);
4673 }
4675 static void raid_exit(void)
4676 {
4677 unregister_md_personality(&raid10_personality);
4678 }
4680 module_init(raid_init);
4681 module_exit(raid_exit);
4682 MODULE_LICENSE("GPL");
4683 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
4684 MODULE_ALIAS("md-personality-9"); /* RAID10 */
4685 MODULE_ALIAS("md-raid10");
4686 MODULE_ALIAS("md-level-10");
4688 module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);