md/raid10: clear bad-block record when write succeeds.
[deliverable/linux.git] / drivers / md / raid10.c
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/seq_file.h>
25 #include <linux/ratelimit.h>
26 #include "md.h"
27 #include "raid10.h"
28 #include "raid0.h"
29 #include "bitmap.h"
30
31 /*
32 * RAID10 provides a combination of RAID0 and RAID1 functionality.
33 * The layout of data is defined by
34 * chunk_size
35 * raid_disks
36 * near_copies (stored in low byte of layout)
37 * far_copies (stored in second byte of layout)
38 * far_offset (stored in bit 16 of layout )
39 *
40 * The data to be stored is divided into chunks using chunksize.
41 * Each device is divided into far_copies sections.
42 * In each section, chunks are laid out in a style similar to raid0, but
43 * near_copies copies of each chunk is stored (each on a different drive).
44 * The starting device for each section is offset near_copies from the starting
45 * device of the previous section.
46 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
47 * drive.
48 * near_copies and far_copies must be at least one, and their product is at most
49 * raid_disks.
50 *
51 * If far_offset is true, then the far_copies are handled a bit differently.
52 * The copies are still in different stripes, but instead of be very far apart
53 * on disk, there are adjacent stripes.
54 */
55
56 /*
57 * Number of guaranteed r10bios in case of extreme VM load:
58 */
59 #define NR_RAID10_BIOS 256
60
61 static void allow_barrier(conf_t *conf);
62 static void lower_barrier(conf_t *conf);
63
64 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
65 {
66 conf_t *conf = data;
67 int size = offsetof(struct r10bio_s, devs[conf->copies]);
68
69 /* allocate a r10bio with room for raid_disks entries in the bios array */
70 return kzalloc(size, gfp_flags);
71 }
72
73 static void r10bio_pool_free(void *r10_bio, void *data)
74 {
75 kfree(r10_bio);
76 }
77
78 /* Maximum size of each resync request */
79 #define RESYNC_BLOCK_SIZE (64*1024)
80 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
81 /* amount of memory to reserve for resync requests */
82 #define RESYNC_WINDOW (1024*1024)
83 /* maximum number of concurrent requests, memory permitting */
84 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
85
86 /*
87 * When performing a resync, we need to read and compare, so
88 * we need as many pages are there are copies.
89 * When performing a recovery, we need 2 bios, one for read,
90 * one for write (we recover only one drive per r10buf)
91 *
92 */
93 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
94 {
95 conf_t *conf = data;
96 struct page *page;
97 r10bio_t *r10_bio;
98 struct bio *bio;
99 int i, j;
100 int nalloc;
101
102 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
103 if (!r10_bio)
104 return NULL;
105
106 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
107 nalloc = conf->copies; /* resync */
108 else
109 nalloc = 2; /* recovery */
110
111 /*
112 * Allocate bios.
113 */
114 for (j = nalloc ; j-- ; ) {
115 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
116 if (!bio)
117 goto out_free_bio;
118 r10_bio->devs[j].bio = bio;
119 }
120 /*
121 * Allocate RESYNC_PAGES data pages and attach them
122 * where needed.
123 */
124 for (j = 0 ; j < nalloc; j++) {
125 bio = r10_bio->devs[j].bio;
126 for (i = 0; i < RESYNC_PAGES; i++) {
127 if (j == 1 && !test_bit(MD_RECOVERY_SYNC,
128 &conf->mddev->recovery)) {
129 /* we can share bv_page's during recovery */
130 struct bio *rbio = r10_bio->devs[0].bio;
131 page = rbio->bi_io_vec[i].bv_page;
132 get_page(page);
133 } else
134 page = alloc_page(gfp_flags);
135 if (unlikely(!page))
136 goto out_free_pages;
137
138 bio->bi_io_vec[i].bv_page = page;
139 }
140 }
141
142 return r10_bio;
143
144 out_free_pages:
145 for ( ; i > 0 ; i--)
146 safe_put_page(bio->bi_io_vec[i-1].bv_page);
147 while (j--)
148 for (i = 0; i < RESYNC_PAGES ; i++)
149 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
150 j = -1;
151 out_free_bio:
152 while ( ++j < nalloc )
153 bio_put(r10_bio->devs[j].bio);
154 r10bio_pool_free(r10_bio, conf);
155 return NULL;
156 }
157
158 static void r10buf_pool_free(void *__r10_bio, void *data)
159 {
160 int i;
161 conf_t *conf = data;
162 r10bio_t *r10bio = __r10_bio;
163 int j;
164
165 for (j=0; j < conf->copies; j++) {
166 struct bio *bio = r10bio->devs[j].bio;
167 if (bio) {
168 for (i = 0; i < RESYNC_PAGES; i++) {
169 safe_put_page(bio->bi_io_vec[i].bv_page);
170 bio->bi_io_vec[i].bv_page = NULL;
171 }
172 bio_put(bio);
173 }
174 }
175 r10bio_pool_free(r10bio, conf);
176 }
177
178 static void put_all_bios(conf_t *conf, r10bio_t *r10_bio)
179 {
180 int i;
181
182 for (i = 0; i < conf->copies; i++) {
183 struct bio **bio = & r10_bio->devs[i].bio;
184 if (!BIO_SPECIAL(*bio))
185 bio_put(*bio);
186 *bio = NULL;
187 }
188 }
189
190 static void free_r10bio(r10bio_t *r10_bio)
191 {
192 conf_t *conf = r10_bio->mddev->private;
193
194 put_all_bios(conf, r10_bio);
195 mempool_free(r10_bio, conf->r10bio_pool);
196 }
197
198 static void put_buf(r10bio_t *r10_bio)
199 {
200 conf_t *conf = r10_bio->mddev->private;
201
202 mempool_free(r10_bio, conf->r10buf_pool);
203
204 lower_barrier(conf);
205 }
206
207 static void reschedule_retry(r10bio_t *r10_bio)
208 {
209 unsigned long flags;
210 mddev_t *mddev = r10_bio->mddev;
211 conf_t *conf = mddev->private;
212
213 spin_lock_irqsave(&conf->device_lock, flags);
214 list_add(&r10_bio->retry_list, &conf->retry_list);
215 conf->nr_queued ++;
216 spin_unlock_irqrestore(&conf->device_lock, flags);
217
218 /* wake up frozen array... */
219 wake_up(&conf->wait_barrier);
220
221 md_wakeup_thread(mddev->thread);
222 }
223
224 /*
225 * raid_end_bio_io() is called when we have finished servicing a mirrored
226 * operation and are ready to return a success/failure code to the buffer
227 * cache layer.
228 */
229 static void raid_end_bio_io(r10bio_t *r10_bio)
230 {
231 struct bio *bio = r10_bio->master_bio;
232 int done;
233 conf_t *conf = r10_bio->mddev->private;
234
235 if (bio->bi_phys_segments) {
236 unsigned long flags;
237 spin_lock_irqsave(&conf->device_lock, flags);
238 bio->bi_phys_segments--;
239 done = (bio->bi_phys_segments == 0);
240 spin_unlock_irqrestore(&conf->device_lock, flags);
241 } else
242 done = 1;
243 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
244 clear_bit(BIO_UPTODATE, &bio->bi_flags);
245 if (done) {
246 bio_endio(bio, 0);
247 /*
248 * Wake up any possible resync thread that waits for the device
249 * to go idle.
250 */
251 allow_barrier(conf);
252 }
253 free_r10bio(r10_bio);
254 }
255
256 /*
257 * Update disk head position estimator based on IRQ completion info.
258 */
259 static inline void update_head_pos(int slot, r10bio_t *r10_bio)
260 {
261 conf_t *conf = r10_bio->mddev->private;
262
263 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
264 r10_bio->devs[slot].addr + (r10_bio->sectors);
265 }
266
267 /*
268 * Find the disk number which triggered given bio
269 */
270 static int find_bio_disk(conf_t *conf, r10bio_t *r10_bio,
271 struct bio *bio, int *slotp)
272 {
273 int slot;
274
275 for (slot = 0; slot < conf->copies; slot++)
276 if (r10_bio->devs[slot].bio == bio)
277 break;
278
279 BUG_ON(slot == conf->copies);
280 update_head_pos(slot, r10_bio);
281
282 if (slotp)
283 *slotp = slot;
284 return r10_bio->devs[slot].devnum;
285 }
286
287 static void raid10_end_read_request(struct bio *bio, int error)
288 {
289 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
290 r10bio_t *r10_bio = bio->bi_private;
291 int slot, dev;
292 conf_t *conf = r10_bio->mddev->private;
293
294
295 slot = r10_bio->read_slot;
296 dev = r10_bio->devs[slot].devnum;
297 /*
298 * this branch is our 'one mirror IO has finished' event handler:
299 */
300 update_head_pos(slot, r10_bio);
301
302 if (uptodate) {
303 /*
304 * Set R10BIO_Uptodate in our master bio, so that
305 * we will return a good error code to the higher
306 * levels even if IO on some other mirrored buffer fails.
307 *
308 * The 'master' represents the composite IO operation to
309 * user-side. So if something waits for IO, then it will
310 * wait for the 'master' bio.
311 */
312 set_bit(R10BIO_Uptodate, &r10_bio->state);
313 raid_end_bio_io(r10_bio);
314 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
315 } else {
316 /*
317 * oops, read error - keep the refcount on the rdev
318 */
319 char b[BDEVNAME_SIZE];
320 printk_ratelimited(KERN_ERR
321 "md/raid10:%s: %s: rescheduling sector %llu\n",
322 mdname(conf->mddev),
323 bdevname(conf->mirrors[dev].rdev->bdev, b),
324 (unsigned long long)r10_bio->sector);
325 set_bit(R10BIO_ReadError, &r10_bio->state);
326 reschedule_retry(r10_bio);
327 }
328 }
329
330 static void raid10_end_write_request(struct bio *bio, int error)
331 {
332 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
333 r10bio_t *r10_bio = bio->bi_private;
334 int dev;
335 int dec_rdev = 1;
336 conf_t *conf = r10_bio->mddev->private;
337 int slot;
338
339 dev = find_bio_disk(conf, r10_bio, bio, &slot);
340
341 /*
342 * this branch is our 'one mirror IO has finished' event handler:
343 */
344 if (!uptodate) {
345 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
346 /* an I/O failed, we can't clear the bitmap */
347 set_bit(R10BIO_Degraded, &r10_bio->state);
348 } else {
349 /*
350 * Set R10BIO_Uptodate in our master bio, so that
351 * we will return a good error code for to the higher
352 * levels even if IO on some other mirrored buffer fails.
353 *
354 * The 'master' represents the composite IO operation to
355 * user-side. So if something waits for IO, then it will
356 * wait for the 'master' bio.
357 */
358 sector_t first_bad;
359 int bad_sectors;
360
361 set_bit(R10BIO_Uptodate, &r10_bio->state);
362
363 /* Maybe we can clear some bad blocks. */
364 if (is_badblock(conf->mirrors[dev].rdev,
365 r10_bio->devs[slot].addr,
366 r10_bio->sectors,
367 &first_bad, &bad_sectors)) {
368 bio_put(bio);
369 r10_bio->devs[slot].bio = IO_MADE_GOOD;
370 dec_rdev = 0;
371 set_bit(R10BIO_MadeGood, &r10_bio->state);
372 }
373 }
374
375 /*
376 *
377 * Let's see if all mirrored write operations have finished
378 * already.
379 */
380 if (atomic_dec_and_test(&r10_bio->remaining)) {
381 /* clear the bitmap if all writes complete successfully */
382 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
383 r10_bio->sectors,
384 !test_bit(R10BIO_Degraded, &r10_bio->state),
385 0);
386 md_write_end(r10_bio->mddev);
387 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
388 reschedule_retry(r10_bio);
389 else
390 raid_end_bio_io(r10_bio);
391 }
392 if (dec_rdev)
393 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
394 }
395
396
397 /*
398 * RAID10 layout manager
399 * As well as the chunksize and raid_disks count, there are two
400 * parameters: near_copies and far_copies.
401 * near_copies * far_copies must be <= raid_disks.
402 * Normally one of these will be 1.
403 * If both are 1, we get raid0.
404 * If near_copies == raid_disks, we get raid1.
405 *
406 * Chunks are laid out in raid0 style with near_copies copies of the
407 * first chunk, followed by near_copies copies of the next chunk and
408 * so on.
409 * If far_copies > 1, then after 1/far_copies of the array has been assigned
410 * as described above, we start again with a device offset of near_copies.
411 * So we effectively have another copy of the whole array further down all
412 * the drives, but with blocks on different drives.
413 * With this layout, and block is never stored twice on the one device.
414 *
415 * raid10_find_phys finds the sector offset of a given virtual sector
416 * on each device that it is on.
417 *
418 * raid10_find_virt does the reverse mapping, from a device and a
419 * sector offset to a virtual address
420 */
421
422 static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
423 {
424 int n,f;
425 sector_t sector;
426 sector_t chunk;
427 sector_t stripe;
428 int dev;
429
430 int slot = 0;
431
432 /* now calculate first sector/dev */
433 chunk = r10bio->sector >> conf->chunk_shift;
434 sector = r10bio->sector & conf->chunk_mask;
435
436 chunk *= conf->near_copies;
437 stripe = chunk;
438 dev = sector_div(stripe, conf->raid_disks);
439 if (conf->far_offset)
440 stripe *= conf->far_copies;
441
442 sector += stripe << conf->chunk_shift;
443
444 /* and calculate all the others */
445 for (n=0; n < conf->near_copies; n++) {
446 int d = dev;
447 sector_t s = sector;
448 r10bio->devs[slot].addr = sector;
449 r10bio->devs[slot].devnum = d;
450 slot++;
451
452 for (f = 1; f < conf->far_copies; f++) {
453 d += conf->near_copies;
454 if (d >= conf->raid_disks)
455 d -= conf->raid_disks;
456 s += conf->stride;
457 r10bio->devs[slot].devnum = d;
458 r10bio->devs[slot].addr = s;
459 slot++;
460 }
461 dev++;
462 if (dev >= conf->raid_disks) {
463 dev = 0;
464 sector += (conf->chunk_mask + 1);
465 }
466 }
467 BUG_ON(slot != conf->copies);
468 }
469
470 static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
471 {
472 sector_t offset, chunk, vchunk;
473
474 offset = sector & conf->chunk_mask;
475 if (conf->far_offset) {
476 int fc;
477 chunk = sector >> conf->chunk_shift;
478 fc = sector_div(chunk, conf->far_copies);
479 dev -= fc * conf->near_copies;
480 if (dev < 0)
481 dev += conf->raid_disks;
482 } else {
483 while (sector >= conf->stride) {
484 sector -= conf->stride;
485 if (dev < conf->near_copies)
486 dev += conf->raid_disks - conf->near_copies;
487 else
488 dev -= conf->near_copies;
489 }
490 chunk = sector >> conf->chunk_shift;
491 }
492 vchunk = chunk * conf->raid_disks + dev;
493 sector_div(vchunk, conf->near_copies);
494 return (vchunk << conf->chunk_shift) + offset;
495 }
496
497 /**
498 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
499 * @q: request queue
500 * @bvm: properties of new bio
501 * @biovec: the request that could be merged to it.
502 *
503 * Return amount of bytes we can accept at this offset
504 * If near_copies == raid_disk, there are no striping issues,
505 * but in that case, the function isn't called at all.
506 */
507 static int raid10_mergeable_bvec(struct request_queue *q,
508 struct bvec_merge_data *bvm,
509 struct bio_vec *biovec)
510 {
511 mddev_t *mddev = q->queuedata;
512 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
513 int max;
514 unsigned int chunk_sectors = mddev->chunk_sectors;
515 unsigned int bio_sectors = bvm->bi_size >> 9;
516
517 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
518 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
519 if (max <= biovec->bv_len && bio_sectors == 0)
520 return biovec->bv_len;
521 else
522 return max;
523 }
524
525 /*
526 * This routine returns the disk from which the requested read should
527 * be done. There is a per-array 'next expected sequential IO' sector
528 * number - if this matches on the next IO then we use the last disk.
529 * There is also a per-disk 'last know head position' sector that is
530 * maintained from IRQ contexts, both the normal and the resync IO
531 * completion handlers update this position correctly. If there is no
532 * perfect sequential match then we pick the disk whose head is closest.
533 *
534 * If there are 2 mirrors in the same 2 devices, performance degrades
535 * because position is mirror, not device based.
536 *
537 * The rdev for the device selected will have nr_pending incremented.
538 */
539
540 /*
541 * FIXME: possibly should rethink readbalancing and do it differently
542 * depending on near_copies / far_copies geometry.
543 */
544 static int read_balance(conf_t *conf, r10bio_t *r10_bio, int *max_sectors)
545 {
546 const sector_t this_sector = r10_bio->sector;
547 int disk, slot;
548 int sectors = r10_bio->sectors;
549 int best_good_sectors;
550 sector_t new_distance, best_dist;
551 mdk_rdev_t *rdev;
552 int do_balance;
553 int best_slot;
554
555 raid10_find_phys(conf, r10_bio);
556 rcu_read_lock();
557 retry:
558 sectors = r10_bio->sectors;
559 best_slot = -1;
560 best_dist = MaxSector;
561 best_good_sectors = 0;
562 do_balance = 1;
563 /*
564 * Check if we can balance. We can balance on the whole
565 * device if no resync is going on (recovery is ok), or below
566 * the resync window. We take the first readable disk when
567 * above the resync window.
568 */
569 if (conf->mddev->recovery_cp < MaxSector
570 && (this_sector + sectors >= conf->next_resync))
571 do_balance = 0;
572
573 for (slot = 0; slot < conf->copies ; slot++) {
574 sector_t first_bad;
575 int bad_sectors;
576 sector_t dev_sector;
577
578 if (r10_bio->devs[slot].bio == IO_BLOCKED)
579 continue;
580 disk = r10_bio->devs[slot].devnum;
581 rdev = rcu_dereference(conf->mirrors[disk].rdev);
582 if (rdev == NULL)
583 continue;
584 if (!test_bit(In_sync, &rdev->flags))
585 continue;
586
587 dev_sector = r10_bio->devs[slot].addr;
588 if (is_badblock(rdev, dev_sector, sectors,
589 &first_bad, &bad_sectors)) {
590 if (best_dist < MaxSector)
591 /* Already have a better slot */
592 continue;
593 if (first_bad <= dev_sector) {
594 /* Cannot read here. If this is the
595 * 'primary' device, then we must not read
596 * beyond 'bad_sectors' from another device.
597 */
598 bad_sectors -= (dev_sector - first_bad);
599 if (!do_balance && sectors > bad_sectors)
600 sectors = bad_sectors;
601 if (best_good_sectors > sectors)
602 best_good_sectors = sectors;
603 } else {
604 sector_t good_sectors =
605 first_bad - dev_sector;
606 if (good_sectors > best_good_sectors) {
607 best_good_sectors = good_sectors;
608 best_slot = slot;
609 }
610 if (!do_balance)
611 /* Must read from here */
612 break;
613 }
614 continue;
615 } else
616 best_good_sectors = sectors;
617
618 if (!do_balance)
619 break;
620
621 /* This optimisation is debatable, and completely destroys
622 * sequential read speed for 'far copies' arrays. So only
623 * keep it for 'near' arrays, and review those later.
624 */
625 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending))
626 break;
627
628 /* for far > 1 always use the lowest address */
629 if (conf->far_copies > 1)
630 new_distance = r10_bio->devs[slot].addr;
631 else
632 new_distance = abs(r10_bio->devs[slot].addr -
633 conf->mirrors[disk].head_position);
634 if (new_distance < best_dist) {
635 best_dist = new_distance;
636 best_slot = slot;
637 }
638 }
639 if (slot == conf->copies)
640 slot = best_slot;
641
642 if (slot >= 0) {
643 disk = r10_bio->devs[slot].devnum;
644 rdev = rcu_dereference(conf->mirrors[disk].rdev);
645 if (!rdev)
646 goto retry;
647 atomic_inc(&rdev->nr_pending);
648 if (test_bit(Faulty, &rdev->flags)) {
649 /* Cannot risk returning a device that failed
650 * before we inc'ed nr_pending
651 */
652 rdev_dec_pending(rdev, conf->mddev);
653 goto retry;
654 }
655 r10_bio->read_slot = slot;
656 } else
657 disk = -1;
658 rcu_read_unlock();
659 *max_sectors = best_good_sectors;
660
661 return disk;
662 }
663
664 static int raid10_congested(void *data, int bits)
665 {
666 mddev_t *mddev = data;
667 conf_t *conf = mddev->private;
668 int i, ret = 0;
669
670 if (mddev_congested(mddev, bits))
671 return 1;
672 rcu_read_lock();
673 for (i = 0; i < conf->raid_disks && ret == 0; i++) {
674 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
675 if (rdev && !test_bit(Faulty, &rdev->flags)) {
676 struct request_queue *q = bdev_get_queue(rdev->bdev);
677
678 ret |= bdi_congested(&q->backing_dev_info, bits);
679 }
680 }
681 rcu_read_unlock();
682 return ret;
683 }
684
685 static void flush_pending_writes(conf_t *conf)
686 {
687 /* Any writes that have been queued but are awaiting
688 * bitmap updates get flushed here.
689 */
690 spin_lock_irq(&conf->device_lock);
691
692 if (conf->pending_bio_list.head) {
693 struct bio *bio;
694 bio = bio_list_get(&conf->pending_bio_list);
695 spin_unlock_irq(&conf->device_lock);
696 /* flush any pending bitmap writes to disk
697 * before proceeding w/ I/O */
698 bitmap_unplug(conf->mddev->bitmap);
699
700 while (bio) { /* submit pending writes */
701 struct bio *next = bio->bi_next;
702 bio->bi_next = NULL;
703 generic_make_request(bio);
704 bio = next;
705 }
706 } else
707 spin_unlock_irq(&conf->device_lock);
708 }
709
710 /* Barriers....
711 * Sometimes we need to suspend IO while we do something else,
712 * either some resync/recovery, or reconfigure the array.
713 * To do this we raise a 'barrier'.
714 * The 'barrier' is a counter that can be raised multiple times
715 * to count how many activities are happening which preclude
716 * normal IO.
717 * We can only raise the barrier if there is no pending IO.
718 * i.e. if nr_pending == 0.
719 * We choose only to raise the barrier if no-one is waiting for the
720 * barrier to go down. This means that as soon as an IO request
721 * is ready, no other operations which require a barrier will start
722 * until the IO request has had a chance.
723 *
724 * So: regular IO calls 'wait_barrier'. When that returns there
725 * is no backgroup IO happening, It must arrange to call
726 * allow_barrier when it has finished its IO.
727 * backgroup IO calls must call raise_barrier. Once that returns
728 * there is no normal IO happeing. It must arrange to call
729 * lower_barrier when the particular background IO completes.
730 */
731
732 static void raise_barrier(conf_t *conf, int force)
733 {
734 BUG_ON(force && !conf->barrier);
735 spin_lock_irq(&conf->resync_lock);
736
737 /* Wait until no block IO is waiting (unless 'force') */
738 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
739 conf->resync_lock, );
740
741 /* block any new IO from starting */
742 conf->barrier++;
743
744 /* Now wait for all pending IO to complete */
745 wait_event_lock_irq(conf->wait_barrier,
746 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
747 conf->resync_lock, );
748
749 spin_unlock_irq(&conf->resync_lock);
750 }
751
752 static void lower_barrier(conf_t *conf)
753 {
754 unsigned long flags;
755 spin_lock_irqsave(&conf->resync_lock, flags);
756 conf->barrier--;
757 spin_unlock_irqrestore(&conf->resync_lock, flags);
758 wake_up(&conf->wait_barrier);
759 }
760
761 static void wait_barrier(conf_t *conf)
762 {
763 spin_lock_irq(&conf->resync_lock);
764 if (conf->barrier) {
765 conf->nr_waiting++;
766 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
767 conf->resync_lock,
768 );
769 conf->nr_waiting--;
770 }
771 conf->nr_pending++;
772 spin_unlock_irq(&conf->resync_lock);
773 }
774
775 static void allow_barrier(conf_t *conf)
776 {
777 unsigned long flags;
778 spin_lock_irqsave(&conf->resync_lock, flags);
779 conf->nr_pending--;
780 spin_unlock_irqrestore(&conf->resync_lock, flags);
781 wake_up(&conf->wait_barrier);
782 }
783
784 static void freeze_array(conf_t *conf)
785 {
786 /* stop syncio and normal IO and wait for everything to
787 * go quiet.
788 * We increment barrier and nr_waiting, and then
789 * wait until nr_pending match nr_queued+1
790 * This is called in the context of one normal IO request
791 * that has failed. Thus any sync request that might be pending
792 * will be blocked by nr_pending, and we need to wait for
793 * pending IO requests to complete or be queued for re-try.
794 * Thus the number queued (nr_queued) plus this request (1)
795 * must match the number of pending IOs (nr_pending) before
796 * we continue.
797 */
798 spin_lock_irq(&conf->resync_lock);
799 conf->barrier++;
800 conf->nr_waiting++;
801 wait_event_lock_irq(conf->wait_barrier,
802 conf->nr_pending == conf->nr_queued+1,
803 conf->resync_lock,
804 flush_pending_writes(conf));
805
806 spin_unlock_irq(&conf->resync_lock);
807 }
808
809 static void unfreeze_array(conf_t *conf)
810 {
811 /* reverse the effect of the freeze */
812 spin_lock_irq(&conf->resync_lock);
813 conf->barrier--;
814 conf->nr_waiting--;
815 wake_up(&conf->wait_barrier);
816 spin_unlock_irq(&conf->resync_lock);
817 }
818
819 static int make_request(mddev_t *mddev, struct bio * bio)
820 {
821 conf_t *conf = mddev->private;
822 mirror_info_t *mirror;
823 r10bio_t *r10_bio;
824 struct bio *read_bio;
825 int i;
826 int chunk_sects = conf->chunk_mask + 1;
827 const int rw = bio_data_dir(bio);
828 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
829 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
830 unsigned long flags;
831 mdk_rdev_t *blocked_rdev;
832 int plugged;
833 int sectors_handled;
834 int max_sectors;
835
836 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
837 md_flush_request(mddev, bio);
838 return 0;
839 }
840
841 /* If this request crosses a chunk boundary, we need to
842 * split it. This will only happen for 1 PAGE (or less) requests.
843 */
844 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
845 > chunk_sects &&
846 conf->near_copies < conf->raid_disks)) {
847 struct bio_pair *bp;
848 /* Sanity check -- queue functions should prevent this happening */
849 if (bio->bi_vcnt != 1 ||
850 bio->bi_idx != 0)
851 goto bad_map;
852 /* This is a one page bio that upper layers
853 * refuse to split for us, so we need to split it.
854 */
855 bp = bio_split(bio,
856 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
857
858 /* Each of these 'make_request' calls will call 'wait_barrier'.
859 * If the first succeeds but the second blocks due to the resync
860 * thread raising the barrier, we will deadlock because the
861 * IO to the underlying device will be queued in generic_make_request
862 * and will never complete, so will never reduce nr_pending.
863 * So increment nr_waiting here so no new raise_barriers will
864 * succeed, and so the second wait_barrier cannot block.
865 */
866 spin_lock_irq(&conf->resync_lock);
867 conf->nr_waiting++;
868 spin_unlock_irq(&conf->resync_lock);
869
870 if (make_request(mddev, &bp->bio1))
871 generic_make_request(&bp->bio1);
872 if (make_request(mddev, &bp->bio2))
873 generic_make_request(&bp->bio2);
874
875 spin_lock_irq(&conf->resync_lock);
876 conf->nr_waiting--;
877 wake_up(&conf->wait_barrier);
878 spin_unlock_irq(&conf->resync_lock);
879
880 bio_pair_release(bp);
881 return 0;
882 bad_map:
883 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
884 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
885 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
886
887 bio_io_error(bio);
888 return 0;
889 }
890
891 md_write_start(mddev, bio);
892
893 /*
894 * Register the new request and wait if the reconstruction
895 * thread has put up a bar for new requests.
896 * Continue immediately if no resync is active currently.
897 */
898 wait_barrier(conf);
899
900 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
901
902 r10_bio->master_bio = bio;
903 r10_bio->sectors = bio->bi_size >> 9;
904
905 r10_bio->mddev = mddev;
906 r10_bio->sector = bio->bi_sector;
907 r10_bio->state = 0;
908
909 /* We might need to issue multiple reads to different
910 * devices if there are bad blocks around, so we keep
911 * track of the number of reads in bio->bi_phys_segments.
912 * If this is 0, there is only one r10_bio and no locking
913 * will be needed when the request completes. If it is
914 * non-zero, then it is the number of not-completed requests.
915 */
916 bio->bi_phys_segments = 0;
917 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
918
919 if (rw == READ) {
920 /*
921 * read balancing logic:
922 */
923 int disk;
924 int slot;
925
926 read_again:
927 disk = read_balance(conf, r10_bio, &max_sectors);
928 slot = r10_bio->read_slot;
929 if (disk < 0) {
930 raid_end_bio_io(r10_bio);
931 return 0;
932 }
933 mirror = conf->mirrors + disk;
934
935 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
936 md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
937 max_sectors);
938
939 r10_bio->devs[slot].bio = read_bio;
940
941 read_bio->bi_sector = r10_bio->devs[slot].addr +
942 mirror->rdev->data_offset;
943 read_bio->bi_bdev = mirror->rdev->bdev;
944 read_bio->bi_end_io = raid10_end_read_request;
945 read_bio->bi_rw = READ | do_sync;
946 read_bio->bi_private = r10_bio;
947
948 if (max_sectors < r10_bio->sectors) {
949 /* Could not read all from this device, so we will
950 * need another r10_bio.
951 */
952 sectors_handled = (r10_bio->sectors + max_sectors
953 - bio->bi_sector);
954 r10_bio->sectors = max_sectors;
955 spin_lock_irq(&conf->device_lock);
956 if (bio->bi_phys_segments == 0)
957 bio->bi_phys_segments = 2;
958 else
959 bio->bi_phys_segments++;
960 spin_unlock(&conf->device_lock);
961 /* Cannot call generic_make_request directly
962 * as that will be queued in __generic_make_request
963 * and subsequent mempool_alloc might block
964 * waiting for it. so hand bio over to raid10d.
965 */
966 reschedule_retry(r10_bio);
967
968 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
969
970 r10_bio->master_bio = bio;
971 r10_bio->sectors = ((bio->bi_size >> 9)
972 - sectors_handled);
973 r10_bio->state = 0;
974 r10_bio->mddev = mddev;
975 r10_bio->sector = bio->bi_sector + sectors_handled;
976 goto read_again;
977 } else
978 generic_make_request(read_bio);
979 return 0;
980 }
981
982 /*
983 * WRITE:
984 */
985 /* first select target devices under rcu_lock and
986 * inc refcount on their rdev. Record them by setting
987 * bios[x] to bio
988 * If there are known/acknowledged bad blocks on any device
989 * on which we have seen a write error, we want to avoid
990 * writing to those blocks. This potentially requires several
991 * writes to write around the bad blocks. Each set of writes
992 * gets its own r10_bio with a set of bios attached. The number
993 * of r10_bios is recored in bio->bi_phys_segments just as with
994 * the read case.
995 */
996 plugged = mddev_check_plugged(mddev);
997
998 raid10_find_phys(conf, r10_bio);
999 retry_write:
1000 blocked_rdev = NULL;
1001 rcu_read_lock();
1002 max_sectors = r10_bio->sectors;
1003
1004 for (i = 0; i < conf->copies; i++) {
1005 int d = r10_bio->devs[i].devnum;
1006 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
1007 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1008 atomic_inc(&rdev->nr_pending);
1009 blocked_rdev = rdev;
1010 break;
1011 }
1012 r10_bio->devs[i].bio = NULL;
1013 if (!rdev || test_bit(Faulty, &rdev->flags)) {
1014 set_bit(R10BIO_Degraded, &r10_bio->state);
1015 continue;
1016 }
1017 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1018 sector_t first_bad;
1019 sector_t dev_sector = r10_bio->devs[i].addr;
1020 int bad_sectors;
1021 int is_bad;
1022
1023 is_bad = is_badblock(rdev, dev_sector,
1024 max_sectors,
1025 &first_bad, &bad_sectors);
1026 if (is_bad < 0) {
1027 /* Mustn't write here until the bad block
1028 * is acknowledged
1029 */
1030 atomic_inc(&rdev->nr_pending);
1031 set_bit(BlockedBadBlocks, &rdev->flags);
1032 blocked_rdev = rdev;
1033 break;
1034 }
1035 if (is_bad && first_bad <= dev_sector) {
1036 /* Cannot write here at all */
1037 bad_sectors -= (dev_sector - first_bad);
1038 if (bad_sectors < max_sectors)
1039 /* Mustn't write more than bad_sectors
1040 * to other devices yet
1041 */
1042 max_sectors = bad_sectors;
1043 /* We don't set R10BIO_Degraded as that
1044 * only applies if the disk is missing,
1045 * so it might be re-added, and we want to
1046 * know to recover this chunk.
1047 * In this case the device is here, and the
1048 * fact that this chunk is not in-sync is
1049 * recorded in the bad block log.
1050 */
1051 continue;
1052 }
1053 if (is_bad) {
1054 int good_sectors = first_bad - dev_sector;
1055 if (good_sectors < max_sectors)
1056 max_sectors = good_sectors;
1057 }
1058 }
1059 r10_bio->devs[i].bio = bio;
1060 atomic_inc(&rdev->nr_pending);
1061 }
1062 rcu_read_unlock();
1063
1064 if (unlikely(blocked_rdev)) {
1065 /* Have to wait for this device to get unblocked, then retry */
1066 int j;
1067 int d;
1068
1069 for (j = 0; j < i; j++)
1070 if (r10_bio->devs[j].bio) {
1071 d = r10_bio->devs[j].devnum;
1072 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1073 }
1074 allow_barrier(conf);
1075 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1076 wait_barrier(conf);
1077 goto retry_write;
1078 }
1079
1080 if (max_sectors < r10_bio->sectors) {
1081 /* We are splitting this into multiple parts, so
1082 * we need to prepare for allocating another r10_bio.
1083 */
1084 r10_bio->sectors = max_sectors;
1085 spin_lock_irq(&conf->device_lock);
1086 if (bio->bi_phys_segments == 0)
1087 bio->bi_phys_segments = 2;
1088 else
1089 bio->bi_phys_segments++;
1090 spin_unlock_irq(&conf->device_lock);
1091 }
1092 sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
1093
1094 atomic_set(&r10_bio->remaining, 1);
1095 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
1096
1097 for (i = 0; i < conf->copies; i++) {
1098 struct bio *mbio;
1099 int d = r10_bio->devs[i].devnum;
1100 if (!r10_bio->devs[i].bio)
1101 continue;
1102
1103 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1104 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1105 max_sectors);
1106 r10_bio->devs[i].bio = mbio;
1107
1108 mbio->bi_sector = (r10_bio->devs[i].addr+
1109 conf->mirrors[d].rdev->data_offset);
1110 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1111 mbio->bi_end_io = raid10_end_write_request;
1112 mbio->bi_rw = WRITE | do_sync | do_fua;
1113 mbio->bi_private = r10_bio;
1114
1115 atomic_inc(&r10_bio->remaining);
1116 spin_lock_irqsave(&conf->device_lock, flags);
1117 bio_list_add(&conf->pending_bio_list, mbio);
1118 spin_unlock_irqrestore(&conf->device_lock, flags);
1119 }
1120
1121 if (atomic_dec_and_test(&r10_bio->remaining)) {
1122 /* This matches the end of raid10_end_write_request() */
1123 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
1124 r10_bio->sectors,
1125 !test_bit(R10BIO_Degraded, &r10_bio->state),
1126 0);
1127 md_write_end(mddev);
1128 raid_end_bio_io(r10_bio);
1129 }
1130
1131 /* In case raid10d snuck in to freeze_array */
1132 wake_up(&conf->wait_barrier);
1133
1134 if (sectors_handled < (bio->bi_size >> 9)) {
1135 /* We need another r1_bio. It has already been counted
1136 * in bio->bi_phys_segments.
1137 */
1138 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1139
1140 r10_bio->master_bio = bio;
1141 r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1142
1143 r10_bio->mddev = mddev;
1144 r10_bio->sector = bio->bi_sector + sectors_handled;
1145 r10_bio->state = 0;
1146 goto retry_write;
1147 }
1148
1149 if (do_sync || !mddev->bitmap || !plugged)
1150 md_wakeup_thread(mddev->thread);
1151 return 0;
1152 }
1153
1154 static void status(struct seq_file *seq, mddev_t *mddev)
1155 {
1156 conf_t *conf = mddev->private;
1157 int i;
1158
1159 if (conf->near_copies < conf->raid_disks)
1160 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
1161 if (conf->near_copies > 1)
1162 seq_printf(seq, " %d near-copies", conf->near_copies);
1163 if (conf->far_copies > 1) {
1164 if (conf->far_offset)
1165 seq_printf(seq, " %d offset-copies", conf->far_copies);
1166 else
1167 seq_printf(seq, " %d far-copies", conf->far_copies);
1168 }
1169 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
1170 conf->raid_disks - mddev->degraded);
1171 for (i = 0; i < conf->raid_disks; i++)
1172 seq_printf(seq, "%s",
1173 conf->mirrors[i].rdev &&
1174 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
1175 seq_printf(seq, "]");
1176 }
1177
1178 /* check if there are enough drives for
1179 * every block to appear on atleast one.
1180 * Don't consider the device numbered 'ignore'
1181 * as we might be about to remove it.
1182 */
1183 static int enough(conf_t *conf, int ignore)
1184 {
1185 int first = 0;
1186
1187 do {
1188 int n = conf->copies;
1189 int cnt = 0;
1190 while (n--) {
1191 if (conf->mirrors[first].rdev &&
1192 first != ignore)
1193 cnt++;
1194 first = (first+1) % conf->raid_disks;
1195 }
1196 if (cnt == 0)
1197 return 0;
1198 } while (first != 0);
1199 return 1;
1200 }
1201
1202 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1203 {
1204 char b[BDEVNAME_SIZE];
1205 conf_t *conf = mddev->private;
1206
1207 /*
1208 * If it is not operational, then we have already marked it as dead
1209 * else if it is the last working disks, ignore the error, let the
1210 * next level up know.
1211 * else mark the drive as failed
1212 */
1213 if (test_bit(In_sync, &rdev->flags)
1214 && !enough(conf, rdev->raid_disk))
1215 /*
1216 * Don't fail the drive, just return an IO error.
1217 */
1218 return;
1219 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1220 unsigned long flags;
1221 spin_lock_irqsave(&conf->device_lock, flags);
1222 mddev->degraded++;
1223 spin_unlock_irqrestore(&conf->device_lock, flags);
1224 /*
1225 * if recovery is running, make sure it aborts.
1226 */
1227 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1228 }
1229 set_bit(Blocked, &rdev->flags);
1230 set_bit(Faulty, &rdev->flags);
1231 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1232 printk(KERN_ALERT
1233 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1234 "md/raid10:%s: Operation continuing on %d devices.\n",
1235 mdname(mddev), bdevname(rdev->bdev, b),
1236 mdname(mddev), conf->raid_disks - mddev->degraded);
1237 }
1238
1239 static void print_conf(conf_t *conf)
1240 {
1241 int i;
1242 mirror_info_t *tmp;
1243
1244 printk(KERN_DEBUG "RAID10 conf printout:\n");
1245 if (!conf) {
1246 printk(KERN_DEBUG "(!conf)\n");
1247 return;
1248 }
1249 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1250 conf->raid_disks);
1251
1252 for (i = 0; i < conf->raid_disks; i++) {
1253 char b[BDEVNAME_SIZE];
1254 tmp = conf->mirrors + i;
1255 if (tmp->rdev)
1256 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
1257 i, !test_bit(In_sync, &tmp->rdev->flags),
1258 !test_bit(Faulty, &tmp->rdev->flags),
1259 bdevname(tmp->rdev->bdev,b));
1260 }
1261 }
1262
1263 static void close_sync(conf_t *conf)
1264 {
1265 wait_barrier(conf);
1266 allow_barrier(conf);
1267
1268 mempool_destroy(conf->r10buf_pool);
1269 conf->r10buf_pool = NULL;
1270 }
1271
1272 static int raid10_spare_active(mddev_t *mddev)
1273 {
1274 int i;
1275 conf_t *conf = mddev->private;
1276 mirror_info_t *tmp;
1277 int count = 0;
1278 unsigned long flags;
1279
1280 /*
1281 * Find all non-in_sync disks within the RAID10 configuration
1282 * and mark them in_sync
1283 */
1284 for (i = 0; i < conf->raid_disks; i++) {
1285 tmp = conf->mirrors + i;
1286 if (tmp->rdev
1287 && !test_bit(Faulty, &tmp->rdev->flags)
1288 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1289 count++;
1290 sysfs_notify_dirent(tmp->rdev->sysfs_state);
1291 }
1292 }
1293 spin_lock_irqsave(&conf->device_lock, flags);
1294 mddev->degraded -= count;
1295 spin_unlock_irqrestore(&conf->device_lock, flags);
1296
1297 print_conf(conf);
1298 return count;
1299 }
1300
1301
1302 static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1303 {
1304 conf_t *conf = mddev->private;
1305 int err = -EEXIST;
1306 int mirror;
1307 int first = 0;
1308 int last = conf->raid_disks - 1;
1309
1310 if (mddev->recovery_cp < MaxSector)
1311 /* only hot-add to in-sync arrays, as recovery is
1312 * very different from resync
1313 */
1314 return -EBUSY;
1315 if (!enough(conf, -1))
1316 return -EINVAL;
1317
1318 if (rdev->raid_disk >= 0)
1319 first = last = rdev->raid_disk;
1320
1321 if (rdev->saved_raid_disk >= first &&
1322 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1323 mirror = rdev->saved_raid_disk;
1324 else
1325 mirror = first;
1326 for ( ; mirror <= last ; mirror++) {
1327 mirror_info_t *p = &conf->mirrors[mirror];
1328 if (p->recovery_disabled == mddev->recovery_disabled)
1329 continue;
1330 if (!p->rdev)
1331 continue;
1332
1333 disk_stack_limits(mddev->gendisk, rdev->bdev,
1334 rdev->data_offset << 9);
1335 /* as we don't honour merge_bvec_fn, we must
1336 * never risk violating it, so limit
1337 * ->max_segments to one lying with a single
1338 * page, as a one page request is never in
1339 * violation.
1340 */
1341 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1342 blk_queue_max_segments(mddev->queue, 1);
1343 blk_queue_segment_boundary(mddev->queue,
1344 PAGE_CACHE_SIZE - 1);
1345 }
1346
1347 p->head_position = 0;
1348 rdev->raid_disk = mirror;
1349 err = 0;
1350 if (rdev->saved_raid_disk != mirror)
1351 conf->fullsync = 1;
1352 rcu_assign_pointer(p->rdev, rdev);
1353 break;
1354 }
1355
1356 md_integrity_add_rdev(rdev, mddev);
1357 print_conf(conf);
1358 return err;
1359 }
1360
1361 static int raid10_remove_disk(mddev_t *mddev, int number)
1362 {
1363 conf_t *conf = mddev->private;
1364 int err = 0;
1365 mdk_rdev_t *rdev;
1366 mirror_info_t *p = conf->mirrors+ number;
1367
1368 print_conf(conf);
1369 rdev = p->rdev;
1370 if (rdev) {
1371 if (test_bit(In_sync, &rdev->flags) ||
1372 atomic_read(&rdev->nr_pending)) {
1373 err = -EBUSY;
1374 goto abort;
1375 }
1376 /* Only remove faulty devices in recovery
1377 * is not possible.
1378 */
1379 if (!test_bit(Faulty, &rdev->flags) &&
1380 mddev->recovery_disabled != p->recovery_disabled &&
1381 enough(conf, -1)) {
1382 err = -EBUSY;
1383 goto abort;
1384 }
1385 p->rdev = NULL;
1386 synchronize_rcu();
1387 if (atomic_read(&rdev->nr_pending)) {
1388 /* lost the race, try later */
1389 err = -EBUSY;
1390 p->rdev = rdev;
1391 goto abort;
1392 }
1393 err = md_integrity_register(mddev);
1394 }
1395 abort:
1396
1397 print_conf(conf);
1398 return err;
1399 }
1400
1401
1402 static void end_sync_read(struct bio *bio, int error)
1403 {
1404 r10bio_t *r10_bio = bio->bi_private;
1405 conf_t *conf = r10_bio->mddev->private;
1406 int d;
1407
1408 d = find_bio_disk(conf, r10_bio, bio, NULL);
1409
1410 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1411 set_bit(R10BIO_Uptodate, &r10_bio->state);
1412 else {
1413 atomic_add(r10_bio->sectors,
1414 &conf->mirrors[d].rdev->corrected_errors);
1415 if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
1416 md_error(r10_bio->mddev,
1417 conf->mirrors[d].rdev);
1418 }
1419
1420 /* for reconstruct, we always reschedule after a read.
1421 * for resync, only after all reads
1422 */
1423 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1424 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1425 atomic_dec_and_test(&r10_bio->remaining)) {
1426 /* we have read all the blocks,
1427 * do the comparison in process context in raid10d
1428 */
1429 reschedule_retry(r10_bio);
1430 }
1431 }
1432
1433 static void end_sync_write(struct bio *bio, int error)
1434 {
1435 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1436 r10bio_t *r10_bio = bio->bi_private;
1437 mddev_t *mddev = r10_bio->mddev;
1438 conf_t *conf = mddev->private;
1439 int d;
1440 sector_t first_bad;
1441 int bad_sectors;
1442 int slot;
1443
1444 d = find_bio_disk(conf, r10_bio, bio, &slot);
1445
1446 if (!uptodate)
1447 md_error(mddev, conf->mirrors[d].rdev);
1448 else if (is_badblock(conf->mirrors[d].rdev,
1449 r10_bio->devs[slot].addr,
1450 r10_bio->sectors,
1451 &first_bad, &bad_sectors))
1452 set_bit(R10BIO_MadeGood, &r10_bio->state);
1453
1454 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1455 while (atomic_dec_and_test(&r10_bio->remaining)) {
1456 if (r10_bio->master_bio == NULL) {
1457 /* the primary of several recovery bios */
1458 sector_t s = r10_bio->sectors;
1459 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
1460 reschedule_retry(r10_bio);
1461 else
1462 put_buf(r10_bio);
1463 md_done_sync(mddev, s, 1);
1464 break;
1465 } else {
1466 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1467 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
1468 reschedule_retry(r10_bio);
1469 else
1470 put_buf(r10_bio);
1471 r10_bio = r10_bio2;
1472 }
1473 }
1474 }
1475
1476 /*
1477 * Note: sync and recover and handled very differently for raid10
1478 * This code is for resync.
1479 * For resync, we read through virtual addresses and read all blocks.
1480 * If there is any error, we schedule a write. The lowest numbered
1481 * drive is authoritative.
1482 * However requests come for physical address, so we need to map.
1483 * For every physical address there are raid_disks/copies virtual addresses,
1484 * which is always are least one, but is not necessarly an integer.
1485 * This means that a physical address can span multiple chunks, so we may
1486 * have to submit multiple io requests for a single sync request.
1487 */
1488 /*
1489 * We check if all blocks are in-sync and only write to blocks that
1490 * aren't in sync
1491 */
1492 static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1493 {
1494 conf_t *conf = mddev->private;
1495 int i, first;
1496 struct bio *tbio, *fbio;
1497
1498 atomic_set(&r10_bio->remaining, 1);
1499
1500 /* find the first device with a block */
1501 for (i=0; i<conf->copies; i++)
1502 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1503 break;
1504
1505 if (i == conf->copies)
1506 goto done;
1507
1508 first = i;
1509 fbio = r10_bio->devs[i].bio;
1510
1511 /* now find blocks with errors */
1512 for (i=0 ; i < conf->copies ; i++) {
1513 int j, d;
1514 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
1515
1516 tbio = r10_bio->devs[i].bio;
1517
1518 if (tbio->bi_end_io != end_sync_read)
1519 continue;
1520 if (i == first)
1521 continue;
1522 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1523 /* We know that the bi_io_vec layout is the same for
1524 * both 'first' and 'i', so we just compare them.
1525 * All vec entries are PAGE_SIZE;
1526 */
1527 for (j = 0; j < vcnt; j++)
1528 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1529 page_address(tbio->bi_io_vec[j].bv_page),
1530 PAGE_SIZE))
1531 break;
1532 if (j == vcnt)
1533 continue;
1534 mddev->resync_mismatches += r10_bio->sectors;
1535 }
1536 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1537 /* Don't fix anything. */
1538 continue;
1539 /* Ok, we need to write this bio
1540 * First we need to fixup bv_offset, bv_len and
1541 * bi_vecs, as the read request might have corrupted these
1542 */
1543 tbio->bi_vcnt = vcnt;
1544 tbio->bi_size = r10_bio->sectors << 9;
1545 tbio->bi_idx = 0;
1546 tbio->bi_phys_segments = 0;
1547 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1548 tbio->bi_flags |= 1 << BIO_UPTODATE;
1549 tbio->bi_next = NULL;
1550 tbio->bi_rw = WRITE;
1551 tbio->bi_private = r10_bio;
1552 tbio->bi_sector = r10_bio->devs[i].addr;
1553
1554 for (j=0; j < vcnt ; j++) {
1555 tbio->bi_io_vec[j].bv_offset = 0;
1556 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1557
1558 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1559 page_address(fbio->bi_io_vec[j].bv_page),
1560 PAGE_SIZE);
1561 }
1562 tbio->bi_end_io = end_sync_write;
1563
1564 d = r10_bio->devs[i].devnum;
1565 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1566 atomic_inc(&r10_bio->remaining);
1567 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1568
1569 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1570 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1571 generic_make_request(tbio);
1572 }
1573
1574 done:
1575 if (atomic_dec_and_test(&r10_bio->remaining)) {
1576 md_done_sync(mddev, r10_bio->sectors, 1);
1577 put_buf(r10_bio);
1578 }
1579 }
1580
1581 /*
1582 * Now for the recovery code.
1583 * Recovery happens across physical sectors.
1584 * We recover all non-is_sync drives by finding the virtual address of
1585 * each, and then choose a working drive that also has that virt address.
1586 * There is a separate r10_bio for each non-in_sync drive.
1587 * Only the first two slots are in use. The first for reading,
1588 * The second for writing.
1589 *
1590 */
1591
1592 static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1593 {
1594 conf_t *conf = mddev->private;
1595 int d;
1596 struct bio *wbio;
1597
1598 /*
1599 * share the pages with the first bio
1600 * and submit the write request
1601 */
1602 wbio = r10_bio->devs[1].bio;
1603 d = r10_bio->devs[1].devnum;
1604
1605 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1606 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
1607 if (test_bit(R10BIO_Uptodate, &r10_bio->state))
1608 generic_make_request(wbio);
1609 else {
1610 printk(KERN_NOTICE
1611 "md/raid10:%s: recovery aborted due to read error\n",
1612 mdname(mddev));
1613 conf->mirrors[d].recovery_disabled = mddev->recovery_disabled;
1614 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1615 bio_endio(wbio, 0);
1616 }
1617 }
1618
1619
1620 /*
1621 * Used by fix_read_error() to decay the per rdev read_errors.
1622 * We halve the read error count for every hour that has elapsed
1623 * since the last recorded read error.
1624 *
1625 */
1626 static void check_decay_read_errors(mddev_t *mddev, mdk_rdev_t *rdev)
1627 {
1628 struct timespec cur_time_mon;
1629 unsigned long hours_since_last;
1630 unsigned int read_errors = atomic_read(&rdev->read_errors);
1631
1632 ktime_get_ts(&cur_time_mon);
1633
1634 if (rdev->last_read_error.tv_sec == 0 &&
1635 rdev->last_read_error.tv_nsec == 0) {
1636 /* first time we've seen a read error */
1637 rdev->last_read_error = cur_time_mon;
1638 return;
1639 }
1640
1641 hours_since_last = (cur_time_mon.tv_sec -
1642 rdev->last_read_error.tv_sec) / 3600;
1643
1644 rdev->last_read_error = cur_time_mon;
1645
1646 /*
1647 * if hours_since_last is > the number of bits in read_errors
1648 * just set read errors to 0. We do this to avoid
1649 * overflowing the shift of read_errors by hours_since_last.
1650 */
1651 if (hours_since_last >= 8 * sizeof(read_errors))
1652 atomic_set(&rdev->read_errors, 0);
1653 else
1654 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
1655 }
1656
1657 /*
1658 * This is a kernel thread which:
1659 *
1660 * 1. Retries failed read operations on working mirrors.
1661 * 2. Updates the raid superblock when problems encounter.
1662 * 3. Performs writes following reads for array synchronising.
1663 */
1664
1665 static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
1666 {
1667 int sect = 0; /* Offset from r10_bio->sector */
1668 int sectors = r10_bio->sectors;
1669 mdk_rdev_t*rdev;
1670 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
1671 int d = r10_bio->devs[r10_bio->read_slot].devnum;
1672
1673 /* still own a reference to this rdev, so it cannot
1674 * have been cleared recently.
1675 */
1676 rdev = conf->mirrors[d].rdev;
1677
1678 if (test_bit(Faulty, &rdev->flags))
1679 /* drive has already been failed, just ignore any
1680 more fix_read_error() attempts */
1681 return;
1682
1683 check_decay_read_errors(mddev, rdev);
1684 atomic_inc(&rdev->read_errors);
1685 if (atomic_read(&rdev->read_errors) > max_read_errors) {
1686 char b[BDEVNAME_SIZE];
1687 bdevname(rdev->bdev, b);
1688
1689 printk(KERN_NOTICE
1690 "md/raid10:%s: %s: Raid device exceeded "
1691 "read_error threshold [cur %d:max %d]\n",
1692 mdname(mddev), b,
1693 atomic_read(&rdev->read_errors), max_read_errors);
1694 printk(KERN_NOTICE
1695 "md/raid10:%s: %s: Failing raid device\n",
1696 mdname(mddev), b);
1697 md_error(mddev, conf->mirrors[d].rdev);
1698 return;
1699 }
1700
1701 while(sectors) {
1702 int s = sectors;
1703 int sl = r10_bio->read_slot;
1704 int success = 0;
1705 int start;
1706
1707 if (s > (PAGE_SIZE>>9))
1708 s = PAGE_SIZE >> 9;
1709
1710 rcu_read_lock();
1711 do {
1712 sector_t first_bad;
1713 int bad_sectors;
1714
1715 d = r10_bio->devs[sl].devnum;
1716 rdev = rcu_dereference(conf->mirrors[d].rdev);
1717 if (rdev &&
1718 test_bit(In_sync, &rdev->flags) &&
1719 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
1720 &first_bad, &bad_sectors) == 0) {
1721 atomic_inc(&rdev->nr_pending);
1722 rcu_read_unlock();
1723 success = sync_page_io(rdev,
1724 r10_bio->devs[sl].addr +
1725 sect,
1726 s<<9,
1727 conf->tmppage, READ, false);
1728 rdev_dec_pending(rdev, mddev);
1729 rcu_read_lock();
1730 if (success)
1731 break;
1732 }
1733 sl++;
1734 if (sl == conf->copies)
1735 sl = 0;
1736 } while (!success && sl != r10_bio->read_slot);
1737 rcu_read_unlock();
1738
1739 if (!success) {
1740 /* Cannot read from anywhere -- bye bye array */
1741 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
1742 md_error(mddev, conf->mirrors[dn].rdev);
1743 break;
1744 }
1745
1746 start = sl;
1747 /* write it back and re-read */
1748 rcu_read_lock();
1749 while (sl != r10_bio->read_slot) {
1750 char b[BDEVNAME_SIZE];
1751
1752 if (sl==0)
1753 sl = conf->copies;
1754 sl--;
1755 d = r10_bio->devs[sl].devnum;
1756 rdev = rcu_dereference(conf->mirrors[d].rdev);
1757 if (!rdev ||
1758 !test_bit(In_sync, &rdev->flags))
1759 continue;
1760
1761 atomic_inc(&rdev->nr_pending);
1762 rcu_read_unlock();
1763 if (sync_page_io(rdev,
1764 r10_bio->devs[sl].addr +
1765 sect,
1766 s<<9, conf->tmppage, WRITE, false)
1767 == 0) {
1768 /* Well, this device is dead */
1769 printk(KERN_NOTICE
1770 "md/raid10:%s: read correction "
1771 "write failed"
1772 " (%d sectors at %llu on %s)\n",
1773 mdname(mddev), s,
1774 (unsigned long long)(
1775 sect + rdev->data_offset),
1776 bdevname(rdev->bdev, b));
1777 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
1778 "drive\n",
1779 mdname(mddev),
1780 bdevname(rdev->bdev, b));
1781 md_error(mddev, rdev);
1782 }
1783 rdev_dec_pending(rdev, mddev);
1784 rcu_read_lock();
1785 }
1786 sl = start;
1787 while (sl != r10_bio->read_slot) {
1788 char b[BDEVNAME_SIZE];
1789
1790 if (sl==0)
1791 sl = conf->copies;
1792 sl--;
1793 d = r10_bio->devs[sl].devnum;
1794 rdev = rcu_dereference(conf->mirrors[d].rdev);
1795 if (!rdev ||
1796 !test_bit(In_sync, &rdev->flags))
1797 continue;
1798
1799 atomic_inc(&rdev->nr_pending);
1800 rcu_read_unlock();
1801 if (sync_page_io(rdev,
1802 r10_bio->devs[sl].addr +
1803 sect,
1804 s<<9, conf->tmppage,
1805 READ, false) == 0) {
1806 /* Well, this device is dead */
1807 printk(KERN_NOTICE
1808 "md/raid10:%s: unable to read back "
1809 "corrected sectors"
1810 " (%d sectors at %llu on %s)\n",
1811 mdname(mddev), s,
1812 (unsigned long long)(
1813 sect + rdev->data_offset),
1814 bdevname(rdev->bdev, b));
1815 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
1816 "drive\n",
1817 mdname(mddev),
1818 bdevname(rdev->bdev, b));
1819
1820 md_error(mddev, rdev);
1821 } else {
1822 printk(KERN_INFO
1823 "md/raid10:%s: read error corrected"
1824 " (%d sectors at %llu on %s)\n",
1825 mdname(mddev), s,
1826 (unsigned long long)(
1827 sect + rdev->data_offset),
1828 bdevname(rdev->bdev, b));
1829 atomic_add(s, &rdev->corrected_errors);
1830 }
1831
1832 rdev_dec_pending(rdev, mddev);
1833 rcu_read_lock();
1834 }
1835 rcu_read_unlock();
1836
1837 sectors -= s;
1838 sect += s;
1839 }
1840 }
1841
1842 static void handle_read_error(mddev_t *mddev, r10bio_t *r10_bio)
1843 {
1844 int slot = r10_bio->read_slot;
1845 int mirror = r10_bio->devs[slot].devnum;
1846 struct bio *bio;
1847 conf_t *conf = mddev->private;
1848 mdk_rdev_t *rdev;
1849 char b[BDEVNAME_SIZE];
1850 unsigned long do_sync;
1851 int max_sectors;
1852
1853 /* we got a read error. Maybe the drive is bad. Maybe just
1854 * the block and we can fix it.
1855 * We freeze all other IO, and try reading the block from
1856 * other devices. When we find one, we re-write
1857 * and check it that fixes the read error.
1858 * This is all done synchronously while the array is
1859 * frozen.
1860 */
1861 if (mddev->ro == 0) {
1862 freeze_array(conf);
1863 fix_read_error(conf, mddev, r10_bio);
1864 unfreeze_array(conf);
1865 }
1866 rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
1867
1868 bio = r10_bio->devs[slot].bio;
1869 bdevname(bio->bi_bdev, b);
1870 r10_bio->devs[slot].bio =
1871 mddev->ro ? IO_BLOCKED : NULL;
1872 read_more:
1873 mirror = read_balance(conf, r10_bio, &max_sectors);
1874 if (mirror == -1) {
1875 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
1876 " read error for block %llu\n",
1877 mdname(mddev), b,
1878 (unsigned long long)r10_bio->sector);
1879 raid_end_bio_io(r10_bio);
1880 bio_put(bio);
1881 return;
1882 }
1883
1884 do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
1885 if (bio)
1886 bio_put(bio);
1887 slot = r10_bio->read_slot;
1888 rdev = conf->mirrors[mirror].rdev;
1889 printk_ratelimited(
1890 KERN_ERR
1891 "md/raid10:%s: %s: redirecting"
1892 "sector %llu to another mirror\n",
1893 mdname(mddev),
1894 bdevname(rdev->bdev, b),
1895 (unsigned long long)r10_bio->sector);
1896 bio = bio_clone_mddev(r10_bio->master_bio,
1897 GFP_NOIO, mddev);
1898 md_trim_bio(bio,
1899 r10_bio->sector - bio->bi_sector,
1900 max_sectors);
1901 r10_bio->devs[slot].bio = bio;
1902 bio->bi_sector = r10_bio->devs[slot].addr
1903 + rdev->data_offset;
1904 bio->bi_bdev = rdev->bdev;
1905 bio->bi_rw = READ | do_sync;
1906 bio->bi_private = r10_bio;
1907 bio->bi_end_io = raid10_end_read_request;
1908 if (max_sectors < r10_bio->sectors) {
1909 /* Drat - have to split this up more */
1910 struct bio *mbio = r10_bio->master_bio;
1911 int sectors_handled =
1912 r10_bio->sector + max_sectors
1913 - mbio->bi_sector;
1914 r10_bio->sectors = max_sectors;
1915 spin_lock_irq(&conf->device_lock);
1916 if (mbio->bi_phys_segments == 0)
1917 mbio->bi_phys_segments = 2;
1918 else
1919 mbio->bi_phys_segments++;
1920 spin_unlock_irq(&conf->device_lock);
1921 generic_make_request(bio);
1922 bio = NULL;
1923
1924 r10_bio = mempool_alloc(conf->r10bio_pool,
1925 GFP_NOIO);
1926 r10_bio->master_bio = mbio;
1927 r10_bio->sectors = (mbio->bi_size >> 9)
1928 - sectors_handled;
1929 r10_bio->state = 0;
1930 set_bit(R10BIO_ReadError,
1931 &r10_bio->state);
1932 r10_bio->mddev = mddev;
1933 r10_bio->sector = mbio->bi_sector
1934 + sectors_handled;
1935
1936 goto read_more;
1937 } else
1938 generic_make_request(bio);
1939 }
1940
1941 static void handle_write_completed(conf_t *conf, r10bio_t *r10_bio)
1942 {
1943 /* Some sort of write request has finished and it
1944 * succeeded in writing where we thought there was a
1945 * bad block. So forget the bad block.
1946 */
1947 int m;
1948 mdk_rdev_t *rdev;
1949
1950 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
1951 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1952 for (m = 0; m < conf->copies; m++)
1953 if (r10_bio->devs[m].bio &&
1954 test_bit(BIO_UPTODATE,
1955 &r10_bio->devs[m].bio->bi_flags)) {
1956 int dev = r10_bio->devs[m].devnum;
1957 rdev = conf->mirrors[dev].rdev;
1958 rdev_clear_badblocks(
1959 rdev,
1960 r10_bio->devs[m].addr,
1961 r10_bio->sectors);
1962 }
1963 put_buf(r10_bio);
1964 } else {
1965 for (m = 0; m < conf->copies; m++)
1966 if (r10_bio->devs[m].bio == IO_MADE_GOOD) {
1967 int dev = r10_bio->devs[m].devnum;
1968 rdev = conf->mirrors[dev].rdev;
1969 rdev_clear_badblocks(
1970 rdev,
1971 r10_bio->devs[m].addr,
1972 r10_bio->sectors);
1973 rdev_dec_pending(rdev, conf->mddev);
1974 }
1975 raid_end_bio_io(r10_bio);
1976 }
1977 }
1978
1979 static void raid10d(mddev_t *mddev)
1980 {
1981 r10bio_t *r10_bio;
1982 unsigned long flags;
1983 conf_t *conf = mddev->private;
1984 struct list_head *head = &conf->retry_list;
1985 struct blk_plug plug;
1986
1987 md_check_recovery(mddev);
1988
1989 blk_start_plug(&plug);
1990 for (;;) {
1991
1992 flush_pending_writes(conf);
1993
1994 spin_lock_irqsave(&conf->device_lock, flags);
1995 if (list_empty(head)) {
1996 spin_unlock_irqrestore(&conf->device_lock, flags);
1997 break;
1998 }
1999 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
2000 list_del(head->prev);
2001 conf->nr_queued--;
2002 spin_unlock_irqrestore(&conf->device_lock, flags);
2003
2004 mddev = r10_bio->mddev;
2005 conf = mddev->private;
2006 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
2007 handle_write_completed(conf, r10_bio);
2008 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
2009 sync_request_write(mddev, r10_bio);
2010 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
2011 recovery_request_write(mddev, r10_bio);
2012 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
2013 handle_read_error(mddev, r10_bio);
2014 else {
2015 /* just a partial read to be scheduled from a
2016 * separate context
2017 */
2018 int slot = r10_bio->read_slot;
2019 generic_make_request(r10_bio->devs[slot].bio);
2020 }
2021
2022 cond_resched();
2023 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2024 md_check_recovery(mddev);
2025 }
2026 blk_finish_plug(&plug);
2027 }
2028
2029
2030 static int init_resync(conf_t *conf)
2031 {
2032 int buffs;
2033
2034 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
2035 BUG_ON(conf->r10buf_pool);
2036 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2037 if (!conf->r10buf_pool)
2038 return -ENOMEM;
2039 conf->next_resync = 0;
2040 return 0;
2041 }
2042
2043 /*
2044 * perform a "sync" on one "block"
2045 *
2046 * We need to make sure that no normal I/O request - particularly write
2047 * requests - conflict with active sync requests.
2048 *
2049 * This is achieved by tracking pending requests and a 'barrier' concept
2050 * that can be installed to exclude normal IO requests.
2051 *
2052 * Resync and recovery are handled very differently.
2053 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2054 *
2055 * For resync, we iterate over virtual addresses, read all copies,
2056 * and update if there are differences. If only one copy is live,
2057 * skip it.
2058 * For recovery, we iterate over physical addresses, read a good
2059 * value for each non-in_sync drive, and over-write.
2060 *
2061 * So, for recovery we may have several outstanding complex requests for a
2062 * given address, one for each out-of-sync device. We model this by allocating
2063 * a number of r10_bio structures, one for each out-of-sync device.
2064 * As we setup these structures, we collect all bio's together into a list
2065 * which we then process collectively to add pages, and then process again
2066 * to pass to generic_make_request.
2067 *
2068 * The r10_bio structures are linked using a borrowed master_bio pointer.
2069 * This link is counted in ->remaining. When the r10_bio that points to NULL
2070 * has its remaining count decremented to 0, the whole complex operation
2071 * is complete.
2072 *
2073 */
2074
2075 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr,
2076 int *skipped, int go_faster)
2077 {
2078 conf_t *conf = mddev->private;
2079 r10bio_t *r10_bio;
2080 struct bio *biolist = NULL, *bio;
2081 sector_t max_sector, nr_sectors;
2082 int i;
2083 int max_sync;
2084 sector_t sync_blocks;
2085 sector_t sectors_skipped = 0;
2086 int chunks_skipped = 0;
2087
2088 if (!conf->r10buf_pool)
2089 if (init_resync(conf))
2090 return 0;
2091
2092 skipped:
2093 max_sector = mddev->dev_sectors;
2094 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2095 max_sector = mddev->resync_max_sectors;
2096 if (sector_nr >= max_sector) {
2097 /* If we aborted, we need to abort the
2098 * sync on the 'current' bitmap chucks (there can
2099 * be several when recovering multiple devices).
2100 * as we may have started syncing it but not finished.
2101 * We can find the current address in
2102 * mddev->curr_resync, but for recovery,
2103 * we need to convert that to several
2104 * virtual addresses.
2105 */
2106 if (mddev->curr_resync < max_sector) { /* aborted */
2107 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2108 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2109 &sync_blocks, 1);
2110 else for (i=0; i<conf->raid_disks; i++) {
2111 sector_t sect =
2112 raid10_find_virt(conf, mddev->curr_resync, i);
2113 bitmap_end_sync(mddev->bitmap, sect,
2114 &sync_blocks, 1);
2115 }
2116 } else /* completed sync */
2117 conf->fullsync = 0;
2118
2119 bitmap_close_sync(mddev->bitmap);
2120 close_sync(conf);
2121 *skipped = 1;
2122 return sectors_skipped;
2123 }
2124 if (chunks_skipped >= conf->raid_disks) {
2125 /* if there has been nothing to do on any drive,
2126 * then there is nothing to do at all..
2127 */
2128 *skipped = 1;
2129 return (max_sector - sector_nr) + sectors_skipped;
2130 }
2131
2132 if (max_sector > mddev->resync_max)
2133 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2134
2135 /* make sure whole request will fit in a chunk - if chunks
2136 * are meaningful
2137 */
2138 if (conf->near_copies < conf->raid_disks &&
2139 max_sector > (sector_nr | conf->chunk_mask))
2140 max_sector = (sector_nr | conf->chunk_mask) + 1;
2141 /*
2142 * If there is non-resync activity waiting for us then
2143 * put in a delay to throttle resync.
2144 */
2145 if (!go_faster && conf->nr_waiting)
2146 msleep_interruptible(1000);
2147
2148 /* Again, very different code for resync and recovery.
2149 * Both must result in an r10bio with a list of bios that
2150 * have bi_end_io, bi_sector, bi_bdev set,
2151 * and bi_private set to the r10bio.
2152 * For recovery, we may actually create several r10bios
2153 * with 2 bios in each, that correspond to the bios in the main one.
2154 * In this case, the subordinate r10bios link back through a
2155 * borrowed master_bio pointer, and the counter in the master
2156 * includes a ref from each subordinate.
2157 */
2158 /* First, we decide what to do and set ->bi_end_io
2159 * To end_sync_read if we want to read, and
2160 * end_sync_write if we will want to write.
2161 */
2162
2163 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
2164 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2165 /* recovery... the complicated one */
2166 int j;
2167 r10_bio = NULL;
2168
2169 for (i=0 ; i<conf->raid_disks; i++) {
2170 int still_degraded;
2171 r10bio_t *rb2;
2172 sector_t sect;
2173 int must_sync;
2174 int any_working;
2175
2176 if (conf->mirrors[i].rdev == NULL ||
2177 test_bit(In_sync, &conf->mirrors[i].rdev->flags))
2178 continue;
2179
2180 still_degraded = 0;
2181 /* want to reconstruct this device */
2182 rb2 = r10_bio;
2183 sect = raid10_find_virt(conf, sector_nr, i);
2184 /* Unless we are doing a full sync, we only need
2185 * to recover the block if it is set in the bitmap
2186 */
2187 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2188 &sync_blocks, 1);
2189 if (sync_blocks < max_sync)
2190 max_sync = sync_blocks;
2191 if (!must_sync &&
2192 !conf->fullsync) {
2193 /* yep, skip the sync_blocks here, but don't assume
2194 * that there will never be anything to do here
2195 */
2196 chunks_skipped = -1;
2197 continue;
2198 }
2199
2200 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2201 raise_barrier(conf, rb2 != NULL);
2202 atomic_set(&r10_bio->remaining, 0);
2203
2204 r10_bio->master_bio = (struct bio*)rb2;
2205 if (rb2)
2206 atomic_inc(&rb2->remaining);
2207 r10_bio->mddev = mddev;
2208 set_bit(R10BIO_IsRecover, &r10_bio->state);
2209 r10_bio->sector = sect;
2210
2211 raid10_find_phys(conf, r10_bio);
2212
2213 /* Need to check if the array will still be
2214 * degraded
2215 */
2216 for (j=0; j<conf->raid_disks; j++)
2217 if (conf->mirrors[j].rdev == NULL ||
2218 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
2219 still_degraded = 1;
2220 break;
2221 }
2222
2223 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2224 &sync_blocks, still_degraded);
2225
2226 any_working = 0;
2227 for (j=0; j<conf->copies;j++) {
2228 int k;
2229 int d = r10_bio->devs[j].devnum;
2230 mdk_rdev_t *rdev;
2231 sector_t sector, first_bad;
2232 int bad_sectors;
2233 if (!conf->mirrors[d].rdev ||
2234 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
2235 continue;
2236 /* This is where we read from */
2237 any_working = 1;
2238 rdev = conf->mirrors[d].rdev;
2239 sector = r10_bio->devs[j].addr;
2240
2241 if (is_badblock(rdev, sector, max_sync,
2242 &first_bad, &bad_sectors)) {
2243 if (first_bad > sector)
2244 max_sync = first_bad - sector;
2245 else {
2246 bad_sectors -= (sector
2247 - first_bad);
2248 if (max_sync > bad_sectors)
2249 max_sync = bad_sectors;
2250 continue;
2251 }
2252 }
2253 bio = r10_bio->devs[0].bio;
2254 bio->bi_next = biolist;
2255 biolist = bio;
2256 bio->bi_private = r10_bio;
2257 bio->bi_end_io = end_sync_read;
2258 bio->bi_rw = READ;
2259 bio->bi_sector = r10_bio->devs[j].addr +
2260 conf->mirrors[d].rdev->data_offset;
2261 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2262 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2263 atomic_inc(&r10_bio->remaining);
2264 /* and we write to 'i' */
2265
2266 for (k=0; k<conf->copies; k++)
2267 if (r10_bio->devs[k].devnum == i)
2268 break;
2269 BUG_ON(k == conf->copies);
2270 bio = r10_bio->devs[1].bio;
2271 bio->bi_next = biolist;
2272 biolist = bio;
2273 bio->bi_private = r10_bio;
2274 bio->bi_end_io = end_sync_write;
2275 bio->bi_rw = WRITE;
2276 bio->bi_sector = r10_bio->devs[k].addr +
2277 conf->mirrors[i].rdev->data_offset;
2278 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
2279
2280 r10_bio->devs[0].devnum = d;
2281 r10_bio->devs[1].devnum = i;
2282
2283 break;
2284 }
2285 if (j == conf->copies) {
2286 /* Cannot recover, so abort the recovery or
2287 * record a bad block */
2288 put_buf(r10_bio);
2289 if (rb2)
2290 atomic_dec(&rb2->remaining);
2291 r10_bio = rb2;
2292 if (any_working) {
2293 /* problem is that there are bad blocks
2294 * on other device(s)
2295 */
2296 int k;
2297 for (k = 0; k < conf->copies; k++)
2298 if (r10_bio->devs[k].devnum == i)
2299 break;
2300 if (!rdev_set_badblocks(
2301 conf->mirrors[i].rdev,
2302 r10_bio->devs[k].addr,
2303 max_sync, 0))
2304 any_working = 0;
2305 }
2306 if (!any_working) {
2307 if (!test_and_set_bit(MD_RECOVERY_INTR,
2308 &mddev->recovery))
2309 printk(KERN_INFO "md/raid10:%s: insufficient "
2310 "working devices for recovery.\n",
2311 mdname(mddev));
2312 conf->mirrors[i].recovery_disabled
2313 = mddev->recovery_disabled;
2314 }
2315 break;
2316 }
2317 }
2318 if (biolist == NULL) {
2319 while (r10_bio) {
2320 r10bio_t *rb2 = r10_bio;
2321 r10_bio = (r10bio_t*) rb2->master_bio;
2322 rb2->master_bio = NULL;
2323 put_buf(rb2);
2324 }
2325 goto giveup;
2326 }
2327 } else {
2328 /* resync. Schedule a read for every block at this virt offset */
2329 int count = 0;
2330
2331 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
2332
2333 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2334 &sync_blocks, mddev->degraded) &&
2335 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
2336 &mddev->recovery)) {
2337 /* We can skip this block */
2338 *skipped = 1;
2339 return sync_blocks + sectors_skipped;
2340 }
2341 if (sync_blocks < max_sync)
2342 max_sync = sync_blocks;
2343 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2344
2345 r10_bio->mddev = mddev;
2346 atomic_set(&r10_bio->remaining, 0);
2347 raise_barrier(conf, 0);
2348 conf->next_resync = sector_nr;
2349
2350 r10_bio->master_bio = NULL;
2351 r10_bio->sector = sector_nr;
2352 set_bit(R10BIO_IsSync, &r10_bio->state);
2353 raid10_find_phys(conf, r10_bio);
2354 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
2355
2356 for (i=0; i<conf->copies; i++) {
2357 int d = r10_bio->devs[i].devnum;
2358 sector_t first_bad, sector;
2359 int bad_sectors;
2360
2361 bio = r10_bio->devs[i].bio;
2362 bio->bi_end_io = NULL;
2363 clear_bit(BIO_UPTODATE, &bio->bi_flags);
2364 if (conf->mirrors[d].rdev == NULL ||
2365 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
2366 continue;
2367 sector = r10_bio->devs[i].addr;
2368 if (is_badblock(conf->mirrors[d].rdev,
2369 sector, max_sync,
2370 &first_bad, &bad_sectors)) {
2371 if (first_bad > sector)
2372 max_sync = first_bad - sector;
2373 else {
2374 bad_sectors -= (sector - first_bad);
2375 if (max_sync > bad_sectors)
2376 max_sync = max_sync;
2377 continue;
2378 }
2379 }
2380 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2381 atomic_inc(&r10_bio->remaining);
2382 bio->bi_next = biolist;
2383 biolist = bio;
2384 bio->bi_private = r10_bio;
2385 bio->bi_end_io = end_sync_read;
2386 bio->bi_rw = READ;
2387 bio->bi_sector = sector +
2388 conf->mirrors[d].rdev->data_offset;
2389 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2390 count++;
2391 }
2392
2393 if (count < 2) {
2394 for (i=0; i<conf->copies; i++) {
2395 int d = r10_bio->devs[i].devnum;
2396 if (r10_bio->devs[i].bio->bi_end_io)
2397 rdev_dec_pending(conf->mirrors[d].rdev,
2398 mddev);
2399 }
2400 put_buf(r10_bio);
2401 biolist = NULL;
2402 goto giveup;
2403 }
2404 }
2405
2406 for (bio = biolist; bio ; bio=bio->bi_next) {
2407
2408 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
2409 if (bio->bi_end_io)
2410 bio->bi_flags |= 1 << BIO_UPTODATE;
2411 bio->bi_vcnt = 0;
2412 bio->bi_idx = 0;
2413 bio->bi_phys_segments = 0;
2414 bio->bi_size = 0;
2415 }
2416
2417 nr_sectors = 0;
2418 if (sector_nr + max_sync < max_sector)
2419 max_sector = sector_nr + max_sync;
2420 do {
2421 struct page *page;
2422 int len = PAGE_SIZE;
2423 if (sector_nr + (len>>9) > max_sector)
2424 len = (max_sector - sector_nr) << 9;
2425 if (len == 0)
2426 break;
2427 for (bio= biolist ; bio ; bio=bio->bi_next) {
2428 struct bio *bio2;
2429 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
2430 if (bio_add_page(bio, page, len, 0))
2431 continue;
2432
2433 /* stop here */
2434 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
2435 for (bio2 = biolist;
2436 bio2 && bio2 != bio;
2437 bio2 = bio2->bi_next) {
2438 /* remove last page from this bio */
2439 bio2->bi_vcnt--;
2440 bio2->bi_size -= len;
2441 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
2442 }
2443 goto bio_full;
2444 }
2445 nr_sectors += len>>9;
2446 sector_nr += len>>9;
2447 } while (biolist->bi_vcnt < RESYNC_PAGES);
2448 bio_full:
2449 r10_bio->sectors = nr_sectors;
2450
2451 while (biolist) {
2452 bio = biolist;
2453 biolist = biolist->bi_next;
2454
2455 bio->bi_next = NULL;
2456 r10_bio = bio->bi_private;
2457 r10_bio->sectors = nr_sectors;
2458
2459 if (bio->bi_end_io == end_sync_read) {
2460 md_sync_acct(bio->bi_bdev, nr_sectors);
2461 generic_make_request(bio);
2462 }
2463 }
2464
2465 if (sectors_skipped)
2466 /* pretend they weren't skipped, it makes
2467 * no important difference in this case
2468 */
2469 md_done_sync(mddev, sectors_skipped, 1);
2470
2471 return sectors_skipped + nr_sectors;
2472 giveup:
2473 /* There is nowhere to write, so all non-sync
2474 * drives must be failed or in resync, all drives
2475 * have a bad block, so try the next chunk...
2476 */
2477 if (sector_nr + max_sync < max_sector)
2478 max_sector = sector_nr + max_sync;
2479
2480 sectors_skipped += (max_sector - sector_nr);
2481 chunks_skipped ++;
2482 sector_nr = max_sector;
2483 goto skipped;
2484 }
2485
2486 static sector_t
2487 raid10_size(mddev_t *mddev, sector_t sectors, int raid_disks)
2488 {
2489 sector_t size;
2490 conf_t *conf = mddev->private;
2491
2492 if (!raid_disks)
2493 raid_disks = conf->raid_disks;
2494 if (!sectors)
2495 sectors = conf->dev_sectors;
2496
2497 size = sectors >> conf->chunk_shift;
2498 sector_div(size, conf->far_copies);
2499 size = size * raid_disks;
2500 sector_div(size, conf->near_copies);
2501
2502 return size << conf->chunk_shift;
2503 }
2504
2505
2506 static conf_t *setup_conf(mddev_t *mddev)
2507 {
2508 conf_t *conf = NULL;
2509 int nc, fc, fo;
2510 sector_t stride, size;
2511 int err = -EINVAL;
2512
2513 if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
2514 !is_power_of_2(mddev->new_chunk_sectors)) {
2515 printk(KERN_ERR "md/raid10:%s: chunk size must be "
2516 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
2517 mdname(mddev), PAGE_SIZE);
2518 goto out;
2519 }
2520
2521 nc = mddev->new_layout & 255;
2522 fc = (mddev->new_layout >> 8) & 255;
2523 fo = mddev->new_layout & (1<<16);
2524
2525 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
2526 (mddev->new_layout >> 17)) {
2527 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
2528 mdname(mddev), mddev->new_layout);
2529 goto out;
2530 }
2531
2532 err = -ENOMEM;
2533 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
2534 if (!conf)
2535 goto out;
2536
2537 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
2538 GFP_KERNEL);
2539 if (!conf->mirrors)
2540 goto out;
2541
2542 conf->tmppage = alloc_page(GFP_KERNEL);
2543 if (!conf->tmppage)
2544 goto out;
2545
2546
2547 conf->raid_disks = mddev->raid_disks;
2548 conf->near_copies = nc;
2549 conf->far_copies = fc;
2550 conf->copies = nc*fc;
2551 conf->far_offset = fo;
2552 conf->chunk_mask = mddev->new_chunk_sectors - 1;
2553 conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
2554
2555 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2556 r10bio_pool_free, conf);
2557 if (!conf->r10bio_pool)
2558 goto out;
2559
2560 size = mddev->dev_sectors >> conf->chunk_shift;
2561 sector_div(size, fc);
2562 size = size * conf->raid_disks;
2563 sector_div(size, nc);
2564 /* 'size' is now the number of chunks in the array */
2565 /* calculate "used chunks per device" in 'stride' */
2566 stride = size * conf->copies;
2567
2568 /* We need to round up when dividing by raid_disks to
2569 * get the stride size.
2570 */
2571 stride += conf->raid_disks - 1;
2572 sector_div(stride, conf->raid_disks);
2573
2574 conf->dev_sectors = stride << conf->chunk_shift;
2575
2576 if (fo)
2577 stride = 1;
2578 else
2579 sector_div(stride, fc);
2580 conf->stride = stride << conf->chunk_shift;
2581
2582
2583 spin_lock_init(&conf->device_lock);
2584 INIT_LIST_HEAD(&conf->retry_list);
2585
2586 spin_lock_init(&conf->resync_lock);
2587 init_waitqueue_head(&conf->wait_barrier);
2588
2589 conf->thread = md_register_thread(raid10d, mddev, NULL);
2590 if (!conf->thread)
2591 goto out;
2592
2593 conf->mddev = mddev;
2594 return conf;
2595
2596 out:
2597 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
2598 mdname(mddev));
2599 if (conf) {
2600 if (conf->r10bio_pool)
2601 mempool_destroy(conf->r10bio_pool);
2602 kfree(conf->mirrors);
2603 safe_put_page(conf->tmppage);
2604 kfree(conf);
2605 }
2606 return ERR_PTR(err);
2607 }
2608
2609 static int run(mddev_t *mddev)
2610 {
2611 conf_t *conf;
2612 int i, disk_idx, chunk_size;
2613 mirror_info_t *disk;
2614 mdk_rdev_t *rdev;
2615 sector_t size;
2616
2617 /*
2618 * copy the already verified devices into our private RAID10
2619 * bookkeeping area. [whatever we allocate in run(),
2620 * should be freed in stop()]
2621 */
2622
2623 if (mddev->private == NULL) {
2624 conf = setup_conf(mddev);
2625 if (IS_ERR(conf))
2626 return PTR_ERR(conf);
2627 mddev->private = conf;
2628 }
2629 conf = mddev->private;
2630 if (!conf)
2631 goto out;
2632
2633 mddev->thread = conf->thread;
2634 conf->thread = NULL;
2635
2636 chunk_size = mddev->chunk_sectors << 9;
2637 blk_queue_io_min(mddev->queue, chunk_size);
2638 if (conf->raid_disks % conf->near_copies)
2639 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
2640 else
2641 blk_queue_io_opt(mddev->queue, chunk_size *
2642 (conf->raid_disks / conf->near_copies));
2643
2644 list_for_each_entry(rdev, &mddev->disks, same_set) {
2645
2646 disk_idx = rdev->raid_disk;
2647 if (disk_idx >= conf->raid_disks
2648 || disk_idx < 0)
2649 continue;
2650 disk = conf->mirrors + disk_idx;
2651
2652 disk->rdev = rdev;
2653 disk_stack_limits(mddev->gendisk, rdev->bdev,
2654 rdev->data_offset << 9);
2655 /* as we don't honour merge_bvec_fn, we must never risk
2656 * violating it, so limit max_segments to 1 lying
2657 * within a single page.
2658 */
2659 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2660 blk_queue_max_segments(mddev->queue, 1);
2661 blk_queue_segment_boundary(mddev->queue,
2662 PAGE_CACHE_SIZE - 1);
2663 }
2664
2665 disk->head_position = 0;
2666 }
2667 /* need to check that every block has at least one working mirror */
2668 if (!enough(conf, -1)) {
2669 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
2670 mdname(mddev));
2671 goto out_free_conf;
2672 }
2673
2674 mddev->degraded = 0;
2675 for (i = 0; i < conf->raid_disks; i++) {
2676
2677 disk = conf->mirrors + i;
2678
2679 if (!disk->rdev ||
2680 !test_bit(In_sync, &disk->rdev->flags)) {
2681 disk->head_position = 0;
2682 mddev->degraded++;
2683 if (disk->rdev)
2684 conf->fullsync = 1;
2685 }
2686 }
2687
2688 if (mddev->recovery_cp != MaxSector)
2689 printk(KERN_NOTICE "md/raid10:%s: not clean"
2690 " -- starting background reconstruction\n",
2691 mdname(mddev));
2692 printk(KERN_INFO
2693 "md/raid10:%s: active with %d out of %d devices\n",
2694 mdname(mddev), conf->raid_disks - mddev->degraded,
2695 conf->raid_disks);
2696 /*
2697 * Ok, everything is just fine now
2698 */
2699 mddev->dev_sectors = conf->dev_sectors;
2700 size = raid10_size(mddev, 0, 0);
2701 md_set_array_sectors(mddev, size);
2702 mddev->resync_max_sectors = size;
2703
2704 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2705 mddev->queue->backing_dev_info.congested_data = mddev;
2706
2707 /* Calculate max read-ahead size.
2708 * We need to readahead at least twice a whole stripe....
2709 * maybe...
2710 */
2711 {
2712 int stripe = conf->raid_disks *
2713 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
2714 stripe /= conf->near_copies;
2715 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2716 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2717 }
2718
2719 if (conf->near_copies < conf->raid_disks)
2720 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
2721
2722 if (md_integrity_register(mddev))
2723 goto out_free_conf;
2724
2725 return 0;
2726
2727 out_free_conf:
2728 md_unregister_thread(mddev->thread);
2729 if (conf->r10bio_pool)
2730 mempool_destroy(conf->r10bio_pool);
2731 safe_put_page(conf->tmppage);
2732 kfree(conf->mirrors);
2733 kfree(conf);
2734 mddev->private = NULL;
2735 out:
2736 return -EIO;
2737 }
2738
2739 static int stop(mddev_t *mddev)
2740 {
2741 conf_t *conf = mddev->private;
2742
2743 raise_barrier(conf, 0);
2744 lower_barrier(conf);
2745
2746 md_unregister_thread(mddev->thread);
2747 mddev->thread = NULL;
2748 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2749 if (conf->r10bio_pool)
2750 mempool_destroy(conf->r10bio_pool);
2751 kfree(conf->mirrors);
2752 kfree(conf);
2753 mddev->private = NULL;
2754 return 0;
2755 }
2756
2757 static void raid10_quiesce(mddev_t *mddev, int state)
2758 {
2759 conf_t *conf = mddev->private;
2760
2761 switch(state) {
2762 case 1:
2763 raise_barrier(conf, 0);
2764 break;
2765 case 0:
2766 lower_barrier(conf);
2767 break;
2768 }
2769 }
2770
2771 static void *raid10_takeover_raid0(mddev_t *mddev)
2772 {
2773 mdk_rdev_t *rdev;
2774 conf_t *conf;
2775
2776 if (mddev->degraded > 0) {
2777 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
2778 mdname(mddev));
2779 return ERR_PTR(-EINVAL);
2780 }
2781
2782 /* Set new parameters */
2783 mddev->new_level = 10;
2784 /* new layout: far_copies = 1, near_copies = 2 */
2785 mddev->new_layout = (1<<8) + 2;
2786 mddev->new_chunk_sectors = mddev->chunk_sectors;
2787 mddev->delta_disks = mddev->raid_disks;
2788 mddev->raid_disks *= 2;
2789 /* make sure it will be not marked as dirty */
2790 mddev->recovery_cp = MaxSector;
2791
2792 conf = setup_conf(mddev);
2793 if (!IS_ERR(conf)) {
2794 list_for_each_entry(rdev, &mddev->disks, same_set)
2795 if (rdev->raid_disk >= 0)
2796 rdev->new_raid_disk = rdev->raid_disk * 2;
2797 conf->barrier = 1;
2798 }
2799
2800 return conf;
2801 }
2802
2803 static void *raid10_takeover(mddev_t *mddev)
2804 {
2805 struct raid0_private_data *raid0_priv;
2806
2807 /* raid10 can take over:
2808 * raid0 - providing it has only two drives
2809 */
2810 if (mddev->level == 0) {
2811 /* for raid0 takeover only one zone is supported */
2812 raid0_priv = mddev->private;
2813 if (raid0_priv->nr_strip_zones > 1) {
2814 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
2815 " with more than one zone.\n",
2816 mdname(mddev));
2817 return ERR_PTR(-EINVAL);
2818 }
2819 return raid10_takeover_raid0(mddev);
2820 }
2821 return ERR_PTR(-EINVAL);
2822 }
2823
2824 static struct mdk_personality raid10_personality =
2825 {
2826 .name = "raid10",
2827 .level = 10,
2828 .owner = THIS_MODULE,
2829 .make_request = make_request,
2830 .run = run,
2831 .stop = stop,
2832 .status = status,
2833 .error_handler = error,
2834 .hot_add_disk = raid10_add_disk,
2835 .hot_remove_disk= raid10_remove_disk,
2836 .spare_active = raid10_spare_active,
2837 .sync_request = sync_request,
2838 .quiesce = raid10_quiesce,
2839 .size = raid10_size,
2840 .takeover = raid10_takeover,
2841 };
2842
2843 static int __init raid_init(void)
2844 {
2845 return register_md_personality(&raid10_personality);
2846 }
2847
2848 static void raid_exit(void)
2849 {
2850 unregister_md_personality(&raid10_personality);
2851 }
2852
2853 module_init(raid_init);
2854 module_exit(raid_exit);
2855 MODULE_LICENSE("GPL");
2856 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
2857 MODULE_ALIAS("md-personality-9"); /* RAID10 */
2858 MODULE_ALIAS("md-raid10");
2859 MODULE_ALIAS("md-level-10");
This page took 0.124452 seconds and 6 git commands to generate.