blob: b16d2ee5e9dd72d470fe12268ff1778655289b10 [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 *
Jan Engelhardt96de0e22007-10-19 23:21:04 +020012 * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * 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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110035#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
NeilBrownbff61972009-03-31 14:33:13 +110037#include <linux/seq_file.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100038#include <linux/ratelimit.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110039#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110040#include "raid1.h"
41#include "bitmap.h"
NeilBrown191ea9b2005-06-21 17:17:23 -070042
43#define DEBUG 0
NeilBrownd2eb35a2011-07-28 11:31:48 +100044#define PRINTK(x...) do { if (DEBUG) printk(x); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/*
47 * Number of guaranteed r1bios in case of extreme VM load:
48 */
49#define NR_RAID1_BIOS 256
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
NeilBrown17999be2006-01-06 00:20:12 -080052static void allow_barrier(conf_t *conf);
53static void lower_barrier(conf_t *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Al Virodd0fc662005-10-07 07:46:04 +010055static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 struct pool_info *pi = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 int size = offsetof(r1bio_t, bios[pi->raid_disks]);
59
60 /* allocate a r1bio with room for raid_disks entries in the bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +010061 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
64static void r1bio_pool_free(void *r1_bio, void *data)
65{
66 kfree(r1_bio);
67}
68
69#define RESYNC_BLOCK_SIZE (64*1024)
70//#define RESYNC_BLOCK_SIZE PAGE_SIZE
71#define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
72#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
73#define RESYNC_WINDOW (2048*1024)
74
Al Virodd0fc662005-10-07 07:46:04 +010075static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 struct pool_info *pi = data;
78 struct page *page;
79 r1bio_t *r1_bio;
80 struct bio *bio;
81 int i, j;
82
83 r1_bio = r1bio_pool_alloc(gfp_flags, pi);
Jens Axboe7eaceac2011-03-10 08:52:07 +010084 if (!r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 /*
88 * Allocate bios : 1 for reading, n-1 for writing
89 */
90 for (j = pi->raid_disks ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +110091 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (!bio)
93 goto out_free_bio;
94 r1_bio->bios[j] = bio;
95 }
96 /*
97 * Allocate RESYNC_PAGES data pages and attach them to
NeilBrownd11c1712006-01-06 00:20:26 -080098 * the first bio.
99 * If this is a user-requested check/repair, allocate
100 * RESYNC_PAGES for each bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
NeilBrownd11c1712006-01-06 00:20:26 -0800102 if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery))
103 j = pi->raid_disks;
104 else
105 j = 1;
106 while(j--) {
107 bio = r1_bio->bios[j];
108 for (i = 0; i < RESYNC_PAGES; i++) {
109 page = alloc_page(gfp_flags);
110 if (unlikely(!page))
111 goto out_free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
NeilBrownd11c1712006-01-06 00:20:26 -0800113 bio->bi_io_vec[i].bv_page = page;
NeilBrown303a0e12009-04-06 14:40:38 +1000114 bio->bi_vcnt = i+1;
NeilBrownd11c1712006-01-06 00:20:26 -0800115 }
116 }
117 /* If not user-requests, copy the page pointers to all bios */
118 if (!test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) {
119 for (i=0; i<RESYNC_PAGES ; i++)
120 for (j=1; j<pi->raid_disks; j++)
121 r1_bio->bios[j]->bi_io_vec[i].bv_page =
122 r1_bio->bios[0]->bi_io_vec[i].bv_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124
125 r1_bio->master_bio = NULL;
126
127 return r1_bio;
128
129out_free_pages:
NeilBrown303a0e12009-04-06 14:40:38 +1000130 for (j=0 ; j < pi->raid_disks; j++)
131 for (i=0; i < r1_bio->bios[j]->bi_vcnt ; i++)
132 put_page(r1_bio->bios[j]->bi_io_vec[i].bv_page);
NeilBrownd11c1712006-01-06 00:20:26 -0800133 j = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134out_free_bio:
135 while ( ++j < pi->raid_disks )
136 bio_put(r1_bio->bios[j]);
137 r1bio_pool_free(r1_bio, data);
138 return NULL;
139}
140
141static void r1buf_pool_free(void *__r1_bio, void *data)
142{
143 struct pool_info *pi = data;
NeilBrownd11c1712006-01-06 00:20:26 -0800144 int i,j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 r1bio_t *r1bio = __r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
NeilBrownd11c1712006-01-06 00:20:26 -0800147 for (i = 0; i < RESYNC_PAGES; i++)
148 for (j = pi->raid_disks; j-- ;) {
149 if (j == 0 ||
150 r1bio->bios[j]->bi_io_vec[i].bv_page !=
151 r1bio->bios[0]->bi_io_vec[i].bv_page)
NeilBrown1345b1d2006-01-06 00:20:40 -0800152 safe_put_page(r1bio->bios[j]->bi_io_vec[i].bv_page);
NeilBrownd11c1712006-01-06 00:20:26 -0800153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 for (i=0 ; i < pi->raid_disks; i++)
155 bio_put(r1bio->bios[i]);
156
157 r1bio_pool_free(r1bio, data);
158}
159
160static void put_all_bios(conf_t *conf, r1bio_t *r1_bio)
161{
162 int i;
163
164 for (i = 0; i < conf->raid_disks; i++) {
165 struct bio **bio = r1_bio->bios + i;
NeilBrown4367af52011-07-28 11:31:49 +1000166 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 bio_put(*bio);
168 *bio = NULL;
169 }
170}
171
Arjan van de Ven858119e2006-01-14 13:20:43 -0800172static void free_r1bio(r1bio_t *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
NeilBrown070ec552009-06-16 16:54:21 +1000174 conf_t *conf = r1_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 put_all_bios(conf, r1_bio);
177 mempool_free(r1_bio, conf->r1bio_pool);
178}
179
Arjan van de Ven858119e2006-01-14 13:20:43 -0800180static void put_buf(r1bio_t *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
NeilBrown070ec552009-06-16 16:54:21 +1000182 conf_t *conf = r1_bio->mddev->private;
NeilBrown3e198f72006-01-06 00:20:21 -0800183 int i;
184
185 for (i=0; i<conf->raid_disks; i++) {
186 struct bio *bio = r1_bio->bios[i];
187 if (bio->bi_end_io)
188 rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev);
189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 mempool_free(r1_bio, conf->r1buf_pool);
192
NeilBrown17999be2006-01-06 00:20:12 -0800193 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196static void reschedule_retry(r1bio_t *r1_bio)
197{
198 unsigned long flags;
199 mddev_t *mddev = r1_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +1000200 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 spin_lock_irqsave(&conf->device_lock, flags);
203 list_add(&r1_bio->retry_list, &conf->retry_list);
NeilBrownddaf22a2006-01-06 00:20:19 -0800204 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 spin_unlock_irqrestore(&conf->device_lock, flags);
206
NeilBrown17999be2006-01-06 00:20:12 -0800207 wake_up(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 md_wakeup_thread(mddev->thread);
209}
210
211/*
212 * raid_end_bio_io() is called when we have finished servicing a mirrored
213 * operation and are ready to return a success/failure code to the buffer
214 * cache layer.
215 */
NeilBrownd2eb35a2011-07-28 11:31:48 +1000216static void call_bio_endio(r1bio_t *r1_bio)
217{
218 struct bio *bio = r1_bio->master_bio;
219 int done;
220 conf_t *conf = r1_bio->mddev->private;
221
222 if (bio->bi_phys_segments) {
223 unsigned long flags;
224 spin_lock_irqsave(&conf->device_lock, flags);
225 bio->bi_phys_segments--;
226 done = (bio->bi_phys_segments == 0);
227 spin_unlock_irqrestore(&conf->device_lock, flags);
228 } else
229 done = 1;
230
231 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
232 clear_bit(BIO_UPTODATE, &bio->bi_flags);
233 if (done) {
234 bio_endio(bio, 0);
235 /*
236 * Wake up any possible resync thread that waits for the device
237 * to go idle.
238 */
239 allow_barrier(conf);
240 }
241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243static void raid_end_bio_io(r1bio_t *r1_bio)
244{
245 struct bio *bio = r1_bio->master_bio;
246
NeilBrown4b6d2872005-09-09 16:23:47 -0700247 /* if nobody has done the final endio yet, do it now */
248 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
249 PRINTK(KERN_DEBUG "raid1: sync end %s on sectors %llu-%llu\n",
250 (bio_data_dir(bio) == WRITE) ? "write" : "read",
251 (unsigned long long) bio->bi_sector,
252 (unsigned long long) bio->bi_sector +
253 (bio->bi_size >> 9) - 1);
254
NeilBrownd2eb35a2011-07-28 11:31:48 +1000255 call_bio_endio(r1_bio);
NeilBrown4b6d2872005-09-09 16:23:47 -0700256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 free_r1bio(r1_bio);
258}
259
260/*
261 * Update disk head position estimator based on IRQ completion info.
262 */
263static inline void update_head_pos(int disk, r1bio_t *r1_bio)
264{
NeilBrown070ec552009-06-16 16:54:21 +1000265 conf_t *conf = r1_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 conf->mirrors[disk].head_position =
268 r1_bio->sector + (r1_bio->sectors);
269}
270
NeilBrown6712ecf2007-09-27 12:47:43 +0200271static void raid1_end_read_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100274 r1bio_t *r1_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 int mirror;
NeilBrown070ec552009-06-16 16:54:21 +1000276 conf_t *conf = r1_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 mirror = r1_bio->read_disk;
279 /*
280 * this branch is our 'one mirror IO has finished' event handler:
281 */
NeilBrownddaf22a2006-01-06 00:20:19 -0800282 update_head_pos(mirror, r1_bio);
283
NeilBrowndd00a992007-05-10 03:15:50 -0700284 if (uptodate)
285 set_bit(R1BIO_Uptodate, &r1_bio->state);
286 else {
287 /* If all other devices have failed, we want to return
288 * the error upwards rather than fail the last device.
289 * Here we redefine "uptodate" to mean "Don't want to retry"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 */
NeilBrowndd00a992007-05-10 03:15:50 -0700291 unsigned long flags;
292 spin_lock_irqsave(&conf->device_lock, flags);
293 if (r1_bio->mddev->degraded == conf->raid_disks ||
294 (r1_bio->mddev->degraded == conf->raid_disks-1 &&
295 !test_bit(Faulty, &conf->mirrors[mirror].rdev->flags)))
296 uptodate = 1;
297 spin_unlock_irqrestore(&conf->device_lock, flags);
298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
NeilBrowndd00a992007-05-10 03:15:50 -0700300 if (uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 raid_end_bio_io(r1_bio);
NeilBrowndd00a992007-05-10 03:15:50 -0700302 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 /*
304 * oops, read error:
305 */
306 char b[BDEVNAME_SIZE];
Christian Dietrich8bda4702011-07-27 11:00:36 +1000307 printk_ratelimited(
308 KERN_ERR "md/raid1:%s: %s: "
309 "rescheduling sector %llu\n",
310 mdname(conf->mddev),
311 bdevname(conf->mirrors[mirror].rdev->bdev,
312 b),
313 (unsigned long long)r1_bio->sector);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000314 set_bit(R1BIO_ReadError, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 reschedule_retry(r1_bio);
316 }
317
318 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
NeilBrownaf6d7b72011-05-11 14:51:19 +1000321static void r1_bio_write_done(r1bio_t *r1_bio)
NeilBrown4e780642010-10-19 12:54:01 +1100322{
323 if (atomic_dec_and_test(&r1_bio->remaining))
324 {
325 /* it really is the end of this request */
326 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
327 /* free extra copy of the data pages */
NeilBrownaf6d7b72011-05-11 14:51:19 +1000328 int i = r1_bio->behind_page_count;
NeilBrown4e780642010-10-19 12:54:01 +1100329 while (i--)
NeilBrown2ca68f52011-07-28 11:32:10 +1000330 safe_put_page(r1_bio->behind_bvecs[i].bv_page);
331 kfree(r1_bio->behind_bvecs);
332 r1_bio->behind_bvecs = NULL;
NeilBrown4e780642010-10-19 12:54:01 +1100333 }
334 /* clear the bitmap if all writes complete successfully */
335 bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
336 r1_bio->sectors,
337 !test_bit(R1BIO_Degraded, &r1_bio->state),
NeilBrownaf6d7b72011-05-11 14:51:19 +1000338 test_bit(R1BIO_BehindIO, &r1_bio->state));
NeilBrown4e780642010-10-19 12:54:01 +1100339 md_write_end(r1_bio->mddev);
NeilBrown4367af52011-07-28 11:31:49 +1000340 if (test_bit(R1BIO_MadeGood, &r1_bio->state))
341 reschedule_retry(r1_bio);
342 else
343 raid_end_bio_io(r1_bio);
NeilBrown4e780642010-10-19 12:54:01 +1100344 }
345}
346
NeilBrown6712ecf2007-09-27 12:47:43 +0200347static void raid1_end_write_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100350 r1bio_t *r1_bio = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800351 int mirror, behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
NeilBrown070ec552009-06-16 16:54:21 +1000352 conf_t *conf = r1_bio->mddev->private;
NeilBrown04b857f2006-03-09 17:33:46 -0800353 struct bio *to_put = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 for (mirror = 0; mirror < conf->raid_disks; mirror++)
357 if (r1_bio->bios[mirror] == bio)
358 break;
359
Tejun Heoe9c74692010-09-03 11:56:18 +0200360 /*
361 * 'one mirror IO has finished' event handler:
362 */
363 r1_bio->bios[mirror] = NULL;
364 to_put = bio;
365 if (!uptodate) {
366 md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
367 /* an I/O failed, we can't clear the bitmap */
368 set_bit(R1BIO_Degraded, &r1_bio->state);
NeilBrown4367af52011-07-28 11:31:49 +1000369 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /*
Tejun Heoe9c74692010-09-03 11:56:18 +0200371 * Set R1BIO_Uptodate in our master bio, so that we
372 * will return a good error code for to the higher
373 * levels even if IO on some other mirrored buffer
374 * fails.
375 *
376 * The 'master' represents the composite IO operation
377 * to user-side. So if something waits for IO, then it
378 * will wait for the 'master' bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 */
NeilBrown4367af52011-07-28 11:31:49 +1000380 sector_t first_bad;
381 int bad_sectors;
382
Tejun Heoe9c74692010-09-03 11:56:18 +0200383 set_bit(R1BIO_Uptodate, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
NeilBrown4367af52011-07-28 11:31:49 +1000385 /* Maybe we can clear some bad blocks. */
386 if (is_badblock(conf->mirrors[mirror].rdev,
387 r1_bio->sector, r1_bio->sectors,
388 &first_bad, &bad_sectors)) {
389 r1_bio->bios[mirror] = IO_MADE_GOOD;
390 set_bit(R1BIO_MadeGood, &r1_bio->state);
391 }
392 }
393
Tejun Heoe9c74692010-09-03 11:56:18 +0200394 update_head_pos(mirror, r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Tejun Heoe9c74692010-09-03 11:56:18 +0200396 if (behind) {
397 if (test_bit(WriteMostly, &conf->mirrors[mirror].rdev->flags))
398 atomic_dec(&r1_bio->behind_remaining);
NeilBrown4b6d2872005-09-09 16:23:47 -0700399
Tejun Heoe9c74692010-09-03 11:56:18 +0200400 /*
401 * In behind mode, we ACK the master bio once the I/O
402 * has safely reached all non-writemostly
403 * disks. Setting the Returned bit ensures that this
404 * gets done only once -- we don't ever want to return
405 * -EIO here, instead we'll wait
406 */
407 if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
408 test_bit(R1BIO_Uptodate, &r1_bio->state)) {
409 /* Maybe we can return now */
410 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
411 struct bio *mbio = r1_bio->master_bio;
412 PRINTK(KERN_DEBUG "raid1: behind end write sectors %llu-%llu\n",
413 (unsigned long long) mbio->bi_sector,
414 (unsigned long long) mbio->bi_sector +
415 (mbio->bi_size >> 9) - 1);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000416 call_bio_endio(r1_bio);
NeilBrown4b6d2872005-09-09 16:23:47 -0700417 }
418 }
419 }
NeilBrown4367af52011-07-28 11:31:49 +1000420 if (r1_bio->bios[mirror] == NULL)
421 rdev_dec_pending(conf->mirrors[mirror].rdev,
422 conf->mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * Let's see if all mirrored write operations have finished
426 * already.
427 */
NeilBrownaf6d7b72011-05-11 14:51:19 +1000428 r1_bio_write_done(r1_bio);
NeilBrownc70810b2006-06-26 00:27:35 -0700429
NeilBrown04b857f2006-03-09 17:33:46 -0800430 if (to_put)
431 bio_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434
435/*
436 * This routine returns the disk from which the requested read should
437 * be done. There is a per-array 'next expected sequential IO' sector
438 * number - if this matches on the next IO then we use the last disk.
439 * There is also a per-disk 'last know head position' sector that is
440 * maintained from IRQ contexts, both the normal and the resync IO
441 * completion handlers update this position correctly. If there is no
442 * perfect sequential match then we pick the disk whose head is closest.
443 *
444 * If there are 2 mirrors in the same 2 devices, performance degrades
445 * because position is mirror, not device based.
446 *
447 * The rdev for the device selected will have nr_pending incremented.
448 */
NeilBrownd2eb35a2011-07-28 11:31:48 +1000449static int read_balance(conf_t *conf, r1bio_t *r1_bio, int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000451 const sector_t this_sector = r1_bio->sector;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000452 int sectors;
453 int best_good_sectors;
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000454 int start_disk;
NeilBrown76073052011-05-11 14:34:56 +1000455 int best_disk;
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000456 int i;
NeilBrown76073052011-05-11 14:34:56 +1000457 sector_t best_dist;
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700458 mdk_rdev_t *rdev;
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000459 int choose_first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 rcu_read_lock();
462 /*
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700463 * Check if we can balance. We can balance on the whole
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 * device if no resync is going on, or below the resync window.
465 * We take the first readable disk when above the resync window.
466 */
467 retry:
NeilBrownd2eb35a2011-07-28 11:31:48 +1000468 sectors = r1_bio->sectors;
NeilBrown76073052011-05-11 14:34:56 +1000469 best_disk = -1;
470 best_dist = MaxSector;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000471 best_good_sectors = 0;
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (conf->mddev->recovery_cp < MaxSector &&
474 (this_sector + sectors >= conf->next_resync)) {
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000475 choose_first = 1;
476 start_disk = 0;
477 } else {
478 choose_first = 0;
479 start_disk = conf->last_used;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000482 for (i = 0 ; i < conf->raid_disks ; i++) {
NeilBrown76073052011-05-11 14:34:56 +1000483 sector_t dist;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000484 sector_t first_bad;
485 int bad_sectors;
486
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000487 int disk = start_disk + i;
488 if (disk >= conf->raid_disks)
489 disk -= conf->raid_disks;
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700490
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000491 rdev = rcu_dereference(conf->mirrors[disk].rdev);
492 if (r1_bio->bios[disk] == IO_BLOCKED
493 || rdev == NULL
NeilBrown76073052011-05-11 14:34:56 +1000494 || test_bit(Faulty, &rdev->flags))
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000495 continue;
NeilBrown76073052011-05-11 14:34:56 +1000496 if (!test_bit(In_sync, &rdev->flags) &&
497 rdev->recovery_offset < this_sector + sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 continue;
NeilBrown76073052011-05-11 14:34:56 +1000499 if (test_bit(WriteMostly, &rdev->flags)) {
500 /* Don't balance among write-mostly, just
501 * use the first as a last resort */
502 if (best_disk < 0)
503 best_disk = disk;
504 continue;
505 }
506 /* This is a reasonable device to use. It might
507 * even be best.
508 */
NeilBrownd2eb35a2011-07-28 11:31:48 +1000509 if (is_badblock(rdev, this_sector, sectors,
510 &first_bad, &bad_sectors)) {
511 if (best_dist < MaxSector)
512 /* already have a better device */
513 continue;
514 if (first_bad <= this_sector) {
515 /* cannot read here. If this is the 'primary'
516 * device, then we must not read beyond
517 * bad_sectors from another device..
518 */
519 bad_sectors -= (this_sector - first_bad);
520 if (choose_first && sectors > bad_sectors)
521 sectors = bad_sectors;
522 if (best_good_sectors > sectors)
523 best_good_sectors = sectors;
524
525 } else {
526 sector_t good_sectors = first_bad - this_sector;
527 if (good_sectors > best_good_sectors) {
528 best_good_sectors = good_sectors;
529 best_disk = disk;
530 }
531 if (choose_first)
532 break;
533 }
534 continue;
535 } else
536 best_good_sectors = sectors;
537
NeilBrown76073052011-05-11 14:34:56 +1000538 dist = abs(this_sector - conf->mirrors[disk].head_position);
539 if (choose_first
540 /* Don't change to another disk for sequential reads */
541 || conf->next_seq_sect == this_sector
542 || dist == 0
543 /* If device is idle, use it */
544 || atomic_read(&rdev->nr_pending) == 0) {
545 best_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 break;
547 }
NeilBrown76073052011-05-11 14:34:56 +1000548 if (dist < best_dist) {
549 best_dist = dist;
550 best_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
NeilBrown76073052011-05-11 14:34:56 +1000554 if (best_disk >= 0) {
555 rdev = rcu_dereference(conf->mirrors[best_disk].rdev);
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700556 if (!rdev)
557 goto retry;
558 atomic_inc(&rdev->nr_pending);
NeilBrown76073052011-05-11 14:34:56 +1000559 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /* cannot risk returning a device that failed
561 * before we inc'ed nr_pending
562 */
NeilBrown03c902e2006-01-06 00:20:46 -0800563 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 goto retry;
565 }
NeilBrownd2eb35a2011-07-28 11:31:48 +1000566 sectors = best_good_sectors;
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700567 conf->next_seq_sect = this_sector + sectors;
NeilBrown76073052011-05-11 14:34:56 +1000568 conf->last_used = best_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570 rcu_read_unlock();
NeilBrownd2eb35a2011-07-28 11:31:48 +1000571 *max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
NeilBrown76073052011-05-11 14:34:56 +1000573 return best_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Jonathan Brassow1ed72422011-06-07 17:50:35 -0500576int md_raid1_congested(mddev_t *mddev, int bits)
NeilBrown0d129222006-10-03 01:15:54 -0700577{
NeilBrown070ec552009-06-16 16:54:21 +1000578 conf_t *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700579 int i, ret = 0;
580
581 rcu_read_lock();
582 for (i = 0; i < mddev->raid_disks; i++) {
583 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
584 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200585 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700586
Jonathan Brassow1ed72422011-06-07 17:50:35 -0500587 BUG_ON(!q);
588
NeilBrown0d129222006-10-03 01:15:54 -0700589 /* Note the '|| 1' - when read_balance prefers
590 * non-congested targets, it can be removed
591 */
Alexander Beregalov91a9e992009-04-07 01:35:56 +0400592 if ((bits & (1<<BDI_async_congested)) || 1)
NeilBrown0d129222006-10-03 01:15:54 -0700593 ret |= bdi_congested(&q->backing_dev_info, bits);
594 else
595 ret &= bdi_congested(&q->backing_dev_info, bits);
596 }
597 }
598 rcu_read_unlock();
599 return ret;
600}
Jonathan Brassow1ed72422011-06-07 17:50:35 -0500601EXPORT_SYMBOL_GPL(md_raid1_congested);
NeilBrown0d129222006-10-03 01:15:54 -0700602
Jonathan Brassow1ed72422011-06-07 17:50:35 -0500603static int raid1_congested(void *data, int bits)
604{
605 mddev_t *mddev = data;
606
607 return mddev_congested(mddev, bits) ||
608 md_raid1_congested(mddev, bits);
609}
NeilBrown0d129222006-10-03 01:15:54 -0700610
Jens Axboe7eaceac2011-03-10 08:52:07 +0100611static void flush_pending_writes(conf_t *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800612{
613 /* Any writes that have been queued but are awaiting
614 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800615 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800616 spin_lock_irq(&conf->device_lock);
617
618 if (conf->pending_bio_list.head) {
619 struct bio *bio;
620 bio = bio_list_get(&conf->pending_bio_list);
NeilBrowna35e63e2008-03-04 14:29:29 -0800621 spin_unlock_irq(&conf->device_lock);
622 /* flush any pending bitmap writes to
623 * disk before proceeding w/ I/O */
624 bitmap_unplug(conf->mddev->bitmap);
625
626 while (bio) { /* submit pending writes */
627 struct bio *next = bio->bi_next;
628 bio->bi_next = NULL;
629 generic_make_request(bio);
630 bio = next;
631 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800632 } else
633 spin_unlock_irq(&conf->device_lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100634}
635
NeilBrown17999be2006-01-06 00:20:12 -0800636/* Barriers....
637 * Sometimes we need to suspend IO while we do something else,
638 * either some resync/recovery, or reconfigure the array.
639 * To do this we raise a 'barrier'.
640 * The 'barrier' is a counter that can be raised multiple times
641 * to count how many activities are happening which preclude
642 * normal IO.
643 * We can only raise the barrier if there is no pending IO.
644 * i.e. if nr_pending == 0.
645 * We choose only to raise the barrier if no-one is waiting for the
646 * barrier to go down. This means that as soon as an IO request
647 * is ready, no other operations which require a barrier will start
648 * until the IO request has had a chance.
649 *
650 * So: regular IO calls 'wait_barrier'. When that returns there
651 * is no backgroup IO happening, It must arrange to call
652 * allow_barrier when it has finished its IO.
653 * backgroup IO calls must call raise_barrier. Once that returns
654 * there is no normal IO happeing. It must arrange to call
655 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 */
657#define RESYNC_DEPTH 32
658
NeilBrown17999be2006-01-06 00:20:12 -0800659static void raise_barrier(conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 spin_lock_irq(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800662
663 /* Wait until no block IO is waiting */
664 wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
NeilBrownc3b328a2011-04-18 18:25:43 +1000665 conf->resync_lock, );
NeilBrown17999be2006-01-06 00:20:12 -0800666
667 /* block any new IO from starting */
668 conf->barrier++;
669
NeilBrown046abee2010-10-26 15:46:20 +1100670 /* Now wait for all pending IO to complete */
NeilBrown17999be2006-01-06 00:20:12 -0800671 wait_event_lock_irq(conf->wait_barrier,
672 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
NeilBrownc3b328a2011-04-18 18:25:43 +1000673 conf->resync_lock, );
NeilBrown17999be2006-01-06 00:20:12 -0800674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 spin_unlock_irq(&conf->resync_lock);
676}
677
NeilBrown17999be2006-01-06 00:20:12 -0800678static void lower_barrier(conf_t *conf)
679{
680 unsigned long flags;
NeilBrown709ae482009-12-14 12:49:51 +1100681 BUG_ON(conf->barrier <= 0);
NeilBrown17999be2006-01-06 00:20:12 -0800682 spin_lock_irqsave(&conf->resync_lock, flags);
683 conf->barrier--;
684 spin_unlock_irqrestore(&conf->resync_lock, flags);
685 wake_up(&conf->wait_barrier);
686}
687
688static void wait_barrier(conf_t *conf)
689{
690 spin_lock_irq(&conf->resync_lock);
691 if (conf->barrier) {
692 conf->nr_waiting++;
693 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
694 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000695 );
NeilBrown17999be2006-01-06 00:20:12 -0800696 conf->nr_waiting--;
697 }
698 conf->nr_pending++;
699 spin_unlock_irq(&conf->resync_lock);
700}
701
702static void allow_barrier(conf_t *conf)
703{
704 unsigned long flags;
705 spin_lock_irqsave(&conf->resync_lock, flags);
706 conf->nr_pending--;
707 spin_unlock_irqrestore(&conf->resync_lock, flags);
708 wake_up(&conf->wait_barrier);
709}
710
NeilBrownddaf22a2006-01-06 00:20:19 -0800711static void freeze_array(conf_t *conf)
712{
713 /* stop syncio and normal IO and wait for everything to
714 * go quite.
715 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800716 * wait until nr_pending match nr_queued+1
717 * This is called in the context of one normal IO request
718 * that has failed. Thus any sync request that might be pending
719 * will be blocked by nr_pending, and we need to wait for
720 * pending IO requests to complete or be queued for re-try.
721 * Thus the number queued (nr_queued) plus this request (1)
722 * must match the number of pending IOs (nr_pending) before
723 * we continue.
NeilBrownddaf22a2006-01-06 00:20:19 -0800724 */
725 spin_lock_irq(&conf->resync_lock);
726 conf->barrier++;
727 conf->nr_waiting++;
728 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -0800729 conf->nr_pending == conf->nr_queued+1,
NeilBrownddaf22a2006-01-06 00:20:19 -0800730 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000731 flush_pending_writes(conf));
NeilBrownddaf22a2006-01-06 00:20:19 -0800732 spin_unlock_irq(&conf->resync_lock);
733}
734static void unfreeze_array(conf_t *conf)
735{
736 /* reverse the effect of the freeze */
737 spin_lock_irq(&conf->resync_lock);
738 conf->barrier--;
739 conf->nr_waiting--;
740 wake_up(&conf->wait_barrier);
741 spin_unlock_irq(&conf->resync_lock);
742}
743
NeilBrown17999be2006-01-06 00:20:12 -0800744
NeilBrown4e780642010-10-19 12:54:01 +1100745/* duplicate the data pages for behind I/O
NeilBrown4e780642010-10-19 12:54:01 +1100746 */
NeilBrownaf6d7b72011-05-11 14:51:19 +1000747static void alloc_behind_pages(struct bio *bio, r1bio_t *r1_bio)
NeilBrown4b6d2872005-09-09 16:23:47 -0700748{
749 int i;
750 struct bio_vec *bvec;
NeilBrown2ca68f52011-07-28 11:32:10 +1000751 struct bio_vec *bvecs = kzalloc(bio->bi_vcnt * sizeof(struct bio_vec),
NeilBrown4b6d2872005-09-09 16:23:47 -0700752 GFP_NOIO);
NeilBrown2ca68f52011-07-28 11:32:10 +1000753 if (unlikely(!bvecs))
NeilBrownaf6d7b72011-05-11 14:51:19 +1000754 return;
NeilBrown4b6d2872005-09-09 16:23:47 -0700755
NeilBrown4b6d2872005-09-09 16:23:47 -0700756 bio_for_each_segment(bvec, bio, i) {
NeilBrown2ca68f52011-07-28 11:32:10 +1000757 bvecs[i] = *bvec;
758 bvecs[i].bv_page = alloc_page(GFP_NOIO);
759 if (unlikely(!bvecs[i].bv_page))
NeilBrown4b6d2872005-09-09 16:23:47 -0700760 goto do_sync_io;
NeilBrown2ca68f52011-07-28 11:32:10 +1000761 memcpy(kmap(bvecs[i].bv_page) + bvec->bv_offset,
762 kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
763 kunmap(bvecs[i].bv_page);
NeilBrown4b6d2872005-09-09 16:23:47 -0700764 kunmap(bvec->bv_page);
765 }
NeilBrown2ca68f52011-07-28 11:32:10 +1000766 r1_bio->behind_bvecs = bvecs;
NeilBrownaf6d7b72011-05-11 14:51:19 +1000767 r1_bio->behind_page_count = bio->bi_vcnt;
768 set_bit(R1BIO_BehindIO, &r1_bio->state);
769 return;
NeilBrown4b6d2872005-09-09 16:23:47 -0700770
771do_sync_io:
NeilBrownaf6d7b72011-05-11 14:51:19 +1000772 for (i = 0; i < bio->bi_vcnt; i++)
NeilBrown2ca68f52011-07-28 11:32:10 +1000773 if (bvecs[i].bv_page)
774 put_page(bvecs[i].bv_page);
775 kfree(bvecs);
NeilBrown4b6d2872005-09-09 16:23:47 -0700776 PRINTK("%dB behind alloc failed, doing sync I/O\n", bio->bi_size);
NeilBrown4b6d2872005-09-09 16:23:47 -0700777}
778
NeilBrown21a52c62010-04-01 15:02:13 +1100779static int make_request(mddev_t *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
NeilBrown070ec552009-06-16 16:54:21 +1000781 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 mirror_info_t *mirror;
783 r1bio_t *r1_bio;
784 struct bio *read_bio;
NeilBrown1f68f0c2011-07-28 11:31:48 +1000785 int i, disks;
NeilBrown84255d12008-05-23 13:04:32 -0700786 struct bitmap *bitmap;
NeilBrown191ea9b2005-06-21 17:17:23 -0700787 unsigned long flags;
Jens Axboea3623572005-11-01 09:26:16 +0100788 const int rw = bio_data_dir(bio);
NeilBrown2c7d46e2010-08-18 16:16:05 +1000789 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
Tejun Heoe9c74692010-09-03 11:56:18 +0200790 const unsigned long do_flush_fua = (bio->bi_rw & (REQ_FLUSH | REQ_FUA));
Dan Williams6bfe0b42008-04-30 00:52:32 -0700791 mdk_rdev_t *blocked_rdev;
NeilBrownc3b328a2011-04-18 18:25:43 +1000792 int plugged;
NeilBrown1f68f0c2011-07-28 11:31:48 +1000793 int first_clone;
794 int sectors_handled;
795 int max_sectors;
NeilBrown191ea9b2005-06-21 17:17:23 -0700796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 /*
798 * Register the new request and wait if the reconstruction
799 * thread has put up a bar for new requests.
800 * Continue immediately if no resync is active currently.
801 */
NeilBrown62de6082006-05-01 12:15:47 -0700802
NeilBrown3d310eb2005-06-21 17:17:26 -0700803 md_write_start(mddev, bio); /* wait on superblock update early */
804
NeilBrown6eef4b22009-12-14 12:49:51 +1100805 if (bio_data_dir(bio) == WRITE &&
806 bio->bi_sector + bio->bi_size/512 > mddev->suspend_lo &&
807 bio->bi_sector < mddev->suspend_hi) {
808 /* As the suspend_* range is controlled by
809 * userspace, we want an interruptible
810 * wait.
811 */
812 DEFINE_WAIT(w);
813 for (;;) {
814 flush_signals(current);
815 prepare_to_wait(&conf->wait_barrier,
816 &w, TASK_INTERRUPTIBLE);
817 if (bio->bi_sector + bio->bi_size/512 <= mddev->suspend_lo ||
818 bio->bi_sector >= mddev->suspend_hi)
819 break;
820 schedule();
821 }
822 finish_wait(&conf->wait_barrier, &w);
823 }
NeilBrown62de6082006-05-01 12:15:47 -0700824
NeilBrown17999be2006-01-06 00:20:12 -0800825 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
NeilBrown84255d12008-05-23 13:04:32 -0700827 bitmap = mddev->bitmap;
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /*
830 * make_request() can abort the operation when READA is being
831 * used and no empty request is available.
832 *
833 */
834 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
835
836 r1_bio->master_bio = bio;
837 r1_bio->sectors = bio->bi_size >> 9;
NeilBrown191ea9b2005-06-21 17:17:23 -0700838 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 r1_bio->mddev = mddev;
840 r1_bio->sector = bio->bi_sector;
841
NeilBrownd2eb35a2011-07-28 11:31:48 +1000842 /* We might need to issue multiple reads to different
843 * devices if there are bad blocks around, so we keep
844 * track of the number of reads in bio->bi_phys_segments.
845 * If this is 0, there is only one r1_bio and no locking
846 * will be needed when requests complete. If it is
847 * non-zero, then it is the number of not-completed requests.
848 */
849 bio->bi_phys_segments = 0;
850 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
851
Jens Axboea3623572005-11-01 09:26:16 +0100852 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /*
854 * read balancing logic:
855 */
NeilBrownd2eb35a2011-07-28 11:31:48 +1000856 int rdisk;
857
858read_again:
859 rdisk = read_balance(conf, r1_bio, &max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 if (rdisk < 0) {
862 /* couldn't find anywhere to read from */
863 raid_end_bio_io(r1_bio);
864 return 0;
865 }
866 mirror = conf->mirrors + rdisk;
867
NeilBrowne5551902010-03-31 11:21:44 +1100868 if (test_bit(WriteMostly, &mirror->rdev->flags) &&
869 bitmap) {
870 /* Reading from a write-mostly device must
871 * take care not to over-take any writes
872 * that are 'behind'
873 */
874 wait_event(bitmap->behind_wait,
875 atomic_read(&bitmap->behind_writes) == 0);
876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 r1_bio->read_disk = rdisk;
878
NeilBrowna167f662010-10-26 18:31:13 +1100879 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000880 md_trim_bio(read_bio, r1_bio->sector - bio->bi_sector,
881 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 r1_bio->bios[rdisk] = read_bio;
884
885 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
886 read_bio->bi_bdev = mirror->rdev->bdev;
887 read_bio->bi_end_io = raid1_end_read_request;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200888 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 read_bio->bi_private = r1_bio;
890
NeilBrownd2eb35a2011-07-28 11:31:48 +1000891 if (max_sectors < r1_bio->sectors) {
892 /* could not read all from this device, so we will
893 * need another r1_bio.
894 */
NeilBrownd2eb35a2011-07-28 11:31:48 +1000895
896 sectors_handled = (r1_bio->sector + max_sectors
897 - bio->bi_sector);
898 r1_bio->sectors = max_sectors;
899 spin_lock_irq(&conf->device_lock);
900 if (bio->bi_phys_segments == 0)
901 bio->bi_phys_segments = 2;
902 else
903 bio->bi_phys_segments++;
904 spin_unlock_irq(&conf->device_lock);
905 /* Cannot call generic_make_request directly
906 * as that will be queued in __make_request
907 * and subsequent mempool_alloc might block waiting
908 * for it. So hand bio over to raid1d.
909 */
910 reschedule_retry(r1_bio);
911
912 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
913
914 r1_bio->master_bio = bio;
915 r1_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
916 r1_bio->state = 0;
917 r1_bio->mddev = mddev;
918 r1_bio->sector = bio->bi_sector + sectors_handled;
919 goto read_again;
920 } else
921 generic_make_request(read_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return 0;
923 }
924
925 /*
926 * WRITE:
927 */
NeilBrown1f68f0c2011-07-28 11:31:48 +1000928 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 * inc refcount on their rdev. Record them by setting
930 * bios[x] to bio
NeilBrown1f68f0c2011-07-28 11:31:48 +1000931 * If there are known/acknowledged bad blocks on any device on
932 * which we have seen a write error, we want to avoid writing those
933 * blocks.
934 * This potentially requires several writes to write around
935 * the bad blocks. Each set of writes gets it's own r1bio
936 * with a set of bios attached.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 */
NeilBrownc3b328a2011-04-18 18:25:43 +1000938 plugged = mddev_check_plugged(mddev);
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 disks = conf->raid_disks;
Dan Williams6bfe0b42008-04-30 00:52:32 -0700941 retry_write:
942 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 rcu_read_lock();
NeilBrown1f68f0c2011-07-28 11:31:48 +1000944 max_sectors = r1_bio->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 for (i = 0; i < disks; i++) {
Dan Williams6bfe0b42008-04-30 00:52:32 -0700946 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
947 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
948 atomic_inc(&rdev->nr_pending);
949 blocked_rdev = rdev;
950 break;
951 }
NeilBrown1f68f0c2011-07-28 11:31:48 +1000952 r1_bio->bios[i] = NULL;
953 if (!rdev || test_bit(Faulty, &rdev->flags)) {
954 set_bit(R1BIO_Degraded, &r1_bio->state);
955 continue;
956 }
957
958 atomic_inc(&rdev->nr_pending);
959 if (test_bit(WriteErrorSeen, &rdev->flags)) {
960 sector_t first_bad;
961 int bad_sectors;
962 int is_bad;
963
964 is_bad = is_badblock(rdev, r1_bio->sector,
965 max_sectors,
966 &first_bad, &bad_sectors);
967 if (is_bad < 0) {
968 /* mustn't write here until the bad block is
969 * acknowledged*/
970 set_bit(BlockedBadBlocks, &rdev->flags);
971 blocked_rdev = rdev;
972 break;
NeilBrown964147d2010-05-18 15:27:13 +1000973 }
NeilBrown1f68f0c2011-07-28 11:31:48 +1000974 if (is_bad && first_bad <= r1_bio->sector) {
975 /* Cannot write here at all */
976 bad_sectors -= (r1_bio->sector - first_bad);
977 if (bad_sectors < max_sectors)
978 /* mustn't write more than bad_sectors
979 * to other devices yet
980 */
981 max_sectors = bad_sectors;
982 rdev_dec_pending(rdev, mddev);
983 /* We don't set R1BIO_Degraded as that
984 * only applies if the disk is
985 * missing, so it might be re-added,
986 * and we want to know to recover this
987 * chunk.
988 * In this case the device is here,
989 * and the fact that this chunk is not
990 * in-sync is recorded in the bad
991 * block log
992 */
993 continue;
994 }
995 if (is_bad) {
996 int good_sectors = first_bad - r1_bio->sector;
997 if (good_sectors < max_sectors)
998 max_sectors = good_sectors;
999 }
1000 }
1001 r1_bio->bios[i] = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003 rcu_read_unlock();
1004
Dan Williams6bfe0b42008-04-30 00:52:32 -07001005 if (unlikely(blocked_rdev)) {
1006 /* Wait for this device to become unblocked */
1007 int j;
1008
1009 for (j = 0; j < i; j++)
1010 if (r1_bio->bios[j])
1011 rdev_dec_pending(conf->mirrors[j].rdev, mddev);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001012 r1_bio->state = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001013 allow_barrier(conf);
1014 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1015 wait_barrier(conf);
1016 goto retry_write;
1017 }
1018
NeilBrown1f68f0c2011-07-28 11:31:48 +10001019 if (max_sectors < r1_bio->sectors) {
1020 /* We are splitting this write into multiple parts, so
1021 * we need to prepare for allocating another r1_bio.
1022 */
1023 r1_bio->sectors = max_sectors;
1024 spin_lock_irq(&conf->device_lock);
1025 if (bio->bi_phys_segments == 0)
1026 bio->bi_phys_segments = 2;
1027 else
1028 bio->bi_phys_segments++;
1029 spin_unlock_irq(&conf->device_lock);
NeilBrown191ea9b2005-06-21 17:17:23 -07001030 }
NeilBrown1f68f0c2011-07-28 11:31:48 +10001031 sectors_handled = r1_bio->sector + max_sectors - bio->bi_sector;
NeilBrown4b6d2872005-09-09 16:23:47 -07001032
NeilBrown4e780642010-10-19 12:54:01 +11001033 atomic_set(&r1_bio->remaining, 1);
NeilBrown4b6d2872005-09-09 16:23:47 -07001034 atomic_set(&r1_bio->behind_remaining, 0);
NeilBrown191ea9b2005-06-21 17:17:23 -07001035
NeilBrown1f68f0c2011-07-28 11:31:48 +10001036 first_clone = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 for (i = 0; i < disks; i++) {
1038 struct bio *mbio;
1039 if (!r1_bio->bios[i])
1040 continue;
1041
NeilBrowna167f662010-10-26 18:31:13 +11001042 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001043 md_trim_bio(mbio, r1_bio->sector - bio->bi_sector, max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
NeilBrown1f68f0c2011-07-28 11:31:48 +10001045 if (first_clone) {
1046 /* do behind I/O ?
1047 * Not if there are too many, or cannot
1048 * allocate memory, or a reader on WriteMostly
1049 * is waiting for behind writes to flush */
1050 if (bitmap &&
1051 (atomic_read(&bitmap->behind_writes)
1052 < mddev->bitmap_info.max_write_behind) &&
1053 !waitqueue_active(&bitmap->behind_wait))
1054 alloc_behind_pages(mbio, r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
NeilBrown1f68f0c2011-07-28 11:31:48 +10001056 bitmap_startwrite(bitmap, r1_bio->sector,
1057 r1_bio->sectors,
1058 test_bit(R1BIO_BehindIO,
1059 &r1_bio->state));
1060 first_clone = 0;
1061 }
NeilBrown2ca68f52011-07-28 11:32:10 +10001062 if (r1_bio->behind_bvecs) {
NeilBrown4b6d2872005-09-09 16:23:47 -07001063 struct bio_vec *bvec;
1064 int j;
1065
1066 /* Yes, I really want the '__' version so that
1067 * we clear any unused pointer in the io_vec, rather
1068 * than leave them unchanged. This is important
1069 * because when we come to free the pages, we won't
NeilBrown046abee2010-10-26 15:46:20 +11001070 * know the original bi_idx, so we just free
NeilBrown4b6d2872005-09-09 16:23:47 -07001071 * them all
1072 */
1073 __bio_for_each_segment(bvec, mbio, j, 0)
NeilBrown2ca68f52011-07-28 11:32:10 +10001074 bvec->bv_page = r1_bio->behind_bvecs[j].bv_page;
NeilBrown4b6d2872005-09-09 16:23:47 -07001075 if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
1076 atomic_inc(&r1_bio->behind_remaining);
1077 }
1078
NeilBrown1f68f0c2011-07-28 11:31:48 +10001079 r1_bio->bios[i] = mbio;
1080
1081 mbio->bi_sector = (r1_bio->sector +
1082 conf->mirrors[i].rdev->data_offset);
1083 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
1084 mbio->bi_end_io = raid1_end_write_request;
1085 mbio->bi_rw = WRITE | do_flush_fua | do_sync;
1086 mbio->bi_private = r1_bio;
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 atomic_inc(&r1_bio->remaining);
NeilBrown4e780642010-10-19 12:54:01 +11001089 spin_lock_irqsave(&conf->device_lock, flags);
1090 bio_list_add(&conf->pending_bio_list, mbio);
NeilBrown4e780642010-10-19 12:54:01 +11001091 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
NeilBrownaf6d7b72011-05-11 14:51:19 +10001093 r1_bio_write_done(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
NeilBrown4e780642010-10-19 12:54:01 +11001095 /* In case raid1d snuck in to freeze_array */
NeilBrowna35e63e2008-03-04 14:29:29 -08001096 wake_up(&conf->wait_barrier);
1097
NeilBrown1f68f0c2011-07-28 11:31:48 +10001098 if (sectors_handled < (bio->bi_size >> 9)) {
1099 /* We need another r1_bio. It has already been counted
1100 * in bio->bi_phys_segments
1101 */
1102 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1103 r1_bio->master_bio = bio;
1104 r1_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1105 r1_bio->state = 0;
1106 r1_bio->mddev = mddev;
1107 r1_bio->sector = bio->bi_sector + sectors_handled;
1108 goto retry_write;
1109 }
1110
NeilBrownc3b328a2011-04-18 18:25:43 +10001111 if (do_sync || !bitmap || !plugged)
Lars Ellenberge3881a62007-01-10 23:15:37 -08001112 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 return 0;
1115}
1116
1117static void status(struct seq_file *seq, mddev_t *mddev)
1118{
NeilBrown070ec552009-06-16 16:54:21 +10001119 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 int i;
1121
1122 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown11ce99e2006-10-03 01:15:52 -07001123 conf->raid_disks - mddev->degraded);
NeilBrownddac7c72006-08-31 21:27:36 -07001124 rcu_read_lock();
1125 for (i = 0; i < conf->raid_disks; i++) {
1126 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 seq_printf(seq, "%s",
NeilBrownddac7c72006-08-31 21:27:36 -07001128 rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1129 }
1130 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 seq_printf(seq, "]");
1132}
1133
1134
1135static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1136{
1137 char b[BDEVNAME_SIZE];
NeilBrown070ec552009-06-16 16:54:21 +10001138 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 /*
1141 * If it is not operational, then we have already marked it as dead
1142 * else if it is the last working disks, ignore the error, let the
1143 * next level up know.
1144 * else mark the drive as failed
1145 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001146 if (test_bit(In_sync, &rdev->flags)
NeilBrown4044ba52009-01-09 08:31:11 +11001147 && (conf->raid_disks - mddev->degraded) == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 /*
1149 * Don't fail the drive, act as though we were just a
NeilBrown4044ba52009-01-09 08:31:11 +11001150 * normal single drive.
1151 * However don't try a recovery from this drive as
1152 * it is very likely to fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 */
NeilBrown53890422011-07-27 11:00:36 +10001154 conf->recovery_disabled = mddev->recovery_disabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return;
NeilBrown4044ba52009-01-09 08:31:11 +11001156 }
NeilBrownde393cd2011-07-28 11:31:48 +10001157 set_bit(Blocked, &rdev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -07001158 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1159 unsigned long flags;
1160 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 mddev->degraded++;
NeilBrowndd00a992007-05-10 03:15:50 -07001162 set_bit(Faulty, &rdev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -07001163 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 /*
1165 * if recovery is running, make sure it aborts.
1166 */
NeilBrowndfc70642008-05-23 13:04:39 -07001167 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrowndd00a992007-05-10 03:15:50 -07001168 } else
1169 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -07001170 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Joe Perches067032b2011-01-14 09:14:33 +11001171 printk(KERN_ALERT
1172 "md/raid1:%s: Disk failure on %s, disabling device.\n"
1173 "md/raid1:%s: Operation continuing on %d devices.\n",
NeilBrown9dd1e2f2010-05-03 14:30:35 +10001174 mdname(mddev), bdevname(rdev->bdev, b),
1175 mdname(mddev), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
1178static void print_conf(conf_t *conf)
1179{
1180 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
NeilBrown9dd1e2f2010-05-03 14:30:35 +10001182 printk(KERN_DEBUG "RAID1 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (!conf) {
NeilBrown9dd1e2f2010-05-03 14:30:35 +10001184 printk(KERN_DEBUG "(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return;
1186 }
NeilBrown9dd1e2f2010-05-03 14:30:35 +10001187 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 conf->raid_disks);
1189
NeilBrownddac7c72006-08-31 21:27:36 -07001190 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 for (i = 0; i < conf->raid_disks; i++) {
1192 char b[BDEVNAME_SIZE];
NeilBrownddac7c72006-08-31 21:27:36 -07001193 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
1194 if (rdev)
NeilBrown9dd1e2f2010-05-03 14:30:35 +10001195 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownddac7c72006-08-31 21:27:36 -07001196 i, !test_bit(In_sync, &rdev->flags),
1197 !test_bit(Faulty, &rdev->flags),
1198 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
NeilBrownddac7c72006-08-31 21:27:36 -07001200 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
1203static void close_sync(conf_t *conf)
1204{
NeilBrown17999be2006-01-06 00:20:12 -08001205 wait_barrier(conf);
1206 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 mempool_destroy(conf->r1buf_pool);
1209 conf->r1buf_pool = NULL;
1210}
1211
1212static int raid1_spare_active(mddev_t *mddev)
1213{
1214 int i;
1215 conf_t *conf = mddev->private;
NeilBrown6b965622010-08-18 11:56:59 +10001216 int count = 0;
1217 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 /*
1220 * Find all failed disks within the RAID1 configuration
NeilBrownddac7c72006-08-31 21:27:36 -07001221 * and mark them readable.
1222 * Called under mddev lock, so rcu protection not needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 */
1224 for (i = 0; i < conf->raid_disks; i++) {
NeilBrownddac7c72006-08-31 21:27:36 -07001225 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
1226 if (rdev
1227 && !test_bit(Faulty, &rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001228 && !test_and_set_bit(In_sync, &rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001229 count++;
Jonathan Brassow654e8b52011-07-27 11:00:36 +10001230 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232 }
NeilBrown6b965622010-08-18 11:56:59 +10001233 spin_lock_irqsave(&conf->device_lock, flags);
1234 mddev->degraded -= count;
1235 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001238 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
1241
1242static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1243{
1244 conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001245 int err = -EEXIST;
NeilBrown41158c72005-06-21 17:17:25 -07001246 int mirror = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 mirror_info_t *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10001248 int first = 0;
1249 int last = mddev->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
NeilBrown53890422011-07-27 11:00:36 +10001251 if (mddev->recovery_disabled == conf->recovery_disabled)
1252 return -EBUSY;
1253
Neil Brown6c2fce22008-06-28 08:31:31 +10001254 if (rdev->raid_disk >= 0)
1255 first = last = rdev->raid_disk;
1256
1257 for (mirror = first; mirror <= last; mirror++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if ( !(p=conf->mirrors+mirror)->rdev) {
1259
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10001260 disk_stack_limits(mddev->gendisk, rdev->bdev,
1261 rdev->data_offset << 9);
NeilBrown627a2d32010-03-08 16:44:38 +11001262 /* as we don't honour merge_bvec_fn, we must
1263 * never risk violating it, so limit
1264 * ->max_segments to one lying with a single
1265 * page, as a one page request is never in
1266 * violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 */
NeilBrown627a2d32010-03-08 16:44:38 +11001268 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1269 blk_queue_max_segments(mddev->queue, 1);
1270 blk_queue_segment_boundary(mddev->queue,
1271 PAGE_CACHE_SIZE - 1);
1272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 p->head_position = 0;
1275 rdev->raid_disk = mirror;
Neil Brown199050e2008-06-28 08:31:33 +10001276 err = 0;
NeilBrown6aea114a2005-11-28 13:44:13 -08001277 /* As all devices are equivalent, we don't need a full recovery
1278 * if this was recently any drive of the array
1279 */
1280 if (rdev->saved_raid_disk < 0)
NeilBrown41158c72005-06-21 17:17:25 -07001281 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001282 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 break;
1284 }
Andre Nollac5e7112009-08-03 10:59:47 +10001285 md_integrity_add_rdev(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001287 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
1290static int raid1_remove_disk(mddev_t *mddev, int number)
1291{
1292 conf_t *conf = mddev->private;
1293 int err = 0;
1294 mdk_rdev_t *rdev;
1295 mirror_info_t *p = conf->mirrors+ number;
1296
1297 print_conf(conf);
1298 rdev = p->rdev;
1299 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001300 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 atomic_read(&rdev->nr_pending)) {
1302 err = -EBUSY;
1303 goto abort;
1304 }
NeilBrown046abee2010-10-26 15:46:20 +11001305 /* Only remove non-faulty devices if recovery
NeilBrowndfc70642008-05-23 13:04:39 -07001306 * is not possible.
1307 */
1308 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown53890422011-07-27 11:00:36 +10001309 mddev->recovery_disabled != conf->recovery_disabled &&
NeilBrowndfc70642008-05-23 13:04:39 -07001310 mddev->degraded < conf->raid_disks) {
1311 err = -EBUSY;
1312 goto abort;
1313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001315 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (atomic_read(&rdev->nr_pending)) {
1317 /* lost the race, try later */
1318 err = -EBUSY;
1319 p->rdev = rdev;
Andre Nollac5e7112009-08-03 10:59:47 +10001320 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001322 err = md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
1324abort:
1325
1326 print_conf(conf);
1327 return err;
1328}
1329
1330
NeilBrown6712ecf2007-09-27 12:47:43 +02001331static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001333 r1bio_t *r1_bio = bio->bi_private;
NeilBrownd11c1712006-01-06 00:20:26 -08001334 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
NeilBrownd11c1712006-01-06 00:20:26 -08001336 for (i=r1_bio->mddev->raid_disks; i--; )
1337 if (r1_bio->bios[i] == bio)
1338 break;
1339 BUG_ON(i < 0);
1340 update_head_pos(i, r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 /*
1342 * we have read a block, now it needs to be re-written,
1343 * or re-read if the read failed.
1344 * We don't do much here, just schedule handling by raid1d
1345 */
NeilBrown69382e82006-01-06 00:20:22 -08001346 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 set_bit(R1BIO_Uptodate, &r1_bio->state);
NeilBrownd11c1712006-01-06 00:20:26 -08001348
1349 if (atomic_dec_and_test(&r1_bio->remaining))
1350 reschedule_retry(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
NeilBrown6712ecf2007-09-27 12:47:43 +02001353static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
1355 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001356 r1bio_t *r1_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 mddev_t *mddev = r1_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001358 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 int i;
1360 int mirror=0;
NeilBrown4367af52011-07-28 11:31:49 +10001361 sector_t first_bad;
1362 int bad_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 for (i = 0; i < conf->raid_disks; i++)
1365 if (r1_bio->bios[i] == bio) {
1366 mirror = i;
1367 break;
1368 }
NeilBrown6b1117d2006-03-31 02:31:57 -08001369 if (!uptodate) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001370 sector_t sync_blocks = 0;
NeilBrown6b1117d2006-03-31 02:31:57 -08001371 sector_t s = r1_bio->sector;
1372 long sectors_to_go = r1_bio->sectors;
1373 /* make sure these bits doesn't get cleared. */
1374 do {
NeilBrown5e3db642006-07-10 04:44:18 -07001375 bitmap_end_sync(mddev->bitmap, s,
NeilBrown6b1117d2006-03-31 02:31:57 -08001376 &sync_blocks, 1);
1377 s += sync_blocks;
1378 sectors_to_go -= sync_blocks;
1379 } while (sectors_to_go > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 md_error(mddev, conf->mirrors[mirror].rdev);
NeilBrown4367af52011-07-28 11:31:49 +10001381 } else if (is_badblock(conf->mirrors[mirror].rdev,
1382 r1_bio->sector,
1383 r1_bio->sectors,
1384 &first_bad, &bad_sectors))
1385 set_bit(R1BIO_MadeGood, &r1_bio->state);
NeilBrowne3b97032005-08-04 12:53:34 -07001386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 update_head_pos(mirror, r1_bio);
1388
1389 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown4367af52011-07-28 11:31:49 +10001390 int s = r1_bio->sectors;
1391 if (test_bit(R1BIO_MadeGood, &r1_bio->state))
1392 reschedule_retry(r1_bio);
1393 else {
1394 put_buf(r1_bio);
1395 md_done_sync(mddev, s, uptodate);
1396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
NeilBrowna68e5872011-05-11 14:40:44 +10001400static int fix_sync_read_error(r1bio_t *r1_bio)
1401{
1402 /* Try some synchronous reads of other devices to get
1403 * good data, much like with normal read errors. Only
1404 * read into the pages we already have so we don't
1405 * need to re-issue the read request.
1406 * We don't need to freeze the array, because being in an
1407 * active sync request, there is no normal IO, and
1408 * no overlapping syncs.
NeilBrown06f60382011-07-28 11:31:48 +10001409 * We don't need to check is_badblock() again as we
1410 * made sure that anything with a bad block in range
1411 * will have bi_end_io clear.
NeilBrowna68e5872011-05-11 14:40:44 +10001412 */
1413 mddev_t *mddev = r1_bio->mddev;
1414 conf_t *conf = mddev->private;
1415 struct bio *bio = r1_bio->bios[r1_bio->read_disk];
1416 sector_t sect = r1_bio->sector;
1417 int sectors = r1_bio->sectors;
1418 int idx = 0;
1419
1420 while(sectors) {
1421 int s = sectors;
1422 int d = r1_bio->read_disk;
1423 int success = 0;
1424 mdk_rdev_t *rdev;
NeilBrown78d7f5f2011-05-11 14:48:56 +10001425 int start;
NeilBrowna68e5872011-05-11 14:40:44 +10001426
1427 if (s > (PAGE_SIZE>>9))
1428 s = PAGE_SIZE >> 9;
1429 do {
1430 if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
1431 /* No rcu protection needed here devices
1432 * can only be removed when no resync is
1433 * active, and resync is currently active
1434 */
1435 rdev = conf->mirrors[d].rdev;
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001436 if (sync_page_io(rdev, sect, s<<9,
NeilBrowna68e5872011-05-11 14:40:44 +10001437 bio->bi_io_vec[idx].bv_page,
1438 READ, false)) {
1439 success = 1;
1440 break;
1441 }
1442 }
1443 d++;
1444 if (d == conf->raid_disks)
1445 d = 0;
1446 } while (!success && d != r1_bio->read_disk);
1447
NeilBrown78d7f5f2011-05-11 14:48:56 +10001448 if (!success) {
NeilBrowna68e5872011-05-11 14:40:44 +10001449 char b[BDEVNAME_SIZE];
1450 /* Cannot read from anywhere, array is toast */
1451 md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
1452 printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O read error"
1453 " for block %llu\n",
1454 mdname(mddev),
1455 bdevname(bio->bi_bdev, b),
1456 (unsigned long long)r1_bio->sector);
1457 md_done_sync(mddev, r1_bio->sectors, 0);
1458 put_buf(r1_bio);
1459 return 0;
1460 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001461
1462 start = d;
1463 /* write it back and re-read */
1464 while (d != r1_bio->read_disk) {
1465 if (d == 0)
1466 d = conf->raid_disks;
1467 d--;
1468 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1469 continue;
1470 rdev = conf->mirrors[d].rdev;
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001471 if (sync_page_io(rdev, sect, s<<9,
NeilBrown78d7f5f2011-05-11 14:48:56 +10001472 bio->bi_io_vec[idx].bv_page,
1473 WRITE, false) == 0) {
1474 r1_bio->bios[d]->bi_end_io = NULL;
1475 rdev_dec_pending(rdev, mddev);
1476 md_error(mddev, rdev);
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001477 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001478 }
1479 d = start;
1480 while (d != r1_bio->read_disk) {
1481 if (d == 0)
1482 d = conf->raid_disks;
1483 d--;
1484 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1485 continue;
1486 rdev = conf->mirrors[d].rdev;
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001487 if (sync_page_io(rdev, sect, s<<9,
NeilBrown78d7f5f2011-05-11 14:48:56 +10001488 bio->bi_io_vec[idx].bv_page,
1489 READ, false) == 0)
1490 md_error(mddev, rdev);
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001491 else
1492 atomic_add(s, &rdev->corrected_errors);
NeilBrown78d7f5f2011-05-11 14:48:56 +10001493 }
NeilBrowna68e5872011-05-11 14:40:44 +10001494 sectors -= s;
1495 sect += s;
1496 idx ++;
1497 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001498 set_bit(R1BIO_Uptodate, &r1_bio->state);
NeilBrown7ca78d52011-05-11 14:50:37 +10001499 set_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrowna68e5872011-05-11 14:40:44 +10001500 return 1;
1501}
1502
1503static int process_checks(r1bio_t *r1_bio)
1504{
1505 /* We have read all readable devices. If we haven't
1506 * got the block, then there is no hope left.
1507 * If we have, then we want to do a comparison
1508 * and skip the write if everything is the same.
1509 * If any blocks failed to read, then we need to
1510 * attempt an over-write
1511 */
1512 mddev_t *mddev = r1_bio->mddev;
1513 conf_t *conf = mddev->private;
1514 int primary;
1515 int i;
1516
NeilBrown78d7f5f2011-05-11 14:48:56 +10001517 for (primary = 0; primary < conf->raid_disks; primary++)
NeilBrowna68e5872011-05-11 14:40:44 +10001518 if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
1519 test_bit(BIO_UPTODATE, &r1_bio->bios[primary]->bi_flags)) {
1520 r1_bio->bios[primary]->bi_end_io = NULL;
1521 rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
1522 break;
1523 }
1524 r1_bio->read_disk = primary;
NeilBrown78d7f5f2011-05-11 14:48:56 +10001525 for (i = 0; i < conf->raid_disks; i++) {
1526 int j;
1527 int vcnt = r1_bio->sectors >> (PAGE_SHIFT- 9);
1528 struct bio *pbio = r1_bio->bios[primary];
1529 struct bio *sbio = r1_bio->bios[i];
1530 int size;
NeilBrowna68e5872011-05-11 14:40:44 +10001531
NeilBrown78d7f5f2011-05-11 14:48:56 +10001532 if (r1_bio->bios[i]->bi_end_io != end_sync_read)
1533 continue;
NeilBrowna68e5872011-05-11 14:40:44 +10001534
NeilBrown78d7f5f2011-05-11 14:48:56 +10001535 if (test_bit(BIO_UPTODATE, &sbio->bi_flags)) {
1536 for (j = vcnt; j-- ; ) {
1537 struct page *p, *s;
1538 p = pbio->bi_io_vec[j].bv_page;
1539 s = sbio->bi_io_vec[j].bv_page;
1540 if (memcmp(page_address(p),
1541 page_address(s),
1542 PAGE_SIZE))
1543 break;
NeilBrowna68e5872011-05-11 14:40:44 +10001544 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001545 } else
1546 j = 0;
1547 if (j >= 0)
1548 mddev->resync_mismatches += r1_bio->sectors;
1549 if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
1550 && test_bit(BIO_UPTODATE, &sbio->bi_flags))) {
1551 /* No need to write to this device. */
1552 sbio->bi_end_io = NULL;
1553 rdev_dec_pending(conf->mirrors[i].rdev, mddev);
1554 continue;
NeilBrowna68e5872011-05-11 14:40:44 +10001555 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001556 /* fixup the bio for reuse */
1557 sbio->bi_vcnt = vcnt;
1558 sbio->bi_size = r1_bio->sectors << 9;
1559 sbio->bi_idx = 0;
1560 sbio->bi_phys_segments = 0;
1561 sbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1562 sbio->bi_flags |= 1 << BIO_UPTODATE;
1563 sbio->bi_next = NULL;
1564 sbio->bi_sector = r1_bio->sector +
1565 conf->mirrors[i].rdev->data_offset;
1566 sbio->bi_bdev = conf->mirrors[i].rdev->bdev;
1567 size = sbio->bi_size;
1568 for (j = 0; j < vcnt ; j++) {
1569 struct bio_vec *bi;
1570 bi = &sbio->bi_io_vec[j];
1571 bi->bv_offset = 0;
1572 if (size > PAGE_SIZE)
1573 bi->bv_len = PAGE_SIZE;
1574 else
1575 bi->bv_len = size;
1576 size -= PAGE_SIZE;
1577 memcpy(page_address(bi->bv_page),
1578 page_address(pbio->bi_io_vec[j].bv_page),
1579 PAGE_SIZE);
1580 }
1581 }
NeilBrowna68e5872011-05-11 14:40:44 +10001582 return 0;
1583}
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
1586{
NeilBrown070ec552009-06-16 16:54:21 +10001587 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 int i;
1589 int disks = conf->raid_disks;
1590 struct bio *bio, *wbio;
1591
1592 bio = r1_bio->bios[r1_bio->read_disk];
1593
NeilBrowna68e5872011-05-11 14:40:44 +10001594 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
1595 /* ouch - failed to read all of that. */
1596 if (!fix_sync_read_error(r1_bio))
1597 return;
NeilBrown7ca78d52011-05-11 14:50:37 +10001598
1599 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1600 if (process_checks(r1_bio) < 0)
1601 return;
NeilBrownd11c1712006-01-06 00:20:26 -08001602 /*
1603 * schedule writes
1604 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 atomic_set(&r1_bio->remaining, 1);
1606 for (i = 0; i < disks ; i++) {
1607 wbio = r1_bio->bios[i];
NeilBrown3e198f72006-01-06 00:20:21 -08001608 if (wbio->bi_end_io == NULL ||
1609 (wbio->bi_end_io == end_sync_read &&
1610 (i == r1_bio->read_disk ||
1611 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 continue;
1613
NeilBrown3e198f72006-01-06 00:20:21 -08001614 wbio->bi_rw = WRITE;
1615 wbio->bi_end_io = end_sync_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 atomic_inc(&r1_bio->remaining);
1617 md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
NeilBrown191ea9b2005-06-21 17:17:23 -07001618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 generic_make_request(wbio);
1620 }
1621
1622 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001623 /* if we're here, all write(s) have completed, so clean up */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 md_done_sync(mddev, r1_bio->sectors, 1);
1625 put_buf(r1_bio);
1626 }
1627}
1628
1629/*
1630 * This is a kernel thread which:
1631 *
1632 * 1. Retries failed read operations on working mirrors.
1633 * 2. Updates the raid superblock when problems encounter.
NeilBrownd2eb35a2011-07-28 11:31:48 +10001634 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 */
1636
NeilBrown867868f2006-10-03 01:15:51 -07001637static void fix_read_error(conf_t *conf, int read_disk,
1638 sector_t sect, int sectors)
1639{
1640 mddev_t *mddev = conf->mddev;
1641 while(sectors) {
1642 int s = sectors;
1643 int d = read_disk;
1644 int success = 0;
1645 int start;
1646 mdk_rdev_t *rdev;
1647
1648 if (s > (PAGE_SIZE>>9))
1649 s = PAGE_SIZE >> 9;
1650
1651 do {
1652 /* Note: no rcu protection needed here
1653 * as this is synchronous in the raid1d thread
1654 * which is the thread that might remove
1655 * a device. If raid1d ever becomes multi-threaded....
1656 */
NeilBrownd2eb35a2011-07-28 11:31:48 +10001657 sector_t first_bad;
1658 int bad_sectors;
1659
NeilBrown867868f2006-10-03 01:15:51 -07001660 rdev = conf->mirrors[d].rdev;
1661 if (rdev &&
1662 test_bit(In_sync, &rdev->flags) &&
NeilBrownd2eb35a2011-07-28 11:31:48 +10001663 is_badblock(rdev, sect, s,
1664 &first_bad, &bad_sectors) == 0 &&
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001665 sync_page_io(rdev, sect, s<<9,
1666 conf->tmppage, READ, false))
NeilBrown867868f2006-10-03 01:15:51 -07001667 success = 1;
1668 else {
1669 d++;
1670 if (d == conf->raid_disks)
1671 d = 0;
1672 }
1673 } while (!success && d != read_disk);
1674
1675 if (!success) {
1676 /* Cannot read from anywhere -- bye bye array */
1677 md_error(mddev, conf->mirrors[read_disk].rdev);
1678 break;
1679 }
1680 /* write it back and re-read */
1681 start = d;
1682 while (d != read_disk) {
1683 if (d==0)
1684 d = conf->raid_disks;
1685 d--;
1686 rdev = conf->mirrors[d].rdev;
1687 if (rdev &&
1688 test_bit(In_sync, &rdev->flags)) {
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001689 if (sync_page_io(rdev, sect, s<<9,
1690 conf->tmppage, WRITE, false)
NeilBrown867868f2006-10-03 01:15:51 -07001691 == 0)
1692 /* Well, this device is dead */
1693 md_error(mddev, rdev);
1694 }
1695 }
1696 d = start;
1697 while (d != read_disk) {
1698 char b[BDEVNAME_SIZE];
1699 if (d==0)
1700 d = conf->raid_disks;
1701 d--;
1702 rdev = conf->mirrors[d].rdev;
1703 if (rdev &&
1704 test_bit(In_sync, &rdev->flags)) {
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001705 if (sync_page_io(rdev, sect, s<<9,
1706 conf->tmppage, READ, false)
NeilBrown867868f2006-10-03 01:15:51 -07001707 == 0)
1708 /* Well, this device is dead */
1709 md_error(mddev, rdev);
1710 else {
1711 atomic_add(s, &rdev->corrected_errors);
1712 printk(KERN_INFO
NeilBrown9dd1e2f2010-05-03 14:30:35 +10001713 "md/raid1:%s: read error corrected "
NeilBrown867868f2006-10-03 01:15:51 -07001714 "(%d sectors at %llu on %s)\n",
1715 mdname(mddev), s,
Randy Dunlap969b7552006-10-28 10:38:32 -07001716 (unsigned long long)(sect +
1717 rdev->data_offset),
NeilBrown867868f2006-10-03 01:15:51 -07001718 bdevname(rdev->bdev, b));
1719 }
1720 }
1721 }
1722 sectors -= s;
1723 sect += s;
1724 }
1725}
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727static void raid1d(mddev_t *mddev)
1728{
1729 r1bio_t *r1_bio;
1730 struct bio *bio;
1731 unsigned long flags;
NeilBrown070ec552009-06-16 16:54:21 +10001732 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 struct list_head *head = &conf->retry_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 mdk_rdev_t *rdev;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10001735 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 md_check_recovery(mddev);
NeilBrowne1dfa0a2011-04-18 18:25:41 +10001738
1739 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 for (;;) {
1741 char b[BDEVNAME_SIZE];
NeilBrowna35e63e2008-03-04 14:29:29 -08001742
NeilBrownc3b328a2011-04-18 18:25:43 +10001743 if (atomic_read(&mddev->plug_cnt) == 0)
1744 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08001745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08001747 if (list_empty(head)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001748 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08001750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
1752 list_del(head->prev);
NeilBrownddaf22a2006-01-06 00:20:19 -08001753 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 spin_unlock_irqrestore(&conf->device_lock, flags);
1755
1756 mddev = r1_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001757 conf = mddev->private;
NeilBrown4367af52011-07-28 11:31:49 +10001758 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
1759 if (test_bit(R1BIO_MadeGood, &r1_bio->state)) {
1760 int m;
1761 int s = r1_bio->sectors;
1762 for (m = 0; m < conf->raid_disks ; m++) {
1763 struct bio *bio = r1_bio->bios[m];
1764 if (bio->bi_end_io != NULL &&
1765 test_bit(BIO_UPTODATE,
1766 &bio->bi_flags)) {
1767 rdev = conf->mirrors[m].rdev;
1768 rdev_clear_badblocks(
1769 rdev,
1770 r1_bio->sector,
1771 r1_bio->sectors);
1772 }
1773 }
1774 put_buf(r1_bio);
1775 md_done_sync(mddev, s, 1);
1776 } else
1777 sync_request_write(mddev, r1_bio);
1778 } else if (test_bit(R1BIO_MadeGood, &r1_bio->state)) {
1779 int m;
1780 for (m = 0; m < conf->raid_disks ; m++)
1781 if (r1_bio->bios[m] == IO_MADE_GOOD) {
1782 rdev = conf->mirrors[m].rdev;
1783 rdev_clear_badblocks(
1784 rdev,
1785 r1_bio->sector,
1786 r1_bio->sectors);
1787 rdev_dec_pending(rdev, mddev);
1788 }
1789 raid_end_bio_io(r1_bio);
1790 } else if (test_bit(R1BIO_ReadError, &r1_bio->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 int disk;
NeilBrownd2eb35a2011-07-28 11:31:48 +10001792 int max_sectors;
NeilBrownddaf22a2006-01-06 00:20:19 -08001793
NeilBrownd2eb35a2011-07-28 11:31:48 +10001794 clear_bit(R1BIO_ReadError, &r1_bio->state);
NeilBrownddaf22a2006-01-06 00:20:19 -08001795 /* we got a read error. Maybe the drive is bad. Maybe just
1796 * the block and we can fix it.
1797 * We freeze all other IO, and try reading the block from
1798 * other devices. When we find one, we re-write
1799 * and check it that fixes the read error.
1800 * This is all done synchronously while the array is
1801 * frozen
1802 */
NeilBrown867868f2006-10-03 01:15:51 -07001803 if (mddev->ro == 0) {
1804 freeze_array(conf);
1805 fix_read_error(conf, r1_bio->read_disk,
1806 r1_bio->sector,
1807 r1_bio->sectors);
1808 unfreeze_array(conf);
NeilBrownd0e26072009-12-01 17:30:59 +11001809 } else
1810 md_error(mddev,
1811 conf->mirrors[r1_bio->read_disk].rdev);
NeilBrownddaf22a2006-01-06 00:20:19 -08001812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 bio = r1_bio->bios[r1_bio->read_disk];
NeilBrownd2eb35a2011-07-28 11:31:48 +10001814 bdevname(bio->bi_bdev, b);
1815read_more:
1816 disk = read_balance(conf, r1_bio, &max_sectors);
1817 if (disk == -1) {
NeilBrown9dd1e2f2010-05-03 14:30:35 +10001818 printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 " read error for block %llu\n",
NeilBrownd2eb35a2011-07-28 11:31:48 +10001820 mdname(mddev), b,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 (unsigned long long)r1_bio->sector);
1822 raid_end_bio_io(r1_bio);
1823 } else {
NeilBrown2c7d46e2010-08-18 16:16:05 +10001824 const unsigned long do_sync = r1_bio->master_bio->bi_rw & REQ_SYNC;
NeilBrownd2eb35a2011-07-28 11:31:48 +10001825 if (bio) {
1826 r1_bio->bios[r1_bio->read_disk] =
1827 mddev->ro ? IO_BLOCKED : NULL;
1828 bio_put(bio);
1829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 r1_bio->read_disk = disk;
NeilBrowna167f662010-10-26 18:31:13 +11001831 bio = bio_clone_mddev(r1_bio->master_bio,
1832 GFP_NOIO, mddev);
NeilBrownd2eb35a2011-07-28 11:31:48 +10001833 md_trim_bio(bio,
1834 r1_bio->sector - bio->bi_sector,
1835 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 r1_bio->bios[r1_bio->read_disk] = bio;
1837 rdev = conf->mirrors[disk].rdev;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001838 printk_ratelimited(
1839 KERN_ERR
1840 "md/raid1:%s: redirecting sector %llu"
1841 " to other mirror: %s\n",
1842 mdname(mddev),
1843 (unsigned long long)r1_bio->sector,
1844 bdevname(rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 bio->bi_sector = r1_bio->sector + rdev->data_offset;
1846 bio->bi_bdev = rdev->bdev;
1847 bio->bi_end_io = raid1_end_read_request;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001848 bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 bio->bi_private = r1_bio;
NeilBrownd2eb35a2011-07-28 11:31:48 +10001850 if (max_sectors < r1_bio->sectors) {
1851 /* Drat - have to split this up more */
1852 struct bio *mbio = r1_bio->master_bio;
1853 int sectors_handled =
1854 r1_bio->sector + max_sectors
1855 - mbio->bi_sector;
1856 r1_bio->sectors = max_sectors;
1857 spin_lock_irq(&conf->device_lock);
1858 if (mbio->bi_phys_segments == 0)
1859 mbio->bi_phys_segments = 2;
1860 else
1861 mbio->bi_phys_segments++;
1862 spin_unlock_irq(&conf->device_lock);
1863 generic_make_request(bio);
1864 bio = NULL;
1865
1866 r1_bio = mempool_alloc(conf->r1bio_pool,
1867 GFP_NOIO);
1868
1869 r1_bio->master_bio = mbio;
1870 r1_bio->sectors = (mbio->bi_size >> 9)
1871 - sectors_handled;
1872 r1_bio->state = 0;
1873 set_bit(R1BIO_ReadError,
1874 &r1_bio->state);
1875 r1_bio->mddev = mddev;
1876 r1_bio->sector = mbio->bi_sector
1877 + sectors_handled;
1878
1879 goto read_more;
1880 } else
1881 generic_make_request(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
NeilBrownd2eb35a2011-07-28 11:31:48 +10001883 } else {
1884 /* just a partial read to be scheduled from separate
1885 * context
1886 */
1887 generic_make_request(r1_bio->bios[r1_bio->read_disk]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
NeilBrown1d9d5242009-10-16 15:55:32 +11001889 cond_resched();
NeilBrownde393cd2011-07-28 11:31:48 +10001890 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
1891 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10001893 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
1896
1897static int init_resync(conf_t *conf)
1898{
1899 int buffs;
1900
1901 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhenn9e77c482006-04-01 01:08:49 +02001902 BUG_ON(conf->r1buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
1904 conf->poolinfo);
1905 if (!conf->r1buf_pool)
1906 return -ENOMEM;
1907 conf->next_resync = 0;
1908 return 0;
1909}
1910
1911/*
1912 * perform a "sync" on one "block"
1913 *
1914 * We need to make sure that no normal I/O request - particularly write
1915 * requests - conflict with active sync requests.
1916 *
1917 * This is achieved by tracking pending requests and a 'barrier' concept
1918 * that can be installed to exclude normal IO requests.
1919 */
1920
NeilBrown57afd892005-06-21 17:17:13 -07001921static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
NeilBrown070ec552009-06-16 16:54:21 +10001923 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 r1bio_t *r1_bio;
1925 struct bio *bio;
1926 sector_t max_sector, nr_sectors;
NeilBrown3e198f72006-01-06 00:20:21 -08001927 int disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 int i;
NeilBrown3e198f72006-01-06 00:20:21 -08001929 int wonly = -1;
1930 int write_targets = 0, read_targets = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001931 sector_t sync_blocks;
NeilBrowne3b97032005-08-04 12:53:34 -07001932 int still_degraded = 0;
NeilBrown06f60382011-07-28 11:31:48 +10001933 int good_sectors = RESYNC_SECTORS;
1934 int min_bad = 0; /* number of sectors that are bad in all devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 if (!conf->r1buf_pool)
1937 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07001938 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Andre Noll58c0fed2009-03-31 14:33:13 +11001940 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (sector_nr >= max_sector) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001942 /* If we aborted, we need to abort the
1943 * sync on the 'current' bitmap chunk (there will
1944 * only be one in raid1 resync.
1945 * We can find the current addess in mddev->curr_resync
1946 */
NeilBrown6a806c52005-07-15 03:56:35 -07001947 if (mddev->curr_resync < max_sector) /* aborted */
1948 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
NeilBrown191ea9b2005-06-21 17:17:23 -07001949 &sync_blocks, 1);
NeilBrown6a806c52005-07-15 03:56:35 -07001950 else /* completed sync */
NeilBrown191ea9b2005-06-21 17:17:23 -07001951 conf->fullsync = 0;
NeilBrown6a806c52005-07-15 03:56:35 -07001952
1953 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 close_sync(conf);
1955 return 0;
1956 }
1957
NeilBrown07d84d102006-06-26 00:27:56 -07001958 if (mddev->bitmap == NULL &&
1959 mddev->recovery_cp == MaxSector &&
NeilBrown6394cca2006-08-27 01:23:50 -07001960 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown07d84d102006-06-26 00:27:56 -07001961 conf->fullsync == 0) {
1962 *skipped = 1;
1963 return max_sector - sector_nr;
1964 }
NeilBrown6394cca2006-08-27 01:23:50 -07001965 /* before building a request, check if we can skip these blocks..
1966 * This call the bitmap_start_sync doesn't actually record anything
1967 */
NeilBrowne3b97032005-08-04 12:53:34 -07001968 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrowne5de485f2005-11-08 21:39:38 -08001969 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001970 /* We can skip this block, and probably several more */
1971 *skipped = 1;
1972 return sync_blocks;
1973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 /*
NeilBrown17999be2006-01-06 00:20:12 -08001975 * If there is non-resync activity waiting for a turn,
1976 * and resync is going fast enough,
1977 * then let it though before starting on this new sync request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 */
NeilBrown17999be2006-01-06 00:20:12 -08001979 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 msleep_interruptible(1000);
NeilBrown17999be2006-01-06 00:20:12 -08001981
NeilBrownb47490c2008-02-06 01:39:50 -08001982 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
NeilBrown1c4588e2010-10-26 17:41:22 +11001983 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
NeilBrown17999be2006-01-06 00:20:12 -08001984 raise_barrier(conf);
1985
1986 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
NeilBrown3e198f72006-01-06 00:20:21 -08001988 rcu_read_lock();
1989 /*
1990 * If we get a correctably read error during resync or recovery,
1991 * we might want to read from a different device. So we
1992 * flag all drives that could conceivably be read from for READ,
1993 * and any others (which will be non-In_sync devices) for WRITE.
1994 * If a read fails, we try reading from something else for which READ
1995 * is OK.
1996 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 r1_bio->mddev = mddev;
1999 r1_bio->sector = sector_nr;
NeilBrown191ea9b2005-06-21 17:17:23 -07002000 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 set_bit(R1BIO_IsSync, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003 for (i=0; i < conf->raid_disks; i++) {
NeilBrown3e198f72006-01-06 00:20:21 -08002004 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 bio = r1_bio->bios[i];
2006
2007 /* take from bio_init */
2008 bio->bi_next = NULL;
NeilBrowndb8d9d32010-10-07 12:00:50 +11002009 bio->bi_flags &= ~(BIO_POOL_MASK-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 bio->bi_flags |= 1 << BIO_UPTODATE;
NeilBrowndb8d9d32010-10-07 12:00:50 +11002011 bio->bi_comp_cpu = -1;
NeilBrown802ba062006-12-13 00:34:13 -08002012 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 bio->bi_vcnt = 0;
2014 bio->bi_idx = 0;
2015 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 bio->bi_size = 0;
2017 bio->bi_end_io = NULL;
2018 bio->bi_private = NULL;
2019
NeilBrown3e198f72006-01-06 00:20:21 -08002020 rdev = rcu_dereference(conf->mirrors[i].rdev);
2021 if (rdev == NULL ||
NeilBrown06f60382011-07-28 11:31:48 +10002022 test_bit(Faulty, &rdev->flags)) {
NeilBrowne3b97032005-08-04 12:53:34 -07002023 still_degraded = 1;
NeilBrown3e198f72006-01-06 00:20:21 -08002024 } else if (!test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 bio->bi_rw = WRITE;
2026 bio->bi_end_io = end_sync_write;
2027 write_targets ++;
NeilBrown3e198f72006-01-06 00:20:21 -08002028 } else {
2029 /* may need to read from here */
NeilBrown06f60382011-07-28 11:31:48 +10002030 sector_t first_bad = MaxSector;
2031 int bad_sectors;
2032
2033 if (is_badblock(rdev, sector_nr, good_sectors,
2034 &first_bad, &bad_sectors)) {
2035 if (first_bad > sector_nr)
2036 good_sectors = first_bad - sector_nr;
2037 else {
2038 bad_sectors -= (sector_nr - first_bad);
2039 if (min_bad == 0 ||
2040 min_bad > bad_sectors)
2041 min_bad = bad_sectors;
2042 }
NeilBrown3e198f72006-01-06 00:20:21 -08002043 }
NeilBrown06f60382011-07-28 11:31:48 +10002044 if (sector_nr < first_bad) {
2045 if (test_bit(WriteMostly, &rdev->flags)) {
2046 if (wonly < 0)
2047 wonly = i;
2048 } else {
2049 if (disk < 0)
2050 disk = i;
2051 }
2052 bio->bi_rw = READ;
2053 bio->bi_end_io = end_sync_read;
2054 read_targets++;
2055 }
NeilBrown3e198f72006-01-06 00:20:21 -08002056 }
NeilBrown06f60382011-07-28 11:31:48 +10002057 if (bio->bi_end_io) {
2058 atomic_inc(&rdev->nr_pending);
2059 bio->bi_sector = sector_nr + rdev->data_offset;
2060 bio->bi_bdev = rdev->bdev;
2061 bio->bi_private = r1_bio;
2062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
NeilBrown3e198f72006-01-06 00:20:21 -08002064 rcu_read_unlock();
2065 if (disk < 0)
2066 disk = wonly;
2067 r1_bio->read_disk = disk;
NeilBrown191ea9b2005-06-21 17:17:23 -07002068
NeilBrown06f60382011-07-28 11:31:48 +10002069 if (read_targets == 0 && min_bad > 0) {
2070 /* These sectors are bad on all InSync devices, so we
2071 * need to mark them bad on all write targets
2072 */
2073 int ok = 1;
2074 for (i = 0 ; i < conf->raid_disks ; i++)
2075 if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
2076 mdk_rdev_t *rdev =
2077 rcu_dereference(conf->mirrors[i].rdev);
2078 ok = rdev_set_badblocks(rdev, sector_nr,
2079 min_bad, 0
2080 ) && ok;
2081 }
2082 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2083 *skipped = 1;
2084 put_buf(r1_bio);
2085
2086 if (!ok) {
2087 /* Cannot record the badblocks, so need to
2088 * abort the resync.
2089 * If there are multiple read targets, could just
2090 * fail the really bad ones ???
2091 */
2092 conf->recovery_disabled = mddev->recovery_disabled;
2093 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2094 return 0;
2095 } else
2096 return min_bad;
2097
2098 }
2099 if (min_bad > 0 && min_bad < good_sectors) {
2100 /* only resync enough to reach the next bad->good
2101 * transition */
2102 good_sectors = min_bad;
2103 }
2104
NeilBrown3e198f72006-01-06 00:20:21 -08002105 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
2106 /* extra read targets are also write targets */
2107 write_targets += read_targets-1;
2108
2109 if (write_targets == 0 || read_targets == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 /* There is nowhere to write, so all non-sync
2111 * drives must be failed - so we are finished
2112 */
NeilBrown57afd892005-06-21 17:17:13 -07002113 sector_t rv = max_sector - sector_nr;
2114 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 put_buf(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 return rv;
2117 }
2118
NeilBrownc6207272008-02-06 01:39:52 -08002119 if (max_sector > mddev->resync_max)
2120 max_sector = mddev->resync_max; /* Don't do IO beyond here */
NeilBrown06f60382011-07-28 11:31:48 +10002121 if (max_sector > sector_nr + good_sectors)
2122 max_sector = sector_nr + good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 nr_sectors = 0;
NeilBrown289e99e2005-06-21 17:17:24 -07002124 sync_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 do {
2126 struct page *page;
2127 int len = PAGE_SIZE;
2128 if (sector_nr + (len>>9) > max_sector)
2129 len = (max_sector - sector_nr) << 9;
2130 if (len == 0)
2131 break;
NeilBrown6a806c52005-07-15 03:56:35 -07002132 if (sync_blocks == 0) {
2133 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
NeilBrowne5de485f2005-11-08 21:39:38 -08002134 &sync_blocks, still_degraded) &&
2135 !conf->fullsync &&
2136 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
NeilBrown6a806c52005-07-15 03:56:35 -07002137 break;
Eric Sesterhenn9e77c482006-04-01 01:08:49 +02002138 BUG_ON(sync_blocks < (PAGE_SIZE>>9));
NeilBrown7571ae82010-10-07 11:54:46 +11002139 if ((len >> 9) > sync_blocks)
NeilBrown6a806c52005-07-15 03:56:35 -07002140 len = sync_blocks<<9;
NeilBrownab7a30c2005-06-21 17:17:23 -07002141 }
NeilBrown191ea9b2005-06-21 17:17:23 -07002142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 for (i=0 ; i < conf->raid_disks; i++) {
2144 bio = r1_bio->bios[i];
2145 if (bio->bi_end_io) {
NeilBrownd11c1712006-01-06 00:20:26 -08002146 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (bio_add_page(bio, page, len, 0) == 0) {
2148 /* stop here */
NeilBrownd11c1712006-01-06 00:20:26 -08002149 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 while (i > 0) {
2151 i--;
2152 bio = r1_bio->bios[i];
NeilBrown6a806c52005-07-15 03:56:35 -07002153 if (bio->bi_end_io==NULL)
2154 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 /* remove last page from this bio */
2156 bio->bi_vcnt--;
2157 bio->bi_size -= len;
2158 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
2159 }
2160 goto bio_full;
2161 }
2162 }
2163 }
2164 nr_sectors += len>>9;
2165 sector_nr += len>>9;
NeilBrown191ea9b2005-06-21 17:17:23 -07002166 sync_blocks -= (len>>9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
2168 bio_full:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 r1_bio->sectors = nr_sectors;
2170
NeilBrownd11c1712006-01-06 00:20:26 -08002171 /* For a user-requested sync, we read all readable devices and do a
2172 * compare
2173 */
2174 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2175 atomic_set(&r1_bio->remaining, read_targets);
2176 for (i=0; i<conf->raid_disks; i++) {
2177 bio = r1_bio->bios[i];
2178 if (bio->bi_end_io == end_sync_read) {
NeilBrownddac7c72006-08-31 21:27:36 -07002179 md_sync_acct(bio->bi_bdev, nr_sectors);
NeilBrownd11c1712006-01-06 00:20:26 -08002180 generic_make_request(bio);
2181 }
2182 }
2183 } else {
2184 atomic_set(&r1_bio->remaining, 1);
2185 bio = r1_bio->bios[r1_bio->read_disk];
NeilBrownddac7c72006-08-31 21:27:36 -07002186 md_sync_acct(bio->bi_bdev, nr_sectors);
NeilBrownd11c1712006-01-06 00:20:26 -08002187 generic_make_request(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
NeilBrownd11c1712006-01-06 00:20:26 -08002189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 return nr_sectors;
2191}
2192
Dan Williams80c3a6c2009-03-17 18:10:40 -07002193static sector_t raid1_size(mddev_t *mddev, sector_t sectors, int raid_disks)
2194{
2195 if (sectors)
2196 return sectors;
2197
2198 return mddev->dev_sectors;
2199}
2200
NeilBrown709ae482009-12-14 12:49:51 +11002201static conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
2203 conf_t *conf;
NeilBrown709ae482009-12-14 12:49:51 +11002204 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 mirror_info_t *disk;
2206 mdk_rdev_t *rdev;
NeilBrown709ae482009-12-14 12:49:51 +11002207 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
NeilBrown9ffae0c2006-01-06 00:20:32 -08002209 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 if (!conf)
NeilBrown709ae482009-12-14 12:49:51 +11002211 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
NeilBrown9ffae0c2006-01-06 00:20:32 -08002213 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 GFP_KERNEL);
2215 if (!conf->mirrors)
NeilBrown709ae482009-12-14 12:49:51 +11002216 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
NeilBrownddaf22a2006-01-06 00:20:19 -08002218 conf->tmppage = alloc_page(GFP_KERNEL);
2219 if (!conf->tmppage)
NeilBrown709ae482009-12-14 12:49:51 +11002220 goto abort;
NeilBrownddaf22a2006-01-06 00:20:19 -08002221
NeilBrown709ae482009-12-14 12:49:51 +11002222 conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 if (!conf->poolinfo)
NeilBrown709ae482009-12-14 12:49:51 +11002224 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 conf->poolinfo->raid_disks = mddev->raid_disks;
2226 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
2227 r1bio_pool_free,
2228 conf->poolinfo);
2229 if (!conf->r1bio_pool)
NeilBrown709ae482009-12-14 12:49:51 +11002230 goto abort;
2231
NeilBrowned9bfdf2009-10-16 15:55:44 +11002232 conf->poolinfo->mddev = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Neil Browne7e72bf2008-05-14 16:05:54 -07002234 spin_lock_init(&conf->device_lock);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002235 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown709ae482009-12-14 12:49:51 +11002236 int disk_idx = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 if (disk_idx >= mddev->raid_disks
2238 || disk_idx < 0)
2239 continue;
2240 disk = conf->mirrors + disk_idx;
2241
2242 disk->rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
2244 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
2246 conf->raid_disks = mddev->raid_disks;
2247 conf->mddev = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 INIT_LIST_HEAD(&conf->retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
2250 spin_lock_init(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -08002251 init_waitqueue_head(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
NeilBrown191ea9b2005-06-21 17:17:23 -07002253 bio_list_init(&conf->pending_bio_list);
NeilBrown191ea9b2005-06-21 17:17:23 -07002254
NeilBrown709ae482009-12-14 12:49:51 +11002255 conf->last_used = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 for (i = 0; i < conf->raid_disks; i++) {
2257
2258 disk = conf->mirrors + i;
2259
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002260 if (!disk->rdev ||
2261 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 disk->head_position = 0;
NeilBrown918f0232007-08-22 14:01:52 -07002263 if (disk->rdev)
2264 conf->fullsync = 1;
NeilBrown709ae482009-12-14 12:49:51 +11002265 } else if (conf->last_used < 0)
2266 /*
2267 * The first working device is used as a
2268 * starting point to read balancing.
2269 */
2270 conf->last_used = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
NeilBrown709ae482009-12-14 12:49:51 +11002272
2273 err = -EIO;
2274 if (conf->last_used < 0) {
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002275 printk(KERN_ERR "md/raid1:%s: no operational mirrors\n",
NeilBrown709ae482009-12-14 12:49:51 +11002276 mdname(mddev));
2277 goto abort;
NeilBrown11ce99e2006-10-03 01:15:52 -07002278 }
NeilBrown709ae482009-12-14 12:49:51 +11002279 err = -ENOMEM;
2280 conf->thread = md_register_thread(raid1d, mddev, NULL);
2281 if (!conf->thread) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002282 printk(KERN_ERR
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002283 "md/raid1:%s: couldn't allocate thread\n",
NeilBrown191ea9b2005-06-21 17:17:23 -07002284 mdname(mddev));
NeilBrown709ae482009-12-14 12:49:51 +11002285 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 }
NeilBrown191ea9b2005-06-21 17:17:23 -07002287
NeilBrown709ae482009-12-14 12:49:51 +11002288 return conf;
2289
2290 abort:
2291 if (conf) {
2292 if (conf->r1bio_pool)
2293 mempool_destroy(conf->r1bio_pool);
2294 kfree(conf->mirrors);
2295 safe_put_page(conf->tmppage);
2296 kfree(conf->poolinfo);
2297 kfree(conf);
2298 }
2299 return ERR_PTR(err);
2300}
2301
2302static int run(mddev_t *mddev)
2303{
2304 conf_t *conf;
2305 int i;
2306 mdk_rdev_t *rdev;
2307
2308 if (mddev->level != 1) {
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002309 printk(KERN_ERR "md/raid1:%s: raid level not set to mirroring (%d)\n",
NeilBrown709ae482009-12-14 12:49:51 +11002310 mdname(mddev), mddev->level);
2311 return -EIO;
2312 }
2313 if (mddev->reshape_position != MaxSector) {
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002314 printk(KERN_ERR "md/raid1:%s: reshape_position set but not supported\n",
NeilBrown709ae482009-12-14 12:49:51 +11002315 mdname(mddev));
2316 return -EIO;
2317 }
2318 /*
2319 * copy the already verified devices into our private RAID1
2320 * bookkeeping area. [whatever we allocate in run(),
2321 * should be freed in stop()]
2322 */
2323 if (mddev->private == NULL)
2324 conf = setup_conf(mddev);
2325 else
2326 conf = mddev->private;
2327
2328 if (IS_ERR(conf))
2329 return PTR_ERR(conf);
2330
NeilBrown709ae482009-12-14 12:49:51 +11002331 list_for_each_entry(rdev, &mddev->disks, same_set) {
Jonathan Brassow1ed72422011-06-07 17:50:35 -05002332 if (!mddev->gendisk)
2333 continue;
NeilBrown709ae482009-12-14 12:49:51 +11002334 disk_stack_limits(mddev->gendisk, rdev->bdev,
2335 rdev->data_offset << 9);
2336 /* as we don't honour merge_bvec_fn, we must never risk
NeilBrown627a2d32010-03-08 16:44:38 +11002337 * violating it, so limit ->max_segments to 1 lying within
2338 * a single page, as a one page request is never in violation.
NeilBrown709ae482009-12-14 12:49:51 +11002339 */
NeilBrown627a2d32010-03-08 16:44:38 +11002340 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2341 blk_queue_max_segments(mddev->queue, 1);
2342 blk_queue_segment_boundary(mddev->queue,
2343 PAGE_CACHE_SIZE - 1);
2344 }
NeilBrown709ae482009-12-14 12:49:51 +11002345 }
2346
2347 mddev->degraded = 0;
2348 for (i=0; i < conf->raid_disks; i++)
2349 if (conf->mirrors[i].rdev == NULL ||
2350 !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
2351 test_bit(Faulty, &conf->mirrors[i].rdev->flags))
2352 mddev->degraded++;
2353
2354 if (conf->raid_disks - mddev->degraded == 1)
2355 mddev->recovery_cp = MaxSector;
2356
Andre Noll8c6ac862009-06-18 08:48:06 +10002357 if (mddev->recovery_cp != MaxSector)
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002358 printk(KERN_NOTICE "md/raid1:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10002359 " -- starting background reconstruction\n",
2360 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 printk(KERN_INFO
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002362 "md/raid1:%s: active with %d out of %d mirrors\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 mdname(mddev), mddev->raid_disks - mddev->degraded,
2364 mddev->raid_disks);
NeilBrown709ae482009-12-14 12:49:51 +11002365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 /*
2367 * Ok, everything is just fine now
2368 */
NeilBrown709ae482009-12-14 12:49:51 +11002369 mddev->thread = conf->thread;
2370 conf->thread = NULL;
2371 mddev->private = conf;
2372
Dan Williams1f403622009-03-31 14:59:03 +11002373 md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Jonathan Brassow1ed72422011-06-07 17:50:35 -05002375 if (mddev->queue) {
2376 mddev->queue->backing_dev_info.congested_fn = raid1_congested;
2377 mddev->queue->backing_dev_info.congested_data = mddev;
2378 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01002379 return md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380}
2381
2382static int stop(mddev_t *mddev)
2383{
NeilBrown070ec552009-06-16 16:54:21 +10002384 conf_t *conf = mddev->private;
NeilBrown4b6d2872005-09-09 16:23:47 -07002385 struct bitmap *bitmap = mddev->bitmap;
NeilBrown4b6d2872005-09-09 16:23:47 -07002386
2387 /* wait for behind writes to complete */
NeilBrowne5551902010-03-31 11:21:44 +11002388 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002389 printk(KERN_INFO "md/raid1:%s: behind writes in progress - waiting to stop.\n",
2390 mdname(mddev));
NeilBrown4b6d2872005-09-09 16:23:47 -07002391 /* need to kick something here to make sure I/O goes? */
NeilBrowne5551902010-03-31 11:21:44 +11002392 wait_event(bitmap->behind_wait,
2393 atomic_read(&bitmap->behind_writes) == 0);
NeilBrown4b6d2872005-09-09 16:23:47 -07002394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
NeilBrown409c57f2009-03-31 14:39:39 +11002396 raise_barrier(conf);
2397 lower_barrier(conf);
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 md_unregister_thread(mddev->thread);
2400 mddev->thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 if (conf->r1bio_pool)
2402 mempool_destroy(conf->r1bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002403 kfree(conf->mirrors);
2404 kfree(conf->poolinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 kfree(conf);
2406 mddev->private = NULL;
2407 return 0;
2408}
2409
2410static int raid1_resize(mddev_t *mddev, sector_t sectors)
2411{
2412 /* no resync is happening, and there is enough space
2413 * on all devices, so we can resize.
2414 * We need to make sure resync covers any new space.
2415 * If the array is shrinking we should possibly wait until
2416 * any io in the removed space completes, but it hardly seems
2417 * worth it.
2418 */
Dan Williams1f403622009-03-31 14:59:03 +11002419 md_set_array_sectors(mddev, raid1_size(mddev, sectors, 0));
Dan Williamsb522adc2009-03-31 15:00:31 +11002420 if (mddev->array_sectors > raid1_size(mddev, sectors, 0))
2421 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10002422 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10002423 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11002424 if (sectors > mddev->dev_sectors &&
NeilBrownb0986362011-05-11 15:52:21 +10002425 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11002426 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2428 }
Dan Williamsb522adc2009-03-31 15:00:31 +11002429 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07002430 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 return 0;
2432}
2433
NeilBrown63c70c42006-03-27 01:18:13 -08002434static int raid1_reshape(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
2436 /* We need to:
2437 * 1/ resize the r1bio_pool
2438 * 2/ resize conf->mirrors
2439 *
2440 * We allocate a new r1bio_pool if we can.
2441 * Then raise a device barrier and wait until all IO stops.
2442 * Then resize conf->mirrors and swap in the new r1bio pool.
NeilBrown6ea9c072005-06-21 17:17:09 -07002443 *
2444 * At the same time, we "pack" the devices so that all the missing
2445 * devices have the higher raid_disk numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 */
2447 mempool_t *newpool, *oldpool;
2448 struct pool_info *newpoolinfo;
2449 mirror_info_t *newmirrors;
NeilBrown070ec552009-06-16 16:54:21 +10002450 conf_t *conf = mddev->private;
NeilBrown63c70c42006-03-27 01:18:13 -08002451 int cnt, raid_disks;
NeilBrownc04be0a2006-10-03 01:15:53 -07002452 unsigned long flags;
Dan Williamsb5470dc2008-06-27 21:44:04 -07002453 int d, d2, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
NeilBrown63c70c42006-03-27 01:18:13 -08002455 /* Cannot change chunk_size, layout, or level */
Andre Noll664e7c42009-06-18 08:45:27 +10002456 if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
NeilBrown63c70c42006-03-27 01:18:13 -08002457 mddev->layout != mddev->new_layout ||
2458 mddev->level != mddev->new_level) {
Andre Noll664e7c42009-06-18 08:45:27 +10002459 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown63c70c42006-03-27 01:18:13 -08002460 mddev->new_layout = mddev->layout;
2461 mddev->new_level = mddev->level;
2462 return -EINVAL;
2463 }
2464
Dan Williamsb5470dc2008-06-27 21:44:04 -07002465 err = md_allow_write(mddev);
2466 if (err)
2467 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08002468
NeilBrown63c70c42006-03-27 01:18:13 -08002469 raid_disks = mddev->raid_disks + mddev->delta_disks;
2470
NeilBrown6ea9c072005-06-21 17:17:09 -07002471 if (raid_disks < conf->raid_disks) {
2472 cnt=0;
2473 for (d= 0; d < conf->raid_disks; d++)
2474 if (conf->mirrors[d].rdev)
2475 cnt++;
2476 if (cnt > raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 return -EBUSY;
NeilBrown6ea9c072005-06-21 17:17:09 -07002478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
2480 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
2481 if (!newpoolinfo)
2482 return -ENOMEM;
2483 newpoolinfo->mddev = mddev;
2484 newpoolinfo->raid_disks = raid_disks;
2485
2486 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
2487 r1bio_pool_free, newpoolinfo);
2488 if (!newpool) {
2489 kfree(newpoolinfo);
2490 return -ENOMEM;
2491 }
NeilBrown9ffae0c2006-01-06 00:20:32 -08002492 newmirrors = kzalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 if (!newmirrors) {
2494 kfree(newpoolinfo);
2495 mempool_destroy(newpool);
2496 return -ENOMEM;
2497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
NeilBrown17999be2006-01-06 00:20:12 -08002499 raise_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
2501 /* ok, everything is stopped */
2502 oldpool = conf->r1bio_pool;
2503 conf->r1bio_pool = newpool;
NeilBrown6ea9c072005-06-21 17:17:09 -07002504
NeilBrowna88aa782007-08-22 14:01:53 -07002505 for (d = d2 = 0; d < conf->raid_disks; d++) {
2506 mdk_rdev_t *rdev = conf->mirrors[d].rdev;
2507 if (rdev && rdev->raid_disk != d2) {
Namhyung Kim36fad852011-07-27 11:00:36 +10002508 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna88aa782007-08-22 14:01:53 -07002509 rdev->raid_disk = d2;
Namhyung Kim36fad852011-07-27 11:00:36 +10002510 sysfs_unlink_rdev(mddev, rdev);
2511 if (sysfs_link_rdev(mddev, rdev))
NeilBrowna88aa782007-08-22 14:01:53 -07002512 printk(KERN_WARNING
Namhyung Kim36fad852011-07-27 11:00:36 +10002513 "md/raid1:%s: cannot register rd%d\n",
2514 mdname(mddev), rdev->raid_disk);
NeilBrown6ea9c072005-06-21 17:17:09 -07002515 }
NeilBrowna88aa782007-08-22 14:01:53 -07002516 if (rdev)
2517 newmirrors[d2++].rdev = rdev;
2518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 kfree(conf->mirrors);
2520 conf->mirrors = newmirrors;
2521 kfree(conf->poolinfo);
2522 conf->poolinfo = newpoolinfo;
2523
NeilBrownc04be0a2006-10-03 01:15:53 -07002524 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 mddev->degraded += (raid_disks - conf->raid_disks);
NeilBrownc04be0a2006-10-03 01:15:53 -07002526 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 conf->raid_disks = mddev->raid_disks = raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08002528 mddev->delta_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
NeilBrown6ea9c072005-06-21 17:17:09 -07002530 conf->last_used = 0; /* just make sure it is in-range */
NeilBrown17999be2006-01-06 00:20:12 -08002531 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
2533 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2534 md_wakeup_thread(mddev->thread);
2535
2536 mempool_destroy(oldpool);
2537 return 0;
2538}
2539
NeilBrown500af872005-09-09 16:23:58 -07002540static void raid1_quiesce(mddev_t *mddev, int state)
NeilBrown36fa3062005-09-09 16:23:45 -07002541{
NeilBrown070ec552009-06-16 16:54:21 +10002542 conf_t *conf = mddev->private;
NeilBrown36fa3062005-09-09 16:23:45 -07002543
2544 switch(state) {
NeilBrown6eef4b22009-12-14 12:49:51 +11002545 case 2: /* wake for suspend */
2546 wake_up(&conf->wait_barrier);
2547 break;
NeilBrown9e6603d2005-09-09 16:23:48 -07002548 case 1:
NeilBrown17999be2006-01-06 00:20:12 -08002549 raise_barrier(conf);
NeilBrown36fa3062005-09-09 16:23:45 -07002550 break;
NeilBrown9e6603d2005-09-09 16:23:48 -07002551 case 0:
NeilBrown17999be2006-01-06 00:20:12 -08002552 lower_barrier(conf);
NeilBrown36fa3062005-09-09 16:23:45 -07002553 break;
2554 }
NeilBrown36fa3062005-09-09 16:23:45 -07002555}
2556
NeilBrown709ae482009-12-14 12:49:51 +11002557static void *raid1_takeover(mddev_t *mddev)
2558{
2559 /* raid1 can take over:
2560 * raid5 with 2 devices, any layout or chunk size
2561 */
2562 if (mddev->level == 5 && mddev->raid_disks == 2) {
2563 conf_t *conf;
2564 mddev->new_level = 1;
2565 mddev->new_layout = 0;
2566 mddev->new_chunk_sectors = 0;
2567 conf = setup_conf(mddev);
2568 if (!IS_ERR(conf))
2569 conf->barrier = 1;
2570 return conf;
2571 }
2572 return ERR_PTR(-EINVAL);
2573}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
NeilBrown2604b702006-01-06 00:20:36 -08002575static struct mdk_personality raid1_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576{
2577 .name = "raid1",
NeilBrown2604b702006-01-06 00:20:36 -08002578 .level = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 .owner = THIS_MODULE,
2580 .make_request = make_request,
2581 .run = run,
2582 .stop = stop,
2583 .status = status,
2584 .error_handler = error,
2585 .hot_add_disk = raid1_add_disk,
2586 .hot_remove_disk= raid1_remove_disk,
2587 .spare_active = raid1_spare_active,
2588 .sync_request = sync_request,
2589 .resize = raid1_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07002590 .size = raid1_size,
NeilBrown63c70c42006-03-27 01:18:13 -08002591 .check_reshape = raid1_reshape,
NeilBrown36fa3062005-09-09 16:23:45 -07002592 .quiesce = raid1_quiesce,
NeilBrown709ae482009-12-14 12:49:51 +11002593 .takeover = raid1_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594};
2595
2596static int __init raid_init(void)
2597{
NeilBrown2604b702006-01-06 00:20:36 -08002598 return register_md_personality(&raid1_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599}
2600
2601static void raid_exit(void)
2602{
NeilBrown2604b702006-01-06 00:20:36 -08002603 unregister_md_personality(&raid1_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604}
2605
2606module_init(raid_init);
2607module_exit(raid_exit);
2608MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11002609MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610MODULE_ALIAS("md-personality-3"); /* RAID1 */
NeilBrownd9d166c2006-01-06 00:20:51 -08002611MODULE_ALIAS("md-raid1");
NeilBrown2604b702006-01-06 00:20:36 -08002612MODULE_ALIAS("md-level-1");