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