2 * raid10.c : Multiple Devices driver for Linux
4 * Copyright (C) 2000-2004 Neil Brown
6 * RAID-10 support for md.
8 * Base on code in raid1.c. See raid1.c for further copyright information.
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)
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.
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 <trace/events/block.h>
35 * RAID10 provides a combination of RAID0 and RAID1 functionality.
36 * The layout of data is defined by
39 * near_copies (stored in low byte of layout)
40 * far_copies (stored in second byte of layout)
41 * far_offset (stored in bit 16 of layout )
42 * use_far_sets (stored in bit 17 of layout )
43 * use_far_sets_bugfixed (stored in bit 18 of layout )
45 * The data to be stored is divided into chunks using chunksize. Each device
46 * is divided into far_copies sections. In each section, chunks are laid out
47 * in a style similar to raid0, but near_copies copies of each chunk is stored
48 * (each on a different drive). The starting device for each section is offset
49 * near_copies from the starting device of the previous section. Thus there
50 * are (near_copies * far_copies) of each chunk, and each is on a different
51 * drive. near_copies and far_copies must be at least one, and their product
52 * is at most raid_disks.
54 * If far_offset is true, then the far_copies are handled a bit differently.
55 * The copies are still in different stripes, but instead of being very far
56 * apart on disk, there are adjacent stripes.
58 * The far and offset algorithms are handled slightly differently if
59 * 'use_far_sets' is true. In this case, the array's devices are grouped into
60 * sets that are (near_copies * far_copies) in size. The far copied stripes
61 * are still shifted by 'near_copies' devices, but this shifting stays confined
62 * to the set rather than the entire array. This is done to improve the number
63 * of device combinations that can fail without causing the array to fail.
64 * Example 'far' algorithm w/o 'use_far_sets' (each letter represents a chunk
69 * Example 'far' algorithm w/ 'use_far_sets' enabled (sets illustrated w/ []'s):
70 * [A B] [C D] [A B] [C D E]
71 * |...| |...| |...| | ... |
72 * [B A] [D C] [B A] [E C D]
76 * Number of guaranteed r10bios in case of extreme VM load:
78 #define NR_RAID10_BIOS 256
80 /* when we get a read error on a read-only array, we redirect to another
81 * device without failing the first device, or trying to over-write to
82 * correct the read error. To keep track of bad blocks on a per-bio
83 * level, we store IO_BLOCKED in the appropriate 'bios' pointer
85 #define IO_BLOCKED ((struct bio *)1)
86 /* When we successfully write to a known bad-block, we need to remove the
87 * bad-block marking which must be done from process context. So we record
88 * the success by setting devs[n].bio to IO_MADE_GOOD
90 #define IO_MADE_GOOD ((struct bio *)2)
92 #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
94 /* When there are this many requests queued to be written by
95 * the raid10 thread, we become 'congested' to provide back-pressure
98 static int max_queued_requests = 1024;
100 static void allow_barrier(struct r10conf *conf);
101 static void lower_barrier(struct r10conf *conf);
102 static int _enough(struct r10conf *conf, int previous, int ignore);
103 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
105 static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio);
106 static void end_reshape_write(struct bio *bio);
107 static void end_reshape(struct r10conf *conf);
109 #define raid10_log(md, fmt, args...) \
110 do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid10 " fmt, ##args); } while (0)
112 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
114 struct r10conf *conf = data;
115 int size = offsetof(struct r10bio, devs[conf->copies]);
117 /* allocate a r10bio with room for raid_disks entries in the
119 return kzalloc(size, gfp_flags);
122 static void r10bio_pool_free(void *r10_bio, void *data)
127 /* Maximum size of each resync request */
128 #define RESYNC_BLOCK_SIZE (64*1024)
129 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
130 /* amount of memory to reserve for resync requests */
131 #define RESYNC_WINDOW (1024*1024)
132 /* maximum number of concurrent requests, memory permitting */
133 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
136 * When performing a resync, we need to read and compare, so
137 * we need as many pages are there are copies.
138 * When performing a recovery, we need 2 bios, one for read,
139 * one for write (we recover only one drive per r10buf)
142 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
144 struct r10conf *conf = data;
146 struct r10bio *r10_bio;
151 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
155 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
156 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
157 nalloc = conf->copies; /* resync */
159 nalloc = 2; /* recovery */
164 for (j = nalloc ; j-- ; ) {
165 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
168 r10_bio->devs[j].bio = bio;
169 if (!conf->have_replacement)
171 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
174 r10_bio->devs[j].repl_bio = bio;
177 * Allocate RESYNC_PAGES data pages and attach them
180 for (j = 0 ; j < nalloc; j++) {
181 struct bio *rbio = r10_bio->devs[j].repl_bio;
182 bio = r10_bio->devs[j].bio;
183 for (i = 0; i < RESYNC_PAGES; i++) {
184 if (j > 0 && !test_bit(MD_RECOVERY_SYNC,
185 &conf->mddev->recovery)) {
186 /* we can share bv_page's during recovery
188 struct bio *rbio = r10_bio->devs[0].bio;
189 page = rbio->bi_io_vec[i].bv_page;
192 page = alloc_page(gfp_flags);
196 bio->bi_io_vec[i].bv_page = page;
198 rbio->bi_io_vec[i].bv_page = page;
206 safe_put_page(bio->bi_io_vec[i-1].bv_page);
208 for (i = 0; i < RESYNC_PAGES ; i++)
209 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
212 for ( ; j < nalloc; j++) {
213 if (r10_bio->devs[j].bio)
214 bio_put(r10_bio->devs[j].bio);
215 if (r10_bio->devs[j].repl_bio)
216 bio_put(r10_bio->devs[j].repl_bio);
218 r10bio_pool_free(r10_bio, conf);
222 static void r10buf_pool_free(void *__r10_bio, void *data)
225 struct r10conf *conf = data;
226 struct r10bio *r10bio = __r10_bio;
229 for (j=0; j < conf->copies; j++) {
230 struct bio *bio = r10bio->devs[j].bio;
232 for (i = 0; i < RESYNC_PAGES; i++) {
233 safe_put_page(bio->bi_io_vec[i].bv_page);
234 bio->bi_io_vec[i].bv_page = NULL;
238 bio = r10bio->devs[j].repl_bio;
242 r10bio_pool_free(r10bio, conf);
245 static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
249 for (i = 0; i < conf->copies; i++) {
250 struct bio **bio = & r10_bio->devs[i].bio;
251 if (!BIO_SPECIAL(*bio))
254 bio = &r10_bio->devs[i].repl_bio;
255 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
261 static void free_r10bio(struct r10bio *r10_bio)
263 struct r10conf *conf = r10_bio->mddev->private;
265 put_all_bios(conf, r10_bio);
266 mempool_free(r10_bio, conf->r10bio_pool);
269 static void put_buf(struct r10bio *r10_bio)
271 struct r10conf *conf = r10_bio->mddev->private;
273 mempool_free(r10_bio, conf->r10buf_pool);
278 static void reschedule_retry(struct r10bio *r10_bio)
281 struct mddev *mddev = r10_bio->mddev;
282 struct r10conf *conf = mddev->private;
284 spin_lock_irqsave(&conf->device_lock, flags);
285 list_add(&r10_bio->retry_list, &conf->retry_list);
287 spin_unlock_irqrestore(&conf->device_lock, flags);
289 /* wake up frozen array... */
290 wake_up(&conf->wait_barrier);
292 md_wakeup_thread(mddev->thread);
296 * raid_end_bio_io() is called when we have finished servicing a mirrored
297 * operation and are ready to return a success/failure code to the buffer
300 static void raid_end_bio_io(struct r10bio *r10_bio)
302 struct bio *bio = r10_bio->master_bio;
304 struct r10conf *conf = r10_bio->mddev->private;
306 if (bio->bi_phys_segments) {
308 spin_lock_irqsave(&conf->device_lock, flags);
309 bio->bi_phys_segments--;
310 done = (bio->bi_phys_segments == 0);
311 spin_unlock_irqrestore(&conf->device_lock, flags);
314 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
315 bio->bi_error = -EIO;
319 * Wake up any possible resync thread that waits for the device
324 free_r10bio(r10_bio);
328 * Update disk head position estimator based on IRQ completion info.
330 static inline void update_head_pos(int slot, struct r10bio *r10_bio)
332 struct r10conf *conf = r10_bio->mddev->private;
334 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
335 r10_bio->devs[slot].addr + (r10_bio->sectors);
339 * Find the disk number which triggered given bio
341 static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
342 struct bio *bio, int *slotp, int *replp)
347 for (slot = 0; slot < conf->copies; slot++) {
348 if (r10_bio->devs[slot].bio == bio)
350 if (r10_bio->devs[slot].repl_bio == bio) {
356 BUG_ON(slot == conf->copies);
357 update_head_pos(slot, r10_bio);
363 return r10_bio->devs[slot].devnum;
366 static void raid10_end_read_request(struct bio *bio)
368 int uptodate = !bio->bi_error;
369 struct r10bio *r10_bio = bio->bi_private;
371 struct md_rdev *rdev;
372 struct r10conf *conf = r10_bio->mddev->private;
374 slot = r10_bio->read_slot;
375 dev = r10_bio->devs[slot].devnum;
376 rdev = r10_bio->devs[slot].rdev;
378 * this branch is our 'one mirror IO has finished' event handler:
380 update_head_pos(slot, r10_bio);
384 * Set R10BIO_Uptodate in our master bio, so that
385 * we will return a good error code to the higher
386 * levels even if IO on some other mirrored buffer fails.
388 * The 'master' represents the composite IO operation to
389 * user-side. So if something waits for IO, then it will
390 * wait for the 'master' bio.
392 set_bit(R10BIO_Uptodate, &r10_bio->state);
394 /* If all other devices that store this block have
395 * failed, we want to return the error upwards rather
396 * than fail the last device. Here we redefine
397 * "uptodate" to mean "Don't want to retry"
399 if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state),
404 raid_end_bio_io(r10_bio);
405 rdev_dec_pending(rdev, conf->mddev);
408 * oops, read error - keep the refcount on the rdev
410 char b[BDEVNAME_SIZE];
411 pr_err_ratelimited("md/raid10:%s: %s: rescheduling sector %llu\n",
413 bdevname(rdev->bdev, b),
414 (unsigned long long)r10_bio->sector);
415 set_bit(R10BIO_ReadError, &r10_bio->state);
416 reschedule_retry(r10_bio);
420 static void close_write(struct r10bio *r10_bio)
422 /* clear the bitmap if all writes complete successfully */
423 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
425 !test_bit(R10BIO_Degraded, &r10_bio->state),
427 md_write_end(r10_bio->mddev);
430 static void one_write_done(struct r10bio *r10_bio)
432 if (atomic_dec_and_test(&r10_bio->remaining)) {
433 if (test_bit(R10BIO_WriteError, &r10_bio->state))
434 reschedule_retry(r10_bio);
436 close_write(r10_bio);
437 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
438 reschedule_retry(r10_bio);
440 raid_end_bio_io(r10_bio);
445 static void raid10_end_write_request(struct bio *bio)
447 struct r10bio *r10_bio = bio->bi_private;
450 struct r10conf *conf = r10_bio->mddev->private;
452 struct md_rdev *rdev = NULL;
455 discard_error = bio->bi_error && bio_op(bio) == REQ_OP_DISCARD;
457 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
460 rdev = conf->mirrors[dev].replacement;
464 rdev = conf->mirrors[dev].rdev;
467 * this branch is our 'one mirror IO has finished' event handler:
469 if (bio->bi_error && !discard_error) {
471 /* Never record new bad blocks to replacement,
474 md_error(rdev->mddev, rdev);
476 set_bit(WriteErrorSeen, &rdev->flags);
477 if (!test_and_set_bit(WantReplacement, &rdev->flags))
478 set_bit(MD_RECOVERY_NEEDED,
479 &rdev->mddev->recovery);
480 set_bit(R10BIO_WriteError, &r10_bio->state);
485 * Set R10BIO_Uptodate in our master bio, so that
486 * we will return a good error code for to the higher
487 * levels even if IO on some other mirrored buffer fails.
489 * The 'master' represents the composite IO operation to
490 * user-side. So if something waits for IO, then it will
491 * wait for the 'master' bio.
497 * Do not set R10BIO_Uptodate if the current device is
498 * rebuilding or Faulty. This is because we cannot use
499 * such device for properly reading the data back (we could
500 * potentially use it, if the current write would have felt
501 * before rdev->recovery_offset, but for simplicity we don't
504 if (test_bit(In_sync, &rdev->flags) &&
505 !test_bit(Faulty, &rdev->flags))
506 set_bit(R10BIO_Uptodate, &r10_bio->state);
508 /* Maybe we can clear some bad blocks. */
509 if (is_badblock(rdev,
510 r10_bio->devs[slot].addr,
512 &first_bad, &bad_sectors) && !discard_error) {
515 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
517 r10_bio->devs[slot].bio = IO_MADE_GOOD;
519 set_bit(R10BIO_MadeGood, &r10_bio->state);
525 * Let's see if all mirrored write operations have finished
528 one_write_done(r10_bio);
530 rdev_dec_pending(rdev, conf->mddev);
534 * RAID10 layout manager
535 * As well as the chunksize and raid_disks count, there are two
536 * parameters: near_copies and far_copies.
537 * near_copies * far_copies must be <= raid_disks.
538 * Normally one of these will be 1.
539 * If both are 1, we get raid0.
540 * If near_copies == raid_disks, we get raid1.
542 * Chunks are laid out in raid0 style with near_copies copies of the
543 * first chunk, followed by near_copies copies of the next chunk and
545 * If far_copies > 1, then after 1/far_copies of the array has been assigned
546 * as described above, we start again with a device offset of near_copies.
547 * So we effectively have another copy of the whole array further down all
548 * the drives, but with blocks on different drives.
549 * With this layout, and block is never stored twice on the one device.
551 * raid10_find_phys finds the sector offset of a given virtual sector
552 * on each device that it is on.
554 * raid10_find_virt does the reverse mapping, from a device and a
555 * sector offset to a virtual address
558 static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
566 int last_far_set_start, last_far_set_size;
568 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
569 last_far_set_start *= geo->far_set_size;
571 last_far_set_size = geo->far_set_size;
572 last_far_set_size += (geo->raid_disks % geo->far_set_size);
574 /* now calculate first sector/dev */
575 chunk = r10bio->sector >> geo->chunk_shift;
576 sector = r10bio->sector & geo->chunk_mask;
578 chunk *= geo->near_copies;
580 dev = sector_div(stripe, geo->raid_disks);
582 stripe *= geo->far_copies;
584 sector += stripe << geo->chunk_shift;
586 /* and calculate all the others */
587 for (n = 0; n < geo->near_copies; n++) {
591 r10bio->devs[slot].devnum = d;
592 r10bio->devs[slot].addr = s;
595 for (f = 1; f < geo->far_copies; f++) {
596 set = d / geo->far_set_size;
597 d += geo->near_copies;
599 if ((geo->raid_disks % geo->far_set_size) &&
600 (d > last_far_set_start)) {
601 d -= last_far_set_start;
602 d %= last_far_set_size;
603 d += last_far_set_start;
605 d %= geo->far_set_size;
606 d += geo->far_set_size * set;
609 r10bio->devs[slot].devnum = d;
610 r10bio->devs[slot].addr = s;
614 if (dev >= geo->raid_disks) {
616 sector += (geo->chunk_mask + 1);
621 static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
623 struct geom *geo = &conf->geo;
625 if (conf->reshape_progress != MaxSector &&
626 ((r10bio->sector >= conf->reshape_progress) !=
627 conf->mddev->reshape_backwards)) {
628 set_bit(R10BIO_Previous, &r10bio->state);
631 clear_bit(R10BIO_Previous, &r10bio->state);
633 __raid10_find_phys(geo, r10bio);
636 static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
638 sector_t offset, chunk, vchunk;
639 /* Never use conf->prev as this is only called during resync
640 * or recovery, so reshape isn't happening
642 struct geom *geo = &conf->geo;
643 int far_set_start = (dev / geo->far_set_size) * geo->far_set_size;
644 int far_set_size = geo->far_set_size;
645 int last_far_set_start;
647 if (geo->raid_disks % geo->far_set_size) {
648 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
649 last_far_set_start *= geo->far_set_size;
651 if (dev >= last_far_set_start) {
652 far_set_size = geo->far_set_size;
653 far_set_size += (geo->raid_disks % geo->far_set_size);
654 far_set_start = last_far_set_start;
658 offset = sector & geo->chunk_mask;
659 if (geo->far_offset) {
661 chunk = sector >> geo->chunk_shift;
662 fc = sector_div(chunk, geo->far_copies);
663 dev -= fc * geo->near_copies;
664 if (dev < far_set_start)
667 while (sector >= geo->stride) {
668 sector -= geo->stride;
669 if (dev < (geo->near_copies + far_set_start))
670 dev += far_set_size - geo->near_copies;
672 dev -= geo->near_copies;
674 chunk = sector >> geo->chunk_shift;
676 vchunk = chunk * geo->raid_disks + dev;
677 sector_div(vchunk, geo->near_copies);
678 return (vchunk << geo->chunk_shift) + offset;
682 * This routine returns the disk from which the requested read should
683 * be done. There is a per-array 'next expected sequential IO' sector
684 * number - if this matches on the next IO then we use the last disk.
685 * There is also a per-disk 'last know head position' sector that is
686 * maintained from IRQ contexts, both the normal and the resync IO
687 * completion handlers update this position correctly. If there is no
688 * perfect sequential match then we pick the disk whose head is closest.
690 * If there are 2 mirrors in the same 2 devices, performance degrades
691 * because position is mirror, not device based.
693 * The rdev for the device selected will have nr_pending incremented.
697 * FIXME: possibly should rethink readbalancing and do it differently
698 * depending on near_copies / far_copies geometry.
700 static struct md_rdev *read_balance(struct r10conf *conf,
701 struct r10bio *r10_bio,
704 const sector_t this_sector = r10_bio->sector;
706 int sectors = r10_bio->sectors;
707 int best_good_sectors;
708 sector_t new_distance, best_dist;
709 struct md_rdev *best_rdev, *rdev = NULL;
712 struct geom *geo = &conf->geo;
714 raid10_find_phys(conf, r10_bio);
716 sectors = r10_bio->sectors;
719 best_dist = MaxSector;
720 best_good_sectors = 0;
723 * Check if we can balance. We can balance on the whole
724 * device if no resync is going on (recovery is ok), or below
725 * the resync window. We take the first readable disk when
726 * above the resync window.
728 if (conf->mddev->recovery_cp < MaxSector
729 && (this_sector + sectors >= conf->next_resync))
732 for (slot = 0; slot < conf->copies ; slot++) {
737 if (r10_bio->devs[slot].bio == IO_BLOCKED)
739 disk = r10_bio->devs[slot].devnum;
740 rdev = rcu_dereference(conf->mirrors[disk].replacement);
741 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
742 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
743 rdev = rcu_dereference(conf->mirrors[disk].rdev);
745 test_bit(Faulty, &rdev->flags))
747 if (!test_bit(In_sync, &rdev->flags) &&
748 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
751 dev_sector = r10_bio->devs[slot].addr;
752 if (is_badblock(rdev, dev_sector, sectors,
753 &first_bad, &bad_sectors)) {
754 if (best_dist < MaxSector)
755 /* Already have a better slot */
757 if (first_bad <= dev_sector) {
758 /* Cannot read here. If this is the
759 * 'primary' device, then we must not read
760 * beyond 'bad_sectors' from another device.
762 bad_sectors -= (dev_sector - first_bad);
763 if (!do_balance && sectors > bad_sectors)
764 sectors = bad_sectors;
765 if (best_good_sectors > sectors)
766 best_good_sectors = sectors;
768 sector_t good_sectors =
769 first_bad - dev_sector;
770 if (good_sectors > best_good_sectors) {
771 best_good_sectors = good_sectors;
776 /* Must read from here */
781 best_good_sectors = sectors;
786 /* This optimisation is debatable, and completely destroys
787 * sequential read speed for 'far copies' arrays. So only
788 * keep it for 'near' arrays, and review those later.
790 if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending))
793 /* for far > 1 always use the lowest address */
794 if (geo->far_copies > 1)
795 new_distance = r10_bio->devs[slot].addr;
797 new_distance = abs(r10_bio->devs[slot].addr -
798 conf->mirrors[disk].head_position);
799 if (new_distance < best_dist) {
800 best_dist = new_distance;
805 if (slot >= conf->copies) {
811 atomic_inc(&rdev->nr_pending);
812 r10_bio->read_slot = slot;
816 *max_sectors = best_good_sectors;
821 static int raid10_congested(struct mddev *mddev, int bits)
823 struct r10conf *conf = mddev->private;
826 if ((bits & (1 << WB_async_congested)) &&
827 conf->pending_count >= max_queued_requests)
832 (i < conf->geo.raid_disks || i < conf->prev.raid_disks)
835 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
836 if (rdev && !test_bit(Faulty, &rdev->flags)) {
837 struct request_queue *q = bdev_get_queue(rdev->bdev);
839 ret |= bdi_congested(&q->backing_dev_info, bits);
846 static void flush_pending_writes(struct r10conf *conf)
848 /* Any writes that have been queued but are awaiting
849 * bitmap updates get flushed here.
851 spin_lock_irq(&conf->device_lock);
853 if (conf->pending_bio_list.head) {
855 bio = bio_list_get(&conf->pending_bio_list);
856 conf->pending_count = 0;
857 spin_unlock_irq(&conf->device_lock);
858 /* flush any pending bitmap writes to disk
859 * before proceeding w/ I/O */
860 bitmap_unplug(conf->mddev->bitmap);
861 wake_up(&conf->wait_barrier);
863 while (bio) { /* submit pending writes */
864 struct bio *next = bio->bi_next;
865 struct md_rdev *rdev = (void*)bio->bi_bdev;
867 bio->bi_bdev = rdev->bdev;
868 if (test_bit(Faulty, &rdev->flags)) {
869 bio->bi_error = -EIO;
871 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
872 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
876 generic_make_request(bio);
880 spin_unlock_irq(&conf->device_lock);
884 * Sometimes we need to suspend IO while we do something else,
885 * either some resync/recovery, or reconfigure the array.
886 * To do this we raise a 'barrier'.
887 * The 'barrier' is a counter that can be raised multiple times
888 * to count how many activities are happening which preclude
890 * We can only raise the barrier if there is no pending IO.
891 * i.e. if nr_pending == 0.
892 * We choose only to raise the barrier if no-one is waiting for the
893 * barrier to go down. This means that as soon as an IO request
894 * is ready, no other operations which require a barrier will start
895 * until the IO request has had a chance.
897 * So: regular IO calls 'wait_barrier'. When that returns there
898 * is no backgroup IO happening, It must arrange to call
899 * allow_barrier when it has finished its IO.
900 * backgroup IO calls must call raise_barrier. Once that returns
901 * there is no normal IO happeing. It must arrange to call
902 * lower_barrier when the particular background IO completes.
905 static void raise_barrier(struct r10conf *conf, int force)
907 BUG_ON(force && !conf->barrier);
908 spin_lock_irq(&conf->resync_lock);
910 /* Wait until no block IO is waiting (unless 'force') */
911 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
914 /* block any new IO from starting */
917 /* Now wait for all pending IO to complete */
918 wait_event_lock_irq(conf->wait_barrier,
919 !atomic_read(&conf->nr_pending) && conf->barrier < RESYNC_DEPTH,
922 spin_unlock_irq(&conf->resync_lock);
925 static void lower_barrier(struct r10conf *conf)
928 spin_lock_irqsave(&conf->resync_lock, flags);
930 spin_unlock_irqrestore(&conf->resync_lock, flags);
931 wake_up(&conf->wait_barrier);
934 static void wait_barrier(struct r10conf *conf)
936 spin_lock_irq(&conf->resync_lock);
939 /* Wait for the barrier to drop.
940 * However if there are already pending
941 * requests (preventing the barrier from
942 * rising completely), and the
943 * pre-process bio queue isn't empty,
944 * then don't wait, as we need to empty
945 * that queue to get the nr_pending
948 raid10_log(conf->mddev, "wait barrier");
949 wait_event_lock_irq(conf->wait_barrier,
951 (atomic_read(&conf->nr_pending) &&
953 !bio_list_empty(current->bio_list)),
956 if (!conf->nr_waiting)
957 wake_up(&conf->wait_barrier);
959 atomic_inc(&conf->nr_pending);
960 spin_unlock_irq(&conf->resync_lock);
963 static void allow_barrier(struct r10conf *conf)
965 if ((atomic_dec_and_test(&conf->nr_pending)) ||
966 (conf->array_freeze_pending))
967 wake_up(&conf->wait_barrier);
970 static void freeze_array(struct r10conf *conf, int extra)
972 /* stop syncio and normal IO and wait for everything to
974 * We increment barrier and nr_waiting, and then
975 * wait until nr_pending match nr_queued+extra
976 * This is called in the context of one normal IO request
977 * that has failed. Thus any sync request that might be pending
978 * will be blocked by nr_pending, and we need to wait for
979 * pending IO requests to complete or be queued for re-try.
980 * Thus the number queued (nr_queued) plus this request (extra)
981 * must match the number of pending IOs (nr_pending) before
984 spin_lock_irq(&conf->resync_lock);
985 conf->array_freeze_pending++;
988 wait_event_lock_irq_cmd(conf->wait_barrier,
989 atomic_read(&conf->nr_pending) == conf->nr_queued+extra,
991 flush_pending_writes(conf));
993 conf->array_freeze_pending--;
994 spin_unlock_irq(&conf->resync_lock);
997 static void unfreeze_array(struct r10conf *conf)
999 /* reverse the effect of the freeze */
1000 spin_lock_irq(&conf->resync_lock);
1003 wake_up(&conf->wait_barrier);
1004 spin_unlock_irq(&conf->resync_lock);
1007 static sector_t choose_data_offset(struct r10bio *r10_bio,
1008 struct md_rdev *rdev)
1010 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1011 test_bit(R10BIO_Previous, &r10_bio->state))
1012 return rdev->data_offset;
1014 return rdev->new_data_offset;
1017 struct raid10_plug_cb {
1018 struct blk_plug_cb cb;
1019 struct bio_list pending;
1023 static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1025 struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb,
1027 struct mddev *mddev = plug->cb.data;
1028 struct r10conf *conf = mddev->private;
1031 if (from_schedule || current->bio_list) {
1032 spin_lock_irq(&conf->device_lock);
1033 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1034 conf->pending_count += plug->pending_cnt;
1035 spin_unlock_irq(&conf->device_lock);
1036 wake_up(&conf->wait_barrier);
1037 md_wakeup_thread(mddev->thread);
1042 /* we aren't scheduling, so we can do the write-out directly. */
1043 bio = bio_list_get(&plug->pending);
1044 bitmap_unplug(mddev->bitmap);
1045 wake_up(&conf->wait_barrier);
1047 while (bio) { /* submit pending writes */
1048 struct bio *next = bio->bi_next;
1049 struct md_rdev *rdev = (void*)bio->bi_bdev;
1050 bio->bi_next = NULL;
1051 bio->bi_bdev = rdev->bdev;
1052 if (test_bit(Faulty, &rdev->flags)) {
1053 bio->bi_error = -EIO;
1055 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
1056 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
1057 /* Just ignore it */
1060 generic_make_request(bio);
1066 static void __make_request(struct mddev *mddev, struct bio *bio)
1068 struct r10conf *conf = mddev->private;
1069 struct r10bio *r10_bio;
1070 struct bio *read_bio;
1072 const int op = bio_op(bio);
1073 const int rw = bio_data_dir(bio);
1074 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1075 const unsigned long do_fua = (bio->bi_opf & REQ_FUA);
1076 unsigned long flags;
1077 struct md_rdev *blocked_rdev;
1078 struct blk_plug_cb *cb;
1079 struct raid10_plug_cb *plug = NULL;
1080 int sectors_handled;
1084 md_write_start(mddev, bio);
1087 * Register the new request and wait if the reconstruction
1088 * thread has put up a bar for new requests.
1089 * Continue immediately if no resync is active currently.
1093 sectors = bio_sectors(bio);
1094 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1095 bio->bi_iter.bi_sector < conf->reshape_progress &&
1096 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
1097 /* IO spans the reshape position. Need to wait for
1100 raid10_log(conf->mddev, "wait reshape");
1101 allow_barrier(conf);
1102 wait_event(conf->wait_barrier,
1103 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1104 conf->reshape_progress >= bio->bi_iter.bi_sector +
1108 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1109 bio_data_dir(bio) == WRITE &&
1110 (mddev->reshape_backwards
1111 ? (bio->bi_iter.bi_sector < conf->reshape_safe &&
1112 bio->bi_iter.bi_sector + sectors > conf->reshape_progress)
1113 : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe &&
1114 bio->bi_iter.bi_sector < conf->reshape_progress))) {
1115 /* Need to update reshape_position in metadata */
1116 mddev->reshape_position = conf->reshape_progress;
1117 set_mask_bits(&mddev->flags, 0,
1118 BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_PENDING));
1119 md_wakeup_thread(mddev->thread);
1120 raid10_log(conf->mddev, "wait reshape metadata");
1121 wait_event(mddev->sb_wait,
1122 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
1124 conf->reshape_safe = mddev->reshape_position;
1127 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1129 r10_bio->master_bio = bio;
1130 r10_bio->sectors = sectors;
1132 r10_bio->mddev = mddev;
1133 r10_bio->sector = bio->bi_iter.bi_sector;
1136 /* We might need to issue multiple reads to different
1137 * devices if there are bad blocks around, so we keep
1138 * track of the number of reads in bio->bi_phys_segments.
1139 * If this is 0, there is only one r10_bio and no locking
1140 * will be needed when the request completes. If it is
1141 * non-zero, then it is the number of not-completed requests.
1143 bio->bi_phys_segments = 0;
1144 bio_clear_flag(bio, BIO_SEG_VALID);
1148 * read balancing logic:
1150 struct md_rdev *rdev;
1154 rdev = read_balance(conf, r10_bio, &max_sectors);
1156 raid_end_bio_io(r10_bio);
1159 slot = r10_bio->read_slot;
1161 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1162 bio_trim(read_bio, r10_bio->sector - bio->bi_iter.bi_sector,
1165 r10_bio->devs[slot].bio = read_bio;
1166 r10_bio->devs[slot].rdev = rdev;
1168 read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr +
1169 choose_data_offset(r10_bio, rdev);
1170 read_bio->bi_bdev = rdev->bdev;
1171 read_bio->bi_end_io = raid10_end_read_request;
1172 bio_set_op_attrs(read_bio, op, do_sync);
1173 read_bio->bi_private = r10_bio;
1176 trace_block_bio_remap(bdev_get_queue(read_bio->bi_bdev),
1177 read_bio, disk_devt(mddev->gendisk),
1179 if (max_sectors < r10_bio->sectors) {
1180 /* Could not read all from this device, so we will
1181 * need another r10_bio.
1183 sectors_handled = (r10_bio->sector + max_sectors
1184 - bio->bi_iter.bi_sector);
1185 r10_bio->sectors = max_sectors;
1186 spin_lock_irq(&conf->device_lock);
1187 if (bio->bi_phys_segments == 0)
1188 bio->bi_phys_segments = 2;
1190 bio->bi_phys_segments++;
1191 spin_unlock_irq(&conf->device_lock);
1192 /* Cannot call generic_make_request directly
1193 * as that will be queued in __generic_make_request
1194 * and subsequent mempool_alloc might block
1195 * waiting for it. so hand bio over to raid10d.
1197 reschedule_retry(r10_bio);
1199 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1201 r10_bio->master_bio = bio;
1202 r10_bio->sectors = bio_sectors(bio) - sectors_handled;
1204 r10_bio->mddev = mddev;
1205 r10_bio->sector = bio->bi_iter.bi_sector +
1209 generic_make_request(read_bio);
1216 if (conf->pending_count >= max_queued_requests) {
1217 md_wakeup_thread(mddev->thread);
1218 raid10_log(mddev, "wait queued");
1219 wait_event(conf->wait_barrier,
1220 conf->pending_count < max_queued_requests);
1222 /* first select target devices under rcu_lock and
1223 * inc refcount on their rdev. Record them by setting
1225 * If there are known/acknowledged bad blocks on any device
1226 * on which we have seen a write error, we want to avoid
1227 * writing to those blocks. This potentially requires several
1228 * writes to write around the bad blocks. Each set of writes
1229 * gets its own r10_bio with a set of bios attached. The number
1230 * of r10_bios is recored in bio->bi_phys_segments just as with
1234 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
1235 raid10_find_phys(conf, r10_bio);
1237 blocked_rdev = NULL;
1239 max_sectors = r10_bio->sectors;
1241 for (i = 0; i < conf->copies; i++) {
1242 int d = r10_bio->devs[i].devnum;
1243 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
1244 struct md_rdev *rrdev = rcu_dereference(
1245 conf->mirrors[d].replacement);
1248 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1249 atomic_inc(&rdev->nr_pending);
1250 blocked_rdev = rdev;
1253 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1254 atomic_inc(&rrdev->nr_pending);
1255 blocked_rdev = rrdev;
1258 if (rdev && (test_bit(Faulty, &rdev->flags)))
1260 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
1263 r10_bio->devs[i].bio = NULL;
1264 r10_bio->devs[i].repl_bio = NULL;
1266 if (!rdev && !rrdev) {
1267 set_bit(R10BIO_Degraded, &r10_bio->state);
1270 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
1272 sector_t dev_sector = r10_bio->devs[i].addr;
1276 is_bad = is_badblock(rdev, dev_sector,
1278 &first_bad, &bad_sectors);
1280 /* Mustn't write here until the bad block
1283 atomic_inc(&rdev->nr_pending);
1284 set_bit(BlockedBadBlocks, &rdev->flags);
1285 blocked_rdev = rdev;
1288 if (is_bad && first_bad <= dev_sector) {
1289 /* Cannot write here at all */
1290 bad_sectors -= (dev_sector - first_bad);
1291 if (bad_sectors < max_sectors)
1292 /* Mustn't write more than bad_sectors
1293 * to other devices yet
1295 max_sectors = bad_sectors;
1296 /* We don't set R10BIO_Degraded as that
1297 * only applies if the disk is missing,
1298 * so it might be re-added, and we want to
1299 * know to recover this chunk.
1300 * In this case the device is here, and the
1301 * fact that this chunk is not in-sync is
1302 * recorded in the bad block log.
1307 int good_sectors = first_bad - dev_sector;
1308 if (good_sectors < max_sectors)
1309 max_sectors = good_sectors;
1313 r10_bio->devs[i].bio = bio;
1314 atomic_inc(&rdev->nr_pending);
1317 r10_bio->devs[i].repl_bio = bio;
1318 atomic_inc(&rrdev->nr_pending);
1323 if (unlikely(blocked_rdev)) {
1324 /* Have to wait for this device to get unblocked, then retry */
1328 for (j = 0; j < i; j++) {
1329 if (r10_bio->devs[j].bio) {
1330 d = r10_bio->devs[j].devnum;
1331 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1333 if (r10_bio->devs[j].repl_bio) {
1334 struct md_rdev *rdev;
1335 d = r10_bio->devs[j].devnum;
1336 rdev = conf->mirrors[d].replacement;
1338 /* Race with remove_disk */
1340 rdev = conf->mirrors[d].rdev;
1342 rdev_dec_pending(rdev, mddev);
1345 allow_barrier(conf);
1346 raid10_log(conf->mddev, "wait rdev %d blocked", blocked_rdev->raid_disk);
1347 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1352 if (max_sectors < r10_bio->sectors) {
1353 /* We are splitting this into multiple parts, so
1354 * we need to prepare for allocating another r10_bio.
1356 r10_bio->sectors = max_sectors;
1357 spin_lock_irq(&conf->device_lock);
1358 if (bio->bi_phys_segments == 0)
1359 bio->bi_phys_segments = 2;
1361 bio->bi_phys_segments++;
1362 spin_unlock_irq(&conf->device_lock);
1364 sectors_handled = r10_bio->sector + max_sectors -
1365 bio->bi_iter.bi_sector;
1367 atomic_set(&r10_bio->remaining, 1);
1368 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
1370 for (i = 0; i < conf->copies; i++) {
1372 int d = r10_bio->devs[i].devnum;
1373 if (r10_bio->devs[i].bio) {
1374 struct md_rdev *rdev = conf->mirrors[d].rdev;
1375 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1376 bio_trim(mbio, r10_bio->sector - bio->bi_iter.bi_sector,
1378 r10_bio->devs[i].bio = mbio;
1380 mbio->bi_iter.bi_sector = (r10_bio->devs[i].addr+
1381 choose_data_offset(r10_bio,
1383 mbio->bi_bdev = rdev->bdev;
1384 mbio->bi_end_io = raid10_end_write_request;
1385 bio_set_op_attrs(mbio, op, do_sync | do_fua);
1386 mbio->bi_private = r10_bio;
1388 if (conf->mddev->gendisk)
1389 trace_block_bio_remap(bdev_get_queue(mbio->bi_bdev),
1390 mbio, disk_devt(conf->mddev->gendisk),
1392 /* flush_pending_writes() needs access to the rdev so...*/
1393 mbio->bi_bdev = (void*)rdev;
1395 atomic_inc(&r10_bio->remaining);
1397 cb = blk_check_plugged(raid10_unplug, mddev,
1400 plug = container_of(cb, struct raid10_plug_cb,
1404 spin_lock_irqsave(&conf->device_lock, flags);
1406 bio_list_add(&plug->pending, mbio);
1407 plug->pending_cnt++;
1409 bio_list_add(&conf->pending_bio_list, mbio);
1410 conf->pending_count++;
1412 spin_unlock_irqrestore(&conf->device_lock, flags);
1414 md_wakeup_thread(mddev->thread);
1417 if (r10_bio->devs[i].repl_bio) {
1418 struct md_rdev *rdev = conf->mirrors[d].replacement;
1420 /* Replacement just got moved to main 'rdev' */
1422 rdev = conf->mirrors[d].rdev;
1424 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1425 bio_trim(mbio, r10_bio->sector - bio->bi_iter.bi_sector,
1427 r10_bio->devs[i].repl_bio = mbio;
1429 mbio->bi_iter.bi_sector = (r10_bio->devs[i].addr +
1432 mbio->bi_bdev = rdev->bdev;
1433 mbio->bi_end_io = raid10_end_write_request;
1434 bio_set_op_attrs(mbio, op, do_sync | do_fua);
1435 mbio->bi_private = r10_bio;
1437 if (conf->mddev->gendisk)
1438 trace_block_bio_remap(bdev_get_queue(mbio->bi_bdev),
1439 mbio, disk_devt(conf->mddev->gendisk),
1441 /* flush_pending_writes() needs access to the rdev so...*/
1442 mbio->bi_bdev = (void*)rdev;
1444 atomic_inc(&r10_bio->remaining);
1445 spin_lock_irqsave(&conf->device_lock, flags);
1446 bio_list_add(&conf->pending_bio_list, mbio);
1447 conf->pending_count++;
1448 spin_unlock_irqrestore(&conf->device_lock, flags);
1449 if (!mddev_check_plugged(mddev))
1450 md_wakeup_thread(mddev->thread);
1454 /* Don't remove the bias on 'remaining' (one_write_done) until
1455 * after checking if we need to go around again.
1458 if (sectors_handled < bio_sectors(bio)) {
1459 one_write_done(r10_bio);
1460 /* We need another r10_bio. It has already been counted
1461 * in bio->bi_phys_segments.
1463 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1465 r10_bio->master_bio = bio;
1466 r10_bio->sectors = bio_sectors(bio) - sectors_handled;
1468 r10_bio->mddev = mddev;
1469 r10_bio->sector = bio->bi_iter.bi_sector + sectors_handled;
1473 one_write_done(r10_bio);
1476 static void raid10_make_request(struct mddev *mddev, struct bio *bio)
1478 struct r10conf *conf = mddev->private;
1479 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
1480 int chunk_sects = chunk_mask + 1;
1484 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1485 md_flush_request(mddev, bio);
1492 * If this request crosses a chunk boundary, we need to split
1495 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
1496 bio_sectors(bio) > chunk_sects
1497 && (conf->geo.near_copies < conf->geo.raid_disks
1498 || conf->prev.near_copies <
1499 conf->prev.raid_disks))) {
1500 split = bio_split(bio, chunk_sects -
1501 (bio->bi_iter.bi_sector &
1503 GFP_NOIO, fs_bio_set);
1504 bio_chain(split, bio);
1509 __make_request(mddev, split);
1510 } while (split != bio);
1512 /* In case raid10d snuck in to freeze_array */
1513 wake_up(&conf->wait_barrier);
1516 static void raid10_status(struct seq_file *seq, struct mddev *mddev)
1518 struct r10conf *conf = mddev->private;
1521 if (conf->geo.near_copies < conf->geo.raid_disks)
1522 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
1523 if (conf->geo.near_copies > 1)
1524 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1525 if (conf->geo.far_copies > 1) {
1526 if (conf->geo.far_offset)
1527 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
1529 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
1530 if (conf->geo.far_set_size != conf->geo.raid_disks)
1531 seq_printf(seq, " %d devices per set", conf->geo.far_set_size);
1533 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1534 conf->geo.raid_disks - mddev->degraded);
1536 for (i = 0; i < conf->geo.raid_disks; i++) {
1537 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1538 seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1541 seq_printf(seq, "]");
1544 /* check if there are enough drives for
1545 * every block to appear on atleast one.
1546 * Don't consider the device numbered 'ignore'
1547 * as we might be about to remove it.
1549 static int _enough(struct r10conf *conf, int previous, int ignore)
1555 disks = conf->prev.raid_disks;
1556 ncopies = conf->prev.near_copies;
1558 disks = conf->geo.raid_disks;
1559 ncopies = conf->geo.near_copies;
1564 int n = conf->copies;
1568 struct md_rdev *rdev;
1569 if (this != ignore &&
1570 (rdev = rcu_dereference(conf->mirrors[this].rdev)) &&
1571 test_bit(In_sync, &rdev->flags))
1573 this = (this+1) % disks;
1577 first = (first + ncopies) % disks;
1578 } while (first != 0);
1585 static int enough(struct r10conf *conf, int ignore)
1587 /* when calling 'enough', both 'prev' and 'geo' must
1589 * This is ensured if ->reconfig_mutex or ->device_lock
1592 return _enough(conf, 0, ignore) &&
1593 _enough(conf, 1, ignore);
1596 static void raid10_error(struct mddev *mddev, struct md_rdev *rdev)
1598 char b[BDEVNAME_SIZE];
1599 struct r10conf *conf = mddev->private;
1600 unsigned long flags;
1603 * If it is not operational, then we have already marked it as dead
1604 * else if it is the last working disks, ignore the error, let the
1605 * next level up know.
1606 * else mark the drive as failed
1608 spin_lock_irqsave(&conf->device_lock, flags);
1609 if (test_bit(In_sync, &rdev->flags)
1610 && !enough(conf, rdev->raid_disk)) {
1612 * Don't fail the drive, just return an IO error.
1614 spin_unlock_irqrestore(&conf->device_lock, flags);
1617 if (test_and_clear_bit(In_sync, &rdev->flags))
1620 * If recovery is running, make sure it aborts.
1622 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1623 set_bit(Blocked, &rdev->flags);
1624 set_bit(Faulty, &rdev->flags);
1625 set_mask_bits(&mddev->flags, 0,
1626 BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_PENDING));
1627 spin_unlock_irqrestore(&conf->device_lock, flags);
1628 pr_crit("md/raid10:%s: Disk failure on %s, disabling device.\n"
1629 "md/raid10:%s: Operation continuing on %d devices.\n",
1630 mdname(mddev), bdevname(rdev->bdev, b),
1631 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
1634 static void print_conf(struct r10conf *conf)
1637 struct md_rdev *rdev;
1639 pr_debug("RAID10 conf printout:\n");
1641 pr_debug("(!conf)\n");
1644 pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
1645 conf->geo.raid_disks);
1647 /* This is only called with ->reconfix_mutex held, so
1648 * rcu protection of rdev is not needed */
1649 for (i = 0; i < conf->geo.raid_disks; i++) {
1650 char b[BDEVNAME_SIZE];
1651 rdev = conf->mirrors[i].rdev;
1653 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1654 i, !test_bit(In_sync, &rdev->flags),
1655 !test_bit(Faulty, &rdev->flags),
1656 bdevname(rdev->bdev,b));
1660 static void close_sync(struct r10conf *conf)
1663 allow_barrier(conf);
1665 mempool_destroy(conf->r10buf_pool);
1666 conf->r10buf_pool = NULL;
1669 static int raid10_spare_active(struct mddev *mddev)
1672 struct r10conf *conf = mddev->private;
1673 struct raid10_info *tmp;
1675 unsigned long flags;
1678 * Find all non-in_sync disks within the RAID10 configuration
1679 * and mark them in_sync
1681 for (i = 0; i < conf->geo.raid_disks; i++) {
1682 tmp = conf->mirrors + i;
1683 if (tmp->replacement
1684 && tmp->replacement->recovery_offset == MaxSector
1685 && !test_bit(Faulty, &tmp->replacement->flags)
1686 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1687 /* Replacement has just become active */
1689 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1692 /* Replaced device not technically faulty,
1693 * but we need to be sure it gets removed
1694 * and never re-added.
1696 set_bit(Faulty, &tmp->rdev->flags);
1697 sysfs_notify_dirent_safe(
1698 tmp->rdev->sysfs_state);
1700 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1701 } else if (tmp->rdev
1702 && tmp->rdev->recovery_offset == MaxSector
1703 && !test_bit(Faulty, &tmp->rdev->flags)
1704 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1706 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
1709 spin_lock_irqsave(&conf->device_lock, flags);
1710 mddev->degraded -= count;
1711 spin_unlock_irqrestore(&conf->device_lock, flags);
1717 static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1719 struct r10conf *conf = mddev->private;
1723 int last = conf->geo.raid_disks - 1;
1725 if (mddev->recovery_cp < MaxSector)
1726 /* only hot-add to in-sync arrays, as recovery is
1727 * very different from resync
1730 if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
1733 if (md_integrity_add_rdev(rdev, mddev))
1736 if (rdev->raid_disk >= 0)
1737 first = last = rdev->raid_disk;
1739 if (rdev->saved_raid_disk >= first &&
1740 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1741 mirror = rdev->saved_raid_disk;
1744 for ( ; mirror <= last ; mirror++) {
1745 struct raid10_info *p = &conf->mirrors[mirror];
1746 if (p->recovery_disabled == mddev->recovery_disabled)
1749 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1750 p->replacement != NULL)
1752 clear_bit(In_sync, &rdev->flags);
1753 set_bit(Replacement, &rdev->flags);
1754 rdev->raid_disk = mirror;
1757 disk_stack_limits(mddev->gendisk, rdev->bdev,
1758 rdev->data_offset << 9);
1760 rcu_assign_pointer(p->replacement, rdev);
1765 disk_stack_limits(mddev->gendisk, rdev->bdev,
1766 rdev->data_offset << 9);
1768 p->head_position = 0;
1769 p->recovery_disabled = mddev->recovery_disabled - 1;
1770 rdev->raid_disk = mirror;
1772 if (rdev->saved_raid_disk != mirror)
1774 rcu_assign_pointer(p->rdev, rdev);
1777 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
1778 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
1784 static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1786 struct r10conf *conf = mddev->private;
1788 int number = rdev->raid_disk;
1789 struct md_rdev **rdevp;
1790 struct raid10_info *p = conf->mirrors + number;
1793 if (rdev == p->rdev)
1795 else if (rdev == p->replacement)
1796 rdevp = &p->replacement;
1800 if (test_bit(In_sync, &rdev->flags) ||
1801 atomic_read(&rdev->nr_pending)) {
1805 /* Only remove non-faulty devices if recovery
1808 if (!test_bit(Faulty, &rdev->flags) &&
1809 mddev->recovery_disabled != p->recovery_disabled &&
1810 (!p->replacement || p->replacement == rdev) &&
1811 number < conf->geo.raid_disks &&
1817 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
1819 if (atomic_read(&rdev->nr_pending)) {
1820 /* lost the race, try later */
1826 if (p->replacement) {
1827 /* We must have just cleared 'rdev' */
1828 p->rdev = p->replacement;
1829 clear_bit(Replacement, &p->replacement->flags);
1830 smp_mb(); /* Make sure other CPUs may see both as identical
1831 * but will never see neither -- if they are careful.
1833 p->replacement = NULL;
1834 clear_bit(WantReplacement, &rdev->flags);
1836 /* We might have just remove the Replacement as faulty
1837 * Clear the flag just in case
1839 clear_bit(WantReplacement, &rdev->flags);
1841 err = md_integrity_register(mddev);
1849 static void end_sync_read(struct bio *bio)
1851 struct r10bio *r10_bio = bio->bi_private;
1852 struct r10conf *conf = r10_bio->mddev->private;
1855 if (bio == r10_bio->master_bio) {
1856 /* this is a reshape read */
1857 d = r10_bio->read_slot; /* really the read dev */
1859 d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
1862 set_bit(R10BIO_Uptodate, &r10_bio->state);
1864 /* The write handler will notice the lack of
1865 * R10BIO_Uptodate and record any errors etc
1867 atomic_add(r10_bio->sectors,
1868 &conf->mirrors[d].rdev->corrected_errors);
1870 /* for reconstruct, we always reschedule after a read.
1871 * for resync, only after all reads
1873 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1874 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1875 atomic_dec_and_test(&r10_bio->remaining)) {
1876 /* we have read all the blocks,
1877 * do the comparison in process context in raid10d
1879 reschedule_retry(r10_bio);
1883 static void end_sync_request(struct r10bio *r10_bio)
1885 struct mddev *mddev = r10_bio->mddev;
1887 while (atomic_dec_and_test(&r10_bio->remaining)) {
1888 if (r10_bio->master_bio == NULL) {
1889 /* the primary of several recovery bios */
1890 sector_t s = r10_bio->sectors;
1891 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1892 test_bit(R10BIO_WriteError, &r10_bio->state))
1893 reschedule_retry(r10_bio);
1896 md_done_sync(mddev, s, 1);
1899 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
1900 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1901 test_bit(R10BIO_WriteError, &r10_bio->state))
1902 reschedule_retry(r10_bio);
1910 static void end_sync_write(struct bio *bio)
1912 struct r10bio *r10_bio = bio->bi_private;
1913 struct mddev *mddev = r10_bio->mddev;
1914 struct r10conf *conf = mddev->private;
1920 struct md_rdev *rdev = NULL;
1922 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1924 rdev = conf->mirrors[d].replacement;
1926 rdev = conf->mirrors[d].rdev;
1928 if (bio->bi_error) {
1930 md_error(mddev, rdev);
1932 set_bit(WriteErrorSeen, &rdev->flags);
1933 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1934 set_bit(MD_RECOVERY_NEEDED,
1935 &rdev->mddev->recovery);
1936 set_bit(R10BIO_WriteError, &r10_bio->state);
1938 } else if (is_badblock(rdev,
1939 r10_bio->devs[slot].addr,
1941 &first_bad, &bad_sectors))
1942 set_bit(R10BIO_MadeGood, &r10_bio->state);
1944 rdev_dec_pending(rdev, mddev);
1946 end_sync_request(r10_bio);
1950 * Note: sync and recover and handled very differently for raid10
1951 * This code is for resync.
1952 * For resync, we read through virtual addresses and read all blocks.
1953 * If there is any error, we schedule a write. The lowest numbered
1954 * drive is authoritative.
1955 * However requests come for physical address, so we need to map.
1956 * For every physical address there are raid_disks/copies virtual addresses,
1957 * which is always are least one, but is not necessarly an integer.
1958 * This means that a physical address can span multiple chunks, so we may
1959 * have to submit multiple io requests for a single sync request.
1962 * We check if all blocks are in-sync and only write to blocks that
1965 static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
1967 struct r10conf *conf = mddev->private;
1969 struct bio *tbio, *fbio;
1972 atomic_set(&r10_bio->remaining, 1);
1974 /* find the first device with a block */
1975 for (i=0; i<conf->copies; i++)
1976 if (!r10_bio->devs[i].bio->bi_error)
1979 if (i == conf->copies)
1983 fbio = r10_bio->devs[i].bio;
1984 fbio->bi_iter.bi_size = r10_bio->sectors << 9;
1985 fbio->bi_iter.bi_idx = 0;
1987 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
1988 /* now find blocks with errors */
1989 for (i=0 ; i < conf->copies ; i++) {
1992 tbio = r10_bio->devs[i].bio;
1994 if (tbio->bi_end_io != end_sync_read)
1998 if (!r10_bio->devs[i].bio->bi_error) {
1999 /* We know that the bi_io_vec layout is the same for
2000 * both 'first' and 'i', so we just compare them.
2001 * All vec entries are PAGE_SIZE;
2003 int sectors = r10_bio->sectors;
2004 for (j = 0; j < vcnt; j++) {
2005 int len = PAGE_SIZE;
2006 if (sectors < (len / 512))
2007 len = sectors * 512;
2008 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
2009 page_address(tbio->bi_io_vec[j].bv_page),
2016 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
2017 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2018 /* Don't fix anything. */
2021 /* Ok, we need to write this bio, either to correct an
2022 * inconsistency or to correct an unreadable block.
2023 * First we need to fixup bv_offset, bv_len and
2024 * bi_vecs, as the read request might have corrupted these
2028 tbio->bi_vcnt = vcnt;
2029 tbio->bi_iter.bi_size = fbio->bi_iter.bi_size;
2030 tbio->bi_private = r10_bio;
2031 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr;
2032 tbio->bi_end_io = end_sync_write;
2033 bio_set_op_attrs(tbio, REQ_OP_WRITE, 0);
2035 bio_copy_data(tbio, fbio);
2037 d = r10_bio->devs[i].devnum;
2038 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2039 atomic_inc(&r10_bio->remaining);
2040 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
2042 tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset;
2043 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
2044 generic_make_request(tbio);
2047 /* Now write out to any replacement devices
2050 for (i = 0; i < conf->copies; i++) {
2053 tbio = r10_bio->devs[i].repl_bio;
2054 if (!tbio || !tbio->bi_end_io)
2056 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2057 && r10_bio->devs[i].bio != fbio)
2058 bio_copy_data(tbio, fbio);
2059 d = r10_bio->devs[i].devnum;
2060 atomic_inc(&r10_bio->remaining);
2061 md_sync_acct(conf->mirrors[d].replacement->bdev,
2063 generic_make_request(tbio);
2067 if (atomic_dec_and_test(&r10_bio->remaining)) {
2068 md_done_sync(mddev, r10_bio->sectors, 1);
2074 * Now for the recovery code.
2075 * Recovery happens across physical sectors.
2076 * We recover all non-is_sync drives by finding the virtual address of
2077 * each, and then choose a working drive that also has that virt address.
2078 * There is a separate r10_bio for each non-in_sync drive.
2079 * Only the first two slots are in use. The first for reading,
2080 * The second for writing.
2083 static void fix_recovery_read_error(struct r10bio *r10_bio)
2085 /* We got a read error during recovery.
2086 * We repeat the read in smaller page-sized sections.
2087 * If a read succeeds, write it to the new device or record
2088 * a bad block if we cannot.
2089 * If a read fails, record a bad block on both old and
2092 struct mddev *mddev = r10_bio->mddev;
2093 struct r10conf *conf = mddev->private;
2094 struct bio *bio = r10_bio->devs[0].bio;
2096 int sectors = r10_bio->sectors;
2098 int dr = r10_bio->devs[0].devnum;
2099 int dw = r10_bio->devs[1].devnum;
2103 struct md_rdev *rdev;
2107 if (s > (PAGE_SIZE>>9))
2110 rdev = conf->mirrors[dr].rdev;
2111 addr = r10_bio->devs[0].addr + sect,
2112 ok = sync_page_io(rdev,
2115 bio->bi_io_vec[idx].bv_page,
2116 REQ_OP_READ, 0, false);
2118 rdev = conf->mirrors[dw].rdev;
2119 addr = r10_bio->devs[1].addr + sect;
2120 ok = sync_page_io(rdev,
2123 bio->bi_io_vec[idx].bv_page,
2124 REQ_OP_WRITE, 0, false);
2126 set_bit(WriteErrorSeen, &rdev->flags);
2127 if (!test_and_set_bit(WantReplacement,
2129 set_bit(MD_RECOVERY_NEEDED,
2130 &rdev->mddev->recovery);
2134 /* We don't worry if we cannot set a bad block -
2135 * it really is bad so there is no loss in not
2138 rdev_set_badblocks(rdev, addr, s, 0);
2140 if (rdev != conf->mirrors[dw].rdev) {
2141 /* need bad block on destination too */
2142 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
2143 addr = r10_bio->devs[1].addr + sect;
2144 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2146 /* just abort the recovery */
2147 pr_notice("md/raid10:%s: recovery aborted due to read error\n",
2150 conf->mirrors[dw].recovery_disabled
2151 = mddev->recovery_disabled;
2152 set_bit(MD_RECOVERY_INTR,
2165 static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
2167 struct r10conf *conf = mddev->private;
2169 struct bio *wbio, *wbio2;
2171 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2172 fix_recovery_read_error(r10_bio);
2173 end_sync_request(r10_bio);
2178 * share the pages with the first bio
2179 * and submit the write request
2181 d = r10_bio->devs[1].devnum;
2182 wbio = r10_bio->devs[1].bio;
2183 wbio2 = r10_bio->devs[1].repl_bio;
2184 /* Need to test wbio2->bi_end_io before we call
2185 * generic_make_request as if the former is NULL,
2186 * the latter is free to free wbio2.
2188 if (wbio2 && !wbio2->bi_end_io)
2190 if (wbio->bi_end_io) {
2191 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2192 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
2193 generic_make_request(wbio);
2196 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2197 md_sync_acct(conf->mirrors[d].replacement->bdev,
2198 bio_sectors(wbio2));
2199 generic_make_request(wbio2);
2204 * Used by fix_read_error() to decay the per rdev read_errors.
2205 * We halve the read error count for every hour that has elapsed
2206 * since the last recorded read error.
2209 static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
2212 unsigned long hours_since_last;
2213 unsigned int read_errors = atomic_read(&rdev->read_errors);
2215 cur_time_mon = ktime_get_seconds();
2217 if (rdev->last_read_error == 0) {
2218 /* first time we've seen a read error */
2219 rdev->last_read_error = cur_time_mon;
2223 hours_since_last = (long)(cur_time_mon -
2224 rdev->last_read_error) / 3600;
2226 rdev->last_read_error = cur_time_mon;
2229 * if hours_since_last is > the number of bits in read_errors
2230 * just set read errors to 0. We do this to avoid
2231 * overflowing the shift of read_errors by hours_since_last.
2233 if (hours_since_last >= 8 * sizeof(read_errors))
2234 atomic_set(&rdev->read_errors, 0);
2236 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2239 static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
2240 int sectors, struct page *page, int rw)
2245 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2246 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2248 if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
2252 set_bit(WriteErrorSeen, &rdev->flags);
2253 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2254 set_bit(MD_RECOVERY_NEEDED,
2255 &rdev->mddev->recovery);
2257 /* need to record an error - either for the block or the device */
2258 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2259 md_error(rdev->mddev, rdev);
2264 * This is a kernel thread which:
2266 * 1. Retries failed read operations on working mirrors.
2267 * 2. Updates the raid superblock when problems encounter.
2268 * 3. Performs writes following reads for array synchronising.
2271 static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
2273 int sect = 0; /* Offset from r10_bio->sector */
2274 int sectors = r10_bio->sectors;
2275 struct md_rdev*rdev;
2276 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
2277 int d = r10_bio->devs[r10_bio->read_slot].devnum;
2279 /* still own a reference to this rdev, so it cannot
2280 * have been cleared recently.
2282 rdev = conf->mirrors[d].rdev;
2284 if (test_bit(Faulty, &rdev->flags))
2285 /* drive has already been failed, just ignore any
2286 more fix_read_error() attempts */
2289 check_decay_read_errors(mddev, rdev);
2290 atomic_inc(&rdev->read_errors);
2291 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2292 char b[BDEVNAME_SIZE];
2293 bdevname(rdev->bdev, b);
2295 pr_notice("md/raid10:%s: %s: Raid device exceeded read_error threshold [cur %d:max %d]\n",
2297 atomic_read(&rdev->read_errors), max_read_errors);
2298 pr_notice("md/raid10:%s: %s: Failing raid device\n",
2300 md_error(mddev, rdev);
2301 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
2307 int sl = r10_bio->read_slot;
2311 if (s > (PAGE_SIZE>>9))
2319 d = r10_bio->devs[sl].devnum;
2320 rdev = rcu_dereference(conf->mirrors[d].rdev);
2322 test_bit(In_sync, &rdev->flags) &&
2323 !test_bit(Faulty, &rdev->flags) &&
2324 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2325 &first_bad, &bad_sectors) == 0) {
2326 atomic_inc(&rdev->nr_pending);
2328 success = sync_page_io(rdev,
2329 r10_bio->devs[sl].addr +
2333 REQ_OP_READ, 0, false);
2334 rdev_dec_pending(rdev, mddev);
2340 if (sl == conf->copies)
2342 } while (!success && sl != r10_bio->read_slot);
2346 /* Cannot read from anywhere, just mark the block
2347 * as bad on the first device to discourage future
2350 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
2351 rdev = conf->mirrors[dn].rdev;
2353 if (!rdev_set_badblocks(
2355 r10_bio->devs[r10_bio->read_slot].addr
2358 md_error(mddev, rdev);
2359 r10_bio->devs[r10_bio->read_slot].bio
2366 /* write it back and re-read */
2368 while (sl != r10_bio->read_slot) {
2369 char b[BDEVNAME_SIZE];
2374 d = r10_bio->devs[sl].devnum;
2375 rdev = rcu_dereference(conf->mirrors[d].rdev);
2377 test_bit(Faulty, &rdev->flags) ||
2378 !test_bit(In_sync, &rdev->flags))
2381 atomic_inc(&rdev->nr_pending);
2383 if (r10_sync_page_io(rdev,
2384 r10_bio->devs[sl].addr +
2386 s, conf->tmppage, WRITE)
2388 /* Well, this device is dead */
2389 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %s)\n",
2391 (unsigned long long)(
2393 choose_data_offset(r10_bio,
2395 bdevname(rdev->bdev, b));
2396 pr_notice("md/raid10:%s: %s: failing drive\n",
2398 bdevname(rdev->bdev, b));
2400 rdev_dec_pending(rdev, mddev);
2404 while (sl != r10_bio->read_slot) {
2405 char b[BDEVNAME_SIZE];
2410 d = r10_bio->devs[sl].devnum;
2411 rdev = rcu_dereference(conf->mirrors[d].rdev);
2413 test_bit(Faulty, &rdev->flags) ||
2414 !test_bit(In_sync, &rdev->flags))
2417 atomic_inc(&rdev->nr_pending);
2419 switch (r10_sync_page_io(rdev,
2420 r10_bio->devs[sl].addr +
2425 /* Well, this device is dead */
2426 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %s)\n",
2428 (unsigned long long)(
2430 choose_data_offset(r10_bio, rdev)),
2431 bdevname(rdev->bdev, b));
2432 pr_notice("md/raid10:%s: %s: failing drive\n",
2434 bdevname(rdev->bdev, b));
2437 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %s)\n",
2439 (unsigned long long)(
2441 choose_data_offset(r10_bio, rdev)),
2442 bdevname(rdev->bdev, b));
2443 atomic_add(s, &rdev->corrected_errors);
2446 rdev_dec_pending(rdev, mddev);
2456 static int narrow_write_error(struct r10bio *r10_bio, int i)
2458 struct bio *bio = r10_bio->master_bio;
2459 struct mddev *mddev = r10_bio->mddev;
2460 struct r10conf *conf = mddev->private;
2461 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
2462 /* bio has the data to be written to slot 'i' where
2463 * we just recently had a write error.
2464 * We repeatedly clone the bio and trim down to one block,
2465 * then try the write. Where the write fails we record
2467 * It is conceivable that the bio doesn't exactly align with
2468 * blocks. We must handle this.
2470 * We currently own a reference to the rdev.
2476 int sect_to_write = r10_bio->sectors;
2479 if (rdev->badblocks.shift < 0)
2482 block_sectors = roundup(1 << rdev->badblocks.shift,
2483 bdev_logical_block_size(rdev->bdev) >> 9);
2484 sector = r10_bio->sector;
2485 sectors = ((r10_bio->sector + block_sectors)
2486 & ~(sector_t)(block_sectors - 1))
2489 while (sect_to_write) {
2492 if (sectors > sect_to_write)
2493 sectors = sect_to_write;
2494 /* Write at 'sector' for 'sectors' */
2495 wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
2496 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors);
2497 wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector);
2498 wbio->bi_iter.bi_sector = wsector +
2499 choose_data_offset(r10_bio, rdev);
2500 wbio->bi_bdev = rdev->bdev;
2501 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
2503 if (submit_bio_wait(wbio) < 0)
2505 ok = rdev_set_badblocks(rdev, wsector,
2510 sect_to_write -= sectors;
2512 sectors = block_sectors;
2517 static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
2519 int slot = r10_bio->read_slot;
2521 struct r10conf *conf = mddev->private;
2522 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
2523 char b[BDEVNAME_SIZE];
2524 unsigned long do_sync;
2527 sector_t bio_last_sector;
2529 /* we got a read error. Maybe the drive is bad. Maybe just
2530 * the block and we can fix it.
2531 * We freeze all other IO, and try reading the block from
2532 * other devices. When we find one, we re-write
2533 * and check it that fixes the read error.
2534 * This is all done synchronously while the array is
2537 bio = r10_bio->devs[slot].bio;
2538 bdevname(bio->bi_bdev, b);
2539 bio_dev = bio->bi_bdev->bd_dev;
2540 bio_last_sector = r10_bio->devs[slot].addr + rdev->data_offset + r10_bio->sectors;
2542 r10_bio->devs[slot].bio = NULL;
2544 if (mddev->ro == 0) {
2545 freeze_array(conf, 1);
2546 fix_read_error(conf, mddev, r10_bio);
2547 unfreeze_array(conf);
2549 r10_bio->devs[slot].bio = IO_BLOCKED;
2551 rdev_dec_pending(rdev, mddev);
2554 rdev = read_balance(conf, r10_bio, &max_sectors);
2556 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
2558 (unsigned long long)r10_bio->sector);
2559 raid_end_bio_io(r10_bio);
2563 do_sync = (r10_bio->master_bio->bi_opf & REQ_SYNC);
2564 slot = r10_bio->read_slot;
2565 pr_err_ratelimited("md/raid10:%s: %s: redirecting sector %llu to another mirror\n",
2567 bdevname(rdev->bdev, b),
2568 (unsigned long long)r10_bio->sector);
2569 bio = bio_clone_mddev(r10_bio->master_bio,
2571 bio_trim(bio, r10_bio->sector - bio->bi_iter.bi_sector, max_sectors);
2572 r10_bio->devs[slot].bio = bio;
2573 r10_bio->devs[slot].rdev = rdev;
2574 bio->bi_iter.bi_sector = r10_bio->devs[slot].addr
2575 + choose_data_offset(r10_bio, rdev);
2576 bio->bi_bdev = rdev->bdev;
2577 bio_set_op_attrs(bio, REQ_OP_READ, do_sync);
2578 bio->bi_private = r10_bio;
2579 bio->bi_end_io = raid10_end_read_request;
2580 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev),
2582 bio_last_sector - r10_bio->sectors);
2584 if (max_sectors < r10_bio->sectors) {
2585 /* Drat - have to split this up more */
2586 struct bio *mbio = r10_bio->master_bio;
2587 int sectors_handled =
2588 r10_bio->sector + max_sectors
2589 - mbio->bi_iter.bi_sector;
2590 r10_bio->sectors = max_sectors;
2591 spin_lock_irq(&conf->device_lock);
2592 if (mbio->bi_phys_segments == 0)
2593 mbio->bi_phys_segments = 2;
2595 mbio->bi_phys_segments++;
2596 spin_unlock_irq(&conf->device_lock);
2597 generic_make_request(bio);
2599 r10_bio = mempool_alloc(conf->r10bio_pool,
2601 r10_bio->master_bio = mbio;
2602 r10_bio->sectors = bio_sectors(mbio) - sectors_handled;
2604 set_bit(R10BIO_ReadError,
2606 r10_bio->mddev = mddev;
2607 r10_bio->sector = mbio->bi_iter.bi_sector
2612 generic_make_request(bio);
2615 static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
2617 /* Some sort of write request has finished and it
2618 * succeeded in writing where we thought there was a
2619 * bad block. So forget the bad block.
2620 * Or possibly if failed and we need to record
2624 struct md_rdev *rdev;
2626 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2627 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
2628 for (m = 0; m < conf->copies; m++) {
2629 int dev = r10_bio->devs[m].devnum;
2630 rdev = conf->mirrors[dev].rdev;
2631 if (r10_bio->devs[m].bio == NULL)
2633 if (!r10_bio->devs[m].bio->bi_error) {
2634 rdev_clear_badblocks(
2636 r10_bio->devs[m].addr,
2637 r10_bio->sectors, 0);
2639 if (!rdev_set_badblocks(
2641 r10_bio->devs[m].addr,
2642 r10_bio->sectors, 0))
2643 md_error(conf->mddev, rdev);
2645 rdev = conf->mirrors[dev].replacement;
2646 if (r10_bio->devs[m].repl_bio == NULL)
2649 if (!r10_bio->devs[m].repl_bio->bi_error) {
2650 rdev_clear_badblocks(
2652 r10_bio->devs[m].addr,
2653 r10_bio->sectors, 0);
2655 if (!rdev_set_badblocks(
2657 r10_bio->devs[m].addr,
2658 r10_bio->sectors, 0))
2659 md_error(conf->mddev, rdev);
2665 for (m = 0; m < conf->copies; m++) {
2666 int dev = r10_bio->devs[m].devnum;
2667 struct bio *bio = r10_bio->devs[m].bio;
2668 rdev = conf->mirrors[dev].rdev;
2669 if (bio == IO_MADE_GOOD) {
2670 rdev_clear_badblocks(
2672 r10_bio->devs[m].addr,
2673 r10_bio->sectors, 0);
2674 rdev_dec_pending(rdev, conf->mddev);
2675 } else if (bio != NULL && bio->bi_error) {
2677 if (!narrow_write_error(r10_bio, m)) {
2678 md_error(conf->mddev, rdev);
2679 set_bit(R10BIO_Degraded,
2682 rdev_dec_pending(rdev, conf->mddev);
2684 bio = r10_bio->devs[m].repl_bio;
2685 rdev = conf->mirrors[dev].replacement;
2686 if (rdev && bio == IO_MADE_GOOD) {
2687 rdev_clear_badblocks(
2689 r10_bio->devs[m].addr,
2690 r10_bio->sectors, 0);
2691 rdev_dec_pending(rdev, conf->mddev);
2695 spin_lock_irq(&conf->device_lock);
2696 list_add(&r10_bio->retry_list, &conf->bio_end_io_list);
2698 spin_unlock_irq(&conf->device_lock);
2699 md_wakeup_thread(conf->mddev->thread);
2701 if (test_bit(R10BIO_WriteError,
2703 close_write(r10_bio);
2704 raid_end_bio_io(r10_bio);
2709 static void raid10d(struct md_thread *thread)
2711 struct mddev *mddev = thread->mddev;
2712 struct r10bio *r10_bio;
2713 unsigned long flags;
2714 struct r10conf *conf = mddev->private;
2715 struct list_head *head = &conf->retry_list;
2716 struct blk_plug plug;
2718 md_check_recovery(mddev);
2720 if (!list_empty_careful(&conf->bio_end_io_list) &&
2721 !test_bit(MD_CHANGE_PENDING, &mddev->flags)) {
2723 spin_lock_irqsave(&conf->device_lock, flags);
2724 if (!test_bit(MD_CHANGE_PENDING, &mddev->flags)) {
2725 while (!list_empty(&conf->bio_end_io_list)) {
2726 list_move(conf->bio_end_io_list.prev, &tmp);
2730 spin_unlock_irqrestore(&conf->device_lock, flags);
2731 while (!list_empty(&tmp)) {
2732 r10_bio = list_first_entry(&tmp, struct r10bio,
2734 list_del(&r10_bio->retry_list);
2735 if (mddev->degraded)
2736 set_bit(R10BIO_Degraded, &r10_bio->state);
2738 if (test_bit(R10BIO_WriteError,
2740 close_write(r10_bio);
2741 raid_end_bio_io(r10_bio);
2745 blk_start_plug(&plug);
2748 flush_pending_writes(conf);
2750 spin_lock_irqsave(&conf->device_lock, flags);
2751 if (list_empty(head)) {
2752 spin_unlock_irqrestore(&conf->device_lock, flags);
2755 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
2756 list_del(head->prev);
2758 spin_unlock_irqrestore(&conf->device_lock, flags);
2760 mddev = r10_bio->mddev;
2761 conf = mddev->private;
2762 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2763 test_bit(R10BIO_WriteError, &r10_bio->state))
2764 handle_write_completed(conf, r10_bio);
2765 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
2766 reshape_request_write(mddev, r10_bio);
2767 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
2768 sync_request_write(mddev, r10_bio);
2769 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
2770 recovery_request_write(mddev, r10_bio);
2771 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
2772 handle_read_error(mddev, r10_bio);
2774 /* just a partial read to be scheduled from a
2777 int slot = r10_bio->read_slot;
2778 generic_make_request(r10_bio->devs[slot].bio);
2782 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2783 md_check_recovery(mddev);
2785 blk_finish_plug(&plug);
2788 static int init_resync(struct r10conf *conf)
2793 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
2794 BUG_ON(conf->r10buf_pool);
2795 conf->have_replacement = 0;
2796 for (i = 0; i < conf->geo.raid_disks; i++)
2797 if (conf->mirrors[i].replacement)
2798 conf->have_replacement = 1;
2799 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2800 if (!conf->r10buf_pool)
2802 conf->next_resync = 0;
2807 * perform a "sync" on one "block"
2809 * We need to make sure that no normal I/O request - particularly write
2810 * requests - conflict with active sync requests.
2812 * This is achieved by tracking pending requests and a 'barrier' concept
2813 * that can be installed to exclude normal IO requests.
2815 * Resync and recovery are handled very differently.
2816 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2818 * For resync, we iterate over virtual addresses, read all copies,
2819 * and update if there are differences. If only one copy is live,
2821 * For recovery, we iterate over physical addresses, read a good
2822 * value for each non-in_sync drive, and over-write.
2824 * So, for recovery we may have several outstanding complex requests for a
2825 * given address, one for each out-of-sync device. We model this by allocating
2826 * a number of r10_bio structures, one for each out-of-sync device.
2827 * As we setup these structures, we collect all bio's together into a list
2828 * which we then process collectively to add pages, and then process again
2829 * to pass to generic_make_request.
2831 * The r10_bio structures are linked using a borrowed master_bio pointer.
2832 * This link is counted in ->remaining. When the r10_bio that points to NULL
2833 * has its remaining count decremented to 0, the whole complex operation
2838 static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
2841 struct r10conf *conf = mddev->private;
2842 struct r10bio *r10_bio;
2843 struct bio *biolist = NULL, *bio;
2844 sector_t max_sector, nr_sectors;
2847 sector_t sync_blocks;
2848 sector_t sectors_skipped = 0;
2849 int chunks_skipped = 0;
2850 sector_t chunk_mask = conf->geo.chunk_mask;
2852 if (!conf->r10buf_pool)
2853 if (init_resync(conf))
2857 * Allow skipping a full rebuild for incremental assembly
2858 * of a clean array, like RAID1 does.
2860 if (mddev->bitmap == NULL &&
2861 mddev->recovery_cp == MaxSector &&
2862 mddev->reshape_position == MaxSector &&
2863 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
2864 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
2865 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
2866 conf->fullsync == 0) {
2868 return mddev->dev_sectors - sector_nr;
2872 max_sector = mddev->dev_sectors;
2873 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
2874 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2875 max_sector = mddev->resync_max_sectors;
2876 if (sector_nr >= max_sector) {
2877 /* If we aborted, we need to abort the
2878 * sync on the 'current' bitmap chucks (there can
2879 * be several when recovering multiple devices).
2880 * as we may have started syncing it but not finished.
2881 * We can find the current address in
2882 * mddev->curr_resync, but for recovery,
2883 * we need to convert that to several
2884 * virtual addresses.
2886 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
2892 if (mddev->curr_resync < max_sector) { /* aborted */
2893 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2894 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2896 else for (i = 0; i < conf->geo.raid_disks; i++) {
2898 raid10_find_virt(conf, mddev->curr_resync, i);
2899 bitmap_end_sync(mddev->bitmap, sect,
2903 /* completed sync */
2904 if ((!mddev->bitmap || conf->fullsync)
2905 && conf->have_replacement
2906 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2907 /* Completed a full sync so the replacements
2908 * are now fully recovered.
2911 for (i = 0; i < conf->geo.raid_disks; i++) {
2912 struct md_rdev *rdev =
2913 rcu_dereference(conf->mirrors[i].replacement);
2915 rdev->recovery_offset = MaxSector;
2921 bitmap_close_sync(mddev->bitmap);
2924 return sectors_skipped;
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..
2935 return (max_sector - sector_nr) + sectors_skipped;
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
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;
2949 * If there is non-resync activity waiting for a turn, then let it
2950 * though before starting on this new sync request.
2952 if (conf->nr_waiting)
2953 schedule_timeout_uninterruptible(1);
2955 /* Again, very different code for resync and recovery.
2956 * Both must result in an r10bio with a list of bios that
2957 * have bi_end_io, bi_sector, bi_bdev set,
2958 * and bi_private set to the r10bio.
2959 * For recovery, we may actually create several r10bios
2960 * with 2 bios in each, that correspond to the bios in the main one.
2961 * In this case, the subordinate r10bios link back through a
2962 * borrowed master_bio pointer, and the counter in the master
2963 * includes a ref from each subordinate.
2965 /* First, we decide what to do and set ->bi_end_io
2966 * To end_sync_read if we want to read, and
2967 * end_sync_write if we will want to write.
2970 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
2971 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2972 /* recovery... the complicated one */
2976 for (i = 0 ; i < conf->geo.raid_disks; i++) {
2982 struct raid10_info *mirror = &conf->mirrors[i];
2983 struct md_rdev *mrdev, *mreplace;
2986 mrdev = rcu_dereference(mirror->rdev);
2987 mreplace = rcu_dereference(mirror->replacement);
2989 if ((mrdev == NULL ||
2990 test_bit(Faulty, &mrdev->flags) ||
2991 test_bit(In_sync, &mrdev->flags)) &&
2992 (mreplace == NULL ||
2993 test_bit(Faulty, &mreplace->flags))) {
2999 /* want to reconstruct this device */
3001 sect = raid10_find_virt(conf, sector_nr, i);
3002 if (sect >= mddev->resync_max_sectors) {
3003 /* last stripe is not complete - don't
3004 * try to recover this sector.
3009 if (mreplace && test_bit(Faulty, &mreplace->flags))
3011 /* Unless we are doing a full sync, or a replacement
3012 * we only need to recover the block if it is set in
3015 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3017 if (sync_blocks < max_sync)
3018 max_sync = sync_blocks;
3022 /* yep, skip the sync_blocks here, but don't assume
3023 * that there will never be anything to do here
3025 chunks_skipped = -1;
3029 atomic_inc(&mrdev->nr_pending);
3031 atomic_inc(&mreplace->nr_pending);
3034 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
3036 raise_barrier(conf, rb2 != NULL);
3037 atomic_set(&r10_bio->remaining, 0);
3039 r10_bio->master_bio = (struct bio*)rb2;
3041 atomic_inc(&rb2->remaining);
3042 r10_bio->mddev = mddev;
3043 set_bit(R10BIO_IsRecover, &r10_bio->state);
3044 r10_bio->sector = sect;
3046 raid10_find_phys(conf, r10_bio);
3048 /* Need to check if the array will still be
3052 for (j = 0; j < conf->geo.raid_disks; j++) {
3053 struct md_rdev *rdev = rcu_dereference(
3054 conf->mirrors[j].rdev);
3055 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3061 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3062 &sync_blocks, still_degraded);
3065 for (j=0; j<conf->copies;j++) {
3067 int d = r10_bio->devs[j].devnum;
3068 sector_t from_addr, to_addr;
3069 struct md_rdev *rdev =
3070 rcu_dereference(conf->mirrors[d].rdev);
3071 sector_t sector, first_bad;
3074 !test_bit(In_sync, &rdev->flags))
3076 /* This is where we read from */
3078 sector = r10_bio->devs[j].addr;
3080 if (is_badblock(rdev, sector, max_sync,
3081 &first_bad, &bad_sectors)) {
3082 if (first_bad > sector)
3083 max_sync = first_bad - sector;
3085 bad_sectors -= (sector
3087 if (max_sync > bad_sectors)
3088 max_sync = bad_sectors;
3092 bio = r10_bio->devs[0].bio;
3094 bio->bi_next = biolist;
3096 bio->bi_private = r10_bio;
3097 bio->bi_end_io = end_sync_read;
3098 bio_set_op_attrs(bio, REQ_OP_READ, 0);
3099 from_addr = r10_bio->devs[j].addr;
3100 bio->bi_iter.bi_sector = from_addr +
3102 bio->bi_bdev = rdev->bdev;
3103 atomic_inc(&rdev->nr_pending);
3104 /* and we write to 'i' (if not in_sync) */
3106 for (k=0; k<conf->copies; k++)
3107 if (r10_bio->devs[k].devnum == i)
3109 BUG_ON(k == conf->copies);
3110 to_addr = r10_bio->devs[k].addr;
3111 r10_bio->devs[0].devnum = d;
3112 r10_bio->devs[0].addr = from_addr;
3113 r10_bio->devs[1].devnum = i;
3114 r10_bio->devs[1].addr = to_addr;
3116 if (!test_bit(In_sync, &mrdev->flags)) {
3117 bio = r10_bio->devs[1].bio;
3119 bio->bi_next = biolist;
3121 bio->bi_private = r10_bio;
3122 bio->bi_end_io = end_sync_write;
3123 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
3124 bio->bi_iter.bi_sector = to_addr
3125 + mrdev->data_offset;
3126 bio->bi_bdev = mrdev->bdev;
3127 atomic_inc(&r10_bio->remaining);
3129 r10_bio->devs[1].bio->bi_end_io = NULL;
3131 /* and maybe write to replacement */
3132 bio = r10_bio->devs[1].repl_bio;
3134 bio->bi_end_io = NULL;
3135 /* Note: if mreplace != NULL, then bio
3136 * cannot be NULL as r10buf_pool_alloc will
3137 * have allocated it.
3138 * So the second test here is pointless.
3139 * But it keeps semantic-checkers happy, and
3140 * this comment keeps human reviewers
3143 if (mreplace == NULL || bio == NULL ||
3144 test_bit(Faulty, &mreplace->flags))
3147 bio->bi_next = biolist;
3149 bio->bi_private = r10_bio;
3150 bio->bi_end_io = end_sync_write;
3151 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
3152 bio->bi_iter.bi_sector = to_addr +
3153 mreplace->data_offset;
3154 bio->bi_bdev = mreplace->bdev;
3155 atomic_inc(&r10_bio->remaining);
3159 if (j == conf->copies) {
3160 /* Cannot recover, so abort the recovery or
3161 * record a bad block */
3163 /* problem is that there are bad blocks
3164 * on other device(s)
3167 for (k = 0; k < conf->copies; k++)
3168 if (r10_bio->devs[k].devnum == i)
3170 if (!test_bit(In_sync,
3172 && !rdev_set_badblocks(
3174 r10_bio->devs[k].addr,
3178 !rdev_set_badblocks(
3180 r10_bio->devs[k].addr,
3185 if (!test_and_set_bit(MD_RECOVERY_INTR,
3187 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
3189 mirror->recovery_disabled
3190 = mddev->recovery_disabled;
3194 atomic_dec(&rb2->remaining);
3196 rdev_dec_pending(mrdev, mddev);
3198 rdev_dec_pending(mreplace, mddev);
3201 rdev_dec_pending(mrdev, mddev);
3203 rdev_dec_pending(mreplace, mddev);
3205 if (biolist == NULL) {
3207 struct r10bio *rb2 = r10_bio;
3208 r10_bio = (struct r10bio*) rb2->master_bio;
3209 rb2->master_bio = NULL;
3215 /* resync. Schedule a read for every block at this virt offset */
3218 bitmap_cond_end_sync(mddev->bitmap, sector_nr, 0);
3220 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
3221 &sync_blocks, mddev->degraded) &&
3222 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3223 &mddev->recovery)) {
3224 /* We can skip this block */
3226 return sync_blocks + sectors_skipped;
3228 if (sync_blocks < max_sync)
3229 max_sync = sync_blocks;
3230 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
3233 r10_bio->mddev = mddev;
3234 atomic_set(&r10_bio->remaining, 0);
3235 raise_barrier(conf, 0);
3236 conf->next_resync = sector_nr;
3238 r10_bio->master_bio = NULL;
3239 r10_bio->sector = sector_nr;
3240 set_bit(R10BIO_IsSync, &r10_bio->state);
3241 raid10_find_phys(conf, r10_bio);
3242 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
3244 for (i = 0; i < conf->copies; i++) {
3245 int d = r10_bio->devs[i].devnum;
3246 sector_t first_bad, sector;
3248 struct md_rdev *rdev;
3250 if (r10_bio->devs[i].repl_bio)
3251 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3253 bio = r10_bio->devs[i].bio;
3255 bio->bi_error = -EIO;
3257 rdev = rcu_dereference(conf->mirrors[d].rdev);
3258 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3262 sector = r10_bio->devs[i].addr;
3263 if (is_badblock(rdev, sector, max_sync,
3264 &first_bad, &bad_sectors)) {
3265 if (first_bad > sector)
3266 max_sync = first_bad - sector;
3268 bad_sectors -= (sector - first_bad);
3269 if (max_sync > bad_sectors)
3270 max_sync = bad_sectors;
3275 atomic_inc(&rdev->nr_pending);
3276 atomic_inc(&r10_bio->remaining);
3277 bio->bi_next = biolist;
3279 bio->bi_private = r10_bio;
3280 bio->bi_end_io = end_sync_read;
3281 bio_set_op_attrs(bio, REQ_OP_READ, 0);
3282 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3283 bio->bi_bdev = rdev->bdev;
3286 rdev = rcu_dereference(conf->mirrors[d].replacement);
3287 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3291 atomic_inc(&rdev->nr_pending);
3294 /* Need to set up for writing to the replacement */
3295 bio = r10_bio->devs[i].repl_bio;
3297 bio->bi_error = -EIO;
3299 sector = r10_bio->devs[i].addr;
3300 bio->bi_next = biolist;
3302 bio->bi_private = r10_bio;
3303 bio->bi_end_io = end_sync_write;
3304 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
3305 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3306 bio->bi_bdev = rdev->bdev;
3311 for (i=0; i<conf->copies; i++) {
3312 int d = r10_bio->devs[i].devnum;
3313 if (r10_bio->devs[i].bio->bi_end_io)
3314 rdev_dec_pending(conf->mirrors[d].rdev,
3316 if (r10_bio->devs[i].repl_bio &&
3317 r10_bio->devs[i].repl_bio->bi_end_io)
3319 conf->mirrors[d].replacement,
3329 if (sector_nr + max_sync < max_sector)
3330 max_sector = sector_nr + max_sync;
3333 int len = PAGE_SIZE;
3334 if (sector_nr + (len>>9) > max_sector)
3335 len = (max_sector - sector_nr) << 9;
3338 for (bio= biolist ; bio ; bio=bio->bi_next) {
3340 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
3341 if (bio_add_page(bio, page, len, 0))
3345 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
3346 for (bio2 = biolist;
3347 bio2 && bio2 != bio;
3348 bio2 = bio2->bi_next) {
3349 /* remove last page from this bio */
3351 bio2->bi_iter.bi_size -= len;
3352 bio_clear_flag(bio2, BIO_SEG_VALID);
3356 nr_sectors += len>>9;
3357 sector_nr += len>>9;
3358 } while (biolist->bi_vcnt < RESYNC_PAGES);
3360 r10_bio->sectors = nr_sectors;
3364 biolist = biolist->bi_next;
3366 bio->bi_next = NULL;
3367 r10_bio = bio->bi_private;
3368 r10_bio->sectors = nr_sectors;
3370 if (bio->bi_end_io == end_sync_read) {
3371 md_sync_acct(bio->bi_bdev, nr_sectors);
3373 generic_make_request(bio);
3377 if (sectors_skipped)
3378 /* pretend they weren't skipped, it makes
3379 * no important difference in this case
3381 md_done_sync(mddev, sectors_skipped, 1);
3383 return sectors_skipped + nr_sectors;
3385 /* There is nowhere to write, so all non-sync
3386 * drives must be failed or in resync, all drives
3387 * have a bad block, so try the next chunk...
3389 if (sector_nr + max_sync < max_sector)
3390 max_sector = sector_nr + max_sync;
3392 sectors_skipped += (max_sector - sector_nr);
3394 sector_nr = max_sector;
3399 raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
3402 struct r10conf *conf = mddev->private;
3405 raid_disks = min(conf->geo.raid_disks,
3406 conf->prev.raid_disks);
3408 sectors = conf->dev_sectors;
3410 size = sectors >> conf->geo.chunk_shift;
3411 sector_div(size, conf->geo.far_copies);
3412 size = size * raid_disks;
3413 sector_div(size, conf->geo.near_copies);
3415 return size << conf->geo.chunk_shift;
3418 static void calc_sectors(struct r10conf *conf, sector_t size)
3420 /* Calculate the number of sectors-per-device that will
3421 * actually be used, and set conf->dev_sectors and
3425 size = size >> conf->geo.chunk_shift;
3426 sector_div(size, conf->geo.far_copies);
3427 size = size * conf->geo.raid_disks;
3428 sector_div(size, conf->geo.near_copies);
3429 /* 'size' is now the number of chunks in the array */
3430 /* calculate "used chunks per device" */
3431 size = size * conf->copies;
3433 /* We need to round up when dividing by raid_disks to
3434 * get the stride size.
3436 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
3438 conf->dev_sectors = size << conf->geo.chunk_shift;
3440 if (conf->geo.far_offset)
3441 conf->geo.stride = 1 << conf->geo.chunk_shift;
3443 sector_div(size, conf->geo.far_copies);
3444 conf->geo.stride = size << conf->geo.chunk_shift;
3448 enum geo_type {geo_new, geo_old, geo_start};
3449 static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3452 int layout, chunk, disks;
3455 layout = mddev->layout;
3456 chunk = mddev->chunk_sectors;
3457 disks = mddev->raid_disks - mddev->delta_disks;
3460 layout = mddev->new_layout;
3461 chunk = mddev->new_chunk_sectors;
3462 disks = mddev->raid_disks;
3464 default: /* avoid 'may be unused' warnings */
3465 case geo_start: /* new when starting reshape - raid_disks not
3467 layout = mddev->new_layout;
3468 chunk = mddev->new_chunk_sectors;
3469 disks = mddev->raid_disks + mddev->delta_disks;
3474 if (chunk < (PAGE_SIZE >> 9) ||
3475 !is_power_of_2(chunk))
3478 fc = (layout >> 8) & 255;
3479 fo = layout & (1<<16);
3480 geo->raid_disks = disks;
3481 geo->near_copies = nc;
3482 geo->far_copies = fc;
3483 geo->far_offset = fo;
3484 switch (layout >> 17) {
3485 case 0: /* original layout. simple but not always optimal */
3486 geo->far_set_size = disks;
3488 case 1: /* "improved" layout which was buggy. Hopefully no-one is
3489 * actually using this, but leave code here just in case.*/
3490 geo->far_set_size = disks/fc;
3491 WARN(geo->far_set_size < fc,
3492 "This RAID10 layout does not provide data safety - please backup and create new array\n");
3494 case 2: /* "improved" layout fixed to match documentation */
3495 geo->far_set_size = fc * nc;
3497 default: /* Not a valid layout */
3500 geo->chunk_mask = chunk - 1;
3501 geo->chunk_shift = ffz(~chunk);
3505 static struct r10conf *setup_conf(struct mddev *mddev)
3507 struct r10conf *conf = NULL;
3512 copies = setup_geo(&geo, mddev, geo_new);
3515 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
3516 mdname(mddev), PAGE_SIZE);
3520 if (copies < 2 || copies > mddev->raid_disks) {
3521 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3522 mdname(mddev), mddev->new_layout);
3527 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
3531 /* FIXME calc properly */
3532 conf->mirrors = kzalloc(sizeof(struct raid10_info)*(mddev->raid_disks +
3533 max(0,-mddev->delta_disks)),
3538 conf->tmppage = alloc_page(GFP_KERNEL);
3543 conf->copies = copies;
3544 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
3545 r10bio_pool_free, conf);
3546 if (!conf->r10bio_pool)
3549 calc_sectors(conf, mddev->dev_sectors);
3550 if (mddev->reshape_position == MaxSector) {
3551 conf->prev = conf->geo;
3552 conf->reshape_progress = MaxSector;
3554 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
3558 conf->reshape_progress = mddev->reshape_position;
3559 if (conf->prev.far_offset)
3560 conf->prev.stride = 1 << conf->prev.chunk_shift;
3562 /* far_copies must be 1 */
3563 conf->prev.stride = conf->dev_sectors;
3565 conf->reshape_safe = conf->reshape_progress;
3566 spin_lock_init(&conf->device_lock);
3567 INIT_LIST_HEAD(&conf->retry_list);
3568 INIT_LIST_HEAD(&conf->bio_end_io_list);
3570 spin_lock_init(&conf->resync_lock);
3571 init_waitqueue_head(&conf->wait_barrier);
3572 atomic_set(&conf->nr_pending, 0);
3574 conf->thread = md_register_thread(raid10d, mddev, "raid10");
3578 conf->mddev = mddev;
3583 mempool_destroy(conf->r10bio_pool);
3584 kfree(conf->mirrors);
3585 safe_put_page(conf->tmppage);
3588 return ERR_PTR(err);
3591 static int raid10_run(struct mddev *mddev)
3593 struct r10conf *conf;
3594 int i, disk_idx, chunk_size;
3595 struct raid10_info *disk;
3596 struct md_rdev *rdev;
3598 sector_t min_offset_diff = 0;
3600 bool discard_supported = false;
3602 if (mddev->private == NULL) {
3603 conf = setup_conf(mddev);
3605 return PTR_ERR(conf);
3606 mddev->private = conf;
3608 conf = mddev->private;
3612 mddev->thread = conf->thread;
3613 conf->thread = NULL;
3615 chunk_size = mddev->chunk_sectors << 9;
3617 blk_queue_max_discard_sectors(mddev->queue,
3618 mddev->chunk_sectors);
3619 blk_queue_max_write_same_sectors(mddev->queue, 0);
3620 blk_queue_io_min(mddev->queue, chunk_size);
3621 if (conf->geo.raid_disks % conf->geo.near_copies)
3622 blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
3624 blk_queue_io_opt(mddev->queue, chunk_size *
3625 (conf->geo.raid_disks / conf->geo.near_copies));
3628 rdev_for_each(rdev, mddev) {
3630 struct request_queue *q;
3632 disk_idx = rdev->raid_disk;
3635 if (disk_idx >= conf->geo.raid_disks &&
3636 disk_idx >= conf->prev.raid_disks)
3638 disk = conf->mirrors + disk_idx;
3640 if (test_bit(Replacement, &rdev->flags)) {
3641 if (disk->replacement)
3643 disk->replacement = rdev;
3649 q = bdev_get_queue(rdev->bdev);
3650 diff = (rdev->new_data_offset - rdev->data_offset);
3651 if (!mddev->reshape_backwards)
3655 if (first || diff < min_offset_diff)
3656 min_offset_diff = diff;
3659 disk_stack_limits(mddev->gendisk, rdev->bdev,
3660 rdev->data_offset << 9);
3662 disk->head_position = 0;
3664 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3665 discard_supported = true;
3669 if (discard_supported)
3670 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
3673 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
3676 /* need to check that every block has at least one working mirror */
3677 if (!enough(conf, -1)) {
3678 pr_err("md/raid10:%s: not enough operational mirrors.\n",
3683 if (conf->reshape_progress != MaxSector) {
3684 /* must ensure that shape change is supported */
3685 if (conf->geo.far_copies != 1 &&
3686 conf->geo.far_offset == 0)
3688 if (conf->prev.far_copies != 1 &&
3689 conf->prev.far_offset == 0)
3693 mddev->degraded = 0;
3695 i < conf->geo.raid_disks
3696 || i < conf->prev.raid_disks;
3699 disk = conf->mirrors + i;
3701 if (!disk->rdev && disk->replacement) {
3702 /* The replacement is all we have - use it */
3703 disk->rdev = disk->replacement;
3704 disk->replacement = NULL;
3705 clear_bit(Replacement, &disk->rdev->flags);
3709 !test_bit(In_sync, &disk->rdev->flags)) {
3710 disk->head_position = 0;
3713 disk->rdev->saved_raid_disk < 0)
3716 disk->recovery_disabled = mddev->recovery_disabled - 1;
3719 if (mddev->recovery_cp != MaxSector)
3720 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
3722 pr_info("md/raid10:%s: active with %d out of %d devices\n",
3723 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
3724 conf->geo.raid_disks);
3726 * Ok, everything is just fine now
3728 mddev->dev_sectors = conf->dev_sectors;
3729 size = raid10_size(mddev, 0, 0);
3730 md_set_array_sectors(mddev, size);
3731 mddev->resync_max_sectors = size;
3734 int stripe = conf->geo.raid_disks *
3735 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
3737 /* Calculate max read-ahead size.
3738 * We need to readahead at least twice a whole stripe....
3741 stripe /= conf->geo.near_copies;
3742 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3743 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
3746 if (md_integrity_register(mddev))
3749 if (conf->reshape_progress != MaxSector) {
3750 unsigned long before_length, after_length;
3752 before_length = ((1 << conf->prev.chunk_shift) *
3753 conf->prev.far_copies);
3754 after_length = ((1 << conf->geo.chunk_shift) *
3755 conf->geo.far_copies);
3757 if (max(before_length, after_length) > min_offset_diff) {
3758 /* This cannot work */
3759 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
3762 conf->offset_diff = min_offset_diff;
3764 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3765 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3766 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3767 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3768 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3775 md_unregister_thread(&mddev->thread);
3776 mempool_destroy(conf->r10bio_pool);
3777 safe_put_page(conf->tmppage);
3778 kfree(conf->mirrors);
3780 mddev->private = NULL;
3785 static void raid10_free(struct mddev *mddev, void *priv)
3787 struct r10conf *conf = priv;
3789 mempool_destroy(conf->r10bio_pool);
3790 safe_put_page(conf->tmppage);
3791 kfree(conf->mirrors);
3792 kfree(conf->mirrors_old);
3793 kfree(conf->mirrors_new);
3797 static void raid10_quiesce(struct mddev *mddev, int state)
3799 struct r10conf *conf = mddev->private;
3803 raise_barrier(conf, 0);
3806 lower_barrier(conf);
3811 static int raid10_resize(struct mddev *mddev, sector_t sectors)
3813 /* Resize of 'far' arrays is not supported.
3814 * For 'near' and 'offset' arrays we can set the
3815 * number of sectors used to be an appropriate multiple
3816 * of the chunk size.
3817 * For 'offset', this is far_copies*chunksize.
3818 * For 'near' the multiplier is the LCM of
3819 * near_copies and raid_disks.
3820 * So if far_copies > 1 && !far_offset, fail.
3821 * Else find LCM(raid_disks, near_copy)*far_copies and
3822 * multiply by chunk_size. Then round to this number.
3823 * This is mostly done by raid10_size()
3825 struct r10conf *conf = mddev->private;
3826 sector_t oldsize, size;
3828 if (mddev->reshape_position != MaxSector)
3831 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
3834 oldsize = raid10_size(mddev, 0, 0);
3835 size = raid10_size(mddev, sectors, 0);
3836 if (mddev->external_size &&
3837 mddev->array_sectors > size)
3839 if (mddev->bitmap) {
3840 int ret = bitmap_resize(mddev->bitmap, size, 0, 0);
3844 md_set_array_sectors(mddev, size);
3846 set_capacity(mddev->gendisk, mddev->array_sectors);
3847 revalidate_disk(mddev->gendisk);
3849 if (sectors > mddev->dev_sectors &&
3850 mddev->recovery_cp > oldsize) {
3851 mddev->recovery_cp = oldsize;
3852 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3854 calc_sectors(conf, sectors);
3855 mddev->dev_sectors = conf->dev_sectors;
3856 mddev->resync_max_sectors = size;
3860 static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs)
3862 struct md_rdev *rdev;
3863 struct r10conf *conf;
3865 if (mddev->degraded > 0) {
3866 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
3868 return ERR_PTR(-EINVAL);
3870 sector_div(size, devs);
3872 /* Set new parameters */
3873 mddev->new_level = 10;
3874 /* new layout: far_copies = 1, near_copies = 2 */
3875 mddev->new_layout = (1<<8) + 2;
3876 mddev->new_chunk_sectors = mddev->chunk_sectors;
3877 mddev->delta_disks = mddev->raid_disks;
3878 mddev->raid_disks *= 2;
3879 /* make sure it will be not marked as dirty */
3880 mddev->recovery_cp = MaxSector;
3881 mddev->dev_sectors = size;
3883 conf = setup_conf(mddev);
3884 if (!IS_ERR(conf)) {
3885 rdev_for_each(rdev, mddev)
3886 if (rdev->raid_disk >= 0) {
3887 rdev->new_raid_disk = rdev->raid_disk * 2;
3888 rdev->sectors = size;
3896 static void *raid10_takeover(struct mddev *mddev)
3898 struct r0conf *raid0_conf;
3900 /* raid10 can take over:
3901 * raid0 - providing it has only two drives
3903 if (mddev->level == 0) {
3904 /* for raid0 takeover only one zone is supported */
3905 raid0_conf = mddev->private;
3906 if (raid0_conf->nr_strip_zones > 1) {
3907 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
3909 return ERR_PTR(-EINVAL);
3911 return raid10_takeover_raid0(mddev,
3912 raid0_conf->strip_zone->zone_end,
3913 raid0_conf->strip_zone->nb_dev);
3915 return ERR_PTR(-EINVAL);
3918 static int raid10_check_reshape(struct mddev *mddev)
3920 /* Called when there is a request to change
3921 * - layout (to ->new_layout)
3922 * - chunk size (to ->new_chunk_sectors)
3923 * - raid_disks (by delta_disks)
3924 * or when trying to restart a reshape that was ongoing.
3926 * We need to validate the request and possibly allocate
3927 * space if that might be an issue later.
3929 * Currently we reject any reshape of a 'far' mode array,
3930 * allow chunk size to change if new is generally acceptable,
3931 * allow raid_disks to increase, and allow
3932 * a switch between 'near' mode and 'offset' mode.
3934 struct r10conf *conf = mddev->private;
3937 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
3940 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
3941 /* mustn't change number of copies */
3943 if (geo.far_copies > 1 && !geo.far_offset)
3944 /* Cannot switch to 'far' mode */
3947 if (mddev->array_sectors & geo.chunk_mask)
3948 /* not factor of array size */
3951 if (!enough(conf, -1))
3954 kfree(conf->mirrors_new);
3955 conf->mirrors_new = NULL;
3956 if (mddev->delta_disks > 0) {
3957 /* allocate new 'mirrors' list */
3958 conf->mirrors_new = kzalloc(
3959 sizeof(struct raid10_info)
3960 *(mddev->raid_disks +
3961 mddev->delta_disks),
3963 if (!conf->mirrors_new)
3970 * Need to check if array has failed when deciding whether to:
3972 * - remove non-faulty devices
3975 * This determination is simple when no reshape is happening.
3976 * However if there is a reshape, we need to carefully check
3977 * both the before and after sections.
3978 * This is because some failed devices may only affect one
3979 * of the two sections, and some non-in_sync devices may
3980 * be insync in the section most affected by failed devices.
3982 static int calc_degraded(struct r10conf *conf)
3984 int degraded, degraded2;
3989 /* 'prev' section first */
3990 for (i = 0; i < conf->prev.raid_disks; i++) {
3991 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
3992 if (!rdev || test_bit(Faulty, &rdev->flags))
3994 else if (!test_bit(In_sync, &rdev->flags))
3995 /* When we can reduce the number of devices in
3996 * an array, this might not contribute to
3997 * 'degraded'. It does now.
4002 if (conf->geo.raid_disks == conf->prev.raid_disks)
4006 for (i = 0; i < conf->geo.raid_disks; i++) {
4007 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4008 if (!rdev || test_bit(Faulty, &rdev->flags))
4010 else if (!test_bit(In_sync, &rdev->flags)) {
4011 /* If reshape is increasing the number of devices,
4012 * this section has already been recovered, so
4013 * it doesn't contribute to degraded.
4016 if (conf->geo.raid_disks <= conf->prev.raid_disks)
4021 if (degraded2 > degraded)
4026 static int raid10_start_reshape(struct mddev *mddev)
4028 /* A 'reshape' has been requested. This commits
4029 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4030 * This also checks if there are enough spares and adds them
4032 * We currently require enough spares to make the final
4033 * array non-degraded. We also require that the difference
4034 * between old and new data_offset - on each device - is
4035 * enough that we never risk over-writing.
4038 unsigned long before_length, after_length;
4039 sector_t min_offset_diff = 0;
4042 struct r10conf *conf = mddev->private;
4043 struct md_rdev *rdev;
4047 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4050 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4053 before_length = ((1 << conf->prev.chunk_shift) *
4054 conf->prev.far_copies);
4055 after_length = ((1 << conf->geo.chunk_shift) *
4056 conf->geo.far_copies);
4058 rdev_for_each(rdev, mddev) {
4059 if (!test_bit(In_sync, &rdev->flags)
4060 && !test_bit(Faulty, &rdev->flags))
4062 if (rdev->raid_disk >= 0) {
4063 long long diff = (rdev->new_data_offset
4064 - rdev->data_offset);
4065 if (!mddev->reshape_backwards)
4069 if (first || diff < min_offset_diff)
4070 min_offset_diff = diff;
4074 if (max(before_length, after_length) > min_offset_diff)
4077 if (spares < mddev->delta_disks)
4080 conf->offset_diff = min_offset_diff;
4081 spin_lock_irq(&conf->device_lock);
4082 if (conf->mirrors_new) {
4083 memcpy(conf->mirrors_new, conf->mirrors,
4084 sizeof(struct raid10_info)*conf->prev.raid_disks);
4086 kfree(conf->mirrors_old);
4087 conf->mirrors_old = conf->mirrors;
4088 conf->mirrors = conf->mirrors_new;
4089 conf->mirrors_new = NULL;
4091 setup_geo(&conf->geo, mddev, geo_start);
4093 if (mddev->reshape_backwards) {
4094 sector_t size = raid10_size(mddev, 0, 0);
4095 if (size < mddev->array_sectors) {
4096 spin_unlock_irq(&conf->device_lock);
4097 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4101 mddev->resync_max_sectors = size;
4102 conf->reshape_progress = size;
4104 conf->reshape_progress = 0;
4105 conf->reshape_safe = conf->reshape_progress;
4106 spin_unlock_irq(&conf->device_lock);
4108 if (mddev->delta_disks && mddev->bitmap) {
4109 ret = bitmap_resize(mddev->bitmap,
4110 raid10_size(mddev, 0,
4111 conf->geo.raid_disks),
4116 if (mddev->delta_disks > 0) {
4117 rdev_for_each(rdev, mddev)
4118 if (rdev->raid_disk < 0 &&
4119 !test_bit(Faulty, &rdev->flags)) {
4120 if (raid10_add_disk(mddev, rdev) == 0) {
4121 if (rdev->raid_disk >=
4122 conf->prev.raid_disks)
4123 set_bit(In_sync, &rdev->flags);
4125 rdev->recovery_offset = 0;
4127 if (sysfs_link_rdev(mddev, rdev))
4128 /* Failure here is OK */;
4130 } else if (rdev->raid_disk >= conf->prev.raid_disks
4131 && !test_bit(Faulty, &rdev->flags)) {
4132 /* This is a spare that was manually added */
4133 set_bit(In_sync, &rdev->flags);
4136 /* When a reshape changes the number of devices,
4137 * ->degraded is measured against the larger of the
4138 * pre and post numbers.
4140 spin_lock_irq(&conf->device_lock);
4141 mddev->degraded = calc_degraded(conf);
4142 spin_unlock_irq(&conf->device_lock);
4143 mddev->raid_disks = conf->geo.raid_disks;
4144 mddev->reshape_position = conf->reshape_progress;
4145 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4147 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4148 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4149 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
4150 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4151 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4153 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4155 if (!mddev->sync_thread) {
4159 conf->reshape_checkpoint = jiffies;
4160 md_wakeup_thread(mddev->sync_thread);
4161 md_new_event(mddev);
4165 mddev->recovery = 0;
4166 spin_lock_irq(&conf->device_lock);
4167 conf->geo = conf->prev;
4168 mddev->raid_disks = conf->geo.raid_disks;
4169 rdev_for_each(rdev, mddev)
4170 rdev->new_data_offset = rdev->data_offset;
4172 conf->reshape_progress = MaxSector;
4173 conf->reshape_safe = MaxSector;
4174 mddev->reshape_position = MaxSector;
4175 spin_unlock_irq(&conf->device_lock);
4179 /* Calculate the last device-address that could contain
4180 * any block from the chunk that includes the array-address 's'
4181 * and report the next address.
4182 * i.e. the address returned will be chunk-aligned and after
4183 * any data that is in the chunk containing 's'.
4185 static sector_t last_dev_address(sector_t s, struct geom *geo)
4187 s = (s | geo->chunk_mask) + 1;
4188 s >>= geo->chunk_shift;
4189 s *= geo->near_copies;
4190 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4191 s *= geo->far_copies;
4192 s <<= geo->chunk_shift;
4196 /* Calculate the first device-address that could contain
4197 * any block from the chunk that includes the array-address 's'.
4198 * This too will be the start of a chunk
4200 static sector_t first_dev_address(sector_t s, struct geom *geo)
4202 s >>= geo->chunk_shift;
4203 s *= geo->near_copies;
4204 sector_div(s, geo->raid_disks);
4205 s *= geo->far_copies;
4206 s <<= geo->chunk_shift;
4210 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4213 /* We simply copy at most one chunk (smallest of old and new)
4214 * at a time, possibly less if that exceeds RESYNC_PAGES,
4215 * or we hit a bad block or something.
4216 * This might mean we pause for normal IO in the middle of
4217 * a chunk, but that is not a problem as mddev->reshape_position
4218 * can record any location.
4220 * If we will want to write to a location that isn't
4221 * yet recorded as 'safe' (i.e. in metadata on disk) then
4222 * we need to flush all reshape requests and update the metadata.
4224 * When reshaping forwards (e.g. to more devices), we interpret
4225 * 'safe' as the earliest block which might not have been copied
4226 * down yet. We divide this by previous stripe size and multiply
4227 * by previous stripe length to get lowest device offset that we
4228 * cannot write to yet.
4229 * We interpret 'sector_nr' as an address that we want to write to.
4230 * From this we use last_device_address() to find where we might
4231 * write to, and first_device_address on the 'safe' position.
4232 * If this 'next' write position is after the 'safe' position,
4233 * we must update the metadata to increase the 'safe' position.
4235 * When reshaping backwards, we round in the opposite direction
4236 * and perform the reverse test: next write position must not be
4237 * less than current safe position.
4239 * In all this the minimum difference in data offsets
4240 * (conf->offset_diff - always positive) allows a bit of slack,
4241 * so next can be after 'safe', but not by more than offset_diff
4243 * We need to prepare all the bios here before we start any IO
4244 * to ensure the size we choose is acceptable to all devices.
4245 * The means one for each copy for write-out and an extra one for
4247 * We store the read-in bio in ->master_bio and the others in
4248 * ->devs[x].bio and ->devs[x].repl_bio.
4250 struct r10conf *conf = mddev->private;
4251 struct r10bio *r10_bio;
4252 sector_t next, safe, last;
4256 struct md_rdev *rdev;
4259 struct bio *bio, *read_bio;
4260 int sectors_done = 0;
4262 if (sector_nr == 0) {
4263 /* If restarting in the middle, skip the initial sectors */
4264 if (mddev->reshape_backwards &&
4265 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4266 sector_nr = (raid10_size(mddev, 0, 0)
4267 - conf->reshape_progress);
4268 } else if (!mddev->reshape_backwards &&
4269 conf->reshape_progress > 0)
4270 sector_nr = conf->reshape_progress;
4272 mddev->curr_resync_completed = sector_nr;
4273 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4279 /* We don't use sector_nr to track where we are up to
4280 * as that doesn't work well for ->reshape_backwards.
4281 * So just use ->reshape_progress.
4283 if (mddev->reshape_backwards) {
4284 /* 'next' is the earliest device address that we might
4285 * write to for this chunk in the new layout
4287 next = first_dev_address(conf->reshape_progress - 1,
4290 /* 'safe' is the last device address that we might read from
4291 * in the old layout after a restart
4293 safe = last_dev_address(conf->reshape_safe - 1,
4296 if (next + conf->offset_diff < safe)
4299 last = conf->reshape_progress - 1;
4300 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4301 & conf->prev.chunk_mask);
4302 if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
4303 sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
4305 /* 'next' is after the last device address that we
4306 * might write to for this chunk in the new layout
4308 next = last_dev_address(conf->reshape_progress, &conf->geo);
4310 /* 'safe' is the earliest device address that we might
4311 * read from in the old layout after a restart
4313 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4315 /* Need to update metadata if 'next' might be beyond 'safe'
4316 * as that would possibly corrupt data
4318 if (next > safe + conf->offset_diff)
4321 sector_nr = conf->reshape_progress;
4322 last = sector_nr | (conf->geo.chunk_mask
4323 & conf->prev.chunk_mask);
4325 if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
4326 last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
4330 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4331 /* Need to update reshape_position in metadata */
4333 mddev->reshape_position = conf->reshape_progress;
4334 if (mddev->reshape_backwards)
4335 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4336 - conf->reshape_progress;
4338 mddev->curr_resync_completed = conf->reshape_progress;
4339 conf->reshape_checkpoint = jiffies;
4340 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4341 md_wakeup_thread(mddev->thread);
4342 wait_event(mddev->sb_wait, mddev->flags == 0 ||
4343 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4344 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4345 allow_barrier(conf);
4346 return sectors_done;
4348 conf->reshape_safe = mddev->reshape_position;
4349 allow_barrier(conf);
4353 /* Now schedule reads for blocks from sector_nr to last */
4354 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
4356 raise_barrier(conf, sectors_done != 0);
4357 atomic_set(&r10_bio->remaining, 0);
4358 r10_bio->mddev = mddev;
4359 r10_bio->sector = sector_nr;
4360 set_bit(R10BIO_IsReshape, &r10_bio->state);
4361 r10_bio->sectors = last - sector_nr + 1;
4362 rdev = read_balance(conf, r10_bio, &max_sectors);
4363 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4366 /* Cannot read from here, so need to record bad blocks
4367 * on all the target devices.
4370 mempool_free(r10_bio, conf->r10buf_pool);
4371 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4372 return sectors_done;
4375 read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
4377 read_bio->bi_bdev = rdev->bdev;
4378 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
4379 + rdev->data_offset);
4380 read_bio->bi_private = r10_bio;
4381 read_bio->bi_end_io = end_sync_read;
4382 bio_set_op_attrs(read_bio, REQ_OP_READ, 0);
4383 read_bio->bi_flags &= (~0UL << BIO_RESET_BITS);
4384 read_bio->bi_error = 0;
4385 read_bio->bi_vcnt = 0;
4386 read_bio->bi_iter.bi_size = 0;
4387 r10_bio->master_bio = read_bio;
4388 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4390 /* Now find the locations in the new layout */
4391 __raid10_find_phys(&conf->geo, r10_bio);
4394 read_bio->bi_next = NULL;
4397 for (s = 0; s < conf->copies*2; s++) {
4399 int d = r10_bio->devs[s/2].devnum;
4400 struct md_rdev *rdev2;
4402 rdev2 = rcu_dereference(conf->mirrors[d].replacement);
4403 b = r10_bio->devs[s/2].repl_bio;
4405 rdev2 = rcu_dereference(conf->mirrors[d].rdev);
4406 b = r10_bio->devs[s/2].bio;
4408 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4412 b->bi_bdev = rdev2->bdev;
4413 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr +
4414 rdev2->new_data_offset;
4415 b->bi_private = r10_bio;
4416 b->bi_end_io = end_reshape_write;
4417 bio_set_op_attrs(b, REQ_OP_WRITE, 0);
4422 /* Now add as many pages as possible to all of these bios. */
4425 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
4426 struct page *page = r10_bio->devs[0].bio->bi_io_vec[s/(PAGE_SIZE>>9)].bv_page;
4427 int len = (max_sectors - s) << 9;
4428 if (len > PAGE_SIZE)
4430 for (bio = blist; bio ; bio = bio->bi_next) {
4432 if (bio_add_page(bio, page, len, 0))
4435 /* Didn't fit, must stop */
4437 bio2 && bio2 != bio;
4438 bio2 = bio2->bi_next) {
4439 /* Remove last page from this bio */
4441 bio2->bi_iter.bi_size -= len;
4442 bio_clear_flag(bio2, BIO_SEG_VALID);
4446 sector_nr += len >> 9;
4447 nr_sectors += len >> 9;
4451 r10_bio->sectors = nr_sectors;
4453 /* Now submit the read */
4454 md_sync_acct(read_bio->bi_bdev, r10_bio->sectors);
4455 atomic_inc(&r10_bio->remaining);
4456 read_bio->bi_next = NULL;
4457 generic_make_request(read_bio);
4458 sector_nr += nr_sectors;
4459 sectors_done += nr_sectors;
4460 if (sector_nr <= last)
4463 /* Now that we have done the whole section we can
4464 * update reshape_progress
4466 if (mddev->reshape_backwards)
4467 conf->reshape_progress -= sectors_done;
4469 conf->reshape_progress += sectors_done;
4471 return sectors_done;
4474 static void end_reshape_request(struct r10bio *r10_bio);
4475 static int handle_reshape_read_error(struct mddev *mddev,
4476 struct r10bio *r10_bio);
4477 static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
4479 /* Reshape read completed. Hopefully we have a block
4481 * If we got a read error then we do sync 1-page reads from
4482 * elsewhere until we find the data - or give up.
4484 struct r10conf *conf = mddev->private;
4487 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
4488 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
4489 /* Reshape has been aborted */
4490 md_done_sync(mddev, r10_bio->sectors, 0);
4494 /* We definitely have the data in the pages, schedule the
4497 atomic_set(&r10_bio->remaining, 1);
4498 for (s = 0; s < conf->copies*2; s++) {
4500 int d = r10_bio->devs[s/2].devnum;
4501 struct md_rdev *rdev;
4504 rdev = rcu_dereference(conf->mirrors[d].replacement);
4505 b = r10_bio->devs[s/2].repl_bio;
4507 rdev = rcu_dereference(conf->mirrors[d].rdev);
4508 b = r10_bio->devs[s/2].bio;
4510 if (!rdev || test_bit(Faulty, &rdev->flags)) {
4514 atomic_inc(&rdev->nr_pending);
4516 md_sync_acct(b->bi_bdev, r10_bio->sectors);
4517 atomic_inc(&r10_bio->remaining);
4519 generic_make_request(b);
4521 end_reshape_request(r10_bio);
4524 static void end_reshape(struct r10conf *conf)
4526 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
4529 spin_lock_irq(&conf->device_lock);
4530 conf->prev = conf->geo;
4531 md_finish_reshape(conf->mddev);
4533 conf->reshape_progress = MaxSector;
4534 conf->reshape_safe = MaxSector;
4535 spin_unlock_irq(&conf->device_lock);
4537 /* read-ahead size must cover two whole stripes, which is
4538 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4540 if (conf->mddev->queue) {
4541 int stripe = conf->geo.raid_disks *
4542 ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
4543 stripe /= conf->geo.near_copies;
4544 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4545 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4550 static int handle_reshape_read_error(struct mddev *mddev,
4551 struct r10bio *r10_bio)
4553 /* Use sync reads to get the blocks from somewhere else */
4554 int sectors = r10_bio->sectors;
4555 struct r10conf *conf = mddev->private;
4557 struct r10bio r10_bio;
4558 struct r10dev devs[conf->copies];
4560 struct r10bio *r10b = &on_stack.r10_bio;
4563 struct bio_vec *bvec = r10_bio->master_bio->bi_io_vec;
4565 r10b->sector = r10_bio->sector;
4566 __raid10_find_phys(&conf->prev, r10b);
4571 int first_slot = slot;
4573 if (s > (PAGE_SIZE >> 9))
4578 int d = r10b->devs[slot].devnum;
4579 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
4582 test_bit(Faulty, &rdev->flags) ||
4583 !test_bit(In_sync, &rdev->flags))
4586 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
4587 atomic_inc(&rdev->nr_pending);
4589 success = sync_page_io(rdev,
4593 REQ_OP_READ, 0, false);
4594 rdev_dec_pending(rdev, mddev);
4600 if (slot >= conf->copies)
4602 if (slot == first_slot)
4607 /* couldn't read this block, must give up */
4608 set_bit(MD_RECOVERY_INTR,
4618 static void end_reshape_write(struct bio *bio)
4620 struct r10bio *r10_bio = bio->bi_private;
4621 struct mddev *mddev = r10_bio->mddev;
4622 struct r10conf *conf = mddev->private;
4626 struct md_rdev *rdev = NULL;
4628 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
4630 rdev = conf->mirrors[d].replacement;
4633 rdev = conf->mirrors[d].rdev;
4636 if (bio->bi_error) {
4637 /* FIXME should record badblock */
4638 md_error(mddev, rdev);
4641 rdev_dec_pending(rdev, mddev);
4642 end_reshape_request(r10_bio);
4645 static void end_reshape_request(struct r10bio *r10_bio)
4647 if (!atomic_dec_and_test(&r10_bio->remaining))
4649 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
4650 bio_put(r10_bio->master_bio);
4654 static void raid10_finish_reshape(struct mddev *mddev)
4656 struct r10conf *conf = mddev->private;
4658 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4661 if (mddev->delta_disks > 0) {
4662 sector_t size = raid10_size(mddev, 0, 0);
4663 md_set_array_sectors(mddev, size);
4664 if (mddev->recovery_cp > mddev->resync_max_sectors) {
4665 mddev->recovery_cp = mddev->resync_max_sectors;
4666 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4668 mddev->resync_max_sectors = size;
4670 set_capacity(mddev->gendisk, mddev->array_sectors);
4671 revalidate_disk(mddev->gendisk);
4676 for (d = conf->geo.raid_disks ;
4677 d < conf->geo.raid_disks - mddev->delta_disks;
4679 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
4681 clear_bit(In_sync, &rdev->flags);
4682 rdev = rcu_dereference(conf->mirrors[d].replacement);
4684 clear_bit(In_sync, &rdev->flags);
4688 mddev->layout = mddev->new_layout;
4689 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
4690 mddev->reshape_position = MaxSector;
4691 mddev->delta_disks = 0;
4692 mddev->reshape_backwards = 0;
4695 static struct md_personality raid10_personality =
4699 .owner = THIS_MODULE,
4700 .make_request = raid10_make_request,
4702 .free = raid10_free,
4703 .status = raid10_status,
4704 .error_handler = raid10_error,
4705 .hot_add_disk = raid10_add_disk,
4706 .hot_remove_disk= raid10_remove_disk,
4707 .spare_active = raid10_spare_active,
4708 .sync_request = raid10_sync_request,
4709 .quiesce = raid10_quiesce,
4710 .size = raid10_size,
4711 .resize = raid10_resize,
4712 .takeover = raid10_takeover,
4713 .check_reshape = raid10_check_reshape,
4714 .start_reshape = raid10_start_reshape,
4715 .finish_reshape = raid10_finish_reshape,
4716 .congested = raid10_congested,
4719 static int __init raid_init(void)
4721 return register_md_personality(&raid10_personality);
4724 static void raid_exit(void)
4726 unregister_md_personality(&raid10_personality);
4729 module_init(raid_init);
4730 module_exit(raid_exit);
4731 MODULE_LICENSE("GPL");
4732 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
4733 MODULE_ALIAS("md-personality-9"); /* RAID10 */
4734 MODULE_ALIAS("md-raid10");
4735 MODULE_ALIAS("md-level-10");
4737 module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);