blob: 14a8fe0349c75140d5f08e0bb6a151a7784c0f7b [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);
NeilBrown3e198f72006-01-06 00:20:21 -0800180 int i;
181
182 for (i=0; i<conf->raid_disks; i++) {
183 struct bio *bio = r1_bio->bios[i];
184 if (bio->bi_end_io)
185 rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev);
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 mempool_free(r1_bio, conf->r1buf_pool);
189
NeilBrown17999be2006-01-06 00:20:12 -0800190 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
193static void reschedule_retry(r1bio_t *r1_bio)
194{
195 unsigned long flags;
196 mddev_t *mddev = r1_bio->mddev;
197 conf_t *conf = mddev_to_conf(mddev);
198
199 spin_lock_irqsave(&conf->device_lock, flags);
200 list_add(&r1_bio->retry_list, &conf->retry_list);
NeilBrownddaf22a2006-01-06 00:20:19 -0800201 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 spin_unlock_irqrestore(&conf->device_lock, flags);
203
NeilBrown17999be2006-01-06 00:20:12 -0800204 wake_up(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 md_wakeup_thread(mddev->thread);
206}
207
208/*
209 * raid_end_bio_io() is called when we have finished servicing a mirrored
210 * operation and are ready to return a success/failure code to the buffer
211 * cache layer.
212 */
213static void raid_end_bio_io(r1bio_t *r1_bio)
214{
215 struct bio *bio = r1_bio->master_bio;
216
NeilBrown4b6d2872005-09-09 16:23:47 -0700217 /* if nobody has done the final endio yet, do it now */
218 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
219 PRINTK(KERN_DEBUG "raid1: sync end %s on sectors %llu-%llu\n",
220 (bio_data_dir(bio) == WRITE) ? "write" : "read",
221 (unsigned long long) bio->bi_sector,
222 (unsigned long long) bio->bi_sector +
223 (bio->bi_size >> 9) - 1);
224
225 bio_endio(bio, bio->bi_size,
226 test_bit(R1BIO_Uptodate, &r1_bio->state) ? 0 : -EIO);
227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 free_r1bio(r1_bio);
229}
230
231/*
232 * Update disk head position estimator based on IRQ completion info.
233 */
234static inline void update_head_pos(int disk, r1bio_t *r1_bio)
235{
236 conf_t *conf = mddev_to_conf(r1_bio->mddev);
237
238 conf->mirrors[disk].head_position =
239 r1_bio->sector + (r1_bio->sectors);
240}
241
242static int raid1_end_read_request(struct bio *bio, unsigned int bytes_done, int error)
243{
244 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
245 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
246 int mirror;
247 conf_t *conf = mddev_to_conf(r1_bio->mddev);
248
249 if (bio->bi_size)
250 return 1;
251
252 mirror = r1_bio->read_disk;
253 /*
254 * this branch is our 'one mirror IO has finished' event handler:
255 */
NeilBrownddaf22a2006-01-06 00:20:19 -0800256 update_head_pos(mirror, r1_bio);
257
258 if (uptodate || conf->working_disks <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 /*
260 * Set R1BIO_Uptodate in our master bio, so that
261 * we will return a good error code for to the higher
262 * levels even if IO on some other mirrored buffer fails.
263 *
264 * The 'master' represents the composite IO operation to
265 * user-side. So if something waits for IO, then it will
266 * wait for the 'master' bio.
267 */
268 set_bit(R1BIO_Uptodate, &r1_bio->state);
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 raid_end_bio_io(r1_bio);
NeilBrownddaf22a2006-01-06 00:20:19 -0800271 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 /*
273 * oops, read error:
274 */
275 char b[BDEVNAME_SIZE];
276 if (printk_ratelimit())
277 printk(KERN_ERR "raid1: %s: rescheduling sector %llu\n",
278 bdevname(conf->mirrors[mirror].rdev->bdev,b), (unsigned long long)r1_bio->sector);
279 reschedule_retry(r1_bio);
280 }
281
282 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
283 return 0;
284}
285
286static int raid1_end_write_request(struct bio *bio, unsigned int bytes_done, int error)
287{
288 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
289 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
NeilBrowna9701a32005-11-08 21:39:34 -0800290 int mirror, behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 conf_t *conf = mddev_to_conf(r1_bio->mddev);
292
293 if (bio->bi_size)
294 return 1;
295
296 for (mirror = 0; mirror < conf->raid_disks; mirror++)
297 if (r1_bio->bios[mirror] == bio)
298 break;
299
NeilBrowna9701a32005-11-08 21:39:34 -0800300 if (error == -ENOTSUPP && test_bit(R1BIO_Barrier, &r1_bio->state)) {
301 set_bit(BarriersNotsupp, &conf->mirrors[mirror].rdev->flags);
302 set_bit(R1BIO_BarrierRetry, &r1_bio->state);
303 r1_bio->mddev->barriers_work = 0;
304 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 /*
NeilBrowna9701a32005-11-08 21:39:34 -0800306 * this branch is our 'one mirror IO has finished' event handler:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 */
NeilBrowna9701a32005-11-08 21:39:34 -0800308 r1_bio->bios[mirror] = NULL;
NeilBrowna9701a32005-11-08 21:39:34 -0800309 if (!uptodate) {
310 md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
311 /* an I/O failed, we can't clear the bitmap */
312 set_bit(R1BIO_Degraded, &r1_bio->state);
313 } else
314 /*
315 * Set R1BIO_Uptodate in our master bio, so that
316 * we will return a good error code for to the higher
317 * levels even if IO on some other mirrored buffer fails.
318 *
319 * The 'master' represents the composite IO operation to
320 * user-side. So if something waits for IO, then it will
321 * wait for the 'master' bio.
322 */
323 set_bit(R1BIO_Uptodate, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
NeilBrowna9701a32005-11-08 21:39:34 -0800325 update_head_pos(mirror, r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
NeilBrowna9701a32005-11-08 21:39:34 -0800327 if (behind) {
328 if (test_bit(WriteMostly, &conf->mirrors[mirror].rdev->flags))
329 atomic_dec(&r1_bio->behind_remaining);
NeilBrown4b6d2872005-09-09 16:23:47 -0700330
NeilBrowna9701a32005-11-08 21:39:34 -0800331 /* In behind mode, we ACK the master bio once the I/O has safely
332 * reached all non-writemostly disks. Setting the Returned bit
333 * ensures that this gets done only once -- we don't ever want to
334 * return -EIO here, instead we'll wait */
NeilBrown4b6d2872005-09-09 16:23:47 -0700335
NeilBrowna9701a32005-11-08 21:39:34 -0800336 if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
337 test_bit(R1BIO_Uptodate, &r1_bio->state)) {
338 /* Maybe we can return now */
339 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
340 struct bio *mbio = r1_bio->master_bio;
341 PRINTK(KERN_DEBUG "raid1: behind end write sectors %llu-%llu\n",
342 (unsigned long long) mbio->bi_sector,
343 (unsigned long long) mbio->bi_sector +
344 (mbio->bi_size >> 9) - 1);
345 bio_endio(mbio, mbio->bi_size, 0);
346 }
NeilBrown4b6d2872005-09-09 16:23:47 -0700347 }
348 }
349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 /*
351 *
352 * Let's see if all mirrored write operations have finished
353 * already.
354 */
355 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrowna9701a32005-11-08 21:39:34 -0800356 if (test_bit(R1BIO_BarrierRetry, &r1_bio->state)) {
357 reschedule_retry(r1_bio);
358 /* Don't dec_pending yet, we want to hold
359 * the reference over the retry
360 */
361 return 0;
362 }
NeilBrown4b6d2872005-09-09 16:23:47 -0700363 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
364 /* free extra copy of the data pages */
365 int i = bio->bi_vcnt;
366 while (i--)
367 __free_page(bio->bi_io_vec[i].bv_page);
368 }
NeilBrown191ea9b2005-06-21 17:17:23 -0700369 /* clear the bitmap if all writes complete successfully */
370 bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
371 r1_bio->sectors,
NeilBrown4b6d2872005-09-09 16:23:47 -0700372 !test_bit(R1BIO_Degraded, &r1_bio->state),
373 behind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 md_write_end(r1_bio->mddev);
375 raid_end_bio_io(r1_bio);
376 }
377
NeilBrown3795bb02005-12-12 02:39:16 -0800378 if (r1_bio->bios[mirror]==NULL)
379 bio_put(bio);
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
382 return 0;
383}
384
385
386/*
387 * This routine returns the disk from which the requested read should
388 * be done. There is a per-array 'next expected sequential IO' sector
389 * number - if this matches on the next IO then we use the last disk.
390 * There is also a per-disk 'last know head position' sector that is
391 * maintained from IRQ contexts, both the normal and the resync IO
392 * completion handlers update this position correctly. If there is no
393 * perfect sequential match then we pick the disk whose head is closest.
394 *
395 * If there are 2 mirrors in the same 2 devices, performance degrades
396 * because position is mirror, not device based.
397 *
398 * The rdev for the device selected will have nr_pending incremented.
399 */
400static int read_balance(conf_t *conf, r1bio_t *r1_bio)
401{
402 const unsigned long this_sector = r1_bio->sector;
403 int new_disk = conf->last_used, disk = new_disk;
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700404 int wonly_disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 const int sectors = r1_bio->sectors;
406 sector_t new_distance, current_distance;
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700407 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 rcu_read_lock();
410 /*
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700411 * Check if we can balance. We can balance on the whole
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 * device if no resync is going on, or below the resync window.
413 * We take the first readable disk when above the resync window.
414 */
415 retry:
416 if (conf->mddev->recovery_cp < MaxSector &&
417 (this_sector + sectors >= conf->next_resync)) {
418 /* Choose the first operation device, for consistancy */
419 new_disk = 0;
420
Suzanne Woodd6065f72005-11-08 21:39:27 -0800421 for (rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800422 !rdev || !test_bit(In_sync, &rdev->flags)
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700423 || test_bit(WriteMostly, &rdev->flags);
Suzanne Woodd6065f72005-11-08 21:39:27 -0800424 rdev = rcu_dereference(conf->mirrors[++new_disk].rdev)) {
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700425
NeilBrownb2d444d2005-11-08 21:39:31 -0800426 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700427 wonly_disk = new_disk;
428
429 if (new_disk == conf->raid_disks - 1) {
430 new_disk = wonly_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 break;
432 }
433 }
434 goto rb_out;
435 }
436
437
438 /* make sure the disk is operational */
Suzanne Woodd6065f72005-11-08 21:39:27 -0800439 for (rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800440 !rdev || !test_bit(In_sync, &rdev->flags) ||
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700441 test_bit(WriteMostly, &rdev->flags);
Suzanne Woodd6065f72005-11-08 21:39:27 -0800442 rdev = rcu_dereference(conf->mirrors[new_disk].rdev)) {
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700443
NeilBrownb2d444d2005-11-08 21:39:31 -0800444 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700445 wonly_disk = new_disk;
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (new_disk <= 0)
448 new_disk = conf->raid_disks;
449 new_disk--;
450 if (new_disk == disk) {
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700451 new_disk = wonly_disk;
452 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700455
456 if (new_disk < 0)
457 goto rb_out;
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 disk = new_disk;
460 /* now disk == new_disk == starting point for search */
461
462 /*
463 * Don't change to another disk for sequential reads:
464 */
465 if (conf->next_seq_sect == this_sector)
466 goto rb_out;
467 if (this_sector == conf->mirrors[new_disk].head_position)
468 goto rb_out;
469
470 current_distance = abs(this_sector - conf->mirrors[disk].head_position);
471
472 /* Find the disk whose head is closest */
473
474 do {
475 if (disk <= 0)
476 disk = conf->raid_disks;
477 disk--;
478
Suzanne Woodd6065f72005-11-08 21:39:27 -0800479 rdev = rcu_dereference(conf->mirrors[disk].rdev);
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700480
481 if (!rdev ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800482 !test_bit(In_sync, &rdev->flags) ||
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700483 test_bit(WriteMostly, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 continue;
485
486 if (!atomic_read(&rdev->nr_pending)) {
487 new_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 break;
489 }
490 new_distance = abs(this_sector - conf->mirrors[disk].head_position);
491 if (new_distance < current_distance) {
492 current_distance = new_distance;
493 new_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495 } while (disk != conf->last_used);
496
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700497 rb_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499
500 if (new_disk >= 0) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800501 rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700502 if (!rdev)
503 goto retry;
504 atomic_inc(&rdev->nr_pending);
NeilBrownb2d444d2005-11-08 21:39:31 -0800505 if (!test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /* cannot risk returning a device that failed
507 * before we inc'ed nr_pending
508 */
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700509 atomic_dec(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 goto retry;
511 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700512 conf->next_seq_sect = this_sector + sectors;
513 conf->last_used = new_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515 rcu_read_unlock();
516
517 return new_disk;
518}
519
520static void unplug_slaves(mddev_t *mddev)
521{
522 conf_t *conf = mddev_to_conf(mddev);
523 int i;
524
525 rcu_read_lock();
526 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800527 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800528 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
530
531 atomic_inc(&rdev->nr_pending);
532 rcu_read_unlock();
533
534 if (r_queue->unplug_fn)
535 r_queue->unplug_fn(r_queue);
536
537 rdev_dec_pending(rdev, mddev);
538 rcu_read_lock();
539 }
540 }
541 rcu_read_unlock();
542}
543
544static void raid1_unplug(request_queue_t *q)
545{
NeilBrown191ea9b2005-06-21 17:17:23 -0700546 mddev_t *mddev = q->queuedata;
547
548 unplug_slaves(mddev);
549 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
552static int raid1_issue_flush(request_queue_t *q, struct gendisk *disk,
553 sector_t *error_sector)
554{
555 mddev_t *mddev = q->queuedata;
556 conf_t *conf = mddev_to_conf(mddev);
557 int i, ret = 0;
558
559 rcu_read_lock();
560 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800561 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800562 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 struct block_device *bdev = rdev->bdev;
564 request_queue_t *r_queue = bdev_get_queue(bdev);
565
566 if (!r_queue->issue_flush_fn)
567 ret = -EOPNOTSUPP;
568 else {
569 atomic_inc(&rdev->nr_pending);
570 rcu_read_unlock();
571 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
572 error_sector);
573 rdev_dec_pending(rdev, mddev);
574 rcu_read_lock();
575 }
576 }
577 }
578 rcu_read_unlock();
579 return ret;
580}
581
NeilBrown17999be2006-01-06 00:20:12 -0800582/* Barriers....
583 * Sometimes we need to suspend IO while we do something else,
584 * either some resync/recovery, or reconfigure the array.
585 * To do this we raise a 'barrier'.
586 * The 'barrier' is a counter that can be raised multiple times
587 * to count how many activities are happening which preclude
588 * normal IO.
589 * We can only raise the barrier if there is no pending IO.
590 * i.e. if nr_pending == 0.
591 * We choose only to raise the barrier if no-one is waiting for the
592 * barrier to go down. This means that as soon as an IO request
593 * is ready, no other operations which require a barrier will start
594 * until the IO request has had a chance.
595 *
596 * So: regular IO calls 'wait_barrier'. When that returns there
597 * is no backgroup IO happening, It must arrange to call
598 * allow_barrier when it has finished its IO.
599 * backgroup IO calls must call raise_barrier. Once that returns
600 * there is no normal IO happeing. It must arrange to call
601 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 */
603#define RESYNC_DEPTH 32
604
NeilBrown17999be2006-01-06 00:20:12 -0800605static void raise_barrier(conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 spin_lock_irq(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800608
609 /* Wait until no block IO is waiting */
610 wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
611 conf->resync_lock,
612 raid1_unplug(conf->mddev->queue));
613
614 /* block any new IO from starting */
615 conf->barrier++;
616
617 /* No wait for all pending IO to complete */
618 wait_event_lock_irq(conf->wait_barrier,
619 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
620 conf->resync_lock,
621 raid1_unplug(conf->mddev->queue));
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 spin_unlock_irq(&conf->resync_lock);
624}
625
NeilBrown17999be2006-01-06 00:20:12 -0800626static void lower_barrier(conf_t *conf)
627{
628 unsigned long flags;
629 spin_lock_irqsave(&conf->resync_lock, flags);
630 conf->barrier--;
631 spin_unlock_irqrestore(&conf->resync_lock, flags);
632 wake_up(&conf->wait_barrier);
633}
634
635static void wait_barrier(conf_t *conf)
636{
637 spin_lock_irq(&conf->resync_lock);
638 if (conf->barrier) {
639 conf->nr_waiting++;
640 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
641 conf->resync_lock,
642 raid1_unplug(conf->mddev->queue));
643 conf->nr_waiting--;
644 }
645 conf->nr_pending++;
646 spin_unlock_irq(&conf->resync_lock);
647}
648
649static void allow_barrier(conf_t *conf)
650{
651 unsigned long flags;
652 spin_lock_irqsave(&conf->resync_lock, flags);
653 conf->nr_pending--;
654 spin_unlock_irqrestore(&conf->resync_lock, flags);
655 wake_up(&conf->wait_barrier);
656}
657
NeilBrownddaf22a2006-01-06 00:20:19 -0800658static void freeze_array(conf_t *conf)
659{
660 /* stop syncio and normal IO and wait for everything to
661 * go quite.
662 * We increment barrier and nr_waiting, and then
663 * wait until barrier+nr_pending match nr_queued+2
664 */
665 spin_lock_irq(&conf->resync_lock);
666 conf->barrier++;
667 conf->nr_waiting++;
668 wait_event_lock_irq(conf->wait_barrier,
669 conf->barrier+conf->nr_pending == conf->nr_queued+2,
670 conf->resync_lock,
671 raid1_unplug(conf->mddev->queue));
672 spin_unlock_irq(&conf->resync_lock);
673}
674static void unfreeze_array(conf_t *conf)
675{
676 /* reverse the effect of the freeze */
677 spin_lock_irq(&conf->resync_lock);
678 conf->barrier--;
679 conf->nr_waiting--;
680 wake_up(&conf->wait_barrier);
681 spin_unlock_irq(&conf->resync_lock);
682}
683
NeilBrown17999be2006-01-06 00:20:12 -0800684
NeilBrown4b6d2872005-09-09 16:23:47 -0700685/* duplicate the data pages for behind I/O */
686static struct page **alloc_behind_pages(struct bio *bio)
687{
688 int i;
689 struct bio_vec *bvec;
690 struct page **pages = kmalloc(bio->bi_vcnt * sizeof(struct page *),
691 GFP_NOIO);
692 if (unlikely(!pages))
693 goto do_sync_io;
694
695 memset(pages, 0, bio->bi_vcnt * sizeof(struct page *));
696
697 bio_for_each_segment(bvec, bio, i) {
698 pages[i] = alloc_page(GFP_NOIO);
699 if (unlikely(!pages[i]))
700 goto do_sync_io;
701 memcpy(kmap(pages[i]) + bvec->bv_offset,
702 kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
703 kunmap(pages[i]);
704 kunmap(bvec->bv_page);
705 }
706
707 return pages;
708
709do_sync_io:
710 if (pages)
711 for (i = 0; i < bio->bi_vcnt && pages[i]; i++)
712 __free_page(pages[i]);
713 kfree(pages);
714 PRINTK("%dB behind alloc failed, doing sync I/O\n", bio->bi_size);
715 return NULL;
716}
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718static int make_request(request_queue_t *q, struct bio * bio)
719{
720 mddev_t *mddev = q->queuedata;
721 conf_t *conf = mddev_to_conf(mddev);
722 mirror_info_t *mirror;
723 r1bio_t *r1_bio;
724 struct bio *read_bio;
NeilBrown191ea9b2005-06-21 17:17:23 -0700725 int i, targets = 0, disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 mdk_rdev_t *rdev;
NeilBrown191ea9b2005-06-21 17:17:23 -0700727 struct bitmap *bitmap = mddev->bitmap;
728 unsigned long flags;
729 struct bio_list bl;
NeilBrown4b6d2872005-09-09 16:23:47 -0700730 struct page **behind_pages = NULL;
Jens Axboea3623572005-11-01 09:26:16 +0100731 const int rw = bio_data_dir(bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800732 int do_barriers;
NeilBrown191ea9b2005-06-21 17:17:23 -0700733
NeilBrowna9701a32005-11-08 21:39:34 -0800734 if (unlikely(!mddev->barriers_work && bio_barrier(bio))) {
NeilBrowne5dcdd82005-09-09 16:23:41 -0700735 bio_endio(bio, bio->bi_size, -EOPNOTSUPP);
736 return 0;
737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 /*
740 * Register the new request and wait if the reconstruction
741 * thread has put up a bar for new requests.
742 * Continue immediately if no resync is active currently.
743 */
NeilBrown3d310eb2005-06-21 17:17:26 -0700744 md_write_start(mddev, bio); /* wait on superblock update early */
745
NeilBrown17999be2006-01-06 00:20:12 -0800746 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Jens Axboea3623572005-11-01 09:26:16 +0100748 disk_stat_inc(mddev->gendisk, ios[rw]);
749 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 /*
752 * make_request() can abort the operation when READA is being
753 * used and no empty request is available.
754 *
755 */
756 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
757
758 r1_bio->master_bio = bio;
759 r1_bio->sectors = bio->bi_size >> 9;
NeilBrown191ea9b2005-06-21 17:17:23 -0700760 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 r1_bio->mddev = mddev;
762 r1_bio->sector = bio->bi_sector;
763
Jens Axboea3623572005-11-01 09:26:16 +0100764 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 /*
766 * read balancing logic:
767 */
768 int rdisk = read_balance(conf, r1_bio);
769
770 if (rdisk < 0) {
771 /* couldn't find anywhere to read from */
772 raid_end_bio_io(r1_bio);
773 return 0;
774 }
775 mirror = conf->mirrors + rdisk;
776
777 r1_bio->read_disk = rdisk;
778
779 read_bio = bio_clone(bio, GFP_NOIO);
780
781 r1_bio->bios[rdisk] = read_bio;
782
783 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
784 read_bio->bi_bdev = mirror->rdev->bdev;
785 read_bio->bi_end_io = raid1_end_read_request;
786 read_bio->bi_rw = READ;
787 read_bio->bi_private = r1_bio;
788
789 generic_make_request(read_bio);
790 return 0;
791 }
792
793 /*
794 * WRITE:
795 */
796 /* first select target devices under spinlock and
797 * inc refcount on their rdev. Record them by setting
798 * bios[x] to bio
799 */
800 disks = conf->raid_disks;
NeilBrown191ea9b2005-06-21 17:17:23 -0700801#if 0
802 { static int first=1;
803 if (first) printk("First Write sector %llu disks %d\n",
804 (unsigned long long)r1_bio->sector, disks);
805 first = 0;
806 }
807#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 rcu_read_lock();
809 for (i = 0; i < disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800810 if ((rdev=rcu_dereference(conf->mirrors[i].rdev)) != NULL &&
NeilBrownb2d444d2005-11-08 21:39:31 -0800811 !test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 atomic_inc(&rdev->nr_pending);
NeilBrownb2d444d2005-11-08 21:39:31 -0800813 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 atomic_dec(&rdev->nr_pending);
815 r1_bio->bios[i] = NULL;
816 } else
817 r1_bio->bios[i] = bio;
NeilBrown191ea9b2005-06-21 17:17:23 -0700818 targets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 } else
820 r1_bio->bios[i] = NULL;
821 }
822 rcu_read_unlock();
823
NeilBrown4b6d2872005-09-09 16:23:47 -0700824 BUG_ON(targets == 0); /* we never fail the last device */
825
NeilBrown191ea9b2005-06-21 17:17:23 -0700826 if (targets < conf->raid_disks) {
827 /* array is degraded, we will not clear the bitmap
828 * on I/O completion (see raid1_end_write_request) */
829 set_bit(R1BIO_Degraded, &r1_bio->state);
830 }
NeilBrown06d91a52005-06-21 17:17:12 -0700831
NeilBrown4b6d2872005-09-09 16:23:47 -0700832 /* do behind I/O ? */
833 if (bitmap &&
834 atomic_read(&bitmap->behind_writes) < bitmap->max_write_behind &&
835 (behind_pages = alloc_behind_pages(bio)) != NULL)
836 set_bit(R1BIO_BehindIO, &r1_bio->state);
837
NeilBrown191ea9b2005-06-21 17:17:23 -0700838 atomic_set(&r1_bio->remaining, 0);
NeilBrown4b6d2872005-09-09 16:23:47 -0700839 atomic_set(&r1_bio->behind_remaining, 0);
NeilBrown191ea9b2005-06-21 17:17:23 -0700840
NeilBrowna9701a32005-11-08 21:39:34 -0800841 do_barriers = bio->bi_rw & BIO_RW_BARRIER;
842 if (do_barriers)
843 set_bit(R1BIO_Barrier, &r1_bio->state);
844
NeilBrown191ea9b2005-06-21 17:17:23 -0700845 bio_list_init(&bl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 for (i = 0; i < disks; i++) {
847 struct bio *mbio;
848 if (!r1_bio->bios[i])
849 continue;
850
851 mbio = bio_clone(bio, GFP_NOIO);
852 r1_bio->bios[i] = mbio;
853
854 mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
855 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
856 mbio->bi_end_io = raid1_end_write_request;
NeilBrowna9701a32005-11-08 21:39:34 -0800857 mbio->bi_rw = WRITE | do_barriers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 mbio->bi_private = r1_bio;
859
NeilBrown4b6d2872005-09-09 16:23:47 -0700860 if (behind_pages) {
861 struct bio_vec *bvec;
862 int j;
863
864 /* Yes, I really want the '__' version so that
865 * we clear any unused pointer in the io_vec, rather
866 * than leave them unchanged. This is important
867 * because when we come to free the pages, we won't
868 * know the originial bi_idx, so we just free
869 * them all
870 */
871 __bio_for_each_segment(bvec, mbio, j, 0)
872 bvec->bv_page = behind_pages[j];
873 if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
874 atomic_inc(&r1_bio->behind_remaining);
875 }
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 atomic_inc(&r1_bio->remaining);
NeilBrown191ea9b2005-06-21 17:17:23 -0700878
879 bio_list_add(&bl, mbio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
NeilBrown4b6d2872005-09-09 16:23:47 -0700881 kfree(behind_pages); /* the behind pages are attached to the bios now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
NeilBrown4b6d2872005-09-09 16:23:47 -0700883 bitmap_startwrite(bitmap, bio->bi_sector, r1_bio->sectors,
884 test_bit(R1BIO_BehindIO, &r1_bio->state));
NeilBrown191ea9b2005-06-21 17:17:23 -0700885 spin_lock_irqsave(&conf->device_lock, flags);
886 bio_list_merge(&conf->pending_bio_list, &bl);
887 bio_list_init(&bl);
888
889 blk_plug_device(mddev->queue);
890 spin_unlock_irqrestore(&conf->device_lock, flags);
891
892#if 0
893 while ((bio = bio_list_pop(&bl)) != NULL)
894 generic_make_request(bio);
895#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 return 0;
898}
899
900static void status(struct seq_file *seq, mddev_t *mddev)
901{
902 conf_t *conf = mddev_to_conf(mddev);
903 int i;
904
905 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
906 conf->working_disks);
907 for (i = 0; i < conf->raid_disks; i++)
908 seq_printf(seq, "%s",
909 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -0800910 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 seq_printf(seq, "]");
912}
913
914
915static void error(mddev_t *mddev, mdk_rdev_t *rdev)
916{
917 char b[BDEVNAME_SIZE];
918 conf_t *conf = mddev_to_conf(mddev);
919
920 /*
921 * If it is not operational, then we have already marked it as dead
922 * else if it is the last working disks, ignore the error, let the
923 * next level up know.
924 * else mark the drive as failed
925 */
NeilBrownb2d444d2005-11-08 21:39:31 -0800926 if (test_bit(In_sync, &rdev->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 && conf->working_disks == 1)
928 /*
929 * Don't fail the drive, act as though we were just a
930 * normal single drive
931 */
932 return;
NeilBrownb2d444d2005-11-08 21:39:31 -0800933 if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 mddev->degraded++;
935 conf->working_disks--;
936 /*
937 * if recovery is running, make sure it aborts.
938 */
939 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
940 }
NeilBrownb2d444d2005-11-08 21:39:31 -0800941 clear_bit(In_sync, &rdev->flags);
942 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 mddev->sb_dirty = 1;
944 printk(KERN_ALERT "raid1: Disk failure on %s, disabling device. \n"
945 " Operation continuing on %d devices\n",
946 bdevname(rdev->bdev,b), conf->working_disks);
947}
948
949static void print_conf(conf_t *conf)
950{
951 int i;
952 mirror_info_t *tmp;
953
954 printk("RAID1 conf printout:\n");
955 if (!conf) {
956 printk("(!conf)\n");
957 return;
958 }
959 printk(" --- wd:%d rd:%d\n", conf->working_disks,
960 conf->raid_disks);
961
962 for (i = 0; i < conf->raid_disks; i++) {
963 char b[BDEVNAME_SIZE];
964 tmp = conf->mirrors + i;
965 if (tmp->rdev)
966 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -0800967 i, !test_bit(In_sync, &tmp->rdev->flags), !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 bdevname(tmp->rdev->bdev,b));
969 }
970}
971
972static void close_sync(conf_t *conf)
973{
NeilBrown17999be2006-01-06 00:20:12 -0800974 wait_barrier(conf);
975 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 mempool_destroy(conf->r1buf_pool);
978 conf->r1buf_pool = NULL;
979}
980
981static int raid1_spare_active(mddev_t *mddev)
982{
983 int i;
984 conf_t *conf = mddev->private;
985 mirror_info_t *tmp;
986
987 /*
988 * Find all failed disks within the RAID1 configuration
989 * and mark them readable
990 */
991 for (i = 0; i < conf->raid_disks; i++) {
992 tmp = conf->mirrors + i;
993 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -0800994 && !test_bit(Faulty, &tmp->rdev->flags)
995 && !test_bit(In_sync, &tmp->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 conf->working_disks++;
997 mddev->degraded--;
NeilBrownb2d444d2005-11-08 21:39:31 -0800998 set_bit(In_sync, &tmp->rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000 }
1001
1002 print_conf(conf);
1003 return 0;
1004}
1005
1006
1007static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1008{
1009 conf_t *conf = mddev->private;
1010 int found = 0;
NeilBrown41158c72005-06-21 17:17:25 -07001011 int mirror = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 mirror_info_t *p;
1013
1014 for (mirror=0; mirror < mddev->raid_disks; mirror++)
1015 if ( !(p=conf->mirrors+mirror)->rdev) {
1016
1017 blk_queue_stack_limits(mddev->queue,
1018 rdev->bdev->bd_disk->queue);
1019 /* as we don't honour merge_bvec_fn, we must never risk
1020 * violating it, so limit ->max_sector to one PAGE, as
1021 * a one page request is never in violation.
1022 */
1023 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1024 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1025 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
1026
1027 p->head_position = 0;
1028 rdev->raid_disk = mirror;
1029 found = 1;
NeilBrown6aea114a2005-11-28 13:44:13 -08001030 /* As all devices are equivalent, we don't need a full recovery
1031 * if this was recently any drive of the array
1032 */
1033 if (rdev->saved_raid_disk < 0)
NeilBrown41158c72005-06-21 17:17:25 -07001034 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001035 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 break;
1037 }
1038
1039 print_conf(conf);
1040 return found;
1041}
1042
1043static int raid1_remove_disk(mddev_t *mddev, int number)
1044{
1045 conf_t *conf = mddev->private;
1046 int err = 0;
1047 mdk_rdev_t *rdev;
1048 mirror_info_t *p = conf->mirrors+ number;
1049
1050 print_conf(conf);
1051 rdev = p->rdev;
1052 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001053 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 atomic_read(&rdev->nr_pending)) {
1055 err = -EBUSY;
1056 goto abort;
1057 }
1058 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001059 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if (atomic_read(&rdev->nr_pending)) {
1061 /* lost the race, try later */
1062 err = -EBUSY;
1063 p->rdev = rdev;
1064 }
1065 }
1066abort:
1067
1068 print_conf(conf);
1069 return err;
1070}
1071
1072
1073static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
1074{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 if (bio->bi_size)
1078 return 1;
1079
1080 if (r1_bio->bios[r1_bio->read_disk] != bio)
1081 BUG();
1082 update_head_pos(r1_bio->read_disk, r1_bio);
1083 /*
1084 * we have read a block, now it needs to be re-written,
1085 * or re-read if the read failed.
1086 * We don't do much here, just schedule handling by raid1d
1087 */
NeilBrown69382e82006-01-06 00:20:22 -08001088 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 set_bit(R1BIO_Uptodate, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 reschedule_retry(r1_bio);
1091 return 0;
1092}
1093
1094static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
1095{
1096 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1097 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
1098 mddev_t *mddev = r1_bio->mddev;
1099 conf_t *conf = mddev_to_conf(mddev);
1100 int i;
1101 int mirror=0;
1102
1103 if (bio->bi_size)
1104 return 1;
1105
1106 for (i = 0; i < conf->raid_disks; i++)
1107 if (r1_bio->bios[i] == bio) {
1108 mirror = i;
1109 break;
1110 }
NeilBrowne3b97032005-08-04 12:53:34 -07001111 if (!uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 md_error(mddev, conf->mirrors[mirror].rdev);
NeilBrowne3b97032005-08-04 12:53:34 -07001113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 update_head_pos(mirror, r1_bio);
1115
1116 if (atomic_dec_and_test(&r1_bio->remaining)) {
1117 md_done_sync(mddev, r1_bio->sectors, uptodate);
1118 put_buf(r1_bio);
1119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 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
NeilBrown69382e82006-01-06 00:20:22 -08001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 /*
1134 * schedule writes
1135 */
1136 if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
NeilBrown69382e82006-01-06 00:20:22 -08001137 /* ouch - failed to read all of that.
1138 * Try some synchronous reads of other devices to get
1139 * good data, much like with normal read errors. Only
1140 * read into the pages we already have so they we don't
1141 * need to re-issue the read request.
1142 * We don't need to freeze the array, because being in an
1143 * active sync request, there is no normal IO, and
1144 * no overlapping syncs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 */
NeilBrown69382e82006-01-06 00:20:22 -08001146 sector_t sect = r1_bio->sector;
1147 int sectors = r1_bio->sectors;
1148 int idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
NeilBrown69382e82006-01-06 00:20:22 -08001150 while(sectors) {
1151 int s = sectors;
1152 int d = r1_bio->read_disk;
1153 int success = 0;
1154 mdk_rdev_t *rdev;
1155
1156 if (s > (PAGE_SIZE>>9))
1157 s = PAGE_SIZE >> 9;
1158 do {
1159 if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
1160 rdev = conf->mirrors[d].rdev;
1161 if (sync_page_io(rdev->bdev,
1162 sect + rdev->data_offset,
1163 s<<9,
1164 bio->bi_io_vec[idx].bv_page,
1165 READ)) {
1166 success = 1;
1167 break;
1168 }
1169 }
1170 d++;
1171 if (d == conf->raid_disks)
1172 d = 0;
1173 } while (!success && d != r1_bio->read_disk);
1174
1175 if (success) {
1176 /* write it back and re-read */
1177 set_bit(R1BIO_Uptodate, &r1_bio->state);
1178 while (d != r1_bio->read_disk) {
1179 if (d == 0)
1180 d = conf->raid_disks;
1181 d--;
1182 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1183 continue;
1184 rdev = conf->mirrors[d].rdev;
1185 if (sync_page_io(rdev->bdev,
1186 sect + rdev->data_offset,
1187 s<<9,
1188 bio->bi_io_vec[idx].bv_page,
1189 WRITE) == 0 ||
1190 sync_page_io(rdev->bdev,
1191 sect + rdev->data_offset,
1192 s<<9,
1193 bio->bi_io_vec[idx].bv_page,
1194 READ) == 0) {
1195 md_error(mddev, rdev);
1196 }
1197 }
1198 } else {
1199 char b[BDEVNAME_SIZE];
1200 /* Cannot read from anywhere, array is toast */
1201 md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
1202 printk(KERN_ALERT "raid1: %s: unrecoverable I/O read error"
1203 " for block %llu\n",
1204 bdevname(bio->bi_bdev,b),
1205 (unsigned long long)r1_bio->sector);
1206 md_done_sync(mddev, r1_bio->sectors, 0);
1207 put_buf(r1_bio);
1208 return;
1209 }
1210 sectors -= s;
1211 sect += s;
1212 idx ++;
1213 }
1214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 atomic_set(&r1_bio->remaining, 1);
1216 for (i = 0; i < disks ; i++) {
1217 wbio = r1_bio->bios[i];
NeilBrown3e198f72006-01-06 00:20:21 -08001218 if (wbio->bi_end_io == NULL ||
1219 (wbio->bi_end_io == end_sync_read &&
1220 (i == r1_bio->read_disk ||
1221 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 continue;
1223
NeilBrown3e198f72006-01-06 00:20:21 -08001224 wbio->bi_rw = WRITE;
1225 wbio->bi_end_io = end_sync_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 atomic_inc(&r1_bio->remaining);
1227 md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
NeilBrown191ea9b2005-06-21 17:17:23 -07001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 generic_make_request(wbio);
1230 }
1231
1232 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001233 /* if we're here, all write(s) have completed, so clean up */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 md_done_sync(mddev, r1_bio->sectors, 1);
1235 put_buf(r1_bio);
1236 }
1237}
1238
1239/*
1240 * This is a kernel thread which:
1241 *
1242 * 1. Retries failed read operations on working mirrors.
1243 * 2. Updates the raid superblock when problems encounter.
1244 * 3. Performs writes following reads for array syncronising.
1245 */
1246
1247static void raid1d(mddev_t *mddev)
1248{
1249 r1bio_t *r1_bio;
1250 struct bio *bio;
1251 unsigned long flags;
1252 conf_t *conf = mddev_to_conf(mddev);
1253 struct list_head *head = &conf->retry_list;
1254 int unplug=0;
1255 mdk_rdev_t *rdev;
1256
1257 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 for (;;) {
1260 char b[BDEVNAME_SIZE];
1261 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown191ea9b2005-06-21 17:17:23 -07001262
1263 if (conf->pending_bio_list.head) {
1264 bio = bio_list_get(&conf->pending_bio_list);
1265 blk_remove_plug(mddev->queue);
1266 spin_unlock_irqrestore(&conf->device_lock, flags);
1267 /* flush any pending bitmap writes to disk before proceeding w/ I/O */
1268 if (bitmap_unplug(mddev->bitmap) != 0)
1269 printk("%s: bitmap file write failed!\n", mdname(mddev));
1270
1271 while (bio) { /* submit pending writes */
1272 struct bio *next = bio->bi_next;
1273 bio->bi_next = NULL;
1274 generic_make_request(bio);
1275 bio = next;
1276 }
1277 unplug = 1;
1278
1279 continue;
1280 }
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (list_empty(head))
1283 break;
1284 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
1285 list_del(head->prev);
NeilBrownddaf22a2006-01-06 00:20:19 -08001286 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 spin_unlock_irqrestore(&conf->device_lock, flags);
1288
1289 mddev = r1_bio->mddev;
1290 conf = mddev_to_conf(mddev);
1291 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
1292 sync_request_write(mddev, r1_bio);
1293 unplug = 1;
NeilBrowna9701a32005-11-08 21:39:34 -08001294 } else if (test_bit(R1BIO_BarrierRetry, &r1_bio->state)) {
1295 /* some requests in the r1bio were BIO_RW_BARRIER
1296 * requests which failed with -ENOTSUPP. Hohumm..
1297 * Better resubmit without the barrier.
1298 * We know which devices to resubmit for, because
1299 * all others have had their bios[] entry cleared.
1300 */
1301 int i;
1302 clear_bit(R1BIO_BarrierRetry, &r1_bio->state);
1303 clear_bit(R1BIO_Barrier, &r1_bio->state);
1304 for (i=0; i < conf->raid_disks; i++)
1305 if (r1_bio->bios[i]) {
1306 struct bio_vec *bvec;
1307 int j;
1308
1309 bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
1310 /* copy pages from the failed bio, as
1311 * this might be a write-behind device */
1312 __bio_for_each_segment(bvec, bio, j, 0)
1313 bvec->bv_page = bio_iovec_idx(r1_bio->bios[i], j)->bv_page;
1314 bio_put(r1_bio->bios[i]);
1315 bio->bi_sector = r1_bio->sector +
1316 conf->mirrors[i].rdev->data_offset;
1317 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1318 bio->bi_end_io = raid1_end_write_request;
1319 bio->bi_rw = WRITE;
1320 bio->bi_private = r1_bio;
1321 r1_bio->bios[i] = bio;
1322 generic_make_request(bio);
1323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 } else {
1325 int disk;
NeilBrownddaf22a2006-01-06 00:20:19 -08001326
1327 /* we got a read error. Maybe the drive is bad. Maybe just
1328 * the block and we can fix it.
1329 * We freeze all other IO, and try reading the block from
1330 * other devices. When we find one, we re-write
1331 * and check it that fixes the read error.
1332 * This is all done synchronously while the array is
1333 * frozen
1334 */
1335 sector_t sect = r1_bio->sector;
1336 int sectors = r1_bio->sectors;
1337 freeze_array(conf);
1338 while(sectors) {
1339 int s = sectors;
1340 int d = r1_bio->read_disk;
1341 int success = 0;
1342
1343 if (s > (PAGE_SIZE>>9))
1344 s = PAGE_SIZE >> 9;
1345
1346 do {
1347 rdev = conf->mirrors[d].rdev;
1348 if (rdev &&
1349 test_bit(In_sync, &rdev->flags) &&
1350 sync_page_io(rdev->bdev,
1351 sect + rdev->data_offset,
1352 s<<9,
1353 conf->tmppage, READ))
1354 success = 1;
1355 else {
1356 d++;
1357 if (d == conf->raid_disks)
1358 d = 0;
1359 }
1360 } while (!success && d != r1_bio->read_disk);
1361
1362 if (success) {
1363 /* write it back and re-read */
1364 while (d != r1_bio->read_disk) {
1365 if (d==0)
1366 d = conf->raid_disks;
1367 d--;
1368 rdev = conf->mirrors[d].rdev;
1369 if (rdev &&
1370 test_bit(In_sync, &rdev->flags)) {
1371 if (sync_page_io(rdev->bdev,
1372 sect + rdev->data_offset,
1373 s<<9, conf->tmppage, WRITE) == 0 ||
1374 sync_page_io(rdev->bdev,
1375 sect + rdev->data_offset,
1376 s<<9, conf->tmppage, READ) == 0) {
1377 /* Well, this device is dead */
1378 md_error(mddev, rdev);
1379 }
1380 }
1381 }
1382 } else {
1383 /* Cannot read from anywhere -- bye bye array */
1384 md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
1385 break;
1386 }
1387 sectors -= s;
1388 sect += s;
1389 }
1390
1391
1392 unfreeze_array(conf);
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 bio = r1_bio->bios[r1_bio->read_disk];
1395 if ((disk=read_balance(conf, r1_bio)) == -1) {
1396 printk(KERN_ALERT "raid1: %s: unrecoverable I/O"
1397 " read error for block %llu\n",
1398 bdevname(bio->bi_bdev,b),
1399 (unsigned long long)r1_bio->sector);
1400 raid_end_bio_io(r1_bio);
1401 } else {
1402 r1_bio->bios[r1_bio->read_disk] = NULL;
1403 r1_bio->read_disk = disk;
1404 bio_put(bio);
1405 bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
1406 r1_bio->bios[r1_bio->read_disk] = bio;
1407 rdev = conf->mirrors[disk].rdev;
1408 if (printk_ratelimit())
1409 printk(KERN_ERR "raid1: %s: redirecting sector %llu to"
1410 " another mirror\n",
1411 bdevname(rdev->bdev,b),
1412 (unsigned long long)r1_bio->sector);
1413 bio->bi_sector = r1_bio->sector + rdev->data_offset;
1414 bio->bi_bdev = rdev->bdev;
1415 bio->bi_end_io = raid1_end_read_request;
1416 bio->bi_rw = READ;
1417 bio->bi_private = r1_bio;
1418 unplug = 1;
1419 generic_make_request(bio);
1420 }
1421 }
1422 }
1423 spin_unlock_irqrestore(&conf->device_lock, flags);
1424 if (unplug)
1425 unplug_slaves(mddev);
1426}
1427
1428
1429static int init_resync(conf_t *conf)
1430{
1431 int buffs;
1432
1433 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
1434 if (conf->r1buf_pool)
1435 BUG();
1436 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
1437 conf->poolinfo);
1438 if (!conf->r1buf_pool)
1439 return -ENOMEM;
1440 conf->next_resync = 0;
1441 return 0;
1442}
1443
1444/*
1445 * perform a "sync" on one "block"
1446 *
1447 * We need to make sure that no normal I/O request - particularly write
1448 * requests - conflict with active sync requests.
1449 *
1450 * This is achieved by tracking pending requests and a 'barrier' concept
1451 * that can be installed to exclude normal IO requests.
1452 */
1453
NeilBrown57afd892005-06-21 17:17:13 -07001454static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
1456 conf_t *conf = mddev_to_conf(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 r1bio_t *r1_bio;
1458 struct bio *bio;
1459 sector_t max_sector, nr_sectors;
NeilBrown3e198f72006-01-06 00:20:21 -08001460 int disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 int i;
NeilBrown3e198f72006-01-06 00:20:21 -08001462 int wonly = -1;
1463 int write_targets = 0, read_targets = 0;
NeilBrown191ea9b2005-06-21 17:17:23 -07001464 int sync_blocks;
NeilBrowne3b97032005-08-04 12:53:34 -07001465 int still_degraded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 if (!conf->r1buf_pool)
NeilBrown191ea9b2005-06-21 17:17:23 -07001468 {
1469/*
1470 printk("sync start - bitmap %p\n", mddev->bitmap);
1471*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07001473 return 0;
NeilBrown191ea9b2005-06-21 17:17:23 -07001474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 max_sector = mddev->size << 1;
1477 if (sector_nr >= max_sector) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001478 /* If we aborted, we need to abort the
1479 * sync on the 'current' bitmap chunk (there will
1480 * only be one in raid1 resync.
1481 * We can find the current addess in mddev->curr_resync
1482 */
NeilBrown6a806c52005-07-15 03:56:35 -07001483 if (mddev->curr_resync < max_sector) /* aborted */
1484 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
NeilBrown191ea9b2005-06-21 17:17:23 -07001485 &sync_blocks, 1);
NeilBrown6a806c52005-07-15 03:56:35 -07001486 else /* completed sync */
NeilBrown191ea9b2005-06-21 17:17:23 -07001487 conf->fullsync = 0;
NeilBrown6a806c52005-07-15 03:56:35 -07001488
1489 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 close_sync(conf);
1491 return 0;
1492 }
1493
NeilBrowne3b97032005-08-04 12:53:34 -07001494 /* before building a request, check if we can skip these blocks..
1495 * This call the bitmap_start_sync doesn't actually record anything
1496 */
1497 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrowne5de485f2005-11-08 21:39:38 -08001498 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001499 /* We can skip this block, and probably several more */
1500 *skipped = 1;
1501 return sync_blocks;
1502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 /*
NeilBrown17999be2006-01-06 00:20:12 -08001504 * If there is non-resync activity waiting for a turn,
1505 * and resync is going fast enough,
1506 * then let it though before starting on this new sync request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 */
NeilBrown17999be2006-01-06 00:20:12 -08001508 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 msleep_interruptible(1000);
NeilBrown17999be2006-01-06 00:20:12 -08001510
1511 raise_barrier(conf);
1512
1513 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
NeilBrown3e198f72006-01-06 00:20:21 -08001516 rcu_read_lock();
1517 /*
1518 * If we get a correctably read error during resync or recovery,
1519 * we might want to read from a different device. So we
1520 * flag all drives that could conceivably be read from for READ,
1521 * and any others (which will be non-In_sync devices) for WRITE.
1522 * If a read fails, we try reading from something else for which READ
1523 * is OK.
1524 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 r1_bio->mddev = mddev;
1527 r1_bio->sector = sector_nr;
NeilBrown191ea9b2005-06-21 17:17:23 -07001528 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 set_bit(R1BIO_IsSync, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 for (i=0; i < conf->raid_disks; i++) {
NeilBrown3e198f72006-01-06 00:20:21 -08001532 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 bio = r1_bio->bios[i];
1534
1535 /* take from bio_init */
1536 bio->bi_next = NULL;
1537 bio->bi_flags |= 1 << BIO_UPTODATE;
1538 bio->bi_rw = 0;
1539 bio->bi_vcnt = 0;
1540 bio->bi_idx = 0;
1541 bio->bi_phys_segments = 0;
1542 bio->bi_hw_segments = 0;
1543 bio->bi_size = 0;
1544 bio->bi_end_io = NULL;
1545 bio->bi_private = NULL;
1546
NeilBrown3e198f72006-01-06 00:20:21 -08001547 rdev = rcu_dereference(conf->mirrors[i].rdev);
1548 if (rdev == NULL ||
1549 test_bit(Faulty, &rdev->flags)) {
NeilBrowne3b97032005-08-04 12:53:34 -07001550 still_degraded = 1;
1551 continue;
NeilBrown3e198f72006-01-06 00:20:21 -08001552 } else if (!test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 bio->bi_rw = WRITE;
1554 bio->bi_end_io = end_sync_write;
1555 write_targets ++;
NeilBrown3e198f72006-01-06 00:20:21 -08001556 } else {
1557 /* may need to read from here */
1558 bio->bi_rw = READ;
1559 bio->bi_end_io = end_sync_read;
1560 if (test_bit(WriteMostly, &rdev->flags)) {
1561 if (wonly < 0)
1562 wonly = i;
1563 } else {
1564 if (disk < 0)
1565 disk = i;
1566 }
1567 read_targets++;
1568 }
1569 atomic_inc(&rdev->nr_pending);
1570 bio->bi_sector = sector_nr + rdev->data_offset;
1571 bio->bi_bdev = rdev->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 bio->bi_private = r1_bio;
1573 }
NeilBrown3e198f72006-01-06 00:20:21 -08001574 rcu_read_unlock();
1575 if (disk < 0)
1576 disk = wonly;
1577 r1_bio->read_disk = disk;
NeilBrown191ea9b2005-06-21 17:17:23 -07001578
NeilBrown3e198f72006-01-06 00:20:21 -08001579 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
1580 /* extra read targets are also write targets */
1581 write_targets += read_targets-1;
1582
1583 if (write_targets == 0 || read_targets == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 /* There is nowhere to write, so all non-sync
1585 * drives must be failed - so we are finished
1586 */
NeilBrown57afd892005-06-21 17:17:13 -07001587 sector_t rv = max_sector - sector_nr;
1588 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 put_buf(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 return rv;
1591 }
1592
1593 nr_sectors = 0;
NeilBrown289e99e2005-06-21 17:17:24 -07001594 sync_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 do {
1596 struct page *page;
1597 int len = PAGE_SIZE;
1598 if (sector_nr + (len>>9) > max_sector)
1599 len = (max_sector - sector_nr) << 9;
1600 if (len == 0)
1601 break;
NeilBrown6a806c52005-07-15 03:56:35 -07001602 if (sync_blocks == 0) {
1603 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
NeilBrowne5de485f2005-11-08 21:39:38 -08001604 &sync_blocks, still_degraded) &&
1605 !conf->fullsync &&
1606 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
NeilBrown6a806c52005-07-15 03:56:35 -07001607 break;
1608 if (sync_blocks < (PAGE_SIZE>>9))
1609 BUG();
1610 if (len > (sync_blocks<<9))
1611 len = sync_blocks<<9;
NeilBrownab7a30c2005-06-21 17:17:23 -07001612 }
NeilBrown191ea9b2005-06-21 17:17:23 -07001613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 for (i=0 ; i < conf->raid_disks; i++) {
1615 bio = r1_bio->bios[i];
1616 if (bio->bi_end_io) {
1617 page = r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page;
1618 if (bio_add_page(bio, page, len, 0) == 0) {
1619 /* stop here */
1620 r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page = page;
1621 while (i > 0) {
1622 i--;
1623 bio = r1_bio->bios[i];
NeilBrown6a806c52005-07-15 03:56:35 -07001624 if (bio->bi_end_io==NULL)
1625 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 /* remove last page from this bio */
1627 bio->bi_vcnt--;
1628 bio->bi_size -= len;
1629 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
1630 }
1631 goto bio_full;
1632 }
1633 }
1634 }
1635 nr_sectors += len>>9;
1636 sector_nr += len>>9;
NeilBrown191ea9b2005-06-21 17:17:23 -07001637 sync_blocks -= (len>>9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
1639 bio_full:
NeilBrown3e198f72006-01-06 00:20:21 -08001640 bio = r1_bio->bios[r1_bio->read_disk];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 r1_bio->sectors = nr_sectors;
1642
NeilBrown3e198f72006-01-06 00:20:21 -08001643 md_sync_acct(conf->mirrors[r1_bio->read_disk].rdev->bdev, nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 generic_make_request(bio);
1646
1647 return nr_sectors;
1648}
1649
1650static int run(mddev_t *mddev)
1651{
1652 conf_t *conf;
1653 int i, j, disk_idx;
1654 mirror_info_t *disk;
1655 mdk_rdev_t *rdev;
1656 struct list_head *tmp;
1657
1658 if (mddev->level != 1) {
1659 printk("raid1: %s: raid level not set to mirroring (%d)\n",
1660 mdname(mddev), mddev->level);
1661 goto out;
1662 }
1663 /*
1664 * copy the already verified devices into our private RAID1
1665 * bookkeeping area. [whatever we allocate in run(),
1666 * should be freed in stop()]
1667 */
1668 conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
1669 mddev->private = conf;
1670 if (!conf)
1671 goto out_no_mem;
1672
1673 memset(conf, 0, sizeof(*conf));
1674 conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks,
1675 GFP_KERNEL);
1676 if (!conf->mirrors)
1677 goto out_no_mem;
1678
1679 memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
1680
NeilBrownddaf22a2006-01-06 00:20:19 -08001681 conf->tmppage = alloc_page(GFP_KERNEL);
1682 if (!conf->tmppage)
1683 goto out_no_mem;
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 conf->poolinfo = kmalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
1686 if (!conf->poolinfo)
1687 goto out_no_mem;
1688 conf->poolinfo->mddev = mddev;
1689 conf->poolinfo->raid_disks = mddev->raid_disks;
1690 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1691 r1bio_pool_free,
1692 conf->poolinfo);
1693 if (!conf->r1bio_pool)
1694 goto out_no_mem;
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 ITERATE_RDEV(mddev, rdev, tmp) {
1697 disk_idx = rdev->raid_disk;
1698 if (disk_idx >= mddev->raid_disks
1699 || disk_idx < 0)
1700 continue;
1701 disk = conf->mirrors + disk_idx;
1702
1703 disk->rdev = rdev;
1704
1705 blk_queue_stack_limits(mddev->queue,
1706 rdev->bdev->bd_disk->queue);
1707 /* as we don't honour merge_bvec_fn, we must never risk
1708 * violating it, so limit ->max_sector to one PAGE, as
1709 * a one page request is never in violation.
1710 */
1711 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1712 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1713 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
1714
1715 disk->head_position = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08001716 if (!test_bit(Faulty, &rdev->flags) && test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 conf->working_disks++;
1718 }
1719 conf->raid_disks = mddev->raid_disks;
1720 conf->mddev = mddev;
1721 spin_lock_init(&conf->device_lock);
1722 INIT_LIST_HEAD(&conf->retry_list);
1723 if (conf->working_disks == 1)
1724 mddev->recovery_cp = MaxSector;
1725
1726 spin_lock_init(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -08001727 init_waitqueue_head(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
NeilBrown191ea9b2005-06-21 17:17:23 -07001729 bio_list_init(&conf->pending_bio_list);
1730 bio_list_init(&conf->flushing_bio_list);
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 if (!conf->working_disks) {
1733 printk(KERN_ERR "raid1: no operational mirrors for %s\n",
1734 mdname(mddev));
1735 goto out_free_conf;
1736 }
1737
1738 mddev->degraded = 0;
1739 for (i = 0; i < conf->raid_disks; i++) {
1740
1741 disk = conf->mirrors + i;
1742
1743 if (!disk->rdev) {
1744 disk->head_position = 0;
1745 mddev->degraded++;
1746 }
1747 }
1748
1749 /*
1750 * find the first working one and use it as a starting point
1751 * to read balancing.
1752 */
1753 for (j = 0; j < conf->raid_disks &&
1754 (!conf->mirrors[j].rdev ||
NeilBrownb2d444d2005-11-08 21:39:31 -08001755 !test_bit(In_sync, &conf->mirrors[j].rdev->flags)) ; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 /* nothing */;
1757 conf->last_used = j;
1758
1759
NeilBrown191ea9b2005-06-21 17:17:23 -07001760 mddev->thread = md_register_thread(raid1d, mddev, "%s_raid1");
1761 if (!mddev->thread) {
1762 printk(KERN_ERR
1763 "raid1: couldn't allocate thread for %s\n",
1764 mdname(mddev));
1765 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 }
NeilBrown191ea9b2005-06-21 17:17:23 -07001767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 printk(KERN_INFO
1769 "raid1: raid set %s active with %d out of %d mirrors\n",
1770 mdname(mddev), mddev->raid_disks - mddev->degraded,
1771 mddev->raid_disks);
1772 /*
1773 * Ok, everything is just fine now
1774 */
1775 mddev->array_size = mddev->size;
1776
NeilBrown7a5febe2005-05-16 21:53:16 -07001777 mddev->queue->unplug_fn = raid1_unplug;
1778 mddev->queue->issue_flush_fn = raid1_issue_flush;
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return 0;
1781
1782out_no_mem:
1783 printk(KERN_ERR "raid1: couldn't allocate memory for %s\n",
1784 mdname(mddev));
1785
1786out_free_conf:
1787 if (conf) {
1788 if (conf->r1bio_pool)
1789 mempool_destroy(conf->r1bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07001790 kfree(conf->mirrors);
NeilBrownddaf22a2006-01-06 00:20:19 -08001791 __free_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07001792 kfree(conf->poolinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 kfree(conf);
1794 mddev->private = NULL;
1795 }
1796out:
1797 return -EIO;
1798}
1799
1800static int stop(mddev_t *mddev)
1801{
1802 conf_t *conf = mddev_to_conf(mddev);
NeilBrown4b6d2872005-09-09 16:23:47 -07001803 struct bitmap *bitmap = mddev->bitmap;
1804 int behind_wait = 0;
1805
1806 /* wait for behind writes to complete */
1807 while (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
1808 behind_wait++;
1809 printk(KERN_INFO "raid1: behind writes in progress on device %s, waiting to stop (%d)\n", mdname(mddev), behind_wait);
1810 set_current_state(TASK_UNINTERRUPTIBLE);
1811 schedule_timeout(HZ); /* wait a second */
1812 /* need to kick something here to make sure I/O goes? */
1813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815 md_unregister_thread(mddev->thread);
1816 mddev->thread = NULL;
1817 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
1818 if (conf->r1bio_pool)
1819 mempool_destroy(conf->r1bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07001820 kfree(conf->mirrors);
1821 kfree(conf->poolinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 kfree(conf);
1823 mddev->private = NULL;
1824 return 0;
1825}
1826
1827static int raid1_resize(mddev_t *mddev, sector_t sectors)
1828{
1829 /* no resync is happening, and there is enough space
1830 * on all devices, so we can resize.
1831 * We need to make sure resync covers any new space.
1832 * If the array is shrinking we should possibly wait until
1833 * any io in the removed space completes, but it hardly seems
1834 * worth it.
1835 */
1836 mddev->array_size = sectors>>1;
1837 set_capacity(mddev->gendisk, mddev->array_size << 1);
1838 mddev->changed = 1;
1839 if (mddev->array_size > mddev->size && mddev->recovery_cp == MaxSector) {
1840 mddev->recovery_cp = mddev->size << 1;
1841 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1842 }
1843 mddev->size = mddev->array_size;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07001844 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 return 0;
1846}
1847
1848static int raid1_reshape(mddev_t *mddev, int raid_disks)
1849{
1850 /* We need to:
1851 * 1/ resize the r1bio_pool
1852 * 2/ resize conf->mirrors
1853 *
1854 * We allocate a new r1bio_pool if we can.
1855 * Then raise a device barrier and wait until all IO stops.
1856 * Then resize conf->mirrors and swap in the new r1bio pool.
NeilBrown6ea9c072005-06-21 17:17:09 -07001857 *
1858 * At the same time, we "pack" the devices so that all the missing
1859 * devices have the higher raid_disk numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 */
1861 mempool_t *newpool, *oldpool;
1862 struct pool_info *newpoolinfo;
1863 mirror_info_t *newmirrors;
1864 conf_t *conf = mddev_to_conf(mddev);
NeilBrown6ea9c072005-06-21 17:17:09 -07001865 int cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
NeilBrown6ea9c072005-06-21 17:17:09 -07001867 int d, d2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
NeilBrown6ea9c072005-06-21 17:17:09 -07001869 if (raid_disks < conf->raid_disks) {
1870 cnt=0;
1871 for (d= 0; d < conf->raid_disks; d++)
1872 if (conf->mirrors[d].rdev)
1873 cnt++;
1874 if (cnt > raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 return -EBUSY;
NeilBrown6ea9c072005-06-21 17:17:09 -07001876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
1878 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
1879 if (!newpoolinfo)
1880 return -ENOMEM;
1881 newpoolinfo->mddev = mddev;
1882 newpoolinfo->raid_disks = raid_disks;
1883
1884 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1885 r1bio_pool_free, newpoolinfo);
1886 if (!newpool) {
1887 kfree(newpoolinfo);
1888 return -ENOMEM;
1889 }
1890 newmirrors = kmalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
1891 if (!newmirrors) {
1892 kfree(newpoolinfo);
1893 mempool_destroy(newpool);
1894 return -ENOMEM;
1895 }
1896 memset(newmirrors, 0, sizeof(struct mirror_info)*raid_disks);
1897
NeilBrown17999be2006-01-06 00:20:12 -08001898 raise_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 /* ok, everything is stopped */
1901 oldpool = conf->r1bio_pool;
1902 conf->r1bio_pool = newpool;
NeilBrown6ea9c072005-06-21 17:17:09 -07001903
1904 for (d=d2=0; d < conf->raid_disks; d++)
1905 if (conf->mirrors[d].rdev) {
1906 conf->mirrors[d].rdev->raid_disk = d2;
1907 newmirrors[d2++].rdev = conf->mirrors[d].rdev;
1908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 kfree(conf->mirrors);
1910 conf->mirrors = newmirrors;
1911 kfree(conf->poolinfo);
1912 conf->poolinfo = newpoolinfo;
1913
1914 mddev->degraded += (raid_disks - conf->raid_disks);
1915 conf->raid_disks = mddev->raid_disks = raid_disks;
1916
NeilBrown6ea9c072005-06-21 17:17:09 -07001917 conf->last_used = 0; /* just make sure it is in-range */
NeilBrown17999be2006-01-06 00:20:12 -08001918 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1921 md_wakeup_thread(mddev->thread);
1922
1923 mempool_destroy(oldpool);
1924 return 0;
1925}
1926
NeilBrown500af872005-09-09 16:23:58 -07001927static void raid1_quiesce(mddev_t *mddev, int state)
NeilBrown36fa3062005-09-09 16:23:45 -07001928{
1929 conf_t *conf = mddev_to_conf(mddev);
1930
1931 switch(state) {
NeilBrown9e6603d2005-09-09 16:23:48 -07001932 case 1:
NeilBrown17999be2006-01-06 00:20:12 -08001933 raise_barrier(conf);
NeilBrown36fa3062005-09-09 16:23:45 -07001934 break;
NeilBrown9e6603d2005-09-09 16:23:48 -07001935 case 0:
NeilBrown17999be2006-01-06 00:20:12 -08001936 lower_barrier(conf);
NeilBrown36fa3062005-09-09 16:23:45 -07001937 break;
1938 }
NeilBrown36fa3062005-09-09 16:23:45 -07001939}
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942static mdk_personality_t raid1_personality =
1943{
1944 .name = "raid1",
1945 .owner = THIS_MODULE,
1946 .make_request = make_request,
1947 .run = run,
1948 .stop = stop,
1949 .status = status,
1950 .error_handler = error,
1951 .hot_add_disk = raid1_add_disk,
1952 .hot_remove_disk= raid1_remove_disk,
1953 .spare_active = raid1_spare_active,
1954 .sync_request = sync_request,
1955 .resize = raid1_resize,
1956 .reshape = raid1_reshape,
NeilBrown36fa3062005-09-09 16:23:45 -07001957 .quiesce = raid1_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958};
1959
1960static int __init raid_init(void)
1961{
1962 return register_md_personality(RAID1, &raid1_personality);
1963}
1964
1965static void raid_exit(void)
1966{
1967 unregister_md_personality(RAID1);
1968}
1969
1970module_init(raid_init);
1971module_exit(raid_exit);
1972MODULE_LICENSE("GPL");
1973MODULE_ALIAS("md-personality-3"); /* RAID1 */