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