MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / md / raid10.c
blob6c3fde9ba19235816d2bb9764740a601e7b0b873
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 <linux/raid/raid10.h>
24 * RAID10 provides a combination of RAID0 and RAID1 functionality.
25 * The layout of data is defined by
26 * chunk_size
27 * raid_disks
28 * near_copies (stored in low byte of layout)
29 * far_copies (stored in second byte of layout)
31 * The data to be stored is divided into chunks using chunksize.
32 * Each device is divided into far_copies sections.
33 * In each section, chunks are laid out in a style similar to raid0, but
34 * near_copies copies of each chunk is stored (each on a different drive).
35 * The starting device for each section is offset near_copies from the starting
36 * device of the previous section.
37 * Thus there are (near_copies*far_copies) of each chunk, and each is on a different
38 * drive.
39 * near_copies and far_copies must be at least one, and there product is at most
40 * raid_disks.
44 * Number of guaranteed r10bios in case of extreme VM load:
46 #define NR_RAID10_BIOS 256
48 static void unplug_slaves(mddev_t *mddev);
50 static void * r10bio_pool_alloc(int gfp_flags, void *data)
52 conf_t *conf = data;
53 r10bio_t *r10_bio;
54 int size = offsetof(struct r10bio_s, devs[conf->copies]);
56 /* allocate a r10bio with room for raid_disks entries in the bios array */
57 r10_bio = kmalloc(size, gfp_flags);
58 if (r10_bio)
59 memset(r10_bio, 0, size);
60 else
61 unplug_slaves(conf->mddev);
63 return r10_bio;
66 static void r10bio_pool_free(void *r10_bio, void *data)
68 kfree(r10_bio);
71 #define RESYNC_BLOCK_SIZE (64*1024)
72 //#define RESYNC_BLOCK_SIZE PAGE_SIZE
73 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
74 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
75 #define RESYNC_WINDOW (2048*1024)
78 * When performing a resync, we need to read and compare, so
79 * we need as many pages are there are copies.
80 * When performing a recovery, we need 2 bios, one for read,
81 * one for write (we recover only one drive per r10buf)
84 static void * r10buf_pool_alloc(int gfp_flags, void *data)
86 conf_t *conf = data;
87 struct page *page;
88 r10bio_t *r10_bio;
89 struct bio *bio;
90 int i, j;
91 int nalloc;
93 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
94 if (!r10_bio) {
95 unplug_slaves(conf->mddev);
96 return NULL;
99 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
100 nalloc = conf->copies; /* resync */
101 else
102 nalloc = 2; /* recovery */
105 * Allocate bios.
107 for (j = nalloc ; j-- ; ) {
108 bio = bio_alloc(gfp_flags, RESYNC_PAGES);
109 if (!bio)
110 goto out_free_bio;
111 r10_bio->devs[j].bio = bio;
114 * Allocate RESYNC_PAGES data pages and attach them
115 * where needed.
117 for (j = 0 ; j < nalloc; j++) {
118 bio = r10_bio->devs[j].bio;
119 for (i = 0; i < RESYNC_PAGES; i++) {
120 page = alloc_page(gfp_flags);
121 if (unlikely(!page))
122 goto out_free_pages;
124 bio->bi_io_vec[i].bv_page = page;
128 return r10_bio;
130 out_free_pages:
131 for ( ; i > 0 ; i--)
132 __free_page(bio->bi_io_vec[i-1].bv_page);
133 while (j--)
134 for (i = 0; i < RESYNC_PAGES ; i++)
135 __free_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
136 j = -1;
137 out_free_bio:
138 while ( ++j < nalloc )
139 bio_put(r10_bio->devs[j].bio);
140 r10bio_pool_free(r10_bio, conf);
141 return NULL;
144 static void r10buf_pool_free(void *__r10_bio, void *data)
146 int i;
147 conf_t *conf = data;
148 r10bio_t *r10bio = __r10_bio;
149 int j;
151 for (j=0; j < conf->copies; j++) {
152 struct bio *bio = r10bio->devs[j].bio;
153 if (bio) {
154 for (i = 0; i < RESYNC_PAGES; i++) {
155 __free_page(bio->bi_io_vec[i].bv_page);
156 bio->bi_io_vec[i].bv_page = NULL;
158 bio_put(bio);
161 r10bio_pool_free(r10bio, conf);
164 static void put_all_bios(conf_t *conf, r10bio_t *r10_bio)
166 int i;
168 for (i = 0; i < conf->copies; i++) {
169 struct bio **bio = & r10_bio->devs[i].bio;
170 if (*bio)
171 bio_put(*bio);
172 *bio = NULL;
176 static inline void free_r10bio(r10bio_t *r10_bio)
178 unsigned long flags;
180 conf_t *conf = mddev_to_conf(r10_bio->mddev);
183 * Wake up any possible resync thread that waits for the device
184 * to go idle.
186 spin_lock_irqsave(&conf->resync_lock, flags);
187 if (!--conf->nr_pending) {
188 wake_up(&conf->wait_idle);
189 wake_up(&conf->wait_resume);
191 spin_unlock_irqrestore(&conf->resync_lock, flags);
193 put_all_bios(conf, r10_bio);
194 mempool_free(r10_bio, conf->r10bio_pool);
197 static inline void put_buf(r10bio_t *r10_bio)
199 conf_t *conf = mddev_to_conf(r10_bio->mddev);
200 unsigned long flags;
202 mempool_free(r10_bio, conf->r10buf_pool);
204 spin_lock_irqsave(&conf->resync_lock, flags);
205 if (!conf->barrier)
206 BUG();
207 --conf->barrier;
208 wake_up(&conf->wait_resume);
209 wake_up(&conf->wait_idle);
211 if (!--conf->nr_pending) {
212 wake_up(&conf->wait_idle);
213 wake_up(&conf->wait_resume);
215 spin_unlock_irqrestore(&conf->resync_lock, flags);
218 static void reschedule_retry(r10bio_t *r10_bio)
220 unsigned long flags;
221 mddev_t *mddev = r10_bio->mddev;
222 conf_t *conf = mddev_to_conf(mddev);
224 spin_lock_irqsave(&conf->device_lock, flags);
225 list_add(&r10_bio->retry_list, &conf->retry_list);
226 spin_unlock_irqrestore(&conf->device_lock, flags);
228 md_wakeup_thread(mddev->thread);
232 * raid_end_bio_io() is called when we have finished servicing a mirrored
233 * operation and are ready to return a success/failure code to the buffer
234 * cache layer.
236 static void raid_end_bio_io(r10bio_t *r10_bio)
238 struct bio *bio = r10_bio->master_bio;
240 bio_endio(bio, bio->bi_size,
241 test_bit(R10BIO_Uptodate, &r10_bio->state) ? 0 : -EIO);
242 free_r10bio(r10_bio);
246 * Update disk head position estimator based on IRQ completion info.
248 static inline void update_head_pos(int slot, r10bio_t *r10_bio)
250 conf_t *conf = mddev_to_conf(r10_bio->mddev);
252 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
253 r10_bio->devs[slot].addr + (r10_bio->sectors);
256 static int raid10_end_read_request(struct bio *bio, unsigned int bytes_done, int error)
258 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
259 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
260 int slot, dev;
261 conf_t *conf = mddev_to_conf(r10_bio->mddev);
263 if (bio->bi_size)
264 return 1;
266 slot = r10_bio->read_slot;
267 dev = r10_bio->devs[slot].devnum;
269 * this branch is our 'one mirror IO has finished' event handler:
271 if (!uptodate)
272 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
273 else
275 * Set R10BIO_Uptodate in our master bio, so that
276 * we will return a good error code to the higher
277 * levels even if IO on some other mirrored buffer fails.
279 * The 'master' represents the composite IO operation to
280 * user-side. So if something waits for IO, then it will
281 * wait for the 'master' bio.
283 set_bit(R10BIO_Uptodate, &r10_bio->state);
285 update_head_pos(slot, r10_bio);
288 * we have only one bio on the read side
290 if (uptodate)
291 raid_end_bio_io(r10_bio);
292 else {
294 * oops, read error:
296 char b[BDEVNAME_SIZE];
297 if (printk_ratelimit())
298 printk(KERN_ERR "raid10: %s: rescheduling sector %llu\n",
299 bdevname(conf->mirrors[dev].rdev->bdev,b), (unsigned long long)r10_bio->sector);
300 reschedule_retry(r10_bio);
303 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
304 return 0;
307 static int raid10_end_write_request(struct bio *bio, unsigned int bytes_done, int error)
309 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
310 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
311 int slot, dev;
312 conf_t *conf = mddev_to_conf(r10_bio->mddev);
314 if (bio->bi_size)
315 return 1;
317 for (slot = 0; slot < conf->copies; slot++)
318 if (r10_bio->devs[slot].bio == bio)
319 break;
320 dev = r10_bio->devs[slot].devnum;
323 * this branch is our 'one mirror IO has finished' event handler:
325 if (!uptodate)
326 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
327 else
329 * Set R10BIO_Uptodate in our master bio, so that
330 * we will return a good error code for to the higher
331 * levels even if IO on some other mirrored buffer fails.
333 * The 'master' represents the composite IO operation to
334 * user-side. So if something waits for IO, then it will
335 * wait for the 'master' bio.
337 set_bit(R10BIO_Uptodate, &r10_bio->state);
339 update_head_pos(slot, r10_bio);
343 * Let's see if all mirrored write operations have finished
344 * already.
346 if (atomic_dec_and_test(&r10_bio->remaining)) {
347 md_write_end(r10_bio->mddev);
348 raid_end_bio_io(r10_bio);
351 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
352 return 0;
357 * RAID10 layout manager
358 * Aswell as the chunksize and raid_disks count, there are two
359 * parameters: near_copies and far_copies.
360 * near_copies * far_copies must be <= raid_disks.
361 * Normally one of these will be 1.
362 * If both are 1, we get raid0.
363 * If near_copies == raid_disks, we get raid1.
365 * Chunks are layed out in raid0 style with near_copies copies of the
366 * first chunk, followed by near_copies copies of the next chunk and
367 * so on.
368 * If far_copies > 1, then after 1/far_copies of the array has been assigned
369 * as described above, we start again with a device offset of near_copies.
370 * So we effectively have another copy of the whole array further down all
371 * the drives, but with blocks on different drives.
372 * With this layout, and block is never stored twice on the one device.
374 * raid10_find_phys finds the sector offset of a given virtual sector
375 * on each device that it is on. If a block isn't on a device,
376 * that entry in the array is set to MaxSector.
378 * raid10_find_virt does the reverse mapping, from a device and a
379 * sector offset to a virtual address
382 static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
384 int n,f;
385 sector_t sector;
386 sector_t chunk;
387 sector_t stripe;
388 int dev;
390 int slot = 0;
392 /* now calculate first sector/dev */
393 chunk = r10bio->sector >> conf->chunk_shift;
394 sector = r10bio->sector & conf->chunk_mask;
396 chunk *= conf->near_copies;
397 stripe = chunk;
398 dev = sector_div(stripe, conf->raid_disks);
400 sector += stripe << conf->chunk_shift;
402 /* and calculate all the others */
403 for (n=0; n < conf->near_copies; n++) {
404 int d = dev;
405 sector_t s = sector;
406 r10bio->devs[slot].addr = sector;
407 r10bio->devs[slot].devnum = d;
408 slot++;
410 for (f = 1; f < conf->far_copies; f++) {
411 d += conf->near_copies;
412 if (d >= conf->raid_disks)
413 d -= conf->raid_disks;
414 s += conf->stride;
415 r10bio->devs[slot].devnum = d;
416 r10bio->devs[slot].addr = s;
417 slot++;
419 dev++;
420 if (dev >= conf->raid_disks) {
421 dev = 0;
422 sector += (conf->chunk_mask + 1);
425 BUG_ON(slot != conf->copies);
428 static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
430 sector_t offset, chunk, vchunk;
432 while (sector > conf->stride) {
433 sector -= conf->stride;
434 if (dev < conf->near_copies)
435 dev += conf->raid_disks - conf->near_copies;
436 else
437 dev -= conf->near_copies;
440 offset = sector & conf->chunk_mask;
441 chunk = sector >> conf->chunk_shift;
442 vchunk = chunk * conf->raid_disks + dev;
443 sector_div(vchunk, conf->near_copies);
444 return (vchunk << conf->chunk_shift) + offset;
448 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
449 * @q: request queue
450 * @bio: the buffer head that's been built up so far
451 * @biovec: the request that could be merged to it.
453 * Return amount of bytes we can accept at this offset
454 * If near_copies == raid_disk, there are no striping issues,
455 * but in that case, the function isn't called at all.
457 static int raid10_mergeable_bvec(request_queue_t *q, struct bio *bio,
458 struct bio_vec *bio_vec)
460 mddev_t *mddev = q->queuedata;
461 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
462 int max;
463 unsigned int chunk_sectors = mddev->chunk_size >> 9;
464 unsigned int bio_sectors = bio->bi_size >> 9;
466 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
467 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
468 if (max <= bio_vec->bv_len && bio_sectors == 0)
469 return bio_vec->bv_len;
470 else
471 return max;
475 * This routine returns the disk from which the requested read should
476 * be done. There is a per-array 'next expected sequential IO' sector
477 * number - if this matches on the next IO then we use the last disk.
478 * There is also a per-disk 'last know head position' sector that is
479 * maintained from IRQ contexts, both the normal and the resync IO
480 * completion handlers update this position correctly. If there is no
481 * perfect sequential match then we pick the disk whose head is closest.
483 * If there are 2 mirrors in the same 2 devices, performance degrades
484 * because position is mirror, not device based.
486 * The rdev for the device selected will have nr_pending incremented.
490 * FIXME: possibly should rethink readbalancing and do it differently
491 * depending on near_copies / far_copies geometry.
493 static int read_balance(conf_t *conf, r10bio_t *r10_bio)
495 const unsigned long this_sector = r10_bio->sector;
496 int disk, slot, nslot;
497 const int sectors = r10_bio->sectors;
498 sector_t new_distance, current_distance;
500 raid10_find_phys(conf, r10_bio);
501 spin_lock_irq(&conf->device_lock);
503 * Check if we can balance. We can balance on the whole
504 * device if no resync is going on, or below the resync window.
505 * We take the first readable disk when above the resync window.
507 if (conf->mddev->recovery_cp < MaxSector
508 && (this_sector + sectors >= conf->next_resync)) {
509 /* make sure that disk is operational */
510 slot = 0;
511 disk = r10_bio->devs[slot].devnum;
513 while (!conf->mirrors[disk].rdev ||
514 !conf->mirrors[disk].rdev->in_sync) {
515 slot++;
516 if (slot == conf->copies) {
517 slot = 0;
518 disk = -1;
519 break;
521 disk = r10_bio->devs[slot].devnum;
523 goto rb_out;
527 /* make sure the disk is operational */
528 slot = 0;
529 disk = r10_bio->devs[slot].devnum;
530 while (!conf->mirrors[disk].rdev ||
531 !conf->mirrors[disk].rdev->in_sync) {
532 slot ++;
533 if (slot == conf->copies) {
534 disk = -1;
535 goto rb_out;
537 disk = r10_bio->devs[slot].devnum;
541 current_distance = abs(this_sector - conf->mirrors[disk].head_position);
543 /* Find the disk whose head is closest */
545 for (nslot = slot; nslot < conf->copies; nslot++) {
546 int ndisk = r10_bio->devs[nslot].devnum;
549 if (!conf->mirrors[ndisk].rdev ||
550 !conf->mirrors[ndisk].rdev->in_sync)
551 continue;
553 if (!atomic_read(&conf->mirrors[ndisk].rdev->nr_pending)) {
554 disk = ndisk;
555 slot = nslot;
556 break;
558 new_distance = abs(r10_bio->devs[nslot].addr -
559 conf->mirrors[ndisk].head_position);
560 if (new_distance < current_distance) {
561 current_distance = new_distance;
562 disk = ndisk;
563 slot = nslot;
567 rb_out:
568 r10_bio->read_slot = slot;
569 /* conf->next_seq_sect = this_sector + sectors;*/
571 if (disk >= 0 && conf->mirrors[disk].rdev)
572 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
573 spin_unlock_irq(&conf->device_lock);
575 return disk;
578 static void unplug_slaves(mddev_t *mddev)
580 conf_t *conf = mddev_to_conf(mddev);
581 int i;
582 unsigned long flags;
584 spin_lock_irqsave(&conf->device_lock, flags);
585 for (i=0; i<mddev->raid_disks; i++) {
586 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
587 if (rdev && atomic_read(&rdev->nr_pending)) {
588 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
590 atomic_inc(&rdev->nr_pending);
591 spin_unlock_irqrestore(&conf->device_lock, flags);
593 if (r_queue->unplug_fn)
594 r_queue->unplug_fn(r_queue);
596 spin_lock_irqsave(&conf->device_lock, flags);
597 atomic_dec(&rdev->nr_pending);
600 spin_unlock_irqrestore(&conf->device_lock, flags);
602 static void raid10_unplug(request_queue_t *q)
604 unplug_slaves(q->queuedata);
607 static int raid10_issue_flush(request_queue_t *q, struct gendisk *disk,
608 sector_t *error_sector)
610 mddev_t *mddev = q->queuedata;
611 conf_t *conf = mddev_to_conf(mddev);
612 unsigned long flags;
613 int i, ret = 0;
615 spin_lock_irqsave(&conf->device_lock, flags);
616 for (i=0; i<mddev->raid_disks; i++) {
617 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
618 if (rdev && !rdev->faulty) {
619 struct block_device *bdev = rdev->bdev;
620 request_queue_t *r_queue = bdev_get_queue(bdev);
622 if (r_queue->issue_flush_fn) {
623 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk, error_sector);
624 if (ret)
625 break;
629 spin_unlock_irqrestore(&conf->device_lock, flags);
630 return ret;
634 * Throttle resync depth, so that we can both get proper overlapping of
635 * requests, but are still able to handle normal requests quickly.
637 #define RESYNC_DEPTH 32
639 static void device_barrier(conf_t *conf, sector_t sect)
641 spin_lock_irq(&conf->resync_lock);
642 wait_event_lock_irq(conf->wait_idle, !waitqueue_active(&conf->wait_resume),
643 conf->resync_lock, unplug_slaves(conf->mddev));
645 if (!conf->barrier++) {
646 wait_event_lock_irq(conf->wait_idle, !conf->nr_pending,
647 conf->resync_lock, unplug_slaves(conf->mddev));
648 if (conf->nr_pending)
649 BUG();
651 wait_event_lock_irq(conf->wait_resume, conf->barrier < RESYNC_DEPTH,
652 conf->resync_lock, unplug_slaves(conf->mddev));
653 conf->next_resync = sect;
654 spin_unlock_irq(&conf->resync_lock);
657 static int make_request(request_queue_t *q, struct bio * bio)
659 mddev_t *mddev = q->queuedata;
660 conf_t *conf = mddev_to_conf(mddev);
661 mirror_info_t *mirror;
662 r10bio_t *r10_bio;
663 struct bio *read_bio;
664 int i;
665 int chunk_sects = conf->chunk_mask + 1;
667 /* If this request crosses a chunk boundary, we need to
668 * split it. This will only happen for 1 PAGE (or less) requests.
670 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
671 > chunk_sects &&
672 conf->near_copies < conf->raid_disks)) {
673 struct bio_pair *bp;
674 /* Sanity check -- queue functions should prevent this happening */
675 if (bio->bi_vcnt != 1 ||
676 bio->bi_idx != 0)
677 goto bad_map;
678 /* This is a one page bio that upper layers
679 * refuse to split for us, so we need to split it.
681 bp = bio_split(bio, bio_split_pool,
682 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
683 if (make_request(q, &bp->bio1))
684 generic_make_request(&bp->bio1);
685 if (make_request(q, &bp->bio2))
686 generic_make_request(&bp->bio2);
688 bio_pair_release(bp);
689 return 0;
690 bad_map:
691 printk("raid10_make_request bug: can't convert block across chunks"
692 " or bigger than %dk %llu %d\n", chunk_sects/2,
693 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
695 bio_io_error(bio, bio->bi_size);
696 return 0;
700 * Register the new request and wait if the reconstruction
701 * thread has put up a bar for new requests.
702 * Continue immediately if no resync is active currently.
704 spin_lock_irq(&conf->resync_lock);
705 wait_event_lock_irq(conf->wait_resume, !conf->barrier, conf->resync_lock, );
706 conf->nr_pending++;
707 spin_unlock_irq(&conf->resync_lock);
709 if (bio_data_dir(bio)==WRITE) {
710 disk_stat_inc(mddev->gendisk, writes);
711 disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bio));
712 } else {
713 disk_stat_inc(mddev->gendisk, reads);
714 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bio));
717 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
719 r10_bio->master_bio = bio;
720 r10_bio->sectors = bio->bi_size >> 9;
722 r10_bio->mddev = mddev;
723 r10_bio->sector = bio->bi_sector;
725 if (bio_data_dir(bio) == READ) {
727 * read balancing logic:
729 int disk = read_balance(conf, r10_bio);
730 int slot = r10_bio->read_slot;
731 if (disk < 0) {
732 raid_end_bio_io(r10_bio);
733 return 0;
735 mirror = conf->mirrors + disk;
737 read_bio = bio_clone(bio, GFP_NOIO);
739 r10_bio->devs[slot].bio = read_bio;
741 read_bio->bi_sector = r10_bio->devs[slot].addr +
742 mirror->rdev->data_offset;
743 read_bio->bi_bdev = mirror->rdev->bdev;
744 read_bio->bi_end_io = raid10_end_read_request;
745 read_bio->bi_rw = READ;
746 read_bio->bi_private = r10_bio;
748 generic_make_request(read_bio);
749 return 0;
753 * WRITE:
755 /* first select target devices under spinlock and
756 * inc refcount on their rdev. Record them by setting
757 * bios[x] to bio
759 raid10_find_phys(conf, r10_bio);
760 spin_lock_irq(&conf->device_lock);
761 for (i = 0; i < conf->copies; i++) {
762 int d = r10_bio->devs[i].devnum;
763 if (conf->mirrors[d].rdev &&
764 !conf->mirrors[d].rdev->faulty) {
765 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
766 r10_bio->devs[i].bio = bio;
767 } else
768 r10_bio->devs[i].bio = NULL;
770 spin_unlock_irq(&conf->device_lock);
772 atomic_set(&r10_bio->remaining, 1);
773 md_write_start(mddev);
774 for (i = 0; i < conf->copies; i++) {
775 struct bio *mbio;
776 int d = r10_bio->devs[i].devnum;
777 if (!r10_bio->devs[i].bio)
778 continue;
780 mbio = bio_clone(bio, GFP_NOIO);
781 r10_bio->devs[i].bio = mbio;
783 mbio->bi_sector = r10_bio->devs[i].addr+
784 conf->mirrors[d].rdev->data_offset;
785 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
786 mbio->bi_end_io = raid10_end_write_request;
787 mbio->bi_rw = WRITE;
788 mbio->bi_private = r10_bio;
790 atomic_inc(&r10_bio->remaining);
791 generic_make_request(mbio);
794 if (atomic_dec_and_test(&r10_bio->remaining)) {
795 md_write_end(mddev);
796 raid_end_bio_io(r10_bio);
799 return 0;
802 static void status(struct seq_file *seq, mddev_t *mddev)
804 conf_t *conf = mddev_to_conf(mddev);
805 int i;
807 if (conf->near_copies < conf->raid_disks)
808 seq_printf(seq, " %dK chunks", mddev->chunk_size/1024);
809 if (conf->near_copies > 1)
810 seq_printf(seq, " %d near-copies", conf->near_copies);
811 if (conf->far_copies > 1)
812 seq_printf(seq, " %d far-copies", conf->far_copies);
814 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
815 conf->working_disks);
816 for (i = 0; i < conf->raid_disks; i++)
817 seq_printf(seq, "%s",
818 conf->mirrors[i].rdev &&
819 conf->mirrors[i].rdev->in_sync ? "U" : "_");
820 seq_printf(seq, "]");
823 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
825 char b[BDEVNAME_SIZE];
826 conf_t *conf = mddev_to_conf(mddev);
829 * If it is not operational, then we have already marked it as dead
830 * else if it is the last working disks, ignore the error, let the
831 * next level up know.
832 * else mark the drive as failed
834 if (rdev->in_sync
835 && conf->working_disks == 1)
837 * Don't fail the drive, just return an IO error.
838 * The test should really be more sophisticated than
839 * "working_disks == 1", but it isn't critical, and
840 * can wait until we do more sophisticated "is the drive
841 * really dead" tests...
843 return;
844 if (rdev->in_sync) {
845 mddev->degraded++;
846 conf->working_disks--;
848 * if recovery is running, make sure it aborts.
850 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
852 rdev->in_sync = 0;
853 rdev->faulty = 1;
854 mddev->sb_dirty = 1;
855 printk(KERN_ALERT "raid10: Disk failure on %s, disabling device. \n"
856 " Operation continuing on %d devices\n",
857 bdevname(rdev->bdev,b), conf->working_disks);
860 static void print_conf(conf_t *conf)
862 int i;
863 mirror_info_t *tmp;
865 printk("RAID10 conf printout:\n");
866 if (!conf) {
867 printk("(!conf)\n");
868 return;
870 printk(" --- wd:%d rd:%d\n", conf->working_disks,
871 conf->raid_disks);
873 for (i = 0; i < conf->raid_disks; i++) {
874 char b[BDEVNAME_SIZE];
875 tmp = conf->mirrors + i;
876 if (tmp->rdev)
877 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
878 i, !tmp->rdev->in_sync, !tmp->rdev->faulty,
879 bdevname(tmp->rdev->bdev,b));
883 static void close_sync(conf_t *conf)
885 spin_lock_irq(&conf->resync_lock);
886 wait_event_lock_irq(conf->wait_resume, !conf->barrier,
887 conf->resync_lock, unplug_slaves(conf->mddev));
888 spin_unlock_irq(&conf->resync_lock);
890 if (conf->barrier) BUG();
891 if (waitqueue_active(&conf->wait_idle)) BUG();
893 mempool_destroy(conf->r10buf_pool);
894 conf->r10buf_pool = NULL;
897 static int raid10_spare_active(mddev_t *mddev)
899 int i;
900 conf_t *conf = mddev->private;
901 mirror_info_t *tmp;
903 spin_lock_irq(&conf->device_lock);
905 * Find all non-in_sync disks within the RAID10 configuration
906 * and mark them in_sync
908 for (i = 0; i < conf->raid_disks; i++) {
909 tmp = conf->mirrors + i;
910 if (tmp->rdev
911 && !tmp->rdev->faulty
912 && !tmp->rdev->in_sync) {
913 conf->working_disks++;
914 mddev->degraded--;
915 tmp->rdev->in_sync = 1;
918 spin_unlock_irq(&conf->device_lock);
920 print_conf(conf);
921 return 0;
925 static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
927 conf_t *conf = mddev->private;
928 int found = 0;
929 int mirror;
930 mirror_info_t *p;
932 if (mddev->recovery_cp < MaxSector)
933 /* only hot-add to in-sync arrays, as recovery is
934 * very different from resync
936 return 0;
937 spin_lock_irq(&conf->device_lock);
938 for (mirror=0; mirror < mddev->raid_disks; mirror++)
939 if ( !(p=conf->mirrors+mirror)->rdev) {
940 p->rdev = rdev;
942 blk_queue_stack_limits(mddev->queue,
943 rdev->bdev->bd_disk->queue);
944 /* as we don't honour merge_bvec_fn, we must never risk
945 * violating it, so limit ->max_sector to one PAGE, as
946 * a one page request is never in violation.
948 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
949 mddev->queue->max_sectors > (PAGE_SIZE>>9))
950 mddev->queue->max_sectors = (PAGE_SIZE>>9);
952 p->head_position = 0;
953 rdev->raid_disk = mirror;
954 found = 1;
955 break;
957 spin_unlock_irq(&conf->device_lock);
959 print_conf(conf);
960 return found;
963 static int raid10_remove_disk(mddev_t *mddev, int number)
965 conf_t *conf = mddev->private;
966 int err = 1;
967 mirror_info_t *p = conf->mirrors+ number;
969 print_conf(conf);
970 spin_lock_irq(&conf->device_lock);
971 if (p->rdev) {
972 if (p->rdev->in_sync ||
973 atomic_read(&p->rdev->nr_pending)) {
974 err = -EBUSY;
975 goto abort;
977 p->rdev = NULL;
978 err = 0;
980 if (err)
981 MD_BUG();
982 abort:
983 spin_unlock_irq(&conf->device_lock);
985 print_conf(conf);
986 return err;
990 static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
992 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
993 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
994 conf_t *conf = mddev_to_conf(r10_bio->mddev);
995 int i,d;
997 if (bio->bi_size)
998 return 1;
1000 for (i=0; i<conf->copies; i++)
1001 if (r10_bio->devs[i].bio == bio)
1002 break;
1003 if (i == conf->copies)
1004 BUG();
1005 update_head_pos(i, r10_bio);
1006 d = r10_bio->devs[i].devnum;
1007 if (!uptodate)
1008 md_error(r10_bio->mddev,
1009 conf->mirrors[d].rdev);
1011 /* for reconstruct, we always reschedule after a read.
1012 * for resync, only after all reads
1014 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1015 atomic_dec_and_test(&r10_bio->remaining)) {
1016 /* we have read all the blocks,
1017 * do the comparison in process context in raid10d
1019 reschedule_retry(r10_bio);
1021 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1022 return 0;
1025 static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
1027 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1028 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1029 mddev_t *mddev = r10_bio->mddev;
1030 conf_t *conf = mddev_to_conf(mddev);
1031 int i,d;
1033 if (bio->bi_size)
1034 return 1;
1036 for (i = 0; i < conf->copies; i++)
1037 if (r10_bio->devs[i].bio == bio)
1038 break;
1039 d = r10_bio->devs[i].devnum;
1041 if (!uptodate)
1042 md_error(mddev, conf->mirrors[d].rdev);
1043 update_head_pos(i, r10_bio);
1045 while (atomic_dec_and_test(&r10_bio->remaining)) {
1046 if (r10_bio->master_bio == NULL) {
1047 /* the primary of several recovery bios */
1048 md_done_sync(mddev, r10_bio->sectors, 1);
1049 put_buf(r10_bio);
1050 break;
1051 } else {
1052 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1053 put_buf(r10_bio);
1054 r10_bio = r10_bio2;
1057 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1058 return 0;
1062 * Note: sync and recover and handled very differently for raid10
1063 * This code is for resync.
1064 * For resync, we read through virtual addresses and read all blocks.
1065 * If there is any error, we schedule a write. The lowest numbered
1066 * drive is authoritative.
1067 * However requests come for physical address, so we need to map.
1068 * For every physical address there are raid_disks/copies virtual addresses,
1069 * which is always are least one, but is not necessarly an integer.
1070 * This means that a physical address can span multiple chunks, so we may
1071 * have to submit multiple io requests for a single sync request.
1074 * We check if all blocks are in-sync and only write to blocks that
1075 * aren't in sync
1077 static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1079 conf_t *conf = mddev_to_conf(mddev);
1080 int i, first;
1081 struct bio *tbio, *fbio;
1083 atomic_set(&r10_bio->remaining, 1);
1085 /* find the first device with a block */
1086 for (i=0; i<conf->copies; i++)
1087 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1088 break;
1090 if (i == conf->copies)
1091 goto done;
1093 first = i;
1094 fbio = r10_bio->devs[i].bio;
1096 /* now find blocks with errors */
1097 for (i=first+1 ; i < conf->copies ; i++) {
1098 int vcnt, j, d;
1100 if (!test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1101 continue;
1102 /* We know that the bi_io_vec layout is the same for
1103 * both 'first' and 'i', so we just compare them.
1104 * All vec entries are PAGE_SIZE;
1106 tbio = r10_bio->devs[i].bio;
1107 vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
1108 for (j = 0; j < vcnt; j++)
1109 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1110 page_address(tbio->bi_io_vec[j].bv_page),
1111 PAGE_SIZE))
1112 break;
1113 if (j == vcnt)
1114 continue;
1115 /* Ok, we need to write this bio
1116 * First we need to fixup bv_offset, bv_len and
1117 * bi_vecs, as the read request might have corrupted these
1119 tbio->bi_vcnt = vcnt;
1120 tbio->bi_size = r10_bio->sectors << 9;
1121 tbio->bi_idx = 0;
1122 tbio->bi_phys_segments = 0;
1123 tbio->bi_hw_segments = 0;
1124 tbio->bi_hw_front_size = 0;
1125 tbio->bi_hw_back_size = 0;
1126 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1127 tbio->bi_flags |= 1 << BIO_UPTODATE;
1128 tbio->bi_next = NULL;
1129 tbio->bi_rw = WRITE;
1130 tbio->bi_private = r10_bio;
1131 tbio->bi_sector = r10_bio->devs[i].addr;
1133 for (j=0; j < vcnt ; j++) {
1134 tbio->bi_io_vec[j].bv_offset = 0;
1135 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1137 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1138 page_address(fbio->bi_io_vec[j].bv_page),
1139 PAGE_SIZE);
1141 tbio->bi_end_io = end_sync_write;
1143 d = r10_bio->devs[i].devnum;
1144 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1145 atomic_inc(&r10_bio->remaining);
1146 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1148 generic_make_request(tbio);
1151 done:
1152 if (atomic_dec_and_test(&r10_bio->remaining)) {
1153 md_done_sync(mddev, r10_bio->sectors, 1);
1154 put_buf(r10_bio);
1159 * Now for the recovery code.
1160 * Recovery happens across physical sectors.
1161 * We recover all non-is_sync drives by finding the virtual address of
1162 * each, and then choose a working drive that also has that virt address.
1163 * There is a separate r10_bio for each non-in_sync drive.
1164 * Only the first two slots are in use. The first for reading,
1165 * The second for writing.
1169 static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1171 conf_t *conf = mddev_to_conf(mddev);
1172 int i, d;
1173 struct bio *bio, *wbio;
1176 /* move the pages across to the second bio
1177 * and submit the write request
1179 bio = r10_bio->devs[0].bio;
1180 wbio = r10_bio->devs[1].bio;
1181 for (i=0; i < wbio->bi_vcnt; i++) {
1182 struct page *p = bio->bi_io_vec[i].bv_page;
1183 bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
1184 wbio->bi_io_vec[i].bv_page = p;
1186 d = r10_bio->devs[1].devnum;
1188 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1189 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
1190 generic_make_request(wbio);
1195 * This is a kernel thread which:
1197 * 1. Retries failed read operations on working mirrors.
1198 * 2. Updates the raid superblock when problems encounter.
1199 * 3. Performs writes following reads for array syncronising.
1202 static void raid10d(mddev_t *mddev)
1204 r10bio_t *r10_bio;
1205 struct bio *bio;
1206 unsigned long flags;
1207 conf_t *conf = mddev_to_conf(mddev);
1208 struct list_head *head = &conf->retry_list;
1209 int unplug=0;
1210 mdk_rdev_t *rdev;
1212 md_check_recovery(mddev);
1213 md_handle_safemode(mddev);
1215 for (;;) {
1216 char b[BDEVNAME_SIZE];
1217 spin_lock_irqsave(&conf->device_lock, flags);
1218 if (list_empty(head))
1219 break;
1220 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1221 list_del(head->prev);
1222 spin_unlock_irqrestore(&conf->device_lock, flags);
1224 mddev = r10_bio->mddev;
1225 conf = mddev_to_conf(mddev);
1226 if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
1227 sync_request_write(mddev, r10_bio);
1228 unplug = 1;
1229 } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1230 recovery_request_write(mddev, r10_bio);
1231 unplug = 1;
1232 } else {
1233 int mirror;
1234 bio = r10_bio->devs[r10_bio->read_slot].bio;
1235 r10_bio->devs[r10_bio->read_slot].bio = NULL;
1236 mirror = read_balance(conf, r10_bio);
1237 r10_bio->devs[r10_bio->read_slot].bio = bio;
1238 if (mirror == -1) {
1239 printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
1240 " read error for block %llu\n",
1241 bdevname(bio->bi_bdev,b),
1242 (unsigned long long)r10_bio->sector);
1243 raid_end_bio_io(r10_bio);
1244 } else {
1245 rdev = conf->mirrors[mirror].rdev;
1246 if (printk_ratelimit())
1247 printk(KERN_ERR "raid10: %s: redirecting sector %llu to"
1248 " another mirror\n",
1249 bdevname(rdev->bdev,b),
1250 (unsigned long long)r10_bio->sector);
1251 bio->bi_bdev = rdev->bdev;
1252 bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
1253 + rdev->data_offset;
1254 bio->bi_next = NULL;
1255 bio->bi_flags &= (1<<BIO_CLONED);
1256 bio->bi_flags |= 1 << BIO_UPTODATE;
1257 bio->bi_idx = 0;
1258 bio->bi_size = r10_bio->sectors << 9;
1259 bio->bi_rw = READ;
1260 unplug = 1;
1261 generic_make_request(bio);
1265 spin_unlock_irqrestore(&conf->device_lock, flags);
1266 if (unplug)
1267 unplug_slaves(mddev);
1271 static int init_resync(conf_t *conf)
1273 int buffs;
1275 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
1276 if (conf->r10buf_pool)
1277 BUG();
1278 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1279 if (!conf->r10buf_pool)
1280 return -ENOMEM;
1281 conf->next_resync = 0;
1282 return 0;
1286 * perform a "sync" on one "block"
1288 * We need to make sure that no normal I/O request - particularly write
1289 * requests - conflict with active sync requests.
1291 * This is achieved by tracking pending requests and a 'barrier' concept
1292 * that can be installed to exclude normal IO requests.
1294 * Resync and recovery are handled very differently.
1295 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1297 * For resync, we iterate over virtual addresses, read all copies,
1298 * and update if there are differences. If only one copy is live,
1299 * skip it.
1300 * For recovery, we iterate over physical addresses, read a good
1301 * value for each non-in_sync drive, and over-write.
1303 * So, for recovery we may have several outstanding complex requests for a
1304 * given address, one for each out-of-sync device. We model this by allocating
1305 * a number of r10_bio structures, one for each out-of-sync device.
1306 * As we setup these structures, we collect all bio's together into a list
1307 * which we then process collectively to add pages, and then process again
1308 * to pass to generic_make_request.
1310 * The r10_bio structures are linked using a borrowed master_bio pointer.
1311 * This link is counted in ->remaining. When the r10_bio that points to NULL
1312 * has its remaining count decremented to 0, the whole complex operation
1313 * is complete.
1317 static int sync_request(mddev_t *mddev, sector_t sector_nr, int go_faster)
1319 conf_t *conf = mddev_to_conf(mddev);
1320 r10bio_t *r10_bio;
1321 struct bio *biolist = NULL, *bio;
1322 sector_t max_sector, nr_sectors;
1323 int disk;
1324 int i;
1326 sector_t sectors_skipped = 0;
1327 int chunks_skipped = 0;
1329 if (!conf->r10buf_pool)
1330 if (init_resync(conf))
1331 return -ENOMEM;
1333 skipped:
1334 max_sector = mddev->size << 1;
1335 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1336 max_sector = mddev->resync_max_sectors;
1337 if (sector_nr >= max_sector) {
1338 close_sync(conf);
1339 return sectors_skipped;
1341 if (chunks_skipped >= conf->raid_disks) {
1342 /* if there has been nothing to do on any drive,
1343 * then there is nothing to do at all..
1345 sector_t sec = max_sector - sector_nr;
1346 md_done_sync(mddev, sec, 1);
1347 return sec + sectors_skipped;
1350 /* make sure whole request will fit in a chunk - if chunks
1351 * are meaningful
1353 if (conf->near_copies < conf->raid_disks &&
1354 max_sector > (sector_nr | conf->chunk_mask))
1355 max_sector = (sector_nr | conf->chunk_mask) + 1;
1357 * If there is non-resync activity waiting for us then
1358 * put in a delay to throttle resync.
1360 if (!go_faster && waitqueue_active(&conf->wait_resume))
1361 schedule_timeout(HZ);
1362 device_barrier(conf, sector_nr + RESYNC_SECTORS);
1364 /* Again, very different code for resync and recovery.
1365 * Both must result in an r10bio with a list of bios that
1366 * have bi_end_io, bi_sector, bi_bdev set,
1367 * and bi_private set to the r10bio.
1368 * For recovery, we may actually create several r10bios
1369 * with 2 bios in each, that correspond to the bios in the main one.
1370 * In this case, the subordinate r10bios link back through a
1371 * borrowed master_bio pointer, and the counter in the master
1372 * includes a ref from each subordinate.
1374 /* First, we decide what to do and set ->bi_end_io
1375 * To end_sync_read if we want to read, and
1376 * end_sync_write if we will want to write.
1379 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1380 /* recovery... the complicated one */
1381 int i, j, k;
1382 r10_bio = NULL;
1384 for (i=0 ; i<conf->raid_disks; i++)
1385 if (conf->mirrors[i].rdev &&
1386 !conf->mirrors[i].rdev->in_sync) {
1387 /* want to reconstruct this device */
1388 r10bio_t *rb2 = r10_bio;
1390 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1391 spin_lock_irq(&conf->resync_lock);
1392 conf->nr_pending++;
1393 if (rb2) conf->barrier++;
1394 spin_unlock_irq(&conf->resync_lock);
1395 atomic_set(&r10_bio->remaining, 0);
1397 r10_bio->master_bio = (struct bio*)rb2;
1398 if (rb2)
1399 atomic_inc(&rb2->remaining);
1400 r10_bio->mddev = mddev;
1401 set_bit(R10BIO_IsRecover, &r10_bio->state);
1402 r10_bio->sector = raid10_find_virt(conf, sector_nr, i);
1403 raid10_find_phys(conf, r10_bio);
1404 for (j=0; j<conf->copies;j++) {
1405 int d = r10_bio->devs[j].devnum;
1406 if (conf->mirrors[d].rdev &&
1407 conf->mirrors[d].rdev->in_sync) {
1408 /* This is where we read from */
1409 bio = r10_bio->devs[0].bio;
1410 bio->bi_next = biolist;
1411 biolist = bio;
1412 bio->bi_private = r10_bio;
1413 bio->bi_end_io = end_sync_read;
1414 bio->bi_rw = 0;
1415 bio->bi_sector = r10_bio->devs[j].addr +
1416 conf->mirrors[d].rdev->data_offset;
1417 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1418 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1419 atomic_inc(&r10_bio->remaining);
1420 /* and we write to 'i' */
1422 for (k=0; k<conf->copies; k++)
1423 if (r10_bio->devs[k].devnum == i)
1424 break;
1425 bio = r10_bio->devs[1].bio;
1426 bio->bi_next = biolist;
1427 biolist = bio;
1428 bio->bi_private = r10_bio;
1429 bio->bi_end_io = end_sync_write;
1430 bio->bi_rw = 1;
1431 bio->bi_sector = r10_bio->devs[k].addr +
1432 conf->mirrors[i].rdev->data_offset;
1433 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1435 r10_bio->devs[0].devnum = d;
1436 r10_bio->devs[1].devnum = i;
1438 break;
1441 if (j == conf->copies) {
1442 BUG();
1445 if (biolist == NULL) {
1446 while (r10_bio) {
1447 r10bio_t *rb2 = r10_bio;
1448 r10_bio = (r10bio_t*) rb2->master_bio;
1449 rb2->master_bio = NULL;
1450 put_buf(rb2);
1452 goto giveup;
1454 } else {
1455 /* resync. Schedule a read for every block at this virt offset */
1456 int count = 0;
1457 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1459 spin_lock_irq(&conf->resync_lock);
1460 conf->nr_pending++;
1461 spin_unlock_irq(&conf->resync_lock);
1463 r10_bio->mddev = mddev;
1464 atomic_set(&r10_bio->remaining, 0);
1466 r10_bio->master_bio = NULL;
1467 r10_bio->sector = sector_nr;
1468 set_bit(R10BIO_IsSync, &r10_bio->state);
1469 raid10_find_phys(conf, r10_bio);
1470 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
1471 spin_lock_irq(&conf->device_lock);
1472 for (i=0; i<conf->copies; i++) {
1473 int d = r10_bio->devs[i].devnum;
1474 bio = r10_bio->devs[i].bio;
1475 bio->bi_end_io = NULL;
1476 if (conf->mirrors[d].rdev == NULL ||
1477 conf->mirrors[d].rdev->faulty)
1478 continue;
1479 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1480 atomic_inc(&r10_bio->remaining);
1481 bio->bi_next = biolist;
1482 biolist = bio;
1483 bio->bi_private = r10_bio;
1484 bio->bi_end_io = end_sync_read;
1485 bio->bi_rw = 0;
1486 bio->bi_sector = r10_bio->devs[i].addr +
1487 conf->mirrors[d].rdev->data_offset;
1488 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1489 count++;
1491 spin_unlock_irq(&conf->device_lock);
1492 if (count < 2) {
1493 for (i=0; i<conf->copies; i++) {
1494 int d = r10_bio->devs[i].devnum;
1495 if (r10_bio->devs[i].bio->bi_end_io)
1496 atomic_dec(&conf->mirrors[d].rdev->nr_pending);
1498 put_buf(r10_bio);
1499 goto giveup;
1503 for (bio = biolist; bio ; bio=bio->bi_next) {
1505 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
1506 if (bio->bi_end_io)
1507 bio->bi_flags |= 1 << BIO_UPTODATE;
1508 bio->bi_vcnt = 0;
1509 bio->bi_idx = 0;
1510 bio->bi_phys_segments = 0;
1511 bio->bi_hw_segments = 0;
1512 bio->bi_size = 0;
1515 nr_sectors = 0;
1516 do {
1517 struct page *page;
1518 int len = PAGE_SIZE;
1519 disk = 0;
1520 if (sector_nr + (len>>9) > max_sector)
1521 len = (max_sector - sector_nr) << 9;
1522 if (len == 0)
1523 break;
1524 for (bio= biolist ; bio ; bio=bio->bi_next) {
1525 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
1526 if (bio_add_page(bio, page, len, 0) == 0) {
1527 /* stop here */
1528 struct bio *bio2;
1529 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
1530 for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
1531 /* remove last page from this bio */
1532 bio2->bi_vcnt--;
1533 bio2->bi_size -= len;
1534 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
1536 goto bio_full;
1538 disk = i;
1540 nr_sectors += len>>9;
1541 sector_nr += len>>9;
1542 } while (biolist->bi_vcnt < RESYNC_PAGES);
1543 bio_full:
1544 r10_bio->sectors = nr_sectors;
1546 while (biolist) {
1547 bio = biolist;
1548 biolist = biolist->bi_next;
1550 bio->bi_next = NULL;
1551 r10_bio = bio->bi_private;
1552 r10_bio->sectors = nr_sectors;
1554 if (bio->bi_end_io == end_sync_read) {
1555 md_sync_acct(bio->bi_bdev, nr_sectors);
1556 generic_make_request(bio);
1560 return nr_sectors;
1561 giveup:
1562 /* There is nowhere to write, so all non-sync
1563 * drives must be failed, so try the next chunk...
1566 int sec = max_sector - sector_nr;
1567 sectors_skipped += sec;
1568 chunks_skipped ++;
1569 sector_nr = max_sector;
1570 md_done_sync(mddev, sec, 1);
1571 goto skipped;
1575 static int run(mddev_t *mddev)
1577 conf_t *conf;
1578 int i, disk_idx;
1579 mirror_info_t *disk;
1580 mdk_rdev_t *rdev;
1581 struct list_head *tmp;
1582 int nc, fc;
1583 sector_t stride, size;
1585 if (mddev->level != 10) {
1586 printk(KERN_ERR "raid10: %s: raid level not set correctly... (%d)\n",
1587 mdname(mddev), mddev->level);
1588 goto out;
1590 nc = mddev->layout & 255;
1591 fc = (mddev->layout >> 8) & 255;
1592 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
1593 (mddev->layout >> 16)) {
1594 printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n",
1595 mdname(mddev), mddev->layout);
1596 goto out;
1599 * copy the already verified devices into our private RAID10
1600 * bookkeeping area. [whatever we allocate in run(),
1601 * should be freed in stop()]
1603 conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
1604 mddev->private = conf;
1605 if (!conf) {
1606 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1607 mdname(mddev));
1608 goto out;
1610 memset(conf, 0, sizeof(*conf));
1611 conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks,
1612 GFP_KERNEL);
1613 if (!conf->mirrors) {
1614 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1615 mdname(mddev));
1616 goto out_free_conf;
1618 memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
1620 conf->near_copies = nc;
1621 conf->far_copies = fc;
1622 conf->copies = nc*fc;
1623 conf->chunk_mask = (sector_t)(mddev->chunk_size>>9)-1;
1624 conf->chunk_shift = ffz(~mddev->chunk_size) - 9;
1625 stride = mddev->size >> (conf->chunk_shift-1);
1626 sector_div(stride, fc);
1627 conf->stride = stride << conf->chunk_shift;
1629 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
1630 r10bio_pool_free, conf);
1631 if (!conf->r10bio_pool) {
1632 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1633 mdname(mddev));
1634 goto out_free_conf;
1636 mddev->queue->unplug_fn = raid10_unplug;
1638 mddev->queue->issue_flush_fn = raid10_issue_flush;
1640 ITERATE_RDEV(mddev, rdev, tmp) {
1641 disk_idx = rdev->raid_disk;
1642 if (disk_idx >= mddev->raid_disks
1643 || disk_idx < 0)
1644 continue;
1645 disk = conf->mirrors + disk_idx;
1647 disk->rdev = rdev;
1649 blk_queue_stack_limits(mddev->queue,
1650 rdev->bdev->bd_disk->queue);
1651 /* as we don't honour merge_bvec_fn, we must never risk
1652 * violating it, so limit ->max_sector to one PAGE, as
1653 * a one page request is never in violation.
1655 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1656 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1657 mddev->queue->max_sectors = (PAGE_SIZE>>9);
1659 disk->head_position = 0;
1660 if (!rdev->faulty && rdev->in_sync)
1661 conf->working_disks++;
1663 conf->raid_disks = mddev->raid_disks;
1664 conf->mddev = mddev;
1665 conf->device_lock = SPIN_LOCK_UNLOCKED;
1666 INIT_LIST_HEAD(&conf->retry_list);
1668 conf->resync_lock = SPIN_LOCK_UNLOCKED;
1669 init_waitqueue_head(&conf->wait_idle);
1670 init_waitqueue_head(&conf->wait_resume);
1672 if (!conf->working_disks) {
1673 printk(KERN_ERR "raid10: no operational mirrors for %s\n",
1674 mdname(mddev));
1675 goto out_free_conf;
1678 mddev->degraded = 0;
1679 for (i = 0; i < conf->raid_disks; i++) {
1681 disk = conf->mirrors + i;
1683 if (!disk->rdev) {
1684 disk->head_position = 0;
1685 mddev->degraded++;
1690 mddev->thread = md_register_thread(raid10d, mddev, "%s_raid10");
1691 if (!mddev->thread) {
1692 printk(KERN_ERR
1693 "raid10: couldn't allocate thread for %s\n",
1694 mdname(mddev));
1695 goto out_free_conf;
1698 printk(KERN_INFO
1699 "raid10: raid set %s active with %d out of %d devices\n",
1700 mdname(mddev), mddev->raid_disks - mddev->degraded,
1701 mddev->raid_disks);
1703 * Ok, everything is just fine now
1705 size = conf->stride * conf->raid_disks;
1706 sector_div(size, conf->near_copies);
1707 mddev->array_size = size/2;
1708 mddev->resync_max_sectors = size;
1710 /* Calculate max read-ahead size.
1711 * We need to readahead at least twice a whole stripe....
1712 * maybe...
1715 int stripe = conf->raid_disks * mddev->chunk_size / PAGE_CACHE_SIZE;
1716 stripe /= conf->near_copies;
1717 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
1718 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
1721 if (conf->near_copies < mddev->raid_disks)
1722 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
1723 return 0;
1725 out_free_conf:
1726 if (conf->r10bio_pool)
1727 mempool_destroy(conf->r10bio_pool);
1728 if (conf->mirrors)
1729 kfree(conf->mirrors);
1730 kfree(conf);
1731 mddev->private = NULL;
1732 out:
1733 return -EIO;
1736 static int stop(mddev_t *mddev)
1738 conf_t *conf = mddev_to_conf(mddev);
1740 md_unregister_thread(mddev->thread);
1741 mddev->thread = NULL;
1742 if (conf->r10bio_pool)
1743 mempool_destroy(conf->r10bio_pool);
1744 if (conf->mirrors)
1745 kfree(conf->mirrors);
1746 kfree(conf);
1747 mddev->private = NULL;
1748 return 0;
1752 static mdk_personality_t raid10_personality =
1754 .name = "raid10",
1755 .owner = THIS_MODULE,
1756 .make_request = make_request,
1757 .run = run,
1758 .stop = stop,
1759 .status = status,
1760 .error_handler = error,
1761 .hot_add_disk = raid10_add_disk,
1762 .hot_remove_disk= raid10_remove_disk,
1763 .spare_active = raid10_spare_active,
1764 .sync_request = sync_request,
1767 static int __init raid_init(void)
1769 return register_md_personality(RAID10, &raid10_personality);
1772 static void raid_exit(void)
1774 unregister_md_personality(RAID10);
1777 module_init(raid_init);
1778 module_exit(raid_exit);
1779 MODULE_LICENSE("GPL");
1780 MODULE_ALIAS("md-personality-9"); /* RAID10 */