dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / md / raid10.c
blob89111d455b71bae079715fb7c6c22782bee1ece9
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 "md.h"
29 #include "raid10.h"
30 #include "raid0.h"
31 #include "bitmap.h"
34 * RAID10 provides a combination of RAID0 and RAID1 functionality.
35 * The layout of data is defined by
36 * chunk_size
37 * raid_disks
38 * near_copies (stored in low byte of layout)
39 * far_copies (stored in second byte of layout)
40 * far_offset (stored in bit 16 of layout )
41 * use_far_sets (stored in bit 17 of layout )
42 * use_far_sets_bugfixed (stored in bit 18 of layout )
44 * The data to be stored is divided into chunks using chunksize. Each device
45 * is divided into far_copies sections. In each section, chunks are laid out
46 * in a style similar to raid0, but near_copies copies of each chunk is stored
47 * (each on a different drive). The starting device for each section is offset
48 * near_copies from the starting device of the previous section. Thus there
49 * are (near_copies * far_copies) of each chunk, and each is on a different
50 * drive. near_copies and far_copies must be at least one, and their product
51 * is at most raid_disks.
53 * If far_offset is true, then the far_copies are handled a bit differently.
54 * The copies are still in different stripes, but instead of being very far
55 * apart on disk, there are adjacent stripes.
57 * The far and offset algorithms are handled slightly differently if
58 * 'use_far_sets' is true. In this case, the array's devices are grouped into
59 * sets that are (near_copies * far_copies) in size. The far copied stripes
60 * are still shifted by 'near_copies' devices, but this shifting stays confined
61 * to the set rather than the entire array. This is done to improve the number
62 * of device combinations that can fail without causing the array to fail.
63 * Example 'far' algorithm w/o 'use_far_sets' (each letter represents a chunk
64 * on a device):
65 * A B C D A B C D E
66 * ... ...
67 * D A B C E A B C D
68 * Example 'far' algorithm w/ 'use_far_sets' enabled (sets illustrated w/ []'s):
69 * [A B] [C D] [A B] [C D E]
70 * |...| |...| |...| | ... |
71 * [B A] [D C] [B A] [E C D]
75 * Number of guaranteed r10bios in case of extreme VM load:
77 #define NR_RAID10_BIOS 256
79 /* when we get a read error on a read-only array, we redirect to another
80 * device without failing the first device, or trying to over-write to
81 * correct the read error. To keep track of bad blocks on a per-bio
82 * level, we store IO_BLOCKED in the appropriate 'bios' pointer
84 #define IO_BLOCKED ((struct bio *)1)
85 /* When we successfully write to a known bad-block, we need to remove the
86 * bad-block marking which must be done from process context. So we record
87 * the success by setting devs[n].bio to IO_MADE_GOOD
89 #define IO_MADE_GOOD ((struct bio *)2)
91 #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
93 /* When there are this many requests queued to be written by
94 * the raid10 thread, we become 'congested' to provide back-pressure
95 * for writeback.
97 static int max_queued_requests = 1024;
99 static void allow_barrier(struct r10conf *conf);
100 static void lower_barrier(struct r10conf *conf);
101 static int _enough(struct r10conf *conf, int previous, int ignore);
102 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
103 int *skipped);
104 static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio);
105 static void end_reshape_write(struct bio *bio);
106 static void end_reshape(struct r10conf *conf);
108 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
110 struct r10conf *conf = data;
111 int size = offsetof(struct r10bio, devs[conf->copies]);
113 /* allocate a r10bio with room for raid_disks entries in the
114 * bios array */
115 return kzalloc(size, gfp_flags);
118 static void r10bio_pool_free(void *r10_bio, void *data)
120 kfree(r10_bio);
123 /* Maximum size of each resync request */
124 #define RESYNC_BLOCK_SIZE (64*1024)
125 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
126 /* amount of memory to reserve for resync requests */
127 #define RESYNC_WINDOW (1024*1024)
128 /* maximum number of concurrent requests, memory permitting */
129 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
132 * When performing a resync, we need to read and compare, so
133 * we need as many pages are there are copies.
134 * When performing a recovery, we need 2 bios, one for read,
135 * one for write (we recover only one drive per r10buf)
138 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
140 struct r10conf *conf = data;
141 struct page *page;
142 struct r10bio *r10_bio;
143 struct bio *bio;
144 int i, j;
145 int nalloc;
147 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
148 if (!r10_bio)
149 return NULL;
151 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
152 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
153 nalloc = conf->copies; /* resync */
154 else
155 nalloc = 2; /* recovery */
158 * Allocate bios.
160 for (j = nalloc ; j-- ; ) {
161 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
162 if (!bio)
163 goto out_free_bio;
164 r10_bio->devs[j].bio = bio;
165 if (!conf->have_replacement)
166 continue;
167 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
168 if (!bio)
169 goto out_free_bio;
170 r10_bio->devs[j].repl_bio = bio;
173 * Allocate RESYNC_PAGES data pages and attach them
174 * where needed.
176 for (j = 0 ; j < nalloc; j++) {
177 struct bio *rbio = r10_bio->devs[j].repl_bio;
178 bio = r10_bio->devs[j].bio;
179 for (i = 0; i < RESYNC_PAGES; i++) {
180 if (j > 0 && !test_bit(MD_RECOVERY_SYNC,
181 &conf->mddev->recovery)) {
182 /* we can share bv_page's during recovery
183 * and reshape */
184 struct bio *rbio = r10_bio->devs[0].bio;
185 page = rbio->bi_io_vec[i].bv_page;
186 get_page(page);
187 } else
188 page = alloc_page(gfp_flags);
189 if (unlikely(!page))
190 goto out_free_pages;
192 bio->bi_io_vec[i].bv_page = page;
193 if (rbio)
194 rbio->bi_io_vec[i].bv_page = page;
198 return r10_bio;
200 out_free_pages:
201 for ( ; i > 0 ; i--)
202 safe_put_page(bio->bi_io_vec[i-1].bv_page);
203 while (j--)
204 for (i = 0; i < RESYNC_PAGES ; i++)
205 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
206 j = 0;
207 out_free_bio:
208 for ( ; j < nalloc; j++) {
209 if (r10_bio->devs[j].bio)
210 bio_put(r10_bio->devs[j].bio);
211 if (r10_bio->devs[j].repl_bio)
212 bio_put(r10_bio->devs[j].repl_bio);
214 r10bio_pool_free(r10_bio, conf);
215 return NULL;
218 static void r10buf_pool_free(void *__r10_bio, void *data)
220 int i;
221 struct r10conf *conf = data;
222 struct r10bio *r10bio = __r10_bio;
223 int j;
225 for (j=0; j < conf->copies; j++) {
226 struct bio *bio = r10bio->devs[j].bio;
227 if (bio) {
228 for (i = 0; i < RESYNC_PAGES; i++) {
229 safe_put_page(bio->bi_io_vec[i].bv_page);
230 bio->bi_io_vec[i].bv_page = NULL;
232 bio_put(bio);
234 bio = r10bio->devs[j].repl_bio;
235 if (bio)
236 bio_put(bio);
238 r10bio_pool_free(r10bio, conf);
241 static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
243 int i;
245 for (i = 0; i < conf->copies; i++) {
246 struct bio **bio = & r10_bio->devs[i].bio;
247 if (!BIO_SPECIAL(*bio))
248 bio_put(*bio);
249 *bio = NULL;
250 bio = &r10_bio->devs[i].repl_bio;
251 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
252 bio_put(*bio);
253 *bio = NULL;
257 static void free_r10bio(struct r10bio *r10_bio)
259 struct r10conf *conf = r10_bio->mddev->private;
261 put_all_bios(conf, r10_bio);
262 mempool_free(r10_bio, conf->r10bio_pool);
265 static void put_buf(struct r10bio *r10_bio)
267 struct r10conf *conf = r10_bio->mddev->private;
269 mempool_free(r10_bio, conf->r10buf_pool);
271 lower_barrier(conf);
274 static void reschedule_retry(struct r10bio *r10_bio)
276 unsigned long flags;
277 struct mddev *mddev = r10_bio->mddev;
278 struct r10conf *conf = mddev->private;
280 spin_lock_irqsave(&conf->device_lock, flags);
281 list_add(&r10_bio->retry_list, &conf->retry_list);
282 conf->nr_queued ++;
283 spin_unlock_irqrestore(&conf->device_lock, flags);
285 /* wake up frozen array... */
286 wake_up(&conf->wait_barrier);
288 md_wakeup_thread(mddev->thread);
292 * raid_end_bio_io() is called when we have finished servicing a mirrored
293 * operation and are ready to return a success/failure code to the buffer
294 * cache layer.
296 static void raid_end_bio_io(struct r10bio *r10_bio)
298 struct bio *bio = r10_bio->master_bio;
299 int done;
300 struct r10conf *conf = r10_bio->mddev->private;
302 if (bio->bi_phys_segments) {
303 unsigned long flags;
304 spin_lock_irqsave(&conf->device_lock, flags);
305 bio->bi_phys_segments--;
306 done = (bio->bi_phys_segments == 0);
307 spin_unlock_irqrestore(&conf->device_lock, flags);
308 } else
309 done = 1;
310 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
311 bio->bi_error = -EIO;
312 if (done) {
313 bio_endio(bio);
315 * Wake up any possible resync thread that waits for the device
316 * to go idle.
318 allow_barrier(conf);
320 free_r10bio(r10_bio);
324 * Update disk head position estimator based on IRQ completion info.
326 static inline void update_head_pos(int slot, struct r10bio *r10_bio)
328 struct r10conf *conf = r10_bio->mddev->private;
330 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
331 r10_bio->devs[slot].addr + (r10_bio->sectors);
335 * Find the disk number which triggered given bio
337 static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
338 struct bio *bio, int *slotp, int *replp)
340 int slot;
341 int repl = 0;
343 for (slot = 0; slot < conf->copies; slot++) {
344 if (r10_bio->devs[slot].bio == bio)
345 break;
346 if (r10_bio->devs[slot].repl_bio == bio) {
347 repl = 1;
348 break;
352 BUG_ON(slot == conf->copies);
353 update_head_pos(slot, r10_bio);
355 if (slotp)
356 *slotp = slot;
357 if (replp)
358 *replp = repl;
359 return r10_bio->devs[slot].devnum;
362 static void raid10_end_read_request(struct bio *bio)
364 int uptodate = !bio->bi_error;
365 struct r10bio *r10_bio = bio->bi_private;
366 int slot, dev;
367 struct md_rdev *rdev;
368 struct r10conf *conf = r10_bio->mddev->private;
370 slot = r10_bio->read_slot;
371 dev = r10_bio->devs[slot].devnum;
372 rdev = r10_bio->devs[slot].rdev;
374 * this branch is our 'one mirror IO has finished' event handler:
376 update_head_pos(slot, r10_bio);
378 if (uptodate) {
380 * Set R10BIO_Uptodate in our master bio, so that
381 * we will return a good error code to the higher
382 * levels even if IO on some other mirrored buffer fails.
384 * The 'master' represents the composite IO operation to
385 * user-side. So if something waits for IO, then it will
386 * wait for the 'master' bio.
388 set_bit(R10BIO_Uptodate, &r10_bio->state);
389 } else {
390 /* If all other devices that store this block have
391 * failed, we want to return the error upwards rather
392 * than fail the last device. Here we redefine
393 * "uptodate" to mean "Don't want to retry"
395 if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state),
396 rdev->raid_disk))
397 uptodate = 1;
399 if (uptodate) {
400 raid_end_bio_io(r10_bio);
401 rdev_dec_pending(rdev, conf->mddev);
402 } else {
404 * oops, read error - keep the refcount on the rdev
406 char b[BDEVNAME_SIZE];
407 printk_ratelimited(KERN_ERR
408 "md/raid10:%s: %s: rescheduling sector %llu\n",
409 mdname(conf->mddev),
410 bdevname(rdev->bdev, b),
411 (unsigned long long)r10_bio->sector);
412 set_bit(R10BIO_ReadError, &r10_bio->state);
413 reschedule_retry(r10_bio);
417 static void close_write(struct r10bio *r10_bio)
419 /* clear the bitmap if all writes complete successfully */
420 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
421 r10_bio->sectors,
422 !test_bit(R10BIO_Degraded, &r10_bio->state),
424 md_write_end(r10_bio->mddev);
427 static void one_write_done(struct r10bio *r10_bio)
429 if (atomic_dec_and_test(&r10_bio->remaining)) {
430 if (test_bit(R10BIO_WriteError, &r10_bio->state))
431 reschedule_retry(r10_bio);
432 else {
433 close_write(r10_bio);
434 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
435 reschedule_retry(r10_bio);
436 else
437 raid_end_bio_io(r10_bio);
442 static void raid10_end_write_request(struct bio *bio)
444 struct r10bio *r10_bio = bio->bi_private;
445 int dev;
446 int dec_rdev = 1;
447 struct r10conf *conf = r10_bio->mddev->private;
448 int slot, repl;
449 struct md_rdev *rdev = NULL;
451 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
453 if (repl)
454 rdev = conf->mirrors[dev].replacement;
455 if (!rdev) {
456 smp_rmb();
457 repl = 0;
458 rdev = conf->mirrors[dev].rdev;
461 * this branch is our 'one mirror IO has finished' event handler:
463 if (bio->bi_error) {
464 if (repl)
465 /* Never record new bad blocks to replacement,
466 * just fail it.
468 md_error(rdev->mddev, rdev);
469 else {
470 set_bit(WriteErrorSeen, &rdev->flags);
471 if (!test_and_set_bit(WantReplacement, &rdev->flags))
472 set_bit(MD_RECOVERY_NEEDED,
473 &rdev->mddev->recovery);
474 set_bit(R10BIO_WriteError, &r10_bio->state);
475 dec_rdev = 0;
477 } else {
479 * Set R10BIO_Uptodate in our master bio, so that
480 * we will return a good error code for to the higher
481 * levels even if IO on some other mirrored buffer fails.
483 * The 'master' represents the composite IO operation to
484 * user-side. So if something waits for IO, then it will
485 * wait for the 'master' bio.
487 sector_t first_bad;
488 int bad_sectors;
491 * Do not set R10BIO_Uptodate if the current device is
492 * rebuilding or Faulty. This is because we cannot use
493 * such device for properly reading the data back (we could
494 * potentially use it, if the current write would have felt
495 * before rdev->recovery_offset, but for simplicity we don't
496 * check this here.
498 if (test_bit(In_sync, &rdev->flags) &&
499 !test_bit(Faulty, &rdev->flags))
500 set_bit(R10BIO_Uptodate, &r10_bio->state);
502 /* Maybe we can clear some bad blocks. */
503 if (is_badblock(rdev,
504 r10_bio->devs[slot].addr,
505 r10_bio->sectors,
506 &first_bad, &bad_sectors)) {
507 bio_put(bio);
508 if (repl)
509 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
510 else
511 r10_bio->devs[slot].bio = IO_MADE_GOOD;
512 dec_rdev = 0;
513 set_bit(R10BIO_MadeGood, &r10_bio->state);
519 * Let's see if all mirrored write operations have finished
520 * already.
522 one_write_done(r10_bio);
523 if (dec_rdev)
524 rdev_dec_pending(rdev, conf->mddev);
528 * RAID10 layout manager
529 * As well as the chunksize and raid_disks count, there are two
530 * parameters: near_copies and far_copies.
531 * near_copies * far_copies must be <= raid_disks.
532 * Normally one of these will be 1.
533 * If both are 1, we get raid0.
534 * If near_copies == raid_disks, we get raid1.
536 * Chunks are laid out in raid0 style with near_copies copies of the
537 * first chunk, followed by near_copies copies of the next chunk and
538 * so on.
539 * If far_copies > 1, then after 1/far_copies of the array has been assigned
540 * as described above, we start again with a device offset of near_copies.
541 * So we effectively have another copy of the whole array further down all
542 * the drives, but with blocks on different drives.
543 * With this layout, and block is never stored twice on the one device.
545 * raid10_find_phys finds the sector offset of a given virtual sector
546 * on each device that it is on.
548 * raid10_find_virt does the reverse mapping, from a device and a
549 * sector offset to a virtual address
552 static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
554 int n,f;
555 sector_t sector;
556 sector_t chunk;
557 sector_t stripe;
558 int dev;
559 int slot = 0;
560 int last_far_set_start, last_far_set_size;
562 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
563 last_far_set_start *= geo->far_set_size;
565 last_far_set_size = geo->far_set_size;
566 last_far_set_size += (geo->raid_disks % geo->far_set_size);
568 /* now calculate first sector/dev */
569 chunk = r10bio->sector >> geo->chunk_shift;
570 sector = r10bio->sector & geo->chunk_mask;
572 chunk *= geo->near_copies;
573 stripe = chunk;
574 dev = sector_div(stripe, geo->raid_disks);
575 if (geo->far_offset)
576 stripe *= geo->far_copies;
578 sector += stripe << geo->chunk_shift;
580 /* and calculate all the others */
581 for (n = 0; n < geo->near_copies; n++) {
582 int d = dev;
583 int set;
584 sector_t s = sector;
585 r10bio->devs[slot].devnum = d;
586 r10bio->devs[slot].addr = s;
587 slot++;
589 for (f = 1; f < geo->far_copies; f++) {
590 set = d / geo->far_set_size;
591 d += geo->near_copies;
593 if ((geo->raid_disks % geo->far_set_size) &&
594 (d > last_far_set_start)) {
595 d -= last_far_set_start;
596 d %= last_far_set_size;
597 d += last_far_set_start;
598 } else {
599 d %= geo->far_set_size;
600 d += geo->far_set_size * set;
602 s += geo->stride;
603 r10bio->devs[slot].devnum = d;
604 r10bio->devs[slot].addr = s;
605 slot++;
607 dev++;
608 if (dev >= geo->raid_disks) {
609 dev = 0;
610 sector += (geo->chunk_mask + 1);
615 static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
617 struct geom *geo = &conf->geo;
619 if (conf->reshape_progress != MaxSector &&
620 ((r10bio->sector >= conf->reshape_progress) !=
621 conf->mddev->reshape_backwards)) {
622 set_bit(R10BIO_Previous, &r10bio->state);
623 geo = &conf->prev;
624 } else
625 clear_bit(R10BIO_Previous, &r10bio->state);
627 __raid10_find_phys(geo, r10bio);
630 static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
632 sector_t offset, chunk, vchunk;
633 /* Never use conf->prev as this is only called during resync
634 * or recovery, so reshape isn't happening
636 struct geom *geo = &conf->geo;
637 int far_set_start = (dev / geo->far_set_size) * geo->far_set_size;
638 int far_set_size = geo->far_set_size;
639 int last_far_set_start;
641 if (geo->raid_disks % geo->far_set_size) {
642 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
643 last_far_set_start *= geo->far_set_size;
645 if (dev >= last_far_set_start) {
646 far_set_size = geo->far_set_size;
647 far_set_size += (geo->raid_disks % geo->far_set_size);
648 far_set_start = last_far_set_start;
652 offset = sector & geo->chunk_mask;
653 if (geo->far_offset) {
654 int fc;
655 chunk = sector >> geo->chunk_shift;
656 fc = sector_div(chunk, geo->far_copies);
657 dev -= fc * geo->near_copies;
658 if (dev < far_set_start)
659 dev += far_set_size;
660 } else {
661 while (sector >= geo->stride) {
662 sector -= geo->stride;
663 if (dev < (geo->near_copies + far_set_start))
664 dev += far_set_size - geo->near_copies;
665 else
666 dev -= geo->near_copies;
668 chunk = sector >> geo->chunk_shift;
670 vchunk = chunk * geo->raid_disks + dev;
671 sector_div(vchunk, geo->near_copies);
672 return (vchunk << geo->chunk_shift) + offset;
676 * This routine returns the disk from which the requested read should
677 * be done. There is a per-array 'next expected sequential IO' sector
678 * number - if this matches on the next IO then we use the last disk.
679 * There is also a per-disk 'last know head position' sector that is
680 * maintained from IRQ contexts, both the normal and the resync IO
681 * completion handlers update this position correctly. If there is no
682 * perfect sequential match then we pick the disk whose head is closest.
684 * If there are 2 mirrors in the same 2 devices, performance degrades
685 * because position is mirror, not device based.
687 * The rdev for the device selected will have nr_pending incremented.
691 * FIXME: possibly should rethink readbalancing and do it differently
692 * depending on near_copies / far_copies geometry.
694 static struct md_rdev *read_balance(struct r10conf *conf,
695 struct r10bio *r10_bio,
696 int *max_sectors)
698 const sector_t this_sector = r10_bio->sector;
699 int disk, slot;
700 int sectors = r10_bio->sectors;
701 int best_good_sectors;
702 sector_t new_distance, best_dist;
703 struct md_rdev *best_rdev, *rdev = NULL;
704 int do_balance;
705 int best_slot;
706 struct geom *geo = &conf->geo;
708 raid10_find_phys(conf, r10_bio);
709 rcu_read_lock();
710 retry:
711 sectors = r10_bio->sectors;
712 best_slot = -1;
713 best_rdev = NULL;
714 best_dist = MaxSector;
715 best_good_sectors = 0;
716 do_balance = 1;
718 * Check if we can balance. We can balance on the whole
719 * device if no resync is going on (recovery is ok), or below
720 * the resync window. We take the first readable disk when
721 * above the resync window.
723 if (conf->mddev->recovery_cp < MaxSector
724 && (this_sector + sectors >= conf->next_resync))
725 do_balance = 0;
727 for (slot = 0; slot < conf->copies ; slot++) {
728 sector_t first_bad;
729 int bad_sectors;
730 sector_t dev_sector;
732 if (r10_bio->devs[slot].bio == IO_BLOCKED)
733 continue;
734 disk = r10_bio->devs[slot].devnum;
735 rdev = rcu_dereference(conf->mirrors[disk].replacement);
736 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
737 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
738 rdev = rcu_dereference(conf->mirrors[disk].rdev);
739 if (rdev == NULL ||
740 test_bit(Faulty, &rdev->flags))
741 continue;
742 if (!test_bit(In_sync, &rdev->flags) &&
743 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
744 continue;
746 dev_sector = r10_bio->devs[slot].addr;
747 if (is_badblock(rdev, dev_sector, sectors,
748 &first_bad, &bad_sectors)) {
749 if (best_dist < MaxSector)
750 /* Already have a better slot */
751 continue;
752 if (first_bad <= dev_sector) {
753 /* Cannot read here. If this is the
754 * 'primary' device, then we must not read
755 * beyond 'bad_sectors' from another device.
757 bad_sectors -= (dev_sector - first_bad);
758 if (!do_balance && sectors > bad_sectors)
759 sectors = bad_sectors;
760 if (best_good_sectors > sectors)
761 best_good_sectors = sectors;
762 } else {
763 sector_t good_sectors =
764 first_bad - dev_sector;
765 if (good_sectors > best_good_sectors) {
766 best_good_sectors = good_sectors;
767 best_slot = slot;
768 best_rdev = rdev;
770 if (!do_balance)
771 /* Must read from here */
772 break;
774 continue;
775 } else
776 best_good_sectors = sectors;
778 if (!do_balance)
779 break;
781 /* This optimisation is debatable, and completely destroys
782 * sequential read speed for 'far copies' arrays. So only
783 * keep it for 'near' arrays, and review those later.
785 if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending))
786 break;
788 /* for far > 1 always use the lowest address */
789 if (geo->far_copies > 1)
790 new_distance = r10_bio->devs[slot].addr;
791 else
792 new_distance = abs(r10_bio->devs[slot].addr -
793 conf->mirrors[disk].head_position);
794 if (new_distance < best_dist) {
795 best_dist = new_distance;
796 best_slot = slot;
797 best_rdev = rdev;
800 if (slot >= conf->copies) {
801 slot = best_slot;
802 rdev = best_rdev;
805 if (slot >= 0) {
806 atomic_inc(&rdev->nr_pending);
807 if (test_bit(Faulty, &rdev->flags)) {
808 /* Cannot risk returning a device that failed
809 * before we inc'ed nr_pending
811 rdev_dec_pending(rdev, conf->mddev);
812 goto retry;
814 r10_bio->read_slot = slot;
815 } else
816 rdev = NULL;
817 rcu_read_unlock();
818 *max_sectors = best_good_sectors;
820 return rdev;
823 static int raid10_congested(struct mddev *mddev, int bits)
825 struct r10conf *conf = mddev->private;
826 int i, ret = 0;
828 if ((bits & (1 << WB_async_congested)) &&
829 conf->pending_count >= max_queued_requests)
830 return 1;
832 rcu_read_lock();
833 for (i = 0;
834 (i < conf->geo.raid_disks || i < conf->prev.raid_disks)
835 && ret == 0;
836 i++) {
837 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
838 if (rdev && !test_bit(Faulty, &rdev->flags)) {
839 struct request_queue *q = bdev_get_queue(rdev->bdev);
841 ret |= bdi_congested(&q->backing_dev_info, bits);
844 rcu_read_unlock();
845 return ret;
848 static void flush_pending_writes(struct r10conf *conf)
850 /* Any writes that have been queued but are awaiting
851 * bitmap updates get flushed here.
853 spin_lock_irq(&conf->device_lock);
855 if (conf->pending_bio_list.head) {
856 struct bio *bio;
857 bio = bio_list_get(&conf->pending_bio_list);
858 conf->pending_count = 0;
859 spin_unlock_irq(&conf->device_lock);
860 /* flush any pending bitmap writes to disk
861 * before proceeding w/ I/O */
862 bitmap_unplug(conf->mddev->bitmap);
863 wake_up(&conf->wait_barrier);
865 while (bio) { /* submit pending writes */
866 struct bio *next = bio->bi_next;
867 bio->bi_next = NULL;
868 if (unlikely((bio->bi_rw & REQ_DISCARD) &&
869 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
870 /* Just ignore it */
871 bio_endio(bio);
872 else
873 generic_make_request(bio);
874 bio = next;
876 } else
877 spin_unlock_irq(&conf->device_lock);
880 /* Barriers....
881 * Sometimes we need to suspend IO while we do something else,
882 * either some resync/recovery, or reconfigure the array.
883 * To do this we raise a 'barrier'.
884 * The 'barrier' is a counter that can be raised multiple times
885 * to count how many activities are happening which preclude
886 * normal IO.
887 * We can only raise the barrier if there is no pending IO.
888 * i.e. if nr_pending == 0.
889 * We choose only to raise the barrier if no-one is waiting for the
890 * barrier to go down. This means that as soon as an IO request
891 * is ready, no other operations which require a barrier will start
892 * until the IO request has had a chance.
894 * So: regular IO calls 'wait_barrier'. When that returns there
895 * is no backgroup IO happening, It must arrange to call
896 * allow_barrier when it has finished its IO.
897 * backgroup IO calls must call raise_barrier. Once that returns
898 * there is no normal IO happeing. It must arrange to call
899 * lower_barrier when the particular background IO completes.
902 static void raise_barrier(struct r10conf *conf, int force)
904 BUG_ON(force && !conf->barrier);
905 spin_lock_irq(&conf->resync_lock);
907 /* Wait until no block IO is waiting (unless 'force') */
908 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
909 conf->resync_lock);
911 /* block any new IO from starting */
912 conf->barrier++;
914 /* Now wait for all pending IO to complete */
915 wait_event_lock_irq(conf->wait_barrier,
916 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
917 conf->resync_lock);
919 spin_unlock_irq(&conf->resync_lock);
922 static void lower_barrier(struct r10conf *conf)
924 unsigned long flags;
925 spin_lock_irqsave(&conf->resync_lock, flags);
926 conf->barrier--;
927 spin_unlock_irqrestore(&conf->resync_lock, flags);
928 wake_up(&conf->wait_barrier);
931 static void wait_barrier(struct r10conf *conf)
933 spin_lock_irq(&conf->resync_lock);
934 if (conf->barrier) {
935 conf->nr_waiting++;
936 /* Wait for the barrier to drop.
937 * However if there are already pending
938 * requests (preventing the barrier from
939 * rising completely), and the
940 * pre-process bio queue isn't empty,
941 * then don't wait, as we need to empty
942 * that queue to get the nr_pending
943 * count down.
945 wait_event_lock_irq(conf->wait_barrier,
946 !conf->barrier ||
947 (conf->nr_pending &&
948 current->bio_list &&
949 (!bio_list_empty(&current->bio_list[0]) ||
950 !bio_list_empty(&current->bio_list[1]))),
951 conf->resync_lock);
952 conf->nr_waiting--;
954 conf->nr_pending++;
955 spin_unlock_irq(&conf->resync_lock);
958 static void allow_barrier(struct r10conf *conf)
960 unsigned long flags;
961 spin_lock_irqsave(&conf->resync_lock, flags);
962 conf->nr_pending--;
963 spin_unlock_irqrestore(&conf->resync_lock, flags);
964 wake_up(&conf->wait_barrier);
967 static void freeze_array(struct r10conf *conf, int extra)
969 /* stop syncio and normal IO and wait for everything to
970 * go quiet.
971 * We increment barrier and nr_waiting, and then
972 * wait until nr_pending match nr_queued+extra
973 * This is called in the context of one normal IO request
974 * that has failed. Thus any sync request that might be pending
975 * will be blocked by nr_pending, and we need to wait for
976 * pending IO requests to complete or be queued for re-try.
977 * Thus the number queued (nr_queued) plus this request (extra)
978 * must match the number of pending IOs (nr_pending) before
979 * we continue.
981 spin_lock_irq(&conf->resync_lock);
982 conf->barrier++;
983 conf->nr_waiting++;
984 wait_event_lock_irq_cmd(conf->wait_barrier,
985 conf->nr_pending == conf->nr_queued+extra,
986 conf->resync_lock,
987 flush_pending_writes(conf));
989 spin_unlock_irq(&conf->resync_lock);
992 static void unfreeze_array(struct r10conf *conf)
994 /* reverse the effect of the freeze */
995 spin_lock_irq(&conf->resync_lock);
996 conf->barrier--;
997 conf->nr_waiting--;
998 wake_up(&conf->wait_barrier);
999 spin_unlock_irq(&conf->resync_lock);
1002 static sector_t choose_data_offset(struct r10bio *r10_bio,
1003 struct md_rdev *rdev)
1005 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1006 test_bit(R10BIO_Previous, &r10_bio->state))
1007 return rdev->data_offset;
1008 else
1009 return rdev->new_data_offset;
1012 struct raid10_plug_cb {
1013 struct blk_plug_cb cb;
1014 struct bio_list pending;
1015 int pending_cnt;
1018 static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1020 struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb,
1021 cb);
1022 struct mddev *mddev = plug->cb.data;
1023 struct r10conf *conf = mddev->private;
1024 struct bio *bio;
1026 if (from_schedule || current->bio_list) {
1027 spin_lock_irq(&conf->device_lock);
1028 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1029 conf->pending_count += plug->pending_cnt;
1030 spin_unlock_irq(&conf->device_lock);
1031 wake_up(&conf->wait_barrier);
1032 md_wakeup_thread(mddev->thread);
1033 kfree(plug);
1034 return;
1037 /* we aren't scheduling, so we can do the write-out directly. */
1038 bio = bio_list_get(&plug->pending);
1039 bitmap_unplug(mddev->bitmap);
1040 wake_up(&conf->wait_barrier);
1042 while (bio) { /* submit pending writes */
1043 struct bio *next = bio->bi_next;
1044 bio->bi_next = NULL;
1045 if (unlikely((bio->bi_rw & REQ_DISCARD) &&
1046 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
1047 /* Just ignore it */
1048 bio_endio(bio);
1049 else
1050 generic_make_request(bio);
1051 bio = next;
1053 kfree(plug);
1056 static void __make_request(struct mddev *mddev, struct bio *bio)
1058 struct r10conf *conf = mddev->private;
1059 struct r10bio *r10_bio;
1060 struct bio *read_bio;
1061 int i;
1062 const int rw = bio_data_dir(bio);
1063 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
1064 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
1065 const unsigned long do_discard = (bio->bi_rw
1066 & (REQ_DISCARD | REQ_SECURE));
1067 const unsigned long do_same = (bio->bi_rw & REQ_WRITE_SAME);
1068 unsigned long flags;
1069 struct md_rdev *blocked_rdev;
1070 struct blk_plug_cb *cb;
1071 struct raid10_plug_cb *plug = NULL;
1072 int sectors_handled;
1073 int max_sectors;
1074 int sectors;
1076 md_write_start(mddev, bio);
1079 * Register the new request and wait if the reconstruction
1080 * thread has put up a bar for new requests.
1081 * Continue immediately if no resync is active currently.
1083 wait_barrier(conf);
1085 sectors = bio_sectors(bio);
1086 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1087 bio->bi_iter.bi_sector < conf->reshape_progress &&
1088 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
1089 /* IO spans the reshape position. Need to wait for
1090 * reshape to pass
1092 allow_barrier(conf);
1093 wait_event(conf->wait_barrier,
1094 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1095 conf->reshape_progress >= bio->bi_iter.bi_sector +
1096 sectors);
1097 wait_barrier(conf);
1099 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1100 bio_data_dir(bio) == WRITE &&
1101 (mddev->reshape_backwards
1102 ? (bio->bi_iter.bi_sector < conf->reshape_safe &&
1103 bio->bi_iter.bi_sector + sectors > conf->reshape_progress)
1104 : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe &&
1105 bio->bi_iter.bi_sector < conf->reshape_progress))) {
1106 /* Need to update reshape_position in metadata */
1107 mddev->reshape_position = conf->reshape_progress;
1108 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1109 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1110 md_wakeup_thread(mddev->thread);
1111 wait_event(mddev->sb_wait,
1112 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
1114 conf->reshape_safe = mddev->reshape_position;
1117 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1119 r10_bio->master_bio = bio;
1120 r10_bio->sectors = sectors;
1122 r10_bio->mddev = mddev;
1123 r10_bio->sector = bio->bi_iter.bi_sector;
1124 r10_bio->state = 0;
1126 /* We might need to issue multiple reads to different
1127 * devices if there are bad blocks around, so we keep
1128 * track of the number of reads in bio->bi_phys_segments.
1129 * If this is 0, there is only one r10_bio and no locking
1130 * will be needed when the request completes. If it is
1131 * non-zero, then it is the number of not-completed requests.
1133 bio->bi_phys_segments = 0;
1134 bio_clear_flag(bio, BIO_SEG_VALID);
1136 if (rw == READ) {
1138 * read balancing logic:
1140 struct md_rdev *rdev;
1141 int slot;
1143 read_again:
1144 rdev = read_balance(conf, r10_bio, &max_sectors);
1145 if (!rdev) {
1146 raid_end_bio_io(r10_bio);
1147 return;
1149 slot = r10_bio->read_slot;
1151 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1152 bio_trim(read_bio, r10_bio->sector - bio->bi_iter.bi_sector,
1153 max_sectors);
1155 r10_bio->devs[slot].bio = read_bio;
1156 r10_bio->devs[slot].rdev = rdev;
1158 read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr +
1159 choose_data_offset(r10_bio, rdev);
1160 read_bio->bi_bdev = rdev->bdev;
1161 read_bio->bi_end_io = raid10_end_read_request;
1162 read_bio->bi_rw = READ | do_sync;
1163 read_bio->bi_private = r10_bio;
1165 if (max_sectors < r10_bio->sectors) {
1166 /* Could not read all from this device, so we will
1167 * need another r10_bio.
1169 sectors_handled = (r10_bio->sector + max_sectors
1170 - bio->bi_iter.bi_sector);
1171 r10_bio->sectors = max_sectors;
1172 spin_lock_irq(&conf->device_lock);
1173 if (bio->bi_phys_segments == 0)
1174 bio->bi_phys_segments = 2;
1175 else
1176 bio->bi_phys_segments++;
1177 spin_unlock_irq(&conf->device_lock);
1178 /* Cannot call generic_make_request directly
1179 * as that will be queued in __generic_make_request
1180 * and subsequent mempool_alloc might block
1181 * waiting for it. so hand bio over to raid10d.
1183 reschedule_retry(r10_bio);
1185 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1187 r10_bio->master_bio = bio;
1188 r10_bio->sectors = bio_sectors(bio) - sectors_handled;
1189 r10_bio->state = 0;
1190 r10_bio->mddev = mddev;
1191 r10_bio->sector = bio->bi_iter.bi_sector +
1192 sectors_handled;
1193 goto read_again;
1194 } else
1195 generic_make_request(read_bio);
1196 return;
1200 * WRITE:
1202 if (conf->pending_count >= max_queued_requests) {
1203 md_wakeup_thread(mddev->thread);
1204 wait_event(conf->wait_barrier,
1205 conf->pending_count < max_queued_requests);
1207 /* first select target devices under rcu_lock and
1208 * inc refcount on their rdev. Record them by setting
1209 * bios[x] to bio
1210 * If there are known/acknowledged bad blocks on any device
1211 * on which we have seen a write error, we want to avoid
1212 * writing to those blocks. This potentially requires several
1213 * writes to write around the bad blocks. Each set of writes
1214 * gets its own r10_bio with a set of bios attached. The number
1215 * of r10_bios is recored in bio->bi_phys_segments just as with
1216 * the read case.
1219 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
1220 raid10_find_phys(conf, r10_bio);
1221 retry_write:
1222 blocked_rdev = NULL;
1223 rcu_read_lock();
1224 max_sectors = r10_bio->sectors;
1226 for (i = 0; i < conf->copies; i++) {
1227 int d = r10_bio->devs[i].devnum;
1228 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
1229 struct md_rdev *rrdev = rcu_dereference(
1230 conf->mirrors[d].replacement);
1231 if (rdev == rrdev)
1232 rrdev = NULL;
1233 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1234 atomic_inc(&rdev->nr_pending);
1235 blocked_rdev = rdev;
1236 break;
1238 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1239 atomic_inc(&rrdev->nr_pending);
1240 blocked_rdev = rrdev;
1241 break;
1243 if (rdev && (test_bit(Faulty, &rdev->flags)))
1244 rdev = NULL;
1245 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
1246 rrdev = NULL;
1248 r10_bio->devs[i].bio = NULL;
1249 r10_bio->devs[i].repl_bio = NULL;
1251 if (!rdev && !rrdev) {
1252 set_bit(R10BIO_Degraded, &r10_bio->state);
1253 continue;
1255 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
1256 sector_t first_bad;
1257 sector_t dev_sector = r10_bio->devs[i].addr;
1258 int bad_sectors;
1259 int is_bad;
1261 is_bad = is_badblock(rdev, dev_sector,
1262 max_sectors,
1263 &first_bad, &bad_sectors);
1264 if (is_bad < 0) {
1265 /* Mustn't write here until the bad block
1266 * is acknowledged
1268 atomic_inc(&rdev->nr_pending);
1269 set_bit(BlockedBadBlocks, &rdev->flags);
1270 blocked_rdev = rdev;
1271 break;
1273 if (is_bad && first_bad <= dev_sector) {
1274 /* Cannot write here at all */
1275 bad_sectors -= (dev_sector - first_bad);
1276 if (bad_sectors < max_sectors)
1277 /* Mustn't write more than bad_sectors
1278 * to other devices yet
1280 max_sectors = bad_sectors;
1281 /* We don't set R10BIO_Degraded as that
1282 * only applies if the disk is missing,
1283 * so it might be re-added, and we want to
1284 * know to recover this chunk.
1285 * In this case the device is here, and the
1286 * fact that this chunk is not in-sync is
1287 * recorded in the bad block log.
1289 continue;
1291 if (is_bad) {
1292 int good_sectors = first_bad - dev_sector;
1293 if (good_sectors < max_sectors)
1294 max_sectors = good_sectors;
1297 if (rdev) {
1298 r10_bio->devs[i].bio = bio;
1299 atomic_inc(&rdev->nr_pending);
1301 if (rrdev) {
1302 r10_bio->devs[i].repl_bio = bio;
1303 atomic_inc(&rrdev->nr_pending);
1306 rcu_read_unlock();
1308 if (unlikely(blocked_rdev)) {
1309 /* Have to wait for this device to get unblocked, then retry */
1310 int j;
1311 int d;
1313 for (j = 0; j < i; j++) {
1314 if (r10_bio->devs[j].bio) {
1315 d = r10_bio->devs[j].devnum;
1316 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1318 if (r10_bio->devs[j].repl_bio) {
1319 struct md_rdev *rdev;
1320 d = r10_bio->devs[j].devnum;
1321 rdev = conf->mirrors[d].replacement;
1322 if (!rdev) {
1323 /* Race with remove_disk */
1324 smp_mb();
1325 rdev = conf->mirrors[d].rdev;
1327 rdev_dec_pending(rdev, mddev);
1330 allow_barrier(conf);
1331 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1332 wait_barrier(conf);
1333 goto retry_write;
1336 if (max_sectors < r10_bio->sectors) {
1337 /* We are splitting this into multiple parts, so
1338 * we need to prepare for allocating another r10_bio.
1340 r10_bio->sectors = max_sectors;
1341 spin_lock_irq(&conf->device_lock);
1342 if (bio->bi_phys_segments == 0)
1343 bio->bi_phys_segments = 2;
1344 else
1345 bio->bi_phys_segments++;
1346 spin_unlock_irq(&conf->device_lock);
1348 sectors_handled = r10_bio->sector + max_sectors -
1349 bio->bi_iter.bi_sector;
1351 atomic_set(&r10_bio->remaining, 1);
1352 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
1354 for (i = 0; i < conf->copies; i++) {
1355 struct bio *mbio;
1356 int d = r10_bio->devs[i].devnum;
1357 if (r10_bio->devs[i].bio) {
1358 struct md_rdev *rdev = conf->mirrors[d].rdev;
1359 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1360 bio_trim(mbio, r10_bio->sector - bio->bi_iter.bi_sector,
1361 max_sectors);
1362 r10_bio->devs[i].bio = mbio;
1364 mbio->bi_iter.bi_sector = (r10_bio->devs[i].addr+
1365 choose_data_offset(r10_bio,
1366 rdev));
1367 mbio->bi_bdev = rdev->bdev;
1368 mbio->bi_end_io = raid10_end_write_request;
1369 mbio->bi_rw =
1370 WRITE | do_sync | do_fua | do_discard | do_same;
1371 mbio->bi_private = r10_bio;
1373 atomic_inc(&r10_bio->remaining);
1375 cb = blk_check_plugged(raid10_unplug, mddev,
1376 sizeof(*plug));
1377 if (cb)
1378 plug = container_of(cb, struct raid10_plug_cb,
1379 cb);
1380 else
1381 plug = NULL;
1382 spin_lock_irqsave(&conf->device_lock, flags);
1383 if (plug) {
1384 bio_list_add(&plug->pending, mbio);
1385 plug->pending_cnt++;
1386 } else {
1387 bio_list_add(&conf->pending_bio_list, mbio);
1388 conf->pending_count++;
1390 spin_unlock_irqrestore(&conf->device_lock, flags);
1391 if (!plug)
1392 md_wakeup_thread(mddev->thread);
1395 if (r10_bio->devs[i].repl_bio) {
1396 struct md_rdev *rdev = conf->mirrors[d].replacement;
1397 if (rdev == NULL) {
1398 /* Replacement just got moved to main 'rdev' */
1399 smp_mb();
1400 rdev = conf->mirrors[d].rdev;
1402 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1403 bio_trim(mbio, r10_bio->sector - bio->bi_iter.bi_sector,
1404 max_sectors);
1405 r10_bio->devs[i].repl_bio = mbio;
1407 mbio->bi_iter.bi_sector = (r10_bio->devs[i].addr +
1408 choose_data_offset(
1409 r10_bio, rdev));
1410 mbio->bi_bdev = rdev->bdev;
1411 mbio->bi_end_io = raid10_end_write_request;
1412 mbio->bi_rw =
1413 WRITE | do_sync | do_fua | do_discard | do_same;
1414 mbio->bi_private = r10_bio;
1416 atomic_inc(&r10_bio->remaining);
1418 cb = blk_check_plugged(raid10_unplug, mddev,
1419 sizeof(*plug));
1420 if (cb)
1421 plug = container_of(cb, struct raid10_plug_cb,
1422 cb);
1423 else
1424 plug = NULL;
1425 spin_lock_irqsave(&conf->device_lock, flags);
1426 if (plug) {
1427 bio_list_add(&plug->pending, mbio);
1428 plug->pending_cnt++;
1429 } else {
1430 bio_list_add(&conf->pending_bio_list, mbio);
1431 conf->pending_count++;
1433 spin_unlock_irqrestore(&conf->device_lock, flags);
1434 if (!plug)
1435 md_wakeup_thread(mddev->thread);
1439 /* Don't remove the bias on 'remaining' (one_write_done) until
1440 * after checking if we need to go around again.
1443 if (sectors_handled < bio_sectors(bio)) {
1444 one_write_done(r10_bio);
1445 /* We need another r10_bio. It has already been counted
1446 * in bio->bi_phys_segments.
1448 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1450 r10_bio->master_bio = bio;
1451 r10_bio->sectors = bio_sectors(bio) - sectors_handled;
1453 r10_bio->mddev = mddev;
1454 r10_bio->sector = bio->bi_iter.bi_sector + sectors_handled;
1455 r10_bio->state = 0;
1456 goto retry_write;
1458 one_write_done(r10_bio);
1461 static void make_request(struct mddev *mddev, struct bio *bio)
1463 struct r10conf *conf = mddev->private;
1464 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
1465 int chunk_sects = chunk_mask + 1;
1467 struct bio *split;
1469 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
1470 md_flush_request(mddev, bio);
1471 return;
1474 do {
1477 * If this request crosses a chunk boundary, we need to split
1478 * it.
1480 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
1481 bio_sectors(bio) > chunk_sects
1482 && (conf->geo.near_copies < conf->geo.raid_disks
1483 || conf->prev.near_copies <
1484 conf->prev.raid_disks))) {
1485 split = bio_split(bio, chunk_sects -
1486 (bio->bi_iter.bi_sector &
1487 (chunk_sects - 1)),
1488 GFP_NOIO, fs_bio_set);
1489 bio_chain(split, bio);
1490 } else {
1491 split = bio;
1495 * If a bio is splitted, the first part of bio will pass
1496 * barrier but the bio is queued in current->bio_list (see
1497 * generic_make_request). If there is a raise_barrier() called
1498 * here, the second part of bio can't pass barrier. But since
1499 * the first part bio isn't dispatched to underlaying disks
1500 * yet, the barrier is never released, hence raise_barrier will
1501 * alays wait. We have a deadlock.
1502 * Note, this only happens in read path. For write path, the
1503 * first part of bio is dispatched in a schedule() call
1504 * (because of blk plug) or offloaded to raid10d.
1505 * Quitting from the function immediately can change the bio
1506 * order queued in bio_list and avoid the deadlock.
1508 __make_request(mddev, split);
1509 if (split != bio && bio_data_dir(bio) == READ) {
1510 generic_make_request(bio);
1511 break;
1513 } while (split != bio);
1515 /* In case raid10d snuck in to freeze_array */
1516 wake_up(&conf->wait_barrier);
1519 static void status(struct seq_file *seq, struct mddev *mddev)
1521 struct r10conf *conf = mddev->private;
1522 int i;
1524 if (conf->geo.near_copies < conf->geo.raid_disks)
1525 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
1526 if (conf->geo.near_copies > 1)
1527 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1528 if (conf->geo.far_copies > 1) {
1529 if (conf->geo.far_offset)
1530 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
1531 else
1532 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
1533 if (conf->geo.far_set_size != conf->geo.raid_disks)
1534 seq_printf(seq, " %d devices per set", conf->geo.far_set_size);
1536 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1537 conf->geo.raid_disks - mddev->degraded);
1538 for (i = 0; i < conf->geo.raid_disks; i++)
1539 seq_printf(seq, "%s",
1540 conf->mirrors[i].rdev &&
1541 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
1542 seq_printf(seq, "]");
1545 /* check if there are enough drives for
1546 * every block to appear on atleast one.
1547 * Don't consider the device numbered 'ignore'
1548 * as we might be about to remove it.
1550 static int _enough(struct r10conf *conf, int previous, int ignore)
1552 int first = 0;
1553 int has_enough = 0;
1554 int disks, ncopies;
1555 if (previous) {
1556 disks = conf->prev.raid_disks;
1557 ncopies = conf->prev.near_copies;
1558 } else {
1559 disks = conf->geo.raid_disks;
1560 ncopies = conf->geo.near_copies;
1563 rcu_read_lock();
1564 do {
1565 int n = conf->copies;
1566 int cnt = 0;
1567 int this = first;
1568 while (n--) {
1569 struct md_rdev *rdev;
1570 if (this != ignore &&
1571 (rdev = rcu_dereference(conf->mirrors[this].rdev)) &&
1572 test_bit(In_sync, &rdev->flags))
1573 cnt++;
1574 this = (this+1) % disks;
1576 if (cnt == 0)
1577 goto out;
1578 first = (first + ncopies) % disks;
1579 } while (first != 0);
1580 has_enough = 1;
1581 out:
1582 rcu_read_unlock();
1583 return has_enough;
1586 static int enough(struct r10conf *conf, int ignore)
1588 /* when calling 'enough', both 'prev' and 'geo' must
1589 * be stable.
1590 * This is ensured if ->reconfig_mutex or ->device_lock
1591 * is held.
1593 return _enough(conf, 0, ignore) &&
1594 _enough(conf, 1, ignore);
1597 static void error(struct mddev *mddev, struct md_rdev *rdev)
1599 char b[BDEVNAME_SIZE];
1600 struct r10conf *conf = mddev->private;
1601 unsigned long flags;
1604 * If it is not operational, then we have already marked it as dead
1605 * else if it is the last working disks, ignore the error, let the
1606 * next level up know.
1607 * else mark the drive as failed
1609 spin_lock_irqsave(&conf->device_lock, flags);
1610 if (test_bit(In_sync, &rdev->flags)
1611 && !enough(conf, rdev->raid_disk)) {
1613 * Don't fail the drive, just return an IO error.
1615 spin_unlock_irqrestore(&conf->device_lock, flags);
1616 return;
1618 if (test_and_clear_bit(In_sync, &rdev->flags))
1619 mddev->degraded++;
1621 * If recovery is running, make sure it aborts.
1623 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1624 set_bit(Blocked, &rdev->flags);
1625 set_bit(Faulty, &rdev->flags);
1626 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1627 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1628 spin_unlock_irqrestore(&conf->device_lock, flags);
1629 printk(KERN_ALERT
1630 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1631 "md/raid10:%s: Operation continuing on %d devices.\n",
1632 mdname(mddev), bdevname(rdev->bdev, b),
1633 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
1636 static void print_conf(struct r10conf *conf)
1638 int i;
1639 struct raid10_info *tmp;
1641 printk(KERN_DEBUG "RAID10 conf printout:\n");
1642 if (!conf) {
1643 printk(KERN_DEBUG "(!conf)\n");
1644 return;
1646 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
1647 conf->geo.raid_disks);
1649 for (i = 0; i < conf->geo.raid_disks; i++) {
1650 char b[BDEVNAME_SIZE];
1651 tmp = conf->mirrors + i;
1652 if (tmp->rdev)
1653 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
1654 i, !test_bit(In_sync, &tmp->rdev->flags),
1655 !test_bit(Faulty, &tmp->rdev->flags),
1656 bdevname(tmp->rdev->bdev,b));
1660 static void close_sync(struct r10conf *conf)
1662 wait_barrier(conf);
1663 allow_barrier(conf);
1665 mempool_destroy(conf->r10buf_pool);
1666 conf->r10buf_pool = NULL;
1669 static int raid10_spare_active(struct mddev *mddev)
1671 int i;
1672 struct r10conf *conf = mddev->private;
1673 struct raid10_info *tmp;
1674 int count = 0;
1675 unsigned long flags;
1678 * Find all non-in_sync disks within the RAID10 configuration
1679 * and mark them in_sync
1681 for (i = 0; i < conf->geo.raid_disks; i++) {
1682 tmp = conf->mirrors + i;
1683 if (tmp->replacement
1684 && tmp->replacement->recovery_offset == MaxSector
1685 && !test_bit(Faulty, &tmp->replacement->flags)
1686 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1687 /* Replacement has just become active */
1688 if (!tmp->rdev
1689 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1690 count++;
1691 if (tmp->rdev) {
1692 /* Replaced device not technically faulty,
1693 * but we need to be sure it gets removed
1694 * and never re-added.
1696 set_bit(Faulty, &tmp->rdev->flags);
1697 sysfs_notify_dirent_safe(
1698 tmp->rdev->sysfs_state);
1700 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1701 } else if (tmp->rdev
1702 && tmp->rdev->recovery_offset == MaxSector
1703 && !test_bit(Faulty, &tmp->rdev->flags)
1704 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1705 count++;
1706 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
1709 spin_lock_irqsave(&conf->device_lock, flags);
1710 mddev->degraded -= count;
1711 spin_unlock_irqrestore(&conf->device_lock, flags);
1713 print_conf(conf);
1714 return count;
1717 static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1719 struct r10conf *conf = mddev->private;
1720 int err = -EEXIST;
1721 int mirror;
1722 int first = 0;
1723 int last = conf->geo.raid_disks - 1;
1725 if (mddev->recovery_cp < MaxSector)
1726 /* only hot-add to in-sync arrays, as recovery is
1727 * very different from resync
1729 return -EBUSY;
1730 if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
1731 return -EINVAL;
1733 if (md_integrity_add_rdev(rdev, mddev))
1734 return -ENXIO;
1736 if (rdev->raid_disk >= 0)
1737 first = last = rdev->raid_disk;
1739 if (rdev->saved_raid_disk >= first &&
1740 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1741 mirror = rdev->saved_raid_disk;
1742 else
1743 mirror = first;
1744 for ( ; mirror <= last ; mirror++) {
1745 struct raid10_info *p = &conf->mirrors[mirror];
1746 if (p->recovery_disabled == mddev->recovery_disabled)
1747 continue;
1748 if (p->rdev) {
1749 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1750 p->replacement != NULL)
1751 continue;
1752 clear_bit(In_sync, &rdev->flags);
1753 set_bit(Replacement, &rdev->flags);
1754 rdev->raid_disk = mirror;
1755 err = 0;
1756 if (mddev->gendisk)
1757 disk_stack_limits(mddev->gendisk, rdev->bdev,
1758 rdev->data_offset << 9);
1759 conf->fullsync = 1;
1760 rcu_assign_pointer(p->replacement, rdev);
1761 break;
1764 if (mddev->gendisk)
1765 disk_stack_limits(mddev->gendisk, rdev->bdev,
1766 rdev->data_offset << 9);
1768 p->head_position = 0;
1769 p->recovery_disabled = mddev->recovery_disabled - 1;
1770 rdev->raid_disk = mirror;
1771 err = 0;
1772 if (rdev->saved_raid_disk != mirror)
1773 conf->fullsync = 1;
1774 rcu_assign_pointer(p->rdev, rdev);
1775 break;
1777 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
1778 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
1780 print_conf(conf);
1781 return err;
1784 static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1786 struct r10conf *conf = mddev->private;
1787 int err = 0;
1788 int number = rdev->raid_disk;
1789 struct md_rdev **rdevp;
1790 struct raid10_info *p = conf->mirrors + number;
1792 print_conf(conf);
1793 if (rdev == p->rdev)
1794 rdevp = &p->rdev;
1795 else if (rdev == p->replacement)
1796 rdevp = &p->replacement;
1797 else
1798 return 0;
1800 if (test_bit(In_sync, &rdev->flags) ||
1801 atomic_read(&rdev->nr_pending)) {
1802 err = -EBUSY;
1803 goto abort;
1805 /* Only remove faulty devices if recovery
1806 * is not possible.
1808 if (!test_bit(Faulty, &rdev->flags) &&
1809 mddev->recovery_disabled != p->recovery_disabled &&
1810 (!p->replacement || p->replacement == rdev) &&
1811 number < conf->geo.raid_disks &&
1812 enough(conf, -1)) {
1813 err = -EBUSY;
1814 goto abort;
1816 *rdevp = NULL;
1817 synchronize_rcu();
1818 if (atomic_read(&rdev->nr_pending)) {
1819 /* lost the race, try later */
1820 err = -EBUSY;
1821 *rdevp = rdev;
1822 goto abort;
1823 } else if (p->replacement) {
1824 /* We must have just cleared 'rdev' */
1825 p->rdev = p->replacement;
1826 clear_bit(Replacement, &p->replacement->flags);
1827 smp_mb(); /* Make sure other CPUs may see both as identical
1828 * but will never see neither -- if they are careful.
1830 p->replacement = NULL;
1831 clear_bit(WantReplacement, &rdev->flags);
1832 } else
1833 /* We might have just remove the Replacement as faulty
1834 * Clear the flag just in case
1836 clear_bit(WantReplacement, &rdev->flags);
1838 err = md_integrity_register(mddev);
1840 abort:
1842 print_conf(conf);
1843 return err;
1846 static void end_sync_read(struct bio *bio)
1848 struct r10bio *r10_bio = bio->bi_private;
1849 struct r10conf *conf = r10_bio->mddev->private;
1850 int d;
1852 if (bio == r10_bio->master_bio) {
1853 /* this is a reshape read */
1854 d = r10_bio->read_slot; /* really the read dev */
1855 } else
1856 d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
1858 if (!bio->bi_error)
1859 set_bit(R10BIO_Uptodate, &r10_bio->state);
1860 else
1861 /* The write handler will notice the lack of
1862 * R10BIO_Uptodate and record any errors etc
1864 atomic_add(r10_bio->sectors,
1865 &conf->mirrors[d].rdev->corrected_errors);
1867 /* for reconstruct, we always reschedule after a read.
1868 * for resync, only after all reads
1870 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1871 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1872 atomic_dec_and_test(&r10_bio->remaining)) {
1873 /* we have read all the blocks,
1874 * do the comparison in process context in raid10d
1876 reschedule_retry(r10_bio);
1880 static void end_sync_request(struct r10bio *r10_bio)
1882 struct mddev *mddev = r10_bio->mddev;
1884 while (atomic_dec_and_test(&r10_bio->remaining)) {
1885 if (r10_bio->master_bio == NULL) {
1886 /* the primary of several recovery bios */
1887 sector_t s = r10_bio->sectors;
1888 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1889 test_bit(R10BIO_WriteError, &r10_bio->state))
1890 reschedule_retry(r10_bio);
1891 else
1892 put_buf(r10_bio);
1893 md_done_sync(mddev, s, 1);
1894 break;
1895 } else {
1896 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
1897 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1898 test_bit(R10BIO_WriteError, &r10_bio->state))
1899 reschedule_retry(r10_bio);
1900 else
1901 put_buf(r10_bio);
1902 r10_bio = r10_bio2;
1907 static void end_sync_write(struct bio *bio)
1909 struct r10bio *r10_bio = bio->bi_private;
1910 struct mddev *mddev = r10_bio->mddev;
1911 struct r10conf *conf = mddev->private;
1912 int d;
1913 sector_t first_bad;
1914 int bad_sectors;
1915 int slot;
1916 int repl;
1917 struct md_rdev *rdev = NULL;
1919 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1920 if (repl)
1921 rdev = conf->mirrors[d].replacement;
1922 else
1923 rdev = conf->mirrors[d].rdev;
1925 if (bio->bi_error) {
1926 if (repl)
1927 md_error(mddev, rdev);
1928 else {
1929 set_bit(WriteErrorSeen, &rdev->flags);
1930 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1931 set_bit(MD_RECOVERY_NEEDED,
1932 &rdev->mddev->recovery);
1933 set_bit(R10BIO_WriteError, &r10_bio->state);
1935 } else if (is_badblock(rdev,
1936 r10_bio->devs[slot].addr,
1937 r10_bio->sectors,
1938 &first_bad, &bad_sectors))
1939 set_bit(R10BIO_MadeGood, &r10_bio->state);
1941 rdev_dec_pending(rdev, mddev);
1943 end_sync_request(r10_bio);
1947 * Note: sync and recover and handled very differently for raid10
1948 * This code is for resync.
1949 * For resync, we read through virtual addresses and read all blocks.
1950 * If there is any error, we schedule a write. The lowest numbered
1951 * drive is authoritative.
1952 * However requests come for physical address, so we need to map.
1953 * For every physical address there are raid_disks/copies virtual addresses,
1954 * which is always are least one, but is not necessarly an integer.
1955 * This means that a physical address can span multiple chunks, so we may
1956 * have to submit multiple io requests for a single sync request.
1959 * We check if all blocks are in-sync and only write to blocks that
1960 * aren't in sync
1962 static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
1964 struct r10conf *conf = mddev->private;
1965 int i, first;
1966 struct bio *tbio, *fbio;
1967 int vcnt;
1969 atomic_set(&r10_bio->remaining, 1);
1971 /* find the first device with a block */
1972 for (i=0; i<conf->copies; i++)
1973 if (!r10_bio->devs[i].bio->bi_error)
1974 break;
1976 if (i == conf->copies)
1977 goto done;
1979 first = i;
1980 fbio = r10_bio->devs[i].bio;
1981 fbio->bi_iter.bi_size = r10_bio->sectors << 9;
1982 fbio->bi_iter.bi_idx = 0;
1984 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
1985 /* now find blocks with errors */
1986 for (i=0 ; i < conf->copies ; i++) {
1987 int j, d;
1989 tbio = r10_bio->devs[i].bio;
1991 if (tbio->bi_end_io != end_sync_read)
1992 continue;
1993 if (i == first)
1994 continue;
1995 if (!r10_bio->devs[i].bio->bi_error) {
1996 /* We know that the bi_io_vec layout is the same for
1997 * both 'first' and 'i', so we just compare them.
1998 * All vec entries are PAGE_SIZE;
2000 int sectors = r10_bio->sectors;
2001 for (j = 0; j < vcnt; j++) {
2002 int len = PAGE_SIZE;
2003 if (sectors < (len / 512))
2004 len = sectors * 512;
2005 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
2006 page_address(tbio->bi_io_vec[j].bv_page),
2007 len))
2008 break;
2009 sectors -= len/512;
2011 if (j == vcnt)
2012 continue;
2013 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
2014 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2015 /* Don't fix anything. */
2016 continue;
2018 /* Ok, we need to write this bio, either to correct an
2019 * inconsistency or to correct an unreadable block.
2020 * First we need to fixup bv_offset, bv_len and
2021 * bi_vecs, as the read request might have corrupted these
2023 bio_reset(tbio);
2025 tbio->bi_vcnt = vcnt;
2026 tbio->bi_iter.bi_size = fbio->bi_iter.bi_size;
2027 tbio->bi_rw = WRITE;
2028 tbio->bi_private = r10_bio;
2029 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr;
2030 tbio->bi_end_io = end_sync_write;
2032 bio_copy_data(tbio, fbio);
2034 d = r10_bio->devs[i].devnum;
2035 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2036 atomic_inc(&r10_bio->remaining);
2037 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
2039 tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset;
2040 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
2041 generic_make_request(tbio);
2044 /* Now write out to any replacement devices
2045 * that are active
2047 for (i = 0; i < conf->copies; i++) {
2048 int d;
2050 tbio = r10_bio->devs[i].repl_bio;
2051 if (!tbio || !tbio->bi_end_io)
2052 continue;
2053 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2054 && r10_bio->devs[i].bio != fbio)
2055 bio_copy_data(tbio, fbio);
2056 d = r10_bio->devs[i].devnum;
2057 atomic_inc(&r10_bio->remaining);
2058 md_sync_acct(conf->mirrors[d].replacement->bdev,
2059 bio_sectors(tbio));
2060 generic_make_request(tbio);
2063 done:
2064 if (atomic_dec_and_test(&r10_bio->remaining)) {
2065 md_done_sync(mddev, r10_bio->sectors, 1);
2066 put_buf(r10_bio);
2071 * Now for the recovery code.
2072 * Recovery happens across physical sectors.
2073 * We recover all non-is_sync drives by finding the virtual address of
2074 * each, and then choose a working drive that also has that virt address.
2075 * There is a separate r10_bio for each non-in_sync drive.
2076 * Only the first two slots are in use. The first for reading,
2077 * The second for writing.
2080 static void fix_recovery_read_error(struct r10bio *r10_bio)
2082 /* We got a read error during recovery.
2083 * We repeat the read in smaller page-sized sections.
2084 * If a read succeeds, write it to the new device or record
2085 * a bad block if we cannot.
2086 * If a read fails, record a bad block on both old and
2087 * new devices.
2089 struct mddev *mddev = r10_bio->mddev;
2090 struct r10conf *conf = mddev->private;
2091 struct bio *bio = r10_bio->devs[0].bio;
2092 sector_t sect = 0;
2093 int sectors = r10_bio->sectors;
2094 int idx = 0;
2095 int dr = r10_bio->devs[0].devnum;
2096 int dw = r10_bio->devs[1].devnum;
2098 while (sectors) {
2099 int s = sectors;
2100 struct md_rdev *rdev;
2101 sector_t addr;
2102 int ok;
2104 if (s > (PAGE_SIZE>>9))
2105 s = PAGE_SIZE >> 9;
2107 rdev = conf->mirrors[dr].rdev;
2108 addr = r10_bio->devs[0].addr + sect,
2109 ok = sync_page_io(rdev,
2110 addr,
2111 s << 9,
2112 bio->bi_io_vec[idx].bv_page,
2113 READ, false);
2114 if (ok) {
2115 rdev = conf->mirrors[dw].rdev;
2116 addr = r10_bio->devs[1].addr + sect;
2117 ok = sync_page_io(rdev,
2118 addr,
2119 s << 9,
2120 bio->bi_io_vec[idx].bv_page,
2121 WRITE, false);
2122 if (!ok) {
2123 set_bit(WriteErrorSeen, &rdev->flags);
2124 if (!test_and_set_bit(WantReplacement,
2125 &rdev->flags))
2126 set_bit(MD_RECOVERY_NEEDED,
2127 &rdev->mddev->recovery);
2130 if (!ok) {
2131 /* We don't worry if we cannot set a bad block -
2132 * it really is bad so there is no loss in not
2133 * recording it yet
2135 rdev_set_badblocks(rdev, addr, s, 0);
2137 if (rdev != conf->mirrors[dw].rdev) {
2138 /* need bad block on destination too */
2139 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
2140 addr = r10_bio->devs[1].addr + sect;
2141 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2142 if (!ok) {
2143 /* just abort the recovery */
2144 printk(KERN_NOTICE
2145 "md/raid10:%s: recovery aborted"
2146 " due to read error\n",
2147 mdname(mddev));
2149 conf->mirrors[dw].recovery_disabled
2150 = mddev->recovery_disabled;
2151 set_bit(MD_RECOVERY_INTR,
2152 &mddev->recovery);
2153 break;
2158 sectors -= s;
2159 sect += s;
2160 idx++;
2164 static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
2166 struct r10conf *conf = mddev->private;
2167 int d;
2168 struct bio *wbio, *wbio2;
2170 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2171 fix_recovery_read_error(r10_bio);
2172 end_sync_request(r10_bio);
2173 return;
2177 * share the pages with the first bio
2178 * and submit the write request
2180 d = r10_bio->devs[1].devnum;
2181 wbio = r10_bio->devs[1].bio;
2182 wbio2 = r10_bio->devs[1].repl_bio;
2183 /* Need to test wbio2->bi_end_io before we call
2184 * generic_make_request as if the former is NULL,
2185 * the latter is free to free wbio2.
2187 if (wbio2 && !wbio2->bi_end_io)
2188 wbio2 = NULL;
2189 if (wbio->bi_end_io) {
2190 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2191 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
2192 generic_make_request(wbio);
2194 if (wbio2) {
2195 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2196 md_sync_acct(conf->mirrors[d].replacement->bdev,
2197 bio_sectors(wbio2));
2198 generic_make_request(wbio2);
2203 * Used by fix_read_error() to decay the per rdev read_errors.
2204 * We halve the read error count for every hour that has elapsed
2205 * since the last recorded read error.
2208 static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
2210 struct timespec cur_time_mon;
2211 unsigned long hours_since_last;
2212 unsigned int read_errors = atomic_read(&rdev->read_errors);
2214 ktime_get_ts(&cur_time_mon);
2216 if (rdev->last_read_error.tv_sec == 0 &&
2217 rdev->last_read_error.tv_nsec == 0) {
2218 /* first time we've seen a read error */
2219 rdev->last_read_error = cur_time_mon;
2220 return;
2223 hours_since_last = (cur_time_mon.tv_sec -
2224 rdev->last_read_error.tv_sec) / 3600;
2226 rdev->last_read_error = cur_time_mon;
2229 * if hours_since_last is > the number of bits in read_errors
2230 * just set read errors to 0. We do this to avoid
2231 * overflowing the shift of read_errors by hours_since_last.
2233 if (hours_since_last >= 8 * sizeof(read_errors))
2234 atomic_set(&rdev->read_errors, 0);
2235 else
2236 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2239 static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
2240 int sectors, struct page *page, int rw)
2242 sector_t first_bad;
2243 int bad_sectors;
2245 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2246 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2247 return -1;
2248 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
2249 /* success */
2250 return 1;
2251 if (rw == WRITE) {
2252 set_bit(WriteErrorSeen, &rdev->flags);
2253 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2254 set_bit(MD_RECOVERY_NEEDED,
2255 &rdev->mddev->recovery);
2257 /* need to record an error - either for the block or the device */
2258 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2259 md_error(rdev->mddev, rdev);
2260 return 0;
2264 * This is a kernel thread which:
2266 * 1. Retries failed read operations on working mirrors.
2267 * 2. Updates the raid superblock when problems encounter.
2268 * 3. Performs writes following reads for array synchronising.
2271 static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
2273 int sect = 0; /* Offset from r10_bio->sector */
2274 int sectors = r10_bio->sectors;
2275 struct md_rdev*rdev;
2276 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
2277 int d = r10_bio->devs[r10_bio->read_slot].devnum;
2279 /* still own a reference to this rdev, so it cannot
2280 * have been cleared recently.
2282 rdev = conf->mirrors[d].rdev;
2284 if (test_bit(Faulty, &rdev->flags))
2285 /* drive has already been failed, just ignore any
2286 more fix_read_error() attempts */
2287 return;
2289 check_decay_read_errors(mddev, rdev);
2290 atomic_inc(&rdev->read_errors);
2291 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2292 char b[BDEVNAME_SIZE];
2293 bdevname(rdev->bdev, b);
2295 printk(KERN_NOTICE
2296 "md/raid10:%s: %s: Raid device exceeded "
2297 "read_error threshold [cur %d:max %d]\n",
2298 mdname(mddev), b,
2299 atomic_read(&rdev->read_errors), max_read_errors);
2300 printk(KERN_NOTICE
2301 "md/raid10:%s: %s: Failing raid device\n",
2302 mdname(mddev), b);
2303 md_error(mddev, conf->mirrors[d].rdev);
2304 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
2305 return;
2308 while(sectors) {
2309 int s = sectors;
2310 int sl = r10_bio->read_slot;
2311 int success = 0;
2312 int start;
2314 if (s > (PAGE_SIZE>>9))
2315 s = PAGE_SIZE >> 9;
2317 rcu_read_lock();
2318 do {
2319 sector_t first_bad;
2320 int bad_sectors;
2322 d = r10_bio->devs[sl].devnum;
2323 rdev = rcu_dereference(conf->mirrors[d].rdev);
2324 if (rdev &&
2325 test_bit(In_sync, &rdev->flags) &&
2326 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2327 &first_bad, &bad_sectors) == 0) {
2328 atomic_inc(&rdev->nr_pending);
2329 rcu_read_unlock();
2330 success = sync_page_io(rdev,
2331 r10_bio->devs[sl].addr +
2332 sect,
2333 s<<9,
2334 conf->tmppage, READ, false);
2335 rdev_dec_pending(rdev, mddev);
2336 rcu_read_lock();
2337 if (success)
2338 break;
2340 sl++;
2341 if (sl == conf->copies)
2342 sl = 0;
2343 } while (!success && sl != r10_bio->read_slot);
2344 rcu_read_unlock();
2346 if (!success) {
2347 /* Cannot read from anywhere, just mark the block
2348 * as bad on the first device to discourage future
2349 * reads.
2351 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
2352 rdev = conf->mirrors[dn].rdev;
2354 if (!rdev_set_badblocks(
2355 rdev,
2356 r10_bio->devs[r10_bio->read_slot].addr
2357 + sect,
2358 s, 0)) {
2359 md_error(mddev, rdev);
2360 r10_bio->devs[r10_bio->read_slot].bio
2361 = IO_BLOCKED;
2363 break;
2366 start = sl;
2367 /* write it back and re-read */
2368 rcu_read_lock();
2369 while (sl != r10_bio->read_slot) {
2370 char b[BDEVNAME_SIZE];
2372 if (sl==0)
2373 sl = conf->copies;
2374 sl--;
2375 d = r10_bio->devs[sl].devnum;
2376 rdev = rcu_dereference(conf->mirrors[d].rdev);
2377 if (!rdev ||
2378 !test_bit(In_sync, &rdev->flags))
2379 continue;
2381 atomic_inc(&rdev->nr_pending);
2382 rcu_read_unlock();
2383 if (r10_sync_page_io(rdev,
2384 r10_bio->devs[sl].addr +
2385 sect,
2386 s, conf->tmppage, WRITE)
2387 == 0) {
2388 /* Well, this device is dead */
2389 printk(KERN_NOTICE
2390 "md/raid10:%s: read correction "
2391 "write failed"
2392 " (%d sectors at %llu on %s)\n",
2393 mdname(mddev), s,
2394 (unsigned long long)(
2395 sect +
2396 choose_data_offset(r10_bio,
2397 rdev)),
2398 bdevname(rdev->bdev, b));
2399 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2400 "drive\n",
2401 mdname(mddev),
2402 bdevname(rdev->bdev, b));
2404 rdev_dec_pending(rdev, mddev);
2405 rcu_read_lock();
2407 sl = start;
2408 while (sl != r10_bio->read_slot) {
2409 char b[BDEVNAME_SIZE];
2411 if (sl==0)
2412 sl = conf->copies;
2413 sl--;
2414 d = r10_bio->devs[sl].devnum;
2415 rdev = rcu_dereference(conf->mirrors[d].rdev);
2416 if (!rdev ||
2417 !test_bit(In_sync, &rdev->flags))
2418 continue;
2420 atomic_inc(&rdev->nr_pending);
2421 rcu_read_unlock();
2422 switch (r10_sync_page_io(rdev,
2423 r10_bio->devs[sl].addr +
2424 sect,
2425 s, conf->tmppage,
2426 READ)) {
2427 case 0:
2428 /* Well, this device is dead */
2429 printk(KERN_NOTICE
2430 "md/raid10:%s: unable to read back "
2431 "corrected sectors"
2432 " (%d sectors at %llu on %s)\n",
2433 mdname(mddev), s,
2434 (unsigned long long)(
2435 sect +
2436 choose_data_offset(r10_bio, rdev)),
2437 bdevname(rdev->bdev, b));
2438 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2439 "drive\n",
2440 mdname(mddev),
2441 bdevname(rdev->bdev, b));
2442 break;
2443 case 1:
2444 printk(KERN_INFO
2445 "md/raid10:%s: read error corrected"
2446 " (%d sectors at %llu on %s)\n",
2447 mdname(mddev), s,
2448 (unsigned long long)(
2449 sect +
2450 choose_data_offset(r10_bio, rdev)),
2451 bdevname(rdev->bdev, b));
2452 atomic_add(s, &rdev->corrected_errors);
2455 rdev_dec_pending(rdev, mddev);
2456 rcu_read_lock();
2458 rcu_read_unlock();
2460 sectors -= s;
2461 sect += s;
2465 static int narrow_write_error(struct r10bio *r10_bio, int i)
2467 struct bio *bio = r10_bio->master_bio;
2468 struct mddev *mddev = r10_bio->mddev;
2469 struct r10conf *conf = mddev->private;
2470 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
2471 /* bio has the data to be written to slot 'i' where
2472 * we just recently had a write error.
2473 * We repeatedly clone the bio and trim down to one block,
2474 * then try the write. Where the write fails we record
2475 * a bad block.
2476 * It is conceivable that the bio doesn't exactly align with
2477 * blocks. We must handle this.
2479 * We currently own a reference to the rdev.
2482 int block_sectors;
2483 sector_t sector;
2484 int sectors;
2485 int sect_to_write = r10_bio->sectors;
2486 int ok = 1;
2488 if (rdev->badblocks.shift < 0)
2489 return 0;
2491 block_sectors = roundup(1 << rdev->badblocks.shift,
2492 bdev_logical_block_size(rdev->bdev) >> 9);
2493 sector = r10_bio->sector;
2494 sectors = ((r10_bio->sector + block_sectors)
2495 & ~(sector_t)(block_sectors - 1))
2496 - sector;
2498 while (sect_to_write) {
2499 struct bio *wbio;
2500 if (sectors > sect_to_write)
2501 sectors = sect_to_write;
2502 /* Write at 'sector' for 'sectors' */
2503 wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
2504 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors);
2505 wbio->bi_iter.bi_sector = (r10_bio->devs[i].addr+
2506 choose_data_offset(r10_bio, rdev) +
2507 (sector - r10_bio->sector));
2508 wbio->bi_bdev = rdev->bdev;
2509 if (submit_bio_wait(WRITE, wbio) < 0)
2510 /* Failure! */
2511 ok = rdev_set_badblocks(rdev, sector,
2512 sectors, 0)
2513 && ok;
2515 bio_put(wbio);
2516 sect_to_write -= sectors;
2517 sector += sectors;
2518 sectors = block_sectors;
2520 return ok;
2523 static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
2525 int slot = r10_bio->read_slot;
2526 struct bio *bio;
2527 struct r10conf *conf = mddev->private;
2528 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
2529 char b[BDEVNAME_SIZE];
2530 unsigned long do_sync;
2531 int max_sectors;
2533 /* we got a read error. Maybe the drive is bad. Maybe just
2534 * the block and we can fix it.
2535 * We freeze all other IO, and try reading the block from
2536 * other devices. When we find one, we re-write
2537 * and check it that fixes the read error.
2538 * This is all done synchronously while the array is
2539 * frozen.
2541 bio = r10_bio->devs[slot].bio;
2542 bdevname(bio->bi_bdev, b);
2543 bio_put(bio);
2544 r10_bio->devs[slot].bio = NULL;
2546 if (mddev->ro == 0) {
2547 freeze_array(conf, 1);
2548 fix_read_error(conf, mddev, r10_bio);
2549 unfreeze_array(conf);
2550 } else
2551 r10_bio->devs[slot].bio = IO_BLOCKED;
2553 rdev_dec_pending(rdev, mddev);
2555 read_more:
2556 rdev = read_balance(conf, r10_bio, &max_sectors);
2557 if (rdev == NULL) {
2558 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
2559 " read error for block %llu\n",
2560 mdname(mddev), b,
2561 (unsigned long long)r10_bio->sector);
2562 raid_end_bio_io(r10_bio);
2563 return;
2566 do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
2567 slot = r10_bio->read_slot;
2568 printk_ratelimited(
2569 KERN_ERR
2570 "md/raid10:%s: %s: redirecting "
2571 "sector %llu to another mirror\n",
2572 mdname(mddev),
2573 bdevname(rdev->bdev, b),
2574 (unsigned long long)r10_bio->sector);
2575 bio = bio_clone_mddev(r10_bio->master_bio,
2576 GFP_NOIO, mddev);
2577 bio_trim(bio, r10_bio->sector - bio->bi_iter.bi_sector, max_sectors);
2578 r10_bio->devs[slot].bio = bio;
2579 r10_bio->devs[slot].rdev = rdev;
2580 bio->bi_iter.bi_sector = r10_bio->devs[slot].addr
2581 + choose_data_offset(r10_bio, rdev);
2582 bio->bi_bdev = rdev->bdev;
2583 bio->bi_rw = READ | do_sync;
2584 bio->bi_private = r10_bio;
2585 bio->bi_end_io = raid10_end_read_request;
2586 if (max_sectors < r10_bio->sectors) {
2587 /* Drat - have to split this up more */
2588 struct bio *mbio = r10_bio->master_bio;
2589 int sectors_handled =
2590 r10_bio->sector + max_sectors
2591 - mbio->bi_iter.bi_sector;
2592 r10_bio->sectors = max_sectors;
2593 spin_lock_irq(&conf->device_lock);
2594 if (mbio->bi_phys_segments == 0)
2595 mbio->bi_phys_segments = 2;
2596 else
2597 mbio->bi_phys_segments++;
2598 spin_unlock_irq(&conf->device_lock);
2599 generic_make_request(bio);
2601 r10_bio = mempool_alloc(conf->r10bio_pool,
2602 GFP_NOIO);
2603 r10_bio->master_bio = mbio;
2604 r10_bio->sectors = bio_sectors(mbio) - sectors_handled;
2605 r10_bio->state = 0;
2606 set_bit(R10BIO_ReadError,
2607 &r10_bio->state);
2608 r10_bio->mddev = mddev;
2609 r10_bio->sector = mbio->bi_iter.bi_sector
2610 + sectors_handled;
2612 goto read_more;
2613 } else
2614 generic_make_request(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_error) {
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_error) {
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_error) {
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_CHANGE_PENDING, &mddev->flags)) {
2731 LIST_HEAD(tmp);
2732 spin_lock_irqsave(&conf->device_lock, flags);
2733 if (!test_bit(MD_CHANGE_PENDING, &mddev->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 /* just a partial read to be scheduled from a
2784 * separate context
2786 int slot = r10_bio->read_slot;
2787 generic_make_request(r10_bio->devs[slot].bio);
2790 cond_resched();
2791 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2792 md_check_recovery(mddev);
2794 blk_finish_plug(&plug);
2797 static int init_resync(struct r10conf *conf)
2799 int buffs;
2800 int i;
2802 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
2803 BUG_ON(conf->r10buf_pool);
2804 conf->have_replacement = 0;
2805 for (i = 0; i < conf->geo.raid_disks; i++)
2806 if (conf->mirrors[i].replacement)
2807 conf->have_replacement = 1;
2808 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2809 if (!conf->r10buf_pool)
2810 return -ENOMEM;
2811 conf->next_resync = 0;
2812 return 0;
2816 * perform a "sync" on one "block"
2818 * We need to make sure that no normal I/O request - particularly write
2819 * requests - conflict with active sync requests.
2821 * This is achieved by tracking pending requests and a 'barrier' concept
2822 * that can be installed to exclude normal IO requests.
2824 * Resync and recovery are handled very differently.
2825 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2827 * For resync, we iterate over virtual addresses, read all copies,
2828 * and update if there are differences. If only one copy is live,
2829 * skip it.
2830 * For recovery, we iterate over physical addresses, read a good
2831 * value for each non-in_sync drive, and over-write.
2833 * So, for recovery we may have several outstanding complex requests for a
2834 * given address, one for each out-of-sync device. We model this by allocating
2835 * a number of r10_bio structures, one for each out-of-sync device.
2836 * As we setup these structures, we collect all bio's together into a list
2837 * which we then process collectively to add pages, and then process again
2838 * to pass to generic_make_request.
2840 * The r10_bio structures are linked using a borrowed master_bio pointer.
2841 * This link is counted in ->remaining. When the r10_bio that points to NULL
2842 * has its remaining count decremented to 0, the whole complex operation
2843 * is complete.
2847 static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
2848 int *skipped)
2850 struct r10conf *conf = mddev->private;
2851 struct r10bio *r10_bio;
2852 struct bio *biolist = NULL, *bio;
2853 sector_t max_sector, nr_sectors;
2854 int i;
2855 int max_sync;
2856 sector_t sync_blocks;
2857 sector_t sectors_skipped = 0;
2858 int chunks_skipped = 0;
2859 sector_t chunk_mask = conf->geo.chunk_mask;
2861 if (!conf->r10buf_pool)
2862 if (init_resync(conf))
2863 return 0;
2866 * Allow skipping a full rebuild for incremental assembly
2867 * of a clean array, like RAID1 does.
2869 if (mddev->bitmap == NULL &&
2870 mddev->recovery_cp == MaxSector &&
2871 mddev->reshape_position == MaxSector &&
2872 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
2873 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
2874 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
2875 conf->fullsync == 0) {
2876 *skipped = 1;
2877 return mddev->dev_sectors - sector_nr;
2880 skipped:
2881 max_sector = mddev->dev_sectors;
2882 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
2883 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2884 max_sector = mddev->resync_max_sectors;
2885 if (sector_nr >= max_sector) {
2886 /* If we aborted, we need to abort the
2887 * sync on the 'current' bitmap chucks (there can
2888 * be several when recovering multiple devices).
2889 * as we may have started syncing it but not finished.
2890 * We can find the current address in
2891 * mddev->curr_resync, but for recovery,
2892 * we need to convert that to several
2893 * virtual addresses.
2895 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
2896 end_reshape(conf);
2897 close_sync(conf);
2898 return 0;
2901 if (mddev->curr_resync < max_sector) { /* aborted */
2902 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2903 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2904 &sync_blocks, 1);
2905 else for (i = 0; i < conf->geo.raid_disks; i++) {
2906 sector_t sect =
2907 raid10_find_virt(conf, mddev->curr_resync, i);
2908 bitmap_end_sync(mddev->bitmap, sect,
2909 &sync_blocks, 1);
2911 } else {
2912 /* completed sync */
2913 if ((!mddev->bitmap || conf->fullsync)
2914 && conf->have_replacement
2915 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2916 /* Completed a full sync so the replacements
2917 * are now fully recovered.
2919 for (i = 0; i < conf->geo.raid_disks; i++)
2920 if (conf->mirrors[i].replacement)
2921 conf->mirrors[i].replacement
2922 ->recovery_offset
2923 = MaxSector;
2925 conf->fullsync = 0;
2927 bitmap_close_sync(mddev->bitmap);
2928 close_sync(conf);
2929 *skipped = 1;
2930 return sectors_skipped;
2933 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2934 return reshape_request(mddev, sector_nr, skipped);
2936 if (chunks_skipped >= conf->geo.raid_disks) {
2937 /* if there has been nothing to do on any drive,
2938 * then there is nothing to do at all..
2940 *skipped = 1;
2941 return (max_sector - sector_nr) + sectors_skipped;
2944 if (max_sector > mddev->resync_max)
2945 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2947 /* make sure whole request will fit in a chunk - if chunks
2948 * are meaningful
2950 if (conf->geo.near_copies < conf->geo.raid_disks &&
2951 max_sector > (sector_nr | chunk_mask))
2952 max_sector = (sector_nr | chunk_mask) + 1;
2954 /* Again, very different code for resync and recovery.
2955 * Both must result in an r10bio with a list of bios that
2956 * have bi_end_io, bi_sector, bi_bdev set,
2957 * and bi_private set to the r10bio.
2958 * For recovery, we may actually create several r10bios
2959 * with 2 bios in each, that correspond to the bios in the main one.
2960 * In this case, the subordinate r10bios link back through a
2961 * borrowed master_bio pointer, and the counter in the master
2962 * includes a ref from each subordinate.
2964 /* First, we decide what to do and set ->bi_end_io
2965 * To end_sync_read if we want to read, and
2966 * end_sync_write if we will want to write.
2969 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
2970 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2971 /* recovery... the complicated one */
2972 int j;
2973 r10_bio = NULL;
2975 for (i = 0 ; i < conf->geo.raid_disks; i++) {
2976 int still_degraded;
2977 struct r10bio *rb2;
2978 sector_t sect;
2979 int must_sync;
2980 int any_working;
2981 struct raid10_info *mirror = &conf->mirrors[i];
2983 if ((mirror->rdev == NULL ||
2984 test_bit(In_sync, &mirror->rdev->flags))
2986 (mirror->replacement == NULL ||
2987 test_bit(Faulty,
2988 &mirror->replacement->flags)))
2989 continue;
2991 still_degraded = 0;
2992 /* want to reconstruct this device */
2993 rb2 = r10_bio;
2994 sect = raid10_find_virt(conf, sector_nr, i);
2995 if (sect >= mddev->resync_max_sectors) {
2996 /* last stripe is not complete - don't
2997 * try to recover this sector.
2999 continue;
3001 /* Unless we are doing a full sync, or a replacement
3002 * we only need to recover the block if it is set in
3003 * the bitmap
3005 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3006 &sync_blocks, 1);
3007 if (sync_blocks < max_sync)
3008 max_sync = sync_blocks;
3009 if (!must_sync &&
3010 mirror->replacement == NULL &&
3011 !conf->fullsync) {
3012 /* yep, skip the sync_blocks here, but don't assume
3013 * that there will never be anything to do here
3015 chunks_skipped = -1;
3016 continue;
3019 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
3020 r10_bio->state = 0;
3021 raise_barrier(conf, rb2 != NULL);
3022 atomic_set(&r10_bio->remaining, 0);
3024 r10_bio->master_bio = (struct bio*)rb2;
3025 if (rb2)
3026 atomic_inc(&rb2->remaining);
3027 r10_bio->mddev = mddev;
3028 set_bit(R10BIO_IsRecover, &r10_bio->state);
3029 r10_bio->sector = sect;
3031 raid10_find_phys(conf, r10_bio);
3033 /* Need to check if the array will still be
3034 * degraded
3036 for (j = 0; j < conf->geo.raid_disks; j++)
3037 if (conf->mirrors[j].rdev == NULL ||
3038 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
3039 still_degraded = 1;
3040 break;
3043 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3044 &sync_blocks, still_degraded);
3046 any_working = 0;
3047 for (j=0; j<conf->copies;j++) {
3048 int k;
3049 int d = r10_bio->devs[j].devnum;
3050 sector_t from_addr, to_addr;
3051 struct md_rdev *rdev;
3052 sector_t sector, first_bad;
3053 int bad_sectors;
3054 if (!conf->mirrors[d].rdev ||
3055 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
3056 continue;
3057 /* This is where we read from */
3058 any_working = 1;
3059 rdev = conf->mirrors[d].rdev;
3060 sector = r10_bio->devs[j].addr;
3062 if (is_badblock(rdev, sector, max_sync,
3063 &first_bad, &bad_sectors)) {
3064 if (first_bad > sector)
3065 max_sync = first_bad - sector;
3066 else {
3067 bad_sectors -= (sector
3068 - first_bad);
3069 if (max_sync > bad_sectors)
3070 max_sync = bad_sectors;
3071 continue;
3074 bio = r10_bio->devs[0].bio;
3075 bio_reset(bio);
3076 bio->bi_next = biolist;
3077 biolist = bio;
3078 bio->bi_private = r10_bio;
3079 bio->bi_end_io = end_sync_read;
3080 bio->bi_rw = READ;
3081 from_addr = r10_bio->devs[j].addr;
3082 bio->bi_iter.bi_sector = from_addr +
3083 rdev->data_offset;
3084 bio->bi_bdev = rdev->bdev;
3085 atomic_inc(&rdev->nr_pending);
3086 /* and we write to 'i' (if not in_sync) */
3088 for (k=0; k<conf->copies; k++)
3089 if (r10_bio->devs[k].devnum == i)
3090 break;
3091 BUG_ON(k == conf->copies);
3092 to_addr = r10_bio->devs[k].addr;
3093 r10_bio->devs[0].devnum = d;
3094 r10_bio->devs[0].addr = from_addr;
3095 r10_bio->devs[1].devnum = i;
3096 r10_bio->devs[1].addr = to_addr;
3098 rdev = mirror->rdev;
3099 if (!test_bit(In_sync, &rdev->flags)) {
3100 bio = r10_bio->devs[1].bio;
3101 bio_reset(bio);
3102 bio->bi_next = biolist;
3103 biolist = bio;
3104 bio->bi_private = r10_bio;
3105 bio->bi_end_io = end_sync_write;
3106 bio->bi_rw = WRITE;
3107 bio->bi_iter.bi_sector = to_addr
3108 + rdev->data_offset;
3109 bio->bi_bdev = rdev->bdev;
3110 atomic_inc(&r10_bio->remaining);
3111 } else
3112 r10_bio->devs[1].bio->bi_end_io = NULL;
3114 /* and maybe write to replacement */
3115 bio = r10_bio->devs[1].repl_bio;
3116 if (bio)
3117 bio->bi_end_io = NULL;
3118 rdev = mirror->replacement;
3119 /* Note: if rdev != NULL, then bio
3120 * cannot be NULL as r10buf_pool_alloc will
3121 * have allocated it.
3122 * So the second test here is pointless.
3123 * But it keeps semantic-checkers happy, and
3124 * this comment keeps human reviewers
3125 * happy.
3127 if (rdev == NULL || bio == NULL ||
3128 test_bit(Faulty, &rdev->flags))
3129 break;
3130 bio_reset(bio);
3131 bio->bi_next = biolist;
3132 biolist = bio;
3133 bio->bi_private = r10_bio;
3134 bio->bi_end_io = end_sync_write;
3135 bio->bi_rw = WRITE;
3136 bio->bi_iter.bi_sector = to_addr +
3137 rdev->data_offset;
3138 bio->bi_bdev = rdev->bdev;
3139 atomic_inc(&r10_bio->remaining);
3140 break;
3142 if (j == conf->copies) {
3143 /* Cannot recover, so abort the recovery or
3144 * record a bad block */
3145 if (any_working) {
3146 /* problem is that there are bad blocks
3147 * on other device(s)
3149 int k;
3150 for (k = 0; k < conf->copies; k++)
3151 if (r10_bio->devs[k].devnum == i)
3152 break;
3153 if (!test_bit(In_sync,
3154 &mirror->rdev->flags)
3155 && !rdev_set_badblocks(
3156 mirror->rdev,
3157 r10_bio->devs[k].addr,
3158 max_sync, 0))
3159 any_working = 0;
3160 if (mirror->replacement &&
3161 !rdev_set_badblocks(
3162 mirror->replacement,
3163 r10_bio->devs[k].addr,
3164 max_sync, 0))
3165 any_working = 0;
3167 if (!any_working) {
3168 if (!test_and_set_bit(MD_RECOVERY_INTR,
3169 &mddev->recovery))
3170 printk(KERN_INFO "md/raid10:%s: insufficient "
3171 "working devices for recovery.\n",
3172 mdname(mddev));
3173 mirror->recovery_disabled
3174 = mddev->recovery_disabled;
3176 put_buf(r10_bio);
3177 if (rb2)
3178 atomic_dec(&rb2->remaining);
3179 r10_bio = rb2;
3180 break;
3183 if (biolist == NULL) {
3184 while (r10_bio) {
3185 struct r10bio *rb2 = r10_bio;
3186 r10_bio = (struct r10bio*) rb2->master_bio;
3187 rb2->master_bio = NULL;
3188 put_buf(rb2);
3190 goto giveup;
3192 } else {
3193 /* resync. Schedule a read for every block at this virt offset */
3194 int count = 0;
3196 bitmap_cond_end_sync(mddev->bitmap, sector_nr, 0);
3198 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
3199 &sync_blocks, mddev->degraded) &&
3200 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3201 &mddev->recovery)) {
3202 /* We can skip this block */
3203 *skipped = 1;
3204 return sync_blocks + sectors_skipped;
3206 if (sync_blocks < max_sync)
3207 max_sync = sync_blocks;
3208 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
3209 r10_bio->state = 0;
3211 r10_bio->mddev = mddev;
3212 atomic_set(&r10_bio->remaining, 0);
3213 raise_barrier(conf, 0);
3214 conf->next_resync = sector_nr;
3216 r10_bio->master_bio = NULL;
3217 r10_bio->sector = sector_nr;
3218 set_bit(R10BIO_IsSync, &r10_bio->state);
3219 raid10_find_phys(conf, r10_bio);
3220 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
3222 for (i = 0; i < conf->copies; i++) {
3223 int d = r10_bio->devs[i].devnum;
3224 sector_t first_bad, sector;
3225 int bad_sectors;
3227 if (r10_bio->devs[i].repl_bio)
3228 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3230 bio = r10_bio->devs[i].bio;
3231 bio_reset(bio);
3232 bio->bi_error = -EIO;
3233 if (conf->mirrors[d].rdev == NULL ||
3234 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
3235 continue;
3236 sector = r10_bio->devs[i].addr;
3237 if (is_badblock(conf->mirrors[d].rdev,
3238 sector, max_sync,
3239 &first_bad, &bad_sectors)) {
3240 if (first_bad > sector)
3241 max_sync = first_bad - sector;
3242 else {
3243 bad_sectors -= (sector - first_bad);
3244 if (max_sync > bad_sectors)
3245 max_sync = bad_sectors;
3246 continue;
3249 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
3250 atomic_inc(&r10_bio->remaining);
3251 bio->bi_next = biolist;
3252 biolist = bio;
3253 bio->bi_private = r10_bio;
3254 bio->bi_end_io = end_sync_read;
3255 bio->bi_rw = READ;
3256 bio->bi_iter.bi_sector = sector +
3257 conf->mirrors[d].rdev->data_offset;
3258 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
3259 count++;
3261 if (conf->mirrors[d].replacement == NULL ||
3262 test_bit(Faulty,
3263 &conf->mirrors[d].replacement->flags))
3264 continue;
3266 /* Need to set up for writing to the replacement */
3267 bio = r10_bio->devs[i].repl_bio;
3268 bio_reset(bio);
3269 bio->bi_error = -EIO;
3271 sector = r10_bio->devs[i].addr;
3272 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
3273 bio->bi_next = biolist;
3274 biolist = bio;
3275 bio->bi_private = r10_bio;
3276 bio->bi_end_io = end_sync_write;
3277 bio->bi_rw = WRITE;
3278 bio->bi_iter.bi_sector = sector +
3279 conf->mirrors[d].replacement->data_offset;
3280 bio->bi_bdev = conf->mirrors[d].replacement->bdev;
3281 count++;
3284 if (count < 2) {
3285 for (i=0; i<conf->copies; i++) {
3286 int d = r10_bio->devs[i].devnum;
3287 if (r10_bio->devs[i].bio->bi_end_io)
3288 rdev_dec_pending(conf->mirrors[d].rdev,
3289 mddev);
3290 if (r10_bio->devs[i].repl_bio &&
3291 r10_bio->devs[i].repl_bio->bi_end_io)
3292 rdev_dec_pending(
3293 conf->mirrors[d].replacement,
3294 mddev);
3296 put_buf(r10_bio);
3297 biolist = NULL;
3298 goto giveup;
3302 nr_sectors = 0;
3303 if (sector_nr + max_sync < max_sector)
3304 max_sector = sector_nr + max_sync;
3305 do {
3306 struct page *page;
3307 int len = PAGE_SIZE;
3308 if (sector_nr + (len>>9) > max_sector)
3309 len = (max_sector - sector_nr) << 9;
3310 if (len == 0)
3311 break;
3312 for (bio= biolist ; bio ; bio=bio->bi_next) {
3313 struct bio *bio2;
3314 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
3315 if (bio_add_page(bio, page, len, 0))
3316 continue;
3318 /* stop here */
3319 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
3320 for (bio2 = biolist;
3321 bio2 && bio2 != bio;
3322 bio2 = bio2->bi_next) {
3323 /* remove last page from this bio */
3324 bio2->bi_vcnt--;
3325 bio2->bi_iter.bi_size -= len;
3326 bio_clear_flag(bio2, BIO_SEG_VALID);
3328 goto bio_full;
3330 nr_sectors += len>>9;
3331 sector_nr += len>>9;
3332 } while (biolist->bi_vcnt < RESYNC_PAGES);
3333 bio_full:
3334 r10_bio->sectors = nr_sectors;
3336 while (biolist) {
3337 bio = biolist;
3338 biolist = biolist->bi_next;
3340 bio->bi_next = NULL;
3341 r10_bio = bio->bi_private;
3342 r10_bio->sectors = nr_sectors;
3344 if (bio->bi_end_io == end_sync_read) {
3345 md_sync_acct(bio->bi_bdev, nr_sectors);
3346 bio->bi_error = 0;
3347 generic_make_request(bio);
3351 if (sectors_skipped)
3352 /* pretend they weren't skipped, it makes
3353 * no important difference in this case
3355 md_done_sync(mddev, sectors_skipped, 1);
3357 return sectors_skipped + nr_sectors;
3358 giveup:
3359 /* There is nowhere to write, so all non-sync
3360 * drives must be failed or in resync, all drives
3361 * have a bad block, so try the next chunk...
3363 if (sector_nr + max_sync < max_sector)
3364 max_sector = sector_nr + max_sync;
3366 sectors_skipped += (max_sector - sector_nr);
3367 chunks_skipped ++;
3368 sector_nr = max_sector;
3369 goto skipped;
3372 static sector_t
3373 raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
3375 sector_t size;
3376 struct r10conf *conf = mddev->private;
3378 if (!raid_disks)
3379 raid_disks = min(conf->geo.raid_disks,
3380 conf->prev.raid_disks);
3381 if (!sectors)
3382 sectors = conf->dev_sectors;
3384 size = sectors >> conf->geo.chunk_shift;
3385 sector_div(size, conf->geo.far_copies);
3386 size = size * raid_disks;
3387 sector_div(size, conf->geo.near_copies);
3389 return size << conf->geo.chunk_shift;
3392 static void calc_sectors(struct r10conf *conf, sector_t size)
3394 /* Calculate the number of sectors-per-device that will
3395 * actually be used, and set conf->dev_sectors and
3396 * conf->stride
3399 size = size >> conf->geo.chunk_shift;
3400 sector_div(size, conf->geo.far_copies);
3401 size = size * conf->geo.raid_disks;
3402 sector_div(size, conf->geo.near_copies);
3403 /* 'size' is now the number of chunks in the array */
3404 /* calculate "used chunks per device" */
3405 size = size * conf->copies;
3407 /* We need to round up when dividing by raid_disks to
3408 * get the stride size.
3410 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
3412 conf->dev_sectors = size << conf->geo.chunk_shift;
3414 if (conf->geo.far_offset)
3415 conf->geo.stride = 1 << conf->geo.chunk_shift;
3416 else {
3417 sector_div(size, conf->geo.far_copies);
3418 conf->geo.stride = size << conf->geo.chunk_shift;
3422 enum geo_type {geo_new, geo_old, geo_start};
3423 static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3425 int nc, fc, fo;
3426 int layout, chunk, disks;
3427 switch (new) {
3428 case geo_old:
3429 layout = mddev->layout;
3430 chunk = mddev->chunk_sectors;
3431 disks = mddev->raid_disks - mddev->delta_disks;
3432 break;
3433 case geo_new:
3434 layout = mddev->new_layout;
3435 chunk = mddev->new_chunk_sectors;
3436 disks = mddev->raid_disks;
3437 break;
3438 default: /* avoid 'may be unused' warnings */
3439 case geo_start: /* new when starting reshape - raid_disks not
3440 * updated yet. */
3441 layout = mddev->new_layout;
3442 chunk = mddev->new_chunk_sectors;
3443 disks = mddev->raid_disks + mddev->delta_disks;
3444 break;
3446 if (layout >> 19)
3447 return -1;
3448 if (chunk < (PAGE_SIZE >> 9) ||
3449 !is_power_of_2(chunk))
3450 return -2;
3451 nc = layout & 255;
3452 fc = (layout >> 8) & 255;
3453 fo = layout & (1<<16);
3454 geo->raid_disks = disks;
3455 geo->near_copies = nc;
3456 geo->far_copies = fc;
3457 geo->far_offset = fo;
3458 switch (layout >> 17) {
3459 case 0: /* original layout. simple but not always optimal */
3460 geo->far_set_size = disks;
3461 break;
3462 case 1: /* "improved" layout which was buggy. Hopefully no-one is
3463 * actually using this, but leave code here just in case.*/
3464 geo->far_set_size = disks/fc;
3465 WARN(geo->far_set_size < fc,
3466 "This RAID10 layout does not provide data safety - please backup and create new array\n");
3467 break;
3468 case 2: /* "improved" layout fixed to match documentation */
3469 geo->far_set_size = fc * nc;
3470 break;
3471 default: /* Not a valid layout */
3472 return -1;
3474 geo->chunk_mask = chunk - 1;
3475 geo->chunk_shift = ffz(~chunk);
3476 return nc*fc;
3479 static struct r10conf *setup_conf(struct mddev *mddev)
3481 struct r10conf *conf = NULL;
3482 int err = -EINVAL;
3483 struct geom geo;
3484 int copies;
3486 copies = setup_geo(&geo, mddev, geo_new);
3488 if (copies == -2) {
3489 printk(KERN_ERR "md/raid10:%s: chunk size must be "
3490 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
3491 mdname(mddev), PAGE_SIZE);
3492 goto out;
3495 if (copies < 2 || copies > mddev->raid_disks) {
3496 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3497 mdname(mddev), mddev->new_layout);
3498 goto out;
3501 err = -ENOMEM;
3502 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
3503 if (!conf)
3504 goto out;
3506 /* FIXME calc properly */
3507 conf->mirrors = kzalloc(sizeof(struct raid10_info)*(mddev->raid_disks +
3508 max(0,-mddev->delta_disks)),
3509 GFP_KERNEL);
3510 if (!conf->mirrors)
3511 goto out;
3513 conf->tmppage = alloc_page(GFP_KERNEL);
3514 if (!conf->tmppage)
3515 goto out;
3517 conf->geo = geo;
3518 conf->copies = copies;
3519 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
3520 r10bio_pool_free, conf);
3521 if (!conf->r10bio_pool)
3522 goto out;
3524 calc_sectors(conf, mddev->dev_sectors);
3525 if (mddev->reshape_position == MaxSector) {
3526 conf->prev = conf->geo;
3527 conf->reshape_progress = MaxSector;
3528 } else {
3529 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
3530 err = -EINVAL;
3531 goto out;
3533 conf->reshape_progress = mddev->reshape_position;
3534 if (conf->prev.far_offset)
3535 conf->prev.stride = 1 << conf->prev.chunk_shift;
3536 else
3537 /* far_copies must be 1 */
3538 conf->prev.stride = conf->dev_sectors;
3540 conf->reshape_safe = conf->reshape_progress;
3541 spin_lock_init(&conf->device_lock);
3542 INIT_LIST_HEAD(&conf->retry_list);
3543 INIT_LIST_HEAD(&conf->bio_end_io_list);
3545 spin_lock_init(&conf->resync_lock);
3546 init_waitqueue_head(&conf->wait_barrier);
3548 conf->thread = md_register_thread(raid10d, mddev, "raid10");
3549 if (!conf->thread)
3550 goto out;
3552 conf->mddev = mddev;
3553 return conf;
3555 out:
3556 if (err == -ENOMEM)
3557 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
3558 mdname(mddev));
3559 if (conf) {
3560 mempool_destroy(conf->r10bio_pool);
3561 kfree(conf->mirrors);
3562 safe_put_page(conf->tmppage);
3563 kfree(conf);
3565 return ERR_PTR(err);
3568 static int run(struct mddev *mddev)
3570 struct r10conf *conf;
3571 int i, disk_idx, chunk_size;
3572 struct raid10_info *disk;
3573 struct md_rdev *rdev;
3574 sector_t size;
3575 sector_t min_offset_diff = 0;
3576 int first = 1;
3577 bool discard_supported = false;
3579 if (mddev->private == NULL) {
3580 conf = setup_conf(mddev);
3581 if (IS_ERR(conf))
3582 return PTR_ERR(conf);
3583 mddev->private = conf;
3585 conf = mddev->private;
3586 if (!conf)
3587 goto out;
3589 mddev->thread = conf->thread;
3590 conf->thread = NULL;
3592 chunk_size = mddev->chunk_sectors << 9;
3593 if (mddev->queue) {
3594 blk_queue_max_discard_sectors(mddev->queue,
3595 mddev->chunk_sectors);
3596 blk_queue_max_write_same_sectors(mddev->queue, 0);
3597 blk_queue_io_min(mddev->queue, chunk_size);
3598 if (conf->geo.raid_disks % conf->geo.near_copies)
3599 blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
3600 else
3601 blk_queue_io_opt(mddev->queue, chunk_size *
3602 (conf->geo.raid_disks / conf->geo.near_copies));
3605 rdev_for_each(rdev, mddev) {
3606 long long diff;
3607 struct request_queue *q;
3609 disk_idx = rdev->raid_disk;
3610 if (disk_idx < 0)
3611 continue;
3612 if (disk_idx >= conf->geo.raid_disks &&
3613 disk_idx >= conf->prev.raid_disks)
3614 continue;
3615 disk = conf->mirrors + disk_idx;
3617 if (test_bit(Replacement, &rdev->flags)) {
3618 if (disk->replacement)
3619 goto out_free_conf;
3620 disk->replacement = rdev;
3621 } else {
3622 if (disk->rdev)
3623 goto out_free_conf;
3624 disk->rdev = rdev;
3626 q = bdev_get_queue(rdev->bdev);
3627 diff = (rdev->new_data_offset - rdev->data_offset);
3628 if (!mddev->reshape_backwards)
3629 diff = -diff;
3630 if (diff < 0)
3631 diff = 0;
3632 if (first || diff < min_offset_diff)
3633 min_offset_diff = diff;
3635 if (mddev->gendisk)
3636 disk_stack_limits(mddev->gendisk, rdev->bdev,
3637 rdev->data_offset << 9);
3639 disk->head_position = 0;
3641 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3642 discard_supported = true;
3643 first = 0;
3646 if (mddev->queue) {
3647 if (discard_supported)
3648 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
3649 mddev->queue);
3650 else
3651 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
3652 mddev->queue);
3654 /* need to check that every block has at least one working mirror */
3655 if (!enough(conf, -1)) {
3656 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
3657 mdname(mddev));
3658 goto out_free_conf;
3661 if (conf->reshape_progress != MaxSector) {
3662 /* must ensure that shape change is supported */
3663 if (conf->geo.far_copies != 1 &&
3664 conf->geo.far_offset == 0)
3665 goto out_free_conf;
3666 if (conf->prev.far_copies != 1 &&
3667 conf->prev.far_offset == 0)
3668 goto out_free_conf;
3671 mddev->degraded = 0;
3672 for (i = 0;
3673 i < conf->geo.raid_disks
3674 || i < conf->prev.raid_disks;
3675 i++) {
3677 disk = conf->mirrors + i;
3679 if (!disk->rdev && disk->replacement) {
3680 /* The replacement is all we have - use it */
3681 disk->rdev = disk->replacement;
3682 disk->replacement = NULL;
3683 clear_bit(Replacement, &disk->rdev->flags);
3686 if (!disk->rdev ||
3687 !test_bit(In_sync, &disk->rdev->flags)) {
3688 disk->head_position = 0;
3689 mddev->degraded++;
3690 if (disk->rdev &&
3691 disk->rdev->saved_raid_disk < 0)
3692 conf->fullsync = 1;
3695 if (disk->replacement &&
3696 !test_bit(In_sync, &disk->replacement->flags) &&
3697 disk->replacement->saved_raid_disk < 0) {
3698 conf->fullsync = 1;
3701 disk->recovery_disabled = mddev->recovery_disabled - 1;
3704 if (mddev->recovery_cp != MaxSector)
3705 printk(KERN_NOTICE "md/raid10:%s: not clean"
3706 " -- starting background reconstruction\n",
3707 mdname(mddev));
3708 printk(KERN_INFO
3709 "md/raid10:%s: active with %d out of %d devices\n",
3710 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
3711 conf->geo.raid_disks);
3713 * Ok, everything is just fine now
3715 mddev->dev_sectors = conf->dev_sectors;
3716 size = raid10_size(mddev, 0, 0);
3717 md_set_array_sectors(mddev, size);
3718 mddev->resync_max_sectors = size;
3720 if (mddev->queue) {
3721 int stripe = conf->geo.raid_disks *
3722 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
3724 /* Calculate max read-ahead size.
3725 * We need to readahead at least twice a whole stripe....
3726 * maybe...
3728 stripe /= conf->geo.near_copies;
3729 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3730 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
3733 if (md_integrity_register(mddev))
3734 goto out_free_conf;
3736 if (conf->reshape_progress != MaxSector) {
3737 unsigned long before_length, after_length;
3739 before_length = ((1 << conf->prev.chunk_shift) *
3740 conf->prev.far_copies);
3741 after_length = ((1 << conf->geo.chunk_shift) *
3742 conf->geo.far_copies);
3744 if (max(before_length, after_length) > min_offset_diff) {
3745 /* This cannot work */
3746 printk("md/raid10: offset difference not enough to continue reshape\n");
3747 goto out_free_conf;
3749 conf->offset_diff = min_offset_diff;
3751 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3752 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3753 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3754 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3755 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3756 "reshape");
3759 return 0;
3761 out_free_conf:
3762 md_unregister_thread(&mddev->thread);
3763 mempool_destroy(conf->r10bio_pool);
3764 safe_put_page(conf->tmppage);
3765 kfree(conf->mirrors);
3766 kfree(conf);
3767 mddev->private = NULL;
3768 out:
3769 return -EIO;
3772 static void raid10_free(struct mddev *mddev, void *priv)
3774 struct r10conf *conf = priv;
3776 mempool_destroy(conf->r10bio_pool);
3777 safe_put_page(conf->tmppage);
3778 kfree(conf->mirrors);
3779 kfree(conf->mirrors_old);
3780 kfree(conf->mirrors_new);
3781 kfree(conf);
3784 static void raid10_quiesce(struct mddev *mddev, int state)
3786 struct r10conf *conf = mddev->private;
3788 switch(state) {
3789 case 1:
3790 raise_barrier(conf, 0);
3791 break;
3792 case 0:
3793 lower_barrier(conf);
3794 break;
3798 static int raid10_resize(struct mddev *mddev, sector_t sectors)
3800 /* Resize of 'far' arrays is not supported.
3801 * For 'near' and 'offset' arrays we can set the
3802 * number of sectors used to be an appropriate multiple
3803 * of the chunk size.
3804 * For 'offset', this is far_copies*chunksize.
3805 * For 'near' the multiplier is the LCM of
3806 * near_copies and raid_disks.
3807 * So if far_copies > 1 && !far_offset, fail.
3808 * Else find LCM(raid_disks, near_copy)*far_copies and
3809 * multiply by chunk_size. Then round to this number.
3810 * This is mostly done by raid10_size()
3812 struct r10conf *conf = mddev->private;
3813 sector_t oldsize, size;
3815 if (mddev->reshape_position != MaxSector)
3816 return -EBUSY;
3818 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
3819 return -EINVAL;
3821 oldsize = raid10_size(mddev, 0, 0);
3822 size = raid10_size(mddev, sectors, 0);
3823 if (mddev->external_size &&
3824 mddev->array_sectors > size)
3825 return -EINVAL;
3826 if (mddev->bitmap) {
3827 int ret = bitmap_resize(mddev->bitmap, size, 0, 0);
3828 if (ret)
3829 return ret;
3831 md_set_array_sectors(mddev, size);
3832 set_capacity(mddev->gendisk, mddev->array_sectors);
3833 revalidate_disk(mddev->gendisk);
3834 if (sectors > mddev->dev_sectors &&
3835 mddev->recovery_cp > oldsize) {
3836 mddev->recovery_cp = oldsize;
3837 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3839 calc_sectors(conf, sectors);
3840 mddev->dev_sectors = conf->dev_sectors;
3841 mddev->resync_max_sectors = size;
3842 return 0;
3845 static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs)
3847 struct md_rdev *rdev;
3848 struct r10conf *conf;
3850 if (mddev->degraded > 0) {
3851 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
3852 mdname(mddev));
3853 return ERR_PTR(-EINVAL);
3855 sector_div(size, devs);
3857 /* Set new parameters */
3858 mddev->new_level = 10;
3859 /* new layout: far_copies = 1, near_copies = 2 */
3860 mddev->new_layout = (1<<8) + 2;
3861 mddev->new_chunk_sectors = mddev->chunk_sectors;
3862 mddev->delta_disks = mddev->raid_disks;
3863 mddev->raid_disks *= 2;
3864 /* make sure it will be not marked as dirty */
3865 mddev->recovery_cp = MaxSector;
3866 mddev->dev_sectors = size;
3868 conf = setup_conf(mddev);
3869 if (!IS_ERR(conf)) {
3870 rdev_for_each(rdev, mddev)
3871 if (rdev->raid_disk >= 0) {
3872 rdev->new_raid_disk = rdev->raid_disk * 2;
3873 rdev->sectors = size;
3875 conf->barrier = 1;
3878 return conf;
3881 static void *raid10_takeover(struct mddev *mddev)
3883 struct r0conf *raid0_conf;
3885 /* raid10 can take over:
3886 * raid0 - providing it has only two drives
3888 if (mddev->level == 0) {
3889 /* for raid0 takeover only one zone is supported */
3890 raid0_conf = mddev->private;
3891 if (raid0_conf->nr_strip_zones > 1) {
3892 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
3893 " with more than one zone.\n",
3894 mdname(mddev));
3895 return ERR_PTR(-EINVAL);
3897 return raid10_takeover_raid0(mddev,
3898 raid0_conf->strip_zone->zone_end,
3899 raid0_conf->strip_zone->nb_dev);
3901 return ERR_PTR(-EINVAL);
3904 static int raid10_check_reshape(struct mddev *mddev)
3906 /* Called when there is a request to change
3907 * - layout (to ->new_layout)
3908 * - chunk size (to ->new_chunk_sectors)
3909 * - raid_disks (by delta_disks)
3910 * or when trying to restart a reshape that was ongoing.
3912 * We need to validate the request and possibly allocate
3913 * space if that might be an issue later.
3915 * Currently we reject any reshape of a 'far' mode array,
3916 * allow chunk size to change if new is generally acceptable,
3917 * allow raid_disks to increase, and allow
3918 * a switch between 'near' mode and 'offset' mode.
3920 struct r10conf *conf = mddev->private;
3921 struct geom geo;
3923 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
3924 return -EINVAL;
3926 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
3927 /* mustn't change number of copies */
3928 return -EINVAL;
3929 if (geo.far_copies > 1 && !geo.far_offset)
3930 /* Cannot switch to 'far' mode */
3931 return -EINVAL;
3933 if (mddev->array_sectors & geo.chunk_mask)
3934 /* not factor of array size */
3935 return -EINVAL;
3937 if (!enough(conf, -1))
3938 return -EINVAL;
3940 kfree(conf->mirrors_new);
3941 conf->mirrors_new = NULL;
3942 if (mddev->delta_disks > 0) {
3943 /* allocate new 'mirrors' list */
3944 conf->mirrors_new = kzalloc(
3945 sizeof(struct raid10_info)
3946 *(mddev->raid_disks +
3947 mddev->delta_disks),
3948 GFP_KERNEL);
3949 if (!conf->mirrors_new)
3950 return -ENOMEM;
3952 return 0;
3956 * Need to check if array has failed when deciding whether to:
3957 * - start an array
3958 * - remove non-faulty devices
3959 * - add a spare
3960 * - allow a reshape
3961 * This determination is simple when no reshape is happening.
3962 * However if there is a reshape, we need to carefully check
3963 * both the before and after sections.
3964 * This is because some failed devices may only affect one
3965 * of the two sections, and some non-in_sync devices may
3966 * be insync in the section most affected by failed devices.
3968 static int calc_degraded(struct r10conf *conf)
3970 int degraded, degraded2;
3971 int i;
3973 rcu_read_lock();
3974 degraded = 0;
3975 /* 'prev' section first */
3976 for (i = 0; i < conf->prev.raid_disks; i++) {
3977 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
3978 if (!rdev || test_bit(Faulty, &rdev->flags))
3979 degraded++;
3980 else if (!test_bit(In_sync, &rdev->flags))
3981 /* When we can reduce the number of devices in
3982 * an array, this might not contribute to
3983 * 'degraded'. It does now.
3985 degraded++;
3987 rcu_read_unlock();
3988 if (conf->geo.raid_disks == conf->prev.raid_disks)
3989 return degraded;
3990 rcu_read_lock();
3991 degraded2 = 0;
3992 for (i = 0; i < conf->geo.raid_disks; i++) {
3993 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
3994 if (!rdev || test_bit(Faulty, &rdev->flags))
3995 degraded2++;
3996 else if (!test_bit(In_sync, &rdev->flags)) {
3997 /* If reshape is increasing the number of devices,
3998 * this section has already been recovered, so
3999 * it doesn't contribute to degraded.
4000 * else it does.
4002 if (conf->geo.raid_disks <= conf->prev.raid_disks)
4003 degraded2++;
4006 rcu_read_unlock();
4007 if (degraded2 > degraded)
4008 return degraded2;
4009 return degraded;
4012 static int raid10_start_reshape(struct mddev *mddev)
4014 /* A 'reshape' has been requested. This commits
4015 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4016 * This also checks if there are enough spares and adds them
4017 * to the array.
4018 * We currently require enough spares to make the final
4019 * array non-degraded. We also require that the difference
4020 * between old and new data_offset - on each device - is
4021 * enough that we never risk over-writing.
4024 unsigned long before_length, after_length;
4025 sector_t min_offset_diff = 0;
4026 int first = 1;
4027 struct geom new;
4028 struct r10conf *conf = mddev->private;
4029 struct md_rdev *rdev;
4030 int spares = 0;
4031 int ret;
4033 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4034 return -EBUSY;
4036 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4037 return -EINVAL;
4039 before_length = ((1 << conf->prev.chunk_shift) *
4040 conf->prev.far_copies);
4041 after_length = ((1 << conf->geo.chunk_shift) *
4042 conf->geo.far_copies);
4044 rdev_for_each(rdev, mddev) {
4045 if (!test_bit(In_sync, &rdev->flags)
4046 && !test_bit(Faulty, &rdev->flags))
4047 spares++;
4048 if (rdev->raid_disk >= 0) {
4049 long long diff = (rdev->new_data_offset
4050 - rdev->data_offset);
4051 if (!mddev->reshape_backwards)
4052 diff = -diff;
4053 if (diff < 0)
4054 diff = 0;
4055 if (first || diff < min_offset_diff)
4056 min_offset_diff = diff;
4057 first = 0;
4061 if (max(before_length, after_length) > min_offset_diff)
4062 return -EINVAL;
4064 if (spares < mddev->delta_disks)
4065 return -EINVAL;
4067 conf->offset_diff = min_offset_diff;
4068 spin_lock_irq(&conf->device_lock);
4069 if (conf->mirrors_new) {
4070 memcpy(conf->mirrors_new, conf->mirrors,
4071 sizeof(struct raid10_info)*conf->prev.raid_disks);
4072 smp_mb();
4073 kfree(conf->mirrors_old);
4074 conf->mirrors_old = conf->mirrors;
4075 conf->mirrors = conf->mirrors_new;
4076 conf->mirrors_new = NULL;
4078 setup_geo(&conf->geo, mddev, geo_start);
4079 smp_mb();
4080 if (mddev->reshape_backwards) {
4081 sector_t size = raid10_size(mddev, 0, 0);
4082 if (size < mddev->array_sectors) {
4083 spin_unlock_irq(&conf->device_lock);
4084 printk(KERN_ERR "md/raid10:%s: array size must be reduce before number of disks\n",
4085 mdname(mddev));
4086 return -EINVAL;
4088 mddev->resync_max_sectors = size;
4089 conf->reshape_progress = size;
4090 } else
4091 conf->reshape_progress = 0;
4092 conf->reshape_safe = conf->reshape_progress;
4093 spin_unlock_irq(&conf->device_lock);
4095 if (mddev->delta_disks && mddev->bitmap) {
4096 ret = bitmap_resize(mddev->bitmap,
4097 raid10_size(mddev, 0,
4098 conf->geo.raid_disks),
4099 0, 0);
4100 if (ret)
4101 goto abort;
4103 if (mddev->delta_disks > 0) {
4104 rdev_for_each(rdev, mddev)
4105 if (rdev->raid_disk < 0 &&
4106 !test_bit(Faulty, &rdev->flags)) {
4107 if (raid10_add_disk(mddev, rdev) == 0) {
4108 if (rdev->raid_disk >=
4109 conf->prev.raid_disks)
4110 set_bit(In_sync, &rdev->flags);
4111 else
4112 rdev->recovery_offset = 0;
4114 if (sysfs_link_rdev(mddev, rdev))
4115 /* Failure here is OK */;
4117 } else if (rdev->raid_disk >= conf->prev.raid_disks
4118 && !test_bit(Faulty, &rdev->flags)) {
4119 /* This is a spare that was manually added */
4120 set_bit(In_sync, &rdev->flags);
4123 /* When a reshape changes the number of devices,
4124 * ->degraded is measured against the larger of the
4125 * pre and post numbers.
4127 spin_lock_irq(&conf->device_lock);
4128 mddev->degraded = calc_degraded(conf);
4129 spin_unlock_irq(&conf->device_lock);
4130 mddev->raid_disks = conf->geo.raid_disks;
4131 mddev->reshape_position = conf->reshape_progress;
4132 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4134 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4135 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4136 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
4137 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4138 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4140 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4141 "reshape");
4142 if (!mddev->sync_thread) {
4143 ret = -EAGAIN;
4144 goto abort;
4146 conf->reshape_checkpoint = jiffies;
4147 md_wakeup_thread(mddev->sync_thread);
4148 md_new_event(mddev);
4149 return 0;
4151 abort:
4152 mddev->recovery = 0;
4153 spin_lock_irq(&conf->device_lock);
4154 conf->geo = conf->prev;
4155 mddev->raid_disks = conf->geo.raid_disks;
4156 rdev_for_each(rdev, mddev)
4157 rdev->new_data_offset = rdev->data_offset;
4158 smp_wmb();
4159 conf->reshape_progress = MaxSector;
4160 conf->reshape_safe = MaxSector;
4161 mddev->reshape_position = MaxSector;
4162 spin_unlock_irq(&conf->device_lock);
4163 return ret;
4166 /* Calculate the last device-address that could contain
4167 * any block from the chunk that includes the array-address 's'
4168 * and report the next address.
4169 * i.e. the address returned will be chunk-aligned and after
4170 * any data that is in the chunk containing 's'.
4172 static sector_t last_dev_address(sector_t s, struct geom *geo)
4174 s = (s | geo->chunk_mask) + 1;
4175 s >>= geo->chunk_shift;
4176 s *= geo->near_copies;
4177 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4178 s *= geo->far_copies;
4179 s <<= geo->chunk_shift;
4180 return s;
4183 /* Calculate the first device-address that could contain
4184 * any block from the chunk that includes the array-address 's'.
4185 * This too will be the start of a chunk
4187 static sector_t first_dev_address(sector_t s, struct geom *geo)
4189 s >>= geo->chunk_shift;
4190 s *= geo->near_copies;
4191 sector_div(s, geo->raid_disks);
4192 s *= geo->far_copies;
4193 s <<= geo->chunk_shift;
4194 return s;
4197 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4198 int *skipped)
4200 /* We simply copy at most one chunk (smallest of old and new)
4201 * at a time, possibly less if that exceeds RESYNC_PAGES,
4202 * or we hit a bad block or something.
4203 * This might mean we pause for normal IO in the middle of
4204 * a chunk, but that is not a problem as mddev->reshape_position
4205 * can record any location.
4207 * If we will want to write to a location that isn't
4208 * yet recorded as 'safe' (i.e. in metadata on disk) then
4209 * we need to flush all reshape requests and update the metadata.
4211 * When reshaping forwards (e.g. to more devices), we interpret
4212 * 'safe' as the earliest block which might not have been copied
4213 * down yet. We divide this by previous stripe size and multiply
4214 * by previous stripe length to get lowest device offset that we
4215 * cannot write to yet.
4216 * We interpret 'sector_nr' as an address that we want to write to.
4217 * From this we use last_device_address() to find where we might
4218 * write to, and first_device_address on the 'safe' position.
4219 * If this 'next' write position is after the 'safe' position,
4220 * we must update the metadata to increase the 'safe' position.
4222 * When reshaping backwards, we round in the opposite direction
4223 * and perform the reverse test: next write position must not be
4224 * less than current safe position.
4226 * In all this the minimum difference in data offsets
4227 * (conf->offset_diff - always positive) allows a bit of slack,
4228 * so next can be after 'safe', but not by more than offset_diff
4230 * We need to prepare all the bios here before we start any IO
4231 * to ensure the size we choose is acceptable to all devices.
4232 * The means one for each copy for write-out and an extra one for
4233 * read-in.
4234 * We store the read-in bio in ->master_bio and the others in
4235 * ->devs[x].bio and ->devs[x].repl_bio.
4237 struct r10conf *conf = mddev->private;
4238 struct r10bio *r10_bio;
4239 sector_t next, safe, last;
4240 int max_sectors;
4241 int nr_sectors;
4242 int s;
4243 struct md_rdev *rdev;
4244 int need_flush = 0;
4245 struct bio *blist;
4246 struct bio *bio, *read_bio;
4247 int sectors_done = 0;
4249 if (sector_nr == 0) {
4250 /* If restarting in the middle, skip the initial sectors */
4251 if (mddev->reshape_backwards &&
4252 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4253 sector_nr = (raid10_size(mddev, 0, 0)
4254 - conf->reshape_progress);
4255 } else if (!mddev->reshape_backwards &&
4256 conf->reshape_progress > 0)
4257 sector_nr = conf->reshape_progress;
4258 if (sector_nr) {
4259 mddev->curr_resync_completed = sector_nr;
4260 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4261 *skipped = 1;
4262 return sector_nr;
4266 /* We don't use sector_nr to track where we are up to
4267 * as that doesn't work well for ->reshape_backwards.
4268 * So just use ->reshape_progress.
4270 if (mddev->reshape_backwards) {
4271 /* 'next' is the earliest device address that we might
4272 * write to for this chunk in the new layout
4274 next = first_dev_address(conf->reshape_progress - 1,
4275 &conf->geo);
4277 /* 'safe' is the last device address that we might read from
4278 * in the old layout after a restart
4280 safe = last_dev_address(conf->reshape_safe - 1,
4281 &conf->prev);
4283 if (next + conf->offset_diff < safe)
4284 need_flush = 1;
4286 last = conf->reshape_progress - 1;
4287 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4288 & conf->prev.chunk_mask);
4289 if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
4290 sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
4291 } else {
4292 /* 'next' is after the last device address that we
4293 * might write to for this chunk in the new layout
4295 next = last_dev_address(conf->reshape_progress, &conf->geo);
4297 /* 'safe' is the earliest device address that we might
4298 * read from in the old layout after a restart
4300 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4302 /* Need to update metadata if 'next' might be beyond 'safe'
4303 * as that would possibly corrupt data
4305 if (next > safe + conf->offset_diff)
4306 need_flush = 1;
4308 sector_nr = conf->reshape_progress;
4309 last = sector_nr | (conf->geo.chunk_mask
4310 & conf->prev.chunk_mask);
4312 if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
4313 last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
4316 if (need_flush ||
4317 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4318 /* Need to update reshape_position in metadata */
4319 wait_barrier(conf);
4320 mddev->reshape_position = conf->reshape_progress;
4321 if (mddev->reshape_backwards)
4322 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4323 - conf->reshape_progress;
4324 else
4325 mddev->curr_resync_completed = conf->reshape_progress;
4326 conf->reshape_checkpoint = jiffies;
4327 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4328 md_wakeup_thread(mddev->thread);
4329 wait_event(mddev->sb_wait, mddev->flags == 0 ||
4330 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4331 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4332 allow_barrier(conf);
4333 return sectors_done;
4335 conf->reshape_safe = mddev->reshape_position;
4336 allow_barrier(conf);
4339 raise_barrier(conf, 0);
4340 read_more:
4341 /* Now schedule reads for blocks from sector_nr to last */
4342 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
4343 r10_bio->state = 0;
4344 raise_barrier(conf, 1);
4345 atomic_set(&r10_bio->remaining, 0);
4346 r10_bio->mddev = mddev;
4347 r10_bio->sector = sector_nr;
4348 set_bit(R10BIO_IsReshape, &r10_bio->state);
4349 r10_bio->sectors = last - sector_nr + 1;
4350 rdev = read_balance(conf, r10_bio, &max_sectors);
4351 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4353 if (!rdev) {
4354 /* Cannot read from here, so need to record bad blocks
4355 * on all the target devices.
4357 // FIXME
4358 mempool_free(r10_bio, conf->r10buf_pool);
4359 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4360 return sectors_done;
4363 read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
4365 read_bio->bi_bdev = rdev->bdev;
4366 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
4367 + rdev->data_offset);
4368 read_bio->bi_private = r10_bio;
4369 read_bio->bi_end_io = end_sync_read;
4370 read_bio->bi_rw = READ;
4371 read_bio->bi_flags &= (~0UL << BIO_RESET_BITS);
4372 read_bio->bi_error = 0;
4373 read_bio->bi_vcnt = 0;
4374 read_bio->bi_iter.bi_size = 0;
4375 r10_bio->master_bio = read_bio;
4376 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4378 /* Now find the locations in the new layout */
4379 __raid10_find_phys(&conf->geo, r10_bio);
4381 blist = read_bio;
4382 read_bio->bi_next = NULL;
4384 for (s = 0; s < conf->copies*2; s++) {
4385 struct bio *b;
4386 int d = r10_bio->devs[s/2].devnum;
4387 struct md_rdev *rdev2;
4388 if (s&1) {
4389 rdev2 = conf->mirrors[d].replacement;
4390 b = r10_bio->devs[s/2].repl_bio;
4391 } else {
4392 rdev2 = conf->mirrors[d].rdev;
4393 b = r10_bio->devs[s/2].bio;
4395 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4396 continue;
4398 bio_reset(b);
4399 b->bi_bdev = rdev2->bdev;
4400 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr +
4401 rdev2->new_data_offset;
4402 b->bi_private = r10_bio;
4403 b->bi_end_io = end_reshape_write;
4404 b->bi_rw = WRITE;
4405 b->bi_next = blist;
4406 blist = b;
4409 /* Now add as many pages as possible to all of these bios. */
4411 nr_sectors = 0;
4412 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
4413 struct page *page = r10_bio->devs[0].bio->bi_io_vec[s/(PAGE_SIZE>>9)].bv_page;
4414 int len = (max_sectors - s) << 9;
4415 if (len > PAGE_SIZE)
4416 len = PAGE_SIZE;
4417 for (bio = blist; bio ; bio = bio->bi_next) {
4418 struct bio *bio2;
4419 if (bio_add_page(bio, page, len, 0))
4420 continue;
4422 /* Didn't fit, must stop */
4423 for (bio2 = blist;
4424 bio2 && bio2 != bio;
4425 bio2 = bio2->bi_next) {
4426 /* Remove last page from this bio */
4427 bio2->bi_vcnt--;
4428 bio2->bi_iter.bi_size -= len;
4429 bio_clear_flag(bio2, BIO_SEG_VALID);
4431 goto bio_full;
4433 sector_nr += len >> 9;
4434 nr_sectors += len >> 9;
4436 bio_full:
4437 r10_bio->sectors = nr_sectors;
4439 /* Now submit the read */
4440 md_sync_acct(read_bio->bi_bdev, r10_bio->sectors);
4441 atomic_inc(&r10_bio->remaining);
4442 read_bio->bi_next = NULL;
4443 generic_make_request(read_bio);
4444 sector_nr += nr_sectors;
4445 sectors_done += nr_sectors;
4446 if (sector_nr <= last)
4447 goto read_more;
4449 lower_barrier(conf);
4451 /* Now that we have done the whole section we can
4452 * update reshape_progress
4454 if (mddev->reshape_backwards)
4455 conf->reshape_progress -= sectors_done;
4456 else
4457 conf->reshape_progress += sectors_done;
4459 return sectors_done;
4462 static void end_reshape_request(struct r10bio *r10_bio);
4463 static int handle_reshape_read_error(struct mddev *mddev,
4464 struct r10bio *r10_bio);
4465 static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
4467 /* Reshape read completed. Hopefully we have a block
4468 * to write out.
4469 * If we got a read error then we do sync 1-page reads from
4470 * elsewhere until we find the data - or give up.
4472 struct r10conf *conf = mddev->private;
4473 int s;
4475 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
4476 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
4477 /* Reshape has been aborted */
4478 md_done_sync(mddev, r10_bio->sectors, 0);
4479 return;
4482 /* We definitely have the data in the pages, schedule the
4483 * writes.
4485 atomic_set(&r10_bio->remaining, 1);
4486 for (s = 0; s < conf->copies*2; s++) {
4487 struct bio *b;
4488 int d = r10_bio->devs[s/2].devnum;
4489 struct md_rdev *rdev;
4490 if (s&1) {
4491 rdev = conf->mirrors[d].replacement;
4492 b = r10_bio->devs[s/2].repl_bio;
4493 } else {
4494 rdev = conf->mirrors[d].rdev;
4495 b = r10_bio->devs[s/2].bio;
4497 if (!rdev || test_bit(Faulty, &rdev->flags))
4498 continue;
4499 atomic_inc(&rdev->nr_pending);
4500 md_sync_acct(b->bi_bdev, r10_bio->sectors);
4501 atomic_inc(&r10_bio->remaining);
4502 b->bi_next = NULL;
4503 generic_make_request(b);
4505 end_reshape_request(r10_bio);
4508 static void end_reshape(struct r10conf *conf)
4510 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
4511 return;
4513 spin_lock_irq(&conf->device_lock);
4514 conf->prev = conf->geo;
4515 md_finish_reshape(conf->mddev);
4516 smp_wmb();
4517 conf->reshape_progress = MaxSector;
4518 conf->reshape_safe = MaxSector;
4519 spin_unlock_irq(&conf->device_lock);
4521 /* read-ahead size must cover two whole stripes, which is
4522 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4524 if (conf->mddev->queue) {
4525 int stripe = conf->geo.raid_disks *
4526 ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
4527 stripe /= conf->geo.near_copies;
4528 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4529 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4531 conf->fullsync = 0;
4534 static int handle_reshape_read_error(struct mddev *mddev,
4535 struct r10bio *r10_bio)
4537 /* Use sync reads to get the blocks from somewhere else */
4538 int sectors = r10_bio->sectors;
4539 struct r10conf *conf = mddev->private;
4540 struct {
4541 struct r10bio r10_bio;
4542 struct r10dev devs[conf->copies];
4543 } on_stack;
4544 struct r10bio *r10b = &on_stack.r10_bio;
4545 int slot = 0;
4546 int idx = 0;
4547 struct bio_vec *bvec = r10_bio->master_bio->bi_io_vec;
4549 r10b->sector = r10_bio->sector;
4550 __raid10_find_phys(&conf->prev, r10b);
4552 while (sectors) {
4553 int s = sectors;
4554 int success = 0;
4555 int first_slot = slot;
4557 if (s > (PAGE_SIZE >> 9))
4558 s = PAGE_SIZE >> 9;
4560 while (!success) {
4561 int d = r10b->devs[slot].devnum;
4562 struct md_rdev *rdev = conf->mirrors[d].rdev;
4563 sector_t addr;
4564 if (rdev == NULL ||
4565 test_bit(Faulty, &rdev->flags) ||
4566 !test_bit(In_sync, &rdev->flags))
4567 goto failed;
4569 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
4570 success = sync_page_io(rdev,
4571 addr,
4572 s << 9,
4573 bvec[idx].bv_page,
4574 READ, false);
4575 if (success)
4576 break;
4577 failed:
4578 slot++;
4579 if (slot >= conf->copies)
4580 slot = 0;
4581 if (slot == first_slot)
4582 break;
4584 if (!success) {
4585 /* couldn't read this block, must give up */
4586 set_bit(MD_RECOVERY_INTR,
4587 &mddev->recovery);
4588 return -EIO;
4590 sectors -= s;
4591 idx++;
4593 return 0;
4596 static void end_reshape_write(struct bio *bio)
4598 struct r10bio *r10_bio = bio->bi_private;
4599 struct mddev *mddev = r10_bio->mddev;
4600 struct r10conf *conf = mddev->private;
4601 int d;
4602 int slot;
4603 int repl;
4604 struct md_rdev *rdev = NULL;
4606 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
4607 if (repl)
4608 rdev = conf->mirrors[d].replacement;
4609 if (!rdev) {
4610 smp_mb();
4611 rdev = conf->mirrors[d].rdev;
4614 if (bio->bi_error) {
4615 /* FIXME should record badblock */
4616 md_error(mddev, rdev);
4619 rdev_dec_pending(rdev, mddev);
4620 end_reshape_request(r10_bio);
4623 static void end_reshape_request(struct r10bio *r10_bio)
4625 if (!atomic_dec_and_test(&r10_bio->remaining))
4626 return;
4627 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
4628 bio_put(r10_bio->master_bio);
4629 put_buf(r10_bio);
4632 static void raid10_finish_reshape(struct mddev *mddev)
4634 struct r10conf *conf = mddev->private;
4636 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4637 return;
4639 if (mddev->delta_disks > 0) {
4640 sector_t size = raid10_size(mddev, 0, 0);
4641 md_set_array_sectors(mddev, size);
4642 if (mddev->recovery_cp > mddev->resync_max_sectors) {
4643 mddev->recovery_cp = mddev->resync_max_sectors;
4644 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4646 mddev->resync_max_sectors = size;
4647 set_capacity(mddev->gendisk, mddev->array_sectors);
4648 revalidate_disk(mddev->gendisk);
4649 } else {
4650 int d;
4651 for (d = conf->geo.raid_disks ;
4652 d < conf->geo.raid_disks - mddev->delta_disks;
4653 d++) {
4654 struct md_rdev *rdev = conf->mirrors[d].rdev;
4655 if (rdev)
4656 clear_bit(In_sync, &rdev->flags);
4657 rdev = conf->mirrors[d].replacement;
4658 if (rdev)
4659 clear_bit(In_sync, &rdev->flags);
4662 mddev->layout = mddev->new_layout;
4663 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
4664 mddev->reshape_position = MaxSector;
4665 mddev->delta_disks = 0;
4666 mddev->reshape_backwards = 0;
4669 static struct md_personality raid10_personality =
4671 .name = "raid10",
4672 .level = 10,
4673 .owner = THIS_MODULE,
4674 .make_request = make_request,
4675 .run = run,
4676 .free = raid10_free,
4677 .status = status,
4678 .error_handler = error,
4679 .hot_add_disk = raid10_add_disk,
4680 .hot_remove_disk= raid10_remove_disk,
4681 .spare_active = raid10_spare_active,
4682 .sync_request = sync_request,
4683 .quiesce = raid10_quiesce,
4684 .size = raid10_size,
4685 .resize = raid10_resize,
4686 .takeover = raid10_takeover,
4687 .check_reshape = raid10_check_reshape,
4688 .start_reshape = raid10_start_reshape,
4689 .finish_reshape = raid10_finish_reshape,
4690 .congested = raid10_congested,
4693 static int __init raid_init(void)
4695 return register_md_personality(&raid10_personality);
4698 static void raid_exit(void)
4700 unregister_md_personality(&raid10_personality);
4703 module_init(raid_init);
4704 module_exit(raid_exit);
4705 MODULE_LICENSE("GPL");
4706 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
4707 MODULE_ALIAS("md-personality-9"); /* RAID10 */
4708 MODULE_ALIAS("md-raid10");
4709 MODULE_ALIAS("md-level-10");
4711 module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);