blob: 7fbb60883280842d8b31ab954df878e5c0aae36c [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
NeilBrownd11c1712006-01-06 00:20:26 -0800109 * the first bio.
110 * If this is a user-requested check/repair, allocate
111 * RESYNC_PAGES for each bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 */
NeilBrownd11c1712006-01-06 00:20:26 -0800113 if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery))
114 j = pi->raid_disks;
115 else
116 j = 1;
117 while(j--) {
118 bio = r1_bio->bios[j];
119 for (i = 0; i < RESYNC_PAGES; i++) {
120 page = alloc_page(gfp_flags);
121 if (unlikely(!page))
122 goto out_free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
NeilBrownd11c1712006-01-06 00:20:26 -0800124 bio->bi_io_vec[i].bv_page = page;
125 }
126 }
127 /* If not user-requests, copy the page pointers to all bios */
128 if (!test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) {
129 for (i=0; i<RESYNC_PAGES ; i++)
130 for (j=1; j<pi->raid_disks; j++)
131 r1_bio->bios[j]->bi_io_vec[i].bv_page =
132 r1_bio->bios[0]->bi_io_vec[i].bv_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
134
135 r1_bio->master_bio = NULL;
136
137 return r1_bio;
138
139out_free_pages:
NeilBrownd11c1712006-01-06 00:20:26 -0800140 for (i=0; i < RESYNC_PAGES ; i++)
141 for (j=0 ; j < pi->raid_disks; j++)
142 __free_page(r1_bio->bios[j]->bi_io_vec[i].bv_page);
143 j = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144out_free_bio:
145 while ( ++j < pi->raid_disks )
146 bio_put(r1_bio->bios[j]);
147 r1bio_pool_free(r1_bio, data);
148 return NULL;
149}
150
151static void r1buf_pool_free(void *__r1_bio, void *data)
152{
153 struct pool_info *pi = data;
NeilBrownd11c1712006-01-06 00:20:26 -0800154 int i,j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 r1bio_t *r1bio = __r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
NeilBrownd11c1712006-01-06 00:20:26 -0800157 for (i = 0; i < RESYNC_PAGES; i++)
158 for (j = pi->raid_disks; j-- ;) {
159 if (j == 0 ||
160 r1bio->bios[j]->bi_io_vec[i].bv_page !=
161 r1bio->bios[0]->bi_io_vec[i].bv_page)
162 __free_page(r1bio->bios[j]->bi_io_vec[i].bv_page);
163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 for (i=0 ; i < pi->raid_disks; i++)
165 bio_put(r1bio->bios[i]);
166
167 r1bio_pool_free(r1bio, data);
168}
169
170static void put_all_bios(conf_t *conf, r1bio_t *r1_bio)
171{
172 int i;
173
174 for (i = 0; i < conf->raid_disks; i++) {
175 struct bio **bio = r1_bio->bios + i;
NeilBrowncf30a472006-01-06 00:20:23 -0800176 if (*bio && *bio != IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 bio_put(*bio);
178 *bio = NULL;
179 }
180}
181
182static inline void free_r1bio(r1bio_t *r1_bio)
183{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 conf_t *conf = mddev_to_conf(r1_bio->mddev);
185
186 /*
187 * Wake up any possible resync thread that waits for the device
188 * to go idle.
189 */
NeilBrown17999be2006-01-06 00:20:12 -0800190 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 put_all_bios(conf, r1_bio);
193 mempool_free(r1_bio, conf->r1bio_pool);
194}
195
196static inline void put_buf(r1bio_t *r1_bio)
197{
198 conf_t *conf = mddev_to_conf(r1_bio->mddev);
NeilBrown3e198f72006-01-06 00:20:21 -0800199 int i;
200
201 for (i=0; i<conf->raid_disks; i++) {
202 struct bio *bio = r1_bio->bios[i];
203 if (bio->bi_end_io)
204 rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev);
205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 mempool_free(r1_bio, conf->r1buf_pool);
208
NeilBrown17999be2006-01-06 00:20:12 -0800209 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212static void reschedule_retry(r1bio_t *r1_bio)
213{
214 unsigned long flags;
215 mddev_t *mddev = r1_bio->mddev;
216 conf_t *conf = mddev_to_conf(mddev);
217
218 spin_lock_irqsave(&conf->device_lock, flags);
219 list_add(&r1_bio->retry_list, &conf->retry_list);
NeilBrownddaf22a2006-01-06 00:20:19 -0800220 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 spin_unlock_irqrestore(&conf->device_lock, flags);
222
NeilBrown17999be2006-01-06 00:20:12 -0800223 wake_up(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 md_wakeup_thread(mddev->thread);
225}
226
227/*
228 * raid_end_bio_io() is called when we have finished servicing a mirrored
229 * operation and are ready to return a success/failure code to the buffer
230 * cache layer.
231 */
232static void raid_end_bio_io(r1bio_t *r1_bio)
233{
234 struct bio *bio = r1_bio->master_bio;
235
NeilBrown4b6d2872005-09-09 16:23:47 -0700236 /* if nobody has done the final endio yet, do it now */
237 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
238 PRINTK(KERN_DEBUG "raid1: sync end %s on sectors %llu-%llu\n",
239 (bio_data_dir(bio) == WRITE) ? "write" : "read",
240 (unsigned long long) bio->bi_sector,
241 (unsigned long long) bio->bi_sector +
242 (bio->bi_size >> 9) - 1);
243
244 bio_endio(bio, bio->bi_size,
245 test_bit(R1BIO_Uptodate, &r1_bio->state) ? 0 : -EIO);
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 free_r1bio(r1_bio);
248}
249
250/*
251 * Update disk head position estimator based on IRQ completion info.
252 */
253static inline void update_head_pos(int disk, r1bio_t *r1_bio)
254{
255 conf_t *conf = mddev_to_conf(r1_bio->mddev);
256
257 conf->mirrors[disk].head_position =
258 r1_bio->sector + (r1_bio->sectors);
259}
260
261static int raid1_end_read_request(struct bio *bio, unsigned int bytes_done, int error)
262{
263 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
264 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
265 int mirror;
266 conf_t *conf = mddev_to_conf(r1_bio->mddev);
267
268 if (bio->bi_size)
269 return 1;
270
271 mirror = r1_bio->read_disk;
272 /*
273 * this branch is our 'one mirror IO has finished' event handler:
274 */
NeilBrownddaf22a2006-01-06 00:20:19 -0800275 update_head_pos(mirror, r1_bio);
276
277 if (uptodate || conf->working_disks <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 /*
279 * Set R1BIO_Uptodate in our master bio, so that
280 * we will return a good error code for to the higher
281 * levels even if IO on some other mirrored buffer fails.
282 *
283 * The 'master' represents the composite IO operation to
284 * user-side. So if something waits for IO, then it will
285 * wait for the 'master' bio.
286 */
287 set_bit(R1BIO_Uptodate, &r1_bio->state);
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 raid_end_bio_io(r1_bio);
NeilBrownddaf22a2006-01-06 00:20:19 -0800290 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 /*
292 * oops, read error:
293 */
294 char b[BDEVNAME_SIZE];
295 if (printk_ratelimit())
296 printk(KERN_ERR "raid1: %s: rescheduling sector %llu\n",
297 bdevname(conf->mirrors[mirror].rdev->bdev,b), (unsigned long long)r1_bio->sector);
298 reschedule_retry(r1_bio);
299 }
300
301 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
302 return 0;
303}
304
305static int raid1_end_write_request(struct bio *bio, unsigned int bytes_done, int error)
306{
307 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
308 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
NeilBrowna9701a32005-11-08 21:39:34 -0800309 int mirror, behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 conf_t *conf = mddev_to_conf(r1_bio->mddev);
311
312 if (bio->bi_size)
313 return 1;
314
315 for (mirror = 0; mirror < conf->raid_disks; mirror++)
316 if (r1_bio->bios[mirror] == bio)
317 break;
318
NeilBrowna9701a32005-11-08 21:39:34 -0800319 if (error == -ENOTSUPP && test_bit(R1BIO_Barrier, &r1_bio->state)) {
320 set_bit(BarriersNotsupp, &conf->mirrors[mirror].rdev->flags);
321 set_bit(R1BIO_BarrierRetry, &r1_bio->state);
322 r1_bio->mddev->barriers_work = 0;
323 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 /*
NeilBrowna9701a32005-11-08 21:39:34 -0800325 * this branch is our 'one mirror IO has finished' event handler:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 */
NeilBrowna9701a32005-11-08 21:39:34 -0800327 r1_bio->bios[mirror] = NULL;
NeilBrowna9701a32005-11-08 21:39:34 -0800328 if (!uptodate) {
329 md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
330 /* an I/O failed, we can't clear the bitmap */
331 set_bit(R1BIO_Degraded, &r1_bio->state);
332 } else
333 /*
334 * Set R1BIO_Uptodate in our master bio, so that
335 * we will return a good error code for to the higher
336 * levels even if IO on some other mirrored buffer fails.
337 *
338 * The 'master' represents the composite IO operation to
339 * user-side. So if something waits for IO, then it will
340 * wait for the 'master' bio.
341 */
342 set_bit(R1BIO_Uptodate, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
NeilBrowna9701a32005-11-08 21:39:34 -0800344 update_head_pos(mirror, r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
NeilBrowna9701a32005-11-08 21:39:34 -0800346 if (behind) {
347 if (test_bit(WriteMostly, &conf->mirrors[mirror].rdev->flags))
348 atomic_dec(&r1_bio->behind_remaining);
NeilBrown4b6d2872005-09-09 16:23:47 -0700349
NeilBrowna9701a32005-11-08 21:39:34 -0800350 /* In behind mode, we ACK the master bio once the I/O has safely
351 * reached all non-writemostly disks. Setting the Returned bit
352 * ensures that this gets done only once -- we don't ever want to
353 * return -EIO here, instead we'll wait */
NeilBrown4b6d2872005-09-09 16:23:47 -0700354
NeilBrowna9701a32005-11-08 21:39:34 -0800355 if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
356 test_bit(R1BIO_Uptodate, &r1_bio->state)) {
357 /* Maybe we can return now */
358 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
359 struct bio *mbio = r1_bio->master_bio;
360 PRINTK(KERN_DEBUG "raid1: behind end write sectors %llu-%llu\n",
361 (unsigned long long) mbio->bi_sector,
362 (unsigned long long) mbio->bi_sector +
363 (mbio->bi_size >> 9) - 1);
364 bio_endio(mbio, mbio->bi_size, 0);
365 }
NeilBrown4b6d2872005-09-09 16:23:47 -0700366 }
367 }
368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 /*
370 *
371 * Let's see if all mirrored write operations have finished
372 * already.
373 */
374 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrowna9701a32005-11-08 21:39:34 -0800375 if (test_bit(R1BIO_BarrierRetry, &r1_bio->state)) {
376 reschedule_retry(r1_bio);
377 /* Don't dec_pending yet, we want to hold
378 * the reference over the retry
379 */
380 return 0;
381 }
NeilBrown4b6d2872005-09-09 16:23:47 -0700382 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
383 /* free extra copy of the data pages */
384 int i = bio->bi_vcnt;
385 while (i--)
386 __free_page(bio->bi_io_vec[i].bv_page);
387 }
NeilBrown191ea9b2005-06-21 17:17:23 -0700388 /* clear the bitmap if all writes complete successfully */
389 bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
390 r1_bio->sectors,
NeilBrown4b6d2872005-09-09 16:23:47 -0700391 !test_bit(R1BIO_Degraded, &r1_bio->state),
392 behind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 md_write_end(r1_bio->mddev);
394 raid_end_bio_io(r1_bio);
395 }
396
NeilBrown3795bb02005-12-12 02:39:16 -0800397 if (r1_bio->bios[mirror]==NULL)
398 bio_put(bio);
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
401 return 0;
402}
403
404
405/*
406 * This routine returns the disk from which the requested read should
407 * be done. There is a per-array 'next expected sequential IO' sector
408 * number - if this matches on the next IO then we use the last disk.
409 * There is also a per-disk 'last know head position' sector that is
410 * maintained from IRQ contexts, both the normal and the resync IO
411 * completion handlers update this position correctly. If there is no
412 * perfect sequential match then we pick the disk whose head is closest.
413 *
414 * If there are 2 mirrors in the same 2 devices, performance degrades
415 * because position is mirror, not device based.
416 *
417 * The rdev for the device selected will have nr_pending incremented.
418 */
419static int read_balance(conf_t *conf, r1bio_t *r1_bio)
420{
421 const unsigned long this_sector = r1_bio->sector;
422 int new_disk = conf->last_used, disk = new_disk;
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700423 int wonly_disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 const int sectors = r1_bio->sectors;
425 sector_t new_distance, current_distance;
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700426 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 rcu_read_lock();
429 /*
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700430 * Check if we can balance. We can balance on the whole
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 * device if no resync is going on, or below the resync window.
432 * We take the first readable disk when above the resync window.
433 */
434 retry:
435 if (conf->mddev->recovery_cp < MaxSector &&
436 (this_sector + sectors >= conf->next_resync)) {
437 /* Choose the first operation device, for consistancy */
438 new_disk = 0;
439
Suzanne Woodd6065f72005-11-08 21:39:27 -0800440 for (rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
NeilBrowncf30a472006-01-06 00:20:23 -0800441 r1_bio->bios[new_disk] == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800442 !rdev || !test_bit(In_sync, &rdev->flags)
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700443 || test_bit(WriteMostly, &rdev->flags);
Suzanne Woodd6065f72005-11-08 21:39:27 -0800444 rdev = rcu_dereference(conf->mirrors[++new_disk].rdev)) {
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700445
NeilBrowncf30a472006-01-06 00:20:23 -0800446 if (rdev && test_bit(In_sync, &rdev->flags) &&
447 r1_bio->bios[new_disk] != IO_BLOCKED)
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700448 wonly_disk = new_disk;
449
450 if (new_disk == conf->raid_disks - 1) {
451 new_disk = wonly_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 break;
453 }
454 }
455 goto rb_out;
456 }
457
458
459 /* make sure the disk is operational */
Suzanne Woodd6065f72005-11-08 21:39:27 -0800460 for (rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
NeilBrowncf30a472006-01-06 00:20:23 -0800461 r1_bio->bios[new_disk] == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800462 !rdev || !test_bit(In_sync, &rdev->flags) ||
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700463 test_bit(WriteMostly, &rdev->flags);
Suzanne Woodd6065f72005-11-08 21:39:27 -0800464 rdev = rcu_dereference(conf->mirrors[new_disk].rdev)) {
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700465
NeilBrowncf30a472006-01-06 00:20:23 -0800466 if (rdev && test_bit(In_sync, &rdev->flags) &&
467 r1_bio->bios[new_disk] != IO_BLOCKED)
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700468 wonly_disk = new_disk;
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (new_disk <= 0)
471 new_disk = conf->raid_disks;
472 new_disk--;
473 if (new_disk == disk) {
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700474 new_disk = wonly_disk;
475 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700478
479 if (new_disk < 0)
480 goto rb_out;
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 disk = new_disk;
483 /* now disk == new_disk == starting point for search */
484
485 /*
486 * Don't change to another disk for sequential reads:
487 */
488 if (conf->next_seq_sect == this_sector)
489 goto rb_out;
490 if (this_sector == conf->mirrors[new_disk].head_position)
491 goto rb_out;
492
493 current_distance = abs(this_sector - conf->mirrors[disk].head_position);
494
495 /* Find the disk whose head is closest */
496
497 do {
498 if (disk <= 0)
499 disk = conf->raid_disks;
500 disk--;
501
Suzanne Woodd6065f72005-11-08 21:39:27 -0800502 rdev = rcu_dereference(conf->mirrors[disk].rdev);
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700503
NeilBrowncf30a472006-01-06 00:20:23 -0800504 if (!rdev || r1_bio->bios[disk] == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800505 !test_bit(In_sync, &rdev->flags) ||
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700506 test_bit(WriteMostly, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 continue;
508
509 if (!atomic_read(&rdev->nr_pending)) {
510 new_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 break;
512 }
513 new_distance = abs(this_sector - conf->mirrors[disk].head_position);
514 if (new_distance < current_distance) {
515 current_distance = new_distance;
516 new_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518 } while (disk != conf->last_used);
519
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700520 rb_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522
523 if (new_disk >= 0) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800524 rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700525 if (!rdev)
526 goto retry;
527 atomic_inc(&rdev->nr_pending);
NeilBrownb2d444d2005-11-08 21:39:31 -0800528 if (!test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /* cannot risk returning a device that failed
530 * before we inc'ed nr_pending
531 */
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700532 atomic_dec(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 goto retry;
534 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700535 conf->next_seq_sect = this_sector + sectors;
536 conf->last_used = new_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538 rcu_read_unlock();
539
540 return new_disk;
541}
542
543static void unplug_slaves(mddev_t *mddev)
544{
545 conf_t *conf = mddev_to_conf(mddev);
546 int i;
547
548 rcu_read_lock();
549 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800550 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800551 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
553
554 atomic_inc(&rdev->nr_pending);
555 rcu_read_unlock();
556
557 if (r_queue->unplug_fn)
558 r_queue->unplug_fn(r_queue);
559
560 rdev_dec_pending(rdev, mddev);
561 rcu_read_lock();
562 }
563 }
564 rcu_read_unlock();
565}
566
567static void raid1_unplug(request_queue_t *q)
568{
NeilBrown191ea9b2005-06-21 17:17:23 -0700569 mddev_t *mddev = q->queuedata;
570
571 unplug_slaves(mddev);
572 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
575static int raid1_issue_flush(request_queue_t *q, struct gendisk *disk,
576 sector_t *error_sector)
577{
578 mddev_t *mddev = q->queuedata;
579 conf_t *conf = mddev_to_conf(mddev);
580 int i, ret = 0;
581
582 rcu_read_lock();
583 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800584 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800585 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 struct block_device *bdev = rdev->bdev;
587 request_queue_t *r_queue = bdev_get_queue(bdev);
588
589 if (!r_queue->issue_flush_fn)
590 ret = -EOPNOTSUPP;
591 else {
592 atomic_inc(&rdev->nr_pending);
593 rcu_read_unlock();
594 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
595 error_sector);
596 rdev_dec_pending(rdev, mddev);
597 rcu_read_lock();
598 }
599 }
600 }
601 rcu_read_unlock();
602 return ret;
603}
604
NeilBrown17999be2006-01-06 00:20:12 -0800605/* Barriers....
606 * Sometimes we need to suspend IO while we do something else,
607 * either some resync/recovery, or reconfigure the array.
608 * To do this we raise a 'barrier'.
609 * The 'barrier' is a counter that can be raised multiple times
610 * to count how many activities are happening which preclude
611 * normal IO.
612 * We can only raise the barrier if there is no pending IO.
613 * i.e. if nr_pending == 0.
614 * We choose only to raise the barrier if no-one is waiting for the
615 * barrier to go down. This means that as soon as an IO request
616 * is ready, no other operations which require a barrier will start
617 * until the IO request has had a chance.
618 *
619 * So: regular IO calls 'wait_barrier'. When that returns there
620 * is no backgroup IO happening, It must arrange to call
621 * allow_barrier when it has finished its IO.
622 * backgroup IO calls must call raise_barrier. Once that returns
623 * there is no normal IO happeing. It must arrange to call
624 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 */
626#define RESYNC_DEPTH 32
627
NeilBrown17999be2006-01-06 00:20:12 -0800628static void raise_barrier(conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 spin_lock_irq(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800631
632 /* Wait until no block IO is waiting */
633 wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
634 conf->resync_lock,
635 raid1_unplug(conf->mddev->queue));
636
637 /* block any new IO from starting */
638 conf->barrier++;
639
640 /* No wait for all pending IO to complete */
641 wait_event_lock_irq(conf->wait_barrier,
642 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
643 conf->resync_lock,
644 raid1_unplug(conf->mddev->queue));
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 spin_unlock_irq(&conf->resync_lock);
647}
648
NeilBrown17999be2006-01-06 00:20:12 -0800649static void lower_barrier(conf_t *conf)
650{
651 unsigned long flags;
652 spin_lock_irqsave(&conf->resync_lock, flags);
653 conf->barrier--;
654 spin_unlock_irqrestore(&conf->resync_lock, flags);
655 wake_up(&conf->wait_barrier);
656}
657
658static void wait_barrier(conf_t *conf)
659{
660 spin_lock_irq(&conf->resync_lock);
661 if (conf->barrier) {
662 conf->nr_waiting++;
663 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
664 conf->resync_lock,
665 raid1_unplug(conf->mddev->queue));
666 conf->nr_waiting--;
667 }
668 conf->nr_pending++;
669 spin_unlock_irq(&conf->resync_lock);
670}
671
672static void allow_barrier(conf_t *conf)
673{
674 unsigned long flags;
675 spin_lock_irqsave(&conf->resync_lock, flags);
676 conf->nr_pending--;
677 spin_unlock_irqrestore(&conf->resync_lock, flags);
678 wake_up(&conf->wait_barrier);
679}
680
NeilBrownddaf22a2006-01-06 00:20:19 -0800681static void freeze_array(conf_t *conf)
682{
683 /* stop syncio and normal IO and wait for everything to
684 * go quite.
685 * We increment barrier and nr_waiting, and then
686 * wait until barrier+nr_pending match nr_queued+2
687 */
688 spin_lock_irq(&conf->resync_lock);
689 conf->barrier++;
690 conf->nr_waiting++;
691 wait_event_lock_irq(conf->wait_barrier,
692 conf->barrier+conf->nr_pending == conf->nr_queued+2,
693 conf->resync_lock,
694 raid1_unplug(conf->mddev->queue));
695 spin_unlock_irq(&conf->resync_lock);
696}
697static void unfreeze_array(conf_t *conf)
698{
699 /* reverse the effect of the freeze */
700 spin_lock_irq(&conf->resync_lock);
701 conf->barrier--;
702 conf->nr_waiting--;
703 wake_up(&conf->wait_barrier);
704 spin_unlock_irq(&conf->resync_lock);
705}
706
NeilBrown17999be2006-01-06 00:20:12 -0800707
NeilBrown4b6d2872005-09-09 16:23:47 -0700708/* duplicate the data pages for behind I/O */
709static struct page **alloc_behind_pages(struct bio *bio)
710{
711 int i;
712 struct bio_vec *bvec;
713 struct page **pages = kmalloc(bio->bi_vcnt * sizeof(struct page *),
714 GFP_NOIO);
715 if (unlikely(!pages))
716 goto do_sync_io;
717
718 memset(pages, 0, bio->bi_vcnt * sizeof(struct page *));
719
720 bio_for_each_segment(bvec, bio, i) {
721 pages[i] = alloc_page(GFP_NOIO);
722 if (unlikely(!pages[i]))
723 goto do_sync_io;
724 memcpy(kmap(pages[i]) + bvec->bv_offset,
725 kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
726 kunmap(pages[i]);
727 kunmap(bvec->bv_page);
728 }
729
730 return pages;
731
732do_sync_io:
733 if (pages)
734 for (i = 0; i < bio->bi_vcnt && pages[i]; i++)
735 __free_page(pages[i]);
736 kfree(pages);
737 PRINTK("%dB behind alloc failed, doing sync I/O\n", bio->bi_size);
738 return NULL;
739}
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741static int make_request(request_queue_t *q, struct bio * bio)
742{
743 mddev_t *mddev = q->queuedata;
744 conf_t *conf = mddev_to_conf(mddev);
745 mirror_info_t *mirror;
746 r1bio_t *r1_bio;
747 struct bio *read_bio;
NeilBrown191ea9b2005-06-21 17:17:23 -0700748 int i, targets = 0, disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 mdk_rdev_t *rdev;
NeilBrown191ea9b2005-06-21 17:17:23 -0700750 struct bitmap *bitmap = mddev->bitmap;
751 unsigned long flags;
752 struct bio_list bl;
NeilBrown4b6d2872005-09-09 16:23:47 -0700753 struct page **behind_pages = NULL;
Jens Axboea3623572005-11-01 09:26:16 +0100754 const int rw = bio_data_dir(bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800755 int do_barriers;
NeilBrown191ea9b2005-06-21 17:17:23 -0700756
NeilBrowna9701a32005-11-08 21:39:34 -0800757 if (unlikely(!mddev->barriers_work && bio_barrier(bio))) {
NeilBrowne5dcdd82005-09-09 16:23:41 -0700758 bio_endio(bio, bio->bi_size, -EOPNOTSUPP);
759 return 0;
760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 /*
763 * Register the new request and wait if the reconstruction
764 * thread has put up a bar for new requests.
765 * Continue immediately if no resync is active currently.
766 */
NeilBrown3d310eb2005-06-21 17:17:26 -0700767 md_write_start(mddev, bio); /* wait on superblock update early */
768
NeilBrown17999be2006-01-06 00:20:12 -0800769 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Jens Axboea3623572005-11-01 09:26:16 +0100771 disk_stat_inc(mddev->gendisk, ios[rw]);
772 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 /*
775 * make_request() can abort the operation when READA is being
776 * used and no empty request is available.
777 *
778 */
779 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
780
781 r1_bio->master_bio = bio;
782 r1_bio->sectors = bio->bi_size >> 9;
NeilBrown191ea9b2005-06-21 17:17:23 -0700783 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 r1_bio->mddev = mddev;
785 r1_bio->sector = bio->bi_sector;
786
Jens Axboea3623572005-11-01 09:26:16 +0100787 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 /*
789 * read balancing logic:
790 */
791 int rdisk = read_balance(conf, r1_bio);
792
793 if (rdisk < 0) {
794 /* couldn't find anywhere to read from */
795 raid_end_bio_io(r1_bio);
796 return 0;
797 }
798 mirror = conf->mirrors + rdisk;
799
800 r1_bio->read_disk = rdisk;
801
802 read_bio = bio_clone(bio, GFP_NOIO);
803
804 r1_bio->bios[rdisk] = read_bio;
805
806 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
807 read_bio->bi_bdev = mirror->rdev->bdev;
808 read_bio->bi_end_io = raid1_end_read_request;
809 read_bio->bi_rw = READ;
810 read_bio->bi_private = r1_bio;
811
812 generic_make_request(read_bio);
813 return 0;
814 }
815
816 /*
817 * WRITE:
818 */
819 /* first select target devices under spinlock and
820 * inc refcount on their rdev. Record them by setting
821 * bios[x] to bio
822 */
823 disks = conf->raid_disks;
NeilBrown191ea9b2005-06-21 17:17:23 -0700824#if 0
825 { static int first=1;
826 if (first) printk("First Write sector %llu disks %d\n",
827 (unsigned long long)r1_bio->sector, disks);
828 first = 0;
829 }
830#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 rcu_read_lock();
832 for (i = 0; i < disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800833 if ((rdev=rcu_dereference(conf->mirrors[i].rdev)) != NULL &&
NeilBrownb2d444d2005-11-08 21:39:31 -0800834 !test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 atomic_inc(&rdev->nr_pending);
NeilBrownb2d444d2005-11-08 21:39:31 -0800836 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 atomic_dec(&rdev->nr_pending);
838 r1_bio->bios[i] = NULL;
839 } else
840 r1_bio->bios[i] = bio;
NeilBrown191ea9b2005-06-21 17:17:23 -0700841 targets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 } else
843 r1_bio->bios[i] = NULL;
844 }
845 rcu_read_unlock();
846
NeilBrown4b6d2872005-09-09 16:23:47 -0700847 BUG_ON(targets == 0); /* we never fail the last device */
848
NeilBrown191ea9b2005-06-21 17:17:23 -0700849 if (targets < conf->raid_disks) {
850 /* array is degraded, we will not clear the bitmap
851 * on I/O completion (see raid1_end_write_request) */
852 set_bit(R1BIO_Degraded, &r1_bio->state);
853 }
NeilBrown06d91a52005-06-21 17:17:12 -0700854
NeilBrown4b6d2872005-09-09 16:23:47 -0700855 /* do behind I/O ? */
856 if (bitmap &&
857 atomic_read(&bitmap->behind_writes) < bitmap->max_write_behind &&
858 (behind_pages = alloc_behind_pages(bio)) != NULL)
859 set_bit(R1BIO_BehindIO, &r1_bio->state);
860
NeilBrown191ea9b2005-06-21 17:17:23 -0700861 atomic_set(&r1_bio->remaining, 0);
NeilBrown4b6d2872005-09-09 16:23:47 -0700862 atomic_set(&r1_bio->behind_remaining, 0);
NeilBrown191ea9b2005-06-21 17:17:23 -0700863
NeilBrowna9701a32005-11-08 21:39:34 -0800864 do_barriers = bio->bi_rw & BIO_RW_BARRIER;
865 if (do_barriers)
866 set_bit(R1BIO_Barrier, &r1_bio->state);
867
NeilBrown191ea9b2005-06-21 17:17:23 -0700868 bio_list_init(&bl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 for (i = 0; i < disks; i++) {
870 struct bio *mbio;
871 if (!r1_bio->bios[i])
872 continue;
873
874 mbio = bio_clone(bio, GFP_NOIO);
875 r1_bio->bios[i] = mbio;
876
877 mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
878 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
879 mbio->bi_end_io = raid1_end_write_request;
NeilBrowna9701a32005-11-08 21:39:34 -0800880 mbio->bi_rw = WRITE | do_barriers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 mbio->bi_private = r1_bio;
882
NeilBrown4b6d2872005-09-09 16:23:47 -0700883 if (behind_pages) {
884 struct bio_vec *bvec;
885 int j;
886
887 /* Yes, I really want the '__' version so that
888 * we clear any unused pointer in the io_vec, rather
889 * than leave them unchanged. This is important
890 * because when we come to free the pages, we won't
891 * know the originial bi_idx, so we just free
892 * them all
893 */
894 __bio_for_each_segment(bvec, mbio, j, 0)
895 bvec->bv_page = behind_pages[j];
896 if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
897 atomic_inc(&r1_bio->behind_remaining);
898 }
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 atomic_inc(&r1_bio->remaining);
NeilBrown191ea9b2005-06-21 17:17:23 -0700901
902 bio_list_add(&bl, mbio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
NeilBrown4b6d2872005-09-09 16:23:47 -0700904 kfree(behind_pages); /* the behind pages are attached to the bios now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
NeilBrown4b6d2872005-09-09 16:23:47 -0700906 bitmap_startwrite(bitmap, bio->bi_sector, r1_bio->sectors,
907 test_bit(R1BIO_BehindIO, &r1_bio->state));
NeilBrown191ea9b2005-06-21 17:17:23 -0700908 spin_lock_irqsave(&conf->device_lock, flags);
909 bio_list_merge(&conf->pending_bio_list, &bl);
910 bio_list_init(&bl);
911
912 blk_plug_device(mddev->queue);
913 spin_unlock_irqrestore(&conf->device_lock, flags);
914
915#if 0
916 while ((bio = bio_list_pop(&bl)) != NULL)
917 generic_make_request(bio);
918#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 return 0;
921}
922
923static void status(struct seq_file *seq, mddev_t *mddev)
924{
925 conf_t *conf = mddev_to_conf(mddev);
926 int i;
927
928 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
929 conf->working_disks);
930 for (i = 0; i < conf->raid_disks; i++)
931 seq_printf(seq, "%s",
932 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -0800933 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 seq_printf(seq, "]");
935}
936
937
938static void error(mddev_t *mddev, mdk_rdev_t *rdev)
939{
940 char b[BDEVNAME_SIZE];
941 conf_t *conf = mddev_to_conf(mddev);
942
943 /*
944 * If it is not operational, then we have already marked it as dead
945 * else if it is the last working disks, ignore the error, let the
946 * next level up know.
947 * else mark the drive as failed
948 */
NeilBrownb2d444d2005-11-08 21:39:31 -0800949 if (test_bit(In_sync, &rdev->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 && conf->working_disks == 1)
951 /*
952 * Don't fail the drive, act as though we were just a
953 * normal single drive
954 */
955 return;
NeilBrownb2d444d2005-11-08 21:39:31 -0800956 if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 mddev->degraded++;
958 conf->working_disks--;
959 /*
960 * if recovery is running, make sure it aborts.
961 */
962 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
963 }
NeilBrownb2d444d2005-11-08 21:39:31 -0800964 clear_bit(In_sync, &rdev->flags);
965 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 mddev->sb_dirty = 1;
967 printk(KERN_ALERT "raid1: Disk failure on %s, disabling device. \n"
968 " Operation continuing on %d devices\n",
969 bdevname(rdev->bdev,b), conf->working_disks);
970}
971
972static void print_conf(conf_t *conf)
973{
974 int i;
975 mirror_info_t *tmp;
976
977 printk("RAID1 conf printout:\n");
978 if (!conf) {
979 printk("(!conf)\n");
980 return;
981 }
982 printk(" --- wd:%d rd:%d\n", conf->working_disks,
983 conf->raid_disks);
984
985 for (i = 0; i < conf->raid_disks; i++) {
986 char b[BDEVNAME_SIZE];
987 tmp = conf->mirrors + i;
988 if (tmp->rdev)
989 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -0800990 i, !test_bit(In_sync, &tmp->rdev->flags), !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 bdevname(tmp->rdev->bdev,b));
992 }
993}
994
995static void close_sync(conf_t *conf)
996{
NeilBrown17999be2006-01-06 00:20:12 -0800997 wait_barrier(conf);
998 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 mempool_destroy(conf->r1buf_pool);
1001 conf->r1buf_pool = NULL;
1002}
1003
1004static int raid1_spare_active(mddev_t *mddev)
1005{
1006 int i;
1007 conf_t *conf = mddev->private;
1008 mirror_info_t *tmp;
1009
1010 /*
1011 * Find all failed disks within the RAID1 configuration
1012 * and mark them readable
1013 */
1014 for (i = 0; i < conf->raid_disks; i++) {
1015 tmp = conf->mirrors + i;
1016 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08001017 && !test_bit(Faulty, &tmp->rdev->flags)
1018 && !test_bit(In_sync, &tmp->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 conf->working_disks++;
1020 mddev->degraded--;
NeilBrownb2d444d2005-11-08 21:39:31 -08001021 set_bit(In_sync, &tmp->rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
1023 }
1024
1025 print_conf(conf);
1026 return 0;
1027}
1028
1029
1030static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1031{
1032 conf_t *conf = mddev->private;
1033 int found = 0;
NeilBrown41158c72005-06-21 17:17:25 -07001034 int mirror = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 mirror_info_t *p;
1036
1037 for (mirror=0; mirror < mddev->raid_disks; mirror++)
1038 if ( !(p=conf->mirrors+mirror)->rdev) {
1039
1040 blk_queue_stack_limits(mddev->queue,
1041 rdev->bdev->bd_disk->queue);
1042 /* as we don't honour merge_bvec_fn, we must never risk
1043 * violating it, so limit ->max_sector to one PAGE, as
1044 * a one page request is never in violation.
1045 */
1046 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1047 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1048 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
1049
1050 p->head_position = 0;
1051 rdev->raid_disk = mirror;
1052 found = 1;
NeilBrown6aea114a2005-11-28 13:44:13 -08001053 /* As all devices are equivalent, we don't need a full recovery
1054 * if this was recently any drive of the array
1055 */
1056 if (rdev->saved_raid_disk < 0)
NeilBrown41158c72005-06-21 17:17:25 -07001057 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001058 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 break;
1060 }
1061
1062 print_conf(conf);
1063 return found;
1064}
1065
1066static int raid1_remove_disk(mddev_t *mddev, int number)
1067{
1068 conf_t *conf = mddev->private;
1069 int err = 0;
1070 mdk_rdev_t *rdev;
1071 mirror_info_t *p = conf->mirrors+ number;
1072
1073 print_conf(conf);
1074 rdev = p->rdev;
1075 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001076 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 atomic_read(&rdev->nr_pending)) {
1078 err = -EBUSY;
1079 goto abort;
1080 }
1081 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001082 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (atomic_read(&rdev->nr_pending)) {
1084 /* lost the race, try later */
1085 err = -EBUSY;
1086 p->rdev = rdev;
1087 }
1088 }
1089abort:
1090
1091 print_conf(conf);
1092 return err;
1093}
1094
1095
1096static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
1097{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
NeilBrownd11c1712006-01-06 00:20:26 -08001099 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 if (bio->bi_size)
1102 return 1;
1103
NeilBrownd11c1712006-01-06 00:20:26 -08001104 for (i=r1_bio->mddev->raid_disks; i--; )
1105 if (r1_bio->bios[i] == bio)
1106 break;
1107 BUG_ON(i < 0);
1108 update_head_pos(i, r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 /*
1110 * we have read a block, now it needs to be re-written,
1111 * or re-read if the read failed.
1112 * We don't do much here, just schedule handling by raid1d
1113 */
NeilBrown69382e82006-01-06 00:20:22 -08001114 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 set_bit(R1BIO_Uptodate, &r1_bio->state);
NeilBrownd11c1712006-01-06 00:20:26 -08001116
1117 if (atomic_dec_and_test(&r1_bio->remaining))
1118 reschedule_retry(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return 0;
1120}
1121
1122static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
1123{
1124 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1125 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
1126 mddev_t *mddev = r1_bio->mddev;
1127 conf_t *conf = mddev_to_conf(mddev);
1128 int i;
1129 int mirror=0;
1130
1131 if (bio->bi_size)
1132 return 1;
1133
1134 for (i = 0; i < conf->raid_disks; i++)
1135 if (r1_bio->bios[i] == bio) {
1136 mirror = i;
1137 break;
1138 }
NeilBrowne3b97032005-08-04 12:53:34 -07001139 if (!uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 md_error(mddev, conf->mirrors[mirror].rdev);
NeilBrowne3b97032005-08-04 12:53:34 -07001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 update_head_pos(mirror, r1_bio);
1143
1144 if (atomic_dec_and_test(&r1_bio->remaining)) {
1145 md_done_sync(mddev, r1_bio->sectors, uptodate);
1146 put_buf(r1_bio);
1147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return 0;
1149}
1150
1151static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
1152{
1153 conf_t *conf = mddev_to_conf(mddev);
1154 int i;
1155 int disks = conf->raid_disks;
1156 struct bio *bio, *wbio;
1157
1158 bio = r1_bio->bios[r1_bio->read_disk];
1159
NeilBrown69382e82006-01-06 00:20:22 -08001160
NeilBrownd11c1712006-01-06 00:20:26 -08001161 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1162 /* We have read all readable devices. If we haven't
1163 * got the block, then there is no hope left.
1164 * If we have, then we want to do a comparison
1165 * and skip the write if everything is the same.
1166 * If any blocks failed to read, then we need to
1167 * attempt an over-write
1168 */
1169 int primary;
1170 if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
1171 for (i=0; i<mddev->raid_disks; i++)
1172 if (r1_bio->bios[i]->bi_end_io == end_sync_read)
1173 md_error(mddev, conf->mirrors[i].rdev);
1174
1175 md_done_sync(mddev, r1_bio->sectors, 1);
1176 put_buf(r1_bio);
1177 return;
1178 }
1179 for (primary=0; primary<mddev->raid_disks; primary++)
1180 if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
1181 test_bit(BIO_UPTODATE, &r1_bio->bios[primary]->bi_flags)) {
1182 r1_bio->bios[primary]->bi_end_io = NULL;
1183 break;
1184 }
1185 r1_bio->read_disk = primary;
1186 for (i=0; i<mddev->raid_disks; i++)
1187 if (r1_bio->bios[i]->bi_end_io == end_sync_read &&
1188 test_bit(BIO_UPTODATE, &r1_bio->bios[i]->bi_flags)) {
1189 int j;
1190 int vcnt = r1_bio->sectors >> (PAGE_SHIFT- 9);
1191 struct bio *pbio = r1_bio->bios[primary];
1192 struct bio *sbio = r1_bio->bios[i];
1193 for (j = vcnt; j-- ; )
1194 if (memcmp(page_address(pbio->bi_io_vec[j].bv_page),
1195 page_address(sbio->bi_io_vec[j].bv_page),
1196 PAGE_SIZE))
1197 break;
1198 if (j >= 0)
1199 mddev->resync_mismatches += r1_bio->sectors;
1200 if (j < 0 || test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1201 sbio->bi_end_io = NULL;
1202 else {
1203 /* fixup the bio for reuse */
1204 sbio->bi_vcnt = vcnt;
1205 sbio->bi_size = r1_bio->sectors << 9;
1206 sbio->bi_idx = 0;
1207 sbio->bi_phys_segments = 0;
1208 sbio->bi_hw_segments = 0;
1209 sbio->bi_hw_front_size = 0;
1210 sbio->bi_hw_back_size = 0;
1211 sbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1212 sbio->bi_flags |= 1 << BIO_UPTODATE;
1213 sbio->bi_next = NULL;
1214 sbio->bi_sector = r1_bio->sector +
1215 conf->mirrors[i].rdev->data_offset;
1216 sbio->bi_bdev = conf->mirrors[i].rdev->bdev;
1217 }
1218 }
1219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
NeilBrown69382e82006-01-06 00:20:22 -08001221 /* ouch - failed to read all of that.
1222 * Try some synchronous reads of other devices to get
1223 * good data, much like with normal read errors. Only
1224 * read into the pages we already have so they we don't
1225 * need to re-issue the read request.
1226 * We don't need to freeze the array, because being in an
1227 * active sync request, there is no normal IO, and
1228 * no overlapping syncs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 */
NeilBrown69382e82006-01-06 00:20:22 -08001230 sector_t sect = r1_bio->sector;
1231 int sectors = r1_bio->sectors;
1232 int idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
NeilBrown69382e82006-01-06 00:20:22 -08001234 while(sectors) {
1235 int s = sectors;
1236 int d = r1_bio->read_disk;
1237 int success = 0;
1238 mdk_rdev_t *rdev;
1239
1240 if (s > (PAGE_SIZE>>9))
1241 s = PAGE_SIZE >> 9;
1242 do {
1243 if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
1244 rdev = conf->mirrors[d].rdev;
1245 if (sync_page_io(rdev->bdev,
1246 sect + rdev->data_offset,
1247 s<<9,
1248 bio->bi_io_vec[idx].bv_page,
1249 READ)) {
1250 success = 1;
1251 break;
1252 }
1253 }
1254 d++;
1255 if (d == conf->raid_disks)
1256 d = 0;
1257 } while (!success && d != r1_bio->read_disk);
1258
1259 if (success) {
1260 /* write it back and re-read */
1261 set_bit(R1BIO_Uptodate, &r1_bio->state);
1262 while (d != r1_bio->read_disk) {
1263 if (d == 0)
1264 d = conf->raid_disks;
1265 d--;
1266 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1267 continue;
1268 rdev = conf->mirrors[d].rdev;
1269 if (sync_page_io(rdev->bdev,
1270 sect + rdev->data_offset,
1271 s<<9,
1272 bio->bi_io_vec[idx].bv_page,
1273 WRITE) == 0 ||
1274 sync_page_io(rdev->bdev,
1275 sect + rdev->data_offset,
1276 s<<9,
1277 bio->bi_io_vec[idx].bv_page,
1278 READ) == 0) {
1279 md_error(mddev, rdev);
1280 }
1281 }
1282 } else {
1283 char b[BDEVNAME_SIZE];
1284 /* Cannot read from anywhere, array is toast */
1285 md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
1286 printk(KERN_ALERT "raid1: %s: unrecoverable I/O read error"
1287 " for block %llu\n",
1288 bdevname(bio->bi_bdev,b),
1289 (unsigned long long)r1_bio->sector);
1290 md_done_sync(mddev, r1_bio->sectors, 0);
1291 put_buf(r1_bio);
1292 return;
1293 }
1294 sectors -= s;
1295 sect += s;
1296 idx ++;
1297 }
1298 }
NeilBrownd11c1712006-01-06 00:20:26 -08001299
1300 /*
1301 * schedule writes
1302 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 atomic_set(&r1_bio->remaining, 1);
1304 for (i = 0; i < disks ; i++) {
1305 wbio = r1_bio->bios[i];
NeilBrown3e198f72006-01-06 00:20:21 -08001306 if (wbio->bi_end_io == NULL ||
1307 (wbio->bi_end_io == end_sync_read &&
1308 (i == r1_bio->read_disk ||
1309 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 continue;
1311
NeilBrown3e198f72006-01-06 00:20:21 -08001312 wbio->bi_rw = WRITE;
1313 wbio->bi_end_io = end_sync_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 atomic_inc(&r1_bio->remaining);
1315 md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
NeilBrown191ea9b2005-06-21 17:17:23 -07001316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 generic_make_request(wbio);
1318 }
1319
1320 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001321 /* if we're here, all write(s) have completed, so clean up */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 md_done_sync(mddev, r1_bio->sectors, 1);
1323 put_buf(r1_bio);
1324 }
1325}
1326
1327/*
1328 * This is a kernel thread which:
1329 *
1330 * 1. Retries failed read operations on working mirrors.
1331 * 2. Updates the raid superblock when problems encounter.
1332 * 3. Performs writes following reads for array syncronising.
1333 */
1334
1335static void raid1d(mddev_t *mddev)
1336{
1337 r1bio_t *r1_bio;
1338 struct bio *bio;
1339 unsigned long flags;
1340 conf_t *conf = mddev_to_conf(mddev);
1341 struct list_head *head = &conf->retry_list;
1342 int unplug=0;
1343 mdk_rdev_t *rdev;
1344
1345 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 for (;;) {
1348 char b[BDEVNAME_SIZE];
1349 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown191ea9b2005-06-21 17:17:23 -07001350
1351 if (conf->pending_bio_list.head) {
1352 bio = bio_list_get(&conf->pending_bio_list);
1353 blk_remove_plug(mddev->queue);
1354 spin_unlock_irqrestore(&conf->device_lock, flags);
1355 /* flush any pending bitmap writes to disk before proceeding w/ I/O */
1356 if (bitmap_unplug(mddev->bitmap) != 0)
1357 printk("%s: bitmap file write failed!\n", mdname(mddev));
1358
1359 while (bio) { /* submit pending writes */
1360 struct bio *next = bio->bi_next;
1361 bio->bi_next = NULL;
1362 generic_make_request(bio);
1363 bio = next;
1364 }
1365 unplug = 1;
1366
1367 continue;
1368 }
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (list_empty(head))
1371 break;
1372 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
1373 list_del(head->prev);
NeilBrownddaf22a2006-01-06 00:20:19 -08001374 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 spin_unlock_irqrestore(&conf->device_lock, flags);
1376
1377 mddev = r1_bio->mddev;
1378 conf = mddev_to_conf(mddev);
1379 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
1380 sync_request_write(mddev, r1_bio);
1381 unplug = 1;
NeilBrowna9701a32005-11-08 21:39:34 -08001382 } else if (test_bit(R1BIO_BarrierRetry, &r1_bio->state)) {
1383 /* some requests in the r1bio were BIO_RW_BARRIER
1384 * requests which failed with -ENOTSUPP. Hohumm..
1385 * Better resubmit without the barrier.
1386 * We know which devices to resubmit for, because
1387 * all others have had their bios[] entry cleared.
1388 */
1389 int i;
1390 clear_bit(R1BIO_BarrierRetry, &r1_bio->state);
1391 clear_bit(R1BIO_Barrier, &r1_bio->state);
1392 for (i=0; i < conf->raid_disks; i++)
1393 if (r1_bio->bios[i]) {
1394 struct bio_vec *bvec;
1395 int j;
1396
1397 bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
1398 /* copy pages from the failed bio, as
1399 * this might be a write-behind device */
1400 __bio_for_each_segment(bvec, bio, j, 0)
1401 bvec->bv_page = bio_iovec_idx(r1_bio->bios[i], j)->bv_page;
1402 bio_put(r1_bio->bios[i]);
1403 bio->bi_sector = r1_bio->sector +
1404 conf->mirrors[i].rdev->data_offset;
1405 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1406 bio->bi_end_io = raid1_end_write_request;
1407 bio->bi_rw = WRITE;
1408 bio->bi_private = r1_bio;
1409 r1_bio->bios[i] = bio;
1410 generic_make_request(bio);
1411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 } else {
1413 int disk;
NeilBrownddaf22a2006-01-06 00:20:19 -08001414
1415 /* we got a read error. Maybe the drive is bad. Maybe just
1416 * the block and we can fix it.
1417 * We freeze all other IO, and try reading the block from
1418 * other devices. When we find one, we re-write
1419 * and check it that fixes the read error.
1420 * This is all done synchronously while the array is
1421 * frozen
1422 */
1423 sector_t sect = r1_bio->sector;
1424 int sectors = r1_bio->sectors;
1425 freeze_array(conf);
NeilBrowncf30a472006-01-06 00:20:23 -08001426 if (mddev->ro == 0) while(sectors) {
NeilBrownddaf22a2006-01-06 00:20:19 -08001427 int s = sectors;
1428 int d = r1_bio->read_disk;
1429 int success = 0;
1430
1431 if (s > (PAGE_SIZE>>9))
1432 s = PAGE_SIZE >> 9;
1433
1434 do {
1435 rdev = conf->mirrors[d].rdev;
1436 if (rdev &&
1437 test_bit(In_sync, &rdev->flags) &&
1438 sync_page_io(rdev->bdev,
1439 sect + rdev->data_offset,
1440 s<<9,
1441 conf->tmppage, READ))
1442 success = 1;
1443 else {
1444 d++;
1445 if (d == conf->raid_disks)
1446 d = 0;
1447 }
1448 } while (!success && d != r1_bio->read_disk);
1449
1450 if (success) {
1451 /* write it back and re-read */
1452 while (d != r1_bio->read_disk) {
1453 if (d==0)
1454 d = conf->raid_disks;
1455 d--;
1456 rdev = conf->mirrors[d].rdev;
1457 if (rdev &&
1458 test_bit(In_sync, &rdev->flags)) {
1459 if (sync_page_io(rdev->bdev,
1460 sect + rdev->data_offset,
1461 s<<9, conf->tmppage, WRITE) == 0 ||
1462 sync_page_io(rdev->bdev,
1463 sect + rdev->data_offset,
1464 s<<9, conf->tmppage, READ) == 0) {
1465 /* Well, this device is dead */
1466 md_error(mddev, rdev);
1467 }
1468 }
1469 }
1470 } else {
1471 /* Cannot read from anywhere -- bye bye array */
1472 md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
1473 break;
1474 }
1475 sectors -= s;
1476 sect += s;
1477 }
1478
NeilBrownddaf22a2006-01-06 00:20:19 -08001479 unfreeze_array(conf);
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 bio = r1_bio->bios[r1_bio->read_disk];
1482 if ((disk=read_balance(conf, r1_bio)) == -1) {
1483 printk(KERN_ALERT "raid1: %s: unrecoverable I/O"
1484 " read error for block %llu\n",
1485 bdevname(bio->bi_bdev,b),
1486 (unsigned long long)r1_bio->sector);
1487 raid_end_bio_io(r1_bio);
1488 } else {
NeilBrowncf30a472006-01-06 00:20:23 -08001489 r1_bio->bios[r1_bio->read_disk] =
1490 mddev->ro ? IO_BLOCKED : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 r1_bio->read_disk = disk;
1492 bio_put(bio);
1493 bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
1494 r1_bio->bios[r1_bio->read_disk] = bio;
1495 rdev = conf->mirrors[disk].rdev;
1496 if (printk_ratelimit())
1497 printk(KERN_ERR "raid1: %s: redirecting sector %llu to"
1498 " another mirror\n",
1499 bdevname(rdev->bdev,b),
1500 (unsigned long long)r1_bio->sector);
1501 bio->bi_sector = r1_bio->sector + rdev->data_offset;
1502 bio->bi_bdev = rdev->bdev;
1503 bio->bi_end_io = raid1_end_read_request;
1504 bio->bi_rw = READ;
1505 bio->bi_private = r1_bio;
1506 unplug = 1;
1507 generic_make_request(bio);
1508 }
1509 }
1510 }
1511 spin_unlock_irqrestore(&conf->device_lock, flags);
1512 if (unplug)
1513 unplug_slaves(mddev);
1514}
1515
1516
1517static int init_resync(conf_t *conf)
1518{
1519 int buffs;
1520
1521 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
1522 if (conf->r1buf_pool)
1523 BUG();
1524 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
1525 conf->poolinfo);
1526 if (!conf->r1buf_pool)
1527 return -ENOMEM;
1528 conf->next_resync = 0;
1529 return 0;
1530}
1531
1532/*
1533 * perform a "sync" on one "block"
1534 *
1535 * We need to make sure that no normal I/O request - particularly write
1536 * requests - conflict with active sync requests.
1537 *
1538 * This is achieved by tracking pending requests and a 'barrier' concept
1539 * that can be installed to exclude normal IO requests.
1540 */
1541
NeilBrown57afd892005-06-21 17:17:13 -07001542static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
1544 conf_t *conf = mddev_to_conf(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 r1bio_t *r1_bio;
1546 struct bio *bio;
1547 sector_t max_sector, nr_sectors;
NeilBrown3e198f72006-01-06 00:20:21 -08001548 int disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 int i;
NeilBrown3e198f72006-01-06 00:20:21 -08001550 int wonly = -1;
1551 int write_targets = 0, read_targets = 0;
NeilBrown191ea9b2005-06-21 17:17:23 -07001552 int sync_blocks;
NeilBrowne3b97032005-08-04 12:53:34 -07001553 int still_degraded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555 if (!conf->r1buf_pool)
NeilBrown191ea9b2005-06-21 17:17:23 -07001556 {
1557/*
1558 printk("sync start - bitmap %p\n", mddev->bitmap);
1559*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07001561 return 0;
NeilBrown191ea9b2005-06-21 17:17:23 -07001562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 max_sector = mddev->size << 1;
1565 if (sector_nr >= max_sector) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001566 /* If we aborted, we need to abort the
1567 * sync on the 'current' bitmap chunk (there will
1568 * only be one in raid1 resync.
1569 * We can find the current addess in mddev->curr_resync
1570 */
NeilBrown6a806c52005-07-15 03:56:35 -07001571 if (mddev->curr_resync < max_sector) /* aborted */
1572 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
NeilBrown191ea9b2005-06-21 17:17:23 -07001573 &sync_blocks, 1);
NeilBrown6a806c52005-07-15 03:56:35 -07001574 else /* completed sync */
NeilBrown191ea9b2005-06-21 17:17:23 -07001575 conf->fullsync = 0;
NeilBrown6a806c52005-07-15 03:56:35 -07001576
1577 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 close_sync(conf);
1579 return 0;
1580 }
1581
NeilBrowne3b97032005-08-04 12:53:34 -07001582 /* before building a request, check if we can skip these blocks..
1583 * This call the bitmap_start_sync doesn't actually record anything
1584 */
1585 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrowne5de485f2005-11-08 21:39:38 -08001586 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001587 /* We can skip this block, and probably several more */
1588 *skipped = 1;
1589 return sync_blocks;
1590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 /*
NeilBrown17999be2006-01-06 00:20:12 -08001592 * If there is non-resync activity waiting for a turn,
1593 * and resync is going fast enough,
1594 * then let it though before starting on this new sync request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 */
NeilBrown17999be2006-01-06 00:20:12 -08001596 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 msleep_interruptible(1000);
NeilBrown17999be2006-01-06 00:20:12 -08001598
1599 raise_barrier(conf);
1600
1601 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
NeilBrown3e198f72006-01-06 00:20:21 -08001604 rcu_read_lock();
1605 /*
1606 * If we get a correctably read error during resync or recovery,
1607 * we might want to read from a different device. So we
1608 * flag all drives that could conceivably be read from for READ,
1609 * and any others (which will be non-In_sync devices) for WRITE.
1610 * If a read fails, we try reading from something else for which READ
1611 * is OK.
1612 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 r1_bio->mddev = mddev;
1615 r1_bio->sector = sector_nr;
NeilBrown191ea9b2005-06-21 17:17:23 -07001616 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 set_bit(R1BIO_IsSync, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 for (i=0; i < conf->raid_disks; i++) {
NeilBrown3e198f72006-01-06 00:20:21 -08001620 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 bio = r1_bio->bios[i];
1622
1623 /* take from bio_init */
1624 bio->bi_next = NULL;
1625 bio->bi_flags |= 1 << BIO_UPTODATE;
1626 bio->bi_rw = 0;
1627 bio->bi_vcnt = 0;
1628 bio->bi_idx = 0;
1629 bio->bi_phys_segments = 0;
1630 bio->bi_hw_segments = 0;
1631 bio->bi_size = 0;
1632 bio->bi_end_io = NULL;
1633 bio->bi_private = NULL;
1634
NeilBrown3e198f72006-01-06 00:20:21 -08001635 rdev = rcu_dereference(conf->mirrors[i].rdev);
1636 if (rdev == NULL ||
1637 test_bit(Faulty, &rdev->flags)) {
NeilBrowne3b97032005-08-04 12:53:34 -07001638 still_degraded = 1;
1639 continue;
NeilBrown3e198f72006-01-06 00:20:21 -08001640 } else if (!test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 bio->bi_rw = WRITE;
1642 bio->bi_end_io = end_sync_write;
1643 write_targets ++;
NeilBrown3e198f72006-01-06 00:20:21 -08001644 } else {
1645 /* may need to read from here */
1646 bio->bi_rw = READ;
1647 bio->bi_end_io = end_sync_read;
1648 if (test_bit(WriteMostly, &rdev->flags)) {
1649 if (wonly < 0)
1650 wonly = i;
1651 } else {
1652 if (disk < 0)
1653 disk = i;
1654 }
1655 read_targets++;
1656 }
1657 atomic_inc(&rdev->nr_pending);
1658 bio->bi_sector = sector_nr + rdev->data_offset;
1659 bio->bi_bdev = rdev->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 bio->bi_private = r1_bio;
1661 }
NeilBrown3e198f72006-01-06 00:20:21 -08001662 rcu_read_unlock();
1663 if (disk < 0)
1664 disk = wonly;
1665 r1_bio->read_disk = disk;
NeilBrown191ea9b2005-06-21 17:17:23 -07001666
NeilBrown3e198f72006-01-06 00:20:21 -08001667 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
1668 /* extra read targets are also write targets */
1669 write_targets += read_targets-1;
1670
1671 if (write_targets == 0 || read_targets == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 /* There is nowhere to write, so all non-sync
1673 * drives must be failed - so we are finished
1674 */
NeilBrown57afd892005-06-21 17:17:13 -07001675 sector_t rv = max_sector - sector_nr;
1676 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 put_buf(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 return rv;
1679 }
1680
1681 nr_sectors = 0;
NeilBrown289e99e2005-06-21 17:17:24 -07001682 sync_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 do {
1684 struct page *page;
1685 int len = PAGE_SIZE;
1686 if (sector_nr + (len>>9) > max_sector)
1687 len = (max_sector - sector_nr) << 9;
1688 if (len == 0)
1689 break;
NeilBrown6a806c52005-07-15 03:56:35 -07001690 if (sync_blocks == 0) {
1691 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
NeilBrowne5de485f2005-11-08 21:39:38 -08001692 &sync_blocks, still_degraded) &&
1693 !conf->fullsync &&
1694 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
NeilBrown6a806c52005-07-15 03:56:35 -07001695 break;
1696 if (sync_blocks < (PAGE_SIZE>>9))
1697 BUG();
1698 if (len > (sync_blocks<<9))
1699 len = sync_blocks<<9;
NeilBrownab7a30c2005-06-21 17:17:23 -07001700 }
NeilBrown191ea9b2005-06-21 17:17:23 -07001701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 for (i=0 ; i < conf->raid_disks; i++) {
1703 bio = r1_bio->bios[i];
1704 if (bio->bi_end_io) {
NeilBrownd11c1712006-01-06 00:20:26 -08001705 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (bio_add_page(bio, page, len, 0) == 0) {
1707 /* stop here */
NeilBrownd11c1712006-01-06 00:20:26 -08001708 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 while (i > 0) {
1710 i--;
1711 bio = r1_bio->bios[i];
NeilBrown6a806c52005-07-15 03:56:35 -07001712 if (bio->bi_end_io==NULL)
1713 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 /* remove last page from this bio */
1715 bio->bi_vcnt--;
1716 bio->bi_size -= len;
1717 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
1718 }
1719 goto bio_full;
1720 }
1721 }
1722 }
1723 nr_sectors += len>>9;
1724 sector_nr += len>>9;
NeilBrown191ea9b2005-06-21 17:17:23 -07001725 sync_blocks -= (len>>9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
1727 bio_full:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 r1_bio->sectors = nr_sectors;
1729
NeilBrownd11c1712006-01-06 00:20:26 -08001730 /* For a user-requested sync, we read all readable devices and do a
1731 * compare
1732 */
1733 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1734 atomic_set(&r1_bio->remaining, read_targets);
1735 for (i=0; i<conf->raid_disks; i++) {
1736 bio = r1_bio->bios[i];
1737 if (bio->bi_end_io == end_sync_read) {
1738 md_sync_acct(conf->mirrors[i].rdev->bdev, nr_sectors);
1739 generic_make_request(bio);
1740 }
1741 }
1742 } else {
1743 atomic_set(&r1_bio->remaining, 1);
1744 bio = r1_bio->bios[r1_bio->read_disk];
1745 md_sync_acct(conf->mirrors[r1_bio->read_disk].rdev->bdev,
1746 nr_sectors);
1747 generic_make_request(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
NeilBrownd11c1712006-01-06 00:20:26 -08001749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 return nr_sectors;
1752}
1753
1754static int run(mddev_t *mddev)
1755{
1756 conf_t *conf;
1757 int i, j, disk_idx;
1758 mirror_info_t *disk;
1759 mdk_rdev_t *rdev;
1760 struct list_head *tmp;
1761
1762 if (mddev->level != 1) {
1763 printk("raid1: %s: raid level not set to mirroring (%d)\n",
1764 mdname(mddev), mddev->level);
1765 goto out;
1766 }
1767 /*
1768 * copy the already verified devices into our private RAID1
1769 * bookkeeping area. [whatever we allocate in run(),
1770 * should be freed in stop()]
1771 */
1772 conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
1773 mddev->private = conf;
1774 if (!conf)
1775 goto out_no_mem;
1776
1777 memset(conf, 0, sizeof(*conf));
1778 conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks,
1779 GFP_KERNEL);
1780 if (!conf->mirrors)
1781 goto out_no_mem;
1782
1783 memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
1784
NeilBrownddaf22a2006-01-06 00:20:19 -08001785 conf->tmppage = alloc_page(GFP_KERNEL);
1786 if (!conf->tmppage)
1787 goto out_no_mem;
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 conf->poolinfo = kmalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
1790 if (!conf->poolinfo)
1791 goto out_no_mem;
1792 conf->poolinfo->mddev = mddev;
1793 conf->poolinfo->raid_disks = mddev->raid_disks;
1794 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1795 r1bio_pool_free,
1796 conf->poolinfo);
1797 if (!conf->r1bio_pool)
1798 goto out_no_mem;
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 ITERATE_RDEV(mddev, rdev, tmp) {
1801 disk_idx = rdev->raid_disk;
1802 if (disk_idx >= mddev->raid_disks
1803 || disk_idx < 0)
1804 continue;
1805 disk = conf->mirrors + disk_idx;
1806
1807 disk->rdev = rdev;
1808
1809 blk_queue_stack_limits(mddev->queue,
1810 rdev->bdev->bd_disk->queue);
1811 /* as we don't honour merge_bvec_fn, we must never risk
1812 * violating it, so limit ->max_sector to one PAGE, as
1813 * a one page request is never in violation.
1814 */
1815 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1816 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1817 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
1818
1819 disk->head_position = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08001820 if (!test_bit(Faulty, &rdev->flags) && test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 conf->working_disks++;
1822 }
1823 conf->raid_disks = mddev->raid_disks;
1824 conf->mddev = mddev;
1825 spin_lock_init(&conf->device_lock);
1826 INIT_LIST_HEAD(&conf->retry_list);
1827 if (conf->working_disks == 1)
1828 mddev->recovery_cp = MaxSector;
1829
1830 spin_lock_init(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -08001831 init_waitqueue_head(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
NeilBrown191ea9b2005-06-21 17:17:23 -07001833 bio_list_init(&conf->pending_bio_list);
1834 bio_list_init(&conf->flushing_bio_list);
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (!conf->working_disks) {
1837 printk(KERN_ERR "raid1: no operational mirrors for %s\n",
1838 mdname(mddev));
1839 goto out_free_conf;
1840 }
1841
1842 mddev->degraded = 0;
1843 for (i = 0; i < conf->raid_disks; i++) {
1844
1845 disk = conf->mirrors + i;
1846
1847 if (!disk->rdev) {
1848 disk->head_position = 0;
1849 mddev->degraded++;
1850 }
1851 }
1852
1853 /*
1854 * find the first working one and use it as a starting point
1855 * to read balancing.
1856 */
1857 for (j = 0; j < conf->raid_disks &&
1858 (!conf->mirrors[j].rdev ||
NeilBrownb2d444d2005-11-08 21:39:31 -08001859 !test_bit(In_sync, &conf->mirrors[j].rdev->flags)) ; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 /* nothing */;
1861 conf->last_used = j;
1862
1863
NeilBrown191ea9b2005-06-21 17:17:23 -07001864 mddev->thread = md_register_thread(raid1d, mddev, "%s_raid1");
1865 if (!mddev->thread) {
1866 printk(KERN_ERR
1867 "raid1: couldn't allocate thread for %s\n",
1868 mdname(mddev));
1869 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 }
NeilBrown191ea9b2005-06-21 17:17:23 -07001871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 printk(KERN_INFO
1873 "raid1: raid set %s active with %d out of %d mirrors\n",
1874 mdname(mddev), mddev->raid_disks - mddev->degraded,
1875 mddev->raid_disks);
1876 /*
1877 * Ok, everything is just fine now
1878 */
1879 mddev->array_size = mddev->size;
1880
NeilBrown7a5febe2005-05-16 21:53:16 -07001881 mddev->queue->unplug_fn = raid1_unplug;
1882 mddev->queue->issue_flush_fn = raid1_issue_flush;
1883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return 0;
1885
1886out_no_mem:
1887 printk(KERN_ERR "raid1: couldn't allocate memory for %s\n",
1888 mdname(mddev));
1889
1890out_free_conf:
1891 if (conf) {
1892 if (conf->r1bio_pool)
1893 mempool_destroy(conf->r1bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07001894 kfree(conf->mirrors);
NeilBrownddaf22a2006-01-06 00:20:19 -08001895 __free_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07001896 kfree(conf->poolinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 kfree(conf);
1898 mddev->private = NULL;
1899 }
1900out:
1901 return -EIO;
1902}
1903
1904static int stop(mddev_t *mddev)
1905{
1906 conf_t *conf = mddev_to_conf(mddev);
NeilBrown4b6d2872005-09-09 16:23:47 -07001907 struct bitmap *bitmap = mddev->bitmap;
1908 int behind_wait = 0;
1909
1910 /* wait for behind writes to complete */
1911 while (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
1912 behind_wait++;
1913 printk(KERN_INFO "raid1: behind writes in progress on device %s, waiting to stop (%d)\n", mdname(mddev), behind_wait);
1914 set_current_state(TASK_UNINTERRUPTIBLE);
1915 schedule_timeout(HZ); /* wait a second */
1916 /* need to kick something here to make sure I/O goes? */
1917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 md_unregister_thread(mddev->thread);
1920 mddev->thread = NULL;
1921 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
1922 if (conf->r1bio_pool)
1923 mempool_destroy(conf->r1bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07001924 kfree(conf->mirrors);
1925 kfree(conf->poolinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 kfree(conf);
1927 mddev->private = NULL;
1928 return 0;
1929}
1930
1931static int raid1_resize(mddev_t *mddev, sector_t sectors)
1932{
1933 /* no resync is happening, and there is enough space
1934 * on all devices, so we can resize.
1935 * We need to make sure resync covers any new space.
1936 * If the array is shrinking we should possibly wait until
1937 * any io in the removed space completes, but it hardly seems
1938 * worth it.
1939 */
1940 mddev->array_size = sectors>>1;
1941 set_capacity(mddev->gendisk, mddev->array_size << 1);
1942 mddev->changed = 1;
1943 if (mddev->array_size > mddev->size && mddev->recovery_cp == MaxSector) {
1944 mddev->recovery_cp = mddev->size << 1;
1945 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1946 }
1947 mddev->size = mddev->array_size;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07001948 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 return 0;
1950}
1951
1952static int raid1_reshape(mddev_t *mddev, int raid_disks)
1953{
1954 /* We need to:
1955 * 1/ resize the r1bio_pool
1956 * 2/ resize conf->mirrors
1957 *
1958 * We allocate a new r1bio_pool if we can.
1959 * Then raise a device barrier and wait until all IO stops.
1960 * Then resize conf->mirrors and swap in the new r1bio pool.
NeilBrown6ea9c072005-06-21 17:17:09 -07001961 *
1962 * At the same time, we "pack" the devices so that all the missing
1963 * devices have the higher raid_disk numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 */
1965 mempool_t *newpool, *oldpool;
1966 struct pool_info *newpoolinfo;
1967 mirror_info_t *newmirrors;
1968 conf_t *conf = mddev_to_conf(mddev);
NeilBrown6ea9c072005-06-21 17:17:09 -07001969 int cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
NeilBrown6ea9c072005-06-21 17:17:09 -07001971 int d, d2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
NeilBrown6ea9c072005-06-21 17:17:09 -07001973 if (raid_disks < conf->raid_disks) {
1974 cnt=0;
1975 for (d= 0; d < conf->raid_disks; d++)
1976 if (conf->mirrors[d].rdev)
1977 cnt++;
1978 if (cnt > raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 return -EBUSY;
NeilBrown6ea9c072005-06-21 17:17:09 -07001980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
1983 if (!newpoolinfo)
1984 return -ENOMEM;
1985 newpoolinfo->mddev = mddev;
1986 newpoolinfo->raid_disks = raid_disks;
1987
1988 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1989 r1bio_pool_free, newpoolinfo);
1990 if (!newpool) {
1991 kfree(newpoolinfo);
1992 return -ENOMEM;
1993 }
1994 newmirrors = kmalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
1995 if (!newmirrors) {
1996 kfree(newpoolinfo);
1997 mempool_destroy(newpool);
1998 return -ENOMEM;
1999 }
2000 memset(newmirrors, 0, sizeof(struct mirror_info)*raid_disks);
2001
NeilBrown17999be2006-01-06 00:20:12 -08002002 raise_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 /* ok, everything is stopped */
2005 oldpool = conf->r1bio_pool;
2006 conf->r1bio_pool = newpool;
NeilBrown6ea9c072005-06-21 17:17:09 -07002007
2008 for (d=d2=0; d < conf->raid_disks; d++)
2009 if (conf->mirrors[d].rdev) {
2010 conf->mirrors[d].rdev->raid_disk = d2;
2011 newmirrors[d2++].rdev = conf->mirrors[d].rdev;
2012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 kfree(conf->mirrors);
2014 conf->mirrors = newmirrors;
2015 kfree(conf->poolinfo);
2016 conf->poolinfo = newpoolinfo;
2017
2018 mddev->degraded += (raid_disks - conf->raid_disks);
2019 conf->raid_disks = mddev->raid_disks = raid_disks;
2020
NeilBrown6ea9c072005-06-21 17:17:09 -07002021 conf->last_used = 0; /* just make sure it is in-range */
NeilBrown17999be2006-01-06 00:20:12 -08002022 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2025 md_wakeup_thread(mddev->thread);
2026
2027 mempool_destroy(oldpool);
2028 return 0;
2029}
2030
NeilBrown500af872005-09-09 16:23:58 -07002031static void raid1_quiesce(mddev_t *mddev, int state)
NeilBrown36fa3062005-09-09 16:23:45 -07002032{
2033 conf_t *conf = mddev_to_conf(mddev);
2034
2035 switch(state) {
NeilBrown9e6603d2005-09-09 16:23:48 -07002036 case 1:
NeilBrown17999be2006-01-06 00:20:12 -08002037 raise_barrier(conf);
NeilBrown36fa3062005-09-09 16:23:45 -07002038 break;
NeilBrown9e6603d2005-09-09 16:23:48 -07002039 case 0:
NeilBrown17999be2006-01-06 00:20:12 -08002040 lower_barrier(conf);
NeilBrown36fa3062005-09-09 16:23:45 -07002041 break;
2042 }
NeilBrown36fa3062005-09-09 16:23:45 -07002043}
2044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046static mdk_personality_t raid1_personality =
2047{
2048 .name = "raid1",
2049 .owner = THIS_MODULE,
2050 .make_request = make_request,
2051 .run = run,
2052 .stop = stop,
2053 .status = status,
2054 .error_handler = error,
2055 .hot_add_disk = raid1_add_disk,
2056 .hot_remove_disk= raid1_remove_disk,
2057 .spare_active = raid1_spare_active,
2058 .sync_request = sync_request,
2059 .resize = raid1_resize,
2060 .reshape = raid1_reshape,
NeilBrown36fa3062005-09-09 16:23:45 -07002061 .quiesce = raid1_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062};
2063
2064static int __init raid_init(void)
2065{
2066 return register_md_personality(RAID1, &raid1_personality);
2067}
2068
2069static void raid_exit(void)
2070{
2071 unregister_md_personality(RAID1);
2072}
2073
2074module_init(raid_init);
2075module_exit(raid_exit);
2076MODULE_LICENSE("GPL");
2077MODULE_ALIAS("md-personality-3"); /* RAID1 */