blob: a0c02fb8af2ad55d6ed033f07373f8b2732b6db2 [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
Namhyung Kimba3ae3b2011-10-07 14:22:53 +11001361 /* this will call update_head_pos() */
1362 find_bio_disk(r1_bio, bio);
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 /*
1365 * we have read a block, now it needs to be re-written,
1366 * or re-read if the read failed.
1367 * We don't do much here, just schedule handling by raid1d
1368 */
NeilBrown69382e82006-01-06 00:20:22 -08001369 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 set_bit(R1BIO_Uptodate, &r1_bio->state);
NeilBrownd11c1712006-01-06 00:20:26 -08001371
1372 if (atomic_dec_and_test(&r1_bio->remaining))
1373 reschedule_retry(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
NeilBrown6712ecf2007-09-27 12:47:43 +02001376static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001379 r1bio_t *r1_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 mddev_t *mddev = r1_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001381 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 int mirror=0;
NeilBrown4367af52011-07-28 11:31:49 +10001383 sector_t first_bad;
1384 int bad_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Namhyung Kimba3ae3b2011-10-07 14:22:53 +11001386 mirror = find_bio_disk(r1_bio, bio);
1387
NeilBrown6b1117d2006-03-31 02:31:57 -08001388 if (!uptodate) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001389 sector_t sync_blocks = 0;
NeilBrown6b1117d2006-03-31 02:31:57 -08001390 sector_t s = r1_bio->sector;
1391 long sectors_to_go = r1_bio->sectors;
1392 /* make sure these bits doesn't get cleared. */
1393 do {
NeilBrown5e3db642006-07-10 04:44:18 -07001394 bitmap_end_sync(mddev->bitmap, s,
NeilBrown6b1117d2006-03-31 02:31:57 -08001395 &sync_blocks, 1);
1396 s += sync_blocks;
1397 sectors_to_go -= sync_blocks;
1398 } while (sectors_to_go > 0);
NeilBrownd8f05d22011-07-28 11:33:00 +10001399 set_bit(WriteErrorSeen,
1400 &conf->mirrors[mirror].rdev->flags);
1401 set_bit(R1BIO_WriteError, &r1_bio->state);
NeilBrown4367af52011-07-28 11:31:49 +10001402 } else if (is_badblock(conf->mirrors[mirror].rdev,
1403 r1_bio->sector,
1404 r1_bio->sectors,
NeilBrown3a9f28a2011-07-28 11:33:42 +10001405 &first_bad, &bad_sectors) &&
1406 !is_badblock(conf->mirrors[r1_bio->read_disk].rdev,
1407 r1_bio->sector,
1408 r1_bio->sectors,
1409 &first_bad, &bad_sectors)
1410 )
NeilBrown4367af52011-07-28 11:31:49 +10001411 set_bit(R1BIO_MadeGood, &r1_bio->state);
NeilBrowne3b97032005-08-04 12:53:34 -07001412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown4367af52011-07-28 11:31:49 +10001414 int s = r1_bio->sectors;
NeilBrownd8f05d22011-07-28 11:33:00 +10001415 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
1416 test_bit(R1BIO_WriteError, &r1_bio->state))
NeilBrown4367af52011-07-28 11:31:49 +10001417 reschedule_retry(r1_bio);
1418 else {
1419 put_buf(r1_bio);
1420 md_done_sync(mddev, s, uptodate);
1421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
NeilBrownd8f05d22011-07-28 11:33:00 +10001425static int r1_sync_page_io(mdk_rdev_t *rdev, sector_t sector,
1426 int sectors, struct page *page, int rw)
1427{
1428 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
1429 /* success */
1430 return 1;
1431 if (rw == WRITE)
1432 set_bit(WriteErrorSeen, &rdev->flags);
1433 /* need to record an error - either for the block or the device */
1434 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
1435 md_error(rdev->mddev, rdev);
1436 return 0;
1437}
1438
NeilBrowna68e5872011-05-11 14:40:44 +10001439static int fix_sync_read_error(r1bio_t *r1_bio)
1440{
1441 /* Try some synchronous reads of other devices to get
1442 * good data, much like with normal read errors. Only
1443 * read into the pages we already have so we don't
1444 * need to re-issue the read request.
1445 * We don't need to freeze the array, because being in an
1446 * active sync request, there is no normal IO, and
1447 * no overlapping syncs.
NeilBrown06f60382011-07-28 11:31:48 +10001448 * We don't need to check is_badblock() again as we
1449 * made sure that anything with a bad block in range
1450 * will have bi_end_io clear.
NeilBrowna68e5872011-05-11 14:40:44 +10001451 */
1452 mddev_t *mddev = r1_bio->mddev;
1453 conf_t *conf = mddev->private;
1454 struct bio *bio = r1_bio->bios[r1_bio->read_disk];
1455 sector_t sect = r1_bio->sector;
1456 int sectors = r1_bio->sectors;
1457 int idx = 0;
1458
1459 while(sectors) {
1460 int s = sectors;
1461 int d = r1_bio->read_disk;
1462 int success = 0;
1463 mdk_rdev_t *rdev;
NeilBrown78d7f5f2011-05-11 14:48:56 +10001464 int start;
NeilBrowna68e5872011-05-11 14:40:44 +10001465
1466 if (s > (PAGE_SIZE>>9))
1467 s = PAGE_SIZE >> 9;
1468 do {
1469 if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
1470 /* No rcu protection needed here devices
1471 * can only be removed when no resync is
1472 * active, and resync is currently active
1473 */
1474 rdev = conf->mirrors[d].rdev;
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001475 if (sync_page_io(rdev, sect, s<<9,
NeilBrowna68e5872011-05-11 14:40:44 +10001476 bio->bi_io_vec[idx].bv_page,
1477 READ, false)) {
1478 success = 1;
1479 break;
1480 }
1481 }
1482 d++;
1483 if (d == conf->raid_disks)
1484 d = 0;
1485 } while (!success && d != r1_bio->read_disk);
1486
NeilBrown78d7f5f2011-05-11 14:48:56 +10001487 if (!success) {
NeilBrowna68e5872011-05-11 14:40:44 +10001488 char b[BDEVNAME_SIZE];
NeilBrown3a9f28a2011-07-28 11:33:42 +10001489 int abort = 0;
1490 /* Cannot read from anywhere, this block is lost.
1491 * Record a bad block on each device. If that doesn't
1492 * work just disable and interrupt the recovery.
1493 * Don't fail devices as that won't really help.
1494 */
NeilBrowna68e5872011-05-11 14:40:44 +10001495 printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O read error"
1496 " for block %llu\n",
1497 mdname(mddev),
1498 bdevname(bio->bi_bdev, b),
1499 (unsigned long long)r1_bio->sector);
NeilBrown3a9f28a2011-07-28 11:33:42 +10001500 for (d = 0; d < conf->raid_disks; d++) {
1501 rdev = conf->mirrors[d].rdev;
1502 if (!rdev || test_bit(Faulty, &rdev->flags))
1503 continue;
1504 if (!rdev_set_badblocks(rdev, sect, s, 0))
1505 abort = 1;
1506 }
1507 if (abort) {
1508 mddev->recovery_disabled = 1;
1509 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1510 md_done_sync(mddev, r1_bio->sectors, 0);
1511 put_buf(r1_bio);
1512 return 0;
1513 }
1514 /* Try next page */
1515 sectors -= s;
1516 sect += s;
1517 idx++;
1518 continue;
NeilBrowna68e5872011-05-11 14:40:44 +10001519 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001520
1521 start = d;
1522 /* write it back and re-read */
1523 while (d != r1_bio->read_disk) {
1524 if (d == 0)
1525 d = conf->raid_disks;
1526 d--;
1527 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1528 continue;
1529 rdev = conf->mirrors[d].rdev;
NeilBrownd8f05d22011-07-28 11:33:00 +10001530 if (r1_sync_page_io(rdev, sect, s,
1531 bio->bi_io_vec[idx].bv_page,
1532 WRITE) == 0) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10001533 r1_bio->bios[d]->bi_end_io = NULL;
1534 rdev_dec_pending(rdev, mddev);
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001535 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001536 }
1537 d = start;
1538 while (d != r1_bio->read_disk) {
1539 if (d == 0)
1540 d = conf->raid_disks;
1541 d--;
1542 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1543 continue;
1544 rdev = conf->mirrors[d].rdev;
NeilBrownd8f05d22011-07-28 11:33:00 +10001545 if (r1_sync_page_io(rdev, sect, s,
1546 bio->bi_io_vec[idx].bv_page,
1547 READ) != 0)
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001548 atomic_add(s, &rdev->corrected_errors);
NeilBrown78d7f5f2011-05-11 14:48:56 +10001549 }
NeilBrowna68e5872011-05-11 14:40:44 +10001550 sectors -= s;
1551 sect += s;
1552 idx ++;
1553 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001554 set_bit(R1BIO_Uptodate, &r1_bio->state);
NeilBrown7ca78d52011-05-11 14:50:37 +10001555 set_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrowna68e5872011-05-11 14:40:44 +10001556 return 1;
1557}
1558
1559static int process_checks(r1bio_t *r1_bio)
1560{
1561 /* We have read all readable devices. If we haven't
1562 * got the block, then there is no hope left.
1563 * If we have, then we want to do a comparison
1564 * and skip the write if everything is the same.
1565 * If any blocks failed to read, then we need to
1566 * attempt an over-write
1567 */
1568 mddev_t *mddev = r1_bio->mddev;
1569 conf_t *conf = mddev->private;
1570 int primary;
1571 int i;
1572
NeilBrown78d7f5f2011-05-11 14:48:56 +10001573 for (primary = 0; primary < conf->raid_disks; primary++)
NeilBrowna68e5872011-05-11 14:40:44 +10001574 if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
1575 test_bit(BIO_UPTODATE, &r1_bio->bios[primary]->bi_flags)) {
1576 r1_bio->bios[primary]->bi_end_io = NULL;
1577 rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
1578 break;
1579 }
1580 r1_bio->read_disk = primary;
NeilBrown78d7f5f2011-05-11 14:48:56 +10001581 for (i = 0; i < conf->raid_disks; i++) {
1582 int j;
1583 int vcnt = r1_bio->sectors >> (PAGE_SHIFT- 9);
1584 struct bio *pbio = r1_bio->bios[primary];
1585 struct bio *sbio = r1_bio->bios[i];
1586 int size;
NeilBrowna68e5872011-05-11 14:40:44 +10001587
NeilBrown78d7f5f2011-05-11 14:48:56 +10001588 if (r1_bio->bios[i]->bi_end_io != end_sync_read)
1589 continue;
NeilBrowna68e5872011-05-11 14:40:44 +10001590
NeilBrown78d7f5f2011-05-11 14:48:56 +10001591 if (test_bit(BIO_UPTODATE, &sbio->bi_flags)) {
1592 for (j = vcnt; j-- ; ) {
1593 struct page *p, *s;
1594 p = pbio->bi_io_vec[j].bv_page;
1595 s = sbio->bi_io_vec[j].bv_page;
1596 if (memcmp(page_address(p),
1597 page_address(s),
1598 PAGE_SIZE))
1599 break;
NeilBrowna68e5872011-05-11 14:40:44 +10001600 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001601 } else
1602 j = 0;
1603 if (j >= 0)
1604 mddev->resync_mismatches += r1_bio->sectors;
1605 if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
1606 && test_bit(BIO_UPTODATE, &sbio->bi_flags))) {
1607 /* No need to write to this device. */
1608 sbio->bi_end_io = NULL;
1609 rdev_dec_pending(conf->mirrors[i].rdev, mddev);
1610 continue;
NeilBrowna68e5872011-05-11 14:40:44 +10001611 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001612 /* fixup the bio for reuse */
1613 sbio->bi_vcnt = vcnt;
1614 sbio->bi_size = r1_bio->sectors << 9;
1615 sbio->bi_idx = 0;
1616 sbio->bi_phys_segments = 0;
1617 sbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1618 sbio->bi_flags |= 1 << BIO_UPTODATE;
1619 sbio->bi_next = NULL;
1620 sbio->bi_sector = r1_bio->sector +
1621 conf->mirrors[i].rdev->data_offset;
1622 sbio->bi_bdev = conf->mirrors[i].rdev->bdev;
1623 size = sbio->bi_size;
1624 for (j = 0; j < vcnt ; j++) {
1625 struct bio_vec *bi;
1626 bi = &sbio->bi_io_vec[j];
1627 bi->bv_offset = 0;
1628 if (size > PAGE_SIZE)
1629 bi->bv_len = PAGE_SIZE;
1630 else
1631 bi->bv_len = size;
1632 size -= PAGE_SIZE;
1633 memcpy(page_address(bi->bv_page),
1634 page_address(pbio->bi_io_vec[j].bv_page),
1635 PAGE_SIZE);
1636 }
1637 }
NeilBrowna68e5872011-05-11 14:40:44 +10001638 return 0;
1639}
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
1642{
NeilBrown070ec552009-06-16 16:54:21 +10001643 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 int i;
1645 int disks = conf->raid_disks;
1646 struct bio *bio, *wbio;
1647
1648 bio = r1_bio->bios[r1_bio->read_disk];
1649
NeilBrowna68e5872011-05-11 14:40:44 +10001650 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
1651 /* ouch - failed to read all of that. */
1652 if (!fix_sync_read_error(r1_bio))
1653 return;
NeilBrown7ca78d52011-05-11 14:50:37 +10001654
1655 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1656 if (process_checks(r1_bio) < 0)
1657 return;
NeilBrownd11c1712006-01-06 00:20:26 -08001658 /*
1659 * schedule writes
1660 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 atomic_set(&r1_bio->remaining, 1);
1662 for (i = 0; i < disks ; i++) {
1663 wbio = r1_bio->bios[i];
NeilBrown3e198f72006-01-06 00:20:21 -08001664 if (wbio->bi_end_io == NULL ||
1665 (wbio->bi_end_io == end_sync_read &&
1666 (i == r1_bio->read_disk ||
1667 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 continue;
1669
NeilBrown3e198f72006-01-06 00:20:21 -08001670 wbio->bi_rw = WRITE;
1671 wbio->bi_end_io = end_sync_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 atomic_inc(&r1_bio->remaining);
1673 md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
NeilBrown191ea9b2005-06-21 17:17:23 -07001674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 generic_make_request(wbio);
1676 }
1677
1678 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001679 /* if we're here, all write(s) have completed, so clean up */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 md_done_sync(mddev, r1_bio->sectors, 1);
1681 put_buf(r1_bio);
1682 }
1683}
1684
1685/*
1686 * This is a kernel thread which:
1687 *
1688 * 1. Retries failed read operations on working mirrors.
1689 * 2. Updates the raid superblock when problems encounter.
NeilBrownd2eb35a2011-07-28 11:31:48 +10001690 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 */
1692
NeilBrown867868f2006-10-03 01:15:51 -07001693static void fix_read_error(conf_t *conf, int read_disk,
1694 sector_t sect, int sectors)
1695{
1696 mddev_t *mddev = conf->mddev;
1697 while(sectors) {
1698 int s = sectors;
1699 int d = read_disk;
1700 int success = 0;
1701 int start;
1702 mdk_rdev_t *rdev;
1703
1704 if (s > (PAGE_SIZE>>9))
1705 s = PAGE_SIZE >> 9;
1706
1707 do {
1708 /* Note: no rcu protection needed here
1709 * as this is synchronous in the raid1d thread
1710 * which is the thread that might remove
1711 * a device. If raid1d ever becomes multi-threaded....
1712 */
NeilBrownd2eb35a2011-07-28 11:31:48 +10001713 sector_t first_bad;
1714 int bad_sectors;
1715
NeilBrown867868f2006-10-03 01:15:51 -07001716 rdev = conf->mirrors[d].rdev;
1717 if (rdev &&
1718 test_bit(In_sync, &rdev->flags) &&
NeilBrownd2eb35a2011-07-28 11:31:48 +10001719 is_badblock(rdev, sect, s,
1720 &first_bad, &bad_sectors) == 0 &&
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001721 sync_page_io(rdev, sect, s<<9,
1722 conf->tmppage, READ, false))
NeilBrown867868f2006-10-03 01:15:51 -07001723 success = 1;
1724 else {
1725 d++;
1726 if (d == conf->raid_disks)
1727 d = 0;
1728 }
1729 } while (!success && d != read_disk);
1730
1731 if (!success) {
NeilBrownd8f05d22011-07-28 11:33:00 +10001732 /* Cannot read from anywhere - mark it bad */
1733 mdk_rdev_t *rdev = conf->mirrors[read_disk].rdev;
1734 if (!rdev_set_badblocks(rdev, sect, s, 0))
1735 md_error(mddev, rdev);
NeilBrown867868f2006-10-03 01:15:51 -07001736 break;
1737 }
1738 /* write it back and re-read */
1739 start = d;
1740 while (d != read_disk) {
1741 if (d==0)
1742 d = conf->raid_disks;
1743 d--;
1744 rdev = conf->mirrors[d].rdev;
1745 if (rdev &&
NeilBrownd8f05d22011-07-28 11:33:00 +10001746 test_bit(In_sync, &rdev->flags))
1747 r1_sync_page_io(rdev, sect, s,
1748 conf->tmppage, WRITE);
NeilBrown867868f2006-10-03 01:15:51 -07001749 }
1750 d = start;
1751 while (d != read_disk) {
1752 char b[BDEVNAME_SIZE];
1753 if (d==0)
1754 d = conf->raid_disks;
1755 d--;
1756 rdev = conf->mirrors[d].rdev;
1757 if (rdev &&
1758 test_bit(In_sync, &rdev->flags)) {
NeilBrownd8f05d22011-07-28 11:33:00 +10001759 if (r1_sync_page_io(rdev, sect, s,
1760 conf->tmppage, READ)) {
NeilBrown867868f2006-10-03 01:15:51 -07001761 atomic_add(s, &rdev->corrected_errors);
1762 printk(KERN_INFO
NeilBrown9dd1e2f2010-05-03 14:30:35 +10001763 "md/raid1:%s: read error corrected "
NeilBrown867868f2006-10-03 01:15:51 -07001764 "(%d sectors at %llu on %s)\n",
1765 mdname(mddev), s,
Randy Dunlap969b7552006-10-28 10:38:32 -07001766 (unsigned long long)(sect +
1767 rdev->data_offset),
NeilBrown867868f2006-10-03 01:15:51 -07001768 bdevname(rdev->bdev, b));
1769 }
1770 }
1771 }
1772 sectors -= s;
1773 sect += s;
1774 }
1775}
1776
NeilBrowncd5ff9a12011-07-28 11:32:41 +10001777static void bi_complete(struct bio *bio, int error)
1778{
1779 complete((struct completion *)bio->bi_private);
1780}
1781
1782static int submit_bio_wait(int rw, struct bio *bio)
1783{
1784 struct completion event;
1785 rw |= REQ_SYNC;
1786
1787 init_completion(&event);
1788 bio->bi_private = &event;
1789 bio->bi_end_io = bi_complete;
1790 submit_bio(rw, bio);
1791 wait_for_completion(&event);
1792
1793 return test_bit(BIO_UPTODATE, &bio->bi_flags);
1794}
1795
1796static int narrow_write_error(r1bio_t *r1_bio, int i)
1797{
1798 mddev_t *mddev = r1_bio->mddev;
1799 conf_t *conf = mddev->private;
1800 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
1801 int vcnt, idx;
1802 struct bio_vec *vec;
1803
1804 /* bio has the data to be written to device 'i' where
1805 * we just recently had a write error.
1806 * We repeatedly clone the bio and trim down to one block,
1807 * then try the write. Where the write fails we record
1808 * a bad block.
1809 * It is conceivable that the bio doesn't exactly align with
1810 * blocks. We must handle this somehow.
1811 *
1812 * We currently own a reference on the rdev.
1813 */
1814
1815 int block_sectors;
1816 sector_t sector;
1817 int sectors;
1818 int sect_to_write = r1_bio->sectors;
1819 int ok = 1;
1820
1821 if (rdev->badblocks.shift < 0)
1822 return 0;
1823
1824 block_sectors = 1 << rdev->badblocks.shift;
1825 sector = r1_bio->sector;
1826 sectors = ((sector + block_sectors)
1827 & ~(sector_t)(block_sectors - 1))
1828 - sector;
1829
1830 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
1831 vcnt = r1_bio->behind_page_count;
1832 vec = r1_bio->behind_bvecs;
1833 idx = 0;
1834 while (vec[idx].bv_page == NULL)
1835 idx++;
1836 } else {
1837 vcnt = r1_bio->master_bio->bi_vcnt;
1838 vec = r1_bio->master_bio->bi_io_vec;
1839 idx = r1_bio->master_bio->bi_idx;
1840 }
1841 while (sect_to_write) {
1842 struct bio *wbio;
1843 if (sectors > sect_to_write)
1844 sectors = sect_to_write;
1845 /* Write at 'sector' for 'sectors'*/
1846
1847 wbio = bio_alloc_mddev(GFP_NOIO, vcnt, mddev);
1848 memcpy(wbio->bi_io_vec, vec, vcnt * sizeof(struct bio_vec));
1849 wbio->bi_sector = r1_bio->sector;
1850 wbio->bi_rw = WRITE;
1851 wbio->bi_vcnt = vcnt;
1852 wbio->bi_size = r1_bio->sectors << 9;
1853 wbio->bi_idx = idx;
1854
1855 md_trim_bio(wbio, sector - r1_bio->sector, sectors);
1856 wbio->bi_sector += rdev->data_offset;
1857 wbio->bi_bdev = rdev->bdev;
1858 if (submit_bio_wait(WRITE, wbio) == 0)
1859 /* failure! */
1860 ok = rdev_set_badblocks(rdev, sector,
1861 sectors, 0)
1862 && ok;
1863
1864 bio_put(wbio);
1865 sect_to_write -= sectors;
1866 sector += sectors;
1867 sectors = block_sectors;
1868 }
1869 return ok;
1870}
1871
NeilBrown62096bc2011-07-28 11:38:13 +10001872static void handle_sync_write_finished(conf_t *conf, r1bio_t *r1_bio)
1873{
1874 int m;
1875 int s = r1_bio->sectors;
1876 for (m = 0; m < conf->raid_disks ; m++) {
1877 mdk_rdev_t *rdev = conf->mirrors[m].rdev;
1878 struct bio *bio = r1_bio->bios[m];
1879 if (bio->bi_end_io == NULL)
1880 continue;
1881 if (test_bit(BIO_UPTODATE, &bio->bi_flags) &&
1882 test_bit(R1BIO_MadeGood, &r1_bio->state)) {
1883 rdev_clear_badblocks(rdev, r1_bio->sector, s);
1884 }
1885 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
1886 test_bit(R1BIO_WriteError, &r1_bio->state)) {
1887 if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0))
1888 md_error(conf->mddev, rdev);
1889 }
1890 }
1891 put_buf(r1_bio);
1892 md_done_sync(conf->mddev, s, 1);
1893}
1894
1895static void handle_write_finished(conf_t *conf, r1bio_t *r1_bio)
1896{
1897 int m;
1898 for (m = 0; m < conf->raid_disks ; m++)
1899 if (r1_bio->bios[m] == IO_MADE_GOOD) {
1900 mdk_rdev_t *rdev = conf->mirrors[m].rdev;
1901 rdev_clear_badblocks(rdev,
1902 r1_bio->sector,
1903 r1_bio->sectors);
1904 rdev_dec_pending(rdev, conf->mddev);
1905 } else if (r1_bio->bios[m] != NULL) {
1906 /* This drive got a write error. We need to
1907 * narrow down and record precise write
1908 * errors.
1909 */
1910 if (!narrow_write_error(r1_bio, m)) {
1911 md_error(conf->mddev,
1912 conf->mirrors[m].rdev);
1913 /* an I/O failed, we can't clear the bitmap */
1914 set_bit(R1BIO_Degraded, &r1_bio->state);
1915 }
1916 rdev_dec_pending(conf->mirrors[m].rdev,
1917 conf->mddev);
1918 }
1919 if (test_bit(R1BIO_WriteError, &r1_bio->state))
1920 close_write(r1_bio);
1921 raid_end_bio_io(r1_bio);
1922}
1923
1924static void handle_read_error(conf_t *conf, r1bio_t *r1_bio)
1925{
1926 int disk;
1927 int max_sectors;
1928 mddev_t *mddev = conf->mddev;
1929 struct bio *bio;
1930 char b[BDEVNAME_SIZE];
1931 mdk_rdev_t *rdev;
1932
1933 clear_bit(R1BIO_ReadError, &r1_bio->state);
1934 /* we got a read error. Maybe the drive is bad. Maybe just
1935 * the block and we can fix it.
1936 * We freeze all other IO, and try reading the block from
1937 * other devices. When we find one, we re-write
1938 * and check it that fixes the read error.
1939 * This is all done synchronously while the array is
1940 * frozen
1941 */
1942 if (mddev->ro == 0) {
1943 freeze_array(conf);
1944 fix_read_error(conf, r1_bio->read_disk,
1945 r1_bio->sector, r1_bio->sectors);
1946 unfreeze_array(conf);
1947 } else
1948 md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
1949
1950 bio = r1_bio->bios[r1_bio->read_disk];
1951 bdevname(bio->bi_bdev, b);
1952read_more:
1953 disk = read_balance(conf, r1_bio, &max_sectors);
1954 if (disk == -1) {
1955 printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O"
1956 " read error for block %llu\n",
1957 mdname(mddev), b, (unsigned long long)r1_bio->sector);
1958 raid_end_bio_io(r1_bio);
1959 } else {
1960 const unsigned long do_sync
1961 = r1_bio->master_bio->bi_rw & REQ_SYNC;
1962 if (bio) {
1963 r1_bio->bios[r1_bio->read_disk] =
1964 mddev->ro ? IO_BLOCKED : NULL;
1965 bio_put(bio);
1966 }
1967 r1_bio->read_disk = disk;
1968 bio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev);
1969 md_trim_bio(bio, r1_bio->sector - bio->bi_sector, max_sectors);
1970 r1_bio->bios[r1_bio->read_disk] = bio;
1971 rdev = conf->mirrors[disk].rdev;
1972 printk_ratelimited(KERN_ERR
1973 "md/raid1:%s: redirecting sector %llu"
1974 " to other mirror: %s\n",
1975 mdname(mddev),
1976 (unsigned long long)r1_bio->sector,
1977 bdevname(rdev->bdev, b));
1978 bio->bi_sector = r1_bio->sector + rdev->data_offset;
1979 bio->bi_bdev = rdev->bdev;
1980 bio->bi_end_io = raid1_end_read_request;
1981 bio->bi_rw = READ | do_sync;
1982 bio->bi_private = r1_bio;
1983 if (max_sectors < r1_bio->sectors) {
1984 /* Drat - have to split this up more */
1985 struct bio *mbio = r1_bio->master_bio;
1986 int sectors_handled = (r1_bio->sector + max_sectors
1987 - mbio->bi_sector);
1988 r1_bio->sectors = max_sectors;
1989 spin_lock_irq(&conf->device_lock);
1990 if (mbio->bi_phys_segments == 0)
1991 mbio->bi_phys_segments = 2;
1992 else
1993 mbio->bi_phys_segments++;
1994 spin_unlock_irq(&conf->device_lock);
1995 generic_make_request(bio);
1996 bio = NULL;
1997
1998 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1999
2000 r1_bio->master_bio = mbio;
2001 r1_bio->sectors = (mbio->bi_size >> 9)
2002 - sectors_handled;
2003 r1_bio->state = 0;
2004 set_bit(R1BIO_ReadError, &r1_bio->state);
2005 r1_bio->mddev = mddev;
2006 r1_bio->sector = mbio->bi_sector + sectors_handled;
2007
2008 goto read_more;
2009 } else
2010 generic_make_request(bio);
2011 }
2012}
2013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014static void raid1d(mddev_t *mddev)
2015{
2016 r1bio_t *r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 unsigned long flags;
NeilBrown070ec552009-06-16 16:54:21 +10002018 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002020 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 md_check_recovery(mddev);
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002023
2024 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002026
NeilBrownc3b328a2011-04-18 18:25:43 +10002027 if (atomic_read(&mddev->plug_cnt) == 0)
2028 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002031 if (list_empty(head)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002032 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
2036 list_del(head->prev);
NeilBrownddaf22a2006-01-06 00:20:19 -08002037 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 spin_unlock_irqrestore(&conf->device_lock, flags);
2039
2040 mddev = r1_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002041 conf = mddev->private;
NeilBrown4367af52011-07-28 11:31:49 +10002042 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
NeilBrownd8f05d22011-07-28 11:33:00 +10002043 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
NeilBrown62096bc2011-07-28 11:38:13 +10002044 test_bit(R1BIO_WriteError, &r1_bio->state))
2045 handle_sync_write_finished(conf, r1_bio);
2046 else
NeilBrown4367af52011-07-28 11:31:49 +10002047 sync_request_write(mddev, r1_bio);
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002048 } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
NeilBrown62096bc2011-07-28 11:38:13 +10002049 test_bit(R1BIO_WriteError, &r1_bio->state))
2050 handle_write_finished(conf, r1_bio);
2051 else if (test_bit(R1BIO_ReadError, &r1_bio->state))
2052 handle_read_error(conf, r1_bio);
2053 else
NeilBrownd2eb35a2011-07-28 11:31:48 +10002054 /* just a partial read to be scheduled from separate
2055 * context
2056 */
2057 generic_make_request(r1_bio->bios[r1_bio->read_disk]);
NeilBrown62096bc2011-07-28 11:38:13 +10002058
NeilBrown1d9d5242009-10-16 15:55:32 +11002059 cond_resched();
NeilBrownde393cd2011-07-28 11:31:48 +10002060 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2061 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002063 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
2066
2067static int init_resync(conf_t *conf)
2068{
2069 int buffs;
2070
2071 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhenn9e77c482006-04-01 01:08:49 +02002072 BUG_ON(conf->r1buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
2074 conf->poolinfo);
2075 if (!conf->r1buf_pool)
2076 return -ENOMEM;
2077 conf->next_resync = 0;
2078 return 0;
2079}
2080
2081/*
2082 * perform a "sync" on one "block"
2083 *
2084 * We need to make sure that no normal I/O request - particularly write
2085 * requests - conflict with active sync requests.
2086 *
2087 * This is achieved by tracking pending requests and a 'barrier' concept
2088 * that can be installed to exclude normal IO requests.
2089 */
2090
NeilBrown57afd892005-06-21 17:17:13 -07002091static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
NeilBrown070ec552009-06-16 16:54:21 +10002093 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 r1bio_t *r1_bio;
2095 struct bio *bio;
2096 sector_t max_sector, nr_sectors;
NeilBrown3e198f72006-01-06 00:20:21 -08002097 int disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 int i;
NeilBrown3e198f72006-01-06 00:20:21 -08002099 int wonly = -1;
2100 int write_targets = 0, read_targets = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11002101 sector_t sync_blocks;
NeilBrowne3b97032005-08-04 12:53:34 -07002102 int still_degraded = 0;
NeilBrown06f60382011-07-28 11:31:48 +10002103 int good_sectors = RESYNC_SECTORS;
2104 int min_bad = 0; /* number of sectors that are bad in all devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106 if (!conf->r1buf_pool)
2107 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002108 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Andre Noll58c0fed2009-03-31 14:33:13 +11002110 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 if (sector_nr >= max_sector) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002112 /* If we aborted, we need to abort the
2113 * sync on the 'current' bitmap chunk (there will
2114 * only be one in raid1 resync.
2115 * We can find the current addess in mddev->curr_resync
2116 */
NeilBrown6a806c52005-07-15 03:56:35 -07002117 if (mddev->curr_resync < max_sector) /* aborted */
2118 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
NeilBrown191ea9b2005-06-21 17:17:23 -07002119 &sync_blocks, 1);
NeilBrown6a806c52005-07-15 03:56:35 -07002120 else /* completed sync */
NeilBrown191ea9b2005-06-21 17:17:23 -07002121 conf->fullsync = 0;
NeilBrown6a806c52005-07-15 03:56:35 -07002122
2123 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 close_sync(conf);
2125 return 0;
2126 }
2127
NeilBrown07d84d102006-06-26 00:27:56 -07002128 if (mddev->bitmap == NULL &&
2129 mddev->recovery_cp == MaxSector &&
NeilBrown6394cca2006-08-27 01:23:50 -07002130 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown07d84d102006-06-26 00:27:56 -07002131 conf->fullsync == 0) {
2132 *skipped = 1;
2133 return max_sector - sector_nr;
2134 }
NeilBrown6394cca2006-08-27 01:23:50 -07002135 /* before building a request, check if we can skip these blocks..
2136 * This call the bitmap_start_sync doesn't actually record anything
2137 */
NeilBrowne3b97032005-08-04 12:53:34 -07002138 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrowne5de485f2005-11-08 21:39:38 -08002139 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002140 /* We can skip this block, and probably several more */
2141 *skipped = 1;
2142 return sync_blocks;
2143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 /*
NeilBrown17999be2006-01-06 00:20:12 -08002145 * If there is non-resync activity waiting for a turn,
2146 * and resync is going fast enough,
2147 * then let it though before starting on this new sync request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 */
NeilBrown17999be2006-01-06 00:20:12 -08002149 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 msleep_interruptible(1000);
NeilBrown17999be2006-01-06 00:20:12 -08002151
NeilBrownb47490c2008-02-06 01:39:50 -08002152 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
NeilBrown1c4588e2010-10-26 17:41:22 +11002153 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
NeilBrown17999be2006-01-06 00:20:12 -08002154 raise_barrier(conf);
2155
2156 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
NeilBrown3e198f72006-01-06 00:20:21 -08002158 rcu_read_lock();
2159 /*
2160 * If we get a correctably read error during resync or recovery,
2161 * we might want to read from a different device. So we
2162 * flag all drives that could conceivably be read from for READ,
2163 * and any others (which will be non-In_sync devices) for WRITE.
2164 * If a read fails, we try reading from something else for which READ
2165 * is OK.
2166 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 r1_bio->mddev = mddev;
2169 r1_bio->sector = sector_nr;
NeilBrown191ea9b2005-06-21 17:17:23 -07002170 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 set_bit(R1BIO_IsSync, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 for (i=0; i < conf->raid_disks; i++) {
NeilBrown3e198f72006-01-06 00:20:21 -08002174 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 bio = r1_bio->bios[i];
2176
2177 /* take from bio_init */
2178 bio->bi_next = NULL;
NeilBrowndb8d9d32010-10-07 12:00:50 +11002179 bio->bi_flags &= ~(BIO_POOL_MASK-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 bio->bi_flags |= 1 << BIO_UPTODATE;
NeilBrowndb8d9d32010-10-07 12:00:50 +11002181 bio->bi_comp_cpu = -1;
NeilBrown802ba062006-12-13 00:34:13 -08002182 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 bio->bi_vcnt = 0;
2184 bio->bi_idx = 0;
2185 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 bio->bi_size = 0;
2187 bio->bi_end_io = NULL;
2188 bio->bi_private = NULL;
2189
NeilBrown3e198f72006-01-06 00:20:21 -08002190 rdev = rcu_dereference(conf->mirrors[i].rdev);
2191 if (rdev == NULL ||
NeilBrown06f60382011-07-28 11:31:48 +10002192 test_bit(Faulty, &rdev->flags)) {
NeilBrowne3b97032005-08-04 12:53:34 -07002193 still_degraded = 1;
NeilBrown3e198f72006-01-06 00:20:21 -08002194 } else if (!test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 bio->bi_rw = WRITE;
2196 bio->bi_end_io = end_sync_write;
2197 write_targets ++;
NeilBrown3e198f72006-01-06 00:20:21 -08002198 } else {
2199 /* may need to read from here */
NeilBrown06f60382011-07-28 11:31:48 +10002200 sector_t first_bad = MaxSector;
2201 int bad_sectors;
2202
2203 if (is_badblock(rdev, sector_nr, good_sectors,
2204 &first_bad, &bad_sectors)) {
2205 if (first_bad > sector_nr)
2206 good_sectors = first_bad - sector_nr;
2207 else {
2208 bad_sectors -= (sector_nr - first_bad);
2209 if (min_bad == 0 ||
2210 min_bad > bad_sectors)
2211 min_bad = bad_sectors;
2212 }
NeilBrown3e198f72006-01-06 00:20:21 -08002213 }
NeilBrown06f60382011-07-28 11:31:48 +10002214 if (sector_nr < first_bad) {
2215 if (test_bit(WriteMostly, &rdev->flags)) {
2216 if (wonly < 0)
2217 wonly = i;
2218 } else {
2219 if (disk < 0)
2220 disk = i;
2221 }
2222 bio->bi_rw = READ;
2223 bio->bi_end_io = end_sync_read;
2224 read_targets++;
2225 }
NeilBrown3e198f72006-01-06 00:20:21 -08002226 }
NeilBrown06f60382011-07-28 11:31:48 +10002227 if (bio->bi_end_io) {
2228 atomic_inc(&rdev->nr_pending);
2229 bio->bi_sector = sector_nr + rdev->data_offset;
2230 bio->bi_bdev = rdev->bdev;
2231 bio->bi_private = r1_bio;
2232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 }
NeilBrown3e198f72006-01-06 00:20:21 -08002234 rcu_read_unlock();
2235 if (disk < 0)
2236 disk = wonly;
2237 r1_bio->read_disk = disk;
NeilBrown191ea9b2005-06-21 17:17:23 -07002238
NeilBrown06f60382011-07-28 11:31:48 +10002239 if (read_targets == 0 && min_bad > 0) {
2240 /* These sectors are bad on all InSync devices, so we
2241 * need to mark them bad on all write targets
2242 */
2243 int ok = 1;
2244 for (i = 0 ; i < conf->raid_disks ; i++)
2245 if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
2246 mdk_rdev_t *rdev =
2247 rcu_dereference(conf->mirrors[i].rdev);
2248 ok = rdev_set_badblocks(rdev, sector_nr,
2249 min_bad, 0
2250 ) && ok;
2251 }
2252 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2253 *skipped = 1;
2254 put_buf(r1_bio);
2255
2256 if (!ok) {
2257 /* Cannot record the badblocks, so need to
2258 * abort the resync.
2259 * If there are multiple read targets, could just
2260 * fail the really bad ones ???
2261 */
2262 conf->recovery_disabled = mddev->recovery_disabled;
2263 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2264 return 0;
2265 } else
2266 return min_bad;
2267
2268 }
2269 if (min_bad > 0 && min_bad < good_sectors) {
2270 /* only resync enough to reach the next bad->good
2271 * transition */
2272 good_sectors = min_bad;
2273 }
2274
NeilBrown3e198f72006-01-06 00:20:21 -08002275 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
2276 /* extra read targets are also write targets */
2277 write_targets += read_targets-1;
2278
2279 if (write_targets == 0 || read_targets == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 /* There is nowhere to write, so all non-sync
2281 * drives must be failed - so we are finished
2282 */
NeilBrown57afd892005-06-21 17:17:13 -07002283 sector_t rv = max_sector - sector_nr;
2284 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 put_buf(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 return rv;
2287 }
2288
NeilBrownc6207272008-02-06 01:39:52 -08002289 if (max_sector > mddev->resync_max)
2290 max_sector = mddev->resync_max; /* Don't do IO beyond here */
NeilBrown06f60382011-07-28 11:31:48 +10002291 if (max_sector > sector_nr + good_sectors)
2292 max_sector = sector_nr + good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 nr_sectors = 0;
NeilBrown289e99e2005-06-21 17:17:24 -07002294 sync_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 do {
2296 struct page *page;
2297 int len = PAGE_SIZE;
2298 if (sector_nr + (len>>9) > max_sector)
2299 len = (max_sector - sector_nr) << 9;
2300 if (len == 0)
2301 break;
NeilBrown6a806c52005-07-15 03:56:35 -07002302 if (sync_blocks == 0) {
2303 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
NeilBrowne5de485f2005-11-08 21:39:38 -08002304 &sync_blocks, still_degraded) &&
2305 !conf->fullsync &&
2306 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
NeilBrown6a806c52005-07-15 03:56:35 -07002307 break;
Eric Sesterhenn9e77c482006-04-01 01:08:49 +02002308 BUG_ON(sync_blocks < (PAGE_SIZE>>9));
NeilBrown7571ae82010-10-07 11:54:46 +11002309 if ((len >> 9) > sync_blocks)
NeilBrown6a806c52005-07-15 03:56:35 -07002310 len = sync_blocks<<9;
NeilBrownab7a30c2005-06-21 17:17:23 -07002311 }
NeilBrown191ea9b2005-06-21 17:17:23 -07002312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 for (i=0 ; i < conf->raid_disks; i++) {
2314 bio = r1_bio->bios[i];
2315 if (bio->bi_end_io) {
NeilBrownd11c1712006-01-06 00:20:26 -08002316 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 if (bio_add_page(bio, page, len, 0) == 0) {
2318 /* stop here */
NeilBrownd11c1712006-01-06 00:20:26 -08002319 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 while (i > 0) {
2321 i--;
2322 bio = r1_bio->bios[i];
NeilBrown6a806c52005-07-15 03:56:35 -07002323 if (bio->bi_end_io==NULL)
2324 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 /* remove last page from this bio */
2326 bio->bi_vcnt--;
2327 bio->bi_size -= len;
2328 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
2329 }
2330 goto bio_full;
2331 }
2332 }
2333 }
2334 nr_sectors += len>>9;
2335 sector_nr += len>>9;
NeilBrown191ea9b2005-06-21 17:17:23 -07002336 sync_blocks -= (len>>9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
2338 bio_full:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 r1_bio->sectors = nr_sectors;
2340
NeilBrownd11c1712006-01-06 00:20:26 -08002341 /* For a user-requested sync, we read all readable devices and do a
2342 * compare
2343 */
2344 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2345 atomic_set(&r1_bio->remaining, read_targets);
2346 for (i=0; i<conf->raid_disks; i++) {
2347 bio = r1_bio->bios[i];
2348 if (bio->bi_end_io == end_sync_read) {
NeilBrownddac7c72006-08-31 21:27:36 -07002349 md_sync_acct(bio->bi_bdev, nr_sectors);
NeilBrownd11c1712006-01-06 00:20:26 -08002350 generic_make_request(bio);
2351 }
2352 }
2353 } else {
2354 atomic_set(&r1_bio->remaining, 1);
2355 bio = r1_bio->bios[r1_bio->read_disk];
NeilBrownddac7c72006-08-31 21:27:36 -07002356 md_sync_acct(bio->bi_bdev, nr_sectors);
NeilBrownd11c1712006-01-06 00:20:26 -08002357 generic_make_request(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
NeilBrownd11c1712006-01-06 00:20:26 -08002359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 return nr_sectors;
2361}
2362
Dan Williams80c3a6c2009-03-17 18:10:40 -07002363static sector_t raid1_size(mddev_t *mddev, sector_t sectors, int raid_disks)
2364{
2365 if (sectors)
2366 return sectors;
2367
2368 return mddev->dev_sectors;
2369}
2370
NeilBrown709ae482009-12-14 12:49:51 +11002371static conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372{
2373 conf_t *conf;
NeilBrown709ae482009-12-14 12:49:51 +11002374 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 mirror_info_t *disk;
2376 mdk_rdev_t *rdev;
NeilBrown709ae482009-12-14 12:49:51 +11002377 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
NeilBrown9ffae0c2006-01-06 00:20:32 -08002379 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 if (!conf)
NeilBrown709ae482009-12-14 12:49:51 +11002381 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
NeilBrown9ffae0c2006-01-06 00:20:32 -08002383 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 GFP_KERNEL);
2385 if (!conf->mirrors)
NeilBrown709ae482009-12-14 12:49:51 +11002386 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
NeilBrownddaf22a2006-01-06 00:20:19 -08002388 conf->tmppage = alloc_page(GFP_KERNEL);
2389 if (!conf->tmppage)
NeilBrown709ae482009-12-14 12:49:51 +11002390 goto abort;
NeilBrownddaf22a2006-01-06 00:20:19 -08002391
NeilBrown709ae482009-12-14 12:49:51 +11002392 conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 if (!conf->poolinfo)
NeilBrown709ae482009-12-14 12:49:51 +11002394 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 conf->poolinfo->raid_disks = mddev->raid_disks;
2396 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
2397 r1bio_pool_free,
2398 conf->poolinfo);
2399 if (!conf->r1bio_pool)
NeilBrown709ae482009-12-14 12:49:51 +11002400 goto abort;
2401
NeilBrowned9bfdf2009-10-16 15:55:44 +11002402 conf->poolinfo->mddev = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Neil Browne7e72bf2008-05-14 16:05:54 -07002404 spin_lock_init(&conf->device_lock);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002405 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown709ae482009-12-14 12:49:51 +11002406 int disk_idx = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 if (disk_idx >= mddev->raid_disks
2408 || disk_idx < 0)
2409 continue;
2410 disk = conf->mirrors + disk_idx;
2411
2412 disk->rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
2414 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 }
2416 conf->raid_disks = mddev->raid_disks;
2417 conf->mddev = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 INIT_LIST_HEAD(&conf->retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
2420 spin_lock_init(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -08002421 init_waitqueue_head(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
NeilBrown191ea9b2005-06-21 17:17:23 -07002423 bio_list_init(&conf->pending_bio_list);
NeilBrown191ea9b2005-06-21 17:17:23 -07002424
NeilBrown709ae482009-12-14 12:49:51 +11002425 conf->last_used = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 for (i = 0; i < conf->raid_disks; i++) {
2427
2428 disk = conf->mirrors + i;
2429
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002430 if (!disk->rdev ||
2431 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 disk->head_position = 0;
NeilBrown918f0232007-08-22 14:01:52 -07002433 if (disk->rdev)
2434 conf->fullsync = 1;
NeilBrown709ae482009-12-14 12:49:51 +11002435 } else if (conf->last_used < 0)
2436 /*
2437 * The first working device is used as a
2438 * starting point to read balancing.
2439 */
2440 conf->last_used = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 }
NeilBrown709ae482009-12-14 12:49:51 +11002442
2443 err = -EIO;
2444 if (conf->last_used < 0) {
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002445 printk(KERN_ERR "md/raid1:%s: no operational mirrors\n",
NeilBrown709ae482009-12-14 12:49:51 +11002446 mdname(mddev));
2447 goto abort;
NeilBrown11ce99e2006-10-03 01:15:52 -07002448 }
NeilBrown709ae482009-12-14 12:49:51 +11002449 err = -ENOMEM;
2450 conf->thread = md_register_thread(raid1d, mddev, NULL);
2451 if (!conf->thread) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002452 printk(KERN_ERR
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002453 "md/raid1:%s: couldn't allocate thread\n",
NeilBrown191ea9b2005-06-21 17:17:23 -07002454 mdname(mddev));
NeilBrown709ae482009-12-14 12:49:51 +11002455 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 }
NeilBrown191ea9b2005-06-21 17:17:23 -07002457
NeilBrown709ae482009-12-14 12:49:51 +11002458 return conf;
2459
2460 abort:
2461 if (conf) {
2462 if (conf->r1bio_pool)
2463 mempool_destroy(conf->r1bio_pool);
2464 kfree(conf->mirrors);
2465 safe_put_page(conf->tmppage);
2466 kfree(conf->poolinfo);
2467 kfree(conf);
2468 }
2469 return ERR_PTR(err);
2470}
2471
2472static int run(mddev_t *mddev)
2473{
2474 conf_t *conf;
2475 int i;
2476 mdk_rdev_t *rdev;
2477
2478 if (mddev->level != 1) {
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002479 printk(KERN_ERR "md/raid1:%s: raid level not set to mirroring (%d)\n",
NeilBrown709ae482009-12-14 12:49:51 +11002480 mdname(mddev), mddev->level);
2481 return -EIO;
2482 }
2483 if (mddev->reshape_position != MaxSector) {
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002484 printk(KERN_ERR "md/raid1:%s: reshape_position set but not supported\n",
NeilBrown709ae482009-12-14 12:49:51 +11002485 mdname(mddev));
2486 return -EIO;
2487 }
2488 /*
2489 * copy the already verified devices into our private RAID1
2490 * bookkeeping area. [whatever we allocate in run(),
2491 * should be freed in stop()]
2492 */
2493 if (mddev->private == NULL)
2494 conf = setup_conf(mddev);
2495 else
2496 conf = mddev->private;
2497
2498 if (IS_ERR(conf))
2499 return PTR_ERR(conf);
2500
NeilBrown709ae482009-12-14 12:49:51 +11002501 list_for_each_entry(rdev, &mddev->disks, same_set) {
Jonathan Brassow1ed72422011-06-07 17:50:35 -05002502 if (!mddev->gendisk)
2503 continue;
NeilBrown709ae482009-12-14 12:49:51 +11002504 disk_stack_limits(mddev->gendisk, rdev->bdev,
2505 rdev->data_offset << 9);
2506 /* as we don't honour merge_bvec_fn, we must never risk
NeilBrown627a2d32010-03-08 16:44:38 +11002507 * violating it, so limit ->max_segments to 1 lying within
2508 * a single page, as a one page request is never in violation.
NeilBrown709ae482009-12-14 12:49:51 +11002509 */
NeilBrown627a2d32010-03-08 16:44:38 +11002510 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2511 blk_queue_max_segments(mddev->queue, 1);
2512 blk_queue_segment_boundary(mddev->queue,
2513 PAGE_CACHE_SIZE - 1);
2514 }
NeilBrown709ae482009-12-14 12:49:51 +11002515 }
2516
2517 mddev->degraded = 0;
2518 for (i=0; i < conf->raid_disks; i++)
2519 if (conf->mirrors[i].rdev == NULL ||
2520 !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
2521 test_bit(Faulty, &conf->mirrors[i].rdev->flags))
2522 mddev->degraded++;
2523
2524 if (conf->raid_disks - mddev->degraded == 1)
2525 mddev->recovery_cp = MaxSector;
2526
Andre Noll8c6ac862009-06-18 08:48:06 +10002527 if (mddev->recovery_cp != MaxSector)
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002528 printk(KERN_NOTICE "md/raid1:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10002529 " -- starting background reconstruction\n",
2530 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 printk(KERN_INFO
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002532 "md/raid1:%s: active with %d out of %d mirrors\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 mdname(mddev), mddev->raid_disks - mddev->degraded,
2534 mddev->raid_disks);
NeilBrown709ae482009-12-14 12:49:51 +11002535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 /*
2537 * Ok, everything is just fine now
2538 */
NeilBrown709ae482009-12-14 12:49:51 +11002539 mddev->thread = conf->thread;
2540 conf->thread = NULL;
2541 mddev->private = conf;
2542
Dan Williams1f403622009-03-31 14:59:03 +11002543 md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Jonathan Brassow1ed72422011-06-07 17:50:35 -05002545 if (mddev->queue) {
2546 mddev->queue->backing_dev_info.congested_fn = raid1_congested;
2547 mddev->queue->backing_dev_info.congested_data = mddev;
2548 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01002549 return md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550}
2551
2552static int stop(mddev_t *mddev)
2553{
NeilBrown070ec552009-06-16 16:54:21 +10002554 conf_t *conf = mddev->private;
NeilBrown4b6d2872005-09-09 16:23:47 -07002555 struct bitmap *bitmap = mddev->bitmap;
NeilBrown4b6d2872005-09-09 16:23:47 -07002556
2557 /* wait for behind writes to complete */
NeilBrowne5551902010-03-31 11:21:44 +11002558 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002559 printk(KERN_INFO "md/raid1:%s: behind writes in progress - waiting to stop.\n",
2560 mdname(mddev));
NeilBrown4b6d2872005-09-09 16:23:47 -07002561 /* need to kick something here to make sure I/O goes? */
NeilBrowne5551902010-03-31 11:21:44 +11002562 wait_event(bitmap->behind_wait,
2563 atomic_read(&bitmap->behind_writes) == 0);
NeilBrown4b6d2872005-09-09 16:23:47 -07002564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
NeilBrown409c57f2009-03-31 14:39:39 +11002566 raise_barrier(conf);
2567 lower_barrier(conf);
2568
NeilBrown01f96c02011-09-21 15:30:20 +10002569 md_unregister_thread(&mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 if (conf->r1bio_pool)
2571 mempool_destroy(conf->r1bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002572 kfree(conf->mirrors);
2573 kfree(conf->poolinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 kfree(conf);
2575 mddev->private = NULL;
2576 return 0;
2577}
2578
2579static int raid1_resize(mddev_t *mddev, sector_t sectors)
2580{
2581 /* no resync is happening, and there is enough space
2582 * on all devices, so we can resize.
2583 * We need to make sure resync covers any new space.
2584 * If the array is shrinking we should possibly wait until
2585 * any io in the removed space completes, but it hardly seems
2586 * worth it.
2587 */
Dan Williams1f403622009-03-31 14:59:03 +11002588 md_set_array_sectors(mddev, raid1_size(mddev, sectors, 0));
Dan Williamsb522adc2009-03-31 15:00:31 +11002589 if (mddev->array_sectors > raid1_size(mddev, sectors, 0))
2590 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10002591 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10002592 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11002593 if (sectors > mddev->dev_sectors &&
NeilBrownb0986362011-05-11 15:52:21 +10002594 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11002595 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2597 }
Dan Williamsb522adc2009-03-31 15:00:31 +11002598 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07002599 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 return 0;
2601}
2602
NeilBrown63c70c42006-03-27 01:18:13 -08002603static int raid1_reshape(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
2605 /* We need to:
2606 * 1/ resize the r1bio_pool
2607 * 2/ resize conf->mirrors
2608 *
2609 * We allocate a new r1bio_pool if we can.
2610 * Then raise a device barrier and wait until all IO stops.
2611 * Then resize conf->mirrors and swap in the new r1bio pool.
NeilBrown6ea9c072005-06-21 17:17:09 -07002612 *
2613 * At the same time, we "pack" the devices so that all the missing
2614 * devices have the higher raid_disk numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 */
2616 mempool_t *newpool, *oldpool;
2617 struct pool_info *newpoolinfo;
2618 mirror_info_t *newmirrors;
NeilBrown070ec552009-06-16 16:54:21 +10002619 conf_t *conf = mddev->private;
NeilBrown63c70c42006-03-27 01:18:13 -08002620 int cnt, raid_disks;
NeilBrownc04be0a2006-10-03 01:15:53 -07002621 unsigned long flags;
Dan Williamsb5470dc2008-06-27 21:44:04 -07002622 int d, d2, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
NeilBrown63c70c42006-03-27 01:18:13 -08002624 /* Cannot change chunk_size, layout, or level */
Andre Noll664e7c42009-06-18 08:45:27 +10002625 if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
NeilBrown63c70c42006-03-27 01:18:13 -08002626 mddev->layout != mddev->new_layout ||
2627 mddev->level != mddev->new_level) {
Andre Noll664e7c42009-06-18 08:45:27 +10002628 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown63c70c42006-03-27 01:18:13 -08002629 mddev->new_layout = mddev->layout;
2630 mddev->new_level = mddev->level;
2631 return -EINVAL;
2632 }
2633
Dan Williamsb5470dc2008-06-27 21:44:04 -07002634 err = md_allow_write(mddev);
2635 if (err)
2636 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08002637
NeilBrown63c70c42006-03-27 01:18:13 -08002638 raid_disks = mddev->raid_disks + mddev->delta_disks;
2639
NeilBrown6ea9c072005-06-21 17:17:09 -07002640 if (raid_disks < conf->raid_disks) {
2641 cnt=0;
2642 for (d= 0; d < conf->raid_disks; d++)
2643 if (conf->mirrors[d].rdev)
2644 cnt++;
2645 if (cnt > raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 return -EBUSY;
NeilBrown6ea9c072005-06-21 17:17:09 -07002647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
2649 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
2650 if (!newpoolinfo)
2651 return -ENOMEM;
2652 newpoolinfo->mddev = mddev;
2653 newpoolinfo->raid_disks = raid_disks;
2654
2655 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
2656 r1bio_pool_free, newpoolinfo);
2657 if (!newpool) {
2658 kfree(newpoolinfo);
2659 return -ENOMEM;
2660 }
NeilBrown9ffae0c2006-01-06 00:20:32 -08002661 newmirrors = kzalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 if (!newmirrors) {
2663 kfree(newpoolinfo);
2664 mempool_destroy(newpool);
2665 return -ENOMEM;
2666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
NeilBrown17999be2006-01-06 00:20:12 -08002668 raise_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
2670 /* ok, everything is stopped */
2671 oldpool = conf->r1bio_pool;
2672 conf->r1bio_pool = newpool;
NeilBrown6ea9c072005-06-21 17:17:09 -07002673
NeilBrowna88aa782007-08-22 14:01:53 -07002674 for (d = d2 = 0; d < conf->raid_disks; d++) {
2675 mdk_rdev_t *rdev = conf->mirrors[d].rdev;
2676 if (rdev && rdev->raid_disk != d2) {
Namhyung Kim36fad852011-07-27 11:00:36 +10002677 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna88aa782007-08-22 14:01:53 -07002678 rdev->raid_disk = d2;
Namhyung Kim36fad852011-07-27 11:00:36 +10002679 sysfs_unlink_rdev(mddev, rdev);
2680 if (sysfs_link_rdev(mddev, rdev))
NeilBrowna88aa782007-08-22 14:01:53 -07002681 printk(KERN_WARNING
Namhyung Kim36fad852011-07-27 11:00:36 +10002682 "md/raid1:%s: cannot register rd%d\n",
2683 mdname(mddev), rdev->raid_disk);
NeilBrown6ea9c072005-06-21 17:17:09 -07002684 }
NeilBrowna88aa782007-08-22 14:01:53 -07002685 if (rdev)
2686 newmirrors[d2++].rdev = rdev;
2687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 kfree(conf->mirrors);
2689 conf->mirrors = newmirrors;
2690 kfree(conf->poolinfo);
2691 conf->poolinfo = newpoolinfo;
2692
NeilBrownc04be0a2006-10-03 01:15:53 -07002693 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 mddev->degraded += (raid_disks - conf->raid_disks);
NeilBrownc04be0a2006-10-03 01:15:53 -07002695 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 conf->raid_disks = mddev->raid_disks = raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08002697 mddev->delta_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
NeilBrown6ea9c072005-06-21 17:17:09 -07002699 conf->last_used = 0; /* just make sure it is in-range */
NeilBrown17999be2006-01-06 00:20:12 -08002700 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
2702 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2703 md_wakeup_thread(mddev->thread);
2704
2705 mempool_destroy(oldpool);
2706 return 0;
2707}
2708
NeilBrown500af872005-09-09 16:23:58 -07002709static void raid1_quiesce(mddev_t *mddev, int state)
NeilBrown36fa3062005-09-09 16:23:45 -07002710{
NeilBrown070ec552009-06-16 16:54:21 +10002711 conf_t *conf = mddev->private;
NeilBrown36fa3062005-09-09 16:23:45 -07002712
2713 switch(state) {
NeilBrown6eef4b22009-12-14 12:49:51 +11002714 case 2: /* wake for suspend */
2715 wake_up(&conf->wait_barrier);
2716 break;
NeilBrown9e6603d2005-09-09 16:23:48 -07002717 case 1:
NeilBrown17999be2006-01-06 00:20:12 -08002718 raise_barrier(conf);
NeilBrown36fa3062005-09-09 16:23:45 -07002719 break;
NeilBrown9e6603d2005-09-09 16:23:48 -07002720 case 0:
NeilBrown17999be2006-01-06 00:20:12 -08002721 lower_barrier(conf);
NeilBrown36fa3062005-09-09 16:23:45 -07002722 break;
2723 }
NeilBrown36fa3062005-09-09 16:23:45 -07002724}
2725
NeilBrown709ae482009-12-14 12:49:51 +11002726static void *raid1_takeover(mddev_t *mddev)
2727{
2728 /* raid1 can take over:
2729 * raid5 with 2 devices, any layout or chunk size
2730 */
2731 if (mddev->level == 5 && mddev->raid_disks == 2) {
2732 conf_t *conf;
2733 mddev->new_level = 1;
2734 mddev->new_layout = 0;
2735 mddev->new_chunk_sectors = 0;
2736 conf = setup_conf(mddev);
2737 if (!IS_ERR(conf))
2738 conf->barrier = 1;
2739 return conf;
2740 }
2741 return ERR_PTR(-EINVAL);
2742}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
NeilBrown2604b702006-01-06 00:20:36 -08002744static struct mdk_personality raid1_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745{
2746 .name = "raid1",
NeilBrown2604b702006-01-06 00:20:36 -08002747 .level = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 .owner = THIS_MODULE,
2749 .make_request = make_request,
2750 .run = run,
2751 .stop = stop,
2752 .status = status,
2753 .error_handler = error,
2754 .hot_add_disk = raid1_add_disk,
2755 .hot_remove_disk= raid1_remove_disk,
2756 .spare_active = raid1_spare_active,
2757 .sync_request = sync_request,
2758 .resize = raid1_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07002759 .size = raid1_size,
NeilBrown63c70c42006-03-27 01:18:13 -08002760 .check_reshape = raid1_reshape,
NeilBrown36fa3062005-09-09 16:23:45 -07002761 .quiesce = raid1_quiesce,
NeilBrown709ae482009-12-14 12:49:51 +11002762 .takeover = raid1_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763};
2764
2765static int __init raid_init(void)
2766{
NeilBrown2604b702006-01-06 00:20:36 -08002767 return register_md_personality(&raid1_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768}
2769
2770static void raid_exit(void)
2771{
NeilBrown2604b702006-01-06 00:20:36 -08002772 unregister_md_personality(&raid1_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773}
2774
2775module_init(raid_init);
2776module_exit(raid_exit);
2777MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11002778MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779MODULE_ALIAS("md-personality-3"); /* RAID1 */
NeilBrownd9d166c2006-01-06 00:20:51 -08002780MODULE_ALIAS("md-raid1");
NeilBrown2604b702006-01-06 00:20:36 -08002781MODULE_ALIAS("md-level-1");