md/raid10: fix deadlock with unaligned read during resync
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / md / raid10.c
blob07de6da4675136b6773d4bbee81fa81b1c24e402
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/slab.h>
22 #include <linux/delay.h>
23 #include <linux/blkdev.h>
24 #include <linux/seq_file.h>
25 #include "md.h"
26 #include "raid10.h"
27 #include "bitmap.h"
30 * RAID10 provides a combination of RAID0 and RAID1 functionality.
31 * The layout of data is defined by
32 * chunk_size
33 * raid_disks
34 * near_copies (stored in low byte of layout)
35 * far_copies (stored in second byte of layout)
36 * far_offset (stored in bit 16 of layout )
38 * The data to be stored is divided into chunks using chunksize.
39 * Each device is divided into far_copies sections.
40 * In each section, chunks are laid out in a style similar to raid0, but
41 * near_copies copies of each chunk is stored (each on a different drive).
42 * The starting device for each section is offset near_copies from the starting
43 * device of the previous section.
44 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
45 * drive.
46 * near_copies and far_copies must be at least one, and their product is at most
47 * raid_disks.
49 * If far_offset is true, then the far_copies are handled a bit differently.
50 * The copies are still in different stripes, but instead of be very far apart
51 * on disk, there are adjacent stripes.
55 * Number of guaranteed r10bios in case of extreme VM load:
57 #define NR_RAID10_BIOS 256
59 static void unplug_slaves(mddev_t *mddev);
61 static void allow_barrier(conf_t *conf);
62 static void lower_barrier(conf_t *conf);
64 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
66 conf_t *conf = data;
67 r10bio_t *r10_bio;
68 int size = offsetof(struct r10bio_s, devs[conf->copies]);
70 /* allocate a r10bio with room for raid_disks entries in the bios array */
71 r10_bio = kzalloc(size, gfp_flags);
72 if (!r10_bio && conf->mddev)
73 unplug_slaves(conf->mddev);
75 return r10_bio;
78 static void r10bio_pool_free(void *r10_bio, void *data)
80 kfree(r10_bio);
83 /* Maximum size of each resync request */
84 #define RESYNC_BLOCK_SIZE (64*1024)
85 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
86 /* amount of memory to reserve for resync requests */
87 #define RESYNC_WINDOW (1024*1024)
88 /* maximum number of concurrent requests, memory permitting */
89 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
92 * When performing a resync, we need to read and compare, so
93 * we need as many pages are there are copies.
94 * When performing a recovery, we need 2 bios, one for read,
95 * one for write (we recover only one drive per r10buf)
98 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
100 conf_t *conf = data;
101 struct page *page;
102 r10bio_t *r10_bio;
103 struct bio *bio;
104 int i, j;
105 int nalloc;
107 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
108 if (!r10_bio) {
109 unplug_slaves(conf->mddev);
110 return NULL;
113 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
114 nalloc = conf->copies; /* resync */
115 else
116 nalloc = 2; /* recovery */
119 * Allocate bios.
121 for (j = nalloc ; j-- ; ) {
122 bio = bio_alloc(gfp_flags, RESYNC_PAGES);
123 if (!bio)
124 goto out_free_bio;
125 r10_bio->devs[j].bio = bio;
128 * Allocate RESYNC_PAGES data pages and attach them
129 * where needed.
131 for (j = 0 ; j < nalloc; j++) {
132 bio = r10_bio->devs[j].bio;
133 for (i = 0; i < RESYNC_PAGES; i++) {
134 page = alloc_page(gfp_flags);
135 if (unlikely(!page))
136 goto out_free_pages;
138 bio->bi_io_vec[i].bv_page = page;
142 return r10_bio;
144 out_free_pages:
145 for ( ; i > 0 ; i--)
146 safe_put_page(bio->bi_io_vec[i-1].bv_page);
147 while (j--)
148 for (i = 0; i < RESYNC_PAGES ; i++)
149 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
150 j = -1;
151 out_free_bio:
152 while ( ++j < nalloc )
153 bio_put(r10_bio->devs[j].bio);
154 r10bio_pool_free(r10_bio, conf);
155 return NULL;
158 static void r10buf_pool_free(void *__r10_bio, void *data)
160 int i;
161 conf_t *conf = data;
162 r10bio_t *r10bio = __r10_bio;
163 int j;
165 for (j=0; j < conf->copies; j++) {
166 struct bio *bio = r10bio->devs[j].bio;
167 if (bio) {
168 for (i = 0; i < RESYNC_PAGES; i++) {
169 safe_put_page(bio->bi_io_vec[i].bv_page);
170 bio->bi_io_vec[i].bv_page = NULL;
172 bio_put(bio);
175 r10bio_pool_free(r10bio, conf);
178 static void put_all_bios(conf_t *conf, r10bio_t *r10_bio)
180 int i;
182 for (i = 0; i < conf->copies; i++) {
183 struct bio **bio = & r10_bio->devs[i].bio;
184 if (*bio && *bio != IO_BLOCKED)
185 bio_put(*bio);
186 *bio = NULL;
190 static void free_r10bio(r10bio_t *r10_bio)
192 conf_t *conf = r10_bio->mddev->private;
195 * Wake up any possible resync thread that waits for the device
196 * to go idle.
198 allow_barrier(conf);
200 put_all_bios(conf, r10_bio);
201 mempool_free(r10_bio, conf->r10bio_pool);
204 static void put_buf(r10bio_t *r10_bio)
206 conf_t *conf = r10_bio->mddev->private;
208 mempool_free(r10_bio, conf->r10buf_pool);
210 lower_barrier(conf);
213 static void reschedule_retry(r10bio_t *r10_bio)
215 unsigned long flags;
216 mddev_t *mddev = r10_bio->mddev;
217 conf_t *conf = mddev->private;
219 spin_lock_irqsave(&conf->device_lock, flags);
220 list_add(&r10_bio->retry_list, &conf->retry_list);
221 conf->nr_queued ++;
222 spin_unlock_irqrestore(&conf->device_lock, flags);
224 /* wake up frozen array... */
225 wake_up(&conf->wait_barrier);
227 md_wakeup_thread(mddev->thread);
231 * raid_end_bio_io() is called when we have finished servicing a mirrored
232 * operation and are ready to return a success/failure code to the buffer
233 * cache layer.
235 static void raid_end_bio_io(r10bio_t *r10_bio)
237 struct bio *bio = r10_bio->master_bio;
239 bio_endio(bio,
240 test_bit(R10BIO_Uptodate, &r10_bio->state) ? 0 : -EIO);
241 free_r10bio(r10_bio);
245 * Update disk head position estimator based on IRQ completion info.
247 static inline void update_head_pos(int slot, r10bio_t *r10_bio)
249 conf_t *conf = r10_bio->mddev->private;
251 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
252 r10_bio->devs[slot].addr + (r10_bio->sectors);
255 static void raid10_end_read_request(struct bio *bio, int error)
257 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
258 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
259 int slot, dev;
260 conf_t *conf = r10_bio->mddev->private;
263 slot = r10_bio->read_slot;
264 dev = r10_bio->devs[slot].devnum;
266 * this branch is our 'one mirror IO has finished' event handler:
268 update_head_pos(slot, r10_bio);
270 if (uptodate) {
272 * Set R10BIO_Uptodate in our master bio, so that
273 * we will return a good error code to the higher
274 * levels even if IO on some other mirrored buffer fails.
276 * The 'master' represents the composite IO operation to
277 * user-side. So if something waits for IO, then it will
278 * wait for the 'master' bio.
280 set_bit(R10BIO_Uptodate, &r10_bio->state);
281 raid_end_bio_io(r10_bio);
282 } else {
284 * oops, read error:
286 char b[BDEVNAME_SIZE];
287 if (printk_ratelimit())
288 printk(KERN_ERR "raid10: %s: rescheduling sector %llu\n",
289 bdevname(conf->mirrors[dev].rdev->bdev,b), (unsigned long long)r10_bio->sector);
290 reschedule_retry(r10_bio);
293 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
296 static void raid10_end_write_request(struct bio *bio, int error)
298 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
299 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
300 int slot, dev;
301 conf_t *conf = r10_bio->mddev->private;
303 for (slot = 0; slot < conf->copies; slot++)
304 if (r10_bio->devs[slot].bio == bio)
305 break;
306 dev = r10_bio->devs[slot].devnum;
309 * this branch is our 'one mirror IO has finished' event handler:
311 if (!uptodate) {
312 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
313 /* an I/O failed, we can't clear the bitmap */
314 set_bit(R10BIO_Degraded, &r10_bio->state);
315 } else
317 * Set R10BIO_Uptodate in our master bio, so that
318 * we will return a good error code for to the higher
319 * levels even if IO on some other mirrored buffer fails.
321 * The 'master' represents the composite IO operation to
322 * user-side. So if something waits for IO, then it will
323 * wait for the 'master' bio.
325 set_bit(R10BIO_Uptodate, &r10_bio->state);
327 update_head_pos(slot, r10_bio);
331 * Let's see if all mirrored write operations have finished
332 * already.
334 if (atomic_dec_and_test(&r10_bio->remaining)) {
335 /* clear the bitmap if all writes complete successfully */
336 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
337 r10_bio->sectors,
338 !test_bit(R10BIO_Degraded, &r10_bio->state),
340 md_write_end(r10_bio->mddev);
341 raid_end_bio_io(r10_bio);
344 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
349 * RAID10 layout manager
350 * Aswell as the chunksize and raid_disks count, there are two
351 * parameters: near_copies and far_copies.
352 * near_copies * far_copies must be <= raid_disks.
353 * Normally one of these will be 1.
354 * If both are 1, we get raid0.
355 * If near_copies == raid_disks, we get raid1.
357 * Chunks are layed out in raid0 style with near_copies copies of the
358 * first chunk, followed by near_copies copies of the next chunk and
359 * so on.
360 * If far_copies > 1, then after 1/far_copies of the array has been assigned
361 * as described above, we start again with a device offset of near_copies.
362 * So we effectively have another copy of the whole array further down all
363 * the drives, but with blocks on different drives.
364 * With this layout, and block is never stored twice on the one device.
366 * raid10_find_phys finds the sector offset of a given virtual sector
367 * on each device that it is on.
369 * raid10_find_virt does the reverse mapping, from a device and a
370 * sector offset to a virtual address
373 static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
375 int n,f;
376 sector_t sector;
377 sector_t chunk;
378 sector_t stripe;
379 int dev;
381 int slot = 0;
383 /* now calculate first sector/dev */
384 chunk = r10bio->sector >> conf->chunk_shift;
385 sector = r10bio->sector & conf->chunk_mask;
387 chunk *= conf->near_copies;
388 stripe = chunk;
389 dev = sector_div(stripe, conf->raid_disks);
390 if (conf->far_offset)
391 stripe *= conf->far_copies;
393 sector += stripe << conf->chunk_shift;
395 /* and calculate all the others */
396 for (n=0; n < conf->near_copies; n++) {
397 int d = dev;
398 sector_t s = sector;
399 r10bio->devs[slot].addr = sector;
400 r10bio->devs[slot].devnum = d;
401 slot++;
403 for (f = 1; f < conf->far_copies; f++) {
404 d += conf->near_copies;
405 if (d >= conf->raid_disks)
406 d -= conf->raid_disks;
407 s += conf->stride;
408 r10bio->devs[slot].devnum = d;
409 r10bio->devs[slot].addr = s;
410 slot++;
412 dev++;
413 if (dev >= conf->raid_disks) {
414 dev = 0;
415 sector += (conf->chunk_mask + 1);
418 BUG_ON(slot != conf->copies);
421 static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
423 sector_t offset, chunk, vchunk;
425 offset = sector & conf->chunk_mask;
426 if (conf->far_offset) {
427 int fc;
428 chunk = sector >> conf->chunk_shift;
429 fc = sector_div(chunk, conf->far_copies);
430 dev -= fc * conf->near_copies;
431 if (dev < 0)
432 dev += conf->raid_disks;
433 } else {
434 while (sector >= conf->stride) {
435 sector -= conf->stride;
436 if (dev < conf->near_copies)
437 dev += conf->raid_disks - conf->near_copies;
438 else
439 dev -= conf->near_copies;
441 chunk = sector >> conf->chunk_shift;
443 vchunk = chunk * conf->raid_disks + dev;
444 sector_div(vchunk, conf->near_copies);
445 return (vchunk << conf->chunk_shift) + offset;
449 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
450 * @q: request queue
451 * @bvm: properties of new bio
452 * @biovec: the request that could be merged to it.
454 * Return amount of bytes we can accept at this offset
455 * If near_copies == raid_disk, there are no striping issues,
456 * but in that case, the function isn't called at all.
458 static int raid10_mergeable_bvec(struct request_queue *q,
459 struct bvec_merge_data *bvm,
460 struct bio_vec *biovec)
462 mddev_t *mddev = q->queuedata;
463 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
464 int max;
465 unsigned int chunk_sectors = mddev->chunk_sectors;
466 unsigned int bio_sectors = bvm->bi_size >> 9;
468 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
469 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
470 if (max <= biovec->bv_len && bio_sectors == 0)
471 return biovec->bv_len;
472 else
473 return max;
477 * This routine returns the disk from which the requested read should
478 * be done. There is a per-array 'next expected sequential IO' sector
479 * number - if this matches on the next IO then we use the last disk.
480 * There is also a per-disk 'last know head position' sector that is
481 * maintained from IRQ contexts, both the normal and the resync IO
482 * completion handlers update this position correctly. If there is no
483 * perfect sequential match then we pick the disk whose head is closest.
485 * If there are 2 mirrors in the same 2 devices, performance degrades
486 * because position is mirror, not device based.
488 * The rdev for the device selected will have nr_pending incremented.
492 * FIXME: possibly should rethink readbalancing and do it differently
493 * depending on near_copies / far_copies geometry.
495 static int read_balance(conf_t *conf, r10bio_t *r10_bio)
497 const sector_t this_sector = r10_bio->sector;
498 int disk, slot, nslot;
499 const int sectors = r10_bio->sectors;
500 sector_t new_distance, current_distance;
501 mdk_rdev_t *rdev;
503 raid10_find_phys(conf, r10_bio);
504 rcu_read_lock();
506 * Check if we can balance. We can balance on the whole
507 * device if no resync is going on (recovery is ok), or below
508 * the resync window. We take the first readable disk when
509 * above the resync window.
511 if (conf->mddev->recovery_cp < MaxSector
512 && (this_sector + sectors >= conf->next_resync)) {
513 /* make sure that disk is operational */
514 slot = 0;
515 disk = r10_bio->devs[slot].devnum;
517 while ((rdev = rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
518 r10_bio->devs[slot].bio == IO_BLOCKED ||
519 !test_bit(In_sync, &rdev->flags)) {
520 slot++;
521 if (slot == conf->copies) {
522 slot = 0;
523 disk = -1;
524 break;
526 disk = r10_bio->devs[slot].devnum;
528 goto rb_out;
532 /* make sure the disk is operational */
533 slot = 0;
534 disk = r10_bio->devs[slot].devnum;
535 while ((rdev=rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
536 r10_bio->devs[slot].bio == IO_BLOCKED ||
537 !test_bit(In_sync, &rdev->flags)) {
538 slot ++;
539 if (slot == conf->copies) {
540 disk = -1;
541 goto rb_out;
543 disk = r10_bio->devs[slot].devnum;
547 current_distance = abs(r10_bio->devs[slot].addr -
548 conf->mirrors[disk].head_position);
550 /* Find the disk whose head is closest,
551 * or - for far > 1 - find the closest to partition beginning */
553 for (nslot = slot; nslot < conf->copies; nslot++) {
554 int ndisk = r10_bio->devs[nslot].devnum;
557 if ((rdev=rcu_dereference(conf->mirrors[ndisk].rdev)) == NULL ||
558 r10_bio->devs[nslot].bio == IO_BLOCKED ||
559 !test_bit(In_sync, &rdev->flags))
560 continue;
562 /* This optimisation is debatable, and completely destroys
563 * sequential read speed for 'far copies' arrays. So only
564 * keep it for 'near' arrays, and review those later.
566 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) {
567 disk = ndisk;
568 slot = nslot;
569 break;
572 /* for far > 1 always use the lowest address */
573 if (conf->far_copies > 1)
574 new_distance = r10_bio->devs[nslot].addr;
575 else
576 new_distance = abs(r10_bio->devs[nslot].addr -
577 conf->mirrors[ndisk].head_position);
578 if (new_distance < current_distance) {
579 current_distance = new_distance;
580 disk = ndisk;
581 slot = nslot;
585 rb_out:
586 r10_bio->read_slot = slot;
587 /* conf->next_seq_sect = this_sector + sectors;*/
589 if (disk >= 0 && (rdev=rcu_dereference(conf->mirrors[disk].rdev))!= NULL)
590 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
591 else
592 disk = -1;
593 rcu_read_unlock();
595 return disk;
598 static void unplug_slaves(mddev_t *mddev)
600 conf_t *conf = mddev->private;
601 int i;
603 rcu_read_lock();
604 for (i=0; i<mddev->raid_disks; i++) {
605 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
606 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
607 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
609 atomic_inc(&rdev->nr_pending);
610 rcu_read_unlock();
612 blk_unplug(r_queue);
614 rdev_dec_pending(rdev, mddev);
615 rcu_read_lock();
618 rcu_read_unlock();
621 static void raid10_unplug(struct request_queue *q)
623 mddev_t *mddev = q->queuedata;
625 unplug_slaves(q->queuedata);
626 md_wakeup_thread(mddev->thread);
629 static int raid10_congested(void *data, int bits)
631 mddev_t *mddev = data;
632 conf_t *conf = mddev->private;
633 int i, ret = 0;
635 if (mddev_congested(mddev, bits))
636 return 1;
637 rcu_read_lock();
638 for (i = 0; i < mddev->raid_disks && ret == 0; i++) {
639 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
640 if (rdev && !test_bit(Faulty, &rdev->flags)) {
641 struct request_queue *q = bdev_get_queue(rdev->bdev);
643 ret |= bdi_congested(&q->backing_dev_info, bits);
646 rcu_read_unlock();
647 return ret;
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 /* Barriers....
681 * Sometimes we need to suspend IO while we do something else,
682 * either some resync/recovery, or reconfigure the array.
683 * To do this we raise a 'barrier'.
684 * The 'barrier' is a counter that can be raised multiple times
685 * to count how many activities are happening which preclude
686 * normal IO.
687 * We can only raise the barrier if there is no pending IO.
688 * i.e. if nr_pending == 0.
689 * We choose only to raise the barrier if no-one is waiting for the
690 * barrier to go down. This means that as soon as an IO request
691 * is ready, no other operations which require a barrier will start
692 * until the IO request has had a chance.
694 * So: regular IO calls 'wait_barrier'. When that returns there
695 * is no backgroup IO happening, It must arrange to call
696 * allow_barrier when it has finished its IO.
697 * backgroup IO calls must call raise_barrier. Once that returns
698 * there is no normal IO happeing. It must arrange to call
699 * lower_barrier when the particular background IO completes.
702 static void raise_barrier(conf_t *conf, int force)
704 BUG_ON(force && !conf->barrier);
705 spin_lock_irq(&conf->resync_lock);
707 /* Wait until no block IO is waiting (unless 'force') */
708 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
709 conf->resync_lock,
710 raid10_unplug(conf->mddev->queue));
712 /* block any new IO from starting */
713 conf->barrier++;
715 /* No wait for all pending IO to complete */
716 wait_event_lock_irq(conf->wait_barrier,
717 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
718 conf->resync_lock,
719 raid10_unplug(conf->mddev->queue));
721 spin_unlock_irq(&conf->resync_lock);
724 static void lower_barrier(conf_t *conf)
726 unsigned long flags;
727 spin_lock_irqsave(&conf->resync_lock, flags);
728 conf->barrier--;
729 spin_unlock_irqrestore(&conf->resync_lock, flags);
730 wake_up(&conf->wait_barrier);
733 static void wait_barrier(conf_t *conf)
735 spin_lock_irq(&conf->resync_lock);
736 if (conf->barrier) {
737 conf->nr_waiting++;
738 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
739 conf->resync_lock,
740 raid10_unplug(conf->mddev->queue));
741 conf->nr_waiting--;
743 conf->nr_pending++;
744 spin_unlock_irq(&conf->resync_lock);
747 static void allow_barrier(conf_t *conf)
749 unsigned long flags;
750 spin_lock_irqsave(&conf->resync_lock, flags);
751 conf->nr_pending--;
752 spin_unlock_irqrestore(&conf->resync_lock, flags);
753 wake_up(&conf->wait_barrier);
756 static void freeze_array(conf_t *conf)
758 /* stop syncio and normal IO and wait for everything to
759 * go quiet.
760 * We increment barrier and nr_waiting, and then
761 * wait until nr_pending match nr_queued+1
762 * This is called in the context of one normal IO request
763 * that has failed. Thus any sync request that might be pending
764 * will be blocked by nr_pending, and we need to wait for
765 * pending IO requests to complete or be queued for re-try.
766 * Thus the number queued (nr_queued) plus this request (1)
767 * must match the number of pending IOs (nr_pending) before
768 * we continue.
770 spin_lock_irq(&conf->resync_lock);
771 conf->barrier++;
772 conf->nr_waiting++;
773 wait_event_lock_irq(conf->wait_barrier,
774 conf->nr_pending == conf->nr_queued+1,
775 conf->resync_lock,
776 ({ flush_pending_writes(conf);
777 raid10_unplug(conf->mddev->queue); }));
778 spin_unlock_irq(&conf->resync_lock);
781 static void unfreeze_array(conf_t *conf)
783 /* reverse the effect of the freeze */
784 spin_lock_irq(&conf->resync_lock);
785 conf->barrier--;
786 conf->nr_waiting--;
787 wake_up(&conf->wait_barrier);
788 spin_unlock_irq(&conf->resync_lock);
791 static int make_request(struct request_queue *q, struct bio * bio)
793 mddev_t *mddev = q->queuedata;
794 conf_t *conf = mddev->private;
795 mirror_info_t *mirror;
796 r10bio_t *r10_bio;
797 struct bio *read_bio;
798 int cpu;
799 int i;
800 int chunk_sects = conf->chunk_mask + 1;
801 const int rw = bio_data_dir(bio);
802 const bool do_sync = bio_rw_flagged(bio, BIO_RW_SYNCIO);
803 struct bio_list bl;
804 unsigned long flags;
805 mdk_rdev_t *blocked_rdev;
807 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
808 md_barrier_request(mddev, bio);
809 return 0;
812 /* If this request crosses a chunk boundary, we need to
813 * split it. This will only happen for 1 PAGE (or less) requests.
815 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
816 > chunk_sects &&
817 conf->near_copies < conf->raid_disks)) {
818 struct bio_pair *bp;
819 /* Sanity check -- queue functions should prevent this happening */
820 if (bio->bi_vcnt != 1 ||
821 bio->bi_idx != 0)
822 goto bad_map;
823 /* This is a one page bio that upper layers
824 * refuse to split for us, so we need to split it.
826 bp = bio_split(bio,
827 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
829 /* Each of these 'make_request' calls will call 'wait_barrier'.
830 * If the first succeeds but the second blocks due to the resync
831 * thread raising the barrier, we will deadlock because the
832 * IO to the underlying device will be queued in generic_make_request
833 * and will never complete, so will never reduce nr_pending.
834 * So increment nr_waiting here so no new raise_barriers will
835 * succeed, and so the second wait_barrier cannot block.
837 spin_lock_irq(&conf->resync_lock);
838 conf->nr_waiting++;
839 spin_unlock_irq(&conf->resync_lock);
841 if (make_request(q, &bp->bio1))
842 generic_make_request(&bp->bio1);
843 if (make_request(q, &bp->bio2))
844 generic_make_request(&bp->bio2);
846 spin_lock_irq(&conf->resync_lock);
847 conf->nr_waiting--;
848 wake_up(&conf->wait_barrier);
849 spin_unlock_irq(&conf->resync_lock);
851 bio_pair_release(bp);
852 return 0;
853 bad_map:
854 printk("raid10_make_request bug: can't convert block across chunks"
855 " or bigger than %dk %llu %d\n", chunk_sects/2,
856 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
858 bio_io_error(bio);
859 return 0;
862 md_write_start(mddev, bio);
865 * Register the new request and wait if the reconstruction
866 * thread has put up a bar for new requests.
867 * Continue immediately if no resync is active currently.
869 wait_barrier(conf);
871 cpu = part_stat_lock();
872 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
873 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
874 bio_sectors(bio));
875 part_stat_unlock();
877 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
879 r10_bio->master_bio = bio;
880 r10_bio->sectors = bio->bi_size >> 9;
882 r10_bio->mddev = mddev;
883 r10_bio->sector = bio->bi_sector;
884 r10_bio->state = 0;
886 if (rw == READ) {
888 * read balancing logic:
890 int disk = read_balance(conf, r10_bio);
891 int slot = r10_bio->read_slot;
892 if (disk < 0) {
893 raid_end_bio_io(r10_bio);
894 return 0;
896 mirror = conf->mirrors + disk;
898 read_bio = bio_clone(bio, GFP_NOIO);
900 r10_bio->devs[slot].bio = read_bio;
902 read_bio->bi_sector = r10_bio->devs[slot].addr +
903 mirror->rdev->data_offset;
904 read_bio->bi_bdev = mirror->rdev->bdev;
905 read_bio->bi_end_io = raid10_end_read_request;
906 read_bio->bi_rw = READ | (do_sync << BIO_RW_SYNCIO);
907 read_bio->bi_private = r10_bio;
909 generic_make_request(read_bio);
910 return 0;
914 * WRITE:
916 /* first select target devices under rcu_lock and
917 * inc refcount on their rdev. Record them by setting
918 * bios[x] to bio
920 raid10_find_phys(conf, r10_bio);
921 retry_write:
922 blocked_rdev = NULL;
923 rcu_read_lock();
924 for (i = 0; i < conf->copies; i++) {
925 int d = r10_bio->devs[i].devnum;
926 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
927 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
928 atomic_inc(&rdev->nr_pending);
929 blocked_rdev = rdev;
930 break;
932 if (rdev && !test_bit(Faulty, &rdev->flags)) {
933 atomic_inc(&rdev->nr_pending);
934 r10_bio->devs[i].bio = bio;
935 } else {
936 r10_bio->devs[i].bio = NULL;
937 set_bit(R10BIO_Degraded, &r10_bio->state);
940 rcu_read_unlock();
942 if (unlikely(blocked_rdev)) {
943 /* Have to wait for this device to get unblocked, then retry */
944 int j;
945 int d;
947 for (j = 0; j < i; j++)
948 if (r10_bio->devs[j].bio) {
949 d = r10_bio->devs[j].devnum;
950 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
952 allow_barrier(conf);
953 md_wait_for_blocked_rdev(blocked_rdev, mddev);
954 wait_barrier(conf);
955 goto retry_write;
958 atomic_set(&r10_bio->remaining, 0);
960 bio_list_init(&bl);
961 for (i = 0; i < conf->copies; i++) {
962 struct bio *mbio;
963 int d = r10_bio->devs[i].devnum;
964 if (!r10_bio->devs[i].bio)
965 continue;
967 mbio = bio_clone(bio, GFP_NOIO);
968 r10_bio->devs[i].bio = mbio;
970 mbio->bi_sector = r10_bio->devs[i].addr+
971 conf->mirrors[d].rdev->data_offset;
972 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
973 mbio->bi_end_io = raid10_end_write_request;
974 mbio->bi_rw = WRITE | (do_sync << BIO_RW_SYNCIO);
975 mbio->bi_private = r10_bio;
977 atomic_inc(&r10_bio->remaining);
978 bio_list_add(&bl, mbio);
981 if (unlikely(!atomic_read(&r10_bio->remaining))) {
982 /* the array is dead */
983 md_write_end(mddev);
984 raid_end_bio_io(r10_bio);
985 return 0;
988 bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
989 spin_lock_irqsave(&conf->device_lock, flags);
990 bio_list_merge(&conf->pending_bio_list, &bl);
991 blk_plug_device(mddev->queue);
992 spin_unlock_irqrestore(&conf->device_lock, flags);
994 /* In case raid10d snuck in to freeze_array */
995 wake_up(&conf->wait_barrier);
997 if (do_sync)
998 md_wakeup_thread(mddev->thread);
1000 return 0;
1003 static void status(struct seq_file *seq, mddev_t *mddev)
1005 conf_t *conf = mddev->private;
1006 int i;
1008 if (conf->near_copies < conf->raid_disks)
1009 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
1010 if (conf->near_copies > 1)
1011 seq_printf(seq, " %d near-copies", conf->near_copies);
1012 if (conf->far_copies > 1) {
1013 if (conf->far_offset)
1014 seq_printf(seq, " %d offset-copies", conf->far_copies);
1015 else
1016 seq_printf(seq, " %d far-copies", conf->far_copies);
1018 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
1019 conf->raid_disks - mddev->degraded);
1020 for (i = 0; i < conf->raid_disks; i++)
1021 seq_printf(seq, "%s",
1022 conf->mirrors[i].rdev &&
1023 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
1024 seq_printf(seq, "]");
1027 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1029 char b[BDEVNAME_SIZE];
1030 conf_t *conf = mddev->private;
1033 * If it is not operational, then we have already marked it as dead
1034 * else if it is the last working disks, ignore the error, let the
1035 * next level up know.
1036 * else mark the drive as failed
1038 if (test_bit(In_sync, &rdev->flags)
1039 && conf->raid_disks-mddev->degraded == 1)
1041 * Don't fail the drive, just return an IO error.
1042 * The test should really be more sophisticated than
1043 * "working_disks == 1", but it isn't critical, and
1044 * can wait until we do more sophisticated "is the drive
1045 * really dead" tests...
1047 return;
1048 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1049 unsigned long flags;
1050 spin_lock_irqsave(&conf->device_lock, flags);
1051 mddev->degraded++;
1052 spin_unlock_irqrestore(&conf->device_lock, flags);
1054 * if recovery is running, make sure it aborts.
1056 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1058 set_bit(Faulty, &rdev->flags);
1059 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1060 printk(KERN_ALERT "raid10: Disk failure on %s, disabling device.\n"
1061 "raid10: Operation continuing on %d devices.\n",
1062 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
1065 static void print_conf(conf_t *conf)
1067 int i;
1068 mirror_info_t *tmp;
1070 printk("RAID10 conf printout:\n");
1071 if (!conf) {
1072 printk("(!conf)\n");
1073 return;
1075 printk(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1076 conf->raid_disks);
1078 for (i = 0; i < conf->raid_disks; i++) {
1079 char b[BDEVNAME_SIZE];
1080 tmp = conf->mirrors + i;
1081 if (tmp->rdev)
1082 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
1083 i, !test_bit(In_sync, &tmp->rdev->flags),
1084 !test_bit(Faulty, &tmp->rdev->flags),
1085 bdevname(tmp->rdev->bdev,b));
1089 static void close_sync(conf_t *conf)
1091 wait_barrier(conf);
1092 allow_barrier(conf);
1094 mempool_destroy(conf->r10buf_pool);
1095 conf->r10buf_pool = NULL;
1098 /* check if there are enough drives for
1099 * every block to appear on atleast one
1101 static int enough(conf_t *conf)
1103 int first = 0;
1105 do {
1106 int n = conf->copies;
1107 int cnt = 0;
1108 while (n--) {
1109 if (conf->mirrors[first].rdev)
1110 cnt++;
1111 first = (first+1) % conf->raid_disks;
1113 if (cnt == 0)
1114 return 0;
1115 } while (first != 0);
1116 return 1;
1119 static int raid10_spare_active(mddev_t *mddev)
1121 int i;
1122 conf_t *conf = mddev->private;
1123 mirror_info_t *tmp;
1126 * Find all non-in_sync disks within the RAID10 configuration
1127 * and mark them in_sync
1129 for (i = 0; i < conf->raid_disks; i++) {
1130 tmp = conf->mirrors + i;
1131 if (tmp->rdev
1132 && !test_bit(Faulty, &tmp->rdev->flags)
1133 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1134 unsigned long flags;
1135 spin_lock_irqsave(&conf->device_lock, flags);
1136 mddev->degraded--;
1137 spin_unlock_irqrestore(&conf->device_lock, flags);
1141 print_conf(conf);
1142 return 0;
1146 static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1148 conf_t *conf = mddev->private;
1149 int err = -EEXIST;
1150 int mirror;
1151 mirror_info_t *p;
1152 int first = 0;
1153 int last = mddev->raid_disks - 1;
1155 if (mddev->recovery_cp < MaxSector)
1156 /* only hot-add to in-sync arrays, as recovery is
1157 * very different from resync
1159 return -EBUSY;
1160 if (!enough(conf))
1161 return -EINVAL;
1163 if (rdev->raid_disk >= 0)
1164 first = last = rdev->raid_disk;
1166 if (rdev->saved_raid_disk >= 0 &&
1167 rdev->saved_raid_disk >= first &&
1168 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1169 mirror = rdev->saved_raid_disk;
1170 else
1171 mirror = first;
1172 for ( ; mirror <= last ; mirror++)
1173 if ( !(p=conf->mirrors+mirror)->rdev) {
1175 disk_stack_limits(mddev->gendisk, rdev->bdev,
1176 rdev->data_offset << 9);
1177 /* as we don't honour merge_bvec_fn, we must
1178 * never risk violating it, so limit
1179 * ->max_segments to one lying with a single
1180 * page, as a one page request is never in
1181 * violation.
1183 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1184 blk_queue_max_segments(mddev->queue, 1);
1185 blk_queue_segment_boundary(mddev->queue,
1186 PAGE_CACHE_SIZE - 1);
1189 p->head_position = 0;
1190 rdev->raid_disk = mirror;
1191 err = 0;
1192 if (rdev->saved_raid_disk != mirror)
1193 conf->fullsync = 1;
1194 rcu_assign_pointer(p->rdev, rdev);
1195 break;
1198 md_integrity_add_rdev(rdev, mddev);
1199 print_conf(conf);
1200 return err;
1203 static int raid10_remove_disk(mddev_t *mddev, int number)
1205 conf_t *conf = mddev->private;
1206 int err = 0;
1207 mdk_rdev_t *rdev;
1208 mirror_info_t *p = conf->mirrors+ number;
1210 print_conf(conf);
1211 rdev = p->rdev;
1212 if (rdev) {
1213 if (test_bit(In_sync, &rdev->flags) ||
1214 atomic_read(&rdev->nr_pending)) {
1215 err = -EBUSY;
1216 goto abort;
1218 /* Only remove faulty devices in recovery
1219 * is not possible.
1221 if (!test_bit(Faulty, &rdev->flags) &&
1222 enough(conf)) {
1223 err = -EBUSY;
1224 goto abort;
1226 p->rdev = NULL;
1227 synchronize_rcu();
1228 if (atomic_read(&rdev->nr_pending)) {
1229 /* lost the race, try later */
1230 err = -EBUSY;
1231 p->rdev = rdev;
1232 goto abort;
1234 md_integrity_register(mddev);
1236 abort:
1238 print_conf(conf);
1239 return err;
1243 static void end_sync_read(struct bio *bio, int error)
1245 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1246 conf_t *conf = r10_bio->mddev->private;
1247 int i,d;
1249 for (i=0; i<conf->copies; i++)
1250 if (r10_bio->devs[i].bio == bio)
1251 break;
1252 BUG_ON(i == conf->copies);
1253 update_head_pos(i, r10_bio);
1254 d = r10_bio->devs[i].devnum;
1256 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1257 set_bit(R10BIO_Uptodate, &r10_bio->state);
1258 else {
1259 atomic_add(r10_bio->sectors,
1260 &conf->mirrors[d].rdev->corrected_errors);
1261 if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
1262 md_error(r10_bio->mddev,
1263 conf->mirrors[d].rdev);
1266 /* for reconstruct, we always reschedule after a read.
1267 * for resync, only after all reads
1269 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1270 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1271 atomic_dec_and_test(&r10_bio->remaining)) {
1272 /* we have read all the blocks,
1273 * do the comparison in process context in raid10d
1275 reschedule_retry(r10_bio);
1279 static void end_sync_write(struct bio *bio, int error)
1281 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1282 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1283 mddev_t *mddev = r10_bio->mddev;
1284 conf_t *conf = mddev->private;
1285 int i,d;
1287 for (i = 0; i < conf->copies; i++)
1288 if (r10_bio->devs[i].bio == bio)
1289 break;
1290 d = r10_bio->devs[i].devnum;
1292 if (!uptodate)
1293 md_error(mddev, conf->mirrors[d].rdev);
1295 update_head_pos(i, r10_bio);
1297 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1298 while (atomic_dec_and_test(&r10_bio->remaining)) {
1299 if (r10_bio->master_bio == NULL) {
1300 /* the primary of several recovery bios */
1301 sector_t s = r10_bio->sectors;
1302 put_buf(r10_bio);
1303 md_done_sync(mddev, s, 1);
1304 break;
1305 } else {
1306 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1307 put_buf(r10_bio);
1308 r10_bio = r10_bio2;
1314 * Note: sync and recover and handled very differently for raid10
1315 * This code is for resync.
1316 * For resync, we read through virtual addresses and read all blocks.
1317 * If there is any error, we schedule a write. The lowest numbered
1318 * drive is authoritative.
1319 * However requests come for physical address, so we need to map.
1320 * For every physical address there are raid_disks/copies virtual addresses,
1321 * which is always are least one, but is not necessarly an integer.
1322 * This means that a physical address can span multiple chunks, so we may
1323 * have to submit multiple io requests for a single sync request.
1326 * We check if all blocks are in-sync and only write to blocks that
1327 * aren't in sync
1329 static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1331 conf_t *conf = mddev->private;
1332 int i, first;
1333 struct bio *tbio, *fbio;
1335 atomic_set(&r10_bio->remaining, 1);
1337 /* find the first device with a block */
1338 for (i=0; i<conf->copies; i++)
1339 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1340 break;
1342 if (i == conf->copies)
1343 goto done;
1345 first = i;
1346 fbio = r10_bio->devs[i].bio;
1348 /* now find blocks with errors */
1349 for (i=0 ; i < conf->copies ; i++) {
1350 int j, d;
1351 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
1353 tbio = r10_bio->devs[i].bio;
1355 if (tbio->bi_end_io != end_sync_read)
1356 continue;
1357 if (i == first)
1358 continue;
1359 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1360 /* We know that the bi_io_vec layout is the same for
1361 * both 'first' and 'i', so we just compare them.
1362 * All vec entries are PAGE_SIZE;
1364 for (j = 0; j < vcnt; j++)
1365 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1366 page_address(tbio->bi_io_vec[j].bv_page),
1367 PAGE_SIZE))
1368 break;
1369 if (j == vcnt)
1370 continue;
1371 mddev->resync_mismatches += r10_bio->sectors;
1373 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1374 /* Don't fix anything. */
1375 continue;
1376 /* Ok, we need to write this bio
1377 * First we need to fixup bv_offset, bv_len and
1378 * bi_vecs, as the read request might have corrupted these
1380 tbio->bi_vcnt = vcnt;
1381 tbio->bi_size = r10_bio->sectors << 9;
1382 tbio->bi_idx = 0;
1383 tbio->bi_phys_segments = 0;
1384 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1385 tbio->bi_flags |= 1 << BIO_UPTODATE;
1386 tbio->bi_next = NULL;
1387 tbio->bi_rw = WRITE;
1388 tbio->bi_private = r10_bio;
1389 tbio->bi_sector = r10_bio->devs[i].addr;
1391 for (j=0; j < vcnt ; j++) {
1392 tbio->bi_io_vec[j].bv_offset = 0;
1393 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1395 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1396 page_address(fbio->bi_io_vec[j].bv_page),
1397 PAGE_SIZE);
1399 tbio->bi_end_io = end_sync_write;
1401 d = r10_bio->devs[i].devnum;
1402 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1403 atomic_inc(&r10_bio->remaining);
1404 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1406 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1407 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1408 generic_make_request(tbio);
1411 done:
1412 if (atomic_dec_and_test(&r10_bio->remaining)) {
1413 md_done_sync(mddev, r10_bio->sectors, 1);
1414 put_buf(r10_bio);
1419 * Now for the recovery code.
1420 * Recovery happens across physical sectors.
1421 * We recover all non-is_sync drives by finding the virtual address of
1422 * each, and then choose a working drive that also has that virt address.
1423 * There is a separate r10_bio for each non-in_sync drive.
1424 * Only the first two slots are in use. The first for reading,
1425 * The second for writing.
1429 static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1431 conf_t *conf = mddev->private;
1432 int i, d;
1433 struct bio *bio, *wbio;
1436 /* move the pages across to the second bio
1437 * and submit the write request
1439 bio = r10_bio->devs[0].bio;
1440 wbio = r10_bio->devs[1].bio;
1441 for (i=0; i < wbio->bi_vcnt; i++) {
1442 struct page *p = bio->bi_io_vec[i].bv_page;
1443 bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
1444 wbio->bi_io_vec[i].bv_page = p;
1446 d = r10_bio->devs[1].devnum;
1448 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1449 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
1450 if (test_bit(R10BIO_Uptodate, &r10_bio->state))
1451 generic_make_request(wbio);
1452 else
1453 bio_endio(wbio, -EIO);
1458 * Used by fix_read_error() to decay the per rdev read_errors.
1459 * We halve the read error count for every hour that has elapsed
1460 * since the last recorded read error.
1463 static void check_decay_read_errors(mddev_t *mddev, mdk_rdev_t *rdev)
1465 struct timespec cur_time_mon;
1466 unsigned long hours_since_last;
1467 unsigned int read_errors = atomic_read(&rdev->read_errors);
1469 ktime_get_ts(&cur_time_mon);
1471 if (rdev->last_read_error.tv_sec == 0 &&
1472 rdev->last_read_error.tv_nsec == 0) {
1473 /* first time we've seen a read error */
1474 rdev->last_read_error = cur_time_mon;
1475 return;
1478 hours_since_last = (cur_time_mon.tv_sec -
1479 rdev->last_read_error.tv_sec) / 3600;
1481 rdev->last_read_error = cur_time_mon;
1484 * if hours_since_last is > the number of bits in read_errors
1485 * just set read errors to 0. We do this to avoid
1486 * overflowing the shift of read_errors by hours_since_last.
1488 if (hours_since_last >= 8 * sizeof(read_errors))
1489 atomic_set(&rdev->read_errors, 0);
1490 else
1491 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
1495 * This is a kernel thread which:
1497 * 1. Retries failed read operations on working mirrors.
1498 * 2. Updates the raid superblock when problems encounter.
1499 * 3. Performs writes following reads for array synchronising.
1502 static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
1504 int sect = 0; /* Offset from r10_bio->sector */
1505 int sectors = r10_bio->sectors;
1506 mdk_rdev_t*rdev;
1507 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
1508 int d = r10_bio->devs[r10_bio->read_slot].devnum;
1510 rcu_read_lock();
1511 rdev = rcu_dereference(conf->mirrors[d].rdev);
1512 if (rdev) { /* If rdev is not NULL */
1513 char b[BDEVNAME_SIZE];
1514 int cur_read_error_count = 0;
1516 bdevname(rdev->bdev, b);
1518 if (test_bit(Faulty, &rdev->flags)) {
1519 rcu_read_unlock();
1520 /* drive has already been failed, just ignore any
1521 more fix_read_error() attempts */
1522 return;
1525 check_decay_read_errors(mddev, rdev);
1526 atomic_inc(&rdev->read_errors);
1527 cur_read_error_count = atomic_read(&rdev->read_errors);
1528 if (cur_read_error_count > max_read_errors) {
1529 rcu_read_unlock();
1530 printk(KERN_NOTICE
1531 "raid10: %s: Raid device exceeded "
1532 "read_error threshold "
1533 "[cur %d:max %d]\n",
1534 b, cur_read_error_count, max_read_errors);
1535 printk(KERN_NOTICE
1536 "raid10: %s: Failing raid "
1537 "device\n", b);
1538 md_error(mddev, conf->mirrors[d].rdev);
1539 return;
1542 rcu_read_unlock();
1544 while(sectors) {
1545 int s = sectors;
1546 int sl = r10_bio->read_slot;
1547 int success = 0;
1548 int start;
1550 if (s > (PAGE_SIZE>>9))
1551 s = PAGE_SIZE >> 9;
1553 rcu_read_lock();
1554 do {
1555 d = r10_bio->devs[sl].devnum;
1556 rdev = rcu_dereference(conf->mirrors[d].rdev);
1557 if (rdev &&
1558 test_bit(In_sync, &rdev->flags)) {
1559 atomic_inc(&rdev->nr_pending);
1560 rcu_read_unlock();
1561 success = sync_page_io(rdev->bdev,
1562 r10_bio->devs[sl].addr +
1563 sect + rdev->data_offset,
1564 s<<9,
1565 conf->tmppage, READ);
1566 rdev_dec_pending(rdev, mddev);
1567 rcu_read_lock();
1568 if (success)
1569 break;
1571 sl++;
1572 if (sl == conf->copies)
1573 sl = 0;
1574 } while (!success && sl != r10_bio->read_slot);
1575 rcu_read_unlock();
1577 if (!success) {
1578 /* Cannot read from anywhere -- bye bye array */
1579 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
1580 md_error(mddev, conf->mirrors[dn].rdev);
1581 break;
1584 start = sl;
1585 /* write it back and re-read */
1586 rcu_read_lock();
1587 while (sl != r10_bio->read_slot) {
1588 char b[BDEVNAME_SIZE];
1590 if (sl==0)
1591 sl = conf->copies;
1592 sl--;
1593 d = r10_bio->devs[sl].devnum;
1594 rdev = rcu_dereference(conf->mirrors[d].rdev);
1595 if (rdev &&
1596 test_bit(In_sync, &rdev->flags)) {
1597 atomic_inc(&rdev->nr_pending);
1598 rcu_read_unlock();
1599 atomic_add(s, &rdev->corrected_errors);
1600 if (sync_page_io(rdev->bdev,
1601 r10_bio->devs[sl].addr +
1602 sect + rdev->data_offset,
1603 s<<9, conf->tmppage, WRITE)
1604 == 0) {
1605 /* Well, this device is dead */
1606 printk(KERN_NOTICE
1607 "raid10:%s: read correction "
1608 "write failed"
1609 " (%d sectors at %llu on %s)\n",
1610 mdname(mddev), s,
1611 (unsigned long long)(sect+
1612 rdev->data_offset),
1613 bdevname(rdev->bdev, b));
1614 printk(KERN_NOTICE "raid10:%s: failing "
1615 "drive\n",
1616 bdevname(rdev->bdev, b));
1617 md_error(mddev, rdev);
1619 rdev_dec_pending(rdev, mddev);
1620 rcu_read_lock();
1623 sl = start;
1624 while (sl != r10_bio->read_slot) {
1626 if (sl==0)
1627 sl = conf->copies;
1628 sl--;
1629 d = r10_bio->devs[sl].devnum;
1630 rdev = rcu_dereference(conf->mirrors[d].rdev);
1631 if (rdev &&
1632 test_bit(In_sync, &rdev->flags)) {
1633 char b[BDEVNAME_SIZE];
1634 atomic_inc(&rdev->nr_pending);
1635 rcu_read_unlock();
1636 if (sync_page_io(rdev->bdev,
1637 r10_bio->devs[sl].addr +
1638 sect + rdev->data_offset,
1639 s<<9, conf->tmppage,
1640 READ) == 0) {
1641 /* Well, this device is dead */
1642 printk(KERN_NOTICE
1643 "raid10:%s: unable to read back "
1644 "corrected sectors"
1645 " (%d sectors at %llu on %s)\n",
1646 mdname(mddev), s,
1647 (unsigned long long)(sect+
1648 rdev->data_offset),
1649 bdevname(rdev->bdev, b));
1650 printk(KERN_NOTICE "raid10:%s: failing drive\n",
1651 bdevname(rdev->bdev, b));
1653 md_error(mddev, rdev);
1654 } else {
1655 printk(KERN_INFO
1656 "raid10:%s: read error corrected"
1657 " (%d sectors at %llu on %s)\n",
1658 mdname(mddev), s,
1659 (unsigned long long)(sect+
1660 rdev->data_offset),
1661 bdevname(rdev->bdev, b));
1664 rdev_dec_pending(rdev, mddev);
1665 rcu_read_lock();
1668 rcu_read_unlock();
1670 sectors -= s;
1671 sect += s;
1675 static void raid10d(mddev_t *mddev)
1677 r10bio_t *r10_bio;
1678 struct bio *bio;
1679 unsigned long flags;
1680 conf_t *conf = mddev->private;
1681 struct list_head *head = &conf->retry_list;
1682 int unplug=0;
1683 mdk_rdev_t *rdev;
1685 md_check_recovery(mddev);
1687 for (;;) {
1688 char b[BDEVNAME_SIZE];
1690 unplug += flush_pending_writes(conf);
1692 spin_lock_irqsave(&conf->device_lock, flags);
1693 if (list_empty(head)) {
1694 spin_unlock_irqrestore(&conf->device_lock, flags);
1695 break;
1697 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1698 list_del(head->prev);
1699 conf->nr_queued--;
1700 spin_unlock_irqrestore(&conf->device_lock, flags);
1702 mddev = r10_bio->mddev;
1703 conf = mddev->private;
1704 if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
1705 sync_request_write(mddev, r10_bio);
1706 unplug = 1;
1707 } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1708 recovery_request_write(mddev, r10_bio);
1709 unplug = 1;
1710 } else {
1711 int mirror;
1712 /* we got a read error. Maybe the drive is bad. Maybe just
1713 * the block and we can fix it.
1714 * We freeze all other IO, and try reading the block from
1715 * other devices. When we find one, we re-write
1716 * and check it that fixes the read error.
1717 * This is all done synchronously while the array is
1718 * frozen.
1720 if (mddev->ro == 0) {
1721 freeze_array(conf);
1722 fix_read_error(conf, mddev, r10_bio);
1723 unfreeze_array(conf);
1726 bio = r10_bio->devs[r10_bio->read_slot].bio;
1727 r10_bio->devs[r10_bio->read_slot].bio =
1728 mddev->ro ? IO_BLOCKED : NULL;
1729 mirror = read_balance(conf, r10_bio);
1730 if (mirror == -1) {
1731 printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
1732 " read error for block %llu\n",
1733 bdevname(bio->bi_bdev,b),
1734 (unsigned long long)r10_bio->sector);
1735 raid_end_bio_io(r10_bio);
1736 bio_put(bio);
1737 } else {
1738 const bool do_sync = bio_rw_flagged(r10_bio->master_bio, BIO_RW_SYNCIO);
1739 bio_put(bio);
1740 rdev = conf->mirrors[mirror].rdev;
1741 if (printk_ratelimit())
1742 printk(KERN_ERR "raid10: %s: redirecting sector %llu to"
1743 " another mirror\n",
1744 bdevname(rdev->bdev,b),
1745 (unsigned long long)r10_bio->sector);
1746 bio = bio_clone(r10_bio->master_bio, GFP_NOIO);
1747 r10_bio->devs[r10_bio->read_slot].bio = bio;
1748 bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
1749 + rdev->data_offset;
1750 bio->bi_bdev = rdev->bdev;
1751 bio->bi_rw = READ | (do_sync << BIO_RW_SYNCIO);
1752 bio->bi_private = r10_bio;
1753 bio->bi_end_io = raid10_end_read_request;
1754 unplug = 1;
1755 generic_make_request(bio);
1758 cond_resched();
1760 if (unplug)
1761 unplug_slaves(mddev);
1765 static int init_resync(conf_t *conf)
1767 int buffs;
1769 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
1770 BUG_ON(conf->r10buf_pool);
1771 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1772 if (!conf->r10buf_pool)
1773 return -ENOMEM;
1774 conf->next_resync = 0;
1775 return 0;
1779 * perform a "sync" on one "block"
1781 * We need to make sure that no normal I/O request - particularly write
1782 * requests - conflict with active sync requests.
1784 * This is achieved by tracking pending requests and a 'barrier' concept
1785 * that can be installed to exclude normal IO requests.
1787 * Resync and recovery are handled very differently.
1788 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1790 * For resync, we iterate over virtual addresses, read all copies,
1791 * and update if there are differences. If only one copy is live,
1792 * skip it.
1793 * For recovery, we iterate over physical addresses, read a good
1794 * value for each non-in_sync drive, and over-write.
1796 * So, for recovery we may have several outstanding complex requests for a
1797 * given address, one for each out-of-sync device. We model this by allocating
1798 * a number of r10_bio structures, one for each out-of-sync device.
1799 * As we setup these structures, we collect all bio's together into a list
1800 * which we then process collectively to add pages, and then process again
1801 * to pass to generic_make_request.
1803 * The r10_bio structures are linked using a borrowed master_bio pointer.
1804 * This link is counted in ->remaining. When the r10_bio that points to NULL
1805 * has its remaining count decremented to 0, the whole complex operation
1806 * is complete.
1810 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1812 conf_t *conf = mddev->private;
1813 r10bio_t *r10_bio;
1814 struct bio *biolist = NULL, *bio;
1815 sector_t max_sector, nr_sectors;
1816 int disk;
1817 int i;
1818 int max_sync;
1819 int sync_blocks;
1821 sector_t sectors_skipped = 0;
1822 int chunks_skipped = 0;
1824 if (!conf->r10buf_pool)
1825 if (init_resync(conf))
1826 return 0;
1828 skipped:
1829 max_sector = mddev->dev_sectors;
1830 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1831 max_sector = mddev->resync_max_sectors;
1832 if (sector_nr >= max_sector) {
1833 /* If we aborted, we need to abort the
1834 * sync on the 'current' bitmap chucks (there can
1835 * be several when recovering multiple devices).
1836 * as we may have started syncing it but not finished.
1837 * We can find the current address in
1838 * mddev->curr_resync, but for recovery,
1839 * we need to convert that to several
1840 * virtual addresses.
1842 if (mddev->curr_resync < max_sector) { /* aborted */
1843 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1844 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1845 &sync_blocks, 1);
1846 else for (i=0; i<conf->raid_disks; i++) {
1847 sector_t sect =
1848 raid10_find_virt(conf, mddev->curr_resync, i);
1849 bitmap_end_sync(mddev->bitmap, sect,
1850 &sync_blocks, 1);
1852 } else /* completed sync */
1853 conf->fullsync = 0;
1855 bitmap_close_sync(mddev->bitmap);
1856 close_sync(conf);
1857 *skipped = 1;
1858 return sectors_skipped;
1860 if (chunks_skipped >= conf->raid_disks) {
1861 /* if there has been nothing to do on any drive,
1862 * then there is nothing to do at all..
1864 *skipped = 1;
1865 return (max_sector - sector_nr) + sectors_skipped;
1868 if (max_sector > mddev->resync_max)
1869 max_sector = mddev->resync_max; /* Don't do IO beyond here */
1871 /* make sure whole request will fit in a chunk - if chunks
1872 * are meaningful
1874 if (conf->near_copies < conf->raid_disks &&
1875 max_sector > (sector_nr | conf->chunk_mask))
1876 max_sector = (sector_nr | conf->chunk_mask) + 1;
1878 * If there is non-resync activity waiting for us then
1879 * put in a delay to throttle resync.
1881 if (!go_faster && conf->nr_waiting)
1882 msleep_interruptible(1000);
1884 /* Again, very different code for resync and recovery.
1885 * Both must result in an r10bio with a list of bios that
1886 * have bi_end_io, bi_sector, bi_bdev set,
1887 * and bi_private set to the r10bio.
1888 * For recovery, we may actually create several r10bios
1889 * with 2 bios in each, that correspond to the bios in the main one.
1890 * In this case, the subordinate r10bios link back through a
1891 * borrowed master_bio pointer, and the counter in the master
1892 * includes a ref from each subordinate.
1894 /* First, we decide what to do and set ->bi_end_io
1895 * To end_sync_read if we want to read, and
1896 * end_sync_write if we will want to write.
1899 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
1900 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1901 /* recovery... the complicated one */
1902 int j, k;
1903 r10_bio = NULL;
1905 for (i=0 ; i<conf->raid_disks; i++)
1906 if (conf->mirrors[i].rdev &&
1907 !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) {
1908 int still_degraded = 0;
1909 /* want to reconstruct this device */
1910 r10bio_t *rb2 = r10_bio;
1911 sector_t sect = raid10_find_virt(conf, sector_nr, i);
1912 int must_sync;
1913 /* Unless we are doing a full sync, we only need
1914 * to recover the block if it is set in the bitmap
1916 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1917 &sync_blocks, 1);
1918 if (sync_blocks < max_sync)
1919 max_sync = sync_blocks;
1920 if (!must_sync &&
1921 !conf->fullsync) {
1922 /* yep, skip the sync_blocks here, but don't assume
1923 * that there will never be anything to do here
1925 chunks_skipped = -1;
1926 continue;
1929 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1930 raise_barrier(conf, rb2 != NULL);
1931 atomic_set(&r10_bio->remaining, 0);
1933 r10_bio->master_bio = (struct bio*)rb2;
1934 if (rb2)
1935 atomic_inc(&rb2->remaining);
1936 r10_bio->mddev = mddev;
1937 set_bit(R10BIO_IsRecover, &r10_bio->state);
1938 r10_bio->sector = sect;
1940 raid10_find_phys(conf, r10_bio);
1942 /* Need to check if the array will still be
1943 * degraded
1945 for (j=0; j<conf->raid_disks; j++)
1946 if (conf->mirrors[j].rdev == NULL ||
1947 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
1948 still_degraded = 1;
1949 break;
1952 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1953 &sync_blocks, still_degraded);
1955 for (j=0; j<conf->copies;j++) {
1956 int d = r10_bio->devs[j].devnum;
1957 if (conf->mirrors[d].rdev &&
1958 test_bit(In_sync, &conf->mirrors[d].rdev->flags)) {
1959 /* This is where we read from */
1960 bio = r10_bio->devs[0].bio;
1961 bio->bi_next = biolist;
1962 biolist = bio;
1963 bio->bi_private = r10_bio;
1964 bio->bi_end_io = end_sync_read;
1965 bio->bi_rw = READ;
1966 bio->bi_sector = r10_bio->devs[j].addr +
1967 conf->mirrors[d].rdev->data_offset;
1968 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1969 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1970 atomic_inc(&r10_bio->remaining);
1971 /* and we write to 'i' */
1973 for (k=0; k<conf->copies; k++)
1974 if (r10_bio->devs[k].devnum == i)
1975 break;
1976 BUG_ON(k == conf->copies);
1977 bio = r10_bio->devs[1].bio;
1978 bio->bi_next = biolist;
1979 biolist = bio;
1980 bio->bi_private = r10_bio;
1981 bio->bi_end_io = end_sync_write;
1982 bio->bi_rw = WRITE;
1983 bio->bi_sector = r10_bio->devs[k].addr +
1984 conf->mirrors[i].rdev->data_offset;
1985 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1987 r10_bio->devs[0].devnum = d;
1988 r10_bio->devs[1].devnum = i;
1990 break;
1993 if (j == conf->copies) {
1994 /* Cannot recover, so abort the recovery */
1995 put_buf(r10_bio);
1996 if (rb2)
1997 atomic_dec(&rb2->remaining);
1998 r10_bio = rb2;
1999 if (!test_and_set_bit(MD_RECOVERY_INTR,
2000 &mddev->recovery))
2001 printk(KERN_INFO "raid10: %s: insufficient working devices for recovery.\n",
2002 mdname(mddev));
2003 break;
2006 if (biolist == NULL) {
2007 while (r10_bio) {
2008 r10bio_t *rb2 = r10_bio;
2009 r10_bio = (r10bio_t*) rb2->master_bio;
2010 rb2->master_bio = NULL;
2011 put_buf(rb2);
2013 goto giveup;
2015 } else {
2016 /* resync. Schedule a read for every block at this virt offset */
2017 int count = 0;
2019 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
2021 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2022 &sync_blocks, mddev->degraded) &&
2023 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2024 /* We can skip this block */
2025 *skipped = 1;
2026 return sync_blocks + sectors_skipped;
2028 if (sync_blocks < max_sync)
2029 max_sync = sync_blocks;
2030 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2032 r10_bio->mddev = mddev;
2033 atomic_set(&r10_bio->remaining, 0);
2034 raise_barrier(conf, 0);
2035 conf->next_resync = sector_nr;
2037 r10_bio->master_bio = NULL;
2038 r10_bio->sector = sector_nr;
2039 set_bit(R10BIO_IsSync, &r10_bio->state);
2040 raid10_find_phys(conf, r10_bio);
2041 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
2043 for (i=0; i<conf->copies; i++) {
2044 int d = r10_bio->devs[i].devnum;
2045 bio = r10_bio->devs[i].bio;
2046 bio->bi_end_io = NULL;
2047 clear_bit(BIO_UPTODATE, &bio->bi_flags);
2048 if (conf->mirrors[d].rdev == NULL ||
2049 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
2050 continue;
2051 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2052 atomic_inc(&r10_bio->remaining);
2053 bio->bi_next = biolist;
2054 biolist = bio;
2055 bio->bi_private = r10_bio;
2056 bio->bi_end_io = end_sync_read;
2057 bio->bi_rw = READ;
2058 bio->bi_sector = r10_bio->devs[i].addr +
2059 conf->mirrors[d].rdev->data_offset;
2060 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2061 count++;
2064 if (count < 2) {
2065 for (i=0; i<conf->copies; i++) {
2066 int d = r10_bio->devs[i].devnum;
2067 if (r10_bio->devs[i].bio->bi_end_io)
2068 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
2070 put_buf(r10_bio);
2071 biolist = NULL;
2072 goto giveup;
2076 for (bio = biolist; bio ; bio=bio->bi_next) {
2078 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
2079 if (bio->bi_end_io)
2080 bio->bi_flags |= 1 << BIO_UPTODATE;
2081 bio->bi_vcnt = 0;
2082 bio->bi_idx = 0;
2083 bio->bi_phys_segments = 0;
2084 bio->bi_size = 0;
2087 nr_sectors = 0;
2088 if (sector_nr + max_sync < max_sector)
2089 max_sector = sector_nr + max_sync;
2090 do {
2091 struct page *page;
2092 int len = PAGE_SIZE;
2093 disk = 0;
2094 if (sector_nr + (len>>9) > max_sector)
2095 len = (max_sector - sector_nr) << 9;
2096 if (len == 0)
2097 break;
2098 for (bio= biolist ; bio ; bio=bio->bi_next) {
2099 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
2100 if (bio_add_page(bio, page, len, 0) == 0) {
2101 /* stop here */
2102 struct bio *bio2;
2103 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
2104 for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
2105 /* remove last page from this bio */
2106 bio2->bi_vcnt--;
2107 bio2->bi_size -= len;
2108 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
2110 goto bio_full;
2112 disk = i;
2114 nr_sectors += len>>9;
2115 sector_nr += len>>9;
2116 } while (biolist->bi_vcnt < RESYNC_PAGES);
2117 bio_full:
2118 r10_bio->sectors = nr_sectors;
2120 while (biolist) {
2121 bio = biolist;
2122 biolist = biolist->bi_next;
2124 bio->bi_next = NULL;
2125 r10_bio = bio->bi_private;
2126 r10_bio->sectors = nr_sectors;
2128 if (bio->bi_end_io == end_sync_read) {
2129 md_sync_acct(bio->bi_bdev, nr_sectors);
2130 generic_make_request(bio);
2134 if (sectors_skipped)
2135 /* pretend they weren't skipped, it makes
2136 * no important difference in this case
2138 md_done_sync(mddev, sectors_skipped, 1);
2140 return sectors_skipped + nr_sectors;
2141 giveup:
2142 /* There is nowhere to write, so all non-sync
2143 * drives must be failed, so try the next chunk...
2145 if (sector_nr + max_sync < max_sector)
2146 max_sector = sector_nr + max_sync;
2148 sectors_skipped += (max_sector - sector_nr);
2149 chunks_skipped ++;
2150 sector_nr = max_sector;
2151 goto skipped;
2154 static sector_t
2155 raid10_size(mddev_t *mddev, sector_t sectors, int raid_disks)
2157 sector_t size;
2158 conf_t *conf = mddev->private;
2160 if (!raid_disks)
2161 raid_disks = mddev->raid_disks;
2162 if (!sectors)
2163 sectors = mddev->dev_sectors;
2165 size = sectors >> conf->chunk_shift;
2166 sector_div(size, conf->far_copies);
2167 size = size * raid_disks;
2168 sector_div(size, conf->near_copies);
2170 return size << conf->chunk_shift;
2173 static int run(mddev_t *mddev)
2175 conf_t *conf;
2176 int i, disk_idx, chunk_size;
2177 mirror_info_t *disk;
2178 mdk_rdev_t *rdev;
2179 int nc, fc, fo;
2180 sector_t stride, size;
2182 if (mddev->chunk_sectors < (PAGE_SIZE >> 9) ||
2183 !is_power_of_2(mddev->chunk_sectors)) {
2184 printk(KERN_ERR "md/raid10: chunk size must be "
2185 "at least PAGE_SIZE(%ld) and be a power of 2.\n", PAGE_SIZE);
2186 return -EINVAL;
2189 nc = mddev->layout & 255;
2190 fc = (mddev->layout >> 8) & 255;
2191 fo = mddev->layout & (1<<16);
2192 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
2193 (mddev->layout >> 17)) {
2194 printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n",
2195 mdname(mddev), mddev->layout);
2196 goto out;
2199 * copy the already verified devices into our private RAID10
2200 * bookkeeping area. [whatever we allocate in run(),
2201 * should be freed in stop()]
2203 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
2204 mddev->private = conf;
2205 if (!conf) {
2206 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2207 mdname(mddev));
2208 goto out;
2210 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
2211 GFP_KERNEL);
2212 if (!conf->mirrors) {
2213 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2214 mdname(mddev));
2215 goto out_free_conf;
2218 conf->tmppage = alloc_page(GFP_KERNEL);
2219 if (!conf->tmppage)
2220 goto out_free_conf;
2222 conf->raid_disks = mddev->raid_disks;
2223 conf->near_copies = nc;
2224 conf->far_copies = fc;
2225 conf->copies = nc*fc;
2226 conf->far_offset = fo;
2227 conf->chunk_mask = mddev->chunk_sectors - 1;
2228 conf->chunk_shift = ffz(~mddev->chunk_sectors);
2229 size = mddev->dev_sectors >> conf->chunk_shift;
2230 sector_div(size, fc);
2231 size = size * conf->raid_disks;
2232 sector_div(size, nc);
2233 /* 'size' is now the number of chunks in the array */
2234 /* calculate "used chunks per device" in 'stride' */
2235 stride = size * conf->copies;
2237 /* We need to round up when dividing by raid_disks to
2238 * get the stride size.
2240 stride += conf->raid_disks - 1;
2241 sector_div(stride, conf->raid_disks);
2242 mddev->dev_sectors = stride << conf->chunk_shift;
2244 if (fo)
2245 stride = 1;
2246 else
2247 sector_div(stride, fc);
2248 conf->stride = stride << conf->chunk_shift;
2250 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2251 r10bio_pool_free, conf);
2252 if (!conf->r10bio_pool) {
2253 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2254 mdname(mddev));
2255 goto out_free_conf;
2258 conf->mddev = mddev;
2259 spin_lock_init(&conf->device_lock);
2260 mddev->queue->queue_lock = &conf->device_lock;
2262 chunk_size = mddev->chunk_sectors << 9;
2263 blk_queue_io_min(mddev->queue, chunk_size);
2264 if (conf->raid_disks % conf->near_copies)
2265 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
2266 else
2267 blk_queue_io_opt(mddev->queue, chunk_size *
2268 (conf->raid_disks / conf->near_copies));
2270 list_for_each_entry(rdev, &mddev->disks, same_set) {
2271 disk_idx = rdev->raid_disk;
2272 if (disk_idx >= mddev->raid_disks
2273 || disk_idx < 0)
2274 continue;
2275 disk = conf->mirrors + disk_idx;
2277 disk->rdev = rdev;
2278 disk_stack_limits(mddev->gendisk, rdev->bdev,
2279 rdev->data_offset << 9);
2280 /* as we don't honour merge_bvec_fn, we must never risk
2281 * violating it, so limit max_segments to 1 lying
2282 * within a single page.
2284 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2285 blk_queue_max_segments(mddev->queue, 1);
2286 blk_queue_segment_boundary(mddev->queue,
2287 PAGE_CACHE_SIZE - 1);
2290 disk->head_position = 0;
2292 INIT_LIST_HEAD(&conf->retry_list);
2294 spin_lock_init(&conf->resync_lock);
2295 init_waitqueue_head(&conf->wait_barrier);
2297 /* need to check that every block has at least one working mirror */
2298 if (!enough(conf)) {
2299 printk(KERN_ERR "raid10: not enough operational mirrors for %s\n",
2300 mdname(mddev));
2301 goto out_free_conf;
2304 mddev->degraded = 0;
2305 for (i = 0; i < conf->raid_disks; i++) {
2307 disk = conf->mirrors + i;
2309 if (!disk->rdev ||
2310 !test_bit(In_sync, &disk->rdev->flags)) {
2311 disk->head_position = 0;
2312 mddev->degraded++;
2313 if (disk->rdev)
2314 conf->fullsync = 1;
2319 mddev->thread = md_register_thread(raid10d, mddev, NULL);
2320 if (!mddev->thread) {
2321 printk(KERN_ERR
2322 "raid10: couldn't allocate thread for %s\n",
2323 mdname(mddev));
2324 goto out_free_conf;
2327 if (mddev->recovery_cp != MaxSector)
2328 printk(KERN_NOTICE "raid10: %s is not clean"
2329 " -- starting background reconstruction\n",
2330 mdname(mddev));
2331 printk(KERN_INFO
2332 "raid10: raid set %s active with %d out of %d devices\n",
2333 mdname(mddev), mddev->raid_disks - mddev->degraded,
2334 mddev->raid_disks);
2336 * Ok, everything is just fine now
2338 md_set_array_sectors(mddev, raid10_size(mddev, 0, 0));
2339 mddev->resync_max_sectors = raid10_size(mddev, 0, 0);
2341 mddev->queue->unplug_fn = raid10_unplug;
2342 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2343 mddev->queue->backing_dev_info.congested_data = mddev;
2345 /* Calculate max read-ahead size.
2346 * We need to readahead at least twice a whole stripe....
2347 * maybe...
2350 int stripe = conf->raid_disks *
2351 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
2352 stripe /= conf->near_copies;
2353 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2354 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2357 if (conf->near_copies < mddev->raid_disks)
2358 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
2359 md_integrity_register(mddev);
2360 return 0;
2362 out_free_conf:
2363 if (conf->r10bio_pool)
2364 mempool_destroy(conf->r10bio_pool);
2365 safe_put_page(conf->tmppage);
2366 kfree(conf->mirrors);
2367 kfree(conf);
2368 mddev->private = NULL;
2369 out:
2370 return -EIO;
2373 static int stop(mddev_t *mddev)
2375 conf_t *conf = mddev->private;
2377 raise_barrier(conf, 0);
2378 lower_barrier(conf);
2380 md_unregister_thread(mddev->thread);
2381 mddev->thread = NULL;
2382 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2383 if (conf->r10bio_pool)
2384 mempool_destroy(conf->r10bio_pool);
2385 kfree(conf->mirrors);
2386 kfree(conf);
2387 mddev->private = NULL;
2388 return 0;
2391 static void raid10_quiesce(mddev_t *mddev, int state)
2393 conf_t *conf = mddev->private;
2395 switch(state) {
2396 case 1:
2397 raise_barrier(conf, 0);
2398 break;
2399 case 0:
2400 lower_barrier(conf);
2401 break;
2405 static struct mdk_personality raid10_personality =
2407 .name = "raid10",
2408 .level = 10,
2409 .owner = THIS_MODULE,
2410 .make_request = make_request,
2411 .run = run,
2412 .stop = stop,
2413 .status = status,
2414 .error_handler = error,
2415 .hot_add_disk = raid10_add_disk,
2416 .hot_remove_disk= raid10_remove_disk,
2417 .spare_active = raid10_spare_active,
2418 .sync_request = sync_request,
2419 .quiesce = raid10_quiesce,
2420 .size = raid10_size,
2423 static int __init raid_init(void)
2425 return register_md_personality(&raid10_personality);
2428 static void raid_exit(void)
2430 unregister_md_personality(&raid10_personality);
2433 module_init(raid_init);
2434 module_exit(raid_exit);
2435 MODULE_LICENSE("GPL");
2436 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
2437 MODULE_ALIAS("md-personality-9"); /* RAID10 */
2438 MODULE_ALIAS("md-raid10");
2439 MODULE_ALIAS("md-level-10");