uapi/if_ether.h: move __UAPI_DEF_ETHHDR libc define
[linux/fpc-iii.git] / drivers / md / raid10.c
blob2ce079a0b0bdbc286bfe94e5dd927dbbe1ee30a9
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 "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 /* amount of memory to reserve for resync requests */
140 #define RESYNC_WINDOW (1024*1024)
141 /* maximum number of concurrent requests, memory permitting */
142 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
145 * When performing a resync, we need to read and compare, so
146 * we need as many pages are there are copies.
147 * When performing a recovery, we need 2 bios, one for read,
148 * one for write (we recover only one drive per r10buf)
151 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
153 struct r10conf *conf = data;
154 struct r10bio *r10_bio;
155 struct bio *bio;
156 int j;
157 int nalloc, nalloc_rp;
158 struct resync_pages *rps;
160 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
161 if (!r10_bio)
162 return NULL;
164 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
165 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
166 nalloc = conf->copies; /* resync */
167 else
168 nalloc = 2; /* recovery */
170 /* allocate once for all bios */
171 if (!conf->have_replacement)
172 nalloc_rp = nalloc;
173 else
174 nalloc_rp = nalloc * 2;
175 rps = kmalloc(sizeof(struct resync_pages) * nalloc_rp, gfp_flags);
176 if (!rps)
177 goto out_free_r10bio;
180 * Allocate bios.
182 for (j = nalloc ; j-- ; ) {
183 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
184 if (!bio)
185 goto out_free_bio;
186 r10_bio->devs[j].bio = bio;
187 if (!conf->have_replacement)
188 continue;
189 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
190 if (!bio)
191 goto out_free_bio;
192 r10_bio->devs[j].repl_bio = bio;
195 * Allocate RESYNC_PAGES data pages and attach them
196 * where needed.
198 for (j = 0; j < nalloc; j++) {
199 struct bio *rbio = r10_bio->devs[j].repl_bio;
200 struct resync_pages *rp, *rp_repl;
202 rp = &rps[j];
203 if (rbio)
204 rp_repl = &rps[nalloc + j];
206 bio = r10_bio->devs[j].bio;
208 if (!j || test_bit(MD_RECOVERY_SYNC,
209 &conf->mddev->recovery)) {
210 if (resync_alloc_pages(rp, gfp_flags))
211 goto out_free_pages;
212 } else {
213 memcpy(rp, &rps[0], sizeof(*rp));
214 resync_get_all_pages(rp);
217 rp->raid_bio = r10_bio;
218 bio->bi_private = rp;
219 if (rbio) {
220 memcpy(rp_repl, rp, sizeof(*rp));
221 rbio->bi_private = rp_repl;
225 return r10_bio;
227 out_free_pages:
228 while (--j >= 0)
229 resync_free_pages(&rps[j * 2]);
231 j = 0;
232 out_free_bio:
233 for ( ; j < nalloc; j++) {
234 if (r10_bio->devs[j].bio)
235 bio_put(r10_bio->devs[j].bio);
236 if (r10_bio->devs[j].repl_bio)
237 bio_put(r10_bio->devs[j].repl_bio);
239 kfree(rps);
240 out_free_r10bio:
241 r10bio_pool_free(r10_bio, conf);
242 return NULL;
245 static void r10buf_pool_free(void *__r10_bio, void *data)
247 struct r10conf *conf = data;
248 struct r10bio *r10bio = __r10_bio;
249 int j;
250 struct resync_pages *rp = NULL;
252 for (j = conf->copies; j--; ) {
253 struct bio *bio = r10bio->devs[j].bio;
255 rp = get_resync_pages(bio);
256 resync_free_pages(rp);
257 bio_put(bio);
259 bio = r10bio->devs[j].repl_bio;
260 if (bio)
261 bio_put(bio);
264 /* resync pages array stored in the 1st bio's .bi_private */
265 kfree(rp);
267 r10bio_pool_free(r10bio, conf);
270 static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
272 int i;
274 for (i = 0; i < conf->copies; i++) {
275 struct bio **bio = & r10_bio->devs[i].bio;
276 if (!BIO_SPECIAL(*bio))
277 bio_put(*bio);
278 *bio = NULL;
279 bio = &r10_bio->devs[i].repl_bio;
280 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
281 bio_put(*bio);
282 *bio = NULL;
286 static void free_r10bio(struct r10bio *r10_bio)
288 struct r10conf *conf = r10_bio->mddev->private;
290 put_all_bios(conf, r10_bio);
291 mempool_free(r10_bio, conf->r10bio_pool);
294 static void put_buf(struct r10bio *r10_bio)
296 struct r10conf *conf = r10_bio->mddev->private;
298 mempool_free(r10_bio, conf->r10buf_pool);
300 lower_barrier(conf);
303 static void reschedule_retry(struct r10bio *r10_bio)
305 unsigned long flags;
306 struct mddev *mddev = r10_bio->mddev;
307 struct r10conf *conf = mddev->private;
309 spin_lock_irqsave(&conf->device_lock, flags);
310 list_add(&r10_bio->retry_list, &conf->retry_list);
311 conf->nr_queued ++;
312 spin_unlock_irqrestore(&conf->device_lock, flags);
314 /* wake up frozen array... */
315 wake_up(&conf->wait_barrier);
317 md_wakeup_thread(mddev->thread);
321 * raid_end_bio_io() is called when we have finished servicing a mirrored
322 * operation and are ready to return a success/failure code to the buffer
323 * cache layer.
325 static void raid_end_bio_io(struct r10bio *r10_bio)
327 struct bio *bio = r10_bio->master_bio;
328 struct r10conf *conf = r10_bio->mddev->private;
330 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
331 bio->bi_status = BLK_STS_IOERR;
333 bio_endio(bio);
335 * Wake up any possible resync thread that waits for the device
336 * to go idle.
338 allow_barrier(conf);
340 free_r10bio(r10_bio);
344 * Update disk head position estimator based on IRQ completion info.
346 static inline void update_head_pos(int slot, struct r10bio *r10_bio)
348 struct r10conf *conf = r10_bio->mddev->private;
350 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
351 r10_bio->devs[slot].addr + (r10_bio->sectors);
355 * Find the disk number which triggered given bio
357 static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
358 struct bio *bio, int *slotp, int *replp)
360 int slot;
361 int repl = 0;
363 for (slot = 0; slot < conf->copies; slot++) {
364 if (r10_bio->devs[slot].bio == bio)
365 break;
366 if (r10_bio->devs[slot].repl_bio == bio) {
367 repl = 1;
368 break;
372 BUG_ON(slot == conf->copies);
373 update_head_pos(slot, r10_bio);
375 if (slotp)
376 *slotp = slot;
377 if (replp)
378 *replp = repl;
379 return r10_bio->devs[slot].devnum;
382 static void raid10_end_read_request(struct bio *bio)
384 int uptodate = !bio->bi_status;
385 struct r10bio *r10_bio = bio->bi_private;
386 int slot, dev;
387 struct md_rdev *rdev;
388 struct r10conf *conf = r10_bio->mddev->private;
390 slot = r10_bio->read_slot;
391 dev = r10_bio->devs[slot].devnum;
392 rdev = r10_bio->devs[slot].rdev;
394 * this branch is our 'one mirror IO has finished' event handler:
396 update_head_pos(slot, r10_bio);
398 if (uptodate) {
400 * Set R10BIO_Uptodate in our master bio, so that
401 * we will return a good error code to the higher
402 * levels even if IO on some other mirrored buffer fails.
404 * The 'master' represents the composite IO operation to
405 * user-side. So if something waits for IO, then it will
406 * wait for the 'master' bio.
408 set_bit(R10BIO_Uptodate, &r10_bio->state);
409 } else {
410 /* If all other devices that store this block have
411 * failed, we want to return the error upwards rather
412 * than fail the last device. Here we redefine
413 * "uptodate" to mean "Don't want to retry"
415 if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state),
416 rdev->raid_disk))
417 uptodate = 1;
419 if (uptodate) {
420 raid_end_bio_io(r10_bio);
421 rdev_dec_pending(rdev, conf->mddev);
422 } else {
424 * oops, read error - keep the refcount on the rdev
426 char b[BDEVNAME_SIZE];
427 pr_err_ratelimited("md/raid10:%s: %s: rescheduling sector %llu\n",
428 mdname(conf->mddev),
429 bdevname(rdev->bdev, b),
430 (unsigned long long)r10_bio->sector);
431 set_bit(R10BIO_ReadError, &r10_bio->state);
432 reschedule_retry(r10_bio);
436 static void close_write(struct r10bio *r10_bio)
438 /* clear the bitmap if all writes complete successfully */
439 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
440 r10_bio->sectors,
441 !test_bit(R10BIO_Degraded, &r10_bio->state),
443 md_write_end(r10_bio->mddev);
446 static void one_write_done(struct r10bio *r10_bio)
448 if (atomic_dec_and_test(&r10_bio->remaining)) {
449 if (test_bit(R10BIO_WriteError, &r10_bio->state))
450 reschedule_retry(r10_bio);
451 else {
452 close_write(r10_bio);
453 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
454 reschedule_retry(r10_bio);
455 else
456 raid_end_bio_io(r10_bio);
461 static void raid10_end_write_request(struct bio *bio)
463 struct r10bio *r10_bio = bio->bi_private;
464 int dev;
465 int dec_rdev = 1;
466 struct r10conf *conf = r10_bio->mddev->private;
467 int slot, repl;
468 struct md_rdev *rdev = NULL;
469 struct bio *to_put = NULL;
470 bool discard_error;
472 discard_error = bio->bi_status && bio_op(bio) == REQ_OP_DISCARD;
474 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
476 if (repl)
477 rdev = conf->mirrors[dev].replacement;
478 if (!rdev) {
479 smp_rmb();
480 repl = 0;
481 rdev = conf->mirrors[dev].rdev;
484 * this branch is our 'one mirror IO has finished' event handler:
486 if (bio->bi_status && !discard_error) {
487 if (repl)
488 /* Never record new bad blocks to replacement,
489 * just fail it.
491 md_error(rdev->mddev, rdev);
492 else {
493 set_bit(WriteErrorSeen, &rdev->flags);
494 if (!test_and_set_bit(WantReplacement, &rdev->flags))
495 set_bit(MD_RECOVERY_NEEDED,
496 &rdev->mddev->recovery);
498 dec_rdev = 0;
499 if (test_bit(FailFast, &rdev->flags) &&
500 (bio->bi_opf & MD_FAILFAST)) {
501 md_error(rdev->mddev, rdev);
502 if (!test_bit(Faulty, &rdev->flags))
503 /* This is the only remaining device,
504 * We need to retry the write without
505 * FailFast
507 set_bit(R10BIO_WriteError, &r10_bio->state);
508 else {
509 r10_bio->devs[slot].bio = NULL;
510 to_put = bio;
511 dec_rdev = 1;
513 } else
514 set_bit(R10BIO_WriteError, &r10_bio->state);
516 } else {
518 * Set R10BIO_Uptodate in our master bio, so that
519 * we will return a good error code for to the higher
520 * levels even if IO on some other mirrored buffer fails.
522 * The 'master' represents the composite IO operation to
523 * user-side. So if something waits for IO, then it will
524 * wait for the 'master' bio.
526 sector_t first_bad;
527 int bad_sectors;
530 * Do not set R10BIO_Uptodate if the current device is
531 * rebuilding or Faulty. This is because we cannot use
532 * such device for properly reading the data back (we could
533 * potentially use it, if the current write would have felt
534 * before rdev->recovery_offset, but for simplicity we don't
535 * check this here.
537 if (test_bit(In_sync, &rdev->flags) &&
538 !test_bit(Faulty, &rdev->flags))
539 set_bit(R10BIO_Uptodate, &r10_bio->state);
541 /* Maybe we can clear some bad blocks. */
542 if (is_badblock(rdev,
543 r10_bio->devs[slot].addr,
544 r10_bio->sectors,
545 &first_bad, &bad_sectors) && !discard_error) {
546 bio_put(bio);
547 if (repl)
548 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
549 else
550 r10_bio->devs[slot].bio = IO_MADE_GOOD;
551 dec_rdev = 0;
552 set_bit(R10BIO_MadeGood, &r10_bio->state);
558 * Let's see if all mirrored write operations have finished
559 * already.
561 one_write_done(r10_bio);
562 if (dec_rdev)
563 rdev_dec_pending(rdev, conf->mddev);
564 if (to_put)
565 bio_put(to_put);
569 * RAID10 layout manager
570 * As well as the chunksize and raid_disks count, there are two
571 * parameters: near_copies and far_copies.
572 * near_copies * far_copies must be <= raid_disks.
573 * Normally one of these will be 1.
574 * If both are 1, we get raid0.
575 * If near_copies == raid_disks, we get raid1.
577 * Chunks are laid out in raid0 style with near_copies copies of the
578 * first chunk, followed by near_copies copies of the next chunk and
579 * so on.
580 * If far_copies > 1, then after 1/far_copies of the array has been assigned
581 * as described above, we start again with a device offset of near_copies.
582 * So we effectively have another copy of the whole array further down all
583 * the drives, but with blocks on different drives.
584 * With this layout, and block is never stored twice on the one device.
586 * raid10_find_phys finds the sector offset of a given virtual sector
587 * on each device that it is on.
589 * raid10_find_virt does the reverse mapping, from a device and a
590 * sector offset to a virtual address
593 static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
595 int n,f;
596 sector_t sector;
597 sector_t chunk;
598 sector_t stripe;
599 int dev;
600 int slot = 0;
601 int last_far_set_start, last_far_set_size;
603 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
604 last_far_set_start *= geo->far_set_size;
606 last_far_set_size = geo->far_set_size;
607 last_far_set_size += (geo->raid_disks % geo->far_set_size);
609 /* now calculate first sector/dev */
610 chunk = r10bio->sector >> geo->chunk_shift;
611 sector = r10bio->sector & geo->chunk_mask;
613 chunk *= geo->near_copies;
614 stripe = chunk;
615 dev = sector_div(stripe, geo->raid_disks);
616 if (geo->far_offset)
617 stripe *= geo->far_copies;
619 sector += stripe << geo->chunk_shift;
621 /* and calculate all the others */
622 for (n = 0; n < geo->near_copies; n++) {
623 int d = dev;
624 int set;
625 sector_t s = sector;
626 r10bio->devs[slot].devnum = d;
627 r10bio->devs[slot].addr = s;
628 slot++;
630 for (f = 1; f < geo->far_copies; f++) {
631 set = d / geo->far_set_size;
632 d += geo->near_copies;
634 if ((geo->raid_disks % geo->far_set_size) &&
635 (d > last_far_set_start)) {
636 d -= last_far_set_start;
637 d %= last_far_set_size;
638 d += last_far_set_start;
639 } else {
640 d %= geo->far_set_size;
641 d += geo->far_set_size * set;
643 s += geo->stride;
644 r10bio->devs[slot].devnum = d;
645 r10bio->devs[slot].addr = s;
646 slot++;
648 dev++;
649 if (dev >= geo->raid_disks) {
650 dev = 0;
651 sector += (geo->chunk_mask + 1);
656 static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
658 struct geom *geo = &conf->geo;
660 if (conf->reshape_progress != MaxSector &&
661 ((r10bio->sector >= conf->reshape_progress) !=
662 conf->mddev->reshape_backwards)) {
663 set_bit(R10BIO_Previous, &r10bio->state);
664 geo = &conf->prev;
665 } else
666 clear_bit(R10BIO_Previous, &r10bio->state);
668 __raid10_find_phys(geo, r10bio);
671 static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
673 sector_t offset, chunk, vchunk;
674 /* Never use conf->prev as this is only called during resync
675 * or recovery, so reshape isn't happening
677 struct geom *geo = &conf->geo;
678 int far_set_start = (dev / geo->far_set_size) * geo->far_set_size;
679 int far_set_size = geo->far_set_size;
680 int last_far_set_start;
682 if (geo->raid_disks % geo->far_set_size) {
683 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
684 last_far_set_start *= geo->far_set_size;
686 if (dev >= last_far_set_start) {
687 far_set_size = geo->far_set_size;
688 far_set_size += (geo->raid_disks % geo->far_set_size);
689 far_set_start = last_far_set_start;
693 offset = sector & geo->chunk_mask;
694 if (geo->far_offset) {
695 int fc;
696 chunk = sector >> geo->chunk_shift;
697 fc = sector_div(chunk, geo->far_copies);
698 dev -= fc * geo->near_copies;
699 if (dev < far_set_start)
700 dev += far_set_size;
701 } else {
702 while (sector >= geo->stride) {
703 sector -= geo->stride;
704 if (dev < (geo->near_copies + far_set_start))
705 dev += far_set_size - geo->near_copies;
706 else
707 dev -= geo->near_copies;
709 chunk = sector >> geo->chunk_shift;
711 vchunk = chunk * geo->raid_disks + dev;
712 sector_div(vchunk, geo->near_copies);
713 return (vchunk << geo->chunk_shift) + offset;
717 * This routine returns the disk from which the requested read should
718 * be done. There is a per-array 'next expected sequential IO' sector
719 * number - if this matches on the next IO then we use the last disk.
720 * There is also a per-disk 'last know head position' sector that is
721 * maintained from IRQ contexts, both the normal and the resync IO
722 * completion handlers update this position correctly. If there is no
723 * perfect sequential match then we pick the disk whose head is closest.
725 * If there are 2 mirrors in the same 2 devices, performance degrades
726 * because position is mirror, not device based.
728 * The rdev for the device selected will have nr_pending incremented.
732 * FIXME: possibly should rethink readbalancing and do it differently
733 * depending on near_copies / far_copies geometry.
735 static struct md_rdev *read_balance(struct r10conf *conf,
736 struct r10bio *r10_bio,
737 int *max_sectors)
739 const sector_t this_sector = r10_bio->sector;
740 int disk, slot;
741 int sectors = r10_bio->sectors;
742 int best_good_sectors;
743 sector_t new_distance, best_dist;
744 struct md_rdev *best_rdev, *rdev = NULL;
745 int do_balance;
746 int best_slot;
747 struct geom *geo = &conf->geo;
749 raid10_find_phys(conf, r10_bio);
750 rcu_read_lock();
751 sectors = r10_bio->sectors;
752 best_slot = -1;
753 best_rdev = NULL;
754 best_dist = MaxSector;
755 best_good_sectors = 0;
756 do_balance = 1;
757 clear_bit(R10BIO_FailFast, &r10_bio->state);
759 * Check if we can balance. We can balance on the whole
760 * device if no resync is going on (recovery is ok), or below
761 * the resync window. We take the first readable disk when
762 * above the resync window.
764 if (conf->mddev->recovery_cp < MaxSector
765 && (this_sector + sectors >= conf->next_resync))
766 do_balance = 0;
768 for (slot = 0; slot < conf->copies ; slot++) {
769 sector_t first_bad;
770 int bad_sectors;
771 sector_t dev_sector;
773 if (r10_bio->devs[slot].bio == IO_BLOCKED)
774 continue;
775 disk = r10_bio->devs[slot].devnum;
776 rdev = rcu_dereference(conf->mirrors[disk].replacement);
777 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
778 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
779 rdev = rcu_dereference(conf->mirrors[disk].rdev);
780 if (rdev == NULL ||
781 test_bit(Faulty, &rdev->flags))
782 continue;
783 if (!test_bit(In_sync, &rdev->flags) &&
784 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
785 continue;
787 dev_sector = r10_bio->devs[slot].addr;
788 if (is_badblock(rdev, dev_sector, sectors,
789 &first_bad, &bad_sectors)) {
790 if (best_dist < MaxSector)
791 /* Already have a better slot */
792 continue;
793 if (first_bad <= dev_sector) {
794 /* Cannot read here. If this is the
795 * 'primary' device, then we must not read
796 * beyond 'bad_sectors' from another device.
798 bad_sectors -= (dev_sector - first_bad);
799 if (!do_balance && sectors > bad_sectors)
800 sectors = bad_sectors;
801 if (best_good_sectors > sectors)
802 best_good_sectors = sectors;
803 } else {
804 sector_t good_sectors =
805 first_bad - dev_sector;
806 if (good_sectors > best_good_sectors) {
807 best_good_sectors = good_sectors;
808 best_slot = slot;
809 best_rdev = rdev;
811 if (!do_balance)
812 /* Must read from here */
813 break;
815 continue;
816 } else
817 best_good_sectors = sectors;
819 if (!do_balance)
820 break;
822 if (best_slot >= 0)
823 /* At least 2 disks to choose from so failfast is OK */
824 set_bit(R10BIO_FailFast, &r10_bio->state);
825 /* This optimisation is debatable, and completely destroys
826 * sequential read speed for 'far copies' arrays. So only
827 * keep it for 'near' arrays, and review those later.
829 if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending))
830 new_distance = 0;
832 /* for far > 1 always use the lowest address */
833 else if (geo->far_copies > 1)
834 new_distance = r10_bio->devs[slot].addr;
835 else
836 new_distance = abs(r10_bio->devs[slot].addr -
837 conf->mirrors[disk].head_position);
838 if (new_distance < best_dist) {
839 best_dist = new_distance;
840 best_slot = slot;
841 best_rdev = rdev;
844 if (slot >= conf->copies) {
845 slot = best_slot;
846 rdev = best_rdev;
849 if (slot >= 0) {
850 atomic_inc(&rdev->nr_pending);
851 r10_bio->read_slot = slot;
852 } else
853 rdev = NULL;
854 rcu_read_unlock();
855 *max_sectors = best_good_sectors;
857 return rdev;
860 static int raid10_congested(struct mddev *mddev, int bits)
862 struct r10conf *conf = mddev->private;
863 int i, ret = 0;
865 if ((bits & (1 << WB_async_congested)) &&
866 conf->pending_count >= max_queued_requests)
867 return 1;
869 rcu_read_lock();
870 for (i = 0;
871 (i < conf->geo.raid_disks || i < conf->prev.raid_disks)
872 && ret == 0;
873 i++) {
874 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
875 if (rdev && !test_bit(Faulty, &rdev->flags)) {
876 struct request_queue *q = bdev_get_queue(rdev->bdev);
878 ret |= bdi_congested(q->backing_dev_info, bits);
881 rcu_read_unlock();
882 return ret;
885 static void flush_pending_writes(struct r10conf *conf)
887 /* Any writes that have been queued but are awaiting
888 * bitmap updates get flushed here.
890 spin_lock_irq(&conf->device_lock);
892 if (conf->pending_bio_list.head) {
893 struct blk_plug plug;
894 struct bio *bio;
896 bio = bio_list_get(&conf->pending_bio_list);
897 conf->pending_count = 0;
898 spin_unlock_irq(&conf->device_lock);
899 blk_start_plug(&plug);
900 /* flush any pending bitmap writes to disk
901 * before proceeding w/ I/O */
902 bitmap_unplug(conf->mddev->bitmap);
903 wake_up(&conf->wait_barrier);
905 while (bio) { /* submit pending writes */
906 struct bio *next = bio->bi_next;
907 struct md_rdev *rdev = (void*)bio->bi_disk;
908 bio->bi_next = NULL;
909 bio_set_dev(bio, rdev->bdev);
910 if (test_bit(Faulty, &rdev->flags)) {
911 bio_io_error(bio);
912 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
913 !blk_queue_discard(bio->bi_disk->queue)))
914 /* Just ignore it */
915 bio_endio(bio);
916 else
917 generic_make_request(bio);
918 bio = next;
920 blk_finish_plug(&plug);
921 } else
922 spin_unlock_irq(&conf->device_lock);
925 /* Barriers....
926 * Sometimes we need to suspend IO while we do something else,
927 * either some resync/recovery, or reconfigure the array.
928 * To do this we raise a 'barrier'.
929 * The 'barrier' is a counter that can be raised multiple times
930 * to count how many activities are happening which preclude
931 * normal IO.
932 * We can only raise the barrier if there is no pending IO.
933 * i.e. if nr_pending == 0.
934 * We choose only to raise the barrier if no-one is waiting for the
935 * barrier to go down. This means that as soon as an IO request
936 * is ready, no other operations which require a barrier will start
937 * until the IO request has had a chance.
939 * So: regular IO calls 'wait_barrier'. When that returns there
940 * is no backgroup IO happening, It must arrange to call
941 * allow_barrier when it has finished its IO.
942 * backgroup IO calls must call raise_barrier. Once that returns
943 * there is no normal IO happeing. It must arrange to call
944 * lower_barrier when the particular background IO completes.
947 static void raise_barrier(struct r10conf *conf, int force)
949 BUG_ON(force && !conf->barrier);
950 spin_lock_irq(&conf->resync_lock);
952 /* Wait until no block IO is waiting (unless 'force') */
953 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
954 conf->resync_lock);
956 /* block any new IO from starting */
957 conf->barrier++;
959 /* Now wait for all pending IO to complete */
960 wait_event_lock_irq(conf->wait_barrier,
961 !atomic_read(&conf->nr_pending) && conf->barrier < RESYNC_DEPTH,
962 conf->resync_lock);
964 spin_unlock_irq(&conf->resync_lock);
967 static void lower_barrier(struct r10conf *conf)
969 unsigned long flags;
970 spin_lock_irqsave(&conf->resync_lock, flags);
971 conf->barrier--;
972 spin_unlock_irqrestore(&conf->resync_lock, flags);
973 wake_up(&conf->wait_barrier);
976 static void wait_barrier(struct r10conf *conf)
978 spin_lock_irq(&conf->resync_lock);
979 if (conf->barrier) {
980 conf->nr_waiting++;
981 /* Wait for the barrier to drop.
982 * However if there are already pending
983 * requests (preventing the barrier from
984 * rising completely), and the
985 * pre-process bio queue isn't empty,
986 * then don't wait, as we need to empty
987 * that queue to get the nr_pending
988 * count down.
990 raid10_log(conf->mddev, "wait barrier");
991 wait_event_lock_irq(conf->wait_barrier,
992 !conf->barrier ||
993 (atomic_read(&conf->nr_pending) &&
994 current->bio_list &&
995 (!bio_list_empty(&current->bio_list[0]) ||
996 !bio_list_empty(&current->bio_list[1]))),
997 conf->resync_lock);
998 conf->nr_waiting--;
999 if (!conf->nr_waiting)
1000 wake_up(&conf->wait_barrier);
1002 atomic_inc(&conf->nr_pending);
1003 spin_unlock_irq(&conf->resync_lock);
1006 static void allow_barrier(struct r10conf *conf)
1008 if ((atomic_dec_and_test(&conf->nr_pending)) ||
1009 (conf->array_freeze_pending))
1010 wake_up(&conf->wait_barrier);
1013 static void freeze_array(struct r10conf *conf, int extra)
1015 /* stop syncio and normal IO and wait for everything to
1016 * go quiet.
1017 * We increment barrier and nr_waiting, and then
1018 * wait until nr_pending match nr_queued+extra
1019 * This is called in the context of one normal IO request
1020 * that has failed. Thus any sync request that might be pending
1021 * will be blocked by nr_pending, and we need to wait for
1022 * pending IO requests to complete or be queued for re-try.
1023 * Thus the number queued (nr_queued) plus this request (extra)
1024 * must match the number of pending IOs (nr_pending) before
1025 * we continue.
1027 spin_lock_irq(&conf->resync_lock);
1028 conf->array_freeze_pending++;
1029 conf->barrier++;
1030 conf->nr_waiting++;
1031 wait_event_lock_irq_cmd(conf->wait_barrier,
1032 atomic_read(&conf->nr_pending) == conf->nr_queued+extra,
1033 conf->resync_lock,
1034 flush_pending_writes(conf));
1036 conf->array_freeze_pending--;
1037 spin_unlock_irq(&conf->resync_lock);
1040 static void unfreeze_array(struct r10conf *conf)
1042 /* reverse the effect of the freeze */
1043 spin_lock_irq(&conf->resync_lock);
1044 conf->barrier--;
1045 conf->nr_waiting--;
1046 wake_up(&conf->wait_barrier);
1047 spin_unlock_irq(&conf->resync_lock);
1050 static sector_t choose_data_offset(struct r10bio *r10_bio,
1051 struct md_rdev *rdev)
1053 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1054 test_bit(R10BIO_Previous, &r10_bio->state))
1055 return rdev->data_offset;
1056 else
1057 return rdev->new_data_offset;
1060 struct raid10_plug_cb {
1061 struct blk_plug_cb cb;
1062 struct bio_list pending;
1063 int pending_cnt;
1066 static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1068 struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb,
1069 cb);
1070 struct mddev *mddev = plug->cb.data;
1071 struct r10conf *conf = mddev->private;
1072 struct bio *bio;
1074 if (from_schedule || current->bio_list) {
1075 spin_lock_irq(&conf->device_lock);
1076 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1077 conf->pending_count += plug->pending_cnt;
1078 spin_unlock_irq(&conf->device_lock);
1079 wake_up(&conf->wait_barrier);
1080 md_wakeup_thread(mddev->thread);
1081 kfree(plug);
1082 return;
1085 /* we aren't scheduling, so we can do the write-out directly. */
1086 bio = bio_list_get(&plug->pending);
1087 bitmap_unplug(mddev->bitmap);
1088 wake_up(&conf->wait_barrier);
1090 while (bio) { /* submit pending writes */
1091 struct bio *next = bio->bi_next;
1092 struct md_rdev *rdev = (void*)bio->bi_disk;
1093 bio->bi_next = NULL;
1094 bio_set_dev(bio, rdev->bdev);
1095 if (test_bit(Faulty, &rdev->flags)) {
1096 bio_io_error(bio);
1097 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
1098 !blk_queue_discard(bio->bi_disk->queue)))
1099 /* Just ignore it */
1100 bio_endio(bio);
1101 else
1102 generic_make_request(bio);
1103 bio = next;
1105 kfree(plug);
1108 static void raid10_read_request(struct mddev *mddev, struct bio *bio,
1109 struct r10bio *r10_bio)
1111 struct r10conf *conf = mddev->private;
1112 struct bio *read_bio;
1113 const int op = bio_op(bio);
1114 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1115 int max_sectors;
1116 sector_t sectors;
1117 struct md_rdev *rdev;
1118 char b[BDEVNAME_SIZE];
1119 int slot = r10_bio->read_slot;
1120 struct md_rdev *err_rdev = NULL;
1121 gfp_t gfp = GFP_NOIO;
1123 if (r10_bio->devs[slot].rdev) {
1125 * This is an error retry, but we cannot
1126 * safely dereference the rdev in the r10_bio,
1127 * we must use the one in conf.
1128 * If it has already been disconnected (unlikely)
1129 * we lose the device name in error messages.
1131 int disk;
1133 * As we are blocking raid10, it is a little safer to
1134 * use __GFP_HIGH.
1136 gfp = GFP_NOIO | __GFP_HIGH;
1138 rcu_read_lock();
1139 disk = r10_bio->devs[slot].devnum;
1140 err_rdev = rcu_dereference(conf->mirrors[disk].rdev);
1141 if (err_rdev)
1142 bdevname(err_rdev->bdev, b);
1143 else {
1144 strcpy(b, "???");
1145 /* This never gets dereferenced */
1146 err_rdev = r10_bio->devs[slot].rdev;
1148 rcu_read_unlock();
1151 * Register the new request and wait if the reconstruction
1152 * thread has put up a bar for new requests.
1153 * Continue immediately if no resync is active currently.
1155 wait_barrier(conf);
1157 sectors = r10_bio->sectors;
1158 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1159 bio->bi_iter.bi_sector < conf->reshape_progress &&
1160 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
1162 * IO spans the reshape position. Need to wait for reshape to
1163 * pass
1165 raid10_log(conf->mddev, "wait reshape");
1166 allow_barrier(conf);
1167 wait_event(conf->wait_barrier,
1168 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1169 conf->reshape_progress >= bio->bi_iter.bi_sector +
1170 sectors);
1171 wait_barrier(conf);
1174 rdev = read_balance(conf, r10_bio, &max_sectors);
1175 if (!rdev) {
1176 if (err_rdev) {
1177 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
1178 mdname(mddev), b,
1179 (unsigned long long)r10_bio->sector);
1181 raid_end_bio_io(r10_bio);
1182 return;
1184 if (err_rdev)
1185 pr_err_ratelimited("md/raid10:%s: %s: redirecting sector %llu to another mirror\n",
1186 mdname(mddev),
1187 bdevname(rdev->bdev, b),
1188 (unsigned long long)r10_bio->sector);
1189 if (max_sectors < bio_sectors(bio)) {
1190 struct bio *split = bio_split(bio, max_sectors,
1191 gfp, conf->bio_split);
1192 bio_chain(split, bio);
1193 allow_barrier(conf);
1194 generic_make_request(bio);
1195 wait_barrier(conf);
1196 bio = split;
1197 r10_bio->master_bio = bio;
1198 r10_bio->sectors = max_sectors;
1200 slot = r10_bio->read_slot;
1202 read_bio = bio_clone_fast(bio, gfp, mddev->bio_set);
1204 r10_bio->devs[slot].bio = read_bio;
1205 r10_bio->devs[slot].rdev = rdev;
1207 read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr +
1208 choose_data_offset(r10_bio, rdev);
1209 bio_set_dev(read_bio, rdev->bdev);
1210 read_bio->bi_end_io = raid10_end_read_request;
1211 bio_set_op_attrs(read_bio, op, do_sync);
1212 if (test_bit(FailFast, &rdev->flags) &&
1213 test_bit(R10BIO_FailFast, &r10_bio->state))
1214 read_bio->bi_opf |= MD_FAILFAST;
1215 read_bio->bi_private = r10_bio;
1217 if (mddev->gendisk)
1218 trace_block_bio_remap(read_bio->bi_disk->queue,
1219 read_bio, disk_devt(mddev->gendisk),
1220 r10_bio->sector);
1221 generic_make_request(read_bio);
1222 return;
1225 static void raid10_write_one_disk(struct mddev *mddev, struct r10bio *r10_bio,
1226 struct bio *bio, bool replacement,
1227 int n_copy)
1229 const int op = bio_op(bio);
1230 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1231 const unsigned long do_fua = (bio->bi_opf & REQ_FUA);
1232 unsigned long flags;
1233 struct blk_plug_cb *cb;
1234 struct raid10_plug_cb *plug = NULL;
1235 struct r10conf *conf = mddev->private;
1236 struct md_rdev *rdev;
1237 int devnum = r10_bio->devs[n_copy].devnum;
1238 struct bio *mbio;
1240 if (replacement) {
1241 rdev = conf->mirrors[devnum].replacement;
1242 if (rdev == NULL) {
1243 /* Replacement just got moved to main 'rdev' */
1244 smp_mb();
1245 rdev = conf->mirrors[devnum].rdev;
1247 } else
1248 rdev = conf->mirrors[devnum].rdev;
1250 mbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
1251 if (replacement)
1252 r10_bio->devs[n_copy].repl_bio = mbio;
1253 else
1254 r10_bio->devs[n_copy].bio = mbio;
1256 mbio->bi_iter.bi_sector = (r10_bio->devs[n_copy].addr +
1257 choose_data_offset(r10_bio, rdev));
1258 bio_set_dev(mbio, rdev->bdev);
1259 mbio->bi_end_io = raid10_end_write_request;
1260 bio_set_op_attrs(mbio, op, do_sync | do_fua);
1261 if (!replacement && test_bit(FailFast,
1262 &conf->mirrors[devnum].rdev->flags)
1263 && enough(conf, devnum))
1264 mbio->bi_opf |= MD_FAILFAST;
1265 mbio->bi_private = r10_bio;
1267 if (conf->mddev->gendisk)
1268 trace_block_bio_remap(mbio->bi_disk->queue,
1269 mbio, disk_devt(conf->mddev->gendisk),
1270 r10_bio->sector);
1271 /* flush_pending_writes() needs access to the rdev so...*/
1272 mbio->bi_disk = (void *)rdev;
1274 atomic_inc(&r10_bio->remaining);
1276 cb = blk_check_plugged(raid10_unplug, mddev, sizeof(*plug));
1277 if (cb)
1278 plug = container_of(cb, struct raid10_plug_cb, cb);
1279 else
1280 plug = NULL;
1281 if (plug) {
1282 bio_list_add(&plug->pending, mbio);
1283 plug->pending_cnt++;
1284 } else {
1285 spin_lock_irqsave(&conf->device_lock, flags);
1286 bio_list_add(&conf->pending_bio_list, mbio);
1287 conf->pending_count++;
1288 spin_unlock_irqrestore(&conf->device_lock, flags);
1289 md_wakeup_thread(mddev->thread);
1293 static void raid10_write_request(struct mddev *mddev, struct bio *bio,
1294 struct r10bio *r10_bio)
1296 struct r10conf *conf = mddev->private;
1297 int i;
1298 struct md_rdev *blocked_rdev;
1299 sector_t sectors;
1300 int max_sectors;
1303 * Register the new request and wait if the reconstruction
1304 * thread has put up a bar for new requests.
1305 * Continue immediately if no resync is active currently.
1307 wait_barrier(conf);
1309 sectors = r10_bio->sectors;
1310 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1311 bio->bi_iter.bi_sector < conf->reshape_progress &&
1312 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
1314 * IO spans the reshape position. Need to wait for reshape to
1315 * pass
1317 raid10_log(conf->mddev, "wait reshape");
1318 allow_barrier(conf);
1319 wait_event(conf->wait_barrier,
1320 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1321 conf->reshape_progress >= bio->bi_iter.bi_sector +
1322 sectors);
1323 wait_barrier(conf);
1326 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1327 (mddev->reshape_backwards
1328 ? (bio->bi_iter.bi_sector < conf->reshape_safe &&
1329 bio->bi_iter.bi_sector + sectors > conf->reshape_progress)
1330 : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe &&
1331 bio->bi_iter.bi_sector < conf->reshape_progress))) {
1332 /* Need to update reshape_position in metadata */
1333 mddev->reshape_position = conf->reshape_progress;
1334 set_mask_bits(&mddev->sb_flags, 0,
1335 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
1336 md_wakeup_thread(mddev->thread);
1337 raid10_log(conf->mddev, "wait reshape metadata");
1338 wait_event(mddev->sb_wait,
1339 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
1341 conf->reshape_safe = mddev->reshape_position;
1344 if (conf->pending_count >= max_queued_requests) {
1345 md_wakeup_thread(mddev->thread);
1346 raid10_log(mddev, "wait queued");
1347 wait_event(conf->wait_barrier,
1348 conf->pending_count < max_queued_requests);
1350 /* first select target devices under rcu_lock and
1351 * inc refcount on their rdev. Record them by setting
1352 * bios[x] to bio
1353 * If there are known/acknowledged bad blocks on any device
1354 * on which we have seen a write error, we want to avoid
1355 * writing to those blocks. This potentially requires several
1356 * writes to write around the bad blocks. Each set of writes
1357 * gets its own r10_bio with a set of bios attached.
1360 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
1361 raid10_find_phys(conf, r10_bio);
1362 retry_write:
1363 blocked_rdev = NULL;
1364 rcu_read_lock();
1365 max_sectors = r10_bio->sectors;
1367 for (i = 0; i < conf->copies; i++) {
1368 int d = r10_bio->devs[i].devnum;
1369 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
1370 struct md_rdev *rrdev = rcu_dereference(
1371 conf->mirrors[d].replacement);
1372 if (rdev == rrdev)
1373 rrdev = NULL;
1374 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1375 atomic_inc(&rdev->nr_pending);
1376 blocked_rdev = rdev;
1377 break;
1379 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1380 atomic_inc(&rrdev->nr_pending);
1381 blocked_rdev = rrdev;
1382 break;
1384 if (rdev && (test_bit(Faulty, &rdev->flags)))
1385 rdev = NULL;
1386 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
1387 rrdev = NULL;
1389 r10_bio->devs[i].bio = NULL;
1390 r10_bio->devs[i].repl_bio = NULL;
1392 if (!rdev && !rrdev) {
1393 set_bit(R10BIO_Degraded, &r10_bio->state);
1394 continue;
1396 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
1397 sector_t first_bad;
1398 sector_t dev_sector = r10_bio->devs[i].addr;
1399 int bad_sectors;
1400 int is_bad;
1402 is_bad = is_badblock(rdev, dev_sector, max_sectors,
1403 &first_bad, &bad_sectors);
1404 if (is_bad < 0) {
1405 /* Mustn't write here until the bad block
1406 * is acknowledged
1408 atomic_inc(&rdev->nr_pending);
1409 set_bit(BlockedBadBlocks, &rdev->flags);
1410 blocked_rdev = rdev;
1411 break;
1413 if (is_bad && first_bad <= dev_sector) {
1414 /* Cannot write here at all */
1415 bad_sectors -= (dev_sector - first_bad);
1416 if (bad_sectors < max_sectors)
1417 /* Mustn't write more than bad_sectors
1418 * to other devices yet
1420 max_sectors = bad_sectors;
1421 /* We don't set R10BIO_Degraded as that
1422 * only applies if the disk is missing,
1423 * so it might be re-added, and we want to
1424 * know to recover this chunk.
1425 * In this case the device is here, and the
1426 * fact that this chunk is not in-sync is
1427 * recorded in the bad block log.
1429 continue;
1431 if (is_bad) {
1432 int good_sectors = first_bad - dev_sector;
1433 if (good_sectors < max_sectors)
1434 max_sectors = good_sectors;
1437 if (rdev) {
1438 r10_bio->devs[i].bio = bio;
1439 atomic_inc(&rdev->nr_pending);
1441 if (rrdev) {
1442 r10_bio->devs[i].repl_bio = bio;
1443 atomic_inc(&rrdev->nr_pending);
1446 rcu_read_unlock();
1448 if (unlikely(blocked_rdev)) {
1449 /* Have to wait for this device to get unblocked, then retry */
1450 int j;
1451 int d;
1453 for (j = 0; j < i; j++) {
1454 if (r10_bio->devs[j].bio) {
1455 d = r10_bio->devs[j].devnum;
1456 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1458 if (r10_bio->devs[j].repl_bio) {
1459 struct md_rdev *rdev;
1460 d = r10_bio->devs[j].devnum;
1461 rdev = conf->mirrors[d].replacement;
1462 if (!rdev) {
1463 /* Race with remove_disk */
1464 smp_mb();
1465 rdev = conf->mirrors[d].rdev;
1467 rdev_dec_pending(rdev, mddev);
1470 allow_barrier(conf);
1471 raid10_log(conf->mddev, "wait rdev %d blocked", blocked_rdev->raid_disk);
1472 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1473 wait_barrier(conf);
1474 goto retry_write;
1477 if (max_sectors < r10_bio->sectors)
1478 r10_bio->sectors = max_sectors;
1480 if (r10_bio->sectors < bio_sectors(bio)) {
1481 struct bio *split = bio_split(bio, r10_bio->sectors,
1482 GFP_NOIO, conf->bio_split);
1483 bio_chain(split, bio);
1484 allow_barrier(conf);
1485 generic_make_request(bio);
1486 wait_barrier(conf);
1487 bio = split;
1488 r10_bio->master_bio = bio;
1491 atomic_set(&r10_bio->remaining, 1);
1492 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
1494 for (i = 0; i < conf->copies; i++) {
1495 if (r10_bio->devs[i].bio)
1496 raid10_write_one_disk(mddev, r10_bio, bio, false, i);
1497 if (r10_bio->devs[i].repl_bio)
1498 raid10_write_one_disk(mddev, r10_bio, bio, true, i);
1500 one_write_done(r10_bio);
1503 static void __make_request(struct mddev *mddev, struct bio *bio, int sectors)
1505 struct r10conf *conf = mddev->private;
1506 struct r10bio *r10_bio;
1508 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1510 r10_bio->master_bio = bio;
1511 r10_bio->sectors = sectors;
1513 r10_bio->mddev = mddev;
1514 r10_bio->sector = bio->bi_iter.bi_sector;
1515 r10_bio->state = 0;
1516 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) * conf->copies);
1518 if (bio_data_dir(bio) == READ)
1519 raid10_read_request(mddev, bio, r10_bio);
1520 else
1521 raid10_write_request(mddev, bio, r10_bio);
1524 static bool raid10_make_request(struct mddev *mddev, struct bio *bio)
1526 struct r10conf *conf = mddev->private;
1527 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
1528 int chunk_sects = chunk_mask + 1;
1529 int sectors = bio_sectors(bio);
1531 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1532 md_flush_request(mddev, bio);
1533 return true;
1536 if (!md_write_start(mddev, bio))
1537 return false;
1540 * If this request crosses a chunk boundary, we need to split
1541 * it.
1543 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
1544 sectors > chunk_sects
1545 && (conf->geo.near_copies < conf->geo.raid_disks
1546 || conf->prev.near_copies <
1547 conf->prev.raid_disks)))
1548 sectors = chunk_sects -
1549 (bio->bi_iter.bi_sector &
1550 (chunk_sects - 1));
1551 __make_request(mddev, bio, sectors);
1553 /* In case raid10d snuck in to freeze_array */
1554 wake_up(&conf->wait_barrier);
1555 return true;
1558 static void raid10_status(struct seq_file *seq, struct mddev *mddev)
1560 struct r10conf *conf = mddev->private;
1561 int i;
1563 if (conf->geo.near_copies < conf->geo.raid_disks)
1564 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
1565 if (conf->geo.near_copies > 1)
1566 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1567 if (conf->geo.far_copies > 1) {
1568 if (conf->geo.far_offset)
1569 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
1570 else
1571 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
1572 if (conf->geo.far_set_size != conf->geo.raid_disks)
1573 seq_printf(seq, " %d devices per set", conf->geo.far_set_size);
1575 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1576 conf->geo.raid_disks - mddev->degraded);
1577 rcu_read_lock();
1578 for (i = 0; i < conf->geo.raid_disks; i++) {
1579 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1580 seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1582 rcu_read_unlock();
1583 seq_printf(seq, "]");
1586 /* check if there are enough drives for
1587 * every block to appear on atleast one.
1588 * Don't consider the device numbered 'ignore'
1589 * as we might be about to remove it.
1591 static int _enough(struct r10conf *conf, int previous, int ignore)
1593 int first = 0;
1594 int has_enough = 0;
1595 int disks, ncopies;
1596 if (previous) {
1597 disks = conf->prev.raid_disks;
1598 ncopies = conf->prev.near_copies;
1599 } else {
1600 disks = conf->geo.raid_disks;
1601 ncopies = conf->geo.near_copies;
1604 rcu_read_lock();
1605 do {
1606 int n = conf->copies;
1607 int cnt = 0;
1608 int this = first;
1609 while (n--) {
1610 struct md_rdev *rdev;
1611 if (this != ignore &&
1612 (rdev = rcu_dereference(conf->mirrors[this].rdev)) &&
1613 test_bit(In_sync, &rdev->flags))
1614 cnt++;
1615 this = (this+1) % disks;
1617 if (cnt == 0)
1618 goto out;
1619 first = (first + ncopies) % disks;
1620 } while (first != 0);
1621 has_enough = 1;
1622 out:
1623 rcu_read_unlock();
1624 return has_enough;
1627 static int enough(struct r10conf *conf, int ignore)
1629 /* when calling 'enough', both 'prev' and 'geo' must
1630 * be stable.
1631 * This is ensured if ->reconfig_mutex or ->device_lock
1632 * is held.
1634 return _enough(conf, 0, ignore) &&
1635 _enough(conf, 1, ignore);
1638 static void raid10_error(struct mddev *mddev, struct md_rdev *rdev)
1640 char b[BDEVNAME_SIZE];
1641 struct r10conf *conf = mddev->private;
1642 unsigned long flags;
1645 * If it is not operational, then we have already marked it as dead
1646 * else if it is the last working disks, ignore the error, let the
1647 * next level up know.
1648 * else mark the drive as failed
1650 spin_lock_irqsave(&conf->device_lock, flags);
1651 if (test_bit(In_sync, &rdev->flags)
1652 && !enough(conf, rdev->raid_disk)) {
1654 * Don't fail the drive, just return an IO error.
1656 spin_unlock_irqrestore(&conf->device_lock, flags);
1657 return;
1659 if (test_and_clear_bit(In_sync, &rdev->flags))
1660 mddev->degraded++;
1662 * If recovery is running, make sure it aborts.
1664 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1665 set_bit(Blocked, &rdev->flags);
1666 set_bit(Faulty, &rdev->flags);
1667 set_mask_bits(&mddev->sb_flags, 0,
1668 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
1669 spin_unlock_irqrestore(&conf->device_lock, flags);
1670 pr_crit("md/raid10:%s: Disk failure on %s, disabling device.\n"
1671 "md/raid10:%s: Operation continuing on %d devices.\n",
1672 mdname(mddev), bdevname(rdev->bdev, b),
1673 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
1676 static void print_conf(struct r10conf *conf)
1678 int i;
1679 struct md_rdev *rdev;
1681 pr_debug("RAID10 conf printout:\n");
1682 if (!conf) {
1683 pr_debug("(!conf)\n");
1684 return;
1686 pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
1687 conf->geo.raid_disks);
1689 /* This is only called with ->reconfix_mutex held, so
1690 * rcu protection of rdev is not needed */
1691 for (i = 0; i < conf->geo.raid_disks; i++) {
1692 char b[BDEVNAME_SIZE];
1693 rdev = conf->mirrors[i].rdev;
1694 if (rdev)
1695 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1696 i, !test_bit(In_sync, &rdev->flags),
1697 !test_bit(Faulty, &rdev->flags),
1698 bdevname(rdev->bdev,b));
1702 static void close_sync(struct r10conf *conf)
1704 wait_barrier(conf);
1705 allow_barrier(conf);
1707 mempool_destroy(conf->r10buf_pool);
1708 conf->r10buf_pool = NULL;
1711 static int raid10_spare_active(struct mddev *mddev)
1713 int i;
1714 struct r10conf *conf = mddev->private;
1715 struct raid10_info *tmp;
1716 int count = 0;
1717 unsigned long flags;
1720 * Find all non-in_sync disks within the RAID10 configuration
1721 * and mark them in_sync
1723 for (i = 0; i < conf->geo.raid_disks; i++) {
1724 tmp = conf->mirrors + i;
1725 if (tmp->replacement
1726 && tmp->replacement->recovery_offset == MaxSector
1727 && !test_bit(Faulty, &tmp->replacement->flags)
1728 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1729 /* Replacement has just become active */
1730 if (!tmp->rdev
1731 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1732 count++;
1733 if (tmp->rdev) {
1734 /* Replaced device not technically faulty,
1735 * but we need to be sure it gets removed
1736 * and never re-added.
1738 set_bit(Faulty, &tmp->rdev->flags);
1739 sysfs_notify_dirent_safe(
1740 tmp->rdev->sysfs_state);
1742 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1743 } else if (tmp->rdev
1744 && tmp->rdev->recovery_offset == MaxSector
1745 && !test_bit(Faulty, &tmp->rdev->flags)
1746 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1747 count++;
1748 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
1751 spin_lock_irqsave(&conf->device_lock, flags);
1752 mddev->degraded -= count;
1753 spin_unlock_irqrestore(&conf->device_lock, flags);
1755 print_conf(conf);
1756 return count;
1759 static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1761 struct r10conf *conf = mddev->private;
1762 int err = -EEXIST;
1763 int mirror;
1764 int first = 0;
1765 int last = conf->geo.raid_disks - 1;
1767 if (mddev->recovery_cp < MaxSector)
1768 /* only hot-add to in-sync arrays, as recovery is
1769 * very different from resync
1771 return -EBUSY;
1772 if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
1773 return -EINVAL;
1775 if (md_integrity_add_rdev(rdev, mddev))
1776 return -ENXIO;
1778 if (rdev->raid_disk >= 0)
1779 first = last = rdev->raid_disk;
1781 if (rdev->saved_raid_disk >= first &&
1782 rdev->saved_raid_disk < conf->geo.raid_disks &&
1783 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1784 mirror = rdev->saved_raid_disk;
1785 else
1786 mirror = first;
1787 for ( ; mirror <= last ; mirror++) {
1788 struct raid10_info *p = &conf->mirrors[mirror];
1789 if (p->recovery_disabled == mddev->recovery_disabled)
1790 continue;
1791 if (p->rdev) {
1792 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1793 p->replacement != NULL)
1794 continue;
1795 clear_bit(In_sync, &rdev->flags);
1796 set_bit(Replacement, &rdev->flags);
1797 rdev->raid_disk = mirror;
1798 err = 0;
1799 if (mddev->gendisk)
1800 disk_stack_limits(mddev->gendisk, rdev->bdev,
1801 rdev->data_offset << 9);
1802 conf->fullsync = 1;
1803 rcu_assign_pointer(p->replacement, rdev);
1804 break;
1807 if (mddev->gendisk)
1808 disk_stack_limits(mddev->gendisk, rdev->bdev,
1809 rdev->data_offset << 9);
1811 p->head_position = 0;
1812 p->recovery_disabled = mddev->recovery_disabled - 1;
1813 rdev->raid_disk = mirror;
1814 err = 0;
1815 if (rdev->saved_raid_disk != mirror)
1816 conf->fullsync = 1;
1817 rcu_assign_pointer(p->rdev, rdev);
1818 break;
1820 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
1821 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
1823 print_conf(conf);
1824 return err;
1827 static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1829 struct r10conf *conf = mddev->private;
1830 int err = 0;
1831 int number = rdev->raid_disk;
1832 struct md_rdev **rdevp;
1833 struct raid10_info *p = conf->mirrors + number;
1835 print_conf(conf);
1836 if (rdev == p->rdev)
1837 rdevp = &p->rdev;
1838 else if (rdev == p->replacement)
1839 rdevp = &p->replacement;
1840 else
1841 return 0;
1843 if (test_bit(In_sync, &rdev->flags) ||
1844 atomic_read(&rdev->nr_pending)) {
1845 err = -EBUSY;
1846 goto abort;
1848 /* Only remove non-faulty devices if recovery
1849 * is not possible.
1851 if (!test_bit(Faulty, &rdev->flags) &&
1852 mddev->recovery_disabled != p->recovery_disabled &&
1853 (!p->replacement || p->replacement == rdev) &&
1854 number < conf->geo.raid_disks &&
1855 enough(conf, -1)) {
1856 err = -EBUSY;
1857 goto abort;
1859 *rdevp = NULL;
1860 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
1861 synchronize_rcu();
1862 if (atomic_read(&rdev->nr_pending)) {
1863 /* lost the race, try later */
1864 err = -EBUSY;
1865 *rdevp = rdev;
1866 goto abort;
1869 if (p->replacement) {
1870 /* We must have just cleared 'rdev' */
1871 p->rdev = p->replacement;
1872 clear_bit(Replacement, &p->replacement->flags);
1873 smp_mb(); /* Make sure other CPUs may see both as identical
1874 * but will never see neither -- if they are careful.
1876 p->replacement = NULL;
1879 clear_bit(WantReplacement, &rdev->flags);
1880 err = md_integrity_register(mddev);
1882 abort:
1884 print_conf(conf);
1885 return err;
1888 static void __end_sync_read(struct r10bio *r10_bio, struct bio *bio, int d)
1890 struct r10conf *conf = r10_bio->mddev->private;
1892 if (!bio->bi_status)
1893 set_bit(R10BIO_Uptodate, &r10_bio->state);
1894 else
1895 /* The write handler will notice the lack of
1896 * R10BIO_Uptodate and record any errors etc
1898 atomic_add(r10_bio->sectors,
1899 &conf->mirrors[d].rdev->corrected_errors);
1901 /* for reconstruct, we always reschedule after a read.
1902 * for resync, only after all reads
1904 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1905 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1906 atomic_dec_and_test(&r10_bio->remaining)) {
1907 /* we have read all the blocks,
1908 * do the comparison in process context in raid10d
1910 reschedule_retry(r10_bio);
1914 static void end_sync_read(struct bio *bio)
1916 struct r10bio *r10_bio = get_resync_r10bio(bio);
1917 struct r10conf *conf = r10_bio->mddev->private;
1918 int d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
1920 __end_sync_read(r10_bio, bio, d);
1923 static void end_reshape_read(struct bio *bio)
1925 /* reshape read bio isn't allocated from r10buf_pool */
1926 struct r10bio *r10_bio = bio->bi_private;
1928 __end_sync_read(r10_bio, bio, r10_bio->read_slot);
1931 static void end_sync_request(struct r10bio *r10_bio)
1933 struct mddev *mddev = r10_bio->mddev;
1935 while (atomic_dec_and_test(&r10_bio->remaining)) {
1936 if (r10_bio->master_bio == NULL) {
1937 /* the primary of several recovery bios */
1938 sector_t s = r10_bio->sectors;
1939 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1940 test_bit(R10BIO_WriteError, &r10_bio->state))
1941 reschedule_retry(r10_bio);
1942 else
1943 put_buf(r10_bio);
1944 md_done_sync(mddev, s, 1);
1945 break;
1946 } else {
1947 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
1948 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1949 test_bit(R10BIO_WriteError, &r10_bio->state))
1950 reschedule_retry(r10_bio);
1951 else
1952 put_buf(r10_bio);
1953 r10_bio = r10_bio2;
1958 static void end_sync_write(struct bio *bio)
1960 struct r10bio *r10_bio = get_resync_r10bio(bio);
1961 struct mddev *mddev = r10_bio->mddev;
1962 struct r10conf *conf = mddev->private;
1963 int d;
1964 sector_t first_bad;
1965 int bad_sectors;
1966 int slot;
1967 int repl;
1968 struct md_rdev *rdev = NULL;
1970 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1971 if (repl)
1972 rdev = conf->mirrors[d].replacement;
1973 else
1974 rdev = conf->mirrors[d].rdev;
1976 if (bio->bi_status) {
1977 if (repl)
1978 md_error(mddev, rdev);
1979 else {
1980 set_bit(WriteErrorSeen, &rdev->flags);
1981 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1982 set_bit(MD_RECOVERY_NEEDED,
1983 &rdev->mddev->recovery);
1984 set_bit(R10BIO_WriteError, &r10_bio->state);
1986 } else if (is_badblock(rdev,
1987 r10_bio->devs[slot].addr,
1988 r10_bio->sectors,
1989 &first_bad, &bad_sectors))
1990 set_bit(R10BIO_MadeGood, &r10_bio->state);
1992 rdev_dec_pending(rdev, mddev);
1994 end_sync_request(r10_bio);
1998 * Note: sync and recover and handled very differently for raid10
1999 * This code is for resync.
2000 * For resync, we read through virtual addresses and read all blocks.
2001 * If there is any error, we schedule a write. The lowest numbered
2002 * drive is authoritative.
2003 * However requests come for physical address, so we need to map.
2004 * For every physical address there are raid_disks/copies virtual addresses,
2005 * which is always are least one, but is not necessarly an integer.
2006 * This means that a physical address can span multiple chunks, so we may
2007 * have to submit multiple io requests for a single sync request.
2010 * We check if all blocks are in-sync and only write to blocks that
2011 * aren't in sync
2013 static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
2015 struct r10conf *conf = mddev->private;
2016 int i, first;
2017 struct bio *tbio, *fbio;
2018 int vcnt;
2019 struct page **tpages, **fpages;
2021 atomic_set(&r10_bio->remaining, 1);
2023 /* find the first device with a block */
2024 for (i=0; i<conf->copies; i++)
2025 if (!r10_bio->devs[i].bio->bi_status)
2026 break;
2028 if (i == conf->copies)
2029 goto done;
2031 first = i;
2032 fbio = r10_bio->devs[i].bio;
2033 fbio->bi_iter.bi_size = r10_bio->sectors << 9;
2034 fbio->bi_iter.bi_idx = 0;
2035 fpages = get_resync_pages(fbio)->pages;
2037 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
2038 /* now find blocks with errors */
2039 for (i=0 ; i < conf->copies ; i++) {
2040 int j, d;
2041 struct md_rdev *rdev;
2042 struct resync_pages *rp;
2044 tbio = r10_bio->devs[i].bio;
2046 if (tbio->bi_end_io != end_sync_read)
2047 continue;
2048 if (i == first)
2049 continue;
2051 tpages = get_resync_pages(tbio)->pages;
2052 d = r10_bio->devs[i].devnum;
2053 rdev = conf->mirrors[d].rdev;
2054 if (!r10_bio->devs[i].bio->bi_status) {
2055 /* We know that the bi_io_vec layout is the same for
2056 * both 'first' and 'i', so we just compare them.
2057 * All vec entries are PAGE_SIZE;
2059 int sectors = r10_bio->sectors;
2060 for (j = 0; j < vcnt; j++) {
2061 int len = PAGE_SIZE;
2062 if (sectors < (len / 512))
2063 len = sectors * 512;
2064 if (memcmp(page_address(fpages[j]),
2065 page_address(tpages[j]),
2066 len))
2067 break;
2068 sectors -= len/512;
2070 if (j == vcnt)
2071 continue;
2072 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
2073 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2074 /* Don't fix anything. */
2075 continue;
2076 } else if (test_bit(FailFast, &rdev->flags)) {
2077 /* Just give up on this device */
2078 md_error(rdev->mddev, rdev);
2079 continue;
2081 /* Ok, we need to write this bio, either to correct an
2082 * inconsistency or to correct an unreadable block.
2083 * First we need to fixup bv_offset, bv_len and
2084 * bi_vecs, as the read request might have corrupted these
2086 rp = get_resync_pages(tbio);
2087 bio_reset(tbio);
2089 md_bio_reset_resync_pages(tbio, rp, fbio->bi_iter.bi_size);
2091 rp->raid_bio = r10_bio;
2092 tbio->bi_private = rp;
2093 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr;
2094 tbio->bi_end_io = end_sync_write;
2095 bio_set_op_attrs(tbio, REQ_OP_WRITE, 0);
2097 bio_copy_data(tbio, fbio);
2099 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2100 atomic_inc(&r10_bio->remaining);
2101 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
2103 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
2104 tbio->bi_opf |= MD_FAILFAST;
2105 tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset;
2106 bio_set_dev(tbio, conf->mirrors[d].rdev->bdev);
2107 generic_make_request(tbio);
2110 /* Now write out to any replacement devices
2111 * that are active
2113 for (i = 0; i < conf->copies; i++) {
2114 int d;
2116 tbio = r10_bio->devs[i].repl_bio;
2117 if (!tbio || !tbio->bi_end_io)
2118 continue;
2119 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2120 && r10_bio->devs[i].bio != fbio)
2121 bio_copy_data(tbio, fbio);
2122 d = r10_bio->devs[i].devnum;
2123 atomic_inc(&r10_bio->remaining);
2124 md_sync_acct(conf->mirrors[d].replacement->bdev,
2125 bio_sectors(tbio));
2126 generic_make_request(tbio);
2129 done:
2130 if (atomic_dec_and_test(&r10_bio->remaining)) {
2131 md_done_sync(mddev, r10_bio->sectors, 1);
2132 put_buf(r10_bio);
2137 * Now for the recovery code.
2138 * Recovery happens across physical sectors.
2139 * We recover all non-is_sync drives by finding the virtual address of
2140 * each, and then choose a working drive that also has that virt address.
2141 * There is a separate r10_bio for each non-in_sync drive.
2142 * Only the first two slots are in use. The first for reading,
2143 * The second for writing.
2146 static void fix_recovery_read_error(struct r10bio *r10_bio)
2148 /* We got a read error during recovery.
2149 * We repeat the read in smaller page-sized sections.
2150 * If a read succeeds, write it to the new device or record
2151 * a bad block if we cannot.
2152 * If a read fails, record a bad block on both old and
2153 * new devices.
2155 struct mddev *mddev = r10_bio->mddev;
2156 struct r10conf *conf = mddev->private;
2157 struct bio *bio = r10_bio->devs[0].bio;
2158 sector_t sect = 0;
2159 int sectors = r10_bio->sectors;
2160 int idx = 0;
2161 int dr = r10_bio->devs[0].devnum;
2162 int dw = r10_bio->devs[1].devnum;
2163 struct page **pages = get_resync_pages(bio)->pages;
2165 while (sectors) {
2166 int s = sectors;
2167 struct md_rdev *rdev;
2168 sector_t addr;
2169 int ok;
2171 if (s > (PAGE_SIZE>>9))
2172 s = PAGE_SIZE >> 9;
2174 rdev = conf->mirrors[dr].rdev;
2175 addr = r10_bio->devs[0].addr + sect,
2176 ok = sync_page_io(rdev,
2177 addr,
2178 s << 9,
2179 pages[idx],
2180 REQ_OP_READ, 0, false);
2181 if (ok) {
2182 rdev = conf->mirrors[dw].rdev;
2183 addr = r10_bio->devs[1].addr + sect;
2184 ok = sync_page_io(rdev,
2185 addr,
2186 s << 9,
2187 pages[idx],
2188 REQ_OP_WRITE, 0, false);
2189 if (!ok) {
2190 set_bit(WriteErrorSeen, &rdev->flags);
2191 if (!test_and_set_bit(WantReplacement,
2192 &rdev->flags))
2193 set_bit(MD_RECOVERY_NEEDED,
2194 &rdev->mddev->recovery);
2197 if (!ok) {
2198 /* We don't worry if we cannot set a bad block -
2199 * it really is bad so there is no loss in not
2200 * recording it yet
2202 rdev_set_badblocks(rdev, addr, s, 0);
2204 if (rdev != conf->mirrors[dw].rdev) {
2205 /* need bad block on destination too */
2206 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
2207 addr = r10_bio->devs[1].addr + sect;
2208 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2209 if (!ok) {
2210 /* just abort the recovery */
2211 pr_notice("md/raid10:%s: recovery aborted due to read error\n",
2212 mdname(mddev));
2214 conf->mirrors[dw].recovery_disabled
2215 = mddev->recovery_disabled;
2216 set_bit(MD_RECOVERY_INTR,
2217 &mddev->recovery);
2218 break;
2223 sectors -= s;
2224 sect += s;
2225 idx++;
2229 static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
2231 struct r10conf *conf = mddev->private;
2232 int d;
2233 struct bio *wbio, *wbio2;
2235 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2236 fix_recovery_read_error(r10_bio);
2237 end_sync_request(r10_bio);
2238 return;
2242 * share the pages with the first bio
2243 * and submit the write request
2245 d = r10_bio->devs[1].devnum;
2246 wbio = r10_bio->devs[1].bio;
2247 wbio2 = r10_bio->devs[1].repl_bio;
2248 /* Need to test wbio2->bi_end_io before we call
2249 * generic_make_request as if the former is NULL,
2250 * the latter is free to free wbio2.
2252 if (wbio2 && !wbio2->bi_end_io)
2253 wbio2 = NULL;
2254 if (wbio->bi_end_io) {
2255 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2256 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
2257 generic_make_request(wbio);
2259 if (wbio2) {
2260 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2261 md_sync_acct(conf->mirrors[d].replacement->bdev,
2262 bio_sectors(wbio2));
2263 generic_make_request(wbio2);
2268 * Used by fix_read_error() to decay the per rdev read_errors.
2269 * We halve the read error count for every hour that has elapsed
2270 * since the last recorded read error.
2273 static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
2275 long cur_time_mon;
2276 unsigned long hours_since_last;
2277 unsigned int read_errors = atomic_read(&rdev->read_errors);
2279 cur_time_mon = ktime_get_seconds();
2281 if (rdev->last_read_error == 0) {
2282 /* first time we've seen a read error */
2283 rdev->last_read_error = cur_time_mon;
2284 return;
2287 hours_since_last = (long)(cur_time_mon -
2288 rdev->last_read_error) / 3600;
2290 rdev->last_read_error = cur_time_mon;
2293 * if hours_since_last is > the number of bits in read_errors
2294 * just set read errors to 0. We do this to avoid
2295 * overflowing the shift of read_errors by hours_since_last.
2297 if (hours_since_last >= 8 * sizeof(read_errors))
2298 atomic_set(&rdev->read_errors, 0);
2299 else
2300 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2303 static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
2304 int sectors, struct page *page, int rw)
2306 sector_t first_bad;
2307 int bad_sectors;
2309 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2310 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2311 return -1;
2312 if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
2313 /* success */
2314 return 1;
2315 if (rw == WRITE) {
2316 set_bit(WriteErrorSeen, &rdev->flags);
2317 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2318 set_bit(MD_RECOVERY_NEEDED,
2319 &rdev->mddev->recovery);
2321 /* need to record an error - either for the block or the device */
2322 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2323 md_error(rdev->mddev, rdev);
2324 return 0;
2328 * This is a kernel thread which:
2330 * 1. Retries failed read operations on working mirrors.
2331 * 2. Updates the raid superblock when problems encounter.
2332 * 3. Performs writes following reads for array synchronising.
2335 static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
2337 int sect = 0; /* Offset from r10_bio->sector */
2338 int sectors = r10_bio->sectors;
2339 struct md_rdev*rdev;
2340 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
2341 int d = r10_bio->devs[r10_bio->read_slot].devnum;
2343 /* still own a reference to this rdev, so it cannot
2344 * have been cleared recently.
2346 rdev = conf->mirrors[d].rdev;
2348 if (test_bit(Faulty, &rdev->flags))
2349 /* drive has already been failed, just ignore any
2350 more fix_read_error() attempts */
2351 return;
2353 check_decay_read_errors(mddev, rdev);
2354 atomic_inc(&rdev->read_errors);
2355 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2356 char b[BDEVNAME_SIZE];
2357 bdevname(rdev->bdev, b);
2359 pr_notice("md/raid10:%s: %s: Raid device exceeded read_error threshold [cur %d:max %d]\n",
2360 mdname(mddev), b,
2361 atomic_read(&rdev->read_errors), max_read_errors);
2362 pr_notice("md/raid10:%s: %s: Failing raid device\n",
2363 mdname(mddev), b);
2364 md_error(mddev, rdev);
2365 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
2366 return;
2369 while(sectors) {
2370 int s = sectors;
2371 int sl = r10_bio->read_slot;
2372 int success = 0;
2373 int start;
2375 if (s > (PAGE_SIZE>>9))
2376 s = PAGE_SIZE >> 9;
2378 rcu_read_lock();
2379 do {
2380 sector_t first_bad;
2381 int bad_sectors;
2383 d = r10_bio->devs[sl].devnum;
2384 rdev = rcu_dereference(conf->mirrors[d].rdev);
2385 if (rdev &&
2386 test_bit(In_sync, &rdev->flags) &&
2387 !test_bit(Faulty, &rdev->flags) &&
2388 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2389 &first_bad, &bad_sectors) == 0) {
2390 atomic_inc(&rdev->nr_pending);
2391 rcu_read_unlock();
2392 success = sync_page_io(rdev,
2393 r10_bio->devs[sl].addr +
2394 sect,
2395 s<<9,
2396 conf->tmppage,
2397 REQ_OP_READ, 0, false);
2398 rdev_dec_pending(rdev, mddev);
2399 rcu_read_lock();
2400 if (success)
2401 break;
2403 sl++;
2404 if (sl == conf->copies)
2405 sl = 0;
2406 } while (!success && sl != r10_bio->read_slot);
2407 rcu_read_unlock();
2409 if (!success) {
2410 /* Cannot read from anywhere, just mark the block
2411 * as bad on the first device to discourage future
2412 * reads.
2414 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
2415 rdev = conf->mirrors[dn].rdev;
2417 if (!rdev_set_badblocks(
2418 rdev,
2419 r10_bio->devs[r10_bio->read_slot].addr
2420 + sect,
2421 s, 0)) {
2422 md_error(mddev, rdev);
2423 r10_bio->devs[r10_bio->read_slot].bio
2424 = IO_BLOCKED;
2426 break;
2429 start = sl;
2430 /* write it back and re-read */
2431 rcu_read_lock();
2432 while (sl != r10_bio->read_slot) {
2433 char b[BDEVNAME_SIZE];
2435 if (sl==0)
2436 sl = conf->copies;
2437 sl--;
2438 d = r10_bio->devs[sl].devnum;
2439 rdev = rcu_dereference(conf->mirrors[d].rdev);
2440 if (!rdev ||
2441 test_bit(Faulty, &rdev->flags) ||
2442 !test_bit(In_sync, &rdev->flags))
2443 continue;
2445 atomic_inc(&rdev->nr_pending);
2446 rcu_read_unlock();
2447 if (r10_sync_page_io(rdev,
2448 r10_bio->devs[sl].addr +
2449 sect,
2450 s, conf->tmppage, WRITE)
2451 == 0) {
2452 /* Well, this device is dead */
2453 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %s)\n",
2454 mdname(mddev), s,
2455 (unsigned long long)(
2456 sect +
2457 choose_data_offset(r10_bio,
2458 rdev)),
2459 bdevname(rdev->bdev, b));
2460 pr_notice("md/raid10:%s: %s: failing drive\n",
2461 mdname(mddev),
2462 bdevname(rdev->bdev, b));
2464 rdev_dec_pending(rdev, mddev);
2465 rcu_read_lock();
2467 sl = start;
2468 while (sl != r10_bio->read_slot) {
2469 char b[BDEVNAME_SIZE];
2471 if (sl==0)
2472 sl = conf->copies;
2473 sl--;
2474 d = r10_bio->devs[sl].devnum;
2475 rdev = rcu_dereference(conf->mirrors[d].rdev);
2476 if (!rdev ||
2477 test_bit(Faulty, &rdev->flags) ||
2478 !test_bit(In_sync, &rdev->flags))
2479 continue;
2481 atomic_inc(&rdev->nr_pending);
2482 rcu_read_unlock();
2483 switch (r10_sync_page_io(rdev,
2484 r10_bio->devs[sl].addr +
2485 sect,
2486 s, conf->tmppage,
2487 READ)) {
2488 case 0:
2489 /* Well, this device is dead */
2490 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %s)\n",
2491 mdname(mddev), s,
2492 (unsigned long long)(
2493 sect +
2494 choose_data_offset(r10_bio, rdev)),
2495 bdevname(rdev->bdev, b));
2496 pr_notice("md/raid10:%s: %s: failing drive\n",
2497 mdname(mddev),
2498 bdevname(rdev->bdev, b));
2499 break;
2500 case 1:
2501 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %s)\n",
2502 mdname(mddev), s,
2503 (unsigned long long)(
2504 sect +
2505 choose_data_offset(r10_bio, rdev)),
2506 bdevname(rdev->bdev, b));
2507 atomic_add(s, &rdev->corrected_errors);
2510 rdev_dec_pending(rdev, mddev);
2511 rcu_read_lock();
2513 rcu_read_unlock();
2515 sectors -= s;
2516 sect += s;
2520 static int narrow_write_error(struct r10bio *r10_bio, int i)
2522 struct bio *bio = r10_bio->master_bio;
2523 struct mddev *mddev = r10_bio->mddev;
2524 struct r10conf *conf = mddev->private;
2525 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
2526 /* bio has the data to be written to slot 'i' where
2527 * we just recently had a write error.
2528 * We repeatedly clone the bio and trim down to one block,
2529 * then try the write. Where the write fails we record
2530 * a bad block.
2531 * It is conceivable that the bio doesn't exactly align with
2532 * blocks. We must handle this.
2534 * We currently own a reference to the rdev.
2537 int block_sectors;
2538 sector_t sector;
2539 int sectors;
2540 int sect_to_write = r10_bio->sectors;
2541 int ok = 1;
2543 if (rdev->badblocks.shift < 0)
2544 return 0;
2546 block_sectors = roundup(1 << rdev->badblocks.shift,
2547 bdev_logical_block_size(rdev->bdev) >> 9);
2548 sector = r10_bio->sector;
2549 sectors = ((r10_bio->sector + block_sectors)
2550 & ~(sector_t)(block_sectors - 1))
2551 - sector;
2553 while (sect_to_write) {
2554 struct bio *wbio;
2555 sector_t wsector;
2556 if (sectors > sect_to_write)
2557 sectors = sect_to_write;
2558 /* Write at 'sector' for 'sectors' */
2559 wbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
2560 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors);
2561 wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector);
2562 wbio->bi_iter.bi_sector = wsector +
2563 choose_data_offset(r10_bio, rdev);
2564 bio_set_dev(wbio, rdev->bdev);
2565 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
2567 if (submit_bio_wait(wbio) < 0)
2568 /* Failure! */
2569 ok = rdev_set_badblocks(rdev, wsector,
2570 sectors, 0)
2571 && ok;
2573 bio_put(wbio);
2574 sect_to_write -= sectors;
2575 sector += sectors;
2576 sectors = block_sectors;
2578 return ok;
2581 static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
2583 int slot = r10_bio->read_slot;
2584 struct bio *bio;
2585 struct r10conf *conf = mddev->private;
2586 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
2587 sector_t bio_last_sector;
2589 /* we got a read error. Maybe the drive is bad. Maybe just
2590 * the block and we can fix it.
2591 * We freeze all other IO, and try reading the block from
2592 * other devices. When we find one, we re-write
2593 * and check it that fixes the read error.
2594 * This is all done synchronously while the array is
2595 * frozen.
2597 bio = r10_bio->devs[slot].bio;
2598 bio_last_sector = r10_bio->devs[slot].addr + rdev->data_offset + r10_bio->sectors;
2599 bio_put(bio);
2600 r10_bio->devs[slot].bio = NULL;
2602 if (mddev->ro)
2603 r10_bio->devs[slot].bio = IO_BLOCKED;
2604 else if (!test_bit(FailFast, &rdev->flags)) {
2605 freeze_array(conf, 1);
2606 fix_read_error(conf, mddev, r10_bio);
2607 unfreeze_array(conf);
2608 } else
2609 md_error(mddev, rdev);
2611 rdev_dec_pending(rdev, mddev);
2612 allow_barrier(conf);
2613 r10_bio->state = 0;
2614 raid10_read_request(mddev, r10_bio->master_bio, r10_bio);
2617 static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
2619 /* Some sort of write request has finished and it
2620 * succeeded in writing where we thought there was a
2621 * bad block. So forget the bad block.
2622 * Or possibly if failed and we need to record
2623 * a bad block.
2625 int m;
2626 struct md_rdev *rdev;
2628 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2629 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
2630 for (m = 0; m < conf->copies; m++) {
2631 int dev = r10_bio->devs[m].devnum;
2632 rdev = conf->mirrors[dev].rdev;
2633 if (r10_bio->devs[m].bio == NULL ||
2634 r10_bio->devs[m].bio->bi_end_io == NULL)
2635 continue;
2636 if (!r10_bio->devs[m].bio->bi_status) {
2637 rdev_clear_badblocks(
2638 rdev,
2639 r10_bio->devs[m].addr,
2640 r10_bio->sectors, 0);
2641 } else {
2642 if (!rdev_set_badblocks(
2643 rdev,
2644 r10_bio->devs[m].addr,
2645 r10_bio->sectors, 0))
2646 md_error(conf->mddev, rdev);
2648 rdev = conf->mirrors[dev].replacement;
2649 if (r10_bio->devs[m].repl_bio == NULL ||
2650 r10_bio->devs[m].repl_bio->bi_end_io == NULL)
2651 continue;
2653 if (!r10_bio->devs[m].repl_bio->bi_status) {
2654 rdev_clear_badblocks(
2655 rdev,
2656 r10_bio->devs[m].addr,
2657 r10_bio->sectors, 0);
2658 } else {
2659 if (!rdev_set_badblocks(
2660 rdev,
2661 r10_bio->devs[m].addr,
2662 r10_bio->sectors, 0))
2663 md_error(conf->mddev, rdev);
2666 put_buf(r10_bio);
2667 } else {
2668 bool fail = false;
2669 for (m = 0; m < conf->copies; m++) {
2670 int dev = r10_bio->devs[m].devnum;
2671 struct bio *bio = r10_bio->devs[m].bio;
2672 rdev = conf->mirrors[dev].rdev;
2673 if (bio == IO_MADE_GOOD) {
2674 rdev_clear_badblocks(
2675 rdev,
2676 r10_bio->devs[m].addr,
2677 r10_bio->sectors, 0);
2678 rdev_dec_pending(rdev, conf->mddev);
2679 } else if (bio != NULL && bio->bi_status) {
2680 fail = true;
2681 if (!narrow_write_error(r10_bio, m)) {
2682 md_error(conf->mddev, rdev);
2683 set_bit(R10BIO_Degraded,
2684 &r10_bio->state);
2686 rdev_dec_pending(rdev, conf->mddev);
2688 bio = r10_bio->devs[m].repl_bio;
2689 rdev = conf->mirrors[dev].replacement;
2690 if (rdev && bio == IO_MADE_GOOD) {
2691 rdev_clear_badblocks(
2692 rdev,
2693 r10_bio->devs[m].addr,
2694 r10_bio->sectors, 0);
2695 rdev_dec_pending(rdev, conf->mddev);
2698 if (fail) {
2699 spin_lock_irq(&conf->device_lock);
2700 list_add(&r10_bio->retry_list, &conf->bio_end_io_list);
2701 conf->nr_queued++;
2702 spin_unlock_irq(&conf->device_lock);
2704 * In case freeze_array() is waiting for condition
2705 * nr_pending == nr_queued + extra to be true.
2707 wake_up(&conf->wait_barrier);
2708 md_wakeup_thread(conf->mddev->thread);
2709 } else {
2710 if (test_bit(R10BIO_WriteError,
2711 &r10_bio->state))
2712 close_write(r10_bio);
2713 raid_end_bio_io(r10_bio);
2718 static void raid10d(struct md_thread *thread)
2720 struct mddev *mddev = thread->mddev;
2721 struct r10bio *r10_bio;
2722 unsigned long flags;
2723 struct r10conf *conf = mddev->private;
2724 struct list_head *head = &conf->retry_list;
2725 struct blk_plug plug;
2727 md_check_recovery(mddev);
2729 if (!list_empty_careful(&conf->bio_end_io_list) &&
2730 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
2731 LIST_HEAD(tmp);
2732 spin_lock_irqsave(&conf->device_lock, flags);
2733 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
2734 while (!list_empty(&conf->bio_end_io_list)) {
2735 list_move(conf->bio_end_io_list.prev, &tmp);
2736 conf->nr_queued--;
2739 spin_unlock_irqrestore(&conf->device_lock, flags);
2740 while (!list_empty(&tmp)) {
2741 r10_bio = list_first_entry(&tmp, struct r10bio,
2742 retry_list);
2743 list_del(&r10_bio->retry_list);
2744 if (mddev->degraded)
2745 set_bit(R10BIO_Degraded, &r10_bio->state);
2747 if (test_bit(R10BIO_WriteError,
2748 &r10_bio->state))
2749 close_write(r10_bio);
2750 raid_end_bio_io(r10_bio);
2754 blk_start_plug(&plug);
2755 for (;;) {
2757 flush_pending_writes(conf);
2759 spin_lock_irqsave(&conf->device_lock, flags);
2760 if (list_empty(head)) {
2761 spin_unlock_irqrestore(&conf->device_lock, flags);
2762 break;
2764 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
2765 list_del(head->prev);
2766 conf->nr_queued--;
2767 spin_unlock_irqrestore(&conf->device_lock, flags);
2769 mddev = r10_bio->mddev;
2770 conf = mddev->private;
2771 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2772 test_bit(R10BIO_WriteError, &r10_bio->state))
2773 handle_write_completed(conf, r10_bio);
2774 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
2775 reshape_request_write(mddev, r10_bio);
2776 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
2777 sync_request_write(mddev, r10_bio);
2778 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
2779 recovery_request_write(mddev, r10_bio);
2780 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
2781 handle_read_error(mddev, r10_bio);
2782 else
2783 WARN_ON_ONCE(1);
2785 cond_resched();
2786 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
2787 md_check_recovery(mddev);
2789 blk_finish_plug(&plug);
2792 static int init_resync(struct r10conf *conf)
2794 int buffs;
2795 int i;
2797 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
2798 BUG_ON(conf->r10buf_pool);
2799 conf->have_replacement = 0;
2800 for (i = 0; i < conf->geo.raid_disks; i++)
2801 if (conf->mirrors[i].replacement)
2802 conf->have_replacement = 1;
2803 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2804 if (!conf->r10buf_pool)
2805 return -ENOMEM;
2806 conf->next_resync = 0;
2807 return 0;
2810 static struct r10bio *raid10_alloc_init_r10buf(struct r10conf *conf)
2812 struct r10bio *r10bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2813 struct rsync_pages *rp;
2814 struct bio *bio;
2815 int nalloc;
2816 int i;
2818 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
2819 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
2820 nalloc = conf->copies; /* resync */
2821 else
2822 nalloc = 2; /* recovery */
2824 for (i = 0; i < nalloc; i++) {
2825 bio = r10bio->devs[i].bio;
2826 rp = bio->bi_private;
2827 bio_reset(bio);
2828 bio->bi_private = rp;
2829 bio = r10bio->devs[i].repl_bio;
2830 if (bio) {
2831 rp = bio->bi_private;
2832 bio_reset(bio);
2833 bio->bi_private = rp;
2836 return r10bio;
2840 * perform a "sync" on one "block"
2842 * We need to make sure that no normal I/O request - particularly write
2843 * requests - conflict with active sync requests.
2845 * This is achieved by tracking pending requests and a 'barrier' concept
2846 * that can be installed to exclude normal IO requests.
2848 * Resync and recovery are handled very differently.
2849 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2851 * For resync, we iterate over virtual addresses, read all copies,
2852 * and update if there are differences. If only one copy is live,
2853 * skip it.
2854 * For recovery, we iterate over physical addresses, read a good
2855 * value for each non-in_sync drive, and over-write.
2857 * So, for recovery we may have several outstanding complex requests for a
2858 * given address, one for each out-of-sync device. We model this by allocating
2859 * a number of r10_bio structures, one for each out-of-sync device.
2860 * As we setup these structures, we collect all bio's together into a list
2861 * which we then process collectively to add pages, and then process again
2862 * to pass to generic_make_request.
2864 * The r10_bio structures are linked using a borrowed master_bio pointer.
2865 * This link is counted in ->remaining. When the r10_bio that points to NULL
2866 * has its remaining count decremented to 0, the whole complex operation
2867 * is complete.
2871 static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
2872 int *skipped)
2874 struct r10conf *conf = mddev->private;
2875 struct r10bio *r10_bio;
2876 struct bio *biolist = NULL, *bio;
2877 sector_t max_sector, nr_sectors;
2878 int i;
2879 int max_sync;
2880 sector_t sync_blocks;
2881 sector_t sectors_skipped = 0;
2882 int chunks_skipped = 0;
2883 sector_t chunk_mask = conf->geo.chunk_mask;
2884 int page_idx = 0;
2886 if (!conf->r10buf_pool)
2887 if (init_resync(conf))
2888 return 0;
2891 * Allow skipping a full rebuild for incremental assembly
2892 * of a clean array, like RAID1 does.
2894 if (mddev->bitmap == NULL &&
2895 mddev->recovery_cp == MaxSector &&
2896 mddev->reshape_position == MaxSector &&
2897 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
2898 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
2899 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
2900 conf->fullsync == 0) {
2901 *skipped = 1;
2902 return mddev->dev_sectors - sector_nr;
2905 skipped:
2906 max_sector = mddev->dev_sectors;
2907 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
2908 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2909 max_sector = mddev->resync_max_sectors;
2910 if (sector_nr >= max_sector) {
2911 /* If we aborted, we need to abort the
2912 * sync on the 'current' bitmap chucks (there can
2913 * be several when recovering multiple devices).
2914 * as we may have started syncing it but not finished.
2915 * We can find the current address in
2916 * mddev->curr_resync, but for recovery,
2917 * we need to convert that to several
2918 * virtual addresses.
2920 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
2921 end_reshape(conf);
2922 close_sync(conf);
2923 return 0;
2926 if (mddev->curr_resync < max_sector) { /* aborted */
2927 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2928 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2929 &sync_blocks, 1);
2930 else for (i = 0; i < conf->geo.raid_disks; i++) {
2931 sector_t sect =
2932 raid10_find_virt(conf, mddev->curr_resync, i);
2933 bitmap_end_sync(mddev->bitmap, sect,
2934 &sync_blocks, 1);
2936 } else {
2937 /* completed sync */
2938 if ((!mddev->bitmap || conf->fullsync)
2939 && conf->have_replacement
2940 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2941 /* Completed a full sync so the replacements
2942 * are now fully recovered.
2944 rcu_read_lock();
2945 for (i = 0; i < conf->geo.raid_disks; i++) {
2946 struct md_rdev *rdev =
2947 rcu_dereference(conf->mirrors[i].replacement);
2948 if (rdev)
2949 rdev->recovery_offset = MaxSector;
2951 rcu_read_unlock();
2953 conf->fullsync = 0;
2955 bitmap_close_sync(mddev->bitmap);
2956 close_sync(conf);
2957 *skipped = 1;
2958 return sectors_skipped;
2961 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2962 return reshape_request(mddev, sector_nr, skipped);
2964 if (chunks_skipped >= conf->geo.raid_disks) {
2965 /* if there has been nothing to do on any drive,
2966 * then there is nothing to do at all..
2968 *skipped = 1;
2969 return (max_sector - sector_nr) + sectors_skipped;
2972 if (max_sector > mddev->resync_max)
2973 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2975 /* make sure whole request will fit in a chunk - if chunks
2976 * are meaningful
2978 if (conf->geo.near_copies < conf->geo.raid_disks &&
2979 max_sector > (sector_nr | chunk_mask))
2980 max_sector = (sector_nr | chunk_mask) + 1;
2983 * If there is non-resync activity waiting for a turn, then let it
2984 * though before starting on this new sync request.
2986 if (conf->nr_waiting)
2987 schedule_timeout_uninterruptible(1);
2989 /* Again, very different code for resync and recovery.
2990 * Both must result in an r10bio with a list of bios that
2991 * have bi_end_io, bi_sector, bi_disk set,
2992 * and bi_private set to the r10bio.
2993 * For recovery, we may actually create several r10bios
2994 * with 2 bios in each, that correspond to the bios in the main one.
2995 * In this case, the subordinate r10bios link back through a
2996 * borrowed master_bio pointer, and the counter in the master
2997 * includes a ref from each subordinate.
2999 /* First, we decide what to do and set ->bi_end_io
3000 * To end_sync_read if we want to read, and
3001 * end_sync_write if we will want to write.
3004 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
3005 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3006 /* recovery... the complicated one */
3007 int j;
3008 r10_bio = NULL;
3010 for (i = 0 ; i < conf->geo.raid_disks; i++) {
3011 int still_degraded;
3012 struct r10bio *rb2;
3013 sector_t sect;
3014 int must_sync;
3015 int any_working;
3016 struct raid10_info *mirror = &conf->mirrors[i];
3017 struct md_rdev *mrdev, *mreplace;
3019 rcu_read_lock();
3020 mrdev = rcu_dereference(mirror->rdev);
3021 mreplace = rcu_dereference(mirror->replacement);
3023 if ((mrdev == NULL ||
3024 test_bit(Faulty, &mrdev->flags) ||
3025 test_bit(In_sync, &mrdev->flags)) &&
3026 (mreplace == NULL ||
3027 test_bit(Faulty, &mreplace->flags))) {
3028 rcu_read_unlock();
3029 continue;
3032 still_degraded = 0;
3033 /* want to reconstruct this device */
3034 rb2 = r10_bio;
3035 sect = raid10_find_virt(conf, sector_nr, i);
3036 if (sect >= mddev->resync_max_sectors) {
3037 /* last stripe is not complete - don't
3038 * try to recover this sector.
3040 rcu_read_unlock();
3041 continue;
3043 if (mreplace && test_bit(Faulty, &mreplace->flags))
3044 mreplace = NULL;
3045 /* Unless we are doing a full sync, or a replacement
3046 * we only need to recover the block if it is set in
3047 * the bitmap
3049 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3050 &sync_blocks, 1);
3051 if (sync_blocks < max_sync)
3052 max_sync = sync_blocks;
3053 if (!must_sync &&
3054 mreplace == NULL &&
3055 !conf->fullsync) {
3056 /* yep, skip the sync_blocks here, but don't assume
3057 * that there will never be anything to do here
3059 chunks_skipped = -1;
3060 rcu_read_unlock();
3061 continue;
3063 atomic_inc(&mrdev->nr_pending);
3064 if (mreplace)
3065 atomic_inc(&mreplace->nr_pending);
3066 rcu_read_unlock();
3068 r10_bio = raid10_alloc_init_r10buf(conf);
3069 r10_bio->state = 0;
3070 raise_barrier(conf, rb2 != NULL);
3071 atomic_set(&r10_bio->remaining, 0);
3073 r10_bio->master_bio = (struct bio*)rb2;
3074 if (rb2)
3075 atomic_inc(&rb2->remaining);
3076 r10_bio->mddev = mddev;
3077 set_bit(R10BIO_IsRecover, &r10_bio->state);
3078 r10_bio->sector = sect;
3080 raid10_find_phys(conf, r10_bio);
3082 /* Need to check if the array will still be
3083 * degraded
3085 rcu_read_lock();
3086 for (j = 0; j < conf->geo.raid_disks; j++) {
3087 struct md_rdev *rdev = rcu_dereference(
3088 conf->mirrors[j].rdev);
3089 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3090 still_degraded = 1;
3091 break;
3095 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3096 &sync_blocks, still_degraded);
3098 any_working = 0;
3099 for (j=0; j<conf->copies;j++) {
3100 int k;
3101 int d = r10_bio->devs[j].devnum;
3102 sector_t from_addr, to_addr;
3103 struct md_rdev *rdev =
3104 rcu_dereference(conf->mirrors[d].rdev);
3105 sector_t sector, first_bad;
3106 int bad_sectors;
3107 if (!rdev ||
3108 !test_bit(In_sync, &rdev->flags))
3109 continue;
3110 /* This is where we read from */
3111 any_working = 1;
3112 sector = r10_bio->devs[j].addr;
3114 if (is_badblock(rdev, sector, max_sync,
3115 &first_bad, &bad_sectors)) {
3116 if (first_bad > sector)
3117 max_sync = first_bad - sector;
3118 else {
3119 bad_sectors -= (sector
3120 - first_bad);
3121 if (max_sync > bad_sectors)
3122 max_sync = bad_sectors;
3123 continue;
3126 bio = r10_bio->devs[0].bio;
3127 bio->bi_next = biolist;
3128 biolist = bio;
3129 bio->bi_end_io = end_sync_read;
3130 bio_set_op_attrs(bio, REQ_OP_READ, 0);
3131 if (test_bit(FailFast, &rdev->flags))
3132 bio->bi_opf |= MD_FAILFAST;
3133 from_addr = r10_bio->devs[j].addr;
3134 bio->bi_iter.bi_sector = from_addr +
3135 rdev->data_offset;
3136 bio_set_dev(bio, rdev->bdev);
3137 atomic_inc(&rdev->nr_pending);
3138 /* and we write to 'i' (if not in_sync) */
3140 for (k=0; k<conf->copies; k++)
3141 if (r10_bio->devs[k].devnum == i)
3142 break;
3143 BUG_ON(k == conf->copies);
3144 to_addr = r10_bio->devs[k].addr;
3145 r10_bio->devs[0].devnum = d;
3146 r10_bio->devs[0].addr = from_addr;
3147 r10_bio->devs[1].devnum = i;
3148 r10_bio->devs[1].addr = to_addr;
3150 if (!test_bit(In_sync, &mrdev->flags)) {
3151 bio = r10_bio->devs[1].bio;
3152 bio->bi_next = biolist;
3153 biolist = bio;
3154 bio->bi_end_io = end_sync_write;
3155 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
3156 bio->bi_iter.bi_sector = to_addr
3157 + mrdev->data_offset;
3158 bio_set_dev(bio, mrdev->bdev);
3159 atomic_inc(&r10_bio->remaining);
3160 } else
3161 r10_bio->devs[1].bio->bi_end_io = NULL;
3163 /* and maybe write to replacement */
3164 bio = r10_bio->devs[1].repl_bio;
3165 if (bio)
3166 bio->bi_end_io = NULL;
3167 /* Note: if mreplace != NULL, then bio
3168 * cannot be NULL as r10buf_pool_alloc will
3169 * have allocated it.
3170 * So the second test here is pointless.
3171 * But it keeps semantic-checkers happy, and
3172 * this comment keeps human reviewers
3173 * happy.
3175 if (mreplace == NULL || bio == NULL ||
3176 test_bit(Faulty, &mreplace->flags))
3177 break;
3178 bio->bi_next = biolist;
3179 biolist = bio;
3180 bio->bi_end_io = end_sync_write;
3181 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
3182 bio->bi_iter.bi_sector = to_addr +
3183 mreplace->data_offset;
3184 bio_set_dev(bio, mreplace->bdev);
3185 atomic_inc(&r10_bio->remaining);
3186 break;
3188 rcu_read_unlock();
3189 if (j == conf->copies) {
3190 /* Cannot recover, so abort the recovery or
3191 * record a bad block */
3192 if (any_working) {
3193 /* problem is that there are bad blocks
3194 * on other device(s)
3196 int k;
3197 for (k = 0; k < conf->copies; k++)
3198 if (r10_bio->devs[k].devnum == i)
3199 break;
3200 if (!test_bit(In_sync,
3201 &mrdev->flags)
3202 && !rdev_set_badblocks(
3203 mrdev,
3204 r10_bio->devs[k].addr,
3205 max_sync, 0))
3206 any_working = 0;
3207 if (mreplace &&
3208 !rdev_set_badblocks(
3209 mreplace,
3210 r10_bio->devs[k].addr,
3211 max_sync, 0))
3212 any_working = 0;
3214 if (!any_working) {
3215 if (!test_and_set_bit(MD_RECOVERY_INTR,
3216 &mddev->recovery))
3217 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
3218 mdname(mddev));
3219 mirror->recovery_disabled
3220 = mddev->recovery_disabled;
3222 put_buf(r10_bio);
3223 if (rb2)
3224 atomic_dec(&rb2->remaining);
3225 r10_bio = rb2;
3226 rdev_dec_pending(mrdev, mddev);
3227 if (mreplace)
3228 rdev_dec_pending(mreplace, mddev);
3229 break;
3231 rdev_dec_pending(mrdev, mddev);
3232 if (mreplace)
3233 rdev_dec_pending(mreplace, mddev);
3234 if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) {
3235 /* Only want this if there is elsewhere to
3236 * read from. 'j' is currently the first
3237 * readable copy.
3239 int targets = 1;
3240 for (; j < conf->copies; j++) {
3241 int d = r10_bio->devs[j].devnum;
3242 if (conf->mirrors[d].rdev &&
3243 test_bit(In_sync,
3244 &conf->mirrors[d].rdev->flags))
3245 targets++;
3247 if (targets == 1)
3248 r10_bio->devs[0].bio->bi_opf
3249 &= ~MD_FAILFAST;
3252 if (biolist == NULL) {
3253 while (r10_bio) {
3254 struct r10bio *rb2 = r10_bio;
3255 r10_bio = (struct r10bio*) rb2->master_bio;
3256 rb2->master_bio = NULL;
3257 put_buf(rb2);
3259 goto giveup;
3261 } else {
3262 /* resync. Schedule a read for every block at this virt offset */
3263 int count = 0;
3265 bitmap_cond_end_sync(mddev->bitmap, sector_nr, 0);
3267 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
3268 &sync_blocks, mddev->degraded) &&
3269 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3270 &mddev->recovery)) {
3271 /* We can skip this block */
3272 *skipped = 1;
3273 return sync_blocks + sectors_skipped;
3275 if (sync_blocks < max_sync)
3276 max_sync = sync_blocks;
3277 r10_bio = raid10_alloc_init_r10buf(conf);
3278 r10_bio->state = 0;
3280 r10_bio->mddev = mddev;
3281 atomic_set(&r10_bio->remaining, 0);
3282 raise_barrier(conf, 0);
3283 conf->next_resync = sector_nr;
3285 r10_bio->master_bio = NULL;
3286 r10_bio->sector = sector_nr;
3287 set_bit(R10BIO_IsSync, &r10_bio->state);
3288 raid10_find_phys(conf, r10_bio);
3289 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
3291 for (i = 0; i < conf->copies; i++) {
3292 int d = r10_bio->devs[i].devnum;
3293 sector_t first_bad, sector;
3294 int bad_sectors;
3295 struct md_rdev *rdev;
3297 if (r10_bio->devs[i].repl_bio)
3298 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3300 bio = r10_bio->devs[i].bio;
3301 bio->bi_status = BLK_STS_IOERR;
3302 rcu_read_lock();
3303 rdev = rcu_dereference(conf->mirrors[d].rdev);
3304 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3305 rcu_read_unlock();
3306 continue;
3308 sector = r10_bio->devs[i].addr;
3309 if (is_badblock(rdev, sector, max_sync,
3310 &first_bad, &bad_sectors)) {
3311 if (first_bad > sector)
3312 max_sync = first_bad - sector;
3313 else {
3314 bad_sectors -= (sector - first_bad);
3315 if (max_sync > bad_sectors)
3316 max_sync = bad_sectors;
3317 rcu_read_unlock();
3318 continue;
3321 atomic_inc(&rdev->nr_pending);
3322 atomic_inc(&r10_bio->remaining);
3323 bio->bi_next = biolist;
3324 biolist = bio;
3325 bio->bi_end_io = end_sync_read;
3326 bio_set_op_attrs(bio, REQ_OP_READ, 0);
3327 if (test_bit(FailFast, &rdev->flags))
3328 bio->bi_opf |= MD_FAILFAST;
3329 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3330 bio_set_dev(bio, rdev->bdev);
3331 count++;
3333 rdev = rcu_dereference(conf->mirrors[d].replacement);
3334 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3335 rcu_read_unlock();
3336 continue;
3338 atomic_inc(&rdev->nr_pending);
3340 /* Need to set up for writing to the replacement */
3341 bio = r10_bio->devs[i].repl_bio;
3342 bio->bi_status = BLK_STS_IOERR;
3344 sector = r10_bio->devs[i].addr;
3345 bio->bi_next = biolist;
3346 biolist = bio;
3347 bio->bi_end_io = end_sync_write;
3348 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
3349 if (test_bit(FailFast, &rdev->flags))
3350 bio->bi_opf |= MD_FAILFAST;
3351 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3352 bio_set_dev(bio, rdev->bdev);
3353 count++;
3354 rcu_read_unlock();
3357 if (count < 2) {
3358 for (i=0; i<conf->copies; i++) {
3359 int d = r10_bio->devs[i].devnum;
3360 if (r10_bio->devs[i].bio->bi_end_io)
3361 rdev_dec_pending(conf->mirrors[d].rdev,
3362 mddev);
3363 if (r10_bio->devs[i].repl_bio &&
3364 r10_bio->devs[i].repl_bio->bi_end_io)
3365 rdev_dec_pending(
3366 conf->mirrors[d].replacement,
3367 mddev);
3369 put_buf(r10_bio);
3370 biolist = NULL;
3371 goto giveup;
3375 nr_sectors = 0;
3376 if (sector_nr + max_sync < max_sector)
3377 max_sector = sector_nr + max_sync;
3378 do {
3379 struct page *page;
3380 int len = PAGE_SIZE;
3381 if (sector_nr + (len>>9) > max_sector)
3382 len = (max_sector - sector_nr) << 9;
3383 if (len == 0)
3384 break;
3385 for (bio= biolist ; bio ; bio=bio->bi_next) {
3386 struct resync_pages *rp = get_resync_pages(bio);
3387 page = resync_fetch_page(rp, page_idx);
3389 * won't fail because the vec table is big enough
3390 * to hold all these pages
3392 bio_add_page(bio, page, len, 0);
3394 nr_sectors += len>>9;
3395 sector_nr += len>>9;
3396 } while (++page_idx < RESYNC_PAGES);
3397 r10_bio->sectors = nr_sectors;
3399 while (biolist) {
3400 bio = biolist;
3401 biolist = biolist->bi_next;
3403 bio->bi_next = NULL;
3404 r10_bio = get_resync_r10bio(bio);
3405 r10_bio->sectors = nr_sectors;
3407 if (bio->bi_end_io == end_sync_read) {
3408 md_sync_acct_bio(bio, nr_sectors);
3409 bio->bi_status = 0;
3410 generic_make_request(bio);
3414 if (sectors_skipped)
3415 /* pretend they weren't skipped, it makes
3416 * no important difference in this case
3418 md_done_sync(mddev, sectors_skipped, 1);
3420 return sectors_skipped + nr_sectors;
3421 giveup:
3422 /* There is nowhere to write, so all non-sync
3423 * drives must be failed or in resync, all drives
3424 * have a bad block, so try the next chunk...
3426 if (sector_nr + max_sync < max_sector)
3427 max_sector = sector_nr + max_sync;
3429 sectors_skipped += (max_sector - sector_nr);
3430 chunks_skipped ++;
3431 sector_nr = max_sector;
3432 goto skipped;
3435 static sector_t
3436 raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
3438 sector_t size;
3439 struct r10conf *conf = mddev->private;
3441 if (!raid_disks)
3442 raid_disks = min(conf->geo.raid_disks,
3443 conf->prev.raid_disks);
3444 if (!sectors)
3445 sectors = conf->dev_sectors;
3447 size = sectors >> conf->geo.chunk_shift;
3448 sector_div(size, conf->geo.far_copies);
3449 size = size * raid_disks;
3450 sector_div(size, conf->geo.near_copies);
3452 return size << conf->geo.chunk_shift;
3455 static void calc_sectors(struct r10conf *conf, sector_t size)
3457 /* Calculate the number of sectors-per-device that will
3458 * actually be used, and set conf->dev_sectors and
3459 * conf->stride
3462 size = size >> conf->geo.chunk_shift;
3463 sector_div(size, conf->geo.far_copies);
3464 size = size * conf->geo.raid_disks;
3465 sector_div(size, conf->geo.near_copies);
3466 /* 'size' is now the number of chunks in the array */
3467 /* calculate "used chunks per device" */
3468 size = size * conf->copies;
3470 /* We need to round up when dividing by raid_disks to
3471 * get the stride size.
3473 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
3475 conf->dev_sectors = size << conf->geo.chunk_shift;
3477 if (conf->geo.far_offset)
3478 conf->geo.stride = 1 << conf->geo.chunk_shift;
3479 else {
3480 sector_div(size, conf->geo.far_copies);
3481 conf->geo.stride = size << conf->geo.chunk_shift;
3485 enum geo_type {geo_new, geo_old, geo_start};
3486 static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3488 int nc, fc, fo;
3489 int layout, chunk, disks;
3490 switch (new) {
3491 case geo_old:
3492 layout = mddev->layout;
3493 chunk = mddev->chunk_sectors;
3494 disks = mddev->raid_disks - mddev->delta_disks;
3495 break;
3496 case geo_new:
3497 layout = mddev->new_layout;
3498 chunk = mddev->new_chunk_sectors;
3499 disks = mddev->raid_disks;
3500 break;
3501 default: /* avoid 'may be unused' warnings */
3502 case geo_start: /* new when starting reshape - raid_disks not
3503 * updated yet. */
3504 layout = mddev->new_layout;
3505 chunk = mddev->new_chunk_sectors;
3506 disks = mddev->raid_disks + mddev->delta_disks;
3507 break;
3509 if (layout >> 19)
3510 return -1;
3511 if (chunk < (PAGE_SIZE >> 9) ||
3512 !is_power_of_2(chunk))
3513 return -2;
3514 nc = layout & 255;
3515 fc = (layout >> 8) & 255;
3516 fo = layout & (1<<16);
3517 geo->raid_disks = disks;
3518 geo->near_copies = nc;
3519 geo->far_copies = fc;
3520 geo->far_offset = fo;
3521 switch (layout >> 17) {
3522 case 0: /* original layout. simple but not always optimal */
3523 geo->far_set_size = disks;
3524 break;
3525 case 1: /* "improved" layout which was buggy. Hopefully no-one is
3526 * actually using this, but leave code here just in case.*/
3527 geo->far_set_size = disks/fc;
3528 WARN(geo->far_set_size < fc,
3529 "This RAID10 layout does not provide data safety - please backup and create new array\n");
3530 break;
3531 case 2: /* "improved" layout fixed to match documentation */
3532 geo->far_set_size = fc * nc;
3533 break;
3534 default: /* Not a valid layout */
3535 return -1;
3537 geo->chunk_mask = chunk - 1;
3538 geo->chunk_shift = ffz(~chunk);
3539 return nc*fc;
3542 static struct r10conf *setup_conf(struct mddev *mddev)
3544 struct r10conf *conf = NULL;
3545 int err = -EINVAL;
3546 struct geom geo;
3547 int copies;
3549 copies = setup_geo(&geo, mddev, geo_new);
3551 if (copies == -2) {
3552 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
3553 mdname(mddev), PAGE_SIZE);
3554 goto out;
3557 if (copies < 2 || copies > mddev->raid_disks) {
3558 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3559 mdname(mddev), mddev->new_layout);
3560 goto out;
3563 err = -ENOMEM;
3564 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
3565 if (!conf)
3566 goto out;
3568 /* FIXME calc properly */
3569 conf->mirrors = kzalloc(sizeof(struct raid10_info)*(mddev->raid_disks +
3570 max(0,-mddev->delta_disks)),
3571 GFP_KERNEL);
3572 if (!conf->mirrors)
3573 goto out;
3575 conf->tmppage = alloc_page(GFP_KERNEL);
3576 if (!conf->tmppage)
3577 goto out;
3579 conf->geo = geo;
3580 conf->copies = copies;
3581 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
3582 r10bio_pool_free, conf);
3583 if (!conf->r10bio_pool)
3584 goto out;
3586 conf->bio_split = bioset_create(BIO_POOL_SIZE, 0, 0);
3587 if (!conf->bio_split)
3588 goto out;
3590 calc_sectors(conf, mddev->dev_sectors);
3591 if (mddev->reshape_position == MaxSector) {
3592 conf->prev = conf->geo;
3593 conf->reshape_progress = MaxSector;
3594 } else {
3595 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
3596 err = -EINVAL;
3597 goto out;
3599 conf->reshape_progress = mddev->reshape_position;
3600 if (conf->prev.far_offset)
3601 conf->prev.stride = 1 << conf->prev.chunk_shift;
3602 else
3603 /* far_copies must be 1 */
3604 conf->prev.stride = conf->dev_sectors;
3606 conf->reshape_safe = conf->reshape_progress;
3607 spin_lock_init(&conf->device_lock);
3608 INIT_LIST_HEAD(&conf->retry_list);
3609 INIT_LIST_HEAD(&conf->bio_end_io_list);
3611 spin_lock_init(&conf->resync_lock);
3612 init_waitqueue_head(&conf->wait_barrier);
3613 atomic_set(&conf->nr_pending, 0);
3615 conf->thread = md_register_thread(raid10d, mddev, "raid10");
3616 if (!conf->thread)
3617 goto out;
3619 conf->mddev = mddev;
3620 return conf;
3622 out:
3623 if (conf) {
3624 mempool_destroy(conf->r10bio_pool);
3625 kfree(conf->mirrors);
3626 safe_put_page(conf->tmppage);
3627 if (conf->bio_split)
3628 bioset_free(conf->bio_split);
3629 kfree(conf);
3631 return ERR_PTR(err);
3634 static int raid10_run(struct mddev *mddev)
3636 struct r10conf *conf;
3637 int i, disk_idx, chunk_size;
3638 struct raid10_info *disk;
3639 struct md_rdev *rdev;
3640 sector_t size;
3641 sector_t min_offset_diff = 0;
3642 int first = 1;
3643 bool discard_supported = false;
3645 if (mddev_init_writes_pending(mddev) < 0)
3646 return -ENOMEM;
3648 if (mddev->private == NULL) {
3649 conf = setup_conf(mddev);
3650 if (IS_ERR(conf))
3651 return PTR_ERR(conf);
3652 mddev->private = conf;
3654 conf = mddev->private;
3655 if (!conf)
3656 goto out;
3658 mddev->thread = conf->thread;
3659 conf->thread = NULL;
3661 chunk_size = mddev->chunk_sectors << 9;
3662 if (mddev->queue) {
3663 blk_queue_max_discard_sectors(mddev->queue,
3664 mddev->chunk_sectors);
3665 blk_queue_max_write_same_sectors(mddev->queue, 0);
3666 blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
3667 blk_queue_io_min(mddev->queue, chunk_size);
3668 if (conf->geo.raid_disks % conf->geo.near_copies)
3669 blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
3670 else
3671 blk_queue_io_opt(mddev->queue, chunk_size *
3672 (conf->geo.raid_disks / conf->geo.near_copies));
3675 rdev_for_each(rdev, mddev) {
3676 long long diff;
3678 disk_idx = rdev->raid_disk;
3679 if (disk_idx < 0)
3680 continue;
3681 if (disk_idx >= conf->geo.raid_disks &&
3682 disk_idx >= conf->prev.raid_disks)
3683 continue;
3684 disk = conf->mirrors + disk_idx;
3686 if (test_bit(Replacement, &rdev->flags)) {
3687 if (disk->replacement)
3688 goto out_free_conf;
3689 disk->replacement = rdev;
3690 } else {
3691 if (disk->rdev)
3692 goto out_free_conf;
3693 disk->rdev = rdev;
3695 diff = (rdev->new_data_offset - rdev->data_offset);
3696 if (!mddev->reshape_backwards)
3697 diff = -diff;
3698 if (diff < 0)
3699 diff = 0;
3700 if (first || diff < min_offset_diff)
3701 min_offset_diff = diff;
3703 if (mddev->gendisk)
3704 disk_stack_limits(mddev->gendisk, rdev->bdev,
3705 rdev->data_offset << 9);
3707 disk->head_position = 0;
3709 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3710 discard_supported = true;
3711 first = 0;
3714 if (mddev->queue) {
3715 if (discard_supported)
3716 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
3717 mddev->queue);
3718 else
3719 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
3720 mddev->queue);
3722 /* need to check that every block has at least one working mirror */
3723 if (!enough(conf, -1)) {
3724 pr_err("md/raid10:%s: not enough operational mirrors.\n",
3725 mdname(mddev));
3726 goto out_free_conf;
3729 if (conf->reshape_progress != MaxSector) {
3730 /* must ensure that shape change is supported */
3731 if (conf->geo.far_copies != 1 &&
3732 conf->geo.far_offset == 0)
3733 goto out_free_conf;
3734 if (conf->prev.far_copies != 1 &&
3735 conf->prev.far_offset == 0)
3736 goto out_free_conf;
3739 mddev->degraded = 0;
3740 for (i = 0;
3741 i < conf->geo.raid_disks
3742 || i < conf->prev.raid_disks;
3743 i++) {
3745 disk = conf->mirrors + i;
3747 if (!disk->rdev && disk->replacement) {
3748 /* The replacement is all we have - use it */
3749 disk->rdev = disk->replacement;
3750 disk->replacement = NULL;
3751 clear_bit(Replacement, &disk->rdev->flags);
3754 if (!disk->rdev ||
3755 !test_bit(In_sync, &disk->rdev->flags)) {
3756 disk->head_position = 0;
3757 mddev->degraded++;
3758 if (disk->rdev &&
3759 disk->rdev->saved_raid_disk < 0)
3760 conf->fullsync = 1;
3763 if (disk->replacement &&
3764 !test_bit(In_sync, &disk->replacement->flags) &&
3765 disk->replacement->saved_raid_disk < 0) {
3766 conf->fullsync = 1;
3769 disk->recovery_disabled = mddev->recovery_disabled - 1;
3772 if (mddev->recovery_cp != MaxSector)
3773 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
3774 mdname(mddev));
3775 pr_info("md/raid10:%s: active with %d out of %d devices\n",
3776 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
3777 conf->geo.raid_disks);
3779 * Ok, everything is just fine now
3781 mddev->dev_sectors = conf->dev_sectors;
3782 size = raid10_size(mddev, 0, 0);
3783 md_set_array_sectors(mddev, size);
3784 mddev->resync_max_sectors = size;
3785 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
3787 if (mddev->queue) {
3788 int stripe = conf->geo.raid_disks *
3789 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
3791 /* Calculate max read-ahead size.
3792 * We need to readahead at least twice a whole stripe....
3793 * maybe...
3795 stripe /= conf->geo.near_copies;
3796 if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
3797 mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
3800 if (md_integrity_register(mddev))
3801 goto out_free_conf;
3803 if (conf->reshape_progress != MaxSector) {
3804 unsigned long before_length, after_length;
3806 before_length = ((1 << conf->prev.chunk_shift) *
3807 conf->prev.far_copies);
3808 after_length = ((1 << conf->geo.chunk_shift) *
3809 conf->geo.far_copies);
3811 if (max(before_length, after_length) > min_offset_diff) {
3812 /* This cannot work */
3813 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
3814 goto out_free_conf;
3816 conf->offset_diff = min_offset_diff;
3818 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3819 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3820 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3821 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3822 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3823 "reshape");
3826 return 0;
3828 out_free_conf:
3829 md_unregister_thread(&mddev->thread);
3830 mempool_destroy(conf->r10bio_pool);
3831 safe_put_page(conf->tmppage);
3832 kfree(conf->mirrors);
3833 kfree(conf);
3834 mddev->private = NULL;
3835 out:
3836 return -EIO;
3839 static void raid10_free(struct mddev *mddev, void *priv)
3841 struct r10conf *conf = priv;
3843 mempool_destroy(conf->r10bio_pool);
3844 safe_put_page(conf->tmppage);
3845 kfree(conf->mirrors);
3846 kfree(conf->mirrors_old);
3847 kfree(conf->mirrors_new);
3848 if (conf->bio_split)
3849 bioset_free(conf->bio_split);
3850 kfree(conf);
3853 static void raid10_quiesce(struct mddev *mddev, int quiesce)
3855 struct r10conf *conf = mddev->private;
3857 if (quiesce)
3858 raise_barrier(conf, 0);
3859 else
3860 lower_barrier(conf);
3863 static int raid10_resize(struct mddev *mddev, sector_t sectors)
3865 /* Resize of 'far' arrays is not supported.
3866 * For 'near' and 'offset' arrays we can set the
3867 * number of sectors used to be an appropriate multiple
3868 * of the chunk size.
3869 * For 'offset', this is far_copies*chunksize.
3870 * For 'near' the multiplier is the LCM of
3871 * near_copies and raid_disks.
3872 * So if far_copies > 1 && !far_offset, fail.
3873 * Else find LCM(raid_disks, near_copy)*far_copies and
3874 * multiply by chunk_size. Then round to this number.
3875 * This is mostly done by raid10_size()
3877 struct r10conf *conf = mddev->private;
3878 sector_t oldsize, size;
3880 if (mddev->reshape_position != MaxSector)
3881 return -EBUSY;
3883 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
3884 return -EINVAL;
3886 oldsize = raid10_size(mddev, 0, 0);
3887 size = raid10_size(mddev, sectors, 0);
3888 if (mddev->external_size &&
3889 mddev->array_sectors > size)
3890 return -EINVAL;
3891 if (mddev->bitmap) {
3892 int ret = bitmap_resize(mddev->bitmap, size, 0, 0);
3893 if (ret)
3894 return ret;
3896 md_set_array_sectors(mddev, size);
3897 if (sectors > mddev->dev_sectors &&
3898 mddev->recovery_cp > oldsize) {
3899 mddev->recovery_cp = oldsize;
3900 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3902 calc_sectors(conf, sectors);
3903 mddev->dev_sectors = conf->dev_sectors;
3904 mddev->resync_max_sectors = size;
3905 return 0;
3908 static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs)
3910 struct md_rdev *rdev;
3911 struct r10conf *conf;
3913 if (mddev->degraded > 0) {
3914 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
3915 mdname(mddev));
3916 return ERR_PTR(-EINVAL);
3918 sector_div(size, devs);
3920 /* Set new parameters */
3921 mddev->new_level = 10;
3922 /* new layout: far_copies = 1, near_copies = 2 */
3923 mddev->new_layout = (1<<8) + 2;
3924 mddev->new_chunk_sectors = mddev->chunk_sectors;
3925 mddev->delta_disks = mddev->raid_disks;
3926 mddev->raid_disks *= 2;
3927 /* make sure it will be not marked as dirty */
3928 mddev->recovery_cp = MaxSector;
3929 mddev->dev_sectors = size;
3931 conf = setup_conf(mddev);
3932 if (!IS_ERR(conf)) {
3933 rdev_for_each(rdev, mddev)
3934 if (rdev->raid_disk >= 0) {
3935 rdev->new_raid_disk = rdev->raid_disk * 2;
3936 rdev->sectors = size;
3938 conf->barrier = 1;
3941 return conf;
3944 static void *raid10_takeover(struct mddev *mddev)
3946 struct r0conf *raid0_conf;
3948 /* raid10 can take over:
3949 * raid0 - providing it has only two drives
3951 if (mddev->level == 0) {
3952 /* for raid0 takeover only one zone is supported */
3953 raid0_conf = mddev->private;
3954 if (raid0_conf->nr_strip_zones > 1) {
3955 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
3956 mdname(mddev));
3957 return ERR_PTR(-EINVAL);
3959 return raid10_takeover_raid0(mddev,
3960 raid0_conf->strip_zone->zone_end,
3961 raid0_conf->strip_zone->nb_dev);
3963 return ERR_PTR(-EINVAL);
3966 static int raid10_check_reshape(struct mddev *mddev)
3968 /* Called when there is a request to change
3969 * - layout (to ->new_layout)
3970 * - chunk size (to ->new_chunk_sectors)
3971 * - raid_disks (by delta_disks)
3972 * or when trying to restart a reshape that was ongoing.
3974 * We need to validate the request and possibly allocate
3975 * space if that might be an issue later.
3977 * Currently we reject any reshape of a 'far' mode array,
3978 * allow chunk size to change if new is generally acceptable,
3979 * allow raid_disks to increase, and allow
3980 * a switch between 'near' mode and 'offset' mode.
3982 struct r10conf *conf = mddev->private;
3983 struct geom geo;
3985 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
3986 return -EINVAL;
3988 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
3989 /* mustn't change number of copies */
3990 return -EINVAL;
3991 if (geo.far_copies > 1 && !geo.far_offset)
3992 /* Cannot switch to 'far' mode */
3993 return -EINVAL;
3995 if (mddev->array_sectors & geo.chunk_mask)
3996 /* not factor of array size */
3997 return -EINVAL;
3999 if (!enough(conf, -1))
4000 return -EINVAL;
4002 kfree(conf->mirrors_new);
4003 conf->mirrors_new = NULL;
4004 if (mddev->delta_disks > 0) {
4005 /* allocate new 'mirrors' list */
4006 conf->mirrors_new = kzalloc(
4007 sizeof(struct raid10_info)
4008 *(mddev->raid_disks +
4009 mddev->delta_disks),
4010 GFP_KERNEL);
4011 if (!conf->mirrors_new)
4012 return -ENOMEM;
4014 return 0;
4018 * Need to check if array has failed when deciding whether to:
4019 * - start an array
4020 * - remove non-faulty devices
4021 * - add a spare
4022 * - allow a reshape
4023 * This determination is simple when no reshape is happening.
4024 * However if there is a reshape, we need to carefully check
4025 * both the before and after sections.
4026 * This is because some failed devices may only affect one
4027 * of the two sections, and some non-in_sync devices may
4028 * be insync in the section most affected by failed devices.
4030 static int calc_degraded(struct r10conf *conf)
4032 int degraded, degraded2;
4033 int i;
4035 rcu_read_lock();
4036 degraded = 0;
4037 /* 'prev' section first */
4038 for (i = 0; i < conf->prev.raid_disks; i++) {
4039 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4040 if (!rdev || test_bit(Faulty, &rdev->flags))
4041 degraded++;
4042 else if (!test_bit(In_sync, &rdev->flags))
4043 /* When we can reduce the number of devices in
4044 * an array, this might not contribute to
4045 * 'degraded'. It does now.
4047 degraded++;
4049 rcu_read_unlock();
4050 if (conf->geo.raid_disks == conf->prev.raid_disks)
4051 return degraded;
4052 rcu_read_lock();
4053 degraded2 = 0;
4054 for (i = 0; i < conf->geo.raid_disks; i++) {
4055 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4056 if (!rdev || test_bit(Faulty, &rdev->flags))
4057 degraded2++;
4058 else if (!test_bit(In_sync, &rdev->flags)) {
4059 /* If reshape is increasing the number of devices,
4060 * this section has already been recovered, so
4061 * it doesn't contribute to degraded.
4062 * else it does.
4064 if (conf->geo.raid_disks <= conf->prev.raid_disks)
4065 degraded2++;
4068 rcu_read_unlock();
4069 if (degraded2 > degraded)
4070 return degraded2;
4071 return degraded;
4074 static int raid10_start_reshape(struct mddev *mddev)
4076 /* A 'reshape' has been requested. This commits
4077 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4078 * This also checks if there are enough spares and adds them
4079 * to the array.
4080 * We currently require enough spares to make the final
4081 * array non-degraded. We also require that the difference
4082 * between old and new data_offset - on each device - is
4083 * enough that we never risk over-writing.
4086 unsigned long before_length, after_length;
4087 sector_t min_offset_diff = 0;
4088 int first = 1;
4089 struct geom new;
4090 struct r10conf *conf = mddev->private;
4091 struct md_rdev *rdev;
4092 int spares = 0;
4093 int ret;
4095 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4096 return -EBUSY;
4098 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4099 return -EINVAL;
4101 before_length = ((1 << conf->prev.chunk_shift) *
4102 conf->prev.far_copies);
4103 after_length = ((1 << conf->geo.chunk_shift) *
4104 conf->geo.far_copies);
4106 rdev_for_each(rdev, mddev) {
4107 if (!test_bit(In_sync, &rdev->flags)
4108 && !test_bit(Faulty, &rdev->flags))
4109 spares++;
4110 if (rdev->raid_disk >= 0) {
4111 long long diff = (rdev->new_data_offset
4112 - rdev->data_offset);
4113 if (!mddev->reshape_backwards)
4114 diff = -diff;
4115 if (diff < 0)
4116 diff = 0;
4117 if (first || diff < min_offset_diff)
4118 min_offset_diff = diff;
4119 first = 0;
4123 if (max(before_length, after_length) > min_offset_diff)
4124 return -EINVAL;
4126 if (spares < mddev->delta_disks)
4127 return -EINVAL;
4129 conf->offset_diff = min_offset_diff;
4130 spin_lock_irq(&conf->device_lock);
4131 if (conf->mirrors_new) {
4132 memcpy(conf->mirrors_new, conf->mirrors,
4133 sizeof(struct raid10_info)*conf->prev.raid_disks);
4134 smp_mb();
4135 kfree(conf->mirrors_old);
4136 conf->mirrors_old = conf->mirrors;
4137 conf->mirrors = conf->mirrors_new;
4138 conf->mirrors_new = NULL;
4140 setup_geo(&conf->geo, mddev, geo_start);
4141 smp_mb();
4142 if (mddev->reshape_backwards) {
4143 sector_t size = raid10_size(mddev, 0, 0);
4144 if (size < mddev->array_sectors) {
4145 spin_unlock_irq(&conf->device_lock);
4146 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4147 mdname(mddev));
4148 return -EINVAL;
4150 mddev->resync_max_sectors = size;
4151 conf->reshape_progress = size;
4152 } else
4153 conf->reshape_progress = 0;
4154 conf->reshape_safe = conf->reshape_progress;
4155 spin_unlock_irq(&conf->device_lock);
4157 if (mddev->delta_disks && mddev->bitmap) {
4158 ret = bitmap_resize(mddev->bitmap,
4159 raid10_size(mddev, 0,
4160 conf->geo.raid_disks),
4161 0, 0);
4162 if (ret)
4163 goto abort;
4165 if (mddev->delta_disks > 0) {
4166 rdev_for_each(rdev, mddev)
4167 if (rdev->raid_disk < 0 &&
4168 !test_bit(Faulty, &rdev->flags)) {
4169 if (raid10_add_disk(mddev, rdev) == 0) {
4170 if (rdev->raid_disk >=
4171 conf->prev.raid_disks)
4172 set_bit(In_sync, &rdev->flags);
4173 else
4174 rdev->recovery_offset = 0;
4176 if (sysfs_link_rdev(mddev, rdev))
4177 /* Failure here is OK */;
4179 } else if (rdev->raid_disk >= conf->prev.raid_disks
4180 && !test_bit(Faulty, &rdev->flags)) {
4181 /* This is a spare that was manually added */
4182 set_bit(In_sync, &rdev->flags);
4185 /* When a reshape changes the number of devices,
4186 * ->degraded is measured against the larger of the
4187 * pre and post numbers.
4189 spin_lock_irq(&conf->device_lock);
4190 mddev->degraded = calc_degraded(conf);
4191 spin_unlock_irq(&conf->device_lock);
4192 mddev->raid_disks = conf->geo.raid_disks;
4193 mddev->reshape_position = conf->reshape_progress;
4194 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
4196 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4197 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4198 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
4199 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4200 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4202 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4203 "reshape");
4204 if (!mddev->sync_thread) {
4205 ret = -EAGAIN;
4206 goto abort;
4208 conf->reshape_checkpoint = jiffies;
4209 md_wakeup_thread(mddev->sync_thread);
4210 md_new_event(mddev);
4211 return 0;
4213 abort:
4214 mddev->recovery = 0;
4215 spin_lock_irq(&conf->device_lock);
4216 conf->geo = conf->prev;
4217 mddev->raid_disks = conf->geo.raid_disks;
4218 rdev_for_each(rdev, mddev)
4219 rdev->new_data_offset = rdev->data_offset;
4220 smp_wmb();
4221 conf->reshape_progress = MaxSector;
4222 conf->reshape_safe = MaxSector;
4223 mddev->reshape_position = MaxSector;
4224 spin_unlock_irq(&conf->device_lock);
4225 return ret;
4228 /* Calculate the last device-address that could contain
4229 * any block from the chunk that includes the array-address 's'
4230 * and report the next address.
4231 * i.e. the address returned will be chunk-aligned and after
4232 * any data that is in the chunk containing 's'.
4234 static sector_t last_dev_address(sector_t s, struct geom *geo)
4236 s = (s | geo->chunk_mask) + 1;
4237 s >>= geo->chunk_shift;
4238 s *= geo->near_copies;
4239 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4240 s *= geo->far_copies;
4241 s <<= geo->chunk_shift;
4242 return s;
4245 /* Calculate the first device-address that could contain
4246 * any block from the chunk that includes the array-address 's'.
4247 * This too will be the start of a chunk
4249 static sector_t first_dev_address(sector_t s, struct geom *geo)
4251 s >>= geo->chunk_shift;
4252 s *= geo->near_copies;
4253 sector_div(s, geo->raid_disks);
4254 s *= geo->far_copies;
4255 s <<= geo->chunk_shift;
4256 return s;
4259 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4260 int *skipped)
4262 /* We simply copy at most one chunk (smallest of old and new)
4263 * at a time, possibly less if that exceeds RESYNC_PAGES,
4264 * or we hit a bad block or something.
4265 * This might mean we pause for normal IO in the middle of
4266 * a chunk, but that is not a problem as mddev->reshape_position
4267 * can record any location.
4269 * If we will want to write to a location that isn't
4270 * yet recorded as 'safe' (i.e. in metadata on disk) then
4271 * we need to flush all reshape requests and update the metadata.
4273 * When reshaping forwards (e.g. to more devices), we interpret
4274 * 'safe' as the earliest block which might not have been copied
4275 * down yet. We divide this by previous stripe size and multiply
4276 * by previous stripe length to get lowest device offset that we
4277 * cannot write to yet.
4278 * We interpret 'sector_nr' as an address that we want to write to.
4279 * From this we use last_device_address() to find where we might
4280 * write to, and first_device_address on the 'safe' position.
4281 * If this 'next' write position is after the 'safe' position,
4282 * we must update the metadata to increase the 'safe' position.
4284 * When reshaping backwards, we round in the opposite direction
4285 * and perform the reverse test: next write position must not be
4286 * less than current safe position.
4288 * In all this the minimum difference in data offsets
4289 * (conf->offset_diff - always positive) allows a bit of slack,
4290 * so next can be after 'safe', but not by more than offset_diff
4292 * We need to prepare all the bios here before we start any IO
4293 * to ensure the size we choose is acceptable to all devices.
4294 * The means one for each copy for write-out and an extra one for
4295 * read-in.
4296 * We store the read-in bio in ->master_bio and the others in
4297 * ->devs[x].bio and ->devs[x].repl_bio.
4299 struct r10conf *conf = mddev->private;
4300 struct r10bio *r10_bio;
4301 sector_t next, safe, last;
4302 int max_sectors;
4303 int nr_sectors;
4304 int s;
4305 struct md_rdev *rdev;
4306 int need_flush = 0;
4307 struct bio *blist;
4308 struct bio *bio, *read_bio;
4309 int sectors_done = 0;
4310 struct page **pages;
4312 if (sector_nr == 0) {
4313 /* If restarting in the middle, skip the initial sectors */
4314 if (mddev->reshape_backwards &&
4315 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4316 sector_nr = (raid10_size(mddev, 0, 0)
4317 - conf->reshape_progress);
4318 } else if (!mddev->reshape_backwards &&
4319 conf->reshape_progress > 0)
4320 sector_nr = conf->reshape_progress;
4321 if (sector_nr) {
4322 mddev->curr_resync_completed = sector_nr;
4323 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4324 *skipped = 1;
4325 return sector_nr;
4329 /* We don't use sector_nr to track where we are up to
4330 * as that doesn't work well for ->reshape_backwards.
4331 * So just use ->reshape_progress.
4333 if (mddev->reshape_backwards) {
4334 /* 'next' is the earliest device address that we might
4335 * write to for this chunk in the new layout
4337 next = first_dev_address(conf->reshape_progress - 1,
4338 &conf->geo);
4340 /* 'safe' is the last device address that we might read from
4341 * in the old layout after a restart
4343 safe = last_dev_address(conf->reshape_safe - 1,
4344 &conf->prev);
4346 if (next + conf->offset_diff < safe)
4347 need_flush = 1;
4349 last = conf->reshape_progress - 1;
4350 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4351 & conf->prev.chunk_mask);
4352 if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
4353 sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
4354 } else {
4355 /* 'next' is after the last device address that we
4356 * might write to for this chunk in the new layout
4358 next = last_dev_address(conf->reshape_progress, &conf->geo);
4360 /* 'safe' is the earliest device address that we might
4361 * read from in the old layout after a restart
4363 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4365 /* Need to update metadata if 'next' might be beyond 'safe'
4366 * as that would possibly corrupt data
4368 if (next > safe + conf->offset_diff)
4369 need_flush = 1;
4371 sector_nr = conf->reshape_progress;
4372 last = sector_nr | (conf->geo.chunk_mask
4373 & conf->prev.chunk_mask);
4375 if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
4376 last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
4379 if (need_flush ||
4380 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4381 /* Need to update reshape_position in metadata */
4382 wait_barrier(conf);
4383 mddev->reshape_position = conf->reshape_progress;
4384 if (mddev->reshape_backwards)
4385 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4386 - conf->reshape_progress;
4387 else
4388 mddev->curr_resync_completed = conf->reshape_progress;
4389 conf->reshape_checkpoint = jiffies;
4390 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
4391 md_wakeup_thread(mddev->thread);
4392 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
4393 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4394 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4395 allow_barrier(conf);
4396 return sectors_done;
4398 conf->reshape_safe = mddev->reshape_position;
4399 allow_barrier(conf);
4402 raise_barrier(conf, 0);
4403 read_more:
4404 /* Now schedule reads for blocks from sector_nr to last */
4405 r10_bio = raid10_alloc_init_r10buf(conf);
4406 r10_bio->state = 0;
4407 raise_barrier(conf, 1);
4408 atomic_set(&r10_bio->remaining, 0);
4409 r10_bio->mddev = mddev;
4410 r10_bio->sector = sector_nr;
4411 set_bit(R10BIO_IsReshape, &r10_bio->state);
4412 r10_bio->sectors = last - sector_nr + 1;
4413 rdev = read_balance(conf, r10_bio, &max_sectors);
4414 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4416 if (!rdev) {
4417 /* Cannot read from here, so need to record bad blocks
4418 * on all the target devices.
4420 // FIXME
4421 mempool_free(r10_bio, conf->r10buf_pool);
4422 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4423 return sectors_done;
4426 read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
4428 bio_set_dev(read_bio, rdev->bdev);
4429 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
4430 + rdev->data_offset);
4431 read_bio->bi_private = r10_bio;
4432 read_bio->bi_end_io = end_reshape_read;
4433 bio_set_op_attrs(read_bio, REQ_OP_READ, 0);
4434 read_bio->bi_flags &= (~0UL << BIO_RESET_BITS);
4435 read_bio->bi_status = 0;
4436 read_bio->bi_vcnt = 0;
4437 read_bio->bi_iter.bi_size = 0;
4438 r10_bio->master_bio = read_bio;
4439 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4441 /* Now find the locations in the new layout */
4442 __raid10_find_phys(&conf->geo, r10_bio);
4444 blist = read_bio;
4445 read_bio->bi_next = NULL;
4447 rcu_read_lock();
4448 for (s = 0; s < conf->copies*2; s++) {
4449 struct bio *b;
4450 int d = r10_bio->devs[s/2].devnum;
4451 struct md_rdev *rdev2;
4452 if (s&1) {
4453 rdev2 = rcu_dereference(conf->mirrors[d].replacement);
4454 b = r10_bio->devs[s/2].repl_bio;
4455 } else {
4456 rdev2 = rcu_dereference(conf->mirrors[d].rdev);
4457 b = r10_bio->devs[s/2].bio;
4459 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4460 continue;
4462 bio_set_dev(b, rdev2->bdev);
4463 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr +
4464 rdev2->new_data_offset;
4465 b->bi_end_io = end_reshape_write;
4466 bio_set_op_attrs(b, REQ_OP_WRITE, 0);
4467 b->bi_next = blist;
4468 blist = b;
4471 /* Now add as many pages as possible to all of these bios. */
4473 nr_sectors = 0;
4474 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
4475 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
4476 struct page *page = pages[s / (PAGE_SIZE >> 9)];
4477 int len = (max_sectors - s) << 9;
4478 if (len > PAGE_SIZE)
4479 len = PAGE_SIZE;
4480 for (bio = blist; bio ; bio = bio->bi_next) {
4482 * won't fail because the vec table is big enough
4483 * to hold all these pages
4485 bio_add_page(bio, page, len, 0);
4487 sector_nr += len >> 9;
4488 nr_sectors += len >> 9;
4490 rcu_read_unlock();
4491 r10_bio->sectors = nr_sectors;
4493 /* Now submit the read */
4494 md_sync_acct_bio(read_bio, r10_bio->sectors);
4495 atomic_inc(&r10_bio->remaining);
4496 read_bio->bi_next = NULL;
4497 generic_make_request(read_bio);
4498 sector_nr += nr_sectors;
4499 sectors_done += nr_sectors;
4500 if (sector_nr <= last)
4501 goto read_more;
4503 lower_barrier(conf);
4505 /* Now that we have done the whole section we can
4506 * update reshape_progress
4508 if (mddev->reshape_backwards)
4509 conf->reshape_progress -= sectors_done;
4510 else
4511 conf->reshape_progress += sectors_done;
4513 return sectors_done;
4516 static void end_reshape_request(struct r10bio *r10_bio);
4517 static int handle_reshape_read_error(struct mddev *mddev,
4518 struct r10bio *r10_bio);
4519 static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
4521 /* Reshape read completed. Hopefully we have a block
4522 * to write out.
4523 * If we got a read error then we do sync 1-page reads from
4524 * elsewhere until we find the data - or give up.
4526 struct r10conf *conf = mddev->private;
4527 int s;
4529 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
4530 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
4531 /* Reshape has been aborted */
4532 md_done_sync(mddev, r10_bio->sectors, 0);
4533 return;
4536 /* We definitely have the data in the pages, schedule the
4537 * writes.
4539 atomic_set(&r10_bio->remaining, 1);
4540 for (s = 0; s < conf->copies*2; s++) {
4541 struct bio *b;
4542 int d = r10_bio->devs[s/2].devnum;
4543 struct md_rdev *rdev;
4544 rcu_read_lock();
4545 if (s&1) {
4546 rdev = rcu_dereference(conf->mirrors[d].replacement);
4547 b = r10_bio->devs[s/2].repl_bio;
4548 } else {
4549 rdev = rcu_dereference(conf->mirrors[d].rdev);
4550 b = r10_bio->devs[s/2].bio;
4552 if (!rdev || test_bit(Faulty, &rdev->flags)) {
4553 rcu_read_unlock();
4554 continue;
4556 atomic_inc(&rdev->nr_pending);
4557 rcu_read_unlock();
4558 md_sync_acct_bio(b, r10_bio->sectors);
4559 atomic_inc(&r10_bio->remaining);
4560 b->bi_next = NULL;
4561 generic_make_request(b);
4563 end_reshape_request(r10_bio);
4566 static void end_reshape(struct r10conf *conf)
4568 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
4569 return;
4571 spin_lock_irq(&conf->device_lock);
4572 conf->prev = conf->geo;
4573 md_finish_reshape(conf->mddev);
4574 smp_wmb();
4575 conf->reshape_progress = MaxSector;
4576 conf->reshape_safe = MaxSector;
4577 spin_unlock_irq(&conf->device_lock);
4579 /* read-ahead size must cover two whole stripes, which is
4580 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4582 if (conf->mddev->queue) {
4583 int stripe = conf->geo.raid_disks *
4584 ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
4585 stripe /= conf->geo.near_copies;
4586 if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
4587 conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
4589 conf->fullsync = 0;
4592 static int handle_reshape_read_error(struct mddev *mddev,
4593 struct r10bio *r10_bio)
4595 /* Use sync reads to get the blocks from somewhere else */
4596 int sectors = r10_bio->sectors;
4597 struct r10conf *conf = mddev->private;
4598 struct r10bio *r10b;
4599 int slot = 0;
4600 int idx = 0;
4601 struct page **pages;
4603 r10b = kmalloc(sizeof(*r10b) +
4604 sizeof(struct r10dev) * conf->copies, GFP_NOIO);
4605 if (!r10b) {
4606 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4607 return -ENOMEM;
4610 /* reshape IOs share pages from .devs[0].bio */
4611 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
4613 r10b->sector = r10_bio->sector;
4614 __raid10_find_phys(&conf->prev, r10b);
4616 while (sectors) {
4617 int s = sectors;
4618 int success = 0;
4619 int first_slot = slot;
4621 if (s > (PAGE_SIZE >> 9))
4622 s = PAGE_SIZE >> 9;
4624 rcu_read_lock();
4625 while (!success) {
4626 int d = r10b->devs[slot].devnum;
4627 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
4628 sector_t addr;
4629 if (rdev == NULL ||
4630 test_bit(Faulty, &rdev->flags) ||
4631 !test_bit(In_sync, &rdev->flags))
4632 goto failed;
4634 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
4635 atomic_inc(&rdev->nr_pending);
4636 rcu_read_unlock();
4637 success = sync_page_io(rdev,
4638 addr,
4639 s << 9,
4640 pages[idx],
4641 REQ_OP_READ, 0, false);
4642 rdev_dec_pending(rdev, mddev);
4643 rcu_read_lock();
4644 if (success)
4645 break;
4646 failed:
4647 slot++;
4648 if (slot >= conf->copies)
4649 slot = 0;
4650 if (slot == first_slot)
4651 break;
4653 rcu_read_unlock();
4654 if (!success) {
4655 /* couldn't read this block, must give up */
4656 set_bit(MD_RECOVERY_INTR,
4657 &mddev->recovery);
4658 kfree(r10b);
4659 return -EIO;
4661 sectors -= s;
4662 idx++;
4664 kfree(r10b);
4665 return 0;
4668 static void end_reshape_write(struct bio *bio)
4670 struct r10bio *r10_bio = get_resync_r10bio(bio);
4671 struct mddev *mddev = r10_bio->mddev;
4672 struct r10conf *conf = mddev->private;
4673 int d;
4674 int slot;
4675 int repl;
4676 struct md_rdev *rdev = NULL;
4678 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
4679 if (repl)
4680 rdev = conf->mirrors[d].replacement;
4681 if (!rdev) {
4682 smp_mb();
4683 rdev = conf->mirrors[d].rdev;
4686 if (bio->bi_status) {
4687 /* FIXME should record badblock */
4688 md_error(mddev, rdev);
4691 rdev_dec_pending(rdev, mddev);
4692 end_reshape_request(r10_bio);
4695 static void end_reshape_request(struct r10bio *r10_bio)
4697 if (!atomic_dec_and_test(&r10_bio->remaining))
4698 return;
4699 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
4700 bio_put(r10_bio->master_bio);
4701 put_buf(r10_bio);
4704 static void raid10_finish_reshape(struct mddev *mddev)
4706 struct r10conf *conf = mddev->private;
4708 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4709 return;
4711 if (mddev->delta_disks > 0) {
4712 if (mddev->recovery_cp > mddev->resync_max_sectors) {
4713 mddev->recovery_cp = mddev->resync_max_sectors;
4714 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4716 mddev->resync_max_sectors = mddev->array_sectors;
4717 } else {
4718 int d;
4719 rcu_read_lock();
4720 for (d = conf->geo.raid_disks ;
4721 d < conf->geo.raid_disks - mddev->delta_disks;
4722 d++) {
4723 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
4724 if (rdev)
4725 clear_bit(In_sync, &rdev->flags);
4726 rdev = rcu_dereference(conf->mirrors[d].replacement);
4727 if (rdev)
4728 clear_bit(In_sync, &rdev->flags);
4730 rcu_read_unlock();
4732 mddev->layout = mddev->new_layout;
4733 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
4734 mddev->reshape_position = MaxSector;
4735 mddev->delta_disks = 0;
4736 mddev->reshape_backwards = 0;
4739 static struct md_personality raid10_personality =
4741 .name = "raid10",
4742 .level = 10,
4743 .owner = THIS_MODULE,
4744 .make_request = raid10_make_request,
4745 .run = raid10_run,
4746 .free = raid10_free,
4747 .status = raid10_status,
4748 .error_handler = raid10_error,
4749 .hot_add_disk = raid10_add_disk,
4750 .hot_remove_disk= raid10_remove_disk,
4751 .spare_active = raid10_spare_active,
4752 .sync_request = raid10_sync_request,
4753 .quiesce = raid10_quiesce,
4754 .size = raid10_size,
4755 .resize = raid10_resize,
4756 .takeover = raid10_takeover,
4757 .check_reshape = raid10_check_reshape,
4758 .start_reshape = raid10_start_reshape,
4759 .finish_reshape = raid10_finish_reshape,
4760 .congested = raid10_congested,
4763 static int __init raid_init(void)
4765 return register_md_personality(&raid10_personality);
4768 static void raid_exit(void)
4770 unregister_md_personality(&raid10_personality);
4773 module_init(raid_init);
4774 module_exit(raid_exit);
4775 MODULE_LICENSE("GPL");
4776 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
4777 MODULE_ALIAS("md-personality-9"); /* RAID10 */
4778 MODULE_ALIAS("md-raid10");
4779 MODULE_ALIAS("md-level-10");
4781 module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);