blob: 9d9acc3e51a7e7827d3bb4e256b090688e8b95ca [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 *
NeilBrown191ea9b2005-06-21 17:17:23 -070015 * 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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * 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
NeilBrown191ea9b2005-06-21 17:17:23 -070034#include "dm-bio-list.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/raid/raid1.h>
NeilBrown191ea9b2005-06-21 17:17:23 -070036#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/*
46 * Number of guaranteed r1bios in case of extreme VM load:
47 */
48#define NR_RAID1_BIOS 256
49
50static mdk_personality_t raid1_personality;
51
52static void unplug_slaves(mddev_t *mddev);
53
54
55static 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
71static 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
82static 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
122out_free_pages:
123 for ( ; i > 0 ; i--)
124 __free_page(bio->bi_io_vec[i-1].bv_page);
125out_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
132static 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
149static 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
161static 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
182static 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
203static 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 */
221static 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 */
233static 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
241static 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
291static 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 */
NeilBrown191ea9b2005-06-21 17:17:23 -0700308 if (!uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
NeilBrown191ea9b2005-06-21 17:17:23 -0700310 /* an I/O failed, we can't clear the bitmap */
311 set_bit(R1BIO_Degraded, &r1_bio->state);
312 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /*
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)) {
NeilBrown191ea9b2005-06-21 17:17:23 -0700332 /* 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 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 */
359static 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 const int sectors = r1_bio->sectors;
364 sector_t new_distance, current_distance;
365 mdk_rdev_t *new_rdev, *rdev;
366
367 rcu_read_lock();
368 /*
369 * Check if it if we can balance. We can balance on the whole
370 * device if no resync is going on, or below the resync window.
371 * We take the first readable disk when above the resync window.
372 */
373 retry:
374 if (conf->mddev->recovery_cp < MaxSector &&
375 (this_sector + sectors >= conf->next_resync)) {
376 /* Choose the first operation device, for consistancy */
377 new_disk = 0;
378
379 while ((new_rdev=conf->mirrors[new_disk].rdev) == NULL ||
380 !new_rdev->in_sync) {
381 new_disk++;
382 if (new_disk == conf->raid_disks) {
383 new_disk = -1;
384 break;
385 }
386 }
387 goto rb_out;
388 }
389
390
391 /* make sure the disk is operational */
392 while ((new_rdev=conf->mirrors[new_disk].rdev) == NULL ||
393 !new_rdev->in_sync) {
394 if (new_disk <= 0)
395 new_disk = conf->raid_disks;
396 new_disk--;
397 if (new_disk == disk) {
398 new_disk = -1;
399 goto rb_out;
400 }
401 }
402 disk = new_disk;
403 /* now disk == new_disk == starting point for search */
404
405 /*
406 * Don't change to another disk for sequential reads:
407 */
408 if (conf->next_seq_sect == this_sector)
409 goto rb_out;
410 if (this_sector == conf->mirrors[new_disk].head_position)
411 goto rb_out;
412
413 current_distance = abs(this_sector - conf->mirrors[disk].head_position);
414
415 /* Find the disk whose head is closest */
416
417 do {
418 if (disk <= 0)
419 disk = conf->raid_disks;
420 disk--;
421
422 if ((rdev=conf->mirrors[disk].rdev) == NULL ||
423 !rdev->in_sync)
424 continue;
425
426 if (!atomic_read(&rdev->nr_pending)) {
427 new_disk = disk;
428 new_rdev = rdev;
429 break;
430 }
431 new_distance = abs(this_sector - conf->mirrors[disk].head_position);
432 if (new_distance < current_distance) {
433 current_distance = new_distance;
434 new_disk = disk;
435 new_rdev = rdev;
436 }
437 } while (disk != conf->last_used);
438
439rb_out:
440
441
442 if (new_disk >= 0) {
443 conf->next_seq_sect = this_sector + sectors;
444 conf->last_used = new_disk;
445 atomic_inc(&new_rdev->nr_pending);
446 if (!new_rdev->in_sync) {
447 /* cannot risk returning a device that failed
448 * before we inc'ed nr_pending
449 */
450 atomic_dec(&new_rdev->nr_pending);
451 goto retry;
452 }
453 }
454 rcu_read_unlock();
455
456 return new_disk;
457}
458
459static void unplug_slaves(mddev_t *mddev)
460{
461 conf_t *conf = mddev_to_conf(mddev);
462 int i;
463
464 rcu_read_lock();
465 for (i=0; i<mddev->raid_disks; i++) {
466 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
467 if (rdev && !rdev->faulty && atomic_read(&rdev->nr_pending)) {
468 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
469
470 atomic_inc(&rdev->nr_pending);
471 rcu_read_unlock();
472
473 if (r_queue->unplug_fn)
474 r_queue->unplug_fn(r_queue);
475
476 rdev_dec_pending(rdev, mddev);
477 rcu_read_lock();
478 }
479 }
480 rcu_read_unlock();
481}
482
483static void raid1_unplug(request_queue_t *q)
484{
NeilBrown191ea9b2005-06-21 17:17:23 -0700485 mddev_t *mddev = q->queuedata;
486
487 unplug_slaves(mddev);
488 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
491static int raid1_issue_flush(request_queue_t *q, struct gendisk *disk,
492 sector_t *error_sector)
493{
494 mddev_t *mddev = q->queuedata;
495 conf_t *conf = mddev_to_conf(mddev);
496 int i, ret = 0;
497
498 rcu_read_lock();
499 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
500 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
501 if (rdev && !rdev->faulty) {
502 struct block_device *bdev = rdev->bdev;
503 request_queue_t *r_queue = bdev_get_queue(bdev);
504
505 if (!r_queue->issue_flush_fn)
506 ret = -EOPNOTSUPP;
507 else {
508 atomic_inc(&rdev->nr_pending);
509 rcu_read_unlock();
510 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
511 error_sector);
512 rdev_dec_pending(rdev, mddev);
513 rcu_read_lock();
514 }
515 }
516 }
517 rcu_read_unlock();
518 return ret;
519}
520
521/*
522 * Throttle resync depth, so that we can both get proper overlapping of
523 * requests, but are still able to handle normal requests quickly.
524 */
525#define RESYNC_DEPTH 32
526
527static void device_barrier(conf_t *conf, sector_t sect)
528{
529 spin_lock_irq(&conf->resync_lock);
530 wait_event_lock_irq(conf->wait_idle, !waitqueue_active(&conf->wait_resume),
NeilBrown191ea9b2005-06-21 17:17:23 -0700531 conf->resync_lock, raid1_unplug(conf->mddev->queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 if (!conf->barrier++) {
534 wait_event_lock_irq(conf->wait_idle, !conf->nr_pending,
NeilBrown191ea9b2005-06-21 17:17:23 -0700535 conf->resync_lock, raid1_unplug(conf->mddev->queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (conf->nr_pending)
537 BUG();
538 }
539 wait_event_lock_irq(conf->wait_resume, conf->barrier < RESYNC_DEPTH,
NeilBrown191ea9b2005-06-21 17:17:23 -0700540 conf->resync_lock, raid1_unplug(conf->mddev->queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 conf->next_resync = sect;
542 spin_unlock_irq(&conf->resync_lock);
543}
544
545static int make_request(request_queue_t *q, struct bio * bio)
546{
547 mddev_t *mddev = q->queuedata;
548 conf_t *conf = mddev_to_conf(mddev);
549 mirror_info_t *mirror;
550 r1bio_t *r1_bio;
551 struct bio *read_bio;
NeilBrown191ea9b2005-06-21 17:17:23 -0700552 int i, targets = 0, disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 mdk_rdev_t *rdev;
NeilBrown191ea9b2005-06-21 17:17:23 -0700554 struct bitmap *bitmap = mddev->bitmap;
555 unsigned long flags;
556 struct bio_list bl;
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 /*
560 * Register the new request and wait if the reconstruction
561 * thread has put up a bar for new requests.
562 * Continue immediately if no resync is active currently.
563 */
NeilBrown06d91a52005-06-21 17:17:12 -0700564 if (md_write_start(mddev, bio)==0)
565 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 spin_lock_irq(&conf->resync_lock);
567 wait_event_lock_irq(conf->wait_resume, !conf->barrier, conf->resync_lock, );
568 conf->nr_pending++;
569 spin_unlock_irq(&conf->resync_lock);
570
571 if (bio_data_dir(bio)==WRITE) {
572 disk_stat_inc(mddev->gendisk, writes);
573 disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bio));
574 } else {
575 disk_stat_inc(mddev->gendisk, reads);
576 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bio));
577 }
578
579 /*
580 * make_request() can abort the operation when READA is being
581 * used and no empty request is available.
582 *
583 */
584 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
585
586 r1_bio->master_bio = bio;
587 r1_bio->sectors = bio->bi_size >> 9;
NeilBrown191ea9b2005-06-21 17:17:23 -0700588 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 r1_bio->mddev = mddev;
590 r1_bio->sector = bio->bi_sector;
591
592 r1_bio->state = 0;
593
594 if (bio_data_dir(bio) == READ) {
595 /*
596 * read balancing logic:
597 */
598 int rdisk = read_balance(conf, r1_bio);
599
600 if (rdisk < 0) {
601 /* couldn't find anywhere to read from */
602 raid_end_bio_io(r1_bio);
603 return 0;
604 }
605 mirror = conf->mirrors + rdisk;
606
607 r1_bio->read_disk = rdisk;
608
609 read_bio = bio_clone(bio, GFP_NOIO);
610
611 r1_bio->bios[rdisk] = read_bio;
612
613 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
614 read_bio->bi_bdev = mirror->rdev->bdev;
615 read_bio->bi_end_io = raid1_end_read_request;
616 read_bio->bi_rw = READ;
617 read_bio->bi_private = r1_bio;
618
619 generic_make_request(read_bio);
620 return 0;
621 }
622
623 /*
624 * WRITE:
625 */
626 /* first select target devices under spinlock and
627 * inc refcount on their rdev. Record them by setting
628 * bios[x] to bio
629 */
630 disks = conf->raid_disks;
NeilBrown191ea9b2005-06-21 17:17:23 -0700631#if 0
632 { static int first=1;
633 if (first) printk("First Write sector %llu disks %d\n",
634 (unsigned long long)r1_bio->sector, disks);
635 first = 0;
636 }
637#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 rcu_read_lock();
639 for (i = 0; i < disks; i++) {
640 if ((rdev=conf->mirrors[i].rdev) != NULL &&
641 !rdev->faulty) {
642 atomic_inc(&rdev->nr_pending);
643 if (rdev->faulty) {
644 atomic_dec(&rdev->nr_pending);
645 r1_bio->bios[i] = NULL;
646 } else
647 r1_bio->bios[i] = bio;
NeilBrown191ea9b2005-06-21 17:17:23 -0700648 targets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 } else
650 r1_bio->bios[i] = NULL;
651 }
652 rcu_read_unlock();
653
NeilBrown191ea9b2005-06-21 17:17:23 -0700654 if (targets < conf->raid_disks) {
655 /* array is degraded, we will not clear the bitmap
656 * on I/O completion (see raid1_end_write_request) */
657 set_bit(R1BIO_Degraded, &r1_bio->state);
658 }
NeilBrown06d91a52005-06-21 17:17:12 -0700659
NeilBrown191ea9b2005-06-21 17:17:23 -0700660 atomic_set(&r1_bio->remaining, 0);
661
662 bio_list_init(&bl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 for (i = 0; i < disks; i++) {
664 struct bio *mbio;
665 if (!r1_bio->bios[i])
666 continue;
667
668 mbio = bio_clone(bio, GFP_NOIO);
669 r1_bio->bios[i] = mbio;
670
671 mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
672 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
673 mbio->bi_end_io = raid1_end_write_request;
674 mbio->bi_rw = WRITE;
675 mbio->bi_private = r1_bio;
676
677 atomic_inc(&r1_bio->remaining);
NeilBrown191ea9b2005-06-21 17:17:23 -0700678
679 bio_list_add(&bl, mbio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681
NeilBrown191ea9b2005-06-21 17:17:23 -0700682 bitmap_startwrite(bitmap, bio->bi_sector, r1_bio->sectors);
683 spin_lock_irqsave(&conf->device_lock, flags);
684 bio_list_merge(&conf->pending_bio_list, &bl);
685 bio_list_init(&bl);
686
687 blk_plug_device(mddev->queue);
688 spin_unlock_irqrestore(&conf->device_lock, flags);
689
690#if 0
691 while ((bio = bio_list_pop(&bl)) != NULL)
692 generic_make_request(bio);
693#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 return 0;
696}
697
698static void status(struct seq_file *seq, mddev_t *mddev)
699{
700 conf_t *conf = mddev_to_conf(mddev);
701 int i;
702
703 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
704 conf->working_disks);
705 for (i = 0; i < conf->raid_disks; i++)
706 seq_printf(seq, "%s",
707 conf->mirrors[i].rdev &&
708 conf->mirrors[i].rdev->in_sync ? "U" : "_");
709 seq_printf(seq, "]");
710}
711
712
713static void error(mddev_t *mddev, mdk_rdev_t *rdev)
714{
715 char b[BDEVNAME_SIZE];
716 conf_t *conf = mddev_to_conf(mddev);
717
718 /*
719 * If it is not operational, then we have already marked it as dead
720 * else if it is the last working disks, ignore the error, let the
721 * next level up know.
722 * else mark the drive as failed
723 */
724 if (rdev->in_sync
725 && conf->working_disks == 1)
726 /*
727 * Don't fail the drive, act as though we were just a
728 * normal single drive
729 */
730 return;
731 if (rdev->in_sync) {
732 mddev->degraded++;
733 conf->working_disks--;
734 /*
735 * if recovery is running, make sure it aborts.
736 */
737 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
738 }
739 rdev->in_sync = 0;
740 rdev->faulty = 1;
741 mddev->sb_dirty = 1;
742 printk(KERN_ALERT "raid1: Disk failure on %s, disabling device. \n"
743 " Operation continuing on %d devices\n",
744 bdevname(rdev->bdev,b), conf->working_disks);
745}
746
747static void print_conf(conf_t *conf)
748{
749 int i;
750 mirror_info_t *tmp;
751
752 printk("RAID1 conf printout:\n");
753 if (!conf) {
754 printk("(!conf)\n");
755 return;
756 }
757 printk(" --- wd:%d rd:%d\n", conf->working_disks,
758 conf->raid_disks);
759
760 for (i = 0; i < conf->raid_disks; i++) {
761 char b[BDEVNAME_SIZE];
762 tmp = conf->mirrors + i;
763 if (tmp->rdev)
764 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
765 i, !tmp->rdev->in_sync, !tmp->rdev->faulty,
766 bdevname(tmp->rdev->bdev,b));
767 }
768}
769
770static void close_sync(conf_t *conf)
771{
772 spin_lock_irq(&conf->resync_lock);
773 wait_event_lock_irq(conf->wait_resume, !conf->barrier,
NeilBrown191ea9b2005-06-21 17:17:23 -0700774 conf->resync_lock, raid1_unplug(conf->mddev->queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 spin_unlock_irq(&conf->resync_lock);
776
777 if (conf->barrier) BUG();
778 if (waitqueue_active(&conf->wait_idle)) BUG();
779
780 mempool_destroy(conf->r1buf_pool);
781 conf->r1buf_pool = NULL;
782}
783
784static int raid1_spare_active(mddev_t *mddev)
785{
786 int i;
787 conf_t *conf = mddev->private;
788 mirror_info_t *tmp;
789
790 /*
791 * Find all failed disks within the RAID1 configuration
792 * and mark them readable
793 */
794 for (i = 0; i < conf->raid_disks; i++) {
795 tmp = conf->mirrors + i;
796 if (tmp->rdev
797 && !tmp->rdev->faulty
798 && !tmp->rdev->in_sync) {
799 conf->working_disks++;
800 mddev->degraded--;
801 tmp->rdev->in_sync = 1;
802 }
803 }
804
805 print_conf(conf);
806 return 0;
807}
808
809
810static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
811{
812 conf_t *conf = mddev->private;
813 int found = 0;
814 int mirror;
815 mirror_info_t *p;
816
817 for (mirror=0; mirror < mddev->raid_disks; mirror++)
818 if ( !(p=conf->mirrors+mirror)->rdev) {
819
820 blk_queue_stack_limits(mddev->queue,
821 rdev->bdev->bd_disk->queue);
822 /* as we don't honour merge_bvec_fn, we must never risk
823 * violating it, so limit ->max_sector to one PAGE, as
824 * a one page request is never in violation.
825 */
826 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
827 mddev->queue->max_sectors > (PAGE_SIZE>>9))
828 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
829
830 p->head_position = 0;
831 rdev->raid_disk = mirror;
832 found = 1;
833 p->rdev = rdev;
834 break;
835 }
836
837 print_conf(conf);
838 return found;
839}
840
841static int raid1_remove_disk(mddev_t *mddev, int number)
842{
843 conf_t *conf = mddev->private;
844 int err = 0;
845 mdk_rdev_t *rdev;
846 mirror_info_t *p = conf->mirrors+ number;
847
848 print_conf(conf);
849 rdev = p->rdev;
850 if (rdev) {
851 if (rdev->in_sync ||
852 atomic_read(&rdev->nr_pending)) {
853 err = -EBUSY;
854 goto abort;
855 }
856 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -0700857 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (atomic_read(&rdev->nr_pending)) {
859 /* lost the race, try later */
860 err = -EBUSY;
861 p->rdev = rdev;
862 }
863 }
864abort:
865
866 print_conf(conf);
867 return err;
868}
869
870
871static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
872{
873 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
874 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
875 conf_t *conf = mddev_to_conf(r1_bio->mddev);
876
877 if (bio->bi_size)
878 return 1;
879
880 if (r1_bio->bios[r1_bio->read_disk] != bio)
881 BUG();
882 update_head_pos(r1_bio->read_disk, r1_bio);
883 /*
884 * we have read a block, now it needs to be re-written,
885 * or re-read if the read failed.
886 * We don't do much here, just schedule handling by raid1d
887 */
NeilBrown191ea9b2005-06-21 17:17:23 -0700888 if (!uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 md_error(r1_bio->mddev,
890 conf->mirrors[r1_bio->read_disk].rdev);
NeilBrown191ea9b2005-06-21 17:17:23 -0700891 set_bit(R1BIO_Degraded, &r1_bio->state);
892 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 set_bit(R1BIO_Uptodate, &r1_bio->state);
894 rdev_dec_pending(conf->mirrors[r1_bio->read_disk].rdev, conf->mddev);
895 reschedule_retry(r1_bio);
896 return 0;
897}
898
899static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
900{
901 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
902 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
903 mddev_t *mddev = r1_bio->mddev;
904 conf_t *conf = mddev_to_conf(mddev);
905 int i;
906 int mirror=0;
907
908 if (bio->bi_size)
909 return 1;
910
911 for (i = 0; i < conf->raid_disks; i++)
912 if (r1_bio->bios[i] == bio) {
913 mirror = i;
914 break;
915 }
NeilBrown191ea9b2005-06-21 17:17:23 -0700916 if (!uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 md_error(mddev, conf->mirrors[mirror].rdev);
NeilBrown191ea9b2005-06-21 17:17:23 -0700918 set_bit(R1BIO_Degraded, &r1_bio->state);
919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 update_head_pos(mirror, r1_bio);
921
922 if (atomic_dec_and_test(&r1_bio->remaining)) {
923 md_done_sync(mddev, r1_bio->sectors, uptodate);
924 put_buf(r1_bio);
925 }
926 rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
927 return 0;
928}
929
930static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
931{
932 conf_t *conf = mddev_to_conf(mddev);
933 int i;
934 int disks = conf->raid_disks;
935 struct bio *bio, *wbio;
936
937 bio = r1_bio->bios[r1_bio->read_disk];
938
NeilBrown191ea9b2005-06-21 17:17:23 -0700939/*
940 if (r1_bio->sector == 0) printk("First sync write startss\n");
941*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 /*
943 * schedule writes
944 */
945 if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
946 /*
947 * There is no point trying a read-for-reconstruct as
948 * reconstruct is about to be aborted
949 */
950 char b[BDEVNAME_SIZE];
951 printk(KERN_ALERT "raid1: %s: unrecoverable I/O read error"
952 " for block %llu\n",
953 bdevname(bio->bi_bdev,b),
954 (unsigned long long)r1_bio->sector);
955 md_done_sync(mddev, r1_bio->sectors, 0);
956 put_buf(r1_bio);
957 return;
958 }
959
960 atomic_set(&r1_bio->remaining, 1);
961 for (i = 0; i < disks ; i++) {
962 wbio = r1_bio->bios[i];
963 if (wbio->bi_end_io != end_sync_write)
964 continue;
965
966 atomic_inc(&conf->mirrors[i].rdev->nr_pending);
967 atomic_inc(&r1_bio->remaining);
968 md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
NeilBrown191ea9b2005-06-21 17:17:23 -0700969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 generic_make_request(wbio);
971 }
972
973 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown191ea9b2005-06-21 17:17:23 -0700974 /* if we're here, all write(s) have completed, so clean up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 md_done_sync(mddev, r1_bio->sectors, 1);
976 put_buf(r1_bio);
977 }
978}
979
980/*
981 * This is a kernel thread which:
982 *
983 * 1. Retries failed read operations on working mirrors.
984 * 2. Updates the raid superblock when problems encounter.
985 * 3. Performs writes following reads for array syncronising.
986 */
987
988static void raid1d(mddev_t *mddev)
989{
990 r1bio_t *r1_bio;
991 struct bio *bio;
992 unsigned long flags;
993 conf_t *conf = mddev_to_conf(mddev);
994 struct list_head *head = &conf->retry_list;
995 int unplug=0;
996 mdk_rdev_t *rdev;
997
998 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 for (;;) {
1001 char b[BDEVNAME_SIZE];
1002 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown191ea9b2005-06-21 17:17:23 -07001003
1004 if (conf->pending_bio_list.head) {
1005 bio = bio_list_get(&conf->pending_bio_list);
1006 blk_remove_plug(mddev->queue);
1007 spin_unlock_irqrestore(&conf->device_lock, flags);
1008 /* flush any pending bitmap writes to disk before proceeding w/ I/O */
1009 if (bitmap_unplug(mddev->bitmap) != 0)
1010 printk("%s: bitmap file write failed!\n", mdname(mddev));
1011
1012 while (bio) { /* submit pending writes */
1013 struct bio *next = bio->bi_next;
1014 bio->bi_next = NULL;
1015 generic_make_request(bio);
1016 bio = next;
1017 }
1018 unplug = 1;
1019
1020 continue;
1021 }
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (list_empty(head))
1024 break;
1025 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
1026 list_del(head->prev);
1027 spin_unlock_irqrestore(&conf->device_lock, flags);
1028
1029 mddev = r1_bio->mddev;
1030 conf = mddev_to_conf(mddev);
1031 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
1032 sync_request_write(mddev, r1_bio);
1033 unplug = 1;
1034 } else {
1035 int disk;
1036 bio = r1_bio->bios[r1_bio->read_disk];
1037 if ((disk=read_balance(conf, r1_bio)) == -1) {
1038 printk(KERN_ALERT "raid1: %s: unrecoverable I/O"
1039 " read error for block %llu\n",
1040 bdevname(bio->bi_bdev,b),
1041 (unsigned long long)r1_bio->sector);
1042 raid_end_bio_io(r1_bio);
1043 } else {
1044 r1_bio->bios[r1_bio->read_disk] = NULL;
1045 r1_bio->read_disk = disk;
1046 bio_put(bio);
1047 bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
1048 r1_bio->bios[r1_bio->read_disk] = bio;
1049 rdev = conf->mirrors[disk].rdev;
1050 if (printk_ratelimit())
1051 printk(KERN_ERR "raid1: %s: redirecting sector %llu to"
1052 " another mirror\n",
1053 bdevname(rdev->bdev,b),
1054 (unsigned long long)r1_bio->sector);
1055 bio->bi_sector = r1_bio->sector + rdev->data_offset;
1056 bio->bi_bdev = rdev->bdev;
1057 bio->bi_end_io = raid1_end_read_request;
1058 bio->bi_rw = READ;
1059 bio->bi_private = r1_bio;
1060 unplug = 1;
1061 generic_make_request(bio);
1062 }
1063 }
1064 }
1065 spin_unlock_irqrestore(&conf->device_lock, flags);
1066 if (unplug)
1067 unplug_slaves(mddev);
1068}
1069
1070
1071static int init_resync(conf_t *conf)
1072{
1073 int buffs;
1074
1075 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
1076 if (conf->r1buf_pool)
1077 BUG();
1078 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
1079 conf->poolinfo);
1080 if (!conf->r1buf_pool)
1081 return -ENOMEM;
1082 conf->next_resync = 0;
1083 return 0;
1084}
1085
1086/*
1087 * perform a "sync" on one "block"
1088 *
1089 * We need to make sure that no normal I/O request - particularly write
1090 * requests - conflict with active sync requests.
1091 *
1092 * This is achieved by tracking pending requests and a 'barrier' concept
1093 * that can be installed to exclude normal IO requests.
1094 */
1095
NeilBrown57afd892005-06-21 17:17:13 -07001096static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
1098 conf_t *conf = mddev_to_conf(mddev);
1099 mirror_info_t *mirror;
1100 r1bio_t *r1_bio;
1101 struct bio *bio;
1102 sector_t max_sector, nr_sectors;
1103 int disk;
1104 int i;
1105 int write_targets = 0;
NeilBrown191ea9b2005-06-21 17:17:23 -07001106 int sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 if (!conf->r1buf_pool)
NeilBrown191ea9b2005-06-21 17:17:23 -07001109 {
1110/*
1111 printk("sync start - bitmap %p\n", mddev->bitmap);
1112*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07001114 return 0;
NeilBrown191ea9b2005-06-21 17:17:23 -07001115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 max_sector = mddev->size << 1;
1118 if (sector_nr >= max_sector) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001119 /* If we aborted, we need to abort the
1120 * sync on the 'current' bitmap chunk (there will
1121 * only be one in raid1 resync.
1122 * We can find the current addess in mddev->curr_resync
1123 */
1124 if (!conf->fullsync) {
1125 if (mddev->curr_resync < max_sector)
1126 bitmap_end_sync(mddev->bitmap,
1127 mddev->curr_resync,
1128 &sync_blocks, 1);
1129 bitmap_close_sync(mddev->bitmap);
1130 }
1131 if (mddev->curr_resync >= max_sector)
1132 conf->fullsync = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 close_sync(conf);
1134 return 0;
1135 }
1136
NeilBrown191ea9b2005-06-21 17:17:23 -07001137 if (!conf->fullsync &&
1138 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks)) {
1139 /* We can skip this block, and probably several more */
1140 *skipped = 1;
1141 return sync_blocks;
1142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 /*
1144 * If there is non-resync activity waiting for us then
1145 * put in a delay to throttle resync.
1146 */
1147 if (!go_faster && waitqueue_active(&conf->wait_resume))
1148 msleep_interruptible(1000);
1149 device_barrier(conf, sector_nr + RESYNC_SECTORS);
1150
1151 /*
1152 * If reconstructing, and >1 working disc,
1153 * could dedicate one to rebuild and others to
1154 * service read requests ..
1155 */
1156 disk = conf->last_used;
1157 /* make sure disk is operational */
1158
1159 while (conf->mirrors[disk].rdev == NULL ||
1160 !conf->mirrors[disk].rdev->in_sync) {
1161 if (disk <= 0)
1162 disk = conf->raid_disks;
1163 disk--;
1164 if (disk == conf->last_used)
1165 break;
1166 }
1167 conf->last_used = disk;
1168 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
1169
1170
1171 mirror = conf->mirrors + disk;
1172
1173 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
1174
1175 spin_lock_irq(&conf->resync_lock);
1176 conf->nr_pending++;
1177 spin_unlock_irq(&conf->resync_lock);
1178
1179 r1_bio->mddev = mddev;
1180 r1_bio->sector = sector_nr;
NeilBrown191ea9b2005-06-21 17:17:23 -07001181 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 set_bit(R1BIO_IsSync, &r1_bio->state);
1183 r1_bio->read_disk = disk;
1184
1185 for (i=0; i < conf->raid_disks; i++) {
1186 bio = r1_bio->bios[i];
1187
1188 /* take from bio_init */
1189 bio->bi_next = NULL;
1190 bio->bi_flags |= 1 << BIO_UPTODATE;
1191 bio->bi_rw = 0;
1192 bio->bi_vcnt = 0;
1193 bio->bi_idx = 0;
1194 bio->bi_phys_segments = 0;
1195 bio->bi_hw_segments = 0;
1196 bio->bi_size = 0;
1197 bio->bi_end_io = NULL;
1198 bio->bi_private = NULL;
1199
1200 if (i == disk) {
1201 bio->bi_rw = READ;
1202 bio->bi_end_io = end_sync_read;
1203 } else if (conf->mirrors[i].rdev &&
1204 !conf->mirrors[i].rdev->faulty &&
1205 (!conf->mirrors[i].rdev->in_sync ||
1206 sector_nr + RESYNC_SECTORS > mddev->recovery_cp)) {
1207 bio->bi_rw = WRITE;
1208 bio->bi_end_io = end_sync_write;
1209 write_targets ++;
1210 } else
1211 continue;
1212 bio->bi_sector = sector_nr + conf->mirrors[i].rdev->data_offset;
1213 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1214 bio->bi_private = r1_bio;
1215 }
NeilBrown191ea9b2005-06-21 17:17:23 -07001216
1217 if (write_targets + 1 < conf->raid_disks)
1218 /* array degraded, can't clear bitmap */
1219 set_bit(R1BIO_Degraded, &r1_bio->state);
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (write_targets == 0) {
1222 /* There is nowhere to write, so all non-sync
1223 * drives must be failed - so we are finished
1224 */
NeilBrown57afd892005-06-21 17:17:13 -07001225 sector_t rv = max_sector - sector_nr;
1226 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 put_buf(r1_bio);
1228 rdev_dec_pending(conf->mirrors[disk].rdev, mddev);
1229 return rv;
1230 }
1231
1232 nr_sectors = 0;
1233 do {
1234 struct page *page;
1235 int len = PAGE_SIZE;
1236 if (sector_nr + (len>>9) > max_sector)
1237 len = (max_sector - sector_nr) << 9;
1238 if (len == 0)
1239 break;
NeilBrownab7a30c2005-06-21 17:17:23 -07001240 if (!conf->fullsync) {
1241 if (sync_blocks == 0) {
1242 if (!bitmap_start_sync(mddev->bitmap,
1243 sector_nr, &sync_blocks))
1244 break;
1245 if (sync_blocks < (PAGE_SIZE>>9))
1246 BUG();
1247 if (len > (sync_blocks<<9)) len = sync_blocks<<9;
1248 }
1249 }
NeilBrown191ea9b2005-06-21 17:17:23 -07001250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 for (i=0 ; i < conf->raid_disks; i++) {
1252 bio = r1_bio->bios[i];
1253 if (bio->bi_end_io) {
1254 page = r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page;
1255 if (bio_add_page(bio, page, len, 0) == 0) {
1256 /* stop here */
1257 r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page = page;
1258 while (i > 0) {
1259 i--;
1260 bio = r1_bio->bios[i];
1261 if (bio->bi_end_io==NULL) continue;
1262 /* remove last page from this bio */
1263 bio->bi_vcnt--;
1264 bio->bi_size -= len;
1265 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
1266 }
1267 goto bio_full;
1268 }
1269 }
1270 }
1271 nr_sectors += len>>9;
1272 sector_nr += len>>9;
NeilBrown191ea9b2005-06-21 17:17:23 -07001273 sync_blocks -= (len>>9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
1275 bio_full:
1276 bio = r1_bio->bios[disk];
1277 r1_bio->sectors = nr_sectors;
1278
1279 md_sync_acct(mirror->rdev->bdev, nr_sectors);
1280
1281 generic_make_request(bio);
1282
1283 return nr_sectors;
1284}
1285
1286static int run(mddev_t *mddev)
1287{
1288 conf_t *conf;
1289 int i, j, disk_idx;
1290 mirror_info_t *disk;
1291 mdk_rdev_t *rdev;
1292 struct list_head *tmp;
1293
1294 if (mddev->level != 1) {
1295 printk("raid1: %s: raid level not set to mirroring (%d)\n",
1296 mdname(mddev), mddev->level);
1297 goto out;
1298 }
1299 /*
1300 * copy the already verified devices into our private RAID1
1301 * bookkeeping area. [whatever we allocate in run(),
1302 * should be freed in stop()]
1303 */
1304 conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
1305 mddev->private = conf;
1306 if (!conf)
1307 goto out_no_mem;
1308
1309 memset(conf, 0, sizeof(*conf));
1310 conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks,
1311 GFP_KERNEL);
1312 if (!conf->mirrors)
1313 goto out_no_mem;
1314
1315 memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
1316
1317 conf->poolinfo = kmalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
1318 if (!conf->poolinfo)
1319 goto out_no_mem;
1320 conf->poolinfo->mddev = mddev;
1321 conf->poolinfo->raid_disks = mddev->raid_disks;
1322 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1323 r1bio_pool_free,
1324 conf->poolinfo);
1325 if (!conf->r1bio_pool)
1326 goto out_no_mem;
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 ITERATE_RDEV(mddev, rdev, tmp) {
1329 disk_idx = rdev->raid_disk;
1330 if (disk_idx >= mddev->raid_disks
1331 || disk_idx < 0)
1332 continue;
1333 disk = conf->mirrors + disk_idx;
1334
1335 disk->rdev = rdev;
1336
1337 blk_queue_stack_limits(mddev->queue,
1338 rdev->bdev->bd_disk->queue);
1339 /* as we don't honour merge_bvec_fn, we must never risk
1340 * violating it, so limit ->max_sector to one PAGE, as
1341 * a one page request is never in violation.
1342 */
1343 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1344 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1345 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
1346
1347 disk->head_position = 0;
1348 if (!rdev->faulty && rdev->in_sync)
1349 conf->working_disks++;
1350 }
1351 conf->raid_disks = mddev->raid_disks;
1352 conf->mddev = mddev;
1353 spin_lock_init(&conf->device_lock);
1354 INIT_LIST_HEAD(&conf->retry_list);
1355 if (conf->working_disks == 1)
1356 mddev->recovery_cp = MaxSector;
1357
1358 spin_lock_init(&conf->resync_lock);
1359 init_waitqueue_head(&conf->wait_idle);
1360 init_waitqueue_head(&conf->wait_resume);
1361
NeilBrown191ea9b2005-06-21 17:17:23 -07001362 bio_list_init(&conf->pending_bio_list);
1363 bio_list_init(&conf->flushing_bio_list);
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (!conf->working_disks) {
1366 printk(KERN_ERR "raid1: no operational mirrors for %s\n",
1367 mdname(mddev));
1368 goto out_free_conf;
1369 }
1370
1371 mddev->degraded = 0;
1372 for (i = 0; i < conf->raid_disks; i++) {
1373
1374 disk = conf->mirrors + i;
1375
1376 if (!disk->rdev) {
1377 disk->head_position = 0;
1378 mddev->degraded++;
1379 }
1380 }
1381
1382 /*
1383 * find the first working one and use it as a starting point
1384 * to read balancing.
1385 */
1386 for (j = 0; j < conf->raid_disks &&
1387 (!conf->mirrors[j].rdev ||
1388 !conf->mirrors[j].rdev->in_sync) ; j++)
1389 /* nothing */;
1390 conf->last_used = j;
1391
1392
NeilBrown191ea9b2005-06-21 17:17:23 -07001393 mddev->thread = md_register_thread(raid1d, mddev, "%s_raid1");
1394 if (!mddev->thread) {
1395 printk(KERN_ERR
1396 "raid1: couldn't allocate thread for %s\n",
1397 mdname(mddev));
1398 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
NeilBrown191ea9b2005-06-21 17:17:23 -07001400 if (mddev->bitmap) mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
1401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 printk(KERN_INFO
1403 "raid1: raid set %s active with %d out of %d mirrors\n",
1404 mdname(mddev), mddev->raid_disks - mddev->degraded,
1405 mddev->raid_disks);
1406 /*
1407 * Ok, everything is just fine now
1408 */
1409 mddev->array_size = mddev->size;
1410
NeilBrown7a5febe2005-05-16 21:53:16 -07001411 mddev->queue->unplug_fn = raid1_unplug;
1412 mddev->queue->issue_flush_fn = raid1_issue_flush;
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return 0;
1415
1416out_no_mem:
1417 printk(KERN_ERR "raid1: couldn't allocate memory for %s\n",
1418 mdname(mddev));
1419
1420out_free_conf:
1421 if (conf) {
1422 if (conf->r1bio_pool)
1423 mempool_destroy(conf->r1bio_pool);
1424 if (conf->mirrors)
1425 kfree(conf->mirrors);
1426 if (conf->poolinfo)
1427 kfree(conf->poolinfo);
1428 kfree(conf);
1429 mddev->private = NULL;
1430 }
1431out:
1432 return -EIO;
1433}
1434
1435static int stop(mddev_t *mddev)
1436{
1437 conf_t *conf = mddev_to_conf(mddev);
1438
1439 md_unregister_thread(mddev->thread);
1440 mddev->thread = NULL;
1441 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
1442 if (conf->r1bio_pool)
1443 mempool_destroy(conf->r1bio_pool);
1444 if (conf->mirrors)
1445 kfree(conf->mirrors);
1446 if (conf->poolinfo)
1447 kfree(conf->poolinfo);
1448 kfree(conf);
1449 mddev->private = NULL;
1450 return 0;
1451}
1452
1453static int raid1_resize(mddev_t *mddev, sector_t sectors)
1454{
1455 /* no resync is happening, and there is enough space
1456 * on all devices, so we can resize.
1457 * We need to make sure resync covers any new space.
1458 * If the array is shrinking we should possibly wait until
1459 * any io in the removed space completes, but it hardly seems
1460 * worth it.
1461 */
1462 mddev->array_size = sectors>>1;
1463 set_capacity(mddev->gendisk, mddev->array_size << 1);
1464 mddev->changed = 1;
1465 if (mddev->array_size > mddev->size && mddev->recovery_cp == MaxSector) {
1466 mddev->recovery_cp = mddev->size << 1;
1467 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1468 }
1469 mddev->size = mddev->array_size;
1470 return 0;
1471}
1472
1473static int raid1_reshape(mddev_t *mddev, int raid_disks)
1474{
1475 /* We need to:
1476 * 1/ resize the r1bio_pool
1477 * 2/ resize conf->mirrors
1478 *
1479 * We allocate a new r1bio_pool if we can.
1480 * Then raise a device barrier and wait until all IO stops.
1481 * Then resize conf->mirrors and swap in the new r1bio pool.
NeilBrown6ea9c072005-06-21 17:17:09 -07001482 *
1483 * At the same time, we "pack" the devices so that all the missing
1484 * devices have the higher raid_disk numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 */
1486 mempool_t *newpool, *oldpool;
1487 struct pool_info *newpoolinfo;
1488 mirror_info_t *newmirrors;
1489 conf_t *conf = mddev_to_conf(mddev);
NeilBrown6ea9c072005-06-21 17:17:09 -07001490 int cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
NeilBrown6ea9c072005-06-21 17:17:09 -07001492 int d, d2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
NeilBrown6ea9c072005-06-21 17:17:09 -07001494 if (raid_disks < conf->raid_disks) {
1495 cnt=0;
1496 for (d= 0; d < conf->raid_disks; d++)
1497 if (conf->mirrors[d].rdev)
1498 cnt++;
1499 if (cnt > raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return -EBUSY;
NeilBrown6ea9c072005-06-21 17:17:09 -07001501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
1504 if (!newpoolinfo)
1505 return -ENOMEM;
1506 newpoolinfo->mddev = mddev;
1507 newpoolinfo->raid_disks = raid_disks;
1508
1509 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1510 r1bio_pool_free, newpoolinfo);
1511 if (!newpool) {
1512 kfree(newpoolinfo);
1513 return -ENOMEM;
1514 }
1515 newmirrors = kmalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
1516 if (!newmirrors) {
1517 kfree(newpoolinfo);
1518 mempool_destroy(newpool);
1519 return -ENOMEM;
1520 }
1521 memset(newmirrors, 0, sizeof(struct mirror_info)*raid_disks);
1522
1523 spin_lock_irq(&conf->resync_lock);
1524 conf->barrier++;
1525 wait_event_lock_irq(conf->wait_idle, !conf->nr_pending,
NeilBrown191ea9b2005-06-21 17:17:23 -07001526 conf->resync_lock, raid1_unplug(mddev->queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 spin_unlock_irq(&conf->resync_lock);
1528
1529 /* ok, everything is stopped */
1530 oldpool = conf->r1bio_pool;
1531 conf->r1bio_pool = newpool;
NeilBrown6ea9c072005-06-21 17:17:09 -07001532
1533 for (d=d2=0; d < conf->raid_disks; d++)
1534 if (conf->mirrors[d].rdev) {
1535 conf->mirrors[d].rdev->raid_disk = d2;
1536 newmirrors[d2++].rdev = conf->mirrors[d].rdev;
1537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 kfree(conf->mirrors);
1539 conf->mirrors = newmirrors;
1540 kfree(conf->poolinfo);
1541 conf->poolinfo = newpoolinfo;
1542
1543 mddev->degraded += (raid_disks - conf->raid_disks);
1544 conf->raid_disks = mddev->raid_disks = raid_disks;
1545
NeilBrown6ea9c072005-06-21 17:17:09 -07001546 conf->last_used = 0; /* just make sure it is in-range */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 spin_lock_irq(&conf->resync_lock);
1548 conf->barrier--;
1549 spin_unlock_irq(&conf->resync_lock);
1550 wake_up(&conf->wait_resume);
1551 wake_up(&conf->wait_idle);
1552
1553
1554 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1555 md_wakeup_thread(mddev->thread);
1556
1557 mempool_destroy(oldpool);
1558 return 0;
1559}
1560
1561
1562static mdk_personality_t raid1_personality =
1563{
1564 .name = "raid1",
1565 .owner = THIS_MODULE,
1566 .make_request = make_request,
1567 .run = run,
1568 .stop = stop,
1569 .status = status,
1570 .error_handler = error,
1571 .hot_add_disk = raid1_add_disk,
1572 .hot_remove_disk= raid1_remove_disk,
1573 .spare_active = raid1_spare_active,
1574 .sync_request = sync_request,
1575 .resize = raid1_resize,
1576 .reshape = raid1_reshape,
1577};
1578
1579static int __init raid_init(void)
1580{
1581 return register_md_personality(RAID1, &raid1_personality);
1582}
1583
1584static void raid_exit(void)
1585{
1586 unregister_md_personality(RAID1);
1587}
1588
1589module_init(raid_init);
1590module_exit(raid_exit);
1591MODULE_LICENSE("GPL");
1592MODULE_ALIAS("md-personality-3"); /* RAID1 */