blob: 8c31c39b6f8c91de111d9fea4cc148026d471185 [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;
NeilBrowncf30a472006-01-06 00:20:23 -0800166 if (*bio && *bio != IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 bio_put(*bio);
168 *bio = NULL;
169 }
170}
171
Arjan van de Ven858119e2006-01-14 13:20:43 -0800172static void free_r1bio(r1bio_t *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
NeilBrown070ec552009-06-16 16:54:21 +1000174 conf_t *conf = r1_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 put_all_bios(conf, r1_bio);
177 mempool_free(r1_bio, conf->r1bio_pool);
178}
179
Arjan van de Ven858119e2006-01-14 13:20:43 -0800180static void put_buf(r1bio_t *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
NeilBrown070ec552009-06-16 16:54:21 +1000182 conf_t *conf = r1_bio->mddev->private;
NeilBrown3e198f72006-01-06 00:20:21 -0800183 int i;
184
185 for (i=0; i<conf->raid_disks; i++) {
186 struct bio *bio = r1_bio->bios[i];
187 if (bio->bi_end_io)
188 rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev);
189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 mempool_free(r1_bio, conf->r1buf_pool);
192
NeilBrown17999be2006-01-06 00:20:12 -0800193 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196static void reschedule_retry(r1bio_t *r1_bio)
197{
198 unsigned long flags;
199 mddev_t *mddev = r1_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +1000200 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 spin_lock_irqsave(&conf->device_lock, flags);
203 list_add(&r1_bio->retry_list, &conf->retry_list);
NeilBrownddaf22a2006-01-06 00:20:19 -0800204 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 spin_unlock_irqrestore(&conf->device_lock, flags);
206
NeilBrown17999be2006-01-06 00:20:12 -0800207 wake_up(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 md_wakeup_thread(mddev->thread);
209}
210
211/*
212 * raid_end_bio_io() is called when we have finished servicing a mirrored
213 * operation and are ready to return a success/failure code to the buffer
214 * cache layer.
215 */
NeilBrownd2eb35a2011-07-28 11:31:48 +1000216static void call_bio_endio(r1bio_t *r1_bio)
217{
218 struct bio *bio = r1_bio->master_bio;
219 int done;
220 conf_t *conf = r1_bio->mddev->private;
221
222 if (bio->bi_phys_segments) {
223 unsigned long flags;
224 spin_lock_irqsave(&conf->device_lock, flags);
225 bio->bi_phys_segments--;
226 done = (bio->bi_phys_segments == 0);
227 spin_unlock_irqrestore(&conf->device_lock, flags);
228 } else
229 done = 1;
230
231 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
232 clear_bit(BIO_UPTODATE, &bio->bi_flags);
233 if (done) {
234 bio_endio(bio, 0);
235 /*
236 * Wake up any possible resync thread that waits for the device
237 * to go idle.
238 */
239 allow_barrier(conf);
240 }
241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243static void raid_end_bio_io(r1bio_t *r1_bio)
244{
245 struct bio *bio = r1_bio->master_bio;
246
NeilBrown4b6d2872005-09-09 16:23:47 -0700247 /* if nobody has done the final endio yet, do it now */
248 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
249 PRINTK(KERN_DEBUG "raid1: sync end %s on sectors %llu-%llu\n",
250 (bio_data_dir(bio) == WRITE) ? "write" : "read",
251 (unsigned long long) bio->bi_sector,
252 (unsigned long long) bio->bi_sector +
253 (bio->bi_size >> 9) - 1);
254
NeilBrownd2eb35a2011-07-28 11:31:48 +1000255 call_bio_endio(r1_bio);
NeilBrown4b6d2872005-09-09 16:23:47 -0700256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 free_r1bio(r1_bio);
258}
259
260/*
261 * Update disk head position estimator based on IRQ completion info.
262 */
263static inline void update_head_pos(int disk, r1bio_t *r1_bio)
264{
NeilBrown070ec552009-06-16 16:54:21 +1000265 conf_t *conf = r1_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 conf->mirrors[disk].head_position =
268 r1_bio->sector + (r1_bio->sectors);
269}
270
NeilBrown6712ecf2007-09-27 12:47:43 +0200271static void raid1_end_read_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100274 r1bio_t *r1_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 int mirror;
NeilBrown070ec552009-06-16 16:54:21 +1000276 conf_t *conf = r1_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 mirror = r1_bio->read_disk;
279 /*
280 * this branch is our 'one mirror IO has finished' event handler:
281 */
NeilBrownddaf22a2006-01-06 00:20:19 -0800282 update_head_pos(mirror, r1_bio);
283
NeilBrowndd00a992007-05-10 03:15:50 -0700284 if (uptodate)
285 set_bit(R1BIO_Uptodate, &r1_bio->state);
286 else {
287 /* If all other devices have failed, we want to return
288 * the error upwards rather than fail the last device.
289 * Here we redefine "uptodate" to mean "Don't want to retry"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 */
NeilBrowndd00a992007-05-10 03:15:50 -0700291 unsigned long flags;
292 spin_lock_irqsave(&conf->device_lock, flags);
293 if (r1_bio->mddev->degraded == conf->raid_disks ||
294 (r1_bio->mddev->degraded == conf->raid_disks-1 &&
295 !test_bit(Faulty, &conf->mirrors[mirror].rdev->flags)))
296 uptodate = 1;
297 spin_unlock_irqrestore(&conf->device_lock, flags);
298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
NeilBrowndd00a992007-05-10 03:15:50 -0700300 if (uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 raid_end_bio_io(r1_bio);
NeilBrowndd00a992007-05-10 03:15:50 -0700302 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 /*
304 * oops, read error:
305 */
306 char b[BDEVNAME_SIZE];
Christian Dietrich8bda4702011-07-27 11:00:36 +1000307 printk_ratelimited(
308 KERN_ERR "md/raid1:%s: %s: "
309 "rescheduling sector %llu\n",
310 mdname(conf->mddev),
311 bdevname(conf->mirrors[mirror].rdev->bdev,
312 b),
313 (unsigned long long)r1_bio->sector);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000314 set_bit(R1BIO_ReadError, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 reschedule_retry(r1_bio);
316 }
317
318 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
NeilBrownaf6d7b72011-05-11 14:51:19 +1000321static void r1_bio_write_done(r1bio_t *r1_bio)
NeilBrown4e780642010-10-19 12:54:01 +1100322{
323 if (atomic_dec_and_test(&r1_bio->remaining))
324 {
325 /* it really is the end of this request */
326 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
327 /* free extra copy of the data pages */
NeilBrownaf6d7b72011-05-11 14:51:19 +1000328 int i = r1_bio->behind_page_count;
NeilBrown4e780642010-10-19 12:54:01 +1100329 while (i--)
NeilBrownaf6d7b72011-05-11 14:51:19 +1000330 safe_put_page(r1_bio->behind_pages[i]);
331 kfree(r1_bio->behind_pages);
332 r1_bio->behind_pages = NULL;
NeilBrown4e780642010-10-19 12:54:01 +1100333 }
334 /* clear the bitmap if all writes complete successfully */
335 bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
336 r1_bio->sectors,
337 !test_bit(R1BIO_Degraded, &r1_bio->state),
NeilBrownaf6d7b72011-05-11 14:51:19 +1000338 test_bit(R1BIO_BehindIO, &r1_bio->state));
NeilBrown4e780642010-10-19 12:54:01 +1100339 md_write_end(r1_bio->mddev);
340 raid_end_bio_io(r1_bio);
341 }
342}
343
NeilBrown6712ecf2007-09-27 12:47:43 +0200344static void raid1_end_write_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100347 r1bio_t *r1_bio = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800348 int mirror, behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
NeilBrown070ec552009-06-16 16:54:21 +1000349 conf_t *conf = r1_bio->mddev->private;
NeilBrown04b857f2006-03-09 17:33:46 -0800350 struct bio *to_put = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 for (mirror = 0; mirror < conf->raid_disks; mirror++)
354 if (r1_bio->bios[mirror] == bio)
355 break;
356
Tejun Heoe9c74692010-09-03 11:56:18 +0200357 /*
358 * 'one mirror IO has finished' event handler:
359 */
360 r1_bio->bios[mirror] = NULL;
361 to_put = bio;
362 if (!uptodate) {
363 md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
364 /* an I/O failed, we can't clear the bitmap */
365 set_bit(R1BIO_Degraded, &r1_bio->state);
366 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 /*
Tejun Heoe9c74692010-09-03 11:56:18 +0200368 * Set R1BIO_Uptodate in our master bio, so that we
369 * will return a good error code for to the higher
370 * levels even if IO on some other mirrored buffer
371 * fails.
372 *
373 * The 'master' represents the composite IO operation
374 * to user-side. So if something waits for IO, then it
375 * will wait for the 'master' bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 */
Tejun Heoe9c74692010-09-03 11:56:18 +0200377 set_bit(R1BIO_Uptodate, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Tejun Heoe9c74692010-09-03 11:56:18 +0200379 update_head_pos(mirror, r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Tejun Heoe9c74692010-09-03 11:56:18 +0200381 if (behind) {
382 if (test_bit(WriteMostly, &conf->mirrors[mirror].rdev->flags))
383 atomic_dec(&r1_bio->behind_remaining);
NeilBrown4b6d2872005-09-09 16:23:47 -0700384
Tejun Heoe9c74692010-09-03 11:56:18 +0200385 /*
386 * In behind mode, we ACK the master bio once the I/O
387 * has safely reached all non-writemostly
388 * disks. Setting the Returned bit ensures that this
389 * gets done only once -- we don't ever want to return
390 * -EIO here, instead we'll wait
391 */
392 if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
393 test_bit(R1BIO_Uptodate, &r1_bio->state)) {
394 /* Maybe we can return now */
395 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
396 struct bio *mbio = r1_bio->master_bio;
397 PRINTK(KERN_DEBUG "raid1: behind end write sectors %llu-%llu\n",
398 (unsigned long long) mbio->bi_sector,
399 (unsigned long long) mbio->bi_sector +
400 (mbio->bi_size >> 9) - 1);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000401 call_bio_endio(r1_bio);
NeilBrown4b6d2872005-09-09 16:23:47 -0700402 }
403 }
404 }
Tejun Heoe9c74692010-09-03 11:56:18 +0200405 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 * Let's see if all mirrored write operations have finished
409 * already.
410 */
NeilBrownaf6d7b72011-05-11 14:51:19 +1000411 r1_bio_write_done(r1_bio);
NeilBrownc70810b2006-06-26 00:27:35 -0700412
NeilBrown04b857f2006-03-09 17:33:46 -0800413 if (to_put)
414 bio_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
417
418/*
419 * This routine returns the disk from which the requested read should
420 * be done. There is a per-array 'next expected sequential IO' sector
421 * number - if this matches on the next IO then we use the last disk.
422 * There is also a per-disk 'last know head position' sector that is
423 * maintained from IRQ contexts, both the normal and the resync IO
424 * completion handlers update this position correctly. If there is no
425 * perfect sequential match then we pick the disk whose head is closest.
426 *
427 * If there are 2 mirrors in the same 2 devices, performance degrades
428 * because position is mirror, not device based.
429 *
430 * The rdev for the device selected will have nr_pending incremented.
431 */
NeilBrownd2eb35a2011-07-28 11:31:48 +1000432static int read_balance(conf_t *conf, r1bio_t *r1_bio, int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000434 const sector_t this_sector = r1_bio->sector;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000435 int sectors;
436 int best_good_sectors;
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000437 int start_disk;
NeilBrown76073052011-05-11 14:34:56 +1000438 int best_disk;
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000439 int i;
NeilBrown76073052011-05-11 14:34:56 +1000440 sector_t best_dist;
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700441 mdk_rdev_t *rdev;
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000442 int choose_first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 rcu_read_lock();
445 /*
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700446 * Check if we can balance. We can balance on the whole
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 * device if no resync is going on, or below the resync window.
448 * We take the first readable disk when above the resync window.
449 */
450 retry:
NeilBrownd2eb35a2011-07-28 11:31:48 +1000451 sectors = r1_bio->sectors;
NeilBrown76073052011-05-11 14:34:56 +1000452 best_disk = -1;
453 best_dist = MaxSector;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000454 best_good_sectors = 0;
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (conf->mddev->recovery_cp < MaxSector &&
457 (this_sector + sectors >= conf->next_resync)) {
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000458 choose_first = 1;
459 start_disk = 0;
460 } else {
461 choose_first = 0;
462 start_disk = conf->last_used;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000465 for (i = 0 ; i < conf->raid_disks ; i++) {
NeilBrown76073052011-05-11 14:34:56 +1000466 sector_t dist;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000467 sector_t first_bad;
468 int bad_sectors;
469
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000470 int disk = start_disk + i;
471 if (disk >= conf->raid_disks)
472 disk -= conf->raid_disks;
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700473
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000474 rdev = rcu_dereference(conf->mirrors[disk].rdev);
475 if (r1_bio->bios[disk] == IO_BLOCKED
476 || rdev == NULL
NeilBrown76073052011-05-11 14:34:56 +1000477 || test_bit(Faulty, &rdev->flags))
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000478 continue;
NeilBrown76073052011-05-11 14:34:56 +1000479 if (!test_bit(In_sync, &rdev->flags) &&
480 rdev->recovery_offset < this_sector + sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 continue;
NeilBrown76073052011-05-11 14:34:56 +1000482 if (test_bit(WriteMostly, &rdev->flags)) {
483 /* Don't balance among write-mostly, just
484 * use the first as a last resort */
485 if (best_disk < 0)
486 best_disk = disk;
487 continue;
488 }
489 /* This is a reasonable device to use. It might
490 * even be best.
491 */
NeilBrownd2eb35a2011-07-28 11:31:48 +1000492 if (is_badblock(rdev, this_sector, sectors,
493 &first_bad, &bad_sectors)) {
494 if (best_dist < MaxSector)
495 /* already have a better device */
496 continue;
497 if (first_bad <= this_sector) {
498 /* cannot read here. If this is the 'primary'
499 * device, then we must not read beyond
500 * bad_sectors from another device..
501 */
502 bad_sectors -= (this_sector - first_bad);
503 if (choose_first && sectors > bad_sectors)
504 sectors = bad_sectors;
505 if (best_good_sectors > sectors)
506 best_good_sectors = sectors;
507
508 } else {
509 sector_t good_sectors = first_bad - this_sector;
510 if (good_sectors > best_good_sectors) {
511 best_good_sectors = good_sectors;
512 best_disk = disk;
513 }
514 if (choose_first)
515 break;
516 }
517 continue;
518 } else
519 best_good_sectors = sectors;
520
NeilBrown76073052011-05-11 14:34:56 +1000521 dist = abs(this_sector - conf->mirrors[disk].head_position);
522 if (choose_first
523 /* Don't change to another disk for sequential reads */
524 || conf->next_seq_sect == this_sector
525 || dist == 0
526 /* If device is idle, use it */
527 || atomic_read(&rdev->nr_pending) == 0) {
528 best_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 break;
530 }
NeilBrown76073052011-05-11 14:34:56 +1000531 if (dist < best_dist) {
532 best_dist = dist;
533 best_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
NeilBrown76073052011-05-11 14:34:56 +1000537 if (best_disk >= 0) {
538 rdev = rcu_dereference(conf->mirrors[best_disk].rdev);
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700539 if (!rdev)
540 goto retry;
541 atomic_inc(&rdev->nr_pending);
NeilBrown76073052011-05-11 14:34:56 +1000542 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /* cannot risk returning a device that failed
544 * before we inc'ed nr_pending
545 */
NeilBrown03c902e2006-01-06 00:20:46 -0800546 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 goto retry;
548 }
NeilBrownd2eb35a2011-07-28 11:31:48 +1000549 sectors = best_good_sectors;
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700550 conf->next_seq_sect = this_sector + sectors;
NeilBrown76073052011-05-11 14:34:56 +1000551 conf->last_used = best_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553 rcu_read_unlock();
NeilBrownd2eb35a2011-07-28 11:31:48 +1000554 *max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
NeilBrown76073052011-05-11 14:34:56 +1000556 return best_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Jonathan Brassow1ed72422011-06-07 17:50:35 -0500559int md_raid1_congested(mddev_t *mddev, int bits)
NeilBrown0d129222006-10-03 01:15:54 -0700560{
NeilBrown070ec552009-06-16 16:54:21 +1000561 conf_t *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700562 int i, ret = 0;
563
564 rcu_read_lock();
565 for (i = 0; i < mddev->raid_disks; i++) {
566 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
567 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200568 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700569
Jonathan Brassow1ed72422011-06-07 17:50:35 -0500570 BUG_ON(!q);
571
NeilBrown0d129222006-10-03 01:15:54 -0700572 /* Note the '|| 1' - when read_balance prefers
573 * non-congested targets, it can be removed
574 */
Alexander Beregalov91a9e992009-04-07 01:35:56 +0400575 if ((bits & (1<<BDI_async_congested)) || 1)
NeilBrown0d129222006-10-03 01:15:54 -0700576 ret |= bdi_congested(&q->backing_dev_info, bits);
577 else
578 ret &= bdi_congested(&q->backing_dev_info, bits);
579 }
580 }
581 rcu_read_unlock();
582 return ret;
583}
Jonathan Brassow1ed72422011-06-07 17:50:35 -0500584EXPORT_SYMBOL_GPL(md_raid1_congested);
NeilBrown0d129222006-10-03 01:15:54 -0700585
Jonathan Brassow1ed72422011-06-07 17:50:35 -0500586static int raid1_congested(void *data, int bits)
587{
588 mddev_t *mddev = data;
589
590 return mddev_congested(mddev, bits) ||
591 md_raid1_congested(mddev, bits);
592}
NeilBrown0d129222006-10-03 01:15:54 -0700593
Jens Axboe7eaceac2011-03-10 08:52:07 +0100594static void flush_pending_writes(conf_t *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800595{
596 /* Any writes that have been queued but are awaiting
597 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800598 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800599 spin_lock_irq(&conf->device_lock);
600
601 if (conf->pending_bio_list.head) {
602 struct bio *bio;
603 bio = bio_list_get(&conf->pending_bio_list);
NeilBrowna35e63e2008-03-04 14:29:29 -0800604 spin_unlock_irq(&conf->device_lock);
605 /* flush any pending bitmap writes to
606 * disk before proceeding w/ I/O */
607 bitmap_unplug(conf->mddev->bitmap);
608
609 while (bio) { /* submit pending writes */
610 struct bio *next = bio->bi_next;
611 bio->bi_next = NULL;
612 generic_make_request(bio);
613 bio = next;
614 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800615 } else
616 spin_unlock_irq(&conf->device_lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100617}
618
NeilBrown17999be2006-01-06 00:20:12 -0800619/* Barriers....
620 * Sometimes we need to suspend IO while we do something else,
621 * either some resync/recovery, or reconfigure the array.
622 * To do this we raise a 'barrier'.
623 * The 'barrier' is a counter that can be raised multiple times
624 * to count how many activities are happening which preclude
625 * normal IO.
626 * We can only raise the barrier if there is no pending IO.
627 * i.e. if nr_pending == 0.
628 * We choose only to raise the barrier if no-one is waiting for the
629 * barrier to go down. This means that as soon as an IO request
630 * is ready, no other operations which require a barrier will start
631 * until the IO request has had a chance.
632 *
633 * So: regular IO calls 'wait_barrier'. When that returns there
634 * is no backgroup IO happening, It must arrange to call
635 * allow_barrier when it has finished its IO.
636 * backgroup IO calls must call raise_barrier. Once that returns
637 * there is no normal IO happeing. It must arrange to call
638 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 */
640#define RESYNC_DEPTH 32
641
NeilBrown17999be2006-01-06 00:20:12 -0800642static void raise_barrier(conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 spin_lock_irq(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800645
646 /* Wait until no block IO is waiting */
647 wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
NeilBrownc3b328a2011-04-18 18:25:43 +1000648 conf->resync_lock, );
NeilBrown17999be2006-01-06 00:20:12 -0800649
650 /* block any new IO from starting */
651 conf->barrier++;
652
NeilBrown046abee2010-10-26 15:46:20 +1100653 /* Now wait for all pending IO to complete */
NeilBrown17999be2006-01-06 00:20:12 -0800654 wait_event_lock_irq(conf->wait_barrier,
655 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
NeilBrownc3b328a2011-04-18 18:25:43 +1000656 conf->resync_lock, );
NeilBrown17999be2006-01-06 00:20:12 -0800657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 spin_unlock_irq(&conf->resync_lock);
659}
660
NeilBrown17999be2006-01-06 00:20:12 -0800661static void lower_barrier(conf_t *conf)
662{
663 unsigned long flags;
NeilBrown709ae482009-12-14 12:49:51 +1100664 BUG_ON(conf->barrier <= 0);
NeilBrown17999be2006-01-06 00:20:12 -0800665 spin_lock_irqsave(&conf->resync_lock, flags);
666 conf->barrier--;
667 spin_unlock_irqrestore(&conf->resync_lock, flags);
668 wake_up(&conf->wait_barrier);
669}
670
671static void wait_barrier(conf_t *conf)
672{
673 spin_lock_irq(&conf->resync_lock);
674 if (conf->barrier) {
675 conf->nr_waiting++;
676 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
677 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000678 );
NeilBrown17999be2006-01-06 00:20:12 -0800679 conf->nr_waiting--;
680 }
681 conf->nr_pending++;
682 spin_unlock_irq(&conf->resync_lock);
683}
684
685static void allow_barrier(conf_t *conf)
686{
687 unsigned long flags;
688 spin_lock_irqsave(&conf->resync_lock, flags);
689 conf->nr_pending--;
690 spin_unlock_irqrestore(&conf->resync_lock, flags);
691 wake_up(&conf->wait_barrier);
692}
693
NeilBrownddaf22a2006-01-06 00:20:19 -0800694static void freeze_array(conf_t *conf)
695{
696 /* stop syncio and normal IO and wait for everything to
697 * go quite.
698 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800699 * wait until nr_pending match nr_queued+1
700 * This is called in the context of one normal IO request
701 * that has failed. Thus any sync request that might be pending
702 * will be blocked by nr_pending, and we need to wait for
703 * pending IO requests to complete or be queued for re-try.
704 * Thus the number queued (nr_queued) plus this request (1)
705 * must match the number of pending IOs (nr_pending) before
706 * we continue.
NeilBrownddaf22a2006-01-06 00:20:19 -0800707 */
708 spin_lock_irq(&conf->resync_lock);
709 conf->barrier++;
710 conf->nr_waiting++;
711 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -0800712 conf->nr_pending == conf->nr_queued+1,
NeilBrownddaf22a2006-01-06 00:20:19 -0800713 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000714 flush_pending_writes(conf));
NeilBrownddaf22a2006-01-06 00:20:19 -0800715 spin_unlock_irq(&conf->resync_lock);
716}
717static void unfreeze_array(conf_t *conf)
718{
719 /* reverse the effect of the freeze */
720 spin_lock_irq(&conf->resync_lock);
721 conf->barrier--;
722 conf->nr_waiting--;
723 wake_up(&conf->wait_barrier);
724 spin_unlock_irq(&conf->resync_lock);
725}
726
NeilBrown17999be2006-01-06 00:20:12 -0800727
NeilBrown4e780642010-10-19 12:54:01 +1100728/* duplicate the data pages for behind I/O
NeilBrown4e780642010-10-19 12:54:01 +1100729 */
NeilBrownaf6d7b72011-05-11 14:51:19 +1000730static void alloc_behind_pages(struct bio *bio, r1bio_t *r1_bio)
NeilBrown4b6d2872005-09-09 16:23:47 -0700731{
732 int i;
733 struct bio_vec *bvec;
NeilBrownaf6d7b72011-05-11 14:51:19 +1000734 struct page **pages = kzalloc(bio->bi_vcnt * sizeof(struct page*),
NeilBrown4b6d2872005-09-09 16:23:47 -0700735 GFP_NOIO);
736 if (unlikely(!pages))
NeilBrownaf6d7b72011-05-11 14:51:19 +1000737 return;
NeilBrown4b6d2872005-09-09 16:23:47 -0700738
NeilBrown4b6d2872005-09-09 16:23:47 -0700739 bio_for_each_segment(bvec, bio, i) {
NeilBrownaf6d7b72011-05-11 14:51:19 +1000740 pages[i] = alloc_page(GFP_NOIO);
741 if (unlikely(!pages[i]))
NeilBrown4b6d2872005-09-09 16:23:47 -0700742 goto do_sync_io;
NeilBrownaf6d7b72011-05-11 14:51:19 +1000743 memcpy(kmap(pages[i]) + bvec->bv_offset,
NeilBrown4b6d2872005-09-09 16:23:47 -0700744 kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
NeilBrownaf6d7b72011-05-11 14:51:19 +1000745 kunmap(pages[i]);
NeilBrown4b6d2872005-09-09 16:23:47 -0700746 kunmap(bvec->bv_page);
747 }
NeilBrownaf6d7b72011-05-11 14:51:19 +1000748 r1_bio->behind_pages = pages;
749 r1_bio->behind_page_count = bio->bi_vcnt;
750 set_bit(R1BIO_BehindIO, &r1_bio->state);
751 return;
NeilBrown4b6d2872005-09-09 16:23:47 -0700752
753do_sync_io:
NeilBrownaf6d7b72011-05-11 14:51:19 +1000754 for (i = 0; i < bio->bi_vcnt; i++)
755 if (pages[i])
756 put_page(pages[i]);
NeilBrown4b6d2872005-09-09 16:23:47 -0700757 kfree(pages);
758 PRINTK("%dB behind alloc failed, doing sync I/O\n", bio->bi_size);
NeilBrown4b6d2872005-09-09 16:23:47 -0700759}
760
NeilBrown21a52c62010-04-01 15:02:13 +1100761static int make_request(mddev_t *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
NeilBrown070ec552009-06-16 16:54:21 +1000763 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 mirror_info_t *mirror;
765 r1bio_t *r1_bio;
766 struct bio *read_bio;
NeilBrown191ea9b2005-06-21 17:17:23 -0700767 int i, targets = 0, disks;
NeilBrown84255d12008-05-23 13:04:32 -0700768 struct bitmap *bitmap;
NeilBrown191ea9b2005-06-21 17:17:23 -0700769 unsigned long flags;
Jens Axboea3623572005-11-01 09:26:16 +0100770 const int rw = bio_data_dir(bio);
NeilBrown2c7d46e2010-08-18 16:16:05 +1000771 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
Tejun Heoe9c74692010-09-03 11:56:18 +0200772 const unsigned long do_flush_fua = (bio->bi_rw & (REQ_FLUSH | REQ_FUA));
Dan Williams6bfe0b42008-04-30 00:52:32 -0700773 mdk_rdev_t *blocked_rdev;
NeilBrownc3b328a2011-04-18 18:25:43 +1000774 int plugged;
NeilBrown191ea9b2005-06-21 17:17:23 -0700775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /*
777 * Register the new request and wait if the reconstruction
778 * thread has put up a bar for new requests.
779 * Continue immediately if no resync is active currently.
780 */
NeilBrown62de6082006-05-01 12:15:47 -0700781
NeilBrown3d310eb2005-06-21 17:17:26 -0700782 md_write_start(mddev, bio); /* wait on superblock update early */
783
NeilBrown6eef4b22009-12-14 12:49:51 +1100784 if (bio_data_dir(bio) == WRITE &&
785 bio->bi_sector + bio->bi_size/512 > mddev->suspend_lo &&
786 bio->bi_sector < mddev->suspend_hi) {
787 /* As the suspend_* range is controlled by
788 * userspace, we want an interruptible
789 * wait.
790 */
791 DEFINE_WAIT(w);
792 for (;;) {
793 flush_signals(current);
794 prepare_to_wait(&conf->wait_barrier,
795 &w, TASK_INTERRUPTIBLE);
796 if (bio->bi_sector + bio->bi_size/512 <= mddev->suspend_lo ||
797 bio->bi_sector >= mddev->suspend_hi)
798 break;
799 schedule();
800 }
801 finish_wait(&conf->wait_barrier, &w);
802 }
NeilBrown62de6082006-05-01 12:15:47 -0700803
NeilBrown17999be2006-01-06 00:20:12 -0800804 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
NeilBrown84255d12008-05-23 13:04:32 -0700806 bitmap = mddev->bitmap;
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 /*
809 * make_request() can abort the operation when READA is being
810 * used and no empty request is available.
811 *
812 */
813 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
814
815 r1_bio->master_bio = bio;
816 r1_bio->sectors = bio->bi_size >> 9;
NeilBrown191ea9b2005-06-21 17:17:23 -0700817 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 r1_bio->mddev = mddev;
819 r1_bio->sector = bio->bi_sector;
820
NeilBrownd2eb35a2011-07-28 11:31:48 +1000821 /* We might need to issue multiple reads to different
822 * devices if there are bad blocks around, so we keep
823 * track of the number of reads in bio->bi_phys_segments.
824 * If this is 0, there is only one r1_bio and no locking
825 * will be needed when requests complete. If it is
826 * non-zero, then it is the number of not-completed requests.
827 */
828 bio->bi_phys_segments = 0;
829 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
830
Jens Axboea3623572005-11-01 09:26:16 +0100831 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /*
833 * read balancing logic:
834 */
NeilBrownd2eb35a2011-07-28 11:31:48 +1000835 int max_sectors;
836 int rdisk;
837
838read_again:
839 rdisk = read_balance(conf, r1_bio, &max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 if (rdisk < 0) {
842 /* couldn't find anywhere to read from */
843 raid_end_bio_io(r1_bio);
844 return 0;
845 }
846 mirror = conf->mirrors + rdisk;
847
NeilBrowne5551902010-03-31 11:21:44 +1100848 if (test_bit(WriteMostly, &mirror->rdev->flags) &&
849 bitmap) {
850 /* Reading from a write-mostly device must
851 * take care not to over-take any writes
852 * that are 'behind'
853 */
854 wait_event(bitmap->behind_wait,
855 atomic_read(&bitmap->behind_writes) == 0);
856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 r1_bio->read_disk = rdisk;
858
NeilBrowna167f662010-10-26 18:31:13 +1100859 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000860 md_trim_bio(read_bio, r1_bio->sector - bio->bi_sector,
861 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 r1_bio->bios[rdisk] = read_bio;
864
865 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
866 read_bio->bi_bdev = mirror->rdev->bdev;
867 read_bio->bi_end_io = raid1_end_read_request;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200868 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 read_bio->bi_private = r1_bio;
870
NeilBrownd2eb35a2011-07-28 11:31:48 +1000871 if (max_sectors < r1_bio->sectors) {
872 /* could not read all from this device, so we will
873 * need another r1_bio.
874 */
875 int sectors_handled;
876
877 sectors_handled = (r1_bio->sector + max_sectors
878 - bio->bi_sector);
879 r1_bio->sectors = max_sectors;
880 spin_lock_irq(&conf->device_lock);
881 if (bio->bi_phys_segments == 0)
882 bio->bi_phys_segments = 2;
883 else
884 bio->bi_phys_segments++;
885 spin_unlock_irq(&conf->device_lock);
886 /* Cannot call generic_make_request directly
887 * as that will be queued in __make_request
888 * and subsequent mempool_alloc might block waiting
889 * for it. So hand bio over to raid1d.
890 */
891 reschedule_retry(r1_bio);
892
893 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
894
895 r1_bio->master_bio = bio;
896 r1_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
897 r1_bio->state = 0;
898 r1_bio->mddev = mddev;
899 r1_bio->sector = bio->bi_sector + sectors_handled;
900 goto read_again;
901 } else
902 generic_make_request(read_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return 0;
904 }
905
906 /*
907 * WRITE:
908 */
909 /* first select target devices under spinlock and
910 * inc refcount on their rdev. Record them by setting
911 * bios[x] to bio
912 */
NeilBrownc3b328a2011-04-18 18:25:43 +1000913 plugged = mddev_check_plugged(mddev);
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 disks = conf->raid_disks;
Dan Williams6bfe0b42008-04-30 00:52:32 -0700916 retry_write:
917 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 rcu_read_lock();
919 for (i = 0; i < disks; i++) {
Dan Williams6bfe0b42008-04-30 00:52:32 -0700920 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
921 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
922 atomic_inc(&rdev->nr_pending);
923 blocked_rdev = rdev;
924 break;
925 }
926 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 atomic_inc(&rdev->nr_pending);
NeilBrownb2d444d2005-11-08 21:39:31 -0800928 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown03c902e2006-01-06 00:20:46 -0800929 rdev_dec_pending(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 r1_bio->bios[i] = NULL;
NeilBrown964147d2010-05-18 15:27:13 +1000931 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 r1_bio->bios[i] = bio;
NeilBrown964147d2010-05-18 15:27:13 +1000933 targets++;
934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 } else
936 r1_bio->bios[i] = NULL;
937 }
938 rcu_read_unlock();
939
Dan Williams6bfe0b42008-04-30 00:52:32 -0700940 if (unlikely(blocked_rdev)) {
941 /* Wait for this device to become unblocked */
942 int j;
943
944 for (j = 0; j < i; j++)
945 if (r1_bio->bios[j])
946 rdev_dec_pending(conf->mirrors[j].rdev, mddev);
947
948 allow_barrier(conf);
949 md_wait_for_blocked_rdev(blocked_rdev, mddev);
950 wait_barrier(conf);
951 goto retry_write;
952 }
953
NeilBrown191ea9b2005-06-21 17:17:23 -0700954 if (targets < conf->raid_disks) {
955 /* array is degraded, we will not clear the bitmap
956 * on I/O completion (see raid1_end_write_request) */
957 set_bit(R1BIO_Degraded, &r1_bio->state);
958 }
NeilBrown06d91a52005-06-21 17:17:12 -0700959
NeilBrowne5551902010-03-31 11:21:44 +1100960 /* do behind I/O ?
961 * Not if there are too many, or cannot allocate memory,
962 * or a reader on WriteMostly is waiting for behind writes
963 * to flush */
NeilBrown4b6d2872005-09-09 16:23:47 -0700964 if (bitmap &&
NeilBrown42a04b52009-12-14 12:49:53 +1100965 (atomic_read(&bitmap->behind_writes)
966 < mddev->bitmap_info.max_write_behind) &&
NeilBrownaf6d7b72011-05-11 14:51:19 +1000967 !waitqueue_active(&bitmap->behind_wait))
968 alloc_behind_pages(bio, r1_bio);
NeilBrown4b6d2872005-09-09 16:23:47 -0700969
NeilBrown4e780642010-10-19 12:54:01 +1100970 atomic_set(&r1_bio->remaining, 1);
NeilBrown4b6d2872005-09-09 16:23:47 -0700971 atomic_set(&r1_bio->behind_remaining, 0);
NeilBrown191ea9b2005-06-21 17:17:23 -0700972
NeilBrown4e780642010-10-19 12:54:01 +1100973 bitmap_startwrite(bitmap, bio->bi_sector, r1_bio->sectors,
974 test_bit(R1BIO_BehindIO, &r1_bio->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 for (i = 0; i < disks; i++) {
976 struct bio *mbio;
977 if (!r1_bio->bios[i])
978 continue;
979
NeilBrowna167f662010-10-26 18:31:13 +1100980 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 r1_bio->bios[i] = mbio;
982
983 mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
984 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
985 mbio->bi_end_io = raid1_end_write_request;
Tejun Heoe9c74692010-09-03 11:56:18 +0200986 mbio->bi_rw = WRITE | do_flush_fua | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 mbio->bi_private = r1_bio;
988
NeilBrownaf6d7b72011-05-11 14:51:19 +1000989 if (r1_bio->behind_pages) {
NeilBrown4b6d2872005-09-09 16:23:47 -0700990 struct bio_vec *bvec;
991 int j;
992
993 /* Yes, I really want the '__' version so that
994 * we clear any unused pointer in the io_vec, rather
995 * than leave them unchanged. This is important
996 * because when we come to free the pages, we won't
NeilBrown046abee2010-10-26 15:46:20 +1100997 * know the original bi_idx, so we just free
NeilBrown4b6d2872005-09-09 16:23:47 -0700998 * them all
999 */
1000 __bio_for_each_segment(bvec, mbio, j, 0)
NeilBrownaf6d7b72011-05-11 14:51:19 +10001001 bvec->bv_page = r1_bio->behind_pages[j];
NeilBrown4b6d2872005-09-09 16:23:47 -07001002 if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
1003 atomic_inc(&r1_bio->behind_remaining);
1004 }
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 atomic_inc(&r1_bio->remaining);
NeilBrown4e780642010-10-19 12:54:01 +11001007 spin_lock_irqsave(&conf->device_lock, flags);
1008 bio_list_add(&conf->pending_bio_list, mbio);
NeilBrown4e780642010-10-19 12:54:01 +11001009 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
NeilBrownaf6d7b72011-05-11 14:51:19 +10001011 r1_bio_write_done(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
NeilBrown4e780642010-10-19 12:54:01 +11001013 /* In case raid1d snuck in to freeze_array */
NeilBrowna35e63e2008-03-04 14:29:29 -08001014 wake_up(&conf->wait_barrier);
1015
NeilBrownc3b328a2011-04-18 18:25:43 +10001016 if (do_sync || !bitmap || !plugged)
Lars Ellenberge3881a62007-01-10 23:15:37 -08001017 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 return 0;
1020}
1021
1022static void status(struct seq_file *seq, mddev_t *mddev)
1023{
NeilBrown070ec552009-06-16 16:54:21 +10001024 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 int i;
1026
1027 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown11ce99e2006-10-03 01:15:52 -07001028 conf->raid_disks - mddev->degraded);
NeilBrownddac7c72006-08-31 21:27:36 -07001029 rcu_read_lock();
1030 for (i = 0; i < conf->raid_disks; i++) {
1031 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 seq_printf(seq, "%s",
NeilBrownddac7c72006-08-31 21:27:36 -07001033 rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1034 }
1035 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 seq_printf(seq, "]");
1037}
1038
1039
1040static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1041{
1042 char b[BDEVNAME_SIZE];
NeilBrown070ec552009-06-16 16:54:21 +10001043 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 /*
1046 * If it is not operational, then we have already marked it as dead
1047 * else if it is the last working disks, ignore the error, let the
1048 * next level up know.
1049 * else mark the drive as failed
1050 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001051 if (test_bit(In_sync, &rdev->flags)
NeilBrown4044ba52009-01-09 08:31:11 +11001052 && (conf->raid_disks - mddev->degraded) == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /*
1054 * Don't fail the drive, act as though we were just a
NeilBrown4044ba52009-01-09 08:31:11 +11001055 * normal single drive.
1056 * However don't try a recovery from this drive as
1057 * it is very likely to fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 */
NeilBrown53890422011-07-27 11:00:36 +10001059 conf->recovery_disabled = mddev->recovery_disabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return;
NeilBrown4044ba52009-01-09 08:31:11 +11001061 }
NeilBrownc04be0a2006-10-03 01:15:53 -07001062 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1063 unsigned long flags;
1064 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 mddev->degraded++;
NeilBrowndd00a992007-05-10 03:15:50 -07001066 set_bit(Faulty, &rdev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -07001067 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 /*
1069 * if recovery is running, make sure it aborts.
1070 */
NeilBrowndfc70642008-05-23 13:04:39 -07001071 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrowndd00a992007-05-10 03:15:50 -07001072 } else
1073 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b422006-10-03 01:15:46 -07001074 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Joe Perches067032b2011-01-14 09:14:33 +11001075 printk(KERN_ALERT
1076 "md/raid1:%s: Disk failure on %s, disabling device.\n"
1077 "md/raid1:%s: Operation continuing on %d devices.\n",
NeilBrown9dd1e2f2010-05-03 14:30:35 +10001078 mdname(mddev), bdevname(rdev->bdev, b),
1079 mdname(mddev), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
1082static void print_conf(conf_t *conf)
1083{
1084 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
NeilBrown9dd1e2f2010-05-03 14:30:35 +10001086 printk(KERN_DEBUG "RAID1 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (!conf) {
NeilBrown9dd1e2f2010-05-03 14:30:35 +10001088 printk(KERN_DEBUG "(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return;
1090 }
NeilBrown9dd1e2f2010-05-03 14:30:35 +10001091 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 conf->raid_disks);
1093
NeilBrownddac7c72006-08-31 21:27:36 -07001094 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 for (i = 0; i < conf->raid_disks; i++) {
1096 char b[BDEVNAME_SIZE];
NeilBrownddac7c72006-08-31 21:27:36 -07001097 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
1098 if (rdev)
NeilBrown9dd1e2f2010-05-03 14:30:35 +10001099 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownddac7c72006-08-31 21:27:36 -07001100 i, !test_bit(In_sync, &rdev->flags),
1101 !test_bit(Faulty, &rdev->flags),
1102 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
NeilBrownddac7c72006-08-31 21:27:36 -07001104 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
1107static void close_sync(conf_t *conf)
1108{
NeilBrown17999be2006-01-06 00:20:12 -08001109 wait_barrier(conf);
1110 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 mempool_destroy(conf->r1buf_pool);
1113 conf->r1buf_pool = NULL;
1114}
1115
1116static int raid1_spare_active(mddev_t *mddev)
1117{
1118 int i;
1119 conf_t *conf = mddev->private;
NeilBrown6b965622010-08-18 11:56:59 +10001120 int count = 0;
1121 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 /*
1124 * Find all failed disks within the RAID1 configuration
NeilBrownddac7c72006-08-31 21:27:36 -07001125 * and mark them readable.
1126 * Called under mddev lock, so rcu protection not needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 */
1128 for (i = 0; i < conf->raid_disks; i++) {
NeilBrownddac7c72006-08-31 21:27:36 -07001129 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
1130 if (rdev
1131 && !test_bit(Faulty, &rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001132 && !test_and_set_bit(In_sync, &rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001133 count++;
Jonathan Brassow654e8b52011-07-27 11:00:36 +10001134 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136 }
NeilBrown6b965622010-08-18 11:56:59 +10001137 spin_lock_irqsave(&conf->device_lock, flags);
1138 mddev->degraded -= count;
1139 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001142 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
1145
1146static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1147{
1148 conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001149 int err = -EEXIST;
NeilBrown41158c72005-06-21 17:17:25 -07001150 int mirror = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 mirror_info_t *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10001152 int first = 0;
1153 int last = mddev->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
NeilBrown53890422011-07-27 11:00:36 +10001155 if (mddev->recovery_disabled == conf->recovery_disabled)
1156 return -EBUSY;
1157
Neil Brown6c2fce22008-06-28 08:31:31 +10001158 if (rdev->raid_disk >= 0)
1159 first = last = rdev->raid_disk;
1160
1161 for (mirror = first; mirror <= last; mirror++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if ( !(p=conf->mirrors+mirror)->rdev) {
1163
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10001164 disk_stack_limits(mddev->gendisk, rdev->bdev,
1165 rdev->data_offset << 9);
NeilBrown627a2d32010-03-08 16:44:38 +11001166 /* as we don't honour merge_bvec_fn, we must
1167 * never risk violating it, so limit
1168 * ->max_segments to one lying with a single
1169 * page, as a one page request is never in
1170 * violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 */
NeilBrown627a2d32010-03-08 16:44:38 +11001172 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1173 blk_queue_max_segments(mddev->queue, 1);
1174 blk_queue_segment_boundary(mddev->queue,
1175 PAGE_CACHE_SIZE - 1);
1176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 p->head_position = 0;
1179 rdev->raid_disk = mirror;
Neil Brown199050e2008-06-28 08:31:33 +10001180 err = 0;
NeilBrown6aea114a2005-11-28 13:44:13 -08001181 /* As all devices are equivalent, we don't need a full recovery
1182 * if this was recently any drive of the array
1183 */
1184 if (rdev->saved_raid_disk < 0)
NeilBrown41158c72005-06-21 17:17:25 -07001185 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001186 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 break;
1188 }
Andre Nollac5e7112009-08-03 10:59:47 +10001189 md_integrity_add_rdev(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001191 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
1194static int raid1_remove_disk(mddev_t *mddev, int number)
1195{
1196 conf_t *conf = mddev->private;
1197 int err = 0;
1198 mdk_rdev_t *rdev;
1199 mirror_info_t *p = conf->mirrors+ number;
1200
1201 print_conf(conf);
1202 rdev = p->rdev;
1203 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001204 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 atomic_read(&rdev->nr_pending)) {
1206 err = -EBUSY;
1207 goto abort;
1208 }
NeilBrown046abee2010-10-26 15:46:20 +11001209 /* Only remove non-faulty devices if recovery
NeilBrowndfc70642008-05-23 13:04:39 -07001210 * is not possible.
1211 */
1212 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown53890422011-07-27 11:00:36 +10001213 mddev->recovery_disabled != conf->recovery_disabled &&
NeilBrowndfc70642008-05-23 13:04:39 -07001214 mddev->degraded < conf->raid_disks) {
1215 err = -EBUSY;
1216 goto abort;
1217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001219 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (atomic_read(&rdev->nr_pending)) {
1221 /* lost the race, try later */
1222 err = -EBUSY;
1223 p->rdev = rdev;
Andre Nollac5e7112009-08-03 10:59:47 +10001224 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001226 err = md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
1228abort:
1229
1230 print_conf(conf);
1231 return err;
1232}
1233
1234
NeilBrown6712ecf2007-09-27 12:47:43 +02001235static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001237 r1bio_t *r1_bio = bio->bi_private;
NeilBrownd11c1712006-01-06 00:20:26 -08001238 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
NeilBrownd11c1712006-01-06 00:20:26 -08001240 for (i=r1_bio->mddev->raid_disks; i--; )
1241 if (r1_bio->bios[i] == bio)
1242 break;
1243 BUG_ON(i < 0);
1244 update_head_pos(i, r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 /*
1246 * we have read a block, now it needs to be re-written,
1247 * or re-read if the read failed.
1248 * We don't do much here, just schedule handling by raid1d
1249 */
NeilBrown69382e82006-01-06 00:20:22 -08001250 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 set_bit(R1BIO_Uptodate, &r1_bio->state);
NeilBrownd11c1712006-01-06 00:20:26 -08001252
1253 if (atomic_dec_and_test(&r1_bio->remaining))
1254 reschedule_retry(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
NeilBrown6712ecf2007-09-27 12:47:43 +02001257static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
1259 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001260 r1bio_t *r1_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 mddev_t *mddev = r1_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001262 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 int i;
1264 int mirror=0;
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 for (i = 0; i < conf->raid_disks; i++)
1267 if (r1_bio->bios[i] == bio) {
1268 mirror = i;
1269 break;
1270 }
NeilBrown6b1117d2006-03-31 02:31:57 -08001271 if (!uptodate) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001272 sector_t sync_blocks = 0;
NeilBrown6b1117d2006-03-31 02:31:57 -08001273 sector_t s = r1_bio->sector;
1274 long sectors_to_go = r1_bio->sectors;
1275 /* make sure these bits doesn't get cleared. */
1276 do {
NeilBrown5e3db642006-07-10 04:44:18 -07001277 bitmap_end_sync(mddev->bitmap, s,
NeilBrown6b1117d2006-03-31 02:31:57 -08001278 &sync_blocks, 1);
1279 s += sync_blocks;
1280 sectors_to_go -= sync_blocks;
1281 } while (sectors_to_go > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 md_error(mddev, conf->mirrors[mirror].rdev);
NeilBrown6b1117d2006-03-31 02:31:57 -08001283 }
NeilBrowne3b97032005-08-04 12:53:34 -07001284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 update_head_pos(mirror, r1_bio);
1286
1287 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown73d5c382009-02-25 13:18:47 +11001288 sector_t s = r1_bio->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 put_buf(r1_bio);
NeilBrown73d5c382009-02-25 13:18:47 +11001290 md_done_sync(mddev, s, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
NeilBrowna68e5872011-05-11 14:40:44 +10001294static int fix_sync_read_error(r1bio_t *r1_bio)
1295{
1296 /* Try some synchronous reads of other devices to get
1297 * good data, much like with normal read errors. Only
1298 * read into the pages we already have so we don't
1299 * need to re-issue the read request.
1300 * We don't need to freeze the array, because being in an
1301 * active sync request, there is no normal IO, and
1302 * no overlapping syncs.
NeilBrown06f60382011-07-28 11:31:48 +10001303 * We don't need to check is_badblock() again as we
1304 * made sure that anything with a bad block in range
1305 * will have bi_end_io clear.
NeilBrowna68e5872011-05-11 14:40:44 +10001306 */
1307 mddev_t *mddev = r1_bio->mddev;
1308 conf_t *conf = mddev->private;
1309 struct bio *bio = r1_bio->bios[r1_bio->read_disk];
1310 sector_t sect = r1_bio->sector;
1311 int sectors = r1_bio->sectors;
1312 int idx = 0;
1313
1314 while(sectors) {
1315 int s = sectors;
1316 int d = r1_bio->read_disk;
1317 int success = 0;
1318 mdk_rdev_t *rdev;
NeilBrown78d7f5f2011-05-11 14:48:56 +10001319 int start;
NeilBrowna68e5872011-05-11 14:40:44 +10001320
1321 if (s > (PAGE_SIZE>>9))
1322 s = PAGE_SIZE >> 9;
1323 do {
1324 if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
1325 /* No rcu protection needed here devices
1326 * can only be removed when no resync is
1327 * active, and resync is currently active
1328 */
1329 rdev = conf->mirrors[d].rdev;
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001330 if (sync_page_io(rdev, sect, s<<9,
NeilBrowna68e5872011-05-11 14:40:44 +10001331 bio->bi_io_vec[idx].bv_page,
1332 READ, false)) {
1333 success = 1;
1334 break;
1335 }
1336 }
1337 d++;
1338 if (d == conf->raid_disks)
1339 d = 0;
1340 } while (!success && d != r1_bio->read_disk);
1341
NeilBrown78d7f5f2011-05-11 14:48:56 +10001342 if (!success) {
NeilBrowna68e5872011-05-11 14:40:44 +10001343 char b[BDEVNAME_SIZE];
1344 /* Cannot read from anywhere, array is toast */
1345 md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
1346 printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O read error"
1347 " for block %llu\n",
1348 mdname(mddev),
1349 bdevname(bio->bi_bdev, b),
1350 (unsigned long long)r1_bio->sector);
1351 md_done_sync(mddev, r1_bio->sectors, 0);
1352 put_buf(r1_bio);
1353 return 0;
1354 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001355
1356 start = d;
1357 /* write it back and re-read */
1358 while (d != r1_bio->read_disk) {
1359 if (d == 0)
1360 d = conf->raid_disks;
1361 d--;
1362 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1363 continue;
1364 rdev = conf->mirrors[d].rdev;
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001365 if (sync_page_io(rdev, sect, s<<9,
NeilBrown78d7f5f2011-05-11 14:48:56 +10001366 bio->bi_io_vec[idx].bv_page,
1367 WRITE, false) == 0) {
1368 r1_bio->bios[d]->bi_end_io = NULL;
1369 rdev_dec_pending(rdev, mddev);
1370 md_error(mddev, rdev);
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001371 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001372 }
1373 d = start;
1374 while (d != r1_bio->read_disk) {
1375 if (d == 0)
1376 d = conf->raid_disks;
1377 d--;
1378 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1379 continue;
1380 rdev = conf->mirrors[d].rdev;
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001381 if (sync_page_io(rdev, sect, s<<9,
NeilBrown78d7f5f2011-05-11 14:48:56 +10001382 bio->bi_io_vec[idx].bv_page,
1383 READ, false) == 0)
1384 md_error(mddev, rdev);
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001385 else
1386 atomic_add(s, &rdev->corrected_errors);
NeilBrown78d7f5f2011-05-11 14:48:56 +10001387 }
NeilBrowna68e5872011-05-11 14:40:44 +10001388 sectors -= s;
1389 sect += s;
1390 idx ++;
1391 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001392 set_bit(R1BIO_Uptodate, &r1_bio->state);
NeilBrown7ca78d52011-05-11 14:50:37 +10001393 set_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrowna68e5872011-05-11 14:40:44 +10001394 return 1;
1395}
1396
1397static int process_checks(r1bio_t *r1_bio)
1398{
1399 /* We have read all readable devices. If we haven't
1400 * got the block, then there is no hope left.
1401 * If we have, then we want to do a comparison
1402 * and skip the write if everything is the same.
1403 * If any blocks failed to read, then we need to
1404 * attempt an over-write
1405 */
1406 mddev_t *mddev = r1_bio->mddev;
1407 conf_t *conf = mddev->private;
1408 int primary;
1409 int i;
1410
NeilBrown78d7f5f2011-05-11 14:48:56 +10001411 for (primary = 0; primary < conf->raid_disks; primary++)
NeilBrowna68e5872011-05-11 14:40:44 +10001412 if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
1413 test_bit(BIO_UPTODATE, &r1_bio->bios[primary]->bi_flags)) {
1414 r1_bio->bios[primary]->bi_end_io = NULL;
1415 rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
1416 break;
1417 }
1418 r1_bio->read_disk = primary;
NeilBrown78d7f5f2011-05-11 14:48:56 +10001419 for (i = 0; i < conf->raid_disks; i++) {
1420 int j;
1421 int vcnt = r1_bio->sectors >> (PAGE_SHIFT- 9);
1422 struct bio *pbio = r1_bio->bios[primary];
1423 struct bio *sbio = r1_bio->bios[i];
1424 int size;
NeilBrowna68e5872011-05-11 14:40:44 +10001425
NeilBrown78d7f5f2011-05-11 14:48:56 +10001426 if (r1_bio->bios[i]->bi_end_io != end_sync_read)
1427 continue;
NeilBrowna68e5872011-05-11 14:40:44 +10001428
NeilBrown78d7f5f2011-05-11 14:48:56 +10001429 if (test_bit(BIO_UPTODATE, &sbio->bi_flags)) {
1430 for (j = vcnt; j-- ; ) {
1431 struct page *p, *s;
1432 p = pbio->bi_io_vec[j].bv_page;
1433 s = sbio->bi_io_vec[j].bv_page;
1434 if (memcmp(page_address(p),
1435 page_address(s),
1436 PAGE_SIZE))
1437 break;
NeilBrowna68e5872011-05-11 14:40:44 +10001438 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001439 } else
1440 j = 0;
1441 if (j >= 0)
1442 mddev->resync_mismatches += r1_bio->sectors;
1443 if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
1444 && test_bit(BIO_UPTODATE, &sbio->bi_flags))) {
1445 /* No need to write to this device. */
1446 sbio->bi_end_io = NULL;
1447 rdev_dec_pending(conf->mirrors[i].rdev, mddev);
1448 continue;
NeilBrowna68e5872011-05-11 14:40:44 +10001449 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001450 /* fixup the bio for reuse */
1451 sbio->bi_vcnt = vcnt;
1452 sbio->bi_size = r1_bio->sectors << 9;
1453 sbio->bi_idx = 0;
1454 sbio->bi_phys_segments = 0;
1455 sbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1456 sbio->bi_flags |= 1 << BIO_UPTODATE;
1457 sbio->bi_next = NULL;
1458 sbio->bi_sector = r1_bio->sector +
1459 conf->mirrors[i].rdev->data_offset;
1460 sbio->bi_bdev = conf->mirrors[i].rdev->bdev;
1461 size = sbio->bi_size;
1462 for (j = 0; j < vcnt ; j++) {
1463 struct bio_vec *bi;
1464 bi = &sbio->bi_io_vec[j];
1465 bi->bv_offset = 0;
1466 if (size > PAGE_SIZE)
1467 bi->bv_len = PAGE_SIZE;
1468 else
1469 bi->bv_len = size;
1470 size -= PAGE_SIZE;
1471 memcpy(page_address(bi->bv_page),
1472 page_address(pbio->bi_io_vec[j].bv_page),
1473 PAGE_SIZE);
1474 }
1475 }
NeilBrowna68e5872011-05-11 14:40:44 +10001476 return 0;
1477}
1478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
1480{
NeilBrown070ec552009-06-16 16:54:21 +10001481 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 int i;
1483 int disks = conf->raid_disks;
1484 struct bio *bio, *wbio;
1485
1486 bio = r1_bio->bios[r1_bio->read_disk];
1487
NeilBrowna68e5872011-05-11 14:40:44 +10001488 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
1489 /* ouch - failed to read all of that. */
1490 if (!fix_sync_read_error(r1_bio))
1491 return;
NeilBrown7ca78d52011-05-11 14:50:37 +10001492
1493 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1494 if (process_checks(r1_bio) < 0)
1495 return;
NeilBrownd11c1712006-01-06 00:20:26 -08001496 /*
1497 * schedule writes
1498 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 atomic_set(&r1_bio->remaining, 1);
1500 for (i = 0; i < disks ; i++) {
1501 wbio = r1_bio->bios[i];
NeilBrown3e198f72006-01-06 00:20:21 -08001502 if (wbio->bi_end_io == NULL ||
1503 (wbio->bi_end_io == end_sync_read &&
1504 (i == r1_bio->read_disk ||
1505 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 continue;
1507
NeilBrown3e198f72006-01-06 00:20:21 -08001508 wbio->bi_rw = WRITE;
1509 wbio->bi_end_io = end_sync_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 atomic_inc(&r1_bio->remaining);
1511 md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
NeilBrown191ea9b2005-06-21 17:17:23 -07001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 generic_make_request(wbio);
1514 }
1515
1516 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001517 /* if we're here, all write(s) have completed, so clean up */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 md_done_sync(mddev, r1_bio->sectors, 1);
1519 put_buf(r1_bio);
1520 }
1521}
1522
1523/*
1524 * This is a kernel thread which:
1525 *
1526 * 1. Retries failed read operations on working mirrors.
1527 * 2. Updates the raid superblock when problems encounter.
NeilBrownd2eb35a2011-07-28 11:31:48 +10001528 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 */
1530
NeilBrown867868f2006-10-03 01:15:51 -07001531static void fix_read_error(conf_t *conf, int read_disk,
1532 sector_t sect, int sectors)
1533{
1534 mddev_t *mddev = conf->mddev;
1535 while(sectors) {
1536 int s = sectors;
1537 int d = read_disk;
1538 int success = 0;
1539 int start;
1540 mdk_rdev_t *rdev;
1541
1542 if (s > (PAGE_SIZE>>9))
1543 s = PAGE_SIZE >> 9;
1544
1545 do {
1546 /* Note: no rcu protection needed here
1547 * as this is synchronous in the raid1d thread
1548 * which is the thread that might remove
1549 * a device. If raid1d ever becomes multi-threaded....
1550 */
NeilBrownd2eb35a2011-07-28 11:31:48 +10001551 sector_t first_bad;
1552 int bad_sectors;
1553
NeilBrown867868f2006-10-03 01:15:51 -07001554 rdev = conf->mirrors[d].rdev;
1555 if (rdev &&
1556 test_bit(In_sync, &rdev->flags) &&
NeilBrownd2eb35a2011-07-28 11:31:48 +10001557 is_badblock(rdev, sect, s,
1558 &first_bad, &bad_sectors) == 0 &&
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001559 sync_page_io(rdev, sect, s<<9,
1560 conf->tmppage, READ, false))
NeilBrown867868f2006-10-03 01:15:51 -07001561 success = 1;
1562 else {
1563 d++;
1564 if (d == conf->raid_disks)
1565 d = 0;
1566 }
1567 } while (!success && d != read_disk);
1568
1569 if (!success) {
1570 /* Cannot read from anywhere -- bye bye array */
1571 md_error(mddev, conf->mirrors[read_disk].rdev);
1572 break;
1573 }
1574 /* write it back and re-read */
1575 start = d;
1576 while (d != read_disk) {
1577 if (d==0)
1578 d = conf->raid_disks;
1579 d--;
1580 rdev = conf->mirrors[d].rdev;
1581 if (rdev &&
1582 test_bit(In_sync, &rdev->flags)) {
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001583 if (sync_page_io(rdev, sect, s<<9,
1584 conf->tmppage, WRITE, false)
NeilBrown867868f2006-10-03 01:15:51 -07001585 == 0)
1586 /* Well, this device is dead */
1587 md_error(mddev, rdev);
1588 }
1589 }
1590 d = start;
1591 while (d != read_disk) {
1592 char b[BDEVNAME_SIZE];
1593 if (d==0)
1594 d = conf->raid_disks;
1595 d--;
1596 rdev = conf->mirrors[d].rdev;
1597 if (rdev &&
1598 test_bit(In_sync, &rdev->flags)) {
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001599 if (sync_page_io(rdev, sect, s<<9,
1600 conf->tmppage, READ, false)
NeilBrown867868f2006-10-03 01:15:51 -07001601 == 0)
1602 /* Well, this device is dead */
1603 md_error(mddev, rdev);
1604 else {
1605 atomic_add(s, &rdev->corrected_errors);
1606 printk(KERN_INFO
NeilBrown9dd1e2f2010-05-03 14:30:35 +10001607 "md/raid1:%s: read error corrected "
NeilBrown867868f2006-10-03 01:15:51 -07001608 "(%d sectors at %llu on %s)\n",
1609 mdname(mddev), s,
Randy Dunlap969b7552006-10-28 10:38:32 -07001610 (unsigned long long)(sect +
1611 rdev->data_offset),
NeilBrown867868f2006-10-03 01:15:51 -07001612 bdevname(rdev->bdev, b));
1613 }
1614 }
1615 }
1616 sectors -= s;
1617 sect += s;
1618 }
1619}
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621static void raid1d(mddev_t *mddev)
1622{
1623 r1bio_t *r1_bio;
1624 struct bio *bio;
1625 unsigned long flags;
NeilBrown070ec552009-06-16 16:54:21 +10001626 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 struct list_head *head = &conf->retry_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 mdk_rdev_t *rdev;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10001629 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 md_check_recovery(mddev);
NeilBrowne1dfa0a2011-04-18 18:25:41 +10001632
1633 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 for (;;) {
1635 char b[BDEVNAME_SIZE];
NeilBrowna35e63e2008-03-04 14:29:29 -08001636
NeilBrownc3b328a2011-04-18 18:25:43 +10001637 if (atomic_read(&mddev->plug_cnt) == 0)
1638 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08001639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08001641 if (list_empty(head)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001642 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08001644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
1646 list_del(head->prev);
NeilBrownddaf22a2006-01-06 00:20:19 -08001647 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 spin_unlock_irqrestore(&conf->device_lock, flags);
1649
1650 mddev = r1_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001651 conf = mddev->private;
Jens Axboe7eaceac2011-03-10 08:52:07 +01001652 if (test_bit(R1BIO_IsSync, &r1_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 sync_request_write(mddev, r1_bio);
NeilBrownd2eb35a2011-07-28 11:31:48 +10001654 else if (test_bit(R1BIO_ReadError, &r1_bio->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 int disk;
NeilBrownd2eb35a2011-07-28 11:31:48 +10001656 int max_sectors;
NeilBrownddaf22a2006-01-06 00:20:19 -08001657
NeilBrownd2eb35a2011-07-28 11:31:48 +10001658 clear_bit(R1BIO_ReadError, &r1_bio->state);
NeilBrownddaf22a2006-01-06 00:20:19 -08001659 /* we got a read error. Maybe the drive is bad. Maybe just
1660 * the block and we can fix it.
1661 * We freeze all other IO, and try reading the block from
1662 * other devices. When we find one, we re-write
1663 * and check it that fixes the read error.
1664 * This is all done synchronously while the array is
1665 * frozen
1666 */
NeilBrown867868f2006-10-03 01:15:51 -07001667 if (mddev->ro == 0) {
1668 freeze_array(conf);
1669 fix_read_error(conf, r1_bio->read_disk,
1670 r1_bio->sector,
1671 r1_bio->sectors);
1672 unfreeze_array(conf);
NeilBrownd0e26072009-12-01 17:30:59 +11001673 } else
1674 md_error(mddev,
1675 conf->mirrors[r1_bio->read_disk].rdev);
NeilBrownddaf22a2006-01-06 00:20:19 -08001676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 bio = r1_bio->bios[r1_bio->read_disk];
NeilBrownd2eb35a2011-07-28 11:31:48 +10001678 bdevname(bio->bi_bdev, b);
1679read_more:
1680 disk = read_balance(conf, r1_bio, &max_sectors);
1681 if (disk == -1) {
NeilBrown9dd1e2f2010-05-03 14:30:35 +10001682 printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 " read error for block %llu\n",
NeilBrownd2eb35a2011-07-28 11:31:48 +10001684 mdname(mddev), b,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 (unsigned long long)r1_bio->sector);
1686 raid_end_bio_io(r1_bio);
1687 } else {
NeilBrown2c7d46e2010-08-18 16:16:05 +10001688 const unsigned long do_sync = r1_bio->master_bio->bi_rw & REQ_SYNC;
NeilBrownd2eb35a2011-07-28 11:31:48 +10001689 if (bio) {
1690 r1_bio->bios[r1_bio->read_disk] =
1691 mddev->ro ? IO_BLOCKED : NULL;
1692 bio_put(bio);
1693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 r1_bio->read_disk = disk;
NeilBrowna167f662010-10-26 18:31:13 +11001695 bio = bio_clone_mddev(r1_bio->master_bio,
1696 GFP_NOIO, mddev);
NeilBrownd2eb35a2011-07-28 11:31:48 +10001697 md_trim_bio(bio,
1698 r1_bio->sector - bio->bi_sector,
1699 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 r1_bio->bios[r1_bio->read_disk] = bio;
1701 rdev = conf->mirrors[disk].rdev;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001702 printk_ratelimited(
1703 KERN_ERR
1704 "md/raid1:%s: redirecting sector %llu"
1705 " to other mirror: %s\n",
1706 mdname(mddev),
1707 (unsigned long long)r1_bio->sector,
1708 bdevname(rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 bio->bi_sector = r1_bio->sector + rdev->data_offset;
1710 bio->bi_bdev = rdev->bdev;
1711 bio->bi_end_io = raid1_end_read_request;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001712 bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 bio->bi_private = r1_bio;
NeilBrownd2eb35a2011-07-28 11:31:48 +10001714 if (max_sectors < r1_bio->sectors) {
1715 /* Drat - have to split this up more */
1716 struct bio *mbio = r1_bio->master_bio;
1717 int sectors_handled =
1718 r1_bio->sector + max_sectors
1719 - mbio->bi_sector;
1720 r1_bio->sectors = max_sectors;
1721 spin_lock_irq(&conf->device_lock);
1722 if (mbio->bi_phys_segments == 0)
1723 mbio->bi_phys_segments = 2;
1724 else
1725 mbio->bi_phys_segments++;
1726 spin_unlock_irq(&conf->device_lock);
1727 generic_make_request(bio);
1728 bio = NULL;
1729
1730 r1_bio = mempool_alloc(conf->r1bio_pool,
1731 GFP_NOIO);
1732
1733 r1_bio->master_bio = mbio;
1734 r1_bio->sectors = (mbio->bi_size >> 9)
1735 - sectors_handled;
1736 r1_bio->state = 0;
1737 set_bit(R1BIO_ReadError,
1738 &r1_bio->state);
1739 r1_bio->mddev = mddev;
1740 r1_bio->sector = mbio->bi_sector
1741 + sectors_handled;
1742
1743 goto read_more;
1744 } else
1745 generic_make_request(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
NeilBrownd2eb35a2011-07-28 11:31:48 +10001747 } else {
1748 /* just a partial read to be scheduled from separate
1749 * context
1750 */
1751 generic_make_request(r1_bio->bios[r1_bio->read_disk]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
NeilBrown1d9d5242009-10-16 15:55:32 +11001753 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10001755 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
1758
1759static int init_resync(conf_t *conf)
1760{
1761 int buffs;
1762
1763 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhenn9e77c482006-04-01 01:08:49 +02001764 BUG_ON(conf->r1buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
1766 conf->poolinfo);
1767 if (!conf->r1buf_pool)
1768 return -ENOMEM;
1769 conf->next_resync = 0;
1770 return 0;
1771}
1772
1773/*
1774 * perform a "sync" on one "block"
1775 *
1776 * We need to make sure that no normal I/O request - particularly write
1777 * requests - conflict with active sync requests.
1778 *
1779 * This is achieved by tracking pending requests and a 'barrier' concept
1780 * that can be installed to exclude normal IO requests.
1781 */
1782
NeilBrown57afd892005-06-21 17:17:13 -07001783static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
NeilBrown070ec552009-06-16 16:54:21 +10001785 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 r1bio_t *r1_bio;
1787 struct bio *bio;
1788 sector_t max_sector, nr_sectors;
NeilBrown3e198f72006-01-06 00:20:21 -08001789 int disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 int i;
NeilBrown3e198f72006-01-06 00:20:21 -08001791 int wonly = -1;
1792 int write_targets = 0, read_targets = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001793 sector_t sync_blocks;
NeilBrowne3b97032005-08-04 12:53:34 -07001794 int still_degraded = 0;
NeilBrown06f60382011-07-28 11:31:48 +10001795 int good_sectors = RESYNC_SECTORS;
1796 int min_bad = 0; /* number of sectors that are bad in all devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 if (!conf->r1buf_pool)
1799 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07001800 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Andre Noll58c0fed2009-03-31 14:33:13 +11001802 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 if (sector_nr >= max_sector) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001804 /* If we aborted, we need to abort the
1805 * sync on the 'current' bitmap chunk (there will
1806 * only be one in raid1 resync.
1807 * We can find the current addess in mddev->curr_resync
1808 */
NeilBrown6a806c52005-07-15 03:56:35 -07001809 if (mddev->curr_resync < max_sector) /* aborted */
1810 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
NeilBrown191ea9b2005-06-21 17:17:23 -07001811 &sync_blocks, 1);
NeilBrown6a806c52005-07-15 03:56:35 -07001812 else /* completed sync */
NeilBrown191ea9b2005-06-21 17:17:23 -07001813 conf->fullsync = 0;
NeilBrown6a806c52005-07-15 03:56:35 -07001814
1815 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 close_sync(conf);
1817 return 0;
1818 }
1819
NeilBrown07d84d102006-06-26 00:27:56 -07001820 if (mddev->bitmap == NULL &&
1821 mddev->recovery_cp == MaxSector &&
NeilBrown6394cca2006-08-27 01:23:50 -07001822 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown07d84d102006-06-26 00:27:56 -07001823 conf->fullsync == 0) {
1824 *skipped = 1;
1825 return max_sector - sector_nr;
1826 }
NeilBrown6394cca2006-08-27 01:23:50 -07001827 /* before building a request, check if we can skip these blocks..
1828 * This call the bitmap_start_sync doesn't actually record anything
1829 */
NeilBrowne3b97032005-08-04 12:53:34 -07001830 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrowne5de485f2005-11-08 21:39:38 -08001831 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07001832 /* We can skip this block, and probably several more */
1833 *skipped = 1;
1834 return sync_blocks;
1835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 /*
NeilBrown17999be2006-01-06 00:20:12 -08001837 * If there is non-resync activity waiting for a turn,
1838 * and resync is going fast enough,
1839 * then let it though before starting on this new sync request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 */
NeilBrown17999be2006-01-06 00:20:12 -08001841 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 msleep_interruptible(1000);
NeilBrown17999be2006-01-06 00:20:12 -08001843
NeilBrownb47490c2008-02-06 01:39:50 -08001844 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
NeilBrown1c4588e2010-10-26 17:41:22 +11001845 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
NeilBrown17999be2006-01-06 00:20:12 -08001846 raise_barrier(conf);
1847
1848 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
NeilBrown3e198f72006-01-06 00:20:21 -08001850 rcu_read_lock();
1851 /*
1852 * If we get a correctably read error during resync or recovery,
1853 * we might want to read from a different device. So we
1854 * flag all drives that could conceivably be read from for READ,
1855 * and any others (which will be non-In_sync devices) for WRITE.
1856 * If a read fails, we try reading from something else for which READ
1857 * is OK.
1858 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 r1_bio->mddev = mddev;
1861 r1_bio->sector = sector_nr;
NeilBrown191ea9b2005-06-21 17:17:23 -07001862 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 set_bit(R1BIO_IsSync, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 for (i=0; i < conf->raid_disks; i++) {
NeilBrown3e198f72006-01-06 00:20:21 -08001866 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 bio = r1_bio->bios[i];
1868
1869 /* take from bio_init */
1870 bio->bi_next = NULL;
NeilBrowndb8d9d32010-10-07 12:00:50 +11001871 bio->bi_flags &= ~(BIO_POOL_MASK-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 bio->bi_flags |= 1 << BIO_UPTODATE;
NeilBrowndb8d9d32010-10-07 12:00:50 +11001873 bio->bi_comp_cpu = -1;
NeilBrown802ba062006-12-13 00:34:13 -08001874 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 bio->bi_vcnt = 0;
1876 bio->bi_idx = 0;
1877 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 bio->bi_size = 0;
1879 bio->bi_end_io = NULL;
1880 bio->bi_private = NULL;
1881
NeilBrown3e198f72006-01-06 00:20:21 -08001882 rdev = rcu_dereference(conf->mirrors[i].rdev);
1883 if (rdev == NULL ||
NeilBrown06f60382011-07-28 11:31:48 +10001884 test_bit(Faulty, &rdev->flags)) {
NeilBrowne3b97032005-08-04 12:53:34 -07001885 still_degraded = 1;
NeilBrown3e198f72006-01-06 00:20:21 -08001886 } else if (!test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 bio->bi_rw = WRITE;
1888 bio->bi_end_io = end_sync_write;
1889 write_targets ++;
NeilBrown3e198f72006-01-06 00:20:21 -08001890 } else {
1891 /* may need to read from here */
NeilBrown06f60382011-07-28 11:31:48 +10001892 sector_t first_bad = MaxSector;
1893 int bad_sectors;
1894
1895 if (is_badblock(rdev, sector_nr, good_sectors,
1896 &first_bad, &bad_sectors)) {
1897 if (first_bad > sector_nr)
1898 good_sectors = first_bad - sector_nr;
1899 else {
1900 bad_sectors -= (sector_nr - first_bad);
1901 if (min_bad == 0 ||
1902 min_bad > bad_sectors)
1903 min_bad = bad_sectors;
1904 }
NeilBrown3e198f72006-01-06 00:20:21 -08001905 }
NeilBrown06f60382011-07-28 11:31:48 +10001906 if (sector_nr < first_bad) {
1907 if (test_bit(WriteMostly, &rdev->flags)) {
1908 if (wonly < 0)
1909 wonly = i;
1910 } else {
1911 if (disk < 0)
1912 disk = i;
1913 }
1914 bio->bi_rw = READ;
1915 bio->bi_end_io = end_sync_read;
1916 read_targets++;
1917 }
NeilBrown3e198f72006-01-06 00:20:21 -08001918 }
NeilBrown06f60382011-07-28 11:31:48 +10001919 if (bio->bi_end_io) {
1920 atomic_inc(&rdev->nr_pending);
1921 bio->bi_sector = sector_nr + rdev->data_offset;
1922 bio->bi_bdev = rdev->bdev;
1923 bio->bi_private = r1_bio;
1924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
NeilBrown3e198f72006-01-06 00:20:21 -08001926 rcu_read_unlock();
1927 if (disk < 0)
1928 disk = wonly;
1929 r1_bio->read_disk = disk;
NeilBrown191ea9b2005-06-21 17:17:23 -07001930
NeilBrown06f60382011-07-28 11:31:48 +10001931 if (read_targets == 0 && min_bad > 0) {
1932 /* These sectors are bad on all InSync devices, so we
1933 * need to mark them bad on all write targets
1934 */
1935 int ok = 1;
1936 for (i = 0 ; i < conf->raid_disks ; i++)
1937 if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
1938 mdk_rdev_t *rdev =
1939 rcu_dereference(conf->mirrors[i].rdev);
1940 ok = rdev_set_badblocks(rdev, sector_nr,
1941 min_bad, 0
1942 ) && ok;
1943 }
1944 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1945 *skipped = 1;
1946 put_buf(r1_bio);
1947
1948 if (!ok) {
1949 /* Cannot record the badblocks, so need to
1950 * abort the resync.
1951 * If there are multiple read targets, could just
1952 * fail the really bad ones ???
1953 */
1954 conf->recovery_disabled = mddev->recovery_disabled;
1955 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1956 return 0;
1957 } else
1958 return min_bad;
1959
1960 }
1961 if (min_bad > 0 && min_bad < good_sectors) {
1962 /* only resync enough to reach the next bad->good
1963 * transition */
1964 good_sectors = min_bad;
1965 }
1966
NeilBrown3e198f72006-01-06 00:20:21 -08001967 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
1968 /* extra read targets are also write targets */
1969 write_targets += read_targets-1;
1970
1971 if (write_targets == 0 || read_targets == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 /* There is nowhere to write, so all non-sync
1973 * drives must be failed - so we are finished
1974 */
NeilBrown57afd892005-06-21 17:17:13 -07001975 sector_t rv = max_sector - sector_nr;
1976 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 put_buf(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 return rv;
1979 }
1980
NeilBrownc6207272008-02-06 01:39:52 -08001981 if (max_sector > mddev->resync_max)
1982 max_sector = mddev->resync_max; /* Don't do IO beyond here */
NeilBrown06f60382011-07-28 11:31:48 +10001983 if (max_sector > sector_nr + good_sectors)
1984 max_sector = sector_nr + good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 nr_sectors = 0;
NeilBrown289e99e2005-06-21 17:17:24 -07001986 sync_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 do {
1988 struct page *page;
1989 int len = PAGE_SIZE;
1990 if (sector_nr + (len>>9) > max_sector)
1991 len = (max_sector - sector_nr) << 9;
1992 if (len == 0)
1993 break;
NeilBrown6a806c52005-07-15 03:56:35 -07001994 if (sync_blocks == 0) {
1995 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
NeilBrowne5de485f2005-11-08 21:39:38 -08001996 &sync_blocks, still_degraded) &&
1997 !conf->fullsync &&
1998 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
NeilBrown6a806c52005-07-15 03:56:35 -07001999 break;
Eric Sesterhenn9e77c482006-04-01 01:08:49 +02002000 BUG_ON(sync_blocks < (PAGE_SIZE>>9));
NeilBrown7571ae82010-10-07 11:54:46 +11002001 if ((len >> 9) > sync_blocks)
NeilBrown6a806c52005-07-15 03:56:35 -07002002 len = sync_blocks<<9;
NeilBrownab7a30c2005-06-21 17:17:23 -07002003 }
NeilBrown191ea9b2005-06-21 17:17:23 -07002004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 for (i=0 ; i < conf->raid_disks; i++) {
2006 bio = r1_bio->bios[i];
2007 if (bio->bi_end_io) {
NeilBrownd11c1712006-01-06 00:20:26 -08002008 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 if (bio_add_page(bio, page, len, 0) == 0) {
2010 /* stop here */
NeilBrownd11c1712006-01-06 00:20:26 -08002011 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 while (i > 0) {
2013 i--;
2014 bio = r1_bio->bios[i];
NeilBrown6a806c52005-07-15 03:56:35 -07002015 if (bio->bi_end_io==NULL)
2016 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 /* remove last page from this bio */
2018 bio->bi_vcnt--;
2019 bio->bi_size -= len;
2020 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
2021 }
2022 goto bio_full;
2023 }
2024 }
2025 }
2026 nr_sectors += len>>9;
2027 sector_nr += len>>9;
NeilBrown191ea9b2005-06-21 17:17:23 -07002028 sync_blocks -= (len>>9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
2030 bio_full:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 r1_bio->sectors = nr_sectors;
2032
NeilBrownd11c1712006-01-06 00:20:26 -08002033 /* For a user-requested sync, we read all readable devices and do a
2034 * compare
2035 */
2036 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2037 atomic_set(&r1_bio->remaining, read_targets);
2038 for (i=0; i<conf->raid_disks; i++) {
2039 bio = r1_bio->bios[i];
2040 if (bio->bi_end_io == end_sync_read) {
NeilBrownddac7c72006-08-31 21:27:36 -07002041 md_sync_acct(bio->bi_bdev, nr_sectors);
NeilBrownd11c1712006-01-06 00:20:26 -08002042 generic_make_request(bio);
2043 }
2044 }
2045 } else {
2046 atomic_set(&r1_bio->remaining, 1);
2047 bio = r1_bio->bios[r1_bio->read_disk];
NeilBrownddac7c72006-08-31 21:27:36 -07002048 md_sync_acct(bio->bi_bdev, nr_sectors);
NeilBrownd11c1712006-01-06 00:20:26 -08002049 generic_make_request(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
NeilBrownd11c1712006-01-06 00:20:26 -08002051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 return nr_sectors;
2053}
2054
Dan Williams80c3a6c2009-03-17 18:10:40 -07002055static sector_t raid1_size(mddev_t *mddev, sector_t sectors, int raid_disks)
2056{
2057 if (sectors)
2058 return sectors;
2059
2060 return mddev->dev_sectors;
2061}
2062
NeilBrown709ae482009-12-14 12:49:51 +11002063static conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
2065 conf_t *conf;
NeilBrown709ae482009-12-14 12:49:51 +11002066 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 mirror_info_t *disk;
2068 mdk_rdev_t *rdev;
NeilBrown709ae482009-12-14 12:49:51 +11002069 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
NeilBrown9ffae0c2006-01-06 00:20:32 -08002071 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 if (!conf)
NeilBrown709ae482009-12-14 12:49:51 +11002073 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
NeilBrown9ffae0c2006-01-06 00:20:32 -08002075 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 GFP_KERNEL);
2077 if (!conf->mirrors)
NeilBrown709ae482009-12-14 12:49:51 +11002078 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
NeilBrownddaf22a2006-01-06 00:20:19 -08002080 conf->tmppage = alloc_page(GFP_KERNEL);
2081 if (!conf->tmppage)
NeilBrown709ae482009-12-14 12:49:51 +11002082 goto abort;
NeilBrownddaf22a2006-01-06 00:20:19 -08002083
NeilBrown709ae482009-12-14 12:49:51 +11002084 conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 if (!conf->poolinfo)
NeilBrown709ae482009-12-14 12:49:51 +11002086 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 conf->poolinfo->raid_disks = mddev->raid_disks;
2088 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
2089 r1bio_pool_free,
2090 conf->poolinfo);
2091 if (!conf->r1bio_pool)
NeilBrown709ae482009-12-14 12:49:51 +11002092 goto abort;
2093
NeilBrowned9bfdf2009-10-16 15:55:44 +11002094 conf->poolinfo->mddev = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Neil Browne7e72bf2008-05-14 16:05:54 -07002096 spin_lock_init(&conf->device_lock);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002097 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown709ae482009-12-14 12:49:51 +11002098 int disk_idx = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 if (disk_idx >= mddev->raid_disks
2100 || disk_idx < 0)
2101 continue;
2102 disk = conf->mirrors + disk_idx;
2103
2104 disk->rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
2108 conf->raid_disks = mddev->raid_disks;
2109 conf->mddev = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 INIT_LIST_HEAD(&conf->retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 spin_lock_init(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -08002113 init_waitqueue_head(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
NeilBrown191ea9b2005-06-21 17:17:23 -07002115 bio_list_init(&conf->pending_bio_list);
NeilBrown191ea9b2005-06-21 17:17:23 -07002116
NeilBrown709ae482009-12-14 12:49:51 +11002117 conf->last_used = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 for (i = 0; i < conf->raid_disks; i++) {
2119
2120 disk = conf->mirrors + i;
2121
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002122 if (!disk->rdev ||
2123 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 disk->head_position = 0;
NeilBrown918f0232007-08-22 14:01:52 -07002125 if (disk->rdev)
2126 conf->fullsync = 1;
NeilBrown709ae482009-12-14 12:49:51 +11002127 } else if (conf->last_used < 0)
2128 /*
2129 * The first working device is used as a
2130 * starting point to read balancing.
2131 */
2132 conf->last_used = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
NeilBrown709ae482009-12-14 12:49:51 +11002134
2135 err = -EIO;
2136 if (conf->last_used < 0) {
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002137 printk(KERN_ERR "md/raid1:%s: no operational mirrors\n",
NeilBrown709ae482009-12-14 12:49:51 +11002138 mdname(mddev));
2139 goto abort;
NeilBrown11ce99e2006-10-03 01:15:52 -07002140 }
NeilBrown709ae482009-12-14 12:49:51 +11002141 err = -ENOMEM;
2142 conf->thread = md_register_thread(raid1d, mddev, NULL);
2143 if (!conf->thread) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002144 printk(KERN_ERR
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002145 "md/raid1:%s: couldn't allocate thread\n",
NeilBrown191ea9b2005-06-21 17:17:23 -07002146 mdname(mddev));
NeilBrown709ae482009-12-14 12:49:51 +11002147 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 }
NeilBrown191ea9b2005-06-21 17:17:23 -07002149
NeilBrown709ae482009-12-14 12:49:51 +11002150 return conf;
2151
2152 abort:
2153 if (conf) {
2154 if (conf->r1bio_pool)
2155 mempool_destroy(conf->r1bio_pool);
2156 kfree(conf->mirrors);
2157 safe_put_page(conf->tmppage);
2158 kfree(conf->poolinfo);
2159 kfree(conf);
2160 }
2161 return ERR_PTR(err);
2162}
2163
2164static int run(mddev_t *mddev)
2165{
2166 conf_t *conf;
2167 int i;
2168 mdk_rdev_t *rdev;
2169
2170 if (mddev->level != 1) {
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002171 printk(KERN_ERR "md/raid1:%s: raid level not set to mirroring (%d)\n",
NeilBrown709ae482009-12-14 12:49:51 +11002172 mdname(mddev), mddev->level);
2173 return -EIO;
2174 }
2175 if (mddev->reshape_position != MaxSector) {
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002176 printk(KERN_ERR "md/raid1:%s: reshape_position set but not supported\n",
NeilBrown709ae482009-12-14 12:49:51 +11002177 mdname(mddev));
2178 return -EIO;
2179 }
2180 /*
2181 * copy the already verified devices into our private RAID1
2182 * bookkeeping area. [whatever we allocate in run(),
2183 * should be freed in stop()]
2184 */
2185 if (mddev->private == NULL)
2186 conf = setup_conf(mddev);
2187 else
2188 conf = mddev->private;
2189
2190 if (IS_ERR(conf))
2191 return PTR_ERR(conf);
2192
NeilBrown709ae482009-12-14 12:49:51 +11002193 list_for_each_entry(rdev, &mddev->disks, same_set) {
Jonathan Brassow1ed72422011-06-07 17:50:35 -05002194 if (!mddev->gendisk)
2195 continue;
NeilBrown709ae482009-12-14 12:49:51 +11002196 disk_stack_limits(mddev->gendisk, rdev->bdev,
2197 rdev->data_offset << 9);
2198 /* as we don't honour merge_bvec_fn, we must never risk
NeilBrown627a2d32010-03-08 16:44:38 +11002199 * violating it, so limit ->max_segments to 1 lying within
2200 * a single page, as a one page request is never in violation.
NeilBrown709ae482009-12-14 12:49:51 +11002201 */
NeilBrown627a2d32010-03-08 16:44:38 +11002202 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2203 blk_queue_max_segments(mddev->queue, 1);
2204 blk_queue_segment_boundary(mddev->queue,
2205 PAGE_CACHE_SIZE - 1);
2206 }
NeilBrown709ae482009-12-14 12:49:51 +11002207 }
2208
2209 mddev->degraded = 0;
2210 for (i=0; i < conf->raid_disks; i++)
2211 if (conf->mirrors[i].rdev == NULL ||
2212 !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
2213 test_bit(Faulty, &conf->mirrors[i].rdev->flags))
2214 mddev->degraded++;
2215
2216 if (conf->raid_disks - mddev->degraded == 1)
2217 mddev->recovery_cp = MaxSector;
2218
Andre Noll8c6ac8682009-06-18 08:48:06 +10002219 if (mddev->recovery_cp != MaxSector)
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002220 printk(KERN_NOTICE "md/raid1:%s: not clean"
Andre Noll8c6ac8682009-06-18 08:48:06 +10002221 " -- starting background reconstruction\n",
2222 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 printk(KERN_INFO
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002224 "md/raid1:%s: active with %d out of %d mirrors\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 mdname(mddev), mddev->raid_disks - mddev->degraded,
2226 mddev->raid_disks);
NeilBrown709ae482009-12-14 12:49:51 +11002227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 /*
2229 * Ok, everything is just fine now
2230 */
NeilBrown709ae482009-12-14 12:49:51 +11002231 mddev->thread = conf->thread;
2232 conf->thread = NULL;
2233 mddev->private = conf;
2234
Dan Williams1f403622009-03-31 14:59:03 +11002235 md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Jonathan Brassow1ed72422011-06-07 17:50:35 -05002237 if (mddev->queue) {
2238 mddev->queue->backing_dev_info.congested_fn = raid1_congested;
2239 mddev->queue->backing_dev_info.congested_data = mddev;
2240 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01002241 return md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
2244static int stop(mddev_t *mddev)
2245{
NeilBrown070ec552009-06-16 16:54:21 +10002246 conf_t *conf = mddev->private;
NeilBrown4b6d2872005-09-09 16:23:47 -07002247 struct bitmap *bitmap = mddev->bitmap;
NeilBrown4b6d2872005-09-09 16:23:47 -07002248
2249 /* wait for behind writes to complete */
NeilBrowne5551902010-03-31 11:21:44 +11002250 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
NeilBrown9dd1e2f2010-05-03 14:30:35 +10002251 printk(KERN_INFO "md/raid1:%s: behind writes in progress - waiting to stop.\n",
2252 mdname(mddev));
NeilBrown4b6d2872005-09-09 16:23:47 -07002253 /* need to kick something here to make sure I/O goes? */
NeilBrowne5551902010-03-31 11:21:44 +11002254 wait_event(bitmap->behind_wait,
2255 atomic_read(&bitmap->behind_writes) == 0);
NeilBrown4b6d2872005-09-09 16:23:47 -07002256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
NeilBrown409c57f2009-03-31 14:39:39 +11002258 raise_barrier(conf);
2259 lower_barrier(conf);
2260
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 md_unregister_thread(mddev->thread);
2262 mddev->thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 if (conf->r1bio_pool)
2264 mempool_destroy(conf->r1bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002265 kfree(conf->mirrors);
2266 kfree(conf->poolinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 kfree(conf);
2268 mddev->private = NULL;
2269 return 0;
2270}
2271
2272static int raid1_resize(mddev_t *mddev, sector_t sectors)
2273{
2274 /* no resync is happening, and there is enough space
2275 * on all devices, so we can resize.
2276 * We need to make sure resync covers any new space.
2277 * If the array is shrinking we should possibly wait until
2278 * any io in the removed space completes, but it hardly seems
2279 * worth it.
2280 */
Dan Williams1f403622009-03-31 14:59:03 +11002281 md_set_array_sectors(mddev, raid1_size(mddev, sectors, 0));
Dan Williamsb522adc2009-03-31 15:00:31 +11002282 if (mddev->array_sectors > raid1_size(mddev, sectors, 0))
2283 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10002284 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10002285 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11002286 if (sectors > mddev->dev_sectors &&
NeilBrownb0986362011-05-11 15:52:21 +10002287 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11002288 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2290 }
Dan Williamsb522adc2009-03-31 15:00:31 +11002291 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07002292 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 return 0;
2294}
2295
NeilBrown63c70c42006-03-27 01:18:13 -08002296static int raid1_reshape(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
2298 /* We need to:
2299 * 1/ resize the r1bio_pool
2300 * 2/ resize conf->mirrors
2301 *
2302 * We allocate a new r1bio_pool if we can.
2303 * Then raise a device barrier and wait until all IO stops.
2304 * Then resize conf->mirrors and swap in the new r1bio pool.
NeilBrown6ea9c072005-06-21 17:17:09 -07002305 *
2306 * At the same time, we "pack" the devices so that all the missing
2307 * devices have the higher raid_disk numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 */
2309 mempool_t *newpool, *oldpool;
2310 struct pool_info *newpoolinfo;
2311 mirror_info_t *newmirrors;
NeilBrown070ec552009-06-16 16:54:21 +10002312 conf_t *conf = mddev->private;
NeilBrown63c70c42006-03-27 01:18:13 -08002313 int cnt, raid_disks;
NeilBrownc04be0a2006-10-03 01:15:53 -07002314 unsigned long flags;
Dan Williamsb5470dc2008-06-27 21:44:04 -07002315 int d, d2, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
NeilBrown63c70c42006-03-27 01:18:13 -08002317 /* Cannot change chunk_size, layout, or level */
Andre Noll664e7c42009-06-18 08:45:27 +10002318 if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
NeilBrown63c70c42006-03-27 01:18:13 -08002319 mddev->layout != mddev->new_layout ||
2320 mddev->level != mddev->new_level) {
Andre Noll664e7c42009-06-18 08:45:27 +10002321 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown63c70c42006-03-27 01:18:13 -08002322 mddev->new_layout = mddev->layout;
2323 mddev->new_level = mddev->level;
2324 return -EINVAL;
2325 }
2326
Dan Williamsb5470dc2008-06-27 21:44:04 -07002327 err = md_allow_write(mddev);
2328 if (err)
2329 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08002330
NeilBrown63c70c42006-03-27 01:18:13 -08002331 raid_disks = mddev->raid_disks + mddev->delta_disks;
2332
NeilBrown6ea9c072005-06-21 17:17:09 -07002333 if (raid_disks < conf->raid_disks) {
2334 cnt=0;
2335 for (d= 0; d < conf->raid_disks; d++)
2336 if (conf->mirrors[d].rdev)
2337 cnt++;
2338 if (cnt > raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 return -EBUSY;
NeilBrown6ea9c072005-06-21 17:17:09 -07002340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
2342 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
2343 if (!newpoolinfo)
2344 return -ENOMEM;
2345 newpoolinfo->mddev = mddev;
2346 newpoolinfo->raid_disks = raid_disks;
2347
2348 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
2349 r1bio_pool_free, newpoolinfo);
2350 if (!newpool) {
2351 kfree(newpoolinfo);
2352 return -ENOMEM;
2353 }
NeilBrown9ffae0c2006-01-06 00:20:32 -08002354 newmirrors = kzalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 if (!newmirrors) {
2356 kfree(newpoolinfo);
2357 mempool_destroy(newpool);
2358 return -ENOMEM;
2359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
NeilBrown17999be2006-01-06 00:20:12 -08002361 raise_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
2363 /* ok, everything is stopped */
2364 oldpool = conf->r1bio_pool;
2365 conf->r1bio_pool = newpool;
NeilBrown6ea9c072005-06-21 17:17:09 -07002366
NeilBrowna88aa782007-08-22 14:01:53 -07002367 for (d = d2 = 0; d < conf->raid_disks; d++) {
2368 mdk_rdev_t *rdev = conf->mirrors[d].rdev;
2369 if (rdev && rdev->raid_disk != d2) {
Namhyung Kim36fad852011-07-27 11:00:36 +10002370 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna88aa782007-08-22 14:01:53 -07002371 rdev->raid_disk = d2;
Namhyung Kim36fad852011-07-27 11:00:36 +10002372 sysfs_unlink_rdev(mddev, rdev);
2373 if (sysfs_link_rdev(mddev, rdev))
NeilBrowna88aa782007-08-22 14:01:53 -07002374 printk(KERN_WARNING
Namhyung Kim36fad852011-07-27 11:00:36 +10002375 "md/raid1:%s: cannot register rd%d\n",
2376 mdname(mddev), rdev->raid_disk);
NeilBrown6ea9c072005-06-21 17:17:09 -07002377 }
NeilBrowna88aa782007-08-22 14:01:53 -07002378 if (rdev)
2379 newmirrors[d2++].rdev = rdev;
2380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 kfree(conf->mirrors);
2382 conf->mirrors = newmirrors;
2383 kfree(conf->poolinfo);
2384 conf->poolinfo = newpoolinfo;
2385
NeilBrownc04be0a2006-10-03 01:15:53 -07002386 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 mddev->degraded += (raid_disks - conf->raid_disks);
NeilBrownc04be0a2006-10-03 01:15:53 -07002388 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 conf->raid_disks = mddev->raid_disks = raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08002390 mddev->delta_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
NeilBrown6ea9c072005-06-21 17:17:09 -07002392 conf->last_used = 0; /* just make sure it is in-range */
NeilBrown17999be2006-01-06 00:20:12 -08002393 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
2395 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2396 md_wakeup_thread(mddev->thread);
2397
2398 mempool_destroy(oldpool);
2399 return 0;
2400}
2401
NeilBrown500af872005-09-09 16:23:58 -07002402static void raid1_quiesce(mddev_t *mddev, int state)
NeilBrown36fa3062005-09-09 16:23:45 -07002403{
NeilBrown070ec552009-06-16 16:54:21 +10002404 conf_t *conf = mddev->private;
NeilBrown36fa3062005-09-09 16:23:45 -07002405
2406 switch(state) {
NeilBrown6eef4b22009-12-14 12:49:51 +11002407 case 2: /* wake for suspend */
2408 wake_up(&conf->wait_barrier);
2409 break;
NeilBrown9e6603d2005-09-09 16:23:48 -07002410 case 1:
NeilBrown17999be2006-01-06 00:20:12 -08002411 raise_barrier(conf);
NeilBrown36fa3062005-09-09 16:23:45 -07002412 break;
NeilBrown9e6603d2005-09-09 16:23:48 -07002413 case 0:
NeilBrown17999be2006-01-06 00:20:12 -08002414 lower_barrier(conf);
NeilBrown36fa3062005-09-09 16:23:45 -07002415 break;
2416 }
NeilBrown36fa3062005-09-09 16:23:45 -07002417}
2418
NeilBrown709ae482009-12-14 12:49:51 +11002419static void *raid1_takeover(mddev_t *mddev)
2420{
2421 /* raid1 can take over:
2422 * raid5 with 2 devices, any layout or chunk size
2423 */
2424 if (mddev->level == 5 && mddev->raid_disks == 2) {
2425 conf_t *conf;
2426 mddev->new_level = 1;
2427 mddev->new_layout = 0;
2428 mddev->new_chunk_sectors = 0;
2429 conf = setup_conf(mddev);
2430 if (!IS_ERR(conf))
2431 conf->barrier = 1;
2432 return conf;
2433 }
2434 return ERR_PTR(-EINVAL);
2435}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
NeilBrown2604b702006-01-06 00:20:36 -08002437static struct mdk_personality raid1_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
2439 .name = "raid1",
NeilBrown2604b702006-01-06 00:20:36 -08002440 .level = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 .owner = THIS_MODULE,
2442 .make_request = make_request,
2443 .run = run,
2444 .stop = stop,
2445 .status = status,
2446 .error_handler = error,
2447 .hot_add_disk = raid1_add_disk,
2448 .hot_remove_disk= raid1_remove_disk,
2449 .spare_active = raid1_spare_active,
2450 .sync_request = sync_request,
2451 .resize = raid1_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07002452 .size = raid1_size,
NeilBrown63c70c42006-03-27 01:18:13 -08002453 .check_reshape = raid1_reshape,
NeilBrown36fa3062005-09-09 16:23:45 -07002454 .quiesce = raid1_quiesce,
NeilBrown709ae482009-12-14 12:49:51 +11002455 .takeover = raid1_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456};
2457
2458static int __init raid_init(void)
2459{
NeilBrown2604b702006-01-06 00:20:36 -08002460 return register_md_personality(&raid1_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461}
2462
2463static void raid_exit(void)
2464{
NeilBrown2604b702006-01-06 00:20:36 -08002465 unregister_md_personality(&raid1_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466}
2467
2468module_init(raid_init);
2469module_exit(raid_exit);
2470MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11002471MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472MODULE_ALIAS("md-personality-3"); /* RAID1 */
NeilBrownd9d166c2006-01-06 00:20:51 -08002473MODULE_ALIAS("md-raid1");
NeilBrown2604b702006-01-06 00:20:36 -08002474MODULE_ALIAS("md-level-1");