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