blob: 77fab07abcee92607688bd4dcc945b5231776ed3 [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
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100271/*
272 * Find the disk number which triggered given bio
273 */
274static int find_bio_disk(r1bio_t *r1_bio, struct bio *bio)
275{
276 int mirror;
277 int raid_disks = r1_bio->mddev->raid_disks;
278
279 for (mirror = 0; mirror < raid_disks; mirror++)
280 if (r1_bio->bios[mirror] == bio)
281 break;
282
283 BUG_ON(mirror == raid_disks);
284 update_head_pos(mirror, r1_bio);
285
286 return mirror;
287}
288
NeilBrown6712ecf2007-09-27 12:47:43 +0200289static void raid1_end_read_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100292 r1bio_t *r1_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 int mirror;
NeilBrown070ec552009-06-16 16:54:21 +1000294 conf_t *conf = r1_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 mirror = r1_bio->read_disk;
297 /*
298 * this branch is our 'one mirror IO has finished' event handler:
299 */
NeilBrownddaf22a2006-01-06 00:20:19 -0800300 update_head_pos(mirror, r1_bio);
301
NeilBrowndd00a992007-05-10 03:15:50 -0700302 if (uptodate)
303 set_bit(R1BIO_Uptodate, &r1_bio->state);
304 else {
305 /* If all other devices have failed, we want to return
306 * the error upwards rather than fail the last device.
307 * Here we redefine "uptodate" to mean "Don't want to retry"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 */
NeilBrowndd00a992007-05-10 03:15:50 -0700309 unsigned long flags;
310 spin_lock_irqsave(&conf->device_lock, flags);
311 if (r1_bio->mddev->degraded == conf->raid_disks ||
312 (r1_bio->mddev->degraded == conf->raid_disks-1 &&
313 !test_bit(Faulty, &conf->mirrors[mirror].rdev->flags)))
314 uptodate = 1;
315 spin_unlock_irqrestore(&conf->device_lock, flags);
316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
NeilBrowndd00a992007-05-10 03:15:50 -0700318 if (uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 raid_end_bio_io(r1_bio);
NeilBrowndd00a992007-05-10 03:15:50 -0700320 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 /*
322 * oops, read error:
323 */
324 char b[BDEVNAME_SIZE];
Christian Dietrich8bda4702011-07-27 11:00:36 +1000325 printk_ratelimited(
326 KERN_ERR "md/raid1:%s: %s: "
327 "rescheduling sector %llu\n",
328 mdname(conf->mddev),
329 bdevname(conf->mirrors[mirror].rdev->bdev,
330 b),
331 (unsigned long long)r1_bio->sector);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000332 set_bit(R1BIO_ReadError, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 reschedule_retry(r1_bio);
334 }
335
336 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
NeilBrowncd5ff9a12011-07-28 11:32:41 +1000339static void close_write(r1bio_t *r1_bio)
340{
341 /* it really is the end of this request */
342 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
343 /* free extra copy of the data pages */
344 int i = r1_bio->behind_page_count;
345 while (i--)
346 safe_put_page(r1_bio->behind_bvecs[i].bv_page);
347 kfree(r1_bio->behind_bvecs);
348 r1_bio->behind_bvecs = NULL;
349 }
350 /* clear the bitmap if all writes complete successfully */
351 bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
352 r1_bio->sectors,
353 !test_bit(R1BIO_Degraded, &r1_bio->state),
354 test_bit(R1BIO_BehindIO, &r1_bio->state));
355 md_write_end(r1_bio->mddev);
356}
357
NeilBrownaf6d7b72011-05-11 14:51:19 +1000358static void r1_bio_write_done(r1bio_t *r1_bio)
NeilBrown4e780642010-10-19 12:54:01 +1100359{
NeilBrowncd5ff9a12011-07-28 11:32:41 +1000360 if (!atomic_dec_and_test(&r1_bio->remaining))
361 return;
362
363 if (test_bit(R1BIO_WriteError, &r1_bio->state))
364 reschedule_retry(r1_bio);
365 else {
366 close_write(r1_bio);
NeilBrown4367af52011-07-28 11:31:49 +1000367 if (test_bit(R1BIO_MadeGood, &r1_bio->state))
368 reschedule_retry(r1_bio);
369 else
370 raid_end_bio_io(r1_bio);
NeilBrown4e780642010-10-19 12:54:01 +1100371 }
372}
373
NeilBrown6712ecf2007-09-27 12:47:43 +0200374static void raid1_end_write_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100377 r1bio_t *r1_bio = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800378 int mirror, behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
NeilBrown070ec552009-06-16 16:54:21 +1000379 conf_t *conf = r1_bio->mddev->private;
NeilBrown04b857f2006-03-09 17:33:46 -0800380 struct bio *to_put = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100382 mirror = find_bio_disk(r1_bio, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Tejun Heoe9c74692010-09-03 11:56:18 +0200384 /*
385 * 'one mirror IO has finished' event handler:
386 */
Tejun Heoe9c74692010-09-03 11:56:18 +0200387 if (!uptodate) {
NeilBrowncd5ff9a12011-07-28 11:32:41 +1000388 set_bit(WriteErrorSeen,
389 &conf->mirrors[mirror].rdev->flags);
390 set_bit(R1BIO_WriteError, &r1_bio->state);
NeilBrown4367af52011-07-28 11:31:49 +1000391 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /*
Tejun Heoe9c74692010-09-03 11:56:18 +0200393 * Set R1BIO_Uptodate in our master bio, so that we
394 * will return a good error code for to the higher
395 * levels even if IO on some other mirrored buffer
396 * fails.
397 *
398 * The 'master' represents the composite IO operation
399 * to user-side. So if something waits for IO, then it
400 * will wait for the 'master' bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 */
NeilBrown4367af52011-07-28 11:31:49 +1000402 sector_t first_bad;
403 int bad_sectors;
404
NeilBrowncd5ff9a12011-07-28 11:32:41 +1000405 r1_bio->bios[mirror] = NULL;
406 to_put = bio;
Tejun Heoe9c74692010-09-03 11:56:18 +0200407 set_bit(R1BIO_Uptodate, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
NeilBrown4367af52011-07-28 11:31:49 +1000409 /* Maybe we can clear some bad blocks. */
410 if (is_badblock(conf->mirrors[mirror].rdev,
411 r1_bio->sector, r1_bio->sectors,
412 &first_bad, &bad_sectors)) {
413 r1_bio->bios[mirror] = IO_MADE_GOOD;
414 set_bit(R1BIO_MadeGood, &r1_bio->state);
415 }
416 }
417
Tejun Heoe9c74692010-09-03 11:56:18 +0200418 if (behind) {
419 if (test_bit(WriteMostly, &conf->mirrors[mirror].rdev->flags))
420 atomic_dec(&r1_bio->behind_remaining);
NeilBrown4b6d2872005-09-09 16:23:47 -0700421
Tejun Heoe9c74692010-09-03 11:56:18 +0200422 /*
423 * In behind mode, we ACK the master bio once the I/O
424 * has safely reached all non-writemostly
425 * disks. Setting the Returned bit ensures that this
426 * gets done only once -- we don't ever want to return
427 * -EIO here, instead we'll wait
428 */
429 if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
430 test_bit(R1BIO_Uptodate, &r1_bio->state)) {
431 /* Maybe we can return now */
432 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
433 struct bio *mbio = r1_bio->master_bio;
434 PRINTK(KERN_DEBUG "raid1: behind end write sectors %llu-%llu\n",
435 (unsigned long long) mbio->bi_sector,
436 (unsigned long long) mbio->bi_sector +
437 (mbio->bi_size >> 9) - 1);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000438 call_bio_endio(r1_bio);
NeilBrown4b6d2872005-09-09 16:23:47 -0700439 }
440 }
441 }
NeilBrown4367af52011-07-28 11:31:49 +1000442 if (r1_bio->bios[mirror] == NULL)
443 rdev_dec_pending(conf->mirrors[mirror].rdev,
444 conf->mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 * Let's see if all mirrored write operations have finished
448 * already.
449 */
NeilBrownaf6d7b72011-05-11 14:51:19 +1000450 r1_bio_write_done(r1_bio);
NeilBrownc70810b2006-06-26 00:27:35 -0700451
NeilBrown04b857f2006-03-09 17:33:46 -0800452 if (to_put)
453 bio_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456
457/*
458 * This routine returns the disk from which the requested read should
459 * be done. There is a per-array 'next expected sequential IO' sector
460 * number - if this matches on the next IO then we use the last disk.
461 * There is also a per-disk 'last know head position' sector that is
462 * maintained from IRQ contexts, both the normal and the resync IO
463 * completion handlers update this position correctly. If there is no
464 * perfect sequential match then we pick the disk whose head is closest.
465 *
466 * If there are 2 mirrors in the same 2 devices, performance degrades
467 * because position is mirror, not device based.
468 *
469 * The rdev for the device selected will have nr_pending incremented.
470 */
NeilBrownd2eb35a2011-07-28 11:31:48 +1000471static int read_balance(conf_t *conf, r1bio_t *r1_bio, int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000473 const sector_t this_sector = r1_bio->sector;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000474 int sectors;
475 int best_good_sectors;
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000476 int start_disk;
NeilBrown76073052011-05-11 14:34:56 +1000477 int best_disk;
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000478 int i;
NeilBrown76073052011-05-11 14:34:56 +1000479 sector_t best_dist;
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700480 mdk_rdev_t *rdev;
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000481 int choose_first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 rcu_read_lock();
484 /*
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700485 * Check if we can balance. We can balance on the whole
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * device if no resync is going on, or below the resync window.
487 * We take the first readable disk when above the resync window.
488 */
489 retry:
NeilBrownd2eb35a2011-07-28 11:31:48 +1000490 sectors = r1_bio->sectors;
NeilBrown76073052011-05-11 14:34:56 +1000491 best_disk = -1;
492 best_dist = MaxSector;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000493 best_good_sectors = 0;
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (conf->mddev->recovery_cp < MaxSector &&
496 (this_sector + sectors >= conf->next_resync)) {
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000497 choose_first = 1;
498 start_disk = 0;
499 } else {
500 choose_first = 0;
501 start_disk = conf->last_used;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000504 for (i = 0 ; i < conf->raid_disks ; i++) {
NeilBrown76073052011-05-11 14:34:56 +1000505 sector_t dist;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000506 sector_t first_bad;
507 int bad_sectors;
508
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000509 int disk = start_disk + i;
510 if (disk >= conf->raid_disks)
511 disk -= conf->raid_disks;
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700512
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000513 rdev = rcu_dereference(conf->mirrors[disk].rdev);
514 if (r1_bio->bios[disk] == IO_BLOCKED
515 || rdev == NULL
NeilBrown76073052011-05-11 14:34:56 +1000516 || test_bit(Faulty, &rdev->flags))
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000517 continue;
NeilBrown76073052011-05-11 14:34:56 +1000518 if (!test_bit(In_sync, &rdev->flags) &&
519 rdev->recovery_offset < this_sector + sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 continue;
NeilBrown76073052011-05-11 14:34:56 +1000521 if (test_bit(WriteMostly, &rdev->flags)) {
522 /* Don't balance among write-mostly, just
523 * use the first as a last resort */
524 if (best_disk < 0)
525 best_disk = disk;
526 continue;
527 }
528 /* This is a reasonable device to use. It might
529 * even be best.
530 */
NeilBrownd2eb35a2011-07-28 11:31:48 +1000531 if (is_badblock(rdev, this_sector, sectors,
532 &first_bad, &bad_sectors)) {
533 if (best_dist < MaxSector)
534 /* already have a better device */
535 continue;
536 if (first_bad <= this_sector) {
537 /* cannot read here. If this is the 'primary'
538 * device, then we must not read beyond
539 * bad_sectors from another device..
540 */
541 bad_sectors -= (this_sector - first_bad);
542 if (choose_first && sectors > bad_sectors)
543 sectors = bad_sectors;
544 if (best_good_sectors > sectors)
545 best_good_sectors = sectors;
546
547 } else {
548 sector_t good_sectors = first_bad - this_sector;
549 if (good_sectors > best_good_sectors) {
550 best_good_sectors = good_sectors;
551 best_disk = disk;
552 }
553 if (choose_first)
554 break;
555 }
556 continue;
557 } else
558 best_good_sectors = sectors;
559
NeilBrown76073052011-05-11 14:34:56 +1000560 dist = abs(this_sector - conf->mirrors[disk].head_position);
561 if (choose_first
562 /* Don't change to another disk for sequential reads */
563 || conf->next_seq_sect == this_sector
564 || dist == 0
565 /* If device is idle, use it */
566 || atomic_read(&rdev->nr_pending) == 0) {
567 best_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 break;
569 }
NeilBrown76073052011-05-11 14:34:56 +1000570 if (dist < best_dist) {
571 best_dist = dist;
572 best_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
NeilBrown76073052011-05-11 14:34:56 +1000576 if (best_disk >= 0) {
577 rdev = rcu_dereference(conf->mirrors[best_disk].rdev);
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700578 if (!rdev)
579 goto retry;
580 atomic_inc(&rdev->nr_pending);
NeilBrown76073052011-05-11 14:34:56 +1000581 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* cannot risk returning a device that failed
583 * before we inc'ed nr_pending
584 */
NeilBrown03c902e2006-01-06 00:20:46 -0800585 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 goto retry;
587 }
NeilBrownd2eb35a2011-07-28 11:31:48 +1000588 sectors = best_good_sectors;
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700589 conf->next_seq_sect = this_sector + sectors;
NeilBrown76073052011-05-11 14:34:56 +1000590 conf->last_used = best_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592 rcu_read_unlock();
NeilBrownd2eb35a2011-07-28 11:31:48 +1000593 *max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
NeilBrown76073052011-05-11 14:34:56 +1000595 return best_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
Jonathan Brassow1ed72422011-06-07 17:50:35 -0500598int md_raid1_congested(mddev_t *mddev, int bits)
NeilBrown0d129222006-10-03 01:15:54 -0700599{
NeilBrown070ec552009-06-16 16:54:21 +1000600 conf_t *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700601 int i, ret = 0;
602
603 rcu_read_lock();
604 for (i = 0; i < mddev->raid_disks; i++) {
605 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
606 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200607 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700608
Jonathan Brassow1ed72422011-06-07 17:50:35 -0500609 BUG_ON(!q);
610
NeilBrown0d129222006-10-03 01:15:54 -0700611 /* Note the '|| 1' - when read_balance prefers
612 * non-congested targets, it can be removed
613 */
Alexander Beregalov91a9e992009-04-07 01:35:56 +0400614 if ((bits & (1<<BDI_async_congested)) || 1)
NeilBrown0d129222006-10-03 01:15:54 -0700615 ret |= bdi_congested(&q->backing_dev_info, bits);
616 else
617 ret &= bdi_congested(&q->backing_dev_info, bits);
618 }
619 }
620 rcu_read_unlock();
621 return ret;
622}
Jonathan Brassow1ed72422011-06-07 17:50:35 -0500623EXPORT_SYMBOL_GPL(md_raid1_congested);
NeilBrown0d129222006-10-03 01:15:54 -0700624
Jonathan Brassow1ed72422011-06-07 17:50:35 -0500625static int raid1_congested(void *data, int bits)
626{
627 mddev_t *mddev = data;
628
629 return mddev_congested(mddev, bits) ||
630 md_raid1_congested(mddev, bits);
631}
NeilBrown0d129222006-10-03 01:15:54 -0700632
Jens Axboe7eaceac2011-03-10 08:52:07 +0100633static void flush_pending_writes(conf_t *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800634{
635 /* Any writes that have been queued but are awaiting
636 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800637 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800638 spin_lock_irq(&conf->device_lock);
639
640 if (conf->pending_bio_list.head) {
641 struct bio *bio;
642 bio = bio_list_get(&conf->pending_bio_list);
NeilBrowna35e63e2008-03-04 14:29:29 -0800643 spin_unlock_irq(&conf->device_lock);
644 /* flush any pending bitmap writes to
645 * disk before proceeding w/ I/O */
646 bitmap_unplug(conf->mddev->bitmap);
647
648 while (bio) { /* submit pending writes */
649 struct bio *next = bio->bi_next;
650 bio->bi_next = NULL;
651 generic_make_request(bio);
652 bio = next;
653 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800654 } else
655 spin_unlock_irq(&conf->device_lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100656}
657
NeilBrown17999be2006-01-06 00:20:12 -0800658/* Barriers....
659 * Sometimes we need to suspend IO while we do something else,
660 * either some resync/recovery, or reconfigure the array.
661 * To do this we raise a 'barrier'.
662 * The 'barrier' is a counter that can be raised multiple times
663 * to count how many activities are happening which preclude
664 * normal IO.
665 * We can only raise the barrier if there is no pending IO.
666 * i.e. if nr_pending == 0.
667 * We choose only to raise the barrier if no-one is waiting for the
668 * barrier to go down. This means that as soon as an IO request
669 * is ready, no other operations which require a barrier will start
670 * until the IO request has had a chance.
671 *
672 * So: regular IO calls 'wait_barrier'. When that returns there
673 * is no backgroup IO happening, It must arrange to call
674 * allow_barrier when it has finished its IO.
675 * backgroup IO calls must call raise_barrier. Once that returns
676 * there is no normal IO happeing. It must arrange to call
677 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 */
679#define RESYNC_DEPTH 32
680
NeilBrown17999be2006-01-06 00:20:12 -0800681static void raise_barrier(conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 spin_lock_irq(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800684
685 /* Wait until no block IO is waiting */
686 wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
NeilBrownc3b328a2011-04-18 18:25:43 +1000687 conf->resync_lock, );
NeilBrown17999be2006-01-06 00:20:12 -0800688
689 /* block any new IO from starting */
690 conf->barrier++;
691
NeilBrown046abee2010-10-26 15:46:20 +1100692 /* Now wait for all pending IO to complete */
NeilBrown17999be2006-01-06 00:20:12 -0800693 wait_event_lock_irq(conf->wait_barrier,
694 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
NeilBrownc3b328a2011-04-18 18:25:43 +1000695 conf->resync_lock, );
NeilBrown17999be2006-01-06 00:20:12 -0800696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 spin_unlock_irq(&conf->resync_lock);
698}
699
NeilBrown17999be2006-01-06 00:20:12 -0800700static void lower_barrier(conf_t *conf)
701{
702 unsigned long flags;
NeilBrown709ae482009-12-14 12:49:51 +1100703 BUG_ON(conf->barrier <= 0);
NeilBrown17999be2006-01-06 00:20:12 -0800704 spin_lock_irqsave(&conf->resync_lock, flags);
705 conf->barrier--;
706 spin_unlock_irqrestore(&conf->resync_lock, flags);
707 wake_up(&conf->wait_barrier);
708}
709
710static void wait_barrier(conf_t *conf)
711{
712 spin_lock_irq(&conf->resync_lock);
713 if (conf->barrier) {
714 conf->nr_waiting++;
715 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
716 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000717 );
NeilBrown17999be2006-01-06 00:20:12 -0800718 conf->nr_waiting--;
719 }
720 conf->nr_pending++;
721 spin_unlock_irq(&conf->resync_lock);
722}
723
724static void allow_barrier(conf_t *conf)
725{
726 unsigned long flags;
727 spin_lock_irqsave(&conf->resync_lock, flags);
728 conf->nr_pending--;
729 spin_unlock_irqrestore(&conf->resync_lock, flags);
730 wake_up(&conf->wait_barrier);
731}
732
NeilBrownddaf22a2006-01-06 00:20:19 -0800733static void freeze_array(conf_t *conf)
734{
735 /* stop syncio and normal IO and wait for everything to
736 * go quite.
737 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800738 * wait until nr_pending match nr_queued+1
739 * This is called in the context of one normal IO request
740 * that has failed. Thus any sync request that might be pending
741 * will be blocked by nr_pending, and we need to wait for
742 * pending IO requests to complete or be queued for re-try.
743 * Thus the number queued (nr_queued) plus this request (1)
744 * must match the number of pending IOs (nr_pending) before
745 * we continue.
NeilBrownddaf22a2006-01-06 00:20:19 -0800746 */
747 spin_lock_irq(&conf->resync_lock);
748 conf->barrier++;
749 conf->nr_waiting++;
750 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -0800751 conf->nr_pending == conf->nr_queued+1,
NeilBrownddaf22a2006-01-06 00:20:19 -0800752 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000753 flush_pending_writes(conf));
NeilBrownddaf22a2006-01-06 00:20:19 -0800754 spin_unlock_irq(&conf->resync_lock);
755}
756static void unfreeze_array(conf_t *conf)
757{
758 /* reverse the effect of the freeze */
759 spin_lock_irq(&conf->resync_lock);
760 conf->barrier--;
761 conf->nr_waiting--;
762 wake_up(&conf->wait_barrier);
763 spin_unlock_irq(&conf->resync_lock);
764}
765
NeilBrown17999be2006-01-06 00:20:12 -0800766
NeilBrown4e780642010-10-19 12:54:01 +1100767/* duplicate the data pages for behind I/O
NeilBrown4e780642010-10-19 12:54:01 +1100768 */
NeilBrownaf6d7b72011-05-11 14:51:19 +1000769static void alloc_behind_pages(struct bio *bio, r1bio_t *r1_bio)
NeilBrown4b6d2872005-09-09 16:23:47 -0700770{
771 int i;
772 struct bio_vec *bvec;
NeilBrown2ca68f52011-07-28 11:32:10 +1000773 struct bio_vec *bvecs = kzalloc(bio->bi_vcnt * sizeof(struct bio_vec),
NeilBrown4b6d2872005-09-09 16:23:47 -0700774 GFP_NOIO);
NeilBrown2ca68f52011-07-28 11:32:10 +1000775 if (unlikely(!bvecs))
NeilBrownaf6d7b72011-05-11 14:51:19 +1000776 return;
NeilBrown4b6d2872005-09-09 16:23:47 -0700777
NeilBrown4b6d2872005-09-09 16:23:47 -0700778 bio_for_each_segment(bvec, bio, i) {
NeilBrown2ca68f52011-07-28 11:32:10 +1000779 bvecs[i] = *bvec;
780 bvecs[i].bv_page = alloc_page(GFP_NOIO);
781 if (unlikely(!bvecs[i].bv_page))
NeilBrown4b6d2872005-09-09 16:23:47 -0700782 goto do_sync_io;
NeilBrown2ca68f52011-07-28 11:32:10 +1000783 memcpy(kmap(bvecs[i].bv_page) + bvec->bv_offset,
784 kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
785 kunmap(bvecs[i].bv_page);
NeilBrown4b6d2872005-09-09 16:23:47 -0700786 kunmap(bvec->bv_page);
787 }
NeilBrown2ca68f52011-07-28 11:32:10 +1000788 r1_bio->behind_bvecs = bvecs;
NeilBrownaf6d7b72011-05-11 14:51:19 +1000789 r1_bio->behind_page_count = bio->bi_vcnt;
790 set_bit(R1BIO_BehindIO, &r1_bio->state);
791 return;
NeilBrown4b6d2872005-09-09 16:23:47 -0700792
793do_sync_io:
NeilBrownaf6d7b72011-05-11 14:51:19 +1000794 for (i = 0; i < bio->bi_vcnt; i++)
NeilBrown2ca68f52011-07-28 11:32:10 +1000795 if (bvecs[i].bv_page)
796 put_page(bvecs[i].bv_page);
797 kfree(bvecs);
NeilBrown4b6d2872005-09-09 16:23:47 -0700798 PRINTK("%dB behind alloc failed, doing sync I/O\n", bio->bi_size);
NeilBrown4b6d2872005-09-09 16:23:47 -0700799}
800
NeilBrown21a52c62010-04-01 15:02:13 +1100801static int make_request(mddev_t *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
NeilBrown070ec552009-06-16 16:54:21 +1000803 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 mirror_info_t *mirror;
805 r1bio_t *r1_bio;
806 struct bio *read_bio;
NeilBrown1f68f0c2011-07-28 11:31:48 +1000807 int i, disks;
NeilBrown84255d12008-05-23 13:04:32 -0700808 struct bitmap *bitmap;
NeilBrown191ea9b2005-06-21 17:17:23 -0700809 unsigned long flags;
Jens Axboea3623572005-11-01 09:26:16 +0100810 const int rw = bio_data_dir(bio);
NeilBrown2c7d46e2010-08-18 16:16:05 +1000811 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
Tejun Heoe9c74692010-09-03 11:56:18 +0200812 const unsigned long do_flush_fua = (bio->bi_rw & (REQ_FLUSH | REQ_FUA));
Dan Williams6bfe0b42008-04-30 00:52:32 -0700813 mdk_rdev_t *blocked_rdev;
NeilBrownc3b328a2011-04-18 18:25:43 +1000814 int plugged;
NeilBrown1f68f0c2011-07-28 11:31:48 +1000815 int first_clone;
816 int sectors_handled;
817 int max_sectors;
NeilBrown191ea9b2005-06-21 17:17:23 -0700818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /*
820 * Register the new request and wait if the reconstruction
821 * thread has put up a bar for new requests.
822 * Continue immediately if no resync is active currently.
823 */
NeilBrown62de6082006-05-01 12:15:47 -0700824
NeilBrown3d310eb2005-06-21 17:17:26 -0700825 md_write_start(mddev, bio); /* wait on superblock update early */
826
NeilBrown6eef4b22009-12-14 12:49:51 +1100827 if (bio_data_dir(bio) == WRITE &&
828 bio->bi_sector + bio->bi_size/512 > mddev->suspend_lo &&
829 bio->bi_sector < mddev->suspend_hi) {
830 /* As the suspend_* range is controlled by
831 * userspace, we want an interruptible
832 * wait.
833 */
834 DEFINE_WAIT(w);
835 for (;;) {
836 flush_signals(current);
837 prepare_to_wait(&conf->wait_barrier,
838 &w, TASK_INTERRUPTIBLE);
839 if (bio->bi_sector + bio->bi_size/512 <= mddev->suspend_lo ||
840 bio->bi_sector >= mddev->suspend_hi)
841 break;
842 schedule();
843 }
844 finish_wait(&conf->wait_barrier, &w);
845 }
NeilBrown62de6082006-05-01 12:15:47 -0700846
NeilBrown17999be2006-01-06 00:20:12 -0800847 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
NeilBrown84255d12008-05-23 13:04:32 -0700849 bitmap = mddev->bitmap;
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 /*
852 * make_request() can abort the operation when READA is being
853 * used and no empty request is available.
854 *
855 */
856 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
857
858 r1_bio->master_bio = bio;
859 r1_bio->sectors = bio->bi_size >> 9;
NeilBrown191ea9b2005-06-21 17:17:23 -0700860 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 r1_bio->mddev = mddev;
862 r1_bio->sector = bio->bi_sector;
863
NeilBrownd2eb35a2011-07-28 11:31:48 +1000864 /* We might need to issue multiple reads to different
865 * devices if there are bad blocks around, so we keep
866 * track of the number of reads in bio->bi_phys_segments.
867 * If this is 0, there is only one r1_bio and no locking
868 * will be needed when requests complete. If it is
869 * non-zero, then it is the number of not-completed requests.
870 */
871 bio->bi_phys_segments = 0;
872 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
873
Jens Axboea3623572005-11-01 09:26:16 +0100874 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 /*
876 * read balancing logic:
877 */
NeilBrownd2eb35a2011-07-28 11:31:48 +1000878 int rdisk;
879
880read_again:
881 rdisk = read_balance(conf, r1_bio, &max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 if (rdisk < 0) {
884 /* couldn't find anywhere to read from */
885 raid_end_bio_io(r1_bio);
886 return 0;
887 }
888 mirror = conf->mirrors + rdisk;
889
NeilBrowne5551902010-03-31 11:21:44 +1100890 if (test_bit(WriteMostly, &mirror->rdev->flags) &&
891 bitmap) {
892 /* Reading from a write-mostly device must
893 * take care not to over-take any writes
894 * that are 'behind'
895 */
896 wait_event(bitmap->behind_wait,
897 atomic_read(&bitmap->behind_writes) == 0);
898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 r1_bio->read_disk = rdisk;
900
NeilBrowna167f662010-10-26 18:31:13 +1100901 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000902 md_trim_bio(read_bio, r1_bio->sector - bio->bi_sector,
903 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 r1_bio->bios[rdisk] = read_bio;
906
907 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
908 read_bio->bi_bdev = mirror->rdev->bdev;
909 read_bio->bi_end_io = raid1_end_read_request;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200910 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 read_bio->bi_private = r1_bio;
912
NeilBrownd2eb35a2011-07-28 11:31:48 +1000913 if (max_sectors < r1_bio->sectors) {
914 /* could not read all from this device, so we will
915 * need another r1_bio.
916 */
NeilBrownd2eb35a2011-07-28 11:31:48 +1000917
918 sectors_handled = (r1_bio->sector + max_sectors
919 - bio->bi_sector);
920 r1_bio->sectors = max_sectors;
921 spin_lock_irq(&conf->device_lock);
922 if (bio->bi_phys_segments == 0)
923 bio->bi_phys_segments = 2;
924 else
925 bio->bi_phys_segments++;
926 spin_unlock_irq(&conf->device_lock);
927 /* Cannot call generic_make_request directly
928 * as that will be queued in __make_request
929 * and subsequent mempool_alloc might block waiting
930 * for it. So hand bio over to raid1d.
931 */
932 reschedule_retry(r1_bio);
933
934 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
935
936 r1_bio->master_bio = bio;
937 r1_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
938 r1_bio->state = 0;
939 r1_bio->mddev = mddev;
940 r1_bio->sector = bio->bi_sector + sectors_handled;
941 goto read_again;
942 } else
943 generic_make_request(read_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return 0;
945 }
946
947 /*
948 * WRITE:
949 */
NeilBrown1f68f0c2011-07-28 11:31:48 +1000950 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 * inc refcount on their rdev. Record them by setting
952 * bios[x] to bio
NeilBrown1f68f0c2011-07-28 11:31:48 +1000953 * If there are known/acknowledged bad blocks on any device on
954 * which we have seen a write error, we want to avoid writing those
955 * blocks.
956 * This potentially requires several writes to write around
957 * the bad blocks. Each set of writes gets it's own r1bio
958 * with a set of bios attached.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 */
NeilBrownc3b328a2011-04-18 18:25:43 +1000960 plugged = mddev_check_plugged(mddev);
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 disks = conf->raid_disks;
Dan Williams6bfe0b42008-04-30 00:52:32 -0700963 retry_write:
964 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 rcu_read_lock();
NeilBrown1f68f0c2011-07-28 11:31:48 +1000966 max_sectors = r1_bio->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 for (i = 0; i < disks; i++) {
Dan Williams6bfe0b42008-04-30 00:52:32 -0700968 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
969 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
970 atomic_inc(&rdev->nr_pending);
971 blocked_rdev = rdev;
972 break;
973 }
NeilBrown1f68f0c2011-07-28 11:31:48 +1000974 r1_bio->bios[i] = NULL;
975 if (!rdev || test_bit(Faulty, &rdev->flags)) {
976 set_bit(R1BIO_Degraded, &r1_bio->state);
977 continue;
978 }
979
980 atomic_inc(&rdev->nr_pending);
981 if (test_bit(WriteErrorSeen, &rdev->flags)) {
982 sector_t first_bad;
983 int bad_sectors;
984 int is_bad;
985
986 is_bad = is_badblock(rdev, r1_bio->sector,
987 max_sectors,
988 &first_bad, &bad_sectors);
989 if (is_bad < 0) {
990 /* mustn't write here until the bad block is
991 * acknowledged*/
992 set_bit(BlockedBadBlocks, &rdev->flags);
993 blocked_rdev = rdev;
994 break;
NeilBrown964147d2010-05-18 15:27:13 +1000995 }
NeilBrown1f68f0c2011-07-28 11:31:48 +1000996 if (is_bad && first_bad <= r1_bio->sector) {
997 /* Cannot write here at all */
998 bad_sectors -= (r1_bio->sector - first_bad);
999 if (bad_sectors < max_sectors)
1000 /* mustn't write more than bad_sectors
1001 * to other devices yet
1002 */
1003 max_sectors = bad_sectors;
1004 rdev_dec_pending(rdev, mddev);
1005 /* We don't set R1BIO_Degraded as that
1006 * only applies if the disk is
1007 * missing, so it might be re-added,
1008 * and we want to know to recover this
1009 * chunk.
1010 * In this case the device is here,
1011 * and the fact that this chunk is not
1012 * in-sync is recorded in the bad
1013 * block log
1014 */
1015 continue;
1016 }
1017 if (is_bad) {
1018 int good_sectors = first_bad - r1_bio->sector;
1019 if (good_sectors < max_sectors)
1020 max_sectors = good_sectors;
1021 }
1022 }
1023 r1_bio->bios[i] = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025 rcu_read_unlock();
1026
Dan Williams6bfe0b42008-04-30 00:52:32 -07001027 if (unlikely(blocked_rdev)) {
1028 /* Wait for this device to become unblocked */
1029 int j;
1030
1031 for (j = 0; j < i; j++)
1032 if (r1_bio->bios[j])
1033 rdev_dec_pending(conf->mirrors[j].rdev, mddev);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001034 r1_bio->state = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001035 allow_barrier(conf);
1036 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1037 wait_barrier(conf);
1038 goto retry_write;
1039 }
1040
NeilBrown1f68f0c2011-07-28 11:31:48 +10001041 if (max_sectors < r1_bio->sectors) {
1042 /* We are splitting this write into multiple parts, so
1043 * we need to prepare for allocating another r1_bio.
1044 */
1045 r1_bio->sectors = max_sectors;
1046 spin_lock_irq(&conf->device_lock);
1047 if (bio->bi_phys_segments == 0)
1048 bio->bi_phys_segments = 2;
1049 else
1050 bio->bi_phys_segments++;
1051 spin_unlock_irq(&conf->device_lock);
NeilBrown191ea9b2005-06-21 17:17:23 -07001052 }
NeilBrown1f68f0c2011-07-28 11:31:48 +10001053 sectors_handled = r1_bio->sector + max_sectors - bio->bi_sector;
NeilBrown4b6d2872005-09-09 16:23:47 -07001054
NeilBrown4e780642010-10-19 12:54:01 +11001055 atomic_set(&r1_bio->remaining, 1);
NeilBrown4b6d2872005-09-09 16:23:47 -07001056 atomic_set(&r1_bio->behind_remaining, 0);
NeilBrown191ea9b2005-06-21 17:17:23 -07001057
NeilBrown1f68f0c2011-07-28 11:31:48 +10001058 first_clone = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 for (i = 0; i < disks; i++) {
1060 struct bio *mbio;
1061 if (!r1_bio->bios[i])
1062 continue;
1063
NeilBrowna167f662010-10-26 18:31:13 +11001064 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001065 md_trim_bio(mbio, r1_bio->sector - bio->bi_sector, max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
NeilBrown1f68f0c2011-07-28 11:31:48 +10001067 if (first_clone) {
1068 /* do behind I/O ?
1069 * Not if there are too many, or cannot
1070 * allocate memory, or a reader on WriteMostly
1071 * is waiting for behind writes to flush */
1072 if (bitmap &&
1073 (atomic_read(&bitmap->behind_writes)
1074 < mddev->bitmap_info.max_write_behind) &&
1075 !waitqueue_active(&bitmap->behind_wait))
1076 alloc_behind_pages(mbio, r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
NeilBrown1f68f0c2011-07-28 11:31:48 +10001078 bitmap_startwrite(bitmap, r1_bio->sector,
1079 r1_bio->sectors,
1080 test_bit(R1BIO_BehindIO,
1081 &r1_bio->state));
1082 first_clone = 0;
1083 }
NeilBrown2ca68f52011-07-28 11:32:10 +10001084 if (r1_bio->behind_bvecs) {
NeilBrown4b6d2872005-09-09 16:23:47 -07001085 struct bio_vec *bvec;
1086 int j;
1087
1088 /* Yes, I really want the '__' version so that
1089 * we clear any unused pointer in the io_vec, rather
1090 * than leave them unchanged. This is important
1091 * because when we come to free the pages, we won't
NeilBrown046abee2010-10-26 15:46:20 +11001092 * know the original bi_idx, so we just free
NeilBrown4b6d2872005-09-09 16:23:47 -07001093 * them all
1094 */
1095 __bio_for_each_segment(bvec, mbio, j, 0)
NeilBrown2ca68f52011-07-28 11:32:10 +10001096 bvec->bv_page = r1_bio->behind_bvecs[j].bv_page;
NeilBrown4b6d2872005-09-09 16:23:47 -07001097 if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
1098 atomic_inc(&r1_bio->behind_remaining);
1099 }
1100
NeilBrown1f68f0c2011-07-28 11:31:48 +10001101 r1_bio->bios[i] = mbio;
1102
1103 mbio->bi_sector = (r1_bio->sector +
1104 conf->mirrors[i].rdev->data_offset);
1105 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
1106 mbio->bi_end_io = raid1_end_write_request;
1107 mbio->bi_rw = WRITE | do_flush_fua | do_sync;
1108 mbio->bi_private = r1_bio;
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 atomic_inc(&r1_bio->remaining);
NeilBrown4e780642010-10-19 12:54:01 +11001111 spin_lock_irqsave(&conf->device_lock, flags);
1112 bio_list_add(&conf->pending_bio_list, mbio);
NeilBrown4e780642010-10-19 12:54:01 +11001113 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
NeilBrown079fa162011-09-10 17:21:23 +10001115 /* Mustn't call r1_bio_write_done before this next test,
1116 * as it could result in the bio being freed.
1117 */
NeilBrown1f68f0c2011-07-28 11:31:48 +10001118 if (sectors_handled < (bio->bi_size >> 9)) {
NeilBrown079fa162011-09-10 17:21:23 +10001119 r1_bio_write_done(r1_bio);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001120 /* We need another r1_bio. It has already been counted
1121 * in bio->bi_phys_segments
1122 */
1123 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1124 r1_bio->master_bio = bio;
1125 r1_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1126 r1_bio->state = 0;
1127 r1_bio->mddev = mddev;
1128 r1_bio->sector = bio->bi_sector + sectors_handled;
1129 goto retry_write;
1130 }
1131
NeilBrown079fa162011-09-10 17:21:23 +10001132 r1_bio_write_done(r1_bio);
1133
1134 /* In case raid1d snuck in to freeze_array */
1135 wake_up(&conf->wait_barrier);
1136
NeilBrownc3b328a2011-04-18 18:25:43 +10001137 if (do_sync || !bitmap || !plugged)
Lars Ellenberge3881a62007-01-10 23:15:37 -08001138 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 return 0;
1141}
1142
1143static void status(struct seq_file *seq, mddev_t *mddev)
1144{
NeilBrown070ec552009-06-16 16:54:21 +10001145 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 int i;
1147
1148 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown11ce99e2006-10-03 01:15:52 -07001149 conf->raid_disks - mddev->degraded);
NeilBrownddac7c72006-08-31 21:27:36 -07001150 rcu_read_lock();
1151 for (i = 0; i < conf->raid_disks; i++) {
1152 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 seq_printf(seq, "%s",
NeilBrownddac7c72006-08-31 21:27:36 -07001154 rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1155 }
1156 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 seq_printf(seq, "]");
1158}
1159
1160
1161static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1162{
1163 char b[BDEVNAME_SIZE];
NeilBrown070ec552009-06-16 16:54:21 +10001164 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 /*
1167 * If it is not operational, then we have already marked it as dead
1168 * else if it is the last working disks, ignore the error, let the
1169 * next level up know.
1170 * else mark the drive as failed
1171 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001172 if (test_bit(In_sync, &rdev->flags)
NeilBrown4044ba52009-01-09 08:31:11 +11001173 && (conf->raid_disks - mddev->degraded) == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 /*
1175 * Don't fail the drive, act as though we were just a
NeilBrown4044ba52009-01-09 08:31:11 +11001176 * normal single drive.
1177 * However don't try a recovery from this drive as
1178 * it is very likely to fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 */
NeilBrown53890422011-07-27 11:00:36 +10001180 conf->recovery_disabled = mddev->recovery_disabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return;
NeilBrown4044ba52009-01-09 08:31:11 +11001182 }
NeilBrownde393cd2011-07-28 11:31:48 +10001183 set_bit(Blocked, &rdev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -07001184 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1185 unsigned long flags;
1186 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 mddev->degraded++;
NeilBrowndd00a992007-05-10 03:15:50 -07001188 set_bit(Faulty, &rdev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -07001189 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 /*
1191 * if recovery is running, make sure it aborts.
1192 */
NeilBrowndfc70642008-05-23 13:04:39 -07001193 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrowndd00a992007-05-10 03:15:50 -07001194 } else
1195 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -07001196 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Joe Perches067032b2011-01-14 09:14:33 +11001197 printk(KERN_ALERT
1198 "md/raid1:%s: Disk failure on %s, disabling device.\n"
1199 "md/raid1:%s: Operation continuing on %d devices.\n",
NeilBrown9dd1e2f2010-05-03 14:30:35 +10001200 mdname(mddev), bdevname(rdev->bdev, b),
1201 mdname(mddev), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
1204static void print_conf(conf_t *conf)
1205{
1206 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
NeilBrown9dd1e2f2010-05-03 14:30:35 +10001208 printk(KERN_DEBUG "RAID1 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (!conf) {
NeilBrown9dd1e2f2010-05-03 14:30:35 +10001210 printk(KERN_DEBUG "(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return;
1212 }
NeilBrown9dd1e2f2010-05-03 14:30:35 +10001213 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 conf->raid_disks);
1215
NeilBrownddac7c72006-08-31 21:27:36 -07001216 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 for (i = 0; i < conf->raid_disks; i++) {
1218 char b[BDEVNAME_SIZE];
NeilBrownddac7c72006-08-31 21:27:36 -07001219 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
1220 if (rdev)
NeilBrown9dd1e2f2010-05-03 14:30:35 +10001221 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownddac7c72006-08-31 21:27:36 -07001222 i, !test_bit(In_sync, &rdev->flags),
1223 !test_bit(Faulty, &rdev->flags),
1224 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
NeilBrownddac7c72006-08-31 21:27:36 -07001226 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
1229static void close_sync(conf_t *conf)
1230{
NeilBrown17999be2006-01-06 00:20:12 -08001231 wait_barrier(conf);
1232 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 mempool_destroy(conf->r1buf_pool);
1235 conf->r1buf_pool = NULL;
1236}
1237
1238static int raid1_spare_active(mddev_t *mddev)
1239{
1240 int i;
1241 conf_t *conf = mddev->private;
NeilBrown6b965622010-08-18 11:56:59 +10001242 int count = 0;
1243 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 /*
1246 * Find all failed disks within the RAID1 configuration
NeilBrownddac7c72006-08-31 21:27:36 -07001247 * and mark them readable.
1248 * Called under mddev lock, so rcu protection not needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 */
1250 for (i = 0; i < conf->raid_disks; i++) {
NeilBrownddac7c72006-08-31 21:27:36 -07001251 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
1252 if (rdev
1253 && !test_bit(Faulty, &rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001254 && !test_and_set_bit(In_sync, &rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001255 count++;
Jonathan Brassow654e8b52011-07-27 11:00:36 +10001256 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
1258 }
NeilBrown6b965622010-08-18 11:56:59 +10001259 spin_lock_irqsave(&conf->device_lock, flags);
1260 mddev->degraded -= count;
1261 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001264 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
1267
1268static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1269{
1270 conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001271 int err = -EEXIST;
NeilBrown41158c72005-06-21 17:17:25 -07001272 int mirror = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 mirror_info_t *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10001274 int first = 0;
1275 int last = mddev->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
NeilBrown53890422011-07-27 11:00:36 +10001277 if (mddev->recovery_disabled == conf->recovery_disabled)
1278 return -EBUSY;
1279
Neil Brown6c2fce22008-06-28 08:31:31 +10001280 if (rdev->raid_disk >= 0)
1281 first = last = rdev->raid_disk;
1282
1283 for (mirror = first; mirror <= last; mirror++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if ( !(p=conf->mirrors+mirror)->rdev) {
1285
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10001286 disk_stack_limits(mddev->gendisk, rdev->bdev,
1287 rdev->data_offset << 9);
NeilBrown627a2d32010-03-08 16:44:38 +11001288 /* as we don't honour merge_bvec_fn, we must
1289 * never risk violating it, so limit
1290 * ->max_segments to one lying with a single
1291 * page, as a one page request is never in
1292 * violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 */
NeilBrown627a2d32010-03-08 16:44:38 +11001294 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1295 blk_queue_max_segments(mddev->queue, 1);
1296 blk_queue_segment_boundary(mddev->queue,
1297 PAGE_CACHE_SIZE - 1);
1298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 p->head_position = 0;
1301 rdev->raid_disk = mirror;
Neil Brown199050e2008-06-28 08:31:33 +10001302 err = 0;
NeilBrown6aea114a2005-11-28 13:44:13 -08001303 /* As all devices are equivalent, we don't need a full recovery
1304 * if this was recently any drive of the array
1305 */
1306 if (rdev->saved_raid_disk < 0)
NeilBrown41158c72005-06-21 17:17:25 -07001307 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001308 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 break;
1310 }
Andre Nollac5e7112009-08-03 10:59:47 +10001311 md_integrity_add_rdev(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001313 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
1316static int raid1_remove_disk(mddev_t *mddev, int number)
1317{
1318 conf_t *conf = mddev->private;
1319 int err = 0;
1320 mdk_rdev_t *rdev;
1321 mirror_info_t *p = conf->mirrors+ number;
1322
1323 print_conf(conf);
1324 rdev = p->rdev;
1325 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001326 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 atomic_read(&rdev->nr_pending)) {
1328 err = -EBUSY;
1329 goto abort;
1330 }
NeilBrown046abee2010-10-26 15:46:20 +11001331 /* Only remove non-faulty devices if recovery
NeilBrowndfc70642008-05-23 13:04:39 -07001332 * is not possible.
1333 */
1334 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown53890422011-07-27 11:00:36 +10001335 mddev->recovery_disabled != conf->recovery_disabled &&
NeilBrowndfc70642008-05-23 13:04:39 -07001336 mddev->degraded < conf->raid_disks) {
1337 err = -EBUSY;
1338 goto abort;
1339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001341 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (atomic_read(&rdev->nr_pending)) {
1343 /* lost the race, try later */
1344 err = -EBUSY;
1345 p->rdev = rdev;
Andre Nollac5e7112009-08-03 10:59:47 +10001346 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001348 err = md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350abort:
1351
1352 print_conf(conf);
1353 return err;
1354}
1355
1356
NeilBrown6712ecf2007-09-27 12:47:43 +02001357static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001359 r1bio_t *r1_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
NeilBrown0fc280f2011-10-07 14:22:55 +11001361 update_head_pos(r1_bio->read_disk, r1_bio);
Namhyung Kimba3ae3b2011-10-07 14:22:53 +11001362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 /*
1364 * we have read a block, now it needs to be re-written,
1365 * or re-read if the read failed.
1366 * We don't do much here, just schedule handling by raid1d
1367 */
NeilBrown69382e82006-01-06 00:20:22 -08001368 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 set_bit(R1BIO_Uptodate, &r1_bio->state);
NeilBrownd11c1712006-01-06 00:20:26 -08001370
1371 if (atomic_dec_and_test(&r1_bio->remaining))
1372 reschedule_retry(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
NeilBrown6712ecf2007-09-27 12:47:43 +02001375static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001378 r1bio_t *r1_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 mddev_t *mddev = r1_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001380 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 int mirror=0;
NeilBrown4367af52011-07-28 11:31:49 +10001382 sector_t first_bad;
1383 int bad_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Namhyung Kimba3ae3b2011-10-07 14:22:53 +11001385 mirror = find_bio_disk(r1_bio, bio);
1386
NeilBrown6b1117d2006-03-31 02:31:57 -08001387 if (!uptodate) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001388 sector_t sync_blocks = 0;
NeilBrown6b1117d2006-03-31 02:31:57 -08001389 sector_t s = r1_bio->sector;
1390 long sectors_to_go = r1_bio->sectors;
1391 /* make sure these bits doesn't get cleared. */
1392 do {
NeilBrown5e3db642006-07-10 04:44:18 -07001393 bitmap_end_sync(mddev->bitmap, s,
NeilBrown6b1117d2006-03-31 02:31:57 -08001394 &sync_blocks, 1);
1395 s += sync_blocks;
1396 sectors_to_go -= sync_blocks;
1397 } while (sectors_to_go > 0);
NeilBrownd8f05d22011-07-28 11:33:00 +10001398 set_bit(WriteErrorSeen,
1399 &conf->mirrors[mirror].rdev->flags);
1400 set_bit(R1BIO_WriteError, &r1_bio->state);
NeilBrown4367af52011-07-28 11:31:49 +10001401 } else if (is_badblock(conf->mirrors[mirror].rdev,
1402 r1_bio->sector,
1403 r1_bio->sectors,
NeilBrown3a9f28a2011-07-28 11:33:42 +10001404 &first_bad, &bad_sectors) &&
1405 !is_badblock(conf->mirrors[r1_bio->read_disk].rdev,
1406 r1_bio->sector,
1407 r1_bio->sectors,
1408 &first_bad, &bad_sectors)
1409 )
NeilBrown4367af52011-07-28 11:31:49 +10001410 set_bit(R1BIO_MadeGood, &r1_bio->state);
NeilBrowne3b97032005-08-04 12:53:34 -07001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown4367af52011-07-28 11:31:49 +10001413 int s = r1_bio->sectors;
NeilBrownd8f05d22011-07-28 11:33:00 +10001414 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
1415 test_bit(R1BIO_WriteError, &r1_bio->state))
NeilBrown4367af52011-07-28 11:31:49 +10001416 reschedule_retry(r1_bio);
1417 else {
1418 put_buf(r1_bio);
1419 md_done_sync(mddev, s, uptodate);
1420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
NeilBrownd8f05d22011-07-28 11:33:00 +10001424static int r1_sync_page_io(mdk_rdev_t *rdev, sector_t sector,
1425 int sectors, struct page *page, int rw)
1426{
1427 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
1428 /* success */
1429 return 1;
1430 if (rw == WRITE)
1431 set_bit(WriteErrorSeen, &rdev->flags);
1432 /* need to record an error - either for the block or the device */
1433 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
1434 md_error(rdev->mddev, rdev);
1435 return 0;
1436}
1437
NeilBrowna68e5872011-05-11 14:40:44 +10001438static int fix_sync_read_error(r1bio_t *r1_bio)
1439{
1440 /* Try some synchronous reads of other devices to get
1441 * good data, much like with normal read errors. Only
1442 * read into the pages we already have so we don't
1443 * need to re-issue the read request.
1444 * We don't need to freeze the array, because being in an
1445 * active sync request, there is no normal IO, and
1446 * no overlapping syncs.
NeilBrown06f60382011-07-28 11:31:48 +10001447 * We don't need to check is_badblock() again as we
1448 * made sure that anything with a bad block in range
1449 * will have bi_end_io clear.
NeilBrowna68e5872011-05-11 14:40:44 +10001450 */
1451 mddev_t *mddev = r1_bio->mddev;
1452 conf_t *conf = mddev->private;
1453 struct bio *bio = r1_bio->bios[r1_bio->read_disk];
1454 sector_t sect = r1_bio->sector;
1455 int sectors = r1_bio->sectors;
1456 int idx = 0;
1457
1458 while(sectors) {
1459 int s = sectors;
1460 int d = r1_bio->read_disk;
1461 int success = 0;
1462 mdk_rdev_t *rdev;
NeilBrown78d7f5f2011-05-11 14:48:56 +10001463 int start;
NeilBrowna68e5872011-05-11 14:40:44 +10001464
1465 if (s > (PAGE_SIZE>>9))
1466 s = PAGE_SIZE >> 9;
1467 do {
1468 if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
1469 /* No rcu protection needed here devices
1470 * can only be removed when no resync is
1471 * active, and resync is currently active
1472 */
1473 rdev = conf->mirrors[d].rdev;
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001474 if (sync_page_io(rdev, sect, s<<9,
NeilBrowna68e5872011-05-11 14:40:44 +10001475 bio->bi_io_vec[idx].bv_page,
1476 READ, false)) {
1477 success = 1;
1478 break;
1479 }
1480 }
1481 d++;
1482 if (d == conf->raid_disks)
1483 d = 0;
1484 } while (!success && d != r1_bio->read_disk);
1485
NeilBrown78d7f5f2011-05-11 14:48:56 +10001486 if (!success) {
NeilBrowna68e5872011-05-11 14:40:44 +10001487 char b[BDEVNAME_SIZE];
NeilBrown3a9f28a2011-07-28 11:33:42 +10001488 int abort = 0;
1489 /* Cannot read from anywhere, this block is lost.
1490 * Record a bad block on each device. If that doesn't
1491 * work just disable and interrupt the recovery.
1492 * Don't fail devices as that won't really help.
1493 */
NeilBrowna68e5872011-05-11 14:40:44 +10001494 printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O read error"
1495 " for block %llu\n",
1496 mdname(mddev),
1497 bdevname(bio->bi_bdev, b),
1498 (unsigned long long)r1_bio->sector);
NeilBrown3a9f28a2011-07-28 11:33:42 +10001499 for (d = 0; d < conf->raid_disks; d++) {
1500 rdev = conf->mirrors[d].rdev;
1501 if (!rdev || test_bit(Faulty, &rdev->flags))
1502 continue;
1503 if (!rdev_set_badblocks(rdev, sect, s, 0))
1504 abort = 1;
1505 }
1506 if (abort) {
1507 mddev->recovery_disabled = 1;
1508 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1509 md_done_sync(mddev, r1_bio->sectors, 0);
1510 put_buf(r1_bio);
1511 return 0;
1512 }
1513 /* Try next page */
1514 sectors -= s;
1515 sect += s;
1516 idx++;
1517 continue;
NeilBrowna68e5872011-05-11 14:40:44 +10001518 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001519
1520 start = d;
1521 /* write it back and re-read */
1522 while (d != r1_bio->read_disk) {
1523 if (d == 0)
1524 d = conf->raid_disks;
1525 d--;
1526 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1527 continue;
1528 rdev = conf->mirrors[d].rdev;
NeilBrownd8f05d22011-07-28 11:33:00 +10001529 if (r1_sync_page_io(rdev, sect, s,
1530 bio->bi_io_vec[idx].bv_page,
1531 WRITE) == 0) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10001532 r1_bio->bios[d]->bi_end_io = NULL;
1533 rdev_dec_pending(rdev, mddev);
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001534 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001535 }
1536 d = start;
1537 while (d != r1_bio->read_disk) {
1538 if (d == 0)
1539 d = conf->raid_disks;
1540 d--;
1541 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1542 continue;
1543 rdev = conf->mirrors[d].rdev;
NeilBrownd8f05d22011-07-28 11:33:00 +10001544 if (r1_sync_page_io(rdev, sect, s,
1545 bio->bi_io_vec[idx].bv_page,
1546 READ) != 0)
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001547 atomic_add(s, &rdev->corrected_errors);
NeilBrown78d7f5f2011-05-11 14:48:56 +10001548 }
NeilBrowna68e5872011-05-11 14:40:44 +10001549 sectors -= s;
1550 sect += s;
1551 idx ++;
1552 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001553 set_bit(R1BIO_Uptodate, &r1_bio->state);
NeilBrown7ca78d52011-05-11 14:50:37 +10001554 set_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrowna68e5872011-05-11 14:40:44 +10001555 return 1;
1556}
1557
1558static int process_checks(r1bio_t *r1_bio)
1559{
1560 /* We have read all readable devices. If we haven't
1561 * got the block, then there is no hope left.
1562 * If we have, then we want to do a comparison
1563 * and skip the write if everything is the same.
1564 * If any blocks failed to read, then we need to
1565 * attempt an over-write
1566 */
1567 mddev_t *mddev = r1_bio->mddev;
1568 conf_t *conf = mddev->private;
1569 int primary;
1570 int i;
1571
NeilBrown78d7f5f2011-05-11 14:48:56 +10001572 for (primary = 0; primary < conf->raid_disks; primary++)
NeilBrowna68e5872011-05-11 14:40:44 +10001573 if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
1574 test_bit(BIO_UPTODATE, &r1_bio->bios[primary]->bi_flags)) {
1575 r1_bio->bios[primary]->bi_end_io = NULL;
1576 rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
1577 break;
1578 }
1579 r1_bio->read_disk = primary;
NeilBrown78d7f5f2011-05-11 14:48:56 +10001580 for (i = 0; i < conf->raid_disks; i++) {
1581 int j;
1582 int vcnt = r1_bio->sectors >> (PAGE_SHIFT- 9);
1583 struct bio *pbio = r1_bio->bios[primary];
1584 struct bio *sbio = r1_bio->bios[i];
1585 int size;
NeilBrowna68e5872011-05-11 14:40:44 +10001586
NeilBrown78d7f5f2011-05-11 14:48:56 +10001587 if (r1_bio->bios[i]->bi_end_io != end_sync_read)
1588 continue;
NeilBrowna68e5872011-05-11 14:40:44 +10001589
NeilBrown78d7f5f2011-05-11 14:48:56 +10001590 if (test_bit(BIO_UPTODATE, &sbio->bi_flags)) {
1591 for (j = vcnt; j-- ; ) {
1592 struct page *p, *s;
1593 p = pbio->bi_io_vec[j].bv_page;
1594 s = sbio->bi_io_vec[j].bv_page;
1595 if (memcmp(page_address(p),
1596 page_address(s),
1597 PAGE_SIZE))
1598 break;
NeilBrowna68e5872011-05-11 14:40:44 +10001599 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001600 } else
1601 j = 0;
1602 if (j >= 0)
1603 mddev->resync_mismatches += r1_bio->sectors;
1604 if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
1605 && test_bit(BIO_UPTODATE, &sbio->bi_flags))) {
1606 /* No need to write to this device. */
1607 sbio->bi_end_io = NULL;
1608 rdev_dec_pending(conf->mirrors[i].rdev, mddev);
1609 continue;
NeilBrowna68e5872011-05-11 14:40:44 +10001610 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001611 /* fixup the bio for reuse */
1612 sbio->bi_vcnt = vcnt;
1613 sbio->bi_size = r1_bio->sectors << 9;
1614 sbio->bi_idx = 0;
1615 sbio->bi_phys_segments = 0;
1616 sbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1617 sbio->bi_flags |= 1 << BIO_UPTODATE;
1618 sbio->bi_next = NULL;
1619 sbio->bi_sector = r1_bio->sector +
1620 conf->mirrors[i].rdev->data_offset;
1621 sbio->bi_bdev = conf->mirrors[i].rdev->bdev;
1622 size = sbio->bi_size;
1623 for (j = 0; j < vcnt ; j++) {
1624 struct bio_vec *bi;
1625 bi = &sbio->bi_io_vec[j];
1626 bi->bv_offset = 0;
1627 if (size > PAGE_SIZE)
1628 bi->bv_len = PAGE_SIZE;
1629 else
1630 bi->bv_len = size;
1631 size -= PAGE_SIZE;
1632 memcpy(page_address(bi->bv_page),
1633 page_address(pbio->bi_io_vec[j].bv_page),
1634 PAGE_SIZE);
1635 }
1636 }
NeilBrowna68e5872011-05-11 14:40:44 +10001637 return 0;
1638}
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
1641{
NeilBrown070ec552009-06-16 16:54:21 +10001642 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 int i;
1644 int disks = conf->raid_disks;
1645 struct bio *bio, *wbio;
1646
1647 bio = r1_bio->bios[r1_bio->read_disk];
1648
NeilBrowna68e5872011-05-11 14:40:44 +10001649 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
1650 /* ouch - failed to read all of that. */
1651 if (!fix_sync_read_error(r1_bio))
1652 return;
NeilBrown7ca78d52011-05-11 14:50:37 +10001653
1654 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1655 if (process_checks(r1_bio) < 0)
1656 return;
NeilBrownd11c1712006-01-06 00:20:26 -08001657 /*
1658 * schedule writes
1659 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 atomic_set(&r1_bio->remaining, 1);
1661 for (i = 0; i < disks ; i++) {
1662 wbio = r1_bio->bios[i];
NeilBrown3e198f72006-01-06 00:20:21 -08001663 if (wbio->bi_end_io == NULL ||
1664 (wbio->bi_end_io == end_sync_read &&
1665 (i == r1_bio->read_disk ||
1666 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 continue;
1668
NeilBrown3e198f72006-01-06 00:20:21 -08001669 wbio->bi_rw = WRITE;
1670 wbio->bi_end_io = end_sync_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 atomic_inc(&r1_bio->remaining);
1672 md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
NeilBrown191ea9b2005-06-21 17:17:23 -07001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 generic_make_request(wbio);
1675 }
1676
1677 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001678 /* if we're here, all write(s) have completed, so clean up */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 md_done_sync(mddev, r1_bio->sectors, 1);
1680 put_buf(r1_bio);
1681 }
1682}
1683
1684/*
1685 * This is a kernel thread which:
1686 *
1687 * 1. Retries failed read operations on working mirrors.
1688 * 2. Updates the raid superblock when problems encounter.
NeilBrownd2eb35a2011-07-28 11:31:48 +10001689 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 */
1691
NeilBrown867868f2006-10-03 01:15:51 -07001692static void fix_read_error(conf_t *conf, int read_disk,
1693 sector_t sect, int sectors)
1694{
1695 mddev_t *mddev = conf->mddev;
1696 while(sectors) {
1697 int s = sectors;
1698 int d = read_disk;
1699 int success = 0;
1700 int start;
1701 mdk_rdev_t *rdev;
1702
1703 if (s > (PAGE_SIZE>>9))
1704 s = PAGE_SIZE >> 9;
1705
1706 do {
1707 /* Note: no rcu protection needed here
1708 * as this is synchronous in the raid1d thread
1709 * which is the thread that might remove
1710 * a device. If raid1d ever becomes multi-threaded....
1711 */
NeilBrownd2eb35a2011-07-28 11:31:48 +10001712 sector_t first_bad;
1713 int bad_sectors;
1714
NeilBrown867868f2006-10-03 01:15:51 -07001715 rdev = conf->mirrors[d].rdev;
1716 if (rdev &&
1717 test_bit(In_sync, &rdev->flags) &&
NeilBrownd2eb35a2011-07-28 11:31:48 +10001718 is_badblock(rdev, sect, s,
1719 &first_bad, &bad_sectors) == 0 &&
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001720 sync_page_io(rdev, sect, s<<9,
1721 conf->tmppage, READ, false))
NeilBrown867868f2006-10-03 01:15:51 -07001722 success = 1;
1723 else {
1724 d++;
1725 if (d == conf->raid_disks)
1726 d = 0;
1727 }
1728 } while (!success && d != read_disk);
1729
1730 if (!success) {
NeilBrownd8f05d22011-07-28 11:33:00 +10001731 /* Cannot read from anywhere - mark it bad */
1732 mdk_rdev_t *rdev = conf->mirrors[read_disk].rdev;
1733 if (!rdev_set_badblocks(rdev, sect, s, 0))
1734 md_error(mddev, rdev);
NeilBrown867868f2006-10-03 01:15:51 -07001735 break;
1736 }
1737 /* write it back and re-read */
1738 start = d;
1739 while (d != read_disk) {
1740 if (d==0)
1741 d = conf->raid_disks;
1742 d--;
1743 rdev = conf->mirrors[d].rdev;
1744 if (rdev &&
NeilBrownd8f05d22011-07-28 11:33:00 +10001745 test_bit(In_sync, &rdev->flags))
1746 r1_sync_page_io(rdev, sect, s,
1747 conf->tmppage, WRITE);
NeilBrown867868f2006-10-03 01:15:51 -07001748 }
1749 d = start;
1750 while (d != read_disk) {
1751 char b[BDEVNAME_SIZE];
1752 if (d==0)
1753 d = conf->raid_disks;
1754 d--;
1755 rdev = conf->mirrors[d].rdev;
1756 if (rdev &&
1757 test_bit(In_sync, &rdev->flags)) {
NeilBrownd8f05d22011-07-28 11:33:00 +10001758 if (r1_sync_page_io(rdev, sect, s,
1759 conf->tmppage, READ)) {
NeilBrown867868f2006-10-03 01:15:51 -07001760 atomic_add(s, &rdev->corrected_errors);
1761 printk(KERN_INFO
NeilBrown9dd1e2f2010-05-03 14:30:35 +10001762 "md/raid1:%s: read error corrected "
NeilBrown867868f2006-10-03 01:15:51 -07001763 "(%d sectors at %llu on %s)\n",
1764 mdname(mddev), s,
Randy Dunlap969b7552006-10-28 10:38:32 -07001765 (unsigned long long)(sect +
1766 rdev->data_offset),
NeilBrown867868f2006-10-03 01:15:51 -07001767 bdevname(rdev->bdev, b));
1768 }
1769 }
1770 }
1771 sectors -= s;
1772 sect += s;
1773 }
1774}
1775
NeilBrowncd5ff9a12011-07-28 11:32:41 +10001776static void bi_complete(struct bio *bio, int error)
1777{
1778 complete((struct completion *)bio->bi_private);
1779}
1780
1781static int submit_bio_wait(int rw, struct bio *bio)
1782{
1783 struct completion event;
1784 rw |= REQ_SYNC;
1785
1786 init_completion(&event);
1787 bio->bi_private = &event;
1788 bio->bi_end_io = bi_complete;
1789 submit_bio(rw, bio);
1790 wait_for_completion(&event);
1791
1792 return test_bit(BIO_UPTODATE, &bio->bi_flags);
1793}
1794
1795static int narrow_write_error(r1bio_t *r1_bio, int i)
1796{
1797 mddev_t *mddev = r1_bio->mddev;
1798 conf_t *conf = mddev->private;
1799 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
1800 int vcnt, idx;
1801 struct bio_vec *vec;
1802
1803 /* bio has the data to be written to device 'i' where
1804 * we just recently had a write error.
1805 * We repeatedly clone the bio and trim down to one block,
1806 * then try the write. Where the write fails we record
1807 * a bad block.
1808 * It is conceivable that the bio doesn't exactly align with
1809 * blocks. We must handle this somehow.
1810 *
1811 * We currently own a reference on the rdev.
1812 */
1813
1814 int block_sectors;
1815 sector_t sector;
1816 int sectors;
1817 int sect_to_write = r1_bio->sectors;
1818 int ok = 1;
1819
1820 if (rdev->badblocks.shift < 0)
1821 return 0;
1822
1823 block_sectors = 1 << rdev->badblocks.shift;
1824 sector = r1_bio->sector;
1825 sectors = ((sector + block_sectors)
1826 & ~(sector_t)(block_sectors - 1))
1827 - sector;
1828
1829 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
1830 vcnt = r1_bio->behind_page_count;
1831 vec = r1_bio->behind_bvecs;
1832 idx = 0;
1833 while (vec[idx].bv_page == NULL)
1834 idx++;
1835 } else {
1836 vcnt = r1_bio->master_bio->bi_vcnt;
1837 vec = r1_bio->master_bio->bi_io_vec;
1838 idx = r1_bio->master_bio->bi_idx;
1839 }
1840 while (sect_to_write) {
1841 struct bio *wbio;
1842 if (sectors > sect_to_write)
1843 sectors = sect_to_write;
1844 /* Write at 'sector' for 'sectors'*/
1845
1846 wbio = bio_alloc_mddev(GFP_NOIO, vcnt, mddev);
1847 memcpy(wbio->bi_io_vec, vec, vcnt * sizeof(struct bio_vec));
1848 wbio->bi_sector = r1_bio->sector;
1849 wbio->bi_rw = WRITE;
1850 wbio->bi_vcnt = vcnt;
1851 wbio->bi_size = r1_bio->sectors << 9;
1852 wbio->bi_idx = idx;
1853
1854 md_trim_bio(wbio, sector - r1_bio->sector, sectors);
1855 wbio->bi_sector += rdev->data_offset;
1856 wbio->bi_bdev = rdev->bdev;
1857 if (submit_bio_wait(WRITE, wbio) == 0)
1858 /* failure! */
1859 ok = rdev_set_badblocks(rdev, sector,
1860 sectors, 0)
1861 && ok;
1862
1863 bio_put(wbio);
1864 sect_to_write -= sectors;
1865 sector += sectors;
1866 sectors = block_sectors;
1867 }
1868 return ok;
1869}
1870
NeilBrown62096bc2011-07-28 11:38:13 +10001871static void handle_sync_write_finished(conf_t *conf, r1bio_t *r1_bio)
1872{
1873 int m;
1874 int s = r1_bio->sectors;
1875 for (m = 0; m < conf->raid_disks ; m++) {
1876 mdk_rdev_t *rdev = conf->mirrors[m].rdev;
1877 struct bio *bio = r1_bio->bios[m];
1878 if (bio->bi_end_io == NULL)
1879 continue;
1880 if (test_bit(BIO_UPTODATE, &bio->bi_flags) &&
1881 test_bit(R1BIO_MadeGood, &r1_bio->state)) {
1882 rdev_clear_badblocks(rdev, r1_bio->sector, s);
1883 }
1884 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
1885 test_bit(R1BIO_WriteError, &r1_bio->state)) {
1886 if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0))
1887 md_error(conf->mddev, rdev);
1888 }
1889 }
1890 put_buf(r1_bio);
1891 md_done_sync(conf->mddev, s, 1);
1892}
1893
1894static void handle_write_finished(conf_t *conf, r1bio_t *r1_bio)
1895{
1896 int m;
1897 for (m = 0; m < conf->raid_disks ; m++)
1898 if (r1_bio->bios[m] == IO_MADE_GOOD) {
1899 mdk_rdev_t *rdev = conf->mirrors[m].rdev;
1900 rdev_clear_badblocks(rdev,
1901 r1_bio->sector,
1902 r1_bio->sectors);
1903 rdev_dec_pending(rdev, conf->mddev);
1904 } else if (r1_bio->bios[m] != NULL) {
1905 /* This drive got a write error. We need to
1906 * narrow down and record precise write
1907 * errors.
1908 */
1909 if (!narrow_write_error(r1_bio, m)) {
1910 md_error(conf->mddev,
1911 conf->mirrors[m].rdev);
1912 /* an I/O failed, we can't clear the bitmap */
1913 set_bit(R1BIO_Degraded, &r1_bio->state);
1914 }
1915 rdev_dec_pending(conf->mirrors[m].rdev,
1916 conf->mddev);
1917 }
1918 if (test_bit(R1BIO_WriteError, &r1_bio->state))
1919 close_write(r1_bio);
1920 raid_end_bio_io(r1_bio);
1921}
1922
1923static void handle_read_error(conf_t *conf, r1bio_t *r1_bio)
1924{
1925 int disk;
1926 int max_sectors;
1927 mddev_t *mddev = conf->mddev;
1928 struct bio *bio;
1929 char b[BDEVNAME_SIZE];
1930 mdk_rdev_t *rdev;
1931
1932 clear_bit(R1BIO_ReadError, &r1_bio->state);
1933 /* we got a read error. Maybe the drive is bad. Maybe just
1934 * the block and we can fix it.
1935 * We freeze all other IO, and try reading the block from
1936 * other devices. When we find one, we re-write
1937 * and check it that fixes the read error.
1938 * This is all done synchronously while the array is
1939 * frozen
1940 */
1941 if (mddev->ro == 0) {
1942 freeze_array(conf);
1943 fix_read_error(conf, r1_bio->read_disk,
1944 r1_bio->sector, r1_bio->sectors);
1945 unfreeze_array(conf);
1946 } else
1947 md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
1948
1949 bio = r1_bio->bios[r1_bio->read_disk];
1950 bdevname(bio->bi_bdev, b);
1951read_more:
1952 disk = read_balance(conf, r1_bio, &max_sectors);
1953 if (disk == -1) {
1954 printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O"
1955 " read error for block %llu\n",
1956 mdname(mddev), b, (unsigned long long)r1_bio->sector);
1957 raid_end_bio_io(r1_bio);
1958 } else {
1959 const unsigned long do_sync
1960 = r1_bio->master_bio->bi_rw & REQ_SYNC;
1961 if (bio) {
1962 r1_bio->bios[r1_bio->read_disk] =
1963 mddev->ro ? IO_BLOCKED : NULL;
1964 bio_put(bio);
1965 }
1966 r1_bio->read_disk = disk;
1967 bio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev);
1968 md_trim_bio(bio, r1_bio->sector - bio->bi_sector, max_sectors);
1969 r1_bio->bios[r1_bio->read_disk] = bio;
1970 rdev = conf->mirrors[disk].rdev;
1971 printk_ratelimited(KERN_ERR
1972 "md/raid1:%s: redirecting sector %llu"
1973 " to other mirror: %s\n",
1974 mdname(mddev),
1975 (unsigned long long)r1_bio->sector,
1976 bdevname(rdev->bdev, b));
1977 bio->bi_sector = r1_bio->sector + rdev->data_offset;
1978 bio->bi_bdev = rdev->bdev;
1979 bio->bi_end_io = raid1_end_read_request;
1980 bio->bi_rw = READ | do_sync;
1981 bio->bi_private = r1_bio;
1982 if (max_sectors < r1_bio->sectors) {
1983 /* Drat - have to split this up more */
1984 struct bio *mbio = r1_bio->master_bio;
1985 int sectors_handled = (r1_bio->sector + max_sectors
1986 - mbio->bi_sector);
1987 r1_bio->sectors = max_sectors;
1988 spin_lock_irq(&conf->device_lock);
1989 if (mbio->bi_phys_segments == 0)
1990 mbio->bi_phys_segments = 2;
1991 else
1992 mbio->bi_phys_segments++;
1993 spin_unlock_irq(&conf->device_lock);
1994 generic_make_request(bio);
1995 bio = NULL;
1996
1997 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1998
1999 r1_bio->master_bio = mbio;
2000 r1_bio->sectors = (mbio->bi_size >> 9)
2001 - sectors_handled;
2002 r1_bio->state = 0;
2003 set_bit(R1BIO_ReadError, &r1_bio->state);
2004 r1_bio->mddev = mddev;
2005 r1_bio->sector = mbio->bi_sector + sectors_handled;
2006
2007 goto read_more;
2008 } else
2009 generic_make_request(bio);
2010 }
2011}
2012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013static void raid1d(mddev_t *mddev)
2014{
2015 r1bio_t *r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 unsigned long flags;
NeilBrown070ec552009-06-16 16:54:21 +10002017 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002019 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021 md_check_recovery(mddev);
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002022
2023 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002025
NeilBrownc3b328a2011-04-18 18:25:43 +10002026 if (atomic_read(&mddev->plug_cnt) == 0)
2027 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002030 if (list_empty(head)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002031 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
2035 list_del(head->prev);
NeilBrownddaf22a2006-01-06 00:20:19 -08002036 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 spin_unlock_irqrestore(&conf->device_lock, flags);
2038
2039 mddev = r1_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002040 conf = mddev->private;
NeilBrown4367af52011-07-28 11:31:49 +10002041 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
NeilBrownd8f05d22011-07-28 11:33:00 +10002042 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
NeilBrown62096bc2011-07-28 11:38:13 +10002043 test_bit(R1BIO_WriteError, &r1_bio->state))
2044 handle_sync_write_finished(conf, r1_bio);
2045 else
NeilBrown4367af52011-07-28 11:31:49 +10002046 sync_request_write(mddev, r1_bio);
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002047 } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
NeilBrown62096bc2011-07-28 11:38:13 +10002048 test_bit(R1BIO_WriteError, &r1_bio->state))
2049 handle_write_finished(conf, r1_bio);
2050 else if (test_bit(R1BIO_ReadError, &r1_bio->state))
2051 handle_read_error(conf, r1_bio);
2052 else
NeilBrownd2eb35a2011-07-28 11:31:48 +10002053 /* just a partial read to be scheduled from separate
2054 * context
2055 */
2056 generic_make_request(r1_bio->bios[r1_bio->read_disk]);
NeilBrown62096bc2011-07-28 11:38:13 +10002057
NeilBrown1d9d5242009-10-16 15:55:32 +11002058 cond_resched();
NeilBrownde393cd2011-07-28 11:31:48 +10002059 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2060 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002062 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
2065
2066static int init_resync(conf_t *conf)
2067{
2068 int buffs;
2069
2070 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhenn9e77c482006-04-01 01:08:49 +02002071 BUG_ON(conf->r1buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
2073 conf->poolinfo);
2074 if (!conf->r1buf_pool)
2075 return -ENOMEM;
2076 conf->next_resync = 0;
2077 return 0;
2078}
2079
2080/*
2081 * perform a "sync" on one "block"
2082 *
2083 * We need to make sure that no normal I/O request - particularly write
2084 * requests - conflict with active sync requests.
2085 *
2086 * This is achieved by tracking pending requests and a 'barrier' concept
2087 * that can be installed to exclude normal IO requests.
2088 */
2089
NeilBrown57afd892005-06-21 17:17:13 -07002090static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
NeilBrown070ec552009-06-16 16:54:21 +10002092 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 r1bio_t *r1_bio;
2094 struct bio *bio;
2095 sector_t max_sector, nr_sectors;
NeilBrown3e198f72006-01-06 00:20:21 -08002096 int disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 int i;
NeilBrown3e198f72006-01-06 00:20:21 -08002098 int wonly = -1;
2099 int write_targets = 0, read_targets = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11002100 sector_t sync_blocks;
NeilBrowne3b97032005-08-04 12:53:34 -07002101 int still_degraded = 0;
NeilBrown06f60382011-07-28 11:31:48 +10002102 int good_sectors = RESYNC_SECTORS;
2103 int min_bad = 0; /* number of sectors that are bad in all devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 if (!conf->r1buf_pool)
2106 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002107 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Andre Noll58c0fed2009-03-31 14:33:13 +11002109 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (sector_nr >= max_sector) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002111 /* If we aborted, we need to abort the
2112 * sync on the 'current' bitmap chunk (there will
2113 * only be one in raid1 resync.
2114 * We can find the current addess in mddev->curr_resync
2115 */
NeilBrown6a806c52005-07-15 03:56:35 -07002116 if (mddev->curr_resync < max_sector) /* aborted */
2117 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
NeilBrown191ea9b2005-06-21 17:17:23 -07002118 &sync_blocks, 1);
NeilBrown6a806c52005-07-15 03:56:35 -07002119 else /* completed sync */
NeilBrown191ea9b2005-06-21 17:17:23 -07002120 conf->fullsync = 0;
NeilBrown6a806c52005-07-15 03:56:35 -07002121
2122 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 close_sync(conf);
2124 return 0;
2125 }
2126
NeilBrown07d84d102006-06-26 00:27:56 -07002127 if (mddev->bitmap == NULL &&
2128 mddev->recovery_cp == MaxSector &&
NeilBrown6394cca2006-08-27 01:23:50 -07002129 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown07d84d102006-06-26 00:27:56 -07002130 conf->fullsync == 0) {
2131 *skipped = 1;
2132 return max_sector - sector_nr;
2133 }
NeilBrown6394cca2006-08-27 01:23:50 -07002134 /* before building a request, check if we can skip these blocks..
2135 * This call the bitmap_start_sync doesn't actually record anything
2136 */
NeilBrowne3b97032005-08-04 12:53:34 -07002137 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrowne5de485f2005-11-08 21:39:38 -08002138 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002139 /* We can skip this block, and probably several more */
2140 *skipped = 1;
2141 return sync_blocks;
2142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 /*
NeilBrown17999be2006-01-06 00:20:12 -08002144 * If there is non-resync activity waiting for a turn,
2145 * and resync is going fast enough,
2146 * then let it though before starting on this new sync request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 */
NeilBrown17999be2006-01-06 00:20:12 -08002148 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 msleep_interruptible(1000);
NeilBrown17999be2006-01-06 00:20:12 -08002150
NeilBrownb47490c2008-02-06 01:39:50 -08002151 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
NeilBrown1c4588e2010-10-26 17:41:22 +11002152 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
NeilBrown17999be2006-01-06 00:20:12 -08002153 raise_barrier(conf);
2154
2155 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
NeilBrown3e198f72006-01-06 00:20:21 -08002157 rcu_read_lock();
2158 /*
2159 * If we get a correctably read error during resync or recovery,
2160 * we might want to read from a different device. So we
2161 * flag all drives that could conceivably be read from for READ,
2162 * and any others (which will be non-In_sync devices) for WRITE.
2163 * If a read fails, we try reading from something else for which READ
2164 * is OK.
2165 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 r1_bio->mddev = mddev;
2168 r1_bio->sector = sector_nr;
NeilBrown191ea9b2005-06-21 17:17:23 -07002169 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 set_bit(R1BIO_IsSync, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 for (i=0; i < conf->raid_disks; i++) {
NeilBrown3e198f72006-01-06 00:20:21 -08002173 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 bio = r1_bio->bios[i];
2175
2176 /* take from bio_init */
2177 bio->bi_next = NULL;
NeilBrowndb8d9d32010-10-07 12:00:50 +11002178 bio->bi_flags &= ~(BIO_POOL_MASK-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 bio->bi_flags |= 1 << BIO_UPTODATE;
NeilBrowndb8d9d32010-10-07 12:00:50 +11002180 bio->bi_comp_cpu = -1;
NeilBrown802ba062006-12-13 00:34:13 -08002181 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 bio->bi_vcnt = 0;
2183 bio->bi_idx = 0;
2184 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 bio->bi_size = 0;
2186 bio->bi_end_io = NULL;
2187 bio->bi_private = NULL;
2188
NeilBrown3e198f72006-01-06 00:20:21 -08002189 rdev = rcu_dereference(conf->mirrors[i].rdev);
2190 if (rdev == NULL ||
NeilBrown06f60382011-07-28 11:31:48 +10002191 test_bit(Faulty, &rdev->flags)) {
NeilBrowne3b97032005-08-04 12:53:34 -07002192 still_degraded = 1;
NeilBrown3e198f72006-01-06 00:20:21 -08002193 } else if (!test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 bio->bi_rw = WRITE;
2195 bio->bi_end_io = end_sync_write;
2196 write_targets ++;
NeilBrown3e198f72006-01-06 00:20:21 -08002197 } else {
2198 /* may need to read from here */
NeilBrown06f60382011-07-28 11:31:48 +10002199 sector_t first_bad = MaxSector;
2200 int bad_sectors;
2201
2202 if (is_badblock(rdev, sector_nr, good_sectors,
2203 &first_bad, &bad_sectors)) {
2204 if (first_bad > sector_nr)
2205 good_sectors = first_bad - sector_nr;
2206 else {
2207 bad_sectors -= (sector_nr - first_bad);
2208 if (min_bad == 0 ||
2209 min_bad > bad_sectors)
2210 min_bad = bad_sectors;
2211 }
NeilBrown3e198f72006-01-06 00:20:21 -08002212 }
NeilBrown06f60382011-07-28 11:31:48 +10002213 if (sector_nr < first_bad) {
2214 if (test_bit(WriteMostly, &rdev->flags)) {
2215 if (wonly < 0)
2216 wonly = i;
2217 } else {
2218 if (disk < 0)
2219 disk = i;
2220 }
2221 bio->bi_rw = READ;
2222 bio->bi_end_io = end_sync_read;
2223 read_targets++;
2224 }
NeilBrown3e198f72006-01-06 00:20:21 -08002225 }
NeilBrown06f60382011-07-28 11:31:48 +10002226 if (bio->bi_end_io) {
2227 atomic_inc(&rdev->nr_pending);
2228 bio->bi_sector = sector_nr + rdev->data_offset;
2229 bio->bi_bdev = rdev->bdev;
2230 bio->bi_private = r1_bio;
2231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 }
NeilBrown3e198f72006-01-06 00:20:21 -08002233 rcu_read_unlock();
2234 if (disk < 0)
2235 disk = wonly;
2236 r1_bio->read_disk = disk;
NeilBrown191ea9b2005-06-21 17:17:23 -07002237
NeilBrown06f60382011-07-28 11:31:48 +10002238 if (read_targets == 0 && min_bad > 0) {
2239 /* These sectors are bad on all InSync devices, so we
2240 * need to mark them bad on all write targets
2241 */
2242 int ok = 1;
2243 for (i = 0 ; i < conf->raid_disks ; i++)
2244 if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
2245 mdk_rdev_t *rdev =
2246 rcu_dereference(conf->mirrors[i].rdev);
2247 ok = rdev_set_badblocks(rdev, sector_nr,
2248 min_bad, 0
2249 ) && ok;
2250 }
2251 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2252 *skipped = 1;
2253 put_buf(r1_bio);
2254
2255 if (!ok) {
2256 /* Cannot record the badblocks, so need to
2257 * abort the resync.
2258 * If there are multiple read targets, could just
2259 * fail the really bad ones ???
2260 */
2261 conf->recovery_disabled = mddev->recovery_disabled;
2262 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2263 return 0;
2264 } else
2265 return min_bad;
2266
2267 }
2268 if (min_bad > 0 && min_bad < good_sectors) {
2269 /* only resync enough to reach the next bad->good
2270 * transition */
2271 good_sectors = min_bad;
2272 }
2273
NeilBrown3e198f72006-01-06 00:20:21 -08002274 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
2275 /* extra read targets are also write targets */
2276 write_targets += read_targets-1;
2277
2278 if (write_targets == 0 || read_targets == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 /* There is nowhere to write, so all non-sync
2280 * drives must be failed - so we are finished
2281 */
NeilBrown57afd892005-06-21 17:17:13 -07002282 sector_t rv = max_sector - sector_nr;
2283 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 put_buf(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 return rv;
2286 }
2287
NeilBrownc6207272008-02-06 01:39:52 -08002288 if (max_sector > mddev->resync_max)
2289 max_sector = mddev->resync_max; /* Don't do IO beyond here */
NeilBrown06f60382011-07-28 11:31:48 +10002290 if (max_sector > sector_nr + good_sectors)
2291 max_sector = sector_nr + good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 nr_sectors = 0;
NeilBrown289e99e2005-06-21 17:17:24 -07002293 sync_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 do {
2295 struct page *page;
2296 int len = PAGE_SIZE;
2297 if (sector_nr + (len>>9) > max_sector)
2298 len = (max_sector - sector_nr) << 9;
2299 if (len == 0)
2300 break;
NeilBrown6a806c52005-07-15 03:56:35 -07002301 if (sync_blocks == 0) {
2302 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
NeilBrowne5de485f2005-11-08 21:39:38 -08002303 &sync_blocks, still_degraded) &&
2304 !conf->fullsync &&
2305 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
NeilBrown6a806c52005-07-15 03:56:35 -07002306 break;
Eric Sesterhenn9e77c482006-04-01 01:08:49 +02002307 BUG_ON(sync_blocks < (PAGE_SIZE>>9));
NeilBrown7571ae82010-10-07 11:54:46 +11002308 if ((len >> 9) > sync_blocks)
NeilBrown6a806c52005-07-15 03:56:35 -07002309 len = sync_blocks<<9;
NeilBrownab7a30c2005-06-21 17:17:23 -07002310 }
NeilBrown191ea9b2005-06-21 17:17:23 -07002311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 for (i=0 ; i < conf->raid_disks; i++) {
2313 bio = r1_bio->bios[i];
2314 if (bio->bi_end_io) {
NeilBrownd11c1712006-01-06 00:20:26 -08002315 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 if (bio_add_page(bio, page, len, 0) == 0) {
2317 /* stop here */
NeilBrownd11c1712006-01-06 00:20:26 -08002318 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 while (i > 0) {
2320 i--;
2321 bio = r1_bio->bios[i];
NeilBrown6a806c52005-07-15 03:56:35 -07002322 if (bio->bi_end_io==NULL)
2323 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 /* remove last page from this bio */
2325 bio->bi_vcnt--;
2326 bio->bi_size -= len;
2327 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
2328 }
2329 goto bio_full;
2330 }
2331 }
2332 }
2333 nr_sectors += len>>9;
2334 sector_nr += len>>9;
NeilBrown191ea9b2005-06-21 17:17:23 -07002335 sync_blocks -= (len>>9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
2337 bio_full:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 r1_bio->sectors = nr_sectors;
2339
NeilBrownd11c1712006-01-06 00:20:26 -08002340 /* For a user-requested sync, we read all readable devices and do a
2341 * compare
2342 */
2343 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2344 atomic_set(&r1_bio->remaining, read_targets);
2345 for (i=0; i<conf->raid_disks; i++) {
2346 bio = r1_bio->bios[i];
2347 if (bio->bi_end_io == end_sync_read) {
NeilBrownddac7c72006-08-31 21:27:36 -07002348 md_sync_acct(bio->bi_bdev, nr_sectors);
NeilBrownd11c1712006-01-06 00:20:26 -08002349 generic_make_request(bio);
2350 }
2351 }
2352 } else {
2353 atomic_set(&r1_bio->remaining, 1);
2354 bio = r1_bio->bios[r1_bio->read_disk];
NeilBrownddac7c72006-08-31 21:27:36 -07002355 md_sync_acct(bio->bi_bdev, nr_sectors);
NeilBrownd11c1712006-01-06 00:20:26 -08002356 generic_make_request(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
NeilBrownd11c1712006-01-06 00:20:26 -08002358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 return nr_sectors;
2360}
2361
Dan Williams80c3a6c2009-03-17 18:10:40 -07002362static sector_t raid1_size(mddev_t *mddev, sector_t sectors, int raid_disks)
2363{
2364 if (sectors)
2365 return sectors;
2366
2367 return mddev->dev_sectors;
2368}
2369
NeilBrown709ae482009-12-14 12:49:51 +11002370static conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
2372 conf_t *conf;
NeilBrown709ae482009-12-14 12:49:51 +11002373 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 mirror_info_t *disk;
2375 mdk_rdev_t *rdev;
NeilBrown709ae482009-12-14 12:49:51 +11002376 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
NeilBrown9ffae0c2006-01-06 00:20:32 -08002378 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 if (!conf)
NeilBrown709ae482009-12-14 12:49:51 +11002380 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
NeilBrown9ffae0c2006-01-06 00:20:32 -08002382 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 GFP_KERNEL);
2384 if (!conf->mirrors)
NeilBrown709ae482009-12-14 12:49:51 +11002385 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
NeilBrownddaf22a2006-01-06 00:20:19 -08002387 conf->tmppage = alloc_page(GFP_KERNEL);
2388 if (!conf->tmppage)
NeilBrown709ae482009-12-14 12:49:51 +11002389 goto abort;
NeilBrownddaf22a2006-01-06 00:20:19 -08002390
NeilBrown709ae482009-12-14 12:49:51 +11002391 conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 if (!conf->poolinfo)
NeilBrown709ae482009-12-14 12:49:51 +11002393 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 conf->poolinfo->raid_disks = mddev->raid_disks;
2395 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
2396 r1bio_pool_free,
2397 conf->poolinfo);
2398 if (!conf->r1bio_pool)
NeilBrown709ae482009-12-14 12:49:51 +11002399 goto abort;
2400
NeilBrowned9bfdf2009-10-16 15:55:44 +11002401 conf->poolinfo->mddev = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Neil Browne7e72bf2008-05-14 16:05:54 -07002403 spin_lock_init(&conf->device_lock);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002404 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown709ae482009-12-14 12:49:51 +11002405 int disk_idx = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 if (disk_idx >= mddev->raid_disks
2407 || disk_idx < 0)
2408 continue;
2409 disk = conf->mirrors + disk_idx;
2410
2411 disk->rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
2413 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 }
2415 conf->raid_disks = mddev->raid_disks;
2416 conf->mddev = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 INIT_LIST_HEAD(&conf->retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
2419 spin_lock_init(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -08002420 init_waitqueue_head(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
NeilBrown191ea9b2005-06-21 17:17:23 -07002422 bio_list_init(&conf->pending_bio_list);
NeilBrown191ea9b2005-06-21 17:17:23 -07002423
NeilBrown709ae482009-12-14 12:49:51 +11002424 conf->last_used = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 for (i = 0; i < conf->raid_disks; i++) {
2426
2427 disk = conf->mirrors + i;
2428
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002429 if (!disk->rdev ||
2430 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 disk->head_position = 0;
NeilBrown918f0232007-08-22 14:01:52 -07002432 if (disk->rdev)
2433 conf->fullsync = 1;
NeilBrown709ae482009-12-14 12:49:51 +11002434 } else if (conf->last_used < 0)
2435 /*
2436 * The first working device is used as a
2437 * starting point to read balancing.
2438 */
2439 conf->last_used = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 }
NeilBrown709ae482009-12-14 12:49:51 +11002441
2442 err = -EIO;
2443 if (conf->last_used < 0) {
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002444 printk(KERN_ERR "md/raid1:%s: no operational mirrors\n",
NeilBrown709ae482009-12-14 12:49:51 +11002445 mdname(mddev));
2446 goto abort;
NeilBrown11ce99e2006-10-03 01:15:52 -07002447 }
NeilBrown709ae482009-12-14 12:49:51 +11002448 err = -ENOMEM;
2449 conf->thread = md_register_thread(raid1d, mddev, NULL);
2450 if (!conf->thread) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002451 printk(KERN_ERR
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002452 "md/raid1:%s: couldn't allocate thread\n",
NeilBrown191ea9b2005-06-21 17:17:23 -07002453 mdname(mddev));
NeilBrown709ae482009-12-14 12:49:51 +11002454 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 }
NeilBrown191ea9b2005-06-21 17:17:23 -07002456
NeilBrown709ae482009-12-14 12:49:51 +11002457 return conf;
2458
2459 abort:
2460 if (conf) {
2461 if (conf->r1bio_pool)
2462 mempool_destroy(conf->r1bio_pool);
2463 kfree(conf->mirrors);
2464 safe_put_page(conf->tmppage);
2465 kfree(conf->poolinfo);
2466 kfree(conf);
2467 }
2468 return ERR_PTR(err);
2469}
2470
2471static int run(mddev_t *mddev)
2472{
2473 conf_t *conf;
2474 int i;
2475 mdk_rdev_t *rdev;
2476
2477 if (mddev->level != 1) {
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002478 printk(KERN_ERR "md/raid1:%s: raid level not set to mirroring (%d)\n",
NeilBrown709ae482009-12-14 12:49:51 +11002479 mdname(mddev), mddev->level);
2480 return -EIO;
2481 }
2482 if (mddev->reshape_position != MaxSector) {
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002483 printk(KERN_ERR "md/raid1:%s: reshape_position set but not supported\n",
NeilBrown709ae482009-12-14 12:49:51 +11002484 mdname(mddev));
2485 return -EIO;
2486 }
2487 /*
2488 * copy the already verified devices into our private RAID1
2489 * bookkeeping area. [whatever we allocate in run(),
2490 * should be freed in stop()]
2491 */
2492 if (mddev->private == NULL)
2493 conf = setup_conf(mddev);
2494 else
2495 conf = mddev->private;
2496
2497 if (IS_ERR(conf))
2498 return PTR_ERR(conf);
2499
NeilBrown709ae482009-12-14 12:49:51 +11002500 list_for_each_entry(rdev, &mddev->disks, same_set) {
Jonathan Brassow1ed72422011-06-07 17:50:35 -05002501 if (!mddev->gendisk)
2502 continue;
NeilBrown709ae482009-12-14 12:49:51 +11002503 disk_stack_limits(mddev->gendisk, rdev->bdev,
2504 rdev->data_offset << 9);
2505 /* as we don't honour merge_bvec_fn, we must never risk
NeilBrown627a2d32010-03-08 16:44:38 +11002506 * violating it, so limit ->max_segments to 1 lying within
2507 * a single page, as a one page request is never in violation.
NeilBrown709ae482009-12-14 12:49:51 +11002508 */
NeilBrown627a2d32010-03-08 16:44:38 +11002509 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2510 blk_queue_max_segments(mddev->queue, 1);
2511 blk_queue_segment_boundary(mddev->queue,
2512 PAGE_CACHE_SIZE - 1);
2513 }
NeilBrown709ae482009-12-14 12:49:51 +11002514 }
2515
2516 mddev->degraded = 0;
2517 for (i=0; i < conf->raid_disks; i++)
2518 if (conf->mirrors[i].rdev == NULL ||
2519 !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
2520 test_bit(Faulty, &conf->mirrors[i].rdev->flags))
2521 mddev->degraded++;
2522
2523 if (conf->raid_disks - mddev->degraded == 1)
2524 mddev->recovery_cp = MaxSector;
2525
Andre Noll8c6ac862009-06-18 08:48:06 +10002526 if (mddev->recovery_cp != MaxSector)
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002527 printk(KERN_NOTICE "md/raid1:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10002528 " -- starting background reconstruction\n",
2529 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 printk(KERN_INFO
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002531 "md/raid1:%s: active with %d out of %d mirrors\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 mdname(mddev), mddev->raid_disks - mddev->degraded,
2533 mddev->raid_disks);
NeilBrown709ae482009-12-14 12:49:51 +11002534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 /*
2536 * Ok, everything is just fine now
2537 */
NeilBrown709ae482009-12-14 12:49:51 +11002538 mddev->thread = conf->thread;
2539 conf->thread = NULL;
2540 mddev->private = conf;
2541
Dan Williams1f403622009-03-31 14:59:03 +11002542 md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Jonathan Brassow1ed72422011-06-07 17:50:35 -05002544 if (mddev->queue) {
2545 mddev->queue->backing_dev_info.congested_fn = raid1_congested;
2546 mddev->queue->backing_dev_info.congested_data = mddev;
2547 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01002548 return md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549}
2550
2551static int stop(mddev_t *mddev)
2552{
NeilBrown070ec552009-06-16 16:54:21 +10002553 conf_t *conf = mddev->private;
NeilBrown4b6d2872005-09-09 16:23:47 -07002554 struct bitmap *bitmap = mddev->bitmap;
NeilBrown4b6d2872005-09-09 16:23:47 -07002555
2556 /* wait for behind writes to complete */
NeilBrowne5551902010-03-31 11:21:44 +11002557 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002558 printk(KERN_INFO "md/raid1:%s: behind writes in progress - waiting to stop.\n",
2559 mdname(mddev));
NeilBrown4b6d2872005-09-09 16:23:47 -07002560 /* need to kick something here to make sure I/O goes? */
NeilBrowne5551902010-03-31 11:21:44 +11002561 wait_event(bitmap->behind_wait,
2562 atomic_read(&bitmap->behind_writes) == 0);
NeilBrown4b6d2872005-09-09 16:23:47 -07002563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
NeilBrown409c57f2009-03-31 14:39:39 +11002565 raise_barrier(conf);
2566 lower_barrier(conf);
2567
NeilBrown01f96c02011-09-21 15:30:20 +10002568 md_unregister_thread(&mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 if (conf->r1bio_pool)
2570 mempool_destroy(conf->r1bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002571 kfree(conf->mirrors);
2572 kfree(conf->poolinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 kfree(conf);
2574 mddev->private = NULL;
2575 return 0;
2576}
2577
2578static int raid1_resize(mddev_t *mddev, sector_t sectors)
2579{
2580 /* no resync is happening, and there is enough space
2581 * on all devices, so we can resize.
2582 * We need to make sure resync covers any new space.
2583 * If the array is shrinking we should possibly wait until
2584 * any io in the removed space completes, but it hardly seems
2585 * worth it.
2586 */
Dan Williams1f403622009-03-31 14:59:03 +11002587 md_set_array_sectors(mddev, raid1_size(mddev, sectors, 0));
Dan Williamsb522adc2009-03-31 15:00:31 +11002588 if (mddev->array_sectors > raid1_size(mddev, sectors, 0))
2589 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10002590 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10002591 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11002592 if (sectors > mddev->dev_sectors &&
NeilBrownb0986362011-05-11 15:52:21 +10002593 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11002594 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2596 }
Dan Williamsb522adc2009-03-31 15:00:31 +11002597 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07002598 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 return 0;
2600}
2601
NeilBrown63c70c42006-03-27 01:18:13 -08002602static int raid1_reshape(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
2604 /* We need to:
2605 * 1/ resize the r1bio_pool
2606 * 2/ resize conf->mirrors
2607 *
2608 * We allocate a new r1bio_pool if we can.
2609 * Then raise a device barrier and wait until all IO stops.
2610 * Then resize conf->mirrors and swap in the new r1bio pool.
NeilBrown6ea9c072005-06-21 17:17:09 -07002611 *
2612 * At the same time, we "pack" the devices so that all the missing
2613 * devices have the higher raid_disk numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 */
2615 mempool_t *newpool, *oldpool;
2616 struct pool_info *newpoolinfo;
2617 mirror_info_t *newmirrors;
NeilBrown070ec552009-06-16 16:54:21 +10002618 conf_t *conf = mddev->private;
NeilBrown63c70c42006-03-27 01:18:13 -08002619 int cnt, raid_disks;
NeilBrownc04be0a2006-10-03 01:15:53 -07002620 unsigned long flags;
Dan Williamsb5470dc2008-06-27 21:44:04 -07002621 int d, d2, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
NeilBrown63c70c42006-03-27 01:18:13 -08002623 /* Cannot change chunk_size, layout, or level */
Andre Noll664e7c42009-06-18 08:45:27 +10002624 if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
NeilBrown63c70c42006-03-27 01:18:13 -08002625 mddev->layout != mddev->new_layout ||
2626 mddev->level != mddev->new_level) {
Andre Noll664e7c42009-06-18 08:45:27 +10002627 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown63c70c42006-03-27 01:18:13 -08002628 mddev->new_layout = mddev->layout;
2629 mddev->new_level = mddev->level;
2630 return -EINVAL;
2631 }
2632
Dan Williamsb5470dc2008-06-27 21:44:04 -07002633 err = md_allow_write(mddev);
2634 if (err)
2635 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08002636
NeilBrown63c70c42006-03-27 01:18:13 -08002637 raid_disks = mddev->raid_disks + mddev->delta_disks;
2638
NeilBrown6ea9c072005-06-21 17:17:09 -07002639 if (raid_disks < conf->raid_disks) {
2640 cnt=0;
2641 for (d= 0; d < conf->raid_disks; d++)
2642 if (conf->mirrors[d].rdev)
2643 cnt++;
2644 if (cnt > raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 return -EBUSY;
NeilBrown6ea9c072005-06-21 17:17:09 -07002646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
2648 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
2649 if (!newpoolinfo)
2650 return -ENOMEM;
2651 newpoolinfo->mddev = mddev;
2652 newpoolinfo->raid_disks = raid_disks;
2653
2654 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
2655 r1bio_pool_free, newpoolinfo);
2656 if (!newpool) {
2657 kfree(newpoolinfo);
2658 return -ENOMEM;
2659 }
NeilBrown9ffae0c2006-01-06 00:20:32 -08002660 newmirrors = kzalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 if (!newmirrors) {
2662 kfree(newpoolinfo);
2663 mempool_destroy(newpool);
2664 return -ENOMEM;
2665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
NeilBrown17999be2006-01-06 00:20:12 -08002667 raise_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
2669 /* ok, everything is stopped */
2670 oldpool = conf->r1bio_pool;
2671 conf->r1bio_pool = newpool;
NeilBrown6ea9c072005-06-21 17:17:09 -07002672
NeilBrowna88aa782007-08-22 14:01:53 -07002673 for (d = d2 = 0; d < conf->raid_disks; d++) {
2674 mdk_rdev_t *rdev = conf->mirrors[d].rdev;
2675 if (rdev && rdev->raid_disk != d2) {
Namhyung Kim36fad852011-07-27 11:00:36 +10002676 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna88aa782007-08-22 14:01:53 -07002677 rdev->raid_disk = d2;
Namhyung Kim36fad852011-07-27 11:00:36 +10002678 sysfs_unlink_rdev(mddev, rdev);
2679 if (sysfs_link_rdev(mddev, rdev))
NeilBrowna88aa782007-08-22 14:01:53 -07002680 printk(KERN_WARNING
Namhyung Kim36fad852011-07-27 11:00:36 +10002681 "md/raid1:%s: cannot register rd%d\n",
2682 mdname(mddev), rdev->raid_disk);
NeilBrown6ea9c072005-06-21 17:17:09 -07002683 }
NeilBrowna88aa782007-08-22 14:01:53 -07002684 if (rdev)
2685 newmirrors[d2++].rdev = rdev;
2686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 kfree(conf->mirrors);
2688 conf->mirrors = newmirrors;
2689 kfree(conf->poolinfo);
2690 conf->poolinfo = newpoolinfo;
2691
NeilBrownc04be0a2006-10-03 01:15:53 -07002692 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 mddev->degraded += (raid_disks - conf->raid_disks);
NeilBrownc04be0a2006-10-03 01:15:53 -07002694 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 conf->raid_disks = mddev->raid_disks = raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08002696 mddev->delta_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
NeilBrown6ea9c072005-06-21 17:17:09 -07002698 conf->last_used = 0; /* just make sure it is in-range */
NeilBrown17999be2006-01-06 00:20:12 -08002699 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
2701 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2702 md_wakeup_thread(mddev->thread);
2703
2704 mempool_destroy(oldpool);
2705 return 0;
2706}
2707
NeilBrown500af872005-09-09 16:23:58 -07002708static void raid1_quiesce(mddev_t *mddev, int state)
NeilBrown36fa3062005-09-09 16:23:45 -07002709{
NeilBrown070ec552009-06-16 16:54:21 +10002710 conf_t *conf = mddev->private;
NeilBrown36fa3062005-09-09 16:23:45 -07002711
2712 switch(state) {
NeilBrown6eef4b22009-12-14 12:49:51 +11002713 case 2: /* wake for suspend */
2714 wake_up(&conf->wait_barrier);
2715 break;
NeilBrown9e6603d2005-09-09 16:23:48 -07002716 case 1:
NeilBrown17999be2006-01-06 00:20:12 -08002717 raise_barrier(conf);
NeilBrown36fa3062005-09-09 16:23:45 -07002718 break;
NeilBrown9e6603d2005-09-09 16:23:48 -07002719 case 0:
NeilBrown17999be2006-01-06 00:20:12 -08002720 lower_barrier(conf);
NeilBrown36fa3062005-09-09 16:23:45 -07002721 break;
2722 }
NeilBrown36fa3062005-09-09 16:23:45 -07002723}
2724
NeilBrown709ae482009-12-14 12:49:51 +11002725static void *raid1_takeover(mddev_t *mddev)
2726{
2727 /* raid1 can take over:
2728 * raid5 with 2 devices, any layout or chunk size
2729 */
2730 if (mddev->level == 5 && mddev->raid_disks == 2) {
2731 conf_t *conf;
2732 mddev->new_level = 1;
2733 mddev->new_layout = 0;
2734 mddev->new_chunk_sectors = 0;
2735 conf = setup_conf(mddev);
2736 if (!IS_ERR(conf))
2737 conf->barrier = 1;
2738 return conf;
2739 }
2740 return ERR_PTR(-EINVAL);
2741}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
NeilBrown2604b702006-01-06 00:20:36 -08002743static struct mdk_personality raid1_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744{
2745 .name = "raid1",
NeilBrown2604b702006-01-06 00:20:36 -08002746 .level = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 .owner = THIS_MODULE,
2748 .make_request = make_request,
2749 .run = run,
2750 .stop = stop,
2751 .status = status,
2752 .error_handler = error,
2753 .hot_add_disk = raid1_add_disk,
2754 .hot_remove_disk= raid1_remove_disk,
2755 .spare_active = raid1_spare_active,
2756 .sync_request = sync_request,
2757 .resize = raid1_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07002758 .size = raid1_size,
NeilBrown63c70c42006-03-27 01:18:13 -08002759 .check_reshape = raid1_reshape,
NeilBrown36fa3062005-09-09 16:23:45 -07002760 .quiesce = raid1_quiesce,
NeilBrown709ae482009-12-14 12:49:51 +11002761 .takeover = raid1_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762};
2763
2764static int __init raid_init(void)
2765{
NeilBrown2604b702006-01-06 00:20:36 -08002766 return register_md_personality(&raid1_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767}
2768
2769static void raid_exit(void)
2770{
NeilBrown2604b702006-01-06 00:20:36 -08002771 unregister_md_personality(&raid1_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772}
2773
2774module_init(raid_init);
2775module_exit(raid_exit);
2776MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11002777MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778MODULE_ALIAS("md-personality-3"); /* RAID1 */
NeilBrownd9d166c2006-01-06 00:20:51 -08002779MODULE_ALIAS("md-raid1");
NeilBrown2604b702006-01-06 00:20:36 -08002780MODULE_ALIAS("md-level-1");