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