powerpc: dts: P1010: Add endianness property to flexcan node
[linux-2.6/btrfs-unstable.git] / drivers / md / raid1.c
blobcc9d337a1ed37e84994622f5de76717734cca4a1
1 /*
2 * raid1.c : Multiple Devices driver for Linux
4 * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
6 * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
8 * RAID-1 management functions.
10 * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000
12 * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk>
13 * Various fixes by Neil Brown <neilb@cse.unsw.edu.au>
15 * Changes by Peter T. Breuer <ptb@it.uc3m.es> 31/1/2003 to support
16 * bitmapped intelligence in resync:
18 * - bitmap marked during normal i/o
19 * - bitmap used to skip nondirty blocks during sync
21 * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology:
22 * - persistent bitmap code
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2, or (at your option)
27 * any later version.
29 * You should have received a copy of the GNU General Public License
30 * (for example /usr/src/linux/COPYING); if not, write to the Free
31 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include <linux/slab.h>
35 #include <linux/delay.h>
36 #include <linux/blkdev.h>
37 #include <linux/module.h>
38 #include <linux/seq_file.h>
39 #include <linux/ratelimit.h>
41 #include <trace/events/block.h>
43 #include "md.h"
44 #include "raid1.h"
45 #include "md-bitmap.h"
47 #define UNSUPPORTED_MDDEV_FLAGS \
48 ((1L << MD_HAS_JOURNAL) | \
49 (1L << MD_JOURNAL_CLEAN) | \
50 (1L << MD_HAS_PPL) | \
51 (1L << MD_HAS_MULTIPLE_PPLS))
54 * Number of guaranteed r1bios in case of extreme VM load:
56 #define NR_RAID1_BIOS 256
58 /* when we get a read error on a read-only array, we redirect to another
59 * device without failing the first device, or trying to over-write to
60 * correct the read error. To keep track of bad blocks on a per-bio
61 * level, we store IO_BLOCKED in the appropriate 'bios' pointer
63 #define IO_BLOCKED ((struct bio *)1)
64 /* When we successfully write to a known bad-block, we need to remove the
65 * bad-block marking which must be done from process context. So we record
66 * the success by setting devs[n].bio to IO_MADE_GOOD
68 #define IO_MADE_GOOD ((struct bio *)2)
70 #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
72 /* When there are this many requests queue to be written by
73 * the raid1 thread, we become 'congested' to provide back-pressure
74 * for writeback.
76 static int max_queued_requests = 1024;
78 static void allow_barrier(struct r1conf *conf, sector_t sector_nr);
79 static void lower_barrier(struct r1conf *conf, sector_t sector_nr);
81 #define raid1_log(md, fmt, args...) \
82 do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid1 " fmt, ##args); } while (0)
84 #include "raid1-10.c"
87 * for resync bio, r1bio pointer can be retrieved from the per-bio
88 * 'struct resync_pages'.
90 static inline struct r1bio *get_resync_r1bio(struct bio *bio)
92 return get_resync_pages(bio)->raid_bio;
95 static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data)
97 struct pool_info *pi = data;
98 int size = offsetof(struct r1bio, bios[pi->raid_disks]);
100 /* allocate a r1bio with room for raid_disks entries in the bios array */
101 return kzalloc(size, gfp_flags);
104 static void r1bio_pool_free(void *r1_bio, void *data)
106 kfree(r1_bio);
109 #define RESYNC_DEPTH 32
110 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
111 #define RESYNC_WINDOW (RESYNC_BLOCK_SIZE * RESYNC_DEPTH)
112 #define RESYNC_WINDOW_SECTORS (RESYNC_WINDOW >> 9)
113 #define CLUSTER_RESYNC_WINDOW (16 * RESYNC_WINDOW)
114 #define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9)
116 static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
118 struct pool_info *pi = data;
119 struct r1bio *r1_bio;
120 struct bio *bio;
121 int need_pages;
122 int j;
123 struct resync_pages *rps;
125 r1_bio = r1bio_pool_alloc(gfp_flags, pi);
126 if (!r1_bio)
127 return NULL;
129 rps = kmalloc(sizeof(struct resync_pages) * pi->raid_disks,
130 gfp_flags);
131 if (!rps)
132 goto out_free_r1bio;
135 * Allocate bios : 1 for reading, n-1 for writing
137 for (j = pi->raid_disks ; j-- ; ) {
138 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
139 if (!bio)
140 goto out_free_bio;
141 r1_bio->bios[j] = bio;
144 * Allocate RESYNC_PAGES data pages and attach them to
145 * the first bio.
146 * If this is a user-requested check/repair, allocate
147 * RESYNC_PAGES for each bio.
149 if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery))
150 need_pages = pi->raid_disks;
151 else
152 need_pages = 1;
153 for (j = 0; j < pi->raid_disks; j++) {
154 struct resync_pages *rp = &rps[j];
156 bio = r1_bio->bios[j];
158 if (j < need_pages) {
159 if (resync_alloc_pages(rp, gfp_flags))
160 goto out_free_pages;
161 } else {
162 memcpy(rp, &rps[0], sizeof(*rp));
163 resync_get_all_pages(rp);
166 rp->raid_bio = r1_bio;
167 bio->bi_private = rp;
170 r1_bio->master_bio = NULL;
172 return r1_bio;
174 out_free_pages:
175 while (--j >= 0)
176 resync_free_pages(&rps[j]);
178 out_free_bio:
179 while (++j < pi->raid_disks)
180 bio_put(r1_bio->bios[j]);
181 kfree(rps);
183 out_free_r1bio:
184 r1bio_pool_free(r1_bio, data);
185 return NULL;
188 static void r1buf_pool_free(void *__r1_bio, void *data)
190 struct pool_info *pi = data;
191 int i;
192 struct r1bio *r1bio = __r1_bio;
193 struct resync_pages *rp = NULL;
195 for (i = pi->raid_disks; i--; ) {
196 rp = get_resync_pages(r1bio->bios[i]);
197 resync_free_pages(rp);
198 bio_put(r1bio->bios[i]);
201 /* resync pages array stored in the 1st bio's .bi_private */
202 kfree(rp);
204 r1bio_pool_free(r1bio, data);
207 static void put_all_bios(struct r1conf *conf, struct r1bio *r1_bio)
209 int i;
211 for (i = 0; i < conf->raid_disks * 2; i++) {
212 struct bio **bio = r1_bio->bios + i;
213 if (!BIO_SPECIAL(*bio))
214 bio_put(*bio);
215 *bio = NULL;
219 static void free_r1bio(struct r1bio *r1_bio)
221 struct r1conf *conf = r1_bio->mddev->private;
223 put_all_bios(conf, r1_bio);
224 mempool_free(r1_bio, conf->r1bio_pool);
227 static void put_buf(struct r1bio *r1_bio)
229 struct r1conf *conf = r1_bio->mddev->private;
230 sector_t sect = r1_bio->sector;
231 int i;
233 for (i = 0; i < conf->raid_disks * 2; i++) {
234 struct bio *bio = r1_bio->bios[i];
235 if (bio->bi_end_io)
236 rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev);
239 mempool_free(r1_bio, conf->r1buf_pool);
241 lower_barrier(conf, sect);
244 static void reschedule_retry(struct r1bio *r1_bio)
246 unsigned long flags;
247 struct mddev *mddev = r1_bio->mddev;
248 struct r1conf *conf = mddev->private;
249 int idx;
251 idx = sector_to_idx(r1_bio->sector);
252 spin_lock_irqsave(&conf->device_lock, flags);
253 list_add(&r1_bio->retry_list, &conf->retry_list);
254 atomic_inc(&conf->nr_queued[idx]);
255 spin_unlock_irqrestore(&conf->device_lock, flags);
257 wake_up(&conf->wait_barrier);
258 md_wakeup_thread(mddev->thread);
262 * raid_end_bio_io() is called when we have finished servicing a mirrored
263 * operation and are ready to return a success/failure code to the buffer
264 * cache layer.
266 static void call_bio_endio(struct r1bio *r1_bio)
268 struct bio *bio = r1_bio->master_bio;
269 struct r1conf *conf = r1_bio->mddev->private;
271 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
272 bio->bi_status = BLK_STS_IOERR;
274 bio_endio(bio);
276 * Wake up any possible resync thread that waits for the device
277 * to go idle.
279 allow_barrier(conf, r1_bio->sector);
282 static void raid_end_bio_io(struct r1bio *r1_bio)
284 struct bio *bio = r1_bio->master_bio;
286 /* if nobody has done the final endio yet, do it now */
287 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
288 pr_debug("raid1: sync end %s on sectors %llu-%llu\n",
289 (bio_data_dir(bio) == WRITE) ? "write" : "read",
290 (unsigned long long) bio->bi_iter.bi_sector,
291 (unsigned long long) bio_end_sector(bio) - 1);
293 call_bio_endio(r1_bio);
295 free_r1bio(r1_bio);
299 * Update disk head position estimator based on IRQ completion info.
301 static inline void update_head_pos(int disk, struct r1bio *r1_bio)
303 struct r1conf *conf = r1_bio->mddev->private;
305 conf->mirrors[disk].head_position =
306 r1_bio->sector + (r1_bio->sectors);
310 * Find the disk number which triggered given bio
312 static int find_bio_disk(struct r1bio *r1_bio, struct bio *bio)
314 int mirror;
315 struct r1conf *conf = r1_bio->mddev->private;
316 int raid_disks = conf->raid_disks;
318 for (mirror = 0; mirror < raid_disks * 2; mirror++)
319 if (r1_bio->bios[mirror] == bio)
320 break;
322 BUG_ON(mirror == raid_disks * 2);
323 update_head_pos(mirror, r1_bio);
325 return mirror;
328 static void raid1_end_read_request(struct bio *bio)
330 int uptodate = !bio->bi_status;
331 struct r1bio *r1_bio = bio->bi_private;
332 struct r1conf *conf = r1_bio->mddev->private;
333 struct md_rdev *rdev = conf->mirrors[r1_bio->read_disk].rdev;
336 * this branch is our 'one mirror IO has finished' event handler:
338 update_head_pos(r1_bio->read_disk, r1_bio);
340 if (uptodate)
341 set_bit(R1BIO_Uptodate, &r1_bio->state);
342 else if (test_bit(FailFast, &rdev->flags) &&
343 test_bit(R1BIO_FailFast, &r1_bio->state))
344 /* This was a fail-fast read so we definitely
345 * want to retry */
347 else {
348 /* If all other devices have failed, we want to return
349 * the error upwards rather than fail the last device.
350 * Here we redefine "uptodate" to mean "Don't want to retry"
352 unsigned long flags;
353 spin_lock_irqsave(&conf->device_lock, flags);
354 if (r1_bio->mddev->degraded == conf->raid_disks ||
355 (r1_bio->mddev->degraded == conf->raid_disks-1 &&
356 test_bit(In_sync, &rdev->flags)))
357 uptodate = 1;
358 spin_unlock_irqrestore(&conf->device_lock, flags);
361 if (uptodate) {
362 raid_end_bio_io(r1_bio);
363 rdev_dec_pending(rdev, conf->mddev);
364 } else {
366 * oops, read error:
368 char b[BDEVNAME_SIZE];
369 pr_err_ratelimited("md/raid1:%s: %s: rescheduling sector %llu\n",
370 mdname(conf->mddev),
371 bdevname(rdev->bdev, b),
372 (unsigned long long)r1_bio->sector);
373 set_bit(R1BIO_ReadError, &r1_bio->state);
374 reschedule_retry(r1_bio);
375 /* don't drop the reference on read_disk yet */
379 static void close_write(struct r1bio *r1_bio)
381 /* it really is the end of this request */
382 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
383 bio_free_pages(r1_bio->behind_master_bio);
384 bio_put(r1_bio->behind_master_bio);
385 r1_bio->behind_master_bio = NULL;
387 /* clear the bitmap if all writes complete successfully */
388 bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
389 r1_bio->sectors,
390 !test_bit(R1BIO_Degraded, &r1_bio->state),
391 test_bit(R1BIO_BehindIO, &r1_bio->state));
392 md_write_end(r1_bio->mddev);
395 static void r1_bio_write_done(struct r1bio *r1_bio)
397 if (!atomic_dec_and_test(&r1_bio->remaining))
398 return;
400 if (test_bit(R1BIO_WriteError, &r1_bio->state))
401 reschedule_retry(r1_bio);
402 else {
403 close_write(r1_bio);
404 if (test_bit(R1BIO_MadeGood, &r1_bio->state))
405 reschedule_retry(r1_bio);
406 else
407 raid_end_bio_io(r1_bio);
411 static void raid1_end_write_request(struct bio *bio)
413 struct r1bio *r1_bio = bio->bi_private;
414 int behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
415 struct r1conf *conf = r1_bio->mddev->private;
416 struct bio *to_put = NULL;
417 int mirror = find_bio_disk(r1_bio, bio);
418 struct md_rdev *rdev = conf->mirrors[mirror].rdev;
419 bool discard_error;
421 discard_error = bio->bi_status && bio_op(bio) == REQ_OP_DISCARD;
424 * 'one mirror IO has finished' event handler:
426 if (bio->bi_status && !discard_error) {
427 set_bit(WriteErrorSeen, &rdev->flags);
428 if (!test_and_set_bit(WantReplacement, &rdev->flags))
429 set_bit(MD_RECOVERY_NEEDED, &
430 conf->mddev->recovery);
432 if (test_bit(FailFast, &rdev->flags) &&
433 (bio->bi_opf & MD_FAILFAST) &&
434 /* We never try FailFast to WriteMostly devices */
435 !test_bit(WriteMostly, &rdev->flags)) {
436 md_error(r1_bio->mddev, rdev);
437 if (!test_bit(Faulty, &rdev->flags))
438 /* This is the only remaining device,
439 * We need to retry the write without
440 * FailFast
442 set_bit(R1BIO_WriteError, &r1_bio->state);
443 else {
444 /* Finished with this branch */
445 r1_bio->bios[mirror] = NULL;
446 to_put = bio;
448 } else
449 set_bit(R1BIO_WriteError, &r1_bio->state);
450 } else {
452 * Set R1BIO_Uptodate in our master bio, so that we
453 * will return a good error code for to the higher
454 * levels even if IO on some other mirrored buffer
455 * fails.
457 * The 'master' represents the composite IO operation
458 * to user-side. So if something waits for IO, then it
459 * will wait for the 'master' bio.
461 sector_t first_bad;
462 int bad_sectors;
464 r1_bio->bios[mirror] = NULL;
465 to_put = bio;
467 * Do not set R1BIO_Uptodate if the current device is
468 * rebuilding or Faulty. This is because we cannot use
469 * such device for properly reading the data back (we could
470 * potentially use it, if the current write would have felt
471 * before rdev->recovery_offset, but for simplicity we don't
472 * check this here.
474 if (test_bit(In_sync, &rdev->flags) &&
475 !test_bit(Faulty, &rdev->flags))
476 set_bit(R1BIO_Uptodate, &r1_bio->state);
478 /* Maybe we can clear some bad blocks. */
479 if (is_badblock(rdev, r1_bio->sector, r1_bio->sectors,
480 &first_bad, &bad_sectors) && !discard_error) {
481 r1_bio->bios[mirror] = IO_MADE_GOOD;
482 set_bit(R1BIO_MadeGood, &r1_bio->state);
486 if (behind) {
487 if (test_bit(WriteMostly, &rdev->flags))
488 atomic_dec(&r1_bio->behind_remaining);
491 * In behind mode, we ACK the master bio once the I/O
492 * has safely reached all non-writemostly
493 * disks. Setting the Returned bit ensures that this
494 * gets done only once -- we don't ever want to return
495 * -EIO here, instead we'll wait
497 if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
498 test_bit(R1BIO_Uptodate, &r1_bio->state)) {
499 /* Maybe we can return now */
500 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
501 struct bio *mbio = r1_bio->master_bio;
502 pr_debug("raid1: behind end write sectors"
503 " %llu-%llu\n",
504 (unsigned long long) mbio->bi_iter.bi_sector,
505 (unsigned long long) bio_end_sector(mbio) - 1);
506 call_bio_endio(r1_bio);
510 if (r1_bio->bios[mirror] == NULL)
511 rdev_dec_pending(rdev, conf->mddev);
514 * Let's see if all mirrored write operations have finished
515 * already.
517 r1_bio_write_done(r1_bio);
519 if (to_put)
520 bio_put(to_put);
523 static sector_t align_to_barrier_unit_end(sector_t start_sector,
524 sector_t sectors)
526 sector_t len;
528 WARN_ON(sectors == 0);
530 * len is the number of sectors from start_sector to end of the
531 * barrier unit which start_sector belongs to.
533 len = round_up(start_sector + 1, BARRIER_UNIT_SECTOR_SIZE) -
534 start_sector;
536 if (len > sectors)
537 len = sectors;
539 return len;
543 * This routine returns the disk from which the requested read should
544 * be done. There is a per-array 'next expected sequential IO' sector
545 * number - if this matches on the next IO then we use the last disk.
546 * There is also a per-disk 'last know head position' sector that is
547 * maintained from IRQ contexts, both the normal and the resync IO
548 * completion handlers update this position correctly. If there is no
549 * perfect sequential match then we pick the disk whose head is closest.
551 * If there are 2 mirrors in the same 2 devices, performance degrades
552 * because position is mirror, not device based.
554 * The rdev for the device selected will have nr_pending incremented.
556 static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sectors)
558 const sector_t this_sector = r1_bio->sector;
559 int sectors;
560 int best_good_sectors;
561 int best_disk, best_dist_disk, best_pending_disk;
562 int has_nonrot_disk;
563 int disk;
564 sector_t best_dist;
565 unsigned int min_pending;
566 struct md_rdev *rdev;
567 int choose_first;
568 int choose_next_idle;
570 rcu_read_lock();
572 * Check if we can balance. We can balance on the whole
573 * device if no resync is going on, or below the resync window.
574 * We take the first readable disk when above the resync window.
576 retry:
577 sectors = r1_bio->sectors;
578 best_disk = -1;
579 best_dist_disk = -1;
580 best_dist = MaxSector;
581 best_pending_disk = -1;
582 min_pending = UINT_MAX;
583 best_good_sectors = 0;
584 has_nonrot_disk = 0;
585 choose_next_idle = 0;
586 clear_bit(R1BIO_FailFast, &r1_bio->state);
588 if ((conf->mddev->recovery_cp < this_sector + sectors) ||
589 (mddev_is_clustered(conf->mddev) &&
590 md_cluster_ops->area_resyncing(conf->mddev, READ, this_sector,
591 this_sector + sectors)))
592 choose_first = 1;
593 else
594 choose_first = 0;
596 for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) {
597 sector_t dist;
598 sector_t first_bad;
599 int bad_sectors;
600 unsigned int pending;
601 bool nonrot;
603 rdev = rcu_dereference(conf->mirrors[disk].rdev);
604 if (r1_bio->bios[disk] == IO_BLOCKED
605 || rdev == NULL
606 || test_bit(Faulty, &rdev->flags))
607 continue;
608 if (!test_bit(In_sync, &rdev->flags) &&
609 rdev->recovery_offset < this_sector + sectors)
610 continue;
611 if (test_bit(WriteMostly, &rdev->flags)) {
612 /* Don't balance among write-mostly, just
613 * use the first as a last resort */
614 if (best_dist_disk < 0) {
615 if (is_badblock(rdev, this_sector, sectors,
616 &first_bad, &bad_sectors)) {
617 if (first_bad <= this_sector)
618 /* Cannot use this */
619 continue;
620 best_good_sectors = first_bad - this_sector;
621 } else
622 best_good_sectors = sectors;
623 best_dist_disk = disk;
624 best_pending_disk = disk;
626 continue;
628 /* This is a reasonable device to use. It might
629 * even be best.
631 if (is_badblock(rdev, this_sector, sectors,
632 &first_bad, &bad_sectors)) {
633 if (best_dist < MaxSector)
634 /* already have a better device */
635 continue;
636 if (first_bad <= this_sector) {
637 /* cannot read here. If this is the 'primary'
638 * device, then we must not read beyond
639 * bad_sectors from another device..
641 bad_sectors -= (this_sector - first_bad);
642 if (choose_first && sectors > bad_sectors)
643 sectors = bad_sectors;
644 if (best_good_sectors > sectors)
645 best_good_sectors = sectors;
647 } else {
648 sector_t good_sectors = first_bad - this_sector;
649 if (good_sectors > best_good_sectors) {
650 best_good_sectors = good_sectors;
651 best_disk = disk;
653 if (choose_first)
654 break;
656 continue;
657 } else {
658 if ((sectors > best_good_sectors) && (best_disk >= 0))
659 best_disk = -1;
660 best_good_sectors = sectors;
663 if (best_disk >= 0)
664 /* At least two disks to choose from so failfast is OK */
665 set_bit(R1BIO_FailFast, &r1_bio->state);
667 nonrot = blk_queue_nonrot(bdev_get_queue(rdev->bdev));
668 has_nonrot_disk |= nonrot;
669 pending = atomic_read(&rdev->nr_pending);
670 dist = abs(this_sector - conf->mirrors[disk].head_position);
671 if (choose_first) {
672 best_disk = disk;
673 break;
675 /* Don't change to another disk for sequential reads */
676 if (conf->mirrors[disk].next_seq_sect == this_sector
677 || dist == 0) {
678 int opt_iosize = bdev_io_opt(rdev->bdev) >> 9;
679 struct raid1_info *mirror = &conf->mirrors[disk];
681 best_disk = disk;
683 * If buffered sequential IO size exceeds optimal
684 * iosize, check if there is idle disk. If yes, choose
685 * the idle disk. read_balance could already choose an
686 * idle disk before noticing it's a sequential IO in
687 * this disk. This doesn't matter because this disk
688 * will idle, next time it will be utilized after the
689 * first disk has IO size exceeds optimal iosize. In
690 * this way, iosize of the first disk will be optimal
691 * iosize at least. iosize of the second disk might be
692 * small, but not a big deal since when the second disk
693 * starts IO, the first disk is likely still busy.
695 if (nonrot && opt_iosize > 0 &&
696 mirror->seq_start != MaxSector &&
697 mirror->next_seq_sect > opt_iosize &&
698 mirror->next_seq_sect - opt_iosize >=
699 mirror->seq_start) {
700 choose_next_idle = 1;
701 continue;
703 break;
706 if (choose_next_idle)
707 continue;
709 if (min_pending > pending) {
710 min_pending = pending;
711 best_pending_disk = disk;
714 if (dist < best_dist) {
715 best_dist = dist;
716 best_dist_disk = disk;
721 * If all disks are rotational, choose the closest disk. If any disk is
722 * non-rotational, choose the disk with less pending request even the
723 * disk is rotational, which might/might not be optimal for raids with
724 * mixed ratation/non-rotational disks depending on workload.
726 if (best_disk == -1) {
727 if (has_nonrot_disk || min_pending == 0)
728 best_disk = best_pending_disk;
729 else
730 best_disk = best_dist_disk;
733 if (best_disk >= 0) {
734 rdev = rcu_dereference(conf->mirrors[best_disk].rdev);
735 if (!rdev)
736 goto retry;
737 atomic_inc(&rdev->nr_pending);
738 sectors = best_good_sectors;
740 if (conf->mirrors[best_disk].next_seq_sect != this_sector)
741 conf->mirrors[best_disk].seq_start = this_sector;
743 conf->mirrors[best_disk].next_seq_sect = this_sector + sectors;
745 rcu_read_unlock();
746 *max_sectors = sectors;
748 return best_disk;
751 static int raid1_congested(struct mddev *mddev, int bits)
753 struct r1conf *conf = mddev->private;
754 int i, ret = 0;
756 if ((bits & (1 << WB_async_congested)) &&
757 conf->pending_count >= max_queued_requests)
758 return 1;
760 rcu_read_lock();
761 for (i = 0; i < conf->raid_disks * 2; i++) {
762 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
763 if (rdev && !test_bit(Faulty, &rdev->flags)) {
764 struct request_queue *q = bdev_get_queue(rdev->bdev);
766 BUG_ON(!q);
768 /* Note the '|| 1' - when read_balance prefers
769 * non-congested targets, it can be removed
771 if ((bits & (1 << WB_async_congested)) || 1)
772 ret |= bdi_congested(q->backing_dev_info, bits);
773 else
774 ret &= bdi_congested(q->backing_dev_info, bits);
777 rcu_read_unlock();
778 return ret;
781 static void flush_bio_list(struct r1conf *conf, struct bio *bio)
783 /* flush any pending bitmap writes to disk before proceeding w/ I/O */
784 bitmap_unplug(conf->mddev->bitmap);
785 wake_up(&conf->wait_barrier);
787 while (bio) { /* submit pending writes */
788 struct bio *next = bio->bi_next;
789 struct md_rdev *rdev = (void *)bio->bi_disk;
790 bio->bi_next = NULL;
791 bio_set_dev(bio, rdev->bdev);
792 if (test_bit(Faulty, &rdev->flags)) {
793 bio_io_error(bio);
794 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
795 !blk_queue_discard(bio->bi_disk->queue)))
796 /* Just ignore it */
797 bio_endio(bio);
798 else
799 generic_make_request(bio);
800 bio = next;
804 static void flush_pending_writes(struct r1conf *conf)
806 /* Any writes that have been queued but are awaiting
807 * bitmap updates get flushed here.
809 spin_lock_irq(&conf->device_lock);
811 if (conf->pending_bio_list.head) {
812 struct bio *bio;
813 bio = bio_list_get(&conf->pending_bio_list);
814 conf->pending_count = 0;
815 spin_unlock_irq(&conf->device_lock);
816 flush_bio_list(conf, bio);
817 } else
818 spin_unlock_irq(&conf->device_lock);
821 /* Barriers....
822 * Sometimes we need to suspend IO while we do something else,
823 * either some resync/recovery, or reconfigure the array.
824 * To do this we raise a 'barrier'.
825 * The 'barrier' is a counter that can be raised multiple times
826 * to count how many activities are happening which preclude
827 * normal IO.
828 * We can only raise the barrier if there is no pending IO.
829 * i.e. if nr_pending == 0.
830 * We choose only to raise the barrier if no-one is waiting for the
831 * barrier to go down. This means that as soon as an IO request
832 * is ready, no other operations which require a barrier will start
833 * until the IO request has had a chance.
835 * So: regular IO calls 'wait_barrier'. When that returns there
836 * is no backgroup IO happening, It must arrange to call
837 * allow_barrier when it has finished its IO.
838 * backgroup IO calls must call raise_barrier. Once that returns
839 * there is no normal IO happeing. It must arrange to call
840 * lower_barrier when the particular background IO completes.
842 static void raise_barrier(struct r1conf *conf, sector_t sector_nr)
844 int idx = sector_to_idx(sector_nr);
846 spin_lock_irq(&conf->resync_lock);
848 /* Wait until no block IO is waiting */
849 wait_event_lock_irq(conf->wait_barrier,
850 !atomic_read(&conf->nr_waiting[idx]),
851 conf->resync_lock);
853 /* block any new IO from starting */
854 atomic_inc(&conf->barrier[idx]);
856 * In raise_barrier() we firstly increase conf->barrier[idx] then
857 * check conf->nr_pending[idx]. In _wait_barrier() we firstly
858 * increase conf->nr_pending[idx] then check conf->barrier[idx].
859 * A memory barrier here to make sure conf->nr_pending[idx] won't
860 * be fetched before conf->barrier[idx] is increased. Otherwise
861 * there will be a race between raise_barrier() and _wait_barrier().
863 smp_mb__after_atomic();
865 /* For these conditions we must wait:
866 * A: while the array is in frozen state
867 * B: while conf->nr_pending[idx] is not 0, meaning regular I/O
868 * existing in corresponding I/O barrier bucket.
869 * C: while conf->barrier[idx] >= RESYNC_DEPTH, meaning reaches
870 * max resync count which allowed on current I/O barrier bucket.
872 wait_event_lock_irq(conf->wait_barrier,
873 !conf->array_frozen &&
874 !atomic_read(&conf->nr_pending[idx]) &&
875 atomic_read(&conf->barrier[idx]) < RESYNC_DEPTH,
876 conf->resync_lock);
878 atomic_inc(&conf->nr_sync_pending);
879 spin_unlock_irq(&conf->resync_lock);
882 static void lower_barrier(struct r1conf *conf, sector_t sector_nr)
884 int idx = sector_to_idx(sector_nr);
886 BUG_ON(atomic_read(&conf->barrier[idx]) <= 0);
888 atomic_dec(&conf->barrier[idx]);
889 atomic_dec(&conf->nr_sync_pending);
890 wake_up(&conf->wait_barrier);
893 static void _wait_barrier(struct r1conf *conf, int idx)
896 * We need to increase conf->nr_pending[idx] very early here,
897 * then raise_barrier() can be blocked when it waits for
898 * conf->nr_pending[idx] to be 0. Then we can avoid holding
899 * conf->resync_lock when there is no barrier raised in same
900 * barrier unit bucket. Also if the array is frozen, I/O
901 * should be blocked until array is unfrozen.
903 atomic_inc(&conf->nr_pending[idx]);
905 * In _wait_barrier() we firstly increase conf->nr_pending[idx], then
906 * check conf->barrier[idx]. In raise_barrier() we firstly increase
907 * conf->barrier[idx], then check conf->nr_pending[idx]. A memory
908 * barrier is necessary here to make sure conf->barrier[idx] won't be
909 * fetched before conf->nr_pending[idx] is increased. Otherwise there
910 * will be a race between _wait_barrier() and raise_barrier().
912 smp_mb__after_atomic();
915 * Don't worry about checking two atomic_t variables at same time
916 * here. If during we check conf->barrier[idx], the array is
917 * frozen (conf->array_frozen is 1), and chonf->barrier[idx] is
918 * 0, it is safe to return and make the I/O continue. Because the
919 * array is frozen, all I/O returned here will eventually complete
920 * or be queued, no race will happen. See code comment in
921 * frozen_array().
923 if (!READ_ONCE(conf->array_frozen) &&
924 !atomic_read(&conf->barrier[idx]))
925 return;
928 * After holding conf->resync_lock, conf->nr_pending[idx]
929 * should be decreased before waiting for barrier to drop.
930 * Otherwise, we may encounter a race condition because
931 * raise_barrer() might be waiting for conf->nr_pending[idx]
932 * to be 0 at same time.
934 spin_lock_irq(&conf->resync_lock);
935 atomic_inc(&conf->nr_waiting[idx]);
936 atomic_dec(&conf->nr_pending[idx]);
938 * In case freeze_array() is waiting for
939 * get_unqueued_pending() == extra
941 wake_up(&conf->wait_barrier);
942 /* Wait for the barrier in same barrier unit bucket to drop. */
943 wait_event_lock_irq(conf->wait_barrier,
944 !conf->array_frozen &&
945 !atomic_read(&conf->barrier[idx]),
946 conf->resync_lock);
947 atomic_inc(&conf->nr_pending[idx]);
948 atomic_dec(&conf->nr_waiting[idx]);
949 spin_unlock_irq(&conf->resync_lock);
952 static void wait_read_barrier(struct r1conf *conf, sector_t sector_nr)
954 int idx = sector_to_idx(sector_nr);
957 * Very similar to _wait_barrier(). The difference is, for read
958 * I/O we don't need wait for sync I/O, but if the whole array
959 * is frozen, the read I/O still has to wait until the array is
960 * unfrozen. Since there is no ordering requirement with
961 * conf->barrier[idx] here, memory barrier is unnecessary as well.
963 atomic_inc(&conf->nr_pending[idx]);
965 if (!READ_ONCE(conf->array_frozen))
966 return;
968 spin_lock_irq(&conf->resync_lock);
969 atomic_inc(&conf->nr_waiting[idx]);
970 atomic_dec(&conf->nr_pending[idx]);
972 * In case freeze_array() is waiting for
973 * get_unqueued_pending() == extra
975 wake_up(&conf->wait_barrier);
976 /* Wait for array to be unfrozen */
977 wait_event_lock_irq(conf->wait_barrier,
978 !conf->array_frozen,
979 conf->resync_lock);
980 atomic_inc(&conf->nr_pending[idx]);
981 atomic_dec(&conf->nr_waiting[idx]);
982 spin_unlock_irq(&conf->resync_lock);
985 static void wait_barrier(struct r1conf *conf, sector_t sector_nr)
987 int idx = sector_to_idx(sector_nr);
989 _wait_barrier(conf, idx);
992 static void _allow_barrier(struct r1conf *conf, int idx)
994 atomic_dec(&conf->nr_pending[idx]);
995 wake_up(&conf->wait_barrier);
998 static void allow_barrier(struct r1conf *conf, sector_t sector_nr)
1000 int idx = sector_to_idx(sector_nr);
1002 _allow_barrier(conf, idx);
1005 /* conf->resync_lock should be held */
1006 static int get_unqueued_pending(struct r1conf *conf)
1008 int idx, ret;
1010 ret = atomic_read(&conf->nr_sync_pending);
1011 for (idx = 0; idx < BARRIER_BUCKETS_NR; idx++)
1012 ret += atomic_read(&conf->nr_pending[idx]) -
1013 atomic_read(&conf->nr_queued[idx]);
1015 return ret;
1018 static void freeze_array(struct r1conf *conf, int extra)
1020 /* Stop sync I/O and normal I/O and wait for everything to
1021 * go quiet.
1022 * This is called in two situations:
1023 * 1) management command handlers (reshape, remove disk, quiesce).
1024 * 2) one normal I/O request failed.
1026 * After array_frozen is set to 1, new sync IO will be blocked at
1027 * raise_barrier(), and new normal I/O will blocked at _wait_barrier()
1028 * or wait_read_barrier(). The flying I/Os will either complete or be
1029 * queued. When everything goes quite, there are only queued I/Os left.
1031 * Every flying I/O contributes to a conf->nr_pending[idx], idx is the
1032 * barrier bucket index which this I/O request hits. When all sync and
1033 * normal I/O are queued, sum of all conf->nr_pending[] will match sum
1034 * of all conf->nr_queued[]. But normal I/O failure is an exception,
1035 * in handle_read_error(), we may call freeze_array() before trying to
1036 * fix the read error. In this case, the error read I/O is not queued,
1037 * so get_unqueued_pending() == 1.
1039 * Therefore before this function returns, we need to wait until
1040 * get_unqueued_pendings(conf) gets equal to extra. For
1041 * normal I/O context, extra is 1, in rested situations extra is 0.
1043 spin_lock_irq(&conf->resync_lock);
1044 conf->array_frozen = 1;
1045 raid1_log(conf->mddev, "wait freeze");
1046 wait_event_lock_irq_cmd(
1047 conf->wait_barrier,
1048 get_unqueued_pending(conf) == extra,
1049 conf->resync_lock,
1050 flush_pending_writes(conf));
1051 spin_unlock_irq(&conf->resync_lock);
1053 static void unfreeze_array(struct r1conf *conf)
1055 /* reverse the effect of the freeze */
1056 spin_lock_irq(&conf->resync_lock);
1057 conf->array_frozen = 0;
1058 spin_unlock_irq(&conf->resync_lock);
1059 wake_up(&conf->wait_barrier);
1062 static void alloc_behind_master_bio(struct r1bio *r1_bio,
1063 struct bio *bio)
1065 int size = bio->bi_iter.bi_size;
1066 unsigned vcnt = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1067 int i = 0;
1068 struct bio *behind_bio = NULL;
1070 behind_bio = bio_alloc_mddev(GFP_NOIO, vcnt, r1_bio->mddev);
1071 if (!behind_bio)
1072 return;
1074 /* discard op, we don't support writezero/writesame yet */
1075 if (!bio_has_data(bio)) {
1076 behind_bio->bi_iter.bi_size = size;
1077 goto skip_copy;
1080 while (i < vcnt && size) {
1081 struct page *page;
1082 int len = min_t(int, PAGE_SIZE, size);
1084 page = alloc_page(GFP_NOIO);
1085 if (unlikely(!page))
1086 goto free_pages;
1088 bio_add_page(behind_bio, page, len, 0);
1090 size -= len;
1091 i++;
1094 bio_copy_data(behind_bio, bio);
1095 skip_copy:
1096 r1_bio->behind_master_bio = behind_bio;;
1097 set_bit(R1BIO_BehindIO, &r1_bio->state);
1099 return;
1101 free_pages:
1102 pr_debug("%dB behind alloc failed, doing sync I/O\n",
1103 bio->bi_iter.bi_size);
1104 bio_free_pages(behind_bio);
1105 bio_put(behind_bio);
1108 struct raid1_plug_cb {
1109 struct blk_plug_cb cb;
1110 struct bio_list pending;
1111 int pending_cnt;
1114 static void raid1_unplug(struct blk_plug_cb *cb, bool from_schedule)
1116 struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb,
1117 cb);
1118 struct mddev *mddev = plug->cb.data;
1119 struct r1conf *conf = mddev->private;
1120 struct bio *bio;
1122 if (from_schedule || current->bio_list) {
1123 spin_lock_irq(&conf->device_lock);
1124 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1125 conf->pending_count += plug->pending_cnt;
1126 spin_unlock_irq(&conf->device_lock);
1127 wake_up(&conf->wait_barrier);
1128 md_wakeup_thread(mddev->thread);
1129 kfree(plug);
1130 return;
1133 /* we aren't scheduling, so we can do the write-out directly. */
1134 bio = bio_list_get(&plug->pending);
1135 flush_bio_list(conf, bio);
1136 kfree(plug);
1139 static void init_r1bio(struct r1bio *r1_bio, struct mddev *mddev, struct bio *bio)
1141 r1_bio->master_bio = bio;
1142 r1_bio->sectors = bio_sectors(bio);
1143 r1_bio->state = 0;
1144 r1_bio->mddev = mddev;
1145 r1_bio->sector = bio->bi_iter.bi_sector;
1148 static inline struct r1bio *
1149 alloc_r1bio(struct mddev *mddev, struct bio *bio)
1151 struct r1conf *conf = mddev->private;
1152 struct r1bio *r1_bio;
1154 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1155 /* Ensure no bio records IO_BLOCKED */
1156 memset(r1_bio->bios, 0, conf->raid_disks * sizeof(r1_bio->bios[0]));
1157 init_r1bio(r1_bio, mddev, bio);
1158 return r1_bio;
1161 static void raid1_read_request(struct mddev *mddev, struct bio *bio,
1162 int max_read_sectors, struct r1bio *r1_bio)
1164 struct r1conf *conf = mddev->private;
1165 struct raid1_info *mirror;
1166 struct bio *read_bio;
1167 struct bitmap *bitmap = mddev->bitmap;
1168 const int op = bio_op(bio);
1169 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1170 int max_sectors;
1171 int rdisk;
1172 bool print_msg = !!r1_bio;
1173 char b[BDEVNAME_SIZE];
1176 * If r1_bio is set, we are blocking the raid1d thread
1177 * so there is a tiny risk of deadlock. So ask for
1178 * emergency memory if needed.
1180 gfp_t gfp = r1_bio ? (GFP_NOIO | __GFP_HIGH) : GFP_NOIO;
1182 if (print_msg) {
1183 /* Need to get the block device name carefully */
1184 struct md_rdev *rdev;
1185 rcu_read_lock();
1186 rdev = rcu_dereference(conf->mirrors[r1_bio->read_disk].rdev);
1187 if (rdev)
1188 bdevname(rdev->bdev, b);
1189 else
1190 strcpy(b, "???");
1191 rcu_read_unlock();
1195 * Still need barrier for READ in case that whole
1196 * array is frozen.
1198 wait_read_barrier(conf, bio->bi_iter.bi_sector);
1200 if (!r1_bio)
1201 r1_bio = alloc_r1bio(mddev, bio);
1202 else
1203 init_r1bio(r1_bio, mddev, bio);
1204 r1_bio->sectors = max_read_sectors;
1207 * make_request() can abort the operation when read-ahead is being
1208 * used and no empty request is available.
1210 rdisk = read_balance(conf, r1_bio, &max_sectors);
1212 if (rdisk < 0) {
1213 /* couldn't find anywhere to read from */
1214 if (print_msg) {
1215 pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
1216 mdname(mddev),
1218 (unsigned long long)r1_bio->sector);
1220 raid_end_bio_io(r1_bio);
1221 return;
1223 mirror = conf->mirrors + rdisk;
1225 if (print_msg)
1226 pr_info_ratelimited("md/raid1:%s: redirecting sector %llu to other mirror: %s\n",
1227 mdname(mddev),
1228 (unsigned long long)r1_bio->sector,
1229 bdevname(mirror->rdev->bdev, b));
1231 if (test_bit(WriteMostly, &mirror->rdev->flags) &&
1232 bitmap) {
1234 * Reading from a write-mostly device must take care not to
1235 * over-take any writes that are 'behind'
1237 raid1_log(mddev, "wait behind writes");
1238 wait_event(bitmap->behind_wait,
1239 atomic_read(&bitmap->behind_writes) == 0);
1242 if (max_sectors < bio_sectors(bio)) {
1243 struct bio *split = bio_split(bio, max_sectors,
1244 gfp, conf->bio_split);
1245 bio_chain(split, bio);
1246 generic_make_request(bio);
1247 bio = split;
1248 r1_bio->master_bio = bio;
1249 r1_bio->sectors = max_sectors;
1252 r1_bio->read_disk = rdisk;
1254 read_bio = bio_clone_fast(bio, gfp, mddev->bio_set);
1256 r1_bio->bios[rdisk] = read_bio;
1258 read_bio->bi_iter.bi_sector = r1_bio->sector +
1259 mirror->rdev->data_offset;
1260 bio_set_dev(read_bio, mirror->rdev->bdev);
1261 read_bio->bi_end_io = raid1_end_read_request;
1262 bio_set_op_attrs(read_bio, op, do_sync);
1263 if (test_bit(FailFast, &mirror->rdev->flags) &&
1264 test_bit(R1BIO_FailFast, &r1_bio->state))
1265 read_bio->bi_opf |= MD_FAILFAST;
1266 read_bio->bi_private = r1_bio;
1268 if (mddev->gendisk)
1269 trace_block_bio_remap(read_bio->bi_disk->queue, read_bio,
1270 disk_devt(mddev->gendisk), r1_bio->sector);
1272 generic_make_request(read_bio);
1275 static void raid1_write_request(struct mddev *mddev, struct bio *bio,
1276 int max_write_sectors)
1278 struct r1conf *conf = mddev->private;
1279 struct r1bio *r1_bio;
1280 int i, disks;
1281 struct bitmap *bitmap = mddev->bitmap;
1282 unsigned long flags;
1283 struct md_rdev *blocked_rdev;
1284 struct blk_plug_cb *cb;
1285 struct raid1_plug_cb *plug = NULL;
1286 int first_clone;
1287 int max_sectors;
1289 if (mddev_is_clustered(mddev) &&
1290 md_cluster_ops->area_resyncing(mddev, WRITE,
1291 bio->bi_iter.bi_sector, bio_end_sector(bio))) {
1293 DEFINE_WAIT(w);
1294 for (;;) {
1295 prepare_to_wait(&conf->wait_barrier,
1296 &w, TASK_IDLE);
1297 if (!md_cluster_ops->area_resyncing(mddev, WRITE,
1298 bio->bi_iter.bi_sector,
1299 bio_end_sector(bio)))
1300 break;
1301 schedule();
1303 finish_wait(&conf->wait_barrier, &w);
1307 * Register the new request and wait if the reconstruction
1308 * thread has put up a bar for new requests.
1309 * Continue immediately if no resync is active currently.
1311 wait_barrier(conf, bio->bi_iter.bi_sector);
1313 r1_bio = alloc_r1bio(mddev, bio);
1314 r1_bio->sectors = max_write_sectors;
1316 if (conf->pending_count >= max_queued_requests) {
1317 md_wakeup_thread(mddev->thread);
1318 raid1_log(mddev, "wait queued");
1319 wait_event(conf->wait_barrier,
1320 conf->pending_count < max_queued_requests);
1322 /* first select target devices under rcu_lock and
1323 * inc refcount on their rdev. Record them by setting
1324 * bios[x] to bio
1325 * If there are known/acknowledged bad blocks on any device on
1326 * which we have seen a write error, we want to avoid writing those
1327 * blocks.
1328 * This potentially requires several writes to write around
1329 * the bad blocks. Each set of writes gets it's own r1bio
1330 * with a set of bios attached.
1333 disks = conf->raid_disks * 2;
1334 retry_write:
1335 blocked_rdev = NULL;
1336 rcu_read_lock();
1337 max_sectors = r1_bio->sectors;
1338 for (i = 0; i < disks; i++) {
1339 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1340 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1341 atomic_inc(&rdev->nr_pending);
1342 blocked_rdev = rdev;
1343 break;
1345 r1_bio->bios[i] = NULL;
1346 if (!rdev || test_bit(Faulty, &rdev->flags)) {
1347 if (i < conf->raid_disks)
1348 set_bit(R1BIO_Degraded, &r1_bio->state);
1349 continue;
1352 atomic_inc(&rdev->nr_pending);
1353 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1354 sector_t first_bad;
1355 int bad_sectors;
1356 int is_bad;
1358 is_bad = is_badblock(rdev, r1_bio->sector, max_sectors,
1359 &first_bad, &bad_sectors);
1360 if (is_bad < 0) {
1361 /* mustn't write here until the bad block is
1362 * acknowledged*/
1363 set_bit(BlockedBadBlocks, &rdev->flags);
1364 blocked_rdev = rdev;
1365 break;
1367 if (is_bad && first_bad <= r1_bio->sector) {
1368 /* Cannot write here at all */
1369 bad_sectors -= (r1_bio->sector - first_bad);
1370 if (bad_sectors < max_sectors)
1371 /* mustn't write more than bad_sectors
1372 * to other devices yet
1374 max_sectors = bad_sectors;
1375 rdev_dec_pending(rdev, mddev);
1376 /* We don't set R1BIO_Degraded as that
1377 * only applies if the disk is
1378 * missing, so it might be re-added,
1379 * and we want to know to recover this
1380 * chunk.
1381 * In this case the device is here,
1382 * and the fact that this chunk is not
1383 * in-sync is recorded in the bad
1384 * block log
1386 continue;
1388 if (is_bad) {
1389 int good_sectors = first_bad - r1_bio->sector;
1390 if (good_sectors < max_sectors)
1391 max_sectors = good_sectors;
1394 r1_bio->bios[i] = bio;
1396 rcu_read_unlock();
1398 if (unlikely(blocked_rdev)) {
1399 /* Wait for this device to become unblocked */
1400 int j;
1402 for (j = 0; j < i; j++)
1403 if (r1_bio->bios[j])
1404 rdev_dec_pending(conf->mirrors[j].rdev, mddev);
1405 r1_bio->state = 0;
1406 allow_barrier(conf, bio->bi_iter.bi_sector);
1407 raid1_log(mddev, "wait rdev %d blocked", blocked_rdev->raid_disk);
1408 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1409 wait_barrier(conf, bio->bi_iter.bi_sector);
1410 goto retry_write;
1413 if (max_sectors < bio_sectors(bio)) {
1414 struct bio *split = bio_split(bio, max_sectors,
1415 GFP_NOIO, conf->bio_split);
1416 bio_chain(split, bio);
1417 generic_make_request(bio);
1418 bio = split;
1419 r1_bio->master_bio = bio;
1420 r1_bio->sectors = max_sectors;
1423 atomic_set(&r1_bio->remaining, 1);
1424 atomic_set(&r1_bio->behind_remaining, 0);
1426 first_clone = 1;
1428 for (i = 0; i < disks; i++) {
1429 struct bio *mbio = NULL;
1430 if (!r1_bio->bios[i])
1431 continue;
1434 if (first_clone) {
1435 /* do behind I/O ?
1436 * Not if there are too many, or cannot
1437 * allocate memory, or a reader on WriteMostly
1438 * is waiting for behind writes to flush */
1439 if (bitmap &&
1440 (atomic_read(&bitmap->behind_writes)
1441 < mddev->bitmap_info.max_write_behind) &&
1442 !waitqueue_active(&bitmap->behind_wait)) {
1443 alloc_behind_master_bio(r1_bio, bio);
1446 bitmap_startwrite(bitmap, r1_bio->sector,
1447 r1_bio->sectors,
1448 test_bit(R1BIO_BehindIO,
1449 &r1_bio->state));
1450 first_clone = 0;
1453 if (r1_bio->behind_master_bio)
1454 mbio = bio_clone_fast(r1_bio->behind_master_bio,
1455 GFP_NOIO, mddev->bio_set);
1456 else
1457 mbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
1459 if (r1_bio->behind_master_bio) {
1460 if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
1461 atomic_inc(&r1_bio->behind_remaining);
1464 r1_bio->bios[i] = mbio;
1466 mbio->bi_iter.bi_sector = (r1_bio->sector +
1467 conf->mirrors[i].rdev->data_offset);
1468 bio_set_dev(mbio, conf->mirrors[i].rdev->bdev);
1469 mbio->bi_end_io = raid1_end_write_request;
1470 mbio->bi_opf = bio_op(bio) | (bio->bi_opf & (REQ_SYNC | REQ_FUA));
1471 if (test_bit(FailFast, &conf->mirrors[i].rdev->flags) &&
1472 !test_bit(WriteMostly, &conf->mirrors[i].rdev->flags) &&
1473 conf->raid_disks - mddev->degraded > 1)
1474 mbio->bi_opf |= MD_FAILFAST;
1475 mbio->bi_private = r1_bio;
1477 atomic_inc(&r1_bio->remaining);
1479 if (mddev->gendisk)
1480 trace_block_bio_remap(mbio->bi_disk->queue,
1481 mbio, disk_devt(mddev->gendisk),
1482 r1_bio->sector);
1483 /* flush_pending_writes() needs access to the rdev so...*/
1484 mbio->bi_disk = (void *)conf->mirrors[i].rdev;
1486 cb = blk_check_plugged(raid1_unplug, mddev, sizeof(*plug));
1487 if (cb)
1488 plug = container_of(cb, struct raid1_plug_cb, cb);
1489 else
1490 plug = NULL;
1491 if (plug) {
1492 bio_list_add(&plug->pending, mbio);
1493 plug->pending_cnt++;
1494 } else {
1495 spin_lock_irqsave(&conf->device_lock, flags);
1496 bio_list_add(&conf->pending_bio_list, mbio);
1497 conf->pending_count++;
1498 spin_unlock_irqrestore(&conf->device_lock, flags);
1499 md_wakeup_thread(mddev->thread);
1503 r1_bio_write_done(r1_bio);
1505 /* In case raid1d snuck in to freeze_array */
1506 wake_up(&conf->wait_barrier);
1509 static bool raid1_make_request(struct mddev *mddev, struct bio *bio)
1511 sector_t sectors;
1513 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1514 md_flush_request(mddev, bio);
1515 return true;
1519 * There is a limit to the maximum size, but
1520 * the read/write handler might find a lower limit
1521 * due to bad blocks. To avoid multiple splits,
1522 * we pass the maximum number of sectors down
1523 * and let the lower level perform the split.
1525 sectors = align_to_barrier_unit_end(
1526 bio->bi_iter.bi_sector, bio_sectors(bio));
1528 if (bio_data_dir(bio) == READ)
1529 raid1_read_request(mddev, bio, sectors, NULL);
1530 else {
1531 if (!md_write_start(mddev,bio))
1532 return false;
1533 raid1_write_request(mddev, bio, sectors);
1535 return true;
1538 static void raid1_status(struct seq_file *seq, struct mddev *mddev)
1540 struct r1conf *conf = mddev->private;
1541 int i;
1543 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
1544 conf->raid_disks - mddev->degraded);
1545 rcu_read_lock();
1546 for (i = 0; i < conf->raid_disks; i++) {
1547 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1548 seq_printf(seq, "%s",
1549 rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1551 rcu_read_unlock();
1552 seq_printf(seq, "]");
1555 static void raid1_error(struct mddev *mddev, struct md_rdev *rdev)
1557 char b[BDEVNAME_SIZE];
1558 struct r1conf *conf = mddev->private;
1559 unsigned long flags;
1562 * If it is not operational, then we have already marked it as dead
1563 * else if it is the last working disks, ignore the error, let the
1564 * next level up know.
1565 * else mark the drive as failed
1567 spin_lock_irqsave(&conf->device_lock, flags);
1568 if (test_bit(In_sync, &rdev->flags)
1569 && (conf->raid_disks - mddev->degraded) == 1) {
1571 * Don't fail the drive, act as though we were just a
1572 * normal single drive.
1573 * However don't try a recovery from this drive as
1574 * it is very likely to fail.
1576 conf->recovery_disabled = mddev->recovery_disabled;
1577 spin_unlock_irqrestore(&conf->device_lock, flags);
1578 return;
1580 set_bit(Blocked, &rdev->flags);
1581 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1582 mddev->degraded++;
1583 set_bit(Faulty, &rdev->flags);
1584 } else
1585 set_bit(Faulty, &rdev->flags);
1586 spin_unlock_irqrestore(&conf->device_lock, flags);
1588 * if recovery is running, make sure it aborts.
1590 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1591 set_mask_bits(&mddev->sb_flags, 0,
1592 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
1593 pr_crit("md/raid1:%s: Disk failure on %s, disabling device.\n"
1594 "md/raid1:%s: Operation continuing on %d devices.\n",
1595 mdname(mddev), bdevname(rdev->bdev, b),
1596 mdname(mddev), conf->raid_disks - mddev->degraded);
1599 static void print_conf(struct r1conf *conf)
1601 int i;
1603 pr_debug("RAID1 conf printout:\n");
1604 if (!conf) {
1605 pr_debug("(!conf)\n");
1606 return;
1608 pr_debug(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1609 conf->raid_disks);
1611 rcu_read_lock();
1612 for (i = 0; i < conf->raid_disks; i++) {
1613 char b[BDEVNAME_SIZE];
1614 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1615 if (rdev)
1616 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1617 i, !test_bit(In_sync, &rdev->flags),
1618 !test_bit(Faulty, &rdev->flags),
1619 bdevname(rdev->bdev,b));
1621 rcu_read_unlock();
1624 static void close_sync(struct r1conf *conf)
1626 int idx;
1628 for (idx = 0; idx < BARRIER_BUCKETS_NR; idx++) {
1629 _wait_barrier(conf, idx);
1630 _allow_barrier(conf, idx);
1633 mempool_destroy(conf->r1buf_pool);
1634 conf->r1buf_pool = NULL;
1637 static int raid1_spare_active(struct mddev *mddev)
1639 int i;
1640 struct r1conf *conf = mddev->private;
1641 int count = 0;
1642 unsigned long flags;
1645 * Find all failed disks within the RAID1 configuration
1646 * and mark them readable.
1647 * Called under mddev lock, so rcu protection not needed.
1648 * device_lock used to avoid races with raid1_end_read_request
1649 * which expects 'In_sync' flags and ->degraded to be consistent.
1651 spin_lock_irqsave(&conf->device_lock, flags);
1652 for (i = 0; i < conf->raid_disks; i++) {
1653 struct md_rdev *rdev = conf->mirrors[i].rdev;
1654 struct md_rdev *repl = conf->mirrors[conf->raid_disks + i].rdev;
1655 if (repl
1656 && !test_bit(Candidate, &repl->flags)
1657 && repl->recovery_offset == MaxSector
1658 && !test_bit(Faulty, &repl->flags)
1659 && !test_and_set_bit(In_sync, &repl->flags)) {
1660 /* replacement has just become active */
1661 if (!rdev ||
1662 !test_and_clear_bit(In_sync, &rdev->flags))
1663 count++;
1664 if (rdev) {
1665 /* Replaced device not technically
1666 * faulty, but we need to be sure
1667 * it gets removed and never re-added
1669 set_bit(Faulty, &rdev->flags);
1670 sysfs_notify_dirent_safe(
1671 rdev->sysfs_state);
1674 if (rdev
1675 && rdev->recovery_offset == MaxSector
1676 && !test_bit(Faulty, &rdev->flags)
1677 && !test_and_set_bit(In_sync, &rdev->flags)) {
1678 count++;
1679 sysfs_notify_dirent_safe(rdev->sysfs_state);
1682 mddev->degraded -= count;
1683 spin_unlock_irqrestore(&conf->device_lock, flags);
1685 print_conf(conf);
1686 return count;
1689 static int raid1_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1691 struct r1conf *conf = mddev->private;
1692 int err = -EEXIST;
1693 int mirror = 0;
1694 struct raid1_info *p;
1695 int first = 0;
1696 int last = conf->raid_disks - 1;
1698 if (mddev->recovery_disabled == conf->recovery_disabled)
1699 return -EBUSY;
1701 if (md_integrity_add_rdev(rdev, mddev))
1702 return -ENXIO;
1704 if (rdev->raid_disk >= 0)
1705 first = last = rdev->raid_disk;
1708 * find the disk ... but prefer rdev->saved_raid_disk
1709 * if possible.
1711 if (rdev->saved_raid_disk >= 0 &&
1712 rdev->saved_raid_disk >= first &&
1713 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1714 first = last = rdev->saved_raid_disk;
1716 for (mirror = first; mirror <= last; mirror++) {
1717 p = conf->mirrors+mirror;
1718 if (!p->rdev) {
1720 if (mddev->gendisk)
1721 disk_stack_limits(mddev->gendisk, rdev->bdev,
1722 rdev->data_offset << 9);
1724 p->head_position = 0;
1725 rdev->raid_disk = mirror;
1726 err = 0;
1727 /* As all devices are equivalent, we don't need a full recovery
1728 * if this was recently any drive of the array
1730 if (rdev->saved_raid_disk < 0)
1731 conf->fullsync = 1;
1732 rcu_assign_pointer(p->rdev, rdev);
1733 break;
1735 if (test_bit(WantReplacement, &p->rdev->flags) &&
1736 p[conf->raid_disks].rdev == NULL) {
1737 /* Add this device as a replacement */
1738 clear_bit(In_sync, &rdev->flags);
1739 set_bit(Replacement, &rdev->flags);
1740 rdev->raid_disk = mirror;
1741 err = 0;
1742 conf->fullsync = 1;
1743 rcu_assign_pointer(p[conf->raid_disks].rdev, rdev);
1744 break;
1747 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
1748 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
1749 print_conf(conf);
1750 return err;
1753 static int raid1_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1755 struct r1conf *conf = mddev->private;
1756 int err = 0;
1757 int number = rdev->raid_disk;
1758 struct raid1_info *p = conf->mirrors + number;
1760 if (rdev != p->rdev)
1761 p = conf->mirrors + conf->raid_disks + number;
1763 print_conf(conf);
1764 if (rdev == p->rdev) {
1765 if (test_bit(In_sync, &rdev->flags) ||
1766 atomic_read(&rdev->nr_pending)) {
1767 err = -EBUSY;
1768 goto abort;
1770 /* Only remove non-faulty devices if recovery
1771 * is not possible.
1773 if (!test_bit(Faulty, &rdev->flags) &&
1774 mddev->recovery_disabled != conf->recovery_disabled &&
1775 mddev->degraded < conf->raid_disks) {
1776 err = -EBUSY;
1777 goto abort;
1779 p->rdev = NULL;
1780 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
1781 synchronize_rcu();
1782 if (atomic_read(&rdev->nr_pending)) {
1783 /* lost the race, try later */
1784 err = -EBUSY;
1785 p->rdev = rdev;
1786 goto abort;
1789 if (conf->mirrors[conf->raid_disks + number].rdev) {
1790 /* We just removed a device that is being replaced.
1791 * Move down the replacement. We drain all IO before
1792 * doing this to avoid confusion.
1794 struct md_rdev *repl =
1795 conf->mirrors[conf->raid_disks + number].rdev;
1796 freeze_array(conf, 0);
1797 clear_bit(Replacement, &repl->flags);
1798 p->rdev = repl;
1799 conf->mirrors[conf->raid_disks + number].rdev = NULL;
1800 unfreeze_array(conf);
1803 clear_bit(WantReplacement, &rdev->flags);
1804 err = md_integrity_register(mddev);
1806 abort:
1808 print_conf(conf);
1809 return err;
1812 static void end_sync_read(struct bio *bio)
1814 struct r1bio *r1_bio = get_resync_r1bio(bio);
1816 update_head_pos(r1_bio->read_disk, r1_bio);
1819 * we have read a block, now it needs to be re-written,
1820 * or re-read if the read failed.
1821 * We don't do much here, just schedule handling by raid1d
1823 if (!bio->bi_status)
1824 set_bit(R1BIO_Uptodate, &r1_bio->state);
1826 if (atomic_dec_and_test(&r1_bio->remaining))
1827 reschedule_retry(r1_bio);
1830 static void end_sync_write(struct bio *bio)
1832 int uptodate = !bio->bi_status;
1833 struct r1bio *r1_bio = get_resync_r1bio(bio);
1834 struct mddev *mddev = r1_bio->mddev;
1835 struct r1conf *conf = mddev->private;
1836 sector_t first_bad;
1837 int bad_sectors;
1838 struct md_rdev *rdev = conf->mirrors[find_bio_disk(r1_bio, bio)].rdev;
1840 if (!uptodate) {
1841 sector_t sync_blocks = 0;
1842 sector_t s = r1_bio->sector;
1843 long sectors_to_go = r1_bio->sectors;
1844 /* make sure these bits doesn't get cleared. */
1845 do {
1846 bitmap_end_sync(mddev->bitmap, s,
1847 &sync_blocks, 1);
1848 s += sync_blocks;
1849 sectors_to_go -= sync_blocks;
1850 } while (sectors_to_go > 0);
1851 set_bit(WriteErrorSeen, &rdev->flags);
1852 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1853 set_bit(MD_RECOVERY_NEEDED, &
1854 mddev->recovery);
1855 set_bit(R1BIO_WriteError, &r1_bio->state);
1856 } else if (is_badblock(rdev, r1_bio->sector, r1_bio->sectors,
1857 &first_bad, &bad_sectors) &&
1858 !is_badblock(conf->mirrors[r1_bio->read_disk].rdev,
1859 r1_bio->sector,
1860 r1_bio->sectors,
1861 &first_bad, &bad_sectors)
1863 set_bit(R1BIO_MadeGood, &r1_bio->state);
1865 if (atomic_dec_and_test(&r1_bio->remaining)) {
1866 int s = r1_bio->sectors;
1867 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
1868 test_bit(R1BIO_WriteError, &r1_bio->state))
1869 reschedule_retry(r1_bio);
1870 else {
1871 put_buf(r1_bio);
1872 md_done_sync(mddev, s, uptodate);
1877 static int r1_sync_page_io(struct md_rdev *rdev, sector_t sector,
1878 int sectors, struct page *page, int rw)
1880 if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
1881 /* success */
1882 return 1;
1883 if (rw == WRITE) {
1884 set_bit(WriteErrorSeen, &rdev->flags);
1885 if (!test_and_set_bit(WantReplacement,
1886 &rdev->flags))
1887 set_bit(MD_RECOVERY_NEEDED, &
1888 rdev->mddev->recovery);
1890 /* need to record an error - either for the block or the device */
1891 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
1892 md_error(rdev->mddev, rdev);
1893 return 0;
1896 static int fix_sync_read_error(struct r1bio *r1_bio)
1898 /* Try some synchronous reads of other devices to get
1899 * good data, much like with normal read errors. Only
1900 * read into the pages we already have so we don't
1901 * need to re-issue the read request.
1902 * We don't need to freeze the array, because being in an
1903 * active sync request, there is no normal IO, and
1904 * no overlapping syncs.
1905 * We don't need to check is_badblock() again as we
1906 * made sure that anything with a bad block in range
1907 * will have bi_end_io clear.
1909 struct mddev *mddev = r1_bio->mddev;
1910 struct r1conf *conf = mddev->private;
1911 struct bio *bio = r1_bio->bios[r1_bio->read_disk];
1912 struct page **pages = get_resync_pages(bio)->pages;
1913 sector_t sect = r1_bio->sector;
1914 int sectors = r1_bio->sectors;
1915 int idx = 0;
1916 struct md_rdev *rdev;
1918 rdev = conf->mirrors[r1_bio->read_disk].rdev;
1919 if (test_bit(FailFast, &rdev->flags)) {
1920 /* Don't try recovering from here - just fail it
1921 * ... unless it is the last working device of course */
1922 md_error(mddev, rdev);
1923 if (test_bit(Faulty, &rdev->flags))
1924 /* Don't try to read from here, but make sure
1925 * put_buf does it's thing
1927 bio->bi_end_io = end_sync_write;
1930 while(sectors) {
1931 int s = sectors;
1932 int d = r1_bio->read_disk;
1933 int success = 0;
1934 int start;
1936 if (s > (PAGE_SIZE>>9))
1937 s = PAGE_SIZE >> 9;
1938 do {
1939 if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
1940 /* No rcu protection needed here devices
1941 * can only be removed when no resync is
1942 * active, and resync is currently active
1944 rdev = conf->mirrors[d].rdev;
1945 if (sync_page_io(rdev, sect, s<<9,
1946 pages[idx],
1947 REQ_OP_READ, 0, false)) {
1948 success = 1;
1949 break;
1952 d++;
1953 if (d == conf->raid_disks * 2)
1954 d = 0;
1955 } while (!success && d != r1_bio->read_disk);
1957 if (!success) {
1958 char b[BDEVNAME_SIZE];
1959 int abort = 0;
1960 /* Cannot read from anywhere, this block is lost.
1961 * Record a bad block on each device. If that doesn't
1962 * work just disable and interrupt the recovery.
1963 * Don't fail devices as that won't really help.
1965 pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
1966 mdname(mddev), bio_devname(bio, b),
1967 (unsigned long long)r1_bio->sector);
1968 for (d = 0; d < conf->raid_disks * 2; d++) {
1969 rdev = conf->mirrors[d].rdev;
1970 if (!rdev || test_bit(Faulty, &rdev->flags))
1971 continue;
1972 if (!rdev_set_badblocks(rdev, sect, s, 0))
1973 abort = 1;
1975 if (abort) {
1976 conf->recovery_disabled =
1977 mddev->recovery_disabled;
1978 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1979 md_done_sync(mddev, r1_bio->sectors, 0);
1980 put_buf(r1_bio);
1981 return 0;
1983 /* Try next page */
1984 sectors -= s;
1985 sect += s;
1986 idx++;
1987 continue;
1990 start = d;
1991 /* write it back and re-read */
1992 while (d != r1_bio->read_disk) {
1993 if (d == 0)
1994 d = conf->raid_disks * 2;
1995 d--;
1996 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1997 continue;
1998 rdev = conf->mirrors[d].rdev;
1999 if (r1_sync_page_io(rdev, sect, s,
2000 pages[idx],
2001 WRITE) == 0) {
2002 r1_bio->bios[d]->bi_end_io = NULL;
2003 rdev_dec_pending(rdev, mddev);
2006 d = start;
2007 while (d != r1_bio->read_disk) {
2008 if (d == 0)
2009 d = conf->raid_disks * 2;
2010 d--;
2011 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
2012 continue;
2013 rdev = conf->mirrors[d].rdev;
2014 if (r1_sync_page_io(rdev, sect, s,
2015 pages[idx],
2016 READ) != 0)
2017 atomic_add(s, &rdev->corrected_errors);
2019 sectors -= s;
2020 sect += s;
2021 idx ++;
2023 set_bit(R1BIO_Uptodate, &r1_bio->state);
2024 bio->bi_status = 0;
2025 return 1;
2028 static void process_checks(struct r1bio *r1_bio)
2030 /* We have read all readable devices. If we haven't
2031 * got the block, then there is no hope left.
2032 * If we have, then we want to do a comparison
2033 * and skip the write if everything is the same.
2034 * If any blocks failed to read, then we need to
2035 * attempt an over-write
2037 struct mddev *mddev = r1_bio->mddev;
2038 struct r1conf *conf = mddev->private;
2039 int primary;
2040 int i;
2041 int vcnt;
2043 /* Fix variable parts of all bios */
2044 vcnt = (r1_bio->sectors + PAGE_SIZE / 512 - 1) >> (PAGE_SHIFT - 9);
2045 for (i = 0; i < conf->raid_disks * 2; i++) {
2046 blk_status_t status;
2047 struct bio *b = r1_bio->bios[i];
2048 struct resync_pages *rp = get_resync_pages(b);
2049 if (b->bi_end_io != end_sync_read)
2050 continue;
2051 /* fixup the bio for reuse, but preserve errno */
2052 status = b->bi_status;
2053 bio_reset(b);
2054 b->bi_status = status;
2055 b->bi_iter.bi_sector = r1_bio->sector +
2056 conf->mirrors[i].rdev->data_offset;
2057 bio_set_dev(b, conf->mirrors[i].rdev->bdev);
2058 b->bi_end_io = end_sync_read;
2059 rp->raid_bio = r1_bio;
2060 b->bi_private = rp;
2062 /* initialize bvec table again */
2063 md_bio_reset_resync_pages(b, rp, r1_bio->sectors << 9);
2065 for (primary = 0; primary < conf->raid_disks * 2; primary++)
2066 if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
2067 !r1_bio->bios[primary]->bi_status) {
2068 r1_bio->bios[primary]->bi_end_io = NULL;
2069 rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
2070 break;
2072 r1_bio->read_disk = primary;
2073 for (i = 0; i < conf->raid_disks * 2; i++) {
2074 int j;
2075 struct bio *pbio = r1_bio->bios[primary];
2076 struct bio *sbio = r1_bio->bios[i];
2077 blk_status_t status = sbio->bi_status;
2078 struct page **ppages = get_resync_pages(pbio)->pages;
2079 struct page **spages = get_resync_pages(sbio)->pages;
2080 struct bio_vec *bi;
2081 int page_len[RESYNC_PAGES] = { 0 };
2083 if (sbio->bi_end_io != end_sync_read)
2084 continue;
2085 /* Now we can 'fixup' the error value */
2086 sbio->bi_status = 0;
2088 bio_for_each_segment_all(bi, sbio, j)
2089 page_len[j] = bi->bv_len;
2091 if (!status) {
2092 for (j = vcnt; j-- ; ) {
2093 if (memcmp(page_address(ppages[j]),
2094 page_address(spages[j]),
2095 page_len[j]))
2096 break;
2098 } else
2099 j = 0;
2100 if (j >= 0)
2101 atomic64_add(r1_bio->sectors, &mddev->resync_mismatches);
2102 if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
2103 && !status)) {
2104 /* No need to write to this device. */
2105 sbio->bi_end_io = NULL;
2106 rdev_dec_pending(conf->mirrors[i].rdev, mddev);
2107 continue;
2110 bio_copy_data(sbio, pbio);
2114 static void sync_request_write(struct mddev *mddev, struct r1bio *r1_bio)
2116 struct r1conf *conf = mddev->private;
2117 int i;
2118 int disks = conf->raid_disks * 2;
2119 struct bio *wbio;
2121 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
2122 /* ouch - failed to read all of that. */
2123 if (!fix_sync_read_error(r1_bio))
2124 return;
2126 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2127 process_checks(r1_bio);
2130 * schedule writes
2132 atomic_set(&r1_bio->remaining, 1);
2133 for (i = 0; i < disks ; i++) {
2134 wbio = r1_bio->bios[i];
2135 if (wbio->bi_end_io == NULL ||
2136 (wbio->bi_end_io == end_sync_read &&
2137 (i == r1_bio->read_disk ||
2138 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
2139 continue;
2140 if (test_bit(Faulty, &conf->mirrors[i].rdev->flags))
2141 continue;
2143 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
2144 if (test_bit(FailFast, &conf->mirrors[i].rdev->flags))
2145 wbio->bi_opf |= MD_FAILFAST;
2147 wbio->bi_end_io = end_sync_write;
2148 atomic_inc(&r1_bio->remaining);
2149 md_sync_acct(conf->mirrors[i].rdev->bdev, bio_sectors(wbio));
2151 generic_make_request(wbio);
2154 if (atomic_dec_and_test(&r1_bio->remaining)) {
2155 /* if we're here, all write(s) have completed, so clean up */
2156 int s = r1_bio->sectors;
2157 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
2158 test_bit(R1BIO_WriteError, &r1_bio->state))
2159 reschedule_retry(r1_bio);
2160 else {
2161 put_buf(r1_bio);
2162 md_done_sync(mddev, s, 1);
2168 * This is a kernel thread which:
2170 * 1. Retries failed read operations on working mirrors.
2171 * 2. Updates the raid superblock when problems encounter.
2172 * 3. Performs writes following reads for array synchronising.
2175 static void fix_read_error(struct r1conf *conf, int read_disk,
2176 sector_t sect, int sectors)
2178 struct mddev *mddev = conf->mddev;
2179 while(sectors) {
2180 int s = sectors;
2181 int d = read_disk;
2182 int success = 0;
2183 int start;
2184 struct md_rdev *rdev;
2186 if (s > (PAGE_SIZE>>9))
2187 s = PAGE_SIZE >> 9;
2189 do {
2190 sector_t first_bad;
2191 int bad_sectors;
2193 rcu_read_lock();
2194 rdev = rcu_dereference(conf->mirrors[d].rdev);
2195 if (rdev &&
2196 (test_bit(In_sync, &rdev->flags) ||
2197 (!test_bit(Faulty, &rdev->flags) &&
2198 rdev->recovery_offset >= sect + s)) &&
2199 is_badblock(rdev, sect, s,
2200 &first_bad, &bad_sectors) == 0) {
2201 atomic_inc(&rdev->nr_pending);
2202 rcu_read_unlock();
2203 if (sync_page_io(rdev, sect, s<<9,
2204 conf->tmppage, REQ_OP_READ, 0, false))
2205 success = 1;
2206 rdev_dec_pending(rdev, mddev);
2207 if (success)
2208 break;
2209 } else
2210 rcu_read_unlock();
2211 d++;
2212 if (d == conf->raid_disks * 2)
2213 d = 0;
2214 } while (!success && d != read_disk);
2216 if (!success) {
2217 /* Cannot read from anywhere - mark it bad */
2218 struct md_rdev *rdev = conf->mirrors[read_disk].rdev;
2219 if (!rdev_set_badblocks(rdev, sect, s, 0))
2220 md_error(mddev, rdev);
2221 break;
2223 /* write it back and re-read */
2224 start = d;
2225 while (d != read_disk) {
2226 if (d==0)
2227 d = conf->raid_disks * 2;
2228 d--;
2229 rcu_read_lock();
2230 rdev = rcu_dereference(conf->mirrors[d].rdev);
2231 if (rdev &&
2232 !test_bit(Faulty, &rdev->flags)) {
2233 atomic_inc(&rdev->nr_pending);
2234 rcu_read_unlock();
2235 r1_sync_page_io(rdev, sect, s,
2236 conf->tmppage, WRITE);
2237 rdev_dec_pending(rdev, mddev);
2238 } else
2239 rcu_read_unlock();
2241 d = start;
2242 while (d != read_disk) {
2243 char b[BDEVNAME_SIZE];
2244 if (d==0)
2245 d = conf->raid_disks * 2;
2246 d--;
2247 rcu_read_lock();
2248 rdev = rcu_dereference(conf->mirrors[d].rdev);
2249 if (rdev &&
2250 !test_bit(Faulty, &rdev->flags)) {
2251 atomic_inc(&rdev->nr_pending);
2252 rcu_read_unlock();
2253 if (r1_sync_page_io(rdev, sect, s,
2254 conf->tmppage, READ)) {
2255 atomic_add(s, &rdev->corrected_errors);
2256 pr_info("md/raid1:%s: read error corrected (%d sectors at %llu on %s)\n",
2257 mdname(mddev), s,
2258 (unsigned long long)(sect +
2259 rdev->data_offset),
2260 bdevname(rdev->bdev, b));
2262 rdev_dec_pending(rdev, mddev);
2263 } else
2264 rcu_read_unlock();
2266 sectors -= s;
2267 sect += s;
2271 static int narrow_write_error(struct r1bio *r1_bio, int i)
2273 struct mddev *mddev = r1_bio->mddev;
2274 struct r1conf *conf = mddev->private;
2275 struct md_rdev *rdev = conf->mirrors[i].rdev;
2277 /* bio has the data to be written to device 'i' where
2278 * we just recently had a write error.
2279 * We repeatedly clone the bio and trim down to one block,
2280 * then try the write. Where the write fails we record
2281 * a bad block.
2282 * It is conceivable that the bio doesn't exactly align with
2283 * blocks. We must handle this somehow.
2285 * We currently own a reference on the rdev.
2288 int block_sectors;
2289 sector_t sector;
2290 int sectors;
2291 int sect_to_write = r1_bio->sectors;
2292 int ok = 1;
2294 if (rdev->badblocks.shift < 0)
2295 return 0;
2297 block_sectors = roundup(1 << rdev->badblocks.shift,
2298 bdev_logical_block_size(rdev->bdev) >> 9);
2299 sector = r1_bio->sector;
2300 sectors = ((sector + block_sectors)
2301 & ~(sector_t)(block_sectors - 1))
2302 - sector;
2304 while (sect_to_write) {
2305 struct bio *wbio;
2306 if (sectors > sect_to_write)
2307 sectors = sect_to_write;
2308 /* Write at 'sector' for 'sectors'*/
2310 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
2311 wbio = bio_clone_fast(r1_bio->behind_master_bio,
2312 GFP_NOIO,
2313 mddev->bio_set);
2314 } else {
2315 wbio = bio_clone_fast(r1_bio->master_bio, GFP_NOIO,
2316 mddev->bio_set);
2319 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
2320 wbio->bi_iter.bi_sector = r1_bio->sector;
2321 wbio->bi_iter.bi_size = r1_bio->sectors << 9;
2323 bio_trim(wbio, sector - r1_bio->sector, sectors);
2324 wbio->bi_iter.bi_sector += rdev->data_offset;
2325 bio_set_dev(wbio, rdev->bdev);
2327 if (submit_bio_wait(wbio) < 0)
2328 /* failure! */
2329 ok = rdev_set_badblocks(rdev, sector,
2330 sectors, 0)
2331 && ok;
2333 bio_put(wbio);
2334 sect_to_write -= sectors;
2335 sector += sectors;
2336 sectors = block_sectors;
2338 return ok;
2341 static void handle_sync_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
2343 int m;
2344 int s = r1_bio->sectors;
2345 for (m = 0; m < conf->raid_disks * 2 ; m++) {
2346 struct md_rdev *rdev = conf->mirrors[m].rdev;
2347 struct bio *bio = r1_bio->bios[m];
2348 if (bio->bi_end_io == NULL)
2349 continue;
2350 if (!bio->bi_status &&
2351 test_bit(R1BIO_MadeGood, &r1_bio->state)) {
2352 rdev_clear_badblocks(rdev, r1_bio->sector, s, 0);
2354 if (bio->bi_status &&
2355 test_bit(R1BIO_WriteError, &r1_bio->state)) {
2356 if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0))
2357 md_error(conf->mddev, rdev);
2360 put_buf(r1_bio);
2361 md_done_sync(conf->mddev, s, 1);
2364 static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
2366 int m, idx;
2367 bool fail = false;
2369 for (m = 0; m < conf->raid_disks * 2 ; m++)
2370 if (r1_bio->bios[m] == IO_MADE_GOOD) {
2371 struct md_rdev *rdev = conf->mirrors[m].rdev;
2372 rdev_clear_badblocks(rdev,
2373 r1_bio->sector,
2374 r1_bio->sectors, 0);
2375 rdev_dec_pending(rdev, conf->mddev);
2376 } else if (r1_bio->bios[m] != NULL) {
2377 /* This drive got a write error. We need to
2378 * narrow down and record precise write
2379 * errors.
2381 fail = true;
2382 if (!narrow_write_error(r1_bio, m)) {
2383 md_error(conf->mddev,
2384 conf->mirrors[m].rdev);
2385 /* an I/O failed, we can't clear the bitmap */
2386 set_bit(R1BIO_Degraded, &r1_bio->state);
2388 rdev_dec_pending(conf->mirrors[m].rdev,
2389 conf->mddev);
2391 if (fail) {
2392 spin_lock_irq(&conf->device_lock);
2393 list_add(&r1_bio->retry_list, &conf->bio_end_io_list);
2394 idx = sector_to_idx(r1_bio->sector);
2395 atomic_inc(&conf->nr_queued[idx]);
2396 spin_unlock_irq(&conf->device_lock);
2398 * In case freeze_array() is waiting for condition
2399 * get_unqueued_pending() == extra to be true.
2401 wake_up(&conf->wait_barrier);
2402 md_wakeup_thread(conf->mddev->thread);
2403 } else {
2404 if (test_bit(R1BIO_WriteError, &r1_bio->state))
2405 close_write(r1_bio);
2406 raid_end_bio_io(r1_bio);
2410 static void handle_read_error(struct r1conf *conf, struct r1bio *r1_bio)
2412 struct mddev *mddev = conf->mddev;
2413 struct bio *bio;
2414 struct md_rdev *rdev;
2415 sector_t bio_sector;
2417 clear_bit(R1BIO_ReadError, &r1_bio->state);
2418 /* we got a read error. Maybe the drive is bad. Maybe just
2419 * the block and we can fix it.
2420 * We freeze all other IO, and try reading the block from
2421 * other devices. When we find one, we re-write
2422 * and check it that fixes the read error.
2423 * This is all done synchronously while the array is
2424 * frozen
2427 bio = r1_bio->bios[r1_bio->read_disk];
2428 bio_sector = conf->mirrors[r1_bio->read_disk].rdev->data_offset + r1_bio->sector;
2429 bio_put(bio);
2430 r1_bio->bios[r1_bio->read_disk] = NULL;
2432 rdev = conf->mirrors[r1_bio->read_disk].rdev;
2433 if (mddev->ro == 0
2434 && !test_bit(FailFast, &rdev->flags)) {
2435 freeze_array(conf, 1);
2436 fix_read_error(conf, r1_bio->read_disk,
2437 r1_bio->sector, r1_bio->sectors);
2438 unfreeze_array(conf);
2439 } else {
2440 r1_bio->bios[r1_bio->read_disk] = IO_BLOCKED;
2443 rdev_dec_pending(rdev, conf->mddev);
2444 allow_barrier(conf, r1_bio->sector);
2445 bio = r1_bio->master_bio;
2447 /* Reuse the old r1_bio so that the IO_BLOCKED settings are preserved */
2448 r1_bio->state = 0;
2449 raid1_read_request(mddev, bio, r1_bio->sectors, r1_bio);
2452 static void raid1d(struct md_thread *thread)
2454 struct mddev *mddev = thread->mddev;
2455 struct r1bio *r1_bio;
2456 unsigned long flags;
2457 struct r1conf *conf = mddev->private;
2458 struct list_head *head = &conf->retry_list;
2459 struct blk_plug plug;
2460 int idx;
2462 md_check_recovery(mddev);
2464 if (!list_empty_careful(&conf->bio_end_io_list) &&
2465 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
2466 LIST_HEAD(tmp);
2467 spin_lock_irqsave(&conf->device_lock, flags);
2468 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
2469 list_splice_init(&conf->bio_end_io_list, &tmp);
2470 spin_unlock_irqrestore(&conf->device_lock, flags);
2471 while (!list_empty(&tmp)) {
2472 r1_bio = list_first_entry(&tmp, struct r1bio,
2473 retry_list);
2474 list_del(&r1_bio->retry_list);
2475 idx = sector_to_idx(r1_bio->sector);
2476 atomic_dec(&conf->nr_queued[idx]);
2477 if (mddev->degraded)
2478 set_bit(R1BIO_Degraded, &r1_bio->state);
2479 if (test_bit(R1BIO_WriteError, &r1_bio->state))
2480 close_write(r1_bio);
2481 raid_end_bio_io(r1_bio);
2485 blk_start_plug(&plug);
2486 for (;;) {
2488 flush_pending_writes(conf);
2490 spin_lock_irqsave(&conf->device_lock, flags);
2491 if (list_empty(head)) {
2492 spin_unlock_irqrestore(&conf->device_lock, flags);
2493 break;
2495 r1_bio = list_entry(head->prev, struct r1bio, retry_list);
2496 list_del(head->prev);
2497 idx = sector_to_idx(r1_bio->sector);
2498 atomic_dec(&conf->nr_queued[idx]);
2499 spin_unlock_irqrestore(&conf->device_lock, flags);
2501 mddev = r1_bio->mddev;
2502 conf = mddev->private;
2503 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
2504 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
2505 test_bit(R1BIO_WriteError, &r1_bio->state))
2506 handle_sync_write_finished(conf, r1_bio);
2507 else
2508 sync_request_write(mddev, r1_bio);
2509 } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
2510 test_bit(R1BIO_WriteError, &r1_bio->state))
2511 handle_write_finished(conf, r1_bio);
2512 else if (test_bit(R1BIO_ReadError, &r1_bio->state))
2513 handle_read_error(conf, r1_bio);
2514 else
2515 WARN_ON_ONCE(1);
2517 cond_resched();
2518 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
2519 md_check_recovery(mddev);
2521 blk_finish_plug(&plug);
2524 static int init_resync(struct r1conf *conf)
2526 int buffs;
2528 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
2529 BUG_ON(conf->r1buf_pool);
2530 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
2531 conf->poolinfo);
2532 if (!conf->r1buf_pool)
2533 return -ENOMEM;
2534 return 0;
2537 static struct r1bio *raid1_alloc_init_r1buf(struct r1conf *conf)
2539 struct r1bio *r1bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
2540 struct resync_pages *rps;
2541 struct bio *bio;
2542 int i;
2544 for (i = conf->poolinfo->raid_disks; i--; ) {
2545 bio = r1bio->bios[i];
2546 rps = bio->bi_private;
2547 bio_reset(bio);
2548 bio->bi_private = rps;
2550 r1bio->master_bio = NULL;
2551 return r1bio;
2555 * perform a "sync" on one "block"
2557 * We need to make sure that no normal I/O request - particularly write
2558 * requests - conflict with active sync requests.
2560 * This is achieved by tracking pending requests and a 'barrier' concept
2561 * that can be installed to exclude normal IO requests.
2564 static sector_t raid1_sync_request(struct mddev *mddev, sector_t sector_nr,
2565 int *skipped)
2567 struct r1conf *conf = mddev->private;
2568 struct r1bio *r1_bio;
2569 struct bio *bio;
2570 sector_t max_sector, nr_sectors;
2571 int disk = -1;
2572 int i;
2573 int wonly = -1;
2574 int write_targets = 0, read_targets = 0;
2575 sector_t sync_blocks;
2576 int still_degraded = 0;
2577 int good_sectors = RESYNC_SECTORS;
2578 int min_bad = 0; /* number of sectors that are bad in all devices */
2579 int idx = sector_to_idx(sector_nr);
2580 int page_idx = 0;
2582 if (!conf->r1buf_pool)
2583 if (init_resync(conf))
2584 return 0;
2586 max_sector = mddev->dev_sectors;
2587 if (sector_nr >= max_sector) {
2588 /* If we aborted, we need to abort the
2589 * sync on the 'current' bitmap chunk (there will
2590 * only be one in raid1 resync.
2591 * We can find the current addess in mddev->curr_resync
2593 if (mddev->curr_resync < max_sector) /* aborted */
2594 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2595 &sync_blocks, 1);
2596 else /* completed sync */
2597 conf->fullsync = 0;
2599 bitmap_close_sync(mddev->bitmap);
2600 close_sync(conf);
2602 if (mddev_is_clustered(mddev)) {
2603 conf->cluster_sync_low = 0;
2604 conf->cluster_sync_high = 0;
2606 return 0;
2609 if (mddev->bitmap == NULL &&
2610 mddev->recovery_cp == MaxSector &&
2611 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
2612 conf->fullsync == 0) {
2613 *skipped = 1;
2614 return max_sector - sector_nr;
2616 /* before building a request, check if we can skip these blocks..
2617 * This call the bitmap_start_sync doesn't actually record anything
2619 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
2620 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2621 /* We can skip this block, and probably several more */
2622 *skipped = 1;
2623 return sync_blocks;
2627 * If there is non-resync activity waiting for a turn, then let it
2628 * though before starting on this new sync request.
2630 if (atomic_read(&conf->nr_waiting[idx]))
2631 schedule_timeout_uninterruptible(1);
2633 /* we are incrementing sector_nr below. To be safe, we check against
2634 * sector_nr + two times RESYNC_SECTORS
2637 bitmap_cond_end_sync(mddev->bitmap, sector_nr,
2638 mddev_is_clustered(mddev) && (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high));
2639 r1_bio = raid1_alloc_init_r1buf(conf);
2641 raise_barrier(conf, sector_nr);
2643 rcu_read_lock();
2645 * If we get a correctably read error during resync or recovery,
2646 * we might want to read from a different device. So we
2647 * flag all drives that could conceivably be read from for READ,
2648 * and any others (which will be non-In_sync devices) for WRITE.
2649 * If a read fails, we try reading from something else for which READ
2650 * is OK.
2653 r1_bio->mddev = mddev;
2654 r1_bio->sector = sector_nr;
2655 r1_bio->state = 0;
2656 set_bit(R1BIO_IsSync, &r1_bio->state);
2657 /* make sure good_sectors won't go across barrier unit boundary */
2658 good_sectors = align_to_barrier_unit_end(sector_nr, good_sectors);
2660 for (i = 0; i < conf->raid_disks * 2; i++) {
2661 struct md_rdev *rdev;
2662 bio = r1_bio->bios[i];
2664 rdev = rcu_dereference(conf->mirrors[i].rdev);
2665 if (rdev == NULL ||
2666 test_bit(Faulty, &rdev->flags)) {
2667 if (i < conf->raid_disks)
2668 still_degraded = 1;
2669 } else if (!test_bit(In_sync, &rdev->flags)) {
2670 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
2671 bio->bi_end_io = end_sync_write;
2672 write_targets ++;
2673 } else {
2674 /* may need to read from here */
2675 sector_t first_bad = MaxSector;
2676 int bad_sectors;
2678 if (is_badblock(rdev, sector_nr, good_sectors,
2679 &first_bad, &bad_sectors)) {
2680 if (first_bad > sector_nr)
2681 good_sectors = first_bad - sector_nr;
2682 else {
2683 bad_sectors -= (sector_nr - first_bad);
2684 if (min_bad == 0 ||
2685 min_bad > bad_sectors)
2686 min_bad = bad_sectors;
2689 if (sector_nr < first_bad) {
2690 if (test_bit(WriteMostly, &rdev->flags)) {
2691 if (wonly < 0)
2692 wonly = i;
2693 } else {
2694 if (disk < 0)
2695 disk = i;
2697 bio_set_op_attrs(bio, REQ_OP_READ, 0);
2698 bio->bi_end_io = end_sync_read;
2699 read_targets++;
2700 } else if (!test_bit(WriteErrorSeen, &rdev->flags) &&
2701 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
2702 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
2704 * The device is suitable for reading (InSync),
2705 * but has bad block(s) here. Let's try to correct them,
2706 * if we are doing resync or repair. Otherwise, leave
2707 * this device alone for this sync request.
2709 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
2710 bio->bi_end_io = end_sync_write;
2711 write_targets++;
2714 if (bio->bi_end_io) {
2715 atomic_inc(&rdev->nr_pending);
2716 bio->bi_iter.bi_sector = sector_nr + rdev->data_offset;
2717 bio_set_dev(bio, rdev->bdev);
2718 if (test_bit(FailFast, &rdev->flags))
2719 bio->bi_opf |= MD_FAILFAST;
2722 rcu_read_unlock();
2723 if (disk < 0)
2724 disk = wonly;
2725 r1_bio->read_disk = disk;
2727 if (read_targets == 0 && min_bad > 0) {
2728 /* These sectors are bad on all InSync devices, so we
2729 * need to mark them bad on all write targets
2731 int ok = 1;
2732 for (i = 0 ; i < conf->raid_disks * 2 ; i++)
2733 if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
2734 struct md_rdev *rdev = conf->mirrors[i].rdev;
2735 ok = rdev_set_badblocks(rdev, sector_nr,
2736 min_bad, 0
2737 ) && ok;
2739 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2740 *skipped = 1;
2741 put_buf(r1_bio);
2743 if (!ok) {
2744 /* Cannot record the badblocks, so need to
2745 * abort the resync.
2746 * If there are multiple read targets, could just
2747 * fail the really bad ones ???
2749 conf->recovery_disabled = mddev->recovery_disabled;
2750 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2751 return 0;
2752 } else
2753 return min_bad;
2756 if (min_bad > 0 && min_bad < good_sectors) {
2757 /* only resync enough to reach the next bad->good
2758 * transition */
2759 good_sectors = min_bad;
2762 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
2763 /* extra read targets are also write targets */
2764 write_targets += read_targets-1;
2766 if (write_targets == 0 || read_targets == 0) {
2767 /* There is nowhere to write, so all non-sync
2768 * drives must be failed - so we are finished
2770 sector_t rv;
2771 if (min_bad > 0)
2772 max_sector = sector_nr + min_bad;
2773 rv = max_sector - sector_nr;
2774 *skipped = 1;
2775 put_buf(r1_bio);
2776 return rv;
2779 if (max_sector > mddev->resync_max)
2780 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2781 if (max_sector > sector_nr + good_sectors)
2782 max_sector = sector_nr + good_sectors;
2783 nr_sectors = 0;
2784 sync_blocks = 0;
2785 do {
2786 struct page *page;
2787 int len = PAGE_SIZE;
2788 if (sector_nr + (len>>9) > max_sector)
2789 len = (max_sector - sector_nr) << 9;
2790 if (len == 0)
2791 break;
2792 if (sync_blocks == 0) {
2793 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2794 &sync_blocks, still_degraded) &&
2795 !conf->fullsync &&
2796 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2797 break;
2798 if ((len >> 9) > sync_blocks)
2799 len = sync_blocks<<9;
2802 for (i = 0 ; i < conf->raid_disks * 2; i++) {
2803 struct resync_pages *rp;
2805 bio = r1_bio->bios[i];
2806 rp = get_resync_pages(bio);
2807 if (bio->bi_end_io) {
2808 page = resync_fetch_page(rp, page_idx);
2811 * won't fail because the vec table is big
2812 * enough to hold all these pages
2814 bio_add_page(bio, page, len, 0);
2817 nr_sectors += len>>9;
2818 sector_nr += len>>9;
2819 sync_blocks -= (len>>9);
2820 } while (++page_idx < RESYNC_PAGES);
2822 r1_bio->sectors = nr_sectors;
2824 if (mddev_is_clustered(mddev) &&
2825 conf->cluster_sync_high < sector_nr + nr_sectors) {
2826 conf->cluster_sync_low = mddev->curr_resync_completed;
2827 conf->cluster_sync_high = conf->cluster_sync_low + CLUSTER_RESYNC_WINDOW_SECTORS;
2828 /* Send resync message */
2829 md_cluster_ops->resync_info_update(mddev,
2830 conf->cluster_sync_low,
2831 conf->cluster_sync_high);
2834 /* For a user-requested sync, we read all readable devices and do a
2835 * compare
2837 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2838 atomic_set(&r1_bio->remaining, read_targets);
2839 for (i = 0; i < conf->raid_disks * 2 && read_targets; i++) {
2840 bio = r1_bio->bios[i];
2841 if (bio->bi_end_io == end_sync_read) {
2842 read_targets--;
2843 md_sync_acct_bio(bio, nr_sectors);
2844 if (read_targets == 1)
2845 bio->bi_opf &= ~MD_FAILFAST;
2846 generic_make_request(bio);
2849 } else {
2850 atomic_set(&r1_bio->remaining, 1);
2851 bio = r1_bio->bios[r1_bio->read_disk];
2852 md_sync_acct_bio(bio, nr_sectors);
2853 if (read_targets == 1)
2854 bio->bi_opf &= ~MD_FAILFAST;
2855 generic_make_request(bio);
2858 return nr_sectors;
2861 static sector_t raid1_size(struct mddev *mddev, sector_t sectors, int raid_disks)
2863 if (sectors)
2864 return sectors;
2866 return mddev->dev_sectors;
2869 static struct r1conf *setup_conf(struct mddev *mddev)
2871 struct r1conf *conf;
2872 int i;
2873 struct raid1_info *disk;
2874 struct md_rdev *rdev;
2875 int err = -ENOMEM;
2877 conf = kzalloc(sizeof(struct r1conf), GFP_KERNEL);
2878 if (!conf)
2879 goto abort;
2881 conf->nr_pending = kcalloc(BARRIER_BUCKETS_NR,
2882 sizeof(atomic_t), GFP_KERNEL);
2883 if (!conf->nr_pending)
2884 goto abort;
2886 conf->nr_waiting = kcalloc(BARRIER_BUCKETS_NR,
2887 sizeof(atomic_t), GFP_KERNEL);
2888 if (!conf->nr_waiting)
2889 goto abort;
2891 conf->nr_queued = kcalloc(BARRIER_BUCKETS_NR,
2892 sizeof(atomic_t), GFP_KERNEL);
2893 if (!conf->nr_queued)
2894 goto abort;
2896 conf->barrier = kcalloc(BARRIER_BUCKETS_NR,
2897 sizeof(atomic_t), GFP_KERNEL);
2898 if (!conf->barrier)
2899 goto abort;
2901 conf->mirrors = kzalloc(sizeof(struct raid1_info)
2902 * mddev->raid_disks * 2,
2903 GFP_KERNEL);
2904 if (!conf->mirrors)
2905 goto abort;
2907 conf->tmppage = alloc_page(GFP_KERNEL);
2908 if (!conf->tmppage)
2909 goto abort;
2911 conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
2912 if (!conf->poolinfo)
2913 goto abort;
2914 conf->poolinfo->raid_disks = mddev->raid_disks * 2;
2915 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
2916 r1bio_pool_free,
2917 conf->poolinfo);
2918 if (!conf->r1bio_pool)
2919 goto abort;
2921 conf->bio_split = bioset_create(BIO_POOL_SIZE, 0, 0);
2922 if (!conf->bio_split)
2923 goto abort;
2925 conf->poolinfo->mddev = mddev;
2927 err = -EINVAL;
2928 spin_lock_init(&conf->device_lock);
2929 rdev_for_each(rdev, mddev) {
2930 int disk_idx = rdev->raid_disk;
2931 if (disk_idx >= mddev->raid_disks
2932 || disk_idx < 0)
2933 continue;
2934 if (test_bit(Replacement, &rdev->flags))
2935 disk = conf->mirrors + mddev->raid_disks + disk_idx;
2936 else
2937 disk = conf->mirrors + disk_idx;
2939 if (disk->rdev)
2940 goto abort;
2941 disk->rdev = rdev;
2942 disk->head_position = 0;
2943 disk->seq_start = MaxSector;
2945 conf->raid_disks = mddev->raid_disks;
2946 conf->mddev = mddev;
2947 INIT_LIST_HEAD(&conf->retry_list);
2948 INIT_LIST_HEAD(&conf->bio_end_io_list);
2950 spin_lock_init(&conf->resync_lock);
2951 init_waitqueue_head(&conf->wait_barrier);
2953 bio_list_init(&conf->pending_bio_list);
2954 conf->pending_count = 0;
2955 conf->recovery_disabled = mddev->recovery_disabled - 1;
2957 err = -EIO;
2958 for (i = 0; i < conf->raid_disks * 2; i++) {
2960 disk = conf->mirrors + i;
2962 if (i < conf->raid_disks &&
2963 disk[conf->raid_disks].rdev) {
2964 /* This slot has a replacement. */
2965 if (!disk->rdev) {
2966 /* No original, just make the replacement
2967 * a recovering spare
2969 disk->rdev =
2970 disk[conf->raid_disks].rdev;
2971 disk[conf->raid_disks].rdev = NULL;
2972 } else if (!test_bit(In_sync, &disk->rdev->flags))
2973 /* Original is not in_sync - bad */
2974 goto abort;
2977 if (!disk->rdev ||
2978 !test_bit(In_sync, &disk->rdev->flags)) {
2979 disk->head_position = 0;
2980 if (disk->rdev &&
2981 (disk->rdev->saved_raid_disk < 0))
2982 conf->fullsync = 1;
2986 err = -ENOMEM;
2987 conf->thread = md_register_thread(raid1d, mddev, "raid1");
2988 if (!conf->thread)
2989 goto abort;
2991 return conf;
2993 abort:
2994 if (conf) {
2995 mempool_destroy(conf->r1bio_pool);
2996 kfree(conf->mirrors);
2997 safe_put_page(conf->tmppage);
2998 kfree(conf->poolinfo);
2999 kfree(conf->nr_pending);
3000 kfree(conf->nr_waiting);
3001 kfree(conf->nr_queued);
3002 kfree(conf->barrier);
3003 if (conf->bio_split)
3004 bioset_free(conf->bio_split);
3005 kfree(conf);
3007 return ERR_PTR(err);
3010 static void raid1_free(struct mddev *mddev, void *priv);
3011 static int raid1_run(struct mddev *mddev)
3013 struct r1conf *conf;
3014 int i;
3015 struct md_rdev *rdev;
3016 int ret;
3017 bool discard_supported = false;
3019 if (mddev->level != 1) {
3020 pr_warn("md/raid1:%s: raid level not set to mirroring (%d)\n",
3021 mdname(mddev), mddev->level);
3022 return -EIO;
3024 if (mddev->reshape_position != MaxSector) {
3025 pr_warn("md/raid1:%s: reshape_position set but not supported\n",
3026 mdname(mddev));
3027 return -EIO;
3029 if (mddev_init_writes_pending(mddev) < 0)
3030 return -ENOMEM;
3032 * copy the already verified devices into our private RAID1
3033 * bookkeeping area. [whatever we allocate in run(),
3034 * should be freed in raid1_free()]
3036 if (mddev->private == NULL)
3037 conf = setup_conf(mddev);
3038 else
3039 conf = mddev->private;
3041 if (IS_ERR(conf))
3042 return PTR_ERR(conf);
3044 if (mddev->queue) {
3045 blk_queue_max_write_same_sectors(mddev->queue, 0);
3046 blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
3049 rdev_for_each(rdev, mddev) {
3050 if (!mddev->gendisk)
3051 continue;
3052 disk_stack_limits(mddev->gendisk, rdev->bdev,
3053 rdev->data_offset << 9);
3054 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3055 discard_supported = true;
3058 mddev->degraded = 0;
3059 for (i=0; i < conf->raid_disks; i++)
3060 if (conf->mirrors[i].rdev == NULL ||
3061 !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
3062 test_bit(Faulty, &conf->mirrors[i].rdev->flags))
3063 mddev->degraded++;
3065 if (conf->raid_disks - mddev->degraded == 1)
3066 mddev->recovery_cp = MaxSector;
3068 if (mddev->recovery_cp != MaxSector)
3069 pr_info("md/raid1:%s: not clean -- starting background reconstruction\n",
3070 mdname(mddev));
3071 pr_info("md/raid1:%s: active with %d out of %d mirrors\n",
3072 mdname(mddev), mddev->raid_disks - mddev->degraded,
3073 mddev->raid_disks);
3076 * Ok, everything is just fine now
3078 mddev->thread = conf->thread;
3079 conf->thread = NULL;
3080 mddev->private = conf;
3081 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
3083 md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
3085 if (mddev->queue) {
3086 if (discard_supported)
3087 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
3088 mddev->queue);
3089 else
3090 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
3091 mddev->queue);
3094 ret = md_integrity_register(mddev);
3095 if (ret) {
3096 md_unregister_thread(&mddev->thread);
3097 raid1_free(mddev, conf);
3099 return ret;
3102 static void raid1_free(struct mddev *mddev, void *priv)
3104 struct r1conf *conf = priv;
3106 mempool_destroy(conf->r1bio_pool);
3107 kfree(conf->mirrors);
3108 safe_put_page(conf->tmppage);
3109 kfree(conf->poolinfo);
3110 kfree(conf->nr_pending);
3111 kfree(conf->nr_waiting);
3112 kfree(conf->nr_queued);
3113 kfree(conf->barrier);
3114 if (conf->bio_split)
3115 bioset_free(conf->bio_split);
3116 kfree(conf);
3119 static int raid1_resize(struct mddev *mddev, sector_t sectors)
3121 /* no resync is happening, and there is enough space
3122 * on all devices, so we can resize.
3123 * We need to make sure resync covers any new space.
3124 * If the array is shrinking we should possibly wait until
3125 * any io in the removed space completes, but it hardly seems
3126 * worth it.
3128 sector_t newsize = raid1_size(mddev, sectors, 0);
3129 if (mddev->external_size &&
3130 mddev->array_sectors > newsize)
3131 return -EINVAL;
3132 if (mddev->bitmap) {
3133 int ret = bitmap_resize(mddev->bitmap, newsize, 0, 0);
3134 if (ret)
3135 return ret;
3137 md_set_array_sectors(mddev, newsize);
3138 if (sectors > mddev->dev_sectors &&
3139 mddev->recovery_cp > mddev->dev_sectors) {
3140 mddev->recovery_cp = mddev->dev_sectors;
3141 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3143 mddev->dev_sectors = sectors;
3144 mddev->resync_max_sectors = sectors;
3145 return 0;
3148 static int raid1_reshape(struct mddev *mddev)
3150 /* We need to:
3151 * 1/ resize the r1bio_pool
3152 * 2/ resize conf->mirrors
3154 * We allocate a new r1bio_pool if we can.
3155 * Then raise a device barrier and wait until all IO stops.
3156 * Then resize conf->mirrors and swap in the new r1bio pool.
3158 * At the same time, we "pack" the devices so that all the missing
3159 * devices have the higher raid_disk numbers.
3161 mempool_t *newpool, *oldpool;
3162 struct pool_info *newpoolinfo;
3163 struct raid1_info *newmirrors;
3164 struct r1conf *conf = mddev->private;
3165 int cnt, raid_disks;
3166 unsigned long flags;
3167 int d, d2;
3169 /* Cannot change chunk_size, layout, or level */
3170 if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
3171 mddev->layout != mddev->new_layout ||
3172 mddev->level != mddev->new_level) {
3173 mddev->new_chunk_sectors = mddev->chunk_sectors;
3174 mddev->new_layout = mddev->layout;
3175 mddev->new_level = mddev->level;
3176 return -EINVAL;
3179 if (!mddev_is_clustered(mddev))
3180 md_allow_write(mddev);
3182 raid_disks = mddev->raid_disks + mddev->delta_disks;
3184 if (raid_disks < conf->raid_disks) {
3185 cnt=0;
3186 for (d= 0; d < conf->raid_disks; d++)
3187 if (conf->mirrors[d].rdev)
3188 cnt++;
3189 if (cnt > raid_disks)
3190 return -EBUSY;
3193 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
3194 if (!newpoolinfo)
3195 return -ENOMEM;
3196 newpoolinfo->mddev = mddev;
3197 newpoolinfo->raid_disks = raid_disks * 2;
3199 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
3200 r1bio_pool_free, newpoolinfo);
3201 if (!newpool) {
3202 kfree(newpoolinfo);
3203 return -ENOMEM;
3205 newmirrors = kzalloc(sizeof(struct raid1_info) * raid_disks * 2,
3206 GFP_KERNEL);
3207 if (!newmirrors) {
3208 kfree(newpoolinfo);
3209 mempool_destroy(newpool);
3210 return -ENOMEM;
3213 freeze_array(conf, 0);
3215 /* ok, everything is stopped */
3216 oldpool = conf->r1bio_pool;
3217 conf->r1bio_pool = newpool;
3219 for (d = d2 = 0; d < conf->raid_disks; d++) {
3220 struct md_rdev *rdev = conf->mirrors[d].rdev;
3221 if (rdev && rdev->raid_disk != d2) {
3222 sysfs_unlink_rdev(mddev, rdev);
3223 rdev->raid_disk = d2;
3224 sysfs_unlink_rdev(mddev, rdev);
3225 if (sysfs_link_rdev(mddev, rdev))
3226 pr_warn("md/raid1:%s: cannot register rd%d\n",
3227 mdname(mddev), rdev->raid_disk);
3229 if (rdev)
3230 newmirrors[d2++].rdev = rdev;
3232 kfree(conf->mirrors);
3233 conf->mirrors = newmirrors;
3234 kfree(conf->poolinfo);
3235 conf->poolinfo = newpoolinfo;
3237 spin_lock_irqsave(&conf->device_lock, flags);
3238 mddev->degraded += (raid_disks - conf->raid_disks);
3239 spin_unlock_irqrestore(&conf->device_lock, flags);
3240 conf->raid_disks = mddev->raid_disks = raid_disks;
3241 mddev->delta_disks = 0;
3243 unfreeze_array(conf);
3245 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3246 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3247 md_wakeup_thread(mddev->thread);
3249 mempool_destroy(oldpool);
3250 return 0;
3253 static void raid1_quiesce(struct mddev *mddev, int quiesce)
3255 struct r1conf *conf = mddev->private;
3257 if (quiesce)
3258 freeze_array(conf, 0);
3259 else
3260 unfreeze_array(conf);
3263 static void *raid1_takeover(struct mddev *mddev)
3265 /* raid1 can take over:
3266 * raid5 with 2 devices, any layout or chunk size
3268 if (mddev->level == 5 && mddev->raid_disks == 2) {
3269 struct r1conf *conf;
3270 mddev->new_level = 1;
3271 mddev->new_layout = 0;
3272 mddev->new_chunk_sectors = 0;
3273 conf = setup_conf(mddev);
3274 if (!IS_ERR(conf)) {
3275 /* Array must appear to be quiesced */
3276 conf->array_frozen = 1;
3277 mddev_clear_unsupported_flags(mddev,
3278 UNSUPPORTED_MDDEV_FLAGS);
3280 return conf;
3282 return ERR_PTR(-EINVAL);
3285 static struct md_personality raid1_personality =
3287 .name = "raid1",
3288 .level = 1,
3289 .owner = THIS_MODULE,
3290 .make_request = raid1_make_request,
3291 .run = raid1_run,
3292 .free = raid1_free,
3293 .status = raid1_status,
3294 .error_handler = raid1_error,
3295 .hot_add_disk = raid1_add_disk,
3296 .hot_remove_disk= raid1_remove_disk,
3297 .spare_active = raid1_spare_active,
3298 .sync_request = raid1_sync_request,
3299 .resize = raid1_resize,
3300 .size = raid1_size,
3301 .check_reshape = raid1_reshape,
3302 .quiesce = raid1_quiesce,
3303 .takeover = raid1_takeover,
3304 .congested = raid1_congested,
3307 static int __init raid_init(void)
3309 return register_md_personality(&raid1_personality);
3312 static void raid_exit(void)
3314 unregister_md_personality(&raid1_personality);
3317 module_init(raid_init);
3318 module_exit(raid_exit);
3319 MODULE_LICENSE("GPL");
3320 MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
3321 MODULE_ALIAS("md-personality-3"); /* RAID1 */
3322 MODULE_ALIAS("md-raid1");
3323 MODULE_ALIAS("md-level-1");
3325 module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);