blob: ace41c571aeb3f2e842d65fc2fa9959240bf4054 [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
NeilBrowne5dcdd82005-09-09 16:23:41 -0700558 if (unlikely(bio_barrier(bio))) {
559 bio_endio(bio, bio->bi_size, -EOPNOTSUPP);
560 return 0;
561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 /*
564 * Register the new request and wait if the reconstruction
565 * thread has put up a bar for new requests.
566 * Continue immediately if no resync is active currently.
567 */
NeilBrown3d310eb2005-06-21 17:17:26 -0700568 md_write_start(mddev, bio); /* wait on superblock update early */
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 spin_lock_irq(&conf->resync_lock);
571 wait_event_lock_irq(conf->wait_resume, !conf->barrier, conf->resync_lock, );
572 conf->nr_pending++;
573 spin_unlock_irq(&conf->resync_lock);
574
575 if (bio_data_dir(bio)==WRITE) {
576 disk_stat_inc(mddev->gendisk, writes);
577 disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bio));
578 } else {
579 disk_stat_inc(mddev->gendisk, reads);
580 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bio));
581 }
582
583 /*
584 * make_request() can abort the operation when READA is being
585 * used and no empty request is available.
586 *
587 */
588 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
589
590 r1_bio->master_bio = bio;
591 r1_bio->sectors = bio->bi_size >> 9;
NeilBrown191ea9b2005-06-21 17:17:23 -0700592 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 r1_bio->mddev = mddev;
594 r1_bio->sector = bio->bi_sector;
595
596 r1_bio->state = 0;
597
598 if (bio_data_dir(bio) == READ) {
599 /*
600 * read balancing logic:
601 */
602 int rdisk = read_balance(conf, r1_bio);
603
604 if (rdisk < 0) {
605 /* couldn't find anywhere to read from */
606 raid_end_bio_io(r1_bio);
607 return 0;
608 }
609 mirror = conf->mirrors + rdisk;
610
611 r1_bio->read_disk = rdisk;
612
613 read_bio = bio_clone(bio, GFP_NOIO);
614
615 r1_bio->bios[rdisk] = read_bio;
616
617 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
618 read_bio->bi_bdev = mirror->rdev->bdev;
619 read_bio->bi_end_io = raid1_end_read_request;
620 read_bio->bi_rw = READ;
621 read_bio->bi_private = r1_bio;
622
623 generic_make_request(read_bio);
624 return 0;
625 }
626
627 /*
628 * WRITE:
629 */
630 /* first select target devices under spinlock and
631 * inc refcount on their rdev. Record them by setting
632 * bios[x] to bio
633 */
634 disks = conf->raid_disks;
NeilBrown191ea9b2005-06-21 17:17:23 -0700635#if 0
636 { static int first=1;
637 if (first) printk("First Write sector %llu disks %d\n",
638 (unsigned long long)r1_bio->sector, disks);
639 first = 0;
640 }
641#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 rcu_read_lock();
643 for (i = 0; i < disks; i++) {
644 if ((rdev=conf->mirrors[i].rdev) != NULL &&
645 !rdev->faulty) {
646 atomic_inc(&rdev->nr_pending);
647 if (rdev->faulty) {
648 atomic_dec(&rdev->nr_pending);
649 r1_bio->bios[i] = NULL;
650 } else
651 r1_bio->bios[i] = bio;
NeilBrown191ea9b2005-06-21 17:17:23 -0700652 targets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 } else
654 r1_bio->bios[i] = NULL;
655 }
656 rcu_read_unlock();
657
NeilBrown191ea9b2005-06-21 17:17:23 -0700658 if (targets < conf->raid_disks) {
659 /* array is degraded, we will not clear the bitmap
660 * on I/O completion (see raid1_end_write_request) */
661 set_bit(R1BIO_Degraded, &r1_bio->state);
662 }
NeilBrown06d91a52005-06-21 17:17:12 -0700663
NeilBrown191ea9b2005-06-21 17:17:23 -0700664 atomic_set(&r1_bio->remaining, 0);
665
666 bio_list_init(&bl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 for (i = 0; i < disks; i++) {
668 struct bio *mbio;
669 if (!r1_bio->bios[i])
670 continue;
671
672 mbio = bio_clone(bio, GFP_NOIO);
673 r1_bio->bios[i] = mbio;
674
675 mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
676 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
677 mbio->bi_end_io = raid1_end_write_request;
678 mbio->bi_rw = WRITE;
679 mbio->bi_private = r1_bio;
680
681 atomic_inc(&r1_bio->remaining);
NeilBrown191ea9b2005-06-21 17:17:23 -0700682
683 bio_list_add(&bl, mbio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685
NeilBrown191ea9b2005-06-21 17:17:23 -0700686 bitmap_startwrite(bitmap, bio->bi_sector, r1_bio->sectors);
687 spin_lock_irqsave(&conf->device_lock, flags);
688 bio_list_merge(&conf->pending_bio_list, &bl);
689 bio_list_init(&bl);
690
691 blk_plug_device(mddev->queue);
692 spin_unlock_irqrestore(&conf->device_lock, flags);
693
694#if 0
695 while ((bio = bio_list_pop(&bl)) != NULL)
696 generic_make_request(bio);
697#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 return 0;
700}
701
702static void status(struct seq_file *seq, mddev_t *mddev)
703{
704 conf_t *conf = mddev_to_conf(mddev);
705 int i;
706
707 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
708 conf->working_disks);
709 for (i = 0; i < conf->raid_disks; i++)
710 seq_printf(seq, "%s",
711 conf->mirrors[i].rdev &&
712 conf->mirrors[i].rdev->in_sync ? "U" : "_");
713 seq_printf(seq, "]");
714}
715
716
717static void error(mddev_t *mddev, mdk_rdev_t *rdev)
718{
719 char b[BDEVNAME_SIZE];
720 conf_t *conf = mddev_to_conf(mddev);
721
722 /*
723 * If it is not operational, then we have already marked it as dead
724 * else if it is the last working disks, ignore the error, let the
725 * next level up know.
726 * else mark the drive as failed
727 */
728 if (rdev->in_sync
729 && conf->working_disks == 1)
730 /*
731 * Don't fail the drive, act as though we were just a
732 * normal single drive
733 */
734 return;
735 if (rdev->in_sync) {
736 mddev->degraded++;
737 conf->working_disks--;
738 /*
739 * if recovery is running, make sure it aborts.
740 */
741 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
742 }
743 rdev->in_sync = 0;
744 rdev->faulty = 1;
745 mddev->sb_dirty = 1;
746 printk(KERN_ALERT "raid1: Disk failure on %s, disabling device. \n"
747 " Operation continuing on %d devices\n",
748 bdevname(rdev->bdev,b), conf->working_disks);
749}
750
751static void print_conf(conf_t *conf)
752{
753 int i;
754 mirror_info_t *tmp;
755
756 printk("RAID1 conf printout:\n");
757 if (!conf) {
758 printk("(!conf)\n");
759 return;
760 }
761 printk(" --- wd:%d rd:%d\n", conf->working_disks,
762 conf->raid_disks);
763
764 for (i = 0; i < conf->raid_disks; i++) {
765 char b[BDEVNAME_SIZE];
766 tmp = conf->mirrors + i;
767 if (tmp->rdev)
768 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
769 i, !tmp->rdev->in_sync, !tmp->rdev->faulty,
770 bdevname(tmp->rdev->bdev,b));
771 }
772}
773
774static void close_sync(conf_t *conf)
775{
776 spin_lock_irq(&conf->resync_lock);
777 wait_event_lock_irq(conf->wait_resume, !conf->barrier,
NeilBrown191ea9b2005-06-21 17:17:23 -0700778 conf->resync_lock, raid1_unplug(conf->mddev->queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 spin_unlock_irq(&conf->resync_lock);
780
781 if (conf->barrier) BUG();
782 if (waitqueue_active(&conf->wait_idle)) BUG();
783
784 mempool_destroy(conf->r1buf_pool);
785 conf->r1buf_pool = NULL;
786}
787
788static int raid1_spare_active(mddev_t *mddev)
789{
790 int i;
791 conf_t *conf = mddev->private;
792 mirror_info_t *tmp;
793
794 /*
795 * Find all failed disks within the RAID1 configuration
796 * and mark them readable
797 */
798 for (i = 0; i < conf->raid_disks; i++) {
799 tmp = conf->mirrors + i;
800 if (tmp->rdev
801 && !tmp->rdev->faulty
802 && !tmp->rdev->in_sync) {
803 conf->working_disks++;
804 mddev->degraded--;
805 tmp->rdev->in_sync = 1;
806 }
807 }
808
809 print_conf(conf);
810 return 0;
811}
812
813
814static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
815{
816 conf_t *conf = mddev->private;
817 int found = 0;
NeilBrown41158c72005-06-21 17:17:25 -0700818 int mirror = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 mirror_info_t *p;
820
NeilBrown41158c72005-06-21 17:17:25 -0700821 if (rdev->saved_raid_disk >= 0 &&
822 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
823 mirror = rdev->saved_raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 for (mirror=0; mirror < mddev->raid_disks; mirror++)
825 if ( !(p=conf->mirrors+mirror)->rdev) {
826
827 blk_queue_stack_limits(mddev->queue,
828 rdev->bdev->bd_disk->queue);
829 /* as we don't honour merge_bvec_fn, we must never risk
830 * violating it, so limit ->max_sector to one PAGE, as
831 * a one page request is never in violation.
832 */
833 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
834 mddev->queue->max_sectors > (PAGE_SIZE>>9))
835 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
836
837 p->head_position = 0;
838 rdev->raid_disk = mirror;
839 found = 1;
NeilBrown41158c72005-06-21 17:17:25 -0700840 if (rdev->saved_raid_disk != mirror)
841 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 p->rdev = rdev;
843 break;
844 }
845
846 print_conf(conf);
847 return found;
848}
849
850static int raid1_remove_disk(mddev_t *mddev, int number)
851{
852 conf_t *conf = mddev->private;
853 int err = 0;
854 mdk_rdev_t *rdev;
855 mirror_info_t *p = conf->mirrors+ number;
856
857 print_conf(conf);
858 rdev = p->rdev;
859 if (rdev) {
860 if (rdev->in_sync ||
861 atomic_read(&rdev->nr_pending)) {
862 err = -EBUSY;
863 goto abort;
864 }
865 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -0700866 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (atomic_read(&rdev->nr_pending)) {
868 /* lost the race, try later */
869 err = -EBUSY;
870 p->rdev = rdev;
871 }
872 }
873abort:
874
875 print_conf(conf);
876 return err;
877}
878
879
880static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
881{
882 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
883 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
884 conf_t *conf = mddev_to_conf(r1_bio->mddev);
885
886 if (bio->bi_size)
887 return 1;
888
889 if (r1_bio->bios[r1_bio->read_disk] != bio)
890 BUG();
891 update_head_pos(r1_bio->read_disk, r1_bio);
892 /*
893 * we have read a block, now it needs to be re-written,
894 * or re-read if the read failed.
895 * We don't do much here, just schedule handling by raid1d
896 */
NeilBrown191ea9b2005-06-21 17:17:23 -0700897 if (!uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 md_error(r1_bio->mddev,
899 conf->mirrors[r1_bio->read_disk].rdev);
NeilBrown191ea9b2005-06-21 17:17:23 -0700900 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 set_bit(R1BIO_Uptodate, &r1_bio->state);
902 rdev_dec_pending(conf->mirrors[r1_bio->read_disk].rdev, conf->mddev);
903 reschedule_retry(r1_bio);
904 return 0;
905}
906
907static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
908{
909 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
910 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
911 mddev_t *mddev = r1_bio->mddev;
912 conf_t *conf = mddev_to_conf(mddev);
913 int i;
914 int mirror=0;
915
916 if (bio->bi_size)
917 return 1;
918
919 for (i = 0; i < conf->raid_disks; i++)
920 if (r1_bio->bios[i] == bio) {
921 mirror = i;
922 break;
923 }
NeilBrowne3b97032005-08-04 12:53:34 -0700924 if (!uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 md_error(mddev, conf->mirrors[mirror].rdev);
NeilBrowne3b97032005-08-04 12:53:34 -0700926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 update_head_pos(mirror, r1_bio);
928
929 if (atomic_dec_and_test(&r1_bio->remaining)) {
930 md_done_sync(mddev, r1_bio->sectors, uptodate);
931 put_buf(r1_bio);
932 }
933 rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
934 return 0;
935}
936
937static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
938{
939 conf_t *conf = mddev_to_conf(mddev);
940 int i;
941 int disks = conf->raid_disks;
942 struct bio *bio, *wbio;
943
944 bio = r1_bio->bios[r1_bio->read_disk];
945
NeilBrown191ea9b2005-06-21 17:17:23 -0700946/*
947 if (r1_bio->sector == 0) printk("First sync write startss\n");
948*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 /*
950 * schedule writes
951 */
952 if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
953 /*
954 * There is no point trying a read-for-reconstruct as
955 * reconstruct is about to be aborted
956 */
957 char b[BDEVNAME_SIZE];
958 printk(KERN_ALERT "raid1: %s: unrecoverable I/O read error"
959 " for block %llu\n",
960 bdevname(bio->bi_bdev,b),
961 (unsigned long long)r1_bio->sector);
962 md_done_sync(mddev, r1_bio->sectors, 0);
963 put_buf(r1_bio);
964 return;
965 }
966
967 atomic_set(&r1_bio->remaining, 1);
968 for (i = 0; i < disks ; i++) {
969 wbio = r1_bio->bios[i];
970 if (wbio->bi_end_io != end_sync_write)
971 continue;
972
973 atomic_inc(&conf->mirrors[i].rdev->nr_pending);
974 atomic_inc(&r1_bio->remaining);
975 md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
NeilBrown191ea9b2005-06-21 17:17:23 -0700976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 generic_make_request(wbio);
978 }
979
980 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown191ea9b2005-06-21 17:17:23 -0700981 /* if we're here, all write(s) have completed, so clean up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 md_done_sync(mddev, r1_bio->sectors, 1);
983 put_buf(r1_bio);
984 }
985}
986
987/*
988 * This is a kernel thread which:
989 *
990 * 1. Retries failed read operations on working mirrors.
991 * 2. Updates the raid superblock when problems encounter.
992 * 3. Performs writes following reads for array syncronising.
993 */
994
995static void raid1d(mddev_t *mddev)
996{
997 r1bio_t *r1_bio;
998 struct bio *bio;
999 unsigned long flags;
1000 conf_t *conf = mddev_to_conf(mddev);
1001 struct list_head *head = &conf->retry_list;
1002 int unplug=0;
1003 mdk_rdev_t *rdev;
1004
1005 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 for (;;) {
1008 char b[BDEVNAME_SIZE];
1009 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown191ea9b2005-06-21 17:17:23 -07001010
1011 if (conf->pending_bio_list.head) {
1012 bio = bio_list_get(&conf->pending_bio_list);
1013 blk_remove_plug(mddev->queue);
1014 spin_unlock_irqrestore(&conf->device_lock, flags);
1015 /* flush any pending bitmap writes to disk before proceeding w/ I/O */
1016 if (bitmap_unplug(mddev->bitmap) != 0)
1017 printk("%s: bitmap file write failed!\n", mdname(mddev));
1018
1019 while (bio) { /* submit pending writes */
1020 struct bio *next = bio->bi_next;
1021 bio->bi_next = NULL;
1022 generic_make_request(bio);
1023 bio = next;
1024 }
1025 unplug = 1;
1026
1027 continue;
1028 }
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (list_empty(head))
1031 break;
1032 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
1033 list_del(head->prev);
1034 spin_unlock_irqrestore(&conf->device_lock, flags);
1035
1036 mddev = r1_bio->mddev;
1037 conf = mddev_to_conf(mddev);
1038 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
1039 sync_request_write(mddev, r1_bio);
1040 unplug = 1;
1041 } else {
1042 int disk;
1043 bio = r1_bio->bios[r1_bio->read_disk];
1044 if ((disk=read_balance(conf, r1_bio)) == -1) {
1045 printk(KERN_ALERT "raid1: %s: unrecoverable I/O"
1046 " read error for block %llu\n",
1047 bdevname(bio->bi_bdev,b),
1048 (unsigned long long)r1_bio->sector);
1049 raid_end_bio_io(r1_bio);
1050 } else {
1051 r1_bio->bios[r1_bio->read_disk] = NULL;
1052 r1_bio->read_disk = disk;
1053 bio_put(bio);
1054 bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
1055 r1_bio->bios[r1_bio->read_disk] = bio;
1056 rdev = conf->mirrors[disk].rdev;
1057 if (printk_ratelimit())
1058 printk(KERN_ERR "raid1: %s: redirecting sector %llu to"
1059 " another mirror\n",
1060 bdevname(rdev->bdev,b),
1061 (unsigned long long)r1_bio->sector);
1062 bio->bi_sector = r1_bio->sector + rdev->data_offset;
1063 bio->bi_bdev = rdev->bdev;
1064 bio->bi_end_io = raid1_end_read_request;
1065 bio->bi_rw = READ;
1066 bio->bi_private = r1_bio;
1067 unplug = 1;
1068 generic_make_request(bio);
1069 }
1070 }
1071 }
1072 spin_unlock_irqrestore(&conf->device_lock, flags);
1073 if (unplug)
1074 unplug_slaves(mddev);
1075}
1076
1077
1078static int init_resync(conf_t *conf)
1079{
1080 int buffs;
1081
1082 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
1083 if (conf->r1buf_pool)
1084 BUG();
1085 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
1086 conf->poolinfo);
1087 if (!conf->r1buf_pool)
1088 return -ENOMEM;
1089 conf->next_resync = 0;
1090 return 0;
1091}
1092
1093/*
1094 * perform a "sync" on one "block"
1095 *
1096 * We need to make sure that no normal I/O request - particularly write
1097 * requests - conflict with active sync requests.
1098 *
1099 * This is achieved by tracking pending requests and a 'barrier' concept
1100 * that can be installed to exclude normal IO requests.
1101 */
1102
NeilBrown57afd892005-06-21 17:17:13 -07001103static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 conf_t *conf = mddev_to_conf(mddev);
1106 mirror_info_t *mirror;
1107 r1bio_t *r1_bio;
1108 struct bio *bio;
1109 sector_t max_sector, nr_sectors;
1110 int disk;
1111 int i;
1112 int write_targets = 0;
NeilBrown191ea9b2005-06-21 17:17:23 -07001113 int sync_blocks;
NeilBrowne3b97032005-08-04 12:53:34 -07001114 int still_degraded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 if (!conf->r1buf_pool)
NeilBrown191ea9b2005-06-21 17:17:23 -07001117 {
1118/*
1119 printk("sync start - bitmap %p\n", mddev->bitmap);
1120*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07001122 return 0;
NeilBrown191ea9b2005-06-21 17:17:23 -07001123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 max_sector = mddev->size << 1;
1126 if (sector_nr >= max_sector) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001127 /* If we aborted, we need to abort the
1128 * sync on the 'current' bitmap chunk (there will
1129 * only be one in raid1 resync.
1130 * We can find the current addess in mddev->curr_resync
1131 */
NeilBrown6a806c52005-07-15 03:56:35 -07001132 if (mddev->curr_resync < max_sector) /* aborted */
1133 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
NeilBrown191ea9b2005-06-21 17:17:23 -07001134 &sync_blocks, 1);
NeilBrown6a806c52005-07-15 03:56:35 -07001135 else /* completed sync */
NeilBrown191ea9b2005-06-21 17:17:23 -07001136 conf->fullsync = 0;
NeilBrown6a806c52005-07-15 03:56:35 -07001137
1138 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 close_sync(conf);
1140 return 0;
1141 }
1142
NeilBrowne3b97032005-08-04 12:53:34 -07001143 /* before building a request, check if we can skip these blocks..
1144 * This call the bitmap_start_sync doesn't actually record anything
1145 */
1146 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown6a806c52005-07-15 03:56:35 -07001147 !conf->fullsync) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001148 /* We can skip this block, and probably several more */
1149 *skipped = 1;
1150 return sync_blocks;
1151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 /*
1153 * If there is non-resync activity waiting for us then
1154 * put in a delay to throttle resync.
1155 */
1156 if (!go_faster && waitqueue_active(&conf->wait_resume))
1157 msleep_interruptible(1000);
1158 device_barrier(conf, sector_nr + RESYNC_SECTORS);
1159
1160 /*
1161 * If reconstructing, and >1 working disc,
1162 * could dedicate one to rebuild and others to
1163 * service read requests ..
1164 */
1165 disk = conf->last_used;
1166 /* make sure disk is operational */
1167
1168 while (conf->mirrors[disk].rdev == NULL ||
1169 !conf->mirrors[disk].rdev->in_sync) {
1170 if (disk <= 0)
1171 disk = conf->raid_disks;
1172 disk--;
1173 if (disk == conf->last_used)
1174 break;
1175 }
1176 conf->last_used = disk;
1177 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
1178
1179
1180 mirror = conf->mirrors + disk;
1181
1182 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
1183
1184 spin_lock_irq(&conf->resync_lock);
1185 conf->nr_pending++;
1186 spin_unlock_irq(&conf->resync_lock);
1187
1188 r1_bio->mddev = mddev;
1189 r1_bio->sector = sector_nr;
NeilBrown191ea9b2005-06-21 17:17:23 -07001190 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 set_bit(R1BIO_IsSync, &r1_bio->state);
1192 r1_bio->read_disk = disk;
1193
1194 for (i=0; i < conf->raid_disks; i++) {
1195 bio = r1_bio->bios[i];
1196
1197 /* take from bio_init */
1198 bio->bi_next = NULL;
1199 bio->bi_flags |= 1 << BIO_UPTODATE;
1200 bio->bi_rw = 0;
1201 bio->bi_vcnt = 0;
1202 bio->bi_idx = 0;
1203 bio->bi_phys_segments = 0;
1204 bio->bi_hw_segments = 0;
1205 bio->bi_size = 0;
1206 bio->bi_end_io = NULL;
1207 bio->bi_private = NULL;
1208
1209 if (i == disk) {
1210 bio->bi_rw = READ;
1211 bio->bi_end_io = end_sync_read;
NeilBrowne3b97032005-08-04 12:53:34 -07001212 } else if (conf->mirrors[i].rdev == NULL ||
1213 conf->mirrors[i].rdev->faulty) {
1214 still_degraded = 1;
1215 continue;
1216 } else if (!conf->mirrors[i].rdev->in_sync ||
1217 sector_nr + RESYNC_SECTORS > mddev->recovery_cp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 bio->bi_rw = WRITE;
1219 bio->bi_end_io = end_sync_write;
1220 write_targets ++;
1221 } else
NeilBrowne3b97032005-08-04 12:53:34 -07001222 /* no need to read or write here */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 continue;
1224 bio->bi_sector = sector_nr + conf->mirrors[i].rdev->data_offset;
1225 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1226 bio->bi_private = r1_bio;
1227 }
NeilBrown191ea9b2005-06-21 17:17:23 -07001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (write_targets == 0) {
1230 /* There is nowhere to write, so all non-sync
1231 * drives must be failed - so we are finished
1232 */
NeilBrown57afd892005-06-21 17:17:13 -07001233 sector_t rv = max_sector - sector_nr;
1234 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 put_buf(r1_bio);
1236 rdev_dec_pending(conf->mirrors[disk].rdev, mddev);
1237 return rv;
1238 }
1239
1240 nr_sectors = 0;
NeilBrown289e99e2005-06-21 17:17:24 -07001241 sync_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 do {
1243 struct page *page;
1244 int len = PAGE_SIZE;
1245 if (sector_nr + (len>>9) > max_sector)
1246 len = (max_sector - sector_nr) << 9;
1247 if (len == 0)
1248 break;
NeilBrown6a806c52005-07-15 03:56:35 -07001249 if (sync_blocks == 0) {
1250 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
NeilBrowne3b97032005-08-04 12:53:34 -07001251 &sync_blocks, still_degraded) &&
NeilBrown6a806c52005-07-15 03:56:35 -07001252 !conf->fullsync)
1253 break;
1254 if (sync_blocks < (PAGE_SIZE>>9))
1255 BUG();
1256 if (len > (sync_blocks<<9))
1257 len = sync_blocks<<9;
NeilBrownab7a30c2005-06-21 17:17:23 -07001258 }
NeilBrown191ea9b2005-06-21 17:17:23 -07001259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 for (i=0 ; i < conf->raid_disks; i++) {
1261 bio = r1_bio->bios[i];
1262 if (bio->bi_end_io) {
1263 page = r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page;
1264 if (bio_add_page(bio, page, len, 0) == 0) {
1265 /* stop here */
1266 r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page = page;
1267 while (i > 0) {
1268 i--;
1269 bio = r1_bio->bios[i];
NeilBrown6a806c52005-07-15 03:56:35 -07001270 if (bio->bi_end_io==NULL)
1271 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 /* remove last page from this bio */
1273 bio->bi_vcnt--;
1274 bio->bi_size -= len;
1275 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
1276 }
1277 goto bio_full;
1278 }
1279 }
1280 }
1281 nr_sectors += len>>9;
1282 sector_nr += len>>9;
NeilBrown191ea9b2005-06-21 17:17:23 -07001283 sync_blocks -= (len>>9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
1285 bio_full:
1286 bio = r1_bio->bios[disk];
1287 r1_bio->sectors = nr_sectors;
1288
1289 md_sync_acct(mirror->rdev->bdev, nr_sectors);
1290
1291 generic_make_request(bio);
1292
1293 return nr_sectors;
1294}
1295
1296static int run(mddev_t *mddev)
1297{
1298 conf_t *conf;
1299 int i, j, disk_idx;
1300 mirror_info_t *disk;
1301 mdk_rdev_t *rdev;
1302 struct list_head *tmp;
1303
1304 if (mddev->level != 1) {
1305 printk("raid1: %s: raid level not set to mirroring (%d)\n",
1306 mdname(mddev), mddev->level);
1307 goto out;
1308 }
1309 /*
1310 * copy the already verified devices into our private RAID1
1311 * bookkeeping area. [whatever we allocate in run(),
1312 * should be freed in stop()]
1313 */
1314 conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
1315 mddev->private = conf;
1316 if (!conf)
1317 goto out_no_mem;
1318
1319 memset(conf, 0, sizeof(*conf));
1320 conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks,
1321 GFP_KERNEL);
1322 if (!conf->mirrors)
1323 goto out_no_mem;
1324
1325 memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
1326
1327 conf->poolinfo = kmalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
1328 if (!conf->poolinfo)
1329 goto out_no_mem;
1330 conf->poolinfo->mddev = mddev;
1331 conf->poolinfo->raid_disks = mddev->raid_disks;
1332 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1333 r1bio_pool_free,
1334 conf->poolinfo);
1335 if (!conf->r1bio_pool)
1336 goto out_no_mem;
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 ITERATE_RDEV(mddev, rdev, tmp) {
1339 disk_idx = rdev->raid_disk;
1340 if (disk_idx >= mddev->raid_disks
1341 || disk_idx < 0)
1342 continue;
1343 disk = conf->mirrors + disk_idx;
1344
1345 disk->rdev = rdev;
1346
1347 blk_queue_stack_limits(mddev->queue,
1348 rdev->bdev->bd_disk->queue);
1349 /* as we don't honour merge_bvec_fn, we must never risk
1350 * violating it, so limit ->max_sector to one PAGE, as
1351 * a one page request is never in violation.
1352 */
1353 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1354 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1355 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
1356
1357 disk->head_position = 0;
1358 if (!rdev->faulty && rdev->in_sync)
1359 conf->working_disks++;
1360 }
1361 conf->raid_disks = mddev->raid_disks;
1362 conf->mddev = mddev;
1363 spin_lock_init(&conf->device_lock);
1364 INIT_LIST_HEAD(&conf->retry_list);
1365 if (conf->working_disks == 1)
1366 mddev->recovery_cp = MaxSector;
1367
1368 spin_lock_init(&conf->resync_lock);
1369 init_waitqueue_head(&conf->wait_idle);
1370 init_waitqueue_head(&conf->wait_resume);
1371
NeilBrown191ea9b2005-06-21 17:17:23 -07001372 bio_list_init(&conf->pending_bio_list);
1373 bio_list_init(&conf->flushing_bio_list);
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (!conf->working_disks) {
1376 printk(KERN_ERR "raid1: no operational mirrors for %s\n",
1377 mdname(mddev));
1378 goto out_free_conf;
1379 }
1380
1381 mddev->degraded = 0;
1382 for (i = 0; i < conf->raid_disks; i++) {
1383
1384 disk = conf->mirrors + i;
1385
1386 if (!disk->rdev) {
1387 disk->head_position = 0;
1388 mddev->degraded++;
1389 }
1390 }
1391
1392 /*
1393 * find the first working one and use it as a starting point
1394 * to read balancing.
1395 */
1396 for (j = 0; j < conf->raid_disks &&
1397 (!conf->mirrors[j].rdev ||
1398 !conf->mirrors[j].rdev->in_sync) ; j++)
1399 /* nothing */;
1400 conf->last_used = j;
1401
1402
NeilBrown191ea9b2005-06-21 17:17:23 -07001403 mddev->thread = md_register_thread(raid1d, mddev, "%s_raid1");
1404 if (!mddev->thread) {
1405 printk(KERN_ERR
1406 "raid1: couldn't allocate thread for %s\n",
1407 mdname(mddev));
1408 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
NeilBrown191ea9b2005-06-21 17:17:23 -07001410 if (mddev->bitmap) mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 printk(KERN_INFO
1413 "raid1: raid set %s active with %d out of %d mirrors\n",
1414 mdname(mddev), mddev->raid_disks - mddev->degraded,
1415 mddev->raid_disks);
1416 /*
1417 * Ok, everything is just fine now
1418 */
1419 mddev->array_size = mddev->size;
1420
NeilBrown7a5febe2005-05-16 21:53:16 -07001421 mddev->queue->unplug_fn = raid1_unplug;
1422 mddev->queue->issue_flush_fn = raid1_issue_flush;
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return 0;
1425
1426out_no_mem:
1427 printk(KERN_ERR "raid1: couldn't allocate memory for %s\n",
1428 mdname(mddev));
1429
1430out_free_conf:
1431 if (conf) {
1432 if (conf->r1bio_pool)
1433 mempool_destroy(conf->r1bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07001434 kfree(conf->mirrors);
1435 kfree(conf->poolinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 kfree(conf);
1437 mddev->private = NULL;
1438 }
1439out:
1440 return -EIO;
1441}
1442
1443static int stop(mddev_t *mddev)
1444{
1445 conf_t *conf = mddev_to_conf(mddev);
1446
1447 md_unregister_thread(mddev->thread);
1448 mddev->thread = NULL;
1449 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
1450 if (conf->r1bio_pool)
1451 mempool_destroy(conf->r1bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07001452 kfree(conf->mirrors);
1453 kfree(conf->poolinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 kfree(conf);
1455 mddev->private = NULL;
1456 return 0;
1457}
1458
1459static int raid1_resize(mddev_t *mddev, sector_t sectors)
1460{
1461 /* no resync is happening, and there is enough space
1462 * on all devices, so we can resize.
1463 * We need to make sure resync covers any new space.
1464 * If the array is shrinking we should possibly wait until
1465 * any io in the removed space completes, but it hardly seems
1466 * worth it.
1467 */
1468 mddev->array_size = sectors>>1;
1469 set_capacity(mddev->gendisk, mddev->array_size << 1);
1470 mddev->changed = 1;
1471 if (mddev->array_size > mddev->size && mddev->recovery_cp == MaxSector) {
1472 mddev->recovery_cp = mddev->size << 1;
1473 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1474 }
1475 mddev->size = mddev->array_size;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07001476 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return 0;
1478}
1479
1480static int raid1_reshape(mddev_t *mddev, int raid_disks)
1481{
1482 /* We need to:
1483 * 1/ resize the r1bio_pool
1484 * 2/ resize conf->mirrors
1485 *
1486 * We allocate a new r1bio_pool if we can.
1487 * Then raise a device barrier and wait until all IO stops.
1488 * Then resize conf->mirrors and swap in the new r1bio pool.
NeilBrown6ea9c072005-06-21 17:17:09 -07001489 *
1490 * At the same time, we "pack" the devices so that all the missing
1491 * devices have the higher raid_disk numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 */
1493 mempool_t *newpool, *oldpool;
1494 struct pool_info *newpoolinfo;
1495 mirror_info_t *newmirrors;
1496 conf_t *conf = mddev_to_conf(mddev);
NeilBrown6ea9c072005-06-21 17:17:09 -07001497 int cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
NeilBrown6ea9c072005-06-21 17:17:09 -07001499 int d, d2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
NeilBrown6ea9c072005-06-21 17:17:09 -07001501 if (raid_disks < conf->raid_disks) {
1502 cnt=0;
1503 for (d= 0; d < conf->raid_disks; d++)
1504 if (conf->mirrors[d].rdev)
1505 cnt++;
1506 if (cnt > raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return -EBUSY;
NeilBrown6ea9c072005-06-21 17:17:09 -07001508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
1511 if (!newpoolinfo)
1512 return -ENOMEM;
1513 newpoolinfo->mddev = mddev;
1514 newpoolinfo->raid_disks = raid_disks;
1515
1516 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1517 r1bio_pool_free, newpoolinfo);
1518 if (!newpool) {
1519 kfree(newpoolinfo);
1520 return -ENOMEM;
1521 }
1522 newmirrors = kmalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
1523 if (!newmirrors) {
1524 kfree(newpoolinfo);
1525 mempool_destroy(newpool);
1526 return -ENOMEM;
1527 }
1528 memset(newmirrors, 0, sizeof(struct mirror_info)*raid_disks);
1529
1530 spin_lock_irq(&conf->resync_lock);
1531 conf->barrier++;
1532 wait_event_lock_irq(conf->wait_idle, !conf->nr_pending,
NeilBrown191ea9b2005-06-21 17:17:23 -07001533 conf->resync_lock, raid1_unplug(mddev->queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 spin_unlock_irq(&conf->resync_lock);
1535
1536 /* ok, everything is stopped */
1537 oldpool = conf->r1bio_pool;
1538 conf->r1bio_pool = newpool;
NeilBrown6ea9c072005-06-21 17:17:09 -07001539
1540 for (d=d2=0; d < conf->raid_disks; d++)
1541 if (conf->mirrors[d].rdev) {
1542 conf->mirrors[d].rdev->raid_disk = d2;
1543 newmirrors[d2++].rdev = conf->mirrors[d].rdev;
1544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 kfree(conf->mirrors);
1546 conf->mirrors = newmirrors;
1547 kfree(conf->poolinfo);
1548 conf->poolinfo = newpoolinfo;
1549
1550 mddev->degraded += (raid_disks - conf->raid_disks);
1551 conf->raid_disks = mddev->raid_disks = raid_disks;
1552
NeilBrown6ea9c072005-06-21 17:17:09 -07001553 conf->last_used = 0; /* just make sure it is in-range */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 spin_lock_irq(&conf->resync_lock);
1555 conf->barrier--;
1556 spin_unlock_irq(&conf->resync_lock);
1557 wake_up(&conf->wait_resume);
1558 wake_up(&conf->wait_idle);
1559
1560
1561 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1562 md_wakeup_thread(mddev->thread);
1563
1564 mempool_destroy(oldpool);
1565 return 0;
1566}
1567
1568
1569static mdk_personality_t raid1_personality =
1570{
1571 .name = "raid1",
1572 .owner = THIS_MODULE,
1573 .make_request = make_request,
1574 .run = run,
1575 .stop = stop,
1576 .status = status,
1577 .error_handler = error,
1578 .hot_add_disk = raid1_add_disk,
1579 .hot_remove_disk= raid1_remove_disk,
1580 .spare_active = raid1_spare_active,
1581 .sync_request = sync_request,
1582 .resize = raid1_resize,
1583 .reshape = raid1_reshape,
1584};
1585
1586static int __init raid_init(void)
1587{
1588 return register_md_personality(RAID1, &raid1_personality);
1589}
1590
1591static void raid_exit(void)
1592{
1593 unregister_md_personality(RAID1);
1594}
1595
1596module_init(raid_init);
1597module_exit(raid_exit);
1598MODULE_LICENSE("GPL");
1599MODULE_ALIAS("md-personality-3"); /* RAID1 */