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