1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * raid10.c : Multiple Devices driver for Linux
4 *
5 * Copyright (C) 2000-2004 Neil Brown
6 *
7 * RAID-10 support for md.
8 *
9 * Base on code in raid1.c. See raid1.c for further copyright information.
10 */
11
12 #include <linux/slab.h>
13 #include <linux/delay.h>
14 #include <linux/blkdev.h>
15 #include <linux/module.h>
16 #include <linux/seq_file.h>
17 #include <linux/ratelimit.h>
18 #include <linux/kthread.h>
19 #include <linux/raid/md_p.h>
20 #include <trace/events/block.h>
21 #include "md.h"
22 #include "raid10.h"
23 #include "raid0.h"
24 #include "md-bitmap.h"
25
26 /*
27 * RAID10 provides a combination of RAID0 and RAID1 functionality.
28 * The layout of data is defined by
29 * chunk_size
30 * raid_disks
31 * near_copies (stored in low byte of layout)
32 * far_copies (stored in second byte of layout)
33 * far_offset (stored in bit 16 of layout )
34 * use_far_sets (stored in bit 17 of layout )
35 * use_far_sets_bugfixed (stored in bit 18 of layout )
36 *
37 * The data to be stored is divided into chunks using chunksize. Each device
38 * is divided into far_copies sections. In each section, chunks are laid out
39 * in a style similar to raid0, but near_copies copies of each chunk is stored
40 * (each on a different drive). The starting device for each section is offset
41 * near_copies from the starting device of the previous section. Thus there
42 * are (near_copies * far_copies) of each chunk, and each is on a different
43 * drive. near_copies and far_copies must be at least one, and their product
44 * is at most raid_disks.
45 *
46 * If far_offset is true, then the far_copies are handled a bit differently.
47 * The copies are still in different stripes, but instead of being very far
48 * apart on disk, there are adjacent stripes.
49 *
50 * The far and offset algorithms are handled slightly differently if
51 * 'use_far_sets' is true. In this case, the array's devices are grouped into
52 * sets that are (near_copies * far_copies) in size. The far copied stripes
53 * are still shifted by 'near_copies' devices, but this shifting stays confined
54 * to the set rather than the entire array. This is done to improve the number
55 * of device combinations that can fail without causing the array to fail.
56 * Example 'far' algorithm w/o 'use_far_sets' (each letter represents a chunk
57 * on a device):
58 * A B C D A B C D E
59 * ... ...
60 * D A B C E A B C D
61 * Example 'far' algorithm w/ 'use_far_sets' enabled (sets illustrated w/ []'s):
62 * [A B] [C D] [A B] [C D E]
63 * |...| |...| |...| | ... |
64 * [B A] [D C] [B A] [E C D]
65 */
66
67 static void allow_barrier(struct r10conf *conf);
68 static void lower_barrier(struct r10conf *conf);
69 static int _enough(struct r10conf *conf, int previous, int ignore);
70 static int enough(struct r10conf *conf, int ignore);
71 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
72 int *skipped);
73 static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio);
74 static void end_reshape_write(struct bio *bio);
75 static void end_reshape(struct r10conf *conf);
76
77 #define raid10_log(md, fmt, args...) \
78 do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid10 " fmt, ##args); } while (0)
79
80 #include "raid1-10.c"
81
82 #define NULL_CMD
83 #define cmd_before(conf, cmd) \
84 do { \
85 write_sequnlock_irq(&(conf)->resync_lock); \
86 cmd; \
87 } while (0)
88 #define cmd_after(conf) write_seqlock_irq(&(conf)->resync_lock)
89
90 #define wait_event_barrier_cmd(conf, cond, cmd) \
91 wait_event_cmd((conf)->wait_barrier, cond, cmd_before(conf, cmd), \
92 cmd_after(conf))
93
94 #define wait_event_barrier(conf, cond) \
95 wait_event_barrier_cmd(conf, cond, NULL_CMD)
96
97 /*
98 * for resync bio, r10bio pointer can be retrieved from the per-bio
99 * 'struct resync_pages'.
100 */
get_resync_r10bio(struct bio * bio)101 static inline struct r10bio *get_resync_r10bio(struct bio *bio)
102 {
103 return get_resync_pages(bio)->raid_bio;
104 }
105
r10bio_pool_alloc(gfp_t gfp_flags,void * data)106 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
107 {
108 struct r10conf *conf = data;
109 int size = offsetof(struct r10bio, devs[conf->geo.raid_disks]);
110
111 /* allocate a r10bio with room for raid_disks entries in the
112 * bios array */
113 return kzalloc(size, gfp_flags);
114 }
115
116 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
117 /* amount of memory to reserve for resync requests */
118 #define RESYNC_WINDOW (1024*1024)
119 /* maximum number of concurrent requests, memory permitting */
120 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
121 #define CLUSTER_RESYNC_WINDOW (32 * RESYNC_WINDOW)
122 #define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9)
123
124 /*
125 * When performing a resync, we need to read and compare, so
126 * we need as many pages are there are copies.
127 * When performing a recovery, we need 2 bios, one for read,
128 * one for write (we recover only one drive per r10buf)
129 *
130 */
r10buf_pool_alloc(gfp_t gfp_flags,void * data)131 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
132 {
133 struct r10conf *conf = data;
134 struct r10bio *r10_bio;
135 struct bio *bio;
136 int j;
137 int nalloc, nalloc_rp;
138 struct resync_pages *rps;
139
140 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
141 if (!r10_bio)
142 return NULL;
143
144 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
145 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
146 nalloc = conf->copies; /* resync */
147 else
148 nalloc = 2; /* recovery */
149
150 /* allocate once for all bios */
151 if (!conf->have_replacement)
152 nalloc_rp = nalloc;
153 else
154 nalloc_rp = nalloc * 2;
155 rps = kmalloc_array(nalloc_rp, sizeof(struct resync_pages), gfp_flags);
156 if (!rps)
157 goto out_free_r10bio;
158
159 /*
160 * Allocate bios.
161 */
162 for (j = nalloc ; j-- ; ) {
163 bio = bio_kmalloc(RESYNC_PAGES, gfp_flags);
164 if (!bio)
165 goto out_free_bio;
166 bio_init(bio, NULL, bio->bi_inline_vecs, RESYNC_PAGES, 0);
167 r10_bio->devs[j].bio = bio;
168 if (!conf->have_replacement)
169 continue;
170 bio = bio_kmalloc(RESYNC_PAGES, gfp_flags);
171 if (!bio)
172 goto out_free_bio;
173 bio_init(bio, NULL, bio->bi_inline_vecs, RESYNC_PAGES, 0);
174 r10_bio->devs[j].repl_bio = bio;
175 }
176 /*
177 * Allocate RESYNC_PAGES data pages and attach them
178 * where needed.
179 */
180 for (j = 0; j < nalloc; j++) {
181 struct bio *rbio = r10_bio->devs[j].repl_bio;
182 struct resync_pages *rp, *rp_repl;
183
184 rp = &rps[j];
185 if (rbio)
186 rp_repl = &rps[nalloc + j];
187
188 bio = r10_bio->devs[j].bio;
189
190 if (!j || test_bit(MD_RECOVERY_SYNC,
191 &conf->mddev->recovery)) {
192 if (resync_alloc_pages(rp, gfp_flags))
193 goto out_free_pages;
194 } else {
195 memcpy(rp, &rps[0], sizeof(*rp));
196 resync_get_all_pages(rp);
197 }
198
199 rp->raid_bio = r10_bio;
200 bio->bi_private = rp;
201 if (rbio) {
202 memcpy(rp_repl, rp, sizeof(*rp));
203 rbio->bi_private = rp_repl;
204 }
205 }
206
207 return r10_bio;
208
209 out_free_pages:
210 while (--j >= 0)
211 resync_free_pages(&rps[j]);
212
213 j = 0;
214 out_free_bio:
215 for ( ; j < nalloc; j++) {
216 if (r10_bio->devs[j].bio)
217 bio_uninit(r10_bio->devs[j].bio);
218 kfree(r10_bio->devs[j].bio);
219 if (r10_bio->devs[j].repl_bio)
220 bio_uninit(r10_bio->devs[j].repl_bio);
221 kfree(r10_bio->devs[j].repl_bio);
222 }
223 kfree(rps);
224 out_free_r10bio:
225 rbio_pool_free(r10_bio, conf);
226 return NULL;
227 }
228
r10buf_pool_free(void * __r10_bio,void * data)229 static void r10buf_pool_free(void *__r10_bio, void *data)
230 {
231 struct r10conf *conf = data;
232 struct r10bio *r10bio = __r10_bio;
233 int j;
234 struct resync_pages *rp = NULL;
235
236 for (j = conf->copies; j--; ) {
237 struct bio *bio = r10bio->devs[j].bio;
238
239 if (bio) {
240 rp = get_resync_pages(bio);
241 resync_free_pages(rp);
242 bio_uninit(bio);
243 kfree(bio);
244 }
245
246 bio = r10bio->devs[j].repl_bio;
247 if (bio) {
248 bio_uninit(bio);
249 kfree(bio);
250 }
251 }
252
253 /* resync pages array stored in the 1st bio's .bi_private */
254 kfree(rp);
255
256 rbio_pool_free(r10bio, conf);
257 }
258
put_all_bios(struct r10conf * conf,struct r10bio * r10_bio)259 static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
260 {
261 int i;
262
263 for (i = 0; i < conf->geo.raid_disks; i++) {
264 struct bio **bio = & r10_bio->devs[i].bio;
265 if (!BIO_SPECIAL(*bio))
266 bio_put(*bio);
267 *bio = NULL;
268 bio = &r10_bio->devs[i].repl_bio;
269 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
270 bio_put(*bio);
271 *bio = NULL;
272 }
273 }
274
free_r10bio(struct r10bio * r10_bio)275 static void free_r10bio(struct r10bio *r10_bio)
276 {
277 struct r10conf *conf = r10_bio->mddev->private;
278
279 put_all_bios(conf, r10_bio);
280 mempool_free(r10_bio, &conf->r10bio_pool);
281 }
282
put_buf(struct r10bio * r10_bio)283 static void put_buf(struct r10bio *r10_bio)
284 {
285 struct r10conf *conf = r10_bio->mddev->private;
286
287 mempool_free(r10_bio, &conf->r10buf_pool);
288
289 lower_barrier(conf);
290 }
291
wake_up_barrier(struct r10conf * conf)292 static void wake_up_barrier(struct r10conf *conf)
293 {
294 if (wq_has_sleeper(&conf->wait_barrier))
295 wake_up(&conf->wait_barrier);
296 }
297
reschedule_retry(struct r10bio * r10_bio)298 static void reschedule_retry(struct r10bio *r10_bio)
299 {
300 unsigned long flags;
301 struct mddev *mddev = r10_bio->mddev;
302 struct r10conf *conf = mddev->private;
303
304 spin_lock_irqsave(&conf->device_lock, flags);
305 list_add(&r10_bio->retry_list, &conf->retry_list);
306 conf->nr_queued ++;
307 spin_unlock_irqrestore(&conf->device_lock, flags);
308
309 /* wake up frozen array... */
310 wake_up(&conf->wait_barrier);
311
312 md_wakeup_thread(mddev->thread);
313 }
314
315 /*
316 * raid_end_bio_io() is called when we have finished servicing a mirrored
317 * operation and are ready to return a success/failure code to the buffer
318 * cache layer.
319 */
raid_end_bio_io(struct r10bio * r10_bio)320 static void raid_end_bio_io(struct r10bio *r10_bio)
321 {
322 struct bio *bio = r10_bio->master_bio;
323 struct r10conf *conf = r10_bio->mddev->private;
324
325 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
326 bio->bi_status = BLK_STS_IOERR;
327
328 bio_endio(bio);
329 /*
330 * Wake up any possible resync thread that waits for the device
331 * to go idle.
332 */
333 allow_barrier(conf);
334
335 free_r10bio(r10_bio);
336 }
337
338 /*
339 * Update disk head position estimator based on IRQ completion info.
340 */
update_head_pos(int slot,struct r10bio * r10_bio)341 static inline void update_head_pos(int slot, struct r10bio *r10_bio)
342 {
343 struct r10conf *conf = r10_bio->mddev->private;
344
345 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
346 r10_bio->devs[slot].addr + (r10_bio->sectors);
347 }
348
349 /*
350 * Find the disk number which triggered given bio
351 */
find_bio_disk(struct r10conf * conf,struct r10bio * r10_bio,struct bio * bio,int * slotp,int * replp)352 static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
353 struct bio *bio, int *slotp, int *replp)
354 {
355 int slot;
356 int repl = 0;
357
358 for (slot = 0; slot < conf->geo.raid_disks; slot++) {
359 if (r10_bio->devs[slot].bio == bio)
360 break;
361 if (r10_bio->devs[slot].repl_bio == bio) {
362 repl = 1;
363 break;
364 }
365 }
366
367 update_head_pos(slot, r10_bio);
368
369 if (slotp)
370 *slotp = slot;
371 if (replp)
372 *replp = repl;
373 return r10_bio->devs[slot].devnum;
374 }
375
raid10_end_read_request(struct bio * bio)376 static void raid10_end_read_request(struct bio *bio)
377 {
378 int uptodate = !bio->bi_status;
379 struct r10bio *r10_bio = bio->bi_private;
380 int slot;
381 struct md_rdev *rdev;
382 struct r10conf *conf = r10_bio->mddev->private;
383
384 slot = r10_bio->read_slot;
385 rdev = r10_bio->devs[slot].rdev;
386 /*
387 * this branch is our 'one mirror IO has finished' event handler:
388 */
389 update_head_pos(slot, r10_bio);
390
391 if (uptodate) {
392 /*
393 * Set R10BIO_Uptodate in our master bio, so that
394 * we will return a good error code to the higher
395 * levels even if IO on some other mirrored buffer fails.
396 *
397 * The 'master' represents the composite IO operation to
398 * user-side. So if something waits for IO, then it will
399 * wait for the 'master' bio.
400 */
401 set_bit(R10BIO_Uptodate, &r10_bio->state);
402 } else {
403 /* If all other devices that store this block have
404 * failed, we want to return the error upwards rather
405 * than fail the last device. Here we redefine
406 * "uptodate" to mean "Don't want to retry"
407 */
408 if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state),
409 rdev->raid_disk))
410 uptodate = 1;
411 }
412 if (uptodate) {
413 raid_end_bio_io(r10_bio);
414 rdev_dec_pending(rdev, conf->mddev);
415 } else {
416 /*
417 * oops, read error - keep the refcount on the rdev
418 */
419 pr_err_ratelimited("md/raid10:%s: %pg: rescheduling sector %llu\n",
420 mdname(conf->mddev),
421 rdev->bdev,
422 (unsigned long long)r10_bio->sector);
423 set_bit(R10BIO_ReadError, &r10_bio->state);
424 reschedule_retry(r10_bio);
425 }
426 }
427
close_write(struct r10bio * r10_bio)428 static void close_write(struct r10bio *r10_bio)
429 {
430 md_write_end(r10_bio->mddev);
431 }
432
one_write_done(struct r10bio * r10_bio)433 static void one_write_done(struct r10bio *r10_bio)
434 {
435 if (atomic_dec_and_test(&r10_bio->remaining)) {
436 if (test_bit(R10BIO_WriteError, &r10_bio->state))
437 reschedule_retry(r10_bio);
438 else {
439 close_write(r10_bio);
440 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
441 reschedule_retry(r10_bio);
442 else
443 raid_end_bio_io(r10_bio);
444 }
445 }
446 }
447
raid10_end_write_request(struct bio * bio)448 static void raid10_end_write_request(struct bio *bio)
449 {
450 struct r10bio *r10_bio = bio->bi_private;
451 int dev;
452 int dec_rdev = 1;
453 struct r10conf *conf = r10_bio->mddev->private;
454 int slot, repl;
455 struct md_rdev *rdev = NULL;
456 struct bio *to_put = NULL;
457 bool discard_error;
458
459 discard_error = bio->bi_status && bio_op(bio) == REQ_OP_DISCARD;
460
461 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
462
463 if (repl)
464 rdev = conf->mirrors[dev].replacement;
465 if (!rdev) {
466 smp_rmb();
467 repl = 0;
468 rdev = conf->mirrors[dev].rdev;
469 }
470 /*
471 * this branch is our 'one mirror IO has finished' event handler:
472 */
473 if (bio->bi_status && !discard_error) {
474 if (repl)
475 /* Never record new bad blocks to replacement,
476 * just fail it.
477 */
478 md_error(rdev->mddev, rdev);
479 else {
480 set_bit(WriteErrorSeen, &rdev->flags);
481 if (!test_and_set_bit(WantReplacement, &rdev->flags))
482 set_bit(MD_RECOVERY_NEEDED,
483 &rdev->mddev->recovery);
484
485 dec_rdev = 0;
486 if (test_bit(FailFast, &rdev->flags) &&
487 (bio->bi_opf & MD_FAILFAST)) {
488 md_error(rdev->mddev, rdev);
489 }
490
491 /*
492 * When the device is faulty, it is not necessary to
493 * handle write error.
494 */
495 if (!test_bit(Faulty, &rdev->flags))
496 set_bit(R10BIO_WriteError, &r10_bio->state);
497 else {
498 /* Fail the request */
499 r10_bio->devs[slot].bio = NULL;
500 to_put = bio;
501 dec_rdev = 1;
502 }
503 }
504 } else {
505 /*
506 * Set R10BIO_Uptodate in our master bio, so that
507 * we will return a good error code for to the higher
508 * levels even if IO on some other mirrored buffer fails.
509 *
510 * The 'master' represents the composite IO operation to
511 * user-side. So if something waits for IO, then it will
512 * wait for the 'master' bio.
513 */
514 sector_t first_bad;
515 int bad_sectors;
516
517 /*
518 * Do not set R10BIO_Uptodate if the current device is
519 * rebuilding or Faulty. This is because we cannot use
520 * such device for properly reading the data back (we could
521 * potentially use it, if the current write would have felt
522 * before rdev->recovery_offset, but for simplicity we don't
523 * check this here.
524 */
525 if (test_bit(In_sync, &rdev->flags) &&
526 !test_bit(Faulty, &rdev->flags))
527 set_bit(R10BIO_Uptodate, &r10_bio->state);
528
529 /* Maybe we can clear some bad blocks. */
530 if (is_badblock(rdev,
531 r10_bio->devs[slot].addr,
532 r10_bio->sectors,
533 &first_bad, &bad_sectors) && !discard_error) {
534 bio_put(bio);
535 if (repl)
536 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
537 else
538 r10_bio->devs[slot].bio = IO_MADE_GOOD;
539 dec_rdev = 0;
540 set_bit(R10BIO_MadeGood, &r10_bio->state);
541 }
542 }
543
544 /*
545 *
546 * Let's see if all mirrored write operations have finished
547 * already.
548 */
549 one_write_done(r10_bio);
550 if (dec_rdev)
551 rdev_dec_pending(rdev, conf->mddev);
552 if (to_put)
553 bio_put(to_put);
554 }
555
556 /*
557 * RAID10 layout manager
558 * As well as the chunksize and raid_disks count, there are two
559 * parameters: near_copies and far_copies.
560 * near_copies * far_copies must be <= raid_disks.
561 * Normally one of these will be 1.
562 * If both are 1, we get raid0.
563 * If near_copies == raid_disks, we get raid1.
564 *
565 * Chunks are laid out in raid0 style with near_copies copies of the
566 * first chunk, followed by near_copies copies of the next chunk and
567 * so on.
568 * If far_copies > 1, then after 1/far_copies of the array has been assigned
569 * as described above, we start again with a device offset of near_copies.
570 * So we effectively have another copy of the whole array further down all
571 * the drives, but with blocks on different drives.
572 * With this layout, and block is never stored twice on the one device.
573 *
574 * raid10_find_phys finds the sector offset of a given virtual sector
575 * on each device that it is on.
576 *
577 * raid10_find_virt does the reverse mapping, from a device and a
578 * sector offset to a virtual address
579 */
580
__raid10_find_phys(struct geom * geo,struct r10bio * r10bio)581 static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
582 {
583 int n,f;
584 sector_t sector;
585 sector_t chunk;
586 sector_t stripe;
587 int dev;
588 int slot = 0;
589 int last_far_set_start, last_far_set_size;
590
591 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
592 last_far_set_start *= geo->far_set_size;
593
594 last_far_set_size = geo->far_set_size;
595 last_far_set_size += (geo->raid_disks % geo->far_set_size);
596
597 /* now calculate first sector/dev */
598 chunk = r10bio->sector >> geo->chunk_shift;
599 sector = r10bio->sector & geo->chunk_mask;
600
601 chunk *= geo->near_copies;
602 stripe = chunk;
603 dev = sector_div(stripe, geo->raid_disks);
604 if (geo->far_offset)
605 stripe *= geo->far_copies;
606
607 sector += stripe << geo->chunk_shift;
608
609 /* and calculate all the others */
610 for (n = 0; n < geo->near_copies; n++) {
611 int d = dev;
612 int set;
613 sector_t s = sector;
614 r10bio->devs[slot].devnum = d;
615 r10bio->devs[slot].addr = s;
616 slot++;
617
618 for (f = 1; f < geo->far_copies; f++) {
619 set = d / geo->far_set_size;
620 d += geo->near_copies;
621
622 if ((geo->raid_disks % geo->far_set_size) &&
623 (d > last_far_set_start)) {
624 d -= last_far_set_start;
625 d %= last_far_set_size;
626 d += last_far_set_start;
627 } else {
628 d %= geo->far_set_size;
629 d += geo->far_set_size * set;
630 }
631 s += geo->stride;
632 r10bio->devs[slot].devnum = d;
633 r10bio->devs[slot].addr = s;
634 slot++;
635 }
636 dev++;
637 if (dev >= geo->raid_disks) {
638 dev = 0;
639 sector += (geo->chunk_mask + 1);
640 }
641 }
642 }
643
raid10_find_phys(struct r10conf * conf,struct r10bio * r10bio)644 static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
645 {
646 struct geom *geo = &conf->geo;
647
648 if (conf->reshape_progress != MaxSector &&
649 ((r10bio->sector >= conf->reshape_progress) !=
650 conf->mddev->reshape_backwards)) {
651 set_bit(R10BIO_Previous, &r10bio->state);
652 geo = &conf->prev;
653 } else
654 clear_bit(R10BIO_Previous, &r10bio->state);
655
656 __raid10_find_phys(geo, r10bio);
657 }
658
raid10_find_virt(struct r10conf * conf,sector_t sector,int dev)659 static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
660 {
661 sector_t offset, chunk, vchunk;
662 /* Never use conf->prev as this is only called during resync
663 * or recovery, so reshape isn't happening
664 */
665 struct geom *geo = &conf->geo;
666 int far_set_start = (dev / geo->far_set_size) * geo->far_set_size;
667 int far_set_size = geo->far_set_size;
668 int last_far_set_start;
669
670 if (geo->raid_disks % geo->far_set_size) {
671 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
672 last_far_set_start *= geo->far_set_size;
673
674 if (dev >= last_far_set_start) {
675 far_set_size = geo->far_set_size;
676 far_set_size += (geo->raid_disks % geo->far_set_size);
677 far_set_start = last_far_set_start;
678 }
679 }
680
681 offset = sector & geo->chunk_mask;
682 if (geo->far_offset) {
683 int fc;
684 chunk = sector >> geo->chunk_shift;
685 fc = sector_div(chunk, geo->far_copies);
686 dev -= fc * geo->near_copies;
687 if (dev < far_set_start)
688 dev += far_set_size;
689 } else {
690 while (sector >= geo->stride) {
691 sector -= geo->stride;
692 if (dev < (geo->near_copies + far_set_start))
693 dev += far_set_size - geo->near_copies;
694 else
695 dev -= geo->near_copies;
696 }
697 chunk = sector >> geo->chunk_shift;
698 }
699 vchunk = chunk * geo->raid_disks + dev;
700 sector_div(vchunk, geo->near_copies);
701 return (vchunk << geo->chunk_shift) + offset;
702 }
703
704 /*
705 * This routine returns the disk from which the requested read should
706 * be done. There is a per-array 'next expected sequential IO' sector
707 * number - if this matches on the next IO then we use the last disk.
708 * There is also a per-disk 'last know head position' sector that is
709 * maintained from IRQ contexts, both the normal and the resync IO
710 * completion handlers update this position correctly. If there is no
711 * perfect sequential match then we pick the disk whose head is closest.
712 *
713 * If there are 2 mirrors in the same 2 devices, performance degrades
714 * because position is mirror, not device based.
715 *
716 * The rdev for the device selected will have nr_pending incremented.
717 */
718
719 /*
720 * FIXME: possibly should rethink readbalancing and do it differently
721 * depending on near_copies / far_copies geometry.
722 */
read_balance(struct r10conf * conf,struct r10bio * r10_bio,int * max_sectors)723 static struct md_rdev *read_balance(struct r10conf *conf,
724 struct r10bio *r10_bio,
725 int *max_sectors)
726 {
727 const sector_t this_sector = r10_bio->sector;
728 int disk, slot;
729 int sectors = r10_bio->sectors;
730 int best_good_sectors;
731 sector_t new_distance, best_dist;
732 struct md_rdev *best_dist_rdev, *best_pending_rdev, *rdev = NULL;
733 int do_balance;
734 int best_dist_slot, best_pending_slot;
735 bool has_nonrot_disk = false;
736 unsigned int min_pending;
737 struct geom *geo = &conf->geo;
738
739 raid10_find_phys(conf, r10_bio);
740 rcu_read_lock();
741 best_dist_slot = -1;
742 min_pending = UINT_MAX;
743 best_dist_rdev = NULL;
744 best_pending_rdev = NULL;
745 best_dist = MaxSector;
746 best_good_sectors = 0;
747 do_balance = 1;
748 clear_bit(R10BIO_FailFast, &r10_bio->state);
749 /*
750 * Check if we can balance. We can balance on the whole
751 * device if no resync is going on (recovery is ok), or below
752 * the resync window. We take the first readable disk when
753 * above the resync window.
754 */
755 if ((conf->mddev->recovery_cp < MaxSector
756 && (this_sector + sectors >= conf->next_resync)) ||
757 (mddev_is_clustered(conf->mddev) &&
758 md_cluster_ops->area_resyncing(conf->mddev, READ, this_sector,
759 this_sector + sectors)))
760 do_balance = 0;
761
762 for (slot = 0; slot < conf->copies ; slot++) {
763 sector_t first_bad;
764 int bad_sectors;
765 sector_t dev_sector;
766 unsigned int pending;
767 bool nonrot;
768
769 if (r10_bio->devs[slot].bio == IO_BLOCKED)
770 continue;
771 disk = r10_bio->devs[slot].devnum;
772 rdev = rcu_dereference(conf->mirrors[disk].replacement);
773 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
774 r10_bio->devs[slot].addr + sectors >
775 rdev->recovery_offset) {
776 /*
777 * Read replacement first to prevent reading both rdev
778 * and replacement as NULL during replacement replace
779 * rdev.
780 */
781 smp_mb();
782 rdev = rcu_dereference(conf->mirrors[disk].rdev);
783 }
784 if (rdev == NULL ||
785 test_bit(Faulty, &rdev->flags))
786 continue;
787 if (!test_bit(In_sync, &rdev->flags) &&
788 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
789 continue;
790
791 dev_sector = r10_bio->devs[slot].addr;
792 if (is_badblock(rdev, dev_sector, sectors,
793 &first_bad, &bad_sectors)) {
794 if (best_dist < MaxSector)
795 /* Already have a better slot */
796 continue;
797 if (first_bad <= dev_sector) {
798 /* Cannot read here. If this is the
799 * 'primary' device, then we must not read
800 * beyond 'bad_sectors' from another device.
801 */
802 bad_sectors -= (dev_sector - first_bad);
803 if (!do_balance && sectors > bad_sectors)
804 sectors = bad_sectors;
805 if (best_good_sectors > sectors)
806 best_good_sectors = sectors;
807 } else {
808 sector_t good_sectors =
809 first_bad - dev_sector;
810 if (good_sectors > best_good_sectors) {
811 best_good_sectors = good_sectors;
812 best_dist_slot = slot;
813 best_dist_rdev = rdev;
814 }
815 if (!do_balance)
816 /* Must read from here */
817 break;
818 }
819 continue;
820 } else
821 best_good_sectors = sectors;
822
823 if (!do_balance)
824 break;
825
826 nonrot = bdev_nonrot(rdev->bdev);
827 has_nonrot_disk |= nonrot;
828 pending = atomic_read(&rdev->nr_pending);
829 if (min_pending > pending && nonrot) {
830 min_pending = pending;
831 best_pending_slot = slot;
832 best_pending_rdev = rdev;
833 }
834
835 if (best_dist_slot >= 0)
836 /* At least 2 disks to choose from so failfast is OK */
837 set_bit(R10BIO_FailFast, &r10_bio->state);
838 /* This optimisation is debatable, and completely destroys
839 * sequential read speed for 'far copies' arrays. So only
840 * keep it for 'near' arrays, and review those later.
841 */
842 if (geo->near_copies > 1 && !pending)
843 new_distance = 0;
844
845 /* for far > 1 always use the lowest address */
846 else if (geo->far_copies > 1)
847 new_distance = r10_bio->devs[slot].addr;
848 else
849 new_distance = abs(r10_bio->devs[slot].addr -
850 conf->mirrors[disk].head_position);
851
852 if (new_distance < best_dist) {
853 best_dist = new_distance;
854 best_dist_slot = slot;
855 best_dist_rdev = rdev;
856 }
857 }
858 if (slot >= conf->copies) {
859 if (has_nonrot_disk) {
860 slot = best_pending_slot;
861 rdev = best_pending_rdev;
862 } else {
863 slot = best_dist_slot;
864 rdev = best_dist_rdev;
865 }
866 }
867
868 if (slot >= 0) {
869 atomic_inc(&rdev->nr_pending);
870 r10_bio->read_slot = slot;
871 } else
872 rdev = NULL;
873 rcu_read_unlock();
874 *max_sectors = best_good_sectors;
875
876 return rdev;
877 }
878
flush_pending_writes(struct r10conf * conf)879 static void flush_pending_writes(struct r10conf *conf)
880 {
881 /* Any writes that have been queued but are awaiting
882 * bitmap updates get flushed here.
883 */
884 spin_lock_irq(&conf->device_lock);
885
886 if (conf->pending_bio_list.head) {
887 struct blk_plug plug;
888 struct bio *bio;
889
890 bio = bio_list_get(&conf->pending_bio_list);
891 spin_unlock_irq(&conf->device_lock);
892
893 /*
894 * As this is called in a wait_event() loop (see freeze_array),
895 * current->state might be TASK_UNINTERRUPTIBLE which will
896 * cause a warning when we prepare to wait again. As it is
897 * rare that this path is taken, it is perfectly safe to force
898 * us to go around the wait_event() loop again, so the warning
899 * is a false-positive. Silence the warning by resetting
900 * thread state
901 */
902 __set_current_state(TASK_RUNNING);
903
904 blk_start_plug(&plug);
905 raid1_prepare_flush_writes(conf->mddev->bitmap);
906 wake_up(&conf->wait_barrier);
907
908 while (bio) { /* submit pending writes */
909 struct bio *next = bio->bi_next;
910
911 raid1_submit_write(bio);
912 bio = next;
913 cond_resched();
914 }
915 blk_finish_plug(&plug);
916 } else
917 spin_unlock_irq(&conf->device_lock);
918 }
919
920 /* Barriers....
921 * Sometimes we need to suspend IO while we do something else,
922 * either some resync/recovery, or reconfigure the array.
923 * To do this we raise a 'barrier'.
924 * The 'barrier' is a counter that can be raised multiple times
925 * to count how many activities are happening which preclude
926 * normal IO.
927 * We can only raise the barrier if there is no pending IO.
928 * i.e. if nr_pending == 0.
929 * We choose only to raise the barrier if no-one is waiting for the
930 * barrier to go down. This means that as soon as an IO request
931 * is ready, no other operations which require a barrier will start
932 * until the IO request has had a chance.
933 *
934 * So: regular IO calls 'wait_barrier'. When that returns there
935 * is no backgroup IO happening, It must arrange to call
936 * allow_barrier when it has finished its IO.
937 * backgroup IO calls must call raise_barrier. Once that returns
938 * there is no normal IO happeing. It must arrange to call
939 * lower_barrier when the particular background IO completes.
940 */
941
raise_barrier(struct r10conf * conf,int force)942 static void raise_barrier(struct r10conf *conf, int force)
943 {
944 write_seqlock_irq(&conf->resync_lock);
945
946 if (WARN_ON_ONCE(force && !conf->barrier))
947 force = false;
948
949 /* Wait until no block IO is waiting (unless 'force') */
950 wait_event_barrier(conf, force || !conf->nr_waiting);
951
952 /* block any new IO from starting */
953 WRITE_ONCE(conf->barrier, conf->barrier + 1);
954
955 /* Now wait for all pending IO to complete */
956 wait_event_barrier(conf, !atomic_read(&conf->nr_pending) &&
957 conf->barrier < RESYNC_DEPTH);
958
959 write_sequnlock_irq(&conf->resync_lock);
960 }
961
lower_barrier(struct r10conf * conf)962 static void lower_barrier(struct r10conf *conf)
963 {
964 unsigned long flags;
965
966 write_seqlock_irqsave(&conf->resync_lock, flags);
967 WRITE_ONCE(conf->barrier, conf->barrier - 1);
968 write_sequnlock_irqrestore(&conf->resync_lock, flags);
969 wake_up(&conf->wait_barrier);
970 }
971
stop_waiting_barrier(struct r10conf * conf)972 static bool stop_waiting_barrier(struct r10conf *conf)
973 {
974 struct bio_list *bio_list = current->bio_list;
975 struct md_thread *thread;
976
977 /* barrier is dropped */
978 if (!conf->barrier)
979 return true;
980
981 /*
982 * If there are already pending requests (preventing the barrier from
983 * rising completely), and the pre-process bio queue isn't empty, then
984 * don't wait, as we need to empty that queue to get the nr_pending
985 * count down.
986 */
987 if (atomic_read(&conf->nr_pending) && bio_list &&
988 (!bio_list_empty(&bio_list[0]) || !bio_list_empty(&bio_list[1])))
989 return true;
990
991 /* daemon thread must exist while handling io */
992 thread = rcu_dereference_protected(conf->mddev->thread, true);
993 /*
994 * move on if io is issued from raid10d(), nr_pending is not released
995 * from original io(see handle_read_error()). All raise barrier is
996 * blocked until this io is done.
997 */
998 if (thread->tsk == current) {
999 WARN_ON_ONCE(atomic_read(&conf->nr_pending) == 0);
1000 return true;
1001 }
1002
1003 return false;
1004 }
1005
wait_barrier_nolock(struct r10conf * conf)1006 static bool wait_barrier_nolock(struct r10conf *conf)
1007 {
1008 unsigned int seq = read_seqbegin(&conf->resync_lock);
1009
1010 if (READ_ONCE(conf->barrier))
1011 return false;
1012
1013 atomic_inc(&conf->nr_pending);
1014 if (!read_seqretry(&conf->resync_lock, seq))
1015 return true;
1016
1017 if (atomic_dec_and_test(&conf->nr_pending))
1018 wake_up_barrier(conf);
1019
1020 return false;
1021 }
1022
wait_barrier(struct r10conf * conf,bool nowait)1023 static bool wait_barrier(struct r10conf *conf, bool nowait)
1024 {
1025 bool ret = true;
1026
1027 if (wait_barrier_nolock(conf))
1028 return true;
1029
1030 write_seqlock_irq(&conf->resync_lock);
1031 if (conf->barrier) {
1032 /* Return false when nowait flag is set */
1033 if (nowait) {
1034 ret = false;
1035 } else {
1036 conf->nr_waiting++;
1037 raid10_log(conf->mddev, "wait barrier");
1038 wait_event_barrier(conf, stop_waiting_barrier(conf));
1039 conf->nr_waiting--;
1040 }
1041 if (!conf->nr_waiting)
1042 wake_up(&conf->wait_barrier);
1043 }
1044 /* Only increment nr_pending when we wait */
1045 if (ret)
1046 atomic_inc(&conf->nr_pending);
1047 write_sequnlock_irq(&conf->resync_lock);
1048 return ret;
1049 }
1050
allow_barrier(struct r10conf * conf)1051 static void allow_barrier(struct r10conf *conf)
1052 {
1053 if ((atomic_dec_and_test(&conf->nr_pending)) ||
1054 (conf->array_freeze_pending))
1055 wake_up_barrier(conf);
1056 }
1057
freeze_array(struct r10conf * conf,int extra)1058 static void freeze_array(struct r10conf *conf, int extra)
1059 {
1060 /* stop syncio and normal IO and wait for everything to
1061 * go quiet.
1062 * We increment barrier and nr_waiting, and then
1063 * wait until nr_pending match nr_queued+extra
1064 * This is called in the context of one normal IO request
1065 * that has failed. Thus any sync request that might be pending
1066 * will be blocked by nr_pending, and we need to wait for
1067 * pending IO requests to complete or be queued for re-try.
1068 * Thus the number queued (nr_queued) plus this request (extra)
1069 * must match the number of pending IOs (nr_pending) before
1070 * we continue.
1071 */
1072 write_seqlock_irq(&conf->resync_lock);
1073 conf->array_freeze_pending++;
1074 WRITE_ONCE(conf->barrier, conf->barrier + 1);
1075 conf->nr_waiting++;
1076 wait_event_barrier_cmd(conf, atomic_read(&conf->nr_pending) ==
1077 conf->nr_queued + extra, flush_pending_writes(conf));
1078 conf->array_freeze_pending--;
1079 write_sequnlock_irq(&conf->resync_lock);
1080 }
1081
unfreeze_array(struct r10conf * conf)1082 static void unfreeze_array(struct r10conf *conf)
1083 {
1084 /* reverse the effect of the freeze */
1085 write_seqlock_irq(&conf->resync_lock);
1086 WRITE_ONCE(conf->barrier, conf->barrier - 1);
1087 conf->nr_waiting--;
1088 wake_up(&conf->wait_barrier);
1089 write_sequnlock_irq(&conf->resync_lock);
1090 }
1091
choose_data_offset(struct r10bio * r10_bio,struct md_rdev * rdev)1092 static sector_t choose_data_offset(struct r10bio *r10_bio,
1093 struct md_rdev *rdev)
1094 {
1095 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1096 test_bit(R10BIO_Previous, &r10_bio->state))
1097 return rdev->data_offset;
1098 else
1099 return rdev->new_data_offset;
1100 }
1101
raid10_unplug(struct blk_plug_cb * cb,bool from_schedule)1102 static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1103 {
1104 struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb, cb);
1105 struct mddev *mddev = plug->cb.data;
1106 struct r10conf *conf = mddev->private;
1107 struct bio *bio;
1108
1109 if (from_schedule) {
1110 spin_lock_irq(&conf->device_lock);
1111 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1112 spin_unlock_irq(&conf->device_lock);
1113 wake_up_barrier(conf);
1114 md_wakeup_thread(mddev->thread);
1115 kfree(plug);
1116 return;
1117 }
1118
1119 /* we aren't scheduling, so we can do the write-out directly. */
1120 bio = bio_list_get(&plug->pending);
1121 raid1_prepare_flush_writes(mddev->bitmap);
1122 wake_up_barrier(conf);
1123
1124 while (bio) { /* submit pending writes */
1125 struct bio *next = bio->bi_next;
1126
1127 raid1_submit_write(bio);
1128 bio = next;
1129 cond_resched();
1130 }
1131 kfree(plug);
1132 }
1133
1134 /*
1135 * 1. Register the new request and wait if the reconstruction thread has put
1136 * up a bar for new requests. Continue immediately if no resync is active
1137 * currently.
1138 * 2. If IO spans the reshape position. Need to wait for reshape to pass.
1139 */
regular_request_wait(struct mddev * mddev,struct r10conf * conf,struct bio * bio,sector_t sectors)1140 static bool regular_request_wait(struct mddev *mddev, struct r10conf *conf,
1141 struct bio *bio, sector_t sectors)
1142 {
1143 /* Bail out if REQ_NOWAIT is set for the bio */
1144 if (!wait_barrier(conf, bio->bi_opf & REQ_NOWAIT)) {
1145 bio_wouldblock_error(bio);
1146 return false;
1147 }
1148 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1149 bio->bi_iter.bi_sector < conf->reshape_progress &&
1150 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
1151 allow_barrier(conf);
1152 if (bio->bi_opf & REQ_NOWAIT) {
1153 bio_wouldblock_error(bio);
1154 return false;
1155 }
1156 raid10_log(conf->mddev, "wait reshape");
1157 wait_event(conf->wait_barrier,
1158 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1159 conf->reshape_progress >= bio->bi_iter.bi_sector +
1160 sectors);
1161 wait_barrier(conf, false);
1162 }
1163 return true;
1164 }
1165
raid10_read_request(struct mddev * mddev,struct bio * bio,struct r10bio * r10_bio,bool io_accounting)1166 static void raid10_read_request(struct mddev *mddev, struct bio *bio,
1167 struct r10bio *r10_bio, bool io_accounting)
1168 {
1169 struct r10conf *conf = mddev->private;
1170 struct bio *read_bio;
1171 const enum req_op op = bio_op(bio);
1172 const blk_opf_t do_sync = bio->bi_opf & REQ_SYNC;
1173 int max_sectors;
1174 struct md_rdev *rdev;
1175 char b[BDEVNAME_SIZE];
1176 int slot = r10_bio->read_slot;
1177 struct md_rdev *err_rdev = NULL;
1178 gfp_t gfp = GFP_NOIO;
1179
1180 if (slot >= 0 && r10_bio->devs[slot].rdev) {
1181 /*
1182 * This is an error retry, but we cannot
1183 * safely dereference the rdev in the r10_bio,
1184 * we must use the one in conf.
1185 * If it has already been disconnected (unlikely)
1186 * we lose the device name in error messages.
1187 */
1188 int disk;
1189 /*
1190 * As we are blocking raid10, it is a little safer to
1191 * use __GFP_HIGH.
1192 */
1193 gfp = GFP_NOIO | __GFP_HIGH;
1194
1195 rcu_read_lock();
1196 disk = r10_bio->devs[slot].devnum;
1197 err_rdev = rcu_dereference(conf->mirrors[disk].rdev);
1198 if (err_rdev)
1199 snprintf(b, sizeof(b), "%pg", err_rdev->bdev);
1200 else {
1201 strcpy(b, "???");
1202 /* This never gets dereferenced */
1203 err_rdev = r10_bio->devs[slot].rdev;
1204 }
1205 rcu_read_unlock();
1206 }
1207
1208 if (!regular_request_wait(mddev, conf, bio, r10_bio->sectors))
1209 return;
1210 rdev = read_balance(conf, r10_bio, &max_sectors);
1211 if (!rdev) {
1212 if (err_rdev) {
1213 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
1214 mdname(mddev), b,
1215 (unsigned long long)r10_bio->sector);
1216 }
1217 raid_end_bio_io(r10_bio);
1218 return;
1219 }
1220 if (err_rdev)
1221 pr_err_ratelimited("md/raid10:%s: %pg: redirecting sector %llu to another mirror\n",
1222 mdname(mddev),
1223 rdev->bdev,
1224 (unsigned long long)r10_bio->sector);
1225 if (max_sectors < bio_sectors(bio)) {
1226 struct bio *split = bio_split(bio, max_sectors,
1227 gfp, &conf->bio_split);
1228 bio_chain(split, bio);
1229 allow_barrier(conf);
1230 submit_bio_noacct(bio);
1231 wait_barrier(conf, false);
1232 bio = split;
1233 r10_bio->master_bio = bio;
1234 r10_bio->sectors = max_sectors;
1235 }
1236 slot = r10_bio->read_slot;
1237
1238 if (io_accounting) {
1239 md_account_bio(mddev, &bio);
1240 r10_bio->master_bio = bio;
1241 }
1242 read_bio = bio_alloc_clone(rdev->bdev, bio, gfp, &mddev->bio_set);
1243
1244 r10_bio->devs[slot].bio = read_bio;
1245 r10_bio->devs[slot].rdev = rdev;
1246
1247 read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr +
1248 choose_data_offset(r10_bio, rdev);
1249 read_bio->bi_end_io = raid10_end_read_request;
1250 read_bio->bi_opf = op | do_sync;
1251 if (test_bit(FailFast, &rdev->flags) &&
1252 test_bit(R10BIO_FailFast, &r10_bio->state))
1253 read_bio->bi_opf |= MD_FAILFAST;
1254 read_bio->bi_private = r10_bio;
1255
1256 if (mddev->gendisk)
1257 trace_block_bio_remap(read_bio, disk_devt(mddev->gendisk),
1258 r10_bio->sector);
1259 submit_bio_noacct(read_bio);
1260 return;
1261 }
1262
raid10_write_one_disk(struct mddev * mddev,struct r10bio * r10_bio,struct bio * bio,bool replacement,int n_copy)1263 static void raid10_write_one_disk(struct mddev *mddev, struct r10bio *r10_bio,
1264 struct bio *bio, bool replacement,
1265 int n_copy)
1266 {
1267 const enum req_op op = bio_op(bio);
1268 const blk_opf_t do_sync = bio->bi_opf & REQ_SYNC;
1269 const blk_opf_t do_fua = bio->bi_opf & REQ_FUA;
1270 unsigned long flags;
1271 struct r10conf *conf = mddev->private;
1272 struct md_rdev *rdev;
1273 int devnum = r10_bio->devs[n_copy].devnum;
1274 struct bio *mbio;
1275
1276 if (replacement) {
1277 rdev = conf->mirrors[devnum].replacement;
1278 if (rdev == NULL) {
1279 /* Replacement just got moved to main 'rdev' */
1280 smp_mb();
1281 rdev = conf->mirrors[devnum].rdev;
1282 }
1283 } else
1284 rdev = conf->mirrors[devnum].rdev;
1285
1286 mbio = bio_alloc_clone(rdev->bdev, bio, GFP_NOIO, &mddev->bio_set);
1287 if (replacement)
1288 r10_bio->devs[n_copy].repl_bio = mbio;
1289 else
1290 r10_bio->devs[n_copy].bio = mbio;
1291
1292 mbio->bi_iter.bi_sector = (r10_bio->devs[n_copy].addr +
1293 choose_data_offset(r10_bio, rdev));
1294 mbio->bi_end_io = raid10_end_write_request;
1295 mbio->bi_opf = op | do_sync | do_fua;
1296 if (!replacement && test_bit(FailFast,
1297 &conf->mirrors[devnum].rdev->flags)
1298 && enough(conf, devnum))
1299 mbio->bi_opf |= MD_FAILFAST;
1300 mbio->bi_private = r10_bio;
1301
1302 if (conf->mddev->gendisk)
1303 trace_block_bio_remap(mbio, disk_devt(conf->mddev->gendisk),
1304 r10_bio->sector);
1305 /* flush_pending_writes() needs access to the rdev so...*/
1306 mbio->bi_bdev = (void *)rdev;
1307
1308 atomic_inc(&r10_bio->remaining);
1309
1310 if (!raid1_add_bio_to_plug(mddev, mbio, raid10_unplug, conf->copies)) {
1311 spin_lock_irqsave(&conf->device_lock, flags);
1312 bio_list_add(&conf->pending_bio_list, mbio);
1313 spin_unlock_irqrestore(&conf->device_lock, flags);
1314 md_wakeup_thread(mddev->thread);
1315 }
1316 }
1317
dereference_rdev_and_rrdev(struct raid10_info * mirror,struct md_rdev ** prrdev)1318 static struct md_rdev *dereference_rdev_and_rrdev(struct raid10_info *mirror,
1319 struct md_rdev **prrdev)
1320 {
1321 struct md_rdev *rdev, *rrdev;
1322
1323 rrdev = rcu_dereference(mirror->replacement);
1324 /*
1325 * Read replacement first to prevent reading both rdev and
1326 * replacement as NULL during replacement replace rdev.
1327 */
1328 smp_mb();
1329 rdev = rcu_dereference(mirror->rdev);
1330 if (rdev == rrdev)
1331 rrdev = NULL;
1332
1333 *prrdev = rrdev;
1334 return rdev;
1335 }
1336
wait_blocked_dev(struct mddev * mddev,struct r10bio * r10_bio)1337 static void wait_blocked_dev(struct mddev *mddev, struct r10bio *r10_bio)
1338 {
1339 int i;
1340 struct r10conf *conf = mddev->private;
1341 struct md_rdev *blocked_rdev;
1342
1343 retry_wait:
1344 blocked_rdev = NULL;
1345 rcu_read_lock();
1346 for (i = 0; i < conf->copies; i++) {
1347 struct md_rdev *rdev, *rrdev;
1348
1349 rdev = dereference_rdev_and_rrdev(&conf->mirrors[i], &rrdev);
1350 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1351 atomic_inc(&rdev->nr_pending);
1352 blocked_rdev = rdev;
1353 break;
1354 }
1355 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1356 atomic_inc(&rrdev->nr_pending);
1357 blocked_rdev = rrdev;
1358 break;
1359 }
1360
1361 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
1362 sector_t first_bad;
1363 sector_t dev_sector = r10_bio->devs[i].addr;
1364 int bad_sectors;
1365 int is_bad;
1366
1367 /*
1368 * Discard request doesn't care the write result
1369 * so it doesn't need to wait blocked disk here.
1370 */
1371 if (!r10_bio->sectors)
1372 continue;
1373
1374 is_bad = is_badblock(rdev, dev_sector, r10_bio->sectors,
1375 &first_bad, &bad_sectors);
1376 if (is_bad < 0) {
1377 /*
1378 * Mustn't write here until the bad block
1379 * is acknowledged
1380 */
1381 atomic_inc(&rdev->nr_pending);
1382 set_bit(BlockedBadBlocks, &rdev->flags);
1383 blocked_rdev = rdev;
1384 break;
1385 }
1386 }
1387 }
1388 rcu_read_unlock();
1389
1390 if (unlikely(blocked_rdev)) {
1391 /* Have to wait for this device to get unblocked, then retry */
1392 allow_barrier(conf);
1393 raid10_log(conf->mddev, "%s wait rdev %d blocked",
1394 __func__, blocked_rdev->raid_disk);
1395 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1396 wait_barrier(conf, false);
1397 goto retry_wait;
1398 }
1399 }
1400
raid10_write_request(struct mddev * mddev,struct bio * bio,struct r10bio * r10_bio)1401 static void raid10_write_request(struct mddev *mddev, struct bio *bio,
1402 struct r10bio *r10_bio)
1403 {
1404 struct r10conf *conf = mddev->private;
1405 int i;
1406 sector_t sectors;
1407 int max_sectors;
1408
1409 if ((mddev_is_clustered(mddev) &&
1410 md_cluster_ops->area_resyncing(mddev, WRITE,
1411 bio->bi_iter.bi_sector,
1412 bio_end_sector(bio)))) {
1413 DEFINE_WAIT(w);
1414 /* Bail out if REQ_NOWAIT is set for the bio */
1415 if (bio->bi_opf & REQ_NOWAIT) {
1416 bio_wouldblock_error(bio);
1417 return;
1418 }
1419 for (;;) {
1420 prepare_to_wait(&conf->wait_barrier,
1421 &w, TASK_IDLE);
1422 if (!md_cluster_ops->area_resyncing(mddev, WRITE,
1423 bio->bi_iter.bi_sector, bio_end_sector(bio)))
1424 break;
1425 schedule();
1426 }
1427 finish_wait(&conf->wait_barrier, &w);
1428 }
1429
1430 sectors = r10_bio->sectors;
1431 if (!regular_request_wait(mddev, conf, bio, sectors))
1432 return;
1433 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1434 (mddev->reshape_backwards
1435 ? (bio->bi_iter.bi_sector < conf->reshape_safe &&
1436 bio->bi_iter.bi_sector + sectors > conf->reshape_progress)
1437 : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe &&
1438 bio->bi_iter.bi_sector < conf->reshape_progress))) {
1439 /* Need to update reshape_position in metadata */
1440 mddev->reshape_position = conf->reshape_progress;
1441 set_mask_bits(&mddev->sb_flags, 0,
1442 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
1443 md_wakeup_thread(mddev->thread);
1444 if (bio->bi_opf & REQ_NOWAIT) {
1445 allow_barrier(conf);
1446 bio_wouldblock_error(bio);
1447 return;
1448 }
1449 raid10_log(conf->mddev, "wait reshape metadata");
1450 wait_event(mddev->sb_wait,
1451 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
1452
1453 conf->reshape_safe = mddev->reshape_position;
1454 }
1455
1456 /* first select target devices under rcu_lock and
1457 * inc refcount on their rdev. Record them by setting
1458 * bios[x] to bio
1459 * If there are known/acknowledged bad blocks on any device
1460 * on which we have seen a write error, we want to avoid
1461 * writing to those blocks. This potentially requires several
1462 * writes to write around the bad blocks. Each set of writes
1463 * gets its own r10_bio with a set of bios attached.
1464 */
1465
1466 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
1467 raid10_find_phys(conf, r10_bio);
1468
1469 wait_blocked_dev(mddev, r10_bio);
1470
1471 rcu_read_lock();
1472 max_sectors = r10_bio->sectors;
1473
1474 for (i = 0; i < conf->copies; i++) {
1475 int d = r10_bio->devs[i].devnum;
1476 struct md_rdev *rdev, *rrdev;
1477
1478 rdev = dereference_rdev_and_rrdev(&conf->mirrors[d], &rrdev);
1479 if (rdev && (test_bit(Faulty, &rdev->flags)))
1480 rdev = NULL;
1481 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
1482 rrdev = NULL;
1483
1484 r10_bio->devs[i].bio = NULL;
1485 r10_bio->devs[i].repl_bio = NULL;
1486
1487 if (!rdev && !rrdev)
1488 continue;
1489 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
1490 sector_t first_bad;
1491 sector_t dev_sector = r10_bio->devs[i].addr;
1492 int bad_sectors;
1493 int is_bad;
1494
1495 is_bad = is_badblock(rdev, dev_sector, max_sectors,
1496 &first_bad, &bad_sectors);
1497 if (is_bad && first_bad <= dev_sector) {
1498 /* Cannot write here at all */
1499 bad_sectors -= (dev_sector - first_bad);
1500 if (bad_sectors < max_sectors)
1501 /* Mustn't write more than bad_sectors
1502 * to other devices yet
1503 */
1504 max_sectors = bad_sectors;
1505 continue;
1506 }
1507 if (is_bad) {
1508 int good_sectors = first_bad - dev_sector;
1509 if (good_sectors < max_sectors)
1510 max_sectors = good_sectors;
1511 }
1512 }
1513 if (rdev) {
1514 r10_bio->devs[i].bio = bio;
1515 atomic_inc(&rdev->nr_pending);
1516 }
1517 if (rrdev) {
1518 r10_bio->devs[i].repl_bio = bio;
1519 atomic_inc(&rrdev->nr_pending);
1520 }
1521 }
1522 rcu_read_unlock();
1523
1524 if (max_sectors < r10_bio->sectors)
1525 r10_bio->sectors = max_sectors;
1526
1527 if (r10_bio->sectors < bio_sectors(bio)) {
1528 struct bio *split = bio_split(bio, r10_bio->sectors,
1529 GFP_NOIO, &conf->bio_split);
1530 bio_chain(split, bio);
1531 allow_barrier(conf);
1532 submit_bio_noacct(bio);
1533 wait_barrier(conf, false);
1534 bio = split;
1535 r10_bio->master_bio = bio;
1536 }
1537
1538 md_account_bio(mddev, &bio);
1539 r10_bio->master_bio = bio;
1540 atomic_set(&r10_bio->remaining, 1);
1541
1542 for (i = 0; i < conf->copies; i++) {
1543 if (r10_bio->devs[i].bio)
1544 raid10_write_one_disk(mddev, r10_bio, bio, false, i);
1545 if (r10_bio->devs[i].repl_bio)
1546 raid10_write_one_disk(mddev, r10_bio, bio, true, i);
1547 }
1548 one_write_done(r10_bio);
1549 }
1550
__make_request(struct mddev * mddev,struct bio * bio,int sectors)1551 static void __make_request(struct mddev *mddev, struct bio *bio, int sectors)
1552 {
1553 struct r10conf *conf = mddev->private;
1554 struct r10bio *r10_bio;
1555
1556 r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO);
1557
1558 r10_bio->master_bio = bio;
1559 r10_bio->sectors = sectors;
1560
1561 r10_bio->mddev = mddev;
1562 r10_bio->sector = bio->bi_iter.bi_sector;
1563 r10_bio->state = 0;
1564 r10_bio->read_slot = -1;
1565 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) *
1566 conf->geo.raid_disks);
1567
1568 if (bio_data_dir(bio) == READ)
1569 raid10_read_request(mddev, bio, r10_bio, true);
1570 else
1571 raid10_write_request(mddev, bio, r10_bio);
1572 }
1573
raid_end_discard_bio(struct r10bio * r10bio)1574 static void raid_end_discard_bio(struct r10bio *r10bio)
1575 {
1576 struct r10conf *conf = r10bio->mddev->private;
1577 struct r10bio *first_r10bio;
1578
1579 while (atomic_dec_and_test(&r10bio->remaining)) {
1580
1581 allow_barrier(conf);
1582
1583 if (!test_bit(R10BIO_Discard, &r10bio->state)) {
1584 first_r10bio = (struct r10bio *)r10bio->master_bio;
1585 free_r10bio(r10bio);
1586 r10bio = first_r10bio;
1587 } else {
1588 md_write_end(r10bio->mddev);
1589 bio_endio(r10bio->master_bio);
1590 free_r10bio(r10bio);
1591 break;
1592 }
1593 }
1594 }
1595
raid10_end_discard_request(struct bio * bio)1596 static void raid10_end_discard_request(struct bio *bio)
1597 {
1598 struct r10bio *r10_bio = bio->bi_private;
1599 struct r10conf *conf = r10_bio->mddev->private;
1600 struct md_rdev *rdev = NULL;
1601 int dev;
1602 int slot, repl;
1603
1604 /*
1605 * We don't care the return value of discard bio
1606 */
1607 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
1608 set_bit(R10BIO_Uptodate, &r10_bio->state);
1609
1610 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1611 if (repl)
1612 rdev = conf->mirrors[dev].replacement;
1613 if (!rdev) {
1614 /*
1615 * raid10_remove_disk uses smp_mb to make sure rdev is set to
1616 * replacement before setting replacement to NULL. It can read
1617 * rdev first without barrier protect even replacement is NULL
1618 */
1619 smp_rmb();
1620 rdev = conf->mirrors[dev].rdev;
1621 }
1622
1623 raid_end_discard_bio(r10_bio);
1624 rdev_dec_pending(rdev, conf->mddev);
1625 }
1626
1627 /*
1628 * There are some limitations to handle discard bio
1629 * 1st, the discard size is bigger than stripe_size*2.
1630 * 2st, if the discard bio spans reshape progress, we use the old way to
1631 * handle discard bio
1632 */
raid10_handle_discard(struct mddev * mddev,struct bio * bio)1633 static int raid10_handle_discard(struct mddev *mddev, struct bio *bio)
1634 {
1635 struct r10conf *conf = mddev->private;
1636 struct geom *geo = &conf->geo;
1637 int far_copies = geo->far_copies;
1638 bool first_copy = true;
1639 struct r10bio *r10_bio, *first_r10bio;
1640 struct bio *split;
1641 int disk;
1642 sector_t chunk;
1643 unsigned int stripe_size;
1644 unsigned int stripe_data_disks;
1645 sector_t split_size;
1646 sector_t bio_start, bio_end;
1647 sector_t first_stripe_index, last_stripe_index;
1648 sector_t start_disk_offset;
1649 unsigned int start_disk_index;
1650 sector_t end_disk_offset;
1651 unsigned int end_disk_index;
1652 unsigned int remainder;
1653
1654 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1655 return -EAGAIN;
1656
1657 if (WARN_ON_ONCE(bio->bi_opf & REQ_NOWAIT)) {
1658 bio_wouldblock_error(bio);
1659 return 0;
1660 }
1661 wait_barrier(conf, false);
1662
1663 /*
1664 * Check reshape again to avoid reshape happens after checking
1665 * MD_RECOVERY_RESHAPE and before wait_barrier
1666 */
1667 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1668 goto out;
1669
1670 if (geo->near_copies)
1671 stripe_data_disks = geo->raid_disks / geo->near_copies +
1672 geo->raid_disks % geo->near_copies;
1673 else
1674 stripe_data_disks = geo->raid_disks;
1675
1676 stripe_size = stripe_data_disks << geo->chunk_shift;
1677
1678 bio_start = bio->bi_iter.bi_sector;
1679 bio_end = bio_end_sector(bio);
1680
1681 /*
1682 * Maybe one discard bio is smaller than strip size or across one
1683 * stripe and discard region is larger than one stripe size. For far
1684 * offset layout, if the discard region is not aligned with stripe
1685 * size, there is hole when we submit discard bio to member disk.
1686 * For simplicity, we only handle discard bio which discard region
1687 * is bigger than stripe_size * 2
1688 */
1689 if (bio_sectors(bio) < stripe_size*2)
1690 goto out;
1691
1692 /*
1693 * Keep bio aligned with strip size.
1694 */
1695 div_u64_rem(bio_start, stripe_size, &remainder);
1696 if (remainder) {
1697 split_size = stripe_size - remainder;
1698 split = bio_split(bio, split_size, GFP_NOIO, &conf->bio_split);
1699 bio_chain(split, bio);
1700 allow_barrier(conf);
1701 /* Resend the fist split part */
1702 submit_bio_noacct(split);
1703 wait_barrier(conf, false);
1704 }
1705 div_u64_rem(bio_end, stripe_size, &remainder);
1706 if (remainder) {
1707 split_size = bio_sectors(bio) - remainder;
1708 split = bio_split(bio, split_size, GFP_NOIO, &conf->bio_split);
1709 bio_chain(split, bio);
1710 allow_barrier(conf);
1711 /* Resend the second split part */
1712 submit_bio_noacct(bio);
1713 bio = split;
1714 wait_barrier(conf, false);
1715 }
1716
1717 bio_start = bio->bi_iter.bi_sector;
1718 bio_end = bio_end_sector(bio);
1719
1720 /*
1721 * Raid10 uses chunk as the unit to store data. It's similar like raid0.
1722 * One stripe contains the chunks from all member disk (one chunk from
1723 * one disk at the same HBA address). For layout detail, see 'man md 4'
1724 */
1725 chunk = bio_start >> geo->chunk_shift;
1726 chunk *= geo->near_copies;
1727 first_stripe_index = chunk;
1728 start_disk_index = sector_div(first_stripe_index, geo->raid_disks);
1729 if (geo->far_offset)
1730 first_stripe_index *= geo->far_copies;
1731 start_disk_offset = (bio_start & geo->chunk_mask) +
1732 (first_stripe_index << geo->chunk_shift);
1733
1734 chunk = bio_end >> geo->chunk_shift;
1735 chunk *= geo->near_copies;
1736 last_stripe_index = chunk;
1737 end_disk_index = sector_div(last_stripe_index, geo->raid_disks);
1738 if (geo->far_offset)
1739 last_stripe_index *= geo->far_copies;
1740 end_disk_offset = (bio_end & geo->chunk_mask) +
1741 (last_stripe_index << geo->chunk_shift);
1742
1743 retry_discard:
1744 r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO);
1745 r10_bio->mddev = mddev;
1746 r10_bio->state = 0;
1747 r10_bio->sectors = 0;
1748 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) * geo->raid_disks);
1749 wait_blocked_dev(mddev, r10_bio);
1750
1751 /*
1752 * For far layout it needs more than one r10bio to cover all regions.
1753 * Inspired by raid10_sync_request, we can use the first r10bio->master_bio
1754 * to record the discard bio. Other r10bio->master_bio record the first
1755 * r10bio. The first r10bio only release after all other r10bios finish.
1756 * The discard bio returns only first r10bio finishes
1757 */
1758 if (first_copy) {
1759 r10_bio->master_bio = bio;
1760 set_bit(R10BIO_Discard, &r10_bio->state);
1761 first_copy = false;
1762 first_r10bio = r10_bio;
1763 } else
1764 r10_bio->master_bio = (struct bio *)first_r10bio;
1765
1766 /*
1767 * first select target devices under rcu_lock and
1768 * inc refcount on their rdev. Record them by setting
1769 * bios[x] to bio
1770 */
1771 rcu_read_lock();
1772 for (disk = 0; disk < geo->raid_disks; disk++) {
1773 struct md_rdev *rdev, *rrdev;
1774
1775 rdev = dereference_rdev_and_rrdev(&conf->mirrors[disk], &rrdev);
1776 r10_bio->devs[disk].bio = NULL;
1777 r10_bio->devs[disk].repl_bio = NULL;
1778
1779 if (rdev && (test_bit(Faulty, &rdev->flags)))
1780 rdev = NULL;
1781 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
1782 rrdev = NULL;
1783 if (!rdev && !rrdev)
1784 continue;
1785
1786 if (rdev) {
1787 r10_bio->devs[disk].bio = bio;
1788 atomic_inc(&rdev->nr_pending);
1789 }
1790 if (rrdev) {
1791 r10_bio->devs[disk].repl_bio = bio;
1792 atomic_inc(&rrdev->nr_pending);
1793 }
1794 }
1795 rcu_read_unlock();
1796
1797 atomic_set(&r10_bio->remaining, 1);
1798 for (disk = 0; disk < geo->raid_disks; disk++) {
1799 sector_t dev_start, dev_end;
1800 struct bio *mbio, *rbio = NULL;
1801
1802 /*
1803 * Now start to calculate the start and end address for each disk.
1804 * The space between dev_start and dev_end is the discard region.
1805 *
1806 * For dev_start, it needs to consider three conditions:
1807 * 1st, the disk is before start_disk, you can imagine the disk in
1808 * the next stripe. So the dev_start is the start address of next
1809 * stripe.
1810 * 2st, the disk is after start_disk, it means the disk is at the
1811 * same stripe of first disk
1812 * 3st, the first disk itself, we can use start_disk_offset directly
1813 */
1814 if (disk < start_disk_index)
1815 dev_start = (first_stripe_index + 1) * mddev->chunk_sectors;
1816 else if (disk > start_disk_index)
1817 dev_start = first_stripe_index * mddev->chunk_sectors;
1818 else
1819 dev_start = start_disk_offset;
1820
1821 if (disk < end_disk_index)
1822 dev_end = (last_stripe_index + 1) * mddev->chunk_sectors;
1823 else if (disk > end_disk_index)
1824 dev_end = last_stripe_index * mddev->chunk_sectors;
1825 else
1826 dev_end = end_disk_offset;
1827
1828 /*
1829 * It only handles discard bio which size is >= stripe size, so
1830 * dev_end > dev_start all the time.
1831 * It doesn't need to use rcu lock to get rdev here. We already
1832 * add rdev->nr_pending in the first loop.
1833 */
1834 if (r10_bio->devs[disk].bio) {
1835 struct md_rdev *rdev = conf->mirrors[disk].rdev;
1836 mbio = bio_alloc_clone(bio->bi_bdev, bio, GFP_NOIO,
1837 &mddev->bio_set);
1838 mbio->bi_end_io = raid10_end_discard_request;
1839 mbio->bi_private = r10_bio;
1840 r10_bio->devs[disk].bio = mbio;
1841 r10_bio->devs[disk].devnum = disk;
1842 atomic_inc(&r10_bio->remaining);
1843 md_submit_discard_bio(mddev, rdev, mbio,
1844 dev_start + choose_data_offset(r10_bio, rdev),
1845 dev_end - dev_start);
1846 bio_endio(mbio);
1847 }
1848 if (r10_bio->devs[disk].repl_bio) {
1849 struct md_rdev *rrdev = conf->mirrors[disk].replacement;
1850 rbio = bio_alloc_clone(bio->bi_bdev, bio, GFP_NOIO,
1851 &mddev->bio_set);
1852 rbio->bi_end_io = raid10_end_discard_request;
1853 rbio->bi_private = r10_bio;
1854 r10_bio->devs[disk].repl_bio = rbio;
1855 r10_bio->devs[disk].devnum = disk;
1856 atomic_inc(&r10_bio->remaining);
1857 md_submit_discard_bio(mddev, rrdev, rbio,
1858 dev_start + choose_data_offset(r10_bio, rrdev),
1859 dev_end - dev_start);
1860 bio_endio(rbio);
1861 }
1862 }
1863
1864 if (!geo->far_offset && --far_copies) {
1865 first_stripe_index += geo->stride >> geo->chunk_shift;
1866 start_disk_offset += geo->stride;
1867 last_stripe_index += geo->stride >> geo->chunk_shift;
1868 end_disk_offset += geo->stride;
1869 atomic_inc(&first_r10bio->remaining);
1870 raid_end_discard_bio(r10_bio);
1871 wait_barrier(conf, false);
1872 goto retry_discard;
1873 }
1874
1875 raid_end_discard_bio(r10_bio);
1876
1877 return 0;
1878 out:
1879 allow_barrier(conf);
1880 return -EAGAIN;
1881 }
1882
raid10_make_request(struct mddev * mddev,struct bio * bio)1883 static bool raid10_make_request(struct mddev *mddev, struct bio *bio)
1884 {
1885 struct r10conf *conf = mddev->private;
1886 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
1887 int chunk_sects = chunk_mask + 1;
1888 int sectors = bio_sectors(bio);
1889
1890 if (unlikely(bio->bi_opf & REQ_PREFLUSH)
1891 && md_flush_request(mddev, bio))
1892 return true;
1893
1894 if (!md_write_start(mddev, bio))
1895 return false;
1896
1897 if (unlikely(bio_op(bio) == REQ_OP_DISCARD))
1898 if (!raid10_handle_discard(mddev, bio))
1899 return true;
1900
1901 /*
1902 * If this request crosses a chunk boundary, we need to split
1903 * it.
1904 */
1905 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
1906 sectors > chunk_sects
1907 && (conf->geo.near_copies < conf->geo.raid_disks
1908 || conf->prev.near_copies <
1909 conf->prev.raid_disks)))
1910 sectors = chunk_sects -
1911 (bio->bi_iter.bi_sector &
1912 (chunk_sects - 1));
1913 __make_request(mddev, bio, sectors);
1914
1915 /* In case raid10d snuck in to freeze_array */
1916 wake_up_barrier(conf);
1917 return true;
1918 }
1919
raid10_status(struct seq_file * seq,struct mddev * mddev)1920 static void raid10_status(struct seq_file *seq, struct mddev *mddev)
1921 {
1922 struct r10conf *conf = mddev->private;
1923 int i;
1924
1925 if (conf->geo.near_copies < conf->geo.raid_disks)
1926 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
1927 if (conf->geo.near_copies > 1)
1928 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1929 if (conf->geo.far_copies > 1) {
1930 if (conf->geo.far_offset)
1931 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
1932 else
1933 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
1934 if (conf->geo.far_set_size != conf->geo.raid_disks)
1935 seq_printf(seq, " %d devices per set", conf->geo.far_set_size);
1936 }
1937 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1938 conf->geo.raid_disks - mddev->degraded);
1939 rcu_read_lock();
1940 for (i = 0; i < conf->geo.raid_disks; i++) {
1941 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1942 seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1943 }
1944 rcu_read_unlock();
1945 seq_printf(seq, "]");
1946 }
1947
1948 /* check if there are enough drives for
1949 * every block to appear on atleast one.
1950 * Don't consider the device numbered 'ignore'
1951 * as we might be about to remove it.
1952 */
_enough(struct r10conf * conf,int previous,int ignore)1953 static int _enough(struct r10conf *conf, int previous, int ignore)
1954 {
1955 int first = 0;
1956 int has_enough = 0;
1957 int disks, ncopies;
1958 if (previous) {
1959 disks = conf->prev.raid_disks;
1960 ncopies = conf->prev.near_copies;
1961 } else {
1962 disks = conf->geo.raid_disks;
1963 ncopies = conf->geo.near_copies;
1964 }
1965
1966 rcu_read_lock();
1967 do {
1968 int n = conf->copies;
1969 int cnt = 0;
1970 int this = first;
1971 while (n--) {
1972 struct md_rdev *rdev;
1973 if (this != ignore &&
1974 (rdev = rcu_dereference(conf->mirrors[this].rdev)) &&
1975 test_bit(In_sync, &rdev->flags))
1976 cnt++;
1977 this = (this+1) % disks;
1978 }
1979 if (cnt == 0)
1980 goto out;
1981 first = (first + ncopies) % disks;
1982 } while (first != 0);
1983 has_enough = 1;
1984 out:
1985 rcu_read_unlock();
1986 return has_enough;
1987 }
1988
enough(struct r10conf * conf,int ignore)1989 static int enough(struct r10conf *conf, int ignore)
1990 {
1991 /* when calling 'enough', both 'prev' and 'geo' must
1992 * be stable.
1993 * This is ensured if ->reconfig_mutex or ->device_lock
1994 * is held.
1995 */
1996 return _enough(conf, 0, ignore) &&
1997 _enough(conf, 1, ignore);
1998 }
1999
2000 /**
2001 * raid10_error() - RAID10 error handler.
2002 * @mddev: affected md device.
2003 * @rdev: member device to fail.
2004 *
2005 * The routine acknowledges &rdev failure and determines new @mddev state.
2006 * If it failed, then:
2007 * - &MD_BROKEN flag is set in &mddev->flags.
2008 * Otherwise, it must be degraded:
2009 * - recovery is interrupted.
2010 * - &mddev->degraded is bumped.
2011 *
2012 * @rdev is marked as &Faulty excluding case when array is failed and
2013 * &mddev->fail_last_dev is off.
2014 */
raid10_error(struct mddev * mddev,struct md_rdev * rdev)2015 static void raid10_error(struct mddev *mddev, struct md_rdev *rdev)
2016 {
2017 struct r10conf *conf = mddev->private;
2018 unsigned long flags;
2019
2020 spin_lock_irqsave(&conf->device_lock, flags);
2021
2022 if (test_bit(In_sync, &rdev->flags) && !enough(conf, rdev->raid_disk)) {
2023 set_bit(MD_BROKEN, &mddev->flags);
2024
2025 if (!mddev->fail_last_dev) {
2026 spin_unlock_irqrestore(&conf->device_lock, flags);
2027 return;
2028 }
2029 }
2030 if (test_and_clear_bit(In_sync, &rdev->flags))
2031 mddev->degraded++;
2032
2033 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2034 set_bit(Blocked, &rdev->flags);
2035 set_bit(Faulty, &rdev->flags);
2036 set_mask_bits(&mddev->sb_flags, 0,
2037 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
2038 spin_unlock_irqrestore(&conf->device_lock, flags);
2039 pr_crit("md/raid10:%s: Disk failure on %pg, disabling device.\n"
2040 "md/raid10:%s: Operation continuing on %d devices.\n",
2041 mdname(mddev), rdev->bdev,
2042 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
2043 }
2044
print_conf(struct r10conf * conf)2045 static void print_conf(struct r10conf *conf)
2046 {
2047 int i;
2048 struct md_rdev *rdev;
2049
2050 pr_debug("RAID10 conf printout:\n");
2051 if (!conf) {
2052 pr_debug("(!conf)\n");
2053 return;
2054 }
2055 pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
2056 conf->geo.raid_disks);
2057
2058 /* This is only called with ->reconfix_mutex held, so
2059 * rcu protection of rdev is not needed */
2060 for (i = 0; i < conf->geo.raid_disks; i++) {
2061 rdev = conf->mirrors[i].rdev;
2062 if (rdev)
2063 pr_debug(" disk %d, wo:%d, o:%d, dev:%pg\n",
2064 i, !test_bit(In_sync, &rdev->flags),
2065 !test_bit(Faulty, &rdev->flags),
2066 rdev->bdev);
2067 }
2068 }
2069
close_sync(struct r10conf * conf)2070 static void close_sync(struct r10conf *conf)
2071 {
2072 wait_barrier(conf, false);
2073 allow_barrier(conf);
2074
2075 mempool_exit(&conf->r10buf_pool);
2076 }
2077
raid10_spare_active(struct mddev * mddev)2078 static int raid10_spare_active(struct mddev *mddev)
2079 {
2080 int i;
2081 struct r10conf *conf = mddev->private;
2082 struct raid10_info *tmp;
2083 int count = 0;
2084 unsigned long flags;
2085
2086 /*
2087 * Find all non-in_sync disks within the RAID10 configuration
2088 * and mark them in_sync
2089 */
2090 for (i = 0; i < conf->geo.raid_disks; i++) {
2091 tmp = conf->mirrors + i;
2092 if (tmp->replacement
2093 && tmp->replacement->recovery_offset == MaxSector
2094 && !test_bit(Faulty, &tmp->replacement->flags)
2095 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
2096 /* Replacement has just become active */
2097 if (!tmp->rdev
2098 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
2099 count++;
2100 if (tmp->rdev) {
2101 /* Replaced device not technically faulty,
2102 * but we need to be sure it gets removed
2103 * and never re-added.
2104 */
2105 set_bit(Faulty, &tmp->rdev->flags);
2106 sysfs_notify_dirent_safe(
2107 tmp->rdev->sysfs_state);
2108 }
2109 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
2110 } else if (tmp->rdev
2111 && tmp->rdev->recovery_offset == MaxSector
2112 && !test_bit(Faulty, &tmp->rdev->flags)
2113 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
2114 count++;
2115 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
2116 }
2117 }
2118 spin_lock_irqsave(&conf->device_lock, flags);
2119 mddev->degraded -= count;
2120 spin_unlock_irqrestore(&conf->device_lock, flags);
2121
2122 print_conf(conf);
2123 return count;
2124 }
2125
raid10_add_disk(struct mddev * mddev,struct md_rdev * rdev)2126 static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
2127 {
2128 struct r10conf *conf = mddev->private;
2129 int err = -EEXIST;
2130 int mirror, repl_slot = -1;
2131 int first = 0;
2132 int last = conf->geo.raid_disks - 1;
2133 struct raid10_info *p;
2134
2135 if (mddev->recovery_cp < MaxSector)
2136 /* only hot-add to in-sync arrays, as recovery is
2137 * very different from resync
2138 */
2139 return -EBUSY;
2140 if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
2141 return -EINVAL;
2142
2143 if (md_integrity_add_rdev(rdev, mddev))
2144 return -ENXIO;
2145
2146 if (rdev->raid_disk >= 0)
2147 first = last = rdev->raid_disk;
2148
2149 if (rdev->saved_raid_disk >= first &&
2150 rdev->saved_raid_disk < conf->geo.raid_disks &&
2151 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
2152 mirror = rdev->saved_raid_disk;
2153 else
2154 mirror = first;
2155 for ( ; mirror <= last ; mirror++) {
2156 p = &conf->mirrors[mirror];
2157 if (p->recovery_disabled == mddev->recovery_disabled)
2158 continue;
2159 if (p->rdev) {
2160 if (test_bit(WantReplacement, &p->rdev->flags) &&
2161 p->replacement == NULL && repl_slot < 0)
2162 repl_slot = mirror;
2163 continue;
2164 }
2165
2166 if (mddev->gendisk)
2167 disk_stack_limits(mddev->gendisk, rdev->bdev,
2168 rdev->data_offset << 9);
2169
2170 p->head_position = 0;
2171 p->recovery_disabled = mddev->recovery_disabled - 1;
2172 rdev->raid_disk = mirror;
2173 err = 0;
2174 if (rdev->saved_raid_disk != mirror)
2175 conf->fullsync = 1;
2176 rcu_assign_pointer(p->rdev, rdev);
2177 break;
2178 }
2179
2180 if (err && repl_slot >= 0) {
2181 p = &conf->mirrors[repl_slot];
2182 clear_bit(In_sync, &rdev->flags);
2183 set_bit(Replacement, &rdev->flags);
2184 rdev->raid_disk = repl_slot;
2185 err = 0;
2186 if (mddev->gendisk)
2187 disk_stack_limits(mddev->gendisk, rdev->bdev,
2188 rdev->data_offset << 9);
2189 conf->fullsync = 1;
2190 rcu_assign_pointer(p->replacement, rdev);
2191 }
2192
2193 print_conf(conf);
2194 return err;
2195 }
2196
raid10_remove_disk(struct mddev * mddev,struct md_rdev * rdev)2197 static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
2198 {
2199 struct r10conf *conf = mddev->private;
2200 int err = 0;
2201 int number = rdev->raid_disk;
2202 struct md_rdev **rdevp;
2203 struct raid10_info *p;
2204
2205 print_conf(conf);
2206 if (unlikely(number >= mddev->raid_disks))
2207 return 0;
2208 p = conf->mirrors + number;
2209 if (rdev == p->rdev)
2210 rdevp = &p->rdev;
2211 else if (rdev == p->replacement)
2212 rdevp = &p->replacement;
2213 else
2214 return 0;
2215
2216 if (test_bit(In_sync, &rdev->flags) ||
2217 atomic_read(&rdev->nr_pending)) {
2218 err = -EBUSY;
2219 goto abort;
2220 }
2221 /* Only remove non-faulty devices if recovery
2222 * is not possible.
2223 */
2224 if (!test_bit(Faulty, &rdev->flags) &&
2225 mddev->recovery_disabled != p->recovery_disabled &&
2226 (!p->replacement || p->replacement == rdev) &&
2227 number < conf->geo.raid_disks &&
2228 enough(conf, -1)) {
2229 err = -EBUSY;
2230 goto abort;
2231 }
2232 *rdevp = NULL;
2233 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
2234 synchronize_rcu();
2235 if (atomic_read(&rdev->nr_pending)) {
2236 /* lost the race, try later */
2237 err = -EBUSY;
2238 *rdevp = rdev;
2239 goto abort;
2240 }
2241 }
2242 if (p->replacement) {
2243 /* We must have just cleared 'rdev' */
2244 p->rdev = p->replacement;
2245 clear_bit(Replacement, &p->replacement->flags);
2246 smp_mb(); /* Make sure other CPUs may see both as identical
2247 * but will never see neither -- if they are careful.
2248 */
2249 p->replacement = NULL;
2250 }
2251
2252 clear_bit(WantReplacement, &rdev->flags);
2253 err = md_integrity_register(mddev);
2254
2255 abort:
2256
2257 print_conf(conf);
2258 return err;
2259 }
2260
__end_sync_read(struct r10bio * r10_bio,struct bio * bio,int d)2261 static void __end_sync_read(struct r10bio *r10_bio, struct bio *bio, int d)
2262 {
2263 struct r10conf *conf = r10_bio->mddev->private;
2264
2265 if (!bio->bi_status)
2266 set_bit(R10BIO_Uptodate, &r10_bio->state);
2267 else
2268 /* The write handler will notice the lack of
2269 * R10BIO_Uptodate and record any errors etc
2270 */
2271 atomic_add(r10_bio->sectors,
2272 &conf->mirrors[d].rdev->corrected_errors);
2273
2274 /* for reconstruct, we always reschedule after a read.
2275 * for resync, only after all reads
2276 */
2277 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
2278 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
2279 atomic_dec_and_test(&r10_bio->remaining)) {
2280 /* we have read all the blocks,
2281 * do the comparison in process context in raid10d
2282 */
2283 reschedule_retry(r10_bio);
2284 }
2285 }
2286
end_sync_read(struct bio * bio)2287 static void end_sync_read(struct bio *bio)
2288 {
2289 struct r10bio *r10_bio = get_resync_r10bio(bio);
2290 struct r10conf *conf = r10_bio->mddev->private;
2291 int d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
2292
2293 __end_sync_read(r10_bio, bio, d);
2294 }
2295
end_reshape_read(struct bio * bio)2296 static void end_reshape_read(struct bio *bio)
2297 {
2298 /* reshape read bio isn't allocated from r10buf_pool */
2299 struct r10bio *r10_bio = bio->bi_private;
2300
2301 __end_sync_read(r10_bio, bio, r10_bio->read_slot);
2302 }
2303
end_sync_request(struct r10bio * r10_bio)2304 static void end_sync_request(struct r10bio *r10_bio)
2305 {
2306 struct mddev *mddev = r10_bio->mddev;
2307
2308 while (atomic_dec_and_test(&r10_bio->remaining)) {
2309 if (r10_bio->master_bio == NULL) {
2310 /* the primary of several recovery bios */
2311 sector_t s = r10_bio->sectors;
2312 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2313 test_bit(R10BIO_WriteError, &r10_bio->state))
2314 reschedule_retry(r10_bio);
2315 else
2316 put_buf(r10_bio);
2317 md_done_sync(mddev, s, 1);
2318 break;
2319 } else {
2320 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
2321 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2322 test_bit(R10BIO_WriteError, &r10_bio->state))
2323 reschedule_retry(r10_bio);
2324 else
2325 put_buf(r10_bio);
2326 r10_bio = r10_bio2;
2327 }
2328 }
2329 }
2330
end_sync_write(struct bio * bio)2331 static void end_sync_write(struct bio *bio)
2332 {
2333 struct r10bio *r10_bio = get_resync_r10bio(bio);
2334 struct mddev *mddev = r10_bio->mddev;
2335 struct r10conf *conf = mddev->private;
2336 int d;
2337 sector_t first_bad;
2338 int bad_sectors;
2339 int slot;
2340 int repl;
2341 struct md_rdev *rdev = NULL;
2342
2343 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
2344 if (repl)
2345 rdev = conf->mirrors[d].replacement;
2346 else
2347 rdev = conf->mirrors[d].rdev;
2348
2349 if (bio->bi_status) {
2350 if (repl)
2351 md_error(mddev, rdev);
2352 else {
2353 set_bit(WriteErrorSeen, &rdev->flags);
2354 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2355 set_bit(MD_RECOVERY_NEEDED,
2356 &rdev->mddev->recovery);
2357 set_bit(R10BIO_WriteError, &r10_bio->state);
2358 }
2359 } else if (is_badblock(rdev,
2360 r10_bio->devs[slot].addr,
2361 r10_bio->sectors,
2362 &first_bad, &bad_sectors))
2363 set_bit(R10BIO_MadeGood, &r10_bio->state);
2364
2365 rdev_dec_pending(rdev, mddev);
2366
2367 end_sync_request(r10_bio);
2368 }
2369
2370 /*
2371 * Note: sync and recover and handled very differently for raid10
2372 * This code is for resync.
2373 * For resync, we read through virtual addresses and read all blocks.
2374 * If there is any error, we schedule a write. The lowest numbered
2375 * drive is authoritative.
2376 * However requests come for physical address, so we need to map.
2377 * For every physical address there are raid_disks/copies virtual addresses,
2378 * which is always are least one, but is not necessarly an integer.
2379 * This means that a physical address can span multiple chunks, so we may
2380 * have to submit multiple io requests for a single sync request.
2381 */
2382 /*
2383 * We check if all blocks are in-sync and only write to blocks that
2384 * aren't in sync
2385 */
sync_request_write(struct mddev * mddev,struct r10bio * r10_bio)2386 static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
2387 {
2388 struct r10conf *conf = mddev->private;
2389 int i, first;
2390 struct bio *tbio, *fbio;
2391 int vcnt;
2392 struct page **tpages, **fpages;
2393
2394 atomic_set(&r10_bio->remaining, 1);
2395
2396 /* find the first device with a block */
2397 for (i=0; i<conf->copies; i++)
2398 if (!r10_bio->devs[i].bio->bi_status)
2399 break;
2400
2401 if (i == conf->copies)
2402 goto done;
2403
2404 first = i;
2405 fbio = r10_bio->devs[i].bio;
2406 fbio->bi_iter.bi_size = r10_bio->sectors << 9;
2407 fbio->bi_iter.bi_idx = 0;
2408 fpages = get_resync_pages(fbio)->pages;
2409
2410 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
2411 /* now find blocks with errors */
2412 for (i=0 ; i < conf->copies ; i++) {
2413 int j, d;
2414 struct md_rdev *rdev;
2415 struct resync_pages *rp;
2416
2417 tbio = r10_bio->devs[i].bio;
2418
2419 if (tbio->bi_end_io != end_sync_read)
2420 continue;
2421 if (i == first)
2422 continue;
2423
2424 tpages = get_resync_pages(tbio)->pages;
2425 d = r10_bio->devs[i].devnum;
2426 rdev = conf->mirrors[d].rdev;
2427 if (!r10_bio->devs[i].bio->bi_status) {
2428 /* We know that the bi_io_vec layout is the same for
2429 * both 'first' and 'i', so we just compare them.
2430 * All vec entries are PAGE_SIZE;
2431 */
2432 int sectors = r10_bio->sectors;
2433 for (j = 0; j < vcnt; j++) {
2434 int len = PAGE_SIZE;
2435 if (sectors < (len / 512))
2436 len = sectors * 512;
2437 if (memcmp(page_address(fpages[j]),
2438 page_address(tpages[j]),
2439 len))
2440 break;
2441 sectors -= len/512;
2442 }
2443 if (j == vcnt)
2444 continue;
2445 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
2446 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2447 /* Don't fix anything. */
2448 continue;
2449 } else if (test_bit(FailFast, &rdev->flags)) {
2450 /* Just give up on this device */
2451 md_error(rdev->mddev, rdev);
2452 continue;
2453 }
2454 /* Ok, we need to write this bio, either to correct an
2455 * inconsistency or to correct an unreadable block.
2456 * First we need to fixup bv_offset, bv_len and
2457 * bi_vecs, as the read request might have corrupted these
2458 */
2459 rp = get_resync_pages(tbio);
2460 bio_reset(tbio, conf->mirrors[d].rdev->bdev, REQ_OP_WRITE);
2461
2462 md_bio_reset_resync_pages(tbio, rp, fbio->bi_iter.bi_size);
2463
2464 rp->raid_bio = r10_bio;
2465 tbio->bi_private = rp;
2466 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr;
2467 tbio->bi_end_io = end_sync_write;
2468
2469 bio_copy_data(tbio, fbio);
2470
2471 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2472 atomic_inc(&r10_bio->remaining);
2473 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
2474
2475 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
2476 tbio->bi_opf |= MD_FAILFAST;
2477 tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset;
2478 submit_bio_noacct(tbio);
2479 }
2480
2481 /* Now write out to any replacement devices
2482 * that are active
2483 */
2484 for (i = 0; i < conf->copies; i++) {
2485 int d;
2486
2487 tbio = r10_bio->devs[i].repl_bio;
2488 if (!tbio || !tbio->bi_end_io)
2489 continue;
2490 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2491 && r10_bio->devs[i].bio != fbio)
2492 bio_copy_data(tbio, fbio);
2493 d = r10_bio->devs[i].devnum;
2494 atomic_inc(&r10_bio->remaining);
2495 md_sync_acct(conf->mirrors[d].replacement->bdev,
2496 bio_sectors(tbio));
2497 submit_bio_noacct(tbio);
2498 }
2499
2500 done:
2501 if (atomic_dec_and_test(&r10_bio->remaining)) {
2502 md_done_sync(mddev, r10_bio->sectors, 1);
2503 put_buf(r10_bio);
2504 }
2505 }
2506
2507 /*
2508 * Now for the recovery code.
2509 * Recovery happens across physical sectors.
2510 * We recover all non-is_sync drives by finding the virtual address of
2511 * each, and then choose a working drive that also has that virt address.
2512 * There is a separate r10_bio for each non-in_sync drive.
2513 * Only the first two slots are in use. The first for reading,
2514 * The second for writing.
2515 *
2516 */
fix_recovery_read_error(struct r10bio * r10_bio)2517 static void fix_recovery_read_error(struct r10bio *r10_bio)
2518 {
2519 /* We got a read error during recovery.
2520 * We repeat the read in smaller page-sized sections.
2521 * If a read succeeds, write it to the new device or record
2522 * a bad block if we cannot.
2523 * If a read fails, record a bad block on both old and
2524 * new devices.
2525 */
2526 struct mddev *mddev = r10_bio->mddev;
2527 struct r10conf *conf = mddev->private;
2528 struct bio *bio = r10_bio->devs[0].bio;
2529 sector_t sect = 0;
2530 int sectors = r10_bio->sectors;
2531 int idx = 0;
2532 int dr = r10_bio->devs[0].devnum;
2533 int dw = r10_bio->devs[1].devnum;
2534 struct page **pages = get_resync_pages(bio)->pages;
2535
2536 while (sectors) {
2537 int s = sectors;
2538 struct md_rdev *rdev;
2539 sector_t addr;
2540 int ok;
2541
2542 if (s > (PAGE_SIZE>>9))
2543 s = PAGE_SIZE >> 9;
2544
2545 rdev = conf->mirrors[dr].rdev;
2546 addr = r10_bio->devs[0].addr + sect,
2547 ok = sync_page_io(rdev,
2548 addr,
2549 s << 9,
2550 pages[idx],
2551 REQ_OP_READ, false);
2552 if (ok) {
2553 rdev = conf->mirrors[dw].rdev;
2554 addr = r10_bio->devs[1].addr + sect;
2555 ok = sync_page_io(rdev,
2556 addr,
2557 s << 9,
2558 pages[idx],
2559 REQ_OP_WRITE, false);
2560 if (!ok) {
2561 set_bit(WriteErrorSeen, &rdev->flags);
2562 if (!test_and_set_bit(WantReplacement,
2563 &rdev->flags))
2564 set_bit(MD_RECOVERY_NEEDED,
2565 &rdev->mddev->recovery);
2566 }
2567 }
2568 if (!ok) {
2569 /* We don't worry if we cannot set a bad block -
2570 * it really is bad so there is no loss in not
2571 * recording it yet
2572 */
2573 rdev_set_badblocks(rdev, addr, s, 0);
2574
2575 if (rdev != conf->mirrors[dw].rdev) {
2576 /* need bad block on destination too */
2577 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
2578 addr = r10_bio->devs[1].addr + sect;
2579 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2580 if (!ok) {
2581 /* just abort the recovery */
2582 pr_notice("md/raid10:%s: recovery aborted due to read error\n",
2583 mdname(mddev));
2584
2585 conf->mirrors[dw].recovery_disabled
2586 = mddev->recovery_disabled;
2587 set_bit(MD_RECOVERY_INTR,
2588 &mddev->recovery);
2589 break;
2590 }
2591 }
2592 }
2593
2594 sectors -= s;
2595 sect += s;
2596 idx++;
2597 }
2598 }
2599
recovery_request_write(struct mddev * mddev,struct r10bio * r10_bio)2600 static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
2601 {
2602 struct r10conf *conf = mddev->private;
2603 int d;
2604 struct bio *wbio = r10_bio->devs[1].bio;
2605 struct bio *wbio2 = r10_bio->devs[1].repl_bio;
2606
2607 /* Need to test wbio2->bi_end_io before we call
2608 * submit_bio_noacct as if the former is NULL,
2609 * the latter is free to free wbio2.
2610 */
2611 if (wbio2 && !wbio2->bi_end_io)
2612 wbio2 = NULL;
2613
2614 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2615 fix_recovery_read_error(r10_bio);
2616 if (wbio->bi_end_io)
2617 end_sync_request(r10_bio);
2618 if (wbio2)
2619 end_sync_request(r10_bio);
2620 return;
2621 }
2622
2623 /*
2624 * share the pages with the first bio
2625 * and submit the write request
2626 */
2627 d = r10_bio->devs[1].devnum;
2628 if (wbio->bi_end_io) {
2629 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2630 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
2631 submit_bio_noacct(wbio);
2632 }
2633 if (wbio2) {
2634 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2635 md_sync_acct(conf->mirrors[d].replacement->bdev,
2636 bio_sectors(wbio2));
2637 submit_bio_noacct(wbio2);
2638 }
2639 }
2640
2641 /*
2642 * Used by fix_read_error() to decay the per rdev read_errors.
2643 * We halve the read error count for every hour that has elapsed
2644 * since the last recorded read error.
2645 *
2646 */
check_decay_read_errors(struct mddev * mddev,struct md_rdev * rdev)2647 static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
2648 {
2649 long cur_time_mon;
2650 unsigned long hours_since_last;
2651 unsigned int read_errors = atomic_read(&rdev->read_errors);
2652
2653 cur_time_mon = ktime_get_seconds();
2654
2655 if (rdev->last_read_error == 0) {
2656 /* first time we've seen a read error */
2657 rdev->last_read_error = cur_time_mon;
2658 return;
2659 }
2660
2661 hours_since_last = (long)(cur_time_mon -
2662 rdev->last_read_error) / 3600;
2663
2664 rdev->last_read_error = cur_time_mon;
2665
2666 /*
2667 * if hours_since_last is > the number of bits in read_errors
2668 * just set read errors to 0. We do this to avoid
2669 * overflowing the shift of read_errors by hours_since_last.
2670 */
2671 if (hours_since_last >= 8 * sizeof(read_errors))
2672 atomic_set(&rdev->read_errors, 0);
2673 else
2674 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2675 }
2676
r10_sync_page_io(struct md_rdev * rdev,sector_t sector,int sectors,struct page * page,enum req_op op)2677 static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
2678 int sectors, struct page *page, enum req_op op)
2679 {
2680 sector_t first_bad;
2681 int bad_sectors;
2682
2683 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2684 && (op == REQ_OP_READ || test_bit(WriteErrorSeen, &rdev->flags)))
2685 return -1;
2686 if (sync_page_io(rdev, sector, sectors << 9, page, op, false))
2687 /* success */
2688 return 1;
2689 if (op == REQ_OP_WRITE) {
2690 set_bit(WriteErrorSeen, &rdev->flags);
2691 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2692 set_bit(MD_RECOVERY_NEEDED,
2693 &rdev->mddev->recovery);
2694 }
2695 /* need to record an error - either for the block or the device */
2696 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2697 md_error(rdev->mddev, rdev);
2698 return 0;
2699 }
2700
2701 /*
2702 * This is a kernel thread which:
2703 *
2704 * 1. Retries failed read operations on working mirrors.
2705 * 2. Updates the raid superblock when problems encounter.
2706 * 3. Performs writes following reads for array synchronising.
2707 */
2708
fix_read_error(struct r10conf * conf,struct mddev * mddev,struct r10bio * r10_bio)2709 static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
2710 {
2711 int sect = 0; /* Offset from r10_bio->sector */
2712 int sectors = r10_bio->sectors, slot = r10_bio->read_slot;
2713 struct md_rdev *rdev;
2714 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
2715 int d = r10_bio->devs[slot].devnum;
2716
2717 /* still own a reference to this rdev, so it cannot
2718 * have been cleared recently.
2719 */
2720 rdev = conf->mirrors[d].rdev;
2721
2722 if (test_bit(Faulty, &rdev->flags))
2723 /* drive has already been failed, just ignore any
2724 more fix_read_error() attempts */
2725 return;
2726
2727 check_decay_read_errors(mddev, rdev);
2728 atomic_inc(&rdev->read_errors);
2729 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2730 pr_notice("md/raid10:%s: %pg: Raid device exceeded read_error threshold [cur %d:max %d]\n",
2731 mdname(mddev), rdev->bdev,
2732 atomic_read(&rdev->read_errors), max_read_errors);
2733 pr_notice("md/raid10:%s: %pg: Failing raid device\n",
2734 mdname(mddev), rdev->bdev);
2735 md_error(mddev, rdev);
2736 r10_bio->devs[slot].bio = IO_BLOCKED;
2737 return;
2738 }
2739
2740 while(sectors) {
2741 int s = sectors;
2742 int sl = slot;
2743 int success = 0;
2744 int start;
2745
2746 if (s > (PAGE_SIZE>>9))
2747 s = PAGE_SIZE >> 9;
2748
2749 rcu_read_lock();
2750 do {
2751 sector_t first_bad;
2752 int bad_sectors;
2753
2754 d = r10_bio->devs[sl].devnum;
2755 rdev = rcu_dereference(conf->mirrors[d].rdev);
2756 if (rdev &&
2757 test_bit(In_sync, &rdev->flags) &&
2758 !test_bit(Faulty, &rdev->flags) &&
2759 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2760 &first_bad, &bad_sectors) == 0) {
2761 atomic_inc(&rdev->nr_pending);
2762 rcu_read_unlock();
2763 success = sync_page_io(rdev,
2764 r10_bio->devs[sl].addr +
2765 sect,
2766 s<<9,
2767 conf->tmppage,
2768 REQ_OP_READ, false);
2769 rdev_dec_pending(rdev, mddev);
2770 rcu_read_lock();
2771 if (success)
2772 break;
2773 }
2774 sl++;
2775 if (sl == conf->copies)
2776 sl = 0;
2777 } while (sl != slot);
2778 rcu_read_unlock();
2779
2780 if (!success) {
2781 /* Cannot read from anywhere, just mark the block
2782 * as bad on the first device to discourage future
2783 * reads.
2784 */
2785 int dn = r10_bio->devs[slot].devnum;
2786 rdev = conf->mirrors[dn].rdev;
2787
2788 if (!rdev_set_badblocks(
2789 rdev,
2790 r10_bio->devs[slot].addr
2791 + sect,
2792 s, 0)) {
2793 md_error(mddev, rdev);
2794 r10_bio->devs[slot].bio
2795 = IO_BLOCKED;
2796 }
2797 break;
2798 }
2799
2800 start = sl;
2801 /* write it back and re-read */
2802 rcu_read_lock();
2803 while (sl != slot) {
2804 if (sl==0)
2805 sl = conf->copies;
2806 sl--;
2807 d = r10_bio->devs[sl].devnum;
2808 rdev = rcu_dereference(conf->mirrors[d].rdev);
2809 if (!rdev ||
2810 test_bit(Faulty, &rdev->flags) ||
2811 !test_bit(In_sync, &rdev->flags))
2812 continue;
2813
2814 atomic_inc(&rdev->nr_pending);
2815 rcu_read_unlock();
2816 if (r10_sync_page_io(rdev,
2817 r10_bio->devs[sl].addr +
2818 sect,
2819 s, conf->tmppage, REQ_OP_WRITE)
2820 == 0) {
2821 /* Well, this device is dead */
2822 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %pg)\n",
2823 mdname(mddev), s,
2824 (unsigned long long)(
2825 sect +
2826 choose_data_offset(r10_bio,
2827 rdev)),
2828 rdev->bdev);
2829 pr_notice("md/raid10:%s: %pg: failing drive\n",
2830 mdname(mddev),
2831 rdev->bdev);
2832 }
2833 rdev_dec_pending(rdev, mddev);
2834 rcu_read_lock();
2835 }
2836 sl = start;
2837 while (sl != slot) {
2838 if (sl==0)
2839 sl = conf->copies;
2840 sl--;
2841 d = r10_bio->devs[sl].devnum;
2842 rdev = rcu_dereference(conf->mirrors[d].rdev);
2843 if (!rdev ||
2844 test_bit(Faulty, &rdev->flags) ||
2845 !test_bit(In_sync, &rdev->flags))
2846 continue;
2847
2848 atomic_inc(&rdev->nr_pending);
2849 rcu_read_unlock();
2850 switch (r10_sync_page_io(rdev,
2851 r10_bio->devs[sl].addr +
2852 sect,
2853 s, conf->tmppage, REQ_OP_READ)) {
2854 case 0:
2855 /* Well, this device is dead */
2856 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %pg)\n",
2857 mdname(mddev), s,
2858 (unsigned long long)(
2859 sect +
2860 choose_data_offset(r10_bio, rdev)),
2861 rdev->bdev);
2862 pr_notice("md/raid10:%s: %pg: failing drive\n",
2863 mdname(mddev),
2864 rdev->bdev);
2865 break;
2866 case 1:
2867 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %pg)\n",
2868 mdname(mddev), s,
2869 (unsigned long long)(
2870 sect +
2871 choose_data_offset(r10_bio, rdev)),
2872 rdev->bdev);
2873 atomic_add(s, &rdev->corrected_errors);
2874 }
2875
2876 rdev_dec_pending(rdev, mddev);
2877 rcu_read_lock();
2878 }
2879 rcu_read_unlock();
2880
2881 sectors -= s;
2882 sect += s;
2883 }
2884 }
2885
narrow_write_error(struct r10bio * r10_bio,int i)2886 static int narrow_write_error(struct r10bio *r10_bio, int i)
2887 {
2888 struct bio *bio = r10_bio->master_bio;
2889 struct mddev *mddev = r10_bio->mddev;
2890 struct r10conf *conf = mddev->private;
2891 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
2892 /* bio has the data to be written to slot 'i' where
2893 * we just recently had a write error.
2894 * We repeatedly clone the bio and trim down to one block,
2895 * then try the write. Where the write fails we record
2896 * a bad block.
2897 * It is conceivable that the bio doesn't exactly align with
2898 * blocks. We must handle this.
2899 *
2900 * We currently own a reference to the rdev.
2901 */
2902
2903 int block_sectors;
2904 sector_t sector;
2905 int sectors;
2906 int sect_to_write = r10_bio->sectors;
2907 int ok = 1;
2908
2909 if (rdev->badblocks.shift < 0)
2910 return 0;
2911
2912 block_sectors = roundup(1 << rdev->badblocks.shift,
2913 bdev_logical_block_size(rdev->bdev) >> 9);
2914 sector = r10_bio->sector;
2915 sectors = ((r10_bio->sector + block_sectors)
2916 & ~(sector_t)(block_sectors - 1))
2917 - sector;
2918
2919 while (sect_to_write) {
2920 struct bio *wbio;
2921 sector_t wsector;
2922 if (sectors > sect_to_write)
2923 sectors = sect_to_write;
2924 /* Write at 'sector' for 'sectors' */
2925 wbio = bio_alloc_clone(rdev->bdev, bio, GFP_NOIO,
2926 &mddev->bio_set);
2927 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors);
2928 wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector);
2929 wbio->bi_iter.bi_sector = wsector +
2930 choose_data_offset(r10_bio, rdev);
2931 wbio->bi_opf = REQ_OP_WRITE;
2932
2933 if (submit_bio_wait(wbio) < 0)
2934 /* Failure! */
2935 ok = rdev_set_badblocks(rdev, wsector,
2936 sectors, 0)
2937 && ok;
2938
2939 bio_put(wbio);
2940 sect_to_write -= sectors;
2941 sector += sectors;
2942 sectors = block_sectors;
2943 }
2944 return ok;
2945 }
2946
handle_read_error(struct mddev * mddev,struct r10bio * r10_bio)2947 static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
2948 {
2949 int slot = r10_bio->read_slot;
2950 struct bio *bio;
2951 struct r10conf *conf = mddev->private;
2952 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
2953
2954 /* we got a read error. Maybe the drive is bad. Maybe just
2955 * the block and we can fix it.
2956 * We freeze all other IO, and try reading the block from
2957 * other devices. When we find one, we re-write
2958 * and check it that fixes the read error.
2959 * This is all done synchronously while the array is
2960 * frozen.
2961 */
2962 bio = r10_bio->devs[slot].bio;
2963 bio_put(bio);
2964 r10_bio->devs[slot].bio = NULL;
2965
2966 if (mddev->ro)
2967 r10_bio->devs[slot].bio = IO_BLOCKED;
2968 else if (!test_bit(FailFast, &rdev->flags)) {
2969 freeze_array(conf, 1);
2970 fix_read_error(conf, mddev, r10_bio);
2971 unfreeze_array(conf);
2972 } else
2973 md_error(mddev, rdev);
2974
2975 rdev_dec_pending(rdev, mddev);
2976 r10_bio->state = 0;
2977 raid10_read_request(mddev, r10_bio->master_bio, r10_bio, false);
2978 /*
2979 * allow_barrier after re-submit to ensure no sync io
2980 * can be issued while regular io pending.
2981 */
2982 allow_barrier(conf);
2983 }
2984
handle_write_completed(struct r10conf * conf,struct r10bio * r10_bio)2985 static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
2986 {
2987 /* Some sort of write request has finished and it
2988 * succeeded in writing where we thought there was a
2989 * bad block. So forget the bad block.
2990 * Or possibly if failed and we need to record
2991 * a bad block.
2992 */
2993 int m;
2994 struct md_rdev *rdev;
2995
2996 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2997 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
2998 for (m = 0; m < conf->copies; m++) {
2999 int dev = r10_bio->devs[m].devnum;
3000 rdev = conf->mirrors[dev].rdev;
3001 if (r10_bio->devs[m].bio == NULL ||
3002 r10_bio->devs[m].bio->bi_end_io == NULL)
3003 continue;
3004 if (!r10_bio->devs[m].bio->bi_status) {
3005 rdev_clear_badblocks(
3006 rdev,
3007 r10_bio->devs[m].addr,
3008 r10_bio->sectors, 0);
3009 } else {
3010 if (!rdev_set_badblocks(
3011 rdev,
3012 r10_bio->devs[m].addr,
3013 r10_bio->sectors, 0))
3014 md_error(conf->mddev, rdev);
3015 }
3016 rdev = conf->mirrors[dev].replacement;
3017 if (r10_bio->devs[m].repl_bio == NULL ||
3018 r10_bio->devs[m].repl_bio->bi_end_io == NULL)
3019 continue;
3020
3021 if (!r10_bio->devs[m].repl_bio->bi_status) {
3022 rdev_clear_badblocks(
3023 rdev,
3024 r10_bio->devs[m].addr,
3025 r10_bio->sectors, 0);
3026 } else {
3027 if (!rdev_set_badblocks(
3028 rdev,
3029 r10_bio->devs[m].addr,
3030 r10_bio->sectors, 0))
3031 md_error(conf->mddev, rdev);
3032 }
3033 }
3034 put_buf(r10_bio);
3035 } else {
3036 bool fail = false;
3037 for (m = 0; m < conf->copies; m++) {
3038 int dev = r10_bio->devs[m].devnum;
3039 struct bio *bio = r10_bio->devs[m].bio;
3040 rdev = conf->mirrors[dev].rdev;
3041 if (bio == IO_MADE_GOOD) {
3042 rdev_clear_badblocks(
3043 rdev,
3044 r10_bio->devs[m].addr,
3045 r10_bio->sectors, 0);
3046 rdev_dec_pending(rdev, conf->mddev);
3047 } else if (bio != NULL && bio->bi_status) {
3048 fail = true;
3049 if (!narrow_write_error(r10_bio, m))
3050 md_error(conf->mddev, rdev);
3051 rdev_dec_pending(rdev, conf->mddev);
3052 }
3053 bio = r10_bio->devs[m].repl_bio;
3054 rdev = conf->mirrors[dev].replacement;
3055 if (rdev && bio == IO_MADE_GOOD) {
3056 rdev_clear_badblocks(
3057 rdev,
3058 r10_bio->devs[m].addr,
3059 r10_bio->sectors, 0);
3060 rdev_dec_pending(rdev, conf->mddev);
3061 }
3062 }
3063 if (fail) {
3064 spin_lock_irq(&conf->device_lock);
3065 list_add(&r10_bio->retry_list, &conf->bio_end_io_list);
3066 conf->nr_queued++;
3067 spin_unlock_irq(&conf->device_lock);
3068 /*
3069 * In case freeze_array() is waiting for condition
3070 * nr_pending == nr_queued + extra to be true.
3071 */
3072 wake_up(&conf->wait_barrier);
3073 md_wakeup_thread(conf->mddev->thread);
3074 } else {
3075 if (test_bit(R10BIO_WriteError,
3076 &r10_bio->state))
3077 close_write(r10_bio);
3078 raid_end_bio_io(r10_bio);
3079 }
3080 }
3081 }
3082
raid10d(struct md_thread * thread)3083 static void raid10d(struct md_thread *thread)
3084 {
3085 struct mddev *mddev = thread->mddev;
3086 struct r10bio *r10_bio;
3087 unsigned long flags;
3088 struct r10conf *conf = mddev->private;
3089 struct list_head *head = &conf->retry_list;
3090 struct blk_plug plug;
3091
3092 md_check_recovery(mddev);
3093
3094 if (!list_empty_careful(&conf->bio_end_io_list) &&
3095 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
3096 LIST_HEAD(tmp);
3097 spin_lock_irqsave(&conf->device_lock, flags);
3098 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
3099 while (!list_empty(&conf->bio_end_io_list)) {
3100 list_move(conf->bio_end_io_list.prev, &tmp);
3101 conf->nr_queued--;
3102 }
3103 }
3104 spin_unlock_irqrestore(&conf->device_lock, flags);
3105 while (!list_empty(&tmp)) {
3106 r10_bio = list_first_entry(&tmp, struct r10bio,
3107 retry_list);
3108 list_del(&r10_bio->retry_list);
3109
3110 if (test_bit(R10BIO_WriteError,
3111 &r10_bio->state))
3112 close_write(r10_bio);
3113 raid_end_bio_io(r10_bio);
3114 }
3115 }
3116
3117 blk_start_plug(&plug);
3118 for (;;) {
3119
3120 flush_pending_writes(conf);
3121
3122 spin_lock_irqsave(&conf->device_lock, flags);
3123 if (list_empty(head)) {
3124 spin_unlock_irqrestore(&conf->device_lock, flags);
3125 break;
3126 }
3127 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
3128 list_del(head->prev);
3129 conf->nr_queued--;
3130 spin_unlock_irqrestore(&conf->device_lock, flags);
3131
3132 mddev = r10_bio->mddev;
3133 conf = mddev->private;
3134 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
3135 test_bit(R10BIO_WriteError, &r10_bio->state))
3136 handle_write_completed(conf, r10_bio);
3137 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
3138 reshape_request_write(mddev, r10_bio);
3139 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
3140 sync_request_write(mddev, r10_bio);
3141 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
3142 recovery_request_write(mddev, r10_bio);
3143 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
3144 handle_read_error(mddev, r10_bio);
3145 else
3146 WARN_ON_ONCE(1);
3147
3148 cond_resched();
3149 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
3150 md_check_recovery(mddev);
3151 }
3152 blk_finish_plug(&plug);
3153 }
3154
init_resync(struct r10conf * conf)3155 static int init_resync(struct r10conf *conf)
3156 {
3157 int ret, buffs, i;
3158
3159 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
3160 BUG_ON(mempool_initialized(&conf->r10buf_pool));
3161 conf->have_replacement = 0;
3162 for (i = 0; i < conf->geo.raid_disks; i++)
3163 if (conf->mirrors[i].replacement)
3164 conf->have_replacement = 1;
3165 ret = mempool_init(&conf->r10buf_pool, buffs,
3166 r10buf_pool_alloc, r10buf_pool_free, conf);
3167 if (ret)
3168 return ret;
3169 conf->next_resync = 0;
3170 return 0;
3171 }
3172
raid10_alloc_init_r10buf(struct r10conf * conf)3173 static struct r10bio *raid10_alloc_init_r10buf(struct r10conf *conf)
3174 {
3175 struct r10bio *r10bio = mempool_alloc(&conf->r10buf_pool, GFP_NOIO);
3176 struct rsync_pages *rp;
3177 struct bio *bio;
3178 int nalloc;
3179 int i;
3180
3181 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
3182 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
3183 nalloc = conf->copies; /* resync */
3184 else
3185 nalloc = 2; /* recovery */
3186
3187 for (i = 0; i < nalloc; i++) {
3188 bio = r10bio->devs[i].bio;
3189 rp = bio->bi_private;
3190 bio_reset(bio, NULL, 0);
3191 bio->bi_private = rp;
3192 bio = r10bio->devs[i].repl_bio;
3193 if (bio) {
3194 rp = bio->bi_private;
3195 bio_reset(bio, NULL, 0);
3196 bio->bi_private = rp;
3197 }
3198 }
3199 return r10bio;
3200 }
3201
3202 /*
3203 * Set cluster_sync_high since we need other nodes to add the
3204 * range [cluster_sync_low, cluster_sync_high] to suspend list.
3205 */
raid10_set_cluster_sync_high(struct r10conf * conf)3206 static void raid10_set_cluster_sync_high(struct r10conf *conf)
3207 {
3208 sector_t window_size;
3209 int extra_chunk, chunks;
3210
3211 /*
3212 * First, here we define "stripe" as a unit which across
3213 * all member devices one time, so we get chunks by use
3214 * raid_disks / near_copies. Otherwise, if near_copies is
3215 * close to raid_disks, then resync window could increases
3216 * linearly with the increase of raid_disks, which means
3217 * we will suspend a really large IO window while it is not
3218 * necessary. If raid_disks is not divisible by near_copies,
3219 * an extra chunk is needed to ensure the whole "stripe" is
3220 * covered.
3221 */
3222
3223 chunks = conf->geo.raid_disks / conf->geo.near_copies;
3224 if (conf->geo.raid_disks % conf->geo.near_copies == 0)
3225 extra_chunk = 0;
3226 else
3227 extra_chunk = 1;
3228 window_size = (chunks + extra_chunk) * conf->mddev->chunk_sectors;
3229
3230 /*
3231 * At least use a 32M window to align with raid1's resync window
3232 */
3233 window_size = (CLUSTER_RESYNC_WINDOW_SECTORS > window_size) ?
3234 CLUSTER_RESYNC_WINDOW_SECTORS : window_size;
3235
3236 conf->cluster_sync_high = conf->cluster_sync_low + window_size;
3237 }
3238
3239 /*
3240 * perform a "sync" on one "block"
3241 *
3242 * We need to make sure that no normal I/O request - particularly write
3243 * requests - conflict with active sync requests.
3244 *
3245 * This is achieved by tracking pending requests and a 'barrier' concept
3246 * that can be installed to exclude normal IO requests.
3247 *
3248 * Resync and recovery are handled very differently.
3249 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
3250 *
3251 * For resync, we iterate over virtual addresses, read all copies,
3252 * and update if there are differences. If only one copy is live,
3253 * skip it.
3254 * For recovery, we iterate over physical addresses, read a good
3255 * value for each non-in_sync drive, and over-write.
3256 *
3257 * So, for recovery we may have several outstanding complex requests for a
3258 * given address, one for each out-of-sync device. We model this by allocating
3259 * a number of r10_bio structures, one for each out-of-sync device.
3260 * As we setup these structures, we collect all bio's together into a list
3261 * which we then process collectively to add pages, and then process again
3262 * to pass to submit_bio_noacct.
3263 *
3264 * The r10_bio structures are linked using a borrowed master_bio pointer.
3265 * This link is counted in ->remaining. When the r10_bio that points to NULL
3266 * has its remaining count decremented to 0, the whole complex operation
3267 * is complete.
3268 *
3269 */
3270
raid10_sync_request(struct mddev * mddev,sector_t sector_nr,int * skipped)3271 static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
3272 int *skipped)
3273 {
3274 struct r10conf *conf = mddev->private;
3275 struct r10bio *r10_bio;
3276 struct bio *biolist = NULL, *bio;
3277 sector_t max_sector, nr_sectors;
3278 int i;
3279 int max_sync;
3280 sector_t sync_blocks;
3281 sector_t sectors_skipped = 0;
3282 int chunks_skipped = 0;
3283 sector_t chunk_mask = conf->geo.chunk_mask;
3284 int page_idx = 0;
3285 int error_disk = -1;
3286
3287 /*
3288 * Allow skipping a full rebuild for incremental assembly
3289 * of a clean array, like RAID1 does.
3290 */
3291 if (mddev->bitmap == NULL &&
3292 mddev->recovery_cp == MaxSector &&
3293 mddev->reshape_position == MaxSector &&
3294 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
3295 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
3296 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
3297 conf->fullsync == 0) {
3298 *skipped = 1;
3299 return mddev->dev_sectors - sector_nr;
3300 }
3301
3302 if (!mempool_initialized(&conf->r10buf_pool))
3303 if (init_resync(conf))
3304 return 0;
3305
3306 skipped:
3307 max_sector = mddev->dev_sectors;
3308 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
3309 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3310 max_sector = mddev->resync_max_sectors;
3311 if (sector_nr >= max_sector) {
3312 conf->cluster_sync_low = 0;
3313 conf->cluster_sync_high = 0;
3314
3315 /* If we aborted, we need to abort the
3316 * sync on the 'current' bitmap chucks (there can
3317 * be several when recovering multiple devices).
3318 * as we may have started syncing it but not finished.
3319 * We can find the current address in
3320 * mddev->curr_resync, but for recovery,
3321 * we need to convert that to several
3322 * virtual addresses.
3323 */
3324 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
3325 end_reshape(conf);
3326 close_sync(conf);
3327 return 0;
3328 }
3329
3330 if (mddev->curr_resync < max_sector) { /* aborted */
3331 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3332 md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
3333 &sync_blocks, 1);
3334 else for (i = 0; i < conf->geo.raid_disks; i++) {
3335 sector_t sect =
3336 raid10_find_virt(conf, mddev->curr_resync, i);
3337 md_bitmap_end_sync(mddev->bitmap, sect,
3338 &sync_blocks, 1);
3339 }
3340 } else {
3341 /* completed sync */
3342 if ((!mddev->bitmap || conf->fullsync)
3343 && conf->have_replacement
3344 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3345 /* Completed a full sync so the replacements
3346 * are now fully recovered.
3347 */
3348 rcu_read_lock();
3349 for (i = 0; i < conf->geo.raid_disks; i++) {
3350 struct md_rdev *rdev =
3351 rcu_dereference(conf->mirrors[i].replacement);
3352 if (rdev)
3353 rdev->recovery_offset = MaxSector;
3354 }
3355 rcu_read_unlock();
3356 }
3357 conf->fullsync = 0;
3358 }
3359 md_bitmap_close_sync(mddev->bitmap);
3360 close_sync(conf);
3361 *skipped = 1;
3362 return sectors_skipped;
3363 }
3364
3365 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3366 return reshape_request(mddev, sector_nr, skipped);
3367
3368 if (chunks_skipped >= conf->geo.raid_disks) {
3369 pr_err("md/raid10:%s: %s fails\n", mdname(mddev),
3370 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? "resync" : "recovery");
3371 if (error_disk >= 0 &&
3372 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3373 /*
3374 * recovery fails, set mirrors.recovery_disabled,
3375 * device shouldn't be added to there.
3376 */
3377 conf->mirrors[error_disk].recovery_disabled =
3378 mddev->recovery_disabled;
3379 return 0;
3380 }
3381 /*
3382 * if there has been nothing to do on any drive,
3383 * then there is nothing to do at all.
3384 */
3385 *skipped = 1;
3386 return (max_sector - sector_nr) + sectors_skipped;
3387 }
3388
3389 if (max_sector > mddev->resync_max)
3390 max_sector = mddev->resync_max; /* Don't do IO beyond here */
3391
3392 /* make sure whole request will fit in a chunk - if chunks
3393 * are meaningful
3394 */
3395 if (conf->geo.near_copies < conf->geo.raid_disks &&
3396 max_sector > (sector_nr | chunk_mask))
3397 max_sector = (sector_nr | chunk_mask) + 1;
3398
3399 /*
3400 * If there is non-resync activity waiting for a turn, then let it
3401 * though before starting on this new sync request.
3402 */
3403 if (conf->nr_waiting)
3404 schedule_timeout_uninterruptible(1);
3405
3406 /* Again, very different code for resync and recovery.
3407 * Both must result in an r10bio with a list of bios that
3408 * have bi_end_io, bi_sector, bi_bdev set,
3409 * and bi_private set to the r10bio.
3410 * For recovery, we may actually create several r10bios
3411 * with 2 bios in each, that correspond to the bios in the main one.
3412 * In this case, the subordinate r10bios link back through a
3413 * borrowed master_bio pointer, and the counter in the master
3414 * includes a ref from each subordinate.
3415 */
3416 /* First, we decide what to do and set ->bi_end_io
3417 * To end_sync_read if we want to read, and
3418 * end_sync_write if we will want to write.
3419 */
3420
3421 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
3422 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3423 /* recovery... the complicated one */
3424 int j;
3425 r10_bio = NULL;
3426
3427 for (i = 0 ; i < conf->geo.raid_disks; i++) {
3428 int still_degraded;
3429 struct r10bio *rb2;
3430 sector_t sect;
3431 int must_sync;
3432 int any_working;
3433 struct raid10_info *mirror = &conf->mirrors[i];
3434 struct md_rdev *mrdev, *mreplace;
3435
3436 rcu_read_lock();
3437 mrdev = rcu_dereference(mirror->rdev);
3438 mreplace = rcu_dereference(mirror->replacement);
3439
3440 if (mrdev && (test_bit(Faulty, &mrdev->flags) ||
3441 test_bit(In_sync, &mrdev->flags)))
3442 mrdev = NULL;
3443 if (mreplace && test_bit(Faulty, &mreplace->flags))
3444 mreplace = NULL;
3445
3446 if (!mrdev && !mreplace) {
3447 rcu_read_unlock();
3448 continue;
3449 }
3450
3451 still_degraded = 0;
3452 /* want to reconstruct this device */
3453 rb2 = r10_bio;
3454 sect = raid10_find_virt(conf, sector_nr, i);
3455 if (sect >= mddev->resync_max_sectors) {
3456 /* last stripe is not complete - don't
3457 * try to recover this sector.
3458 */
3459 rcu_read_unlock();
3460 continue;
3461 }
3462 /* Unless we are doing a full sync, or a replacement
3463 * we only need to recover the block if it is set in
3464 * the bitmap
3465 */
3466 must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
3467 &sync_blocks, 1);
3468 if (sync_blocks < max_sync)
3469 max_sync = sync_blocks;
3470 if (!must_sync &&
3471 mreplace == NULL &&
3472 !conf->fullsync) {
3473 /* yep, skip the sync_blocks here, but don't assume
3474 * that there will never be anything to do here
3475 */
3476 chunks_skipped = -1;
3477 rcu_read_unlock();
3478 continue;
3479 }
3480 if (mrdev)
3481 atomic_inc(&mrdev->nr_pending);
3482 if (mreplace)
3483 atomic_inc(&mreplace->nr_pending);
3484 rcu_read_unlock();
3485
3486 r10_bio = raid10_alloc_init_r10buf(conf);
3487 r10_bio->state = 0;
3488 raise_barrier(conf, rb2 != NULL);
3489 atomic_set(&r10_bio->remaining, 0);
3490
3491 r10_bio->master_bio = (struct bio*)rb2;
3492 if (rb2)
3493 atomic_inc(&rb2->remaining);
3494 r10_bio->mddev = mddev;
3495 set_bit(R10BIO_IsRecover, &r10_bio->state);
3496 r10_bio->sector = sect;
3497
3498 raid10_find_phys(conf, r10_bio);
3499
3500 /* Need to check if the array will still be
3501 * degraded
3502 */
3503 rcu_read_lock();
3504 for (j = 0; j < conf->geo.raid_disks; j++) {
3505 struct md_rdev *rdev = rcu_dereference(
3506 conf->mirrors[j].rdev);
3507 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3508 still_degraded = 1;
3509 break;
3510 }
3511 }
3512
3513 must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
3514 &sync_blocks, still_degraded);
3515
3516 any_working = 0;
3517 for (j=0; j<conf->copies;j++) {
3518 int k;
3519 int d = r10_bio->devs[j].devnum;
3520 sector_t from_addr, to_addr;
3521 struct md_rdev *rdev =
3522 rcu_dereference(conf->mirrors[d].rdev);
3523 sector_t sector, first_bad;
3524 int bad_sectors;
3525 if (!rdev ||
3526 !test_bit(In_sync, &rdev->flags))
3527 continue;
3528 /* This is where we read from */
3529 any_working = 1;
3530 sector = r10_bio->devs[j].addr;
3531
3532 if (is_badblock(rdev, sector, max_sync,
3533 &first_bad, &bad_sectors)) {
3534 if (first_bad > sector)
3535 max_sync = first_bad - sector;
3536 else {
3537 bad_sectors -= (sector
3538 - first_bad);
3539 if (max_sync > bad_sectors)
3540 max_sync = bad_sectors;
3541 continue;
3542 }
3543 }
3544 bio = r10_bio->devs[0].bio;
3545 bio->bi_next = biolist;
3546 biolist = bio;
3547 bio->bi_end_io = end_sync_read;
3548 bio->bi_opf = REQ_OP_READ;
3549 if (test_bit(FailFast, &rdev->flags))
3550 bio->bi_opf |= MD_FAILFAST;
3551 from_addr = r10_bio->devs[j].addr;
3552 bio->bi_iter.bi_sector = from_addr +
3553 rdev->data_offset;
3554 bio_set_dev(bio, rdev->bdev);
3555 atomic_inc(&rdev->nr_pending);
3556 /* and we write to 'i' (if not in_sync) */
3557
3558 for (k=0; k<conf->copies; k++)
3559 if (r10_bio->devs[k].devnum == i)
3560 break;
3561 BUG_ON(k == conf->copies);
3562 to_addr = r10_bio->devs[k].addr;
3563 r10_bio->devs[0].devnum = d;
3564 r10_bio->devs[0].addr = from_addr;
3565 r10_bio->devs[1].devnum = i;
3566 r10_bio->devs[1].addr = to_addr;
3567
3568 if (mrdev) {
3569 bio = r10_bio->devs[1].bio;
3570 bio->bi_next = biolist;
3571 biolist = bio;
3572 bio->bi_end_io = end_sync_write;
3573 bio->bi_opf = REQ_OP_WRITE;
3574 bio->bi_iter.bi_sector = to_addr
3575 + mrdev->data_offset;
3576 bio_set_dev(bio, mrdev->bdev);
3577 atomic_inc(&r10_bio->remaining);
3578 } else
3579 r10_bio->devs[1].bio->bi_end_io = NULL;
3580
3581 /* and maybe write to replacement */
3582 bio = r10_bio->devs[1].repl_bio;
3583 if (bio)
3584 bio->bi_end_io = NULL;
3585 /* Note: if replace is not NULL, then bio
3586 * cannot be NULL as r10buf_pool_alloc will
3587 * have allocated it.
3588 */
3589 if (!mreplace)
3590 break;
3591 bio->bi_next = biolist;
3592 biolist = bio;
3593 bio->bi_end_io = end_sync_write;
3594 bio->bi_opf = REQ_OP_WRITE;
3595 bio->bi_iter.bi_sector = to_addr +
3596 mreplace->data_offset;
3597 bio_set_dev(bio, mreplace->bdev);
3598 atomic_inc(&r10_bio->remaining);
3599 break;
3600 }
3601 rcu_read_unlock();
3602 if (j == conf->copies) {
3603 /* Cannot recover, so abort the recovery or
3604 * record a bad block */
3605 if (any_working) {
3606 /* problem is that there are bad blocks
3607 * on other device(s)
3608 */
3609 int k;
3610 for (k = 0; k < conf->copies; k++)
3611 if (r10_bio->devs[k].devnum == i)
3612 break;
3613 if (mrdev && !test_bit(In_sync,
3614 &mrdev->flags)
3615 && !rdev_set_badblocks(
3616 mrdev,
3617 r10_bio->devs[k].addr,
3618 max_sync, 0))
3619 any_working = 0;
3620 if (mreplace &&
3621 !rdev_set_badblocks(
3622 mreplace,
3623 r10_bio->devs[k].addr,
3624 max_sync, 0))
3625 any_working = 0;
3626 }
3627 if (!any_working) {
3628 if (!test_and_set_bit(MD_RECOVERY_INTR,
3629 &mddev->recovery))
3630 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
3631 mdname(mddev));
3632 mirror->recovery_disabled
3633 = mddev->recovery_disabled;
3634 } else {
3635 error_disk = i;
3636 }
3637 put_buf(r10_bio);
3638 if (rb2)
3639 atomic_dec(&rb2->remaining);
3640 r10_bio = rb2;
3641 if (mrdev)
3642 rdev_dec_pending(mrdev, mddev);
3643 if (mreplace)
3644 rdev_dec_pending(mreplace, mddev);
3645 break;
3646 }
3647 if (mrdev)
3648 rdev_dec_pending(mrdev, mddev);
3649 if (mreplace)
3650 rdev_dec_pending(mreplace, mddev);
3651 if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) {
3652 /* Only want this if there is elsewhere to
3653 * read from. 'j' is currently the first
3654 * readable copy.
3655 */
3656 int targets = 1;
3657 for (; j < conf->copies; j++) {
3658 int d = r10_bio->devs[j].devnum;
3659 if (conf->mirrors[d].rdev &&
3660 test_bit(In_sync,
3661 &conf->mirrors[d].rdev->flags))
3662 targets++;
3663 }
3664 if (targets == 1)
3665 r10_bio->devs[0].bio->bi_opf
3666 &= ~MD_FAILFAST;
3667 }
3668 }
3669 if (biolist == NULL) {
3670 while (r10_bio) {
3671 struct r10bio *rb2 = r10_bio;
3672 r10_bio = (struct r10bio*) rb2->master_bio;
3673 rb2->master_bio = NULL;
3674 put_buf(rb2);
3675 }
3676 goto giveup;
3677 }
3678 } else {
3679 /* resync. Schedule a read for every block at this virt offset */
3680 int count = 0;
3681
3682 /*
3683 * Since curr_resync_completed could probably not update in
3684 * time, and we will set cluster_sync_low based on it.
3685 * Let's check against "sector_nr + 2 * RESYNC_SECTORS" for
3686 * safety reason, which ensures curr_resync_completed is
3687 * updated in bitmap_cond_end_sync.
3688 */
3689 md_bitmap_cond_end_sync(mddev->bitmap, sector_nr,
3690 mddev_is_clustered(mddev) &&
3691 (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high));
3692
3693 if (!md_bitmap_start_sync(mddev->bitmap, sector_nr,
3694 &sync_blocks, mddev->degraded) &&
3695 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3696 &mddev->recovery)) {
3697 /* We can skip this block */
3698 *skipped = 1;
3699 return sync_blocks + sectors_skipped;
3700 }
3701 if (sync_blocks < max_sync)
3702 max_sync = sync_blocks;
3703 r10_bio = raid10_alloc_init_r10buf(conf);
3704 r10_bio->state = 0;
3705
3706 r10_bio->mddev = mddev;
3707 atomic_set(&r10_bio->remaining, 0);
3708 raise_barrier(conf, 0);
3709 conf->next_resync = sector_nr;
3710
3711 r10_bio->master_bio = NULL;
3712 r10_bio->sector = sector_nr;
3713 set_bit(R10BIO_IsSync, &r10_bio->state);
3714 raid10_find_phys(conf, r10_bio);
3715 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
3716
3717 for (i = 0; i < conf->copies; i++) {
3718 int d = r10_bio->devs[i].devnum;
3719 sector_t first_bad, sector;
3720 int bad_sectors;
3721 struct md_rdev *rdev;
3722
3723 if (r10_bio->devs[i].repl_bio)
3724 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3725
3726 bio = r10_bio->devs[i].bio;
3727 bio->bi_status = BLK_STS_IOERR;
3728 rcu_read_lock();
3729 rdev = rcu_dereference(conf->mirrors[d].rdev);
3730 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3731 rcu_read_unlock();
3732 continue;
3733 }
3734 sector = r10_bio->devs[i].addr;
3735 if (is_badblock(rdev, sector, max_sync,
3736 &first_bad, &bad_sectors)) {
3737 if (first_bad > sector)
3738 max_sync = first_bad - sector;
3739 else {
3740 bad_sectors -= (sector - first_bad);
3741 if (max_sync > bad_sectors)
3742 max_sync = bad_sectors;
3743 rcu_read_unlock();
3744 continue;
3745 }
3746 }
3747 atomic_inc(&rdev->nr_pending);
3748 atomic_inc(&r10_bio->remaining);
3749 bio->bi_next = biolist;
3750 biolist = bio;
3751 bio->bi_end_io = end_sync_read;
3752 bio->bi_opf = REQ_OP_READ;
3753 if (test_bit(FailFast, &rdev->flags))
3754 bio->bi_opf |= MD_FAILFAST;
3755 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3756 bio_set_dev(bio, rdev->bdev);
3757 count++;
3758
3759 rdev = rcu_dereference(conf->mirrors[d].replacement);
3760 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3761 rcu_read_unlock();
3762 continue;
3763 }
3764 atomic_inc(&rdev->nr_pending);
3765
3766 /* Need to set up for writing to the replacement */
3767 bio = r10_bio->devs[i].repl_bio;
3768 bio->bi_status = BLK_STS_IOERR;
3769
3770 sector = r10_bio->devs[i].addr;
3771 bio->bi_next = biolist;
3772 biolist = bio;
3773 bio->bi_end_io = end_sync_write;
3774 bio->bi_opf = REQ_OP_WRITE;
3775 if (test_bit(FailFast, &rdev->flags))
3776 bio->bi_opf |= MD_FAILFAST;
3777 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3778 bio_set_dev(bio, rdev->bdev);
3779 count++;
3780 rcu_read_unlock();
3781 }
3782
3783 if (count < 2) {
3784 for (i=0; i<conf->copies; i++) {
3785 int d = r10_bio->devs[i].devnum;
3786 if (r10_bio->devs[i].bio->bi_end_io)
3787 rdev_dec_pending(conf->mirrors[d].rdev,
3788 mddev);
3789 if (r10_bio->devs[i].repl_bio &&
3790 r10_bio->devs[i].repl_bio->bi_end_io)
3791 rdev_dec_pending(
3792 conf->mirrors[d].replacement,
3793 mddev);
3794 }
3795 put_buf(r10_bio);
3796 biolist = NULL;
3797 goto giveup;
3798 }
3799 }
3800
3801 nr_sectors = 0;
3802 if (sector_nr + max_sync < max_sector)
3803 max_sector = sector_nr + max_sync;
3804 do {
3805 struct page *page;
3806 int len = PAGE_SIZE;
3807 if (sector_nr + (len>>9) > max_sector)
3808 len = (max_sector - sector_nr) << 9;
3809 if (len == 0)
3810 break;
3811 for (bio= biolist ; bio ; bio=bio->bi_next) {
3812 struct resync_pages *rp = get_resync_pages(bio);
3813 page = resync_fetch_page(rp, page_idx);
3814 if (WARN_ON(!bio_add_page(bio, page, len, 0))) {
3815 bio->bi_status = BLK_STS_RESOURCE;
3816 bio_endio(bio);
3817 goto giveup;
3818 }
3819 }
3820 nr_sectors += len>>9;
3821 sector_nr += len>>9;
3822 } while (++page_idx < RESYNC_PAGES);
3823 r10_bio->sectors = nr_sectors;
3824
3825 if (mddev_is_clustered(mddev) &&
3826 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3827 /* It is resync not recovery */
3828 if (conf->cluster_sync_high < sector_nr + nr_sectors) {
3829 conf->cluster_sync_low = mddev->curr_resync_completed;
3830 raid10_set_cluster_sync_high(conf);
3831 /* Send resync message */
3832 md_cluster_ops->resync_info_update(mddev,
3833 conf->cluster_sync_low,
3834 conf->cluster_sync_high);
3835 }
3836 } else if (mddev_is_clustered(mddev)) {
3837 /* This is recovery not resync */
3838 sector_t sect_va1, sect_va2;
3839 bool broadcast_msg = false;
3840
3841 for (i = 0; i < conf->geo.raid_disks; i++) {
3842 /*
3843 * sector_nr is a device address for recovery, so we
3844 * need translate it to array address before compare
3845 * with cluster_sync_high.
3846 */
3847 sect_va1 = raid10_find_virt(conf, sector_nr, i);
3848
3849 if (conf->cluster_sync_high < sect_va1 + nr_sectors) {
3850 broadcast_msg = true;
3851 /*
3852 * curr_resync_completed is similar as
3853 * sector_nr, so make the translation too.
3854 */
3855 sect_va2 = raid10_find_virt(conf,
3856 mddev->curr_resync_completed, i);
3857
3858 if (conf->cluster_sync_low == 0 ||
3859 conf->cluster_sync_low > sect_va2)
3860 conf->cluster_sync_low = sect_va2;
3861 }
3862 }
3863 if (broadcast_msg) {
3864 raid10_set_cluster_sync_high(conf);
3865 md_cluster_ops->resync_info_update(mddev,
3866 conf->cluster_sync_low,
3867 conf->cluster_sync_high);
3868 }
3869 }
3870
3871 while (biolist) {
3872 bio = biolist;
3873 biolist = biolist->bi_next;
3874
3875 bio->bi_next = NULL;
3876 r10_bio = get_resync_r10bio(bio);
3877 r10_bio->sectors = nr_sectors;
3878
3879 if (bio->bi_end_io == end_sync_read) {
3880 md_sync_acct_bio(bio, nr_sectors);
3881 bio->bi_status = 0;
3882 submit_bio_noacct(bio);
3883 }
3884 }
3885
3886 if (sectors_skipped)
3887 /* pretend they weren't skipped, it makes
3888 * no important difference in this case
3889 */
3890 md_done_sync(mddev, sectors_skipped, 1);
3891
3892 return sectors_skipped + nr_sectors;
3893 giveup:
3894 /* There is nowhere to write, so all non-sync
3895 * drives must be failed or in resync, all drives
3896 * have a bad block, so try the next chunk...
3897 */
3898 if (sector_nr + max_sync < max_sector)
3899 max_sector = sector_nr + max_sync;
3900
3901 sectors_skipped += (max_sector - sector_nr);
3902 chunks_skipped ++;
3903 sector_nr = max_sector;
3904 goto skipped;
3905 }
3906
3907 static sector_t
raid10_size(struct mddev * mddev,sector_t sectors,int raid_disks)3908 raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
3909 {
3910 sector_t size;
3911 struct r10conf *conf = mddev->private;
3912
3913 if (!raid_disks)
3914 raid_disks = min(conf->geo.raid_disks,
3915 conf->prev.raid_disks);
3916 if (!sectors)
3917 sectors = conf->dev_sectors;
3918
3919 size = sectors >> conf->geo.chunk_shift;
3920 sector_div(size, conf->geo.far_copies);
3921 size = size * raid_disks;
3922 sector_div(size, conf->geo.near_copies);
3923
3924 return size << conf->geo.chunk_shift;
3925 }
3926
calc_sectors(struct r10conf * conf,sector_t size)3927 static void calc_sectors(struct r10conf *conf, sector_t size)
3928 {
3929 /* Calculate the number of sectors-per-device that will
3930 * actually be used, and set conf->dev_sectors and
3931 * conf->stride
3932 */
3933
3934 size = size >> conf->geo.chunk_shift;
3935 sector_div(size, conf->geo.far_copies);
3936 size = size * conf->geo.raid_disks;
3937 sector_div(size, conf->geo.near_copies);
3938 /* 'size' is now the number of chunks in the array */
3939 /* calculate "used chunks per device" */
3940 size = size * conf->copies;
3941
3942 /* We need to round up when dividing by raid_disks to
3943 * get the stride size.
3944 */
3945 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
3946
3947 conf->dev_sectors = size << conf->geo.chunk_shift;
3948
3949 if (conf->geo.far_offset)
3950 conf->geo.stride = 1 << conf->geo.chunk_shift;
3951 else {
3952 sector_div(size, conf->geo.far_copies);
3953 conf->geo.stride = size << conf->geo.chunk_shift;
3954 }
3955 }
3956
3957 enum geo_type {geo_new, geo_old, geo_start};
setup_geo(struct geom * geo,struct mddev * mddev,enum geo_type new)3958 static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3959 {
3960 int nc, fc, fo;
3961 int layout, chunk, disks;
3962 switch (new) {
3963 case geo_old:
3964 layout = mddev->layout;
3965 chunk = mddev->chunk_sectors;
3966 disks = mddev->raid_disks - mddev->delta_disks;
3967 break;
3968 case geo_new:
3969 layout = mddev->new_layout;
3970 chunk = mddev->new_chunk_sectors;
3971 disks = mddev->raid_disks;
3972 break;
3973 default: /* avoid 'may be unused' warnings */
3974 case geo_start: /* new when starting reshape - raid_disks not
3975 * updated yet. */
3976 layout = mddev->new_layout;
3977 chunk = mddev->new_chunk_sectors;
3978 disks = mddev->raid_disks + mddev->delta_disks;
3979 break;
3980 }
3981 if (layout >> 19)
3982 return -1;
3983 if (chunk < (PAGE_SIZE >> 9) ||
3984 !is_power_of_2(chunk))
3985 return -2;
3986 nc = layout & 255;
3987 fc = (layout >> 8) & 255;
3988 fo = layout & (1<<16);
3989 geo->raid_disks = disks;
3990 geo->near_copies = nc;
3991 geo->far_copies = fc;
3992 geo->far_offset = fo;
3993 switch (layout >> 17) {
3994 case 0: /* original layout. simple but not always optimal */
3995 geo->far_set_size = disks;
3996 break;
3997 case 1: /* "improved" layout which was buggy. Hopefully no-one is
3998 * actually using this, but leave code here just in case.*/
3999 geo->far_set_size = disks/fc;
4000 WARN(geo->far_set_size < fc,
4001 "This RAID10 layout does not provide data safety - please backup and create new array\n");
4002 break;
4003 case 2: /* "improved" layout fixed to match documentation */
4004 geo->far_set_size = fc * nc;
4005 break;
4006 default: /* Not a valid layout */
4007 return -1;
4008 }
4009 geo->chunk_mask = chunk - 1;
4010 geo->chunk_shift = ffz(~chunk);
4011 return nc*fc;
4012 }
4013
raid10_free_conf(struct r10conf * conf)4014 static void raid10_free_conf(struct r10conf *conf)
4015 {
4016 if (!conf)
4017 return;
4018
4019 mempool_exit(&conf->r10bio_pool);
4020 kfree(conf->mirrors);
4021 kfree(conf->mirrors_old);
4022 kfree(conf->mirrors_new);
4023 safe_put_page(conf->tmppage);
4024 bioset_exit(&conf->bio_split);
4025 kfree(conf);
4026 }
4027
setup_conf(struct mddev * mddev)4028 static struct r10conf *setup_conf(struct mddev *mddev)
4029 {
4030 struct r10conf *conf = NULL;
4031 int err = -EINVAL;
4032 struct geom geo;
4033 int copies;
4034
4035 copies = setup_geo(&geo, mddev, geo_new);
4036
4037 if (copies == -2) {
4038 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
4039 mdname(mddev), PAGE_SIZE);
4040 goto out;
4041 }
4042
4043 if (copies < 2 || copies > mddev->raid_disks) {
4044 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
4045 mdname(mddev), mddev->new_layout);
4046 goto out;
4047 }
4048
4049 err = -ENOMEM;
4050 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
4051 if (!conf)
4052 goto out;
4053
4054 /* FIXME calc properly */
4055 conf->mirrors = kcalloc(mddev->raid_disks + max(0, -mddev->delta_disks),
4056 sizeof(struct raid10_info),
4057 GFP_KERNEL);
4058 if (!conf->mirrors)
4059 goto out;
4060
4061 conf->tmppage = alloc_page(GFP_KERNEL);
4062 if (!conf->tmppage)
4063 goto out;
4064
4065 conf->geo = geo;
4066 conf->copies = copies;
4067 err = mempool_init(&conf->r10bio_pool, NR_RAID_BIOS, r10bio_pool_alloc,
4068 rbio_pool_free, conf);
4069 if (err)
4070 goto out;
4071
4072 err = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0);
4073 if (err)
4074 goto out;
4075
4076 calc_sectors(conf, mddev->dev_sectors);
4077 if (mddev->reshape_position == MaxSector) {
4078 conf->prev = conf->geo;
4079 conf->reshape_progress = MaxSector;
4080 } else {
4081 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
4082 err = -EINVAL;
4083 goto out;
4084 }
4085 conf->reshape_progress = mddev->reshape_position;
4086 if (conf->prev.far_offset)
4087 conf->prev.stride = 1 << conf->prev.chunk_shift;
4088 else
4089 /* far_copies must be 1 */
4090 conf->prev.stride = conf->dev_sectors;
4091 }
4092 conf->reshape_safe = conf->reshape_progress;
4093 spin_lock_init(&conf->device_lock);
4094 INIT_LIST_HEAD(&conf->retry_list);
4095 INIT_LIST_HEAD(&conf->bio_end_io_list);
4096
4097 seqlock_init(&conf->resync_lock);
4098 init_waitqueue_head(&conf->wait_barrier);
4099 atomic_set(&conf->nr_pending, 0);
4100
4101 err = -ENOMEM;
4102 rcu_assign_pointer(conf->thread,
4103 md_register_thread(raid10d, mddev, "raid10"));
4104 if (!conf->thread)
4105 goto out;
4106
4107 conf->mddev = mddev;
4108 return conf;
4109
4110 out:
4111 raid10_free_conf(conf);
4112 return ERR_PTR(err);
4113 }
4114
raid10_set_io_opt(struct r10conf * conf)4115 static void raid10_set_io_opt(struct r10conf *conf)
4116 {
4117 int raid_disks = conf->geo.raid_disks;
4118
4119 if (!(conf->geo.raid_disks % conf->geo.near_copies))
4120 raid_disks /= conf->geo.near_copies;
4121 blk_queue_io_opt(conf->mddev->queue, (conf->mddev->chunk_sectors << 9) *
4122 raid_disks);
4123 }
4124
raid10_run(struct mddev * mddev)4125 static int raid10_run(struct mddev *mddev)
4126 {
4127 struct r10conf *conf;
4128 int i, disk_idx;
4129 struct raid10_info *disk;
4130 struct md_rdev *rdev;
4131 sector_t size;
4132 sector_t min_offset_diff = 0;
4133 int first = 1;
4134
4135 if (mddev_init_writes_pending(mddev) < 0)
4136 return -ENOMEM;
4137
4138 if (mddev->private == NULL) {
4139 conf = setup_conf(mddev);
4140 if (IS_ERR(conf))
4141 return PTR_ERR(conf);
4142 mddev->private = conf;
4143 }
4144 conf = mddev->private;
4145 if (!conf)
4146 goto out;
4147
4148 rcu_assign_pointer(mddev->thread, conf->thread);
4149 rcu_assign_pointer(conf->thread, NULL);
4150
4151 if (mddev_is_clustered(conf->mddev)) {
4152 int fc, fo;
4153
4154 fc = (mddev->layout >> 8) & 255;
4155 fo = mddev->layout & (1<<16);
4156 if (fc > 1 || fo > 0) {
4157 pr_err("only near layout is supported by clustered"
4158 " raid10\n");
4159 goto out_free_conf;
4160 }
4161 }
4162
4163 if (mddev->queue) {
4164 blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
4165 blk_queue_io_min(mddev->queue, mddev->chunk_sectors << 9);
4166 raid10_set_io_opt(conf);
4167 }
4168
4169 rdev_for_each(rdev, mddev) {
4170 long long diff;
4171
4172 disk_idx = rdev->raid_disk;
4173 if (disk_idx < 0)
4174 continue;
4175 if (disk_idx >= conf->geo.raid_disks &&
4176 disk_idx >= conf->prev.raid_disks)
4177 continue;
4178 disk = conf->mirrors + disk_idx;
4179
4180 if (test_bit(Replacement, &rdev->flags)) {
4181 if (disk->replacement)
4182 goto out_free_conf;
4183 disk->replacement = rdev;
4184 } else {
4185 if (disk->rdev)
4186 goto out_free_conf;
4187 disk->rdev = rdev;
4188 }
4189 diff = (rdev->new_data_offset - rdev->data_offset);
4190 if (!mddev->reshape_backwards)
4191 diff = -diff;
4192 if (diff < 0)
4193 diff = 0;
4194 if (first || diff < min_offset_diff)
4195 min_offset_diff = diff;
4196
4197 if (mddev->gendisk)
4198 disk_stack_limits(mddev->gendisk, rdev->bdev,
4199 rdev->data_offset << 9);
4200
4201 disk->head_position = 0;
4202 first = 0;
4203 }
4204
4205 /* need to check that every block has at least one working mirror */
4206 if (!enough(conf, -1)) {
4207 pr_err("md/raid10:%s: not enough operational mirrors.\n",
4208 mdname(mddev));
4209 goto out_free_conf;
4210 }
4211
4212 if (conf->reshape_progress != MaxSector) {
4213 /* must ensure that shape change is supported */
4214 if (conf->geo.far_copies != 1 &&
4215 conf->geo.far_offset == 0)
4216 goto out_free_conf;
4217 if (conf->prev.far_copies != 1 &&
4218 conf->prev.far_offset == 0)
4219 goto out_free_conf;
4220 }
4221
4222 mddev->degraded = 0;
4223 for (i = 0;
4224 i < conf->geo.raid_disks
4225 || i < conf->prev.raid_disks;
4226 i++) {
4227
4228 disk = conf->mirrors + i;
4229
4230 if (!disk->rdev && disk->replacement) {
4231 /* The replacement is all we have - use it */
4232 disk->rdev = disk->replacement;
4233 disk->replacement = NULL;
4234 clear_bit(Replacement, &disk->rdev->flags);
4235 }
4236
4237 if (!disk->rdev ||
4238 !test_bit(In_sync, &disk->rdev->flags)) {
4239 disk->head_position = 0;
4240 mddev->degraded++;
4241 if (disk->rdev &&
4242 disk->rdev->saved_raid_disk < 0)
4243 conf->fullsync = 1;
4244 }
4245
4246 if (disk->replacement &&
4247 !test_bit(In_sync, &disk->replacement->flags) &&
4248 disk->replacement->saved_raid_disk < 0) {
4249 conf->fullsync = 1;
4250 }
4251
4252 disk->recovery_disabled = mddev->recovery_disabled - 1;
4253 }
4254
4255 if (mddev->recovery_cp != MaxSector)
4256 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
4257 mdname(mddev));
4258 pr_info("md/raid10:%s: active with %d out of %d devices\n",
4259 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
4260 conf->geo.raid_disks);
4261 /*
4262 * Ok, everything is just fine now
4263 */
4264 mddev->dev_sectors = conf->dev_sectors;
4265 size = raid10_size(mddev, 0, 0);
4266 md_set_array_sectors(mddev, size);
4267 mddev->resync_max_sectors = size;
4268 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
4269
4270 if (md_integrity_register(mddev))
4271 goto out_free_conf;
4272
4273 if (conf->reshape_progress != MaxSector) {
4274 unsigned long before_length, after_length;
4275
4276 before_length = ((1 << conf->prev.chunk_shift) *
4277 conf->prev.far_copies);
4278 after_length = ((1 << conf->geo.chunk_shift) *
4279 conf->geo.far_copies);
4280
4281 if (max(before_length, after_length) > min_offset_diff) {
4282 /* This cannot work */
4283 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
4284 goto out_free_conf;
4285 }
4286 conf->offset_diff = min_offset_diff;
4287
4288 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4289 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4290 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4291 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4292 rcu_assign_pointer(mddev->sync_thread,
4293 md_register_thread(md_do_sync, mddev, "reshape"));
4294 if (!mddev->sync_thread)
4295 goto out_free_conf;
4296 }
4297
4298 return 0;
4299
4300 out_free_conf:
4301 md_unregister_thread(mddev, &mddev->thread);
4302 raid10_free_conf(conf);
4303 mddev->private = NULL;
4304 out:
4305 return -EIO;
4306 }
4307
raid10_free(struct mddev * mddev,void * priv)4308 static void raid10_free(struct mddev *mddev, void *priv)
4309 {
4310 raid10_free_conf(priv);
4311 }
4312
raid10_quiesce(struct mddev * mddev,int quiesce)4313 static void raid10_quiesce(struct mddev *mddev, int quiesce)
4314 {
4315 struct r10conf *conf = mddev->private;
4316
4317 if (quiesce)
4318 raise_barrier(conf, 0);
4319 else
4320 lower_barrier(conf);
4321 }
4322
raid10_resize(struct mddev * mddev,sector_t sectors)4323 static int raid10_resize(struct mddev *mddev, sector_t sectors)
4324 {
4325 /* Resize of 'far' arrays is not supported.
4326 * For 'near' and 'offset' arrays we can set the
4327 * number of sectors used to be an appropriate multiple
4328 * of the chunk size.
4329 * For 'offset', this is far_copies*chunksize.
4330 * For 'near' the multiplier is the LCM of
4331 * near_copies and raid_disks.
4332 * So if far_copies > 1 && !far_offset, fail.
4333 * Else find LCM(raid_disks, near_copy)*far_copies and
4334 * multiply by chunk_size. Then round to this number.
4335 * This is mostly done by raid10_size()
4336 */
4337 struct r10conf *conf = mddev->private;
4338 sector_t oldsize, size;
4339
4340 if (mddev->reshape_position != MaxSector)
4341 return -EBUSY;
4342
4343 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
4344 return -EINVAL;
4345
4346 oldsize = raid10_size(mddev, 0, 0);
4347 size = raid10_size(mddev, sectors, 0);
4348 if (mddev->external_size &&
4349 mddev->array_sectors > size)
4350 return -EINVAL;
4351 if (mddev->bitmap) {
4352 int ret = md_bitmap_resize(mddev->bitmap, size, 0, 0);
4353 if (ret)
4354 return ret;
4355 }
4356 md_set_array_sectors(mddev, size);
4357 if (sectors > mddev->dev_sectors &&
4358 mddev->recovery_cp > oldsize) {
4359 mddev->recovery_cp = oldsize;
4360 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4361 }
4362 calc_sectors(conf, sectors);
4363 mddev->dev_sectors = conf->dev_sectors;
4364 mddev->resync_max_sectors = size;
4365 return 0;
4366 }
4367
raid10_takeover_raid0(struct mddev * mddev,sector_t size,int devs)4368 static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs)
4369 {
4370 struct md_rdev *rdev;
4371 struct r10conf *conf;
4372
4373 if (mddev->degraded > 0) {
4374 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
4375 mdname(mddev));
4376 return ERR_PTR(-EINVAL);
4377 }
4378 sector_div(size, devs);
4379
4380 /* Set new parameters */
4381 mddev->new_level = 10;
4382 /* new layout: far_copies = 1, near_copies = 2 */
4383 mddev->new_layout = (1<<8) + 2;
4384 mddev->new_chunk_sectors = mddev->chunk_sectors;
4385 mddev->delta_disks = mddev->raid_disks;
4386 mddev->raid_disks *= 2;
4387 /* make sure it will be not marked as dirty */
4388 mddev->recovery_cp = MaxSector;
4389 mddev->dev_sectors = size;
4390
4391 conf = setup_conf(mddev);
4392 if (!IS_ERR(conf)) {
4393 rdev_for_each(rdev, mddev)
4394 if (rdev->raid_disk >= 0) {
4395 rdev->new_raid_disk = rdev->raid_disk * 2;
4396 rdev->sectors = size;
4397 }
4398 }
4399
4400 return conf;
4401 }
4402
raid10_takeover(struct mddev * mddev)4403 static void *raid10_takeover(struct mddev *mddev)
4404 {
4405 struct r0conf *raid0_conf;
4406
4407 /* raid10 can take over:
4408 * raid0 - providing it has only two drives
4409 */
4410 if (mddev->level == 0) {
4411 /* for raid0 takeover only one zone is supported */
4412 raid0_conf = mddev->private;
4413 if (raid0_conf->nr_strip_zones > 1) {
4414 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
4415 mdname(mddev));
4416 return ERR_PTR(-EINVAL);
4417 }
4418 return raid10_takeover_raid0(mddev,
4419 raid0_conf->strip_zone->zone_end,
4420 raid0_conf->strip_zone->nb_dev);
4421 }
4422 return ERR_PTR(-EINVAL);
4423 }
4424
raid10_check_reshape(struct mddev * mddev)4425 static int raid10_check_reshape(struct mddev *mddev)
4426 {
4427 /* Called when there is a request to change
4428 * - layout (to ->new_layout)
4429 * - chunk size (to ->new_chunk_sectors)
4430 * - raid_disks (by delta_disks)
4431 * or when trying to restart a reshape that was ongoing.
4432 *
4433 * We need to validate the request and possibly allocate
4434 * space if that might be an issue later.
4435 *
4436 * Currently we reject any reshape of a 'far' mode array,
4437 * allow chunk size to change if new is generally acceptable,
4438 * allow raid_disks to increase, and allow
4439 * a switch between 'near' mode and 'offset' mode.
4440 */
4441 struct r10conf *conf = mddev->private;
4442 struct geom geo;
4443
4444 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
4445 return -EINVAL;
4446
4447 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
4448 /* mustn't change number of copies */
4449 return -EINVAL;
4450 if (geo.far_copies > 1 && !geo.far_offset)
4451 /* Cannot switch to 'far' mode */
4452 return -EINVAL;
4453
4454 if (mddev->array_sectors & geo.chunk_mask)
4455 /* not factor of array size */
4456 return -EINVAL;
4457
4458 if (!enough(conf, -1))
4459 return -EINVAL;
4460
4461 kfree(conf->mirrors_new);
4462 conf->mirrors_new = NULL;
4463 if (mddev->delta_disks > 0) {
4464 /* allocate new 'mirrors' list */
4465 conf->mirrors_new =
4466 kcalloc(mddev->raid_disks + mddev->delta_disks,
4467 sizeof(struct raid10_info),
4468 GFP_KERNEL);
4469 if (!conf->mirrors_new)
4470 return -ENOMEM;
4471 }
4472 return 0;
4473 }
4474
4475 /*
4476 * Need to check if array has failed when deciding whether to:
4477 * - start an array
4478 * - remove non-faulty devices
4479 * - add a spare
4480 * - allow a reshape
4481 * This determination is simple when no reshape is happening.
4482 * However if there is a reshape, we need to carefully check
4483 * both the before and after sections.
4484 * This is because some failed devices may only affect one
4485 * of the two sections, and some non-in_sync devices may
4486 * be insync in the section most affected by failed devices.
4487 */
calc_degraded(struct r10conf * conf)4488 static int calc_degraded(struct r10conf *conf)
4489 {
4490 int degraded, degraded2;
4491 int i;
4492
4493 rcu_read_lock();
4494 degraded = 0;
4495 /* 'prev' section first */
4496 for (i = 0; i < conf->prev.raid_disks; i++) {
4497 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4498 if (!rdev || test_bit(Faulty, &rdev->flags))
4499 degraded++;
4500 else if (!test_bit(In_sync, &rdev->flags))
4501 /* When we can reduce the number of devices in
4502 * an array, this might not contribute to
4503 * 'degraded'. It does now.
4504 */
4505 degraded++;
4506 }
4507 rcu_read_unlock();
4508 if (conf->geo.raid_disks == conf->prev.raid_disks)
4509 return degraded;
4510 rcu_read_lock();
4511 degraded2 = 0;
4512 for (i = 0; i < conf->geo.raid_disks; i++) {
4513 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4514 if (!rdev || test_bit(Faulty, &rdev->flags))
4515 degraded2++;
4516 else if (!test_bit(In_sync, &rdev->flags)) {
4517 /* If reshape is increasing the number of devices,
4518 * this section has already been recovered, so
4519 * it doesn't contribute to degraded.
4520 * else it does.
4521 */
4522 if (conf->geo.raid_disks <= conf->prev.raid_disks)
4523 degraded2++;
4524 }
4525 }
4526 rcu_read_unlock();
4527 if (degraded2 > degraded)
4528 return degraded2;
4529 return degraded;
4530 }
4531
raid10_start_reshape(struct mddev * mddev)4532 static int raid10_start_reshape(struct mddev *mddev)
4533 {
4534 /* A 'reshape' has been requested. This commits
4535 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4536 * This also checks if there are enough spares and adds them
4537 * to the array.
4538 * We currently require enough spares to make the final
4539 * array non-degraded. We also require that the difference
4540 * between old and new data_offset - on each device - is
4541 * enough that we never risk over-writing.
4542 */
4543
4544 unsigned long before_length, after_length;
4545 sector_t min_offset_diff = 0;
4546 int first = 1;
4547 struct geom new;
4548 struct r10conf *conf = mddev->private;
4549 struct md_rdev *rdev;
4550 int spares = 0;
4551 int ret;
4552
4553 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4554 return -EBUSY;
4555
4556 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4557 return -EINVAL;
4558
4559 before_length = ((1 << conf->prev.chunk_shift) *
4560 conf->prev.far_copies);
4561 after_length = ((1 << conf->geo.chunk_shift) *
4562 conf->geo.far_copies);
4563
4564 rdev_for_each(rdev, mddev) {
4565 if (!test_bit(In_sync, &rdev->flags)
4566 && !test_bit(Faulty, &rdev->flags))
4567 spares++;
4568 if (rdev->raid_disk >= 0) {
4569 long long diff = (rdev->new_data_offset
4570 - rdev->data_offset);
4571 if (!mddev->reshape_backwards)
4572 diff = -diff;
4573 if (diff < 0)
4574 diff = 0;
4575 if (first || diff < min_offset_diff)
4576 min_offset_diff = diff;
4577 first = 0;
4578 }
4579 }
4580
4581 if (max(before_length, after_length) > min_offset_diff)
4582 return -EINVAL;
4583
4584 if (spares < mddev->delta_disks)
4585 return -EINVAL;
4586
4587 conf->offset_diff = min_offset_diff;
4588 spin_lock_irq(&conf->device_lock);
4589 if (conf->mirrors_new) {
4590 memcpy(conf->mirrors_new, conf->mirrors,
4591 sizeof(struct raid10_info)*conf->prev.raid_disks);
4592 smp_mb();
4593 kfree(conf->mirrors_old);
4594 conf->mirrors_old = conf->mirrors;
4595 conf->mirrors = conf->mirrors_new;
4596 conf->mirrors_new = NULL;
4597 }
4598 setup_geo(&conf->geo, mddev, geo_start);
4599 smp_mb();
4600 if (mddev->reshape_backwards) {
4601 sector_t size = raid10_size(mddev, 0, 0);
4602 if (size < mddev->array_sectors) {
4603 spin_unlock_irq(&conf->device_lock);
4604 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4605 mdname(mddev));
4606 return -EINVAL;
4607 }
4608 mddev->resync_max_sectors = size;
4609 conf->reshape_progress = size;
4610 } else
4611 conf->reshape_progress = 0;
4612 conf->reshape_safe = conf->reshape_progress;
4613 spin_unlock_irq(&conf->device_lock);
4614
4615 if (mddev->delta_disks && mddev->bitmap) {
4616 struct mdp_superblock_1 *sb = NULL;
4617 sector_t oldsize, newsize;
4618
4619 oldsize = raid10_size(mddev, 0, 0);
4620 newsize = raid10_size(mddev, 0, conf->geo.raid_disks);
4621
4622 if (!mddev_is_clustered(mddev)) {
4623 ret = md_bitmap_resize(mddev->bitmap, newsize, 0, 0);
4624 if (ret)
4625 goto abort;
4626 else
4627 goto out;
4628 }
4629
4630 rdev_for_each(rdev, mddev) {
4631 if (rdev->raid_disk > -1 &&
4632 !test_bit(Faulty, &rdev->flags))
4633 sb = page_address(rdev->sb_page);
4634 }
4635
4636 /*
4637 * some node is already performing reshape, and no need to
4638 * call md_bitmap_resize again since it should be called when
4639 * receiving BITMAP_RESIZE msg
4640 */
4641 if ((sb && (le32_to_cpu(sb->feature_map) &
4642 MD_FEATURE_RESHAPE_ACTIVE)) || (oldsize == newsize))
4643 goto out;
4644
4645 ret = md_bitmap_resize(mddev->bitmap, newsize, 0, 0);
4646 if (ret)
4647 goto abort;
4648
4649 ret = md_cluster_ops->resize_bitmaps(mddev, newsize, oldsize);
4650 if (ret) {
4651 md_bitmap_resize(mddev->bitmap, oldsize, 0, 0);
4652 goto abort;
4653 }
4654 }
4655 out:
4656 if (mddev->delta_disks > 0) {
4657 rdev_for_each(rdev, mddev)
4658 if (rdev->raid_disk < 0 &&
4659 !test_bit(Faulty, &rdev->flags)) {
4660 if (raid10_add_disk(mddev, rdev) == 0) {
4661 if (rdev->raid_disk >=
4662 conf->prev.raid_disks)
4663 set_bit(In_sync, &rdev->flags);
4664 else
4665 rdev->recovery_offset = 0;
4666
4667 /* Failure here is OK */
4668 sysfs_link_rdev(mddev, rdev);
4669 }
4670 } else if (rdev->raid_disk >= conf->prev.raid_disks
4671 && !test_bit(Faulty, &rdev->flags)) {
4672 /* This is a spare that was manually added */
4673 set_bit(In_sync, &rdev->flags);
4674 }
4675 }
4676 /* When a reshape changes the number of devices,
4677 * ->degraded is measured against the larger of the
4678 * pre and post numbers.
4679 */
4680 spin_lock_irq(&conf->device_lock);
4681 mddev->degraded = calc_degraded(conf);
4682 spin_unlock_irq(&conf->device_lock);
4683 mddev->raid_disks = conf->geo.raid_disks;
4684 mddev->reshape_position = conf->reshape_progress;
4685 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
4686
4687 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4688 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4689 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
4690 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4691 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4692
4693 rcu_assign_pointer(mddev->sync_thread,
4694 md_register_thread(md_do_sync, mddev, "reshape"));
4695 if (!mddev->sync_thread) {
4696 ret = -EAGAIN;
4697 goto abort;
4698 }
4699 conf->reshape_checkpoint = jiffies;
4700 md_wakeup_thread(mddev->sync_thread);
4701 md_new_event();
4702 return 0;
4703
4704 abort:
4705 mddev->recovery = 0;
4706 spin_lock_irq(&conf->device_lock);
4707 conf->geo = conf->prev;
4708 mddev->raid_disks = conf->geo.raid_disks;
4709 rdev_for_each(rdev, mddev)
4710 rdev->new_data_offset = rdev->data_offset;
4711 smp_wmb();
4712 conf->reshape_progress = MaxSector;
4713 conf->reshape_safe = MaxSector;
4714 mddev->reshape_position = MaxSector;
4715 spin_unlock_irq(&conf->device_lock);
4716 return ret;
4717 }
4718
4719 /* Calculate the last device-address that could contain
4720 * any block from the chunk that includes the array-address 's'
4721 * and report the next address.
4722 * i.e. the address returned will be chunk-aligned and after
4723 * any data that is in the chunk containing 's'.
4724 */
last_dev_address(sector_t s,struct geom * geo)4725 static sector_t last_dev_address(sector_t s, struct geom *geo)
4726 {
4727 s = (s | geo->chunk_mask) + 1;
4728 s >>= geo->chunk_shift;
4729 s *= geo->near_copies;
4730 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4731 s *= geo->far_copies;
4732 s <<= geo->chunk_shift;
4733 return s;
4734 }
4735
4736 /* Calculate the first device-address that could contain
4737 * any block from the chunk that includes the array-address 's'.
4738 * This too will be the start of a chunk
4739 */
first_dev_address(sector_t s,struct geom * geo)4740 static sector_t first_dev_address(sector_t s, struct geom *geo)
4741 {
4742 s >>= geo->chunk_shift;
4743 s *= geo->near_copies;
4744 sector_div(s, geo->raid_disks);
4745 s *= geo->far_copies;
4746 s <<= geo->chunk_shift;
4747 return s;
4748 }
4749
reshape_request(struct mddev * mddev,sector_t sector_nr,int * skipped)4750 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4751 int *skipped)
4752 {
4753 /* We simply copy at most one chunk (smallest of old and new)
4754 * at a time, possibly less if that exceeds RESYNC_PAGES,
4755 * or we hit a bad block or something.
4756 * This might mean we pause for normal IO in the middle of
4757 * a chunk, but that is not a problem as mddev->reshape_position
4758 * can record any location.
4759 *
4760 * If we will want to write to a location that isn't
4761 * yet recorded as 'safe' (i.e. in metadata on disk) then
4762 * we need to flush all reshape requests and update the metadata.
4763 *
4764 * When reshaping forwards (e.g. to more devices), we interpret
4765 * 'safe' as the earliest block which might not have been copied
4766 * down yet. We divide this by previous stripe size and multiply
4767 * by previous stripe length to get lowest device offset that we
4768 * cannot write to yet.
4769 * We interpret 'sector_nr' as an address that we want to write to.
4770 * From this we use last_device_address() to find where we might
4771 * write to, and first_device_address on the 'safe' position.
4772 * If this 'next' write position is after the 'safe' position,
4773 * we must update the metadata to increase the 'safe' position.
4774 *
4775 * When reshaping backwards, we round in the opposite direction
4776 * and perform the reverse test: next write position must not be
4777 * less than current safe position.
4778 *
4779 * In all this the minimum difference in data offsets
4780 * (conf->offset_diff - always positive) allows a bit of slack,
4781 * so next can be after 'safe', but not by more than offset_diff
4782 *
4783 * We need to prepare all the bios here before we start any IO
4784 * to ensure the size we choose is acceptable to all devices.
4785 * The means one for each copy for write-out and an extra one for
4786 * read-in.
4787 * We store the read-in bio in ->master_bio and the others in
4788 * ->devs[x].bio and ->devs[x].repl_bio.
4789 */
4790 struct r10conf *conf = mddev->private;
4791 struct r10bio *r10_bio;
4792 sector_t next, safe, last;
4793 int max_sectors;
4794 int nr_sectors;
4795 int s;
4796 struct md_rdev *rdev;
4797 int need_flush = 0;
4798 struct bio *blist;
4799 struct bio *bio, *read_bio;
4800 int sectors_done = 0;
4801 struct page **pages;
4802
4803 if (sector_nr == 0) {
4804 /* If restarting in the middle, skip the initial sectors */
4805 if (mddev->reshape_backwards &&
4806 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4807 sector_nr = (raid10_size(mddev, 0, 0)
4808 - conf->reshape_progress);
4809 } else if (!mddev->reshape_backwards &&
4810 conf->reshape_progress > 0)
4811 sector_nr = conf->reshape_progress;
4812 if (sector_nr) {
4813 mddev->curr_resync_completed = sector_nr;
4814 sysfs_notify_dirent_safe(mddev->sysfs_completed);
4815 *skipped = 1;
4816 return sector_nr;
4817 }
4818 }
4819
4820 /* We don't use sector_nr to track where we are up to
4821 * as that doesn't work well for ->reshape_backwards.
4822 * So just use ->reshape_progress.
4823 */
4824 if (mddev->reshape_backwards) {
4825 /* 'next' is the earliest device address that we might
4826 * write to for this chunk in the new layout
4827 */
4828 next = first_dev_address(conf->reshape_progress - 1,
4829 &conf->geo);
4830
4831 /* 'safe' is the last device address that we might read from
4832 * in the old layout after a restart
4833 */
4834 safe = last_dev_address(conf->reshape_safe - 1,
4835 &conf->prev);
4836
4837 if (next + conf->offset_diff < safe)
4838 need_flush = 1;
4839
4840 last = conf->reshape_progress - 1;
4841 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4842 & conf->prev.chunk_mask);
4843 if (sector_nr + RESYNC_SECTORS < last)
4844 sector_nr = last + 1 - RESYNC_SECTORS;
4845 } else {
4846 /* 'next' is after the last device address that we
4847 * might write to for this chunk in the new layout
4848 */
4849 next = last_dev_address(conf->reshape_progress, &conf->geo);
4850
4851 /* 'safe' is the earliest device address that we might
4852 * read from in the old layout after a restart
4853 */
4854 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4855
4856 /* Need to update metadata if 'next' might be beyond 'safe'
4857 * as that would possibly corrupt data
4858 */
4859 if (next > safe + conf->offset_diff)
4860 need_flush = 1;
4861
4862 sector_nr = conf->reshape_progress;
4863 last = sector_nr | (conf->geo.chunk_mask
4864 & conf->prev.chunk_mask);
4865
4866 if (sector_nr + RESYNC_SECTORS <= last)
4867 last = sector_nr + RESYNC_SECTORS - 1;
4868 }
4869
4870 if (need_flush ||
4871 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4872 /* Need to update reshape_position in metadata */
4873 wait_barrier(conf, false);
4874 mddev->reshape_position = conf->reshape_progress;
4875 if (mddev->reshape_backwards)
4876 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4877 - conf->reshape_progress;
4878 else
4879 mddev->curr_resync_completed = conf->reshape_progress;
4880 conf->reshape_checkpoint = jiffies;
4881 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
4882 md_wakeup_thread(mddev->thread);
4883 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
4884 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4885 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4886 allow_barrier(conf);
4887 return sectors_done;
4888 }
4889 conf->reshape_safe = mddev->reshape_position;
4890 allow_barrier(conf);
4891 }
4892
4893 raise_barrier(conf, 0);
4894 read_more:
4895 /* Now schedule reads for blocks from sector_nr to last */
4896 r10_bio = raid10_alloc_init_r10buf(conf);
4897 r10_bio->state = 0;
4898 raise_barrier(conf, 1);
4899 atomic_set(&r10_bio->remaining, 0);
4900 r10_bio->mddev = mddev;
4901 r10_bio->sector = sector_nr;
4902 set_bit(R10BIO_IsReshape, &r10_bio->state);
4903 r10_bio->sectors = last - sector_nr + 1;
4904 rdev = read_balance(conf, r10_bio, &max_sectors);
4905 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4906
4907 if (!rdev) {
4908 /* Cannot read from here, so need to record bad blocks
4909 * on all the target devices.
4910 */
4911 // FIXME
4912 mempool_free(r10_bio, &conf->r10buf_pool);
4913 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4914 return sectors_done;
4915 }
4916
4917 read_bio = bio_alloc_bioset(rdev->bdev, RESYNC_PAGES, REQ_OP_READ,
4918 GFP_KERNEL, &mddev->bio_set);
4919 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
4920 + rdev->data_offset);
4921 read_bio->bi_private = r10_bio;
4922 read_bio->bi_end_io = end_reshape_read;
4923 r10_bio->master_bio = read_bio;
4924 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4925
4926 /*
4927 * Broadcast RESYNC message to other nodes, so all nodes would not
4928 * write to the region to avoid conflict.
4929 */
4930 if (mddev_is_clustered(mddev) && conf->cluster_sync_high <= sector_nr) {
4931 struct mdp_superblock_1 *sb = NULL;
4932 int sb_reshape_pos = 0;
4933
4934 conf->cluster_sync_low = sector_nr;
4935 conf->cluster_sync_high = sector_nr + CLUSTER_RESYNC_WINDOW_SECTORS;
4936 sb = page_address(rdev->sb_page);
4937 if (sb) {
4938 sb_reshape_pos = le64_to_cpu(sb->reshape_position);
4939 /*
4940 * Set cluster_sync_low again if next address for array
4941 * reshape is less than cluster_sync_low. Since we can't
4942 * update cluster_sync_low until it has finished reshape.
4943 */
4944 if (sb_reshape_pos < conf->cluster_sync_low)
4945 conf->cluster_sync_low = sb_reshape_pos;
4946 }
4947
4948 md_cluster_ops->resync_info_update(mddev, conf->cluster_sync_low,
4949 conf->cluster_sync_high);
4950 }
4951
4952 /* Now find the locations in the new layout */
4953 __raid10_find_phys(&conf->geo, r10_bio);
4954
4955 blist = read_bio;
4956 read_bio->bi_next = NULL;
4957
4958 rcu_read_lock();
4959 for (s = 0; s < conf->copies*2; s++) {
4960 struct bio *b;
4961 int d = r10_bio->devs[s/2].devnum;
4962 struct md_rdev *rdev2;
4963 if (s&1) {
4964 rdev2 = rcu_dereference(conf->mirrors[d].replacement);
4965 b = r10_bio->devs[s/2].repl_bio;
4966 } else {
4967 rdev2 = rcu_dereference(conf->mirrors[d].rdev);
4968 b = r10_bio->devs[s/2].bio;
4969 }
4970 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4971 continue;
4972
4973 bio_set_dev(b, rdev2->bdev);
4974 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr +
4975 rdev2->new_data_offset;
4976 b->bi_end_io = end_reshape_write;
4977 b->bi_opf = REQ_OP_WRITE;
4978 b->bi_next = blist;
4979 blist = b;
4980 }
4981
4982 /* Now add as many pages as possible to all of these bios. */
4983
4984 nr_sectors = 0;
4985 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
4986 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
4987 struct page *page = pages[s / (PAGE_SIZE >> 9)];
4988 int len = (max_sectors - s) << 9;
4989 if (len > PAGE_SIZE)
4990 len = PAGE_SIZE;
4991 for (bio = blist; bio ; bio = bio->bi_next) {
4992 if (WARN_ON(!bio_add_page(bio, page, len, 0))) {
4993 bio->bi_status = BLK_STS_RESOURCE;
4994 bio_endio(bio);
4995 return sectors_done;
4996 }
4997 }
4998 sector_nr += len >> 9;
4999 nr_sectors += len >> 9;
5000 }
5001 rcu_read_unlock();
5002 r10_bio->sectors = nr_sectors;
5003
5004 /* Now submit the read */
5005 md_sync_acct_bio(read_bio, r10_bio->sectors);
5006 atomic_inc(&r10_bio->remaining);
5007 read_bio->bi_next = NULL;
5008 submit_bio_noacct(read_bio);
5009 sectors_done += nr_sectors;
5010 if (sector_nr <= last)
5011 goto read_more;
5012
5013 lower_barrier(conf);
5014
5015 /* Now that we have done the whole section we can
5016 * update reshape_progress
5017 */
5018 if (mddev->reshape_backwards)
5019 conf->reshape_progress -= sectors_done;
5020 else
5021 conf->reshape_progress += sectors_done;
5022
5023 return sectors_done;
5024 }
5025
5026 static void end_reshape_request(struct r10bio *r10_bio);
5027 static int handle_reshape_read_error(struct mddev *mddev,
5028 struct r10bio *r10_bio);
reshape_request_write(struct mddev * mddev,struct r10bio * r10_bio)5029 static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
5030 {
5031 /* Reshape read completed. Hopefully we have a block
5032 * to write out.
5033 * If we got a read error then we do sync 1-page reads from
5034 * elsewhere until we find the data - or give up.
5035 */
5036 struct r10conf *conf = mddev->private;
5037 int s;
5038
5039 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
5040 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
5041 /* Reshape has been aborted */
5042 md_done_sync(mddev, r10_bio->sectors, 0);
5043 return;
5044 }
5045
5046 /* We definitely have the data in the pages, schedule the
5047 * writes.
5048 */
5049 atomic_set(&r10_bio->remaining, 1);
5050 for (s = 0; s < conf->copies*2; s++) {
5051 struct bio *b;
5052 int d = r10_bio->devs[s/2].devnum;
5053 struct md_rdev *rdev;
5054 rcu_read_lock();
5055 if (s&1) {
5056 rdev = rcu_dereference(conf->mirrors[d].replacement);
5057 b = r10_bio->devs[s/2].repl_bio;
5058 } else {
5059 rdev = rcu_dereference(conf->mirrors[d].rdev);
5060 b = r10_bio->devs[s/2].bio;
5061 }
5062 if (!rdev || test_bit(Faulty, &rdev->flags)) {
5063 rcu_read_unlock();
5064 continue;
5065 }
5066 atomic_inc(&rdev->nr_pending);
5067 rcu_read_unlock();
5068 md_sync_acct_bio(b, r10_bio->sectors);
5069 atomic_inc(&r10_bio->remaining);
5070 b->bi_next = NULL;
5071 submit_bio_noacct(b);
5072 }
5073 end_reshape_request(r10_bio);
5074 }
5075
end_reshape(struct r10conf * conf)5076 static void end_reshape(struct r10conf *conf)
5077 {
5078 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
5079 return;
5080
5081 spin_lock_irq(&conf->device_lock);
5082 conf->prev = conf->geo;
5083 md_finish_reshape(conf->mddev);
5084 smp_wmb();
5085 conf->reshape_progress = MaxSector;
5086 conf->reshape_safe = MaxSector;
5087 spin_unlock_irq(&conf->device_lock);
5088
5089 if (conf->mddev->queue)
5090 raid10_set_io_opt(conf);
5091 conf->fullsync = 0;
5092 }
5093
raid10_update_reshape_pos(struct mddev * mddev)5094 static void raid10_update_reshape_pos(struct mddev *mddev)
5095 {
5096 struct r10conf *conf = mddev->private;
5097 sector_t lo, hi;
5098
5099 md_cluster_ops->resync_info_get(mddev, &lo, &hi);
5100 if (((mddev->reshape_position <= hi) && (mddev->reshape_position >= lo))
5101 || mddev->reshape_position == MaxSector)
5102 conf->reshape_progress = mddev->reshape_position;
5103 else
5104 WARN_ON_ONCE(1);
5105 }
5106
handle_reshape_read_error(struct mddev * mddev,struct r10bio * r10_bio)5107 static int handle_reshape_read_error(struct mddev *mddev,
5108 struct r10bio *r10_bio)
5109 {
5110 /* Use sync reads to get the blocks from somewhere else */
5111 int sectors = r10_bio->sectors;
5112 struct r10conf *conf = mddev->private;
5113 struct r10bio *r10b;
5114 int slot = 0;
5115 int idx = 0;
5116 struct page **pages;
5117
5118 r10b = kmalloc(struct_size(r10b, devs, conf->copies), GFP_NOIO);
5119 if (!r10b) {
5120 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5121 return -ENOMEM;
5122 }
5123
5124 /* reshape IOs share pages from .devs[0].bio */
5125 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
5126
5127 r10b->sector = r10_bio->sector;
5128 __raid10_find_phys(&conf->prev, r10b);
5129
5130 while (sectors) {
5131 int s = sectors;
5132 int success = 0;
5133 int first_slot = slot;
5134
5135 if (s > (PAGE_SIZE >> 9))
5136 s = PAGE_SIZE >> 9;
5137
5138 rcu_read_lock();
5139 while (!success) {
5140 int d = r10b->devs[slot].devnum;
5141 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
5142 sector_t addr;
5143 if (rdev == NULL ||
5144 test_bit(Faulty, &rdev->flags) ||
5145 !test_bit(In_sync, &rdev->flags))
5146 goto failed;
5147
5148 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
5149 atomic_inc(&rdev->nr_pending);
5150 rcu_read_unlock();
5151 success = sync_page_io(rdev,
5152 addr,
5153 s << 9,
5154 pages[idx],
5155 REQ_OP_READ, false);
5156 rdev_dec_pending(rdev, mddev);
5157 rcu_read_lock();
5158 if (success)
5159 break;
5160 failed:
5161 slot++;
5162 if (slot >= conf->copies)
5163 slot = 0;
5164 if (slot == first_slot)
5165 break;
5166 }
5167 rcu_read_unlock();
5168 if (!success) {
5169 /* couldn't read this block, must give up */
5170 set_bit(MD_RECOVERY_INTR,
5171 &mddev->recovery);
5172 kfree(r10b);
5173 return -EIO;
5174 }
5175 sectors -= s;
5176 idx++;
5177 }
5178 kfree(r10b);
5179 return 0;
5180 }
5181
end_reshape_write(struct bio * bio)5182 static void end_reshape_write(struct bio *bio)
5183 {
5184 struct r10bio *r10_bio = get_resync_r10bio(bio);
5185 struct mddev *mddev = r10_bio->mddev;
5186 struct r10conf *conf = mddev->private;
5187 int d;
5188 int slot;
5189 int repl;
5190 struct md_rdev *rdev = NULL;
5191
5192 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
5193 if (repl)
5194 rdev = conf->mirrors[d].replacement;
5195 if (!rdev) {
5196 smp_mb();
5197 rdev = conf->mirrors[d].rdev;
5198 }
5199
5200 if (bio->bi_status) {
5201 /* FIXME should record badblock */
5202 md_error(mddev, rdev);
5203 }
5204
5205 rdev_dec_pending(rdev, mddev);
5206 end_reshape_request(r10_bio);
5207 }
5208
end_reshape_request(struct r10bio * r10_bio)5209 static void end_reshape_request(struct r10bio *r10_bio)
5210 {
5211 if (!atomic_dec_and_test(&r10_bio->remaining))
5212 return;
5213 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
5214 bio_put(r10_bio->master_bio);
5215 put_buf(r10_bio);
5216 }
5217
raid10_finish_reshape(struct mddev * mddev)5218 static void raid10_finish_reshape(struct mddev *mddev)
5219 {
5220 struct r10conf *conf = mddev->private;
5221
5222 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5223 return;
5224
5225 if (mddev->delta_disks > 0) {
5226 if (mddev->recovery_cp > mddev->resync_max_sectors) {
5227 mddev->recovery_cp = mddev->resync_max_sectors;
5228 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5229 }
5230 mddev->resync_max_sectors = mddev->array_sectors;
5231 } else {
5232 int d;
5233 rcu_read_lock();
5234 for (d = conf->geo.raid_disks ;
5235 d < conf->geo.raid_disks - mddev->delta_disks;
5236 d++) {
5237 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
5238 if (rdev)
5239 clear_bit(In_sync, &rdev->flags);
5240 rdev = rcu_dereference(conf->mirrors[d].replacement);
5241 if (rdev)
5242 clear_bit(In_sync, &rdev->flags);
5243 }
5244 rcu_read_unlock();
5245 }
5246 mddev->layout = mddev->new_layout;
5247 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
5248 mddev->reshape_position = MaxSector;
5249 mddev->delta_disks = 0;
5250 mddev->reshape_backwards = 0;
5251 }
5252
5253 static struct md_personality raid10_personality =
5254 {
5255 .name = "raid10",
5256 .level = 10,
5257 .owner = THIS_MODULE,
5258 .make_request = raid10_make_request,
5259 .run = raid10_run,
5260 .free = raid10_free,
5261 .status = raid10_status,
5262 .error_handler = raid10_error,
5263 .hot_add_disk = raid10_add_disk,
5264 .hot_remove_disk= raid10_remove_disk,
5265 .spare_active = raid10_spare_active,
5266 .sync_request = raid10_sync_request,
5267 .quiesce = raid10_quiesce,
5268 .size = raid10_size,
5269 .resize = raid10_resize,
5270 .takeover = raid10_takeover,
5271 .check_reshape = raid10_check_reshape,
5272 .start_reshape = raid10_start_reshape,
5273 .finish_reshape = raid10_finish_reshape,
5274 .update_reshape_pos = raid10_update_reshape_pos,
5275 };
5276
raid_init(void)5277 static int __init raid_init(void)
5278 {
5279 return register_md_personality(&raid10_personality);
5280 }
5281
raid_exit(void)5282 static void raid_exit(void)
5283 {
5284 unregister_md_personality(&raid10_personality);
5285 }
5286
5287 module_init(raid_init);
5288 module_exit(raid_exit);
5289 MODULE_LICENSE("GPL");
5290 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
5291 MODULE_ALIAS("md-personality-9"); /* RAID10 */
5292 MODULE_ALIAS("md-raid10");
5293 MODULE_ALIAS("md-level-10");
5294