blob: 813f52464f8aeeb0675c1c2edb0bdbe5beb51388 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid10.c : Multiple Devices driver for Linux
3 *
4 * Copyright (C) 2000-2004 Neil Brown
5 *
6 * RAID-10 support for md.
7 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008 * Base on code in raid1.c. See raid1.c for further copyright information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110022#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110023#include <linux/blkdev.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040024#include <linux/module.h>
NeilBrownbff61972009-03-31 14:33:13 +110025#include <linux/seq_file.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100026#include <linux/ratelimit.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110027#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110028#include "raid10.h"
Trela, Maciejdab8b292010-03-08 16:02:45 +110029#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110030#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/*
33 * RAID10 provides a combination of RAID0 and RAID1 functionality.
34 * The layout of data is defined by
35 * chunk_size
36 * raid_disks
37 * near_copies (stored in low byte of layout)
38 * far_copies (stored in second byte of layout)
NeilBrownc93983b2006-06-26 00:27:41 -070039 * far_offset (stored in bit 16 of layout )
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 *
41 * The data to be stored is divided into chunks using chunksize.
42 * Each device is divided into far_copies sections.
43 * In each section, chunks are laid out in a style similar to raid0, but
44 * near_copies copies of each chunk is stored (each on a different drive).
45 * The starting device for each section is offset near_copies from the starting
46 * device of the previous section.
NeilBrownc93983b2006-06-26 00:27:41 -070047 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * drive.
49 * near_copies and far_copies must be at least one, and their product is at most
50 * raid_disks.
NeilBrownc93983b2006-06-26 00:27:41 -070051 *
52 * If far_offset is true, then the far_copies are handled a bit differently.
53 * The copies are still in different stripes, but instead of be very far apart
54 * on disk, there are adjacent stripes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 */
56
57/*
58 * Number of guaranteed r10bios in case of extreme VM load:
59 */
60#define NR_RAID10_BIOS 256
61
NeilBrown34db0cd2011-10-11 16:50:01 +110062/* When there are this many requests queue to be written by
63 * the raid10 thread, we become 'congested' to provide back-pressure
64 * for writeback.
65 */
66static int max_queued_requests = 1024;
67
NeilBrowne879a872011-10-11 16:49:02 +110068static void allow_barrier(struct r10conf *conf);
69static void lower_barrier(struct r10conf *conf);
NeilBrown0a27ec92006-01-06 00:20:13 -080070
Al Virodd0fc662005-10-07 07:46:04 +010071static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
NeilBrowne879a872011-10-11 16:49:02 +110073 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +110074 int size = offsetof(struct r10bio, devs[conf->copies]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
NeilBrown69335ef2011-12-23 10:17:54 +110076 /* allocate a r10bio with room for raid_disks entries in the
77 * bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +010078 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
81static void r10bio_pool_free(void *r10_bio, void *data)
82{
83 kfree(r10_bio);
84}
85
NeilBrown0310fa22008-08-05 15:54:14 +100086/* Maximum size of each resync request */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#define RESYNC_BLOCK_SIZE (64*1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
NeilBrown0310fa22008-08-05 15:54:14 +100089/* amount of memory to reserve for resync requests */
90#define RESYNC_WINDOW (1024*1024)
91/* maximum number of concurrent requests, memory permitting */
92#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/*
95 * When performing a resync, we need to read and compare, so
96 * we need as many pages are there are copies.
97 * When performing a recovery, we need 2 bios, one for read,
98 * one for write (we recover only one drive per r10buf)
99 *
100 */
Al Virodd0fc662005-10-07 07:46:04 +0100101static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
NeilBrowne879a872011-10-11 16:49:02 +1100103 struct r10conf *conf = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct page *page;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100105 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct bio *bio;
107 int i, j;
108 int nalloc;
109
110 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100111 if (!r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
115 nalloc = conf->copies; /* resync */
116 else
117 nalloc = 2; /* recovery */
118
119 /*
120 * Allocate bios.
121 */
122 for (j = nalloc ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +1100123 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (!bio)
125 goto out_free_bio;
126 r10_bio->devs[j].bio = bio;
NeilBrown69335ef2011-12-23 10:17:54 +1100127 if (!conf->have_replacement)
128 continue;
129 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
130 if (!bio)
131 goto out_free_bio;
132 r10_bio->devs[j].repl_bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
134 /*
135 * Allocate RESYNC_PAGES data pages and attach them
136 * where needed.
137 */
138 for (j = 0 ; j < nalloc; j++) {
NeilBrown69335ef2011-12-23 10:17:54 +1100139 struct bio *rbio = r10_bio->devs[j].repl_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 bio = r10_bio->devs[j].bio;
141 for (i = 0; i < RESYNC_PAGES; i++) {
Namhyung Kimc65060a2011-07-18 17:38:49 +1000142 if (j == 1 && !test_bit(MD_RECOVERY_SYNC,
143 &conf->mddev->recovery)) {
144 /* we can share bv_page's during recovery */
145 struct bio *rbio = r10_bio->devs[0].bio;
146 page = rbio->bi_io_vec[i].bv_page;
147 get_page(page);
148 } else
149 page = alloc_page(gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (unlikely(!page))
151 goto out_free_pages;
152
153 bio->bi_io_vec[i].bv_page = page;
NeilBrown69335ef2011-12-23 10:17:54 +1100154 if (rbio)
155 rbio->bi_io_vec[i].bv_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157 }
158
159 return r10_bio;
160
161out_free_pages:
162 for ( ; i > 0 ; i--)
NeilBrown1345b1d2006-01-06 00:20:40 -0800163 safe_put_page(bio->bi_io_vec[i-1].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 while (j--)
165 for (i = 0; i < RESYNC_PAGES ; i++)
NeilBrown1345b1d2006-01-06 00:20:40 -0800166 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 j = -1;
168out_free_bio:
NeilBrown69335ef2011-12-23 10:17:54 +1100169 while (++j < nalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 bio_put(r10_bio->devs[j].bio);
NeilBrown69335ef2011-12-23 10:17:54 +1100171 if (r10_bio->devs[j].repl_bio)
172 bio_put(r10_bio->devs[j].repl_bio);
173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 r10bio_pool_free(r10_bio, conf);
175 return NULL;
176}
177
178static void r10buf_pool_free(void *__r10_bio, void *data)
179{
180 int i;
NeilBrowne879a872011-10-11 16:49:02 +1100181 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100182 struct r10bio *r10bio = __r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 int j;
184
185 for (j=0; j < conf->copies; j++) {
186 struct bio *bio = r10bio->devs[j].bio;
187 if (bio) {
188 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown1345b1d2006-01-06 00:20:40 -0800189 safe_put_page(bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 bio->bi_io_vec[i].bv_page = NULL;
191 }
192 bio_put(bio);
193 }
NeilBrown69335ef2011-12-23 10:17:54 +1100194 bio = r10bio->devs[j].repl_bio;
195 if (bio)
196 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198 r10bio_pool_free(r10bio, conf);
199}
200
NeilBrowne879a872011-10-11 16:49:02 +1100201static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 int i;
204
205 for (i = 0; i < conf->copies; i++) {
206 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown749c55e2011-07-28 11:39:24 +1000207 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 bio_put(*bio);
209 *bio = NULL;
NeilBrown69335ef2011-12-23 10:17:54 +1100210 bio = &r10_bio->devs[i].repl_bio;
211 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
212 bio_put(*bio);
213 *bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215}
216
NeilBrown9f2c9d12011-10-11 16:48:43 +1100217static void free_r10bio(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
NeilBrowne879a872011-10-11 16:49:02 +1100219 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 put_all_bios(conf, r10_bio);
222 mempool_free(r10_bio, conf->r10bio_pool);
223}
224
NeilBrown9f2c9d12011-10-11 16:48:43 +1100225static void put_buf(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
NeilBrowne879a872011-10-11 16:49:02 +1100227 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 mempool_free(r10_bio, conf->r10buf_pool);
230
NeilBrown0a27ec92006-01-06 00:20:13 -0800231 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
NeilBrown9f2c9d12011-10-11 16:48:43 +1100234static void reschedule_retry(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +1100237 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +1100238 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 spin_lock_irqsave(&conf->device_lock, flags);
241 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800242 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 spin_unlock_irqrestore(&conf->device_lock, flags);
244
Arthur Jones388667b2008-07-25 12:03:38 -0700245 /* wake up frozen array... */
246 wake_up(&conf->wait_barrier);
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 md_wakeup_thread(mddev->thread);
249}
250
251/*
252 * raid_end_bio_io() is called when we have finished servicing a mirrored
253 * operation and are ready to return a success/failure code to the buffer
254 * cache layer.
255 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100256static void raid_end_bio_io(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 struct bio *bio = r10_bio->master_bio;
NeilBrown856e08e2011-07-28 11:39:23 +1000259 int done;
NeilBrowne879a872011-10-11 16:49:02 +1100260 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
NeilBrown856e08e2011-07-28 11:39:23 +1000262 if (bio->bi_phys_segments) {
263 unsigned long flags;
264 spin_lock_irqsave(&conf->device_lock, flags);
265 bio->bi_phys_segments--;
266 done = (bio->bi_phys_segments == 0);
267 spin_unlock_irqrestore(&conf->device_lock, flags);
268 } else
269 done = 1;
270 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
271 clear_bit(BIO_UPTODATE, &bio->bi_flags);
272 if (done) {
273 bio_endio(bio, 0);
274 /*
275 * Wake up any possible resync thread that waits for the device
276 * to go idle.
277 */
278 allow_barrier(conf);
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 free_r10bio(r10_bio);
281}
282
283/*
284 * Update disk head position estimator based on IRQ completion info.
285 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100286static inline void update_head_pos(int slot, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
NeilBrowne879a872011-10-11 16:49:02 +1100288 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
291 r10_bio->devs[slot].addr + (r10_bio->sectors);
292}
293
Namhyung Kim778ca012011-07-18 17:38:47 +1000294/*
295 * Find the disk number which triggered given bio
296 */
NeilBrowne879a872011-10-11 16:49:02 +1100297static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
NeilBrown69335ef2011-12-23 10:17:54 +1100298 struct bio *bio, int *slotp, int *replp)
Namhyung Kim778ca012011-07-18 17:38:47 +1000299{
300 int slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100301 int repl = 0;
Namhyung Kim778ca012011-07-18 17:38:47 +1000302
NeilBrown69335ef2011-12-23 10:17:54 +1100303 for (slot = 0; slot < conf->copies; slot++) {
Namhyung Kim778ca012011-07-18 17:38:47 +1000304 if (r10_bio->devs[slot].bio == bio)
305 break;
NeilBrown69335ef2011-12-23 10:17:54 +1100306 if (r10_bio->devs[slot].repl_bio == bio) {
307 repl = 1;
308 break;
309 }
310 }
Namhyung Kim778ca012011-07-18 17:38:47 +1000311
312 BUG_ON(slot == conf->copies);
313 update_head_pos(slot, r10_bio);
314
NeilBrown749c55e2011-07-28 11:39:24 +1000315 if (slotp)
316 *slotp = slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100317 if (replp)
318 *replp = repl;
Namhyung Kim778ca012011-07-18 17:38:47 +1000319 return r10_bio->devs[slot].devnum;
320}
321
NeilBrown6712ecf2007-09-27 12:47:43 +0200322static void raid10_end_read_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +1100325 struct r10bio *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 int slot, dev;
NeilBrownabbf0982011-12-23 10:17:54 +1100327 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +1100328 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 slot = r10_bio->read_slot;
332 dev = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100333 rdev = r10_bio->devs[slot].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 /*
335 * this branch is our 'one mirror IO has finished' event handler:
336 */
NeilBrown4443ae12006-01-06 00:20:28 -0800337 update_head_pos(slot, r10_bio);
338
339 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 /*
341 * Set R10BIO_Uptodate in our master bio, so that
342 * we will return a good error code to the higher
343 * levels even if IO on some other mirrored buffer fails.
344 *
345 * The 'master' represents the composite IO operation to
346 * user-side. So if something waits for IO, then it will
347 * wait for the 'master' bio.
348 */
349 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 raid_end_bio_io(r10_bio);
NeilBrownabbf0982011-12-23 10:17:54 +1100351 rdev_dec_pending(rdev, conf->mddev);
NeilBrown4443ae12006-01-06 00:20:28 -0800352 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 /*
NeilBrown7c4e06f2011-05-11 14:53:17 +1000354 * oops, read error - keep the refcount on the rdev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 */
356 char b[BDEVNAME_SIZE];
Christian Dietrich8bda4702011-07-27 11:00:36 +1000357 printk_ratelimited(KERN_ERR
358 "md/raid10:%s: %s: rescheduling sector %llu\n",
359 mdname(conf->mddev),
NeilBrownabbf0982011-12-23 10:17:54 +1100360 bdevname(rdev->bdev, b),
Christian Dietrich8bda4702011-07-27 11:00:36 +1000361 (unsigned long long)r10_bio->sector);
NeilBrown856e08e2011-07-28 11:39:23 +1000362 set_bit(R10BIO_ReadError, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 reschedule_retry(r10_bio);
364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
NeilBrown9f2c9d12011-10-11 16:48:43 +1100367static void close_write(struct r10bio *r10_bio)
NeilBrownbd870a12011-07-28 11:39:24 +1000368{
369 /* clear the bitmap if all writes complete successfully */
370 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
371 r10_bio->sectors,
372 !test_bit(R10BIO_Degraded, &r10_bio->state),
373 0);
374 md_write_end(r10_bio->mddev);
375}
376
NeilBrown9f2c9d12011-10-11 16:48:43 +1100377static void one_write_done(struct r10bio *r10_bio)
NeilBrown19d5f832011-09-10 17:21:17 +1000378{
379 if (atomic_dec_and_test(&r10_bio->remaining)) {
380 if (test_bit(R10BIO_WriteError, &r10_bio->state))
381 reschedule_retry(r10_bio);
382 else {
383 close_write(r10_bio);
384 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
385 reschedule_retry(r10_bio);
386 else
387 raid_end_bio_io(r10_bio);
388 }
389 }
390}
391
NeilBrown6712ecf2007-09-27 12:47:43 +0200392static void raid10_end_write_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +1100395 struct r10bio *r10_bio = bio->bi_private;
Namhyung Kim778ca012011-07-18 17:38:47 +1000396 int dev;
NeilBrown749c55e2011-07-28 11:39:24 +1000397 int dec_rdev = 1;
NeilBrowne879a872011-10-11 16:49:02 +1100398 struct r10conf *conf = r10_bio->mddev->private;
NeilBrown475b0322011-12-23 10:17:55 +1100399 int slot, repl;
400 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
NeilBrown475b0322011-12-23 10:17:55 +1100402 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
NeilBrown475b0322011-12-23 10:17:55 +1100404 if (repl)
405 rdev = conf->mirrors[dev].replacement;
406 else
407 rdev = conf->mirrors[dev].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /*
409 * this branch is our 'one mirror IO has finished' event handler:
410 */
NeilBrown6cce3b22006-01-06 00:20:16 -0800411 if (!uptodate) {
NeilBrown475b0322011-12-23 10:17:55 +1100412 if (repl)
413 /* Never record new bad blocks to replacement,
414 * just fail it.
415 */
416 md_error(rdev->mddev, rdev);
417 else {
418 set_bit(WriteErrorSeen, &rdev->flags);
419 set_bit(R10BIO_WriteError, &r10_bio->state);
420 dec_rdev = 0;
421 }
NeilBrown749c55e2011-07-28 11:39:24 +1000422 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 /*
424 * Set R10BIO_Uptodate in our master bio, so that
425 * we will return a good error code for to the higher
426 * levels even if IO on some other mirrored buffer fails.
427 *
428 * The 'master' represents the composite IO operation to
429 * user-side. So if something waits for IO, then it will
430 * wait for the 'master' bio.
431 */
NeilBrown749c55e2011-07-28 11:39:24 +1000432 sector_t first_bad;
433 int bad_sectors;
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 set_bit(R10BIO_Uptodate, &r10_bio->state);
436
NeilBrown749c55e2011-07-28 11:39:24 +1000437 /* Maybe we can clear some bad blocks. */
NeilBrown475b0322011-12-23 10:17:55 +1100438 if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +1000439 r10_bio->devs[slot].addr,
440 r10_bio->sectors,
441 &first_bad, &bad_sectors)) {
442 bio_put(bio);
NeilBrown475b0322011-12-23 10:17:55 +1100443 if (repl)
444 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
445 else
446 r10_bio->devs[slot].bio = IO_MADE_GOOD;
NeilBrown749c55e2011-07-28 11:39:24 +1000447 dec_rdev = 0;
448 set_bit(R10BIO_MadeGood, &r10_bio->state);
449 }
450 }
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /*
453 *
454 * Let's see if all mirrored write operations have finished
455 * already.
456 */
NeilBrown19d5f832011-09-10 17:21:17 +1000457 one_write_done(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +1000458 if (dec_rdev)
459 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462/*
463 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300464 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 * parameters: near_copies and far_copies.
466 * near_copies * far_copies must be <= raid_disks.
467 * Normally one of these will be 1.
468 * If both are 1, we get raid0.
469 * If near_copies == raid_disks, we get raid1.
470 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300471 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 * first chunk, followed by near_copies copies of the next chunk and
473 * so on.
474 * If far_copies > 1, then after 1/far_copies of the array has been assigned
475 * as described above, we start again with a device offset of near_copies.
476 * So we effectively have another copy of the whole array further down all
477 * the drives, but with blocks on different drives.
478 * With this layout, and block is never stored twice on the one device.
479 *
480 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700481 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 *
483 * raid10_find_virt does the reverse mapping, from a device and a
484 * sector offset to a virtual address
485 */
486
NeilBrowne879a872011-10-11 16:49:02 +1100487static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 int n,f;
490 sector_t sector;
491 sector_t chunk;
492 sector_t stripe;
493 int dev;
494
495 int slot = 0;
496
497 /* now calculate first sector/dev */
498 chunk = r10bio->sector >> conf->chunk_shift;
499 sector = r10bio->sector & conf->chunk_mask;
500
501 chunk *= conf->near_copies;
502 stripe = chunk;
503 dev = sector_div(stripe, conf->raid_disks);
NeilBrownc93983b2006-06-26 00:27:41 -0700504 if (conf->far_offset)
505 stripe *= conf->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 sector += stripe << conf->chunk_shift;
508
509 /* and calculate all the others */
510 for (n=0; n < conf->near_copies; n++) {
511 int d = dev;
512 sector_t s = sector;
513 r10bio->devs[slot].addr = sector;
514 r10bio->devs[slot].devnum = d;
515 slot++;
516
517 for (f = 1; f < conf->far_copies; f++) {
518 d += conf->near_copies;
519 if (d >= conf->raid_disks)
520 d -= conf->raid_disks;
521 s += conf->stride;
522 r10bio->devs[slot].devnum = d;
523 r10bio->devs[slot].addr = s;
524 slot++;
525 }
526 dev++;
527 if (dev >= conf->raid_disks) {
528 dev = 0;
529 sector += (conf->chunk_mask + 1);
530 }
531 }
532 BUG_ON(slot != conf->copies);
533}
534
NeilBrowne879a872011-10-11 16:49:02 +1100535static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 sector_t offset, chunk, vchunk;
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 offset = sector & conf->chunk_mask;
NeilBrownc93983b2006-06-26 00:27:41 -0700540 if (conf->far_offset) {
541 int fc;
542 chunk = sector >> conf->chunk_shift;
543 fc = sector_div(chunk, conf->far_copies);
544 dev -= fc * conf->near_copies;
545 if (dev < 0)
546 dev += conf->raid_disks;
547 } else {
NeilBrown64a742b2007-02-28 20:11:18 -0800548 while (sector >= conf->stride) {
NeilBrownc93983b2006-06-26 00:27:41 -0700549 sector -= conf->stride;
550 if (dev < conf->near_copies)
551 dev += conf->raid_disks - conf->near_copies;
552 else
553 dev -= conf->near_copies;
554 }
555 chunk = sector >> conf->chunk_shift;
556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 vchunk = chunk * conf->raid_disks + dev;
558 sector_div(vchunk, conf->near_copies);
559 return (vchunk << conf->chunk_shift) + offset;
560}
561
562/**
563 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
564 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200565 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 * @biovec: the request that could be merged to it.
567 *
568 * Return amount of bytes we can accept at this offset
569 * If near_copies == raid_disk, there are no striping issues,
570 * but in that case, the function isn't called at all.
571 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200572static int raid10_mergeable_bvec(struct request_queue *q,
573 struct bvec_merge_data *bvm,
574 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
NeilBrownfd01b882011-10-11 16:47:53 +1100576 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200577 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +1000579 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200580 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
583 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200584 if (max <= biovec->bv_len && bio_sectors == 0)
585 return biovec->bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 else
587 return max;
588}
589
590/*
591 * This routine returns the disk from which the requested read should
592 * be done. There is a per-array 'next expected sequential IO' sector
593 * number - if this matches on the next IO then we use the last disk.
594 * There is also a per-disk 'last know head position' sector that is
595 * maintained from IRQ contexts, both the normal and the resync IO
596 * completion handlers update this position correctly. If there is no
597 * perfect sequential match then we pick the disk whose head is closest.
598 *
599 * If there are 2 mirrors in the same 2 devices, performance degrades
600 * because position is mirror, not device based.
601 *
602 * The rdev for the device selected will have nr_pending incremented.
603 */
604
605/*
606 * FIXME: possibly should rethink readbalancing and do it differently
607 * depending on near_copies / far_copies geometry.
608 */
NeilBrown96c3fd12011-12-23 10:17:54 +1100609static struct md_rdev *read_balance(struct r10conf *conf,
610 struct r10bio *r10_bio,
611 int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000613 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000614 int disk, slot;
NeilBrown856e08e2011-07-28 11:39:23 +1000615 int sectors = r10_bio->sectors;
616 int best_good_sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000617 sector_t new_distance, best_dist;
NeilBrownabbf0982011-12-23 10:17:54 +1100618 struct md_rdev *rdev, *best_rdev;
NeilBrown56d99122011-05-11 14:27:03 +1000619 int do_balance;
620 int best_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 raid10_find_phys(conf, r10_bio);
623 rcu_read_lock();
NeilBrown56d99122011-05-11 14:27:03 +1000624retry:
NeilBrown856e08e2011-07-28 11:39:23 +1000625 sectors = r10_bio->sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000626 best_slot = -1;
NeilBrownabbf0982011-12-23 10:17:54 +1100627 best_rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000628 best_dist = MaxSector;
NeilBrown856e08e2011-07-28 11:39:23 +1000629 best_good_sectors = 0;
NeilBrown56d99122011-05-11 14:27:03 +1000630 do_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /*
632 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800633 * device if no resync is going on (recovery is ok), or below
634 * the resync window. We take the first readable disk when
635 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 */
637 if (conf->mddev->recovery_cp < MaxSector
NeilBrown56d99122011-05-11 14:27:03 +1000638 && (this_sector + sectors >= conf->next_resync))
639 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
NeilBrown56d99122011-05-11 14:27:03 +1000641 for (slot = 0; slot < conf->copies ; slot++) {
NeilBrown856e08e2011-07-28 11:39:23 +1000642 sector_t first_bad;
643 int bad_sectors;
644 sector_t dev_sector;
645
NeilBrown56d99122011-05-11 14:27:03 +1000646 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000648 disk = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100649 rdev = rcu_dereference(conf->mirrors[disk].replacement);
650 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
651 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
652 rdev = rcu_dereference(conf->mirrors[disk].rdev);
NeilBrown56d99122011-05-11 14:27:03 +1000653 if (rdev == NULL)
654 continue;
NeilBrownabbf0982011-12-23 10:17:54 +1100655 if (test_bit(Faulty, &rdev->flags))
656 continue;
657 if (!test_bit(In_sync, &rdev->flags) &&
658 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
NeilBrown56d99122011-05-11 14:27:03 +1000659 continue;
660
NeilBrown856e08e2011-07-28 11:39:23 +1000661 dev_sector = r10_bio->devs[slot].addr;
662 if (is_badblock(rdev, dev_sector, sectors,
663 &first_bad, &bad_sectors)) {
664 if (best_dist < MaxSector)
665 /* Already have a better slot */
666 continue;
667 if (first_bad <= dev_sector) {
668 /* Cannot read here. If this is the
669 * 'primary' device, then we must not read
670 * beyond 'bad_sectors' from another device.
671 */
672 bad_sectors -= (dev_sector - first_bad);
673 if (!do_balance && sectors > bad_sectors)
674 sectors = bad_sectors;
675 if (best_good_sectors > sectors)
676 best_good_sectors = sectors;
677 } else {
678 sector_t good_sectors =
679 first_bad - dev_sector;
680 if (good_sectors > best_good_sectors) {
681 best_good_sectors = good_sectors;
682 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100683 best_rdev = rdev;
NeilBrown856e08e2011-07-28 11:39:23 +1000684 }
685 if (!do_balance)
686 /* Must read from here */
687 break;
688 }
689 continue;
690 } else
691 best_good_sectors = sectors;
692
NeilBrown56d99122011-05-11 14:27:03 +1000693 if (!do_balance)
694 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
NeilBrown22dfdf52005-11-28 13:44:09 -0800696 /* This optimisation is debatable, and completely destroys
697 * sequential read speed for 'far copies' arrays. So only
698 * keep it for 'near' arrays, and review those later.
699 */
NeilBrown56d99122011-05-11 14:27:03 +1000700 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 break;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800702
703 /* for far > 1 always use the lowest address */
704 if (conf->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000705 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800706 else
NeilBrown56d99122011-05-11 14:27:03 +1000707 new_distance = abs(r10_bio->devs[slot].addr -
708 conf->mirrors[disk].head_position);
709 if (new_distance < best_dist) {
710 best_dist = new_distance;
711 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100712 best_rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714 }
NeilBrownabbf0982011-12-23 10:17:54 +1100715 if (slot >= conf->copies) {
NeilBrown56d99122011-05-11 14:27:03 +1000716 slot = best_slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100717 rdev = best_rdev;
718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
NeilBrown56d99122011-05-11 14:27:03 +1000720 if (slot >= 0) {
NeilBrown56d99122011-05-11 14:27:03 +1000721 atomic_inc(&rdev->nr_pending);
722 if (test_bit(Faulty, &rdev->flags)) {
723 /* Cannot risk returning a device that failed
724 * before we inc'ed nr_pending
725 */
726 rdev_dec_pending(rdev, conf->mddev);
727 goto retry;
728 }
729 r10_bio->read_slot = slot;
730 } else
NeilBrown96c3fd12011-12-23 10:17:54 +1100731 rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 rcu_read_unlock();
NeilBrown856e08e2011-07-28 11:39:23 +1000733 *max_sectors = best_good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
NeilBrown96c3fd12011-12-23 10:17:54 +1100735 return rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
NeilBrown0d129222006-10-03 01:15:54 -0700738static int raid10_congested(void *data, int bits)
739{
NeilBrownfd01b882011-10-11 16:47:53 +1100740 struct mddev *mddev = data;
NeilBrowne879a872011-10-11 16:49:02 +1100741 struct r10conf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700742 int i, ret = 0;
743
NeilBrown34db0cd2011-10-11 16:50:01 +1100744 if ((bits & (1 << BDI_async_congested)) &&
745 conf->pending_count >= max_queued_requests)
746 return 1;
747
NeilBrown3fa841d2009-09-23 18:10:29 +1000748 if (mddev_congested(mddev, bits))
749 return 1;
NeilBrown0d129222006-10-03 01:15:54 -0700750 rcu_read_lock();
NeilBrown84707f32010-03-16 17:23:35 +1100751 for (i = 0; i < conf->raid_disks && ret == 0; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100752 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700753 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200754 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700755
756 ret |= bdi_congested(&q->backing_dev_info, bits);
757 }
758 }
759 rcu_read_unlock();
760 return ret;
761}
762
NeilBrowne879a872011-10-11 16:49:02 +1100763static void flush_pending_writes(struct r10conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800764{
765 /* Any writes that have been queued but are awaiting
766 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800767 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800768 spin_lock_irq(&conf->device_lock);
769
770 if (conf->pending_bio_list.head) {
771 struct bio *bio;
772 bio = bio_list_get(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +1100773 conf->pending_count = 0;
NeilBrowna35e63e2008-03-04 14:29:29 -0800774 spin_unlock_irq(&conf->device_lock);
775 /* flush any pending bitmap writes to disk
776 * before proceeding w/ I/O */
777 bitmap_unplug(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100778 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800779
780 while (bio) { /* submit pending writes */
781 struct bio *next = bio->bi_next;
782 bio->bi_next = NULL;
783 generic_make_request(bio);
784 bio = next;
785 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800786 } else
787 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800788}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100789
NeilBrown0a27ec92006-01-06 00:20:13 -0800790/* Barriers....
791 * Sometimes we need to suspend IO while we do something else,
792 * either some resync/recovery, or reconfigure the array.
793 * To do this we raise a 'barrier'.
794 * The 'barrier' is a counter that can be raised multiple times
795 * to count how many activities are happening which preclude
796 * normal IO.
797 * We can only raise the barrier if there is no pending IO.
798 * i.e. if nr_pending == 0.
799 * We choose only to raise the barrier if no-one is waiting for the
800 * barrier to go down. This means that as soon as an IO request
801 * is ready, no other operations which require a barrier will start
802 * until the IO request has had a chance.
803 *
804 * So: regular IO calls 'wait_barrier'. When that returns there
805 * is no backgroup IO happening, It must arrange to call
806 * allow_barrier when it has finished its IO.
807 * backgroup IO calls must call raise_barrier. Once that returns
808 * there is no normal IO happeing. It must arrange to call
809 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
NeilBrowne879a872011-10-11 16:49:02 +1100812static void raise_barrier(struct r10conf *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
NeilBrown6cce3b22006-01-06 00:20:16 -0800814 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
NeilBrown6cce3b22006-01-06 00:20:16 -0800817 /* Wait until no block IO is waiting (unless 'force') */
818 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrownc3b328a2011-04-18 18:25:43 +1000819 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800820
821 /* block any new IO from starting */
822 conf->barrier++;
823
NeilBrownc3b328a2011-04-18 18:25:43 +1000824 /* Now wait for all pending IO to complete */
NeilBrown0a27ec92006-01-06 00:20:13 -0800825 wait_event_lock_irq(conf->wait_barrier,
826 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
NeilBrownc3b328a2011-04-18 18:25:43 +1000827 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 spin_unlock_irq(&conf->resync_lock);
830}
831
NeilBrowne879a872011-10-11 16:49:02 +1100832static void lower_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800833{
834 unsigned long flags;
835 spin_lock_irqsave(&conf->resync_lock, flags);
836 conf->barrier--;
837 spin_unlock_irqrestore(&conf->resync_lock, flags);
838 wake_up(&conf->wait_barrier);
839}
840
NeilBrowne879a872011-10-11 16:49:02 +1100841static void wait_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800842{
843 spin_lock_irq(&conf->resync_lock);
844 if (conf->barrier) {
845 conf->nr_waiting++;
846 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
847 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000848 );
NeilBrown0a27ec92006-01-06 00:20:13 -0800849 conf->nr_waiting--;
850 }
851 conf->nr_pending++;
852 spin_unlock_irq(&conf->resync_lock);
853}
854
NeilBrowne879a872011-10-11 16:49:02 +1100855static void allow_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800856{
857 unsigned long flags;
858 spin_lock_irqsave(&conf->resync_lock, flags);
859 conf->nr_pending--;
860 spin_unlock_irqrestore(&conf->resync_lock, flags);
861 wake_up(&conf->wait_barrier);
862}
863
NeilBrowne879a872011-10-11 16:49:02 +1100864static void freeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -0800865{
866 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800867 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800868 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800869 * wait until nr_pending match nr_queued+1
870 * This is called in the context of one normal IO request
871 * that has failed. Thus any sync request that might be pending
872 * will be blocked by nr_pending, and we need to wait for
873 * pending IO requests to complete or be queued for re-try.
874 * Thus the number queued (nr_queued) plus this request (1)
875 * must match the number of pending IOs (nr_pending) before
876 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -0800877 */
878 spin_lock_irq(&conf->resync_lock);
879 conf->barrier++;
880 conf->nr_waiting++;
881 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -0800882 conf->nr_pending == conf->nr_queued+1,
NeilBrown4443ae12006-01-06 00:20:28 -0800883 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000884 flush_pending_writes(conf));
885
NeilBrown4443ae12006-01-06 00:20:28 -0800886 spin_unlock_irq(&conf->resync_lock);
887}
888
NeilBrowne879a872011-10-11 16:49:02 +1100889static void unfreeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -0800890{
891 /* reverse the effect of the freeze */
892 spin_lock_irq(&conf->resync_lock);
893 conf->barrier--;
894 conf->nr_waiting--;
895 wake_up(&conf->wait_barrier);
896 spin_unlock_irq(&conf->resync_lock);
897}
898
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -0700899static void make_request(struct mddev *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
NeilBrowne879a872011-10-11 16:49:02 +1100901 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100902 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 struct bio *read_bio;
904 int i;
905 int chunk_sects = conf->chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +0100906 const int rw = bio_data_dir(bio);
NeilBrown2c7d46e2010-08-18 16:16:05 +1000907 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
Tejun Heoe9c74692010-09-03 11:56:18 +0200908 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
NeilBrown6cce3b22006-01-06 00:20:16 -0800909 unsigned long flags;
NeilBrown3cb03002011-10-11 16:45:26 +1100910 struct md_rdev *blocked_rdev;
NeilBrownc3b328a2011-04-18 18:25:43 +1000911 int plugged;
NeilBrownd4432c22011-07-28 11:39:24 +1000912 int sectors_handled;
913 int max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Tejun Heoe9c74692010-09-03 11:56:18 +0200915 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
916 md_flush_request(mddev, bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200917 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -0700918 }
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 /* If this request crosses a chunk boundary, we need to
921 * split it. This will only happen for 1 PAGE (or less) requests.
922 */
923 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
924 > chunk_sects &&
925 conf->near_copies < conf->raid_disks)) {
926 struct bio_pair *bp;
927 /* Sanity check -- queue functions should prevent this happening */
928 if (bio->bi_vcnt != 1 ||
929 bio->bi_idx != 0)
930 goto bad_map;
931 /* This is a one page bio that upper layers
932 * refuse to split for us, so we need to split it.
933 */
Denis ChengRq6feef532008-10-09 08:57:05 +0200934 bp = bio_split(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
NeilBrown51e9ac72010-08-07 21:17:00 +1000936
937 /* Each of these 'make_request' calls will call 'wait_barrier'.
938 * If the first succeeds but the second blocks due to the resync
939 * thread raising the barrier, we will deadlock because the
940 * IO to the underlying device will be queued in generic_make_request
941 * and will never complete, so will never reduce nr_pending.
942 * So increment nr_waiting here so no new raise_barriers will
943 * succeed, and so the second wait_barrier cannot block.
944 */
945 spin_lock_irq(&conf->resync_lock);
946 conf->nr_waiting++;
947 spin_unlock_irq(&conf->resync_lock);
948
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200949 make_request(mddev, &bp->bio1);
950 make_request(mddev, &bp->bio2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
NeilBrown51e9ac72010-08-07 21:17:00 +1000952 spin_lock_irq(&conf->resync_lock);
953 conf->nr_waiting--;
954 wake_up(&conf->wait_barrier);
955 spin_unlock_irq(&conf->resync_lock);
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 bio_pair_release(bp);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200958 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 bad_map:
NeilBrown128595e2010-05-03 14:47:14 +1000960 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
961 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
963
NeilBrown6712ecf2007-09-27 12:47:43 +0200964 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200965 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
967
NeilBrown3d310eb2005-06-21 17:17:26 -0700968 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -0700969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /*
971 * Register the new request and wait if the reconstruction
972 * thread has put up a bar for new requests.
973 * Continue immediately if no resync is active currently.
974 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800975 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
978
979 r10_bio->master_bio = bio;
980 r10_bio->sectors = bio->bi_size >> 9;
981
982 r10_bio->mddev = mddev;
983 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b22006-01-06 00:20:16 -0800984 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
NeilBrown856e08e2011-07-28 11:39:23 +1000986 /* We might need to issue multiple reads to different
987 * devices if there are bad blocks around, so we keep
988 * track of the number of reads in bio->bi_phys_segments.
989 * If this is 0, there is only one r10_bio and no locking
990 * will be needed when the request completes. If it is
991 * non-zero, then it is the number of not-completed requests.
992 */
993 bio->bi_phys_segments = 0;
994 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
995
Jens Axboea3623572005-11-01 09:26:16 +0100996 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 /*
998 * read balancing logic:
999 */
NeilBrown96c3fd12011-12-23 10:17:54 +11001000 struct md_rdev *rdev;
NeilBrown856e08e2011-07-28 11:39:23 +10001001 int slot;
1002
1003read_again:
NeilBrown96c3fd12011-12-23 10:17:54 +11001004 rdev = read_balance(conf, r10_bio, &max_sectors);
1005 if (!rdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 raid_end_bio_io(r10_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001007 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
NeilBrown96c3fd12011-12-23 10:17:54 +11001009 slot = r10_bio->read_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
NeilBrowna167f662010-10-26 18:31:13 +11001011 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrown856e08e2011-07-28 11:39:23 +10001012 md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
1013 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 r10_bio->devs[slot].bio = read_bio;
NeilBrownabbf0982011-12-23 10:17:54 +11001016 r10_bio->devs[slot].rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 read_bio->bi_sector = r10_bio->devs[slot].addr +
NeilBrown96c3fd12011-12-23 10:17:54 +11001019 rdev->data_offset;
1020 read_bio->bi_bdev = rdev->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 read_bio->bi_end_io = raid10_end_read_request;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001022 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 read_bio->bi_private = r10_bio;
1024
NeilBrown856e08e2011-07-28 11:39:23 +10001025 if (max_sectors < r10_bio->sectors) {
1026 /* Could not read all from this device, so we will
1027 * need another r10_bio.
1028 */
NeilBrown856e08e2011-07-28 11:39:23 +10001029 sectors_handled = (r10_bio->sectors + max_sectors
1030 - bio->bi_sector);
1031 r10_bio->sectors = max_sectors;
1032 spin_lock_irq(&conf->device_lock);
1033 if (bio->bi_phys_segments == 0)
1034 bio->bi_phys_segments = 2;
1035 else
1036 bio->bi_phys_segments++;
1037 spin_unlock(&conf->device_lock);
1038 /* Cannot call generic_make_request directly
1039 * as that will be queued in __generic_make_request
1040 * and subsequent mempool_alloc might block
1041 * waiting for it. so hand bio over to raid10d.
1042 */
1043 reschedule_retry(r10_bio);
1044
1045 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1046
1047 r10_bio->master_bio = bio;
1048 r10_bio->sectors = ((bio->bi_size >> 9)
1049 - sectors_handled);
1050 r10_bio->state = 0;
1051 r10_bio->mddev = mddev;
1052 r10_bio->sector = bio->bi_sector + sectors_handled;
1053 goto read_again;
1054 } else
1055 generic_make_request(read_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001056 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058
1059 /*
1060 * WRITE:
1061 */
NeilBrown34db0cd2011-10-11 16:50:01 +11001062 if (conf->pending_count >= max_queued_requests) {
1063 md_wakeup_thread(mddev->thread);
1064 wait_event(conf->wait_barrier,
1065 conf->pending_count < max_queued_requests);
1066 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001067 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 * inc refcount on their rdev. Record them by setting
1069 * bios[x] to bio
NeilBrownd4432c22011-07-28 11:39:24 +10001070 * If there are known/acknowledged bad blocks on any device
1071 * on which we have seen a write error, we want to avoid
1072 * writing to those blocks. This potentially requires several
1073 * writes to write around the bad blocks. Each set of writes
1074 * gets its own r10_bio with a set of bios attached. The number
1075 * of r10_bios is recored in bio->bi_phys_segments just as with
1076 * the read case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001078 plugged = mddev_check_plugged(mddev);
1079
NeilBrown69335ef2011-12-23 10:17:54 +11001080 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 raid10_find_phys(conf, r10_bio);
NeilBrownd4432c22011-07-28 11:39:24 +10001082retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -07001083 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 rcu_read_lock();
NeilBrownd4432c22011-07-28 11:39:24 +10001085 max_sectors = r10_bio->sectors;
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 for (i = 0; i < conf->copies; i++) {
1088 int d = r10_bio->devs[i].devnum;
NeilBrown3cb03002011-10-11 16:45:26 +11001089 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown475b0322011-12-23 10:17:55 +11001090 struct md_rdev *rrdev = rcu_dereference(
1091 conf->mirrors[d].replacement);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001092 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1093 atomic_inc(&rdev->nr_pending);
1094 blocked_rdev = rdev;
1095 break;
1096 }
NeilBrown475b0322011-12-23 10:17:55 +11001097 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1098 atomic_inc(&rrdev->nr_pending);
1099 blocked_rdev = rrdev;
1100 break;
1101 }
1102 if (rrdev && test_bit(Faulty, &rrdev->flags))
1103 rrdev = NULL;
1104
NeilBrownd4432c22011-07-28 11:39:24 +10001105 r10_bio->devs[i].bio = NULL;
NeilBrown475b0322011-12-23 10:17:55 +11001106 r10_bio->devs[i].repl_bio = NULL;
NeilBrownd4432c22011-07-28 11:39:24 +10001107 if (!rdev || test_bit(Faulty, &rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001108 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrownd4432c22011-07-28 11:39:24 +10001109 continue;
NeilBrown6cce3b22006-01-06 00:20:16 -08001110 }
NeilBrownd4432c22011-07-28 11:39:24 +10001111 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1112 sector_t first_bad;
1113 sector_t dev_sector = r10_bio->devs[i].addr;
1114 int bad_sectors;
1115 int is_bad;
1116
1117 is_bad = is_badblock(rdev, dev_sector,
1118 max_sectors,
1119 &first_bad, &bad_sectors);
1120 if (is_bad < 0) {
1121 /* Mustn't write here until the bad block
1122 * is acknowledged
1123 */
1124 atomic_inc(&rdev->nr_pending);
1125 set_bit(BlockedBadBlocks, &rdev->flags);
1126 blocked_rdev = rdev;
1127 break;
1128 }
1129 if (is_bad && first_bad <= dev_sector) {
1130 /* Cannot write here at all */
1131 bad_sectors -= (dev_sector - first_bad);
1132 if (bad_sectors < max_sectors)
1133 /* Mustn't write more than bad_sectors
1134 * to other devices yet
1135 */
1136 max_sectors = bad_sectors;
1137 /* We don't set R10BIO_Degraded as that
1138 * only applies if the disk is missing,
1139 * so it might be re-added, and we want to
1140 * know to recover this chunk.
1141 * In this case the device is here, and the
1142 * fact that this chunk is not in-sync is
1143 * recorded in the bad block log.
1144 */
1145 continue;
1146 }
1147 if (is_bad) {
1148 int good_sectors = first_bad - dev_sector;
1149 if (good_sectors < max_sectors)
1150 max_sectors = good_sectors;
1151 }
1152 }
1153 r10_bio->devs[i].bio = bio;
1154 atomic_inc(&rdev->nr_pending);
NeilBrown475b0322011-12-23 10:17:55 +11001155 if (rrdev) {
1156 r10_bio->devs[i].repl_bio = bio;
1157 atomic_inc(&rrdev->nr_pending);
1158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
1160 rcu_read_unlock();
1161
Dan Williams6bfe0b42008-04-30 00:52:32 -07001162 if (unlikely(blocked_rdev)) {
1163 /* Have to wait for this device to get unblocked, then retry */
1164 int j;
1165 int d;
1166
NeilBrown475b0322011-12-23 10:17:55 +11001167 for (j = 0; j < i; j++) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07001168 if (r10_bio->devs[j].bio) {
1169 d = r10_bio->devs[j].devnum;
1170 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1171 }
NeilBrown475b0322011-12-23 10:17:55 +11001172 if (r10_bio->devs[j].repl_bio) {
1173 d = r10_bio->devs[j].devnum;
1174 rdev_dec_pending(
1175 conf->mirrors[d].replacement, mddev);
1176 }
1177 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001178 allow_barrier(conf);
1179 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1180 wait_barrier(conf);
1181 goto retry_write;
1182 }
1183
NeilBrownd4432c22011-07-28 11:39:24 +10001184 if (max_sectors < r10_bio->sectors) {
1185 /* We are splitting this into multiple parts, so
1186 * we need to prepare for allocating another r10_bio.
1187 */
1188 r10_bio->sectors = max_sectors;
1189 spin_lock_irq(&conf->device_lock);
1190 if (bio->bi_phys_segments == 0)
1191 bio->bi_phys_segments = 2;
1192 else
1193 bio->bi_phys_segments++;
1194 spin_unlock_irq(&conf->device_lock);
1195 }
1196 sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
1197
NeilBrown4e780642010-10-19 12:54:01 +11001198 atomic_set(&r10_bio->remaining, 1);
NeilBrownd4432c22011-07-28 11:39:24 +10001199 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 for (i = 0; i < conf->copies; i++) {
1202 struct bio *mbio;
1203 int d = r10_bio->devs[i].devnum;
1204 if (!r10_bio->devs[i].bio)
1205 continue;
1206
NeilBrowna167f662010-10-26 18:31:13 +11001207 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrownd4432c22011-07-28 11:39:24 +10001208 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1209 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 r10_bio->devs[i].bio = mbio;
1211
NeilBrownd4432c22011-07-28 11:39:24 +10001212 mbio->bi_sector = (r10_bio->devs[i].addr+
1213 conf->mirrors[d].rdev->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1215 mbio->bi_end_io = raid10_end_write_request;
Tejun Heoe9c74692010-09-03 11:56:18 +02001216 mbio->bi_rw = WRITE | do_sync | do_fua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 mbio->bi_private = r10_bio;
1218
1219 atomic_inc(&r10_bio->remaining);
NeilBrown4e780642010-10-19 12:54:01 +11001220 spin_lock_irqsave(&conf->device_lock, flags);
1221 bio_list_add(&conf->pending_bio_list, mbio);
NeilBrown34db0cd2011-10-11 16:50:01 +11001222 conf->pending_count++;
NeilBrown4e780642010-10-19 12:54:01 +11001223 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown475b0322011-12-23 10:17:55 +11001224
1225 if (!r10_bio->devs[i].repl_bio)
1226 continue;
1227
1228 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1229 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1230 max_sectors);
1231 r10_bio->devs[i].repl_bio = mbio;
1232
1233 mbio->bi_sector = (r10_bio->devs[i].addr+
1234 conf->mirrors[d].replacement->data_offset);
1235 mbio->bi_bdev = conf->mirrors[d].replacement->bdev;
1236 mbio->bi_end_io = raid10_end_write_request;
1237 mbio->bi_rw = WRITE | do_sync | do_fua;
1238 mbio->bi_private = r10_bio;
1239
1240 atomic_inc(&r10_bio->remaining);
1241 spin_lock_irqsave(&conf->device_lock, flags);
1242 bio_list_add(&conf->pending_bio_list, mbio);
1243 conf->pending_count++;
1244 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
1246
NeilBrown079fa162011-09-10 17:21:23 +10001247 /* Don't remove the bias on 'remaining' (one_write_done) until
1248 * after checking if we need to go around again.
1249 */
NeilBrowna35e63e2008-03-04 14:29:29 -08001250
NeilBrownd4432c22011-07-28 11:39:24 +10001251 if (sectors_handled < (bio->bi_size >> 9)) {
NeilBrown079fa162011-09-10 17:21:23 +10001252 one_write_done(r10_bio);
NeilBrown5e570282011-07-28 11:39:25 +10001253 /* We need another r10_bio. It has already been counted
NeilBrownd4432c22011-07-28 11:39:24 +10001254 * in bio->bi_phys_segments.
1255 */
1256 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1257
1258 r10_bio->master_bio = bio;
1259 r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1260
1261 r10_bio->mddev = mddev;
1262 r10_bio->sector = bio->bi_sector + sectors_handled;
1263 r10_bio->state = 0;
1264 goto retry_write;
1265 }
NeilBrown079fa162011-09-10 17:21:23 +10001266 one_write_done(r10_bio);
1267
1268 /* In case raid10d snuck in to freeze_array */
1269 wake_up(&conf->wait_barrier);
NeilBrownd4432c22011-07-28 11:39:24 +10001270
NeilBrownc3b328a2011-04-18 18:25:43 +10001271 if (do_sync || !mddev->bitmap || !plugged)
Lars Ellenberge3881a62007-01-10 23:15:37 -08001272 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
NeilBrownfd01b882011-10-11 16:47:53 +11001275static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
NeilBrowne879a872011-10-11 16:49:02 +11001277 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 int i;
1279
1280 if (conf->near_copies < conf->raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +10001281 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (conf->near_copies > 1)
1283 seq_printf(seq, " %d near-copies", conf->near_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001284 if (conf->far_copies > 1) {
1285 if (conf->far_offset)
1286 seq_printf(seq, " %d offset-copies", conf->far_copies);
1287 else
1288 seq_printf(seq, " %d far-copies", conf->far_copies);
1289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown76186dd2006-10-03 01:15:48 -07001291 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 for (i = 0; i < conf->raid_disks; i++)
1293 seq_printf(seq, "%s",
1294 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001295 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 seq_printf(seq, "]");
1297}
1298
NeilBrown700c7212011-07-27 11:00:36 +10001299/* check if there are enough drives for
1300 * every block to appear on atleast one.
1301 * Don't consider the device numbered 'ignore'
1302 * as we might be about to remove it.
1303 */
NeilBrowne879a872011-10-11 16:49:02 +11001304static int enough(struct r10conf *conf, int ignore)
NeilBrown700c7212011-07-27 11:00:36 +10001305{
1306 int first = 0;
1307
1308 do {
1309 int n = conf->copies;
1310 int cnt = 0;
1311 while (n--) {
1312 if (conf->mirrors[first].rdev &&
1313 first != ignore)
1314 cnt++;
1315 first = (first+1) % conf->raid_disks;
1316 }
1317 if (cnt == 0)
1318 return 0;
1319 } while (first != 0);
1320 return 1;
1321}
1322
NeilBrownfd01b882011-10-11 16:47:53 +11001323static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
1325 char b[BDEVNAME_SIZE];
NeilBrowne879a872011-10-11 16:49:02 +11001326 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 /*
1329 * If it is not operational, then we have already marked it as dead
1330 * else if it is the last working disks, ignore the error, let the
1331 * next level up know.
1332 * else mark the drive as failed
1333 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001334 if (test_bit(In_sync, &rdev->flags)
NeilBrown700c7212011-07-27 11:00:36 +10001335 && !enough(conf, rdev->raid_disk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 /*
1337 * Don't fail the drive, just return an IO error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 */
1339 return;
NeilBrownc04be0a2006-10-03 01:15:53 -07001340 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1341 unsigned long flags;
1342 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001344 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 /*
1346 * if recovery is running, make sure it aborts.
1347 */
NeilBrowndfc70642008-05-23 13:04:39 -07001348 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
NeilBrownde393cd2011-07-28 11:31:48 +10001350 set_bit(Blocked, &rdev->flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001351 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -07001352 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Joe Perches067032b2011-01-14 09:14:33 +11001353 printk(KERN_ALERT
1354 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1355 "md/raid10:%s: Operation continuing on %d devices.\n",
NeilBrown128595e2010-05-03 14:47:14 +10001356 mdname(mddev), bdevname(rdev->bdev, b),
1357 mdname(mddev), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359
NeilBrowne879a872011-10-11 16:49:02 +11001360static void print_conf(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 int i;
NeilBrown0f6d02d2011-10-11 16:48:46 +11001363 struct mirror_info *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
NeilBrown128595e2010-05-03 14:47:14 +10001365 printk(KERN_DEBUG "RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (!conf) {
NeilBrown128595e2010-05-03 14:47:14 +10001367 printk(KERN_DEBUG "(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return;
1369 }
NeilBrown128595e2010-05-03 14:47:14 +10001370 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 conf->raid_disks);
1372
1373 for (i = 0; i < conf->raid_disks; i++) {
1374 char b[BDEVNAME_SIZE];
1375 tmp = conf->mirrors + i;
1376 if (tmp->rdev)
NeilBrown128595e2010-05-03 14:47:14 +10001377 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001378 i, !test_bit(In_sync, &tmp->rdev->flags),
1379 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 bdevname(tmp->rdev->bdev,b));
1381 }
1382}
1383
NeilBrowne879a872011-10-11 16:49:02 +11001384static void close_sync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
NeilBrown0a27ec92006-01-06 00:20:13 -08001386 wait_barrier(conf);
1387 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 mempool_destroy(conf->r10buf_pool);
1390 conf->r10buf_pool = NULL;
1391}
1392
NeilBrownfd01b882011-10-11 16:47:53 +11001393static int raid10_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
1395 int i;
NeilBrowne879a872011-10-11 16:49:02 +11001396 struct r10conf *conf = mddev->private;
NeilBrown0f6d02d2011-10-11 16:48:46 +11001397 struct mirror_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10001398 int count = 0;
1399 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 /*
1402 * Find all non-in_sync disks within the RAID10 configuration
1403 * and mark them in_sync
1404 */
1405 for (i = 0; i < conf->raid_disks; i++) {
1406 tmp = conf->mirrors + i;
1407 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08001408 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001409 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001410 count++;
Adrian Drzewieckie6ffbcb2010-08-18 11:49:02 +10001411 sysfs_notify_dirent(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 }
1413 }
NeilBrown6b965622010-08-18 11:56:59 +10001414 spin_lock_irqsave(&conf->device_lock, flags);
1415 mddev->degraded -= count;
1416 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001419 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
1422
NeilBrownfd01b882011-10-11 16:47:53 +11001423static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
NeilBrowne879a872011-10-11 16:49:02 +11001425 struct r10conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001426 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 int mirror;
Neil Brown6c2fce22008-06-28 08:31:31 +10001428 int first = 0;
NeilBrown84707f32010-03-16 17:23:35 +11001429 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 if (mddev->recovery_cp < MaxSector)
1432 /* only hot-add to in-sync arrays, as recovery is
1433 * very different from resync
1434 */
Neil Brown199050e2008-06-28 08:31:33 +10001435 return -EBUSY;
NeilBrown700c7212011-07-27 11:00:36 +10001436 if (!enough(conf, -1))
Neil Brown199050e2008-06-28 08:31:33 +10001437 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
NeilBrowna53a6c82008-11-06 17:28:20 +11001439 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001440 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Namhyung Kim2c4193d2011-07-18 17:38:43 +10001442 if (rdev->saved_raid_disk >= first &&
NeilBrown6cce3b22006-01-06 00:20:16 -08001443 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1444 mirror = rdev->saved_raid_disk;
1445 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001446 mirror = first;
NeilBrown2bb77732011-07-27 11:00:36 +10001447 for ( ; mirror <= last ; mirror++) {
NeilBrown0f6d02d2011-10-11 16:48:46 +11001448 struct mirror_info *p = &conf->mirrors[mirror];
NeilBrown2bb77732011-07-27 11:00:36 +10001449 if (p->recovery_disabled == mddev->recovery_disabled)
1450 continue;
NeilBrown7fcc7c82011-10-31 12:59:44 +11001451 if (p->rdev)
NeilBrown2bb77732011-07-27 11:00:36 +10001452 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
NeilBrown2bb77732011-07-27 11:00:36 +10001454 disk_stack_limits(mddev->gendisk, rdev->bdev,
1455 rdev->data_offset << 9);
1456 /* as we don't honour merge_bvec_fn, we must
1457 * never risk violating it, so limit
1458 * ->max_segments to one lying with a single
1459 * page, as a one page request is never in
1460 * violation.
1461 */
1462 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1463 blk_queue_max_segments(mddev->queue, 1);
1464 blk_queue_segment_boundary(mddev->queue,
1465 PAGE_CACHE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467
NeilBrown2bb77732011-07-27 11:00:36 +10001468 p->head_position = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11001469 p->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown2bb77732011-07-27 11:00:36 +10001470 rdev->raid_disk = mirror;
1471 err = 0;
1472 if (rdev->saved_raid_disk != mirror)
1473 conf->fullsync = 1;
1474 rcu_assign_pointer(p->rdev, rdev);
1475 break;
1476 }
1477
Andre Nollac5e7112009-08-03 10:59:47 +10001478 md_integrity_add_rdev(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001480 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
1482
NeilBrownb8321b62011-12-23 10:17:51 +11001483static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
NeilBrowne879a872011-10-11 16:49:02 +11001485 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11001487 int number = rdev->raid_disk;
NeilBrownc8ab9032011-12-23 10:17:54 +11001488 struct md_rdev **rdevp;
1489 struct mirror_info *p = conf->mirrors + number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 print_conf(conf);
NeilBrownc8ab9032011-12-23 10:17:54 +11001492 if (rdev == p->rdev)
1493 rdevp = &p->rdev;
1494 else if (rdev == p->replacement)
1495 rdevp = &p->replacement;
1496 else
1497 return 0;
1498
1499 if (test_bit(In_sync, &rdev->flags) ||
1500 atomic_read(&rdev->nr_pending)) {
1501 err = -EBUSY;
1502 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 }
NeilBrownc8ab9032011-12-23 10:17:54 +11001504 /* Only remove faulty devices if recovery
1505 * is not possible.
1506 */
1507 if (!test_bit(Faulty, &rdev->flags) &&
1508 mddev->recovery_disabled != p->recovery_disabled &&
1509 enough(conf, -1)) {
1510 err = -EBUSY;
1511 goto abort;
1512 }
1513 *rdevp = NULL;
1514 synchronize_rcu();
1515 if (atomic_read(&rdev->nr_pending)) {
1516 /* lost the race, try later */
1517 err = -EBUSY;
1518 *rdevp = rdev;
1519 goto abort;
1520 }
1521 err = md_integrity_register(mddev);
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523abort:
1524
1525 print_conf(conf);
1526 return err;
1527}
1528
1529
NeilBrown6712ecf2007-09-27 12:47:43 +02001530static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001532 struct r10bio *r10_bio = bio->bi_private;
NeilBrowne879a872011-10-11 16:49:02 +11001533 struct r10conf *conf = r10_bio->mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001534 int d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
NeilBrown69335ef2011-12-23 10:17:54 +11001536 d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001537
1538 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1539 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrowne684e412011-07-28 11:39:25 +10001540 else
1541 /* The write handler will notice the lack of
1542 * R10BIO_Uptodate and record any errors etc
1543 */
NeilBrown4dbcdc72006-01-06 00:20:52 -08001544 atomic_add(r10_bio->sectors,
1545 &conf->mirrors[d].rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 /* for reconstruct, we always reschedule after a read.
1548 * for resync, only after all reads
1549 */
NeilBrown73d5c382009-02-25 13:18:47 +11001550 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1552 atomic_dec_and_test(&r10_bio->remaining)) {
1553 /* we have read all the blocks,
1554 * do the comparison in process context in raid10d
1555 */
1556 reschedule_retry(r10_bio);
1557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
1559
NeilBrown9f2c9d12011-10-11 16:48:43 +11001560static void end_sync_request(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001561{
NeilBrownfd01b882011-10-11 16:47:53 +11001562 struct mddev *mddev = r10_bio->mddev;
NeilBrown5e570282011-07-28 11:39:25 +10001563
1564 while (atomic_dec_and_test(&r10_bio->remaining)) {
1565 if (r10_bio->master_bio == NULL) {
1566 /* the primary of several recovery bios */
1567 sector_t s = r10_bio->sectors;
1568 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1569 test_bit(R10BIO_WriteError, &r10_bio->state))
1570 reschedule_retry(r10_bio);
1571 else
1572 put_buf(r10_bio);
1573 md_done_sync(mddev, s, 1);
1574 break;
1575 } else {
NeilBrown9f2c9d12011-10-11 16:48:43 +11001576 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
NeilBrown5e570282011-07-28 11:39:25 +10001577 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1578 test_bit(R10BIO_WriteError, &r10_bio->state))
1579 reschedule_retry(r10_bio);
1580 else
1581 put_buf(r10_bio);
1582 r10_bio = r10_bio2;
1583 }
1584 }
1585}
1586
NeilBrown6712ecf2007-09-27 12:47:43 +02001587static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
1589 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +11001590 struct r10bio *r10_bio = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11001591 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001592 struct r10conf *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001593 int d;
NeilBrown749c55e2011-07-28 11:39:24 +10001594 sector_t first_bad;
1595 int bad_sectors;
1596 int slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
NeilBrown69335ef2011-12-23 10:17:54 +11001598 d = find_bio_disk(conf, r10_bio, bio, &slot, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
NeilBrown1a0b7cd2011-07-28 11:39:25 +10001600 if (!uptodate) {
1601 set_bit(WriteErrorSeen, &conf->mirrors[d].rdev->flags);
1602 set_bit(R10BIO_WriteError, &r10_bio->state);
1603 } else if (is_badblock(conf->mirrors[d].rdev,
NeilBrown749c55e2011-07-28 11:39:24 +10001604 r10_bio->devs[slot].addr,
1605 r10_bio->sectors,
1606 &first_bad, &bad_sectors))
1607 set_bit(R10BIO_MadeGood, &r10_bio->state);
NeilBrowndfc70642008-05-23 13:04:39 -07001608
NeilBrown73d5c382009-02-25 13:18:47 +11001609 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
NeilBrown5e570282011-07-28 11:39:25 +10001610
1611 end_sync_request(r10_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
1613
1614/*
1615 * Note: sync and recover and handled very differently for raid10
1616 * This code is for resync.
1617 * For resync, we read through virtual addresses and read all blocks.
1618 * If there is any error, we schedule a write. The lowest numbered
1619 * drive is authoritative.
1620 * However requests come for physical address, so we need to map.
1621 * For every physical address there are raid_disks/copies virtual addresses,
1622 * which is always are least one, but is not necessarly an integer.
1623 * This means that a physical address can span multiple chunks, so we may
1624 * have to submit multiple io requests for a single sync request.
1625 */
1626/*
1627 * We check if all blocks are in-sync and only write to blocks that
1628 * aren't in sync
1629 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11001630static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
NeilBrowne879a872011-10-11 16:49:02 +11001632 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 int i, first;
1634 struct bio *tbio, *fbio;
1635
1636 atomic_set(&r10_bio->remaining, 1);
1637
1638 /* find the first device with a block */
1639 for (i=0; i<conf->copies; i++)
1640 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1641 break;
1642
1643 if (i == conf->copies)
1644 goto done;
1645
1646 first = i;
1647 fbio = r10_bio->devs[i].bio;
1648
1649 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001650 for (i=0 ; i < conf->copies ; i++) {
1651 int j, d;
1652 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001655
1656 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001658 if (i == first)
1659 continue;
1660 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1661 /* We know that the bi_io_vec layout is the same for
1662 * both 'first' and 'i', so we just compare them.
1663 * All vec entries are PAGE_SIZE;
1664 */
1665 for (j = 0; j < vcnt; j++)
1666 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1667 page_address(tbio->bi_io_vec[j].bv_page),
1668 PAGE_SIZE))
1669 break;
1670 if (j == vcnt)
1671 continue;
1672 mddev->resync_mismatches += r10_bio->sectors;
NeilBrownf84ee362011-07-28 11:39:25 +10001673 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1674 /* Don't fix anything. */
1675 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001676 }
NeilBrownf84ee362011-07-28 11:39:25 +10001677 /* Ok, we need to write this bio, either to correct an
1678 * inconsistency or to correct an unreadable block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 * First we need to fixup bv_offset, bv_len and
1680 * bi_vecs, as the read request might have corrupted these
1681 */
1682 tbio->bi_vcnt = vcnt;
1683 tbio->bi_size = r10_bio->sectors << 9;
1684 tbio->bi_idx = 0;
1685 tbio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1687 tbio->bi_flags |= 1 << BIO_UPTODATE;
1688 tbio->bi_next = NULL;
1689 tbio->bi_rw = WRITE;
1690 tbio->bi_private = r10_bio;
1691 tbio->bi_sector = r10_bio->devs[i].addr;
1692
1693 for (j=0; j < vcnt ; j++) {
1694 tbio->bi_io_vec[j].bv_offset = 0;
1695 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1696
1697 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1698 page_address(fbio->bi_io_vec[j].bv_page),
1699 PAGE_SIZE);
1700 }
1701 tbio->bi_end_io = end_sync_write;
1702
1703 d = r10_bio->devs[i].devnum;
1704 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1705 atomic_inc(&r10_bio->remaining);
1706 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1707
1708 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1709 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1710 generic_make_request(tbio);
1711 }
1712
1713done:
1714 if (atomic_dec_and_test(&r10_bio->remaining)) {
1715 md_done_sync(mddev, r10_bio->sectors, 1);
1716 put_buf(r10_bio);
1717 }
1718}
1719
1720/*
1721 * Now for the recovery code.
1722 * Recovery happens across physical sectors.
1723 * We recover all non-is_sync drives by finding the virtual address of
1724 * each, and then choose a working drive that also has that virt address.
1725 * There is a separate r10_bio for each non-in_sync drive.
1726 * Only the first two slots are in use. The first for reading,
1727 * The second for writing.
1728 *
1729 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11001730static void fix_recovery_read_error(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001731{
1732 /* We got a read error during recovery.
1733 * We repeat the read in smaller page-sized sections.
1734 * If a read succeeds, write it to the new device or record
1735 * a bad block if we cannot.
1736 * If a read fails, record a bad block on both old and
1737 * new devices.
1738 */
NeilBrownfd01b882011-10-11 16:47:53 +11001739 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001740 struct r10conf *conf = mddev->private;
NeilBrown5e570282011-07-28 11:39:25 +10001741 struct bio *bio = r10_bio->devs[0].bio;
1742 sector_t sect = 0;
1743 int sectors = r10_bio->sectors;
1744 int idx = 0;
1745 int dr = r10_bio->devs[0].devnum;
1746 int dw = r10_bio->devs[1].devnum;
1747
1748 while (sectors) {
1749 int s = sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11001750 struct md_rdev *rdev;
NeilBrown5e570282011-07-28 11:39:25 +10001751 sector_t addr;
1752 int ok;
1753
1754 if (s > (PAGE_SIZE>>9))
1755 s = PAGE_SIZE >> 9;
1756
1757 rdev = conf->mirrors[dr].rdev;
1758 addr = r10_bio->devs[0].addr + sect,
1759 ok = sync_page_io(rdev,
1760 addr,
1761 s << 9,
1762 bio->bi_io_vec[idx].bv_page,
1763 READ, false);
1764 if (ok) {
1765 rdev = conf->mirrors[dw].rdev;
1766 addr = r10_bio->devs[1].addr + sect;
1767 ok = sync_page_io(rdev,
1768 addr,
1769 s << 9,
1770 bio->bi_io_vec[idx].bv_page,
1771 WRITE, false);
1772 if (!ok)
1773 set_bit(WriteErrorSeen, &rdev->flags);
1774 }
1775 if (!ok) {
1776 /* We don't worry if we cannot set a bad block -
1777 * it really is bad so there is no loss in not
1778 * recording it yet
1779 */
1780 rdev_set_badblocks(rdev, addr, s, 0);
1781
1782 if (rdev != conf->mirrors[dw].rdev) {
1783 /* need bad block on destination too */
NeilBrown3cb03002011-10-11 16:45:26 +11001784 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
NeilBrown5e570282011-07-28 11:39:25 +10001785 addr = r10_bio->devs[1].addr + sect;
1786 ok = rdev_set_badblocks(rdev2, addr, s, 0);
1787 if (!ok) {
1788 /* just abort the recovery */
1789 printk(KERN_NOTICE
1790 "md/raid10:%s: recovery aborted"
1791 " due to read error\n",
1792 mdname(mddev));
1793
1794 conf->mirrors[dw].recovery_disabled
1795 = mddev->recovery_disabled;
1796 set_bit(MD_RECOVERY_INTR,
1797 &mddev->recovery);
1798 break;
1799 }
1800 }
1801 }
1802
1803 sectors -= s;
1804 sect += s;
1805 idx++;
1806 }
1807}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
NeilBrown9f2c9d12011-10-11 16:48:43 +11001809static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
NeilBrowne879a872011-10-11 16:49:02 +11001811 struct r10conf *conf = mddev->private;
Namhyung Kimc65060a2011-07-18 17:38:49 +10001812 int d;
1813 struct bio *wbio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
NeilBrown5e570282011-07-28 11:39:25 +10001815 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
1816 fix_recovery_read_error(r10_bio);
1817 end_sync_request(r10_bio);
1818 return;
1819 }
1820
Namhyung Kimc65060a2011-07-18 17:38:49 +10001821 /*
1822 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 * and submit the write request
1824 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 wbio = r10_bio->devs[1].bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 d = r10_bio->devs[1].devnum;
1827
1828 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1829 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
NeilBrown5e570282011-07-28 11:39:25 +10001830 generic_make_request(wbio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
1832
1833
1834/*
Robert Becker1e509152009-12-14 12:49:58 +11001835 * Used by fix_read_error() to decay the per rdev read_errors.
1836 * We halve the read error count for every hour that has elapsed
1837 * since the last recorded read error.
1838 *
1839 */
NeilBrownfd01b882011-10-11 16:47:53 +11001840static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
Robert Becker1e509152009-12-14 12:49:58 +11001841{
1842 struct timespec cur_time_mon;
1843 unsigned long hours_since_last;
1844 unsigned int read_errors = atomic_read(&rdev->read_errors);
1845
1846 ktime_get_ts(&cur_time_mon);
1847
1848 if (rdev->last_read_error.tv_sec == 0 &&
1849 rdev->last_read_error.tv_nsec == 0) {
1850 /* first time we've seen a read error */
1851 rdev->last_read_error = cur_time_mon;
1852 return;
1853 }
1854
1855 hours_since_last = (cur_time_mon.tv_sec -
1856 rdev->last_read_error.tv_sec) / 3600;
1857
1858 rdev->last_read_error = cur_time_mon;
1859
1860 /*
1861 * if hours_since_last is > the number of bits in read_errors
1862 * just set read errors to 0. We do this to avoid
1863 * overflowing the shift of read_errors by hours_since_last.
1864 */
1865 if (hours_since_last >= 8 * sizeof(read_errors))
1866 atomic_set(&rdev->read_errors, 0);
1867 else
1868 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
1869}
1870
NeilBrown3cb03002011-10-11 16:45:26 +11001871static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrown58c54fc2011-07-28 11:39:25 +10001872 int sectors, struct page *page, int rw)
1873{
1874 sector_t first_bad;
1875 int bad_sectors;
1876
1877 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
1878 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
1879 return -1;
1880 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
1881 /* success */
1882 return 1;
1883 if (rw == WRITE)
1884 set_bit(WriteErrorSeen, &rdev->flags);
1885 /* need to record an error - either for the block or the device */
1886 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
1887 md_error(rdev->mddev, rdev);
1888 return 0;
1889}
1890
Robert Becker1e509152009-12-14 12:49:58 +11001891/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 * This is a kernel thread which:
1893 *
1894 * 1. Retries failed read operations on working mirrors.
1895 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07001896 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 */
1898
NeilBrowne879a872011-10-11 16:49:02 +11001899static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown6814d532006-10-03 01:15:45 -07001900{
1901 int sect = 0; /* Offset from r10_bio->sector */
1902 int sectors = r10_bio->sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11001903 struct md_rdev*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11001904 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001905 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11001906
NeilBrown7c4e06f2011-05-11 14:53:17 +10001907 /* still own a reference to this rdev, so it cannot
1908 * have been cleared recently.
1909 */
1910 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11001911
NeilBrown7c4e06f2011-05-11 14:53:17 +10001912 if (test_bit(Faulty, &rdev->flags))
1913 /* drive has already been failed, just ignore any
1914 more fix_read_error() attempts */
1915 return;
1916
1917 check_decay_read_errors(mddev, rdev);
1918 atomic_inc(&rdev->read_errors);
1919 if (atomic_read(&rdev->read_errors) > max_read_errors) {
1920 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11001921 bdevname(rdev->bdev, b);
1922
NeilBrown7c4e06f2011-05-11 14:53:17 +10001923 printk(KERN_NOTICE
1924 "md/raid10:%s: %s: Raid device exceeded "
1925 "read_error threshold [cur %d:max %d]\n",
1926 mdname(mddev), b,
1927 atomic_read(&rdev->read_errors), max_read_errors);
1928 printk(KERN_NOTICE
1929 "md/raid10:%s: %s: Failing raid device\n",
1930 mdname(mddev), b);
1931 md_error(mddev, conf->mirrors[d].rdev);
1932 return;
Robert Becker1e509152009-12-14 12:49:58 +11001933 }
Robert Becker1e509152009-12-14 12:49:58 +11001934
NeilBrown6814d532006-10-03 01:15:45 -07001935 while(sectors) {
1936 int s = sectors;
1937 int sl = r10_bio->read_slot;
1938 int success = 0;
1939 int start;
1940
1941 if (s > (PAGE_SIZE>>9))
1942 s = PAGE_SIZE >> 9;
1943
1944 rcu_read_lock();
1945 do {
NeilBrown8dbed5c2011-07-28 11:39:24 +10001946 sector_t first_bad;
1947 int bad_sectors;
1948
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001949 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07001950 rdev = rcu_dereference(conf->mirrors[d].rdev);
1951 if (rdev &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10001952 test_bit(In_sync, &rdev->flags) &&
1953 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
1954 &first_bad, &bad_sectors) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07001955 atomic_inc(&rdev->nr_pending);
1956 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11001957 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07001958 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001959 sect,
NeilBrown6814d532006-10-03 01:15:45 -07001960 s<<9,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001961 conf->tmppage, READ, false);
NeilBrown6814d532006-10-03 01:15:45 -07001962 rdev_dec_pending(rdev, mddev);
1963 rcu_read_lock();
1964 if (success)
1965 break;
1966 }
1967 sl++;
1968 if (sl == conf->copies)
1969 sl = 0;
1970 } while (!success && sl != r10_bio->read_slot);
1971 rcu_read_unlock();
1972
1973 if (!success) {
NeilBrown58c54fc2011-07-28 11:39:25 +10001974 /* Cannot read from anywhere, just mark the block
1975 * as bad on the first device to discourage future
1976 * reads.
1977 */
NeilBrown6814d532006-10-03 01:15:45 -07001978 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
NeilBrown58c54fc2011-07-28 11:39:25 +10001979 rdev = conf->mirrors[dn].rdev;
1980
1981 if (!rdev_set_badblocks(
1982 rdev,
1983 r10_bio->devs[r10_bio->read_slot].addr
1984 + sect,
1985 s, 0))
1986 md_error(mddev, rdev);
NeilBrown6814d532006-10-03 01:15:45 -07001987 break;
1988 }
1989
1990 start = sl;
1991 /* write it back and re-read */
1992 rcu_read_lock();
1993 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11001994 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001995
NeilBrown6814d532006-10-03 01:15:45 -07001996 if (sl==0)
1997 sl = conf->copies;
1998 sl--;
1999 d = r10_bio->devs[sl].devnum;
2000 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002001 if (!rdev ||
2002 !test_bit(In_sync, &rdev->flags))
2003 continue;
2004
2005 atomic_inc(&rdev->nr_pending);
2006 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002007 if (r10_sync_page_io(rdev,
2008 r10_bio->devs[sl].addr +
2009 sect,
2010 s<<9, conf->tmppage, WRITE)
NeilBrown1294b9c2011-07-28 11:39:23 +10002011 == 0) {
2012 /* Well, this device is dead */
2013 printk(KERN_NOTICE
2014 "md/raid10:%s: read correction "
2015 "write failed"
2016 " (%d sectors at %llu on %s)\n",
2017 mdname(mddev), s,
2018 (unsigned long long)(
2019 sect + rdev->data_offset),
2020 bdevname(rdev->bdev, b));
2021 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2022 "drive\n",
2023 mdname(mddev),
2024 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07002025 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002026 rdev_dec_pending(rdev, mddev);
2027 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002028 }
2029 sl = start;
2030 while (sl != r10_bio->read_slot) {
NeilBrown1294b9c2011-07-28 11:39:23 +10002031 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002032
NeilBrown6814d532006-10-03 01:15:45 -07002033 if (sl==0)
2034 sl = conf->copies;
2035 sl--;
2036 d = r10_bio->devs[sl].devnum;
2037 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002038 if (!rdev ||
2039 !test_bit(In_sync, &rdev->flags))
2040 continue;
Robert Becker67b8dc42009-12-14 12:49:57 +11002041
NeilBrown1294b9c2011-07-28 11:39:23 +10002042 atomic_inc(&rdev->nr_pending);
2043 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002044 switch (r10_sync_page_io(rdev,
2045 r10_bio->devs[sl].addr +
2046 sect,
2047 s<<9, conf->tmppage,
2048 READ)) {
2049 case 0:
NeilBrown1294b9c2011-07-28 11:39:23 +10002050 /* Well, this device is dead */
2051 printk(KERN_NOTICE
2052 "md/raid10:%s: unable to read back "
2053 "corrected sectors"
2054 " (%d sectors at %llu on %s)\n",
2055 mdname(mddev), s,
2056 (unsigned long long)(
2057 sect + rdev->data_offset),
2058 bdevname(rdev->bdev, b));
2059 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2060 "drive\n",
2061 mdname(mddev),
2062 bdevname(rdev->bdev, b));
NeilBrown58c54fc2011-07-28 11:39:25 +10002063 break;
2064 case 1:
NeilBrown1294b9c2011-07-28 11:39:23 +10002065 printk(KERN_INFO
2066 "md/raid10:%s: read error corrected"
2067 " (%d sectors at %llu on %s)\n",
2068 mdname(mddev), s,
2069 (unsigned long long)(
2070 sect + rdev->data_offset),
2071 bdevname(rdev->bdev, b));
2072 atomic_add(s, &rdev->corrected_errors);
NeilBrown6814d532006-10-03 01:15:45 -07002073 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002074
2075 rdev_dec_pending(rdev, mddev);
2076 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002077 }
2078 rcu_read_unlock();
2079
2080 sectors -= s;
2081 sect += s;
2082 }
2083}
2084
NeilBrownbd870a12011-07-28 11:39:24 +10002085static void bi_complete(struct bio *bio, int error)
2086{
2087 complete((struct completion *)bio->bi_private);
2088}
2089
2090static int submit_bio_wait(int rw, struct bio *bio)
2091{
2092 struct completion event;
2093 rw |= REQ_SYNC;
2094
2095 init_completion(&event);
2096 bio->bi_private = &event;
2097 bio->bi_end_io = bi_complete;
2098 submit_bio(rw, bio);
2099 wait_for_completion(&event);
2100
2101 return test_bit(BIO_UPTODATE, &bio->bi_flags);
2102}
2103
NeilBrown9f2c9d12011-10-11 16:48:43 +11002104static int narrow_write_error(struct r10bio *r10_bio, int i)
NeilBrownbd870a12011-07-28 11:39:24 +10002105{
2106 struct bio *bio = r10_bio->master_bio;
NeilBrownfd01b882011-10-11 16:47:53 +11002107 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002108 struct r10conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002109 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
NeilBrownbd870a12011-07-28 11:39:24 +10002110 /* bio has the data to be written to slot 'i' where
2111 * we just recently had a write error.
2112 * We repeatedly clone the bio and trim down to one block,
2113 * then try the write. Where the write fails we record
2114 * a bad block.
2115 * It is conceivable that the bio doesn't exactly align with
2116 * blocks. We must handle this.
2117 *
2118 * We currently own a reference to the rdev.
2119 */
2120
2121 int block_sectors;
2122 sector_t sector;
2123 int sectors;
2124 int sect_to_write = r10_bio->sectors;
2125 int ok = 1;
2126
2127 if (rdev->badblocks.shift < 0)
2128 return 0;
2129
2130 block_sectors = 1 << rdev->badblocks.shift;
2131 sector = r10_bio->sector;
2132 sectors = ((r10_bio->sector + block_sectors)
2133 & ~(sector_t)(block_sectors - 1))
2134 - sector;
2135
2136 while (sect_to_write) {
2137 struct bio *wbio;
2138 if (sectors > sect_to_write)
2139 sectors = sect_to_write;
2140 /* Write at 'sector' for 'sectors' */
2141 wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
2142 md_trim_bio(wbio, sector - bio->bi_sector, sectors);
2143 wbio->bi_sector = (r10_bio->devs[i].addr+
2144 rdev->data_offset+
2145 (sector - r10_bio->sector));
2146 wbio->bi_bdev = rdev->bdev;
2147 if (submit_bio_wait(WRITE, wbio) == 0)
2148 /* Failure! */
2149 ok = rdev_set_badblocks(rdev, sector,
2150 sectors, 0)
2151 && ok;
2152
2153 bio_put(wbio);
2154 sect_to_write -= sectors;
2155 sector += sectors;
2156 sectors = block_sectors;
2157 }
2158 return ok;
2159}
2160
NeilBrown9f2c9d12011-10-11 16:48:43 +11002161static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown560f8e52011-07-28 11:39:23 +10002162{
2163 int slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002164 struct bio *bio;
NeilBrowne879a872011-10-11 16:49:02 +11002165 struct r10conf *conf = mddev->private;
NeilBrownabbf0982011-12-23 10:17:54 +11002166 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002167 char b[BDEVNAME_SIZE];
2168 unsigned long do_sync;
NeilBrown856e08e2011-07-28 11:39:23 +10002169 int max_sectors;
NeilBrown560f8e52011-07-28 11:39:23 +10002170
2171 /* we got a read error. Maybe the drive is bad. Maybe just
2172 * the block and we can fix it.
2173 * We freeze all other IO, and try reading the block from
2174 * other devices. When we find one, we re-write
2175 * and check it that fixes the read error.
2176 * This is all done synchronously while the array is
2177 * frozen.
2178 */
2179 if (mddev->ro == 0) {
2180 freeze_array(conf);
2181 fix_read_error(conf, mddev, r10_bio);
2182 unfreeze_array(conf);
2183 }
NeilBrownabbf0982011-12-23 10:17:54 +11002184 rdev_dec_pending(rdev, mddev);
NeilBrown560f8e52011-07-28 11:39:23 +10002185
2186 bio = r10_bio->devs[slot].bio;
NeilBrown7399c312011-07-28 11:39:23 +10002187 bdevname(bio->bi_bdev, b);
NeilBrown560f8e52011-07-28 11:39:23 +10002188 r10_bio->devs[slot].bio =
2189 mddev->ro ? IO_BLOCKED : NULL;
NeilBrown7399c312011-07-28 11:39:23 +10002190read_more:
NeilBrown96c3fd12011-12-23 10:17:54 +11002191 rdev = read_balance(conf, r10_bio, &max_sectors);
2192 if (rdev == NULL) {
NeilBrown560f8e52011-07-28 11:39:23 +10002193 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
2194 " read error for block %llu\n",
NeilBrown7399c312011-07-28 11:39:23 +10002195 mdname(mddev), b,
NeilBrown560f8e52011-07-28 11:39:23 +10002196 (unsigned long long)r10_bio->sector);
2197 raid_end_bio_io(r10_bio);
2198 bio_put(bio);
2199 return;
2200 }
2201
2202 do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
NeilBrown7399c312011-07-28 11:39:23 +10002203 if (bio)
2204 bio_put(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002205 slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002206 printk_ratelimited(
2207 KERN_ERR
2208 "md/raid10:%s: %s: redirecting"
2209 "sector %llu to another mirror\n",
2210 mdname(mddev),
2211 bdevname(rdev->bdev, b),
2212 (unsigned long long)r10_bio->sector);
2213 bio = bio_clone_mddev(r10_bio->master_bio,
2214 GFP_NOIO, mddev);
NeilBrown7399c312011-07-28 11:39:23 +10002215 md_trim_bio(bio,
2216 r10_bio->sector - bio->bi_sector,
2217 max_sectors);
NeilBrown560f8e52011-07-28 11:39:23 +10002218 r10_bio->devs[slot].bio = bio;
NeilBrownabbf0982011-12-23 10:17:54 +11002219 r10_bio->devs[slot].rdev = rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002220 bio->bi_sector = r10_bio->devs[slot].addr
2221 + rdev->data_offset;
2222 bio->bi_bdev = rdev->bdev;
2223 bio->bi_rw = READ | do_sync;
2224 bio->bi_private = r10_bio;
2225 bio->bi_end_io = raid10_end_read_request;
NeilBrown7399c312011-07-28 11:39:23 +10002226 if (max_sectors < r10_bio->sectors) {
2227 /* Drat - have to split this up more */
2228 struct bio *mbio = r10_bio->master_bio;
2229 int sectors_handled =
2230 r10_bio->sector + max_sectors
2231 - mbio->bi_sector;
2232 r10_bio->sectors = max_sectors;
2233 spin_lock_irq(&conf->device_lock);
2234 if (mbio->bi_phys_segments == 0)
2235 mbio->bi_phys_segments = 2;
2236 else
2237 mbio->bi_phys_segments++;
2238 spin_unlock_irq(&conf->device_lock);
2239 generic_make_request(bio);
2240 bio = NULL;
2241
2242 r10_bio = mempool_alloc(conf->r10bio_pool,
2243 GFP_NOIO);
2244 r10_bio->master_bio = mbio;
2245 r10_bio->sectors = (mbio->bi_size >> 9)
2246 - sectors_handled;
2247 r10_bio->state = 0;
2248 set_bit(R10BIO_ReadError,
2249 &r10_bio->state);
2250 r10_bio->mddev = mddev;
2251 r10_bio->sector = mbio->bi_sector
2252 + sectors_handled;
2253
2254 goto read_more;
2255 } else
2256 generic_make_request(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002257}
2258
NeilBrowne879a872011-10-11 16:49:02 +11002259static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
NeilBrown749c55e2011-07-28 11:39:24 +10002260{
2261 /* Some sort of write request has finished and it
2262 * succeeded in writing where we thought there was a
2263 * bad block. So forget the bad block.
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002264 * Or possibly if failed and we need to record
2265 * a bad block.
NeilBrown749c55e2011-07-28 11:39:24 +10002266 */
2267 int m;
NeilBrown3cb03002011-10-11 16:45:26 +11002268 struct md_rdev *rdev;
NeilBrown749c55e2011-07-28 11:39:24 +10002269
2270 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2271 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002272 for (m = 0; m < conf->copies; m++) {
2273 int dev = r10_bio->devs[m].devnum;
2274 rdev = conf->mirrors[dev].rdev;
2275 if (r10_bio->devs[m].bio == NULL)
2276 continue;
2277 if (test_bit(BIO_UPTODATE,
NeilBrown749c55e2011-07-28 11:39:24 +10002278 &r10_bio->devs[m].bio->bi_flags)) {
NeilBrown749c55e2011-07-28 11:39:24 +10002279 rdev_clear_badblocks(
2280 rdev,
2281 r10_bio->devs[m].addr,
2282 r10_bio->sectors);
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002283 } else {
2284 if (!rdev_set_badblocks(
2285 rdev,
2286 r10_bio->devs[m].addr,
2287 r10_bio->sectors, 0))
2288 md_error(conf->mddev, rdev);
NeilBrown749c55e2011-07-28 11:39:24 +10002289 }
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002290 }
NeilBrown749c55e2011-07-28 11:39:24 +10002291 put_buf(r10_bio);
2292 } else {
NeilBrownbd870a12011-07-28 11:39:24 +10002293 for (m = 0; m < conf->copies; m++) {
2294 int dev = r10_bio->devs[m].devnum;
2295 struct bio *bio = r10_bio->devs[m].bio;
2296 rdev = conf->mirrors[dev].rdev;
2297 if (bio == IO_MADE_GOOD) {
NeilBrown749c55e2011-07-28 11:39:24 +10002298 rdev_clear_badblocks(
2299 rdev,
2300 r10_bio->devs[m].addr,
2301 r10_bio->sectors);
2302 rdev_dec_pending(rdev, conf->mddev);
NeilBrownbd870a12011-07-28 11:39:24 +10002303 } else if (bio != NULL &&
2304 !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2305 if (!narrow_write_error(r10_bio, m)) {
2306 md_error(conf->mddev, rdev);
2307 set_bit(R10BIO_Degraded,
2308 &r10_bio->state);
2309 }
2310 rdev_dec_pending(rdev, conf->mddev);
NeilBrown749c55e2011-07-28 11:39:24 +10002311 }
NeilBrown475b0322011-12-23 10:17:55 +11002312 bio = r10_bio->devs[m].repl_bio;
2313 rdev = conf->mirrors[dev].replacement;
2314 if (bio == IO_MADE_GOOD) {
2315 rdev_clear_badblocks(
2316 rdev,
2317 r10_bio->devs[m].addr,
2318 r10_bio->sectors);
2319 rdev_dec_pending(rdev, conf->mddev);
2320 }
NeilBrownbd870a12011-07-28 11:39:24 +10002321 }
2322 if (test_bit(R10BIO_WriteError,
2323 &r10_bio->state))
2324 close_write(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10002325 raid_end_bio_io(r10_bio);
2326 }
2327}
2328
NeilBrownfd01b882011-10-11 16:47:53 +11002329static void raid10d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
NeilBrown9f2c9d12011-10-11 16:48:43 +11002331 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 unsigned long flags;
NeilBrowne879a872011-10-11 16:49:02 +11002333 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002335 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002339 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002341
Jens Axboe7eaceac2011-03-10 08:52:07 +01002342 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002345 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002346 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002348 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002349 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08002351 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 spin_unlock_irqrestore(&conf->device_lock, flags);
2353
2354 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002355 conf = mddev->private;
NeilBrownbd870a12011-07-28 11:39:24 +10002356 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2357 test_bit(R10BIO_WriteError, &r10_bio->state))
NeilBrown749c55e2011-07-28 11:39:24 +10002358 handle_write_completed(conf, r10_bio);
2359 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01002361 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 recovery_request_write(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002363 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
NeilBrown560f8e52011-07-28 11:39:23 +10002364 handle_read_error(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002365 else {
2366 /* just a partial read to be scheduled from a
2367 * separate context
2368 */
2369 int slot = r10_bio->read_slot;
2370 generic_make_request(r10_bio->devs[slot].bio);
2371 }
NeilBrown4443ae12006-01-06 00:20:28 -08002372
NeilBrown1d9d5242009-10-16 15:55:32 +11002373 cond_resched();
NeilBrownde393cd2011-07-28 11:31:48 +10002374 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2375 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002377 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
2379
2380
NeilBrowne879a872011-10-11 16:49:02 +11002381static int init_resync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382{
2383 int buffs;
NeilBrown69335ef2011-12-23 10:17:54 +11002384 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
2386 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02002387 BUG_ON(conf->r10buf_pool);
NeilBrown69335ef2011-12-23 10:17:54 +11002388 conf->have_replacement = 0;
2389 for (i = 0; i < conf->raid_disks; i++)
2390 if (conf->mirrors[i].replacement)
2391 conf->have_replacement = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2393 if (!conf->r10buf_pool)
2394 return -ENOMEM;
2395 conf->next_resync = 0;
2396 return 0;
2397}
2398
2399/*
2400 * perform a "sync" on one "block"
2401 *
2402 * We need to make sure that no normal I/O request - particularly write
2403 * requests - conflict with active sync requests.
2404 *
2405 * This is achieved by tracking pending requests and a 'barrier' concept
2406 * that can be installed to exclude normal IO requests.
2407 *
2408 * Resync and recovery are handled very differently.
2409 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2410 *
2411 * For resync, we iterate over virtual addresses, read all copies,
2412 * and update if there are differences. If only one copy is live,
2413 * skip it.
2414 * For recovery, we iterate over physical addresses, read a good
2415 * value for each non-in_sync drive, and over-write.
2416 *
2417 * So, for recovery we may have several outstanding complex requests for a
2418 * given address, one for each out-of-sync device. We model this by allocating
2419 * a number of r10_bio structures, one for each out-of-sync device.
2420 * As we setup these structures, we collect all bio's together into a list
2421 * which we then process collectively to add pages, and then process again
2422 * to pass to generic_make_request.
2423 *
2424 * The r10_bio structures are linked using a borrowed master_bio pointer.
2425 * This link is counted in ->remaining. When the r10_bio that points to NULL
2426 * has its remaining count decremented to 0, the whole complex operation
2427 * is complete.
2428 *
2429 */
2430
NeilBrownfd01b882011-10-11 16:47:53 +11002431static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
NeilBrownab9d47e2011-05-11 14:54:41 +10002432 int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433{
NeilBrowne879a872011-10-11 16:49:02 +11002434 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002435 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 struct bio *biolist = NULL, *bio;
2437 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08002439 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11002440 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 sector_t sectors_skipped = 0;
2442 int chunks_skipped = 0;
2443
2444 if (!conf->r10buf_pool)
2445 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
2448 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11002449 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2451 max_sector = mddev->resync_max_sectors;
2452 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002453 /* If we aborted, we need to abort the
2454 * sync on the 'current' bitmap chucks (there can
2455 * be several when recovering multiple devices).
2456 * as we may have started syncing it but not finished.
2457 * We can find the current address in
2458 * mddev->curr_resync, but for recovery,
2459 * we need to convert that to several
2460 * virtual addresses.
2461 */
2462 if (mddev->curr_resync < max_sector) { /* aborted */
2463 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2464 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2465 &sync_blocks, 1);
2466 else for (i=0; i<conf->raid_disks; i++) {
2467 sector_t sect =
2468 raid10_find_virt(conf, mddev->curr_resync, i);
2469 bitmap_end_sync(mddev->bitmap, sect,
2470 &sync_blocks, 1);
2471 }
2472 } else /* completed sync */
2473 conf->fullsync = 0;
2474
2475 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07002477 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 return sectors_skipped;
2479 }
2480 if (chunks_skipped >= conf->raid_disks) {
2481 /* if there has been nothing to do on any drive,
2482 * then there is nothing to do at all..
2483 */
NeilBrown57afd892005-06-21 17:17:13 -07002484 *skipped = 1;
2485 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 }
2487
NeilBrownc6207272008-02-06 01:39:52 -08002488 if (max_sector > mddev->resync_max)
2489 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 /* make sure whole request will fit in a chunk - if chunks
2492 * are meaningful
2493 */
2494 if (conf->near_copies < conf->raid_disks &&
2495 max_sector > (sector_nr | conf->chunk_mask))
2496 max_sector = (sector_nr | conf->chunk_mask) + 1;
2497 /*
2498 * If there is non-resync activity waiting for us then
2499 * put in a delay to throttle resync.
2500 */
NeilBrown0a27ec92006-01-06 00:20:13 -08002501 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
2504 /* Again, very different code for resync and recovery.
2505 * Both must result in an r10bio with a list of bios that
2506 * have bi_end_io, bi_sector, bi_bdev set,
2507 * and bi_private set to the r10bio.
2508 * For recovery, we may actually create several r10bios
2509 * with 2 bios in each, that correspond to the bios in the main one.
2510 * In this case, the subordinate r10bios link back through a
2511 * borrowed master_bio pointer, and the counter in the master
2512 * includes a ref from each subordinate.
2513 */
2514 /* First, we decide what to do and set ->bi_end_io
2515 * To end_sync_read if we want to read, and
2516 * end_sync_write if we will want to write.
2517 */
2518
NeilBrown6cce3b22006-01-06 00:20:16 -08002519 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2521 /* recovery... the complicated one */
NeilBrowne875ece2011-07-28 11:39:24 +10002522 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 r10_bio = NULL;
2524
NeilBrownab9d47e2011-05-11 14:54:41 +10002525 for (i=0 ; i<conf->raid_disks; i++) {
2526 int still_degraded;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002527 struct r10bio *rb2;
NeilBrownab9d47e2011-05-11 14:54:41 +10002528 sector_t sect;
2529 int must_sync;
NeilBrowne875ece2011-07-28 11:39:24 +10002530 int any_working;
NeilBrownab9d47e2011-05-11 14:54:41 +10002531
2532 if (conf->mirrors[i].rdev == NULL ||
2533 test_bit(In_sync, &conf->mirrors[i].rdev->flags))
2534 continue;
2535
2536 still_degraded = 0;
2537 /* want to reconstruct this device */
2538 rb2 = r10_bio;
2539 sect = raid10_find_virt(conf, sector_nr, i);
2540 /* Unless we are doing a full sync, we only need
2541 * to recover the block if it is set in the bitmap
2542 */
2543 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2544 &sync_blocks, 1);
2545 if (sync_blocks < max_sync)
2546 max_sync = sync_blocks;
2547 if (!must_sync &&
2548 !conf->fullsync) {
2549 /* yep, skip the sync_blocks here, but don't assume
2550 * that there will never be anything to do here
NeilBrown6cce3b22006-01-06 00:20:16 -08002551 */
NeilBrownab9d47e2011-05-11 14:54:41 +10002552 chunks_skipped = -1;
2553 continue;
2554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
NeilBrownab9d47e2011-05-11 14:54:41 +10002556 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2557 raise_barrier(conf, rb2 != NULL);
2558 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
NeilBrownab9d47e2011-05-11 14:54:41 +10002560 r10_bio->master_bio = (struct bio*)rb2;
2561 if (rb2)
2562 atomic_inc(&rb2->remaining);
2563 r10_bio->mddev = mddev;
2564 set_bit(R10BIO_IsRecover, &r10_bio->state);
2565 r10_bio->sector = sect;
NeilBrown6cce3b22006-01-06 00:20:16 -08002566
NeilBrownab9d47e2011-05-11 14:54:41 +10002567 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10002568
NeilBrownab9d47e2011-05-11 14:54:41 +10002569 /* Need to check if the array will still be
2570 * degraded
2571 */
2572 for (j=0; j<conf->raid_disks; j++)
2573 if (conf->mirrors[j].rdev == NULL ||
2574 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
2575 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07002576 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002578
2579 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2580 &sync_blocks, still_degraded);
2581
NeilBrowne875ece2011-07-28 11:39:24 +10002582 any_working = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10002583 for (j=0; j<conf->copies;j++) {
NeilBrowne875ece2011-07-28 11:39:24 +10002584 int k;
NeilBrownab9d47e2011-05-11 14:54:41 +10002585 int d = r10_bio->devs[j].devnum;
NeilBrown5e570282011-07-28 11:39:25 +10002586 sector_t from_addr, to_addr;
NeilBrown3cb03002011-10-11 16:45:26 +11002587 struct md_rdev *rdev;
NeilBrown40c356c2011-07-28 11:39:24 +10002588 sector_t sector, first_bad;
2589 int bad_sectors;
NeilBrownab9d47e2011-05-11 14:54:41 +10002590 if (!conf->mirrors[d].rdev ||
2591 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
2592 continue;
2593 /* This is where we read from */
NeilBrowne875ece2011-07-28 11:39:24 +10002594 any_working = 1;
NeilBrown40c356c2011-07-28 11:39:24 +10002595 rdev = conf->mirrors[d].rdev;
2596 sector = r10_bio->devs[j].addr;
2597
2598 if (is_badblock(rdev, sector, max_sync,
2599 &first_bad, &bad_sectors)) {
2600 if (first_bad > sector)
2601 max_sync = first_bad - sector;
2602 else {
2603 bad_sectors -= (sector
2604 - first_bad);
2605 if (max_sync > bad_sectors)
2606 max_sync = bad_sectors;
2607 continue;
2608 }
2609 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002610 bio = r10_bio->devs[0].bio;
2611 bio->bi_next = biolist;
2612 biolist = bio;
2613 bio->bi_private = r10_bio;
2614 bio->bi_end_io = end_sync_read;
2615 bio->bi_rw = READ;
NeilBrown5e570282011-07-28 11:39:25 +10002616 from_addr = r10_bio->devs[j].addr;
2617 bio->bi_sector = from_addr +
NeilBrownab9d47e2011-05-11 14:54:41 +10002618 conf->mirrors[d].rdev->data_offset;
2619 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2620 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2621 atomic_inc(&r10_bio->remaining);
2622 /* and we write to 'i' */
2623
2624 for (k=0; k<conf->copies; k++)
2625 if (r10_bio->devs[k].devnum == i)
2626 break;
2627 BUG_ON(k == conf->copies);
2628 bio = r10_bio->devs[1].bio;
2629 bio->bi_next = biolist;
2630 biolist = bio;
2631 bio->bi_private = r10_bio;
2632 bio->bi_end_io = end_sync_write;
2633 bio->bi_rw = WRITE;
NeilBrown5e570282011-07-28 11:39:25 +10002634 to_addr = r10_bio->devs[k].addr;
2635 bio->bi_sector = to_addr +
NeilBrownab9d47e2011-05-11 14:54:41 +10002636 conf->mirrors[i].rdev->data_offset;
2637 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
2638
2639 r10_bio->devs[0].devnum = d;
NeilBrown5e570282011-07-28 11:39:25 +10002640 r10_bio->devs[0].addr = from_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10002641 r10_bio->devs[1].devnum = i;
NeilBrown5e570282011-07-28 11:39:25 +10002642 r10_bio->devs[1].addr = to_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10002643
2644 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002646 if (j == conf->copies) {
NeilBrowne875ece2011-07-28 11:39:24 +10002647 /* Cannot recover, so abort the recovery or
2648 * record a bad block */
NeilBrownab9d47e2011-05-11 14:54:41 +10002649 put_buf(r10_bio);
2650 if (rb2)
2651 atomic_dec(&rb2->remaining);
2652 r10_bio = rb2;
NeilBrowne875ece2011-07-28 11:39:24 +10002653 if (any_working) {
2654 /* problem is that there are bad blocks
2655 * on other device(s)
2656 */
2657 int k;
2658 for (k = 0; k < conf->copies; k++)
2659 if (r10_bio->devs[k].devnum == i)
2660 break;
2661 if (!rdev_set_badblocks(
2662 conf->mirrors[i].rdev,
2663 r10_bio->devs[k].addr,
2664 max_sync, 0))
2665 any_working = 0;
2666 }
2667 if (!any_working) {
2668 if (!test_and_set_bit(MD_RECOVERY_INTR,
2669 &mddev->recovery))
2670 printk(KERN_INFO "md/raid10:%s: insufficient "
2671 "working devices for recovery.\n",
2672 mdname(mddev));
2673 conf->mirrors[i].recovery_disabled
2674 = mddev->recovery_disabled;
2675 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002676 break;
2677 }
2678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 if (biolist == NULL) {
2680 while (r10_bio) {
NeilBrown9f2c9d12011-10-11 16:48:43 +11002681 struct r10bio *rb2 = r10_bio;
2682 r10_bio = (struct r10bio*) rb2->master_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 rb2->master_bio = NULL;
2684 put_buf(rb2);
2685 }
2686 goto giveup;
2687 }
2688 } else {
2689 /* resync. Schedule a read for every block at this virt offset */
2690 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08002691
NeilBrown78200d42009-02-25 13:18:47 +11002692 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
2693
NeilBrown6cce3b22006-01-06 00:20:16 -08002694 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2695 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10002696 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
2697 &mddev->recovery)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002698 /* We can skip this block */
2699 *skipped = 1;
2700 return sync_blocks + sectors_skipped;
2701 }
2702 if (sync_blocks < max_sync)
2703 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 r10_bio->mddev = mddev;
2707 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08002708 raise_barrier(conf, 0);
2709 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
2711 r10_bio->master_bio = NULL;
2712 r10_bio->sector = sector_nr;
2713 set_bit(R10BIO_IsSync, &r10_bio->state);
2714 raid10_find_phys(conf, r10_bio);
2715 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
2716
2717 for (i=0; i<conf->copies; i++) {
2718 int d = r10_bio->devs[i].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10002719 sector_t first_bad, sector;
2720 int bad_sectors;
2721
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 bio = r10_bio->devs[i].bio;
2723 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002724 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08002726 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 continue;
NeilBrown40c356c2011-07-28 11:39:24 +10002728 sector = r10_bio->devs[i].addr;
2729 if (is_badblock(conf->mirrors[d].rdev,
2730 sector, max_sync,
2731 &first_bad, &bad_sectors)) {
2732 if (first_bad > sector)
2733 max_sync = first_bad - sector;
2734 else {
2735 bad_sectors -= (sector - first_bad);
2736 if (max_sync > bad_sectors)
2737 max_sync = max_sync;
2738 continue;
2739 }
2740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2742 atomic_inc(&r10_bio->remaining);
2743 bio->bi_next = biolist;
2744 biolist = bio;
2745 bio->bi_private = r10_bio;
2746 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08002747 bio->bi_rw = READ;
NeilBrown40c356c2011-07-28 11:39:24 +10002748 bio->bi_sector = sector +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 conf->mirrors[d].rdev->data_offset;
2750 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2751 count++;
2752 }
2753
2754 if (count < 2) {
2755 for (i=0; i<conf->copies; i++) {
2756 int d = r10_bio->devs[i].devnum;
2757 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10002758 rdev_dec_pending(conf->mirrors[d].rdev,
2759 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 }
2761 put_buf(r10_bio);
2762 biolist = NULL;
2763 goto giveup;
2764 }
2765 }
2766
2767 for (bio = biolist; bio ; bio=bio->bi_next) {
2768
2769 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
2770 if (bio->bi_end_io)
2771 bio->bi_flags |= 1 << BIO_UPTODATE;
2772 bio->bi_vcnt = 0;
2773 bio->bi_idx = 0;
2774 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 bio->bi_size = 0;
2776 }
2777
2778 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08002779 if (sector_nr + max_sync < max_sector)
2780 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 do {
2782 struct page *page;
2783 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 if (sector_nr + (len>>9) > max_sector)
2785 len = (max_sector - sector_nr) << 9;
2786 if (len == 0)
2787 break;
2788 for (bio= biolist ; bio ; bio=bio->bi_next) {
NeilBrownab9d47e2011-05-11 14:54:41 +10002789 struct bio *bio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
NeilBrownab9d47e2011-05-11 14:54:41 +10002791 if (bio_add_page(bio, page, len, 0))
2792 continue;
2793
2794 /* stop here */
2795 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
2796 for (bio2 = biolist;
2797 bio2 && bio2 != bio;
2798 bio2 = bio2->bi_next) {
2799 /* remove last page from this bio */
2800 bio2->bi_vcnt--;
2801 bio2->bi_size -= len;
2802 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002804 goto bio_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 }
2806 nr_sectors += len>>9;
2807 sector_nr += len>>9;
2808 } while (biolist->bi_vcnt < RESYNC_PAGES);
2809 bio_full:
2810 r10_bio->sectors = nr_sectors;
2811
2812 while (biolist) {
2813 bio = biolist;
2814 biolist = biolist->bi_next;
2815
2816 bio->bi_next = NULL;
2817 r10_bio = bio->bi_private;
2818 r10_bio->sectors = nr_sectors;
2819
2820 if (bio->bi_end_io == end_sync_read) {
2821 md_sync_acct(bio->bi_bdev, nr_sectors);
2822 generic_make_request(bio);
2823 }
2824 }
2825
NeilBrown57afd892005-06-21 17:17:13 -07002826 if (sectors_skipped)
2827 /* pretend they weren't skipped, it makes
2828 * no important difference in this case
2829 */
2830 md_done_sync(mddev, sectors_skipped, 1);
2831
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 return sectors_skipped + nr_sectors;
2833 giveup:
2834 /* There is nowhere to write, so all non-sync
NeilBrowne875ece2011-07-28 11:39:24 +10002835 * drives must be failed or in resync, all drives
2836 * have a bad block, so try the next chunk...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 */
NeilBrown09b40682009-02-25 13:18:47 +11002838 if (sector_nr + max_sync < max_sector)
2839 max_sector = sector_nr + max_sync;
2840
2841 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 chunks_skipped ++;
2843 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845}
2846
Dan Williams80c3a6c2009-03-17 18:10:40 -07002847static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11002848raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07002849{
2850 sector_t size;
NeilBrowne879a872011-10-11 16:49:02 +11002851 struct r10conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002852
2853 if (!raid_disks)
NeilBrown84707f32010-03-16 17:23:35 +11002854 raid_disks = conf->raid_disks;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002855 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002856 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002857
2858 size = sectors >> conf->chunk_shift;
2859 sector_div(size, conf->far_copies);
2860 size = size * raid_disks;
2861 sector_div(size, conf->near_copies);
2862
2863 return size << conf->chunk_shift;
2864}
2865
Trela, Maciejdab8b292010-03-08 16:02:45 +11002866
NeilBrowne879a872011-10-11 16:49:02 +11002867static struct r10conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
NeilBrowne879a872011-10-11 16:49:02 +11002869 struct r10conf *conf = NULL;
NeilBrownc93983b2006-06-26 00:27:41 -07002870 int nc, fc, fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 sector_t stride, size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002872 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
Maciej Trelaf73ea872010-06-16 11:46:29 +01002874 if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
2875 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown128595e2010-05-03 14:47:14 +10002876 printk(KERN_ERR "md/raid10:%s: chunk size must be "
2877 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
2878 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002879 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 }
NeilBrown2604b702006-01-06 00:20:36 -08002881
Maciej Trelaf73ea872010-06-16 11:46:29 +01002882 nc = mddev->new_layout & 255;
2883 fc = (mddev->new_layout >> 8) & 255;
2884 fo = mddev->new_layout & (1<<16);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002885
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
Maciej Trelaf73ea872010-06-16 11:46:29 +01002887 (mddev->new_layout >> 17)) {
NeilBrown128595e2010-05-03 14:47:14 +10002888 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
Maciej Trelaf73ea872010-06-16 11:46:29 +01002889 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 goto out;
2891 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11002892
2893 err = -ENOMEM;
NeilBrowne879a872011-10-11 16:49:02 +11002894 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002895 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002897
NeilBrown4443ae12006-01-06 00:20:28 -08002898 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Trela, Maciejdab8b292010-03-08 16:02:45 +11002899 GFP_KERNEL);
2900 if (!conf->mirrors)
2901 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08002902
2903 conf->tmppage = alloc_page(GFP_KERNEL);
2904 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002905 goto out;
2906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
NeilBrown64a742b2007-02-28 20:11:18 -08002908 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 conf->near_copies = nc;
2910 conf->far_copies = fc;
2911 conf->copies = nc*fc;
NeilBrownc93983b2006-06-26 00:27:41 -07002912 conf->far_offset = fo;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002913 conf->chunk_mask = mddev->new_chunk_sectors - 1;
2914 conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
2915
2916 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2917 r10bio_pool_free, conf);
2918 if (!conf->r10bio_pool)
2919 goto out;
2920
Andre Noll58c0fed2009-03-31 14:33:13 +11002921 size = mddev->dev_sectors >> conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002922 sector_div(size, fc);
2923 size = size * conf->raid_disks;
2924 sector_div(size, nc);
2925 /* 'size' is now the number of chunks in the array */
2926 /* calculate "used chunks per device" in 'stride' */
2927 stride = size * conf->copies;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002928
2929 /* We need to round up when dividing by raid_disks to
2930 * get the stride size.
2931 */
2932 stride += conf->raid_disks - 1;
NeilBrown64a742b2007-02-28 20:11:18 -08002933 sector_div(stride, conf->raid_disks);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002934
2935 conf->dev_sectors = stride << conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002936
NeilBrownc93983b2006-06-26 00:27:41 -07002937 if (fo)
NeilBrown64a742b2007-02-28 20:11:18 -08002938 stride = 1;
2939 else
NeilBrownc93983b2006-06-26 00:27:41 -07002940 sector_div(stride, fc);
NeilBrown64a742b2007-02-28 20:11:18 -08002941 conf->stride = stride << conf->chunk_shift;
2942
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Neil Browne7e72bf2008-05-14 16:05:54 -07002944 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002945 INIT_LIST_HEAD(&conf->retry_list);
2946
2947 spin_lock_init(&conf->resync_lock);
2948 init_waitqueue_head(&conf->wait_barrier);
2949
2950 conf->thread = md_register_thread(raid10d, mddev, NULL);
2951 if (!conf->thread)
2952 goto out;
2953
Trela, Maciejdab8b292010-03-08 16:02:45 +11002954 conf->mddev = mddev;
2955 return conf;
2956
2957 out:
NeilBrown128595e2010-05-03 14:47:14 +10002958 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
Trela, Maciejdab8b292010-03-08 16:02:45 +11002959 mdname(mddev));
2960 if (conf) {
2961 if (conf->r10bio_pool)
2962 mempool_destroy(conf->r10bio_pool);
2963 kfree(conf->mirrors);
2964 safe_put_page(conf->tmppage);
2965 kfree(conf);
2966 }
2967 return ERR_PTR(err);
2968}
2969
NeilBrownfd01b882011-10-11 16:47:53 +11002970static int run(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002971{
NeilBrowne879a872011-10-11 16:49:02 +11002972 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002973 int i, disk_idx, chunk_size;
NeilBrown0f6d02d2011-10-11 16:48:46 +11002974 struct mirror_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11002975 struct md_rdev *rdev;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002976 sector_t size;
2977
2978 /*
2979 * copy the already verified devices into our private RAID10
2980 * bookkeeping area. [whatever we allocate in run(),
2981 * should be freed in stop()]
2982 */
2983
2984 if (mddev->private == NULL) {
2985 conf = setup_conf(mddev);
2986 if (IS_ERR(conf))
2987 return PTR_ERR(conf);
2988 mddev->private = conf;
2989 }
2990 conf = mddev->private;
2991 if (!conf)
2992 goto out;
2993
Trela, Maciejdab8b292010-03-08 16:02:45 +11002994 mddev->thread = conf->thread;
2995 conf->thread = NULL;
2996
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002997 chunk_size = mddev->chunk_sectors << 9;
2998 blk_queue_io_min(mddev->queue, chunk_size);
2999 if (conf->raid_disks % conf->near_copies)
3000 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
3001 else
3002 blk_queue_io_opt(mddev->queue, chunk_size *
3003 (conf->raid_disks / conf->near_copies));
3004
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003005 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown34b343c2011-07-28 11:31:47 +10003006
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 disk_idx = rdev->raid_disk;
NeilBrown84707f32010-03-16 17:23:35 +11003008 if (disk_idx >= conf->raid_disks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 || disk_idx < 0)
3010 continue;
3011 disk = conf->mirrors + disk_idx;
3012
3013 disk->rdev = rdev;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003014 disk_stack_limits(mddev->gendisk, rdev->bdev,
3015 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 /* as we don't honour merge_bvec_fn, we must never risk
NeilBrown627a2d32010-03-08 16:44:38 +11003017 * violating it, so limit max_segments to 1 lying
3018 * within a single page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 */
NeilBrown627a2d32010-03-08 16:44:38 +11003020 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
3021 blk_queue_max_segments(mddev->queue, 1);
3022 blk_queue_segment_boundary(mddev->queue,
3023 PAGE_CACHE_SIZE - 1);
3024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
3026 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 }
NeilBrown6d508242005-09-09 16:24:03 -07003028 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10003029 if (!enough(conf, -1)) {
NeilBrown128595e2010-05-03 14:47:14 +10003030 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07003031 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 goto out_free_conf;
3033 }
3034
3035 mddev->degraded = 0;
3036 for (i = 0; i < conf->raid_disks; i++) {
3037
3038 disk = conf->mirrors + i;
3039
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003040 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07003041 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 disk->head_position = 0;
3043 mddev->degraded++;
Neil Brown8c2e8702008-06-28 08:30:52 +10003044 if (disk->rdev)
3045 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 }
NeilBrownd890fa22011-10-26 11:54:39 +11003047 disk->recovery_disabled = mddev->recovery_disabled - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 }
3049
Andre Noll8c6ac862009-06-18 08:48:06 +10003050 if (mddev->recovery_cp != MaxSector)
NeilBrown128595e2010-05-03 14:47:14 +10003051 printk(KERN_NOTICE "md/raid10:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10003052 " -- starting background reconstruction\n",
3053 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 printk(KERN_INFO
NeilBrown128595e2010-05-03 14:47:14 +10003055 "md/raid10:%s: active with %d out of %d devices\n",
NeilBrown84707f32010-03-16 17:23:35 +11003056 mdname(mddev), conf->raid_disks - mddev->degraded,
3057 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 /*
3059 * Ok, everything is just fine now
3060 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11003061 mddev->dev_sectors = conf->dev_sectors;
3062 size = raid10_size(mddev, 0, 0);
3063 md_set_array_sectors(mddev, size);
3064 mddev->resync_max_sectors = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
NeilBrown0d129222006-10-03 01:15:54 -07003066 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
3067 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown7a5febe2005-05-16 21:53:16 -07003068
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 /* Calculate max read-ahead size.
3070 * We need to readahead at least twice a whole stripe....
3071 * maybe...
3072 */
3073 {
Andre Noll9d8f0362009-06-18 08:45:01 +10003074 int stripe = conf->raid_disks *
3075 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 stripe /= conf->near_copies;
3077 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
3078 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
3079 }
3080
NeilBrown84707f32010-03-16 17:23:35 +11003081 if (conf->near_copies < conf->raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
Martin K. Petersena91a2782011-03-17 11:11:05 +01003083
3084 if (md_integrity_register(mddev))
3085 goto out_free_conf;
3086
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 return 0;
3088
3089out_free_conf:
NeilBrown01f96c02011-09-21 15:30:20 +10003090 md_unregister_thread(&mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 if (conf->r10bio_pool)
3092 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08003093 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003094 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 kfree(conf);
3096 mddev->private = NULL;
3097out:
3098 return -EIO;
3099}
3100
NeilBrownfd01b882011-10-11 16:47:53 +11003101static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102{
NeilBrowne879a872011-10-11 16:49:02 +11003103 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
NeilBrown409c57f2009-03-31 14:39:39 +11003105 raise_barrier(conf, 0);
3106 lower_barrier(conf);
3107
NeilBrown01f96c02011-09-21 15:30:20 +10003108 md_unregister_thread(&mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
3110 if (conf->r10bio_pool)
3111 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003112 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 kfree(conf);
3114 mddev->private = NULL;
3115 return 0;
3116}
3117
NeilBrownfd01b882011-10-11 16:47:53 +11003118static void raid10_quiesce(struct mddev *mddev, int state)
NeilBrown6cce3b22006-01-06 00:20:16 -08003119{
NeilBrowne879a872011-10-11 16:49:02 +11003120 struct r10conf *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08003121
3122 switch(state) {
3123 case 1:
3124 raise_barrier(conf, 0);
3125 break;
3126 case 0:
3127 lower_barrier(conf);
3128 break;
3129 }
NeilBrown6cce3b22006-01-06 00:20:16 -08003130}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
NeilBrownfd01b882011-10-11 16:47:53 +11003132static void *raid10_takeover_raid0(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003133{
NeilBrown3cb03002011-10-11 16:45:26 +11003134 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +11003135 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003136
3137 if (mddev->degraded > 0) {
NeilBrown128595e2010-05-03 14:47:14 +10003138 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
3139 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003140 return ERR_PTR(-EINVAL);
3141 }
3142
Trela, Maciejdab8b292010-03-08 16:02:45 +11003143 /* Set new parameters */
3144 mddev->new_level = 10;
3145 /* new layout: far_copies = 1, near_copies = 2 */
3146 mddev->new_layout = (1<<8) + 2;
3147 mddev->new_chunk_sectors = mddev->chunk_sectors;
3148 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003149 mddev->raid_disks *= 2;
3150 /* make sure it will be not marked as dirty */
3151 mddev->recovery_cp = MaxSector;
3152
3153 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003154 if (!IS_ERR(conf)) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003155 list_for_each_entry(rdev, &mddev->disks, same_set)
3156 if (rdev->raid_disk >= 0)
3157 rdev->new_raid_disk = rdev->raid_disk * 2;
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003158 conf->barrier = 1;
3159 }
3160
Trela, Maciejdab8b292010-03-08 16:02:45 +11003161 return conf;
3162}
3163
NeilBrownfd01b882011-10-11 16:47:53 +11003164static void *raid10_takeover(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003165{
NeilBrowne373ab12011-10-11 16:48:59 +11003166 struct r0conf *raid0_conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003167
3168 /* raid10 can take over:
3169 * raid0 - providing it has only two drives
3170 */
3171 if (mddev->level == 0) {
3172 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11003173 raid0_conf = mddev->private;
3174 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown128595e2010-05-03 14:47:14 +10003175 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
3176 " with more than one zone.\n",
3177 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003178 return ERR_PTR(-EINVAL);
3179 }
3180 return raid10_takeover_raid0(mddev);
3181 }
3182 return ERR_PTR(-EINVAL);
3183}
3184
NeilBrown84fc4b52011-10-11 16:49:58 +11003185static struct md_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186{
3187 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08003188 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 .owner = THIS_MODULE,
3190 .make_request = make_request,
3191 .run = run,
3192 .stop = stop,
3193 .status = status,
3194 .error_handler = error,
3195 .hot_add_disk = raid10_add_disk,
3196 .hot_remove_disk= raid10_remove_disk,
3197 .spare_active = raid10_spare_active,
3198 .sync_request = sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08003199 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07003200 .size = raid10_size,
Trela, Maciejdab8b292010-03-08 16:02:45 +11003201 .takeover = raid10_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202};
3203
3204static int __init raid_init(void)
3205{
NeilBrown2604b702006-01-06 00:20:36 -08003206 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207}
3208
3209static void raid_exit(void)
3210{
NeilBrown2604b702006-01-06 00:20:36 -08003211 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212}
3213
3214module_init(raid_init);
3215module_exit(raid_exit);
3216MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11003217MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08003219MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08003220MODULE_ALIAS("md-level-10");
NeilBrown34db0cd2011-10-11 16:50:01 +11003221
3222module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);