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