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