Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / md / raid10.c
blobd64d3bb1c66972760571f169d0d67bc2e7965e6c
1 /*
2 * raid10.c : Multiple Devices driver for Linux
4 * Copyright (C) 2000-2004 Neil Brown
6 * RAID-10 support for md.
8 * Base on code in raid1.c. See raid1.c for futher copyright information.
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.
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.
21 #include "dm-bio-list.h"
22 #include <linux/raid/raid10.h>
23 #include <linux/raid/bitmap.h>
26 * RAID10 provides a combination of RAID0 and RAID1 functionality.
27 * The layout of data is defined by
28 * chunk_size
29 * raid_disks
30 * near_copies (stored in low byte of layout)
31 * far_copies (stored in second byte of layout)
32 * far_offset (stored in bit 16 of layout )
34 * The data to be stored is divided into chunks using chunksize.
35 * Each device is divided into far_copies sections.
36 * In each section, chunks are laid out in a style similar to raid0, but
37 * near_copies copies of each chunk is stored (each on a different drive).
38 * The starting device for each section is offset near_copies from the starting
39 * device of the previous section.
40 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
41 * drive.
42 * near_copies and far_copies must be at least one, and their product is at most
43 * raid_disks.
45 * If far_offset is true, then the far_copies are handled a bit differently.
46 * The copies are still in different stripes, but instead of be very far apart
47 * on disk, there are adjacent stripes.
51 * Number of guaranteed r10bios in case of extreme VM load:
53 #define NR_RAID10_BIOS 256
55 static void unplug_slaves(mddev_t *mddev);
57 static void allow_barrier(conf_t *conf);
58 static void lower_barrier(conf_t *conf);
60 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
62 conf_t *conf = data;
63 r10bio_t *r10_bio;
64 int size = offsetof(struct r10bio_s, devs[conf->copies]);
66 /* allocate a r10bio with room for raid_disks entries in the bios array */
67 r10_bio = kzalloc(size, gfp_flags);
68 if (!r10_bio)
69 unplug_slaves(conf->mddev);
71 return r10_bio;
74 static void r10bio_pool_free(void *r10_bio, void *data)
76 kfree(r10_bio);
79 #define RESYNC_BLOCK_SIZE (64*1024)
80 //#define RESYNC_BLOCK_SIZE PAGE_SIZE
81 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
82 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
83 #define RESYNC_WINDOW (2048*1024)
86 * When performing a resync, we need to read and compare, so
87 * we need as many pages are there are copies.
88 * When performing a recovery, we need 2 bios, one for read,
89 * one for write (we recover only one drive per r10buf)
92 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
94 conf_t *conf = data;
95 struct page *page;
96 r10bio_t *r10_bio;
97 struct bio *bio;
98 int i, j;
99 int nalloc;
101 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
102 if (!r10_bio) {
103 unplug_slaves(conf->mddev);
104 return NULL;
107 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
108 nalloc = conf->copies; /* resync */
109 else
110 nalloc = 2; /* recovery */
113 * Allocate bios.
115 for (j = nalloc ; j-- ; ) {
116 bio = bio_alloc(gfp_flags, RESYNC_PAGES);
117 if (!bio)
118 goto out_free_bio;
119 r10_bio->devs[j].bio = bio;
122 * Allocate RESYNC_PAGES data pages and attach them
123 * where needed.
125 for (j = 0 ; j < nalloc; j++) {
126 bio = r10_bio->devs[j].bio;
127 for (i = 0; i < RESYNC_PAGES; i++) {
128 page = alloc_page(gfp_flags);
129 if (unlikely(!page))
130 goto out_free_pages;
132 bio->bi_io_vec[i].bv_page = page;
136 return r10_bio;
138 out_free_pages:
139 for ( ; i > 0 ; i--)
140 safe_put_page(bio->bi_io_vec[i-1].bv_page);
141 while (j--)
142 for (i = 0; i < RESYNC_PAGES ; i++)
143 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
144 j = -1;
145 out_free_bio:
146 while ( ++j < nalloc )
147 bio_put(r10_bio->devs[j].bio);
148 r10bio_pool_free(r10_bio, conf);
149 return NULL;
152 static void r10buf_pool_free(void *__r10_bio, void *data)
154 int i;
155 conf_t *conf = data;
156 r10bio_t *r10bio = __r10_bio;
157 int j;
159 for (j=0; j < conf->copies; j++) {
160 struct bio *bio = r10bio->devs[j].bio;
161 if (bio) {
162 for (i = 0; i < RESYNC_PAGES; i++) {
163 safe_put_page(bio->bi_io_vec[i].bv_page);
164 bio->bi_io_vec[i].bv_page = NULL;
166 bio_put(bio);
169 r10bio_pool_free(r10bio, conf);
172 static void put_all_bios(conf_t *conf, r10bio_t *r10_bio)
174 int i;
176 for (i = 0; i < conf->copies; i++) {
177 struct bio **bio = & r10_bio->devs[i].bio;
178 if (*bio && *bio != IO_BLOCKED)
179 bio_put(*bio);
180 *bio = NULL;
184 static void free_r10bio(r10bio_t *r10_bio)
186 conf_t *conf = mddev_to_conf(r10_bio->mddev);
189 * Wake up any possible resync thread that waits for the device
190 * to go idle.
192 allow_barrier(conf);
194 put_all_bios(conf, r10_bio);
195 mempool_free(r10_bio, conf->r10bio_pool);
198 static void put_buf(r10bio_t *r10_bio)
200 conf_t *conf = mddev_to_conf(r10_bio->mddev);
202 mempool_free(r10_bio, conf->r10buf_pool);
204 lower_barrier(conf);
207 static void reschedule_retry(r10bio_t *r10_bio)
209 unsigned long flags;
210 mddev_t *mddev = r10_bio->mddev;
211 conf_t *conf = mddev_to_conf(mddev);
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);
218 md_wakeup_thread(mddev->thread);
222 * raid_end_bio_io() is called when we have finished servicing a mirrored
223 * operation and are ready to return a success/failure code to the buffer
224 * cache layer.
226 static void raid_end_bio_io(r10bio_t *r10_bio)
228 struct bio *bio = r10_bio->master_bio;
230 bio_endio(bio,
231 test_bit(R10BIO_Uptodate, &r10_bio->state) ? 0 : -EIO);
232 free_r10bio(r10_bio);
236 * Update disk head position estimator based on IRQ completion info.
238 static inline void update_head_pos(int slot, r10bio_t *r10_bio)
240 conf_t *conf = mddev_to_conf(r10_bio->mddev);
242 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
243 r10_bio->devs[slot].addr + (r10_bio->sectors);
246 static void raid10_end_read_request(struct bio *bio, int error)
248 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
249 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
250 int slot, dev;
251 conf_t *conf = mddev_to_conf(r10_bio->mddev);
254 slot = r10_bio->read_slot;
255 dev = r10_bio->devs[slot].devnum;
257 * this branch is our 'one mirror IO has finished' event handler:
259 update_head_pos(slot, r10_bio);
261 if (uptodate) {
263 * Set R10BIO_Uptodate in our master bio, so that
264 * we will return a good error code to the higher
265 * levels even if IO on some other mirrored buffer fails.
267 * The 'master' represents the composite IO operation to
268 * user-side. So if something waits for IO, then it will
269 * wait for the 'master' bio.
271 set_bit(R10BIO_Uptodate, &r10_bio->state);
272 raid_end_bio_io(r10_bio);
273 } else {
275 * oops, read error:
277 char b[BDEVNAME_SIZE];
278 if (printk_ratelimit())
279 printk(KERN_ERR "raid10: %s: rescheduling sector %llu\n",
280 bdevname(conf->mirrors[dev].rdev->bdev,b), (unsigned long long)r10_bio->sector);
281 reschedule_retry(r10_bio);
284 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
287 static void raid10_end_write_request(struct bio *bio, int error)
289 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
290 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
291 int slot, dev;
292 conf_t *conf = mddev_to_conf(r10_bio->mddev);
294 for (slot = 0; slot < conf->copies; slot++)
295 if (r10_bio->devs[slot].bio == bio)
296 break;
297 dev = r10_bio->devs[slot].devnum;
300 * this branch is our 'one mirror IO has finished' event handler:
302 if (!uptodate) {
303 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
304 /* an I/O failed, we can't clear the bitmap */
305 set_bit(R10BIO_Degraded, &r10_bio->state);
306 } else
308 * Set R10BIO_Uptodate in our master bio, so that
309 * we will return a good error code for to the higher
310 * levels even if IO on some other mirrored buffer fails.
312 * The 'master' represents the composite IO operation to
313 * user-side. So if something waits for IO, then it will
314 * wait for the 'master' bio.
316 set_bit(R10BIO_Uptodate, &r10_bio->state);
318 update_head_pos(slot, r10_bio);
322 * Let's see if all mirrored write operations have finished
323 * already.
325 if (atomic_dec_and_test(&r10_bio->remaining)) {
326 /* clear the bitmap if all writes complete successfully */
327 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
328 r10_bio->sectors,
329 !test_bit(R10BIO_Degraded, &r10_bio->state),
331 md_write_end(r10_bio->mddev);
332 raid_end_bio_io(r10_bio);
335 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
340 * RAID10 layout manager
341 * Aswell as the chunksize and raid_disks count, there are two
342 * parameters: near_copies and far_copies.
343 * near_copies * far_copies must be <= raid_disks.
344 * Normally one of these will be 1.
345 * If both are 1, we get raid0.
346 * If near_copies == raid_disks, we get raid1.
348 * Chunks are layed out in raid0 style with near_copies copies of the
349 * first chunk, followed by near_copies copies of the next chunk and
350 * so on.
351 * If far_copies > 1, then after 1/far_copies of the array has been assigned
352 * as described above, we start again with a device offset of near_copies.
353 * So we effectively have another copy of the whole array further down all
354 * the drives, but with blocks on different drives.
355 * With this layout, and block is never stored twice on the one device.
357 * raid10_find_phys finds the sector offset of a given virtual sector
358 * on each device that it is on.
360 * raid10_find_virt does the reverse mapping, from a device and a
361 * sector offset to a virtual address
364 static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
366 int n,f;
367 sector_t sector;
368 sector_t chunk;
369 sector_t stripe;
370 int dev;
372 int slot = 0;
374 /* now calculate first sector/dev */
375 chunk = r10bio->sector >> conf->chunk_shift;
376 sector = r10bio->sector & conf->chunk_mask;
378 chunk *= conf->near_copies;
379 stripe = chunk;
380 dev = sector_div(stripe, conf->raid_disks);
381 if (conf->far_offset)
382 stripe *= conf->far_copies;
384 sector += stripe << conf->chunk_shift;
386 /* and calculate all the others */
387 for (n=0; n < conf->near_copies; n++) {
388 int d = dev;
389 sector_t s = sector;
390 r10bio->devs[slot].addr = sector;
391 r10bio->devs[slot].devnum = d;
392 slot++;
394 for (f = 1; f < conf->far_copies; f++) {
395 d += conf->near_copies;
396 if (d >= conf->raid_disks)
397 d -= conf->raid_disks;
398 s += conf->stride;
399 r10bio->devs[slot].devnum = d;
400 r10bio->devs[slot].addr = s;
401 slot++;
403 dev++;
404 if (dev >= conf->raid_disks) {
405 dev = 0;
406 sector += (conf->chunk_mask + 1);
409 BUG_ON(slot != conf->copies);
412 static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
414 sector_t offset, chunk, vchunk;
416 offset = sector & conf->chunk_mask;
417 if (conf->far_offset) {
418 int fc;
419 chunk = sector >> conf->chunk_shift;
420 fc = sector_div(chunk, conf->far_copies);
421 dev -= fc * conf->near_copies;
422 if (dev < 0)
423 dev += conf->raid_disks;
424 } else {
425 while (sector >= conf->stride) {
426 sector -= conf->stride;
427 if (dev < conf->near_copies)
428 dev += conf->raid_disks - conf->near_copies;
429 else
430 dev -= conf->near_copies;
432 chunk = sector >> conf->chunk_shift;
434 vchunk = chunk * conf->raid_disks + dev;
435 sector_div(vchunk, conf->near_copies);
436 return (vchunk << conf->chunk_shift) + offset;
440 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
441 * @q: request queue
442 * @bio: the buffer head that's been built up so far
443 * @biovec: the request that could be merged to it.
445 * Return amount of bytes we can accept at this offset
446 * If near_copies == raid_disk, there are no striping issues,
447 * but in that case, the function isn't called at all.
449 static int raid10_mergeable_bvec(struct request_queue *q, struct bio *bio,
450 struct bio_vec *bio_vec)
452 mddev_t *mddev = q->queuedata;
453 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
454 int max;
455 unsigned int chunk_sectors = mddev->chunk_size >> 9;
456 unsigned int bio_sectors = bio->bi_size >> 9;
458 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
459 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
460 if (max <= bio_vec->bv_len && bio_sectors == 0)
461 return bio_vec->bv_len;
462 else
463 return max;
467 * This routine returns the disk from which the requested read should
468 * be done. There is a per-array 'next expected sequential IO' sector
469 * number - if this matches on the next IO then we use the last disk.
470 * There is also a per-disk 'last know head position' sector that is
471 * maintained from IRQ contexts, both the normal and the resync IO
472 * completion handlers update this position correctly. If there is no
473 * perfect sequential match then we pick the disk whose head is closest.
475 * If there are 2 mirrors in the same 2 devices, performance degrades
476 * because position is mirror, not device based.
478 * The rdev for the device selected will have nr_pending incremented.
482 * FIXME: possibly should rethink readbalancing and do it differently
483 * depending on near_copies / far_copies geometry.
485 static int read_balance(conf_t *conf, r10bio_t *r10_bio)
487 const unsigned long this_sector = r10_bio->sector;
488 int disk, slot, nslot;
489 const int sectors = r10_bio->sectors;
490 sector_t new_distance, current_distance;
491 mdk_rdev_t *rdev;
493 raid10_find_phys(conf, r10_bio);
494 rcu_read_lock();
496 * Check if we can balance. We can balance on the whole
497 * device if no resync is going on (recovery is ok), or below
498 * the resync window. We take the first readable disk when
499 * above the resync window.
501 if (conf->mddev->recovery_cp < MaxSector
502 && (this_sector + sectors >= conf->next_resync)) {
503 /* make sure that disk is operational */
504 slot = 0;
505 disk = r10_bio->devs[slot].devnum;
507 while ((rdev = rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
508 r10_bio->devs[slot].bio == IO_BLOCKED ||
509 !test_bit(In_sync, &rdev->flags)) {
510 slot++;
511 if (slot == conf->copies) {
512 slot = 0;
513 disk = -1;
514 break;
516 disk = r10_bio->devs[slot].devnum;
518 goto rb_out;
522 /* make sure the disk is operational */
523 slot = 0;
524 disk = r10_bio->devs[slot].devnum;
525 while ((rdev=rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
526 r10_bio->devs[slot].bio == IO_BLOCKED ||
527 !test_bit(In_sync, &rdev->flags)) {
528 slot ++;
529 if (slot == conf->copies) {
530 disk = -1;
531 goto rb_out;
533 disk = r10_bio->devs[slot].devnum;
537 current_distance = abs(r10_bio->devs[slot].addr -
538 conf->mirrors[disk].head_position);
540 <<<<<<< HEAD:drivers/md/raid10.c
541 /* Find the disk whose head is closest */
542 =======
543 /* Find the disk whose head is closest,
544 * or - for far > 1 - find the closest to partition beginning */
545 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/md/raid10.c
547 for (nslot = slot; nslot < conf->copies; nslot++) {
548 int ndisk = r10_bio->devs[nslot].devnum;
551 if ((rdev=rcu_dereference(conf->mirrors[ndisk].rdev)) == NULL ||
552 r10_bio->devs[nslot].bio == IO_BLOCKED ||
553 !test_bit(In_sync, &rdev->flags))
554 continue;
556 /* This optimisation is debatable, and completely destroys
557 * sequential read speed for 'far copies' arrays. So only
558 * keep it for 'near' arrays, and review those later.
560 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) {
561 disk = ndisk;
562 slot = nslot;
563 break;
565 <<<<<<< HEAD:drivers/md/raid10.c
566 new_distance = abs(r10_bio->devs[nslot].addr -
567 conf->mirrors[ndisk].head_position);
568 =======
570 /* for far > 1 always use the lowest address */
571 if (conf->far_copies > 1)
572 new_distance = r10_bio->devs[nslot].addr;
573 else
574 new_distance = abs(r10_bio->devs[nslot].addr -
575 conf->mirrors[ndisk].head_position);
576 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/md/raid10.c
577 if (new_distance < current_distance) {
578 current_distance = new_distance;
579 disk = ndisk;
580 slot = nslot;
584 rb_out:
585 r10_bio->read_slot = slot;
586 /* conf->next_seq_sect = this_sector + sectors;*/
588 if (disk >= 0 && (rdev=rcu_dereference(conf->mirrors[disk].rdev))!= NULL)
589 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
590 else
591 disk = -1;
592 rcu_read_unlock();
594 return disk;
597 static void unplug_slaves(mddev_t *mddev)
599 conf_t *conf = mddev_to_conf(mddev);
600 int i;
602 rcu_read_lock();
603 for (i=0; i<mddev->raid_disks; i++) {
604 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
605 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
606 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
608 atomic_inc(&rdev->nr_pending);
609 rcu_read_unlock();
611 blk_unplug(r_queue);
613 rdev_dec_pending(rdev, mddev);
614 rcu_read_lock();
617 rcu_read_unlock();
620 static void raid10_unplug(struct request_queue *q)
622 mddev_t *mddev = q->queuedata;
624 unplug_slaves(q->queuedata);
625 md_wakeup_thread(mddev->thread);
628 static int raid10_congested(void *data, int bits)
630 mddev_t *mddev = data;
631 conf_t *conf = mddev_to_conf(mddev);
632 int i, ret = 0;
634 rcu_read_lock();
635 for (i = 0; i < mddev->raid_disks && ret == 0; i++) {
636 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
637 if (rdev && !test_bit(Faulty, &rdev->flags)) {
638 struct request_queue *q = bdev_get_queue(rdev->bdev);
640 ret |= bdi_congested(&q->backing_dev_info, bits);
643 rcu_read_unlock();
644 return ret;
647 <<<<<<< HEAD:drivers/md/raid10.c
649 =======
650 static int flush_pending_writes(conf_t *conf)
652 /* Any writes that have been queued but are awaiting
653 * bitmap updates get flushed here.
654 * We return 1 if any requests were actually submitted.
656 int rv = 0;
658 spin_lock_irq(&conf->device_lock);
660 if (conf->pending_bio_list.head) {
661 struct bio *bio;
662 bio = bio_list_get(&conf->pending_bio_list);
663 blk_remove_plug(conf->mddev->queue);
664 spin_unlock_irq(&conf->device_lock);
665 /* flush any pending bitmap writes to disk
666 * before proceeding w/ I/O */
667 bitmap_unplug(conf->mddev->bitmap);
669 while (bio) { /* submit pending writes */
670 struct bio *next = bio->bi_next;
671 bio->bi_next = NULL;
672 generic_make_request(bio);
673 bio = next;
675 rv = 1;
676 } else
677 spin_unlock_irq(&conf->device_lock);
678 return rv;
680 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/md/raid10.c
681 /* Barriers....
682 * Sometimes we need to suspend IO while we do something else,
683 * either some resync/recovery, or reconfigure the array.
684 * To do this we raise a 'barrier'.
685 * The 'barrier' is a counter that can be raised multiple times
686 * to count how many activities are happening which preclude
687 * normal IO.
688 * We can only raise the barrier if there is no pending IO.
689 * i.e. if nr_pending == 0.
690 * We choose only to raise the barrier if no-one is waiting for the
691 * barrier to go down. This means that as soon as an IO request
692 * is ready, no other operations which require a barrier will start
693 * until the IO request has had a chance.
695 * So: regular IO calls 'wait_barrier'. When that returns there
696 * is no backgroup IO happening, It must arrange to call
697 * allow_barrier when it has finished its IO.
698 * backgroup IO calls must call raise_barrier. Once that returns
699 * there is no normal IO happeing. It must arrange to call
700 * lower_barrier when the particular background IO completes.
702 #define RESYNC_DEPTH 32
704 static void raise_barrier(conf_t *conf, int force)
706 BUG_ON(force && !conf->barrier);
707 spin_lock_irq(&conf->resync_lock);
709 /* Wait until no block IO is waiting (unless 'force') */
710 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
711 conf->resync_lock,
712 raid10_unplug(conf->mddev->queue));
714 /* block any new IO from starting */
715 conf->barrier++;
717 /* No wait for all pending IO to complete */
718 wait_event_lock_irq(conf->wait_barrier,
719 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
720 conf->resync_lock,
721 raid10_unplug(conf->mddev->queue));
723 spin_unlock_irq(&conf->resync_lock);
726 static void lower_barrier(conf_t *conf)
728 unsigned long flags;
729 spin_lock_irqsave(&conf->resync_lock, flags);
730 conf->barrier--;
731 spin_unlock_irqrestore(&conf->resync_lock, flags);
732 wake_up(&conf->wait_barrier);
735 static void wait_barrier(conf_t *conf)
737 spin_lock_irq(&conf->resync_lock);
738 if (conf->barrier) {
739 conf->nr_waiting++;
740 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
741 conf->resync_lock,
742 raid10_unplug(conf->mddev->queue));
743 conf->nr_waiting--;
745 conf->nr_pending++;
746 spin_unlock_irq(&conf->resync_lock);
749 static void allow_barrier(conf_t *conf)
751 unsigned long flags;
752 spin_lock_irqsave(&conf->resync_lock, flags);
753 conf->nr_pending--;
754 spin_unlock_irqrestore(&conf->resync_lock, flags);
755 wake_up(&conf->wait_barrier);
758 static void freeze_array(conf_t *conf)
760 /* stop syncio and normal IO and wait for everything to
761 * go quiet.
762 * We increment barrier and nr_waiting, and then
763 <<<<<<< HEAD:drivers/md/raid10.c
764 * wait until barrier+nr_pending match nr_queued+2
765 =======
766 * wait until nr_pending match nr_queued+1
767 * This is called in the context of one normal IO request
768 * that has failed. Thus any sync request that might be pending
769 * will be blocked by nr_pending, and we need to wait for
770 * pending IO requests to complete or be queued for re-try.
771 * Thus the number queued (nr_queued) plus this request (1)
772 * must match the number of pending IOs (nr_pending) before
773 * we continue.
774 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/md/raid10.c
776 spin_lock_irq(&conf->resync_lock);
777 conf->barrier++;
778 conf->nr_waiting++;
779 wait_event_lock_irq(conf->wait_barrier,
780 <<<<<<< HEAD:drivers/md/raid10.c
781 conf->barrier+conf->nr_pending == conf->nr_queued+2,
782 =======
783 conf->nr_pending == conf->nr_queued+1,
784 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/md/raid10.c
785 conf->resync_lock,
786 <<<<<<< HEAD:drivers/md/raid10.c
787 raid10_unplug(conf->mddev->queue));
788 =======
789 ({ flush_pending_writes(conf);
790 raid10_unplug(conf->mddev->queue); }));
791 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/md/raid10.c
792 spin_unlock_irq(&conf->resync_lock);
795 static void unfreeze_array(conf_t *conf)
797 /* reverse the effect of the freeze */
798 spin_lock_irq(&conf->resync_lock);
799 conf->barrier--;
800 conf->nr_waiting--;
801 wake_up(&conf->wait_barrier);
802 spin_unlock_irq(&conf->resync_lock);
805 static int make_request(struct request_queue *q, struct bio * bio)
807 mddev_t *mddev = q->queuedata;
808 conf_t *conf = mddev_to_conf(mddev);
809 mirror_info_t *mirror;
810 r10bio_t *r10_bio;
811 struct bio *read_bio;
812 int i;
813 int chunk_sects = conf->chunk_mask + 1;
814 const int rw = bio_data_dir(bio);
815 const int do_sync = bio_sync(bio);
816 struct bio_list bl;
817 unsigned long flags;
819 if (unlikely(bio_barrier(bio))) {
820 bio_endio(bio, -EOPNOTSUPP);
821 return 0;
824 /* If this request crosses a chunk boundary, we need to
825 * split it. This will only happen for 1 PAGE (or less) requests.
827 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
828 > chunk_sects &&
829 conf->near_copies < conf->raid_disks)) {
830 struct bio_pair *bp;
831 /* Sanity check -- queue functions should prevent this happening */
832 if (bio->bi_vcnt != 1 ||
833 bio->bi_idx != 0)
834 goto bad_map;
835 /* This is a one page bio that upper layers
836 * refuse to split for us, so we need to split it.
838 bp = bio_split(bio, bio_split_pool,
839 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
840 if (make_request(q, &bp->bio1))
841 generic_make_request(&bp->bio1);
842 if (make_request(q, &bp->bio2))
843 generic_make_request(&bp->bio2);
845 bio_pair_release(bp);
846 return 0;
847 bad_map:
848 printk("raid10_make_request bug: can't convert block across chunks"
849 " or bigger than %dk %llu %d\n", chunk_sects/2,
850 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
852 bio_io_error(bio);
853 return 0;
856 md_write_start(mddev, bio);
859 * Register the new request and wait if the reconstruction
860 * thread has put up a bar for new requests.
861 * Continue immediately if no resync is active currently.
863 wait_barrier(conf);
865 disk_stat_inc(mddev->gendisk, ios[rw]);
866 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
868 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
870 r10_bio->master_bio = bio;
871 r10_bio->sectors = bio->bi_size >> 9;
873 r10_bio->mddev = mddev;
874 r10_bio->sector = bio->bi_sector;
875 r10_bio->state = 0;
877 if (rw == READ) {
879 * read balancing logic:
881 int disk = read_balance(conf, r10_bio);
882 int slot = r10_bio->read_slot;
883 if (disk < 0) {
884 raid_end_bio_io(r10_bio);
885 return 0;
887 mirror = conf->mirrors + disk;
889 read_bio = bio_clone(bio, GFP_NOIO);
891 r10_bio->devs[slot].bio = read_bio;
893 read_bio->bi_sector = r10_bio->devs[slot].addr +
894 mirror->rdev->data_offset;
895 read_bio->bi_bdev = mirror->rdev->bdev;
896 read_bio->bi_end_io = raid10_end_read_request;
897 read_bio->bi_rw = READ | do_sync;
898 read_bio->bi_private = r10_bio;
900 generic_make_request(read_bio);
901 return 0;
905 * WRITE:
907 /* first select target devices under spinlock and
908 * inc refcount on their rdev. Record them by setting
909 * bios[x] to bio
911 raid10_find_phys(conf, r10_bio);
912 rcu_read_lock();
913 for (i = 0; i < conf->copies; i++) {
914 int d = r10_bio->devs[i].devnum;
915 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
916 if (rdev &&
917 !test_bit(Faulty, &rdev->flags)) {
918 atomic_inc(&rdev->nr_pending);
919 r10_bio->devs[i].bio = bio;
920 } else {
921 r10_bio->devs[i].bio = NULL;
922 set_bit(R10BIO_Degraded, &r10_bio->state);
925 rcu_read_unlock();
927 atomic_set(&r10_bio->remaining, 0);
929 bio_list_init(&bl);
930 for (i = 0; i < conf->copies; i++) {
931 struct bio *mbio;
932 int d = r10_bio->devs[i].devnum;
933 if (!r10_bio->devs[i].bio)
934 continue;
936 mbio = bio_clone(bio, GFP_NOIO);
937 r10_bio->devs[i].bio = mbio;
939 mbio->bi_sector = r10_bio->devs[i].addr+
940 conf->mirrors[d].rdev->data_offset;
941 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
942 mbio->bi_end_io = raid10_end_write_request;
943 mbio->bi_rw = WRITE | do_sync;
944 mbio->bi_private = r10_bio;
946 atomic_inc(&r10_bio->remaining);
947 bio_list_add(&bl, mbio);
950 if (unlikely(!atomic_read(&r10_bio->remaining))) {
951 /* the array is dead */
952 md_write_end(mddev);
953 raid_end_bio_io(r10_bio);
954 return 0;
957 bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
958 spin_lock_irqsave(&conf->device_lock, flags);
959 bio_list_merge(&conf->pending_bio_list, &bl);
960 blk_plug_device(mddev->queue);
961 spin_unlock_irqrestore(&conf->device_lock, flags);
963 <<<<<<< HEAD:drivers/md/raid10.c
964 =======
965 /* In case raid10d snuck in to freeze_array */
966 wake_up(&conf->wait_barrier);
968 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/md/raid10.c
969 if (do_sync)
970 md_wakeup_thread(mddev->thread);
972 return 0;
975 static void status(struct seq_file *seq, mddev_t *mddev)
977 conf_t *conf = mddev_to_conf(mddev);
978 int i;
980 if (conf->near_copies < conf->raid_disks)
981 seq_printf(seq, " %dK chunks", mddev->chunk_size/1024);
982 if (conf->near_copies > 1)
983 seq_printf(seq, " %d near-copies", conf->near_copies);
984 if (conf->far_copies > 1) {
985 if (conf->far_offset)
986 seq_printf(seq, " %d offset-copies", conf->far_copies);
987 else
988 seq_printf(seq, " %d far-copies", conf->far_copies);
990 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
991 conf->raid_disks - mddev->degraded);
992 for (i = 0; i < conf->raid_disks; i++)
993 seq_printf(seq, "%s",
994 conf->mirrors[i].rdev &&
995 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
996 seq_printf(seq, "]");
999 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1001 char b[BDEVNAME_SIZE];
1002 conf_t *conf = mddev_to_conf(mddev);
1005 * If it is not operational, then we have already marked it as dead
1006 * else if it is the last working disks, ignore the error, let the
1007 * next level up know.
1008 * else mark the drive as failed
1010 if (test_bit(In_sync, &rdev->flags)
1011 && conf->raid_disks-mddev->degraded == 1)
1013 * Don't fail the drive, just return an IO error.
1014 * The test should really be more sophisticated than
1015 * "working_disks == 1", but it isn't critical, and
1016 * can wait until we do more sophisticated "is the drive
1017 * really dead" tests...
1019 return;
1020 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1021 unsigned long flags;
1022 spin_lock_irqsave(&conf->device_lock, flags);
1023 mddev->degraded++;
1024 spin_unlock_irqrestore(&conf->device_lock, flags);
1026 * if recovery is running, make sure it aborts.
1028 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
1030 set_bit(Faulty, &rdev->flags);
1031 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1032 printk(KERN_ALERT "raid10: Disk failure on %s, disabling device. \n"
1033 " Operation continuing on %d devices\n",
1034 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
1037 static void print_conf(conf_t *conf)
1039 int i;
1040 mirror_info_t *tmp;
1042 printk("RAID10 conf printout:\n");
1043 if (!conf) {
1044 printk("(!conf)\n");
1045 return;
1047 printk(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1048 conf->raid_disks);
1050 for (i = 0; i < conf->raid_disks; i++) {
1051 char b[BDEVNAME_SIZE];
1052 tmp = conf->mirrors + i;
1053 if (tmp->rdev)
1054 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
1055 i, !test_bit(In_sync, &tmp->rdev->flags),
1056 !test_bit(Faulty, &tmp->rdev->flags),
1057 bdevname(tmp->rdev->bdev,b));
1061 static void close_sync(conf_t *conf)
1063 wait_barrier(conf);
1064 allow_barrier(conf);
1066 mempool_destroy(conf->r10buf_pool);
1067 conf->r10buf_pool = NULL;
1070 /* check if there are enough drives for
1071 * every block to appear on atleast one
1073 static int enough(conf_t *conf)
1075 int first = 0;
1077 do {
1078 int n = conf->copies;
1079 int cnt = 0;
1080 while (n--) {
1081 if (conf->mirrors[first].rdev)
1082 cnt++;
1083 first = (first+1) % conf->raid_disks;
1085 if (cnt == 0)
1086 return 0;
1087 } while (first != 0);
1088 return 1;
1091 static int raid10_spare_active(mddev_t *mddev)
1093 int i;
1094 conf_t *conf = mddev->private;
1095 mirror_info_t *tmp;
1098 * Find all non-in_sync disks within the RAID10 configuration
1099 * and mark them in_sync
1101 for (i = 0; i < conf->raid_disks; i++) {
1102 tmp = conf->mirrors + i;
1103 if (tmp->rdev
1104 && !test_bit(Faulty, &tmp->rdev->flags)
1105 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1106 unsigned long flags;
1107 spin_lock_irqsave(&conf->device_lock, flags);
1108 mddev->degraded--;
1109 spin_unlock_irqrestore(&conf->device_lock, flags);
1113 print_conf(conf);
1114 return 0;
1118 static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1120 conf_t *conf = mddev->private;
1121 int found = 0;
1122 int mirror;
1123 mirror_info_t *p;
1125 if (mddev->recovery_cp < MaxSector)
1126 /* only hot-add to in-sync arrays, as recovery is
1127 * very different from resync
1129 return 0;
1130 if (!enough(conf))
1131 return 0;
1133 if (rdev->saved_raid_disk >= 0 &&
1134 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1135 mirror = rdev->saved_raid_disk;
1136 else
1137 mirror = 0;
1138 for ( ; mirror < mddev->raid_disks; mirror++)
1139 if ( !(p=conf->mirrors+mirror)->rdev) {
1141 blk_queue_stack_limits(mddev->queue,
1142 rdev->bdev->bd_disk->queue);
1143 /* as we don't honour merge_bvec_fn, we must never risk
1144 * violating it, so limit ->max_sector to one PAGE, as
1145 * a one page request is never in violation.
1147 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1148 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1149 mddev->queue->max_sectors = (PAGE_SIZE>>9);
1151 p->head_position = 0;
1152 rdev->raid_disk = mirror;
1153 found = 1;
1154 if (rdev->saved_raid_disk != mirror)
1155 conf->fullsync = 1;
1156 rcu_assign_pointer(p->rdev, rdev);
1157 break;
1160 print_conf(conf);
1161 return found;
1164 static int raid10_remove_disk(mddev_t *mddev, int number)
1166 conf_t *conf = mddev->private;
1167 int err = 0;
1168 mdk_rdev_t *rdev;
1169 mirror_info_t *p = conf->mirrors+ number;
1171 print_conf(conf);
1172 rdev = p->rdev;
1173 if (rdev) {
1174 if (test_bit(In_sync, &rdev->flags) ||
1175 atomic_read(&rdev->nr_pending)) {
1176 err = -EBUSY;
1177 goto abort;
1179 p->rdev = NULL;
1180 synchronize_rcu();
1181 if (atomic_read(&rdev->nr_pending)) {
1182 /* lost the race, try later */
1183 err = -EBUSY;
1184 p->rdev = rdev;
1187 abort:
1189 print_conf(conf);
1190 return err;
1194 static void end_sync_read(struct bio *bio, int error)
1196 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1197 conf_t *conf = mddev_to_conf(r10_bio->mddev);
1198 int i,d;
1200 for (i=0; i<conf->copies; i++)
1201 if (r10_bio->devs[i].bio == bio)
1202 break;
1203 BUG_ON(i == conf->copies);
1204 update_head_pos(i, r10_bio);
1205 d = r10_bio->devs[i].devnum;
1207 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1208 set_bit(R10BIO_Uptodate, &r10_bio->state);
1209 else {
1210 atomic_add(r10_bio->sectors,
1211 &conf->mirrors[d].rdev->corrected_errors);
1212 if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
1213 md_error(r10_bio->mddev,
1214 conf->mirrors[d].rdev);
1217 /* for reconstruct, we always reschedule after a read.
1218 * for resync, only after all reads
1220 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1221 atomic_dec_and_test(&r10_bio->remaining)) {
1222 /* we have read all the blocks,
1223 * do the comparison in process context in raid10d
1225 reschedule_retry(r10_bio);
1227 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1230 static void end_sync_write(struct bio *bio, int error)
1232 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1233 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1234 mddev_t *mddev = r10_bio->mddev;
1235 conf_t *conf = mddev_to_conf(mddev);
1236 int i,d;
1238 for (i = 0; i < conf->copies; i++)
1239 if (r10_bio->devs[i].bio == bio)
1240 break;
1241 d = r10_bio->devs[i].devnum;
1243 if (!uptodate)
1244 md_error(mddev, conf->mirrors[d].rdev);
1245 update_head_pos(i, r10_bio);
1247 while (atomic_dec_and_test(&r10_bio->remaining)) {
1248 if (r10_bio->master_bio == NULL) {
1249 /* the primary of several recovery bios */
1250 md_done_sync(mddev, r10_bio->sectors, 1);
1251 put_buf(r10_bio);
1252 break;
1253 } else {
1254 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1255 put_buf(r10_bio);
1256 r10_bio = r10_bio2;
1259 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1263 * Note: sync and recover and handled very differently for raid10
1264 * This code is for resync.
1265 * For resync, we read through virtual addresses and read all blocks.
1266 * If there is any error, we schedule a write. The lowest numbered
1267 * drive is authoritative.
1268 * However requests come for physical address, so we need to map.
1269 * For every physical address there are raid_disks/copies virtual addresses,
1270 * which is always are least one, but is not necessarly an integer.
1271 * This means that a physical address can span multiple chunks, so we may
1272 * have to submit multiple io requests for a single sync request.
1275 * We check if all blocks are in-sync and only write to blocks that
1276 * aren't in sync
1278 static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1280 conf_t *conf = mddev_to_conf(mddev);
1281 int i, first;
1282 struct bio *tbio, *fbio;
1284 atomic_set(&r10_bio->remaining, 1);
1286 /* find the first device with a block */
1287 for (i=0; i<conf->copies; i++)
1288 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1289 break;
1291 if (i == conf->copies)
1292 goto done;
1294 first = i;
1295 fbio = r10_bio->devs[i].bio;
1297 /* now find blocks with errors */
1298 for (i=0 ; i < conf->copies ; i++) {
1299 int j, d;
1300 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
1302 tbio = r10_bio->devs[i].bio;
1304 if (tbio->bi_end_io != end_sync_read)
1305 continue;
1306 if (i == first)
1307 continue;
1308 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1309 /* We know that the bi_io_vec layout is the same for
1310 * both 'first' and 'i', so we just compare them.
1311 * All vec entries are PAGE_SIZE;
1313 for (j = 0; j < vcnt; j++)
1314 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1315 page_address(tbio->bi_io_vec[j].bv_page),
1316 PAGE_SIZE))
1317 break;
1318 if (j == vcnt)
1319 continue;
1320 mddev->resync_mismatches += r10_bio->sectors;
1322 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1323 /* Don't fix anything. */
1324 continue;
1325 /* Ok, we need to write this bio
1326 * First we need to fixup bv_offset, bv_len and
1327 * bi_vecs, as the read request might have corrupted these
1329 tbio->bi_vcnt = vcnt;
1330 tbio->bi_size = r10_bio->sectors << 9;
1331 tbio->bi_idx = 0;
1332 tbio->bi_phys_segments = 0;
1333 tbio->bi_hw_segments = 0;
1334 tbio->bi_hw_front_size = 0;
1335 tbio->bi_hw_back_size = 0;
1336 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1337 tbio->bi_flags |= 1 << BIO_UPTODATE;
1338 tbio->bi_next = NULL;
1339 tbio->bi_rw = WRITE;
1340 tbio->bi_private = r10_bio;
1341 tbio->bi_sector = r10_bio->devs[i].addr;
1343 for (j=0; j < vcnt ; j++) {
1344 tbio->bi_io_vec[j].bv_offset = 0;
1345 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1347 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1348 page_address(fbio->bi_io_vec[j].bv_page),
1349 PAGE_SIZE);
1351 tbio->bi_end_io = end_sync_write;
1353 d = r10_bio->devs[i].devnum;
1354 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1355 atomic_inc(&r10_bio->remaining);
1356 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1358 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1359 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1360 generic_make_request(tbio);
1363 done:
1364 if (atomic_dec_and_test(&r10_bio->remaining)) {
1365 md_done_sync(mddev, r10_bio->sectors, 1);
1366 put_buf(r10_bio);
1371 * Now for the recovery code.
1372 * Recovery happens across physical sectors.
1373 * We recover all non-is_sync drives by finding the virtual address of
1374 * each, and then choose a working drive that also has that virt address.
1375 * There is a separate r10_bio for each non-in_sync drive.
1376 * Only the first two slots are in use. The first for reading,
1377 * The second for writing.
1381 static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1383 conf_t *conf = mddev_to_conf(mddev);
1384 int i, d;
1385 struct bio *bio, *wbio;
1388 /* move the pages across to the second bio
1389 * and submit the write request
1391 bio = r10_bio->devs[0].bio;
1392 wbio = r10_bio->devs[1].bio;
1393 for (i=0; i < wbio->bi_vcnt; i++) {
1394 struct page *p = bio->bi_io_vec[i].bv_page;
1395 bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
1396 wbio->bi_io_vec[i].bv_page = p;
1398 d = r10_bio->devs[1].devnum;
1400 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1401 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
1402 if (test_bit(R10BIO_Uptodate, &r10_bio->state))
1403 generic_make_request(wbio);
1404 else
1405 bio_endio(wbio, -EIO);
1410 * This is a kernel thread which:
1412 * 1. Retries failed read operations on working mirrors.
1413 * 2. Updates the raid superblock when problems encounter.
1414 * 3. Performs writes following reads for array synchronising.
1417 static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
1419 int sect = 0; /* Offset from r10_bio->sector */
1420 int sectors = r10_bio->sectors;
1421 mdk_rdev_t*rdev;
1422 while(sectors) {
1423 int s = sectors;
1424 int sl = r10_bio->read_slot;
1425 int success = 0;
1426 int start;
1428 if (s > (PAGE_SIZE>>9))
1429 s = PAGE_SIZE >> 9;
1431 rcu_read_lock();
1432 do {
1433 int d = r10_bio->devs[sl].devnum;
1434 rdev = rcu_dereference(conf->mirrors[d].rdev);
1435 if (rdev &&
1436 test_bit(In_sync, &rdev->flags)) {
1437 atomic_inc(&rdev->nr_pending);
1438 rcu_read_unlock();
1439 success = sync_page_io(rdev->bdev,
1440 r10_bio->devs[sl].addr +
1441 sect + rdev->data_offset,
1442 s<<9,
1443 conf->tmppage, READ);
1444 rdev_dec_pending(rdev, mddev);
1445 rcu_read_lock();
1446 if (success)
1447 break;
1449 sl++;
1450 if (sl == conf->copies)
1451 sl = 0;
1452 } while (!success && sl != r10_bio->read_slot);
1453 rcu_read_unlock();
1455 if (!success) {
1456 /* Cannot read from anywhere -- bye bye array */
1457 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
1458 md_error(mddev, conf->mirrors[dn].rdev);
1459 break;
1462 start = sl;
1463 /* write it back and re-read */
1464 rcu_read_lock();
1465 while (sl != r10_bio->read_slot) {
1466 int d;
1467 if (sl==0)
1468 sl = conf->copies;
1469 sl--;
1470 d = r10_bio->devs[sl].devnum;
1471 rdev = rcu_dereference(conf->mirrors[d].rdev);
1472 if (rdev &&
1473 test_bit(In_sync, &rdev->flags)) {
1474 atomic_inc(&rdev->nr_pending);
1475 rcu_read_unlock();
1476 atomic_add(s, &rdev->corrected_errors);
1477 if (sync_page_io(rdev->bdev,
1478 r10_bio->devs[sl].addr +
1479 sect + rdev->data_offset,
1480 s<<9, conf->tmppage, WRITE)
1481 == 0)
1482 /* Well, this device is dead */
1483 md_error(mddev, rdev);
1484 rdev_dec_pending(rdev, mddev);
1485 rcu_read_lock();
1488 sl = start;
1489 while (sl != r10_bio->read_slot) {
1490 int d;
1491 if (sl==0)
1492 sl = conf->copies;
1493 sl--;
1494 d = r10_bio->devs[sl].devnum;
1495 rdev = rcu_dereference(conf->mirrors[d].rdev);
1496 if (rdev &&
1497 test_bit(In_sync, &rdev->flags)) {
1498 char b[BDEVNAME_SIZE];
1499 atomic_inc(&rdev->nr_pending);
1500 rcu_read_unlock();
1501 if (sync_page_io(rdev->bdev,
1502 r10_bio->devs[sl].addr +
1503 sect + rdev->data_offset,
1504 s<<9, conf->tmppage, READ) == 0)
1505 /* Well, this device is dead */
1506 md_error(mddev, rdev);
1507 else
1508 printk(KERN_INFO
1509 "raid10:%s: read error corrected"
1510 " (%d sectors at %llu on %s)\n",
1511 mdname(mddev), s,
1512 (unsigned long long)(sect+
1513 rdev->data_offset),
1514 bdevname(rdev->bdev, b));
1516 rdev_dec_pending(rdev, mddev);
1517 rcu_read_lock();
1520 rcu_read_unlock();
1522 sectors -= s;
1523 sect += s;
1527 static void raid10d(mddev_t *mddev)
1529 r10bio_t *r10_bio;
1530 struct bio *bio;
1531 unsigned long flags;
1532 conf_t *conf = mddev_to_conf(mddev);
1533 struct list_head *head = &conf->retry_list;
1534 int unplug=0;
1535 mdk_rdev_t *rdev;
1537 md_check_recovery(mddev);
1539 for (;;) {
1540 char b[BDEVNAME_SIZE];
1541 <<<<<<< HEAD:drivers/md/raid10.c
1542 spin_lock_irqsave(&conf->device_lock, flags);
1543 =======
1544 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/md/raid10.c
1546 <<<<<<< HEAD:drivers/md/raid10.c
1547 if (conf->pending_bio_list.head) {
1548 bio = bio_list_get(&conf->pending_bio_list);
1549 blk_remove_plug(mddev->queue);
1550 spin_unlock_irqrestore(&conf->device_lock, flags);
1551 /* flush any pending bitmap writes to disk before proceeding w/ I/O */
1552 bitmap_unplug(mddev->bitmap);
1554 while (bio) { /* submit pending writes */
1555 struct bio *next = bio->bi_next;
1556 bio->bi_next = NULL;
1557 generic_make_request(bio);
1558 bio = next;
1560 unplug = 1;
1562 continue;
1564 =======
1565 unplug += flush_pending_writes(conf);
1566 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/md/raid10.c
1568 <<<<<<< HEAD:drivers/md/raid10.c
1569 if (list_empty(head))
1570 =======
1571 spin_lock_irqsave(&conf->device_lock, flags);
1572 if (list_empty(head)) {
1573 spin_unlock_irqrestore(&conf->device_lock, flags);
1574 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/md/raid10.c
1575 break;
1576 <<<<<<< HEAD:drivers/md/raid10.c
1577 =======
1579 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/md/raid10.c
1580 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1581 list_del(head->prev);
1582 conf->nr_queued--;
1583 spin_unlock_irqrestore(&conf->device_lock, flags);
1585 mddev = r10_bio->mddev;
1586 conf = mddev_to_conf(mddev);
1587 if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
1588 sync_request_write(mddev, r10_bio);
1589 unplug = 1;
1590 } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1591 recovery_request_write(mddev, r10_bio);
1592 unplug = 1;
1593 } else {
1594 int mirror;
1595 /* we got a read error. Maybe the drive is bad. Maybe just
1596 * the block and we can fix it.
1597 * We freeze all other IO, and try reading the block from
1598 * other devices. When we find one, we re-write
1599 * and check it that fixes the read error.
1600 * This is all done synchronously while the array is
1601 * frozen.
1603 if (mddev->ro == 0) {
1604 freeze_array(conf);
1605 fix_read_error(conf, mddev, r10_bio);
1606 unfreeze_array(conf);
1609 bio = r10_bio->devs[r10_bio->read_slot].bio;
1610 r10_bio->devs[r10_bio->read_slot].bio =
1611 mddev->ro ? IO_BLOCKED : NULL;
1612 mirror = read_balance(conf, r10_bio);
1613 if (mirror == -1) {
1614 printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
1615 " read error for block %llu\n",
1616 bdevname(bio->bi_bdev,b),
1617 (unsigned long long)r10_bio->sector);
1618 raid_end_bio_io(r10_bio);
1619 bio_put(bio);
1620 } else {
1621 const int do_sync = bio_sync(r10_bio->master_bio);
1622 bio_put(bio);
1623 rdev = conf->mirrors[mirror].rdev;
1624 if (printk_ratelimit())
1625 printk(KERN_ERR "raid10: %s: redirecting sector %llu to"
1626 " another mirror\n",
1627 bdevname(rdev->bdev,b),
1628 (unsigned long long)r10_bio->sector);
1629 bio = bio_clone(r10_bio->master_bio, GFP_NOIO);
1630 r10_bio->devs[r10_bio->read_slot].bio = bio;
1631 bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
1632 + rdev->data_offset;
1633 bio->bi_bdev = rdev->bdev;
1634 bio->bi_rw = READ | do_sync;
1635 bio->bi_private = r10_bio;
1636 bio->bi_end_io = raid10_end_read_request;
1637 unplug = 1;
1638 generic_make_request(bio);
1642 <<<<<<< HEAD:drivers/md/raid10.c
1643 spin_unlock_irqrestore(&conf->device_lock, flags);
1644 =======
1645 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/md/raid10.c
1646 if (unplug)
1647 unplug_slaves(mddev);
1651 static int init_resync(conf_t *conf)
1653 int buffs;
1655 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
1656 BUG_ON(conf->r10buf_pool);
1657 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1658 if (!conf->r10buf_pool)
1659 return -ENOMEM;
1660 conf->next_resync = 0;
1661 return 0;
1665 * perform a "sync" on one "block"
1667 * We need to make sure that no normal I/O request - particularly write
1668 * requests - conflict with active sync requests.
1670 * This is achieved by tracking pending requests and a 'barrier' concept
1671 * that can be installed to exclude normal IO requests.
1673 * Resync and recovery are handled very differently.
1674 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1676 * For resync, we iterate over virtual addresses, read all copies,
1677 * and update if there are differences. If only one copy is live,
1678 * skip it.
1679 * For recovery, we iterate over physical addresses, read a good
1680 * value for each non-in_sync drive, and over-write.
1682 * So, for recovery we may have several outstanding complex requests for a
1683 * given address, one for each out-of-sync device. We model this by allocating
1684 * a number of r10_bio structures, one for each out-of-sync device.
1685 * As we setup these structures, we collect all bio's together into a list
1686 * which we then process collectively to add pages, and then process again
1687 * to pass to generic_make_request.
1689 * The r10_bio structures are linked using a borrowed master_bio pointer.
1690 * This link is counted in ->remaining. When the r10_bio that points to NULL
1691 * has its remaining count decremented to 0, the whole complex operation
1692 * is complete.
1696 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1698 conf_t *conf = mddev_to_conf(mddev);
1699 r10bio_t *r10_bio;
1700 struct bio *biolist = NULL, *bio;
1701 sector_t max_sector, nr_sectors;
1702 int disk;
1703 int i;
1704 int max_sync;
1705 int sync_blocks;
1707 sector_t sectors_skipped = 0;
1708 int chunks_skipped = 0;
1710 if (!conf->r10buf_pool)
1711 if (init_resync(conf))
1712 return 0;
1714 skipped:
1715 max_sector = mddev->size << 1;
1716 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1717 max_sector = mddev->resync_max_sectors;
1718 if (sector_nr >= max_sector) {
1719 /* If we aborted, we need to abort the
1720 * sync on the 'current' bitmap chucks (there can
1721 * be several when recovering multiple devices).
1722 * as we may have started syncing it but not finished.
1723 * We can find the current address in
1724 * mddev->curr_resync, but for recovery,
1725 * we need to convert that to several
1726 * virtual addresses.
1728 if (mddev->curr_resync < max_sector) { /* aborted */
1729 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1730 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1731 &sync_blocks, 1);
1732 else for (i=0; i<conf->raid_disks; i++) {
1733 sector_t sect =
1734 raid10_find_virt(conf, mddev->curr_resync, i);
1735 bitmap_end_sync(mddev->bitmap, sect,
1736 &sync_blocks, 1);
1738 } else /* completed sync */
1739 conf->fullsync = 0;
1741 bitmap_close_sync(mddev->bitmap);
1742 close_sync(conf);
1743 *skipped = 1;
1744 return sectors_skipped;
1746 if (chunks_skipped >= conf->raid_disks) {
1747 /* if there has been nothing to do on any drive,
1748 * then there is nothing to do at all..
1750 *skipped = 1;
1751 return (max_sector - sector_nr) + sectors_skipped;
1754 if (max_sector > mddev->resync_max)
1755 max_sector = mddev->resync_max; /* Don't do IO beyond here */
1757 /* make sure whole request will fit in a chunk - if chunks
1758 * are meaningful
1760 if (conf->near_copies < conf->raid_disks &&
1761 max_sector > (sector_nr | conf->chunk_mask))
1762 max_sector = (sector_nr | conf->chunk_mask) + 1;
1764 * If there is non-resync activity waiting for us then
1765 * put in a delay to throttle resync.
1767 if (!go_faster && conf->nr_waiting)
1768 msleep_interruptible(1000);
1770 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
1772 /* Again, very different code for resync and recovery.
1773 * Both must result in an r10bio with a list of bios that
1774 * have bi_end_io, bi_sector, bi_bdev set,
1775 * and bi_private set to the r10bio.
1776 * For recovery, we may actually create several r10bios
1777 * with 2 bios in each, that correspond to the bios in the main one.
1778 * In this case, the subordinate r10bios link back through a
1779 * borrowed master_bio pointer, and the counter in the master
1780 * includes a ref from each subordinate.
1782 /* First, we decide what to do and set ->bi_end_io
1783 * To end_sync_read if we want to read, and
1784 * end_sync_write if we will want to write.
1787 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
1788 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1789 /* recovery... the complicated one */
1790 int i, j, k;
1791 r10_bio = NULL;
1793 for (i=0 ; i<conf->raid_disks; i++)
1794 if (conf->mirrors[i].rdev &&
1795 !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) {
1796 int still_degraded = 0;
1797 /* want to reconstruct this device */
1798 r10bio_t *rb2 = r10_bio;
1799 sector_t sect = raid10_find_virt(conf, sector_nr, i);
1800 int must_sync;
1801 /* Unless we are doing a full sync, we only need
1802 * to recover the block if it is set in the bitmap
1804 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1805 &sync_blocks, 1);
1806 if (sync_blocks < max_sync)
1807 max_sync = sync_blocks;
1808 if (!must_sync &&
1809 !conf->fullsync) {
1810 /* yep, skip the sync_blocks here, but don't assume
1811 * that there will never be anything to do here
1813 chunks_skipped = -1;
1814 continue;
1817 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1818 raise_barrier(conf, rb2 != NULL);
1819 atomic_set(&r10_bio->remaining, 0);
1821 r10_bio->master_bio = (struct bio*)rb2;
1822 if (rb2)
1823 atomic_inc(&rb2->remaining);
1824 r10_bio->mddev = mddev;
1825 set_bit(R10BIO_IsRecover, &r10_bio->state);
1826 r10_bio->sector = sect;
1828 raid10_find_phys(conf, r10_bio);
1829 /* Need to check if this section will still be
1830 * degraded
1832 for (j=0; j<conf->copies;j++) {
1833 int d = r10_bio->devs[j].devnum;
1834 if (conf->mirrors[d].rdev == NULL ||
1835 test_bit(Faulty, &conf->mirrors[d].rdev->flags)) {
1836 still_degraded = 1;
1837 break;
1840 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1841 &sync_blocks, still_degraded);
1843 for (j=0; j<conf->copies;j++) {
1844 int d = r10_bio->devs[j].devnum;
1845 if (conf->mirrors[d].rdev &&
1846 test_bit(In_sync, &conf->mirrors[d].rdev->flags)) {
1847 /* This is where we read from */
1848 bio = r10_bio->devs[0].bio;
1849 bio->bi_next = biolist;
1850 biolist = bio;
1851 bio->bi_private = r10_bio;
1852 bio->bi_end_io = end_sync_read;
1853 bio->bi_rw = READ;
1854 bio->bi_sector = r10_bio->devs[j].addr +
1855 conf->mirrors[d].rdev->data_offset;
1856 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1857 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1858 atomic_inc(&r10_bio->remaining);
1859 /* and we write to 'i' */
1861 for (k=0; k<conf->copies; k++)
1862 if (r10_bio->devs[k].devnum == i)
1863 break;
1864 BUG_ON(k == conf->copies);
1865 bio = r10_bio->devs[1].bio;
1866 bio->bi_next = biolist;
1867 biolist = bio;
1868 bio->bi_private = r10_bio;
1869 bio->bi_end_io = end_sync_write;
1870 bio->bi_rw = WRITE;
1871 bio->bi_sector = r10_bio->devs[k].addr +
1872 conf->mirrors[i].rdev->data_offset;
1873 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1875 r10_bio->devs[0].devnum = d;
1876 r10_bio->devs[1].devnum = i;
1878 break;
1881 if (j == conf->copies) {
1882 /* Cannot recover, so abort the recovery */
1883 put_buf(r10_bio);
1884 <<<<<<< HEAD:drivers/md/raid10.c
1885 =======
1886 if (rb2)
1887 atomic_dec(&rb2->remaining);
1888 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/md/raid10.c
1889 r10_bio = rb2;
1890 if (!test_and_set_bit(MD_RECOVERY_ERR, &mddev->recovery))
1891 printk(KERN_INFO "raid10: %s: insufficient working devices for recovery.\n",
1892 mdname(mddev));
1893 break;
1896 if (biolist == NULL) {
1897 while (r10_bio) {
1898 r10bio_t *rb2 = r10_bio;
1899 r10_bio = (r10bio_t*) rb2->master_bio;
1900 rb2->master_bio = NULL;
1901 put_buf(rb2);
1903 goto giveup;
1905 } else {
1906 /* resync. Schedule a read for every block at this virt offset */
1907 int count = 0;
1909 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1910 &sync_blocks, mddev->degraded) &&
1911 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1912 /* We can skip this block */
1913 *skipped = 1;
1914 return sync_blocks + sectors_skipped;
1916 if (sync_blocks < max_sync)
1917 max_sync = sync_blocks;
1918 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1920 r10_bio->mddev = mddev;
1921 atomic_set(&r10_bio->remaining, 0);
1922 raise_barrier(conf, 0);
1923 conf->next_resync = sector_nr;
1925 r10_bio->master_bio = NULL;
1926 r10_bio->sector = sector_nr;
1927 set_bit(R10BIO_IsSync, &r10_bio->state);
1928 raid10_find_phys(conf, r10_bio);
1929 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
1931 for (i=0; i<conf->copies; i++) {
1932 int d = r10_bio->devs[i].devnum;
1933 bio = r10_bio->devs[i].bio;
1934 bio->bi_end_io = NULL;
1935 clear_bit(BIO_UPTODATE, &bio->bi_flags);
1936 if (conf->mirrors[d].rdev == NULL ||
1937 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
1938 continue;
1939 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1940 atomic_inc(&r10_bio->remaining);
1941 bio->bi_next = biolist;
1942 biolist = bio;
1943 bio->bi_private = r10_bio;
1944 bio->bi_end_io = end_sync_read;
1945 bio->bi_rw = READ;
1946 bio->bi_sector = r10_bio->devs[i].addr +
1947 conf->mirrors[d].rdev->data_offset;
1948 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1949 count++;
1952 if (count < 2) {
1953 for (i=0; i<conf->copies; i++) {
1954 int d = r10_bio->devs[i].devnum;
1955 if (r10_bio->devs[i].bio->bi_end_io)
1956 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1958 put_buf(r10_bio);
1959 biolist = NULL;
1960 goto giveup;
1964 for (bio = biolist; bio ; bio=bio->bi_next) {
1966 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
1967 if (bio->bi_end_io)
1968 bio->bi_flags |= 1 << BIO_UPTODATE;
1969 bio->bi_vcnt = 0;
1970 bio->bi_idx = 0;
1971 bio->bi_phys_segments = 0;
1972 bio->bi_hw_segments = 0;
1973 bio->bi_size = 0;
1976 nr_sectors = 0;
1977 if (sector_nr + max_sync < max_sector)
1978 max_sector = sector_nr + max_sync;
1979 do {
1980 struct page *page;
1981 int len = PAGE_SIZE;
1982 disk = 0;
1983 if (sector_nr + (len>>9) > max_sector)
1984 len = (max_sector - sector_nr) << 9;
1985 if (len == 0)
1986 break;
1987 for (bio= biolist ; bio ; bio=bio->bi_next) {
1988 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
1989 if (bio_add_page(bio, page, len, 0) == 0) {
1990 /* stop here */
1991 struct bio *bio2;
1992 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
1993 for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
1994 /* remove last page from this bio */
1995 bio2->bi_vcnt--;
1996 bio2->bi_size -= len;
1997 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
1999 goto bio_full;
2001 disk = i;
2003 nr_sectors += len>>9;
2004 sector_nr += len>>9;
2005 } while (biolist->bi_vcnt < RESYNC_PAGES);
2006 bio_full:
2007 r10_bio->sectors = nr_sectors;
2009 while (biolist) {
2010 bio = biolist;
2011 biolist = biolist->bi_next;
2013 bio->bi_next = NULL;
2014 r10_bio = bio->bi_private;
2015 r10_bio->sectors = nr_sectors;
2017 if (bio->bi_end_io == end_sync_read) {
2018 md_sync_acct(bio->bi_bdev, nr_sectors);
2019 generic_make_request(bio);
2023 if (sectors_skipped)
2024 /* pretend they weren't skipped, it makes
2025 * no important difference in this case
2027 md_done_sync(mddev, sectors_skipped, 1);
2029 return sectors_skipped + nr_sectors;
2030 giveup:
2031 /* There is nowhere to write, so all non-sync
2032 * drives must be failed, so try the next chunk...
2035 sector_t sec = max_sector - sector_nr;
2036 sectors_skipped += sec;
2037 chunks_skipped ++;
2038 sector_nr = max_sector;
2039 goto skipped;
2043 static int run(mddev_t *mddev)
2045 conf_t *conf;
2046 int i, disk_idx;
2047 mirror_info_t *disk;
2048 mdk_rdev_t *rdev;
2049 struct list_head *tmp;
2050 int nc, fc, fo;
2051 sector_t stride, size;
2053 if (mddev->chunk_size == 0) {
2054 printk(KERN_ERR "md/raid10: non-zero chunk size required.\n");
2055 return -EINVAL;
2058 nc = mddev->layout & 255;
2059 fc = (mddev->layout >> 8) & 255;
2060 fo = mddev->layout & (1<<16);
2061 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
2062 (mddev->layout >> 17)) {
2063 printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n",
2064 mdname(mddev), mddev->layout);
2065 goto out;
2068 * copy the already verified devices into our private RAID10
2069 * bookkeeping area. [whatever we allocate in run(),
2070 * should be freed in stop()]
2072 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
2073 mddev->private = conf;
2074 if (!conf) {
2075 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2076 mdname(mddev));
2077 goto out;
2079 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
2080 GFP_KERNEL);
2081 if (!conf->mirrors) {
2082 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2083 mdname(mddev));
2084 goto out_free_conf;
2087 conf->tmppage = alloc_page(GFP_KERNEL);
2088 if (!conf->tmppage)
2089 goto out_free_conf;
2091 conf->mddev = mddev;
2092 conf->raid_disks = mddev->raid_disks;
2093 conf->near_copies = nc;
2094 conf->far_copies = fc;
2095 conf->copies = nc*fc;
2096 conf->far_offset = fo;
2097 conf->chunk_mask = (sector_t)(mddev->chunk_size>>9)-1;
2098 conf->chunk_shift = ffz(~mddev->chunk_size) - 9;
2099 size = mddev->size >> (conf->chunk_shift-1);
2100 sector_div(size, fc);
2101 size = size * conf->raid_disks;
2102 sector_div(size, nc);
2103 /* 'size' is now the number of chunks in the array */
2104 /* calculate "used chunks per device" in 'stride' */
2105 stride = size * conf->copies;
2107 /* We need to round up when dividing by raid_disks to
2108 * get the stride size.
2110 stride += conf->raid_disks - 1;
2111 sector_div(stride, conf->raid_disks);
2112 mddev->size = stride << (conf->chunk_shift-1);
2114 if (fo)
2115 stride = 1;
2116 else
2117 sector_div(stride, fc);
2118 conf->stride = stride << conf->chunk_shift;
2120 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2121 r10bio_pool_free, conf);
2122 if (!conf->r10bio_pool) {
2123 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2124 mdname(mddev));
2125 goto out_free_conf;
2128 rdev_for_each(rdev, tmp, mddev) {
2129 disk_idx = rdev->raid_disk;
2130 if (disk_idx >= mddev->raid_disks
2131 || disk_idx < 0)
2132 continue;
2133 disk = conf->mirrors + disk_idx;
2135 disk->rdev = rdev;
2137 blk_queue_stack_limits(mddev->queue,
2138 rdev->bdev->bd_disk->queue);
2139 /* as we don't honour merge_bvec_fn, we must never risk
2140 * violating it, so limit ->max_sector to one PAGE, as
2141 * a one page request is never in violation.
2143 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
2144 mddev->queue->max_sectors > (PAGE_SIZE>>9))
2145 mddev->queue->max_sectors = (PAGE_SIZE>>9);
2147 disk->head_position = 0;
2149 spin_lock_init(&conf->device_lock);
2150 INIT_LIST_HEAD(&conf->retry_list);
2152 spin_lock_init(&conf->resync_lock);
2153 init_waitqueue_head(&conf->wait_barrier);
2155 /* need to check that every block has at least one working mirror */
2156 if (!enough(conf)) {
2157 printk(KERN_ERR "raid10: not enough operational mirrors for %s\n",
2158 mdname(mddev));
2159 goto out_free_conf;
2162 mddev->degraded = 0;
2163 for (i = 0; i < conf->raid_disks; i++) {
2165 disk = conf->mirrors + i;
2167 if (!disk->rdev ||
2168 !test_bit(In_sync, &disk->rdev->flags)) {
2169 disk->head_position = 0;
2170 mddev->degraded++;
2175 mddev->thread = md_register_thread(raid10d, mddev, "%s_raid10");
2176 if (!mddev->thread) {
2177 printk(KERN_ERR
2178 "raid10: couldn't allocate thread for %s\n",
2179 mdname(mddev));
2180 goto out_free_conf;
2183 printk(KERN_INFO
2184 "raid10: raid set %s active with %d out of %d devices\n",
2185 mdname(mddev), mddev->raid_disks - mddev->degraded,
2186 mddev->raid_disks);
2188 * Ok, everything is just fine now
2190 mddev->array_size = size << (conf->chunk_shift-1);
2191 mddev->resync_max_sectors = size << conf->chunk_shift;
2193 mddev->queue->unplug_fn = raid10_unplug;
2194 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2195 mddev->queue->backing_dev_info.congested_data = mddev;
2197 /* Calculate max read-ahead size.
2198 * We need to readahead at least twice a whole stripe....
2199 * maybe...
2202 int stripe = conf->raid_disks * (mddev->chunk_size / PAGE_SIZE);
2203 stripe /= conf->near_copies;
2204 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2205 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2208 if (conf->near_copies < mddev->raid_disks)
2209 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
2210 return 0;
2212 out_free_conf:
2213 if (conf->r10bio_pool)
2214 mempool_destroy(conf->r10bio_pool);
2215 safe_put_page(conf->tmppage);
2216 kfree(conf->mirrors);
2217 kfree(conf);
2218 mddev->private = NULL;
2219 out:
2220 return -EIO;
2223 static int stop(mddev_t *mddev)
2225 conf_t *conf = mddev_to_conf(mddev);
2227 md_unregister_thread(mddev->thread);
2228 mddev->thread = NULL;
2229 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2230 if (conf->r10bio_pool)
2231 mempool_destroy(conf->r10bio_pool);
2232 kfree(conf->mirrors);
2233 kfree(conf);
2234 mddev->private = NULL;
2235 return 0;
2238 static void raid10_quiesce(mddev_t *mddev, int state)
2240 conf_t *conf = mddev_to_conf(mddev);
2242 switch(state) {
2243 case 1:
2244 raise_barrier(conf, 0);
2245 break;
2246 case 0:
2247 lower_barrier(conf);
2248 break;
2250 if (mddev->thread) {
2251 if (mddev->bitmap)
2252 mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
2253 else
2254 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
2255 md_wakeup_thread(mddev->thread);
2259 static struct mdk_personality raid10_personality =
2261 .name = "raid10",
2262 .level = 10,
2263 .owner = THIS_MODULE,
2264 .make_request = make_request,
2265 .run = run,
2266 .stop = stop,
2267 .status = status,
2268 .error_handler = error,
2269 .hot_add_disk = raid10_add_disk,
2270 .hot_remove_disk= raid10_remove_disk,
2271 .spare_active = raid10_spare_active,
2272 .sync_request = sync_request,
2273 .quiesce = raid10_quiesce,
2276 static int __init raid_init(void)
2278 return register_md_personality(&raid10_personality);
2281 static void raid_exit(void)
2283 unregister_md_personality(&raid10_personality);
2286 module_init(raid_init);
2287 module_exit(raid_exit);
2288 MODULE_LICENSE("GPL");
2289 MODULE_ALIAS("md-personality-9"); /* RAID10 */
2290 MODULE_ALIAS("md-raid10");
2291 MODULE_ALIAS("md-level-10");