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