blob: a8bc93d6ff63fcd578dee710c3513342efc0fe2a [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;
NeilBrowncf30a472006-01-06 00:20:23 -0800157 if (*bio && *bio != IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 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);
NeilBrowncf30a472006-01-06 00:20:23 -0800422 r1_bio->bios[new_disk] == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800423 !rdev || !test_bit(In_sync, &rdev->flags)
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700424 || test_bit(WriteMostly, &rdev->flags);
Suzanne Woodd6065f72005-11-08 21:39:27 -0800425 rdev = rcu_dereference(conf->mirrors[++new_disk].rdev)) {
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700426
NeilBrowncf30a472006-01-06 00:20:23 -0800427 if (rdev && test_bit(In_sync, &rdev->flags) &&
428 r1_bio->bios[new_disk] != IO_BLOCKED)
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700429 wonly_disk = new_disk;
430
431 if (new_disk == conf->raid_disks - 1) {
432 new_disk = wonly_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 break;
434 }
435 }
436 goto rb_out;
437 }
438
439
440 /* make sure the disk is operational */
Suzanne Woodd6065f72005-11-08 21:39:27 -0800441 for (rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
NeilBrowncf30a472006-01-06 00:20:23 -0800442 r1_bio->bios[new_disk] == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800443 !rdev || !test_bit(In_sync, &rdev->flags) ||
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700444 test_bit(WriteMostly, &rdev->flags);
Suzanne Woodd6065f72005-11-08 21:39:27 -0800445 rdev = rcu_dereference(conf->mirrors[new_disk].rdev)) {
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700446
NeilBrowncf30a472006-01-06 00:20:23 -0800447 if (rdev && test_bit(In_sync, &rdev->flags) &&
448 r1_bio->bios[new_disk] != IO_BLOCKED)
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700449 wonly_disk = new_disk;
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (new_disk <= 0)
452 new_disk = conf->raid_disks;
453 new_disk--;
454 if (new_disk == disk) {
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700455 new_disk = wonly_disk;
456 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700459
460 if (new_disk < 0)
461 goto rb_out;
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 disk = new_disk;
464 /* now disk == new_disk == starting point for search */
465
466 /*
467 * Don't change to another disk for sequential reads:
468 */
469 if (conf->next_seq_sect == this_sector)
470 goto rb_out;
471 if (this_sector == conf->mirrors[new_disk].head_position)
472 goto rb_out;
473
474 current_distance = abs(this_sector - conf->mirrors[disk].head_position);
475
476 /* Find the disk whose head is closest */
477
478 do {
479 if (disk <= 0)
480 disk = conf->raid_disks;
481 disk--;
482
Suzanne Woodd6065f72005-11-08 21:39:27 -0800483 rdev = rcu_dereference(conf->mirrors[disk].rdev);
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700484
NeilBrowncf30a472006-01-06 00:20:23 -0800485 if (!rdev || r1_bio->bios[disk] == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800486 !test_bit(In_sync, &rdev->flags) ||
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700487 test_bit(WriteMostly, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 continue;
489
490 if (!atomic_read(&rdev->nr_pending)) {
491 new_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 break;
493 }
494 new_distance = abs(this_sector - conf->mirrors[disk].head_position);
495 if (new_distance < current_distance) {
496 current_distance = new_distance;
497 new_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499 } while (disk != conf->last_used);
500
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700501 rb_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503
504 if (new_disk >= 0) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800505 rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700506 if (!rdev)
507 goto retry;
508 atomic_inc(&rdev->nr_pending);
NeilBrownb2d444d2005-11-08 21:39:31 -0800509 if (!test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /* cannot risk returning a device that failed
511 * before we inc'ed nr_pending
512 */
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700513 atomic_dec(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 goto retry;
515 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700516 conf->next_seq_sect = this_sector + sectors;
517 conf->last_used = new_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519 rcu_read_unlock();
520
521 return new_disk;
522}
523
524static void unplug_slaves(mddev_t *mddev)
525{
526 conf_t *conf = mddev_to_conf(mddev);
527 int i;
528
529 rcu_read_lock();
530 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800531 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800532 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
534
535 atomic_inc(&rdev->nr_pending);
536 rcu_read_unlock();
537
538 if (r_queue->unplug_fn)
539 r_queue->unplug_fn(r_queue);
540
541 rdev_dec_pending(rdev, mddev);
542 rcu_read_lock();
543 }
544 }
545 rcu_read_unlock();
546}
547
548static void raid1_unplug(request_queue_t *q)
549{
NeilBrown191ea9b2005-06-21 17:17:23 -0700550 mddev_t *mddev = q->queuedata;
551
552 unplug_slaves(mddev);
553 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
556static int raid1_issue_flush(request_queue_t *q, struct gendisk *disk,
557 sector_t *error_sector)
558{
559 mddev_t *mddev = q->queuedata;
560 conf_t *conf = mddev_to_conf(mddev);
561 int i, ret = 0;
562
563 rcu_read_lock();
564 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800565 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800566 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 struct block_device *bdev = rdev->bdev;
568 request_queue_t *r_queue = bdev_get_queue(bdev);
569
570 if (!r_queue->issue_flush_fn)
571 ret = -EOPNOTSUPP;
572 else {
573 atomic_inc(&rdev->nr_pending);
574 rcu_read_unlock();
575 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
576 error_sector);
577 rdev_dec_pending(rdev, mddev);
578 rcu_read_lock();
579 }
580 }
581 }
582 rcu_read_unlock();
583 return ret;
584}
585
NeilBrown17999be2006-01-06 00:20:12 -0800586/* Barriers....
587 * Sometimes we need to suspend IO while we do something else,
588 * either some resync/recovery, or reconfigure the array.
589 * To do this we raise a 'barrier'.
590 * The 'barrier' is a counter that can be raised multiple times
591 * to count how many activities are happening which preclude
592 * normal IO.
593 * We can only raise the barrier if there is no pending IO.
594 * i.e. if nr_pending == 0.
595 * We choose only to raise the barrier if no-one is waiting for the
596 * barrier to go down. This means that as soon as an IO request
597 * is ready, no other operations which require a barrier will start
598 * until the IO request has had a chance.
599 *
600 * So: regular IO calls 'wait_barrier'. When that returns there
601 * is no backgroup IO happening, It must arrange to call
602 * allow_barrier when it has finished its IO.
603 * backgroup IO calls must call raise_barrier. Once that returns
604 * there is no normal IO happeing. It must arrange to call
605 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 */
607#define RESYNC_DEPTH 32
608
NeilBrown17999be2006-01-06 00:20:12 -0800609static void raise_barrier(conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 spin_lock_irq(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800612
613 /* Wait until no block IO is waiting */
614 wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
615 conf->resync_lock,
616 raid1_unplug(conf->mddev->queue));
617
618 /* block any new IO from starting */
619 conf->barrier++;
620
621 /* No wait for all pending IO to complete */
622 wait_event_lock_irq(conf->wait_barrier,
623 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
624 conf->resync_lock,
625 raid1_unplug(conf->mddev->queue));
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 spin_unlock_irq(&conf->resync_lock);
628}
629
NeilBrown17999be2006-01-06 00:20:12 -0800630static void lower_barrier(conf_t *conf)
631{
632 unsigned long flags;
633 spin_lock_irqsave(&conf->resync_lock, flags);
634 conf->barrier--;
635 spin_unlock_irqrestore(&conf->resync_lock, flags);
636 wake_up(&conf->wait_barrier);
637}
638
639static void wait_barrier(conf_t *conf)
640{
641 spin_lock_irq(&conf->resync_lock);
642 if (conf->barrier) {
643 conf->nr_waiting++;
644 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
645 conf->resync_lock,
646 raid1_unplug(conf->mddev->queue));
647 conf->nr_waiting--;
648 }
649 conf->nr_pending++;
650 spin_unlock_irq(&conf->resync_lock);
651}
652
653static void allow_barrier(conf_t *conf)
654{
655 unsigned long flags;
656 spin_lock_irqsave(&conf->resync_lock, flags);
657 conf->nr_pending--;
658 spin_unlock_irqrestore(&conf->resync_lock, flags);
659 wake_up(&conf->wait_barrier);
660}
661
NeilBrownddaf22a2006-01-06 00:20:19 -0800662static void freeze_array(conf_t *conf)
663{
664 /* stop syncio and normal IO and wait for everything to
665 * go quite.
666 * We increment barrier and nr_waiting, and then
667 * wait until barrier+nr_pending match nr_queued+2
668 */
669 spin_lock_irq(&conf->resync_lock);
670 conf->barrier++;
671 conf->nr_waiting++;
672 wait_event_lock_irq(conf->wait_barrier,
673 conf->barrier+conf->nr_pending == conf->nr_queued+2,
674 conf->resync_lock,
675 raid1_unplug(conf->mddev->queue));
676 spin_unlock_irq(&conf->resync_lock);
677}
678static void unfreeze_array(conf_t *conf)
679{
680 /* reverse the effect of the freeze */
681 spin_lock_irq(&conf->resync_lock);
682 conf->barrier--;
683 conf->nr_waiting--;
684 wake_up(&conf->wait_barrier);
685 spin_unlock_irq(&conf->resync_lock);
686}
687
NeilBrown17999be2006-01-06 00:20:12 -0800688
NeilBrown4b6d2872005-09-09 16:23:47 -0700689/* duplicate the data pages for behind I/O */
690static struct page **alloc_behind_pages(struct bio *bio)
691{
692 int i;
693 struct bio_vec *bvec;
694 struct page **pages = kmalloc(bio->bi_vcnt * sizeof(struct page *),
695 GFP_NOIO);
696 if (unlikely(!pages))
697 goto do_sync_io;
698
699 memset(pages, 0, bio->bi_vcnt * sizeof(struct page *));
700
701 bio_for_each_segment(bvec, bio, i) {
702 pages[i] = alloc_page(GFP_NOIO);
703 if (unlikely(!pages[i]))
704 goto do_sync_io;
705 memcpy(kmap(pages[i]) + bvec->bv_offset,
706 kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
707 kunmap(pages[i]);
708 kunmap(bvec->bv_page);
709 }
710
711 return pages;
712
713do_sync_io:
714 if (pages)
715 for (i = 0; i < bio->bi_vcnt && pages[i]; i++)
716 __free_page(pages[i]);
717 kfree(pages);
718 PRINTK("%dB behind alloc failed, doing sync I/O\n", bio->bi_size);
719 return NULL;
720}
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722static int make_request(request_queue_t *q, struct bio * bio)
723{
724 mddev_t *mddev = q->queuedata;
725 conf_t *conf = mddev_to_conf(mddev);
726 mirror_info_t *mirror;
727 r1bio_t *r1_bio;
728 struct bio *read_bio;
NeilBrown191ea9b2005-06-21 17:17:23 -0700729 int i, targets = 0, disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 mdk_rdev_t *rdev;
NeilBrown191ea9b2005-06-21 17:17:23 -0700731 struct bitmap *bitmap = mddev->bitmap;
732 unsigned long flags;
733 struct bio_list bl;
NeilBrown4b6d2872005-09-09 16:23:47 -0700734 struct page **behind_pages = NULL;
Jens Axboea3623572005-11-01 09:26:16 +0100735 const int rw = bio_data_dir(bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800736 int do_barriers;
NeilBrown191ea9b2005-06-21 17:17:23 -0700737
NeilBrowna9701a32005-11-08 21:39:34 -0800738 if (unlikely(!mddev->barriers_work && bio_barrier(bio))) {
NeilBrowne5dcdd82005-09-09 16:23:41 -0700739 bio_endio(bio, bio->bi_size, -EOPNOTSUPP);
740 return 0;
741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 /*
744 * Register the new request and wait if the reconstruction
745 * thread has put up a bar for new requests.
746 * Continue immediately if no resync is active currently.
747 */
NeilBrown3d310eb2005-06-21 17:17:26 -0700748 md_write_start(mddev, bio); /* wait on superblock update early */
749
NeilBrown17999be2006-01-06 00:20:12 -0800750 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Jens Axboea3623572005-11-01 09:26:16 +0100752 disk_stat_inc(mddev->gendisk, ios[rw]);
753 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 /*
756 * make_request() can abort the operation when READA is being
757 * used and no empty request is available.
758 *
759 */
760 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
761
762 r1_bio->master_bio = bio;
763 r1_bio->sectors = bio->bi_size >> 9;
NeilBrown191ea9b2005-06-21 17:17:23 -0700764 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 r1_bio->mddev = mddev;
766 r1_bio->sector = bio->bi_sector;
767
Jens Axboea3623572005-11-01 09:26:16 +0100768 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 /*
770 * read balancing logic:
771 */
772 int rdisk = read_balance(conf, r1_bio);
773
774 if (rdisk < 0) {
775 /* couldn't find anywhere to read from */
776 raid_end_bio_io(r1_bio);
777 return 0;
778 }
779 mirror = conf->mirrors + rdisk;
780
781 r1_bio->read_disk = rdisk;
782
783 read_bio = bio_clone(bio, GFP_NOIO);
784
785 r1_bio->bios[rdisk] = read_bio;
786
787 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
788 read_bio->bi_bdev = mirror->rdev->bdev;
789 read_bio->bi_end_io = raid1_end_read_request;
790 read_bio->bi_rw = READ;
791 read_bio->bi_private = r1_bio;
792
793 generic_make_request(read_bio);
794 return 0;
795 }
796
797 /*
798 * WRITE:
799 */
800 /* first select target devices under spinlock and
801 * inc refcount on their rdev. Record them by setting
802 * bios[x] to bio
803 */
804 disks = conf->raid_disks;
NeilBrown191ea9b2005-06-21 17:17:23 -0700805#if 0
806 { static int first=1;
807 if (first) printk("First Write sector %llu disks %d\n",
808 (unsigned long long)r1_bio->sector, disks);
809 first = 0;
810 }
811#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 rcu_read_lock();
813 for (i = 0; i < disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800814 if ((rdev=rcu_dereference(conf->mirrors[i].rdev)) != NULL &&
NeilBrownb2d444d2005-11-08 21:39:31 -0800815 !test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 atomic_inc(&rdev->nr_pending);
NeilBrownb2d444d2005-11-08 21:39:31 -0800817 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 atomic_dec(&rdev->nr_pending);
819 r1_bio->bios[i] = NULL;
820 } else
821 r1_bio->bios[i] = bio;
NeilBrown191ea9b2005-06-21 17:17:23 -0700822 targets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 } else
824 r1_bio->bios[i] = NULL;
825 }
826 rcu_read_unlock();
827
NeilBrown4b6d2872005-09-09 16:23:47 -0700828 BUG_ON(targets == 0); /* we never fail the last device */
829
NeilBrown191ea9b2005-06-21 17:17:23 -0700830 if (targets < conf->raid_disks) {
831 /* array is degraded, we will not clear the bitmap
832 * on I/O completion (see raid1_end_write_request) */
833 set_bit(R1BIO_Degraded, &r1_bio->state);
834 }
NeilBrown06d91a52005-06-21 17:17:12 -0700835
NeilBrown4b6d2872005-09-09 16:23:47 -0700836 /* do behind I/O ? */
837 if (bitmap &&
838 atomic_read(&bitmap->behind_writes) < bitmap->max_write_behind &&
839 (behind_pages = alloc_behind_pages(bio)) != NULL)
840 set_bit(R1BIO_BehindIO, &r1_bio->state);
841
NeilBrown191ea9b2005-06-21 17:17:23 -0700842 atomic_set(&r1_bio->remaining, 0);
NeilBrown4b6d2872005-09-09 16:23:47 -0700843 atomic_set(&r1_bio->behind_remaining, 0);
NeilBrown191ea9b2005-06-21 17:17:23 -0700844
NeilBrowna9701a32005-11-08 21:39:34 -0800845 do_barriers = bio->bi_rw & BIO_RW_BARRIER;
846 if (do_barriers)
847 set_bit(R1BIO_Barrier, &r1_bio->state);
848
NeilBrown191ea9b2005-06-21 17:17:23 -0700849 bio_list_init(&bl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 for (i = 0; i < disks; i++) {
851 struct bio *mbio;
852 if (!r1_bio->bios[i])
853 continue;
854
855 mbio = bio_clone(bio, GFP_NOIO);
856 r1_bio->bios[i] = mbio;
857
858 mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
859 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
860 mbio->bi_end_io = raid1_end_write_request;
NeilBrowna9701a32005-11-08 21:39:34 -0800861 mbio->bi_rw = WRITE | do_barriers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 mbio->bi_private = r1_bio;
863
NeilBrown4b6d2872005-09-09 16:23:47 -0700864 if (behind_pages) {
865 struct bio_vec *bvec;
866 int j;
867
868 /* Yes, I really want the '__' version so that
869 * we clear any unused pointer in the io_vec, rather
870 * than leave them unchanged. This is important
871 * because when we come to free the pages, we won't
872 * know the originial bi_idx, so we just free
873 * them all
874 */
875 __bio_for_each_segment(bvec, mbio, j, 0)
876 bvec->bv_page = behind_pages[j];
877 if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
878 atomic_inc(&r1_bio->behind_remaining);
879 }
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 atomic_inc(&r1_bio->remaining);
NeilBrown191ea9b2005-06-21 17:17:23 -0700882
883 bio_list_add(&bl, mbio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
NeilBrown4b6d2872005-09-09 16:23:47 -0700885 kfree(behind_pages); /* the behind pages are attached to the bios now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
NeilBrown4b6d2872005-09-09 16:23:47 -0700887 bitmap_startwrite(bitmap, bio->bi_sector, r1_bio->sectors,
888 test_bit(R1BIO_BehindIO, &r1_bio->state));
NeilBrown191ea9b2005-06-21 17:17:23 -0700889 spin_lock_irqsave(&conf->device_lock, flags);
890 bio_list_merge(&conf->pending_bio_list, &bl);
891 bio_list_init(&bl);
892
893 blk_plug_device(mddev->queue);
894 spin_unlock_irqrestore(&conf->device_lock, flags);
895
896#if 0
897 while ((bio = bio_list_pop(&bl)) != NULL)
898 generic_make_request(bio);
899#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 return 0;
902}
903
904static void status(struct seq_file *seq, mddev_t *mddev)
905{
906 conf_t *conf = mddev_to_conf(mddev);
907 int i;
908
909 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
910 conf->working_disks);
911 for (i = 0; i < conf->raid_disks; i++)
912 seq_printf(seq, "%s",
913 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -0800914 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 seq_printf(seq, "]");
916}
917
918
919static void error(mddev_t *mddev, mdk_rdev_t *rdev)
920{
921 char b[BDEVNAME_SIZE];
922 conf_t *conf = mddev_to_conf(mddev);
923
924 /*
925 * If it is not operational, then we have already marked it as dead
926 * else if it is the last working disks, ignore the error, let the
927 * next level up know.
928 * else mark the drive as failed
929 */
NeilBrownb2d444d2005-11-08 21:39:31 -0800930 if (test_bit(In_sync, &rdev->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 && conf->working_disks == 1)
932 /*
933 * Don't fail the drive, act as though we were just a
934 * normal single drive
935 */
936 return;
NeilBrownb2d444d2005-11-08 21:39:31 -0800937 if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 mddev->degraded++;
939 conf->working_disks--;
940 /*
941 * if recovery is running, make sure it aborts.
942 */
943 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
944 }
NeilBrownb2d444d2005-11-08 21:39:31 -0800945 clear_bit(In_sync, &rdev->flags);
946 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 mddev->sb_dirty = 1;
948 printk(KERN_ALERT "raid1: Disk failure on %s, disabling device. \n"
949 " Operation continuing on %d devices\n",
950 bdevname(rdev->bdev,b), conf->working_disks);
951}
952
953static void print_conf(conf_t *conf)
954{
955 int i;
956 mirror_info_t *tmp;
957
958 printk("RAID1 conf printout:\n");
959 if (!conf) {
960 printk("(!conf)\n");
961 return;
962 }
963 printk(" --- wd:%d rd:%d\n", conf->working_disks,
964 conf->raid_disks);
965
966 for (i = 0; i < conf->raid_disks; i++) {
967 char b[BDEVNAME_SIZE];
968 tmp = conf->mirrors + i;
969 if (tmp->rdev)
970 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -0800971 i, !test_bit(In_sync, &tmp->rdev->flags), !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 bdevname(tmp->rdev->bdev,b));
973 }
974}
975
976static void close_sync(conf_t *conf)
977{
NeilBrown17999be2006-01-06 00:20:12 -0800978 wait_barrier(conf);
979 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 mempool_destroy(conf->r1buf_pool);
982 conf->r1buf_pool = NULL;
983}
984
985static int raid1_spare_active(mddev_t *mddev)
986{
987 int i;
988 conf_t *conf = mddev->private;
989 mirror_info_t *tmp;
990
991 /*
992 * Find all failed disks within the RAID1 configuration
993 * and mark them readable
994 */
995 for (i = 0; i < conf->raid_disks; i++) {
996 tmp = conf->mirrors + i;
997 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -0800998 && !test_bit(Faulty, &tmp->rdev->flags)
999 && !test_bit(In_sync, &tmp->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 conf->working_disks++;
1001 mddev->degraded--;
NeilBrownb2d444d2005-11-08 21:39:31 -08001002 set_bit(In_sync, &tmp->rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
1004 }
1005
1006 print_conf(conf);
1007 return 0;
1008}
1009
1010
1011static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1012{
1013 conf_t *conf = mddev->private;
1014 int found = 0;
NeilBrown41158c72005-06-21 17:17:25 -07001015 int mirror = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 mirror_info_t *p;
1017
1018 for (mirror=0; mirror < mddev->raid_disks; mirror++)
1019 if ( !(p=conf->mirrors+mirror)->rdev) {
1020
1021 blk_queue_stack_limits(mddev->queue,
1022 rdev->bdev->bd_disk->queue);
1023 /* as we don't honour merge_bvec_fn, we must never risk
1024 * violating it, so limit ->max_sector to one PAGE, as
1025 * a one page request is never in violation.
1026 */
1027 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1028 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1029 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
1030
1031 p->head_position = 0;
1032 rdev->raid_disk = mirror;
1033 found = 1;
NeilBrown6aea114a2005-11-28 13:44:13 -08001034 /* As all devices are equivalent, we don't need a full recovery
1035 * if this was recently any drive of the array
1036 */
1037 if (rdev->saved_raid_disk < 0)
NeilBrown41158c72005-06-21 17:17:25 -07001038 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001039 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 break;
1041 }
1042
1043 print_conf(conf);
1044 return found;
1045}
1046
1047static int raid1_remove_disk(mddev_t *mddev, int number)
1048{
1049 conf_t *conf = mddev->private;
1050 int err = 0;
1051 mdk_rdev_t *rdev;
1052 mirror_info_t *p = conf->mirrors+ number;
1053
1054 print_conf(conf);
1055 rdev = p->rdev;
1056 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001057 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 atomic_read(&rdev->nr_pending)) {
1059 err = -EBUSY;
1060 goto abort;
1061 }
1062 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001063 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (atomic_read(&rdev->nr_pending)) {
1065 /* lost the race, try later */
1066 err = -EBUSY;
1067 p->rdev = rdev;
1068 }
1069 }
1070abort:
1071
1072 print_conf(conf);
1073 return err;
1074}
1075
1076
1077static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
1078{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 if (bio->bi_size)
1082 return 1;
1083
1084 if (r1_bio->bios[r1_bio->read_disk] != bio)
1085 BUG();
1086 update_head_pos(r1_bio->read_disk, r1_bio);
1087 /*
1088 * we have read a block, now it needs to be re-written,
1089 * or re-read if the read failed.
1090 * We don't do much here, just schedule handling by raid1d
1091 */
NeilBrown69382e82006-01-06 00:20:22 -08001092 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 set_bit(R1BIO_Uptodate, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 reschedule_retry(r1_bio);
1095 return 0;
1096}
1097
1098static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
1099{
1100 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1101 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
1102 mddev_t *mddev = r1_bio->mddev;
1103 conf_t *conf = mddev_to_conf(mddev);
1104 int i;
1105 int mirror=0;
1106
1107 if (bio->bi_size)
1108 return 1;
1109
1110 for (i = 0; i < conf->raid_disks; i++)
1111 if (r1_bio->bios[i] == bio) {
1112 mirror = i;
1113 break;
1114 }
NeilBrowne3b97032005-08-04 12:53:34 -07001115 if (!uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 md_error(mddev, conf->mirrors[mirror].rdev);
NeilBrowne3b97032005-08-04 12:53:34 -07001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 update_head_pos(mirror, r1_bio);
1119
1120 if (atomic_dec_and_test(&r1_bio->remaining)) {
1121 md_done_sync(mddev, r1_bio->sectors, uptodate);
1122 put_buf(r1_bio);
1123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return 0;
1125}
1126
1127static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
1128{
1129 conf_t *conf = mddev_to_conf(mddev);
1130 int i;
1131 int disks = conf->raid_disks;
1132 struct bio *bio, *wbio;
1133
1134 bio = r1_bio->bios[r1_bio->read_disk];
1135
NeilBrown69382e82006-01-06 00:20:22 -08001136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 /*
1138 * schedule writes
1139 */
1140 if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
NeilBrown69382e82006-01-06 00:20:22 -08001141 /* ouch - failed to read all of that.
1142 * Try some synchronous reads of other devices to get
1143 * good data, much like with normal read errors. Only
1144 * read into the pages we already have so they we don't
1145 * need to re-issue the read request.
1146 * We don't need to freeze the array, because being in an
1147 * active sync request, there is no normal IO, and
1148 * no overlapping syncs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 */
NeilBrown69382e82006-01-06 00:20:22 -08001150 sector_t sect = r1_bio->sector;
1151 int sectors = r1_bio->sectors;
1152 int idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
NeilBrown69382e82006-01-06 00:20:22 -08001154 while(sectors) {
1155 int s = sectors;
1156 int d = r1_bio->read_disk;
1157 int success = 0;
1158 mdk_rdev_t *rdev;
1159
1160 if (s > (PAGE_SIZE>>9))
1161 s = PAGE_SIZE >> 9;
1162 do {
1163 if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
1164 rdev = conf->mirrors[d].rdev;
1165 if (sync_page_io(rdev->bdev,
1166 sect + rdev->data_offset,
1167 s<<9,
1168 bio->bi_io_vec[idx].bv_page,
1169 READ)) {
1170 success = 1;
1171 break;
1172 }
1173 }
1174 d++;
1175 if (d == conf->raid_disks)
1176 d = 0;
1177 } while (!success && d != r1_bio->read_disk);
1178
1179 if (success) {
1180 /* write it back and re-read */
1181 set_bit(R1BIO_Uptodate, &r1_bio->state);
1182 while (d != r1_bio->read_disk) {
1183 if (d == 0)
1184 d = conf->raid_disks;
1185 d--;
1186 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1187 continue;
1188 rdev = conf->mirrors[d].rdev;
1189 if (sync_page_io(rdev->bdev,
1190 sect + rdev->data_offset,
1191 s<<9,
1192 bio->bi_io_vec[idx].bv_page,
1193 WRITE) == 0 ||
1194 sync_page_io(rdev->bdev,
1195 sect + rdev->data_offset,
1196 s<<9,
1197 bio->bi_io_vec[idx].bv_page,
1198 READ) == 0) {
1199 md_error(mddev, rdev);
1200 }
1201 }
1202 } else {
1203 char b[BDEVNAME_SIZE];
1204 /* Cannot read from anywhere, array is toast */
1205 md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
1206 printk(KERN_ALERT "raid1: %s: unrecoverable I/O read error"
1207 " for block %llu\n",
1208 bdevname(bio->bi_bdev,b),
1209 (unsigned long long)r1_bio->sector);
1210 md_done_sync(mddev, r1_bio->sectors, 0);
1211 put_buf(r1_bio);
1212 return;
1213 }
1214 sectors -= s;
1215 sect += s;
1216 idx ++;
1217 }
1218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 atomic_set(&r1_bio->remaining, 1);
1220 for (i = 0; i < disks ; i++) {
1221 wbio = r1_bio->bios[i];
NeilBrown3e198f72006-01-06 00:20:21 -08001222 if (wbio->bi_end_io == NULL ||
1223 (wbio->bi_end_io == end_sync_read &&
1224 (i == r1_bio->read_disk ||
1225 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 continue;
1227
NeilBrown3e198f72006-01-06 00:20:21 -08001228 wbio->bi_rw = WRITE;
1229 wbio->bi_end_io = end_sync_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 atomic_inc(&r1_bio->remaining);
1231 md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
NeilBrown191ea9b2005-06-21 17:17:23 -07001232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 generic_make_request(wbio);
1234 }
1235
1236 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001237 /* if we're here, all write(s) have completed, so clean up */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 md_done_sync(mddev, r1_bio->sectors, 1);
1239 put_buf(r1_bio);
1240 }
1241}
1242
1243/*
1244 * This is a kernel thread which:
1245 *
1246 * 1. Retries failed read operations on working mirrors.
1247 * 2. Updates the raid superblock when problems encounter.
1248 * 3. Performs writes following reads for array syncronising.
1249 */
1250
1251static void raid1d(mddev_t *mddev)
1252{
1253 r1bio_t *r1_bio;
1254 struct bio *bio;
1255 unsigned long flags;
1256 conf_t *conf = mddev_to_conf(mddev);
1257 struct list_head *head = &conf->retry_list;
1258 int unplug=0;
1259 mdk_rdev_t *rdev;
1260
1261 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 for (;;) {
1264 char b[BDEVNAME_SIZE];
1265 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown191ea9b2005-06-21 17:17:23 -07001266
1267 if (conf->pending_bio_list.head) {
1268 bio = bio_list_get(&conf->pending_bio_list);
1269 blk_remove_plug(mddev->queue);
1270 spin_unlock_irqrestore(&conf->device_lock, flags);
1271 /* flush any pending bitmap writes to disk before proceeding w/ I/O */
1272 if (bitmap_unplug(mddev->bitmap) != 0)
1273 printk("%s: bitmap file write failed!\n", mdname(mddev));
1274
1275 while (bio) { /* submit pending writes */
1276 struct bio *next = bio->bi_next;
1277 bio->bi_next = NULL;
1278 generic_make_request(bio);
1279 bio = next;
1280 }
1281 unplug = 1;
1282
1283 continue;
1284 }
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (list_empty(head))
1287 break;
1288 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
1289 list_del(head->prev);
NeilBrownddaf22a2006-01-06 00:20:19 -08001290 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 spin_unlock_irqrestore(&conf->device_lock, flags);
1292
1293 mddev = r1_bio->mddev;
1294 conf = mddev_to_conf(mddev);
1295 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
1296 sync_request_write(mddev, r1_bio);
1297 unplug = 1;
NeilBrowna9701a32005-11-08 21:39:34 -08001298 } else if (test_bit(R1BIO_BarrierRetry, &r1_bio->state)) {
1299 /* some requests in the r1bio were BIO_RW_BARRIER
1300 * requests which failed with -ENOTSUPP. Hohumm..
1301 * Better resubmit without the barrier.
1302 * We know which devices to resubmit for, because
1303 * all others have had their bios[] entry cleared.
1304 */
1305 int i;
1306 clear_bit(R1BIO_BarrierRetry, &r1_bio->state);
1307 clear_bit(R1BIO_Barrier, &r1_bio->state);
1308 for (i=0; i < conf->raid_disks; i++)
1309 if (r1_bio->bios[i]) {
1310 struct bio_vec *bvec;
1311 int j;
1312
1313 bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
1314 /* copy pages from the failed bio, as
1315 * this might be a write-behind device */
1316 __bio_for_each_segment(bvec, bio, j, 0)
1317 bvec->bv_page = bio_iovec_idx(r1_bio->bios[i], j)->bv_page;
1318 bio_put(r1_bio->bios[i]);
1319 bio->bi_sector = r1_bio->sector +
1320 conf->mirrors[i].rdev->data_offset;
1321 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1322 bio->bi_end_io = raid1_end_write_request;
1323 bio->bi_rw = WRITE;
1324 bio->bi_private = r1_bio;
1325 r1_bio->bios[i] = bio;
1326 generic_make_request(bio);
1327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 } else {
1329 int disk;
NeilBrownddaf22a2006-01-06 00:20:19 -08001330
1331 /* we got a read error. Maybe the drive is bad. Maybe just
1332 * the block and we can fix it.
1333 * We freeze all other IO, and try reading the block from
1334 * other devices. When we find one, we re-write
1335 * and check it that fixes the read error.
1336 * This is all done synchronously while the array is
1337 * frozen
1338 */
1339 sector_t sect = r1_bio->sector;
1340 int sectors = r1_bio->sectors;
1341 freeze_array(conf);
NeilBrowncf30a472006-01-06 00:20:23 -08001342 if (mddev->ro == 0) while(sectors) {
NeilBrownddaf22a2006-01-06 00:20:19 -08001343 int s = sectors;
1344 int d = r1_bio->read_disk;
1345 int success = 0;
1346
1347 if (s > (PAGE_SIZE>>9))
1348 s = PAGE_SIZE >> 9;
1349
1350 do {
1351 rdev = conf->mirrors[d].rdev;
1352 if (rdev &&
1353 test_bit(In_sync, &rdev->flags) &&
1354 sync_page_io(rdev->bdev,
1355 sect + rdev->data_offset,
1356 s<<9,
1357 conf->tmppage, READ))
1358 success = 1;
1359 else {
1360 d++;
1361 if (d == conf->raid_disks)
1362 d = 0;
1363 }
1364 } while (!success && d != r1_bio->read_disk);
1365
1366 if (success) {
1367 /* write it back and re-read */
1368 while (d != r1_bio->read_disk) {
1369 if (d==0)
1370 d = conf->raid_disks;
1371 d--;
1372 rdev = conf->mirrors[d].rdev;
1373 if (rdev &&
1374 test_bit(In_sync, &rdev->flags)) {
1375 if (sync_page_io(rdev->bdev,
1376 sect + rdev->data_offset,
1377 s<<9, conf->tmppage, WRITE) == 0 ||
1378 sync_page_io(rdev->bdev,
1379 sect + rdev->data_offset,
1380 s<<9, conf->tmppage, READ) == 0) {
1381 /* Well, this device is dead */
1382 md_error(mddev, rdev);
1383 }
1384 }
1385 }
1386 } else {
1387 /* Cannot read from anywhere -- bye bye array */
1388 md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
1389 break;
1390 }
1391 sectors -= s;
1392 sect += s;
1393 }
1394
NeilBrownddaf22a2006-01-06 00:20:19 -08001395 unfreeze_array(conf);
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 bio = r1_bio->bios[r1_bio->read_disk];
1398 if ((disk=read_balance(conf, r1_bio)) == -1) {
1399 printk(KERN_ALERT "raid1: %s: unrecoverable I/O"
1400 " read error for block %llu\n",
1401 bdevname(bio->bi_bdev,b),
1402 (unsigned long long)r1_bio->sector);
1403 raid_end_bio_io(r1_bio);
1404 } else {
NeilBrowncf30a472006-01-06 00:20:23 -08001405 r1_bio->bios[r1_bio->read_disk] =
1406 mddev->ro ? IO_BLOCKED : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 r1_bio->read_disk = disk;
1408 bio_put(bio);
1409 bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
1410 r1_bio->bios[r1_bio->read_disk] = bio;
1411 rdev = conf->mirrors[disk].rdev;
1412 if (printk_ratelimit())
1413 printk(KERN_ERR "raid1: %s: redirecting sector %llu to"
1414 " another mirror\n",
1415 bdevname(rdev->bdev,b),
1416 (unsigned long long)r1_bio->sector);
1417 bio->bi_sector = r1_bio->sector + rdev->data_offset;
1418 bio->bi_bdev = rdev->bdev;
1419 bio->bi_end_io = raid1_end_read_request;
1420 bio->bi_rw = READ;
1421 bio->bi_private = r1_bio;
1422 unplug = 1;
1423 generic_make_request(bio);
1424 }
1425 }
1426 }
1427 spin_unlock_irqrestore(&conf->device_lock, flags);
1428 if (unplug)
1429 unplug_slaves(mddev);
1430}
1431
1432
1433static int init_resync(conf_t *conf)
1434{
1435 int buffs;
1436
1437 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
1438 if (conf->r1buf_pool)
1439 BUG();
1440 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
1441 conf->poolinfo);
1442 if (!conf->r1buf_pool)
1443 return -ENOMEM;
1444 conf->next_resync = 0;
1445 return 0;
1446}
1447
1448/*
1449 * perform a "sync" on one "block"
1450 *
1451 * We need to make sure that no normal I/O request - particularly write
1452 * requests - conflict with active sync requests.
1453 *
1454 * This is achieved by tracking pending requests and a 'barrier' concept
1455 * that can be installed to exclude normal IO requests.
1456 */
1457
NeilBrown57afd892005-06-21 17:17:13 -07001458static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
1460 conf_t *conf = mddev_to_conf(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 r1bio_t *r1_bio;
1462 struct bio *bio;
1463 sector_t max_sector, nr_sectors;
NeilBrown3e198f72006-01-06 00:20:21 -08001464 int disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 int i;
NeilBrown3e198f72006-01-06 00:20:21 -08001466 int wonly = -1;
1467 int write_targets = 0, read_targets = 0;
NeilBrown191ea9b2005-06-21 17:17:23 -07001468 int sync_blocks;
NeilBrowne3b97032005-08-04 12:53:34 -07001469 int still_degraded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 if (!conf->r1buf_pool)
NeilBrown191ea9b2005-06-21 17:17:23 -07001472 {
1473/*
1474 printk("sync start - bitmap %p\n", mddev->bitmap);
1475*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07001477 return 0;
NeilBrown191ea9b2005-06-21 17:17:23 -07001478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 max_sector = mddev->size << 1;
1481 if (sector_nr >= max_sector) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001482 /* If we aborted, we need to abort the
1483 * sync on the 'current' bitmap chunk (there will
1484 * only be one in raid1 resync.
1485 * We can find the current addess in mddev->curr_resync
1486 */
NeilBrown6a806c52005-07-15 03:56:35 -07001487 if (mddev->curr_resync < max_sector) /* aborted */
1488 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
NeilBrown191ea9b2005-06-21 17:17:23 -07001489 &sync_blocks, 1);
NeilBrown6a806c52005-07-15 03:56:35 -07001490 else /* completed sync */
NeilBrown191ea9b2005-06-21 17:17:23 -07001491 conf->fullsync = 0;
NeilBrown6a806c52005-07-15 03:56:35 -07001492
1493 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 close_sync(conf);
1495 return 0;
1496 }
1497
NeilBrowne3b97032005-08-04 12:53:34 -07001498 /* before building a request, check if we can skip these blocks..
1499 * This call the bitmap_start_sync doesn't actually record anything
1500 */
1501 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrowne5de485f2005-11-08 21:39:38 -08001502 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001503 /* We can skip this block, and probably several more */
1504 *skipped = 1;
1505 return sync_blocks;
1506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 /*
NeilBrown17999be2006-01-06 00:20:12 -08001508 * If there is non-resync activity waiting for a turn,
1509 * and resync is going fast enough,
1510 * then let it though before starting on this new sync request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 */
NeilBrown17999be2006-01-06 00:20:12 -08001512 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 msleep_interruptible(1000);
NeilBrown17999be2006-01-06 00:20:12 -08001514
1515 raise_barrier(conf);
1516
1517 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
NeilBrown3e198f72006-01-06 00:20:21 -08001520 rcu_read_lock();
1521 /*
1522 * If we get a correctably read error during resync or recovery,
1523 * we might want to read from a different device. So we
1524 * flag all drives that could conceivably be read from for READ,
1525 * and any others (which will be non-In_sync devices) for WRITE.
1526 * If a read fails, we try reading from something else for which READ
1527 * is OK.
1528 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 r1_bio->mddev = mddev;
1531 r1_bio->sector = sector_nr;
NeilBrown191ea9b2005-06-21 17:17:23 -07001532 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 set_bit(R1BIO_IsSync, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 for (i=0; i < conf->raid_disks; i++) {
NeilBrown3e198f72006-01-06 00:20:21 -08001536 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 bio = r1_bio->bios[i];
1538
1539 /* take from bio_init */
1540 bio->bi_next = NULL;
1541 bio->bi_flags |= 1 << BIO_UPTODATE;
1542 bio->bi_rw = 0;
1543 bio->bi_vcnt = 0;
1544 bio->bi_idx = 0;
1545 bio->bi_phys_segments = 0;
1546 bio->bi_hw_segments = 0;
1547 bio->bi_size = 0;
1548 bio->bi_end_io = NULL;
1549 bio->bi_private = NULL;
1550
NeilBrown3e198f72006-01-06 00:20:21 -08001551 rdev = rcu_dereference(conf->mirrors[i].rdev);
1552 if (rdev == NULL ||
1553 test_bit(Faulty, &rdev->flags)) {
NeilBrowne3b97032005-08-04 12:53:34 -07001554 still_degraded = 1;
1555 continue;
NeilBrown3e198f72006-01-06 00:20:21 -08001556 } else if (!test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 bio->bi_rw = WRITE;
1558 bio->bi_end_io = end_sync_write;
1559 write_targets ++;
NeilBrown3e198f72006-01-06 00:20:21 -08001560 } else {
1561 /* may need to read from here */
1562 bio->bi_rw = READ;
1563 bio->bi_end_io = end_sync_read;
1564 if (test_bit(WriteMostly, &rdev->flags)) {
1565 if (wonly < 0)
1566 wonly = i;
1567 } else {
1568 if (disk < 0)
1569 disk = i;
1570 }
1571 read_targets++;
1572 }
1573 atomic_inc(&rdev->nr_pending);
1574 bio->bi_sector = sector_nr + rdev->data_offset;
1575 bio->bi_bdev = rdev->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 bio->bi_private = r1_bio;
1577 }
NeilBrown3e198f72006-01-06 00:20:21 -08001578 rcu_read_unlock();
1579 if (disk < 0)
1580 disk = wonly;
1581 r1_bio->read_disk = disk;
NeilBrown191ea9b2005-06-21 17:17:23 -07001582
NeilBrown3e198f72006-01-06 00:20:21 -08001583 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
1584 /* extra read targets are also write targets */
1585 write_targets += read_targets-1;
1586
1587 if (write_targets == 0 || read_targets == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 /* There is nowhere to write, so all non-sync
1589 * drives must be failed - so we are finished
1590 */
NeilBrown57afd892005-06-21 17:17:13 -07001591 sector_t rv = max_sector - sector_nr;
1592 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 put_buf(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return rv;
1595 }
1596
1597 nr_sectors = 0;
NeilBrown289e99e2005-06-21 17:17:24 -07001598 sync_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 do {
1600 struct page *page;
1601 int len = PAGE_SIZE;
1602 if (sector_nr + (len>>9) > max_sector)
1603 len = (max_sector - sector_nr) << 9;
1604 if (len == 0)
1605 break;
NeilBrown6a806c52005-07-15 03:56:35 -07001606 if (sync_blocks == 0) {
1607 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
NeilBrowne5de485f2005-11-08 21:39:38 -08001608 &sync_blocks, still_degraded) &&
1609 !conf->fullsync &&
1610 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
NeilBrown6a806c52005-07-15 03:56:35 -07001611 break;
1612 if (sync_blocks < (PAGE_SIZE>>9))
1613 BUG();
1614 if (len > (sync_blocks<<9))
1615 len = sync_blocks<<9;
NeilBrownab7a30c2005-06-21 17:17:23 -07001616 }
NeilBrown191ea9b2005-06-21 17:17:23 -07001617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 for (i=0 ; i < conf->raid_disks; i++) {
1619 bio = r1_bio->bios[i];
1620 if (bio->bi_end_io) {
1621 page = r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page;
1622 if (bio_add_page(bio, page, len, 0) == 0) {
1623 /* stop here */
1624 r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page = page;
1625 while (i > 0) {
1626 i--;
1627 bio = r1_bio->bios[i];
NeilBrown6a806c52005-07-15 03:56:35 -07001628 if (bio->bi_end_io==NULL)
1629 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 /* remove last page from this bio */
1631 bio->bi_vcnt--;
1632 bio->bi_size -= len;
1633 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
1634 }
1635 goto bio_full;
1636 }
1637 }
1638 }
1639 nr_sectors += len>>9;
1640 sector_nr += len>>9;
NeilBrown191ea9b2005-06-21 17:17:23 -07001641 sync_blocks -= (len>>9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
1643 bio_full:
NeilBrown3e198f72006-01-06 00:20:21 -08001644 bio = r1_bio->bios[r1_bio->read_disk];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 r1_bio->sectors = nr_sectors;
1646
NeilBrown3e198f72006-01-06 00:20:21 -08001647 md_sync_acct(conf->mirrors[r1_bio->read_disk].rdev->bdev, nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 generic_make_request(bio);
1650
1651 return nr_sectors;
1652}
1653
1654static int run(mddev_t *mddev)
1655{
1656 conf_t *conf;
1657 int i, j, disk_idx;
1658 mirror_info_t *disk;
1659 mdk_rdev_t *rdev;
1660 struct list_head *tmp;
1661
1662 if (mddev->level != 1) {
1663 printk("raid1: %s: raid level not set to mirroring (%d)\n",
1664 mdname(mddev), mddev->level);
1665 goto out;
1666 }
1667 /*
1668 * copy the already verified devices into our private RAID1
1669 * bookkeeping area. [whatever we allocate in run(),
1670 * should be freed in stop()]
1671 */
1672 conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
1673 mddev->private = conf;
1674 if (!conf)
1675 goto out_no_mem;
1676
1677 memset(conf, 0, sizeof(*conf));
1678 conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks,
1679 GFP_KERNEL);
1680 if (!conf->mirrors)
1681 goto out_no_mem;
1682
1683 memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
1684
NeilBrownddaf22a2006-01-06 00:20:19 -08001685 conf->tmppage = alloc_page(GFP_KERNEL);
1686 if (!conf->tmppage)
1687 goto out_no_mem;
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 conf->poolinfo = kmalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
1690 if (!conf->poolinfo)
1691 goto out_no_mem;
1692 conf->poolinfo->mddev = mddev;
1693 conf->poolinfo->raid_disks = mddev->raid_disks;
1694 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1695 r1bio_pool_free,
1696 conf->poolinfo);
1697 if (!conf->r1bio_pool)
1698 goto out_no_mem;
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 ITERATE_RDEV(mddev, rdev, tmp) {
1701 disk_idx = rdev->raid_disk;
1702 if (disk_idx >= mddev->raid_disks
1703 || disk_idx < 0)
1704 continue;
1705 disk = conf->mirrors + disk_idx;
1706
1707 disk->rdev = rdev;
1708
1709 blk_queue_stack_limits(mddev->queue,
1710 rdev->bdev->bd_disk->queue);
1711 /* as we don't honour merge_bvec_fn, we must never risk
1712 * violating it, so limit ->max_sector to one PAGE, as
1713 * a one page request is never in violation.
1714 */
1715 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1716 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1717 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
1718
1719 disk->head_position = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08001720 if (!test_bit(Faulty, &rdev->flags) && test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 conf->working_disks++;
1722 }
1723 conf->raid_disks = mddev->raid_disks;
1724 conf->mddev = mddev;
1725 spin_lock_init(&conf->device_lock);
1726 INIT_LIST_HEAD(&conf->retry_list);
1727 if (conf->working_disks == 1)
1728 mddev->recovery_cp = MaxSector;
1729
1730 spin_lock_init(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -08001731 init_waitqueue_head(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
NeilBrown191ea9b2005-06-21 17:17:23 -07001733 bio_list_init(&conf->pending_bio_list);
1734 bio_list_init(&conf->flushing_bio_list);
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (!conf->working_disks) {
1737 printk(KERN_ERR "raid1: no operational mirrors for %s\n",
1738 mdname(mddev));
1739 goto out_free_conf;
1740 }
1741
1742 mddev->degraded = 0;
1743 for (i = 0; i < conf->raid_disks; i++) {
1744
1745 disk = conf->mirrors + i;
1746
1747 if (!disk->rdev) {
1748 disk->head_position = 0;
1749 mddev->degraded++;
1750 }
1751 }
1752
1753 /*
1754 * find the first working one and use it as a starting point
1755 * to read balancing.
1756 */
1757 for (j = 0; j < conf->raid_disks &&
1758 (!conf->mirrors[j].rdev ||
NeilBrownb2d444d2005-11-08 21:39:31 -08001759 !test_bit(In_sync, &conf->mirrors[j].rdev->flags)) ; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 /* nothing */;
1761 conf->last_used = j;
1762
1763
NeilBrown191ea9b2005-06-21 17:17:23 -07001764 mddev->thread = md_register_thread(raid1d, mddev, "%s_raid1");
1765 if (!mddev->thread) {
1766 printk(KERN_ERR
1767 "raid1: couldn't allocate thread for %s\n",
1768 mdname(mddev));
1769 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
NeilBrown191ea9b2005-06-21 17:17:23 -07001771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 printk(KERN_INFO
1773 "raid1: raid set %s active with %d out of %d mirrors\n",
1774 mdname(mddev), mddev->raid_disks - mddev->degraded,
1775 mddev->raid_disks);
1776 /*
1777 * Ok, everything is just fine now
1778 */
1779 mddev->array_size = mddev->size;
1780
NeilBrown7a5febe2005-05-16 21:53:16 -07001781 mddev->queue->unplug_fn = raid1_unplug;
1782 mddev->queue->issue_flush_fn = raid1_issue_flush;
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 return 0;
1785
1786out_no_mem:
1787 printk(KERN_ERR "raid1: couldn't allocate memory for %s\n",
1788 mdname(mddev));
1789
1790out_free_conf:
1791 if (conf) {
1792 if (conf->r1bio_pool)
1793 mempool_destroy(conf->r1bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07001794 kfree(conf->mirrors);
NeilBrownddaf22a2006-01-06 00:20:19 -08001795 __free_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07001796 kfree(conf->poolinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 kfree(conf);
1798 mddev->private = NULL;
1799 }
1800out:
1801 return -EIO;
1802}
1803
1804static int stop(mddev_t *mddev)
1805{
1806 conf_t *conf = mddev_to_conf(mddev);
NeilBrown4b6d2872005-09-09 16:23:47 -07001807 struct bitmap *bitmap = mddev->bitmap;
1808 int behind_wait = 0;
1809
1810 /* wait for behind writes to complete */
1811 while (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
1812 behind_wait++;
1813 printk(KERN_INFO "raid1: behind writes in progress on device %s, waiting to stop (%d)\n", mdname(mddev), behind_wait);
1814 set_current_state(TASK_UNINTERRUPTIBLE);
1815 schedule_timeout(HZ); /* wait a second */
1816 /* need to kick something here to make sure I/O goes? */
1817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 md_unregister_thread(mddev->thread);
1820 mddev->thread = NULL;
1821 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
1822 if (conf->r1bio_pool)
1823 mempool_destroy(conf->r1bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07001824 kfree(conf->mirrors);
1825 kfree(conf->poolinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 kfree(conf);
1827 mddev->private = NULL;
1828 return 0;
1829}
1830
1831static int raid1_resize(mddev_t *mddev, sector_t sectors)
1832{
1833 /* no resync is happening, and there is enough space
1834 * on all devices, so we can resize.
1835 * We need to make sure resync covers any new space.
1836 * If the array is shrinking we should possibly wait until
1837 * any io in the removed space completes, but it hardly seems
1838 * worth it.
1839 */
1840 mddev->array_size = sectors>>1;
1841 set_capacity(mddev->gendisk, mddev->array_size << 1);
1842 mddev->changed = 1;
1843 if (mddev->array_size > mddev->size && mddev->recovery_cp == MaxSector) {
1844 mddev->recovery_cp = mddev->size << 1;
1845 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1846 }
1847 mddev->size = mddev->array_size;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07001848 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 return 0;
1850}
1851
1852static int raid1_reshape(mddev_t *mddev, int raid_disks)
1853{
1854 /* We need to:
1855 * 1/ resize the r1bio_pool
1856 * 2/ resize conf->mirrors
1857 *
1858 * We allocate a new r1bio_pool if we can.
1859 * Then raise a device barrier and wait until all IO stops.
1860 * Then resize conf->mirrors and swap in the new r1bio pool.
NeilBrown6ea9c072005-06-21 17:17:09 -07001861 *
1862 * At the same time, we "pack" the devices so that all the missing
1863 * devices have the higher raid_disk numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 */
1865 mempool_t *newpool, *oldpool;
1866 struct pool_info *newpoolinfo;
1867 mirror_info_t *newmirrors;
1868 conf_t *conf = mddev_to_conf(mddev);
NeilBrown6ea9c072005-06-21 17:17:09 -07001869 int cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
NeilBrown6ea9c072005-06-21 17:17:09 -07001871 int d, d2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
NeilBrown6ea9c072005-06-21 17:17:09 -07001873 if (raid_disks < conf->raid_disks) {
1874 cnt=0;
1875 for (d= 0; d < conf->raid_disks; d++)
1876 if (conf->mirrors[d].rdev)
1877 cnt++;
1878 if (cnt > raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 return -EBUSY;
NeilBrown6ea9c072005-06-21 17:17:09 -07001880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
1883 if (!newpoolinfo)
1884 return -ENOMEM;
1885 newpoolinfo->mddev = mddev;
1886 newpoolinfo->raid_disks = raid_disks;
1887
1888 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1889 r1bio_pool_free, newpoolinfo);
1890 if (!newpool) {
1891 kfree(newpoolinfo);
1892 return -ENOMEM;
1893 }
1894 newmirrors = kmalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
1895 if (!newmirrors) {
1896 kfree(newpoolinfo);
1897 mempool_destroy(newpool);
1898 return -ENOMEM;
1899 }
1900 memset(newmirrors, 0, sizeof(struct mirror_info)*raid_disks);
1901
NeilBrown17999be2006-01-06 00:20:12 -08001902 raise_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 /* ok, everything is stopped */
1905 oldpool = conf->r1bio_pool;
1906 conf->r1bio_pool = newpool;
NeilBrown6ea9c072005-06-21 17:17:09 -07001907
1908 for (d=d2=0; d < conf->raid_disks; d++)
1909 if (conf->mirrors[d].rdev) {
1910 conf->mirrors[d].rdev->raid_disk = d2;
1911 newmirrors[d2++].rdev = conf->mirrors[d].rdev;
1912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 kfree(conf->mirrors);
1914 conf->mirrors = newmirrors;
1915 kfree(conf->poolinfo);
1916 conf->poolinfo = newpoolinfo;
1917
1918 mddev->degraded += (raid_disks - conf->raid_disks);
1919 conf->raid_disks = mddev->raid_disks = raid_disks;
1920
NeilBrown6ea9c072005-06-21 17:17:09 -07001921 conf->last_used = 0; /* just make sure it is in-range */
NeilBrown17999be2006-01-06 00:20:12 -08001922 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1925 md_wakeup_thread(mddev->thread);
1926
1927 mempool_destroy(oldpool);
1928 return 0;
1929}
1930
NeilBrown500af872005-09-09 16:23:58 -07001931static void raid1_quiesce(mddev_t *mddev, int state)
NeilBrown36fa3062005-09-09 16:23:45 -07001932{
1933 conf_t *conf = mddev_to_conf(mddev);
1934
1935 switch(state) {
NeilBrown9e6603d2005-09-09 16:23:48 -07001936 case 1:
NeilBrown17999be2006-01-06 00:20:12 -08001937 raise_barrier(conf);
NeilBrown36fa3062005-09-09 16:23:45 -07001938 break;
NeilBrown9e6603d2005-09-09 16:23:48 -07001939 case 0:
NeilBrown17999be2006-01-06 00:20:12 -08001940 lower_barrier(conf);
NeilBrown36fa3062005-09-09 16:23:45 -07001941 break;
1942 }
NeilBrown36fa3062005-09-09 16:23:45 -07001943}
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946static mdk_personality_t raid1_personality =
1947{
1948 .name = "raid1",
1949 .owner = THIS_MODULE,
1950 .make_request = make_request,
1951 .run = run,
1952 .stop = stop,
1953 .status = status,
1954 .error_handler = error,
1955 .hot_add_disk = raid1_add_disk,
1956 .hot_remove_disk= raid1_remove_disk,
1957 .spare_active = raid1_spare_active,
1958 .sync_request = sync_request,
1959 .resize = raid1_resize,
1960 .reshape = raid1_reshape,
NeilBrown36fa3062005-09-09 16:23:45 -07001961 .quiesce = raid1_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962};
1963
1964static int __init raid_init(void)
1965{
1966 return register_md_personality(RAID1, &raid1_personality);
1967}
1968
1969static void raid_exit(void)
1970{
1971 unregister_md_personality(RAID1);
1972}
1973
1974module_init(raid_init);
1975module_exit(raid_exit);
1976MODULE_LICENSE("GPL");
1977MODULE_ALIAS("md-personality-3"); /* RAID1 */