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