blob: b2c8998e13973229915c0f0438f18115f1b68144 [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;
NeilBrown749c55e2011-07-28 11:39:24 +1000399 int slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
NeilBrown69335ef2011-12-23 10:17:54 +1100401 dev = find_bio_disk(conf, r10_bio, bio, &slot, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 /*
404 * this branch is our 'one mirror IO has finished' event handler:
405 */
NeilBrown6cce3b22006-01-06 00:20:16 -0800406 if (!uptodate) {
NeilBrownbd870a12011-07-28 11:39:24 +1000407 set_bit(WriteErrorSeen, &conf->mirrors[dev].rdev->flags);
408 set_bit(R10BIO_WriteError, &r10_bio->state);
409 dec_rdev = 0;
NeilBrown749c55e2011-07-28 11:39:24 +1000410 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /*
412 * Set R10BIO_Uptodate in our master bio, so that
413 * we will return a good error code for to the higher
414 * levels even if IO on some other mirrored buffer fails.
415 *
416 * The 'master' represents the composite IO operation to
417 * user-side. So if something waits for IO, then it will
418 * wait for the 'master' bio.
419 */
NeilBrown749c55e2011-07-28 11:39:24 +1000420 sector_t first_bad;
421 int bad_sectors;
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 set_bit(R10BIO_Uptodate, &r10_bio->state);
424
NeilBrown749c55e2011-07-28 11:39:24 +1000425 /* Maybe we can clear some bad blocks. */
426 if (is_badblock(conf->mirrors[dev].rdev,
427 r10_bio->devs[slot].addr,
428 r10_bio->sectors,
429 &first_bad, &bad_sectors)) {
430 bio_put(bio);
431 r10_bio->devs[slot].bio = IO_MADE_GOOD;
432 dec_rdev = 0;
433 set_bit(R10BIO_MadeGood, &r10_bio->state);
434 }
435 }
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 /*
438 *
439 * Let's see if all mirrored write operations have finished
440 * already.
441 */
NeilBrown19d5f832011-09-10 17:21:17 +1000442 one_write_done(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +1000443 if (dec_rdev)
444 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
447
448/*
449 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300450 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 * parameters: near_copies and far_copies.
452 * near_copies * far_copies must be <= raid_disks.
453 * Normally one of these will be 1.
454 * If both are 1, we get raid0.
455 * If near_copies == raid_disks, we get raid1.
456 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300457 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 * first chunk, followed by near_copies copies of the next chunk and
459 * so on.
460 * If far_copies > 1, then after 1/far_copies of the array has been assigned
461 * as described above, we start again with a device offset of near_copies.
462 * So we effectively have another copy of the whole array further down all
463 * the drives, but with blocks on different drives.
464 * With this layout, and block is never stored twice on the one device.
465 *
466 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700467 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 *
469 * raid10_find_virt does the reverse mapping, from a device and a
470 * sector offset to a virtual address
471 */
472
NeilBrowne879a872011-10-11 16:49:02 +1100473static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 int n,f;
476 sector_t sector;
477 sector_t chunk;
478 sector_t stripe;
479 int dev;
480
481 int slot = 0;
482
483 /* now calculate first sector/dev */
484 chunk = r10bio->sector >> conf->chunk_shift;
485 sector = r10bio->sector & conf->chunk_mask;
486
487 chunk *= conf->near_copies;
488 stripe = chunk;
489 dev = sector_div(stripe, conf->raid_disks);
NeilBrownc93983b2006-06-26 00:27:41 -0700490 if (conf->far_offset)
491 stripe *= conf->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 sector += stripe << conf->chunk_shift;
494
495 /* and calculate all the others */
496 for (n=0; n < conf->near_copies; n++) {
497 int d = dev;
498 sector_t s = sector;
499 r10bio->devs[slot].addr = sector;
500 r10bio->devs[slot].devnum = d;
501 slot++;
502
503 for (f = 1; f < conf->far_copies; f++) {
504 d += conf->near_copies;
505 if (d >= conf->raid_disks)
506 d -= conf->raid_disks;
507 s += conf->stride;
508 r10bio->devs[slot].devnum = d;
509 r10bio->devs[slot].addr = s;
510 slot++;
511 }
512 dev++;
513 if (dev >= conf->raid_disks) {
514 dev = 0;
515 sector += (conf->chunk_mask + 1);
516 }
517 }
518 BUG_ON(slot != conf->copies);
519}
520
NeilBrowne879a872011-10-11 16:49:02 +1100521static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 sector_t offset, chunk, vchunk;
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 offset = sector & conf->chunk_mask;
NeilBrownc93983b2006-06-26 00:27:41 -0700526 if (conf->far_offset) {
527 int fc;
528 chunk = sector >> conf->chunk_shift;
529 fc = sector_div(chunk, conf->far_copies);
530 dev -= fc * conf->near_copies;
531 if (dev < 0)
532 dev += conf->raid_disks;
533 } else {
NeilBrown64a742b2007-02-28 20:11:18 -0800534 while (sector >= conf->stride) {
NeilBrownc93983b2006-06-26 00:27:41 -0700535 sector -= conf->stride;
536 if (dev < conf->near_copies)
537 dev += conf->raid_disks - conf->near_copies;
538 else
539 dev -= conf->near_copies;
540 }
541 chunk = sector >> conf->chunk_shift;
542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 vchunk = chunk * conf->raid_disks + dev;
544 sector_div(vchunk, conf->near_copies);
545 return (vchunk << conf->chunk_shift) + offset;
546}
547
548/**
549 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
550 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200551 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 * @biovec: the request that could be merged to it.
553 *
554 * Return amount of bytes we can accept at this offset
555 * If near_copies == raid_disk, there are no striping issues,
556 * but in that case, the function isn't called at all.
557 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200558static int raid10_mergeable_bvec(struct request_queue *q,
559 struct bvec_merge_data *bvm,
560 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
NeilBrownfd01b882011-10-11 16:47:53 +1100562 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200563 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +1000565 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200566 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
569 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200570 if (max <= biovec->bv_len && bio_sectors == 0)
571 return biovec->bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 else
573 return max;
574}
575
576/*
577 * This routine returns the disk from which the requested read should
578 * be done. There is a per-array 'next expected sequential IO' sector
579 * number - if this matches on the next IO then we use the last disk.
580 * There is also a per-disk 'last know head position' sector that is
581 * maintained from IRQ contexts, both the normal and the resync IO
582 * completion handlers update this position correctly. If there is no
583 * perfect sequential match then we pick the disk whose head is closest.
584 *
585 * If there are 2 mirrors in the same 2 devices, performance degrades
586 * because position is mirror, not device based.
587 *
588 * The rdev for the device selected will have nr_pending incremented.
589 */
590
591/*
592 * FIXME: possibly should rethink readbalancing and do it differently
593 * depending on near_copies / far_copies geometry.
594 */
NeilBrown96c3fd12011-12-23 10:17:54 +1100595static struct md_rdev *read_balance(struct r10conf *conf,
596 struct r10bio *r10_bio,
597 int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000599 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000600 int disk, slot;
NeilBrown856e08e2011-07-28 11:39:23 +1000601 int sectors = r10_bio->sectors;
602 int best_good_sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000603 sector_t new_distance, best_dist;
NeilBrownabbf0982011-12-23 10:17:54 +1100604 struct md_rdev *rdev, *best_rdev;
NeilBrown56d99122011-05-11 14:27:03 +1000605 int do_balance;
606 int best_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 raid10_find_phys(conf, r10_bio);
609 rcu_read_lock();
NeilBrown56d99122011-05-11 14:27:03 +1000610retry:
NeilBrown856e08e2011-07-28 11:39:23 +1000611 sectors = r10_bio->sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000612 best_slot = -1;
NeilBrownabbf0982011-12-23 10:17:54 +1100613 best_rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000614 best_dist = MaxSector;
NeilBrown856e08e2011-07-28 11:39:23 +1000615 best_good_sectors = 0;
NeilBrown56d99122011-05-11 14:27:03 +1000616 do_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /*
618 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800619 * device if no resync is going on (recovery is ok), or below
620 * the resync window. We take the first readable disk when
621 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 */
623 if (conf->mddev->recovery_cp < MaxSector
NeilBrown56d99122011-05-11 14:27:03 +1000624 && (this_sector + sectors >= conf->next_resync))
625 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
NeilBrown56d99122011-05-11 14:27:03 +1000627 for (slot = 0; slot < conf->copies ; slot++) {
NeilBrown856e08e2011-07-28 11:39:23 +1000628 sector_t first_bad;
629 int bad_sectors;
630 sector_t dev_sector;
631
NeilBrown56d99122011-05-11 14:27:03 +1000632 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000634 disk = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100635 rdev = rcu_dereference(conf->mirrors[disk].replacement);
636 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
637 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
638 rdev = rcu_dereference(conf->mirrors[disk].rdev);
NeilBrown56d99122011-05-11 14:27:03 +1000639 if (rdev == NULL)
640 continue;
NeilBrownabbf0982011-12-23 10:17:54 +1100641 if (test_bit(Faulty, &rdev->flags))
642 continue;
643 if (!test_bit(In_sync, &rdev->flags) &&
644 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
NeilBrown56d99122011-05-11 14:27:03 +1000645 continue;
646
NeilBrown856e08e2011-07-28 11:39:23 +1000647 dev_sector = r10_bio->devs[slot].addr;
648 if (is_badblock(rdev, dev_sector, sectors,
649 &first_bad, &bad_sectors)) {
650 if (best_dist < MaxSector)
651 /* Already have a better slot */
652 continue;
653 if (first_bad <= dev_sector) {
654 /* Cannot read here. If this is the
655 * 'primary' device, then we must not read
656 * beyond 'bad_sectors' from another device.
657 */
658 bad_sectors -= (dev_sector - first_bad);
659 if (!do_balance && sectors > bad_sectors)
660 sectors = bad_sectors;
661 if (best_good_sectors > sectors)
662 best_good_sectors = sectors;
663 } else {
664 sector_t good_sectors =
665 first_bad - dev_sector;
666 if (good_sectors > best_good_sectors) {
667 best_good_sectors = good_sectors;
668 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100669 best_rdev = rdev;
NeilBrown856e08e2011-07-28 11:39:23 +1000670 }
671 if (!do_balance)
672 /* Must read from here */
673 break;
674 }
675 continue;
676 } else
677 best_good_sectors = sectors;
678
NeilBrown56d99122011-05-11 14:27:03 +1000679 if (!do_balance)
680 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
NeilBrown22dfdf52005-11-28 13:44:09 -0800682 /* This optimisation is debatable, and completely destroys
683 * sequential read speed for 'far copies' arrays. So only
684 * keep it for 'near' arrays, and review those later.
685 */
NeilBrown56d99122011-05-11 14:27:03 +1000686 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 break;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800688
689 /* for far > 1 always use the lowest address */
690 if (conf->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000691 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800692 else
NeilBrown56d99122011-05-11 14:27:03 +1000693 new_distance = abs(r10_bio->devs[slot].addr -
694 conf->mirrors[disk].head_position);
695 if (new_distance < best_dist) {
696 best_dist = new_distance;
697 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100698 best_rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
700 }
NeilBrownabbf0982011-12-23 10:17:54 +1100701 if (slot >= conf->copies) {
NeilBrown56d99122011-05-11 14:27:03 +1000702 slot = best_slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100703 rdev = best_rdev;
704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
NeilBrown56d99122011-05-11 14:27:03 +1000706 if (slot >= 0) {
NeilBrown56d99122011-05-11 14:27:03 +1000707 atomic_inc(&rdev->nr_pending);
708 if (test_bit(Faulty, &rdev->flags)) {
709 /* Cannot risk returning a device that failed
710 * before we inc'ed nr_pending
711 */
712 rdev_dec_pending(rdev, conf->mddev);
713 goto retry;
714 }
715 r10_bio->read_slot = slot;
716 } else
NeilBrown96c3fd12011-12-23 10:17:54 +1100717 rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 rcu_read_unlock();
NeilBrown856e08e2011-07-28 11:39:23 +1000719 *max_sectors = best_good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
NeilBrown96c3fd12011-12-23 10:17:54 +1100721 return rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
NeilBrown0d129222006-10-03 01:15:54 -0700724static int raid10_congested(void *data, int bits)
725{
NeilBrownfd01b882011-10-11 16:47:53 +1100726 struct mddev *mddev = data;
NeilBrowne879a872011-10-11 16:49:02 +1100727 struct r10conf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700728 int i, ret = 0;
729
NeilBrown34db0cd2011-10-11 16:50:01 +1100730 if ((bits & (1 << BDI_async_congested)) &&
731 conf->pending_count >= max_queued_requests)
732 return 1;
733
NeilBrown3fa841d2009-09-23 18:10:29 +1000734 if (mddev_congested(mddev, bits))
735 return 1;
NeilBrown0d129222006-10-03 01:15:54 -0700736 rcu_read_lock();
NeilBrown84707f32010-03-16 17:23:35 +1100737 for (i = 0; i < conf->raid_disks && ret == 0; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100738 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700739 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200740 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700741
742 ret |= bdi_congested(&q->backing_dev_info, bits);
743 }
744 }
745 rcu_read_unlock();
746 return ret;
747}
748
NeilBrowne879a872011-10-11 16:49:02 +1100749static void flush_pending_writes(struct r10conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800750{
751 /* Any writes that have been queued but are awaiting
752 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800753 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800754 spin_lock_irq(&conf->device_lock);
755
756 if (conf->pending_bio_list.head) {
757 struct bio *bio;
758 bio = bio_list_get(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +1100759 conf->pending_count = 0;
NeilBrowna35e63e2008-03-04 14:29:29 -0800760 spin_unlock_irq(&conf->device_lock);
761 /* flush any pending bitmap writes to disk
762 * before proceeding w/ I/O */
763 bitmap_unplug(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100764 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800765
766 while (bio) { /* submit pending writes */
767 struct bio *next = bio->bi_next;
768 bio->bi_next = NULL;
769 generic_make_request(bio);
770 bio = next;
771 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800772 } else
773 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800774}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100775
NeilBrown0a27ec92006-01-06 00:20:13 -0800776/* Barriers....
777 * Sometimes we need to suspend IO while we do something else,
778 * either some resync/recovery, or reconfigure the array.
779 * To do this we raise a 'barrier'.
780 * The 'barrier' is a counter that can be raised multiple times
781 * to count how many activities are happening which preclude
782 * normal IO.
783 * We can only raise the barrier if there is no pending IO.
784 * i.e. if nr_pending == 0.
785 * We choose only to raise the barrier if no-one is waiting for the
786 * barrier to go down. This means that as soon as an IO request
787 * is ready, no other operations which require a barrier will start
788 * until the IO request has had a chance.
789 *
790 * So: regular IO calls 'wait_barrier'. When that returns there
791 * is no backgroup IO happening, It must arrange to call
792 * allow_barrier when it has finished its IO.
793 * backgroup IO calls must call raise_barrier. Once that returns
794 * there is no normal IO happeing. It must arrange to call
795 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
NeilBrowne879a872011-10-11 16:49:02 +1100798static void raise_barrier(struct r10conf *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
NeilBrown6cce3b22006-01-06 00:20:16 -0800800 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
NeilBrown6cce3b22006-01-06 00:20:16 -0800803 /* Wait until no block IO is waiting (unless 'force') */
804 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrownc3b328a2011-04-18 18:25:43 +1000805 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800806
807 /* block any new IO from starting */
808 conf->barrier++;
809
NeilBrownc3b328a2011-04-18 18:25:43 +1000810 /* Now wait for all pending IO to complete */
NeilBrown0a27ec92006-01-06 00:20:13 -0800811 wait_event_lock_irq(conf->wait_barrier,
812 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
NeilBrownc3b328a2011-04-18 18:25:43 +1000813 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 spin_unlock_irq(&conf->resync_lock);
816}
817
NeilBrowne879a872011-10-11 16:49:02 +1100818static void lower_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800819{
820 unsigned long flags;
821 spin_lock_irqsave(&conf->resync_lock, flags);
822 conf->barrier--;
823 spin_unlock_irqrestore(&conf->resync_lock, flags);
824 wake_up(&conf->wait_barrier);
825}
826
NeilBrowne879a872011-10-11 16:49:02 +1100827static void wait_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800828{
829 spin_lock_irq(&conf->resync_lock);
830 if (conf->barrier) {
831 conf->nr_waiting++;
832 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
833 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000834 );
NeilBrown0a27ec92006-01-06 00:20:13 -0800835 conf->nr_waiting--;
836 }
837 conf->nr_pending++;
838 spin_unlock_irq(&conf->resync_lock);
839}
840
NeilBrowne879a872011-10-11 16:49:02 +1100841static void allow_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800842{
843 unsigned long flags;
844 spin_lock_irqsave(&conf->resync_lock, flags);
845 conf->nr_pending--;
846 spin_unlock_irqrestore(&conf->resync_lock, flags);
847 wake_up(&conf->wait_barrier);
848}
849
NeilBrowne879a872011-10-11 16:49:02 +1100850static void freeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -0800851{
852 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800853 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800854 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800855 * wait until nr_pending match nr_queued+1
856 * This is called in the context of one normal IO request
857 * that has failed. Thus any sync request that might be pending
858 * will be blocked by nr_pending, and we need to wait for
859 * pending IO requests to complete or be queued for re-try.
860 * Thus the number queued (nr_queued) plus this request (1)
861 * must match the number of pending IOs (nr_pending) before
862 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -0800863 */
864 spin_lock_irq(&conf->resync_lock);
865 conf->barrier++;
866 conf->nr_waiting++;
867 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -0800868 conf->nr_pending == conf->nr_queued+1,
NeilBrown4443ae12006-01-06 00:20:28 -0800869 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000870 flush_pending_writes(conf));
871
NeilBrown4443ae12006-01-06 00:20:28 -0800872 spin_unlock_irq(&conf->resync_lock);
873}
874
NeilBrowne879a872011-10-11 16:49:02 +1100875static void unfreeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -0800876{
877 /* reverse the effect of the freeze */
878 spin_lock_irq(&conf->resync_lock);
879 conf->barrier--;
880 conf->nr_waiting--;
881 wake_up(&conf->wait_barrier);
882 spin_unlock_irq(&conf->resync_lock);
883}
884
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -0700885static void make_request(struct mddev *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
NeilBrowne879a872011-10-11 16:49:02 +1100887 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100888 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 struct bio *read_bio;
890 int i;
891 int chunk_sects = conf->chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +0100892 const int rw = bio_data_dir(bio);
NeilBrown2c7d46e2010-08-18 16:16:05 +1000893 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
Tejun Heoe9c74692010-09-03 11:56:18 +0200894 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
NeilBrown6cce3b22006-01-06 00:20:16 -0800895 unsigned long flags;
NeilBrown3cb03002011-10-11 16:45:26 +1100896 struct md_rdev *blocked_rdev;
NeilBrownc3b328a2011-04-18 18:25:43 +1000897 int plugged;
NeilBrownd4432c22011-07-28 11:39:24 +1000898 int sectors_handled;
899 int max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Tejun Heoe9c74692010-09-03 11:56:18 +0200901 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
902 md_flush_request(mddev, bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200903 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -0700904 }
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 /* If this request crosses a chunk boundary, we need to
907 * split it. This will only happen for 1 PAGE (or less) requests.
908 */
909 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
910 > chunk_sects &&
911 conf->near_copies < conf->raid_disks)) {
912 struct bio_pair *bp;
913 /* Sanity check -- queue functions should prevent this happening */
914 if (bio->bi_vcnt != 1 ||
915 bio->bi_idx != 0)
916 goto bad_map;
917 /* This is a one page bio that upper layers
918 * refuse to split for us, so we need to split it.
919 */
Denis ChengRq6feef532008-10-09 08:57:05 +0200920 bp = bio_split(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
NeilBrown51e9ac72010-08-07 21:17:00 +1000922
923 /* Each of these 'make_request' calls will call 'wait_barrier'.
924 * If the first succeeds but the second blocks due to the resync
925 * thread raising the barrier, we will deadlock because the
926 * IO to the underlying device will be queued in generic_make_request
927 * and will never complete, so will never reduce nr_pending.
928 * So increment nr_waiting here so no new raise_barriers will
929 * succeed, and so the second wait_barrier cannot block.
930 */
931 spin_lock_irq(&conf->resync_lock);
932 conf->nr_waiting++;
933 spin_unlock_irq(&conf->resync_lock);
934
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200935 make_request(mddev, &bp->bio1);
936 make_request(mddev, &bp->bio2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
NeilBrown51e9ac72010-08-07 21:17:00 +1000938 spin_lock_irq(&conf->resync_lock);
939 conf->nr_waiting--;
940 wake_up(&conf->wait_barrier);
941 spin_unlock_irq(&conf->resync_lock);
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 bio_pair_release(bp);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200944 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 bad_map:
NeilBrown128595e2010-05-03 14:47:14 +1000946 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
947 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
949
NeilBrown6712ecf2007-09-27 12:47:43 +0200950 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200951 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
953
NeilBrown3d310eb2005-06-21 17:17:26 -0700954 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -0700955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 /*
957 * Register the new request and wait if the reconstruction
958 * thread has put up a bar for new requests.
959 * Continue immediately if no resync is active currently.
960 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800961 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
964
965 r10_bio->master_bio = bio;
966 r10_bio->sectors = bio->bi_size >> 9;
967
968 r10_bio->mddev = mddev;
969 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b22006-01-06 00:20:16 -0800970 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
NeilBrown856e08e2011-07-28 11:39:23 +1000972 /* We might need to issue multiple reads to different
973 * devices if there are bad blocks around, so we keep
974 * track of the number of reads in bio->bi_phys_segments.
975 * If this is 0, there is only one r10_bio and no locking
976 * will be needed when the request completes. If it is
977 * non-zero, then it is the number of not-completed requests.
978 */
979 bio->bi_phys_segments = 0;
980 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
981
Jens Axboea3623572005-11-01 09:26:16 +0100982 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 /*
984 * read balancing logic:
985 */
NeilBrown96c3fd12011-12-23 10:17:54 +1100986 struct md_rdev *rdev;
NeilBrown856e08e2011-07-28 11:39:23 +1000987 int slot;
988
989read_again:
NeilBrown96c3fd12011-12-23 10:17:54 +1100990 rdev = read_balance(conf, r10_bio, &max_sectors);
991 if (!rdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 raid_end_bio_io(r10_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200993 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
NeilBrown96c3fd12011-12-23 10:17:54 +1100995 slot = r10_bio->read_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
NeilBrowna167f662010-10-26 18:31:13 +1100997 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrown856e08e2011-07-28 11:39:23 +1000998 md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
999 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 r10_bio->devs[slot].bio = read_bio;
NeilBrownabbf0982011-12-23 10:17:54 +11001002 r10_bio->devs[slot].rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 read_bio->bi_sector = r10_bio->devs[slot].addr +
NeilBrown96c3fd12011-12-23 10:17:54 +11001005 rdev->data_offset;
1006 read_bio->bi_bdev = rdev->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 read_bio->bi_end_io = raid10_end_read_request;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001008 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 read_bio->bi_private = r10_bio;
1010
NeilBrown856e08e2011-07-28 11:39:23 +10001011 if (max_sectors < r10_bio->sectors) {
1012 /* Could not read all from this device, so we will
1013 * need another r10_bio.
1014 */
NeilBrown856e08e2011-07-28 11:39:23 +10001015 sectors_handled = (r10_bio->sectors + max_sectors
1016 - bio->bi_sector);
1017 r10_bio->sectors = max_sectors;
1018 spin_lock_irq(&conf->device_lock);
1019 if (bio->bi_phys_segments == 0)
1020 bio->bi_phys_segments = 2;
1021 else
1022 bio->bi_phys_segments++;
1023 spin_unlock(&conf->device_lock);
1024 /* Cannot call generic_make_request directly
1025 * as that will be queued in __generic_make_request
1026 * and subsequent mempool_alloc might block
1027 * waiting for it. so hand bio over to raid10d.
1028 */
1029 reschedule_retry(r10_bio);
1030
1031 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1032
1033 r10_bio->master_bio = bio;
1034 r10_bio->sectors = ((bio->bi_size >> 9)
1035 - sectors_handled);
1036 r10_bio->state = 0;
1037 r10_bio->mddev = mddev;
1038 r10_bio->sector = bio->bi_sector + sectors_handled;
1039 goto read_again;
1040 } else
1041 generic_make_request(read_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001042 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044
1045 /*
1046 * WRITE:
1047 */
NeilBrown34db0cd2011-10-11 16:50:01 +11001048 if (conf->pending_count >= max_queued_requests) {
1049 md_wakeup_thread(mddev->thread);
1050 wait_event(conf->wait_barrier,
1051 conf->pending_count < max_queued_requests);
1052 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001053 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 * inc refcount on their rdev. Record them by setting
1055 * bios[x] to bio
NeilBrownd4432c22011-07-28 11:39:24 +10001056 * If there are known/acknowledged bad blocks on any device
1057 * on which we have seen a write error, we want to avoid
1058 * writing to those blocks. This potentially requires several
1059 * writes to write around the bad blocks. Each set of writes
1060 * gets its own r10_bio with a set of bios attached. The number
1061 * of r10_bios is recored in bio->bi_phys_segments just as with
1062 * the read case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001064 plugged = mddev_check_plugged(mddev);
1065
NeilBrown69335ef2011-12-23 10:17:54 +11001066 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 raid10_find_phys(conf, r10_bio);
NeilBrownd4432c22011-07-28 11:39:24 +10001068retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -07001069 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 rcu_read_lock();
NeilBrownd4432c22011-07-28 11:39:24 +10001071 max_sectors = r10_bio->sectors;
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 for (i = 0; i < conf->copies; i++) {
1074 int d = r10_bio->devs[i].devnum;
NeilBrown3cb03002011-10-11 16:45:26 +11001075 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001076 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1077 atomic_inc(&rdev->nr_pending);
1078 blocked_rdev = rdev;
1079 break;
1080 }
NeilBrownd4432c22011-07-28 11:39:24 +10001081 r10_bio->devs[i].bio = NULL;
1082 if (!rdev || test_bit(Faulty, &rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001083 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrownd4432c22011-07-28 11:39:24 +10001084 continue;
NeilBrown6cce3b22006-01-06 00:20:16 -08001085 }
NeilBrownd4432c22011-07-28 11:39:24 +10001086 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1087 sector_t first_bad;
1088 sector_t dev_sector = r10_bio->devs[i].addr;
1089 int bad_sectors;
1090 int is_bad;
1091
1092 is_bad = is_badblock(rdev, dev_sector,
1093 max_sectors,
1094 &first_bad, &bad_sectors);
1095 if (is_bad < 0) {
1096 /* Mustn't write here until the bad block
1097 * is acknowledged
1098 */
1099 atomic_inc(&rdev->nr_pending);
1100 set_bit(BlockedBadBlocks, &rdev->flags);
1101 blocked_rdev = rdev;
1102 break;
1103 }
1104 if (is_bad && first_bad <= dev_sector) {
1105 /* Cannot write here at all */
1106 bad_sectors -= (dev_sector - first_bad);
1107 if (bad_sectors < max_sectors)
1108 /* Mustn't write more than bad_sectors
1109 * to other devices yet
1110 */
1111 max_sectors = bad_sectors;
1112 /* We don't set R10BIO_Degraded as that
1113 * only applies if the disk is missing,
1114 * so it might be re-added, and we want to
1115 * know to recover this chunk.
1116 * In this case the device is here, and the
1117 * fact that this chunk is not in-sync is
1118 * recorded in the bad block log.
1119 */
1120 continue;
1121 }
1122 if (is_bad) {
1123 int good_sectors = first_bad - dev_sector;
1124 if (good_sectors < max_sectors)
1125 max_sectors = good_sectors;
1126 }
1127 }
1128 r10_bio->devs[i].bio = bio;
1129 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131 rcu_read_unlock();
1132
Dan Williams6bfe0b42008-04-30 00:52:32 -07001133 if (unlikely(blocked_rdev)) {
1134 /* Have to wait for this device to get unblocked, then retry */
1135 int j;
1136 int d;
1137
1138 for (j = 0; j < i; j++)
1139 if (r10_bio->devs[j].bio) {
1140 d = r10_bio->devs[j].devnum;
1141 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1142 }
1143 allow_barrier(conf);
1144 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1145 wait_barrier(conf);
1146 goto retry_write;
1147 }
1148
NeilBrownd4432c22011-07-28 11:39:24 +10001149 if (max_sectors < r10_bio->sectors) {
1150 /* We are splitting this into multiple parts, so
1151 * we need to prepare for allocating another r10_bio.
1152 */
1153 r10_bio->sectors = max_sectors;
1154 spin_lock_irq(&conf->device_lock);
1155 if (bio->bi_phys_segments == 0)
1156 bio->bi_phys_segments = 2;
1157 else
1158 bio->bi_phys_segments++;
1159 spin_unlock_irq(&conf->device_lock);
1160 }
1161 sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
1162
NeilBrown4e780642010-10-19 12:54:01 +11001163 atomic_set(&r10_bio->remaining, 1);
NeilBrownd4432c22011-07-28 11:39:24 +10001164 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07001165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 for (i = 0; i < conf->copies; i++) {
1167 struct bio *mbio;
1168 int d = r10_bio->devs[i].devnum;
1169 if (!r10_bio->devs[i].bio)
1170 continue;
1171
NeilBrowna167f662010-10-26 18:31:13 +11001172 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrownd4432c22011-07-28 11:39:24 +10001173 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1174 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 r10_bio->devs[i].bio = mbio;
1176
NeilBrownd4432c22011-07-28 11:39:24 +10001177 mbio->bi_sector = (r10_bio->devs[i].addr+
1178 conf->mirrors[d].rdev->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1180 mbio->bi_end_io = raid10_end_write_request;
Tejun Heoe9c74692010-09-03 11:56:18 +02001181 mbio->bi_rw = WRITE | do_sync | do_fua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 mbio->bi_private = r10_bio;
1183
1184 atomic_inc(&r10_bio->remaining);
NeilBrown4e780642010-10-19 12:54:01 +11001185 spin_lock_irqsave(&conf->device_lock, flags);
1186 bio_list_add(&conf->pending_bio_list, mbio);
NeilBrown34db0cd2011-10-11 16:50:01 +11001187 conf->pending_count++;
NeilBrown4e780642010-10-19 12:54:01 +11001188 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
1190
NeilBrown079fa162011-09-10 17:21:23 +10001191 /* Don't remove the bias on 'remaining' (one_write_done) until
1192 * after checking if we need to go around again.
1193 */
NeilBrowna35e63e2008-03-04 14:29:29 -08001194
NeilBrownd4432c22011-07-28 11:39:24 +10001195 if (sectors_handled < (bio->bi_size >> 9)) {
NeilBrown079fa162011-09-10 17:21:23 +10001196 one_write_done(r10_bio);
NeilBrown5e570282011-07-28 11:39:25 +10001197 /* We need another r10_bio. It has already been counted
NeilBrownd4432c22011-07-28 11:39:24 +10001198 * in bio->bi_phys_segments.
1199 */
1200 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1201
1202 r10_bio->master_bio = bio;
1203 r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1204
1205 r10_bio->mddev = mddev;
1206 r10_bio->sector = bio->bi_sector + sectors_handled;
1207 r10_bio->state = 0;
1208 goto retry_write;
1209 }
NeilBrown079fa162011-09-10 17:21:23 +10001210 one_write_done(r10_bio);
1211
1212 /* In case raid10d snuck in to freeze_array */
1213 wake_up(&conf->wait_barrier);
NeilBrownd4432c22011-07-28 11:39:24 +10001214
NeilBrownc3b328a2011-04-18 18:25:43 +10001215 if (do_sync || !mddev->bitmap || !plugged)
Lars Ellenberge3881a62007-01-10 23:15:37 -08001216 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218
NeilBrownfd01b882011-10-11 16:47:53 +11001219static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
NeilBrowne879a872011-10-11 16:49:02 +11001221 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 int i;
1223
1224 if (conf->near_copies < conf->raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +10001225 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (conf->near_copies > 1)
1227 seq_printf(seq, " %d near-copies", conf->near_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001228 if (conf->far_copies > 1) {
1229 if (conf->far_offset)
1230 seq_printf(seq, " %d offset-copies", conf->far_copies);
1231 else
1232 seq_printf(seq, " %d far-copies", conf->far_copies);
1233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown76186dd2006-10-03 01:15:48 -07001235 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 for (i = 0; i < conf->raid_disks; i++)
1237 seq_printf(seq, "%s",
1238 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001239 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 seq_printf(seq, "]");
1241}
1242
NeilBrown700c7212011-07-27 11:00:36 +10001243/* check if there are enough drives for
1244 * every block to appear on atleast one.
1245 * Don't consider the device numbered 'ignore'
1246 * as we might be about to remove it.
1247 */
NeilBrowne879a872011-10-11 16:49:02 +11001248static int enough(struct r10conf *conf, int ignore)
NeilBrown700c7212011-07-27 11:00:36 +10001249{
1250 int first = 0;
1251
1252 do {
1253 int n = conf->copies;
1254 int cnt = 0;
1255 while (n--) {
1256 if (conf->mirrors[first].rdev &&
1257 first != ignore)
1258 cnt++;
1259 first = (first+1) % conf->raid_disks;
1260 }
1261 if (cnt == 0)
1262 return 0;
1263 } while (first != 0);
1264 return 1;
1265}
1266
NeilBrownfd01b882011-10-11 16:47:53 +11001267static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269 char b[BDEVNAME_SIZE];
NeilBrowne879a872011-10-11 16:49:02 +11001270 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 /*
1273 * If it is not operational, then we have already marked it as dead
1274 * else if it is the last working disks, ignore the error, let the
1275 * next level up know.
1276 * else mark the drive as failed
1277 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001278 if (test_bit(In_sync, &rdev->flags)
NeilBrown700c7212011-07-27 11:00:36 +10001279 && !enough(conf, rdev->raid_disk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 /*
1281 * Don't fail the drive, just return an IO error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 */
1283 return;
NeilBrownc04be0a2006-10-03 01:15:53 -07001284 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1285 unsigned long flags;
1286 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001288 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 /*
1290 * if recovery is running, make sure it aborts.
1291 */
NeilBrowndfc70642008-05-23 13:04:39 -07001292 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
NeilBrownde393cd2011-07-28 11:31:48 +10001294 set_bit(Blocked, &rdev->flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001295 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -07001296 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Joe Perches067032b2011-01-14 09:14:33 +11001297 printk(KERN_ALERT
1298 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1299 "md/raid10:%s: Operation continuing on %d devices.\n",
NeilBrown128595e2010-05-03 14:47:14 +10001300 mdname(mddev), bdevname(rdev->bdev, b),
1301 mdname(mddev), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
1303
NeilBrowne879a872011-10-11 16:49:02 +11001304static void print_conf(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
1306 int i;
NeilBrown0f6d02d2011-10-11 16:48:46 +11001307 struct mirror_info *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
NeilBrown128595e2010-05-03 14:47:14 +10001309 printk(KERN_DEBUG "RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (!conf) {
NeilBrown128595e2010-05-03 14:47:14 +10001311 printk(KERN_DEBUG "(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 return;
1313 }
NeilBrown128595e2010-05-03 14:47:14 +10001314 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 conf->raid_disks);
1316
1317 for (i = 0; i < conf->raid_disks; i++) {
1318 char b[BDEVNAME_SIZE];
1319 tmp = conf->mirrors + i;
1320 if (tmp->rdev)
NeilBrown128595e2010-05-03 14:47:14 +10001321 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001322 i, !test_bit(In_sync, &tmp->rdev->flags),
1323 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 bdevname(tmp->rdev->bdev,b));
1325 }
1326}
1327
NeilBrowne879a872011-10-11 16:49:02 +11001328static void close_sync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
NeilBrown0a27ec92006-01-06 00:20:13 -08001330 wait_barrier(conf);
1331 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 mempool_destroy(conf->r10buf_pool);
1334 conf->r10buf_pool = NULL;
1335}
1336
NeilBrownfd01b882011-10-11 16:47:53 +11001337static int raid10_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 int i;
NeilBrowne879a872011-10-11 16:49:02 +11001340 struct r10conf *conf = mddev->private;
NeilBrown0f6d02d2011-10-11 16:48:46 +11001341 struct mirror_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10001342 int count = 0;
1343 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 /*
1346 * Find all non-in_sync disks within the RAID10 configuration
1347 * and mark them in_sync
1348 */
1349 for (i = 0; i < conf->raid_disks; i++) {
1350 tmp = conf->mirrors + i;
1351 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08001352 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001353 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001354 count++;
Adrian Drzewieckie6ffbcb2010-08-18 11:49:02 +10001355 sysfs_notify_dirent(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
1357 }
NeilBrown6b965622010-08-18 11:56:59 +10001358 spin_lock_irqsave(&conf->device_lock, flags);
1359 mddev->degraded -= count;
1360 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001363 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
1365
1366
NeilBrownfd01b882011-10-11 16:47:53 +11001367static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
NeilBrowne879a872011-10-11 16:49:02 +11001369 struct r10conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001370 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 int mirror;
Neil Brown6c2fce22008-06-28 08:31:31 +10001372 int first = 0;
NeilBrown84707f32010-03-16 17:23:35 +11001373 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 if (mddev->recovery_cp < MaxSector)
1376 /* only hot-add to in-sync arrays, as recovery is
1377 * very different from resync
1378 */
Neil Brown199050e2008-06-28 08:31:33 +10001379 return -EBUSY;
NeilBrown700c7212011-07-27 11:00:36 +10001380 if (!enough(conf, -1))
Neil Brown199050e2008-06-28 08:31:33 +10001381 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
NeilBrowna53a6c82008-11-06 17:28:20 +11001383 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001384 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Namhyung Kim2c4193d2011-07-18 17:38:43 +10001386 if (rdev->saved_raid_disk >= first &&
NeilBrown6cce3b22006-01-06 00:20:16 -08001387 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1388 mirror = rdev->saved_raid_disk;
1389 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001390 mirror = first;
NeilBrown2bb77732011-07-27 11:00:36 +10001391 for ( ; mirror <= last ; mirror++) {
NeilBrown0f6d02d2011-10-11 16:48:46 +11001392 struct mirror_info *p = &conf->mirrors[mirror];
NeilBrown2bb77732011-07-27 11:00:36 +10001393 if (p->recovery_disabled == mddev->recovery_disabled)
1394 continue;
NeilBrown7fcc7c82011-10-31 12:59:44 +11001395 if (p->rdev)
NeilBrown2bb77732011-07-27 11:00:36 +10001396 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
NeilBrown2bb77732011-07-27 11:00:36 +10001398 disk_stack_limits(mddev->gendisk, rdev->bdev,
1399 rdev->data_offset << 9);
1400 /* as we don't honour merge_bvec_fn, we must
1401 * never risk violating it, so limit
1402 * ->max_segments to one lying with a single
1403 * page, as a one page request is never in
1404 * violation.
1405 */
1406 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1407 blk_queue_max_segments(mddev->queue, 1);
1408 blk_queue_segment_boundary(mddev->queue,
1409 PAGE_CACHE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411
NeilBrown2bb77732011-07-27 11:00:36 +10001412 p->head_position = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11001413 p->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown2bb77732011-07-27 11:00:36 +10001414 rdev->raid_disk = mirror;
1415 err = 0;
1416 if (rdev->saved_raid_disk != mirror)
1417 conf->fullsync = 1;
1418 rcu_assign_pointer(p->rdev, rdev);
1419 break;
1420 }
1421
Andre Nollac5e7112009-08-03 10:59:47 +10001422 md_integrity_add_rdev(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001424 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
1426
NeilBrownb8321b62011-12-23 10:17:51 +11001427static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
NeilBrowne879a872011-10-11 16:49:02 +11001429 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11001431 int number = rdev->raid_disk;
NeilBrownc8ab9032011-12-23 10:17:54 +11001432 struct md_rdev **rdevp;
1433 struct mirror_info *p = conf->mirrors + number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 print_conf(conf);
NeilBrownc8ab9032011-12-23 10:17:54 +11001436 if (rdev == p->rdev)
1437 rdevp = &p->rdev;
1438 else if (rdev == p->replacement)
1439 rdevp = &p->replacement;
1440 else
1441 return 0;
1442
1443 if (test_bit(In_sync, &rdev->flags) ||
1444 atomic_read(&rdev->nr_pending)) {
1445 err = -EBUSY;
1446 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 }
NeilBrownc8ab9032011-12-23 10:17:54 +11001448 /* Only remove faulty devices if recovery
1449 * is not possible.
1450 */
1451 if (!test_bit(Faulty, &rdev->flags) &&
1452 mddev->recovery_disabled != p->recovery_disabled &&
1453 enough(conf, -1)) {
1454 err = -EBUSY;
1455 goto abort;
1456 }
1457 *rdevp = NULL;
1458 synchronize_rcu();
1459 if (atomic_read(&rdev->nr_pending)) {
1460 /* lost the race, try later */
1461 err = -EBUSY;
1462 *rdevp = rdev;
1463 goto abort;
1464 }
1465 err = md_integrity_register(mddev);
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467abort:
1468
1469 print_conf(conf);
1470 return err;
1471}
1472
1473
NeilBrown6712ecf2007-09-27 12:47:43 +02001474static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001476 struct r10bio *r10_bio = bio->bi_private;
NeilBrowne879a872011-10-11 16:49:02 +11001477 struct r10conf *conf = r10_bio->mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001478 int d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
NeilBrown69335ef2011-12-23 10:17:54 +11001480 d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001481
1482 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1483 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrowne684e412011-07-28 11:39:25 +10001484 else
1485 /* The write handler will notice the lack of
1486 * R10BIO_Uptodate and record any errors etc
1487 */
NeilBrown4dbcdc72006-01-06 00:20:52 -08001488 atomic_add(r10_bio->sectors,
1489 &conf->mirrors[d].rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 /* for reconstruct, we always reschedule after a read.
1492 * for resync, only after all reads
1493 */
NeilBrown73d5c382009-02-25 13:18:47 +11001494 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1496 atomic_dec_and_test(&r10_bio->remaining)) {
1497 /* we have read all the blocks,
1498 * do the comparison in process context in raid10d
1499 */
1500 reschedule_retry(r10_bio);
1501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
NeilBrown9f2c9d12011-10-11 16:48:43 +11001504static void end_sync_request(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001505{
NeilBrownfd01b882011-10-11 16:47:53 +11001506 struct mddev *mddev = r10_bio->mddev;
NeilBrown5e570282011-07-28 11:39:25 +10001507
1508 while (atomic_dec_and_test(&r10_bio->remaining)) {
1509 if (r10_bio->master_bio == NULL) {
1510 /* the primary of several recovery bios */
1511 sector_t s = r10_bio->sectors;
1512 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1513 test_bit(R10BIO_WriteError, &r10_bio->state))
1514 reschedule_retry(r10_bio);
1515 else
1516 put_buf(r10_bio);
1517 md_done_sync(mddev, s, 1);
1518 break;
1519 } else {
NeilBrown9f2c9d12011-10-11 16:48:43 +11001520 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
NeilBrown5e570282011-07-28 11:39:25 +10001521 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1522 test_bit(R10BIO_WriteError, &r10_bio->state))
1523 reschedule_retry(r10_bio);
1524 else
1525 put_buf(r10_bio);
1526 r10_bio = r10_bio2;
1527 }
1528 }
1529}
1530
NeilBrown6712ecf2007-09-27 12:47:43 +02001531static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
1533 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +11001534 struct r10bio *r10_bio = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11001535 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001536 struct r10conf *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001537 int d;
NeilBrown749c55e2011-07-28 11:39:24 +10001538 sector_t first_bad;
1539 int bad_sectors;
1540 int slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
NeilBrown69335ef2011-12-23 10:17:54 +11001542 d = find_bio_disk(conf, r10_bio, bio, &slot, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
NeilBrown1a0b7cd2011-07-28 11:39:25 +10001544 if (!uptodate) {
1545 set_bit(WriteErrorSeen, &conf->mirrors[d].rdev->flags);
1546 set_bit(R10BIO_WriteError, &r10_bio->state);
1547 } else if (is_badblock(conf->mirrors[d].rdev,
NeilBrown749c55e2011-07-28 11:39:24 +10001548 r10_bio->devs[slot].addr,
1549 r10_bio->sectors,
1550 &first_bad, &bad_sectors))
1551 set_bit(R10BIO_MadeGood, &r10_bio->state);
NeilBrowndfc70642008-05-23 13:04:39 -07001552
NeilBrown73d5c382009-02-25 13:18:47 +11001553 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
NeilBrown5e570282011-07-28 11:39:25 +10001554
1555 end_sync_request(r10_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
1558/*
1559 * Note: sync and recover and handled very differently for raid10
1560 * This code is for resync.
1561 * For resync, we read through virtual addresses and read all blocks.
1562 * If there is any error, we schedule a write. The lowest numbered
1563 * drive is authoritative.
1564 * However requests come for physical address, so we need to map.
1565 * For every physical address there are raid_disks/copies virtual addresses,
1566 * which is always are least one, but is not necessarly an integer.
1567 * This means that a physical address can span multiple chunks, so we may
1568 * have to submit multiple io requests for a single sync request.
1569 */
1570/*
1571 * We check if all blocks are in-sync and only write to blocks that
1572 * aren't in sync
1573 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11001574static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
NeilBrowne879a872011-10-11 16:49:02 +11001576 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 int i, first;
1578 struct bio *tbio, *fbio;
1579
1580 atomic_set(&r10_bio->remaining, 1);
1581
1582 /* find the first device with a block */
1583 for (i=0; i<conf->copies; i++)
1584 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1585 break;
1586
1587 if (i == conf->copies)
1588 goto done;
1589
1590 first = i;
1591 fbio = r10_bio->devs[i].bio;
1592
1593 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001594 for (i=0 ; i < conf->copies ; i++) {
1595 int j, d;
1596 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001599
1600 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001602 if (i == first)
1603 continue;
1604 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1605 /* We know that the bi_io_vec layout is the same for
1606 * both 'first' and 'i', so we just compare them.
1607 * All vec entries are PAGE_SIZE;
1608 */
1609 for (j = 0; j < vcnt; j++)
1610 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1611 page_address(tbio->bi_io_vec[j].bv_page),
1612 PAGE_SIZE))
1613 break;
1614 if (j == vcnt)
1615 continue;
1616 mddev->resync_mismatches += r10_bio->sectors;
NeilBrownf84ee362011-07-28 11:39:25 +10001617 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1618 /* Don't fix anything. */
1619 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001620 }
NeilBrownf84ee362011-07-28 11:39:25 +10001621 /* Ok, we need to write this bio, either to correct an
1622 * inconsistency or to correct an unreadable block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 * First we need to fixup bv_offset, bv_len and
1624 * bi_vecs, as the read request might have corrupted these
1625 */
1626 tbio->bi_vcnt = vcnt;
1627 tbio->bi_size = r10_bio->sectors << 9;
1628 tbio->bi_idx = 0;
1629 tbio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1631 tbio->bi_flags |= 1 << BIO_UPTODATE;
1632 tbio->bi_next = NULL;
1633 tbio->bi_rw = WRITE;
1634 tbio->bi_private = r10_bio;
1635 tbio->bi_sector = r10_bio->devs[i].addr;
1636
1637 for (j=0; j < vcnt ; j++) {
1638 tbio->bi_io_vec[j].bv_offset = 0;
1639 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1640
1641 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1642 page_address(fbio->bi_io_vec[j].bv_page),
1643 PAGE_SIZE);
1644 }
1645 tbio->bi_end_io = end_sync_write;
1646
1647 d = r10_bio->devs[i].devnum;
1648 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1649 atomic_inc(&r10_bio->remaining);
1650 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1651
1652 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1653 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1654 generic_make_request(tbio);
1655 }
1656
1657done:
1658 if (atomic_dec_and_test(&r10_bio->remaining)) {
1659 md_done_sync(mddev, r10_bio->sectors, 1);
1660 put_buf(r10_bio);
1661 }
1662}
1663
1664/*
1665 * Now for the recovery code.
1666 * Recovery happens across physical sectors.
1667 * We recover all non-is_sync drives by finding the virtual address of
1668 * each, and then choose a working drive that also has that virt address.
1669 * There is a separate r10_bio for each non-in_sync drive.
1670 * Only the first two slots are in use. The first for reading,
1671 * The second for writing.
1672 *
1673 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11001674static void fix_recovery_read_error(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001675{
1676 /* We got a read error during recovery.
1677 * We repeat the read in smaller page-sized sections.
1678 * If a read succeeds, write it to the new device or record
1679 * a bad block if we cannot.
1680 * If a read fails, record a bad block on both old and
1681 * new devices.
1682 */
NeilBrownfd01b882011-10-11 16:47:53 +11001683 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001684 struct r10conf *conf = mddev->private;
NeilBrown5e570282011-07-28 11:39:25 +10001685 struct bio *bio = r10_bio->devs[0].bio;
1686 sector_t sect = 0;
1687 int sectors = r10_bio->sectors;
1688 int idx = 0;
1689 int dr = r10_bio->devs[0].devnum;
1690 int dw = r10_bio->devs[1].devnum;
1691
1692 while (sectors) {
1693 int s = sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11001694 struct md_rdev *rdev;
NeilBrown5e570282011-07-28 11:39:25 +10001695 sector_t addr;
1696 int ok;
1697
1698 if (s > (PAGE_SIZE>>9))
1699 s = PAGE_SIZE >> 9;
1700
1701 rdev = conf->mirrors[dr].rdev;
1702 addr = r10_bio->devs[0].addr + sect,
1703 ok = sync_page_io(rdev,
1704 addr,
1705 s << 9,
1706 bio->bi_io_vec[idx].bv_page,
1707 READ, false);
1708 if (ok) {
1709 rdev = conf->mirrors[dw].rdev;
1710 addr = r10_bio->devs[1].addr + sect;
1711 ok = sync_page_io(rdev,
1712 addr,
1713 s << 9,
1714 bio->bi_io_vec[idx].bv_page,
1715 WRITE, false);
1716 if (!ok)
1717 set_bit(WriteErrorSeen, &rdev->flags);
1718 }
1719 if (!ok) {
1720 /* We don't worry if we cannot set a bad block -
1721 * it really is bad so there is no loss in not
1722 * recording it yet
1723 */
1724 rdev_set_badblocks(rdev, addr, s, 0);
1725
1726 if (rdev != conf->mirrors[dw].rdev) {
1727 /* need bad block on destination too */
NeilBrown3cb03002011-10-11 16:45:26 +11001728 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
NeilBrown5e570282011-07-28 11:39:25 +10001729 addr = r10_bio->devs[1].addr + sect;
1730 ok = rdev_set_badblocks(rdev2, addr, s, 0);
1731 if (!ok) {
1732 /* just abort the recovery */
1733 printk(KERN_NOTICE
1734 "md/raid10:%s: recovery aborted"
1735 " due to read error\n",
1736 mdname(mddev));
1737
1738 conf->mirrors[dw].recovery_disabled
1739 = mddev->recovery_disabled;
1740 set_bit(MD_RECOVERY_INTR,
1741 &mddev->recovery);
1742 break;
1743 }
1744 }
1745 }
1746
1747 sectors -= s;
1748 sect += s;
1749 idx++;
1750 }
1751}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
NeilBrown9f2c9d12011-10-11 16:48:43 +11001753static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
NeilBrowne879a872011-10-11 16:49:02 +11001755 struct r10conf *conf = mddev->private;
Namhyung Kimc65060a2011-07-18 17:38:49 +10001756 int d;
1757 struct bio *wbio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
NeilBrown5e570282011-07-28 11:39:25 +10001759 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
1760 fix_recovery_read_error(r10_bio);
1761 end_sync_request(r10_bio);
1762 return;
1763 }
1764
Namhyung Kimc65060a2011-07-18 17:38:49 +10001765 /*
1766 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 * and submit the write request
1768 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 wbio = r10_bio->devs[1].bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 d = r10_bio->devs[1].devnum;
1771
1772 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1773 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
NeilBrown5e570282011-07-28 11:39:25 +10001774 generic_make_request(wbio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
1777
1778/*
Robert Becker1e509152009-12-14 12:49:58 +11001779 * Used by fix_read_error() to decay the per rdev read_errors.
1780 * We halve the read error count for every hour that has elapsed
1781 * since the last recorded read error.
1782 *
1783 */
NeilBrownfd01b882011-10-11 16:47:53 +11001784static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
Robert Becker1e509152009-12-14 12:49:58 +11001785{
1786 struct timespec cur_time_mon;
1787 unsigned long hours_since_last;
1788 unsigned int read_errors = atomic_read(&rdev->read_errors);
1789
1790 ktime_get_ts(&cur_time_mon);
1791
1792 if (rdev->last_read_error.tv_sec == 0 &&
1793 rdev->last_read_error.tv_nsec == 0) {
1794 /* first time we've seen a read error */
1795 rdev->last_read_error = cur_time_mon;
1796 return;
1797 }
1798
1799 hours_since_last = (cur_time_mon.tv_sec -
1800 rdev->last_read_error.tv_sec) / 3600;
1801
1802 rdev->last_read_error = cur_time_mon;
1803
1804 /*
1805 * if hours_since_last is > the number of bits in read_errors
1806 * just set read errors to 0. We do this to avoid
1807 * overflowing the shift of read_errors by hours_since_last.
1808 */
1809 if (hours_since_last >= 8 * sizeof(read_errors))
1810 atomic_set(&rdev->read_errors, 0);
1811 else
1812 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
1813}
1814
NeilBrown3cb03002011-10-11 16:45:26 +11001815static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrown58c54fc2011-07-28 11:39:25 +10001816 int sectors, struct page *page, int rw)
1817{
1818 sector_t first_bad;
1819 int bad_sectors;
1820
1821 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
1822 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
1823 return -1;
1824 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
1825 /* success */
1826 return 1;
1827 if (rw == WRITE)
1828 set_bit(WriteErrorSeen, &rdev->flags);
1829 /* need to record an error - either for the block or the device */
1830 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
1831 md_error(rdev->mddev, rdev);
1832 return 0;
1833}
1834
Robert Becker1e509152009-12-14 12:49:58 +11001835/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 * This is a kernel thread which:
1837 *
1838 * 1. Retries failed read operations on working mirrors.
1839 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07001840 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 */
1842
NeilBrowne879a872011-10-11 16:49:02 +11001843static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown6814d532006-10-03 01:15:45 -07001844{
1845 int sect = 0; /* Offset from r10_bio->sector */
1846 int sectors = r10_bio->sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11001847 struct md_rdev*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11001848 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001849 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11001850
NeilBrown7c4e06f2011-05-11 14:53:17 +10001851 /* still own a reference to this rdev, so it cannot
1852 * have been cleared recently.
1853 */
1854 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11001855
NeilBrown7c4e06f2011-05-11 14:53:17 +10001856 if (test_bit(Faulty, &rdev->flags))
1857 /* drive has already been failed, just ignore any
1858 more fix_read_error() attempts */
1859 return;
1860
1861 check_decay_read_errors(mddev, rdev);
1862 atomic_inc(&rdev->read_errors);
1863 if (atomic_read(&rdev->read_errors) > max_read_errors) {
1864 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11001865 bdevname(rdev->bdev, b);
1866
NeilBrown7c4e06f2011-05-11 14:53:17 +10001867 printk(KERN_NOTICE
1868 "md/raid10:%s: %s: Raid device exceeded "
1869 "read_error threshold [cur %d:max %d]\n",
1870 mdname(mddev), b,
1871 atomic_read(&rdev->read_errors), max_read_errors);
1872 printk(KERN_NOTICE
1873 "md/raid10:%s: %s: Failing raid device\n",
1874 mdname(mddev), b);
1875 md_error(mddev, conf->mirrors[d].rdev);
1876 return;
Robert Becker1e509152009-12-14 12:49:58 +11001877 }
Robert Becker1e509152009-12-14 12:49:58 +11001878
NeilBrown6814d532006-10-03 01:15:45 -07001879 while(sectors) {
1880 int s = sectors;
1881 int sl = r10_bio->read_slot;
1882 int success = 0;
1883 int start;
1884
1885 if (s > (PAGE_SIZE>>9))
1886 s = PAGE_SIZE >> 9;
1887
1888 rcu_read_lock();
1889 do {
NeilBrown8dbed5c2011-07-28 11:39:24 +10001890 sector_t first_bad;
1891 int bad_sectors;
1892
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001893 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07001894 rdev = rcu_dereference(conf->mirrors[d].rdev);
1895 if (rdev &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10001896 test_bit(In_sync, &rdev->flags) &&
1897 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
1898 &first_bad, &bad_sectors) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07001899 atomic_inc(&rdev->nr_pending);
1900 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11001901 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07001902 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001903 sect,
NeilBrown6814d532006-10-03 01:15:45 -07001904 s<<9,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001905 conf->tmppage, READ, false);
NeilBrown6814d532006-10-03 01:15:45 -07001906 rdev_dec_pending(rdev, mddev);
1907 rcu_read_lock();
1908 if (success)
1909 break;
1910 }
1911 sl++;
1912 if (sl == conf->copies)
1913 sl = 0;
1914 } while (!success && sl != r10_bio->read_slot);
1915 rcu_read_unlock();
1916
1917 if (!success) {
NeilBrown58c54fc2011-07-28 11:39:25 +10001918 /* Cannot read from anywhere, just mark the block
1919 * as bad on the first device to discourage future
1920 * reads.
1921 */
NeilBrown6814d532006-10-03 01:15:45 -07001922 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
NeilBrown58c54fc2011-07-28 11:39:25 +10001923 rdev = conf->mirrors[dn].rdev;
1924
1925 if (!rdev_set_badblocks(
1926 rdev,
1927 r10_bio->devs[r10_bio->read_slot].addr
1928 + sect,
1929 s, 0))
1930 md_error(mddev, rdev);
NeilBrown6814d532006-10-03 01:15:45 -07001931 break;
1932 }
1933
1934 start = sl;
1935 /* write it back and re-read */
1936 rcu_read_lock();
1937 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11001938 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001939
NeilBrown6814d532006-10-03 01:15:45 -07001940 if (sl==0)
1941 sl = conf->copies;
1942 sl--;
1943 d = r10_bio->devs[sl].devnum;
1944 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10001945 if (!rdev ||
1946 !test_bit(In_sync, &rdev->flags))
1947 continue;
1948
1949 atomic_inc(&rdev->nr_pending);
1950 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10001951 if (r10_sync_page_io(rdev,
1952 r10_bio->devs[sl].addr +
1953 sect,
1954 s<<9, conf->tmppage, WRITE)
NeilBrown1294b9c2011-07-28 11:39:23 +10001955 == 0) {
1956 /* Well, this device is dead */
1957 printk(KERN_NOTICE
1958 "md/raid10:%s: read correction "
1959 "write failed"
1960 " (%d sectors at %llu on %s)\n",
1961 mdname(mddev), s,
1962 (unsigned long long)(
1963 sect + rdev->data_offset),
1964 bdevname(rdev->bdev, b));
1965 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
1966 "drive\n",
1967 mdname(mddev),
1968 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07001969 }
NeilBrown1294b9c2011-07-28 11:39:23 +10001970 rdev_dec_pending(rdev, mddev);
1971 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07001972 }
1973 sl = start;
1974 while (sl != r10_bio->read_slot) {
NeilBrown1294b9c2011-07-28 11:39:23 +10001975 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001976
NeilBrown6814d532006-10-03 01:15:45 -07001977 if (sl==0)
1978 sl = conf->copies;
1979 sl--;
1980 d = r10_bio->devs[sl].devnum;
1981 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10001982 if (!rdev ||
1983 !test_bit(In_sync, &rdev->flags))
1984 continue;
Robert Becker67b8dc42009-12-14 12:49:57 +11001985
NeilBrown1294b9c2011-07-28 11:39:23 +10001986 atomic_inc(&rdev->nr_pending);
1987 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10001988 switch (r10_sync_page_io(rdev,
1989 r10_bio->devs[sl].addr +
1990 sect,
1991 s<<9, conf->tmppage,
1992 READ)) {
1993 case 0:
NeilBrown1294b9c2011-07-28 11:39:23 +10001994 /* Well, this device is dead */
1995 printk(KERN_NOTICE
1996 "md/raid10:%s: unable to read back "
1997 "corrected sectors"
1998 " (%d sectors at %llu on %s)\n",
1999 mdname(mddev), s,
2000 (unsigned long long)(
2001 sect + rdev->data_offset),
2002 bdevname(rdev->bdev, b));
2003 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2004 "drive\n",
2005 mdname(mddev),
2006 bdevname(rdev->bdev, b));
NeilBrown58c54fc2011-07-28 11:39:25 +10002007 break;
2008 case 1:
NeilBrown1294b9c2011-07-28 11:39:23 +10002009 printk(KERN_INFO
2010 "md/raid10:%s: read error corrected"
2011 " (%d sectors at %llu on %s)\n",
2012 mdname(mddev), s,
2013 (unsigned long long)(
2014 sect + rdev->data_offset),
2015 bdevname(rdev->bdev, b));
2016 atomic_add(s, &rdev->corrected_errors);
NeilBrown6814d532006-10-03 01:15:45 -07002017 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002018
2019 rdev_dec_pending(rdev, mddev);
2020 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002021 }
2022 rcu_read_unlock();
2023
2024 sectors -= s;
2025 sect += s;
2026 }
2027}
2028
NeilBrownbd870a12011-07-28 11:39:24 +10002029static void bi_complete(struct bio *bio, int error)
2030{
2031 complete((struct completion *)bio->bi_private);
2032}
2033
2034static int submit_bio_wait(int rw, struct bio *bio)
2035{
2036 struct completion event;
2037 rw |= REQ_SYNC;
2038
2039 init_completion(&event);
2040 bio->bi_private = &event;
2041 bio->bi_end_io = bi_complete;
2042 submit_bio(rw, bio);
2043 wait_for_completion(&event);
2044
2045 return test_bit(BIO_UPTODATE, &bio->bi_flags);
2046}
2047
NeilBrown9f2c9d12011-10-11 16:48:43 +11002048static int narrow_write_error(struct r10bio *r10_bio, int i)
NeilBrownbd870a12011-07-28 11:39:24 +10002049{
2050 struct bio *bio = r10_bio->master_bio;
NeilBrownfd01b882011-10-11 16:47:53 +11002051 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002052 struct r10conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002053 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
NeilBrownbd870a12011-07-28 11:39:24 +10002054 /* bio has the data to be written to slot 'i' where
2055 * we just recently had a write error.
2056 * We repeatedly clone the bio and trim down to one block,
2057 * then try the write. Where the write fails we record
2058 * a bad block.
2059 * It is conceivable that the bio doesn't exactly align with
2060 * blocks. We must handle this.
2061 *
2062 * We currently own a reference to the rdev.
2063 */
2064
2065 int block_sectors;
2066 sector_t sector;
2067 int sectors;
2068 int sect_to_write = r10_bio->sectors;
2069 int ok = 1;
2070
2071 if (rdev->badblocks.shift < 0)
2072 return 0;
2073
2074 block_sectors = 1 << rdev->badblocks.shift;
2075 sector = r10_bio->sector;
2076 sectors = ((r10_bio->sector + block_sectors)
2077 & ~(sector_t)(block_sectors - 1))
2078 - sector;
2079
2080 while (sect_to_write) {
2081 struct bio *wbio;
2082 if (sectors > sect_to_write)
2083 sectors = sect_to_write;
2084 /* Write at 'sector' for 'sectors' */
2085 wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
2086 md_trim_bio(wbio, sector - bio->bi_sector, sectors);
2087 wbio->bi_sector = (r10_bio->devs[i].addr+
2088 rdev->data_offset+
2089 (sector - r10_bio->sector));
2090 wbio->bi_bdev = rdev->bdev;
2091 if (submit_bio_wait(WRITE, wbio) == 0)
2092 /* Failure! */
2093 ok = rdev_set_badblocks(rdev, sector,
2094 sectors, 0)
2095 && ok;
2096
2097 bio_put(wbio);
2098 sect_to_write -= sectors;
2099 sector += sectors;
2100 sectors = block_sectors;
2101 }
2102 return ok;
2103}
2104
NeilBrown9f2c9d12011-10-11 16:48:43 +11002105static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown560f8e52011-07-28 11:39:23 +10002106{
2107 int slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002108 struct bio *bio;
NeilBrowne879a872011-10-11 16:49:02 +11002109 struct r10conf *conf = mddev->private;
NeilBrownabbf0982011-12-23 10:17:54 +11002110 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002111 char b[BDEVNAME_SIZE];
2112 unsigned long do_sync;
NeilBrown856e08e2011-07-28 11:39:23 +10002113 int max_sectors;
NeilBrown560f8e52011-07-28 11:39:23 +10002114
2115 /* we got a read error. Maybe the drive is bad. Maybe just
2116 * the block and we can fix it.
2117 * We freeze all other IO, and try reading the block from
2118 * other devices. When we find one, we re-write
2119 * and check it that fixes the read error.
2120 * This is all done synchronously while the array is
2121 * frozen.
2122 */
2123 if (mddev->ro == 0) {
2124 freeze_array(conf);
2125 fix_read_error(conf, mddev, r10_bio);
2126 unfreeze_array(conf);
2127 }
NeilBrownabbf0982011-12-23 10:17:54 +11002128 rdev_dec_pending(rdev, mddev);
NeilBrown560f8e52011-07-28 11:39:23 +10002129
2130 bio = r10_bio->devs[slot].bio;
NeilBrown7399c312011-07-28 11:39:23 +10002131 bdevname(bio->bi_bdev, b);
NeilBrown560f8e52011-07-28 11:39:23 +10002132 r10_bio->devs[slot].bio =
2133 mddev->ro ? IO_BLOCKED : NULL;
NeilBrown7399c312011-07-28 11:39:23 +10002134read_more:
NeilBrown96c3fd12011-12-23 10:17:54 +11002135 rdev = read_balance(conf, r10_bio, &max_sectors);
2136 if (rdev == NULL) {
NeilBrown560f8e52011-07-28 11:39:23 +10002137 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
2138 " read error for block %llu\n",
NeilBrown7399c312011-07-28 11:39:23 +10002139 mdname(mddev), b,
NeilBrown560f8e52011-07-28 11:39:23 +10002140 (unsigned long long)r10_bio->sector);
2141 raid_end_bio_io(r10_bio);
2142 bio_put(bio);
2143 return;
2144 }
2145
2146 do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
NeilBrown7399c312011-07-28 11:39:23 +10002147 if (bio)
2148 bio_put(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002149 slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002150 printk_ratelimited(
2151 KERN_ERR
2152 "md/raid10:%s: %s: redirecting"
2153 "sector %llu to another mirror\n",
2154 mdname(mddev),
2155 bdevname(rdev->bdev, b),
2156 (unsigned long long)r10_bio->sector);
2157 bio = bio_clone_mddev(r10_bio->master_bio,
2158 GFP_NOIO, mddev);
NeilBrown7399c312011-07-28 11:39:23 +10002159 md_trim_bio(bio,
2160 r10_bio->sector - bio->bi_sector,
2161 max_sectors);
NeilBrown560f8e52011-07-28 11:39:23 +10002162 r10_bio->devs[slot].bio = bio;
NeilBrownabbf0982011-12-23 10:17:54 +11002163 r10_bio->devs[slot].rdev = rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002164 bio->bi_sector = r10_bio->devs[slot].addr
2165 + rdev->data_offset;
2166 bio->bi_bdev = rdev->bdev;
2167 bio->bi_rw = READ | do_sync;
2168 bio->bi_private = r10_bio;
2169 bio->bi_end_io = raid10_end_read_request;
NeilBrown7399c312011-07-28 11:39:23 +10002170 if (max_sectors < r10_bio->sectors) {
2171 /* Drat - have to split this up more */
2172 struct bio *mbio = r10_bio->master_bio;
2173 int sectors_handled =
2174 r10_bio->sector + max_sectors
2175 - mbio->bi_sector;
2176 r10_bio->sectors = max_sectors;
2177 spin_lock_irq(&conf->device_lock);
2178 if (mbio->bi_phys_segments == 0)
2179 mbio->bi_phys_segments = 2;
2180 else
2181 mbio->bi_phys_segments++;
2182 spin_unlock_irq(&conf->device_lock);
2183 generic_make_request(bio);
2184 bio = NULL;
2185
2186 r10_bio = mempool_alloc(conf->r10bio_pool,
2187 GFP_NOIO);
2188 r10_bio->master_bio = mbio;
2189 r10_bio->sectors = (mbio->bi_size >> 9)
2190 - sectors_handled;
2191 r10_bio->state = 0;
2192 set_bit(R10BIO_ReadError,
2193 &r10_bio->state);
2194 r10_bio->mddev = mddev;
2195 r10_bio->sector = mbio->bi_sector
2196 + sectors_handled;
2197
2198 goto read_more;
2199 } else
2200 generic_make_request(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002201}
2202
NeilBrowne879a872011-10-11 16:49:02 +11002203static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
NeilBrown749c55e2011-07-28 11:39:24 +10002204{
2205 /* Some sort of write request has finished and it
2206 * succeeded in writing where we thought there was a
2207 * bad block. So forget the bad block.
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002208 * Or possibly if failed and we need to record
2209 * a bad block.
NeilBrown749c55e2011-07-28 11:39:24 +10002210 */
2211 int m;
NeilBrown3cb03002011-10-11 16:45:26 +11002212 struct md_rdev *rdev;
NeilBrown749c55e2011-07-28 11:39:24 +10002213
2214 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2215 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002216 for (m = 0; m < conf->copies; m++) {
2217 int dev = r10_bio->devs[m].devnum;
2218 rdev = conf->mirrors[dev].rdev;
2219 if (r10_bio->devs[m].bio == NULL)
2220 continue;
2221 if (test_bit(BIO_UPTODATE,
NeilBrown749c55e2011-07-28 11:39:24 +10002222 &r10_bio->devs[m].bio->bi_flags)) {
NeilBrown749c55e2011-07-28 11:39:24 +10002223 rdev_clear_badblocks(
2224 rdev,
2225 r10_bio->devs[m].addr,
2226 r10_bio->sectors);
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002227 } else {
2228 if (!rdev_set_badblocks(
2229 rdev,
2230 r10_bio->devs[m].addr,
2231 r10_bio->sectors, 0))
2232 md_error(conf->mddev, rdev);
NeilBrown749c55e2011-07-28 11:39:24 +10002233 }
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002234 }
NeilBrown749c55e2011-07-28 11:39:24 +10002235 put_buf(r10_bio);
2236 } else {
NeilBrownbd870a12011-07-28 11:39:24 +10002237 for (m = 0; m < conf->copies; m++) {
2238 int dev = r10_bio->devs[m].devnum;
2239 struct bio *bio = r10_bio->devs[m].bio;
2240 rdev = conf->mirrors[dev].rdev;
2241 if (bio == IO_MADE_GOOD) {
NeilBrown749c55e2011-07-28 11:39:24 +10002242 rdev_clear_badblocks(
2243 rdev,
2244 r10_bio->devs[m].addr,
2245 r10_bio->sectors);
2246 rdev_dec_pending(rdev, conf->mddev);
NeilBrownbd870a12011-07-28 11:39:24 +10002247 } else if (bio != NULL &&
2248 !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2249 if (!narrow_write_error(r10_bio, m)) {
2250 md_error(conf->mddev, rdev);
2251 set_bit(R10BIO_Degraded,
2252 &r10_bio->state);
2253 }
2254 rdev_dec_pending(rdev, conf->mddev);
NeilBrown749c55e2011-07-28 11:39:24 +10002255 }
NeilBrownbd870a12011-07-28 11:39:24 +10002256 }
2257 if (test_bit(R10BIO_WriteError,
2258 &r10_bio->state))
2259 close_write(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10002260 raid_end_bio_io(r10_bio);
2261 }
2262}
2263
NeilBrownfd01b882011-10-11 16:47:53 +11002264static void raid10d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
NeilBrown9f2c9d12011-10-11 16:48:43 +11002266 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 unsigned long flags;
NeilBrowne879a872011-10-11 16:49:02 +11002268 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002270 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
2272 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002274 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002276
Jens Axboe7eaceac2011-03-10 08:52:07 +01002277 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002280 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002281 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002283 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002284 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08002286 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 spin_unlock_irqrestore(&conf->device_lock, flags);
2288
2289 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002290 conf = mddev->private;
NeilBrownbd870a12011-07-28 11:39:24 +10002291 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2292 test_bit(R10BIO_WriteError, &r10_bio->state))
NeilBrown749c55e2011-07-28 11:39:24 +10002293 handle_write_completed(conf, r10_bio);
2294 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01002296 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 recovery_request_write(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002298 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
NeilBrown560f8e52011-07-28 11:39:23 +10002299 handle_read_error(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002300 else {
2301 /* just a partial read to be scheduled from a
2302 * separate context
2303 */
2304 int slot = r10_bio->read_slot;
2305 generic_make_request(r10_bio->devs[slot].bio);
2306 }
NeilBrown4443ae12006-01-06 00:20:28 -08002307
NeilBrown1d9d5242009-10-16 15:55:32 +11002308 cond_resched();
NeilBrownde393cd2011-07-28 11:31:48 +10002309 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2310 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002312 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313}
2314
2315
NeilBrowne879a872011-10-11 16:49:02 +11002316static int init_resync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
2318 int buffs;
NeilBrown69335ef2011-12-23 10:17:54 +11002319 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02002322 BUG_ON(conf->r10buf_pool);
NeilBrown69335ef2011-12-23 10:17:54 +11002323 conf->have_replacement = 0;
2324 for (i = 0; i < conf->raid_disks; i++)
2325 if (conf->mirrors[i].replacement)
2326 conf->have_replacement = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2328 if (!conf->r10buf_pool)
2329 return -ENOMEM;
2330 conf->next_resync = 0;
2331 return 0;
2332}
2333
2334/*
2335 * perform a "sync" on one "block"
2336 *
2337 * We need to make sure that no normal I/O request - particularly write
2338 * requests - conflict with active sync requests.
2339 *
2340 * This is achieved by tracking pending requests and a 'barrier' concept
2341 * that can be installed to exclude normal IO requests.
2342 *
2343 * Resync and recovery are handled very differently.
2344 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2345 *
2346 * For resync, we iterate over virtual addresses, read all copies,
2347 * and update if there are differences. If only one copy is live,
2348 * skip it.
2349 * For recovery, we iterate over physical addresses, read a good
2350 * value for each non-in_sync drive, and over-write.
2351 *
2352 * So, for recovery we may have several outstanding complex requests for a
2353 * given address, one for each out-of-sync device. We model this by allocating
2354 * a number of r10_bio structures, one for each out-of-sync device.
2355 * As we setup these structures, we collect all bio's together into a list
2356 * which we then process collectively to add pages, and then process again
2357 * to pass to generic_make_request.
2358 *
2359 * The r10_bio structures are linked using a borrowed master_bio pointer.
2360 * This link is counted in ->remaining. When the r10_bio that points to NULL
2361 * has its remaining count decremented to 0, the whole complex operation
2362 * is complete.
2363 *
2364 */
2365
NeilBrownfd01b882011-10-11 16:47:53 +11002366static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
NeilBrownab9d47e2011-05-11 14:54:41 +10002367 int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
NeilBrowne879a872011-10-11 16:49:02 +11002369 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002370 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 struct bio *biolist = NULL, *bio;
2372 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08002374 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11002375 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 sector_t sectors_skipped = 0;
2377 int chunks_skipped = 0;
2378
2379 if (!conf->r10buf_pool)
2380 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002381 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11002384 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2386 max_sector = mddev->resync_max_sectors;
2387 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002388 /* If we aborted, we need to abort the
2389 * sync on the 'current' bitmap chucks (there can
2390 * be several when recovering multiple devices).
2391 * as we may have started syncing it but not finished.
2392 * We can find the current address in
2393 * mddev->curr_resync, but for recovery,
2394 * we need to convert that to several
2395 * virtual addresses.
2396 */
2397 if (mddev->curr_resync < max_sector) { /* aborted */
2398 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2399 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2400 &sync_blocks, 1);
2401 else for (i=0; i<conf->raid_disks; i++) {
2402 sector_t sect =
2403 raid10_find_virt(conf, mddev->curr_resync, i);
2404 bitmap_end_sync(mddev->bitmap, sect,
2405 &sync_blocks, 1);
2406 }
2407 } else /* completed sync */
2408 conf->fullsync = 0;
2409
2410 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07002412 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 return sectors_skipped;
2414 }
2415 if (chunks_skipped >= conf->raid_disks) {
2416 /* if there has been nothing to do on any drive,
2417 * then there is nothing to do at all..
2418 */
NeilBrown57afd892005-06-21 17:17:13 -07002419 *skipped = 1;
2420 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 }
2422
NeilBrownc6207272008-02-06 01:39:52 -08002423 if (max_sector > mddev->resync_max)
2424 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 /* make sure whole request will fit in a chunk - if chunks
2427 * are meaningful
2428 */
2429 if (conf->near_copies < conf->raid_disks &&
2430 max_sector > (sector_nr | conf->chunk_mask))
2431 max_sector = (sector_nr | conf->chunk_mask) + 1;
2432 /*
2433 * If there is non-resync activity waiting for us then
2434 * put in a delay to throttle resync.
2435 */
NeilBrown0a27ec92006-01-06 00:20:13 -08002436 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
2439 /* Again, very different code for resync and recovery.
2440 * Both must result in an r10bio with a list of bios that
2441 * have bi_end_io, bi_sector, bi_bdev set,
2442 * and bi_private set to the r10bio.
2443 * For recovery, we may actually create several r10bios
2444 * with 2 bios in each, that correspond to the bios in the main one.
2445 * In this case, the subordinate r10bios link back through a
2446 * borrowed master_bio pointer, and the counter in the master
2447 * includes a ref from each subordinate.
2448 */
2449 /* First, we decide what to do and set ->bi_end_io
2450 * To end_sync_read if we want to read, and
2451 * end_sync_write if we will want to write.
2452 */
2453
NeilBrown6cce3b22006-01-06 00:20:16 -08002454 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2456 /* recovery... the complicated one */
NeilBrowne875ece2011-07-28 11:39:24 +10002457 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 r10_bio = NULL;
2459
NeilBrownab9d47e2011-05-11 14:54:41 +10002460 for (i=0 ; i<conf->raid_disks; i++) {
2461 int still_degraded;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002462 struct r10bio *rb2;
NeilBrownab9d47e2011-05-11 14:54:41 +10002463 sector_t sect;
2464 int must_sync;
NeilBrowne875ece2011-07-28 11:39:24 +10002465 int any_working;
NeilBrownab9d47e2011-05-11 14:54:41 +10002466
2467 if (conf->mirrors[i].rdev == NULL ||
2468 test_bit(In_sync, &conf->mirrors[i].rdev->flags))
2469 continue;
2470
2471 still_degraded = 0;
2472 /* want to reconstruct this device */
2473 rb2 = r10_bio;
2474 sect = raid10_find_virt(conf, sector_nr, i);
2475 /* Unless we are doing a full sync, we only need
2476 * to recover the block if it is set in the bitmap
2477 */
2478 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2479 &sync_blocks, 1);
2480 if (sync_blocks < max_sync)
2481 max_sync = sync_blocks;
2482 if (!must_sync &&
2483 !conf->fullsync) {
2484 /* yep, skip the sync_blocks here, but don't assume
2485 * that there will never be anything to do here
NeilBrown6cce3b22006-01-06 00:20:16 -08002486 */
NeilBrownab9d47e2011-05-11 14:54:41 +10002487 chunks_skipped = -1;
2488 continue;
2489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
NeilBrownab9d47e2011-05-11 14:54:41 +10002491 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2492 raise_barrier(conf, rb2 != NULL);
2493 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
NeilBrownab9d47e2011-05-11 14:54:41 +10002495 r10_bio->master_bio = (struct bio*)rb2;
2496 if (rb2)
2497 atomic_inc(&rb2->remaining);
2498 r10_bio->mddev = mddev;
2499 set_bit(R10BIO_IsRecover, &r10_bio->state);
2500 r10_bio->sector = sect;
NeilBrown6cce3b22006-01-06 00:20:16 -08002501
NeilBrownab9d47e2011-05-11 14:54:41 +10002502 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10002503
NeilBrownab9d47e2011-05-11 14:54:41 +10002504 /* Need to check if the array will still be
2505 * degraded
2506 */
2507 for (j=0; j<conf->raid_disks; j++)
2508 if (conf->mirrors[j].rdev == NULL ||
2509 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
2510 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07002511 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002513
2514 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2515 &sync_blocks, still_degraded);
2516
NeilBrowne875ece2011-07-28 11:39:24 +10002517 any_working = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10002518 for (j=0; j<conf->copies;j++) {
NeilBrowne875ece2011-07-28 11:39:24 +10002519 int k;
NeilBrownab9d47e2011-05-11 14:54:41 +10002520 int d = r10_bio->devs[j].devnum;
NeilBrown5e570282011-07-28 11:39:25 +10002521 sector_t from_addr, to_addr;
NeilBrown3cb03002011-10-11 16:45:26 +11002522 struct md_rdev *rdev;
NeilBrown40c356c2011-07-28 11:39:24 +10002523 sector_t sector, first_bad;
2524 int bad_sectors;
NeilBrownab9d47e2011-05-11 14:54:41 +10002525 if (!conf->mirrors[d].rdev ||
2526 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
2527 continue;
2528 /* This is where we read from */
NeilBrowne875ece2011-07-28 11:39:24 +10002529 any_working = 1;
NeilBrown40c356c2011-07-28 11:39:24 +10002530 rdev = conf->mirrors[d].rdev;
2531 sector = r10_bio->devs[j].addr;
2532
2533 if (is_badblock(rdev, sector, max_sync,
2534 &first_bad, &bad_sectors)) {
2535 if (first_bad > sector)
2536 max_sync = first_bad - sector;
2537 else {
2538 bad_sectors -= (sector
2539 - first_bad);
2540 if (max_sync > bad_sectors)
2541 max_sync = bad_sectors;
2542 continue;
2543 }
2544 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002545 bio = r10_bio->devs[0].bio;
2546 bio->bi_next = biolist;
2547 biolist = bio;
2548 bio->bi_private = r10_bio;
2549 bio->bi_end_io = end_sync_read;
2550 bio->bi_rw = READ;
NeilBrown5e570282011-07-28 11:39:25 +10002551 from_addr = r10_bio->devs[j].addr;
2552 bio->bi_sector = from_addr +
NeilBrownab9d47e2011-05-11 14:54:41 +10002553 conf->mirrors[d].rdev->data_offset;
2554 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2555 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2556 atomic_inc(&r10_bio->remaining);
2557 /* and we write to 'i' */
2558
2559 for (k=0; k<conf->copies; k++)
2560 if (r10_bio->devs[k].devnum == i)
2561 break;
2562 BUG_ON(k == conf->copies);
2563 bio = r10_bio->devs[1].bio;
2564 bio->bi_next = biolist;
2565 biolist = bio;
2566 bio->bi_private = r10_bio;
2567 bio->bi_end_io = end_sync_write;
2568 bio->bi_rw = WRITE;
NeilBrown5e570282011-07-28 11:39:25 +10002569 to_addr = r10_bio->devs[k].addr;
2570 bio->bi_sector = to_addr +
NeilBrownab9d47e2011-05-11 14:54:41 +10002571 conf->mirrors[i].rdev->data_offset;
2572 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
2573
2574 r10_bio->devs[0].devnum = d;
NeilBrown5e570282011-07-28 11:39:25 +10002575 r10_bio->devs[0].addr = from_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10002576 r10_bio->devs[1].devnum = i;
NeilBrown5e570282011-07-28 11:39:25 +10002577 r10_bio->devs[1].addr = to_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10002578
2579 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002581 if (j == conf->copies) {
NeilBrowne875ece2011-07-28 11:39:24 +10002582 /* Cannot recover, so abort the recovery or
2583 * record a bad block */
NeilBrownab9d47e2011-05-11 14:54:41 +10002584 put_buf(r10_bio);
2585 if (rb2)
2586 atomic_dec(&rb2->remaining);
2587 r10_bio = rb2;
NeilBrowne875ece2011-07-28 11:39:24 +10002588 if (any_working) {
2589 /* problem is that there are bad blocks
2590 * on other device(s)
2591 */
2592 int k;
2593 for (k = 0; k < conf->copies; k++)
2594 if (r10_bio->devs[k].devnum == i)
2595 break;
2596 if (!rdev_set_badblocks(
2597 conf->mirrors[i].rdev,
2598 r10_bio->devs[k].addr,
2599 max_sync, 0))
2600 any_working = 0;
2601 }
2602 if (!any_working) {
2603 if (!test_and_set_bit(MD_RECOVERY_INTR,
2604 &mddev->recovery))
2605 printk(KERN_INFO "md/raid10:%s: insufficient "
2606 "working devices for recovery.\n",
2607 mdname(mddev));
2608 conf->mirrors[i].recovery_disabled
2609 = mddev->recovery_disabled;
2610 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002611 break;
2612 }
2613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 if (biolist == NULL) {
2615 while (r10_bio) {
NeilBrown9f2c9d12011-10-11 16:48:43 +11002616 struct r10bio *rb2 = r10_bio;
2617 r10_bio = (struct r10bio*) rb2->master_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 rb2->master_bio = NULL;
2619 put_buf(rb2);
2620 }
2621 goto giveup;
2622 }
2623 } else {
2624 /* resync. Schedule a read for every block at this virt offset */
2625 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08002626
NeilBrown78200d42009-02-25 13:18:47 +11002627 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
2628
NeilBrown6cce3b22006-01-06 00:20:16 -08002629 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2630 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10002631 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
2632 &mddev->recovery)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002633 /* We can skip this block */
2634 *skipped = 1;
2635 return sync_blocks + sectors_skipped;
2636 }
2637 if (sync_blocks < max_sync)
2638 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2640
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 r10_bio->mddev = mddev;
2642 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08002643 raise_barrier(conf, 0);
2644 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
2646 r10_bio->master_bio = NULL;
2647 r10_bio->sector = sector_nr;
2648 set_bit(R10BIO_IsSync, &r10_bio->state);
2649 raid10_find_phys(conf, r10_bio);
2650 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
2651
2652 for (i=0; i<conf->copies; i++) {
2653 int d = r10_bio->devs[i].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10002654 sector_t first_bad, sector;
2655 int bad_sectors;
2656
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 bio = r10_bio->devs[i].bio;
2658 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002659 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08002661 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 continue;
NeilBrown40c356c2011-07-28 11:39:24 +10002663 sector = r10_bio->devs[i].addr;
2664 if (is_badblock(conf->mirrors[d].rdev,
2665 sector, max_sync,
2666 &first_bad, &bad_sectors)) {
2667 if (first_bad > sector)
2668 max_sync = first_bad - sector;
2669 else {
2670 bad_sectors -= (sector - first_bad);
2671 if (max_sync > bad_sectors)
2672 max_sync = max_sync;
2673 continue;
2674 }
2675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2677 atomic_inc(&r10_bio->remaining);
2678 bio->bi_next = biolist;
2679 biolist = bio;
2680 bio->bi_private = r10_bio;
2681 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08002682 bio->bi_rw = READ;
NeilBrown40c356c2011-07-28 11:39:24 +10002683 bio->bi_sector = sector +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 conf->mirrors[d].rdev->data_offset;
2685 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2686 count++;
2687 }
2688
2689 if (count < 2) {
2690 for (i=0; i<conf->copies; i++) {
2691 int d = r10_bio->devs[i].devnum;
2692 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10002693 rdev_dec_pending(conf->mirrors[d].rdev,
2694 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 }
2696 put_buf(r10_bio);
2697 biolist = NULL;
2698 goto giveup;
2699 }
2700 }
2701
2702 for (bio = biolist; bio ; bio=bio->bi_next) {
2703
2704 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
2705 if (bio->bi_end_io)
2706 bio->bi_flags |= 1 << BIO_UPTODATE;
2707 bio->bi_vcnt = 0;
2708 bio->bi_idx = 0;
2709 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 bio->bi_size = 0;
2711 }
2712
2713 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08002714 if (sector_nr + max_sync < max_sector)
2715 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 do {
2717 struct page *page;
2718 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 if (sector_nr + (len>>9) > max_sector)
2720 len = (max_sector - sector_nr) << 9;
2721 if (len == 0)
2722 break;
2723 for (bio= biolist ; bio ; bio=bio->bi_next) {
NeilBrownab9d47e2011-05-11 14:54:41 +10002724 struct bio *bio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
NeilBrownab9d47e2011-05-11 14:54:41 +10002726 if (bio_add_page(bio, page, len, 0))
2727 continue;
2728
2729 /* stop here */
2730 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
2731 for (bio2 = biolist;
2732 bio2 && bio2 != bio;
2733 bio2 = bio2->bi_next) {
2734 /* remove last page from this bio */
2735 bio2->bi_vcnt--;
2736 bio2->bi_size -= len;
2737 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002739 goto bio_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 }
2741 nr_sectors += len>>9;
2742 sector_nr += len>>9;
2743 } while (biolist->bi_vcnt < RESYNC_PAGES);
2744 bio_full:
2745 r10_bio->sectors = nr_sectors;
2746
2747 while (biolist) {
2748 bio = biolist;
2749 biolist = biolist->bi_next;
2750
2751 bio->bi_next = NULL;
2752 r10_bio = bio->bi_private;
2753 r10_bio->sectors = nr_sectors;
2754
2755 if (bio->bi_end_io == end_sync_read) {
2756 md_sync_acct(bio->bi_bdev, nr_sectors);
2757 generic_make_request(bio);
2758 }
2759 }
2760
NeilBrown57afd892005-06-21 17:17:13 -07002761 if (sectors_skipped)
2762 /* pretend they weren't skipped, it makes
2763 * no important difference in this case
2764 */
2765 md_done_sync(mddev, sectors_skipped, 1);
2766
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 return sectors_skipped + nr_sectors;
2768 giveup:
2769 /* There is nowhere to write, so all non-sync
NeilBrowne875ece2011-07-28 11:39:24 +10002770 * drives must be failed or in resync, all drives
2771 * have a bad block, so try the next chunk...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 */
NeilBrown09b40682009-02-25 13:18:47 +11002773 if (sector_nr + max_sync < max_sector)
2774 max_sector = sector_nr + max_sync;
2775
2776 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 chunks_skipped ++;
2778 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780}
2781
Dan Williams80c3a6c2009-03-17 18:10:40 -07002782static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11002783raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07002784{
2785 sector_t size;
NeilBrowne879a872011-10-11 16:49:02 +11002786 struct r10conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002787
2788 if (!raid_disks)
NeilBrown84707f32010-03-16 17:23:35 +11002789 raid_disks = conf->raid_disks;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002790 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002791 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002792
2793 size = sectors >> conf->chunk_shift;
2794 sector_div(size, conf->far_copies);
2795 size = size * raid_disks;
2796 sector_div(size, conf->near_copies);
2797
2798 return size << conf->chunk_shift;
2799}
2800
Trela, Maciejdab8b292010-03-08 16:02:45 +11002801
NeilBrowne879a872011-10-11 16:49:02 +11002802static struct r10conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803{
NeilBrowne879a872011-10-11 16:49:02 +11002804 struct r10conf *conf = NULL;
NeilBrownc93983b2006-06-26 00:27:41 -07002805 int nc, fc, fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 sector_t stride, size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002807 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Maciej Trelaf73ea872010-06-16 11:46:29 +01002809 if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
2810 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown128595e2010-05-03 14:47:14 +10002811 printk(KERN_ERR "md/raid10:%s: chunk size must be "
2812 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
2813 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002814 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 }
NeilBrown2604b702006-01-06 00:20:36 -08002816
Maciej Trelaf73ea872010-06-16 11:46:29 +01002817 nc = mddev->new_layout & 255;
2818 fc = (mddev->new_layout >> 8) & 255;
2819 fo = mddev->new_layout & (1<<16);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002820
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
Maciej Trelaf73ea872010-06-16 11:46:29 +01002822 (mddev->new_layout >> 17)) {
NeilBrown128595e2010-05-03 14:47:14 +10002823 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
Maciej Trelaf73ea872010-06-16 11:46:29 +01002824 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 goto out;
2826 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11002827
2828 err = -ENOMEM;
NeilBrowne879a872011-10-11 16:49:02 +11002829 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002830 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002832
NeilBrown4443ae12006-01-06 00:20:28 -08002833 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Trela, Maciejdab8b292010-03-08 16:02:45 +11002834 GFP_KERNEL);
2835 if (!conf->mirrors)
2836 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08002837
2838 conf->tmppage = alloc_page(GFP_KERNEL);
2839 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002840 goto out;
2841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
NeilBrown64a742b2007-02-28 20:11:18 -08002843 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 conf->near_copies = nc;
2845 conf->far_copies = fc;
2846 conf->copies = nc*fc;
NeilBrownc93983b2006-06-26 00:27:41 -07002847 conf->far_offset = fo;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002848 conf->chunk_mask = mddev->new_chunk_sectors - 1;
2849 conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
2850
2851 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2852 r10bio_pool_free, conf);
2853 if (!conf->r10bio_pool)
2854 goto out;
2855
Andre Noll58c0fed2009-03-31 14:33:13 +11002856 size = mddev->dev_sectors >> conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002857 sector_div(size, fc);
2858 size = size * conf->raid_disks;
2859 sector_div(size, nc);
2860 /* 'size' is now the number of chunks in the array */
2861 /* calculate "used chunks per device" in 'stride' */
2862 stride = size * conf->copies;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002863
2864 /* We need to round up when dividing by raid_disks to
2865 * get the stride size.
2866 */
2867 stride += conf->raid_disks - 1;
NeilBrown64a742b2007-02-28 20:11:18 -08002868 sector_div(stride, conf->raid_disks);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002869
2870 conf->dev_sectors = stride << conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002871
NeilBrownc93983b2006-06-26 00:27:41 -07002872 if (fo)
NeilBrown64a742b2007-02-28 20:11:18 -08002873 stride = 1;
2874 else
NeilBrownc93983b2006-06-26 00:27:41 -07002875 sector_div(stride, fc);
NeilBrown64a742b2007-02-28 20:11:18 -08002876 conf->stride = stride << conf->chunk_shift;
2877
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
Neil Browne7e72bf2008-05-14 16:05:54 -07002879 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002880 INIT_LIST_HEAD(&conf->retry_list);
2881
2882 spin_lock_init(&conf->resync_lock);
2883 init_waitqueue_head(&conf->wait_barrier);
2884
2885 conf->thread = md_register_thread(raid10d, mddev, NULL);
2886 if (!conf->thread)
2887 goto out;
2888
Trela, Maciejdab8b292010-03-08 16:02:45 +11002889 conf->mddev = mddev;
2890 return conf;
2891
2892 out:
NeilBrown128595e2010-05-03 14:47:14 +10002893 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
Trela, Maciejdab8b292010-03-08 16:02:45 +11002894 mdname(mddev));
2895 if (conf) {
2896 if (conf->r10bio_pool)
2897 mempool_destroy(conf->r10bio_pool);
2898 kfree(conf->mirrors);
2899 safe_put_page(conf->tmppage);
2900 kfree(conf);
2901 }
2902 return ERR_PTR(err);
2903}
2904
NeilBrownfd01b882011-10-11 16:47:53 +11002905static int run(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002906{
NeilBrowne879a872011-10-11 16:49:02 +11002907 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002908 int i, disk_idx, chunk_size;
NeilBrown0f6d02d2011-10-11 16:48:46 +11002909 struct mirror_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11002910 struct md_rdev *rdev;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002911 sector_t size;
2912
2913 /*
2914 * copy the already verified devices into our private RAID10
2915 * bookkeeping area. [whatever we allocate in run(),
2916 * should be freed in stop()]
2917 */
2918
2919 if (mddev->private == NULL) {
2920 conf = setup_conf(mddev);
2921 if (IS_ERR(conf))
2922 return PTR_ERR(conf);
2923 mddev->private = conf;
2924 }
2925 conf = mddev->private;
2926 if (!conf)
2927 goto out;
2928
Trela, Maciejdab8b292010-03-08 16:02:45 +11002929 mddev->thread = conf->thread;
2930 conf->thread = NULL;
2931
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002932 chunk_size = mddev->chunk_sectors << 9;
2933 blk_queue_io_min(mddev->queue, chunk_size);
2934 if (conf->raid_disks % conf->near_copies)
2935 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
2936 else
2937 blk_queue_io_opt(mddev->queue, chunk_size *
2938 (conf->raid_disks / conf->near_copies));
2939
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002940 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown34b343c2011-07-28 11:31:47 +10002941
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 disk_idx = rdev->raid_disk;
NeilBrown84707f32010-03-16 17:23:35 +11002943 if (disk_idx >= conf->raid_disks
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 || disk_idx < 0)
2945 continue;
2946 disk = conf->mirrors + disk_idx;
2947
2948 disk->rdev = rdev;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002949 disk_stack_limits(mddev->gendisk, rdev->bdev,
2950 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 /* as we don't honour merge_bvec_fn, we must never risk
NeilBrown627a2d32010-03-08 16:44:38 +11002952 * violating it, so limit max_segments to 1 lying
2953 * within a single page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 */
NeilBrown627a2d32010-03-08 16:44:38 +11002955 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2956 blk_queue_max_segments(mddev->queue, 1);
2957 blk_queue_segment_boundary(mddev->queue,
2958 PAGE_CACHE_SIZE - 1);
2959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
2961 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 }
NeilBrown6d508242005-09-09 16:24:03 -07002963 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10002964 if (!enough(conf, -1)) {
NeilBrown128595e2010-05-03 14:47:14 +10002965 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07002966 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 goto out_free_conf;
2968 }
2969
2970 mddev->degraded = 0;
2971 for (i = 0; i < conf->raid_disks; i++) {
2972
2973 disk = conf->mirrors + i;
2974
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002975 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07002976 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 disk->head_position = 0;
2978 mddev->degraded++;
Neil Brown8c2e8702008-06-28 08:30:52 +10002979 if (disk->rdev)
2980 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 }
NeilBrownd890fa22011-10-26 11:54:39 +11002982 disk->recovery_disabled = mddev->recovery_disabled - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 }
2984
Andre Noll8c6ac862009-06-18 08:48:06 +10002985 if (mddev->recovery_cp != MaxSector)
NeilBrown128595e2010-05-03 14:47:14 +10002986 printk(KERN_NOTICE "md/raid10:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10002987 " -- starting background reconstruction\n",
2988 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 printk(KERN_INFO
NeilBrown128595e2010-05-03 14:47:14 +10002990 "md/raid10:%s: active with %d out of %d devices\n",
NeilBrown84707f32010-03-16 17:23:35 +11002991 mdname(mddev), conf->raid_disks - mddev->degraded,
2992 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 /*
2994 * Ok, everything is just fine now
2995 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11002996 mddev->dev_sectors = conf->dev_sectors;
2997 size = raid10_size(mddev, 0, 0);
2998 md_set_array_sectors(mddev, size);
2999 mddev->resync_max_sectors = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
NeilBrown0d129222006-10-03 01:15:54 -07003001 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
3002 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown7a5febe2005-05-16 21:53:16 -07003003
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 /* Calculate max read-ahead size.
3005 * We need to readahead at least twice a whole stripe....
3006 * maybe...
3007 */
3008 {
Andre Noll9d8f0362009-06-18 08:45:01 +10003009 int stripe = conf->raid_disks *
3010 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 stripe /= conf->near_copies;
3012 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
3013 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
3014 }
3015
NeilBrown84707f32010-03-16 17:23:35 +11003016 if (conf->near_copies < conf->raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
Martin K. Petersena91a2782011-03-17 11:11:05 +01003018
3019 if (md_integrity_register(mddev))
3020 goto out_free_conf;
3021
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 return 0;
3023
3024out_free_conf:
NeilBrown01f96c02011-09-21 15:30:20 +10003025 md_unregister_thread(&mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 if (conf->r10bio_pool)
3027 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08003028 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003029 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 kfree(conf);
3031 mddev->private = NULL;
3032out:
3033 return -EIO;
3034}
3035
NeilBrownfd01b882011-10-11 16:47:53 +11003036static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
NeilBrowne879a872011-10-11 16:49:02 +11003038 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
NeilBrown409c57f2009-03-31 14:39:39 +11003040 raise_barrier(conf, 0);
3041 lower_barrier(conf);
3042
NeilBrown01f96c02011-09-21 15:30:20 +10003043 md_unregister_thread(&mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
3045 if (conf->r10bio_pool)
3046 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003047 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 kfree(conf);
3049 mddev->private = NULL;
3050 return 0;
3051}
3052
NeilBrownfd01b882011-10-11 16:47:53 +11003053static void raid10_quiesce(struct mddev *mddev, int state)
NeilBrown6cce3b22006-01-06 00:20:16 -08003054{
NeilBrowne879a872011-10-11 16:49:02 +11003055 struct r10conf *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08003056
3057 switch(state) {
3058 case 1:
3059 raise_barrier(conf, 0);
3060 break;
3061 case 0:
3062 lower_barrier(conf);
3063 break;
3064 }
NeilBrown6cce3b22006-01-06 00:20:16 -08003065}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
NeilBrownfd01b882011-10-11 16:47:53 +11003067static void *raid10_takeover_raid0(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003068{
NeilBrown3cb03002011-10-11 16:45:26 +11003069 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +11003070 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003071
3072 if (mddev->degraded > 0) {
NeilBrown128595e2010-05-03 14:47:14 +10003073 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
3074 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003075 return ERR_PTR(-EINVAL);
3076 }
3077
Trela, Maciejdab8b292010-03-08 16:02:45 +11003078 /* Set new parameters */
3079 mddev->new_level = 10;
3080 /* new layout: far_copies = 1, near_copies = 2 */
3081 mddev->new_layout = (1<<8) + 2;
3082 mddev->new_chunk_sectors = mddev->chunk_sectors;
3083 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003084 mddev->raid_disks *= 2;
3085 /* make sure it will be not marked as dirty */
3086 mddev->recovery_cp = MaxSector;
3087
3088 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003089 if (!IS_ERR(conf)) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003090 list_for_each_entry(rdev, &mddev->disks, same_set)
3091 if (rdev->raid_disk >= 0)
3092 rdev->new_raid_disk = rdev->raid_disk * 2;
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003093 conf->barrier = 1;
3094 }
3095
Trela, Maciejdab8b292010-03-08 16:02:45 +11003096 return conf;
3097}
3098
NeilBrownfd01b882011-10-11 16:47:53 +11003099static void *raid10_takeover(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003100{
NeilBrowne373ab12011-10-11 16:48:59 +11003101 struct r0conf *raid0_conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003102
3103 /* raid10 can take over:
3104 * raid0 - providing it has only two drives
3105 */
3106 if (mddev->level == 0) {
3107 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11003108 raid0_conf = mddev->private;
3109 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown128595e2010-05-03 14:47:14 +10003110 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
3111 " with more than one zone.\n",
3112 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003113 return ERR_PTR(-EINVAL);
3114 }
3115 return raid10_takeover_raid0(mddev);
3116 }
3117 return ERR_PTR(-EINVAL);
3118}
3119
NeilBrown84fc4b52011-10-11 16:49:58 +11003120static struct md_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121{
3122 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08003123 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 .owner = THIS_MODULE,
3125 .make_request = make_request,
3126 .run = run,
3127 .stop = stop,
3128 .status = status,
3129 .error_handler = error,
3130 .hot_add_disk = raid10_add_disk,
3131 .hot_remove_disk= raid10_remove_disk,
3132 .spare_active = raid10_spare_active,
3133 .sync_request = sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08003134 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07003135 .size = raid10_size,
Trela, Maciejdab8b292010-03-08 16:02:45 +11003136 .takeover = raid10_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137};
3138
3139static int __init raid_init(void)
3140{
NeilBrown2604b702006-01-06 00:20:36 -08003141 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142}
3143
3144static void raid_exit(void)
3145{
NeilBrown2604b702006-01-06 00:20:36 -08003146 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147}
3148
3149module_init(raid_init);
3150module_exit(raid_exit);
3151MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11003152MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08003154MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08003155MODULE_ALIAS("md-level-10");
NeilBrown34db0cd2011-10-11 16:50:01 +11003156
3157module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);