blob: b3856db8d6c2cc719af3c3c541ebafc0aae26b8a [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
NeilBrown17999be2006-01-06 00:20:12 -080054static void allow_barrier(conf_t *conf);
55static void lower_barrier(conf_t *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Al Virodd0fc662005-10-07 07:46:04 +010057static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 struct pool_info *pi = data;
60 r1bio_t *r1_bio;
61 int size = offsetof(r1bio_t, bios[pi->raid_disks]);
62
63 /* allocate a r1bio with room for raid_disks entries in the bios array */
64 r1_bio = kmalloc(size, gfp_flags);
65 if (r1_bio)
66 memset(r1_bio, 0, size);
67 else
68 unplug_slaves(pi->mddev);
69
70 return r1_bio;
71}
72
73static void r1bio_pool_free(void *r1_bio, void *data)
74{
75 kfree(r1_bio);
76}
77
78#define RESYNC_BLOCK_SIZE (64*1024)
79//#define RESYNC_BLOCK_SIZE PAGE_SIZE
80#define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
81#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
82#define RESYNC_WINDOW (2048*1024)
83
Al Virodd0fc662005-10-07 07:46:04 +010084static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 struct pool_info *pi = data;
87 struct page *page;
88 r1bio_t *r1_bio;
89 struct bio *bio;
90 int i, j;
91
92 r1_bio = r1bio_pool_alloc(gfp_flags, pi);
93 if (!r1_bio) {
94 unplug_slaves(pi->mddev);
95 return NULL;
96 }
97
98 /*
99 * Allocate bios : 1 for reading, n-1 for writing
100 */
101 for (j = pi->raid_disks ; j-- ; ) {
102 bio = bio_alloc(gfp_flags, RESYNC_PAGES);
103 if (!bio)
104 goto out_free_bio;
105 r1_bio->bios[j] = bio;
106 }
107 /*
108 * Allocate RESYNC_PAGES data pages and attach them to
109 * the first bio;
110 */
111 bio = r1_bio->bios[0];
112 for (i = 0; i < RESYNC_PAGES; i++) {
113 page = alloc_page(gfp_flags);
114 if (unlikely(!page))
115 goto out_free_pages;
116
117 bio->bi_io_vec[i].bv_page = page;
118 }
119
120 r1_bio->master_bio = NULL;
121
122 return r1_bio;
123
124out_free_pages:
125 for ( ; i > 0 ; i--)
126 __free_page(bio->bi_io_vec[i-1].bv_page);
127out_free_bio:
128 while ( ++j < pi->raid_disks )
129 bio_put(r1_bio->bios[j]);
130 r1bio_pool_free(r1_bio, data);
131 return NULL;
132}
133
134static void r1buf_pool_free(void *__r1_bio, void *data)
135{
136 struct pool_info *pi = data;
137 int i;
138 r1bio_t *r1bio = __r1_bio;
139 struct bio *bio = r1bio->bios[0];
140
141 for (i = 0; i < RESYNC_PAGES; i++) {
142 __free_page(bio->bi_io_vec[i].bv_page);
143 bio->bi_io_vec[i].bv_page = NULL;
144 }
145 for (i=0 ; i < pi->raid_disks; i++)
146 bio_put(r1bio->bios[i]);
147
148 r1bio_pool_free(r1bio, data);
149}
150
151static void put_all_bios(conf_t *conf, r1bio_t *r1_bio)
152{
153 int i;
154
155 for (i = 0; i < conf->raid_disks; i++) {
156 struct bio **bio = r1_bio->bios + i;
157 if (*bio)
158 bio_put(*bio);
159 *bio = NULL;
160 }
161}
162
163static inline void free_r1bio(r1bio_t *r1_bio)
164{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 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 */
NeilBrown17999be2006-01-06 00:20:12 -0800171 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 put_all_bios(conf, r1_bio);
174 mempool_free(r1_bio, conf->r1bio_pool);
175}
176
177static inline void put_buf(r1bio_t *r1_bio)
178{
179 conf_t *conf = mddev_to_conf(r1_bio->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 mempool_free(r1_bio, conf->r1buf_pool);
182
NeilBrown17999be2006-01-06 00:20:12 -0800183 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186static void reschedule_retry(r1bio_t *r1_bio)
187{
188 unsigned long flags;
189 mddev_t *mddev = r1_bio->mddev;
190 conf_t *conf = mddev_to_conf(mddev);
191
192 spin_lock_irqsave(&conf->device_lock, flags);
193 list_add(&r1_bio->retry_list, &conf->retry_list);
NeilBrownddaf22a2006-01-06 00:20:19 -0800194 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 spin_unlock_irqrestore(&conf->device_lock, flags);
196
NeilBrown17999be2006-01-06 00:20:12 -0800197 wake_up(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 md_wakeup_thread(mddev->thread);
199}
200
201/*
202 * raid_end_bio_io() is called when we have finished servicing a mirrored
203 * operation and are ready to return a success/failure code to the buffer
204 * cache layer.
205 */
206static void raid_end_bio_io(r1bio_t *r1_bio)
207{
208 struct bio *bio = r1_bio->master_bio;
209
NeilBrown4b6d2872005-09-09 16:23:47 -0700210 /* if nobody has done the final endio yet, do it now */
211 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
212 PRINTK(KERN_DEBUG "raid1: sync end %s on sectors %llu-%llu\n",
213 (bio_data_dir(bio) == WRITE) ? "write" : "read",
214 (unsigned long long) bio->bi_sector,
215 (unsigned long long) bio->bi_sector +
216 (bio->bi_size >> 9) - 1);
217
218 bio_endio(bio, bio->bi_size,
219 test_bit(R1BIO_Uptodate, &r1_bio->state) ? 0 : -EIO);
220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 free_r1bio(r1_bio);
222}
223
224/*
225 * Update disk head position estimator based on IRQ completion info.
226 */
227static inline void update_head_pos(int disk, r1bio_t *r1_bio)
228{
229 conf_t *conf = mddev_to_conf(r1_bio->mddev);
230
231 conf->mirrors[disk].head_position =
232 r1_bio->sector + (r1_bio->sectors);
233}
234
235static int raid1_end_read_request(struct bio *bio, unsigned int bytes_done, int error)
236{
237 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
238 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
239 int mirror;
240 conf_t *conf = mddev_to_conf(r1_bio->mddev);
241
242 if (bio->bi_size)
243 return 1;
244
245 mirror = r1_bio->read_disk;
246 /*
247 * this branch is our 'one mirror IO has finished' event handler:
248 */
NeilBrownddaf22a2006-01-06 00:20:19 -0800249 update_head_pos(mirror, r1_bio);
250
251 if (uptodate || conf->working_disks <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 /*
253 * Set R1BIO_Uptodate in our master bio, so that
254 * we will return a good error code for to the higher
255 * levels even if IO on some other mirrored buffer fails.
256 *
257 * The 'master' represents the composite IO operation to
258 * user-side. So if something waits for IO, then it will
259 * wait for the 'master' bio.
260 */
261 set_bit(R1BIO_Uptodate, &r1_bio->state);
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 raid_end_bio_io(r1_bio);
NeilBrownddaf22a2006-01-06 00:20:19 -0800264 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 /*
266 * oops, read error:
267 */
268 char b[BDEVNAME_SIZE];
269 if (printk_ratelimit())
270 printk(KERN_ERR "raid1: %s: rescheduling sector %llu\n",
271 bdevname(conf->mirrors[mirror].rdev->bdev,b), (unsigned long long)r1_bio->sector);
272 reschedule_retry(r1_bio);
273 }
274
275 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
276 return 0;
277}
278
279static int raid1_end_write_request(struct bio *bio, unsigned int bytes_done, int error)
280{
281 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
282 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
NeilBrowna9701a32005-11-08 21:39:34 -0800283 int mirror, behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 conf_t *conf = mddev_to_conf(r1_bio->mddev);
285
286 if (bio->bi_size)
287 return 1;
288
289 for (mirror = 0; mirror < conf->raid_disks; mirror++)
290 if (r1_bio->bios[mirror] == bio)
291 break;
292
NeilBrowna9701a32005-11-08 21:39:34 -0800293 if (error == -ENOTSUPP && test_bit(R1BIO_Barrier, &r1_bio->state)) {
294 set_bit(BarriersNotsupp, &conf->mirrors[mirror].rdev->flags);
295 set_bit(R1BIO_BarrierRetry, &r1_bio->state);
296 r1_bio->mddev->barriers_work = 0;
297 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 /*
NeilBrowna9701a32005-11-08 21:39:34 -0800299 * this branch is our 'one mirror IO has finished' event handler:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 */
NeilBrowna9701a32005-11-08 21:39:34 -0800301 r1_bio->bios[mirror] = NULL;
NeilBrowna9701a32005-11-08 21:39:34 -0800302 if (!uptodate) {
303 md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
304 /* an I/O failed, we can't clear the bitmap */
305 set_bit(R1BIO_Degraded, &r1_bio->state);
306 } else
307 /*
308 * Set R1BIO_Uptodate in our master bio, so that
309 * we will return a good error code for to the higher
310 * levels even if IO on some other mirrored buffer fails.
311 *
312 * The 'master' represents the composite IO operation to
313 * user-side. So if something waits for IO, then it will
314 * wait for the 'master' bio.
315 */
316 set_bit(R1BIO_Uptodate, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
NeilBrowna9701a32005-11-08 21:39:34 -0800318 update_head_pos(mirror, r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
NeilBrowna9701a32005-11-08 21:39:34 -0800320 if (behind) {
321 if (test_bit(WriteMostly, &conf->mirrors[mirror].rdev->flags))
322 atomic_dec(&r1_bio->behind_remaining);
NeilBrown4b6d2872005-09-09 16:23:47 -0700323
NeilBrowna9701a32005-11-08 21:39:34 -0800324 /* In behind mode, we ACK the master bio once the I/O has safely
325 * reached all non-writemostly disks. Setting the Returned bit
326 * ensures that this gets done only once -- we don't ever want to
327 * return -EIO here, instead we'll wait */
NeilBrown4b6d2872005-09-09 16:23:47 -0700328
NeilBrowna9701a32005-11-08 21:39:34 -0800329 if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
330 test_bit(R1BIO_Uptodate, &r1_bio->state)) {
331 /* Maybe we can return now */
332 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
333 struct bio *mbio = r1_bio->master_bio;
334 PRINTK(KERN_DEBUG "raid1: behind end write sectors %llu-%llu\n",
335 (unsigned long long) mbio->bi_sector,
336 (unsigned long long) mbio->bi_sector +
337 (mbio->bi_size >> 9) - 1);
338 bio_endio(mbio, mbio->bi_size, 0);
339 }
NeilBrown4b6d2872005-09-09 16:23:47 -0700340 }
341 }
342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 /*
344 *
345 * Let's see if all mirrored write operations have finished
346 * already.
347 */
348 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrowna9701a32005-11-08 21:39:34 -0800349 if (test_bit(R1BIO_BarrierRetry, &r1_bio->state)) {
350 reschedule_retry(r1_bio);
351 /* Don't dec_pending yet, we want to hold
352 * the reference over the retry
353 */
354 return 0;
355 }
NeilBrown4b6d2872005-09-09 16:23:47 -0700356 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
357 /* free extra copy of the data pages */
358 int i = bio->bi_vcnt;
359 while (i--)
360 __free_page(bio->bi_io_vec[i].bv_page);
361 }
NeilBrown191ea9b2005-06-21 17:17:23 -0700362 /* clear the bitmap if all writes complete successfully */
363 bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
364 r1_bio->sectors,
NeilBrown4b6d2872005-09-09 16:23:47 -0700365 !test_bit(R1BIO_Degraded, &r1_bio->state),
366 behind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 md_write_end(r1_bio->mddev);
368 raid_end_bio_io(r1_bio);
369 }
370
NeilBrown3795bb02005-12-12 02:39:16 -0800371 if (r1_bio->bios[mirror]==NULL)
372 bio_put(bio);
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
375 return 0;
376}
377
378
379/*
380 * This routine returns the disk from which the requested read should
381 * be done. There is a per-array 'next expected sequential IO' sector
382 * number - if this matches on the next IO then we use the last disk.
383 * There is also a per-disk 'last know head position' sector that is
384 * maintained from IRQ contexts, both the normal and the resync IO
385 * completion handlers update this position correctly. If there is no
386 * perfect sequential match then we pick the disk whose head is closest.
387 *
388 * If there are 2 mirrors in the same 2 devices, performance degrades
389 * because position is mirror, not device based.
390 *
391 * The rdev for the device selected will have nr_pending incremented.
392 */
393static int read_balance(conf_t *conf, r1bio_t *r1_bio)
394{
395 const unsigned long this_sector = r1_bio->sector;
396 int new_disk = conf->last_used, disk = new_disk;
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700397 int wonly_disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 const int sectors = r1_bio->sectors;
399 sector_t new_distance, current_distance;
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700400 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 rcu_read_lock();
403 /*
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700404 * Check if we can balance. We can balance on the whole
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 * device if no resync is going on, or below the resync window.
406 * We take the first readable disk when above the resync window.
407 */
408 retry:
409 if (conf->mddev->recovery_cp < MaxSector &&
410 (this_sector + sectors >= conf->next_resync)) {
411 /* Choose the first operation device, for consistancy */
412 new_disk = 0;
413
Suzanne Woodd6065f72005-11-08 21:39:27 -0800414 for (rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800415 !rdev || !test_bit(In_sync, &rdev->flags)
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700416 || test_bit(WriteMostly, &rdev->flags);
Suzanne Woodd6065f72005-11-08 21:39:27 -0800417 rdev = rcu_dereference(conf->mirrors[++new_disk].rdev)) {
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700418
NeilBrownb2d444d2005-11-08 21:39:31 -0800419 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700420 wonly_disk = new_disk;
421
422 if (new_disk == conf->raid_disks - 1) {
423 new_disk = wonly_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 break;
425 }
426 }
427 goto rb_out;
428 }
429
430
431 /* make sure the disk is operational */
Suzanne Woodd6065f72005-11-08 21:39:27 -0800432 for (rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800433 !rdev || !test_bit(In_sync, &rdev->flags) ||
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700434 test_bit(WriteMostly, &rdev->flags);
Suzanne Woodd6065f72005-11-08 21:39:27 -0800435 rdev = rcu_dereference(conf->mirrors[new_disk].rdev)) {
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700436
NeilBrownb2d444d2005-11-08 21:39:31 -0800437 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700438 wonly_disk = new_disk;
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (new_disk <= 0)
441 new_disk = conf->raid_disks;
442 new_disk--;
443 if (new_disk == disk) {
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700444 new_disk = wonly_disk;
445 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700448
449 if (new_disk < 0)
450 goto rb_out;
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 disk = new_disk;
453 /* now disk == new_disk == starting point for search */
454
455 /*
456 * Don't change to another disk for sequential reads:
457 */
458 if (conf->next_seq_sect == this_sector)
459 goto rb_out;
460 if (this_sector == conf->mirrors[new_disk].head_position)
461 goto rb_out;
462
463 current_distance = abs(this_sector - conf->mirrors[disk].head_position);
464
465 /* Find the disk whose head is closest */
466
467 do {
468 if (disk <= 0)
469 disk = conf->raid_disks;
470 disk--;
471
Suzanne Woodd6065f72005-11-08 21:39:27 -0800472 rdev = rcu_dereference(conf->mirrors[disk].rdev);
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700473
474 if (!rdev ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800475 !test_bit(In_sync, &rdev->flags) ||
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700476 test_bit(WriteMostly, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 continue;
478
479 if (!atomic_read(&rdev->nr_pending)) {
480 new_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 break;
482 }
483 new_distance = abs(this_sector - conf->mirrors[disk].head_position);
484 if (new_distance < current_distance) {
485 current_distance = new_distance;
486 new_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488 } while (disk != conf->last_used);
489
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700490 rb_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492
493 if (new_disk >= 0) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800494 rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700495 if (!rdev)
496 goto retry;
497 atomic_inc(&rdev->nr_pending);
NeilBrownb2d444d2005-11-08 21:39:31 -0800498 if (!test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /* cannot risk returning a device that failed
500 * before we inc'ed nr_pending
501 */
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700502 atomic_dec(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 goto retry;
504 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700505 conf->next_seq_sect = this_sector + sectors;
506 conf->last_used = new_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508 rcu_read_unlock();
509
510 return new_disk;
511}
512
513static void unplug_slaves(mddev_t *mddev)
514{
515 conf_t *conf = mddev_to_conf(mddev);
516 int i;
517
518 rcu_read_lock();
519 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800520 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800521 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
523
524 atomic_inc(&rdev->nr_pending);
525 rcu_read_unlock();
526
527 if (r_queue->unplug_fn)
528 r_queue->unplug_fn(r_queue);
529
530 rdev_dec_pending(rdev, mddev);
531 rcu_read_lock();
532 }
533 }
534 rcu_read_unlock();
535}
536
537static void raid1_unplug(request_queue_t *q)
538{
NeilBrown191ea9b2005-06-21 17:17:23 -0700539 mddev_t *mddev = q->queuedata;
540
541 unplug_slaves(mddev);
542 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545static int raid1_issue_flush(request_queue_t *q, struct gendisk *disk,
546 sector_t *error_sector)
547{
548 mddev_t *mddev = q->queuedata;
549 conf_t *conf = mddev_to_conf(mddev);
550 int i, ret = 0;
551
552 rcu_read_lock();
553 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800554 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800555 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 struct block_device *bdev = rdev->bdev;
557 request_queue_t *r_queue = bdev_get_queue(bdev);
558
559 if (!r_queue->issue_flush_fn)
560 ret = -EOPNOTSUPP;
561 else {
562 atomic_inc(&rdev->nr_pending);
563 rcu_read_unlock();
564 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
565 error_sector);
566 rdev_dec_pending(rdev, mddev);
567 rcu_read_lock();
568 }
569 }
570 }
571 rcu_read_unlock();
572 return ret;
573}
574
NeilBrown17999be2006-01-06 00:20:12 -0800575/* Barriers....
576 * Sometimes we need to suspend IO while we do something else,
577 * either some resync/recovery, or reconfigure the array.
578 * To do this we raise a 'barrier'.
579 * The 'barrier' is a counter that can be raised multiple times
580 * to count how many activities are happening which preclude
581 * normal IO.
582 * We can only raise the barrier if there is no pending IO.
583 * i.e. if nr_pending == 0.
584 * We choose only to raise the barrier if no-one is waiting for the
585 * barrier to go down. This means that as soon as an IO request
586 * is ready, no other operations which require a barrier will start
587 * until the IO request has had a chance.
588 *
589 * So: regular IO calls 'wait_barrier'. When that returns there
590 * is no backgroup IO happening, It must arrange to call
591 * allow_barrier when it has finished its IO.
592 * backgroup IO calls must call raise_barrier. Once that returns
593 * there is no normal IO happeing. It must arrange to call
594 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 */
596#define RESYNC_DEPTH 32
597
NeilBrown17999be2006-01-06 00:20:12 -0800598static void raise_barrier(conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 spin_lock_irq(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800601
602 /* Wait until no block IO is waiting */
603 wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
604 conf->resync_lock,
605 raid1_unplug(conf->mddev->queue));
606
607 /* block any new IO from starting */
608 conf->barrier++;
609
610 /* No wait for all pending IO to complete */
611 wait_event_lock_irq(conf->wait_barrier,
612 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
613 conf->resync_lock,
614 raid1_unplug(conf->mddev->queue));
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 spin_unlock_irq(&conf->resync_lock);
617}
618
NeilBrown17999be2006-01-06 00:20:12 -0800619static void lower_barrier(conf_t *conf)
620{
621 unsigned long flags;
622 spin_lock_irqsave(&conf->resync_lock, flags);
623 conf->barrier--;
624 spin_unlock_irqrestore(&conf->resync_lock, flags);
625 wake_up(&conf->wait_barrier);
626}
627
628static void wait_barrier(conf_t *conf)
629{
630 spin_lock_irq(&conf->resync_lock);
631 if (conf->barrier) {
632 conf->nr_waiting++;
633 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
634 conf->resync_lock,
635 raid1_unplug(conf->mddev->queue));
636 conf->nr_waiting--;
637 }
638 conf->nr_pending++;
639 spin_unlock_irq(&conf->resync_lock);
640}
641
642static void allow_barrier(conf_t *conf)
643{
644 unsigned long flags;
645 spin_lock_irqsave(&conf->resync_lock, flags);
646 conf->nr_pending--;
647 spin_unlock_irqrestore(&conf->resync_lock, flags);
648 wake_up(&conf->wait_barrier);
649}
650
NeilBrownddaf22a2006-01-06 00:20:19 -0800651static void freeze_array(conf_t *conf)
652{
653 /* stop syncio and normal IO and wait for everything to
654 * go quite.
655 * We increment barrier and nr_waiting, and then
656 * wait until barrier+nr_pending match nr_queued+2
657 */
658 spin_lock_irq(&conf->resync_lock);
659 conf->barrier++;
660 conf->nr_waiting++;
661 wait_event_lock_irq(conf->wait_barrier,
662 conf->barrier+conf->nr_pending == conf->nr_queued+2,
663 conf->resync_lock,
664 raid1_unplug(conf->mddev->queue));
665 spin_unlock_irq(&conf->resync_lock);
666}
667static void unfreeze_array(conf_t *conf)
668{
669 /* reverse the effect of the freeze */
670 spin_lock_irq(&conf->resync_lock);
671 conf->barrier--;
672 conf->nr_waiting--;
673 wake_up(&conf->wait_barrier);
674 spin_unlock_irq(&conf->resync_lock);
675}
676
NeilBrown17999be2006-01-06 00:20:12 -0800677
NeilBrown4b6d2872005-09-09 16:23:47 -0700678/* duplicate the data pages for behind I/O */
679static struct page **alloc_behind_pages(struct bio *bio)
680{
681 int i;
682 struct bio_vec *bvec;
683 struct page **pages = kmalloc(bio->bi_vcnt * sizeof(struct page *),
684 GFP_NOIO);
685 if (unlikely(!pages))
686 goto do_sync_io;
687
688 memset(pages, 0, bio->bi_vcnt * sizeof(struct page *));
689
690 bio_for_each_segment(bvec, bio, i) {
691 pages[i] = alloc_page(GFP_NOIO);
692 if (unlikely(!pages[i]))
693 goto do_sync_io;
694 memcpy(kmap(pages[i]) + bvec->bv_offset,
695 kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
696 kunmap(pages[i]);
697 kunmap(bvec->bv_page);
698 }
699
700 return pages;
701
702do_sync_io:
703 if (pages)
704 for (i = 0; i < bio->bi_vcnt && pages[i]; i++)
705 __free_page(pages[i]);
706 kfree(pages);
707 PRINTK("%dB behind alloc failed, doing sync I/O\n", bio->bi_size);
708 return NULL;
709}
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711static int make_request(request_queue_t *q, struct bio * bio)
712{
713 mddev_t *mddev = q->queuedata;
714 conf_t *conf = mddev_to_conf(mddev);
715 mirror_info_t *mirror;
716 r1bio_t *r1_bio;
717 struct bio *read_bio;
NeilBrown191ea9b2005-06-21 17:17:23 -0700718 int i, targets = 0, disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 mdk_rdev_t *rdev;
NeilBrown191ea9b2005-06-21 17:17:23 -0700720 struct bitmap *bitmap = mddev->bitmap;
721 unsigned long flags;
722 struct bio_list bl;
NeilBrown4b6d2872005-09-09 16:23:47 -0700723 struct page **behind_pages = NULL;
Jens Axboea3623572005-11-01 09:26:16 +0100724 const int rw = bio_data_dir(bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800725 int do_barriers;
NeilBrown191ea9b2005-06-21 17:17:23 -0700726
NeilBrowna9701a32005-11-08 21:39:34 -0800727 if (unlikely(!mddev->barriers_work && bio_barrier(bio))) {
NeilBrowne5dcdd82005-09-09 16:23:41 -0700728 bio_endio(bio, bio->bi_size, -EOPNOTSUPP);
729 return 0;
730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 /*
733 * Register the new request and wait if the reconstruction
734 * thread has put up a bar for new requests.
735 * Continue immediately if no resync is active currently.
736 */
NeilBrown3d310eb2005-06-21 17:17:26 -0700737 md_write_start(mddev, bio); /* wait on superblock update early */
738
NeilBrown17999be2006-01-06 00:20:12 -0800739 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Jens Axboea3623572005-11-01 09:26:16 +0100741 disk_stat_inc(mddev->gendisk, ios[rw]);
742 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 /*
745 * make_request() can abort the operation when READA is being
746 * used and no empty request is available.
747 *
748 */
749 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
750
751 r1_bio->master_bio = bio;
752 r1_bio->sectors = bio->bi_size >> 9;
NeilBrown191ea9b2005-06-21 17:17:23 -0700753 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 r1_bio->mddev = mddev;
755 r1_bio->sector = bio->bi_sector;
756
Jens Axboea3623572005-11-01 09:26:16 +0100757 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 /*
759 * read balancing logic:
760 */
761 int rdisk = read_balance(conf, r1_bio);
762
763 if (rdisk < 0) {
764 /* couldn't find anywhere to read from */
765 raid_end_bio_io(r1_bio);
766 return 0;
767 }
768 mirror = conf->mirrors + rdisk;
769
770 r1_bio->read_disk = rdisk;
771
772 read_bio = bio_clone(bio, GFP_NOIO);
773
774 r1_bio->bios[rdisk] = read_bio;
775
776 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
777 read_bio->bi_bdev = mirror->rdev->bdev;
778 read_bio->bi_end_io = raid1_end_read_request;
779 read_bio->bi_rw = READ;
780 read_bio->bi_private = r1_bio;
781
782 generic_make_request(read_bio);
783 return 0;
784 }
785
786 /*
787 * WRITE:
788 */
789 /* first select target devices under spinlock and
790 * inc refcount on their rdev. Record them by setting
791 * bios[x] to bio
792 */
793 disks = conf->raid_disks;
NeilBrown191ea9b2005-06-21 17:17:23 -0700794#if 0
795 { static int first=1;
796 if (first) printk("First Write sector %llu disks %d\n",
797 (unsigned long long)r1_bio->sector, disks);
798 first = 0;
799 }
800#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 rcu_read_lock();
802 for (i = 0; i < disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800803 if ((rdev=rcu_dereference(conf->mirrors[i].rdev)) != NULL &&
NeilBrownb2d444d2005-11-08 21:39:31 -0800804 !test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 atomic_inc(&rdev->nr_pending);
NeilBrownb2d444d2005-11-08 21:39:31 -0800806 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 atomic_dec(&rdev->nr_pending);
808 r1_bio->bios[i] = NULL;
809 } else
810 r1_bio->bios[i] = bio;
NeilBrown191ea9b2005-06-21 17:17:23 -0700811 targets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 } else
813 r1_bio->bios[i] = NULL;
814 }
815 rcu_read_unlock();
816
NeilBrown4b6d2872005-09-09 16:23:47 -0700817 BUG_ON(targets == 0); /* we never fail the last device */
818
NeilBrown191ea9b2005-06-21 17:17:23 -0700819 if (targets < conf->raid_disks) {
820 /* array is degraded, we will not clear the bitmap
821 * on I/O completion (see raid1_end_write_request) */
822 set_bit(R1BIO_Degraded, &r1_bio->state);
823 }
NeilBrown06d91a52005-06-21 17:17:12 -0700824
NeilBrown4b6d2872005-09-09 16:23:47 -0700825 /* do behind I/O ? */
826 if (bitmap &&
827 atomic_read(&bitmap->behind_writes) < bitmap->max_write_behind &&
828 (behind_pages = alloc_behind_pages(bio)) != NULL)
829 set_bit(R1BIO_BehindIO, &r1_bio->state);
830
NeilBrown191ea9b2005-06-21 17:17:23 -0700831 atomic_set(&r1_bio->remaining, 0);
NeilBrown4b6d2872005-09-09 16:23:47 -0700832 atomic_set(&r1_bio->behind_remaining, 0);
NeilBrown191ea9b2005-06-21 17:17:23 -0700833
NeilBrowna9701a32005-11-08 21:39:34 -0800834 do_barriers = bio->bi_rw & BIO_RW_BARRIER;
835 if (do_barriers)
836 set_bit(R1BIO_Barrier, &r1_bio->state);
837
NeilBrown191ea9b2005-06-21 17:17:23 -0700838 bio_list_init(&bl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 for (i = 0; i < disks; i++) {
840 struct bio *mbio;
841 if (!r1_bio->bios[i])
842 continue;
843
844 mbio = bio_clone(bio, GFP_NOIO);
845 r1_bio->bios[i] = mbio;
846
847 mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
848 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
849 mbio->bi_end_io = raid1_end_write_request;
NeilBrowna9701a32005-11-08 21:39:34 -0800850 mbio->bi_rw = WRITE | do_barriers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 mbio->bi_private = r1_bio;
852
NeilBrown4b6d2872005-09-09 16:23:47 -0700853 if (behind_pages) {
854 struct bio_vec *bvec;
855 int j;
856
857 /* Yes, I really want the '__' version so that
858 * we clear any unused pointer in the io_vec, rather
859 * than leave them unchanged. This is important
860 * because when we come to free the pages, we won't
861 * know the originial bi_idx, so we just free
862 * them all
863 */
864 __bio_for_each_segment(bvec, mbio, j, 0)
865 bvec->bv_page = behind_pages[j];
866 if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
867 atomic_inc(&r1_bio->behind_remaining);
868 }
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 atomic_inc(&r1_bio->remaining);
NeilBrown191ea9b2005-06-21 17:17:23 -0700871
872 bio_list_add(&bl, mbio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
NeilBrown4b6d2872005-09-09 16:23:47 -0700874 kfree(behind_pages); /* the behind pages are attached to the bios now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
NeilBrown4b6d2872005-09-09 16:23:47 -0700876 bitmap_startwrite(bitmap, bio->bi_sector, r1_bio->sectors,
877 test_bit(R1BIO_BehindIO, &r1_bio->state));
NeilBrown191ea9b2005-06-21 17:17:23 -0700878 spin_lock_irqsave(&conf->device_lock, flags);
879 bio_list_merge(&conf->pending_bio_list, &bl);
880 bio_list_init(&bl);
881
882 blk_plug_device(mddev->queue);
883 spin_unlock_irqrestore(&conf->device_lock, flags);
884
885#if 0
886 while ((bio = bio_list_pop(&bl)) != NULL)
887 generic_make_request(bio);
888#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 return 0;
891}
892
893static void status(struct seq_file *seq, mddev_t *mddev)
894{
895 conf_t *conf = mddev_to_conf(mddev);
896 int i;
897
898 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
899 conf->working_disks);
900 for (i = 0; i < conf->raid_disks; i++)
901 seq_printf(seq, "%s",
902 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -0800903 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 seq_printf(seq, "]");
905}
906
907
908static void error(mddev_t *mddev, mdk_rdev_t *rdev)
909{
910 char b[BDEVNAME_SIZE];
911 conf_t *conf = mddev_to_conf(mddev);
912
913 /*
914 * If it is not operational, then we have already marked it as dead
915 * else if it is the last working disks, ignore the error, let the
916 * next level up know.
917 * else mark the drive as failed
918 */
NeilBrownb2d444d2005-11-08 21:39:31 -0800919 if (test_bit(In_sync, &rdev->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 && conf->working_disks == 1)
921 /*
922 * Don't fail the drive, act as though we were just a
923 * normal single drive
924 */
925 return;
NeilBrownb2d444d2005-11-08 21:39:31 -0800926 if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 mddev->degraded++;
928 conf->working_disks--;
929 /*
930 * if recovery is running, make sure it aborts.
931 */
932 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
933 }
NeilBrownb2d444d2005-11-08 21:39:31 -0800934 clear_bit(In_sync, &rdev->flags);
935 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 mddev->sb_dirty = 1;
937 printk(KERN_ALERT "raid1: Disk failure on %s, disabling device. \n"
938 " Operation continuing on %d devices\n",
939 bdevname(rdev->bdev,b), conf->working_disks);
940}
941
942static void print_conf(conf_t *conf)
943{
944 int i;
945 mirror_info_t *tmp;
946
947 printk("RAID1 conf printout:\n");
948 if (!conf) {
949 printk("(!conf)\n");
950 return;
951 }
952 printk(" --- wd:%d rd:%d\n", conf->working_disks,
953 conf->raid_disks);
954
955 for (i = 0; i < conf->raid_disks; i++) {
956 char b[BDEVNAME_SIZE];
957 tmp = conf->mirrors + i;
958 if (tmp->rdev)
959 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -0800960 i, !test_bit(In_sync, &tmp->rdev->flags), !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 bdevname(tmp->rdev->bdev,b));
962 }
963}
964
965static void close_sync(conf_t *conf)
966{
NeilBrown17999be2006-01-06 00:20:12 -0800967 wait_barrier(conf);
968 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 mempool_destroy(conf->r1buf_pool);
971 conf->r1buf_pool = NULL;
972}
973
974static int raid1_spare_active(mddev_t *mddev)
975{
976 int i;
977 conf_t *conf = mddev->private;
978 mirror_info_t *tmp;
979
980 /*
981 * Find all failed disks within the RAID1 configuration
982 * and mark them readable
983 */
984 for (i = 0; i < conf->raid_disks; i++) {
985 tmp = conf->mirrors + i;
986 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -0800987 && !test_bit(Faulty, &tmp->rdev->flags)
988 && !test_bit(In_sync, &tmp->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 conf->working_disks++;
990 mddev->degraded--;
NeilBrownb2d444d2005-11-08 21:39:31 -0800991 set_bit(In_sync, &tmp->rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993 }
994
995 print_conf(conf);
996 return 0;
997}
998
999
1000static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1001{
1002 conf_t *conf = mddev->private;
1003 int found = 0;
NeilBrown41158c7e2005-06-21 17:17:25 -07001004 int mirror = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 mirror_info_t *p;
1006
1007 for (mirror=0; mirror < mddev->raid_disks; mirror++)
1008 if ( !(p=conf->mirrors+mirror)->rdev) {
1009
1010 blk_queue_stack_limits(mddev->queue,
1011 rdev->bdev->bd_disk->queue);
1012 /* as we don't honour merge_bvec_fn, we must never risk
1013 * violating it, so limit ->max_sector to one PAGE, as
1014 * a one page request is never in violation.
1015 */
1016 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1017 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1018 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
1019
1020 p->head_position = 0;
1021 rdev->raid_disk = mirror;
1022 found = 1;
NeilBrown6aea114a2005-11-28 13:44:13 -08001023 /* As all devices are equivalent, we don't need a full recovery
1024 * if this was recently any drive of the array
1025 */
1026 if (rdev->saved_raid_disk < 0)
NeilBrown41158c7e2005-06-21 17:17:25 -07001027 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001028 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 break;
1030 }
1031
1032 print_conf(conf);
1033 return found;
1034}
1035
1036static int raid1_remove_disk(mddev_t *mddev, int number)
1037{
1038 conf_t *conf = mddev->private;
1039 int err = 0;
1040 mdk_rdev_t *rdev;
1041 mirror_info_t *p = conf->mirrors+ number;
1042
1043 print_conf(conf);
1044 rdev = p->rdev;
1045 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001046 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 atomic_read(&rdev->nr_pending)) {
1048 err = -EBUSY;
1049 goto abort;
1050 }
1051 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001052 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (atomic_read(&rdev->nr_pending)) {
1054 /* lost the race, try later */
1055 err = -EBUSY;
1056 p->rdev = rdev;
1057 }
1058 }
1059abort:
1060
1061 print_conf(conf);
1062 return err;
1063}
1064
1065
1066static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
1067{
1068 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1069 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
1070 conf_t *conf = mddev_to_conf(r1_bio->mddev);
1071
1072 if (bio->bi_size)
1073 return 1;
1074
1075 if (r1_bio->bios[r1_bio->read_disk] != bio)
1076 BUG();
1077 update_head_pos(r1_bio->read_disk, r1_bio);
1078 /*
1079 * we have read a block, now it needs to be re-written,
1080 * or re-read if the read failed.
1081 * We don't do much here, just schedule handling by raid1d
1082 */
NeilBrown191ea9b2005-06-21 17:17:23 -07001083 if (!uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 md_error(r1_bio->mddev,
1085 conf->mirrors[r1_bio->read_disk].rdev);
NeilBrown191ea9b2005-06-21 17:17:23 -07001086 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 set_bit(R1BIO_Uptodate, &r1_bio->state);
1088 rdev_dec_pending(conf->mirrors[r1_bio->read_disk].rdev, conf->mddev);
1089 reschedule_retry(r1_bio);
1090 return 0;
1091}
1092
1093static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
1094{
1095 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1096 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
1097 mddev_t *mddev = r1_bio->mddev;
1098 conf_t *conf = mddev_to_conf(mddev);
1099 int i;
1100 int mirror=0;
1101
1102 if (bio->bi_size)
1103 return 1;
1104
1105 for (i = 0; i < conf->raid_disks; i++)
1106 if (r1_bio->bios[i] == bio) {
1107 mirror = i;
1108 break;
1109 }
NeilBrowne3b97032005-08-04 12:53:34 -07001110 if (!uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 md_error(mddev, conf->mirrors[mirror].rdev);
NeilBrowne3b97032005-08-04 12:53:34 -07001112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 update_head_pos(mirror, r1_bio);
1114
1115 if (atomic_dec_and_test(&r1_bio->remaining)) {
1116 md_done_sync(mddev, r1_bio->sectors, uptodate);
1117 put_buf(r1_bio);
1118 }
1119 rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
1120 return 0;
1121}
1122
1123static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
1124{
1125 conf_t *conf = mddev_to_conf(mddev);
1126 int i;
1127 int disks = conf->raid_disks;
1128 struct bio *bio, *wbio;
1129
1130 bio = r1_bio->bios[r1_bio->read_disk];
1131
NeilBrown191ea9b2005-06-21 17:17:23 -07001132/*
1133 if (r1_bio->sector == 0) printk("First sync write startss\n");
1134*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 /*
1136 * schedule writes
1137 */
1138 if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
1139 /*
1140 * There is no point trying a read-for-reconstruct as
1141 * reconstruct is about to be aborted
1142 */
1143 char b[BDEVNAME_SIZE];
1144 printk(KERN_ALERT "raid1: %s: unrecoverable I/O read error"
1145 " for block %llu\n",
1146 bdevname(bio->bi_bdev,b),
1147 (unsigned long long)r1_bio->sector);
1148 md_done_sync(mddev, r1_bio->sectors, 0);
1149 put_buf(r1_bio);
1150 return;
1151 }
1152
1153 atomic_set(&r1_bio->remaining, 1);
1154 for (i = 0; i < disks ; i++) {
1155 wbio = r1_bio->bios[i];
1156 if (wbio->bi_end_io != end_sync_write)
1157 continue;
1158
1159 atomic_inc(&conf->mirrors[i].rdev->nr_pending);
1160 atomic_inc(&r1_bio->remaining);
1161 md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
NeilBrown191ea9b2005-06-21 17:17:23 -07001162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 generic_make_request(wbio);
1164 }
1165
1166 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001167 /* if we're here, all write(s) have completed, so clean up */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 md_done_sync(mddev, r1_bio->sectors, 1);
1169 put_buf(r1_bio);
1170 }
1171}
1172
1173/*
1174 * This is a kernel thread which:
1175 *
1176 * 1. Retries failed read operations on working mirrors.
1177 * 2. Updates the raid superblock when problems encounter.
1178 * 3. Performs writes following reads for array syncronising.
1179 */
1180
1181static void raid1d(mddev_t *mddev)
1182{
1183 r1bio_t *r1_bio;
1184 struct bio *bio;
1185 unsigned long flags;
1186 conf_t *conf = mddev_to_conf(mddev);
1187 struct list_head *head = &conf->retry_list;
1188 int unplug=0;
1189 mdk_rdev_t *rdev;
1190
1191 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 for (;;) {
1194 char b[BDEVNAME_SIZE];
1195 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown191ea9b2005-06-21 17:17:23 -07001196
1197 if (conf->pending_bio_list.head) {
1198 bio = bio_list_get(&conf->pending_bio_list);
1199 blk_remove_plug(mddev->queue);
1200 spin_unlock_irqrestore(&conf->device_lock, flags);
1201 /* flush any pending bitmap writes to disk before proceeding w/ I/O */
1202 if (bitmap_unplug(mddev->bitmap) != 0)
1203 printk("%s: bitmap file write failed!\n", mdname(mddev));
1204
1205 while (bio) { /* submit pending writes */
1206 struct bio *next = bio->bi_next;
1207 bio->bi_next = NULL;
1208 generic_make_request(bio);
1209 bio = next;
1210 }
1211 unplug = 1;
1212
1213 continue;
1214 }
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (list_empty(head))
1217 break;
1218 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
1219 list_del(head->prev);
NeilBrownddaf22a2006-01-06 00:20:19 -08001220 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 spin_unlock_irqrestore(&conf->device_lock, flags);
1222
1223 mddev = r1_bio->mddev;
1224 conf = mddev_to_conf(mddev);
1225 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
1226 sync_request_write(mddev, r1_bio);
1227 unplug = 1;
NeilBrowna9701a32005-11-08 21:39:34 -08001228 } else if (test_bit(R1BIO_BarrierRetry, &r1_bio->state)) {
1229 /* some requests in the r1bio were BIO_RW_BARRIER
1230 * requests which failed with -ENOTSUPP. Hohumm..
1231 * Better resubmit without the barrier.
1232 * We know which devices to resubmit for, because
1233 * all others have had their bios[] entry cleared.
1234 */
1235 int i;
1236 clear_bit(R1BIO_BarrierRetry, &r1_bio->state);
1237 clear_bit(R1BIO_Barrier, &r1_bio->state);
1238 for (i=0; i < conf->raid_disks; i++)
1239 if (r1_bio->bios[i]) {
1240 struct bio_vec *bvec;
1241 int j;
1242
1243 bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
1244 /* copy pages from the failed bio, as
1245 * this might be a write-behind device */
1246 __bio_for_each_segment(bvec, bio, j, 0)
1247 bvec->bv_page = bio_iovec_idx(r1_bio->bios[i], j)->bv_page;
1248 bio_put(r1_bio->bios[i]);
1249 bio->bi_sector = r1_bio->sector +
1250 conf->mirrors[i].rdev->data_offset;
1251 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1252 bio->bi_end_io = raid1_end_write_request;
1253 bio->bi_rw = WRITE;
1254 bio->bi_private = r1_bio;
1255 r1_bio->bios[i] = bio;
1256 generic_make_request(bio);
1257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 } else {
1259 int disk;
NeilBrownddaf22a2006-01-06 00:20:19 -08001260
1261 /* we got a read error. Maybe the drive is bad. Maybe just
1262 * the block and we can fix it.
1263 * We freeze all other IO, and try reading the block from
1264 * other devices. When we find one, we re-write
1265 * and check it that fixes the read error.
1266 * This is all done synchronously while the array is
1267 * frozen
1268 */
1269 sector_t sect = r1_bio->sector;
1270 int sectors = r1_bio->sectors;
1271 freeze_array(conf);
1272 while(sectors) {
1273 int s = sectors;
1274 int d = r1_bio->read_disk;
1275 int success = 0;
1276
1277 if (s > (PAGE_SIZE>>9))
1278 s = PAGE_SIZE >> 9;
1279
1280 do {
1281 rdev = conf->mirrors[d].rdev;
1282 if (rdev &&
1283 test_bit(In_sync, &rdev->flags) &&
1284 sync_page_io(rdev->bdev,
1285 sect + rdev->data_offset,
1286 s<<9,
1287 conf->tmppage, READ))
1288 success = 1;
1289 else {
1290 d++;
1291 if (d == conf->raid_disks)
1292 d = 0;
1293 }
1294 } while (!success && d != r1_bio->read_disk);
1295
1296 if (success) {
1297 /* write it back and re-read */
1298 while (d != r1_bio->read_disk) {
1299 if (d==0)
1300 d = conf->raid_disks;
1301 d--;
1302 rdev = conf->mirrors[d].rdev;
1303 if (rdev &&
1304 test_bit(In_sync, &rdev->flags)) {
1305 if (sync_page_io(rdev->bdev,
1306 sect + rdev->data_offset,
1307 s<<9, conf->tmppage, WRITE) == 0 ||
1308 sync_page_io(rdev->bdev,
1309 sect + rdev->data_offset,
1310 s<<9, conf->tmppage, READ) == 0) {
1311 /* Well, this device is dead */
1312 md_error(mddev, rdev);
1313 }
1314 }
1315 }
1316 } else {
1317 /* Cannot read from anywhere -- bye bye array */
1318 md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
1319 break;
1320 }
1321 sectors -= s;
1322 sect += s;
1323 }
1324
1325
1326 unfreeze_array(conf);
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 bio = r1_bio->bios[r1_bio->read_disk];
1329 if ((disk=read_balance(conf, r1_bio)) == -1) {
1330 printk(KERN_ALERT "raid1: %s: unrecoverable I/O"
1331 " read error for block %llu\n",
1332 bdevname(bio->bi_bdev,b),
1333 (unsigned long long)r1_bio->sector);
1334 raid_end_bio_io(r1_bio);
1335 } else {
1336 r1_bio->bios[r1_bio->read_disk] = NULL;
1337 r1_bio->read_disk = disk;
1338 bio_put(bio);
1339 bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
1340 r1_bio->bios[r1_bio->read_disk] = bio;
1341 rdev = conf->mirrors[disk].rdev;
1342 if (printk_ratelimit())
1343 printk(KERN_ERR "raid1: %s: redirecting sector %llu to"
1344 " another mirror\n",
1345 bdevname(rdev->bdev,b),
1346 (unsigned long long)r1_bio->sector);
1347 bio->bi_sector = r1_bio->sector + rdev->data_offset;
1348 bio->bi_bdev = rdev->bdev;
1349 bio->bi_end_io = raid1_end_read_request;
1350 bio->bi_rw = READ;
1351 bio->bi_private = r1_bio;
1352 unplug = 1;
1353 generic_make_request(bio);
1354 }
1355 }
1356 }
1357 spin_unlock_irqrestore(&conf->device_lock, flags);
1358 if (unplug)
1359 unplug_slaves(mddev);
1360}
1361
1362
1363static int init_resync(conf_t *conf)
1364{
1365 int buffs;
1366
1367 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
1368 if (conf->r1buf_pool)
1369 BUG();
1370 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
1371 conf->poolinfo);
1372 if (!conf->r1buf_pool)
1373 return -ENOMEM;
1374 conf->next_resync = 0;
1375 return 0;
1376}
1377
1378/*
1379 * perform a "sync" on one "block"
1380 *
1381 * We need to make sure that no normal I/O request - particularly write
1382 * requests - conflict with active sync requests.
1383 *
1384 * This is achieved by tracking pending requests and a 'barrier' concept
1385 * that can be installed to exclude normal IO requests.
1386 */
1387
NeilBrown57afd892005-06-21 17:17:13 -07001388static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
1390 conf_t *conf = mddev_to_conf(mddev);
1391 mirror_info_t *mirror;
1392 r1bio_t *r1_bio;
1393 struct bio *bio;
1394 sector_t max_sector, nr_sectors;
1395 int disk;
1396 int i;
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001397 int wonly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 int write_targets = 0;
NeilBrown191ea9b2005-06-21 17:17:23 -07001399 int sync_blocks;
NeilBrowne3b97032005-08-04 12:53:34 -07001400 int still_degraded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 if (!conf->r1buf_pool)
NeilBrown191ea9b2005-06-21 17:17:23 -07001403 {
1404/*
1405 printk("sync start - bitmap %p\n", mddev->bitmap);
1406*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07001408 return 0;
NeilBrown191ea9b2005-06-21 17:17:23 -07001409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 max_sector = mddev->size << 1;
1412 if (sector_nr >= max_sector) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001413 /* If we aborted, we need to abort the
1414 * sync on the 'current' bitmap chunk (there will
1415 * only be one in raid1 resync.
1416 * We can find the current addess in mddev->curr_resync
1417 */
NeilBrown6a806c52005-07-15 03:56:35 -07001418 if (mddev->curr_resync < max_sector) /* aborted */
1419 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
NeilBrown191ea9b2005-06-21 17:17:23 -07001420 &sync_blocks, 1);
NeilBrown6a806c52005-07-15 03:56:35 -07001421 else /* completed sync */
NeilBrown191ea9b2005-06-21 17:17:23 -07001422 conf->fullsync = 0;
NeilBrown6a806c52005-07-15 03:56:35 -07001423
1424 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 close_sync(conf);
1426 return 0;
1427 }
1428
NeilBrowne3b97032005-08-04 12:53:34 -07001429 /* before building a request, check if we can skip these blocks..
1430 * This call the bitmap_start_sync doesn't actually record anything
1431 */
1432 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrowne5de485f2005-11-08 21:39:38 -08001433 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001434 /* We can skip this block, and probably several more */
1435 *skipped = 1;
1436 return sync_blocks;
1437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 /*
NeilBrown17999be2006-01-06 00:20:12 -08001439 * If there is non-resync activity waiting for a turn,
1440 * and resync is going fast enough,
1441 * then let it though before starting on this new sync request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 */
NeilBrown17999be2006-01-06 00:20:12 -08001443 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 msleep_interruptible(1000);
NeilBrown17999be2006-01-06 00:20:12 -08001445
1446 raise_barrier(conf);
1447
1448 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 /*
1451 * If reconstructing, and >1 working disc,
1452 * could dedicate one to rebuild and others to
1453 * service read requests ..
1454 */
1455 disk = conf->last_used;
1456 /* make sure disk is operational */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001457 wonly = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 while (conf->mirrors[disk].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08001459 !test_bit(In_sync, &conf->mirrors[disk].rdev->flags) ||
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001460 test_bit(WriteMostly, &conf->mirrors[disk].rdev->flags)
1461 ) {
1462 if (conf->mirrors[disk].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001463 test_bit(In_sync, &conf->mirrors[disk].rdev->flags))
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001464 wonly = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if (disk <= 0)
1466 disk = conf->raid_disks;
1467 disk--;
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001468 if (disk == conf->last_used) {
1469 disk = wonly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 break;
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
1473 conf->last_used = disk;
1474 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
1475
1476
1477 mirror = conf->mirrors + disk;
1478
1479 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 r1_bio->mddev = mddev;
1482 r1_bio->sector = sector_nr;
NeilBrown191ea9b2005-06-21 17:17:23 -07001483 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 set_bit(R1BIO_IsSync, &r1_bio->state);
1485 r1_bio->read_disk = disk;
1486
1487 for (i=0; i < conf->raid_disks; i++) {
1488 bio = r1_bio->bios[i];
1489
1490 /* take from bio_init */
1491 bio->bi_next = NULL;
1492 bio->bi_flags |= 1 << BIO_UPTODATE;
1493 bio->bi_rw = 0;
1494 bio->bi_vcnt = 0;
1495 bio->bi_idx = 0;
1496 bio->bi_phys_segments = 0;
1497 bio->bi_hw_segments = 0;
1498 bio->bi_size = 0;
1499 bio->bi_end_io = NULL;
1500 bio->bi_private = NULL;
1501
1502 if (i == disk) {
1503 bio->bi_rw = READ;
1504 bio->bi_end_io = end_sync_read;
NeilBrowne3b97032005-08-04 12:53:34 -07001505 } else if (conf->mirrors[i].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08001506 test_bit(Faulty, &conf->mirrors[i].rdev->flags)) {
NeilBrowne3b97032005-08-04 12:53:34 -07001507 still_degraded = 1;
1508 continue;
NeilBrownb2d444d2005-11-08 21:39:31 -08001509 } else if (!test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
NeilBrowne5de485f2005-11-08 21:39:38 -08001510 sector_nr + RESYNC_SECTORS > mddev->recovery_cp ||
1511 test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 bio->bi_rw = WRITE;
1513 bio->bi_end_io = end_sync_write;
1514 write_targets ++;
1515 } else
NeilBrowne3b97032005-08-04 12:53:34 -07001516 /* no need to read or write here */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 continue;
1518 bio->bi_sector = sector_nr + conf->mirrors[i].rdev->data_offset;
1519 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1520 bio->bi_private = r1_bio;
1521 }
NeilBrown191ea9b2005-06-21 17:17:23 -07001522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (write_targets == 0) {
1524 /* There is nowhere to write, so all non-sync
1525 * drives must be failed - so we are finished
1526 */
NeilBrown57afd892005-06-21 17:17:13 -07001527 sector_t rv = max_sector - sector_nr;
1528 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 put_buf(r1_bio);
1530 rdev_dec_pending(conf->mirrors[disk].rdev, mddev);
1531 return rv;
1532 }
1533
1534 nr_sectors = 0;
NeilBrown289e99e2005-06-21 17:17:24 -07001535 sync_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 do {
1537 struct page *page;
1538 int len = PAGE_SIZE;
1539 if (sector_nr + (len>>9) > max_sector)
1540 len = (max_sector - sector_nr) << 9;
1541 if (len == 0)
1542 break;
NeilBrown6a806c52005-07-15 03:56:35 -07001543 if (sync_blocks == 0) {
1544 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
NeilBrowne5de485f2005-11-08 21:39:38 -08001545 &sync_blocks, still_degraded) &&
1546 !conf->fullsync &&
1547 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
NeilBrown6a806c52005-07-15 03:56:35 -07001548 break;
1549 if (sync_blocks < (PAGE_SIZE>>9))
1550 BUG();
1551 if (len > (sync_blocks<<9))
1552 len = sync_blocks<<9;
NeilBrownab7a30c2005-06-21 17:17:23 -07001553 }
NeilBrown191ea9b2005-06-21 17:17:23 -07001554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 for (i=0 ; i < conf->raid_disks; i++) {
1556 bio = r1_bio->bios[i];
1557 if (bio->bi_end_io) {
1558 page = r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page;
1559 if (bio_add_page(bio, page, len, 0) == 0) {
1560 /* stop here */
1561 r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page = page;
1562 while (i > 0) {
1563 i--;
1564 bio = r1_bio->bios[i];
NeilBrown6a806c52005-07-15 03:56:35 -07001565 if (bio->bi_end_io==NULL)
1566 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 /* remove last page from this bio */
1568 bio->bi_vcnt--;
1569 bio->bi_size -= len;
1570 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
1571 }
1572 goto bio_full;
1573 }
1574 }
1575 }
1576 nr_sectors += len>>9;
1577 sector_nr += len>>9;
NeilBrown191ea9b2005-06-21 17:17:23 -07001578 sync_blocks -= (len>>9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
1580 bio_full:
1581 bio = r1_bio->bios[disk];
1582 r1_bio->sectors = nr_sectors;
1583
1584 md_sync_acct(mirror->rdev->bdev, nr_sectors);
1585
1586 generic_make_request(bio);
1587
1588 return nr_sectors;
1589}
1590
1591static int run(mddev_t *mddev)
1592{
1593 conf_t *conf;
1594 int i, j, disk_idx;
1595 mirror_info_t *disk;
1596 mdk_rdev_t *rdev;
1597 struct list_head *tmp;
1598
1599 if (mddev->level != 1) {
1600 printk("raid1: %s: raid level not set to mirroring (%d)\n",
1601 mdname(mddev), mddev->level);
1602 goto out;
1603 }
1604 /*
1605 * copy the already verified devices into our private RAID1
1606 * bookkeeping area. [whatever we allocate in run(),
1607 * should be freed in stop()]
1608 */
1609 conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
1610 mddev->private = conf;
1611 if (!conf)
1612 goto out_no_mem;
1613
1614 memset(conf, 0, sizeof(*conf));
1615 conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks,
1616 GFP_KERNEL);
1617 if (!conf->mirrors)
1618 goto out_no_mem;
1619
1620 memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
1621
NeilBrownddaf22a2006-01-06 00:20:19 -08001622 conf->tmppage = alloc_page(GFP_KERNEL);
1623 if (!conf->tmppage)
1624 goto out_no_mem;
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 conf->poolinfo = kmalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
1627 if (!conf->poolinfo)
1628 goto out_no_mem;
1629 conf->poolinfo->mddev = mddev;
1630 conf->poolinfo->raid_disks = mddev->raid_disks;
1631 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1632 r1bio_pool_free,
1633 conf->poolinfo);
1634 if (!conf->r1bio_pool)
1635 goto out_no_mem;
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 ITERATE_RDEV(mddev, rdev, tmp) {
1638 disk_idx = rdev->raid_disk;
1639 if (disk_idx >= mddev->raid_disks
1640 || disk_idx < 0)
1641 continue;
1642 disk = conf->mirrors + disk_idx;
1643
1644 disk->rdev = rdev;
1645
1646 blk_queue_stack_limits(mddev->queue,
1647 rdev->bdev->bd_disk->queue);
1648 /* as we don't honour merge_bvec_fn, we must never risk
1649 * violating it, so limit ->max_sector to one PAGE, as
1650 * a one page request is never in violation.
1651 */
1652 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1653 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1654 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
1655
1656 disk->head_position = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08001657 if (!test_bit(Faulty, &rdev->flags) && test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 conf->working_disks++;
1659 }
1660 conf->raid_disks = mddev->raid_disks;
1661 conf->mddev = mddev;
1662 spin_lock_init(&conf->device_lock);
1663 INIT_LIST_HEAD(&conf->retry_list);
1664 if (conf->working_disks == 1)
1665 mddev->recovery_cp = MaxSector;
1666
1667 spin_lock_init(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -08001668 init_waitqueue_head(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
NeilBrown191ea9b2005-06-21 17:17:23 -07001670 bio_list_init(&conf->pending_bio_list);
1671 bio_list_init(&conf->flushing_bio_list);
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if (!conf->working_disks) {
1674 printk(KERN_ERR "raid1: no operational mirrors for %s\n",
1675 mdname(mddev));
1676 goto out_free_conf;
1677 }
1678
1679 mddev->degraded = 0;
1680 for (i = 0; i < conf->raid_disks; i++) {
1681
1682 disk = conf->mirrors + i;
1683
1684 if (!disk->rdev) {
1685 disk->head_position = 0;
1686 mddev->degraded++;
1687 }
1688 }
1689
1690 /*
1691 * find the first working one and use it as a starting point
1692 * to read balancing.
1693 */
1694 for (j = 0; j < conf->raid_disks &&
1695 (!conf->mirrors[j].rdev ||
NeilBrownb2d444d2005-11-08 21:39:31 -08001696 !test_bit(In_sync, &conf->mirrors[j].rdev->flags)) ; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 /* nothing */;
1698 conf->last_used = j;
1699
1700
NeilBrown191ea9b2005-06-21 17:17:23 -07001701 mddev->thread = md_register_thread(raid1d, mddev, "%s_raid1");
1702 if (!mddev->thread) {
1703 printk(KERN_ERR
1704 "raid1: couldn't allocate thread for %s\n",
1705 mdname(mddev));
1706 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 }
NeilBrown191ea9b2005-06-21 17:17:23 -07001708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 printk(KERN_INFO
1710 "raid1: raid set %s active with %d out of %d mirrors\n",
1711 mdname(mddev), mddev->raid_disks - mddev->degraded,
1712 mddev->raid_disks);
1713 /*
1714 * Ok, everything is just fine now
1715 */
1716 mddev->array_size = mddev->size;
1717
NeilBrown7a5febe2005-05-16 21:53:16 -07001718 mddev->queue->unplug_fn = raid1_unplug;
1719 mddev->queue->issue_flush_fn = raid1_issue_flush;
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return 0;
1722
1723out_no_mem:
1724 printk(KERN_ERR "raid1: couldn't allocate memory for %s\n",
1725 mdname(mddev));
1726
1727out_free_conf:
1728 if (conf) {
1729 if (conf->r1bio_pool)
1730 mempool_destroy(conf->r1bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07001731 kfree(conf->mirrors);
NeilBrownddaf22a2006-01-06 00:20:19 -08001732 __free_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07001733 kfree(conf->poolinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 kfree(conf);
1735 mddev->private = NULL;
1736 }
1737out:
1738 return -EIO;
1739}
1740
1741static int stop(mddev_t *mddev)
1742{
1743 conf_t *conf = mddev_to_conf(mddev);
NeilBrown4b6d2872005-09-09 16:23:47 -07001744 struct bitmap *bitmap = mddev->bitmap;
1745 int behind_wait = 0;
1746
1747 /* wait for behind writes to complete */
1748 while (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
1749 behind_wait++;
1750 printk(KERN_INFO "raid1: behind writes in progress on device %s, waiting to stop (%d)\n", mdname(mddev), behind_wait);
1751 set_current_state(TASK_UNINTERRUPTIBLE);
1752 schedule_timeout(HZ); /* wait a second */
1753 /* need to kick something here to make sure I/O goes? */
1754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 md_unregister_thread(mddev->thread);
1757 mddev->thread = NULL;
1758 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
1759 if (conf->r1bio_pool)
1760 mempool_destroy(conf->r1bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07001761 kfree(conf->mirrors);
1762 kfree(conf->poolinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 kfree(conf);
1764 mddev->private = NULL;
1765 return 0;
1766}
1767
1768static int raid1_resize(mddev_t *mddev, sector_t sectors)
1769{
1770 /* no resync is happening, and there is enough space
1771 * on all devices, so we can resize.
1772 * We need to make sure resync covers any new space.
1773 * If the array is shrinking we should possibly wait until
1774 * any io in the removed space completes, but it hardly seems
1775 * worth it.
1776 */
1777 mddev->array_size = sectors>>1;
1778 set_capacity(mddev->gendisk, mddev->array_size << 1);
1779 mddev->changed = 1;
1780 if (mddev->array_size > mddev->size && mddev->recovery_cp == MaxSector) {
1781 mddev->recovery_cp = mddev->size << 1;
1782 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1783 }
1784 mddev->size = mddev->array_size;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07001785 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 return 0;
1787}
1788
1789static int raid1_reshape(mddev_t *mddev, int raid_disks)
1790{
1791 /* We need to:
1792 * 1/ resize the r1bio_pool
1793 * 2/ resize conf->mirrors
1794 *
1795 * We allocate a new r1bio_pool if we can.
1796 * Then raise a device barrier and wait until all IO stops.
1797 * Then resize conf->mirrors and swap in the new r1bio pool.
NeilBrown6ea9c072005-06-21 17:17:09 -07001798 *
1799 * At the same time, we "pack" the devices so that all the missing
1800 * devices have the higher raid_disk numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 */
1802 mempool_t *newpool, *oldpool;
1803 struct pool_info *newpoolinfo;
1804 mirror_info_t *newmirrors;
1805 conf_t *conf = mddev_to_conf(mddev);
NeilBrown6ea9c072005-06-21 17:17:09 -07001806 int cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
NeilBrown6ea9c072005-06-21 17:17:09 -07001808 int d, d2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
NeilBrown6ea9c072005-06-21 17:17:09 -07001810 if (raid_disks < conf->raid_disks) {
1811 cnt=0;
1812 for (d= 0; d < conf->raid_disks; d++)
1813 if (conf->mirrors[d].rdev)
1814 cnt++;
1815 if (cnt > raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return -EBUSY;
NeilBrown6ea9c072005-06-21 17:17:09 -07001817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
1820 if (!newpoolinfo)
1821 return -ENOMEM;
1822 newpoolinfo->mddev = mddev;
1823 newpoolinfo->raid_disks = raid_disks;
1824
1825 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1826 r1bio_pool_free, newpoolinfo);
1827 if (!newpool) {
1828 kfree(newpoolinfo);
1829 return -ENOMEM;
1830 }
1831 newmirrors = kmalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
1832 if (!newmirrors) {
1833 kfree(newpoolinfo);
1834 mempool_destroy(newpool);
1835 return -ENOMEM;
1836 }
1837 memset(newmirrors, 0, sizeof(struct mirror_info)*raid_disks);
1838
NeilBrown17999be2006-01-06 00:20:12 -08001839 raise_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
1841 /* ok, everything is stopped */
1842 oldpool = conf->r1bio_pool;
1843 conf->r1bio_pool = newpool;
NeilBrown6ea9c072005-06-21 17:17:09 -07001844
1845 for (d=d2=0; d < conf->raid_disks; d++)
1846 if (conf->mirrors[d].rdev) {
1847 conf->mirrors[d].rdev->raid_disk = d2;
1848 newmirrors[d2++].rdev = conf->mirrors[d].rdev;
1849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 kfree(conf->mirrors);
1851 conf->mirrors = newmirrors;
1852 kfree(conf->poolinfo);
1853 conf->poolinfo = newpoolinfo;
1854
1855 mddev->degraded += (raid_disks - conf->raid_disks);
1856 conf->raid_disks = mddev->raid_disks = raid_disks;
1857
NeilBrown6ea9c072005-06-21 17:17:09 -07001858 conf->last_used = 0; /* just make sure it is in-range */
NeilBrown17999be2006-01-06 00:20:12 -08001859 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1862 md_wakeup_thread(mddev->thread);
1863
1864 mempool_destroy(oldpool);
1865 return 0;
1866}
1867
NeilBrown500af872005-09-09 16:23:58 -07001868static void raid1_quiesce(mddev_t *mddev, int state)
NeilBrown36fa3062005-09-09 16:23:45 -07001869{
1870 conf_t *conf = mddev_to_conf(mddev);
1871
1872 switch(state) {
NeilBrown9e6603d2005-09-09 16:23:48 -07001873 case 1:
NeilBrown17999be2006-01-06 00:20:12 -08001874 raise_barrier(conf);
NeilBrown36fa3062005-09-09 16:23:45 -07001875 break;
NeilBrown9e6603d2005-09-09 16:23:48 -07001876 case 0:
NeilBrown17999be2006-01-06 00:20:12 -08001877 lower_barrier(conf);
NeilBrown36fa3062005-09-09 16:23:45 -07001878 break;
1879 }
NeilBrown36fa3062005-09-09 16:23:45 -07001880}
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883static mdk_personality_t raid1_personality =
1884{
1885 .name = "raid1",
1886 .owner = THIS_MODULE,
1887 .make_request = make_request,
1888 .run = run,
1889 .stop = stop,
1890 .status = status,
1891 .error_handler = error,
1892 .hot_add_disk = raid1_add_disk,
1893 .hot_remove_disk= raid1_remove_disk,
1894 .spare_active = raid1_spare_active,
1895 .sync_request = sync_request,
1896 .resize = raid1_resize,
1897 .reshape = raid1_reshape,
NeilBrown36fa3062005-09-09 16:23:45 -07001898 .quiesce = raid1_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899};
1900
1901static int __init raid_init(void)
1902{
1903 return register_md_personality(RAID1, &raid1_personality);
1904}
1905
1906static void raid_exit(void)
1907{
1908 unregister_md_personality(RAID1);
1909}
1910
1911module_init(raid_init);
1912module_exit(raid_exit);
1913MODULE_LICENSE("GPL");
1914MODULE_ALIAS("md-personality-3"); /* RAID1 */