Linux 4.19.133
[linux/fpc-iii.git] / drivers / md / raid10.c
blob02c5e390f89f32ca26095dd015d4b09f01843c59
1 /*
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)
14 * any later version.
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>
29 #include "md.h"
30 #include "raid10.h"
31 #include "raid0.h"
32 #include "md-bitmap.h"
35 * RAID10 provides a combination of RAID0 and RAID1 functionality.
36 * The layout of data is defined by
37 * chunk_size
38 * raid_disks
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
65 * on a device):
66 * A B C D A B C D E
67 * ... ...
68 * D A B C E A B C D
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
96 * for writeback.
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 int enough(struct r10conf *conf, int ignore);
104 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
105 int *skipped);
106 static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio);
107 static void end_reshape_write(struct bio *bio);
108 static void end_reshape(struct r10conf *conf);
110 #define raid10_log(md, fmt, args...) \
111 do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid10 " fmt, ##args); } while (0)
113 #include "raid1-10.c"
116 * for resync bio, r10bio pointer can be retrieved from the per-bio
117 * 'struct resync_pages'.
119 static inline struct r10bio *get_resync_r10bio(struct bio *bio)
121 return get_resync_pages(bio)->raid_bio;
124 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
126 struct r10conf *conf = data;
127 int size = offsetof(struct r10bio, devs[conf->copies]);
129 /* allocate a r10bio with room for raid_disks entries in the
130 * bios array */
131 return kzalloc(size, gfp_flags);
134 static void r10bio_pool_free(void *r10_bio, void *data)
136 kfree(r10_bio);
139 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
140 /* amount of memory to reserve for resync requests */
141 #define RESYNC_WINDOW (1024*1024)
142 /* maximum number of concurrent requests, memory permitting */
143 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
144 #define CLUSTER_RESYNC_WINDOW (32 * RESYNC_WINDOW)
145 #define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9)
148 * When performing a resync, we need to read and compare, so
149 * we need as many pages are there are copies.
150 * When performing a recovery, we need 2 bios, one for read,
151 * one for write (we recover only one drive per r10buf)
154 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
156 struct r10conf *conf = data;
157 struct r10bio *r10_bio;
158 struct bio *bio;
159 int j;
160 int nalloc, nalloc_rp;
161 struct resync_pages *rps;
163 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
164 if (!r10_bio)
165 return NULL;
167 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
168 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
169 nalloc = conf->copies; /* resync */
170 else
171 nalloc = 2; /* recovery */
173 /* allocate once for all bios */
174 if (!conf->have_replacement)
175 nalloc_rp = nalloc;
176 else
177 nalloc_rp = nalloc * 2;
178 rps = kmalloc_array(nalloc_rp, sizeof(struct resync_pages), gfp_flags);
179 if (!rps)
180 goto out_free_r10bio;
183 * Allocate bios.
185 for (j = nalloc ; j-- ; ) {
186 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
187 if (!bio)
188 goto out_free_bio;
189 r10_bio->devs[j].bio = bio;
190 if (!conf->have_replacement)
191 continue;
192 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
193 if (!bio)
194 goto out_free_bio;
195 r10_bio->devs[j].repl_bio = bio;
198 * Allocate RESYNC_PAGES data pages and attach them
199 * where needed.
201 for (j = 0; j < nalloc; j++) {
202 struct bio *rbio = r10_bio->devs[j].repl_bio;
203 struct resync_pages *rp, *rp_repl;
205 rp = &rps[j];
206 if (rbio)
207 rp_repl = &rps[nalloc + j];
209 bio = r10_bio->devs[j].bio;
211 if (!j || test_bit(MD_RECOVERY_SYNC,
212 &conf->mddev->recovery)) {
213 if (resync_alloc_pages(rp, gfp_flags))
214 goto out_free_pages;
215 } else {
216 memcpy(rp, &rps[0], sizeof(*rp));
217 resync_get_all_pages(rp);
220 rp->raid_bio = r10_bio;
221 bio->bi_private = rp;
222 if (rbio) {
223 memcpy(rp_repl, rp, sizeof(*rp));
224 rbio->bi_private = rp_repl;
228 return r10_bio;
230 out_free_pages:
231 while (--j >= 0)
232 resync_free_pages(&rps[j]);
234 j = 0;
235 out_free_bio:
236 for ( ; j < nalloc; j++) {
237 if (r10_bio->devs[j].bio)
238 bio_put(r10_bio->devs[j].bio);
239 if (r10_bio->devs[j].repl_bio)
240 bio_put(r10_bio->devs[j].repl_bio);
242 kfree(rps);
243 out_free_r10bio:
244 r10bio_pool_free(r10_bio, conf);
245 return NULL;
248 static void r10buf_pool_free(void *__r10_bio, void *data)
250 struct r10conf *conf = data;
251 struct r10bio *r10bio = __r10_bio;
252 int j;
253 struct resync_pages *rp = NULL;
255 for (j = conf->copies; j--; ) {
256 struct bio *bio = r10bio->devs[j].bio;
258 if (bio) {
259 rp = get_resync_pages(bio);
260 resync_free_pages(rp);
261 bio_put(bio);
264 bio = r10bio->devs[j].repl_bio;
265 if (bio)
266 bio_put(bio);
269 /* resync pages array stored in the 1st bio's .bi_private */
270 kfree(rp);
272 r10bio_pool_free(r10bio, conf);
275 static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
277 int i;
279 for (i = 0; i < conf->copies; i++) {
280 struct bio **bio = & r10_bio->devs[i].bio;
281 if (!BIO_SPECIAL(*bio))
282 bio_put(*bio);
283 *bio = NULL;
284 bio = &r10_bio->devs[i].repl_bio;
285 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
286 bio_put(*bio);
287 *bio = NULL;
291 static void free_r10bio(struct r10bio *r10_bio)
293 struct r10conf *conf = r10_bio->mddev->private;
295 put_all_bios(conf, r10_bio);
296 mempool_free(r10_bio, &conf->r10bio_pool);
299 static void put_buf(struct r10bio *r10_bio)
301 struct r10conf *conf = r10_bio->mddev->private;
303 mempool_free(r10_bio, &conf->r10buf_pool);
305 lower_barrier(conf);
308 static void reschedule_retry(struct r10bio *r10_bio)
310 unsigned long flags;
311 struct mddev *mddev = r10_bio->mddev;
312 struct r10conf *conf = mddev->private;
314 spin_lock_irqsave(&conf->device_lock, flags);
315 list_add(&r10_bio->retry_list, &conf->retry_list);
316 conf->nr_queued ++;
317 spin_unlock_irqrestore(&conf->device_lock, flags);
319 /* wake up frozen array... */
320 wake_up(&conf->wait_barrier);
322 md_wakeup_thread(mddev->thread);
326 * raid_end_bio_io() is called when we have finished servicing a mirrored
327 * operation and are ready to return a success/failure code to the buffer
328 * cache layer.
330 static void raid_end_bio_io(struct r10bio *r10_bio)
332 struct bio *bio = r10_bio->master_bio;
333 struct r10conf *conf = r10_bio->mddev->private;
335 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
336 bio->bi_status = BLK_STS_IOERR;
338 bio_endio(bio);
340 * Wake up any possible resync thread that waits for the device
341 * to go idle.
343 allow_barrier(conf);
345 free_r10bio(r10_bio);
349 * Update disk head position estimator based on IRQ completion info.
351 static inline void update_head_pos(int slot, struct r10bio *r10_bio)
353 struct r10conf *conf = r10_bio->mddev->private;
355 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
356 r10_bio->devs[slot].addr + (r10_bio->sectors);
360 * Find the disk number which triggered given bio
362 static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
363 struct bio *bio, int *slotp, int *replp)
365 int slot;
366 int repl = 0;
368 for (slot = 0; slot < conf->copies; slot++) {
369 if (r10_bio->devs[slot].bio == bio)
370 break;
371 if (r10_bio->devs[slot].repl_bio == bio) {
372 repl = 1;
373 break;
377 BUG_ON(slot == conf->copies);
378 update_head_pos(slot, r10_bio);
380 if (slotp)
381 *slotp = slot;
382 if (replp)
383 *replp = repl;
384 return r10_bio->devs[slot].devnum;
387 static void raid10_end_read_request(struct bio *bio)
389 int uptodate = !bio->bi_status;
390 struct r10bio *r10_bio = bio->bi_private;
391 int slot;
392 struct md_rdev *rdev;
393 struct r10conf *conf = r10_bio->mddev->private;
395 slot = r10_bio->read_slot;
396 rdev = r10_bio->devs[slot].rdev;
398 * this branch is our 'one mirror IO has finished' event handler:
400 update_head_pos(slot, r10_bio);
402 if (uptodate) {
404 * Set R10BIO_Uptodate in our master bio, so that
405 * we will return a good error code to the higher
406 * levels even if IO on some other mirrored buffer fails.
408 * The 'master' represents the composite IO operation to
409 * user-side. So if something waits for IO, then it will
410 * wait for the 'master' bio.
412 set_bit(R10BIO_Uptodate, &r10_bio->state);
413 } else {
414 /* If all other devices that store this block have
415 * failed, we want to return the error upwards rather
416 * than fail the last device. Here we redefine
417 * "uptodate" to mean "Don't want to retry"
419 if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state),
420 rdev->raid_disk))
421 uptodate = 1;
423 if (uptodate) {
424 raid_end_bio_io(r10_bio);
425 rdev_dec_pending(rdev, conf->mddev);
426 } else {
428 * oops, read error - keep the refcount on the rdev
430 char b[BDEVNAME_SIZE];
431 pr_err_ratelimited("md/raid10:%s: %s: rescheduling sector %llu\n",
432 mdname(conf->mddev),
433 bdevname(rdev->bdev, b),
434 (unsigned long long)r10_bio->sector);
435 set_bit(R10BIO_ReadError, &r10_bio->state);
436 reschedule_retry(r10_bio);
440 static void close_write(struct r10bio *r10_bio)
442 /* clear the bitmap if all writes complete successfully */
443 md_bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
444 r10_bio->sectors,
445 !test_bit(R10BIO_Degraded, &r10_bio->state),
447 md_write_end(r10_bio->mddev);
450 static void one_write_done(struct r10bio *r10_bio)
452 if (atomic_dec_and_test(&r10_bio->remaining)) {
453 if (test_bit(R10BIO_WriteError, &r10_bio->state))
454 reschedule_retry(r10_bio);
455 else {
456 close_write(r10_bio);
457 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
458 reschedule_retry(r10_bio);
459 else
460 raid_end_bio_io(r10_bio);
465 static void raid10_end_write_request(struct bio *bio)
467 struct r10bio *r10_bio = bio->bi_private;
468 int dev;
469 int dec_rdev = 1;
470 struct r10conf *conf = r10_bio->mddev->private;
471 int slot, repl;
472 struct md_rdev *rdev = NULL;
473 struct bio *to_put = NULL;
474 bool discard_error;
476 discard_error = bio->bi_status && bio_op(bio) == REQ_OP_DISCARD;
478 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
480 if (repl)
481 rdev = conf->mirrors[dev].replacement;
482 if (!rdev) {
483 smp_rmb();
484 repl = 0;
485 rdev = conf->mirrors[dev].rdev;
488 * this branch is our 'one mirror IO has finished' event handler:
490 if (bio->bi_status && !discard_error) {
491 if (repl)
492 /* Never record new bad blocks to replacement,
493 * just fail it.
495 md_error(rdev->mddev, rdev);
496 else {
497 set_bit(WriteErrorSeen, &rdev->flags);
498 if (!test_and_set_bit(WantReplacement, &rdev->flags))
499 set_bit(MD_RECOVERY_NEEDED,
500 &rdev->mddev->recovery);
502 dec_rdev = 0;
503 if (test_bit(FailFast, &rdev->flags) &&
504 (bio->bi_opf & MD_FAILFAST)) {
505 md_error(rdev->mddev, rdev);
506 if (!test_bit(Faulty, &rdev->flags))
507 /* This is the only remaining device,
508 * We need to retry the write without
509 * FailFast
511 set_bit(R10BIO_WriteError, &r10_bio->state);
512 else {
513 r10_bio->devs[slot].bio = NULL;
514 to_put = bio;
515 dec_rdev = 1;
517 } else
518 set_bit(R10BIO_WriteError, &r10_bio->state);
520 } else {
522 * Set R10BIO_Uptodate in our master bio, so that
523 * we will return a good error code for to the higher
524 * levels even if IO on some other mirrored buffer fails.
526 * The 'master' represents the composite IO operation to
527 * user-side. So if something waits for IO, then it will
528 * wait for the 'master' bio.
530 sector_t first_bad;
531 int bad_sectors;
534 * Do not set R10BIO_Uptodate if the current device is
535 * rebuilding or Faulty. This is because we cannot use
536 * such device for properly reading the data back (we could
537 * potentially use it, if the current write would have felt
538 * before rdev->recovery_offset, but for simplicity we don't
539 * check this here.
541 if (test_bit(In_sync, &rdev->flags) &&
542 !test_bit(Faulty, &rdev->flags))
543 set_bit(R10BIO_Uptodate, &r10_bio->state);
545 /* Maybe we can clear some bad blocks. */
546 if (is_badblock(rdev,
547 r10_bio->devs[slot].addr,
548 r10_bio->sectors,
549 &first_bad, &bad_sectors) && !discard_error) {
550 bio_put(bio);
551 if (repl)
552 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
553 else
554 r10_bio->devs[slot].bio = IO_MADE_GOOD;
555 dec_rdev = 0;
556 set_bit(R10BIO_MadeGood, &r10_bio->state);
562 * Let's see if all mirrored write operations have finished
563 * already.
565 one_write_done(r10_bio);
566 if (dec_rdev)
567 rdev_dec_pending(rdev, conf->mddev);
568 if (to_put)
569 bio_put(to_put);
573 * RAID10 layout manager
574 * As well as the chunksize and raid_disks count, there are two
575 * parameters: near_copies and far_copies.
576 * near_copies * far_copies must be <= raid_disks.
577 * Normally one of these will be 1.
578 * If both are 1, we get raid0.
579 * If near_copies == raid_disks, we get raid1.
581 * Chunks are laid out in raid0 style with near_copies copies of the
582 * first chunk, followed by near_copies copies of the next chunk and
583 * so on.
584 * If far_copies > 1, then after 1/far_copies of the array has been assigned
585 * as described above, we start again with a device offset of near_copies.
586 * So we effectively have another copy of the whole array further down all
587 * the drives, but with blocks on different drives.
588 * With this layout, and block is never stored twice on the one device.
590 * raid10_find_phys finds the sector offset of a given virtual sector
591 * on each device that it is on.
593 * raid10_find_virt does the reverse mapping, from a device and a
594 * sector offset to a virtual address
597 static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
599 int n,f;
600 sector_t sector;
601 sector_t chunk;
602 sector_t stripe;
603 int dev;
604 int slot = 0;
605 int last_far_set_start, last_far_set_size;
607 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
608 last_far_set_start *= geo->far_set_size;
610 last_far_set_size = geo->far_set_size;
611 last_far_set_size += (geo->raid_disks % geo->far_set_size);
613 /* now calculate first sector/dev */
614 chunk = r10bio->sector >> geo->chunk_shift;
615 sector = r10bio->sector & geo->chunk_mask;
617 chunk *= geo->near_copies;
618 stripe = chunk;
619 dev = sector_div(stripe, geo->raid_disks);
620 if (geo->far_offset)
621 stripe *= geo->far_copies;
623 sector += stripe << geo->chunk_shift;
625 /* and calculate all the others */
626 for (n = 0; n < geo->near_copies; n++) {
627 int d = dev;
628 int set;
629 sector_t s = sector;
630 r10bio->devs[slot].devnum = d;
631 r10bio->devs[slot].addr = s;
632 slot++;
634 for (f = 1; f < geo->far_copies; f++) {
635 set = d / geo->far_set_size;
636 d += geo->near_copies;
638 if ((geo->raid_disks % geo->far_set_size) &&
639 (d > last_far_set_start)) {
640 d -= last_far_set_start;
641 d %= last_far_set_size;
642 d += last_far_set_start;
643 } else {
644 d %= geo->far_set_size;
645 d += geo->far_set_size * set;
647 s += geo->stride;
648 r10bio->devs[slot].devnum = d;
649 r10bio->devs[slot].addr = s;
650 slot++;
652 dev++;
653 if (dev >= geo->raid_disks) {
654 dev = 0;
655 sector += (geo->chunk_mask + 1);
660 static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
662 struct geom *geo = &conf->geo;
664 if (conf->reshape_progress != MaxSector &&
665 ((r10bio->sector >= conf->reshape_progress) !=
666 conf->mddev->reshape_backwards)) {
667 set_bit(R10BIO_Previous, &r10bio->state);
668 geo = &conf->prev;
669 } else
670 clear_bit(R10BIO_Previous, &r10bio->state);
672 __raid10_find_phys(geo, r10bio);
675 static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
677 sector_t offset, chunk, vchunk;
678 /* Never use conf->prev as this is only called during resync
679 * or recovery, so reshape isn't happening
681 struct geom *geo = &conf->geo;
682 int far_set_start = (dev / geo->far_set_size) * geo->far_set_size;
683 int far_set_size = geo->far_set_size;
684 int last_far_set_start;
686 if (geo->raid_disks % geo->far_set_size) {
687 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
688 last_far_set_start *= geo->far_set_size;
690 if (dev >= last_far_set_start) {
691 far_set_size = geo->far_set_size;
692 far_set_size += (geo->raid_disks % geo->far_set_size);
693 far_set_start = last_far_set_start;
697 offset = sector & geo->chunk_mask;
698 if (geo->far_offset) {
699 int fc;
700 chunk = sector >> geo->chunk_shift;
701 fc = sector_div(chunk, geo->far_copies);
702 dev -= fc * geo->near_copies;
703 if (dev < far_set_start)
704 dev += far_set_size;
705 } else {
706 while (sector >= geo->stride) {
707 sector -= geo->stride;
708 if (dev < (geo->near_copies + far_set_start))
709 dev += far_set_size - geo->near_copies;
710 else
711 dev -= geo->near_copies;
713 chunk = sector >> geo->chunk_shift;
715 vchunk = chunk * geo->raid_disks + dev;
716 sector_div(vchunk, geo->near_copies);
717 return (vchunk << geo->chunk_shift) + offset;
721 * This routine returns the disk from which the requested read should
722 * be done. There is a per-array 'next expected sequential IO' sector
723 * number - if this matches on the next IO then we use the last disk.
724 * There is also a per-disk 'last know head position' sector that is
725 * maintained from IRQ contexts, both the normal and the resync IO
726 * completion handlers update this position correctly. If there is no
727 * perfect sequential match then we pick the disk whose head is closest.
729 * If there are 2 mirrors in the same 2 devices, performance degrades
730 * because position is mirror, not device based.
732 * The rdev for the device selected will have nr_pending incremented.
736 * FIXME: possibly should rethink readbalancing and do it differently
737 * depending on near_copies / far_copies geometry.
739 static struct md_rdev *read_balance(struct r10conf *conf,
740 struct r10bio *r10_bio,
741 int *max_sectors)
743 const sector_t this_sector = r10_bio->sector;
744 int disk, slot;
745 int sectors = r10_bio->sectors;
746 int best_good_sectors;
747 sector_t new_distance, best_dist;
748 struct md_rdev *best_rdev, *rdev = NULL;
749 int do_balance;
750 int best_slot;
751 struct geom *geo = &conf->geo;
753 raid10_find_phys(conf, r10_bio);
754 rcu_read_lock();
755 best_slot = -1;
756 best_rdev = NULL;
757 best_dist = MaxSector;
758 best_good_sectors = 0;
759 do_balance = 1;
760 clear_bit(R10BIO_FailFast, &r10_bio->state);
762 * Check if we can balance. We can balance on the whole
763 * device if no resync is going on (recovery is ok), or below
764 * the resync window. We take the first readable disk when
765 * above the resync window.
767 if ((conf->mddev->recovery_cp < MaxSector
768 && (this_sector + sectors >= conf->next_resync)) ||
769 (mddev_is_clustered(conf->mddev) &&
770 md_cluster_ops->area_resyncing(conf->mddev, READ, this_sector,
771 this_sector + sectors)))
772 do_balance = 0;
774 for (slot = 0; slot < conf->copies ; slot++) {
775 sector_t first_bad;
776 int bad_sectors;
777 sector_t dev_sector;
779 if (r10_bio->devs[slot].bio == IO_BLOCKED)
780 continue;
781 disk = r10_bio->devs[slot].devnum;
782 rdev = rcu_dereference(conf->mirrors[disk].replacement);
783 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
784 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
785 rdev = rcu_dereference(conf->mirrors[disk].rdev);
786 if (rdev == NULL ||
787 test_bit(Faulty, &rdev->flags))
788 continue;
789 if (!test_bit(In_sync, &rdev->flags) &&
790 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
791 continue;
793 dev_sector = r10_bio->devs[slot].addr;
794 if (is_badblock(rdev, dev_sector, sectors,
795 &first_bad, &bad_sectors)) {
796 if (best_dist < MaxSector)
797 /* Already have a better slot */
798 continue;
799 if (first_bad <= dev_sector) {
800 /* Cannot read here. If this is the
801 * 'primary' device, then we must not read
802 * beyond 'bad_sectors' from another device.
804 bad_sectors -= (dev_sector - first_bad);
805 if (!do_balance && sectors > bad_sectors)
806 sectors = bad_sectors;
807 if (best_good_sectors > sectors)
808 best_good_sectors = sectors;
809 } else {
810 sector_t good_sectors =
811 first_bad - dev_sector;
812 if (good_sectors > best_good_sectors) {
813 best_good_sectors = good_sectors;
814 best_slot = slot;
815 best_rdev = rdev;
817 if (!do_balance)
818 /* Must read from here */
819 break;
821 continue;
822 } else
823 best_good_sectors = sectors;
825 if (!do_balance)
826 break;
828 if (best_slot >= 0)
829 /* At least 2 disks to choose from so failfast is OK */
830 set_bit(R10BIO_FailFast, &r10_bio->state);
831 /* This optimisation is debatable, and completely destroys
832 * sequential read speed for 'far copies' arrays. So only
833 * keep it for 'near' arrays, and review those later.
835 if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending))
836 new_distance = 0;
838 /* for far > 1 always use the lowest address */
839 else if (geo->far_copies > 1)
840 new_distance = r10_bio->devs[slot].addr;
841 else
842 new_distance = abs(r10_bio->devs[slot].addr -
843 conf->mirrors[disk].head_position);
844 if (new_distance < best_dist) {
845 best_dist = new_distance;
846 best_slot = slot;
847 best_rdev = rdev;
850 if (slot >= conf->copies) {
851 slot = best_slot;
852 rdev = best_rdev;
855 if (slot >= 0) {
856 atomic_inc(&rdev->nr_pending);
857 r10_bio->read_slot = slot;
858 } else
859 rdev = NULL;
860 rcu_read_unlock();
861 *max_sectors = best_good_sectors;
863 return rdev;
866 static int raid10_congested(struct mddev *mddev, int bits)
868 struct r10conf *conf = mddev->private;
869 int i, ret = 0;
871 if ((bits & (1 << WB_async_congested)) &&
872 conf->pending_count >= max_queued_requests)
873 return 1;
875 rcu_read_lock();
876 for (i = 0;
877 (i < conf->geo.raid_disks || i < conf->prev.raid_disks)
878 && ret == 0;
879 i++) {
880 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
881 if (rdev && !test_bit(Faulty, &rdev->flags)) {
882 struct request_queue *q = bdev_get_queue(rdev->bdev);
884 ret |= bdi_congested(q->backing_dev_info, bits);
887 rcu_read_unlock();
888 return ret;
891 static void flush_pending_writes(struct r10conf *conf)
893 /* Any writes that have been queued but are awaiting
894 * bitmap updates get flushed here.
896 spin_lock_irq(&conf->device_lock);
898 if (conf->pending_bio_list.head) {
899 struct blk_plug plug;
900 struct bio *bio;
902 bio = bio_list_get(&conf->pending_bio_list);
903 conf->pending_count = 0;
904 spin_unlock_irq(&conf->device_lock);
907 * As this is called in a wait_event() loop (see freeze_array),
908 * current->state might be TASK_UNINTERRUPTIBLE which will
909 * cause a warning when we prepare to wait again. As it is
910 * rare that this path is taken, it is perfectly safe to force
911 * us to go around the wait_event() loop again, so the warning
912 * is a false-positive. Silence the warning by resetting
913 * thread state
915 __set_current_state(TASK_RUNNING);
917 blk_start_plug(&plug);
918 /* flush any pending bitmap writes to disk
919 * before proceeding w/ I/O */
920 md_bitmap_unplug(conf->mddev->bitmap);
921 wake_up(&conf->wait_barrier);
923 while (bio) { /* submit pending writes */
924 struct bio *next = bio->bi_next;
925 struct md_rdev *rdev = (void*)bio->bi_disk;
926 bio->bi_next = NULL;
927 bio_set_dev(bio, rdev->bdev);
928 if (test_bit(Faulty, &rdev->flags)) {
929 bio_io_error(bio);
930 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
931 !blk_queue_discard(bio->bi_disk->queue)))
932 /* Just ignore it */
933 bio_endio(bio);
934 else
935 generic_make_request(bio);
936 bio = next;
938 blk_finish_plug(&plug);
939 } else
940 spin_unlock_irq(&conf->device_lock);
943 /* Barriers....
944 * Sometimes we need to suspend IO while we do something else,
945 * either some resync/recovery, or reconfigure the array.
946 * To do this we raise a 'barrier'.
947 * The 'barrier' is a counter that can be raised multiple times
948 * to count how many activities are happening which preclude
949 * normal IO.
950 * We can only raise the barrier if there is no pending IO.
951 * i.e. if nr_pending == 0.
952 * We choose only to raise the barrier if no-one is waiting for the
953 * barrier to go down. This means that as soon as an IO request
954 * is ready, no other operations which require a barrier will start
955 * until the IO request has had a chance.
957 * So: regular IO calls 'wait_barrier'. When that returns there
958 * is no backgroup IO happening, It must arrange to call
959 * allow_barrier when it has finished its IO.
960 * backgroup IO calls must call raise_barrier. Once that returns
961 * there is no normal IO happeing. It must arrange to call
962 * lower_barrier when the particular background IO completes.
965 static void raise_barrier(struct r10conf *conf, int force)
967 BUG_ON(force && !conf->barrier);
968 spin_lock_irq(&conf->resync_lock);
970 /* Wait until no block IO is waiting (unless 'force') */
971 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
972 conf->resync_lock);
974 /* block any new IO from starting */
975 conf->barrier++;
977 /* Now wait for all pending IO to complete */
978 wait_event_lock_irq(conf->wait_barrier,
979 !atomic_read(&conf->nr_pending) && conf->barrier < RESYNC_DEPTH,
980 conf->resync_lock);
982 spin_unlock_irq(&conf->resync_lock);
985 static void lower_barrier(struct r10conf *conf)
987 unsigned long flags;
988 spin_lock_irqsave(&conf->resync_lock, flags);
989 conf->barrier--;
990 spin_unlock_irqrestore(&conf->resync_lock, flags);
991 wake_up(&conf->wait_barrier);
994 static void wait_barrier(struct r10conf *conf)
996 spin_lock_irq(&conf->resync_lock);
997 if (conf->barrier) {
998 conf->nr_waiting++;
999 /* Wait for the barrier to drop.
1000 * However if there are already pending
1001 * requests (preventing the barrier from
1002 * rising completely), and the
1003 * pre-process bio queue isn't empty,
1004 * then don't wait, as we need to empty
1005 * that queue to get the nr_pending
1006 * count down.
1008 raid10_log(conf->mddev, "wait barrier");
1009 wait_event_lock_irq(conf->wait_barrier,
1010 !conf->barrier ||
1011 (atomic_read(&conf->nr_pending) &&
1012 current->bio_list &&
1013 (!bio_list_empty(&current->bio_list[0]) ||
1014 !bio_list_empty(&current->bio_list[1]))),
1015 conf->resync_lock);
1016 conf->nr_waiting--;
1017 if (!conf->nr_waiting)
1018 wake_up(&conf->wait_barrier);
1020 atomic_inc(&conf->nr_pending);
1021 spin_unlock_irq(&conf->resync_lock);
1024 static void allow_barrier(struct r10conf *conf)
1026 if ((atomic_dec_and_test(&conf->nr_pending)) ||
1027 (conf->array_freeze_pending))
1028 wake_up(&conf->wait_barrier);
1031 static void freeze_array(struct r10conf *conf, int extra)
1033 /* stop syncio and normal IO and wait for everything to
1034 * go quiet.
1035 * We increment barrier and nr_waiting, and then
1036 * wait until nr_pending match nr_queued+extra
1037 * This is called in the context of one normal IO request
1038 * that has failed. Thus any sync request that might be pending
1039 * will be blocked by nr_pending, and we need to wait for
1040 * pending IO requests to complete or be queued for re-try.
1041 * Thus the number queued (nr_queued) plus this request (extra)
1042 * must match the number of pending IOs (nr_pending) before
1043 * we continue.
1045 spin_lock_irq(&conf->resync_lock);
1046 conf->array_freeze_pending++;
1047 conf->barrier++;
1048 conf->nr_waiting++;
1049 wait_event_lock_irq_cmd(conf->wait_barrier,
1050 atomic_read(&conf->nr_pending) == conf->nr_queued+extra,
1051 conf->resync_lock,
1052 flush_pending_writes(conf));
1054 conf->array_freeze_pending--;
1055 spin_unlock_irq(&conf->resync_lock);
1058 static void unfreeze_array(struct r10conf *conf)
1060 /* reverse the effect of the freeze */
1061 spin_lock_irq(&conf->resync_lock);
1062 conf->barrier--;
1063 conf->nr_waiting--;
1064 wake_up(&conf->wait_barrier);
1065 spin_unlock_irq(&conf->resync_lock);
1068 static sector_t choose_data_offset(struct r10bio *r10_bio,
1069 struct md_rdev *rdev)
1071 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1072 test_bit(R10BIO_Previous, &r10_bio->state))
1073 return rdev->data_offset;
1074 else
1075 return rdev->new_data_offset;
1078 struct raid10_plug_cb {
1079 struct blk_plug_cb cb;
1080 struct bio_list pending;
1081 int pending_cnt;
1084 static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1086 struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb,
1087 cb);
1088 struct mddev *mddev = plug->cb.data;
1089 struct r10conf *conf = mddev->private;
1090 struct bio *bio;
1092 if (from_schedule || current->bio_list) {
1093 spin_lock_irq(&conf->device_lock);
1094 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1095 conf->pending_count += plug->pending_cnt;
1096 spin_unlock_irq(&conf->device_lock);
1097 wake_up(&conf->wait_barrier);
1098 md_wakeup_thread(mddev->thread);
1099 kfree(plug);
1100 return;
1103 /* we aren't scheduling, so we can do the write-out directly. */
1104 bio = bio_list_get(&plug->pending);
1105 md_bitmap_unplug(mddev->bitmap);
1106 wake_up(&conf->wait_barrier);
1108 while (bio) { /* submit pending writes */
1109 struct bio *next = bio->bi_next;
1110 struct md_rdev *rdev = (void*)bio->bi_disk;
1111 bio->bi_next = NULL;
1112 bio_set_dev(bio, rdev->bdev);
1113 if (test_bit(Faulty, &rdev->flags)) {
1114 bio_io_error(bio);
1115 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
1116 !blk_queue_discard(bio->bi_disk->queue)))
1117 /* Just ignore it */
1118 bio_endio(bio);
1119 else
1120 generic_make_request(bio);
1121 bio = next;
1123 kfree(plug);
1126 static void raid10_read_request(struct mddev *mddev, struct bio *bio,
1127 struct r10bio *r10_bio)
1129 struct r10conf *conf = mddev->private;
1130 struct bio *read_bio;
1131 const int op = bio_op(bio);
1132 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1133 int max_sectors;
1134 sector_t sectors;
1135 struct md_rdev *rdev;
1136 char b[BDEVNAME_SIZE];
1137 int slot = r10_bio->read_slot;
1138 struct md_rdev *err_rdev = NULL;
1139 gfp_t gfp = GFP_NOIO;
1141 if (r10_bio->devs[slot].rdev) {
1143 * This is an error retry, but we cannot
1144 * safely dereference the rdev in the r10_bio,
1145 * we must use the one in conf.
1146 * If it has already been disconnected (unlikely)
1147 * we lose the device name in error messages.
1149 int disk;
1151 * As we are blocking raid10, it is a little safer to
1152 * use __GFP_HIGH.
1154 gfp = GFP_NOIO | __GFP_HIGH;
1156 rcu_read_lock();
1157 disk = r10_bio->devs[slot].devnum;
1158 err_rdev = rcu_dereference(conf->mirrors[disk].rdev);
1159 if (err_rdev)
1160 bdevname(err_rdev->bdev, b);
1161 else {
1162 strcpy(b, "???");
1163 /* This never gets dereferenced */
1164 err_rdev = r10_bio->devs[slot].rdev;
1166 rcu_read_unlock();
1169 * Register the new request and wait if the reconstruction
1170 * thread has put up a bar for new requests.
1171 * Continue immediately if no resync is active currently.
1173 wait_barrier(conf);
1175 sectors = r10_bio->sectors;
1176 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1177 bio->bi_iter.bi_sector < conf->reshape_progress &&
1178 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
1180 * IO spans the reshape position. Need to wait for reshape to
1181 * pass
1183 raid10_log(conf->mddev, "wait reshape");
1184 allow_barrier(conf);
1185 wait_event(conf->wait_barrier,
1186 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1187 conf->reshape_progress >= bio->bi_iter.bi_sector +
1188 sectors);
1189 wait_barrier(conf);
1192 rdev = read_balance(conf, r10_bio, &max_sectors);
1193 if (!rdev) {
1194 if (err_rdev) {
1195 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
1196 mdname(mddev), b,
1197 (unsigned long long)r10_bio->sector);
1199 raid_end_bio_io(r10_bio);
1200 return;
1202 if (err_rdev)
1203 pr_err_ratelimited("md/raid10:%s: %s: redirecting sector %llu to another mirror\n",
1204 mdname(mddev),
1205 bdevname(rdev->bdev, b),
1206 (unsigned long long)r10_bio->sector);
1207 if (max_sectors < bio_sectors(bio)) {
1208 struct bio *split = bio_split(bio, max_sectors,
1209 gfp, &conf->bio_split);
1210 bio_chain(split, bio);
1211 allow_barrier(conf);
1212 generic_make_request(bio);
1213 wait_barrier(conf);
1214 bio = split;
1215 r10_bio->master_bio = bio;
1216 r10_bio->sectors = max_sectors;
1218 slot = r10_bio->read_slot;
1220 read_bio = bio_clone_fast(bio, gfp, &mddev->bio_set);
1222 r10_bio->devs[slot].bio = read_bio;
1223 r10_bio->devs[slot].rdev = rdev;
1225 read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr +
1226 choose_data_offset(r10_bio, rdev);
1227 bio_set_dev(read_bio, rdev->bdev);
1228 read_bio->bi_end_io = raid10_end_read_request;
1229 bio_set_op_attrs(read_bio, op, do_sync);
1230 if (test_bit(FailFast, &rdev->flags) &&
1231 test_bit(R10BIO_FailFast, &r10_bio->state))
1232 read_bio->bi_opf |= MD_FAILFAST;
1233 read_bio->bi_private = r10_bio;
1235 if (mddev->gendisk)
1236 trace_block_bio_remap(read_bio->bi_disk->queue,
1237 read_bio, disk_devt(mddev->gendisk),
1238 r10_bio->sector);
1239 generic_make_request(read_bio);
1240 return;
1243 static void raid10_write_one_disk(struct mddev *mddev, struct r10bio *r10_bio,
1244 struct bio *bio, bool replacement,
1245 int n_copy)
1247 const int op = bio_op(bio);
1248 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1249 const unsigned long do_fua = (bio->bi_opf & REQ_FUA);
1250 unsigned long flags;
1251 struct blk_plug_cb *cb;
1252 struct raid10_plug_cb *plug = NULL;
1253 struct r10conf *conf = mddev->private;
1254 struct md_rdev *rdev;
1255 int devnum = r10_bio->devs[n_copy].devnum;
1256 struct bio *mbio;
1258 if (replacement) {
1259 rdev = conf->mirrors[devnum].replacement;
1260 if (rdev == NULL) {
1261 /* Replacement just got moved to main 'rdev' */
1262 smp_mb();
1263 rdev = conf->mirrors[devnum].rdev;
1265 } else
1266 rdev = conf->mirrors[devnum].rdev;
1268 mbio = bio_clone_fast(bio, GFP_NOIO, &mddev->bio_set);
1269 if (replacement)
1270 r10_bio->devs[n_copy].repl_bio = mbio;
1271 else
1272 r10_bio->devs[n_copy].bio = mbio;
1274 mbio->bi_iter.bi_sector = (r10_bio->devs[n_copy].addr +
1275 choose_data_offset(r10_bio, rdev));
1276 bio_set_dev(mbio, rdev->bdev);
1277 mbio->bi_end_io = raid10_end_write_request;
1278 bio_set_op_attrs(mbio, op, do_sync | do_fua);
1279 if (!replacement && test_bit(FailFast,
1280 &conf->mirrors[devnum].rdev->flags)
1281 && enough(conf, devnum))
1282 mbio->bi_opf |= MD_FAILFAST;
1283 mbio->bi_private = r10_bio;
1285 if (conf->mddev->gendisk)
1286 trace_block_bio_remap(mbio->bi_disk->queue,
1287 mbio, disk_devt(conf->mddev->gendisk),
1288 r10_bio->sector);
1289 /* flush_pending_writes() needs access to the rdev so...*/
1290 mbio->bi_disk = (void *)rdev;
1292 atomic_inc(&r10_bio->remaining);
1294 cb = blk_check_plugged(raid10_unplug, mddev, sizeof(*plug));
1295 if (cb)
1296 plug = container_of(cb, struct raid10_plug_cb, cb);
1297 else
1298 plug = NULL;
1299 if (plug) {
1300 bio_list_add(&plug->pending, mbio);
1301 plug->pending_cnt++;
1302 } else {
1303 spin_lock_irqsave(&conf->device_lock, flags);
1304 bio_list_add(&conf->pending_bio_list, mbio);
1305 conf->pending_count++;
1306 spin_unlock_irqrestore(&conf->device_lock, flags);
1307 md_wakeup_thread(mddev->thread);
1311 static void raid10_write_request(struct mddev *mddev, struct bio *bio,
1312 struct r10bio *r10_bio)
1314 struct r10conf *conf = mddev->private;
1315 int i;
1316 struct md_rdev *blocked_rdev;
1317 sector_t sectors;
1318 int max_sectors;
1320 if ((mddev_is_clustered(mddev) &&
1321 md_cluster_ops->area_resyncing(mddev, WRITE,
1322 bio->bi_iter.bi_sector,
1323 bio_end_sector(bio)))) {
1324 DEFINE_WAIT(w);
1325 for (;;) {
1326 prepare_to_wait(&conf->wait_barrier,
1327 &w, TASK_IDLE);
1328 if (!md_cluster_ops->area_resyncing(mddev, WRITE,
1329 bio->bi_iter.bi_sector, bio_end_sector(bio)))
1330 break;
1331 schedule();
1333 finish_wait(&conf->wait_barrier, &w);
1337 * Register the new request and wait if the reconstruction
1338 * thread has put up a bar for new requests.
1339 * Continue immediately if no resync is active currently.
1341 wait_barrier(conf);
1343 sectors = r10_bio->sectors;
1344 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1345 bio->bi_iter.bi_sector < conf->reshape_progress &&
1346 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
1348 * IO spans the reshape position. Need to wait for reshape to
1349 * pass
1351 raid10_log(conf->mddev, "wait reshape");
1352 allow_barrier(conf);
1353 wait_event(conf->wait_barrier,
1354 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1355 conf->reshape_progress >= bio->bi_iter.bi_sector +
1356 sectors);
1357 wait_barrier(conf);
1360 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1361 (mddev->reshape_backwards
1362 ? (bio->bi_iter.bi_sector < conf->reshape_safe &&
1363 bio->bi_iter.bi_sector + sectors > conf->reshape_progress)
1364 : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe &&
1365 bio->bi_iter.bi_sector < conf->reshape_progress))) {
1366 /* Need to update reshape_position in metadata */
1367 mddev->reshape_position = conf->reshape_progress;
1368 set_mask_bits(&mddev->sb_flags, 0,
1369 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
1370 md_wakeup_thread(mddev->thread);
1371 raid10_log(conf->mddev, "wait reshape metadata");
1372 wait_event(mddev->sb_wait,
1373 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
1375 conf->reshape_safe = mddev->reshape_position;
1378 if (conf->pending_count >= max_queued_requests) {
1379 md_wakeup_thread(mddev->thread);
1380 raid10_log(mddev, "wait queued");
1381 wait_event(conf->wait_barrier,
1382 conf->pending_count < max_queued_requests);
1384 /* first select target devices under rcu_lock and
1385 * inc refcount on their rdev. Record them by setting
1386 * bios[x] to bio
1387 * If there are known/acknowledged bad blocks on any device
1388 * on which we have seen a write error, we want to avoid
1389 * writing to those blocks. This potentially requires several
1390 * writes to write around the bad blocks. Each set of writes
1391 * gets its own r10_bio with a set of bios attached.
1394 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
1395 raid10_find_phys(conf, r10_bio);
1396 retry_write:
1397 blocked_rdev = NULL;
1398 rcu_read_lock();
1399 max_sectors = r10_bio->sectors;
1401 for (i = 0; i < conf->copies; i++) {
1402 int d = r10_bio->devs[i].devnum;
1403 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
1404 struct md_rdev *rrdev = rcu_dereference(
1405 conf->mirrors[d].replacement);
1406 if (rdev == rrdev)
1407 rrdev = NULL;
1408 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1409 atomic_inc(&rdev->nr_pending);
1410 blocked_rdev = rdev;
1411 break;
1413 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1414 atomic_inc(&rrdev->nr_pending);
1415 blocked_rdev = rrdev;
1416 break;
1418 if (rdev && (test_bit(Faulty, &rdev->flags)))
1419 rdev = NULL;
1420 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
1421 rrdev = NULL;
1423 r10_bio->devs[i].bio = NULL;
1424 r10_bio->devs[i].repl_bio = NULL;
1426 if (!rdev && !rrdev) {
1427 set_bit(R10BIO_Degraded, &r10_bio->state);
1428 continue;
1430 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
1431 sector_t first_bad;
1432 sector_t dev_sector = r10_bio->devs[i].addr;
1433 int bad_sectors;
1434 int is_bad;
1436 is_bad = is_badblock(rdev, dev_sector, max_sectors,
1437 &first_bad, &bad_sectors);
1438 if (is_bad < 0) {
1439 /* Mustn't write here until the bad block
1440 * is acknowledged
1442 atomic_inc(&rdev->nr_pending);
1443 set_bit(BlockedBadBlocks, &rdev->flags);
1444 blocked_rdev = rdev;
1445 break;
1447 if (is_bad && first_bad <= dev_sector) {
1448 /* Cannot write here at all */
1449 bad_sectors -= (dev_sector - first_bad);
1450 if (bad_sectors < max_sectors)
1451 /* Mustn't write more than bad_sectors
1452 * to other devices yet
1454 max_sectors = bad_sectors;
1455 /* We don't set R10BIO_Degraded as that
1456 * only applies if the disk is missing,
1457 * so it might be re-added, and we want to
1458 * know to recover this chunk.
1459 * In this case the device is here, and the
1460 * fact that this chunk is not in-sync is
1461 * recorded in the bad block log.
1463 continue;
1465 if (is_bad) {
1466 int good_sectors = first_bad - dev_sector;
1467 if (good_sectors < max_sectors)
1468 max_sectors = good_sectors;
1471 if (rdev) {
1472 r10_bio->devs[i].bio = bio;
1473 atomic_inc(&rdev->nr_pending);
1475 if (rrdev) {
1476 r10_bio->devs[i].repl_bio = bio;
1477 atomic_inc(&rrdev->nr_pending);
1480 rcu_read_unlock();
1482 if (unlikely(blocked_rdev)) {
1483 /* Have to wait for this device to get unblocked, then retry */
1484 int j;
1485 int d;
1487 for (j = 0; j < i; j++) {
1488 if (r10_bio->devs[j].bio) {
1489 d = r10_bio->devs[j].devnum;
1490 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1492 if (r10_bio->devs[j].repl_bio) {
1493 struct md_rdev *rdev;
1494 d = r10_bio->devs[j].devnum;
1495 rdev = conf->mirrors[d].replacement;
1496 if (!rdev) {
1497 /* Race with remove_disk */
1498 smp_mb();
1499 rdev = conf->mirrors[d].rdev;
1501 rdev_dec_pending(rdev, mddev);
1504 allow_barrier(conf);
1505 raid10_log(conf->mddev, "wait rdev %d blocked", blocked_rdev->raid_disk);
1506 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1507 wait_barrier(conf);
1508 goto retry_write;
1511 if (max_sectors < r10_bio->sectors)
1512 r10_bio->sectors = max_sectors;
1514 if (r10_bio->sectors < bio_sectors(bio)) {
1515 struct bio *split = bio_split(bio, r10_bio->sectors,
1516 GFP_NOIO, &conf->bio_split);
1517 bio_chain(split, bio);
1518 allow_barrier(conf);
1519 generic_make_request(bio);
1520 wait_barrier(conf);
1521 bio = split;
1522 r10_bio->master_bio = bio;
1525 atomic_set(&r10_bio->remaining, 1);
1526 md_bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
1528 for (i = 0; i < conf->copies; i++) {
1529 if (r10_bio->devs[i].bio)
1530 raid10_write_one_disk(mddev, r10_bio, bio, false, i);
1531 if (r10_bio->devs[i].repl_bio)
1532 raid10_write_one_disk(mddev, r10_bio, bio, true, i);
1534 one_write_done(r10_bio);
1537 static void __make_request(struct mddev *mddev, struct bio *bio, int sectors)
1539 struct r10conf *conf = mddev->private;
1540 struct r10bio *r10_bio;
1542 r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO);
1544 r10_bio->master_bio = bio;
1545 r10_bio->sectors = sectors;
1547 r10_bio->mddev = mddev;
1548 r10_bio->sector = bio->bi_iter.bi_sector;
1549 r10_bio->state = 0;
1550 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) * conf->copies);
1552 if (bio_data_dir(bio) == READ)
1553 raid10_read_request(mddev, bio, r10_bio);
1554 else
1555 raid10_write_request(mddev, bio, r10_bio);
1558 static bool raid10_make_request(struct mddev *mddev, struct bio *bio)
1560 struct r10conf *conf = mddev->private;
1561 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
1562 int chunk_sects = chunk_mask + 1;
1563 int sectors = bio_sectors(bio);
1565 if (unlikely(bio->bi_opf & REQ_PREFLUSH)
1566 && md_flush_request(mddev, bio))
1567 return true;
1569 if (!md_write_start(mddev, bio))
1570 return false;
1573 * If this request crosses a chunk boundary, we need to split
1574 * it.
1576 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
1577 sectors > chunk_sects
1578 && (conf->geo.near_copies < conf->geo.raid_disks
1579 || conf->prev.near_copies <
1580 conf->prev.raid_disks)))
1581 sectors = chunk_sects -
1582 (bio->bi_iter.bi_sector &
1583 (chunk_sects - 1));
1584 __make_request(mddev, bio, sectors);
1586 /* In case raid10d snuck in to freeze_array */
1587 wake_up(&conf->wait_barrier);
1588 return true;
1591 static void raid10_status(struct seq_file *seq, struct mddev *mddev)
1593 struct r10conf *conf = mddev->private;
1594 int i;
1596 if (conf->geo.near_copies < conf->geo.raid_disks)
1597 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
1598 if (conf->geo.near_copies > 1)
1599 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1600 if (conf->geo.far_copies > 1) {
1601 if (conf->geo.far_offset)
1602 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
1603 else
1604 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
1605 if (conf->geo.far_set_size != conf->geo.raid_disks)
1606 seq_printf(seq, " %d devices per set", conf->geo.far_set_size);
1608 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1609 conf->geo.raid_disks - mddev->degraded);
1610 rcu_read_lock();
1611 for (i = 0; i < conf->geo.raid_disks; i++) {
1612 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1613 seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1615 rcu_read_unlock();
1616 seq_printf(seq, "]");
1619 /* check if there are enough drives for
1620 * every block to appear on atleast one.
1621 * Don't consider the device numbered 'ignore'
1622 * as we might be about to remove it.
1624 static int _enough(struct r10conf *conf, int previous, int ignore)
1626 int first = 0;
1627 int has_enough = 0;
1628 int disks, ncopies;
1629 if (previous) {
1630 disks = conf->prev.raid_disks;
1631 ncopies = conf->prev.near_copies;
1632 } else {
1633 disks = conf->geo.raid_disks;
1634 ncopies = conf->geo.near_copies;
1637 rcu_read_lock();
1638 do {
1639 int n = conf->copies;
1640 int cnt = 0;
1641 int this = first;
1642 while (n--) {
1643 struct md_rdev *rdev;
1644 if (this != ignore &&
1645 (rdev = rcu_dereference(conf->mirrors[this].rdev)) &&
1646 test_bit(In_sync, &rdev->flags))
1647 cnt++;
1648 this = (this+1) % disks;
1650 if (cnt == 0)
1651 goto out;
1652 first = (first + ncopies) % disks;
1653 } while (first != 0);
1654 has_enough = 1;
1655 out:
1656 rcu_read_unlock();
1657 return has_enough;
1660 static int enough(struct r10conf *conf, int ignore)
1662 /* when calling 'enough', both 'prev' and 'geo' must
1663 * be stable.
1664 * This is ensured if ->reconfig_mutex or ->device_lock
1665 * is held.
1667 return _enough(conf, 0, ignore) &&
1668 _enough(conf, 1, ignore);
1671 static void raid10_error(struct mddev *mddev, struct md_rdev *rdev)
1673 char b[BDEVNAME_SIZE];
1674 struct r10conf *conf = mddev->private;
1675 unsigned long flags;
1678 * If it is not operational, then we have already marked it as dead
1679 * else if it is the last working disks, ignore the error, let the
1680 * next level up know.
1681 * else mark the drive as failed
1683 spin_lock_irqsave(&conf->device_lock, flags);
1684 if (test_bit(In_sync, &rdev->flags)
1685 && !enough(conf, rdev->raid_disk)) {
1687 * Don't fail the drive, just return an IO error.
1689 spin_unlock_irqrestore(&conf->device_lock, flags);
1690 return;
1692 if (test_and_clear_bit(In_sync, &rdev->flags))
1693 mddev->degraded++;
1695 * If recovery is running, make sure it aborts.
1697 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1698 set_bit(Blocked, &rdev->flags);
1699 set_bit(Faulty, &rdev->flags);
1700 set_mask_bits(&mddev->sb_flags, 0,
1701 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
1702 spin_unlock_irqrestore(&conf->device_lock, flags);
1703 pr_crit("md/raid10:%s: Disk failure on %s, disabling device.\n"
1704 "md/raid10:%s: Operation continuing on %d devices.\n",
1705 mdname(mddev), bdevname(rdev->bdev, b),
1706 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
1709 static void print_conf(struct r10conf *conf)
1711 int i;
1712 struct md_rdev *rdev;
1714 pr_debug("RAID10 conf printout:\n");
1715 if (!conf) {
1716 pr_debug("(!conf)\n");
1717 return;
1719 pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
1720 conf->geo.raid_disks);
1722 /* This is only called with ->reconfix_mutex held, so
1723 * rcu protection of rdev is not needed */
1724 for (i = 0; i < conf->geo.raid_disks; i++) {
1725 char b[BDEVNAME_SIZE];
1726 rdev = conf->mirrors[i].rdev;
1727 if (rdev)
1728 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1729 i, !test_bit(In_sync, &rdev->flags),
1730 !test_bit(Faulty, &rdev->flags),
1731 bdevname(rdev->bdev,b));
1735 static void close_sync(struct r10conf *conf)
1737 wait_barrier(conf);
1738 allow_barrier(conf);
1740 mempool_exit(&conf->r10buf_pool);
1743 static int raid10_spare_active(struct mddev *mddev)
1745 int i;
1746 struct r10conf *conf = mddev->private;
1747 struct raid10_info *tmp;
1748 int count = 0;
1749 unsigned long flags;
1752 * Find all non-in_sync disks within the RAID10 configuration
1753 * and mark them in_sync
1755 for (i = 0; i < conf->geo.raid_disks; i++) {
1756 tmp = conf->mirrors + i;
1757 if (tmp->replacement
1758 && tmp->replacement->recovery_offset == MaxSector
1759 && !test_bit(Faulty, &tmp->replacement->flags)
1760 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1761 /* Replacement has just become active */
1762 if (!tmp->rdev
1763 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1764 count++;
1765 if (tmp->rdev) {
1766 /* Replaced device not technically faulty,
1767 * but we need to be sure it gets removed
1768 * and never re-added.
1770 set_bit(Faulty, &tmp->rdev->flags);
1771 sysfs_notify_dirent_safe(
1772 tmp->rdev->sysfs_state);
1774 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1775 } else if (tmp->rdev
1776 && tmp->rdev->recovery_offset == MaxSector
1777 && !test_bit(Faulty, &tmp->rdev->flags)
1778 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1779 count++;
1780 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
1783 spin_lock_irqsave(&conf->device_lock, flags);
1784 mddev->degraded -= count;
1785 spin_unlock_irqrestore(&conf->device_lock, flags);
1787 print_conf(conf);
1788 return count;
1791 static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1793 struct r10conf *conf = mddev->private;
1794 int err = -EEXIST;
1795 int mirror;
1796 int first = 0;
1797 int last = conf->geo.raid_disks - 1;
1799 if (mddev->recovery_cp < MaxSector)
1800 /* only hot-add to in-sync arrays, as recovery is
1801 * very different from resync
1803 return -EBUSY;
1804 if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
1805 return -EINVAL;
1807 if (md_integrity_add_rdev(rdev, mddev))
1808 return -ENXIO;
1810 if (rdev->raid_disk >= 0)
1811 first = last = rdev->raid_disk;
1813 if (rdev->saved_raid_disk >= first &&
1814 rdev->saved_raid_disk < conf->geo.raid_disks &&
1815 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1816 mirror = rdev->saved_raid_disk;
1817 else
1818 mirror = first;
1819 for ( ; mirror <= last ; mirror++) {
1820 struct raid10_info *p = &conf->mirrors[mirror];
1821 if (p->recovery_disabled == mddev->recovery_disabled)
1822 continue;
1823 if (p->rdev) {
1824 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1825 p->replacement != NULL)
1826 continue;
1827 clear_bit(In_sync, &rdev->flags);
1828 set_bit(Replacement, &rdev->flags);
1829 rdev->raid_disk = mirror;
1830 err = 0;
1831 if (mddev->gendisk)
1832 disk_stack_limits(mddev->gendisk, rdev->bdev,
1833 rdev->data_offset << 9);
1834 conf->fullsync = 1;
1835 rcu_assign_pointer(p->replacement, rdev);
1836 break;
1839 if (mddev->gendisk)
1840 disk_stack_limits(mddev->gendisk, rdev->bdev,
1841 rdev->data_offset << 9);
1843 p->head_position = 0;
1844 p->recovery_disabled = mddev->recovery_disabled - 1;
1845 rdev->raid_disk = mirror;
1846 err = 0;
1847 if (rdev->saved_raid_disk != mirror)
1848 conf->fullsync = 1;
1849 rcu_assign_pointer(p->rdev, rdev);
1850 break;
1852 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
1853 blk_queue_flag_set(QUEUE_FLAG_DISCARD, mddev->queue);
1855 print_conf(conf);
1856 return err;
1859 static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1861 struct r10conf *conf = mddev->private;
1862 int err = 0;
1863 int number = rdev->raid_disk;
1864 struct md_rdev **rdevp;
1865 struct raid10_info *p = conf->mirrors + number;
1867 print_conf(conf);
1868 if (rdev == p->rdev)
1869 rdevp = &p->rdev;
1870 else if (rdev == p->replacement)
1871 rdevp = &p->replacement;
1872 else
1873 return 0;
1875 if (test_bit(In_sync, &rdev->flags) ||
1876 atomic_read(&rdev->nr_pending)) {
1877 err = -EBUSY;
1878 goto abort;
1880 /* Only remove non-faulty devices if recovery
1881 * is not possible.
1883 if (!test_bit(Faulty, &rdev->flags) &&
1884 mddev->recovery_disabled != p->recovery_disabled &&
1885 (!p->replacement || p->replacement == rdev) &&
1886 number < conf->geo.raid_disks &&
1887 enough(conf, -1)) {
1888 err = -EBUSY;
1889 goto abort;
1891 *rdevp = NULL;
1892 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
1893 synchronize_rcu();
1894 if (atomic_read(&rdev->nr_pending)) {
1895 /* lost the race, try later */
1896 err = -EBUSY;
1897 *rdevp = rdev;
1898 goto abort;
1901 if (p->replacement) {
1902 /* We must have just cleared 'rdev' */
1903 p->rdev = p->replacement;
1904 clear_bit(Replacement, &p->replacement->flags);
1905 smp_mb(); /* Make sure other CPUs may see both as identical
1906 * but will never see neither -- if they are careful.
1908 p->replacement = NULL;
1911 clear_bit(WantReplacement, &rdev->flags);
1912 err = md_integrity_register(mddev);
1914 abort:
1916 print_conf(conf);
1917 return err;
1920 static void __end_sync_read(struct r10bio *r10_bio, struct bio *bio, int d)
1922 struct r10conf *conf = r10_bio->mddev->private;
1924 if (!bio->bi_status)
1925 set_bit(R10BIO_Uptodate, &r10_bio->state);
1926 else
1927 /* The write handler will notice the lack of
1928 * R10BIO_Uptodate and record any errors etc
1930 atomic_add(r10_bio->sectors,
1931 &conf->mirrors[d].rdev->corrected_errors);
1933 /* for reconstruct, we always reschedule after a read.
1934 * for resync, only after all reads
1936 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1937 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1938 atomic_dec_and_test(&r10_bio->remaining)) {
1939 /* we have read all the blocks,
1940 * do the comparison in process context in raid10d
1942 reschedule_retry(r10_bio);
1946 static void end_sync_read(struct bio *bio)
1948 struct r10bio *r10_bio = get_resync_r10bio(bio);
1949 struct r10conf *conf = r10_bio->mddev->private;
1950 int d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
1952 __end_sync_read(r10_bio, bio, d);
1955 static void end_reshape_read(struct bio *bio)
1957 /* reshape read bio isn't allocated from r10buf_pool */
1958 struct r10bio *r10_bio = bio->bi_private;
1960 __end_sync_read(r10_bio, bio, r10_bio->read_slot);
1963 static void end_sync_request(struct r10bio *r10_bio)
1965 struct mddev *mddev = r10_bio->mddev;
1967 while (atomic_dec_and_test(&r10_bio->remaining)) {
1968 if (r10_bio->master_bio == NULL) {
1969 /* the primary of several recovery bios */
1970 sector_t s = r10_bio->sectors;
1971 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1972 test_bit(R10BIO_WriteError, &r10_bio->state))
1973 reschedule_retry(r10_bio);
1974 else
1975 put_buf(r10_bio);
1976 md_done_sync(mddev, s, 1);
1977 break;
1978 } else {
1979 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
1980 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1981 test_bit(R10BIO_WriteError, &r10_bio->state))
1982 reschedule_retry(r10_bio);
1983 else
1984 put_buf(r10_bio);
1985 r10_bio = r10_bio2;
1990 static void end_sync_write(struct bio *bio)
1992 struct r10bio *r10_bio = get_resync_r10bio(bio);
1993 struct mddev *mddev = r10_bio->mddev;
1994 struct r10conf *conf = mddev->private;
1995 int d;
1996 sector_t first_bad;
1997 int bad_sectors;
1998 int slot;
1999 int repl;
2000 struct md_rdev *rdev = NULL;
2002 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
2003 if (repl)
2004 rdev = conf->mirrors[d].replacement;
2005 else
2006 rdev = conf->mirrors[d].rdev;
2008 if (bio->bi_status) {
2009 if (repl)
2010 md_error(mddev, rdev);
2011 else {
2012 set_bit(WriteErrorSeen, &rdev->flags);
2013 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2014 set_bit(MD_RECOVERY_NEEDED,
2015 &rdev->mddev->recovery);
2016 set_bit(R10BIO_WriteError, &r10_bio->state);
2018 } else if (is_badblock(rdev,
2019 r10_bio->devs[slot].addr,
2020 r10_bio->sectors,
2021 &first_bad, &bad_sectors))
2022 set_bit(R10BIO_MadeGood, &r10_bio->state);
2024 rdev_dec_pending(rdev, mddev);
2026 end_sync_request(r10_bio);
2030 * Note: sync and recover and handled very differently for raid10
2031 * This code is for resync.
2032 * For resync, we read through virtual addresses and read all blocks.
2033 * If there is any error, we schedule a write. The lowest numbered
2034 * drive is authoritative.
2035 * However requests come for physical address, so we need to map.
2036 * For every physical address there are raid_disks/copies virtual addresses,
2037 * which is always are least one, but is not necessarly an integer.
2038 * This means that a physical address can span multiple chunks, so we may
2039 * have to submit multiple io requests for a single sync request.
2042 * We check if all blocks are in-sync and only write to blocks that
2043 * aren't in sync
2045 static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
2047 struct r10conf *conf = mddev->private;
2048 int i, first;
2049 struct bio *tbio, *fbio;
2050 int vcnt;
2051 struct page **tpages, **fpages;
2053 atomic_set(&r10_bio->remaining, 1);
2055 /* find the first device with a block */
2056 for (i=0; i<conf->copies; i++)
2057 if (!r10_bio->devs[i].bio->bi_status)
2058 break;
2060 if (i == conf->copies)
2061 goto done;
2063 first = i;
2064 fbio = r10_bio->devs[i].bio;
2065 fbio->bi_iter.bi_size = r10_bio->sectors << 9;
2066 fbio->bi_iter.bi_idx = 0;
2067 fpages = get_resync_pages(fbio)->pages;
2069 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
2070 /* now find blocks with errors */
2071 for (i=0 ; i < conf->copies ; i++) {
2072 int j, d;
2073 struct md_rdev *rdev;
2074 struct resync_pages *rp;
2076 tbio = r10_bio->devs[i].bio;
2078 if (tbio->bi_end_io != end_sync_read)
2079 continue;
2080 if (i == first)
2081 continue;
2083 tpages = get_resync_pages(tbio)->pages;
2084 d = r10_bio->devs[i].devnum;
2085 rdev = conf->mirrors[d].rdev;
2086 if (!r10_bio->devs[i].bio->bi_status) {
2087 /* We know that the bi_io_vec layout is the same for
2088 * both 'first' and 'i', so we just compare them.
2089 * All vec entries are PAGE_SIZE;
2091 int sectors = r10_bio->sectors;
2092 for (j = 0; j < vcnt; j++) {
2093 int len = PAGE_SIZE;
2094 if (sectors < (len / 512))
2095 len = sectors * 512;
2096 if (memcmp(page_address(fpages[j]),
2097 page_address(tpages[j]),
2098 len))
2099 break;
2100 sectors -= len/512;
2102 if (j == vcnt)
2103 continue;
2104 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
2105 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2106 /* Don't fix anything. */
2107 continue;
2108 } else if (test_bit(FailFast, &rdev->flags)) {
2109 /* Just give up on this device */
2110 md_error(rdev->mddev, rdev);
2111 continue;
2113 /* Ok, we need to write this bio, either to correct an
2114 * inconsistency or to correct an unreadable block.
2115 * First we need to fixup bv_offset, bv_len and
2116 * bi_vecs, as the read request might have corrupted these
2118 rp = get_resync_pages(tbio);
2119 bio_reset(tbio);
2121 md_bio_reset_resync_pages(tbio, rp, fbio->bi_iter.bi_size);
2123 rp->raid_bio = r10_bio;
2124 tbio->bi_private = rp;
2125 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr;
2126 tbio->bi_end_io = end_sync_write;
2127 bio_set_op_attrs(tbio, REQ_OP_WRITE, 0);
2129 bio_copy_data(tbio, fbio);
2131 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2132 atomic_inc(&r10_bio->remaining);
2133 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
2135 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
2136 tbio->bi_opf |= MD_FAILFAST;
2137 tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset;
2138 bio_set_dev(tbio, conf->mirrors[d].rdev->bdev);
2139 generic_make_request(tbio);
2142 /* Now write out to any replacement devices
2143 * that are active
2145 for (i = 0; i < conf->copies; i++) {
2146 int d;
2148 tbio = r10_bio->devs[i].repl_bio;
2149 if (!tbio || !tbio->bi_end_io)
2150 continue;
2151 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2152 && r10_bio->devs[i].bio != fbio)
2153 bio_copy_data(tbio, fbio);
2154 d = r10_bio->devs[i].devnum;
2155 atomic_inc(&r10_bio->remaining);
2156 md_sync_acct(conf->mirrors[d].replacement->bdev,
2157 bio_sectors(tbio));
2158 generic_make_request(tbio);
2161 done:
2162 if (atomic_dec_and_test(&r10_bio->remaining)) {
2163 md_done_sync(mddev, r10_bio->sectors, 1);
2164 put_buf(r10_bio);
2169 * Now for the recovery code.
2170 * Recovery happens across physical sectors.
2171 * We recover all non-is_sync drives by finding the virtual address of
2172 * each, and then choose a working drive that also has that virt address.
2173 * There is a separate r10_bio for each non-in_sync drive.
2174 * Only the first two slots are in use. The first for reading,
2175 * The second for writing.
2178 static void fix_recovery_read_error(struct r10bio *r10_bio)
2180 /* We got a read error during recovery.
2181 * We repeat the read in smaller page-sized sections.
2182 * If a read succeeds, write it to the new device or record
2183 * a bad block if we cannot.
2184 * If a read fails, record a bad block on both old and
2185 * new devices.
2187 struct mddev *mddev = r10_bio->mddev;
2188 struct r10conf *conf = mddev->private;
2189 struct bio *bio = r10_bio->devs[0].bio;
2190 sector_t sect = 0;
2191 int sectors = r10_bio->sectors;
2192 int idx = 0;
2193 int dr = r10_bio->devs[0].devnum;
2194 int dw = r10_bio->devs[1].devnum;
2195 struct page **pages = get_resync_pages(bio)->pages;
2197 while (sectors) {
2198 int s = sectors;
2199 struct md_rdev *rdev;
2200 sector_t addr;
2201 int ok;
2203 if (s > (PAGE_SIZE>>9))
2204 s = PAGE_SIZE >> 9;
2206 rdev = conf->mirrors[dr].rdev;
2207 addr = r10_bio->devs[0].addr + sect,
2208 ok = sync_page_io(rdev,
2209 addr,
2210 s << 9,
2211 pages[idx],
2212 REQ_OP_READ, 0, false);
2213 if (ok) {
2214 rdev = conf->mirrors[dw].rdev;
2215 addr = r10_bio->devs[1].addr + sect;
2216 ok = sync_page_io(rdev,
2217 addr,
2218 s << 9,
2219 pages[idx],
2220 REQ_OP_WRITE, 0, false);
2221 if (!ok) {
2222 set_bit(WriteErrorSeen, &rdev->flags);
2223 if (!test_and_set_bit(WantReplacement,
2224 &rdev->flags))
2225 set_bit(MD_RECOVERY_NEEDED,
2226 &rdev->mddev->recovery);
2229 if (!ok) {
2230 /* We don't worry if we cannot set a bad block -
2231 * it really is bad so there is no loss in not
2232 * recording it yet
2234 rdev_set_badblocks(rdev, addr, s, 0);
2236 if (rdev != conf->mirrors[dw].rdev) {
2237 /* need bad block on destination too */
2238 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
2239 addr = r10_bio->devs[1].addr + sect;
2240 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2241 if (!ok) {
2242 /* just abort the recovery */
2243 pr_notice("md/raid10:%s: recovery aborted due to read error\n",
2244 mdname(mddev));
2246 conf->mirrors[dw].recovery_disabled
2247 = mddev->recovery_disabled;
2248 set_bit(MD_RECOVERY_INTR,
2249 &mddev->recovery);
2250 break;
2255 sectors -= s;
2256 sect += s;
2257 idx++;
2261 static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
2263 struct r10conf *conf = mddev->private;
2264 int d;
2265 struct bio *wbio, *wbio2;
2267 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2268 fix_recovery_read_error(r10_bio);
2269 end_sync_request(r10_bio);
2270 return;
2274 * share the pages with the first bio
2275 * and submit the write request
2277 d = r10_bio->devs[1].devnum;
2278 wbio = r10_bio->devs[1].bio;
2279 wbio2 = r10_bio->devs[1].repl_bio;
2280 /* Need to test wbio2->bi_end_io before we call
2281 * generic_make_request as if the former is NULL,
2282 * the latter is free to free wbio2.
2284 if (wbio2 && !wbio2->bi_end_io)
2285 wbio2 = NULL;
2286 if (wbio->bi_end_io) {
2287 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2288 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
2289 generic_make_request(wbio);
2291 if (wbio2) {
2292 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2293 md_sync_acct(conf->mirrors[d].replacement->bdev,
2294 bio_sectors(wbio2));
2295 generic_make_request(wbio2);
2300 * Used by fix_read_error() to decay the per rdev read_errors.
2301 * We halve the read error count for every hour that has elapsed
2302 * since the last recorded read error.
2305 static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
2307 long cur_time_mon;
2308 unsigned long hours_since_last;
2309 unsigned int read_errors = atomic_read(&rdev->read_errors);
2311 cur_time_mon = ktime_get_seconds();
2313 if (rdev->last_read_error == 0) {
2314 /* first time we've seen a read error */
2315 rdev->last_read_error = cur_time_mon;
2316 return;
2319 hours_since_last = (long)(cur_time_mon -
2320 rdev->last_read_error) / 3600;
2322 rdev->last_read_error = cur_time_mon;
2325 * if hours_since_last is > the number of bits in read_errors
2326 * just set read errors to 0. We do this to avoid
2327 * overflowing the shift of read_errors by hours_since_last.
2329 if (hours_since_last >= 8 * sizeof(read_errors))
2330 atomic_set(&rdev->read_errors, 0);
2331 else
2332 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2335 static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
2336 int sectors, struct page *page, int rw)
2338 sector_t first_bad;
2339 int bad_sectors;
2341 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2342 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2343 return -1;
2344 if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
2345 /* success */
2346 return 1;
2347 if (rw == WRITE) {
2348 set_bit(WriteErrorSeen, &rdev->flags);
2349 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2350 set_bit(MD_RECOVERY_NEEDED,
2351 &rdev->mddev->recovery);
2353 /* need to record an error - either for the block or the device */
2354 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2355 md_error(rdev->mddev, rdev);
2356 return 0;
2360 * This is a kernel thread which:
2362 * 1. Retries failed read operations on working mirrors.
2363 * 2. Updates the raid superblock when problems encounter.
2364 * 3. Performs writes following reads for array synchronising.
2367 static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
2369 int sect = 0; /* Offset from r10_bio->sector */
2370 int sectors = r10_bio->sectors;
2371 struct md_rdev *rdev;
2372 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
2373 int d = r10_bio->devs[r10_bio->read_slot].devnum;
2375 /* still own a reference to this rdev, so it cannot
2376 * have been cleared recently.
2378 rdev = conf->mirrors[d].rdev;
2380 if (test_bit(Faulty, &rdev->flags))
2381 /* drive has already been failed, just ignore any
2382 more fix_read_error() attempts */
2383 return;
2385 check_decay_read_errors(mddev, rdev);
2386 atomic_inc(&rdev->read_errors);
2387 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2388 char b[BDEVNAME_SIZE];
2389 bdevname(rdev->bdev, b);
2391 pr_notice("md/raid10:%s: %s: Raid device exceeded read_error threshold [cur %d:max %d]\n",
2392 mdname(mddev), b,
2393 atomic_read(&rdev->read_errors), max_read_errors);
2394 pr_notice("md/raid10:%s: %s: Failing raid device\n",
2395 mdname(mddev), b);
2396 md_error(mddev, rdev);
2397 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
2398 return;
2401 while(sectors) {
2402 int s = sectors;
2403 int sl = r10_bio->read_slot;
2404 int success = 0;
2405 int start;
2407 if (s > (PAGE_SIZE>>9))
2408 s = PAGE_SIZE >> 9;
2410 rcu_read_lock();
2411 do {
2412 sector_t first_bad;
2413 int bad_sectors;
2415 d = r10_bio->devs[sl].devnum;
2416 rdev = rcu_dereference(conf->mirrors[d].rdev);
2417 if (rdev &&
2418 test_bit(In_sync, &rdev->flags) &&
2419 !test_bit(Faulty, &rdev->flags) &&
2420 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2421 &first_bad, &bad_sectors) == 0) {
2422 atomic_inc(&rdev->nr_pending);
2423 rcu_read_unlock();
2424 success = sync_page_io(rdev,
2425 r10_bio->devs[sl].addr +
2426 sect,
2427 s<<9,
2428 conf->tmppage,
2429 REQ_OP_READ, 0, false);
2430 rdev_dec_pending(rdev, mddev);
2431 rcu_read_lock();
2432 if (success)
2433 break;
2435 sl++;
2436 if (sl == conf->copies)
2437 sl = 0;
2438 } while (!success && sl != r10_bio->read_slot);
2439 rcu_read_unlock();
2441 if (!success) {
2442 /* Cannot read from anywhere, just mark the block
2443 * as bad on the first device to discourage future
2444 * reads.
2446 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
2447 rdev = conf->mirrors[dn].rdev;
2449 if (!rdev_set_badblocks(
2450 rdev,
2451 r10_bio->devs[r10_bio->read_slot].addr
2452 + sect,
2453 s, 0)) {
2454 md_error(mddev, rdev);
2455 r10_bio->devs[r10_bio->read_slot].bio
2456 = IO_BLOCKED;
2458 break;
2461 start = sl;
2462 /* write it back and re-read */
2463 rcu_read_lock();
2464 while (sl != r10_bio->read_slot) {
2465 char b[BDEVNAME_SIZE];
2467 if (sl==0)
2468 sl = conf->copies;
2469 sl--;
2470 d = r10_bio->devs[sl].devnum;
2471 rdev = rcu_dereference(conf->mirrors[d].rdev);
2472 if (!rdev ||
2473 test_bit(Faulty, &rdev->flags) ||
2474 !test_bit(In_sync, &rdev->flags))
2475 continue;
2477 atomic_inc(&rdev->nr_pending);
2478 rcu_read_unlock();
2479 if (r10_sync_page_io(rdev,
2480 r10_bio->devs[sl].addr +
2481 sect,
2482 s, conf->tmppage, WRITE)
2483 == 0) {
2484 /* Well, this device is dead */
2485 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %s)\n",
2486 mdname(mddev), s,
2487 (unsigned long long)(
2488 sect +
2489 choose_data_offset(r10_bio,
2490 rdev)),
2491 bdevname(rdev->bdev, b));
2492 pr_notice("md/raid10:%s: %s: failing drive\n",
2493 mdname(mddev),
2494 bdevname(rdev->bdev, b));
2496 rdev_dec_pending(rdev, mddev);
2497 rcu_read_lock();
2499 sl = start;
2500 while (sl != r10_bio->read_slot) {
2501 char b[BDEVNAME_SIZE];
2503 if (sl==0)
2504 sl = conf->copies;
2505 sl--;
2506 d = r10_bio->devs[sl].devnum;
2507 rdev = rcu_dereference(conf->mirrors[d].rdev);
2508 if (!rdev ||
2509 test_bit(Faulty, &rdev->flags) ||
2510 !test_bit(In_sync, &rdev->flags))
2511 continue;
2513 atomic_inc(&rdev->nr_pending);
2514 rcu_read_unlock();
2515 switch (r10_sync_page_io(rdev,
2516 r10_bio->devs[sl].addr +
2517 sect,
2518 s, conf->tmppage,
2519 READ)) {
2520 case 0:
2521 /* Well, this device is dead */
2522 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %s)\n",
2523 mdname(mddev), s,
2524 (unsigned long long)(
2525 sect +
2526 choose_data_offset(r10_bio, rdev)),
2527 bdevname(rdev->bdev, b));
2528 pr_notice("md/raid10:%s: %s: failing drive\n",
2529 mdname(mddev),
2530 bdevname(rdev->bdev, b));
2531 break;
2532 case 1:
2533 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %s)\n",
2534 mdname(mddev), s,
2535 (unsigned long long)(
2536 sect +
2537 choose_data_offset(r10_bio, rdev)),
2538 bdevname(rdev->bdev, b));
2539 atomic_add(s, &rdev->corrected_errors);
2542 rdev_dec_pending(rdev, mddev);
2543 rcu_read_lock();
2545 rcu_read_unlock();
2547 sectors -= s;
2548 sect += s;
2552 static int narrow_write_error(struct r10bio *r10_bio, int i)
2554 struct bio *bio = r10_bio->master_bio;
2555 struct mddev *mddev = r10_bio->mddev;
2556 struct r10conf *conf = mddev->private;
2557 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
2558 /* bio has the data to be written to slot 'i' where
2559 * we just recently had a write error.
2560 * We repeatedly clone the bio and trim down to one block,
2561 * then try the write. Where the write fails we record
2562 * a bad block.
2563 * It is conceivable that the bio doesn't exactly align with
2564 * blocks. We must handle this.
2566 * We currently own a reference to the rdev.
2569 int block_sectors;
2570 sector_t sector;
2571 int sectors;
2572 int sect_to_write = r10_bio->sectors;
2573 int ok = 1;
2575 if (rdev->badblocks.shift < 0)
2576 return 0;
2578 block_sectors = roundup(1 << rdev->badblocks.shift,
2579 bdev_logical_block_size(rdev->bdev) >> 9);
2580 sector = r10_bio->sector;
2581 sectors = ((r10_bio->sector + block_sectors)
2582 & ~(sector_t)(block_sectors - 1))
2583 - sector;
2585 while (sect_to_write) {
2586 struct bio *wbio;
2587 sector_t wsector;
2588 if (sectors > sect_to_write)
2589 sectors = sect_to_write;
2590 /* Write at 'sector' for 'sectors' */
2591 wbio = bio_clone_fast(bio, GFP_NOIO, &mddev->bio_set);
2592 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors);
2593 wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector);
2594 wbio->bi_iter.bi_sector = wsector +
2595 choose_data_offset(r10_bio, rdev);
2596 bio_set_dev(wbio, rdev->bdev);
2597 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
2599 if (submit_bio_wait(wbio) < 0)
2600 /* Failure! */
2601 ok = rdev_set_badblocks(rdev, wsector,
2602 sectors, 0)
2603 && ok;
2605 bio_put(wbio);
2606 sect_to_write -= sectors;
2607 sector += sectors;
2608 sectors = block_sectors;
2610 return ok;
2613 static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
2615 int slot = r10_bio->read_slot;
2616 struct bio *bio;
2617 struct r10conf *conf = mddev->private;
2618 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
2620 /* we got a read error. Maybe the drive is bad. Maybe just
2621 * the block and we can fix it.
2622 * We freeze all other IO, and try reading the block from
2623 * other devices. When we find one, we re-write
2624 * and check it that fixes the read error.
2625 * This is all done synchronously while the array is
2626 * frozen.
2628 bio = r10_bio->devs[slot].bio;
2629 bio_put(bio);
2630 r10_bio->devs[slot].bio = NULL;
2632 if (mddev->ro)
2633 r10_bio->devs[slot].bio = IO_BLOCKED;
2634 else if (!test_bit(FailFast, &rdev->flags)) {
2635 freeze_array(conf, 1);
2636 fix_read_error(conf, mddev, r10_bio);
2637 unfreeze_array(conf);
2638 } else
2639 md_error(mddev, rdev);
2641 rdev_dec_pending(rdev, mddev);
2642 allow_barrier(conf);
2643 r10_bio->state = 0;
2644 raid10_read_request(mddev, r10_bio->master_bio, r10_bio);
2647 static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
2649 /* Some sort of write request has finished and it
2650 * succeeded in writing where we thought there was a
2651 * bad block. So forget the bad block.
2652 * Or possibly if failed and we need to record
2653 * a bad block.
2655 int m;
2656 struct md_rdev *rdev;
2658 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2659 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
2660 for (m = 0; m < conf->copies; m++) {
2661 int dev = r10_bio->devs[m].devnum;
2662 rdev = conf->mirrors[dev].rdev;
2663 if (r10_bio->devs[m].bio == NULL ||
2664 r10_bio->devs[m].bio->bi_end_io == NULL)
2665 continue;
2666 if (!r10_bio->devs[m].bio->bi_status) {
2667 rdev_clear_badblocks(
2668 rdev,
2669 r10_bio->devs[m].addr,
2670 r10_bio->sectors, 0);
2671 } else {
2672 if (!rdev_set_badblocks(
2673 rdev,
2674 r10_bio->devs[m].addr,
2675 r10_bio->sectors, 0))
2676 md_error(conf->mddev, rdev);
2678 rdev = conf->mirrors[dev].replacement;
2679 if (r10_bio->devs[m].repl_bio == NULL ||
2680 r10_bio->devs[m].repl_bio->bi_end_io == NULL)
2681 continue;
2683 if (!r10_bio->devs[m].repl_bio->bi_status) {
2684 rdev_clear_badblocks(
2685 rdev,
2686 r10_bio->devs[m].addr,
2687 r10_bio->sectors, 0);
2688 } else {
2689 if (!rdev_set_badblocks(
2690 rdev,
2691 r10_bio->devs[m].addr,
2692 r10_bio->sectors, 0))
2693 md_error(conf->mddev, rdev);
2696 put_buf(r10_bio);
2697 } else {
2698 bool fail = false;
2699 for (m = 0; m < conf->copies; m++) {
2700 int dev = r10_bio->devs[m].devnum;
2701 struct bio *bio = r10_bio->devs[m].bio;
2702 rdev = conf->mirrors[dev].rdev;
2703 if (bio == IO_MADE_GOOD) {
2704 rdev_clear_badblocks(
2705 rdev,
2706 r10_bio->devs[m].addr,
2707 r10_bio->sectors, 0);
2708 rdev_dec_pending(rdev, conf->mddev);
2709 } else if (bio != NULL && bio->bi_status) {
2710 fail = true;
2711 if (!narrow_write_error(r10_bio, m)) {
2712 md_error(conf->mddev, rdev);
2713 set_bit(R10BIO_Degraded,
2714 &r10_bio->state);
2716 rdev_dec_pending(rdev, conf->mddev);
2718 bio = r10_bio->devs[m].repl_bio;
2719 rdev = conf->mirrors[dev].replacement;
2720 if (rdev && bio == IO_MADE_GOOD) {
2721 rdev_clear_badblocks(
2722 rdev,
2723 r10_bio->devs[m].addr,
2724 r10_bio->sectors, 0);
2725 rdev_dec_pending(rdev, conf->mddev);
2728 if (fail) {
2729 spin_lock_irq(&conf->device_lock);
2730 list_add(&r10_bio->retry_list, &conf->bio_end_io_list);
2731 conf->nr_queued++;
2732 spin_unlock_irq(&conf->device_lock);
2734 * In case freeze_array() is waiting for condition
2735 * nr_pending == nr_queued + extra to be true.
2737 wake_up(&conf->wait_barrier);
2738 md_wakeup_thread(conf->mddev->thread);
2739 } else {
2740 if (test_bit(R10BIO_WriteError,
2741 &r10_bio->state))
2742 close_write(r10_bio);
2743 raid_end_bio_io(r10_bio);
2748 static void raid10d(struct md_thread *thread)
2750 struct mddev *mddev = thread->mddev;
2751 struct r10bio *r10_bio;
2752 unsigned long flags;
2753 struct r10conf *conf = mddev->private;
2754 struct list_head *head = &conf->retry_list;
2755 struct blk_plug plug;
2757 md_check_recovery(mddev);
2759 if (!list_empty_careful(&conf->bio_end_io_list) &&
2760 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
2761 LIST_HEAD(tmp);
2762 spin_lock_irqsave(&conf->device_lock, flags);
2763 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
2764 while (!list_empty(&conf->bio_end_io_list)) {
2765 list_move(conf->bio_end_io_list.prev, &tmp);
2766 conf->nr_queued--;
2769 spin_unlock_irqrestore(&conf->device_lock, flags);
2770 while (!list_empty(&tmp)) {
2771 r10_bio = list_first_entry(&tmp, struct r10bio,
2772 retry_list);
2773 list_del(&r10_bio->retry_list);
2774 if (mddev->degraded)
2775 set_bit(R10BIO_Degraded, &r10_bio->state);
2777 if (test_bit(R10BIO_WriteError,
2778 &r10_bio->state))
2779 close_write(r10_bio);
2780 raid_end_bio_io(r10_bio);
2784 blk_start_plug(&plug);
2785 for (;;) {
2787 flush_pending_writes(conf);
2789 spin_lock_irqsave(&conf->device_lock, flags);
2790 if (list_empty(head)) {
2791 spin_unlock_irqrestore(&conf->device_lock, flags);
2792 break;
2794 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
2795 list_del(head->prev);
2796 conf->nr_queued--;
2797 spin_unlock_irqrestore(&conf->device_lock, flags);
2799 mddev = r10_bio->mddev;
2800 conf = mddev->private;
2801 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2802 test_bit(R10BIO_WriteError, &r10_bio->state))
2803 handle_write_completed(conf, r10_bio);
2804 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
2805 reshape_request_write(mddev, r10_bio);
2806 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
2807 sync_request_write(mddev, r10_bio);
2808 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
2809 recovery_request_write(mddev, r10_bio);
2810 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
2811 handle_read_error(mddev, r10_bio);
2812 else
2813 WARN_ON_ONCE(1);
2815 cond_resched();
2816 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
2817 md_check_recovery(mddev);
2819 blk_finish_plug(&plug);
2822 static int init_resync(struct r10conf *conf)
2824 int ret, buffs, i;
2826 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
2827 BUG_ON(mempool_initialized(&conf->r10buf_pool));
2828 conf->have_replacement = 0;
2829 for (i = 0; i < conf->geo.raid_disks; i++)
2830 if (conf->mirrors[i].replacement)
2831 conf->have_replacement = 1;
2832 ret = mempool_init(&conf->r10buf_pool, buffs,
2833 r10buf_pool_alloc, r10buf_pool_free, conf);
2834 if (ret)
2835 return ret;
2836 conf->next_resync = 0;
2837 return 0;
2840 static struct r10bio *raid10_alloc_init_r10buf(struct r10conf *conf)
2842 struct r10bio *r10bio = mempool_alloc(&conf->r10buf_pool, GFP_NOIO);
2843 struct rsync_pages *rp;
2844 struct bio *bio;
2845 int nalloc;
2846 int i;
2848 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
2849 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
2850 nalloc = conf->copies; /* resync */
2851 else
2852 nalloc = 2; /* recovery */
2854 for (i = 0; i < nalloc; i++) {
2855 bio = r10bio->devs[i].bio;
2856 rp = bio->bi_private;
2857 bio_reset(bio);
2858 bio->bi_private = rp;
2859 bio = r10bio->devs[i].repl_bio;
2860 if (bio) {
2861 rp = bio->bi_private;
2862 bio_reset(bio);
2863 bio->bi_private = rp;
2866 return r10bio;
2870 * Set cluster_sync_high since we need other nodes to add the
2871 * range [cluster_sync_low, cluster_sync_high] to suspend list.
2873 static void raid10_set_cluster_sync_high(struct r10conf *conf)
2875 sector_t window_size;
2876 int extra_chunk, chunks;
2879 * First, here we define "stripe" as a unit which across
2880 * all member devices one time, so we get chunks by use
2881 * raid_disks / near_copies. Otherwise, if near_copies is
2882 * close to raid_disks, then resync window could increases
2883 * linearly with the increase of raid_disks, which means
2884 * we will suspend a really large IO window while it is not
2885 * necessary. If raid_disks is not divisible by near_copies,
2886 * an extra chunk is needed to ensure the whole "stripe" is
2887 * covered.
2890 chunks = conf->geo.raid_disks / conf->geo.near_copies;
2891 if (conf->geo.raid_disks % conf->geo.near_copies == 0)
2892 extra_chunk = 0;
2893 else
2894 extra_chunk = 1;
2895 window_size = (chunks + extra_chunk) * conf->mddev->chunk_sectors;
2898 * At least use a 32M window to align with raid1's resync window
2900 window_size = (CLUSTER_RESYNC_WINDOW_SECTORS > window_size) ?
2901 CLUSTER_RESYNC_WINDOW_SECTORS : window_size;
2903 conf->cluster_sync_high = conf->cluster_sync_low + window_size;
2907 * perform a "sync" on one "block"
2909 * We need to make sure that no normal I/O request - particularly write
2910 * requests - conflict with active sync requests.
2912 * This is achieved by tracking pending requests and a 'barrier' concept
2913 * that can be installed to exclude normal IO requests.
2915 * Resync and recovery are handled very differently.
2916 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2918 * For resync, we iterate over virtual addresses, read all copies,
2919 * and update if there are differences. If only one copy is live,
2920 * skip it.
2921 * For recovery, we iterate over physical addresses, read a good
2922 * value for each non-in_sync drive, and over-write.
2924 * So, for recovery we may have several outstanding complex requests for a
2925 * given address, one for each out-of-sync device. We model this by allocating
2926 * a number of r10_bio structures, one for each out-of-sync device.
2927 * As we setup these structures, we collect all bio's together into a list
2928 * which we then process collectively to add pages, and then process again
2929 * to pass to generic_make_request.
2931 * The r10_bio structures are linked using a borrowed master_bio pointer.
2932 * This link is counted in ->remaining. When the r10_bio that points to NULL
2933 * has its remaining count decremented to 0, the whole complex operation
2934 * is complete.
2938 static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
2939 int *skipped)
2941 struct r10conf *conf = mddev->private;
2942 struct r10bio *r10_bio;
2943 struct bio *biolist = NULL, *bio;
2944 sector_t max_sector, nr_sectors;
2945 int i;
2946 int max_sync;
2947 sector_t sync_blocks;
2948 sector_t sectors_skipped = 0;
2949 int chunks_skipped = 0;
2950 sector_t chunk_mask = conf->geo.chunk_mask;
2951 int page_idx = 0;
2953 if (!mempool_initialized(&conf->r10buf_pool))
2954 if (init_resync(conf))
2955 return 0;
2958 * Allow skipping a full rebuild for incremental assembly
2959 * of a clean array, like RAID1 does.
2961 if (mddev->bitmap == NULL &&
2962 mddev->recovery_cp == MaxSector &&
2963 mddev->reshape_position == MaxSector &&
2964 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
2965 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
2966 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
2967 conf->fullsync == 0) {
2968 *skipped = 1;
2969 return mddev->dev_sectors - sector_nr;
2972 skipped:
2973 max_sector = mddev->dev_sectors;
2974 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
2975 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2976 max_sector = mddev->resync_max_sectors;
2977 if (sector_nr >= max_sector) {
2978 conf->cluster_sync_low = 0;
2979 conf->cluster_sync_high = 0;
2981 /* If we aborted, we need to abort the
2982 * sync on the 'current' bitmap chucks (there can
2983 * be several when recovering multiple devices).
2984 * as we may have started syncing it but not finished.
2985 * We can find the current address in
2986 * mddev->curr_resync, but for recovery,
2987 * we need to convert that to several
2988 * virtual addresses.
2990 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
2991 end_reshape(conf);
2992 close_sync(conf);
2993 return 0;
2996 if (mddev->curr_resync < max_sector) { /* aborted */
2997 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2998 md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2999 &sync_blocks, 1);
3000 else for (i = 0; i < conf->geo.raid_disks; i++) {
3001 sector_t sect =
3002 raid10_find_virt(conf, mddev->curr_resync, i);
3003 md_bitmap_end_sync(mddev->bitmap, sect,
3004 &sync_blocks, 1);
3006 } else {
3007 /* completed sync */
3008 if ((!mddev->bitmap || conf->fullsync)
3009 && conf->have_replacement
3010 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3011 /* Completed a full sync so the replacements
3012 * are now fully recovered.
3014 rcu_read_lock();
3015 for (i = 0; i < conf->geo.raid_disks; i++) {
3016 struct md_rdev *rdev =
3017 rcu_dereference(conf->mirrors[i].replacement);
3018 if (rdev)
3019 rdev->recovery_offset = MaxSector;
3021 rcu_read_unlock();
3023 conf->fullsync = 0;
3025 md_bitmap_close_sync(mddev->bitmap);
3026 close_sync(conf);
3027 *skipped = 1;
3028 return sectors_skipped;
3031 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3032 return reshape_request(mddev, sector_nr, skipped);
3034 if (chunks_skipped >= conf->geo.raid_disks) {
3035 /* if there has been nothing to do on any drive,
3036 * then there is nothing to do at all..
3038 *skipped = 1;
3039 return (max_sector - sector_nr) + sectors_skipped;
3042 if (max_sector > mddev->resync_max)
3043 max_sector = mddev->resync_max; /* Don't do IO beyond here */
3045 /* make sure whole request will fit in a chunk - if chunks
3046 * are meaningful
3048 if (conf->geo.near_copies < conf->geo.raid_disks &&
3049 max_sector > (sector_nr | chunk_mask))
3050 max_sector = (sector_nr | chunk_mask) + 1;
3053 * If there is non-resync activity waiting for a turn, then let it
3054 * though before starting on this new sync request.
3056 if (conf->nr_waiting)
3057 schedule_timeout_uninterruptible(1);
3059 /* Again, very different code for resync and recovery.
3060 * Both must result in an r10bio with a list of bios that
3061 * have bi_end_io, bi_sector, bi_disk set,
3062 * and bi_private set to the r10bio.
3063 * For recovery, we may actually create several r10bios
3064 * with 2 bios in each, that correspond to the bios in the main one.
3065 * In this case, the subordinate r10bios link back through a
3066 * borrowed master_bio pointer, and the counter in the master
3067 * includes a ref from each subordinate.
3069 /* First, we decide what to do and set ->bi_end_io
3070 * To end_sync_read if we want to read, and
3071 * end_sync_write if we will want to write.
3074 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
3075 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3076 /* recovery... the complicated one */
3077 int j;
3078 r10_bio = NULL;
3080 for (i = 0 ; i < conf->geo.raid_disks; i++) {
3081 int still_degraded;
3082 struct r10bio *rb2;
3083 sector_t sect;
3084 int must_sync;
3085 int any_working;
3086 struct raid10_info *mirror = &conf->mirrors[i];
3087 struct md_rdev *mrdev, *mreplace;
3089 rcu_read_lock();
3090 mrdev = rcu_dereference(mirror->rdev);
3091 mreplace = rcu_dereference(mirror->replacement);
3093 if ((mrdev == NULL ||
3094 test_bit(Faulty, &mrdev->flags) ||
3095 test_bit(In_sync, &mrdev->flags)) &&
3096 (mreplace == NULL ||
3097 test_bit(Faulty, &mreplace->flags))) {
3098 rcu_read_unlock();
3099 continue;
3102 still_degraded = 0;
3103 /* want to reconstruct this device */
3104 rb2 = r10_bio;
3105 sect = raid10_find_virt(conf, sector_nr, i);
3106 if (sect >= mddev->resync_max_sectors) {
3107 /* last stripe is not complete - don't
3108 * try to recover this sector.
3110 rcu_read_unlock();
3111 continue;
3113 if (mreplace && test_bit(Faulty, &mreplace->flags))
3114 mreplace = NULL;
3115 /* Unless we are doing a full sync, or a replacement
3116 * we only need to recover the block if it is set in
3117 * the bitmap
3119 must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
3120 &sync_blocks, 1);
3121 if (sync_blocks < max_sync)
3122 max_sync = sync_blocks;
3123 if (!must_sync &&
3124 mreplace == NULL &&
3125 !conf->fullsync) {
3126 /* yep, skip the sync_blocks here, but don't assume
3127 * that there will never be anything to do here
3129 chunks_skipped = -1;
3130 rcu_read_unlock();
3131 continue;
3133 atomic_inc(&mrdev->nr_pending);
3134 if (mreplace)
3135 atomic_inc(&mreplace->nr_pending);
3136 rcu_read_unlock();
3138 r10_bio = raid10_alloc_init_r10buf(conf);
3139 r10_bio->state = 0;
3140 raise_barrier(conf, rb2 != NULL);
3141 atomic_set(&r10_bio->remaining, 0);
3143 r10_bio->master_bio = (struct bio*)rb2;
3144 if (rb2)
3145 atomic_inc(&rb2->remaining);
3146 r10_bio->mddev = mddev;
3147 set_bit(R10BIO_IsRecover, &r10_bio->state);
3148 r10_bio->sector = sect;
3150 raid10_find_phys(conf, r10_bio);
3152 /* Need to check if the array will still be
3153 * degraded
3155 rcu_read_lock();
3156 for (j = 0; j < conf->geo.raid_disks; j++) {
3157 struct md_rdev *rdev = rcu_dereference(
3158 conf->mirrors[j].rdev);
3159 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3160 still_degraded = 1;
3161 break;
3165 must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
3166 &sync_blocks, still_degraded);
3168 any_working = 0;
3169 for (j=0; j<conf->copies;j++) {
3170 int k;
3171 int d = r10_bio->devs[j].devnum;
3172 sector_t from_addr, to_addr;
3173 struct md_rdev *rdev =
3174 rcu_dereference(conf->mirrors[d].rdev);
3175 sector_t sector, first_bad;
3176 int bad_sectors;
3177 if (!rdev ||
3178 !test_bit(In_sync, &rdev->flags))
3179 continue;
3180 /* This is where we read from */
3181 any_working = 1;
3182 sector = r10_bio->devs[j].addr;
3184 if (is_badblock(rdev, sector, max_sync,
3185 &first_bad, &bad_sectors)) {
3186 if (first_bad > sector)
3187 max_sync = first_bad - sector;
3188 else {
3189 bad_sectors -= (sector
3190 - first_bad);
3191 if (max_sync > bad_sectors)
3192 max_sync = bad_sectors;
3193 continue;
3196 bio = r10_bio->devs[0].bio;
3197 bio->bi_next = biolist;
3198 biolist = bio;
3199 bio->bi_end_io = end_sync_read;
3200 bio_set_op_attrs(bio, REQ_OP_READ, 0);
3201 if (test_bit(FailFast, &rdev->flags))
3202 bio->bi_opf |= MD_FAILFAST;
3203 from_addr = r10_bio->devs[j].addr;
3204 bio->bi_iter.bi_sector = from_addr +
3205 rdev->data_offset;
3206 bio_set_dev(bio, rdev->bdev);
3207 atomic_inc(&rdev->nr_pending);
3208 /* and we write to 'i' (if not in_sync) */
3210 for (k=0; k<conf->copies; k++)
3211 if (r10_bio->devs[k].devnum == i)
3212 break;
3213 BUG_ON(k == conf->copies);
3214 to_addr = r10_bio->devs[k].addr;
3215 r10_bio->devs[0].devnum = d;
3216 r10_bio->devs[0].addr = from_addr;
3217 r10_bio->devs[1].devnum = i;
3218 r10_bio->devs[1].addr = to_addr;
3220 if (!test_bit(In_sync, &mrdev->flags)) {
3221 bio = r10_bio->devs[1].bio;
3222 bio->bi_next = biolist;
3223 biolist = bio;
3224 bio->bi_end_io = end_sync_write;
3225 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
3226 bio->bi_iter.bi_sector = to_addr
3227 + mrdev->data_offset;
3228 bio_set_dev(bio, mrdev->bdev);
3229 atomic_inc(&r10_bio->remaining);
3230 } else
3231 r10_bio->devs[1].bio->bi_end_io = NULL;
3233 /* and maybe write to replacement */
3234 bio = r10_bio->devs[1].repl_bio;
3235 if (bio)
3236 bio->bi_end_io = NULL;
3237 /* Note: if mreplace != NULL, then bio
3238 * cannot be NULL as r10buf_pool_alloc will
3239 * have allocated it.
3240 * So the second test here is pointless.
3241 * But it keeps semantic-checkers happy, and
3242 * this comment keeps human reviewers
3243 * happy.
3245 if (mreplace == NULL || bio == NULL ||
3246 test_bit(Faulty, &mreplace->flags))
3247 break;
3248 bio->bi_next = biolist;
3249 biolist = bio;
3250 bio->bi_end_io = end_sync_write;
3251 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
3252 bio->bi_iter.bi_sector = to_addr +
3253 mreplace->data_offset;
3254 bio_set_dev(bio, mreplace->bdev);
3255 atomic_inc(&r10_bio->remaining);
3256 break;
3258 rcu_read_unlock();
3259 if (j == conf->copies) {
3260 /* Cannot recover, so abort the recovery or
3261 * record a bad block */
3262 if (any_working) {
3263 /* problem is that there are bad blocks
3264 * on other device(s)
3266 int k;
3267 for (k = 0; k < conf->copies; k++)
3268 if (r10_bio->devs[k].devnum == i)
3269 break;
3270 if (!test_bit(In_sync,
3271 &mrdev->flags)
3272 && !rdev_set_badblocks(
3273 mrdev,
3274 r10_bio->devs[k].addr,
3275 max_sync, 0))
3276 any_working = 0;
3277 if (mreplace &&
3278 !rdev_set_badblocks(
3279 mreplace,
3280 r10_bio->devs[k].addr,
3281 max_sync, 0))
3282 any_working = 0;
3284 if (!any_working) {
3285 if (!test_and_set_bit(MD_RECOVERY_INTR,
3286 &mddev->recovery))
3287 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
3288 mdname(mddev));
3289 mirror->recovery_disabled
3290 = mddev->recovery_disabled;
3292 put_buf(r10_bio);
3293 if (rb2)
3294 atomic_dec(&rb2->remaining);
3295 r10_bio = rb2;
3296 rdev_dec_pending(mrdev, mddev);
3297 if (mreplace)
3298 rdev_dec_pending(mreplace, mddev);
3299 break;
3301 rdev_dec_pending(mrdev, mddev);
3302 if (mreplace)
3303 rdev_dec_pending(mreplace, mddev);
3304 if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) {
3305 /* Only want this if there is elsewhere to
3306 * read from. 'j' is currently the first
3307 * readable copy.
3309 int targets = 1;
3310 for (; j < conf->copies; j++) {
3311 int d = r10_bio->devs[j].devnum;
3312 if (conf->mirrors[d].rdev &&
3313 test_bit(In_sync,
3314 &conf->mirrors[d].rdev->flags))
3315 targets++;
3317 if (targets == 1)
3318 r10_bio->devs[0].bio->bi_opf
3319 &= ~MD_FAILFAST;
3322 if (biolist == NULL) {
3323 while (r10_bio) {
3324 struct r10bio *rb2 = r10_bio;
3325 r10_bio = (struct r10bio*) rb2->master_bio;
3326 rb2->master_bio = NULL;
3327 put_buf(rb2);
3329 goto giveup;
3331 } else {
3332 /* resync. Schedule a read for every block at this virt offset */
3333 int count = 0;
3336 * Since curr_resync_completed could probably not update in
3337 * time, and we will set cluster_sync_low based on it.
3338 * Let's check against "sector_nr + 2 * RESYNC_SECTORS" for
3339 * safety reason, which ensures curr_resync_completed is
3340 * updated in bitmap_cond_end_sync.
3342 md_bitmap_cond_end_sync(mddev->bitmap, sector_nr,
3343 mddev_is_clustered(mddev) &&
3344 (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high));
3346 if (!md_bitmap_start_sync(mddev->bitmap, sector_nr,
3347 &sync_blocks, mddev->degraded) &&
3348 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3349 &mddev->recovery)) {
3350 /* We can skip this block */
3351 *skipped = 1;
3352 return sync_blocks + sectors_skipped;
3354 if (sync_blocks < max_sync)
3355 max_sync = sync_blocks;
3356 r10_bio = raid10_alloc_init_r10buf(conf);
3357 r10_bio->state = 0;
3359 r10_bio->mddev = mddev;
3360 atomic_set(&r10_bio->remaining, 0);
3361 raise_barrier(conf, 0);
3362 conf->next_resync = sector_nr;
3364 r10_bio->master_bio = NULL;
3365 r10_bio->sector = sector_nr;
3366 set_bit(R10BIO_IsSync, &r10_bio->state);
3367 raid10_find_phys(conf, r10_bio);
3368 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
3370 for (i = 0; i < conf->copies; i++) {
3371 int d = r10_bio->devs[i].devnum;
3372 sector_t first_bad, sector;
3373 int bad_sectors;
3374 struct md_rdev *rdev;
3376 if (r10_bio->devs[i].repl_bio)
3377 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3379 bio = r10_bio->devs[i].bio;
3380 bio->bi_status = BLK_STS_IOERR;
3381 rcu_read_lock();
3382 rdev = rcu_dereference(conf->mirrors[d].rdev);
3383 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3384 rcu_read_unlock();
3385 continue;
3387 sector = r10_bio->devs[i].addr;
3388 if (is_badblock(rdev, sector, max_sync,
3389 &first_bad, &bad_sectors)) {
3390 if (first_bad > sector)
3391 max_sync = first_bad - sector;
3392 else {
3393 bad_sectors -= (sector - first_bad);
3394 if (max_sync > bad_sectors)
3395 max_sync = bad_sectors;
3396 rcu_read_unlock();
3397 continue;
3400 atomic_inc(&rdev->nr_pending);
3401 atomic_inc(&r10_bio->remaining);
3402 bio->bi_next = biolist;
3403 biolist = bio;
3404 bio->bi_end_io = end_sync_read;
3405 bio_set_op_attrs(bio, REQ_OP_READ, 0);
3406 if (test_bit(FailFast, &rdev->flags))
3407 bio->bi_opf |= MD_FAILFAST;
3408 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3409 bio_set_dev(bio, rdev->bdev);
3410 count++;
3412 rdev = rcu_dereference(conf->mirrors[d].replacement);
3413 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3414 rcu_read_unlock();
3415 continue;
3417 atomic_inc(&rdev->nr_pending);
3419 /* Need to set up for writing to the replacement */
3420 bio = r10_bio->devs[i].repl_bio;
3421 bio->bi_status = BLK_STS_IOERR;
3423 sector = r10_bio->devs[i].addr;
3424 bio->bi_next = biolist;
3425 biolist = bio;
3426 bio->bi_end_io = end_sync_write;
3427 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
3428 if (test_bit(FailFast, &rdev->flags))
3429 bio->bi_opf |= MD_FAILFAST;
3430 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3431 bio_set_dev(bio, rdev->bdev);
3432 count++;
3433 rcu_read_unlock();
3436 if (count < 2) {
3437 for (i=0; i<conf->copies; i++) {
3438 int d = r10_bio->devs[i].devnum;
3439 if (r10_bio->devs[i].bio->bi_end_io)
3440 rdev_dec_pending(conf->mirrors[d].rdev,
3441 mddev);
3442 if (r10_bio->devs[i].repl_bio &&
3443 r10_bio->devs[i].repl_bio->bi_end_io)
3444 rdev_dec_pending(
3445 conf->mirrors[d].replacement,
3446 mddev);
3448 put_buf(r10_bio);
3449 biolist = NULL;
3450 goto giveup;
3454 nr_sectors = 0;
3455 if (sector_nr + max_sync < max_sector)
3456 max_sector = sector_nr + max_sync;
3457 do {
3458 struct page *page;
3459 int len = PAGE_SIZE;
3460 if (sector_nr + (len>>9) > max_sector)
3461 len = (max_sector - sector_nr) << 9;
3462 if (len == 0)
3463 break;
3464 for (bio= biolist ; bio ; bio=bio->bi_next) {
3465 struct resync_pages *rp = get_resync_pages(bio);
3466 page = resync_fetch_page(rp, page_idx);
3468 * won't fail because the vec table is big enough
3469 * to hold all these pages
3471 bio_add_page(bio, page, len, 0);
3473 nr_sectors += len>>9;
3474 sector_nr += len>>9;
3475 } while (++page_idx < RESYNC_PAGES);
3476 r10_bio->sectors = nr_sectors;
3478 if (mddev_is_clustered(mddev) &&
3479 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3480 /* It is resync not recovery */
3481 if (conf->cluster_sync_high < sector_nr + nr_sectors) {
3482 conf->cluster_sync_low = mddev->curr_resync_completed;
3483 raid10_set_cluster_sync_high(conf);
3484 /* Send resync message */
3485 md_cluster_ops->resync_info_update(mddev,
3486 conf->cluster_sync_low,
3487 conf->cluster_sync_high);
3489 } else if (mddev_is_clustered(mddev)) {
3490 /* This is recovery not resync */
3491 sector_t sect_va1, sect_va2;
3492 bool broadcast_msg = false;
3494 for (i = 0; i < conf->geo.raid_disks; i++) {
3496 * sector_nr is a device address for recovery, so we
3497 * need translate it to array address before compare
3498 * with cluster_sync_high.
3500 sect_va1 = raid10_find_virt(conf, sector_nr, i);
3502 if (conf->cluster_sync_high < sect_va1 + nr_sectors) {
3503 broadcast_msg = true;
3505 * curr_resync_completed is similar as
3506 * sector_nr, so make the translation too.
3508 sect_va2 = raid10_find_virt(conf,
3509 mddev->curr_resync_completed, i);
3511 if (conf->cluster_sync_low == 0 ||
3512 conf->cluster_sync_low > sect_va2)
3513 conf->cluster_sync_low = sect_va2;
3516 if (broadcast_msg) {
3517 raid10_set_cluster_sync_high(conf);
3518 md_cluster_ops->resync_info_update(mddev,
3519 conf->cluster_sync_low,
3520 conf->cluster_sync_high);
3524 while (biolist) {
3525 bio = biolist;
3526 biolist = biolist->bi_next;
3528 bio->bi_next = NULL;
3529 r10_bio = get_resync_r10bio(bio);
3530 r10_bio->sectors = nr_sectors;
3532 if (bio->bi_end_io == end_sync_read) {
3533 md_sync_acct_bio(bio, nr_sectors);
3534 bio->bi_status = 0;
3535 generic_make_request(bio);
3539 if (sectors_skipped)
3540 /* pretend they weren't skipped, it makes
3541 * no important difference in this case
3543 md_done_sync(mddev, sectors_skipped, 1);
3545 return sectors_skipped + nr_sectors;
3546 giveup:
3547 /* There is nowhere to write, so all non-sync
3548 * drives must be failed or in resync, all drives
3549 * have a bad block, so try the next chunk...
3551 if (sector_nr + max_sync < max_sector)
3552 max_sector = sector_nr + max_sync;
3554 sectors_skipped += (max_sector - sector_nr);
3555 chunks_skipped ++;
3556 sector_nr = max_sector;
3557 goto skipped;
3560 static sector_t
3561 raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
3563 sector_t size;
3564 struct r10conf *conf = mddev->private;
3566 if (!raid_disks)
3567 raid_disks = min(conf->geo.raid_disks,
3568 conf->prev.raid_disks);
3569 if (!sectors)
3570 sectors = conf->dev_sectors;
3572 size = sectors >> conf->geo.chunk_shift;
3573 sector_div(size, conf->geo.far_copies);
3574 size = size * raid_disks;
3575 sector_div(size, conf->geo.near_copies);
3577 return size << conf->geo.chunk_shift;
3580 static void calc_sectors(struct r10conf *conf, sector_t size)
3582 /* Calculate the number of sectors-per-device that will
3583 * actually be used, and set conf->dev_sectors and
3584 * conf->stride
3587 size = size >> conf->geo.chunk_shift;
3588 sector_div(size, conf->geo.far_copies);
3589 size = size * conf->geo.raid_disks;
3590 sector_div(size, conf->geo.near_copies);
3591 /* 'size' is now the number of chunks in the array */
3592 /* calculate "used chunks per device" */
3593 size = size * conf->copies;
3595 /* We need to round up when dividing by raid_disks to
3596 * get the stride size.
3598 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
3600 conf->dev_sectors = size << conf->geo.chunk_shift;
3602 if (conf->geo.far_offset)
3603 conf->geo.stride = 1 << conf->geo.chunk_shift;
3604 else {
3605 sector_div(size, conf->geo.far_copies);
3606 conf->geo.stride = size << conf->geo.chunk_shift;
3610 enum geo_type {geo_new, geo_old, geo_start};
3611 static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3613 int nc, fc, fo;
3614 int layout, chunk, disks;
3615 switch (new) {
3616 case geo_old:
3617 layout = mddev->layout;
3618 chunk = mddev->chunk_sectors;
3619 disks = mddev->raid_disks - mddev->delta_disks;
3620 break;
3621 case geo_new:
3622 layout = mddev->new_layout;
3623 chunk = mddev->new_chunk_sectors;
3624 disks = mddev->raid_disks;
3625 break;
3626 default: /* avoid 'may be unused' warnings */
3627 case geo_start: /* new when starting reshape - raid_disks not
3628 * updated yet. */
3629 layout = mddev->new_layout;
3630 chunk = mddev->new_chunk_sectors;
3631 disks = mddev->raid_disks + mddev->delta_disks;
3632 break;
3634 if (layout >> 19)
3635 return -1;
3636 if (chunk < (PAGE_SIZE >> 9) ||
3637 !is_power_of_2(chunk))
3638 return -2;
3639 nc = layout & 255;
3640 fc = (layout >> 8) & 255;
3641 fo = layout & (1<<16);
3642 geo->raid_disks = disks;
3643 geo->near_copies = nc;
3644 geo->far_copies = fc;
3645 geo->far_offset = fo;
3646 switch (layout >> 17) {
3647 case 0: /* original layout. simple but not always optimal */
3648 geo->far_set_size = disks;
3649 break;
3650 case 1: /* "improved" layout which was buggy. Hopefully no-one is
3651 * actually using this, but leave code here just in case.*/
3652 geo->far_set_size = disks/fc;
3653 WARN(geo->far_set_size < fc,
3654 "This RAID10 layout does not provide data safety - please backup and create new array\n");
3655 break;
3656 case 2: /* "improved" layout fixed to match documentation */
3657 geo->far_set_size = fc * nc;
3658 break;
3659 default: /* Not a valid layout */
3660 return -1;
3662 geo->chunk_mask = chunk - 1;
3663 geo->chunk_shift = ffz(~chunk);
3664 return nc*fc;
3667 static struct r10conf *setup_conf(struct mddev *mddev)
3669 struct r10conf *conf = NULL;
3670 int err = -EINVAL;
3671 struct geom geo;
3672 int copies;
3674 copies = setup_geo(&geo, mddev, geo_new);
3676 if (copies == -2) {
3677 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
3678 mdname(mddev), PAGE_SIZE);
3679 goto out;
3682 if (copies < 2 || copies > mddev->raid_disks) {
3683 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3684 mdname(mddev), mddev->new_layout);
3685 goto out;
3688 err = -ENOMEM;
3689 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
3690 if (!conf)
3691 goto out;
3693 /* FIXME calc properly */
3694 conf->mirrors = kcalloc(mddev->raid_disks + max(0, -mddev->delta_disks),
3695 sizeof(struct raid10_info),
3696 GFP_KERNEL);
3697 if (!conf->mirrors)
3698 goto out;
3700 conf->tmppage = alloc_page(GFP_KERNEL);
3701 if (!conf->tmppage)
3702 goto out;
3704 conf->geo = geo;
3705 conf->copies = copies;
3706 err = mempool_init(&conf->r10bio_pool, NR_RAID10_BIOS, r10bio_pool_alloc,
3707 r10bio_pool_free, conf);
3708 if (err)
3709 goto out;
3711 err = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0);
3712 if (err)
3713 goto out;
3715 calc_sectors(conf, mddev->dev_sectors);
3716 if (mddev->reshape_position == MaxSector) {
3717 conf->prev = conf->geo;
3718 conf->reshape_progress = MaxSector;
3719 } else {
3720 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
3721 err = -EINVAL;
3722 goto out;
3724 conf->reshape_progress = mddev->reshape_position;
3725 if (conf->prev.far_offset)
3726 conf->prev.stride = 1 << conf->prev.chunk_shift;
3727 else
3728 /* far_copies must be 1 */
3729 conf->prev.stride = conf->dev_sectors;
3731 conf->reshape_safe = conf->reshape_progress;
3732 spin_lock_init(&conf->device_lock);
3733 INIT_LIST_HEAD(&conf->retry_list);
3734 INIT_LIST_HEAD(&conf->bio_end_io_list);
3736 spin_lock_init(&conf->resync_lock);
3737 init_waitqueue_head(&conf->wait_barrier);
3738 atomic_set(&conf->nr_pending, 0);
3740 err = -ENOMEM;
3741 conf->thread = md_register_thread(raid10d, mddev, "raid10");
3742 if (!conf->thread)
3743 goto out;
3745 conf->mddev = mddev;
3746 return conf;
3748 out:
3749 if (conf) {
3750 mempool_exit(&conf->r10bio_pool);
3751 kfree(conf->mirrors);
3752 safe_put_page(conf->tmppage);
3753 bioset_exit(&conf->bio_split);
3754 kfree(conf);
3756 return ERR_PTR(err);
3759 static int raid10_run(struct mddev *mddev)
3761 struct r10conf *conf;
3762 int i, disk_idx, chunk_size;
3763 struct raid10_info *disk;
3764 struct md_rdev *rdev;
3765 sector_t size;
3766 sector_t min_offset_diff = 0;
3767 int first = 1;
3768 bool discard_supported = false;
3770 if (mddev_init_writes_pending(mddev) < 0)
3771 return -ENOMEM;
3773 if (mddev->private == NULL) {
3774 conf = setup_conf(mddev);
3775 if (IS_ERR(conf))
3776 return PTR_ERR(conf);
3777 mddev->private = conf;
3779 conf = mddev->private;
3780 if (!conf)
3781 goto out;
3783 if (mddev_is_clustered(conf->mddev)) {
3784 int fc, fo;
3786 fc = (mddev->layout >> 8) & 255;
3787 fo = mddev->layout & (1<<16);
3788 if (fc > 1 || fo > 0) {
3789 pr_err("only near layout is supported by clustered"
3790 " raid10\n");
3791 goto out_free_conf;
3795 mddev->thread = conf->thread;
3796 conf->thread = NULL;
3798 chunk_size = mddev->chunk_sectors << 9;
3799 if (mddev->queue) {
3800 blk_queue_max_discard_sectors(mddev->queue,
3801 mddev->chunk_sectors);
3802 blk_queue_max_write_same_sectors(mddev->queue, 0);
3803 blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
3804 blk_queue_io_min(mddev->queue, chunk_size);
3805 if (conf->geo.raid_disks % conf->geo.near_copies)
3806 blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
3807 else
3808 blk_queue_io_opt(mddev->queue, chunk_size *
3809 (conf->geo.raid_disks / conf->geo.near_copies));
3812 rdev_for_each(rdev, mddev) {
3813 long long diff;
3815 disk_idx = rdev->raid_disk;
3816 if (disk_idx < 0)
3817 continue;
3818 if (disk_idx >= conf->geo.raid_disks &&
3819 disk_idx >= conf->prev.raid_disks)
3820 continue;
3821 disk = conf->mirrors + disk_idx;
3823 if (test_bit(Replacement, &rdev->flags)) {
3824 if (disk->replacement)
3825 goto out_free_conf;
3826 disk->replacement = rdev;
3827 } else {
3828 if (disk->rdev)
3829 goto out_free_conf;
3830 disk->rdev = rdev;
3832 diff = (rdev->new_data_offset - rdev->data_offset);
3833 if (!mddev->reshape_backwards)
3834 diff = -diff;
3835 if (diff < 0)
3836 diff = 0;
3837 if (first || diff < min_offset_diff)
3838 min_offset_diff = diff;
3840 if (mddev->gendisk)
3841 disk_stack_limits(mddev->gendisk, rdev->bdev,
3842 rdev->data_offset << 9);
3844 disk->head_position = 0;
3846 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3847 discard_supported = true;
3848 first = 0;
3851 if (mddev->queue) {
3852 if (discard_supported)
3853 blk_queue_flag_set(QUEUE_FLAG_DISCARD,
3854 mddev->queue);
3855 else
3856 blk_queue_flag_clear(QUEUE_FLAG_DISCARD,
3857 mddev->queue);
3859 /* need to check that every block has at least one working mirror */
3860 if (!enough(conf, -1)) {
3861 pr_err("md/raid10:%s: not enough operational mirrors.\n",
3862 mdname(mddev));
3863 goto out_free_conf;
3866 if (conf->reshape_progress != MaxSector) {
3867 /* must ensure that shape change is supported */
3868 if (conf->geo.far_copies != 1 &&
3869 conf->geo.far_offset == 0)
3870 goto out_free_conf;
3871 if (conf->prev.far_copies != 1 &&
3872 conf->prev.far_offset == 0)
3873 goto out_free_conf;
3876 mddev->degraded = 0;
3877 for (i = 0;
3878 i < conf->geo.raid_disks
3879 || i < conf->prev.raid_disks;
3880 i++) {
3882 disk = conf->mirrors + i;
3884 if (!disk->rdev && disk->replacement) {
3885 /* The replacement is all we have - use it */
3886 disk->rdev = disk->replacement;
3887 disk->replacement = NULL;
3888 clear_bit(Replacement, &disk->rdev->flags);
3891 if (!disk->rdev ||
3892 !test_bit(In_sync, &disk->rdev->flags)) {
3893 disk->head_position = 0;
3894 mddev->degraded++;
3895 if (disk->rdev &&
3896 disk->rdev->saved_raid_disk < 0)
3897 conf->fullsync = 1;
3900 if (disk->replacement &&
3901 !test_bit(In_sync, &disk->replacement->flags) &&
3902 disk->replacement->saved_raid_disk < 0) {
3903 conf->fullsync = 1;
3906 disk->recovery_disabled = mddev->recovery_disabled - 1;
3909 if (mddev->recovery_cp != MaxSector)
3910 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
3911 mdname(mddev));
3912 pr_info("md/raid10:%s: active with %d out of %d devices\n",
3913 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
3914 conf->geo.raid_disks);
3916 * Ok, everything is just fine now
3918 mddev->dev_sectors = conf->dev_sectors;
3919 size = raid10_size(mddev, 0, 0);
3920 md_set_array_sectors(mddev, size);
3921 mddev->resync_max_sectors = size;
3922 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
3924 if (mddev->queue) {
3925 int stripe = conf->geo.raid_disks *
3926 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
3928 /* Calculate max read-ahead size.
3929 * We need to readahead at least twice a whole stripe....
3930 * maybe...
3932 stripe /= conf->geo.near_copies;
3933 if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
3934 mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
3937 if (md_integrity_register(mddev))
3938 goto out_free_conf;
3940 if (conf->reshape_progress != MaxSector) {
3941 unsigned long before_length, after_length;
3943 before_length = ((1 << conf->prev.chunk_shift) *
3944 conf->prev.far_copies);
3945 after_length = ((1 << conf->geo.chunk_shift) *
3946 conf->geo.far_copies);
3948 if (max(before_length, after_length) > min_offset_diff) {
3949 /* This cannot work */
3950 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
3951 goto out_free_conf;
3953 conf->offset_diff = min_offset_diff;
3955 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3956 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3957 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3958 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3959 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3960 "reshape");
3961 if (!mddev->sync_thread)
3962 goto out_free_conf;
3965 return 0;
3967 out_free_conf:
3968 md_unregister_thread(&mddev->thread);
3969 mempool_exit(&conf->r10bio_pool);
3970 safe_put_page(conf->tmppage);
3971 kfree(conf->mirrors);
3972 kfree(conf);
3973 mddev->private = NULL;
3974 out:
3975 return -EIO;
3978 static void raid10_free(struct mddev *mddev, void *priv)
3980 struct r10conf *conf = priv;
3982 mempool_exit(&conf->r10bio_pool);
3983 safe_put_page(conf->tmppage);
3984 kfree(conf->mirrors);
3985 kfree(conf->mirrors_old);
3986 kfree(conf->mirrors_new);
3987 bioset_exit(&conf->bio_split);
3988 kfree(conf);
3991 static void raid10_quiesce(struct mddev *mddev, int quiesce)
3993 struct r10conf *conf = mddev->private;
3995 if (quiesce)
3996 raise_barrier(conf, 0);
3997 else
3998 lower_barrier(conf);
4001 static int raid10_resize(struct mddev *mddev, sector_t sectors)
4003 /* Resize of 'far' arrays is not supported.
4004 * For 'near' and 'offset' arrays we can set the
4005 * number of sectors used to be an appropriate multiple
4006 * of the chunk size.
4007 * For 'offset', this is far_copies*chunksize.
4008 * For 'near' the multiplier is the LCM of
4009 * near_copies and raid_disks.
4010 * So if far_copies > 1 && !far_offset, fail.
4011 * Else find LCM(raid_disks, near_copy)*far_copies and
4012 * multiply by chunk_size. Then round to this number.
4013 * This is mostly done by raid10_size()
4015 struct r10conf *conf = mddev->private;
4016 sector_t oldsize, size;
4018 if (mddev->reshape_position != MaxSector)
4019 return -EBUSY;
4021 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
4022 return -EINVAL;
4024 oldsize = raid10_size(mddev, 0, 0);
4025 size = raid10_size(mddev, sectors, 0);
4026 if (mddev->external_size &&
4027 mddev->array_sectors > size)
4028 return -EINVAL;
4029 if (mddev->bitmap) {
4030 int ret = md_bitmap_resize(mddev->bitmap, size, 0, 0);
4031 if (ret)
4032 return ret;
4034 md_set_array_sectors(mddev, size);
4035 if (sectors > mddev->dev_sectors &&
4036 mddev->recovery_cp > oldsize) {
4037 mddev->recovery_cp = oldsize;
4038 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4040 calc_sectors(conf, sectors);
4041 mddev->dev_sectors = conf->dev_sectors;
4042 mddev->resync_max_sectors = size;
4043 return 0;
4046 static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs)
4048 struct md_rdev *rdev;
4049 struct r10conf *conf;
4051 if (mddev->degraded > 0) {
4052 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
4053 mdname(mddev));
4054 return ERR_PTR(-EINVAL);
4056 sector_div(size, devs);
4058 /* Set new parameters */
4059 mddev->new_level = 10;
4060 /* new layout: far_copies = 1, near_copies = 2 */
4061 mddev->new_layout = (1<<8) + 2;
4062 mddev->new_chunk_sectors = mddev->chunk_sectors;
4063 mddev->delta_disks = mddev->raid_disks;
4064 mddev->raid_disks *= 2;
4065 /* make sure it will be not marked as dirty */
4066 mddev->recovery_cp = MaxSector;
4067 mddev->dev_sectors = size;
4069 conf = setup_conf(mddev);
4070 if (!IS_ERR(conf)) {
4071 rdev_for_each(rdev, mddev)
4072 if (rdev->raid_disk >= 0) {
4073 rdev->new_raid_disk = rdev->raid_disk * 2;
4074 rdev->sectors = size;
4076 conf->barrier = 1;
4079 return conf;
4082 static void *raid10_takeover(struct mddev *mddev)
4084 struct r0conf *raid0_conf;
4086 /* raid10 can take over:
4087 * raid0 - providing it has only two drives
4089 if (mddev->level == 0) {
4090 /* for raid0 takeover only one zone is supported */
4091 raid0_conf = mddev->private;
4092 if (raid0_conf->nr_strip_zones > 1) {
4093 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
4094 mdname(mddev));
4095 return ERR_PTR(-EINVAL);
4097 return raid10_takeover_raid0(mddev,
4098 raid0_conf->strip_zone->zone_end,
4099 raid0_conf->strip_zone->nb_dev);
4101 return ERR_PTR(-EINVAL);
4104 static int raid10_check_reshape(struct mddev *mddev)
4106 /* Called when there is a request to change
4107 * - layout (to ->new_layout)
4108 * - chunk size (to ->new_chunk_sectors)
4109 * - raid_disks (by delta_disks)
4110 * or when trying to restart a reshape that was ongoing.
4112 * We need to validate the request and possibly allocate
4113 * space if that might be an issue later.
4115 * Currently we reject any reshape of a 'far' mode array,
4116 * allow chunk size to change if new is generally acceptable,
4117 * allow raid_disks to increase, and allow
4118 * a switch between 'near' mode and 'offset' mode.
4120 struct r10conf *conf = mddev->private;
4121 struct geom geo;
4123 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
4124 return -EINVAL;
4126 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
4127 /* mustn't change number of copies */
4128 return -EINVAL;
4129 if (geo.far_copies > 1 && !geo.far_offset)
4130 /* Cannot switch to 'far' mode */
4131 return -EINVAL;
4133 if (mddev->array_sectors & geo.chunk_mask)
4134 /* not factor of array size */
4135 return -EINVAL;
4137 if (!enough(conf, -1))
4138 return -EINVAL;
4140 kfree(conf->mirrors_new);
4141 conf->mirrors_new = NULL;
4142 if (mddev->delta_disks > 0) {
4143 /* allocate new 'mirrors' list */
4144 conf->mirrors_new =
4145 kcalloc(mddev->raid_disks + mddev->delta_disks,
4146 sizeof(struct raid10_info),
4147 GFP_KERNEL);
4148 if (!conf->mirrors_new)
4149 return -ENOMEM;
4151 return 0;
4155 * Need to check if array has failed when deciding whether to:
4156 * - start an array
4157 * - remove non-faulty devices
4158 * - add a spare
4159 * - allow a reshape
4160 * This determination is simple when no reshape is happening.
4161 * However if there is a reshape, we need to carefully check
4162 * both the before and after sections.
4163 * This is because some failed devices may only affect one
4164 * of the two sections, and some non-in_sync devices may
4165 * be insync in the section most affected by failed devices.
4167 static int calc_degraded(struct r10conf *conf)
4169 int degraded, degraded2;
4170 int i;
4172 rcu_read_lock();
4173 degraded = 0;
4174 /* 'prev' section first */
4175 for (i = 0; i < conf->prev.raid_disks; i++) {
4176 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4177 if (!rdev || test_bit(Faulty, &rdev->flags))
4178 degraded++;
4179 else if (!test_bit(In_sync, &rdev->flags))
4180 /* When we can reduce the number of devices in
4181 * an array, this might not contribute to
4182 * 'degraded'. It does now.
4184 degraded++;
4186 rcu_read_unlock();
4187 if (conf->geo.raid_disks == conf->prev.raid_disks)
4188 return degraded;
4189 rcu_read_lock();
4190 degraded2 = 0;
4191 for (i = 0; i < conf->geo.raid_disks; i++) {
4192 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4193 if (!rdev || test_bit(Faulty, &rdev->flags))
4194 degraded2++;
4195 else if (!test_bit(In_sync, &rdev->flags)) {
4196 /* If reshape is increasing the number of devices,
4197 * this section has already been recovered, so
4198 * it doesn't contribute to degraded.
4199 * else it does.
4201 if (conf->geo.raid_disks <= conf->prev.raid_disks)
4202 degraded2++;
4205 rcu_read_unlock();
4206 if (degraded2 > degraded)
4207 return degraded2;
4208 return degraded;
4211 static int raid10_start_reshape(struct mddev *mddev)
4213 /* A 'reshape' has been requested. This commits
4214 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4215 * This also checks if there are enough spares and adds them
4216 * to the array.
4217 * We currently require enough spares to make the final
4218 * array non-degraded. We also require that the difference
4219 * between old and new data_offset - on each device - is
4220 * enough that we never risk over-writing.
4223 unsigned long before_length, after_length;
4224 sector_t min_offset_diff = 0;
4225 int first = 1;
4226 struct geom new;
4227 struct r10conf *conf = mddev->private;
4228 struct md_rdev *rdev;
4229 int spares = 0;
4230 int ret;
4232 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4233 return -EBUSY;
4235 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4236 return -EINVAL;
4238 before_length = ((1 << conf->prev.chunk_shift) *
4239 conf->prev.far_copies);
4240 after_length = ((1 << conf->geo.chunk_shift) *
4241 conf->geo.far_copies);
4243 rdev_for_each(rdev, mddev) {
4244 if (!test_bit(In_sync, &rdev->flags)
4245 && !test_bit(Faulty, &rdev->flags))
4246 spares++;
4247 if (rdev->raid_disk >= 0) {
4248 long long diff = (rdev->new_data_offset
4249 - rdev->data_offset);
4250 if (!mddev->reshape_backwards)
4251 diff = -diff;
4252 if (diff < 0)
4253 diff = 0;
4254 if (first || diff < min_offset_diff)
4255 min_offset_diff = diff;
4256 first = 0;
4260 if (max(before_length, after_length) > min_offset_diff)
4261 return -EINVAL;
4263 if (spares < mddev->delta_disks)
4264 return -EINVAL;
4266 conf->offset_diff = min_offset_diff;
4267 spin_lock_irq(&conf->device_lock);
4268 if (conf->mirrors_new) {
4269 memcpy(conf->mirrors_new, conf->mirrors,
4270 sizeof(struct raid10_info)*conf->prev.raid_disks);
4271 smp_mb();
4272 kfree(conf->mirrors_old);
4273 conf->mirrors_old = conf->mirrors;
4274 conf->mirrors = conf->mirrors_new;
4275 conf->mirrors_new = NULL;
4277 setup_geo(&conf->geo, mddev, geo_start);
4278 smp_mb();
4279 if (mddev->reshape_backwards) {
4280 sector_t size = raid10_size(mddev, 0, 0);
4281 if (size < mddev->array_sectors) {
4282 spin_unlock_irq(&conf->device_lock);
4283 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4284 mdname(mddev));
4285 return -EINVAL;
4287 mddev->resync_max_sectors = size;
4288 conf->reshape_progress = size;
4289 } else
4290 conf->reshape_progress = 0;
4291 conf->reshape_safe = conf->reshape_progress;
4292 spin_unlock_irq(&conf->device_lock);
4294 if (mddev->delta_disks && mddev->bitmap) {
4295 ret = md_bitmap_resize(mddev->bitmap,
4296 raid10_size(mddev, 0, conf->geo.raid_disks),
4297 0, 0);
4298 if (ret)
4299 goto abort;
4301 if (mddev->delta_disks > 0) {
4302 rdev_for_each(rdev, mddev)
4303 if (rdev->raid_disk < 0 &&
4304 !test_bit(Faulty, &rdev->flags)) {
4305 if (raid10_add_disk(mddev, rdev) == 0) {
4306 if (rdev->raid_disk >=
4307 conf->prev.raid_disks)
4308 set_bit(In_sync, &rdev->flags);
4309 else
4310 rdev->recovery_offset = 0;
4312 if (sysfs_link_rdev(mddev, rdev))
4313 /* Failure here is OK */;
4315 } else if (rdev->raid_disk >= conf->prev.raid_disks
4316 && !test_bit(Faulty, &rdev->flags)) {
4317 /* This is a spare that was manually added */
4318 set_bit(In_sync, &rdev->flags);
4321 /* When a reshape changes the number of devices,
4322 * ->degraded is measured against the larger of the
4323 * pre and post numbers.
4325 spin_lock_irq(&conf->device_lock);
4326 mddev->degraded = calc_degraded(conf);
4327 spin_unlock_irq(&conf->device_lock);
4328 mddev->raid_disks = conf->geo.raid_disks;
4329 mddev->reshape_position = conf->reshape_progress;
4330 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
4332 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4333 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4334 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
4335 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4336 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4338 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4339 "reshape");
4340 if (!mddev->sync_thread) {
4341 ret = -EAGAIN;
4342 goto abort;
4344 conf->reshape_checkpoint = jiffies;
4345 md_wakeup_thread(mddev->sync_thread);
4346 md_new_event(mddev);
4347 return 0;
4349 abort:
4350 mddev->recovery = 0;
4351 spin_lock_irq(&conf->device_lock);
4352 conf->geo = conf->prev;
4353 mddev->raid_disks = conf->geo.raid_disks;
4354 rdev_for_each(rdev, mddev)
4355 rdev->new_data_offset = rdev->data_offset;
4356 smp_wmb();
4357 conf->reshape_progress = MaxSector;
4358 conf->reshape_safe = MaxSector;
4359 mddev->reshape_position = MaxSector;
4360 spin_unlock_irq(&conf->device_lock);
4361 return ret;
4364 /* Calculate the last device-address that could contain
4365 * any block from the chunk that includes the array-address 's'
4366 * and report the next address.
4367 * i.e. the address returned will be chunk-aligned and after
4368 * any data that is in the chunk containing 's'.
4370 static sector_t last_dev_address(sector_t s, struct geom *geo)
4372 s = (s | geo->chunk_mask) + 1;
4373 s >>= geo->chunk_shift;
4374 s *= geo->near_copies;
4375 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4376 s *= geo->far_copies;
4377 s <<= geo->chunk_shift;
4378 return s;
4381 /* Calculate the first device-address that could contain
4382 * any block from the chunk that includes the array-address 's'.
4383 * This too will be the start of a chunk
4385 static sector_t first_dev_address(sector_t s, struct geom *geo)
4387 s >>= geo->chunk_shift;
4388 s *= geo->near_copies;
4389 sector_div(s, geo->raid_disks);
4390 s *= geo->far_copies;
4391 s <<= geo->chunk_shift;
4392 return s;
4395 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4396 int *skipped)
4398 /* We simply copy at most one chunk (smallest of old and new)
4399 * at a time, possibly less if that exceeds RESYNC_PAGES,
4400 * or we hit a bad block or something.
4401 * This might mean we pause for normal IO in the middle of
4402 * a chunk, but that is not a problem as mddev->reshape_position
4403 * can record any location.
4405 * If we will want to write to a location that isn't
4406 * yet recorded as 'safe' (i.e. in metadata on disk) then
4407 * we need to flush all reshape requests and update the metadata.
4409 * When reshaping forwards (e.g. to more devices), we interpret
4410 * 'safe' as the earliest block which might not have been copied
4411 * down yet. We divide this by previous stripe size and multiply
4412 * by previous stripe length to get lowest device offset that we
4413 * cannot write to yet.
4414 * We interpret 'sector_nr' as an address that we want to write to.
4415 * From this we use last_device_address() to find where we might
4416 * write to, and first_device_address on the 'safe' position.
4417 * If this 'next' write position is after the 'safe' position,
4418 * we must update the metadata to increase the 'safe' position.
4420 * When reshaping backwards, we round in the opposite direction
4421 * and perform the reverse test: next write position must not be
4422 * less than current safe position.
4424 * In all this the minimum difference in data offsets
4425 * (conf->offset_diff - always positive) allows a bit of slack,
4426 * so next can be after 'safe', but not by more than offset_diff
4428 * We need to prepare all the bios here before we start any IO
4429 * to ensure the size we choose is acceptable to all devices.
4430 * The means one for each copy for write-out and an extra one for
4431 * read-in.
4432 * We store the read-in bio in ->master_bio and the others in
4433 * ->devs[x].bio and ->devs[x].repl_bio.
4435 struct r10conf *conf = mddev->private;
4436 struct r10bio *r10_bio;
4437 sector_t next, safe, last;
4438 int max_sectors;
4439 int nr_sectors;
4440 int s;
4441 struct md_rdev *rdev;
4442 int need_flush = 0;
4443 struct bio *blist;
4444 struct bio *bio, *read_bio;
4445 int sectors_done = 0;
4446 struct page **pages;
4448 if (sector_nr == 0) {
4449 /* If restarting in the middle, skip the initial sectors */
4450 if (mddev->reshape_backwards &&
4451 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4452 sector_nr = (raid10_size(mddev, 0, 0)
4453 - conf->reshape_progress);
4454 } else if (!mddev->reshape_backwards &&
4455 conf->reshape_progress > 0)
4456 sector_nr = conf->reshape_progress;
4457 if (sector_nr) {
4458 mddev->curr_resync_completed = sector_nr;
4459 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4460 *skipped = 1;
4461 return sector_nr;
4465 /* We don't use sector_nr to track where we are up to
4466 * as that doesn't work well for ->reshape_backwards.
4467 * So just use ->reshape_progress.
4469 if (mddev->reshape_backwards) {
4470 /* 'next' is the earliest device address that we might
4471 * write to for this chunk in the new layout
4473 next = first_dev_address(conf->reshape_progress - 1,
4474 &conf->geo);
4476 /* 'safe' is the last device address that we might read from
4477 * in the old layout after a restart
4479 safe = last_dev_address(conf->reshape_safe - 1,
4480 &conf->prev);
4482 if (next + conf->offset_diff < safe)
4483 need_flush = 1;
4485 last = conf->reshape_progress - 1;
4486 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4487 & conf->prev.chunk_mask);
4488 if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
4489 sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
4490 } else {
4491 /* 'next' is after the last device address that we
4492 * might write to for this chunk in the new layout
4494 next = last_dev_address(conf->reshape_progress, &conf->geo);
4496 /* 'safe' is the earliest device address that we might
4497 * read from in the old layout after a restart
4499 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4501 /* Need to update metadata if 'next' might be beyond 'safe'
4502 * as that would possibly corrupt data
4504 if (next > safe + conf->offset_diff)
4505 need_flush = 1;
4507 sector_nr = conf->reshape_progress;
4508 last = sector_nr | (conf->geo.chunk_mask
4509 & conf->prev.chunk_mask);
4511 if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
4512 last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
4515 if (need_flush ||
4516 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4517 /* Need to update reshape_position in metadata */
4518 wait_barrier(conf);
4519 mddev->reshape_position = conf->reshape_progress;
4520 if (mddev->reshape_backwards)
4521 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4522 - conf->reshape_progress;
4523 else
4524 mddev->curr_resync_completed = conf->reshape_progress;
4525 conf->reshape_checkpoint = jiffies;
4526 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
4527 md_wakeup_thread(mddev->thread);
4528 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
4529 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4530 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4531 allow_barrier(conf);
4532 return sectors_done;
4534 conf->reshape_safe = mddev->reshape_position;
4535 allow_barrier(conf);
4538 raise_barrier(conf, 0);
4539 read_more:
4540 /* Now schedule reads for blocks from sector_nr to last */
4541 r10_bio = raid10_alloc_init_r10buf(conf);
4542 r10_bio->state = 0;
4543 raise_barrier(conf, 1);
4544 atomic_set(&r10_bio->remaining, 0);
4545 r10_bio->mddev = mddev;
4546 r10_bio->sector = sector_nr;
4547 set_bit(R10BIO_IsReshape, &r10_bio->state);
4548 r10_bio->sectors = last - sector_nr + 1;
4549 rdev = read_balance(conf, r10_bio, &max_sectors);
4550 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4552 if (!rdev) {
4553 /* Cannot read from here, so need to record bad blocks
4554 * on all the target devices.
4556 // FIXME
4557 mempool_free(r10_bio, &conf->r10buf_pool);
4558 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4559 return sectors_done;
4562 read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
4564 bio_set_dev(read_bio, rdev->bdev);
4565 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
4566 + rdev->data_offset);
4567 read_bio->bi_private = r10_bio;
4568 read_bio->bi_end_io = end_reshape_read;
4569 bio_set_op_attrs(read_bio, REQ_OP_READ, 0);
4570 read_bio->bi_flags &= (~0UL << BIO_RESET_BITS);
4571 read_bio->bi_status = 0;
4572 read_bio->bi_vcnt = 0;
4573 read_bio->bi_iter.bi_size = 0;
4574 r10_bio->master_bio = read_bio;
4575 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4577 /* Now find the locations in the new layout */
4578 __raid10_find_phys(&conf->geo, r10_bio);
4580 blist = read_bio;
4581 read_bio->bi_next = NULL;
4583 rcu_read_lock();
4584 for (s = 0; s < conf->copies*2; s++) {
4585 struct bio *b;
4586 int d = r10_bio->devs[s/2].devnum;
4587 struct md_rdev *rdev2;
4588 if (s&1) {
4589 rdev2 = rcu_dereference(conf->mirrors[d].replacement);
4590 b = r10_bio->devs[s/2].repl_bio;
4591 } else {
4592 rdev2 = rcu_dereference(conf->mirrors[d].rdev);
4593 b = r10_bio->devs[s/2].bio;
4595 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4596 continue;
4598 bio_set_dev(b, rdev2->bdev);
4599 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr +
4600 rdev2->new_data_offset;
4601 b->bi_end_io = end_reshape_write;
4602 bio_set_op_attrs(b, REQ_OP_WRITE, 0);
4603 b->bi_next = blist;
4604 blist = b;
4607 /* Now add as many pages as possible to all of these bios. */
4609 nr_sectors = 0;
4610 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
4611 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
4612 struct page *page = pages[s / (PAGE_SIZE >> 9)];
4613 int len = (max_sectors - s) << 9;
4614 if (len > PAGE_SIZE)
4615 len = PAGE_SIZE;
4616 for (bio = blist; bio ; bio = bio->bi_next) {
4618 * won't fail because the vec table is big enough
4619 * to hold all these pages
4621 bio_add_page(bio, page, len, 0);
4623 sector_nr += len >> 9;
4624 nr_sectors += len >> 9;
4626 rcu_read_unlock();
4627 r10_bio->sectors = nr_sectors;
4629 /* Now submit the read */
4630 md_sync_acct_bio(read_bio, r10_bio->sectors);
4631 atomic_inc(&r10_bio->remaining);
4632 read_bio->bi_next = NULL;
4633 generic_make_request(read_bio);
4634 sectors_done += nr_sectors;
4635 if (sector_nr <= last)
4636 goto read_more;
4638 lower_barrier(conf);
4640 /* Now that we have done the whole section we can
4641 * update reshape_progress
4643 if (mddev->reshape_backwards)
4644 conf->reshape_progress -= sectors_done;
4645 else
4646 conf->reshape_progress += sectors_done;
4648 return sectors_done;
4651 static void end_reshape_request(struct r10bio *r10_bio);
4652 static int handle_reshape_read_error(struct mddev *mddev,
4653 struct r10bio *r10_bio);
4654 static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
4656 /* Reshape read completed. Hopefully we have a block
4657 * to write out.
4658 * If we got a read error then we do sync 1-page reads from
4659 * elsewhere until we find the data - or give up.
4661 struct r10conf *conf = mddev->private;
4662 int s;
4664 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
4665 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
4666 /* Reshape has been aborted */
4667 md_done_sync(mddev, r10_bio->sectors, 0);
4668 return;
4671 /* We definitely have the data in the pages, schedule the
4672 * writes.
4674 atomic_set(&r10_bio->remaining, 1);
4675 for (s = 0; s < conf->copies*2; s++) {
4676 struct bio *b;
4677 int d = r10_bio->devs[s/2].devnum;
4678 struct md_rdev *rdev;
4679 rcu_read_lock();
4680 if (s&1) {
4681 rdev = rcu_dereference(conf->mirrors[d].replacement);
4682 b = r10_bio->devs[s/2].repl_bio;
4683 } else {
4684 rdev = rcu_dereference(conf->mirrors[d].rdev);
4685 b = r10_bio->devs[s/2].bio;
4687 if (!rdev || test_bit(Faulty, &rdev->flags)) {
4688 rcu_read_unlock();
4689 continue;
4691 atomic_inc(&rdev->nr_pending);
4692 rcu_read_unlock();
4693 md_sync_acct_bio(b, r10_bio->sectors);
4694 atomic_inc(&r10_bio->remaining);
4695 b->bi_next = NULL;
4696 generic_make_request(b);
4698 end_reshape_request(r10_bio);
4701 static void end_reshape(struct r10conf *conf)
4703 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
4704 return;
4706 spin_lock_irq(&conf->device_lock);
4707 conf->prev = conf->geo;
4708 md_finish_reshape(conf->mddev);
4709 smp_wmb();
4710 conf->reshape_progress = MaxSector;
4711 conf->reshape_safe = MaxSector;
4712 spin_unlock_irq(&conf->device_lock);
4714 /* read-ahead size must cover two whole stripes, which is
4715 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4717 if (conf->mddev->queue) {
4718 int stripe = conf->geo.raid_disks *
4719 ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
4720 stripe /= conf->geo.near_copies;
4721 if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
4722 conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
4724 conf->fullsync = 0;
4727 static int handle_reshape_read_error(struct mddev *mddev,
4728 struct r10bio *r10_bio)
4730 /* Use sync reads to get the blocks from somewhere else */
4731 int sectors = r10_bio->sectors;
4732 struct r10conf *conf = mddev->private;
4733 struct r10bio *r10b;
4734 int slot = 0;
4735 int idx = 0;
4736 struct page **pages;
4738 r10b = kmalloc(sizeof(*r10b) +
4739 sizeof(struct r10dev) * conf->copies, GFP_NOIO);
4740 if (!r10b) {
4741 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4742 return -ENOMEM;
4745 /* reshape IOs share pages from .devs[0].bio */
4746 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
4748 r10b->sector = r10_bio->sector;
4749 __raid10_find_phys(&conf->prev, r10b);
4751 while (sectors) {
4752 int s = sectors;
4753 int success = 0;
4754 int first_slot = slot;
4756 if (s > (PAGE_SIZE >> 9))
4757 s = PAGE_SIZE >> 9;
4759 rcu_read_lock();
4760 while (!success) {
4761 int d = r10b->devs[slot].devnum;
4762 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
4763 sector_t addr;
4764 if (rdev == NULL ||
4765 test_bit(Faulty, &rdev->flags) ||
4766 !test_bit(In_sync, &rdev->flags))
4767 goto failed;
4769 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
4770 atomic_inc(&rdev->nr_pending);
4771 rcu_read_unlock();
4772 success = sync_page_io(rdev,
4773 addr,
4774 s << 9,
4775 pages[idx],
4776 REQ_OP_READ, 0, false);
4777 rdev_dec_pending(rdev, mddev);
4778 rcu_read_lock();
4779 if (success)
4780 break;
4781 failed:
4782 slot++;
4783 if (slot >= conf->copies)
4784 slot = 0;
4785 if (slot == first_slot)
4786 break;
4788 rcu_read_unlock();
4789 if (!success) {
4790 /* couldn't read this block, must give up */
4791 set_bit(MD_RECOVERY_INTR,
4792 &mddev->recovery);
4793 kfree(r10b);
4794 return -EIO;
4796 sectors -= s;
4797 idx++;
4799 kfree(r10b);
4800 return 0;
4803 static void end_reshape_write(struct bio *bio)
4805 struct r10bio *r10_bio = get_resync_r10bio(bio);
4806 struct mddev *mddev = r10_bio->mddev;
4807 struct r10conf *conf = mddev->private;
4808 int d;
4809 int slot;
4810 int repl;
4811 struct md_rdev *rdev = NULL;
4813 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
4814 if (repl)
4815 rdev = conf->mirrors[d].replacement;
4816 if (!rdev) {
4817 smp_mb();
4818 rdev = conf->mirrors[d].rdev;
4821 if (bio->bi_status) {
4822 /* FIXME should record badblock */
4823 md_error(mddev, rdev);
4826 rdev_dec_pending(rdev, mddev);
4827 end_reshape_request(r10_bio);
4830 static void end_reshape_request(struct r10bio *r10_bio)
4832 if (!atomic_dec_and_test(&r10_bio->remaining))
4833 return;
4834 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
4835 bio_put(r10_bio->master_bio);
4836 put_buf(r10_bio);
4839 static void raid10_finish_reshape(struct mddev *mddev)
4841 struct r10conf *conf = mddev->private;
4843 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4844 return;
4846 if (mddev->delta_disks > 0) {
4847 if (mddev->recovery_cp > mddev->resync_max_sectors) {
4848 mddev->recovery_cp = mddev->resync_max_sectors;
4849 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4851 mddev->resync_max_sectors = mddev->array_sectors;
4852 } else {
4853 int d;
4854 rcu_read_lock();
4855 for (d = conf->geo.raid_disks ;
4856 d < conf->geo.raid_disks - mddev->delta_disks;
4857 d++) {
4858 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
4859 if (rdev)
4860 clear_bit(In_sync, &rdev->flags);
4861 rdev = rcu_dereference(conf->mirrors[d].replacement);
4862 if (rdev)
4863 clear_bit(In_sync, &rdev->flags);
4865 rcu_read_unlock();
4867 mddev->layout = mddev->new_layout;
4868 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
4869 mddev->reshape_position = MaxSector;
4870 mddev->delta_disks = 0;
4871 mddev->reshape_backwards = 0;
4874 static struct md_personality raid10_personality =
4876 .name = "raid10",
4877 .level = 10,
4878 .owner = THIS_MODULE,
4879 .make_request = raid10_make_request,
4880 .run = raid10_run,
4881 .free = raid10_free,
4882 .status = raid10_status,
4883 .error_handler = raid10_error,
4884 .hot_add_disk = raid10_add_disk,
4885 .hot_remove_disk= raid10_remove_disk,
4886 .spare_active = raid10_spare_active,
4887 .sync_request = raid10_sync_request,
4888 .quiesce = raid10_quiesce,
4889 .size = raid10_size,
4890 .resize = raid10_resize,
4891 .takeover = raid10_takeover,
4892 .check_reshape = raid10_check_reshape,
4893 .start_reshape = raid10_start_reshape,
4894 .finish_reshape = raid10_finish_reshape,
4895 .congested = raid10_congested,
4898 static int __init raid_init(void)
4900 return register_md_personality(&raid10_personality);
4903 static void raid_exit(void)
4905 unregister_md_personality(&raid10_personality);
4908 module_init(raid_init);
4909 module_exit(raid_exit);
4910 MODULE_LICENSE("GPL");
4911 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
4912 MODULE_ALIAS("md-personality-9"); /* RAID10 */
4913 MODULE_ALIAS("md-raid10");
4914 MODULE_ALIAS("md-level-10");
4916 module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);