blob: b34ad56362dffe26b41014d70c336ea1df1b649f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid1.c : Multiple Devices driver for Linux
3 *
4 * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
5 *
6 * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
7 *
8 * RAID-1 management functions.
9 *
10 * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000
11 *
12 * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk>
13 * Various fixes by Neil Brown <neilb@cse.unsw.edu.au>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2, or (at your option)
18 * any later version.
19 *
20 * You should have received a copy of the GNU General Public License
21 * (for example /usr/src/linux/COPYING); if not, write to the Free
22 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25#include <linux/raid/raid1.h>
26
27/*
28 * Number of guaranteed r1bios in case of extreme VM load:
29 */
30#define NR_RAID1_BIOS 256
31
32static mdk_personality_t raid1_personality;
33
34static void unplug_slaves(mddev_t *mddev);
35
36
37static void * r1bio_pool_alloc(unsigned int __nocast gfp_flags, void *data)
38{
39 struct pool_info *pi = data;
40 r1bio_t *r1_bio;
41 int size = offsetof(r1bio_t, bios[pi->raid_disks]);
42
43 /* allocate a r1bio with room for raid_disks entries in the bios array */
44 r1_bio = kmalloc(size, gfp_flags);
45 if (r1_bio)
46 memset(r1_bio, 0, size);
47 else
48 unplug_slaves(pi->mddev);
49
50 return r1_bio;
51}
52
53static void r1bio_pool_free(void *r1_bio, void *data)
54{
55 kfree(r1_bio);
56}
57
58#define RESYNC_BLOCK_SIZE (64*1024)
59//#define RESYNC_BLOCK_SIZE PAGE_SIZE
60#define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
61#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
62#define RESYNC_WINDOW (2048*1024)
63
64static void * r1buf_pool_alloc(unsigned int __nocast gfp_flags, void *data)
65{
66 struct pool_info *pi = data;
67 struct page *page;
68 r1bio_t *r1_bio;
69 struct bio *bio;
70 int i, j;
71
72 r1_bio = r1bio_pool_alloc(gfp_flags, pi);
73 if (!r1_bio) {
74 unplug_slaves(pi->mddev);
75 return NULL;
76 }
77
78 /*
79 * Allocate bios : 1 for reading, n-1 for writing
80 */
81 for (j = pi->raid_disks ; j-- ; ) {
82 bio = bio_alloc(gfp_flags, RESYNC_PAGES);
83 if (!bio)
84 goto out_free_bio;
85 r1_bio->bios[j] = bio;
86 }
87 /*
88 * Allocate RESYNC_PAGES data pages and attach them to
89 * the first bio;
90 */
91 bio = r1_bio->bios[0];
92 for (i = 0; i < RESYNC_PAGES; i++) {
93 page = alloc_page(gfp_flags);
94 if (unlikely(!page))
95 goto out_free_pages;
96
97 bio->bi_io_vec[i].bv_page = page;
98 }
99
100 r1_bio->master_bio = NULL;
101
102 return r1_bio;
103
104out_free_pages:
105 for ( ; i > 0 ; i--)
106 __free_page(bio->bi_io_vec[i-1].bv_page);
107out_free_bio:
108 while ( ++j < pi->raid_disks )
109 bio_put(r1_bio->bios[j]);
110 r1bio_pool_free(r1_bio, data);
111 return NULL;
112}
113
114static void r1buf_pool_free(void *__r1_bio, void *data)
115{
116 struct pool_info *pi = data;
117 int i;
118 r1bio_t *r1bio = __r1_bio;
119 struct bio *bio = r1bio->bios[0];
120
121 for (i = 0; i < RESYNC_PAGES; i++) {
122 __free_page(bio->bi_io_vec[i].bv_page);
123 bio->bi_io_vec[i].bv_page = NULL;
124 }
125 for (i=0 ; i < pi->raid_disks; i++)
126 bio_put(r1bio->bios[i]);
127
128 r1bio_pool_free(r1bio, data);
129}
130
131static void put_all_bios(conf_t *conf, r1bio_t *r1_bio)
132{
133 int i;
134
135 for (i = 0; i < conf->raid_disks; i++) {
136 struct bio **bio = r1_bio->bios + i;
137 if (*bio)
138 bio_put(*bio);
139 *bio = NULL;
140 }
141}
142
143static inline void free_r1bio(r1bio_t *r1_bio)
144{
145 unsigned long flags;
146
147 conf_t *conf = mddev_to_conf(r1_bio->mddev);
148
149 /*
150 * Wake up any possible resync thread that waits for the device
151 * to go idle.
152 */
153 spin_lock_irqsave(&conf->resync_lock, flags);
154 if (!--conf->nr_pending) {
155 wake_up(&conf->wait_idle);
156 wake_up(&conf->wait_resume);
157 }
158 spin_unlock_irqrestore(&conf->resync_lock, flags);
159
160 put_all_bios(conf, r1_bio);
161 mempool_free(r1_bio, conf->r1bio_pool);
162}
163
164static inline void put_buf(r1bio_t *r1_bio)
165{
166 conf_t *conf = mddev_to_conf(r1_bio->mddev);
167 unsigned long flags;
168
169 mempool_free(r1_bio, conf->r1buf_pool);
170
171 spin_lock_irqsave(&conf->resync_lock, flags);
172 if (!conf->barrier)
173 BUG();
174 --conf->barrier;
175 wake_up(&conf->wait_resume);
176 wake_up(&conf->wait_idle);
177
178 if (!--conf->nr_pending) {
179 wake_up(&conf->wait_idle);
180 wake_up(&conf->wait_resume);
181 }
182 spin_unlock_irqrestore(&conf->resync_lock, flags);
183}
184
185static void reschedule_retry(r1bio_t *r1_bio)
186{
187 unsigned long flags;
188 mddev_t *mddev = r1_bio->mddev;
189 conf_t *conf = mddev_to_conf(mddev);
190
191 spin_lock_irqsave(&conf->device_lock, flags);
192 list_add(&r1_bio->retry_list, &conf->retry_list);
193 spin_unlock_irqrestore(&conf->device_lock, flags);
194
195 md_wakeup_thread(mddev->thread);
196}
197
198/*
199 * raid_end_bio_io() is called when we have finished servicing a mirrored
200 * operation and are ready to return a success/failure code to the buffer
201 * cache layer.
202 */
203static void raid_end_bio_io(r1bio_t *r1_bio)
204{
205 struct bio *bio = r1_bio->master_bio;
206
207 bio_endio(bio, bio->bi_size,
208 test_bit(R1BIO_Uptodate, &r1_bio->state) ? 0 : -EIO);
209 free_r1bio(r1_bio);
210}
211
212/*
213 * Update disk head position estimator based on IRQ completion info.
214 */
215static inline void update_head_pos(int disk, r1bio_t *r1_bio)
216{
217 conf_t *conf = mddev_to_conf(r1_bio->mddev);
218
219 conf->mirrors[disk].head_position =
220 r1_bio->sector + (r1_bio->sectors);
221}
222
223static int raid1_end_read_request(struct bio *bio, unsigned int bytes_done, int error)
224{
225 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
226 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
227 int mirror;
228 conf_t *conf = mddev_to_conf(r1_bio->mddev);
229
230 if (bio->bi_size)
231 return 1;
232
233 mirror = r1_bio->read_disk;
234 /*
235 * this branch is our 'one mirror IO has finished' event handler:
236 */
237 if (!uptodate)
238 md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
239 else
240 /*
241 * Set R1BIO_Uptodate in our master bio, so that
242 * we will return a good error code for to the higher
243 * levels even if IO on some other mirrored buffer fails.
244 *
245 * The 'master' represents the composite IO operation to
246 * user-side. So if something waits for IO, then it will
247 * wait for the 'master' bio.
248 */
249 set_bit(R1BIO_Uptodate, &r1_bio->state);
250
251 update_head_pos(mirror, r1_bio);
252
253 /*
254 * we have only one bio on the read side
255 */
256 if (uptodate)
257 raid_end_bio_io(r1_bio);
258 else {
259 /*
260 * oops, read error:
261 */
262 char b[BDEVNAME_SIZE];
263 if (printk_ratelimit())
264 printk(KERN_ERR "raid1: %s: rescheduling sector %llu\n",
265 bdevname(conf->mirrors[mirror].rdev->bdev,b), (unsigned long long)r1_bio->sector);
266 reschedule_retry(r1_bio);
267 }
268
269 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
270 return 0;
271}
272
273static int raid1_end_write_request(struct bio *bio, unsigned int bytes_done, int error)
274{
275 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
276 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
277 int mirror;
278 conf_t *conf = mddev_to_conf(r1_bio->mddev);
279
280 if (bio->bi_size)
281 return 1;
282
283 for (mirror = 0; mirror < conf->raid_disks; mirror++)
284 if (r1_bio->bios[mirror] == bio)
285 break;
286
287 /*
288 * this branch is our 'one mirror IO has finished' event handler:
289 */
290 if (!uptodate)
291 md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
292 else
293 /*
294 * Set R1BIO_Uptodate in our master bio, so that
295 * we will return a good error code for to the higher
296 * levels even if IO on some other mirrored buffer fails.
297 *
298 * The 'master' represents the composite IO operation to
299 * user-side. So if something waits for IO, then it will
300 * wait for the 'master' bio.
301 */
302 set_bit(R1BIO_Uptodate, &r1_bio->state);
303
304 update_head_pos(mirror, r1_bio);
305
306 /*
307 *
308 * Let's see if all mirrored write operations have finished
309 * already.
310 */
311 if (atomic_dec_and_test(&r1_bio->remaining)) {
312 md_write_end(r1_bio->mddev);
313 raid_end_bio_io(r1_bio);
314 }
315
316 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
317 return 0;
318}
319
320
321/*
322 * This routine returns the disk from which the requested read should
323 * be done. There is a per-array 'next expected sequential IO' sector
324 * number - if this matches on the next IO then we use the last disk.
325 * There is also a per-disk 'last know head position' sector that is
326 * maintained from IRQ contexts, both the normal and the resync IO
327 * completion handlers update this position correctly. If there is no
328 * perfect sequential match then we pick the disk whose head is closest.
329 *
330 * If there are 2 mirrors in the same 2 devices, performance degrades
331 * because position is mirror, not device based.
332 *
333 * The rdev for the device selected will have nr_pending incremented.
334 */
335static int read_balance(conf_t *conf, r1bio_t *r1_bio)
336{
337 const unsigned long this_sector = r1_bio->sector;
338 int new_disk = conf->last_used, disk = new_disk;
339 const int sectors = r1_bio->sectors;
340 sector_t new_distance, current_distance;
341 mdk_rdev_t *new_rdev, *rdev;
342
343 rcu_read_lock();
344 /*
345 * Check if it if we can balance. We can balance on the whole
346 * device if no resync is going on, or below the resync window.
347 * We take the first readable disk when above the resync window.
348 */
349 retry:
350 if (conf->mddev->recovery_cp < MaxSector &&
351 (this_sector + sectors >= conf->next_resync)) {
352 /* Choose the first operation device, for consistancy */
353 new_disk = 0;
354
355 while ((new_rdev=conf->mirrors[new_disk].rdev) == NULL ||
356 !new_rdev->in_sync) {
357 new_disk++;
358 if (new_disk == conf->raid_disks) {
359 new_disk = -1;
360 break;
361 }
362 }
363 goto rb_out;
364 }
365
366
367 /* make sure the disk is operational */
368 while ((new_rdev=conf->mirrors[new_disk].rdev) == NULL ||
369 !new_rdev->in_sync) {
370 if (new_disk <= 0)
371 new_disk = conf->raid_disks;
372 new_disk--;
373 if (new_disk == disk) {
374 new_disk = -1;
375 goto rb_out;
376 }
377 }
378 disk = new_disk;
379 /* now disk == new_disk == starting point for search */
380
381 /*
382 * Don't change to another disk for sequential reads:
383 */
384 if (conf->next_seq_sect == this_sector)
385 goto rb_out;
386 if (this_sector == conf->mirrors[new_disk].head_position)
387 goto rb_out;
388
389 current_distance = abs(this_sector - conf->mirrors[disk].head_position);
390
391 /* Find the disk whose head is closest */
392
393 do {
394 if (disk <= 0)
395 disk = conf->raid_disks;
396 disk--;
397
398 if ((rdev=conf->mirrors[disk].rdev) == NULL ||
399 !rdev->in_sync)
400 continue;
401
402 if (!atomic_read(&rdev->nr_pending)) {
403 new_disk = disk;
404 new_rdev = rdev;
405 break;
406 }
407 new_distance = abs(this_sector - conf->mirrors[disk].head_position);
408 if (new_distance < current_distance) {
409 current_distance = new_distance;
410 new_disk = disk;
411 new_rdev = rdev;
412 }
413 } while (disk != conf->last_used);
414
415rb_out:
416
417
418 if (new_disk >= 0) {
419 conf->next_seq_sect = this_sector + sectors;
420 conf->last_used = new_disk;
421 atomic_inc(&new_rdev->nr_pending);
422 if (!new_rdev->in_sync) {
423 /* cannot risk returning a device that failed
424 * before we inc'ed nr_pending
425 */
426 atomic_dec(&new_rdev->nr_pending);
427 goto retry;
428 }
429 }
430 rcu_read_unlock();
431
432 return new_disk;
433}
434
435static void unplug_slaves(mddev_t *mddev)
436{
437 conf_t *conf = mddev_to_conf(mddev);
438 int i;
439
440 rcu_read_lock();
441 for (i=0; i<mddev->raid_disks; i++) {
442 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
443 if (rdev && !rdev->faulty && atomic_read(&rdev->nr_pending)) {
444 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
445
446 atomic_inc(&rdev->nr_pending);
447 rcu_read_unlock();
448
449 if (r_queue->unplug_fn)
450 r_queue->unplug_fn(r_queue);
451
452 rdev_dec_pending(rdev, mddev);
453 rcu_read_lock();
454 }
455 }
456 rcu_read_unlock();
457}
458
459static void raid1_unplug(request_queue_t *q)
460{
461 unplug_slaves(q->queuedata);
462}
463
464static int raid1_issue_flush(request_queue_t *q, struct gendisk *disk,
465 sector_t *error_sector)
466{
467 mddev_t *mddev = q->queuedata;
468 conf_t *conf = mddev_to_conf(mddev);
469 int i, ret = 0;
470
471 rcu_read_lock();
472 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
473 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
474 if (rdev && !rdev->faulty) {
475 struct block_device *bdev = rdev->bdev;
476 request_queue_t *r_queue = bdev_get_queue(bdev);
477
478 if (!r_queue->issue_flush_fn)
479 ret = -EOPNOTSUPP;
480 else {
481 atomic_inc(&rdev->nr_pending);
482 rcu_read_unlock();
483 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
484 error_sector);
485 rdev_dec_pending(rdev, mddev);
486 rcu_read_lock();
487 }
488 }
489 }
490 rcu_read_unlock();
491 return ret;
492}
493
494/*
495 * Throttle resync depth, so that we can both get proper overlapping of
496 * requests, but are still able to handle normal requests quickly.
497 */
498#define RESYNC_DEPTH 32
499
500static void device_barrier(conf_t *conf, sector_t sect)
501{
502 spin_lock_irq(&conf->resync_lock);
503 wait_event_lock_irq(conf->wait_idle, !waitqueue_active(&conf->wait_resume),
504 conf->resync_lock, unplug_slaves(conf->mddev));
505
506 if (!conf->barrier++) {
507 wait_event_lock_irq(conf->wait_idle, !conf->nr_pending,
508 conf->resync_lock, unplug_slaves(conf->mddev));
509 if (conf->nr_pending)
510 BUG();
511 }
512 wait_event_lock_irq(conf->wait_resume, conf->barrier < RESYNC_DEPTH,
513 conf->resync_lock, unplug_slaves(conf->mddev));
514 conf->next_resync = sect;
515 spin_unlock_irq(&conf->resync_lock);
516}
517
518static int make_request(request_queue_t *q, struct bio * bio)
519{
520 mddev_t *mddev = q->queuedata;
521 conf_t *conf = mddev_to_conf(mddev);
522 mirror_info_t *mirror;
523 r1bio_t *r1_bio;
524 struct bio *read_bio;
525 int i, disks;
526 mdk_rdev_t *rdev;
527
528 /*
529 * Register the new request and wait if the reconstruction
530 * thread has put up a bar for new requests.
531 * Continue immediately if no resync is active currently.
532 */
533 spin_lock_irq(&conf->resync_lock);
534 wait_event_lock_irq(conf->wait_resume, !conf->barrier, conf->resync_lock, );
535 conf->nr_pending++;
536 spin_unlock_irq(&conf->resync_lock);
537
538 if (bio_data_dir(bio)==WRITE) {
539 disk_stat_inc(mddev->gendisk, writes);
540 disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bio));
541 } else {
542 disk_stat_inc(mddev->gendisk, reads);
543 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bio));
544 }
545
546 /*
547 * make_request() can abort the operation when READA is being
548 * used and no empty request is available.
549 *
550 */
551 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
552
553 r1_bio->master_bio = bio;
554 r1_bio->sectors = bio->bi_size >> 9;
555
556 r1_bio->mddev = mddev;
557 r1_bio->sector = bio->bi_sector;
558
559 r1_bio->state = 0;
560
561 if (bio_data_dir(bio) == READ) {
562 /*
563 * read balancing logic:
564 */
565 int rdisk = read_balance(conf, r1_bio);
566
567 if (rdisk < 0) {
568 /* couldn't find anywhere to read from */
569 raid_end_bio_io(r1_bio);
570 return 0;
571 }
572 mirror = conf->mirrors + rdisk;
573
574 r1_bio->read_disk = rdisk;
575
576 read_bio = bio_clone(bio, GFP_NOIO);
577
578 r1_bio->bios[rdisk] = read_bio;
579
580 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
581 read_bio->bi_bdev = mirror->rdev->bdev;
582 read_bio->bi_end_io = raid1_end_read_request;
583 read_bio->bi_rw = READ;
584 read_bio->bi_private = r1_bio;
585
586 generic_make_request(read_bio);
587 return 0;
588 }
589
590 /*
591 * WRITE:
592 */
593 /* first select target devices under spinlock and
594 * inc refcount on their rdev. Record them by setting
595 * bios[x] to bio
596 */
597 disks = conf->raid_disks;
598 rcu_read_lock();
599 for (i = 0; i < disks; i++) {
600 if ((rdev=conf->mirrors[i].rdev) != NULL &&
601 !rdev->faulty) {
602 atomic_inc(&rdev->nr_pending);
603 if (rdev->faulty) {
604 atomic_dec(&rdev->nr_pending);
605 r1_bio->bios[i] = NULL;
606 } else
607 r1_bio->bios[i] = bio;
608 } else
609 r1_bio->bios[i] = NULL;
610 }
611 rcu_read_unlock();
612
613 atomic_set(&r1_bio->remaining, 1);
614 md_write_start(mddev);
615 for (i = 0; i < disks; i++) {
616 struct bio *mbio;
617 if (!r1_bio->bios[i])
618 continue;
619
620 mbio = bio_clone(bio, GFP_NOIO);
621 r1_bio->bios[i] = mbio;
622
623 mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
624 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
625 mbio->bi_end_io = raid1_end_write_request;
626 mbio->bi_rw = WRITE;
627 mbio->bi_private = r1_bio;
628
629 atomic_inc(&r1_bio->remaining);
630 generic_make_request(mbio);
631 }
632
633 if (atomic_dec_and_test(&r1_bio->remaining)) {
634 md_write_end(mddev);
635 raid_end_bio_io(r1_bio);
636 }
637
638 return 0;
639}
640
641static void status(struct seq_file *seq, mddev_t *mddev)
642{
643 conf_t *conf = mddev_to_conf(mddev);
644 int i;
645
646 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
647 conf->working_disks);
648 for (i = 0; i < conf->raid_disks; i++)
649 seq_printf(seq, "%s",
650 conf->mirrors[i].rdev &&
651 conf->mirrors[i].rdev->in_sync ? "U" : "_");
652 seq_printf(seq, "]");
653}
654
655
656static void error(mddev_t *mddev, mdk_rdev_t *rdev)
657{
658 char b[BDEVNAME_SIZE];
659 conf_t *conf = mddev_to_conf(mddev);
660
661 /*
662 * If it is not operational, then we have already marked it as dead
663 * else if it is the last working disks, ignore the error, let the
664 * next level up know.
665 * else mark the drive as failed
666 */
667 if (rdev->in_sync
668 && conf->working_disks == 1)
669 /*
670 * Don't fail the drive, act as though we were just a
671 * normal single drive
672 */
673 return;
674 if (rdev->in_sync) {
675 mddev->degraded++;
676 conf->working_disks--;
677 /*
678 * if recovery is running, make sure it aborts.
679 */
680 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
681 }
682 rdev->in_sync = 0;
683 rdev->faulty = 1;
684 mddev->sb_dirty = 1;
685 printk(KERN_ALERT "raid1: Disk failure on %s, disabling device. \n"
686 " Operation continuing on %d devices\n",
687 bdevname(rdev->bdev,b), conf->working_disks);
688}
689
690static void print_conf(conf_t *conf)
691{
692 int i;
693 mirror_info_t *tmp;
694
695 printk("RAID1 conf printout:\n");
696 if (!conf) {
697 printk("(!conf)\n");
698 return;
699 }
700 printk(" --- wd:%d rd:%d\n", conf->working_disks,
701 conf->raid_disks);
702
703 for (i = 0; i < conf->raid_disks; i++) {
704 char b[BDEVNAME_SIZE];
705 tmp = conf->mirrors + i;
706 if (tmp->rdev)
707 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
708 i, !tmp->rdev->in_sync, !tmp->rdev->faulty,
709 bdevname(tmp->rdev->bdev,b));
710 }
711}
712
713static void close_sync(conf_t *conf)
714{
715 spin_lock_irq(&conf->resync_lock);
716 wait_event_lock_irq(conf->wait_resume, !conf->barrier,
717 conf->resync_lock, unplug_slaves(conf->mddev));
718 spin_unlock_irq(&conf->resync_lock);
719
720 if (conf->barrier) BUG();
721 if (waitqueue_active(&conf->wait_idle)) BUG();
722
723 mempool_destroy(conf->r1buf_pool);
724 conf->r1buf_pool = NULL;
725}
726
727static int raid1_spare_active(mddev_t *mddev)
728{
729 int i;
730 conf_t *conf = mddev->private;
731 mirror_info_t *tmp;
732
733 /*
734 * Find all failed disks within the RAID1 configuration
735 * and mark them readable
736 */
737 for (i = 0; i < conf->raid_disks; i++) {
738 tmp = conf->mirrors + i;
739 if (tmp->rdev
740 && !tmp->rdev->faulty
741 && !tmp->rdev->in_sync) {
742 conf->working_disks++;
743 mddev->degraded--;
744 tmp->rdev->in_sync = 1;
745 }
746 }
747
748 print_conf(conf);
749 return 0;
750}
751
752
753static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
754{
755 conf_t *conf = mddev->private;
756 int found = 0;
757 int mirror;
758 mirror_info_t *p;
759
760 for (mirror=0; mirror < mddev->raid_disks; mirror++)
761 if ( !(p=conf->mirrors+mirror)->rdev) {
762
763 blk_queue_stack_limits(mddev->queue,
764 rdev->bdev->bd_disk->queue);
765 /* as we don't honour merge_bvec_fn, we must never risk
766 * violating it, so limit ->max_sector to one PAGE, as
767 * a one page request is never in violation.
768 */
769 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
770 mddev->queue->max_sectors > (PAGE_SIZE>>9))
771 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
772
773 p->head_position = 0;
774 rdev->raid_disk = mirror;
775 found = 1;
776 p->rdev = rdev;
777 break;
778 }
779
780 print_conf(conf);
781 return found;
782}
783
784static int raid1_remove_disk(mddev_t *mddev, int number)
785{
786 conf_t *conf = mddev->private;
787 int err = 0;
788 mdk_rdev_t *rdev;
789 mirror_info_t *p = conf->mirrors+ number;
790
791 print_conf(conf);
792 rdev = p->rdev;
793 if (rdev) {
794 if (rdev->in_sync ||
795 atomic_read(&rdev->nr_pending)) {
796 err = -EBUSY;
797 goto abort;
798 }
799 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -0700800 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (atomic_read(&rdev->nr_pending)) {
802 /* lost the race, try later */
803 err = -EBUSY;
804 p->rdev = rdev;
805 }
806 }
807abort:
808
809 print_conf(conf);
810 return err;
811}
812
813
814static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
815{
816 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
817 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
818 conf_t *conf = mddev_to_conf(r1_bio->mddev);
819
820 if (bio->bi_size)
821 return 1;
822
823 if (r1_bio->bios[r1_bio->read_disk] != bio)
824 BUG();
825 update_head_pos(r1_bio->read_disk, r1_bio);
826 /*
827 * we have read a block, now it needs to be re-written,
828 * or re-read if the read failed.
829 * We don't do much here, just schedule handling by raid1d
830 */
831 if (!uptodate)
832 md_error(r1_bio->mddev,
833 conf->mirrors[r1_bio->read_disk].rdev);
834 else
835 set_bit(R1BIO_Uptodate, &r1_bio->state);
836 rdev_dec_pending(conf->mirrors[r1_bio->read_disk].rdev, conf->mddev);
837 reschedule_retry(r1_bio);
838 return 0;
839}
840
841static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
842{
843 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
844 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
845 mddev_t *mddev = r1_bio->mddev;
846 conf_t *conf = mddev_to_conf(mddev);
847 int i;
848 int mirror=0;
849
850 if (bio->bi_size)
851 return 1;
852
853 for (i = 0; i < conf->raid_disks; i++)
854 if (r1_bio->bios[i] == bio) {
855 mirror = i;
856 break;
857 }
858 if (!uptodate)
859 md_error(mddev, conf->mirrors[mirror].rdev);
860 update_head_pos(mirror, r1_bio);
861
862 if (atomic_dec_and_test(&r1_bio->remaining)) {
863 md_done_sync(mddev, r1_bio->sectors, uptodate);
864 put_buf(r1_bio);
865 }
866 rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
867 return 0;
868}
869
870static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
871{
872 conf_t *conf = mddev_to_conf(mddev);
873 int i;
874 int disks = conf->raid_disks;
875 struct bio *bio, *wbio;
876
877 bio = r1_bio->bios[r1_bio->read_disk];
878
879 /*
880 * schedule writes
881 */
882 if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
883 /*
884 * There is no point trying a read-for-reconstruct as
885 * reconstruct is about to be aborted
886 */
887 char b[BDEVNAME_SIZE];
888 printk(KERN_ALERT "raid1: %s: unrecoverable I/O read error"
889 " for block %llu\n",
890 bdevname(bio->bi_bdev,b),
891 (unsigned long long)r1_bio->sector);
892 md_done_sync(mddev, r1_bio->sectors, 0);
893 put_buf(r1_bio);
894 return;
895 }
896
897 atomic_set(&r1_bio->remaining, 1);
898 for (i = 0; i < disks ; i++) {
899 wbio = r1_bio->bios[i];
900 if (wbio->bi_end_io != end_sync_write)
901 continue;
902
903 atomic_inc(&conf->mirrors[i].rdev->nr_pending);
904 atomic_inc(&r1_bio->remaining);
905 md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
906 generic_make_request(wbio);
907 }
908
909 if (atomic_dec_and_test(&r1_bio->remaining)) {
910 md_done_sync(mddev, r1_bio->sectors, 1);
911 put_buf(r1_bio);
912 }
913}
914
915/*
916 * This is a kernel thread which:
917 *
918 * 1. Retries failed read operations on working mirrors.
919 * 2. Updates the raid superblock when problems encounter.
920 * 3. Performs writes following reads for array syncronising.
921 */
922
923static void raid1d(mddev_t *mddev)
924{
925 r1bio_t *r1_bio;
926 struct bio *bio;
927 unsigned long flags;
928 conf_t *conf = mddev_to_conf(mddev);
929 struct list_head *head = &conf->retry_list;
930 int unplug=0;
931 mdk_rdev_t *rdev;
932
933 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 for (;;) {
936 char b[BDEVNAME_SIZE];
937 spin_lock_irqsave(&conf->device_lock, flags);
938 if (list_empty(head))
939 break;
940 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
941 list_del(head->prev);
942 spin_unlock_irqrestore(&conf->device_lock, flags);
943
944 mddev = r1_bio->mddev;
945 conf = mddev_to_conf(mddev);
946 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
947 sync_request_write(mddev, r1_bio);
948 unplug = 1;
949 } else {
950 int disk;
951 bio = r1_bio->bios[r1_bio->read_disk];
952 if ((disk=read_balance(conf, r1_bio)) == -1) {
953 printk(KERN_ALERT "raid1: %s: unrecoverable I/O"
954 " read error for block %llu\n",
955 bdevname(bio->bi_bdev,b),
956 (unsigned long long)r1_bio->sector);
957 raid_end_bio_io(r1_bio);
958 } else {
959 r1_bio->bios[r1_bio->read_disk] = NULL;
960 r1_bio->read_disk = disk;
961 bio_put(bio);
962 bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
963 r1_bio->bios[r1_bio->read_disk] = bio;
964 rdev = conf->mirrors[disk].rdev;
965 if (printk_ratelimit())
966 printk(KERN_ERR "raid1: %s: redirecting sector %llu to"
967 " another mirror\n",
968 bdevname(rdev->bdev,b),
969 (unsigned long long)r1_bio->sector);
970 bio->bi_sector = r1_bio->sector + rdev->data_offset;
971 bio->bi_bdev = rdev->bdev;
972 bio->bi_end_io = raid1_end_read_request;
973 bio->bi_rw = READ;
974 bio->bi_private = r1_bio;
975 unplug = 1;
976 generic_make_request(bio);
977 }
978 }
979 }
980 spin_unlock_irqrestore(&conf->device_lock, flags);
981 if (unplug)
982 unplug_slaves(mddev);
983}
984
985
986static int init_resync(conf_t *conf)
987{
988 int buffs;
989
990 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
991 if (conf->r1buf_pool)
992 BUG();
993 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
994 conf->poolinfo);
995 if (!conf->r1buf_pool)
996 return -ENOMEM;
997 conf->next_resync = 0;
998 return 0;
999}
1000
1001/*
1002 * perform a "sync" on one "block"
1003 *
1004 * We need to make sure that no normal I/O request - particularly write
1005 * requests - conflict with active sync requests.
1006 *
1007 * This is achieved by tracking pending requests and a 'barrier' concept
1008 * that can be installed to exclude normal IO requests.
1009 */
1010
1011static int sync_request(mddev_t *mddev, sector_t sector_nr, int go_faster)
1012{
1013 conf_t *conf = mddev_to_conf(mddev);
1014 mirror_info_t *mirror;
1015 r1bio_t *r1_bio;
1016 struct bio *bio;
1017 sector_t max_sector, nr_sectors;
1018 int disk;
1019 int i;
1020 int write_targets = 0;
1021
1022 if (!conf->r1buf_pool)
1023 if (init_resync(conf))
1024 return -ENOMEM;
1025
1026 max_sector = mddev->size << 1;
1027 if (sector_nr >= max_sector) {
1028 close_sync(conf);
1029 return 0;
1030 }
1031
1032 /*
1033 * If there is non-resync activity waiting for us then
1034 * put in a delay to throttle resync.
1035 */
1036 if (!go_faster && waitqueue_active(&conf->wait_resume))
1037 msleep_interruptible(1000);
1038 device_barrier(conf, sector_nr + RESYNC_SECTORS);
1039
1040 /*
1041 * If reconstructing, and >1 working disc,
1042 * could dedicate one to rebuild and others to
1043 * service read requests ..
1044 */
1045 disk = conf->last_used;
1046 /* make sure disk is operational */
1047
1048 while (conf->mirrors[disk].rdev == NULL ||
1049 !conf->mirrors[disk].rdev->in_sync) {
1050 if (disk <= 0)
1051 disk = conf->raid_disks;
1052 disk--;
1053 if (disk == conf->last_used)
1054 break;
1055 }
1056 conf->last_used = disk;
1057 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
1058
1059
1060 mirror = conf->mirrors + disk;
1061
1062 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
1063
1064 spin_lock_irq(&conf->resync_lock);
1065 conf->nr_pending++;
1066 spin_unlock_irq(&conf->resync_lock);
1067
1068 r1_bio->mddev = mddev;
1069 r1_bio->sector = sector_nr;
1070 set_bit(R1BIO_IsSync, &r1_bio->state);
1071 r1_bio->read_disk = disk;
1072
1073 for (i=0; i < conf->raid_disks; i++) {
1074 bio = r1_bio->bios[i];
1075
1076 /* take from bio_init */
1077 bio->bi_next = NULL;
1078 bio->bi_flags |= 1 << BIO_UPTODATE;
1079 bio->bi_rw = 0;
1080 bio->bi_vcnt = 0;
1081 bio->bi_idx = 0;
1082 bio->bi_phys_segments = 0;
1083 bio->bi_hw_segments = 0;
1084 bio->bi_size = 0;
1085 bio->bi_end_io = NULL;
1086 bio->bi_private = NULL;
1087
1088 if (i == disk) {
1089 bio->bi_rw = READ;
1090 bio->bi_end_io = end_sync_read;
1091 } else if (conf->mirrors[i].rdev &&
1092 !conf->mirrors[i].rdev->faulty &&
1093 (!conf->mirrors[i].rdev->in_sync ||
1094 sector_nr + RESYNC_SECTORS > mddev->recovery_cp)) {
1095 bio->bi_rw = WRITE;
1096 bio->bi_end_io = end_sync_write;
1097 write_targets ++;
1098 } else
1099 continue;
1100 bio->bi_sector = sector_nr + conf->mirrors[i].rdev->data_offset;
1101 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1102 bio->bi_private = r1_bio;
1103 }
1104 if (write_targets == 0) {
1105 /* There is nowhere to write, so all non-sync
1106 * drives must be failed - so we are finished
1107 */
1108 int rv = max_sector - sector_nr;
1109 md_done_sync(mddev, rv, 1);
1110 put_buf(r1_bio);
1111 rdev_dec_pending(conf->mirrors[disk].rdev, mddev);
1112 return rv;
1113 }
1114
1115 nr_sectors = 0;
1116 do {
1117 struct page *page;
1118 int len = PAGE_SIZE;
1119 if (sector_nr + (len>>9) > max_sector)
1120 len = (max_sector - sector_nr) << 9;
1121 if (len == 0)
1122 break;
1123 for (i=0 ; i < conf->raid_disks; i++) {
1124 bio = r1_bio->bios[i];
1125 if (bio->bi_end_io) {
1126 page = r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page;
1127 if (bio_add_page(bio, page, len, 0) == 0) {
1128 /* stop here */
1129 r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page = page;
1130 while (i > 0) {
1131 i--;
1132 bio = r1_bio->bios[i];
1133 if (bio->bi_end_io==NULL) continue;
1134 /* remove last page from this bio */
1135 bio->bi_vcnt--;
1136 bio->bi_size -= len;
1137 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
1138 }
1139 goto bio_full;
1140 }
1141 }
1142 }
1143 nr_sectors += len>>9;
1144 sector_nr += len>>9;
1145 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
1146 bio_full:
1147 bio = r1_bio->bios[disk];
1148 r1_bio->sectors = nr_sectors;
1149
1150 md_sync_acct(mirror->rdev->bdev, nr_sectors);
1151
1152 generic_make_request(bio);
1153
1154 return nr_sectors;
1155}
1156
1157static int run(mddev_t *mddev)
1158{
1159 conf_t *conf;
1160 int i, j, disk_idx;
1161 mirror_info_t *disk;
1162 mdk_rdev_t *rdev;
1163 struct list_head *tmp;
1164
1165 if (mddev->level != 1) {
1166 printk("raid1: %s: raid level not set to mirroring (%d)\n",
1167 mdname(mddev), mddev->level);
1168 goto out;
1169 }
1170 /*
1171 * copy the already verified devices into our private RAID1
1172 * bookkeeping area. [whatever we allocate in run(),
1173 * should be freed in stop()]
1174 */
1175 conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
1176 mddev->private = conf;
1177 if (!conf)
1178 goto out_no_mem;
1179
1180 memset(conf, 0, sizeof(*conf));
1181 conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks,
1182 GFP_KERNEL);
1183 if (!conf->mirrors)
1184 goto out_no_mem;
1185
1186 memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
1187
1188 conf->poolinfo = kmalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
1189 if (!conf->poolinfo)
1190 goto out_no_mem;
1191 conf->poolinfo->mddev = mddev;
1192 conf->poolinfo->raid_disks = mddev->raid_disks;
1193 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1194 r1bio_pool_free,
1195 conf->poolinfo);
1196 if (!conf->r1bio_pool)
1197 goto out_no_mem;
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 ITERATE_RDEV(mddev, rdev, tmp) {
1200 disk_idx = rdev->raid_disk;
1201 if (disk_idx >= mddev->raid_disks
1202 || disk_idx < 0)
1203 continue;
1204 disk = conf->mirrors + disk_idx;
1205
1206 disk->rdev = rdev;
1207
1208 blk_queue_stack_limits(mddev->queue,
1209 rdev->bdev->bd_disk->queue);
1210 /* as we don't honour merge_bvec_fn, we must never risk
1211 * violating it, so limit ->max_sector to one PAGE, as
1212 * a one page request is never in violation.
1213 */
1214 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1215 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1216 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
1217
1218 disk->head_position = 0;
1219 if (!rdev->faulty && rdev->in_sync)
1220 conf->working_disks++;
1221 }
1222 conf->raid_disks = mddev->raid_disks;
1223 conf->mddev = mddev;
1224 spin_lock_init(&conf->device_lock);
1225 INIT_LIST_HEAD(&conf->retry_list);
1226 if (conf->working_disks == 1)
1227 mddev->recovery_cp = MaxSector;
1228
1229 spin_lock_init(&conf->resync_lock);
1230 init_waitqueue_head(&conf->wait_idle);
1231 init_waitqueue_head(&conf->wait_resume);
1232
1233 if (!conf->working_disks) {
1234 printk(KERN_ERR "raid1: no operational mirrors for %s\n",
1235 mdname(mddev));
1236 goto out_free_conf;
1237 }
1238
1239 mddev->degraded = 0;
1240 for (i = 0; i < conf->raid_disks; i++) {
1241
1242 disk = conf->mirrors + i;
1243
1244 if (!disk->rdev) {
1245 disk->head_position = 0;
1246 mddev->degraded++;
1247 }
1248 }
1249
1250 /*
1251 * find the first working one and use it as a starting point
1252 * to read balancing.
1253 */
1254 for (j = 0; j < conf->raid_disks &&
1255 (!conf->mirrors[j].rdev ||
1256 !conf->mirrors[j].rdev->in_sync) ; j++)
1257 /* nothing */;
1258 conf->last_used = j;
1259
1260
1261
1262 {
1263 mddev->thread = md_register_thread(raid1d, mddev, "%s_raid1");
1264 if (!mddev->thread) {
1265 printk(KERN_ERR
1266 "raid1: couldn't allocate thread for %s\n",
1267 mdname(mddev));
1268 goto out_free_conf;
1269 }
1270 }
1271 printk(KERN_INFO
1272 "raid1: raid set %s active with %d out of %d mirrors\n",
1273 mdname(mddev), mddev->raid_disks - mddev->degraded,
1274 mddev->raid_disks);
1275 /*
1276 * Ok, everything is just fine now
1277 */
1278 mddev->array_size = mddev->size;
1279
NeilBrown7a5febe2005-05-16 21:53:16 -07001280 mddev->queue->unplug_fn = raid1_unplug;
1281 mddev->queue->issue_flush_fn = raid1_issue_flush;
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return 0;
1284
1285out_no_mem:
1286 printk(KERN_ERR "raid1: couldn't allocate memory for %s\n",
1287 mdname(mddev));
1288
1289out_free_conf:
1290 if (conf) {
1291 if (conf->r1bio_pool)
1292 mempool_destroy(conf->r1bio_pool);
1293 if (conf->mirrors)
1294 kfree(conf->mirrors);
1295 if (conf->poolinfo)
1296 kfree(conf->poolinfo);
1297 kfree(conf);
1298 mddev->private = NULL;
1299 }
1300out:
1301 return -EIO;
1302}
1303
1304static int stop(mddev_t *mddev)
1305{
1306 conf_t *conf = mddev_to_conf(mddev);
1307
1308 md_unregister_thread(mddev->thread);
1309 mddev->thread = NULL;
1310 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
1311 if (conf->r1bio_pool)
1312 mempool_destroy(conf->r1bio_pool);
1313 if (conf->mirrors)
1314 kfree(conf->mirrors);
1315 if (conf->poolinfo)
1316 kfree(conf->poolinfo);
1317 kfree(conf);
1318 mddev->private = NULL;
1319 return 0;
1320}
1321
1322static int raid1_resize(mddev_t *mddev, sector_t sectors)
1323{
1324 /* no resync is happening, and there is enough space
1325 * on all devices, so we can resize.
1326 * We need to make sure resync covers any new space.
1327 * If the array is shrinking we should possibly wait until
1328 * any io in the removed space completes, but it hardly seems
1329 * worth it.
1330 */
1331 mddev->array_size = sectors>>1;
1332 set_capacity(mddev->gendisk, mddev->array_size << 1);
1333 mddev->changed = 1;
1334 if (mddev->array_size > mddev->size && mddev->recovery_cp == MaxSector) {
1335 mddev->recovery_cp = mddev->size << 1;
1336 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1337 }
1338 mddev->size = mddev->array_size;
1339 return 0;
1340}
1341
1342static int raid1_reshape(mddev_t *mddev, int raid_disks)
1343{
1344 /* We need to:
1345 * 1/ resize the r1bio_pool
1346 * 2/ resize conf->mirrors
1347 *
1348 * We allocate a new r1bio_pool if we can.
1349 * Then raise a device barrier and wait until all IO stops.
1350 * Then resize conf->mirrors and swap in the new r1bio pool.
NeilBrown6ea9c072005-06-21 17:17:09 -07001351 *
1352 * At the same time, we "pack" the devices so that all the missing
1353 * devices have the higher raid_disk numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 */
1355 mempool_t *newpool, *oldpool;
1356 struct pool_info *newpoolinfo;
1357 mirror_info_t *newmirrors;
1358 conf_t *conf = mddev_to_conf(mddev);
NeilBrown6ea9c072005-06-21 17:17:09 -07001359 int cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
NeilBrown6ea9c072005-06-21 17:17:09 -07001361 int d, d2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
NeilBrown6ea9c072005-06-21 17:17:09 -07001363 if (raid_disks < conf->raid_disks) {
1364 cnt=0;
1365 for (d= 0; d < conf->raid_disks; d++)
1366 if (conf->mirrors[d].rdev)
1367 cnt++;
1368 if (cnt > raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return -EBUSY;
NeilBrown6ea9c072005-06-21 17:17:09 -07001370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
1373 if (!newpoolinfo)
1374 return -ENOMEM;
1375 newpoolinfo->mddev = mddev;
1376 newpoolinfo->raid_disks = raid_disks;
1377
1378 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1379 r1bio_pool_free, newpoolinfo);
1380 if (!newpool) {
1381 kfree(newpoolinfo);
1382 return -ENOMEM;
1383 }
1384 newmirrors = kmalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
1385 if (!newmirrors) {
1386 kfree(newpoolinfo);
1387 mempool_destroy(newpool);
1388 return -ENOMEM;
1389 }
1390 memset(newmirrors, 0, sizeof(struct mirror_info)*raid_disks);
1391
1392 spin_lock_irq(&conf->resync_lock);
1393 conf->barrier++;
1394 wait_event_lock_irq(conf->wait_idle, !conf->nr_pending,
1395 conf->resync_lock, unplug_slaves(mddev));
1396 spin_unlock_irq(&conf->resync_lock);
1397
1398 /* ok, everything is stopped */
1399 oldpool = conf->r1bio_pool;
1400 conf->r1bio_pool = newpool;
NeilBrown6ea9c072005-06-21 17:17:09 -07001401
1402 for (d=d2=0; d < conf->raid_disks; d++)
1403 if (conf->mirrors[d].rdev) {
1404 conf->mirrors[d].rdev->raid_disk = d2;
1405 newmirrors[d2++].rdev = conf->mirrors[d].rdev;
1406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 kfree(conf->mirrors);
1408 conf->mirrors = newmirrors;
1409 kfree(conf->poolinfo);
1410 conf->poolinfo = newpoolinfo;
1411
1412 mddev->degraded += (raid_disks - conf->raid_disks);
1413 conf->raid_disks = mddev->raid_disks = raid_disks;
1414
NeilBrown6ea9c072005-06-21 17:17:09 -07001415 conf->last_used = 0; /* just make sure it is in-range */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 spin_lock_irq(&conf->resync_lock);
1417 conf->barrier--;
1418 spin_unlock_irq(&conf->resync_lock);
1419 wake_up(&conf->wait_resume);
1420 wake_up(&conf->wait_idle);
1421
1422
1423 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1424 md_wakeup_thread(mddev->thread);
1425
1426 mempool_destroy(oldpool);
1427 return 0;
1428}
1429
1430
1431static mdk_personality_t raid1_personality =
1432{
1433 .name = "raid1",
1434 .owner = THIS_MODULE,
1435 .make_request = make_request,
1436 .run = run,
1437 .stop = stop,
1438 .status = status,
1439 .error_handler = error,
1440 .hot_add_disk = raid1_add_disk,
1441 .hot_remove_disk= raid1_remove_disk,
1442 .spare_active = raid1_spare_active,
1443 .sync_request = sync_request,
1444 .resize = raid1_resize,
1445 .reshape = raid1_reshape,
1446};
1447
1448static int __init raid_init(void)
1449{
1450 return register_md_personality(RAID1, &raid1_personality);
1451}
1452
1453static void raid_exit(void)
1454{
1455 unregister_md_personality(RAID1);
1456}
1457
1458module_init(raid_init);
1459module_exit(raid_exit);
1460MODULE_LICENSE("GPL");
1461MODULE_ALIAS("md-personality-3"); /* RAID1 */