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