parport: netmos 9845 & 9855 1P4S fixes
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / md / raid1.c
blob4a25fa99aed5c69910a2dc4b65635d88839b5890
1 /*
2 * raid1.c : Multiple Devices driver for Linux
4 * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
6 * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
8 * RAID-1 management functions.
10 * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000
12 * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk>
13 * Various fixes by Neil Brown <neilb@cse.unsw.edu.au>
15 * Changes by Peter T. Breuer <ptb@it.uc3m.es> 31/1/2003 to support
16 * bitmapped intelligence in resync:
18 * - bitmap marked during normal i/o
19 * - bitmap used to skip nondirty blocks during sync
21 * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology:
22 * - persistent bitmap code
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2, or (at your option)
27 * any later version.
29 * You should have received a copy of the GNU General Public License
30 * (for example /usr/src/linux/COPYING); if not, write to the Free
31 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include "dm-bio-list.h"
35 #include <linux/delay.h>
36 #include <linux/raid/raid1.h>
37 #include <linux/raid/bitmap.h>
39 #define DEBUG 0
40 #if DEBUG
41 #define PRINTK(x...) printk(x)
42 #else
43 #define PRINTK(x...)
44 #endif
47 * Number of guaranteed r1bios in case of extreme VM load:
49 #define NR_RAID1_BIOS 256
52 static void unplug_slaves(mddev_t *mddev);
54 static void allow_barrier(conf_t *conf);
55 static void lower_barrier(conf_t *conf);
57 static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data)
59 struct pool_info *pi = data;
60 r1bio_t *r1_bio;
61 int size = offsetof(r1bio_t, bios[pi->raid_disks]);
63 /* allocate a r1bio with room for raid_disks entries in the bios array */
64 r1_bio = kzalloc(size, gfp_flags);
65 if (!r1_bio)
66 unplug_slaves(pi->mddev);
68 return r1_bio;
71 static void r1bio_pool_free(void *r1_bio, void *data)
73 kfree(r1_bio);
76 #define RESYNC_BLOCK_SIZE (64*1024)
77 //#define RESYNC_BLOCK_SIZE PAGE_SIZE
78 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
79 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
80 #define RESYNC_WINDOW (2048*1024)
82 static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
84 struct pool_info *pi = data;
85 struct page *page;
86 r1bio_t *r1_bio;
87 struct bio *bio;
88 int i, j;
90 r1_bio = r1bio_pool_alloc(gfp_flags, pi);
91 if (!r1_bio) {
92 unplug_slaves(pi->mddev);
93 return NULL;
97 * Allocate bios : 1 for reading, n-1 for writing
99 for (j = pi->raid_disks ; j-- ; ) {
100 bio = bio_alloc(gfp_flags, RESYNC_PAGES);
101 if (!bio)
102 goto out_free_bio;
103 r1_bio->bios[j] = bio;
106 * Allocate RESYNC_PAGES data pages and attach them to
107 * the first bio.
108 * If this is a user-requested check/repair, allocate
109 * RESYNC_PAGES for each bio.
111 if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery))
112 j = pi->raid_disks;
113 else
114 j = 1;
115 while(j--) {
116 bio = r1_bio->bios[j];
117 for (i = 0; i < RESYNC_PAGES; i++) {
118 page = alloc_page(gfp_flags);
119 if (unlikely(!page))
120 goto out_free_pages;
122 bio->bi_io_vec[i].bv_page = page;
123 bio->bi_vcnt = i+1;
126 /* If not user-requests, copy the page pointers to all bios */
127 if (!test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) {
128 for (i=0; i<RESYNC_PAGES ; i++)
129 for (j=1; j<pi->raid_disks; j++)
130 r1_bio->bios[j]->bi_io_vec[i].bv_page =
131 r1_bio->bios[0]->bi_io_vec[i].bv_page;
134 r1_bio->master_bio = NULL;
136 return r1_bio;
138 out_free_pages:
139 for (j=0 ; j < pi->raid_disks; j++)
140 for (i=0; i < r1_bio->bios[j]->bi_vcnt ; i++)
141 put_page(r1_bio->bios[j]->bi_io_vec[i].bv_page);
142 j = -1;
143 out_free_bio:
144 while ( ++j < pi->raid_disks )
145 bio_put(r1_bio->bios[j]);
146 r1bio_pool_free(r1_bio, data);
147 return NULL;
150 static void r1buf_pool_free(void *__r1_bio, void *data)
152 struct pool_info *pi = data;
153 int i,j;
154 r1bio_t *r1bio = __r1_bio;
156 for (i = 0; i < RESYNC_PAGES; i++)
157 for (j = pi->raid_disks; j-- ;) {
158 if (j == 0 ||
159 r1bio->bios[j]->bi_io_vec[i].bv_page !=
160 r1bio->bios[0]->bi_io_vec[i].bv_page)
161 safe_put_page(r1bio->bios[j]->bi_io_vec[i].bv_page);
163 for (i=0 ; i < pi->raid_disks; i++)
164 bio_put(r1bio->bios[i]);
166 r1bio_pool_free(r1bio, data);
169 static void put_all_bios(conf_t *conf, r1bio_t *r1_bio)
171 int i;
173 for (i = 0; i < conf->raid_disks; i++) {
174 struct bio **bio = r1_bio->bios + i;
175 if (*bio && *bio != IO_BLOCKED)
176 bio_put(*bio);
177 *bio = NULL;
181 static void free_r1bio(r1bio_t *r1_bio)
183 conf_t *conf = mddev_to_conf(r1_bio->mddev);
186 * Wake up any possible resync thread that waits for the device
187 * to go idle.
189 allow_barrier(conf);
191 put_all_bios(conf, r1_bio);
192 mempool_free(r1_bio, conf->r1bio_pool);
195 static void put_buf(r1bio_t *r1_bio)
197 conf_t *conf = mddev_to_conf(r1_bio->mddev);
198 int i;
200 for (i=0; i<conf->raid_disks; i++) {
201 struct bio *bio = r1_bio->bios[i];
202 if (bio->bi_end_io)
203 rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev);
206 mempool_free(r1_bio, conf->r1buf_pool);
208 lower_barrier(conf);
211 static void reschedule_retry(r1bio_t *r1_bio)
213 unsigned long flags;
214 mddev_t *mddev = r1_bio->mddev;
215 conf_t *conf = mddev_to_conf(mddev);
217 spin_lock_irqsave(&conf->device_lock, flags);
218 list_add(&r1_bio->retry_list, &conf->retry_list);
219 conf->nr_queued ++;
220 spin_unlock_irqrestore(&conf->device_lock, flags);
222 wake_up(&conf->wait_barrier);
223 md_wakeup_thread(mddev->thread);
227 * raid_end_bio_io() is called when we have finished servicing a mirrored
228 * operation and are ready to return a success/failure code to the buffer
229 * cache layer.
231 static void raid_end_bio_io(r1bio_t *r1_bio)
233 struct bio *bio = r1_bio->master_bio;
235 /* if nobody has done the final endio yet, do it now */
236 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
237 PRINTK(KERN_DEBUG "raid1: sync end %s on sectors %llu-%llu\n",
238 (bio_data_dir(bio) == WRITE) ? "write" : "read",
239 (unsigned long long) bio->bi_sector,
240 (unsigned long long) bio->bi_sector +
241 (bio->bi_size >> 9) - 1);
243 bio_endio(bio,
244 test_bit(R1BIO_Uptodate, &r1_bio->state) ? 0 : -EIO);
246 free_r1bio(r1_bio);
250 * Update disk head position estimator based on IRQ completion info.
252 static inline void update_head_pos(int disk, r1bio_t *r1_bio)
254 conf_t *conf = mddev_to_conf(r1_bio->mddev);
256 conf->mirrors[disk].head_position =
257 r1_bio->sector + (r1_bio->sectors);
260 static void raid1_end_read_request(struct bio *bio, int error)
262 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
263 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
264 int mirror;
265 conf_t *conf = mddev_to_conf(r1_bio->mddev);
267 mirror = r1_bio->read_disk;
269 * this branch is our 'one mirror IO has finished' event handler:
271 update_head_pos(mirror, r1_bio);
273 if (uptodate)
274 set_bit(R1BIO_Uptodate, &r1_bio->state);
275 else {
276 /* If all other devices have failed, we want to return
277 * the error upwards rather than fail the last device.
278 * Here we redefine "uptodate" to mean "Don't want to retry"
280 unsigned long flags;
281 spin_lock_irqsave(&conf->device_lock, flags);
282 if (r1_bio->mddev->degraded == conf->raid_disks ||
283 (r1_bio->mddev->degraded == conf->raid_disks-1 &&
284 !test_bit(Faulty, &conf->mirrors[mirror].rdev->flags)))
285 uptodate = 1;
286 spin_unlock_irqrestore(&conf->device_lock, flags);
289 if (uptodate)
290 raid_end_bio_io(r1_bio);
291 else {
293 * oops, read error:
295 char b[BDEVNAME_SIZE];
296 if (printk_ratelimit())
297 printk(KERN_ERR "raid1: %s: rescheduling sector %llu\n",
298 bdevname(conf->mirrors[mirror].rdev->bdev,b), (unsigned long long)r1_bio->sector);
299 reschedule_retry(r1_bio);
302 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
305 static void raid1_end_write_request(struct bio *bio, int error)
307 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
308 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
309 int mirror, behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
310 conf_t *conf = mddev_to_conf(r1_bio->mddev);
311 struct bio *to_put = NULL;
314 for (mirror = 0; mirror < conf->raid_disks; mirror++)
315 if (r1_bio->bios[mirror] == bio)
316 break;
318 if (error == -EOPNOTSUPP && test_bit(R1BIO_Barrier, &r1_bio->state)) {
319 set_bit(BarriersNotsupp, &conf->mirrors[mirror].rdev->flags);
320 set_bit(R1BIO_BarrierRetry, &r1_bio->state);
321 r1_bio->mddev->barriers_work = 0;
322 /* Don't rdev_dec_pending in this branch - keep it for the retry */
323 } else {
325 * this branch is our 'one mirror IO has finished' event handler:
327 r1_bio->bios[mirror] = NULL;
328 to_put = bio;
329 if (!uptodate) {
330 md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
331 /* an I/O failed, we can't clear the bitmap */
332 set_bit(R1BIO_Degraded, &r1_bio->state);
333 } else
335 * Set R1BIO_Uptodate in our master bio, so that
336 * we will return a good error code for to the higher
337 * levels even if IO on some other mirrored buffer fails.
339 * The 'master' represents the composite IO operation to
340 * user-side. So if something waits for IO, then it will
341 * wait for the 'master' bio.
343 set_bit(R1BIO_Uptodate, &r1_bio->state);
345 update_head_pos(mirror, r1_bio);
347 if (behind) {
348 if (test_bit(WriteMostly, &conf->mirrors[mirror].rdev->flags))
349 atomic_dec(&r1_bio->behind_remaining);
351 /* In behind mode, we ACK the master bio once the I/O has safely
352 * reached all non-writemostly disks. Setting the Returned bit
353 * ensures that this gets done only once -- we don't ever want to
354 * return -EIO here, instead we'll wait */
356 if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
357 test_bit(R1BIO_Uptodate, &r1_bio->state)) {
358 /* Maybe we can return now */
359 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
360 struct bio *mbio = r1_bio->master_bio;
361 PRINTK(KERN_DEBUG "raid1: behind end write sectors %llu-%llu\n",
362 (unsigned long long) mbio->bi_sector,
363 (unsigned long long) mbio->bi_sector +
364 (mbio->bi_size >> 9) - 1);
365 bio_endio(mbio, 0);
369 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
373 * Let's see if all mirrored write operations have finished
374 * already.
376 if (atomic_dec_and_test(&r1_bio->remaining)) {
377 if (test_bit(R1BIO_BarrierRetry, &r1_bio->state))
378 reschedule_retry(r1_bio);
379 else {
380 /* it really is the end of this request */
381 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
382 /* free extra copy of the data pages */
383 int i = bio->bi_vcnt;
384 while (i--)
385 safe_put_page(bio->bi_io_vec[i].bv_page);
387 /* clear the bitmap if all writes complete successfully */
388 bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
389 r1_bio->sectors,
390 !test_bit(R1BIO_Degraded, &r1_bio->state),
391 behind);
392 md_write_end(r1_bio->mddev);
393 raid_end_bio_io(r1_bio);
397 if (to_put)
398 bio_put(to_put);
403 * This routine returns the disk from which the requested read should
404 * be done. There is a per-array 'next expected sequential IO' sector
405 * number - if this matches on the next IO then we use the last disk.
406 * There is also a per-disk 'last know head position' sector that is
407 * maintained from IRQ contexts, both the normal and the resync IO
408 * completion handlers update this position correctly. If there is no
409 * perfect sequential match then we pick the disk whose head is closest.
411 * If there are 2 mirrors in the same 2 devices, performance degrades
412 * because position is mirror, not device based.
414 * The rdev for the device selected will have nr_pending incremented.
416 static int read_balance(conf_t *conf, r1bio_t *r1_bio)
418 const unsigned long this_sector = r1_bio->sector;
419 int new_disk = conf->last_used, disk = new_disk;
420 int wonly_disk = -1;
421 const int sectors = r1_bio->sectors;
422 sector_t new_distance, current_distance;
423 mdk_rdev_t *rdev;
425 rcu_read_lock();
427 * Check if we can balance. We can balance on the whole
428 * device if no resync is going on, or below the resync window.
429 * We take the first readable disk when above the resync window.
431 retry:
432 if (conf->mddev->recovery_cp < MaxSector &&
433 (this_sector + sectors >= conf->next_resync)) {
434 /* Choose the first operation device, for consistancy */
435 new_disk = 0;
437 for (rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
438 r1_bio->bios[new_disk] == IO_BLOCKED ||
439 !rdev || !test_bit(In_sync, &rdev->flags)
440 || test_bit(WriteMostly, &rdev->flags);
441 rdev = rcu_dereference(conf->mirrors[++new_disk].rdev)) {
443 if (rdev && test_bit(In_sync, &rdev->flags) &&
444 r1_bio->bios[new_disk] != IO_BLOCKED)
445 wonly_disk = new_disk;
447 if (new_disk == conf->raid_disks - 1) {
448 new_disk = wonly_disk;
449 break;
452 goto rb_out;
456 /* make sure the disk is operational */
457 for (rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
458 r1_bio->bios[new_disk] == IO_BLOCKED ||
459 !rdev || !test_bit(In_sync, &rdev->flags) ||
460 test_bit(WriteMostly, &rdev->flags);
461 rdev = rcu_dereference(conf->mirrors[new_disk].rdev)) {
463 if (rdev && test_bit(In_sync, &rdev->flags) &&
464 r1_bio->bios[new_disk] != IO_BLOCKED)
465 wonly_disk = new_disk;
467 if (new_disk <= 0)
468 new_disk = conf->raid_disks;
469 new_disk--;
470 if (new_disk == disk) {
471 new_disk = wonly_disk;
472 break;
476 if (new_disk < 0)
477 goto rb_out;
479 disk = new_disk;
480 /* now disk == new_disk == starting point for search */
483 * Don't change to another disk for sequential reads:
485 if (conf->next_seq_sect == this_sector)
486 goto rb_out;
487 if (this_sector == conf->mirrors[new_disk].head_position)
488 goto rb_out;
490 current_distance = abs(this_sector - conf->mirrors[disk].head_position);
492 /* Find the disk whose head is closest */
494 do {
495 if (disk <= 0)
496 disk = conf->raid_disks;
497 disk--;
499 rdev = rcu_dereference(conf->mirrors[disk].rdev);
501 if (!rdev || r1_bio->bios[disk] == IO_BLOCKED ||
502 !test_bit(In_sync, &rdev->flags) ||
503 test_bit(WriteMostly, &rdev->flags))
504 continue;
506 if (!atomic_read(&rdev->nr_pending)) {
507 new_disk = disk;
508 break;
510 new_distance = abs(this_sector - conf->mirrors[disk].head_position);
511 if (new_distance < current_distance) {
512 current_distance = new_distance;
513 new_disk = disk;
515 } while (disk != conf->last_used);
517 rb_out:
520 if (new_disk >= 0) {
521 rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
522 if (!rdev)
523 goto retry;
524 atomic_inc(&rdev->nr_pending);
525 if (!test_bit(In_sync, &rdev->flags)) {
526 /* cannot risk returning a device that failed
527 * before we inc'ed nr_pending
529 rdev_dec_pending(rdev, conf->mddev);
530 goto retry;
532 conf->next_seq_sect = this_sector + sectors;
533 conf->last_used = new_disk;
535 rcu_read_unlock();
537 return new_disk;
540 static void unplug_slaves(mddev_t *mddev)
542 conf_t *conf = mddev_to_conf(mddev);
543 int i;
545 rcu_read_lock();
546 for (i=0; i<mddev->raid_disks; i++) {
547 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
548 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
549 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
551 atomic_inc(&rdev->nr_pending);
552 rcu_read_unlock();
554 blk_unplug(r_queue);
556 rdev_dec_pending(rdev, mddev);
557 rcu_read_lock();
560 rcu_read_unlock();
563 static void raid1_unplug(struct request_queue *q)
565 mddev_t *mddev = q->queuedata;
567 unplug_slaves(mddev);
568 md_wakeup_thread(mddev->thread);
571 static int raid1_congested(void *data, int bits)
573 mddev_t *mddev = data;
574 conf_t *conf = mddev_to_conf(mddev);
575 int i, ret = 0;
577 rcu_read_lock();
578 for (i = 0; i < mddev->raid_disks; i++) {
579 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
580 if (rdev && !test_bit(Faulty, &rdev->flags)) {
581 struct request_queue *q = bdev_get_queue(rdev->bdev);
583 /* Note the '|| 1' - when read_balance prefers
584 * non-congested targets, it can be removed
586 if ((bits & (1<<BDI_write_congested)) || 1)
587 ret |= bdi_congested(&q->backing_dev_info, bits);
588 else
589 ret &= bdi_congested(&q->backing_dev_info, bits);
592 rcu_read_unlock();
593 return ret;
597 static int flush_pending_writes(conf_t *conf)
599 /* Any writes that have been queued but are awaiting
600 * bitmap updates get flushed here.
601 * We return 1 if any requests were actually submitted.
603 int rv = 0;
605 spin_lock_irq(&conf->device_lock);
607 if (conf->pending_bio_list.head) {
608 struct bio *bio;
609 bio = bio_list_get(&conf->pending_bio_list);
610 blk_remove_plug(conf->mddev->queue);
611 spin_unlock_irq(&conf->device_lock);
612 /* flush any pending bitmap writes to
613 * disk before proceeding w/ I/O */
614 bitmap_unplug(conf->mddev->bitmap);
616 while (bio) { /* submit pending writes */
617 struct bio *next = bio->bi_next;
618 bio->bi_next = NULL;
619 generic_make_request(bio);
620 bio = next;
622 rv = 1;
623 } else
624 spin_unlock_irq(&conf->device_lock);
625 return rv;
628 /* Barriers....
629 * Sometimes we need to suspend IO while we do something else,
630 * either some resync/recovery, or reconfigure the array.
631 * To do this we raise a 'barrier'.
632 * The 'barrier' is a counter that can be raised multiple times
633 * to count how many activities are happening which preclude
634 * normal IO.
635 * We can only raise the barrier if there is no pending IO.
636 * i.e. if nr_pending == 0.
637 * We choose only to raise the barrier if no-one is waiting for the
638 * barrier to go down. This means that as soon as an IO request
639 * is ready, no other operations which require a barrier will start
640 * until the IO request has had a chance.
642 * So: regular IO calls 'wait_barrier'. When that returns there
643 * is no backgroup IO happening, It must arrange to call
644 * allow_barrier when it has finished its IO.
645 * backgroup IO calls must call raise_barrier. Once that returns
646 * there is no normal IO happeing. It must arrange to call
647 * lower_barrier when the particular background IO completes.
649 #define RESYNC_DEPTH 32
651 static void raise_barrier(conf_t *conf)
653 spin_lock_irq(&conf->resync_lock);
655 /* Wait until no block IO is waiting */
656 wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
657 conf->resync_lock,
658 raid1_unplug(conf->mddev->queue));
660 /* block any new IO from starting */
661 conf->barrier++;
663 /* No wait for all pending IO to complete */
664 wait_event_lock_irq(conf->wait_barrier,
665 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
666 conf->resync_lock,
667 raid1_unplug(conf->mddev->queue));
669 spin_unlock_irq(&conf->resync_lock);
672 static void lower_barrier(conf_t *conf)
674 unsigned long flags;
675 spin_lock_irqsave(&conf->resync_lock, flags);
676 conf->barrier--;
677 spin_unlock_irqrestore(&conf->resync_lock, flags);
678 wake_up(&conf->wait_barrier);
681 static void wait_barrier(conf_t *conf)
683 spin_lock_irq(&conf->resync_lock);
684 if (conf->barrier) {
685 conf->nr_waiting++;
686 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
687 conf->resync_lock,
688 raid1_unplug(conf->mddev->queue));
689 conf->nr_waiting--;
691 conf->nr_pending++;
692 spin_unlock_irq(&conf->resync_lock);
695 static void allow_barrier(conf_t *conf)
697 unsigned long flags;
698 spin_lock_irqsave(&conf->resync_lock, flags);
699 conf->nr_pending--;
700 spin_unlock_irqrestore(&conf->resync_lock, flags);
701 wake_up(&conf->wait_barrier);
704 static void freeze_array(conf_t *conf)
706 /* stop syncio and normal IO and wait for everything to
707 * go quite.
708 * We increment barrier and nr_waiting, and then
709 * wait until nr_pending match nr_queued+1
710 * This is called in the context of one normal IO request
711 * that has failed. Thus any sync request that might be pending
712 * will be blocked by nr_pending, and we need to wait for
713 * pending IO requests to complete or be queued for re-try.
714 * Thus the number queued (nr_queued) plus this request (1)
715 * must match the number of pending IOs (nr_pending) before
716 * we continue.
718 spin_lock_irq(&conf->resync_lock);
719 conf->barrier++;
720 conf->nr_waiting++;
721 wait_event_lock_irq(conf->wait_barrier,
722 conf->nr_pending == conf->nr_queued+1,
723 conf->resync_lock,
724 ({ flush_pending_writes(conf);
725 raid1_unplug(conf->mddev->queue); }));
726 spin_unlock_irq(&conf->resync_lock);
728 static void unfreeze_array(conf_t *conf)
730 /* reverse the effect of the freeze */
731 spin_lock_irq(&conf->resync_lock);
732 conf->barrier--;
733 conf->nr_waiting--;
734 wake_up(&conf->wait_barrier);
735 spin_unlock_irq(&conf->resync_lock);
739 /* duplicate the data pages for behind I/O */
740 static struct page **alloc_behind_pages(struct bio *bio)
742 int i;
743 struct bio_vec *bvec;
744 struct page **pages = kzalloc(bio->bi_vcnt * sizeof(struct page *),
745 GFP_NOIO);
746 if (unlikely(!pages))
747 goto do_sync_io;
749 bio_for_each_segment(bvec, bio, i) {
750 pages[i] = alloc_page(GFP_NOIO);
751 if (unlikely(!pages[i]))
752 goto do_sync_io;
753 memcpy(kmap(pages[i]) + bvec->bv_offset,
754 kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
755 kunmap(pages[i]);
756 kunmap(bvec->bv_page);
759 return pages;
761 do_sync_io:
762 if (pages)
763 for (i = 0; i < bio->bi_vcnt && pages[i]; i++)
764 put_page(pages[i]);
765 kfree(pages);
766 PRINTK("%dB behind alloc failed, doing sync I/O\n", bio->bi_size);
767 return NULL;
770 static int make_request(struct request_queue *q, struct bio * bio)
772 mddev_t *mddev = q->queuedata;
773 conf_t *conf = mddev_to_conf(mddev);
774 mirror_info_t *mirror;
775 r1bio_t *r1_bio;
776 struct bio *read_bio;
777 int i, targets = 0, disks;
778 struct bitmap *bitmap;
779 unsigned long flags;
780 struct bio_list bl;
781 struct page **behind_pages = NULL;
782 const int rw = bio_data_dir(bio);
783 const int do_sync = bio_sync(bio);
784 int cpu, do_barriers;
785 mdk_rdev_t *blocked_rdev;
788 * Register the new request and wait if the reconstruction
789 * thread has put up a bar for new requests.
790 * Continue immediately if no resync is active currently.
791 * We test barriers_work *after* md_write_start as md_write_start
792 * may cause the first superblock write, and that will check out
793 * if barriers work.
796 md_write_start(mddev, bio); /* wait on superblock update early */
798 if (unlikely(!mddev->barriers_work && bio_barrier(bio))) {
799 if (rw == WRITE)
800 md_write_end(mddev);
801 bio_endio(bio, -EOPNOTSUPP);
802 return 0;
805 wait_barrier(conf);
807 bitmap = mddev->bitmap;
809 cpu = part_stat_lock();
810 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
811 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
812 bio_sectors(bio));
813 part_stat_unlock();
816 * make_request() can abort the operation when READA is being
817 * used and no empty request is available.
820 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
822 r1_bio->master_bio = bio;
823 r1_bio->sectors = bio->bi_size >> 9;
824 r1_bio->state = 0;
825 r1_bio->mddev = mddev;
826 r1_bio->sector = bio->bi_sector;
828 if (rw == READ) {
830 * read balancing logic:
832 int rdisk = read_balance(conf, r1_bio);
834 if (rdisk < 0) {
835 /* couldn't find anywhere to read from */
836 raid_end_bio_io(r1_bio);
837 return 0;
839 mirror = conf->mirrors + rdisk;
841 r1_bio->read_disk = rdisk;
843 read_bio = bio_clone(bio, GFP_NOIO);
845 r1_bio->bios[rdisk] = read_bio;
847 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
848 read_bio->bi_bdev = mirror->rdev->bdev;
849 read_bio->bi_end_io = raid1_end_read_request;
850 read_bio->bi_rw = READ | do_sync;
851 read_bio->bi_private = r1_bio;
853 generic_make_request(read_bio);
854 return 0;
858 * WRITE:
860 /* first select target devices under spinlock and
861 * inc refcount on their rdev. Record them by setting
862 * bios[x] to bio
864 disks = conf->raid_disks;
865 #if 0
866 { static int first=1;
867 if (first) printk("First Write sector %llu disks %d\n",
868 (unsigned long long)r1_bio->sector, disks);
869 first = 0;
871 #endif
872 retry_write:
873 blocked_rdev = NULL;
874 rcu_read_lock();
875 for (i = 0; i < disks; i++) {
876 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
877 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
878 atomic_inc(&rdev->nr_pending);
879 blocked_rdev = rdev;
880 break;
882 if (rdev && !test_bit(Faulty, &rdev->flags)) {
883 atomic_inc(&rdev->nr_pending);
884 if (test_bit(Faulty, &rdev->flags)) {
885 rdev_dec_pending(rdev, mddev);
886 r1_bio->bios[i] = NULL;
887 } else
888 r1_bio->bios[i] = bio;
889 targets++;
890 } else
891 r1_bio->bios[i] = NULL;
893 rcu_read_unlock();
895 if (unlikely(blocked_rdev)) {
896 /* Wait for this device to become unblocked */
897 int j;
899 for (j = 0; j < i; j++)
900 if (r1_bio->bios[j])
901 rdev_dec_pending(conf->mirrors[j].rdev, mddev);
903 allow_barrier(conf);
904 md_wait_for_blocked_rdev(blocked_rdev, mddev);
905 wait_barrier(conf);
906 goto retry_write;
909 BUG_ON(targets == 0); /* we never fail the last device */
911 if (targets < conf->raid_disks) {
912 /* array is degraded, we will not clear the bitmap
913 * on I/O completion (see raid1_end_write_request) */
914 set_bit(R1BIO_Degraded, &r1_bio->state);
917 /* do behind I/O ? */
918 if (bitmap &&
919 atomic_read(&bitmap->behind_writes) < bitmap->max_write_behind &&
920 (behind_pages = alloc_behind_pages(bio)) != NULL)
921 set_bit(R1BIO_BehindIO, &r1_bio->state);
923 atomic_set(&r1_bio->remaining, 0);
924 atomic_set(&r1_bio->behind_remaining, 0);
926 do_barriers = bio_barrier(bio);
927 if (do_barriers)
928 set_bit(R1BIO_Barrier, &r1_bio->state);
930 bio_list_init(&bl);
931 for (i = 0; i < disks; i++) {
932 struct bio *mbio;
933 if (!r1_bio->bios[i])
934 continue;
936 mbio = bio_clone(bio, GFP_NOIO);
937 r1_bio->bios[i] = mbio;
939 mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
940 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
941 mbio->bi_end_io = raid1_end_write_request;
942 mbio->bi_rw = WRITE | do_barriers | do_sync;
943 mbio->bi_private = r1_bio;
945 if (behind_pages) {
946 struct bio_vec *bvec;
947 int j;
949 /* Yes, I really want the '__' version so that
950 * we clear any unused pointer in the io_vec, rather
951 * than leave them unchanged. This is important
952 * because when we come to free the pages, we won't
953 * know the originial bi_idx, so we just free
954 * them all
956 __bio_for_each_segment(bvec, mbio, j, 0)
957 bvec->bv_page = behind_pages[j];
958 if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
959 atomic_inc(&r1_bio->behind_remaining);
962 atomic_inc(&r1_bio->remaining);
964 bio_list_add(&bl, mbio);
966 kfree(behind_pages); /* the behind pages are attached to the bios now */
968 bitmap_startwrite(bitmap, bio->bi_sector, r1_bio->sectors,
969 test_bit(R1BIO_BehindIO, &r1_bio->state));
970 spin_lock_irqsave(&conf->device_lock, flags);
971 bio_list_merge(&conf->pending_bio_list, &bl);
972 bio_list_init(&bl);
974 blk_plug_device(mddev->queue);
975 spin_unlock_irqrestore(&conf->device_lock, flags);
977 /* In case raid1d snuck into freeze_array */
978 wake_up(&conf->wait_barrier);
980 if (do_sync)
981 md_wakeup_thread(mddev->thread);
982 #if 0
983 while ((bio = bio_list_pop(&bl)) != NULL)
984 generic_make_request(bio);
985 #endif
987 return 0;
990 static void status(struct seq_file *seq, mddev_t *mddev)
992 conf_t *conf = mddev_to_conf(mddev);
993 int i;
995 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
996 conf->raid_disks - mddev->degraded);
997 rcu_read_lock();
998 for (i = 0; i < conf->raid_disks; i++) {
999 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
1000 seq_printf(seq, "%s",
1001 rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1003 rcu_read_unlock();
1004 seq_printf(seq, "]");
1008 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1010 char b[BDEVNAME_SIZE];
1011 conf_t *conf = mddev_to_conf(mddev);
1014 * If it is not operational, then we have already marked it as dead
1015 * else if it is the last working disks, ignore the error, let the
1016 * next level up know.
1017 * else mark the drive as failed
1019 if (test_bit(In_sync, &rdev->flags)
1020 && (conf->raid_disks - mddev->degraded) == 1) {
1022 * Don't fail the drive, act as though we were just a
1023 * normal single drive.
1024 * However don't try a recovery from this drive as
1025 * it is very likely to fail.
1027 mddev->recovery_disabled = 1;
1028 return;
1030 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1031 unsigned long flags;
1032 spin_lock_irqsave(&conf->device_lock, flags);
1033 mddev->degraded++;
1034 set_bit(Faulty, &rdev->flags);
1035 spin_unlock_irqrestore(&conf->device_lock, flags);
1037 * if recovery is running, make sure it aborts.
1039 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1040 } else
1041 set_bit(Faulty, &rdev->flags);
1042 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1043 printk(KERN_ALERT "raid1: Disk failure on %s, disabling device.\n"
1044 "raid1: Operation continuing on %d devices.\n",
1045 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
1048 static void print_conf(conf_t *conf)
1050 int i;
1052 printk("RAID1 conf printout:\n");
1053 if (!conf) {
1054 printk("(!conf)\n");
1055 return;
1057 printk(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1058 conf->raid_disks);
1060 rcu_read_lock();
1061 for (i = 0; i < conf->raid_disks; i++) {
1062 char b[BDEVNAME_SIZE];
1063 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
1064 if (rdev)
1065 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
1066 i, !test_bit(In_sync, &rdev->flags),
1067 !test_bit(Faulty, &rdev->flags),
1068 bdevname(rdev->bdev,b));
1070 rcu_read_unlock();
1073 static void close_sync(conf_t *conf)
1075 wait_barrier(conf);
1076 allow_barrier(conf);
1078 mempool_destroy(conf->r1buf_pool);
1079 conf->r1buf_pool = NULL;
1082 static int raid1_spare_active(mddev_t *mddev)
1084 int i;
1085 conf_t *conf = mddev->private;
1088 * Find all failed disks within the RAID1 configuration
1089 * and mark them readable.
1090 * Called under mddev lock, so rcu protection not needed.
1092 for (i = 0; i < conf->raid_disks; i++) {
1093 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
1094 if (rdev
1095 && !test_bit(Faulty, &rdev->flags)
1096 && !test_and_set_bit(In_sync, &rdev->flags)) {
1097 unsigned long flags;
1098 spin_lock_irqsave(&conf->device_lock, flags);
1099 mddev->degraded--;
1100 spin_unlock_irqrestore(&conf->device_lock, flags);
1104 print_conf(conf);
1105 return 0;
1109 static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1111 conf_t *conf = mddev->private;
1112 int err = -EEXIST;
1113 int mirror = 0;
1114 mirror_info_t *p;
1115 int first = 0;
1116 int last = mddev->raid_disks - 1;
1118 if (rdev->raid_disk >= 0)
1119 first = last = rdev->raid_disk;
1121 for (mirror = first; mirror <= last; mirror++)
1122 if ( !(p=conf->mirrors+mirror)->rdev) {
1124 blk_queue_stack_limits(mddev->queue,
1125 rdev->bdev->bd_disk->queue);
1126 /* as we don't honour merge_bvec_fn, we must never risk
1127 * violating it, so limit ->max_sector to one PAGE, as
1128 * a one page request is never in violation.
1130 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1131 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1132 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
1134 p->head_position = 0;
1135 rdev->raid_disk = mirror;
1136 err = 0;
1137 /* As all devices are equivalent, we don't need a full recovery
1138 * if this was recently any drive of the array
1140 if (rdev->saved_raid_disk < 0)
1141 conf->fullsync = 1;
1142 rcu_assign_pointer(p->rdev, rdev);
1143 break;
1146 print_conf(conf);
1147 return err;
1150 static int raid1_remove_disk(mddev_t *mddev, int number)
1152 conf_t *conf = mddev->private;
1153 int err = 0;
1154 mdk_rdev_t *rdev;
1155 mirror_info_t *p = conf->mirrors+ number;
1157 print_conf(conf);
1158 rdev = p->rdev;
1159 if (rdev) {
1160 if (test_bit(In_sync, &rdev->flags) ||
1161 atomic_read(&rdev->nr_pending)) {
1162 err = -EBUSY;
1163 goto abort;
1165 /* Only remove non-faulty devices is recovery
1166 * is not possible.
1168 if (!test_bit(Faulty, &rdev->flags) &&
1169 mddev->degraded < conf->raid_disks) {
1170 err = -EBUSY;
1171 goto abort;
1173 p->rdev = NULL;
1174 synchronize_rcu();
1175 if (atomic_read(&rdev->nr_pending)) {
1176 /* lost the race, try later */
1177 err = -EBUSY;
1178 p->rdev = rdev;
1181 abort:
1183 print_conf(conf);
1184 return err;
1188 static void end_sync_read(struct bio *bio, int error)
1190 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
1191 int i;
1193 for (i=r1_bio->mddev->raid_disks; i--; )
1194 if (r1_bio->bios[i] == bio)
1195 break;
1196 BUG_ON(i < 0);
1197 update_head_pos(i, r1_bio);
1199 * we have read a block, now it needs to be re-written,
1200 * or re-read if the read failed.
1201 * We don't do much here, just schedule handling by raid1d
1203 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1204 set_bit(R1BIO_Uptodate, &r1_bio->state);
1206 if (atomic_dec_and_test(&r1_bio->remaining))
1207 reschedule_retry(r1_bio);
1210 static void end_sync_write(struct bio *bio, int error)
1212 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1213 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
1214 mddev_t *mddev = r1_bio->mddev;
1215 conf_t *conf = mddev_to_conf(mddev);
1216 int i;
1217 int mirror=0;
1219 for (i = 0; i < conf->raid_disks; i++)
1220 if (r1_bio->bios[i] == bio) {
1221 mirror = i;
1222 break;
1224 if (!uptodate) {
1225 int sync_blocks = 0;
1226 sector_t s = r1_bio->sector;
1227 long sectors_to_go = r1_bio->sectors;
1228 /* make sure these bits doesn't get cleared. */
1229 do {
1230 bitmap_end_sync(mddev->bitmap, s,
1231 &sync_blocks, 1);
1232 s += sync_blocks;
1233 sectors_to_go -= sync_blocks;
1234 } while (sectors_to_go > 0);
1235 md_error(mddev, conf->mirrors[mirror].rdev);
1238 update_head_pos(mirror, r1_bio);
1240 if (atomic_dec_and_test(&r1_bio->remaining)) {
1241 sector_t s = r1_bio->sectors;
1242 put_buf(r1_bio);
1243 md_done_sync(mddev, s, uptodate);
1247 static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
1249 conf_t *conf = mddev_to_conf(mddev);
1250 int i;
1251 int disks = conf->raid_disks;
1252 struct bio *bio, *wbio;
1254 bio = r1_bio->bios[r1_bio->read_disk];
1257 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1258 /* We have read all readable devices. If we haven't
1259 * got the block, then there is no hope left.
1260 * If we have, then we want to do a comparison
1261 * and skip the write if everything is the same.
1262 * If any blocks failed to read, then we need to
1263 * attempt an over-write
1265 int primary;
1266 if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
1267 for (i=0; i<mddev->raid_disks; i++)
1268 if (r1_bio->bios[i]->bi_end_io == end_sync_read)
1269 md_error(mddev, conf->mirrors[i].rdev);
1271 md_done_sync(mddev, r1_bio->sectors, 1);
1272 put_buf(r1_bio);
1273 return;
1275 for (primary=0; primary<mddev->raid_disks; primary++)
1276 if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
1277 test_bit(BIO_UPTODATE, &r1_bio->bios[primary]->bi_flags)) {
1278 r1_bio->bios[primary]->bi_end_io = NULL;
1279 rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
1280 break;
1282 r1_bio->read_disk = primary;
1283 for (i=0; i<mddev->raid_disks; i++)
1284 if (r1_bio->bios[i]->bi_end_io == end_sync_read) {
1285 int j;
1286 int vcnt = r1_bio->sectors >> (PAGE_SHIFT- 9);
1287 struct bio *pbio = r1_bio->bios[primary];
1288 struct bio *sbio = r1_bio->bios[i];
1290 if (test_bit(BIO_UPTODATE, &sbio->bi_flags)) {
1291 for (j = vcnt; j-- ; ) {
1292 struct page *p, *s;
1293 p = pbio->bi_io_vec[j].bv_page;
1294 s = sbio->bi_io_vec[j].bv_page;
1295 if (memcmp(page_address(p),
1296 page_address(s),
1297 PAGE_SIZE))
1298 break;
1300 } else
1301 j = 0;
1302 if (j >= 0)
1303 mddev->resync_mismatches += r1_bio->sectors;
1304 if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
1305 && test_bit(BIO_UPTODATE, &sbio->bi_flags))) {
1306 sbio->bi_end_io = NULL;
1307 rdev_dec_pending(conf->mirrors[i].rdev, mddev);
1308 } else {
1309 /* fixup the bio for reuse */
1310 int size;
1311 sbio->bi_vcnt = vcnt;
1312 sbio->bi_size = r1_bio->sectors << 9;
1313 sbio->bi_idx = 0;
1314 sbio->bi_phys_segments = 0;
1315 sbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1316 sbio->bi_flags |= 1 << BIO_UPTODATE;
1317 sbio->bi_next = NULL;
1318 sbio->bi_sector = r1_bio->sector +
1319 conf->mirrors[i].rdev->data_offset;
1320 sbio->bi_bdev = conf->mirrors[i].rdev->bdev;
1321 size = sbio->bi_size;
1322 for (j = 0; j < vcnt ; j++) {
1323 struct bio_vec *bi;
1324 bi = &sbio->bi_io_vec[j];
1325 bi->bv_offset = 0;
1326 if (size > PAGE_SIZE)
1327 bi->bv_len = PAGE_SIZE;
1328 else
1329 bi->bv_len = size;
1330 size -= PAGE_SIZE;
1331 memcpy(page_address(bi->bv_page),
1332 page_address(pbio->bi_io_vec[j].bv_page),
1333 PAGE_SIZE);
1339 if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
1340 /* ouch - failed to read all of that.
1341 * Try some synchronous reads of other devices to get
1342 * good data, much like with normal read errors. Only
1343 * read into the pages we already have so we don't
1344 * need to re-issue the read request.
1345 * We don't need to freeze the array, because being in an
1346 * active sync request, there is no normal IO, and
1347 * no overlapping syncs.
1349 sector_t sect = r1_bio->sector;
1350 int sectors = r1_bio->sectors;
1351 int idx = 0;
1353 while(sectors) {
1354 int s = sectors;
1355 int d = r1_bio->read_disk;
1356 int success = 0;
1357 mdk_rdev_t *rdev;
1359 if (s > (PAGE_SIZE>>9))
1360 s = PAGE_SIZE >> 9;
1361 do {
1362 if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
1363 /* No rcu protection needed here devices
1364 * can only be removed when no resync is
1365 * active, and resync is currently active
1367 rdev = conf->mirrors[d].rdev;
1368 if (sync_page_io(rdev->bdev,
1369 sect + rdev->data_offset,
1370 s<<9,
1371 bio->bi_io_vec[idx].bv_page,
1372 READ)) {
1373 success = 1;
1374 break;
1377 d++;
1378 if (d == conf->raid_disks)
1379 d = 0;
1380 } while (!success && d != r1_bio->read_disk);
1382 if (success) {
1383 int start = d;
1384 /* write it back and re-read */
1385 set_bit(R1BIO_Uptodate, &r1_bio->state);
1386 while (d != r1_bio->read_disk) {
1387 if (d == 0)
1388 d = conf->raid_disks;
1389 d--;
1390 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1391 continue;
1392 rdev = conf->mirrors[d].rdev;
1393 atomic_add(s, &rdev->corrected_errors);
1394 if (sync_page_io(rdev->bdev,
1395 sect + rdev->data_offset,
1396 s<<9,
1397 bio->bi_io_vec[idx].bv_page,
1398 WRITE) == 0)
1399 md_error(mddev, rdev);
1401 d = start;
1402 while (d != r1_bio->read_disk) {
1403 if (d == 0)
1404 d = conf->raid_disks;
1405 d--;
1406 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1407 continue;
1408 rdev = conf->mirrors[d].rdev;
1409 if (sync_page_io(rdev->bdev,
1410 sect + rdev->data_offset,
1411 s<<9,
1412 bio->bi_io_vec[idx].bv_page,
1413 READ) == 0)
1414 md_error(mddev, rdev);
1416 } else {
1417 char b[BDEVNAME_SIZE];
1418 /* Cannot read from anywhere, array is toast */
1419 md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
1420 printk(KERN_ALERT "raid1: %s: unrecoverable I/O read error"
1421 " for block %llu\n",
1422 bdevname(bio->bi_bdev,b),
1423 (unsigned long long)r1_bio->sector);
1424 md_done_sync(mddev, r1_bio->sectors, 0);
1425 put_buf(r1_bio);
1426 return;
1428 sectors -= s;
1429 sect += s;
1430 idx ++;
1435 * schedule writes
1437 atomic_set(&r1_bio->remaining, 1);
1438 for (i = 0; i < disks ; i++) {
1439 wbio = r1_bio->bios[i];
1440 if (wbio->bi_end_io == NULL ||
1441 (wbio->bi_end_io == end_sync_read &&
1442 (i == r1_bio->read_disk ||
1443 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
1444 continue;
1446 wbio->bi_rw = WRITE;
1447 wbio->bi_end_io = end_sync_write;
1448 atomic_inc(&r1_bio->remaining);
1449 md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
1451 generic_make_request(wbio);
1454 if (atomic_dec_and_test(&r1_bio->remaining)) {
1455 /* if we're here, all write(s) have completed, so clean up */
1456 md_done_sync(mddev, r1_bio->sectors, 1);
1457 put_buf(r1_bio);
1462 * This is a kernel thread which:
1464 * 1. Retries failed read operations on working mirrors.
1465 * 2. Updates the raid superblock when problems encounter.
1466 * 3. Performs writes following reads for array syncronising.
1469 static void fix_read_error(conf_t *conf, int read_disk,
1470 sector_t sect, int sectors)
1472 mddev_t *mddev = conf->mddev;
1473 while(sectors) {
1474 int s = sectors;
1475 int d = read_disk;
1476 int success = 0;
1477 int start;
1478 mdk_rdev_t *rdev;
1480 if (s > (PAGE_SIZE>>9))
1481 s = PAGE_SIZE >> 9;
1483 do {
1484 /* Note: no rcu protection needed here
1485 * as this is synchronous in the raid1d thread
1486 * which is the thread that might remove
1487 * a device. If raid1d ever becomes multi-threaded....
1489 rdev = conf->mirrors[d].rdev;
1490 if (rdev &&
1491 test_bit(In_sync, &rdev->flags) &&
1492 sync_page_io(rdev->bdev,
1493 sect + rdev->data_offset,
1494 s<<9,
1495 conf->tmppage, READ))
1496 success = 1;
1497 else {
1498 d++;
1499 if (d == conf->raid_disks)
1500 d = 0;
1502 } while (!success && d != read_disk);
1504 if (!success) {
1505 /* Cannot read from anywhere -- bye bye array */
1506 md_error(mddev, conf->mirrors[read_disk].rdev);
1507 break;
1509 /* write it back and re-read */
1510 start = d;
1511 while (d != read_disk) {
1512 if (d==0)
1513 d = conf->raid_disks;
1514 d--;
1515 rdev = conf->mirrors[d].rdev;
1516 if (rdev &&
1517 test_bit(In_sync, &rdev->flags)) {
1518 if (sync_page_io(rdev->bdev,
1519 sect + rdev->data_offset,
1520 s<<9, conf->tmppage, WRITE)
1521 == 0)
1522 /* Well, this device is dead */
1523 md_error(mddev, rdev);
1526 d = start;
1527 while (d != read_disk) {
1528 char b[BDEVNAME_SIZE];
1529 if (d==0)
1530 d = conf->raid_disks;
1531 d--;
1532 rdev = conf->mirrors[d].rdev;
1533 if (rdev &&
1534 test_bit(In_sync, &rdev->flags)) {
1535 if (sync_page_io(rdev->bdev,
1536 sect + rdev->data_offset,
1537 s<<9, conf->tmppage, READ)
1538 == 0)
1539 /* Well, this device is dead */
1540 md_error(mddev, rdev);
1541 else {
1542 atomic_add(s, &rdev->corrected_errors);
1543 printk(KERN_INFO
1544 "raid1:%s: read error corrected "
1545 "(%d sectors at %llu on %s)\n",
1546 mdname(mddev), s,
1547 (unsigned long long)(sect +
1548 rdev->data_offset),
1549 bdevname(rdev->bdev, b));
1553 sectors -= s;
1554 sect += s;
1558 static void raid1d(mddev_t *mddev)
1560 r1bio_t *r1_bio;
1561 struct bio *bio;
1562 unsigned long flags;
1563 conf_t *conf = mddev_to_conf(mddev);
1564 struct list_head *head = &conf->retry_list;
1565 int unplug=0;
1566 mdk_rdev_t *rdev;
1568 md_check_recovery(mddev);
1570 for (;;) {
1571 char b[BDEVNAME_SIZE];
1573 unplug += flush_pending_writes(conf);
1575 spin_lock_irqsave(&conf->device_lock, flags);
1576 if (list_empty(head)) {
1577 spin_unlock_irqrestore(&conf->device_lock, flags);
1578 break;
1580 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
1581 list_del(head->prev);
1582 conf->nr_queued--;
1583 spin_unlock_irqrestore(&conf->device_lock, flags);
1585 mddev = r1_bio->mddev;
1586 conf = mddev_to_conf(mddev);
1587 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
1588 sync_request_write(mddev, r1_bio);
1589 unplug = 1;
1590 } else if (test_bit(R1BIO_BarrierRetry, &r1_bio->state)) {
1591 /* some requests in the r1bio were BIO_RW_BARRIER
1592 * requests which failed with -EOPNOTSUPP. Hohumm..
1593 * Better resubmit without the barrier.
1594 * We know which devices to resubmit for, because
1595 * all others have had their bios[] entry cleared.
1596 * We already have a nr_pending reference on these rdevs.
1598 int i;
1599 const int do_sync = bio_sync(r1_bio->master_bio);
1600 clear_bit(R1BIO_BarrierRetry, &r1_bio->state);
1601 clear_bit(R1BIO_Barrier, &r1_bio->state);
1602 for (i=0; i < conf->raid_disks; i++)
1603 if (r1_bio->bios[i])
1604 atomic_inc(&r1_bio->remaining);
1605 for (i=0; i < conf->raid_disks; i++)
1606 if (r1_bio->bios[i]) {
1607 struct bio_vec *bvec;
1608 int j;
1610 bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
1611 /* copy pages from the failed bio, as
1612 * this might be a write-behind device */
1613 __bio_for_each_segment(bvec, bio, j, 0)
1614 bvec->bv_page = bio_iovec_idx(r1_bio->bios[i], j)->bv_page;
1615 bio_put(r1_bio->bios[i]);
1616 bio->bi_sector = r1_bio->sector +
1617 conf->mirrors[i].rdev->data_offset;
1618 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1619 bio->bi_end_io = raid1_end_write_request;
1620 bio->bi_rw = WRITE | do_sync;
1621 bio->bi_private = r1_bio;
1622 r1_bio->bios[i] = bio;
1623 generic_make_request(bio);
1625 } else {
1626 int disk;
1628 /* we got a read error. Maybe the drive is bad. Maybe just
1629 * the block and we can fix it.
1630 * We freeze all other IO, and try reading the block from
1631 * other devices. When we find one, we re-write
1632 * and check it that fixes the read error.
1633 * This is all done synchronously while the array is
1634 * frozen
1636 if (mddev->ro == 0) {
1637 freeze_array(conf);
1638 fix_read_error(conf, r1_bio->read_disk,
1639 r1_bio->sector,
1640 r1_bio->sectors);
1641 unfreeze_array(conf);
1644 bio = r1_bio->bios[r1_bio->read_disk];
1645 if ((disk=read_balance(conf, r1_bio)) == -1 ||
1646 disk == r1_bio->read_disk) {
1647 printk(KERN_ALERT "raid1: %s: unrecoverable I/O"
1648 " read error for block %llu\n",
1649 bdevname(bio->bi_bdev,b),
1650 (unsigned long long)r1_bio->sector);
1651 raid_end_bio_io(r1_bio);
1652 } else {
1653 const int do_sync = bio_sync(r1_bio->master_bio);
1654 r1_bio->bios[r1_bio->read_disk] =
1655 mddev->ro ? IO_BLOCKED : NULL;
1656 r1_bio->read_disk = disk;
1657 bio_put(bio);
1658 bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
1659 r1_bio->bios[r1_bio->read_disk] = bio;
1660 rdev = conf->mirrors[disk].rdev;
1661 if (printk_ratelimit())
1662 printk(KERN_ERR "raid1: %s: redirecting sector %llu to"
1663 " another mirror\n",
1664 bdevname(rdev->bdev,b),
1665 (unsigned long long)r1_bio->sector);
1666 bio->bi_sector = r1_bio->sector + rdev->data_offset;
1667 bio->bi_bdev = rdev->bdev;
1668 bio->bi_end_io = raid1_end_read_request;
1669 bio->bi_rw = READ | do_sync;
1670 bio->bi_private = r1_bio;
1671 unplug = 1;
1672 generic_make_request(bio);
1676 if (unplug)
1677 unplug_slaves(mddev);
1681 static int init_resync(conf_t *conf)
1683 int buffs;
1685 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
1686 BUG_ON(conf->r1buf_pool);
1687 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
1688 conf->poolinfo);
1689 if (!conf->r1buf_pool)
1690 return -ENOMEM;
1691 conf->next_resync = 0;
1692 return 0;
1696 * perform a "sync" on one "block"
1698 * We need to make sure that no normal I/O request - particularly write
1699 * requests - conflict with active sync requests.
1701 * This is achieved by tracking pending requests and a 'barrier' concept
1702 * that can be installed to exclude normal IO requests.
1705 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1707 conf_t *conf = mddev_to_conf(mddev);
1708 r1bio_t *r1_bio;
1709 struct bio *bio;
1710 sector_t max_sector, nr_sectors;
1711 int disk = -1;
1712 int i;
1713 int wonly = -1;
1714 int write_targets = 0, read_targets = 0;
1715 int sync_blocks;
1716 int still_degraded = 0;
1718 if (!conf->r1buf_pool)
1721 printk("sync start - bitmap %p\n", mddev->bitmap);
1723 if (init_resync(conf))
1724 return 0;
1727 max_sector = mddev->size << 1;
1728 if (sector_nr >= max_sector) {
1729 /* If we aborted, we need to abort the
1730 * sync on the 'current' bitmap chunk (there will
1731 * only be one in raid1 resync.
1732 * We can find the current addess in mddev->curr_resync
1734 if (mddev->curr_resync < max_sector) /* aborted */
1735 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1736 &sync_blocks, 1);
1737 else /* completed sync */
1738 conf->fullsync = 0;
1740 bitmap_close_sync(mddev->bitmap);
1741 close_sync(conf);
1742 return 0;
1745 if (mddev->bitmap == NULL &&
1746 mddev->recovery_cp == MaxSector &&
1747 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
1748 conf->fullsync == 0) {
1749 *skipped = 1;
1750 return max_sector - sector_nr;
1752 /* before building a request, check if we can skip these blocks..
1753 * This call the bitmap_start_sync doesn't actually record anything
1755 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
1756 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1757 /* We can skip this block, and probably several more */
1758 *skipped = 1;
1759 return sync_blocks;
1762 * If there is non-resync activity waiting for a turn,
1763 * and resync is going fast enough,
1764 * then let it though before starting on this new sync request.
1766 if (!go_faster && conf->nr_waiting)
1767 msleep_interruptible(1000);
1769 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
1770 raise_barrier(conf);
1772 conf->next_resync = sector_nr;
1774 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
1775 rcu_read_lock();
1777 * If we get a correctably read error during resync or recovery,
1778 * we might want to read from a different device. So we
1779 * flag all drives that could conceivably be read from for READ,
1780 * and any others (which will be non-In_sync devices) for WRITE.
1781 * If a read fails, we try reading from something else for which READ
1782 * is OK.
1785 r1_bio->mddev = mddev;
1786 r1_bio->sector = sector_nr;
1787 r1_bio->state = 0;
1788 set_bit(R1BIO_IsSync, &r1_bio->state);
1790 for (i=0; i < conf->raid_disks; i++) {
1791 mdk_rdev_t *rdev;
1792 bio = r1_bio->bios[i];
1794 /* take from bio_init */
1795 bio->bi_next = NULL;
1796 bio->bi_flags |= 1 << BIO_UPTODATE;
1797 bio->bi_rw = READ;
1798 bio->bi_vcnt = 0;
1799 bio->bi_idx = 0;
1800 bio->bi_phys_segments = 0;
1801 bio->bi_size = 0;
1802 bio->bi_end_io = NULL;
1803 bio->bi_private = NULL;
1805 rdev = rcu_dereference(conf->mirrors[i].rdev);
1806 if (rdev == NULL ||
1807 test_bit(Faulty, &rdev->flags)) {
1808 still_degraded = 1;
1809 continue;
1810 } else if (!test_bit(In_sync, &rdev->flags)) {
1811 bio->bi_rw = WRITE;
1812 bio->bi_end_io = end_sync_write;
1813 write_targets ++;
1814 } else {
1815 /* may need to read from here */
1816 bio->bi_rw = READ;
1817 bio->bi_end_io = end_sync_read;
1818 if (test_bit(WriteMostly, &rdev->flags)) {
1819 if (wonly < 0)
1820 wonly = i;
1821 } else {
1822 if (disk < 0)
1823 disk = i;
1825 read_targets++;
1827 atomic_inc(&rdev->nr_pending);
1828 bio->bi_sector = sector_nr + rdev->data_offset;
1829 bio->bi_bdev = rdev->bdev;
1830 bio->bi_private = r1_bio;
1832 rcu_read_unlock();
1833 if (disk < 0)
1834 disk = wonly;
1835 r1_bio->read_disk = disk;
1837 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
1838 /* extra read targets are also write targets */
1839 write_targets += read_targets-1;
1841 if (write_targets == 0 || read_targets == 0) {
1842 /* There is nowhere to write, so all non-sync
1843 * drives must be failed - so we are finished
1845 sector_t rv = max_sector - sector_nr;
1846 *skipped = 1;
1847 put_buf(r1_bio);
1848 return rv;
1851 if (max_sector > mddev->resync_max)
1852 max_sector = mddev->resync_max; /* Don't do IO beyond here */
1853 nr_sectors = 0;
1854 sync_blocks = 0;
1855 do {
1856 struct page *page;
1857 int len = PAGE_SIZE;
1858 if (sector_nr + (len>>9) > max_sector)
1859 len = (max_sector - sector_nr) << 9;
1860 if (len == 0)
1861 break;
1862 if (sync_blocks == 0) {
1863 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1864 &sync_blocks, still_degraded) &&
1865 !conf->fullsync &&
1866 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1867 break;
1868 BUG_ON(sync_blocks < (PAGE_SIZE>>9));
1869 if (len > (sync_blocks<<9))
1870 len = sync_blocks<<9;
1873 for (i=0 ; i < conf->raid_disks; i++) {
1874 bio = r1_bio->bios[i];
1875 if (bio->bi_end_io) {
1876 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
1877 if (bio_add_page(bio, page, len, 0) == 0) {
1878 /* stop here */
1879 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
1880 while (i > 0) {
1881 i--;
1882 bio = r1_bio->bios[i];
1883 if (bio->bi_end_io==NULL)
1884 continue;
1885 /* remove last page from this bio */
1886 bio->bi_vcnt--;
1887 bio->bi_size -= len;
1888 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
1890 goto bio_full;
1894 nr_sectors += len>>9;
1895 sector_nr += len>>9;
1896 sync_blocks -= (len>>9);
1897 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
1898 bio_full:
1899 r1_bio->sectors = nr_sectors;
1901 /* For a user-requested sync, we read all readable devices and do a
1902 * compare
1904 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1905 atomic_set(&r1_bio->remaining, read_targets);
1906 for (i=0; i<conf->raid_disks; i++) {
1907 bio = r1_bio->bios[i];
1908 if (bio->bi_end_io == end_sync_read) {
1909 md_sync_acct(bio->bi_bdev, nr_sectors);
1910 generic_make_request(bio);
1913 } else {
1914 atomic_set(&r1_bio->remaining, 1);
1915 bio = r1_bio->bios[r1_bio->read_disk];
1916 md_sync_acct(bio->bi_bdev, nr_sectors);
1917 generic_make_request(bio);
1920 return nr_sectors;
1923 static int run(mddev_t *mddev)
1925 conf_t *conf;
1926 int i, j, disk_idx;
1927 mirror_info_t *disk;
1928 mdk_rdev_t *rdev;
1930 if (mddev->level != 1) {
1931 printk("raid1: %s: raid level not set to mirroring (%d)\n",
1932 mdname(mddev), mddev->level);
1933 goto out;
1935 if (mddev->reshape_position != MaxSector) {
1936 printk("raid1: %s: reshape_position set but not supported\n",
1937 mdname(mddev));
1938 goto out;
1941 * copy the already verified devices into our private RAID1
1942 * bookkeeping area. [whatever we allocate in run(),
1943 * should be freed in stop()]
1945 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
1946 mddev->private = conf;
1947 if (!conf)
1948 goto out_no_mem;
1950 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
1951 GFP_KERNEL);
1952 if (!conf->mirrors)
1953 goto out_no_mem;
1955 conf->tmppage = alloc_page(GFP_KERNEL);
1956 if (!conf->tmppage)
1957 goto out_no_mem;
1959 conf->poolinfo = kmalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
1960 if (!conf->poolinfo)
1961 goto out_no_mem;
1962 conf->poolinfo->mddev = mddev;
1963 conf->poolinfo->raid_disks = mddev->raid_disks;
1964 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1965 r1bio_pool_free,
1966 conf->poolinfo);
1967 if (!conf->r1bio_pool)
1968 goto out_no_mem;
1970 spin_lock_init(&conf->device_lock);
1971 mddev->queue->queue_lock = &conf->device_lock;
1973 list_for_each_entry(rdev, &mddev->disks, same_set) {
1974 disk_idx = rdev->raid_disk;
1975 if (disk_idx >= mddev->raid_disks
1976 || disk_idx < 0)
1977 continue;
1978 disk = conf->mirrors + disk_idx;
1980 disk->rdev = rdev;
1982 blk_queue_stack_limits(mddev->queue,
1983 rdev->bdev->bd_disk->queue);
1984 /* as we don't honour merge_bvec_fn, we must never risk
1985 * violating it, so limit ->max_sector to one PAGE, as
1986 * a one page request is never in violation.
1988 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1989 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1990 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
1992 disk->head_position = 0;
1994 conf->raid_disks = mddev->raid_disks;
1995 conf->mddev = mddev;
1996 INIT_LIST_HEAD(&conf->retry_list);
1998 spin_lock_init(&conf->resync_lock);
1999 init_waitqueue_head(&conf->wait_barrier);
2001 bio_list_init(&conf->pending_bio_list);
2002 bio_list_init(&conf->flushing_bio_list);
2005 mddev->degraded = 0;
2006 for (i = 0; i < conf->raid_disks; i++) {
2008 disk = conf->mirrors + i;
2010 if (!disk->rdev ||
2011 !test_bit(In_sync, &disk->rdev->flags)) {
2012 disk->head_position = 0;
2013 mddev->degraded++;
2014 if (disk->rdev)
2015 conf->fullsync = 1;
2018 if (mddev->degraded == conf->raid_disks) {
2019 printk(KERN_ERR "raid1: no operational mirrors for %s\n",
2020 mdname(mddev));
2021 goto out_free_conf;
2023 if (conf->raid_disks - mddev->degraded == 1)
2024 mddev->recovery_cp = MaxSector;
2027 * find the first working one and use it as a starting point
2028 * to read balancing.
2030 for (j = 0; j < conf->raid_disks &&
2031 (!conf->mirrors[j].rdev ||
2032 !test_bit(In_sync, &conf->mirrors[j].rdev->flags)) ; j++)
2033 /* nothing */;
2034 conf->last_used = j;
2037 mddev->thread = md_register_thread(raid1d, mddev, "%s_raid1");
2038 if (!mddev->thread) {
2039 printk(KERN_ERR
2040 "raid1: couldn't allocate thread for %s\n",
2041 mdname(mddev));
2042 goto out_free_conf;
2045 printk(KERN_INFO
2046 "raid1: raid set %s active with %d out of %d mirrors\n",
2047 mdname(mddev), mddev->raid_disks - mddev->degraded,
2048 mddev->raid_disks);
2050 * Ok, everything is just fine now
2052 mddev->array_sectors = mddev->size * 2;
2054 mddev->queue->unplug_fn = raid1_unplug;
2055 mddev->queue->backing_dev_info.congested_fn = raid1_congested;
2056 mddev->queue->backing_dev_info.congested_data = mddev;
2058 return 0;
2060 out_no_mem:
2061 printk(KERN_ERR "raid1: couldn't allocate memory for %s\n",
2062 mdname(mddev));
2064 out_free_conf:
2065 if (conf) {
2066 if (conf->r1bio_pool)
2067 mempool_destroy(conf->r1bio_pool);
2068 kfree(conf->mirrors);
2069 safe_put_page(conf->tmppage);
2070 kfree(conf->poolinfo);
2071 kfree(conf);
2072 mddev->private = NULL;
2074 out:
2075 return -EIO;
2078 static int stop(mddev_t *mddev)
2080 conf_t *conf = mddev_to_conf(mddev);
2081 struct bitmap *bitmap = mddev->bitmap;
2082 int behind_wait = 0;
2084 /* wait for behind writes to complete */
2085 while (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
2086 behind_wait++;
2087 printk(KERN_INFO "raid1: behind writes in progress on device %s, waiting to stop (%d)\n", mdname(mddev), behind_wait);
2088 set_current_state(TASK_UNINTERRUPTIBLE);
2089 schedule_timeout(HZ); /* wait a second */
2090 /* need to kick something here to make sure I/O goes? */
2093 md_unregister_thread(mddev->thread);
2094 mddev->thread = NULL;
2095 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2096 if (conf->r1bio_pool)
2097 mempool_destroy(conf->r1bio_pool);
2098 kfree(conf->mirrors);
2099 kfree(conf->poolinfo);
2100 kfree(conf);
2101 mddev->private = NULL;
2102 return 0;
2105 static int raid1_resize(mddev_t *mddev, sector_t sectors)
2107 /* no resync is happening, and there is enough space
2108 * on all devices, so we can resize.
2109 * We need to make sure resync covers any new space.
2110 * If the array is shrinking we should possibly wait until
2111 * any io in the removed space completes, but it hardly seems
2112 * worth it.
2114 mddev->array_sectors = sectors;
2115 set_capacity(mddev->gendisk, mddev->array_sectors);
2116 mddev->changed = 1;
2117 if (mddev->array_sectors / 2 > mddev->size &&
2118 mddev->recovery_cp == MaxSector) {
2119 mddev->recovery_cp = mddev->size << 1;
2120 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2122 mddev->size = mddev->array_sectors / 2;
2123 mddev->resync_max_sectors = sectors;
2124 return 0;
2127 static int raid1_reshape(mddev_t *mddev)
2129 /* We need to:
2130 * 1/ resize the r1bio_pool
2131 * 2/ resize conf->mirrors
2133 * We allocate a new r1bio_pool if we can.
2134 * Then raise a device barrier and wait until all IO stops.
2135 * Then resize conf->mirrors and swap in the new r1bio pool.
2137 * At the same time, we "pack" the devices so that all the missing
2138 * devices have the higher raid_disk numbers.
2140 mempool_t *newpool, *oldpool;
2141 struct pool_info *newpoolinfo;
2142 mirror_info_t *newmirrors;
2143 conf_t *conf = mddev_to_conf(mddev);
2144 int cnt, raid_disks;
2145 unsigned long flags;
2146 int d, d2, err;
2148 /* Cannot change chunk_size, layout, or level */
2149 if (mddev->chunk_size != mddev->new_chunk ||
2150 mddev->layout != mddev->new_layout ||
2151 mddev->level != mddev->new_level) {
2152 mddev->new_chunk = mddev->chunk_size;
2153 mddev->new_layout = mddev->layout;
2154 mddev->new_level = mddev->level;
2155 return -EINVAL;
2158 err = md_allow_write(mddev);
2159 if (err)
2160 return err;
2162 raid_disks = mddev->raid_disks + mddev->delta_disks;
2164 if (raid_disks < conf->raid_disks) {
2165 cnt=0;
2166 for (d= 0; d < conf->raid_disks; d++)
2167 if (conf->mirrors[d].rdev)
2168 cnt++;
2169 if (cnt > raid_disks)
2170 return -EBUSY;
2173 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
2174 if (!newpoolinfo)
2175 return -ENOMEM;
2176 newpoolinfo->mddev = mddev;
2177 newpoolinfo->raid_disks = raid_disks;
2179 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
2180 r1bio_pool_free, newpoolinfo);
2181 if (!newpool) {
2182 kfree(newpoolinfo);
2183 return -ENOMEM;
2185 newmirrors = kzalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
2186 if (!newmirrors) {
2187 kfree(newpoolinfo);
2188 mempool_destroy(newpool);
2189 return -ENOMEM;
2192 raise_barrier(conf);
2194 /* ok, everything is stopped */
2195 oldpool = conf->r1bio_pool;
2196 conf->r1bio_pool = newpool;
2198 for (d = d2 = 0; d < conf->raid_disks; d++) {
2199 mdk_rdev_t *rdev = conf->mirrors[d].rdev;
2200 if (rdev && rdev->raid_disk != d2) {
2201 char nm[20];
2202 sprintf(nm, "rd%d", rdev->raid_disk);
2203 sysfs_remove_link(&mddev->kobj, nm);
2204 rdev->raid_disk = d2;
2205 sprintf(nm, "rd%d", rdev->raid_disk);
2206 sysfs_remove_link(&mddev->kobj, nm);
2207 if (sysfs_create_link(&mddev->kobj,
2208 &rdev->kobj, nm))
2209 printk(KERN_WARNING
2210 "md/raid1: cannot register "
2211 "%s for %s\n",
2212 nm, mdname(mddev));
2214 if (rdev)
2215 newmirrors[d2++].rdev = rdev;
2217 kfree(conf->mirrors);
2218 conf->mirrors = newmirrors;
2219 kfree(conf->poolinfo);
2220 conf->poolinfo = newpoolinfo;
2222 spin_lock_irqsave(&conf->device_lock, flags);
2223 mddev->degraded += (raid_disks - conf->raid_disks);
2224 spin_unlock_irqrestore(&conf->device_lock, flags);
2225 conf->raid_disks = mddev->raid_disks = raid_disks;
2226 mddev->delta_disks = 0;
2228 conf->last_used = 0; /* just make sure it is in-range */
2229 lower_barrier(conf);
2231 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2232 md_wakeup_thread(mddev->thread);
2234 mempool_destroy(oldpool);
2235 return 0;
2238 static void raid1_quiesce(mddev_t *mddev, int state)
2240 conf_t *conf = mddev_to_conf(mddev);
2242 switch(state) {
2243 case 1:
2244 raise_barrier(conf);
2245 break;
2246 case 0:
2247 lower_barrier(conf);
2248 break;
2253 static struct mdk_personality raid1_personality =
2255 .name = "raid1",
2256 .level = 1,
2257 .owner = THIS_MODULE,
2258 .make_request = make_request,
2259 .run = run,
2260 .stop = stop,
2261 .status = status,
2262 .error_handler = error,
2263 .hot_add_disk = raid1_add_disk,
2264 .hot_remove_disk= raid1_remove_disk,
2265 .spare_active = raid1_spare_active,
2266 .sync_request = sync_request,
2267 .resize = raid1_resize,
2268 .check_reshape = raid1_reshape,
2269 .quiesce = raid1_quiesce,
2272 static int __init raid_init(void)
2274 return register_md_personality(&raid1_personality);
2277 static void raid_exit(void)
2279 unregister_md_personality(&raid1_personality);
2282 module_init(raid_init);
2283 module_exit(raid_exit);
2284 MODULE_LICENSE("GPL");
2285 MODULE_ALIAS("md-personality-3"); /* RAID1 */
2286 MODULE_ALIAS("md-raid1");
2287 MODULE_ALIAS("md-level-1");