blob: 6e8aa213f0d5208d917b8222a56980ab3582b4d6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid10.c : Multiple Devices driver for Linux
3 *
4 * Copyright (C) 2000-2004 Neil Brown
5 *
6 * RAID-10 support for md.
7 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008 * Base on code in raid1.c. See raid1.c for further copyright information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110022#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110023#include <linux/blkdev.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040024#include <linux/module.h>
NeilBrownbff61972009-03-31 14:33:13 +110025#include <linux/seq_file.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100026#include <linux/ratelimit.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110027#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110028#include "raid10.h"
Trela, Maciejdab8b292010-03-08 16:02:45 +110029#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110030#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/*
33 * RAID10 provides a combination of RAID0 and RAID1 functionality.
34 * The layout of data is defined by
35 * chunk_size
36 * raid_disks
37 * near_copies (stored in low byte of layout)
38 * far_copies (stored in second byte of layout)
NeilBrownc93983b2006-06-26 00:27:41 -070039 * far_offset (stored in bit 16 of layout )
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 *
41 * The data to be stored is divided into chunks using chunksize.
42 * Each device is divided into far_copies sections.
43 * In each section, chunks are laid out in a style similar to raid0, but
44 * near_copies copies of each chunk is stored (each on a different drive).
45 * The starting device for each section is offset near_copies from the starting
46 * device of the previous section.
NeilBrownc93983b2006-06-26 00:27:41 -070047 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * drive.
49 * near_copies and far_copies must be at least one, and their product is at most
50 * raid_disks.
NeilBrownc93983b2006-06-26 00:27:41 -070051 *
52 * If far_offset is true, then the far_copies are handled a bit differently.
53 * The copies are still in different stripes, but instead of be very far apart
54 * on disk, there are adjacent stripes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 */
56
57/*
58 * Number of guaranteed r10bios in case of extreme VM load:
59 */
60#define NR_RAID10_BIOS 256
61
NeilBrown34db0cd2011-10-11 16:50:01 +110062/* When there are this many requests queue to be written by
63 * the raid10 thread, we become 'congested' to provide back-pressure
64 * for writeback.
65 */
66static int max_queued_requests = 1024;
67
NeilBrowne879a872011-10-11 16:49:02 +110068static void allow_barrier(struct r10conf *conf);
69static void lower_barrier(struct r10conf *conf);
NeilBrown0a27ec92006-01-06 00:20:13 -080070
Al Virodd0fc662005-10-07 07:46:04 +010071static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
NeilBrowne879a872011-10-11 16:49:02 +110073 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +110074 int size = offsetof(struct r10bio, devs[conf->copies]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
NeilBrown69335ef2011-12-23 10:17:54 +110076 /* allocate a r10bio with room for raid_disks entries in the
77 * bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +010078 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
81static void r10bio_pool_free(void *r10_bio, void *data)
82{
83 kfree(r10_bio);
84}
85
NeilBrown0310fa22008-08-05 15:54:14 +100086/* Maximum size of each resync request */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#define RESYNC_BLOCK_SIZE (64*1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
NeilBrown0310fa22008-08-05 15:54:14 +100089/* amount of memory to reserve for resync requests */
90#define RESYNC_WINDOW (1024*1024)
91/* maximum number of concurrent requests, memory permitting */
92#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/*
95 * When performing a resync, we need to read and compare, so
96 * we need as many pages are there are copies.
97 * When performing a recovery, we need 2 bios, one for read,
98 * one for write (we recover only one drive per r10buf)
99 *
100 */
Al Virodd0fc662005-10-07 07:46:04 +0100101static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
NeilBrowne879a872011-10-11 16:49:02 +1100103 struct r10conf *conf = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct page *page;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100105 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct bio *bio;
107 int i, j;
108 int nalloc;
109
110 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100111 if (!r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
115 nalloc = conf->copies; /* resync */
116 else
117 nalloc = 2; /* recovery */
118
119 /*
120 * Allocate bios.
121 */
122 for (j = nalloc ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +1100123 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (!bio)
125 goto out_free_bio;
126 r10_bio->devs[j].bio = bio;
NeilBrown69335ef2011-12-23 10:17:54 +1100127 if (!conf->have_replacement)
128 continue;
129 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
130 if (!bio)
131 goto out_free_bio;
132 r10_bio->devs[j].repl_bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
134 /*
135 * Allocate RESYNC_PAGES data pages and attach them
136 * where needed.
137 */
138 for (j = 0 ; j < nalloc; j++) {
NeilBrown69335ef2011-12-23 10:17:54 +1100139 struct bio *rbio = r10_bio->devs[j].repl_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 bio = r10_bio->devs[j].bio;
141 for (i = 0; i < RESYNC_PAGES; i++) {
Namhyung Kimc65060a2011-07-18 17:38:49 +1000142 if (j == 1 && !test_bit(MD_RECOVERY_SYNC,
143 &conf->mddev->recovery)) {
144 /* we can share bv_page's during recovery */
145 struct bio *rbio = r10_bio->devs[0].bio;
146 page = rbio->bi_io_vec[i].bv_page;
147 get_page(page);
148 } else
149 page = alloc_page(gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (unlikely(!page))
151 goto out_free_pages;
152
153 bio->bi_io_vec[i].bv_page = page;
NeilBrown69335ef2011-12-23 10:17:54 +1100154 if (rbio)
155 rbio->bi_io_vec[i].bv_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157 }
158
159 return r10_bio;
160
161out_free_pages:
162 for ( ; i > 0 ; i--)
NeilBrown1345b1d2006-01-06 00:20:40 -0800163 safe_put_page(bio->bi_io_vec[i-1].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 while (j--)
165 for (i = 0; i < RESYNC_PAGES ; i++)
NeilBrown1345b1d2006-01-06 00:20:40 -0800166 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 j = -1;
168out_free_bio:
NeilBrown69335ef2011-12-23 10:17:54 +1100169 while (++j < nalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 bio_put(r10_bio->devs[j].bio);
NeilBrown69335ef2011-12-23 10:17:54 +1100171 if (r10_bio->devs[j].repl_bio)
172 bio_put(r10_bio->devs[j].repl_bio);
173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 r10bio_pool_free(r10_bio, conf);
175 return NULL;
176}
177
178static void r10buf_pool_free(void *__r10_bio, void *data)
179{
180 int i;
NeilBrowne879a872011-10-11 16:49:02 +1100181 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100182 struct r10bio *r10bio = __r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 int j;
184
185 for (j=0; j < conf->copies; j++) {
186 struct bio *bio = r10bio->devs[j].bio;
187 if (bio) {
188 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown1345b1d2006-01-06 00:20:40 -0800189 safe_put_page(bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 bio->bi_io_vec[i].bv_page = NULL;
191 }
192 bio_put(bio);
193 }
NeilBrown69335ef2011-12-23 10:17:54 +1100194 bio = r10bio->devs[j].repl_bio;
195 if (bio)
196 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198 r10bio_pool_free(r10bio, conf);
199}
200
NeilBrowne879a872011-10-11 16:49:02 +1100201static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 int i;
204
205 for (i = 0; i < conf->copies; i++) {
206 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown749c55e2011-07-28 11:39:24 +1000207 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 bio_put(*bio);
209 *bio = NULL;
NeilBrown69335ef2011-12-23 10:17:54 +1100210 bio = &r10_bio->devs[i].repl_bio;
211 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
212 bio_put(*bio);
213 *bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215}
216
NeilBrown9f2c9d12011-10-11 16:48:43 +1100217static void free_r10bio(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
NeilBrowne879a872011-10-11 16:49:02 +1100219 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 put_all_bios(conf, r10_bio);
222 mempool_free(r10_bio, conf->r10bio_pool);
223}
224
NeilBrown9f2c9d12011-10-11 16:48:43 +1100225static void put_buf(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
NeilBrowne879a872011-10-11 16:49:02 +1100227 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 mempool_free(r10_bio, conf->r10buf_pool);
230
NeilBrown0a27ec92006-01-06 00:20:13 -0800231 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
NeilBrown9f2c9d12011-10-11 16:48:43 +1100234static void reschedule_retry(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +1100237 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +1100238 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 spin_lock_irqsave(&conf->device_lock, flags);
241 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800242 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 spin_unlock_irqrestore(&conf->device_lock, flags);
244
Arthur Jones388667b2008-07-25 12:03:38 -0700245 /* wake up frozen array... */
246 wake_up(&conf->wait_barrier);
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 md_wakeup_thread(mddev->thread);
249}
250
251/*
252 * raid_end_bio_io() is called when we have finished servicing a mirrored
253 * operation and are ready to return a success/failure code to the buffer
254 * cache layer.
255 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100256static void raid_end_bio_io(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 struct bio *bio = r10_bio->master_bio;
NeilBrown856e08e2011-07-28 11:39:23 +1000259 int done;
NeilBrowne879a872011-10-11 16:49:02 +1100260 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
NeilBrown856e08e2011-07-28 11:39:23 +1000262 if (bio->bi_phys_segments) {
263 unsigned long flags;
264 spin_lock_irqsave(&conf->device_lock, flags);
265 bio->bi_phys_segments--;
266 done = (bio->bi_phys_segments == 0);
267 spin_unlock_irqrestore(&conf->device_lock, flags);
268 } else
269 done = 1;
270 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
271 clear_bit(BIO_UPTODATE, &bio->bi_flags);
272 if (done) {
273 bio_endio(bio, 0);
274 /*
275 * Wake up any possible resync thread that waits for the device
276 * to go idle.
277 */
278 allow_barrier(conf);
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 free_r10bio(r10_bio);
281}
282
283/*
284 * Update disk head position estimator based on IRQ completion info.
285 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100286static inline void update_head_pos(int slot, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
NeilBrowne879a872011-10-11 16:49:02 +1100288 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
291 r10_bio->devs[slot].addr + (r10_bio->sectors);
292}
293
Namhyung Kim778ca012011-07-18 17:38:47 +1000294/*
295 * Find the disk number which triggered given bio
296 */
NeilBrowne879a872011-10-11 16:49:02 +1100297static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
NeilBrown69335ef2011-12-23 10:17:54 +1100298 struct bio *bio, int *slotp, int *replp)
Namhyung Kim778ca012011-07-18 17:38:47 +1000299{
300 int slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100301 int repl = 0;
Namhyung Kim778ca012011-07-18 17:38:47 +1000302
NeilBrown69335ef2011-12-23 10:17:54 +1100303 for (slot = 0; slot < conf->copies; slot++) {
Namhyung Kim778ca012011-07-18 17:38:47 +1000304 if (r10_bio->devs[slot].bio == bio)
305 break;
NeilBrown69335ef2011-12-23 10:17:54 +1100306 if (r10_bio->devs[slot].repl_bio == bio) {
307 repl = 1;
308 break;
309 }
310 }
Namhyung Kim778ca012011-07-18 17:38:47 +1000311
312 BUG_ON(slot == conf->copies);
313 update_head_pos(slot, r10_bio);
314
NeilBrown749c55e2011-07-28 11:39:24 +1000315 if (slotp)
316 *slotp = slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100317 if (replp)
318 *replp = repl;
Namhyung Kim778ca012011-07-18 17:38:47 +1000319 return r10_bio->devs[slot].devnum;
320}
321
NeilBrown6712ecf2007-09-27 12:47:43 +0200322static void raid10_end_read_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +1100325 struct r10bio *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 int slot, dev;
NeilBrownabbf0982011-12-23 10:17:54 +1100327 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +1100328 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 slot = r10_bio->read_slot;
332 dev = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100333 rdev = r10_bio->devs[slot].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 /*
335 * this branch is our 'one mirror IO has finished' event handler:
336 */
NeilBrown4443ae12006-01-06 00:20:28 -0800337 update_head_pos(slot, r10_bio);
338
339 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 /*
341 * Set R10BIO_Uptodate in our master bio, so that
342 * we will return a good error code to the higher
343 * levels even if IO on some other mirrored buffer fails.
344 *
345 * The 'master' represents the composite IO operation to
346 * user-side. So if something waits for IO, then it will
347 * wait for the 'master' bio.
348 */
349 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 raid_end_bio_io(r10_bio);
NeilBrownabbf0982011-12-23 10:17:54 +1100351 rdev_dec_pending(rdev, conf->mddev);
NeilBrown4443ae12006-01-06 00:20:28 -0800352 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 /*
NeilBrown7c4e06f2011-05-11 14:53:17 +1000354 * oops, read error - keep the refcount on the rdev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 */
356 char b[BDEVNAME_SIZE];
Christian Dietrich8bda4702011-07-27 11:00:36 +1000357 printk_ratelimited(KERN_ERR
358 "md/raid10:%s: %s: rescheduling sector %llu\n",
359 mdname(conf->mddev),
NeilBrownabbf0982011-12-23 10:17:54 +1100360 bdevname(rdev->bdev, b),
Christian Dietrich8bda4702011-07-27 11:00:36 +1000361 (unsigned long long)r10_bio->sector);
NeilBrown856e08e2011-07-28 11:39:23 +1000362 set_bit(R10BIO_ReadError, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 reschedule_retry(r10_bio);
364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
NeilBrown9f2c9d12011-10-11 16:48:43 +1100367static void close_write(struct r10bio *r10_bio)
NeilBrownbd870a12011-07-28 11:39:24 +1000368{
369 /* clear the bitmap if all writes complete successfully */
370 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
371 r10_bio->sectors,
372 !test_bit(R10BIO_Degraded, &r10_bio->state),
373 0);
374 md_write_end(r10_bio->mddev);
375}
376
NeilBrown9f2c9d12011-10-11 16:48:43 +1100377static void one_write_done(struct r10bio *r10_bio)
NeilBrown19d5f832011-09-10 17:21:17 +1000378{
379 if (atomic_dec_and_test(&r10_bio->remaining)) {
380 if (test_bit(R10BIO_WriteError, &r10_bio->state))
381 reschedule_retry(r10_bio);
382 else {
383 close_write(r10_bio);
384 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
385 reschedule_retry(r10_bio);
386 else
387 raid_end_bio_io(r10_bio);
388 }
389 }
390}
391
NeilBrown6712ecf2007-09-27 12:47:43 +0200392static void raid10_end_write_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +1100395 struct r10bio *r10_bio = bio->bi_private;
Namhyung Kim778ca012011-07-18 17:38:47 +1000396 int dev;
NeilBrown749c55e2011-07-28 11:39:24 +1000397 int dec_rdev = 1;
NeilBrowne879a872011-10-11 16:49:02 +1100398 struct r10conf *conf = r10_bio->mddev->private;
NeilBrown475b0322011-12-23 10:17:55 +1100399 int slot, repl;
NeilBrown4ca40c22011-12-23 10:17:55 +1100400 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
NeilBrown475b0322011-12-23 10:17:55 +1100402 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
NeilBrown475b0322011-12-23 10:17:55 +1100404 if (repl)
405 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +1100406 if (!rdev) {
407 smp_rmb();
408 repl = 0;
NeilBrown475b0322011-12-23 10:17:55 +1100409 rdev = conf->mirrors[dev].rdev;
NeilBrown4ca40c22011-12-23 10:17:55 +1100410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /*
412 * this branch is our 'one mirror IO has finished' event handler:
413 */
NeilBrown6cce3b22006-01-06 00:20:16 -0800414 if (!uptodate) {
NeilBrown475b0322011-12-23 10:17:55 +1100415 if (repl)
416 /* Never record new bad blocks to replacement,
417 * just fail it.
418 */
419 md_error(rdev->mddev, rdev);
420 else {
421 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +1100422 if (!test_and_set_bit(WantReplacement, &rdev->flags))
423 set_bit(MD_RECOVERY_NEEDED,
424 &rdev->mddev->recovery);
NeilBrown475b0322011-12-23 10:17:55 +1100425 set_bit(R10BIO_WriteError, &r10_bio->state);
426 dec_rdev = 0;
427 }
NeilBrown749c55e2011-07-28 11:39:24 +1000428 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /*
430 * Set R10BIO_Uptodate in our master bio, so that
431 * we will return a good error code for to the higher
432 * levels even if IO on some other mirrored buffer fails.
433 *
434 * The 'master' represents the composite IO operation to
435 * user-side. So if something waits for IO, then it will
436 * wait for the 'master' bio.
437 */
NeilBrown749c55e2011-07-28 11:39:24 +1000438 sector_t first_bad;
439 int bad_sectors;
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 set_bit(R10BIO_Uptodate, &r10_bio->state);
442
NeilBrown749c55e2011-07-28 11:39:24 +1000443 /* Maybe we can clear some bad blocks. */
NeilBrown475b0322011-12-23 10:17:55 +1100444 if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +1000445 r10_bio->devs[slot].addr,
446 r10_bio->sectors,
447 &first_bad, &bad_sectors)) {
448 bio_put(bio);
NeilBrown475b0322011-12-23 10:17:55 +1100449 if (repl)
450 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
451 else
452 r10_bio->devs[slot].bio = IO_MADE_GOOD;
NeilBrown749c55e2011-07-28 11:39:24 +1000453 dec_rdev = 0;
454 set_bit(R10BIO_MadeGood, &r10_bio->state);
455 }
456 }
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 /*
459 *
460 * Let's see if all mirrored write operations have finished
461 * already.
462 */
NeilBrown19d5f832011-09-10 17:21:17 +1000463 one_write_done(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +1000464 if (dec_rdev)
465 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/*
469 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300470 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 * parameters: near_copies and far_copies.
472 * near_copies * far_copies must be <= raid_disks.
473 * Normally one of these will be 1.
474 * If both are 1, we get raid0.
475 * If near_copies == raid_disks, we get raid1.
476 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300477 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 * first chunk, followed by near_copies copies of the next chunk and
479 * so on.
480 * If far_copies > 1, then after 1/far_copies of the array has been assigned
481 * as described above, we start again with a device offset of near_copies.
482 * So we effectively have another copy of the whole array further down all
483 * the drives, but with blocks on different drives.
484 * With this layout, and block is never stored twice on the one device.
485 *
486 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700487 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 *
489 * raid10_find_virt does the reverse mapping, from a device and a
490 * sector offset to a virtual address
491 */
492
NeilBrowne879a872011-10-11 16:49:02 +1100493static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 int n,f;
496 sector_t sector;
497 sector_t chunk;
498 sector_t stripe;
499 int dev;
500
501 int slot = 0;
502
503 /* now calculate first sector/dev */
504 chunk = r10bio->sector >> conf->chunk_shift;
505 sector = r10bio->sector & conf->chunk_mask;
506
507 chunk *= conf->near_copies;
508 stripe = chunk;
509 dev = sector_div(stripe, conf->raid_disks);
NeilBrownc93983b2006-06-26 00:27:41 -0700510 if (conf->far_offset)
511 stripe *= conf->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 sector += stripe << conf->chunk_shift;
514
515 /* and calculate all the others */
516 for (n=0; n < conf->near_copies; n++) {
517 int d = dev;
518 sector_t s = sector;
519 r10bio->devs[slot].addr = sector;
520 r10bio->devs[slot].devnum = d;
521 slot++;
522
523 for (f = 1; f < conf->far_copies; f++) {
524 d += conf->near_copies;
525 if (d >= conf->raid_disks)
526 d -= conf->raid_disks;
527 s += conf->stride;
528 r10bio->devs[slot].devnum = d;
529 r10bio->devs[slot].addr = s;
530 slot++;
531 }
532 dev++;
533 if (dev >= conf->raid_disks) {
534 dev = 0;
535 sector += (conf->chunk_mask + 1);
536 }
537 }
538 BUG_ON(slot != conf->copies);
539}
540
NeilBrowne879a872011-10-11 16:49:02 +1100541static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 sector_t offset, chunk, vchunk;
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 offset = sector & conf->chunk_mask;
NeilBrownc93983b2006-06-26 00:27:41 -0700546 if (conf->far_offset) {
547 int fc;
548 chunk = sector >> conf->chunk_shift;
549 fc = sector_div(chunk, conf->far_copies);
550 dev -= fc * conf->near_copies;
551 if (dev < 0)
552 dev += conf->raid_disks;
553 } else {
NeilBrown64a742b2007-02-28 20:11:18 -0800554 while (sector >= conf->stride) {
NeilBrownc93983b2006-06-26 00:27:41 -0700555 sector -= conf->stride;
556 if (dev < conf->near_copies)
557 dev += conf->raid_disks - conf->near_copies;
558 else
559 dev -= conf->near_copies;
560 }
561 chunk = sector >> conf->chunk_shift;
562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 vchunk = chunk * conf->raid_disks + dev;
564 sector_div(vchunk, conf->near_copies);
565 return (vchunk << conf->chunk_shift) + offset;
566}
567
568/**
569 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
570 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200571 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 * @biovec: the request that could be merged to it.
573 *
574 * Return amount of bytes we can accept at this offset
575 * If near_copies == raid_disk, there are no striping issues,
576 * but in that case, the function isn't called at all.
577 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200578static int raid10_mergeable_bvec(struct request_queue *q,
579 struct bvec_merge_data *bvm,
580 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
NeilBrownfd01b882011-10-11 16:47:53 +1100582 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200583 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +1000585 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200586 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
589 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200590 if (max <= biovec->bv_len && bio_sectors == 0)
591 return biovec->bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 else
593 return max;
594}
595
596/*
597 * This routine returns the disk from which the requested read should
598 * be done. There is a per-array 'next expected sequential IO' sector
599 * number - if this matches on the next IO then we use the last disk.
600 * There is also a per-disk 'last know head position' sector that is
601 * maintained from IRQ contexts, both the normal and the resync IO
602 * completion handlers update this position correctly. If there is no
603 * perfect sequential match then we pick the disk whose head is closest.
604 *
605 * If there are 2 mirrors in the same 2 devices, performance degrades
606 * because position is mirror, not device based.
607 *
608 * The rdev for the device selected will have nr_pending incremented.
609 */
610
611/*
612 * FIXME: possibly should rethink readbalancing and do it differently
613 * depending on near_copies / far_copies geometry.
614 */
NeilBrown96c3fd12011-12-23 10:17:54 +1100615static struct md_rdev *read_balance(struct r10conf *conf,
616 struct r10bio *r10_bio,
617 int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000619 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000620 int disk, slot;
NeilBrown856e08e2011-07-28 11:39:23 +1000621 int sectors = r10_bio->sectors;
622 int best_good_sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000623 sector_t new_distance, best_dist;
NeilBrownabbf0982011-12-23 10:17:54 +1100624 struct md_rdev *rdev, *best_rdev;
NeilBrown56d99122011-05-11 14:27:03 +1000625 int do_balance;
626 int best_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 raid10_find_phys(conf, r10_bio);
629 rcu_read_lock();
NeilBrown56d99122011-05-11 14:27:03 +1000630retry:
NeilBrown856e08e2011-07-28 11:39:23 +1000631 sectors = r10_bio->sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000632 best_slot = -1;
NeilBrownabbf0982011-12-23 10:17:54 +1100633 best_rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000634 best_dist = MaxSector;
NeilBrown856e08e2011-07-28 11:39:23 +1000635 best_good_sectors = 0;
NeilBrown56d99122011-05-11 14:27:03 +1000636 do_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /*
638 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800639 * device if no resync is going on (recovery is ok), or below
640 * the resync window. We take the first readable disk when
641 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 */
643 if (conf->mddev->recovery_cp < MaxSector
NeilBrown56d99122011-05-11 14:27:03 +1000644 && (this_sector + sectors >= conf->next_resync))
645 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
NeilBrown56d99122011-05-11 14:27:03 +1000647 for (slot = 0; slot < conf->copies ; slot++) {
NeilBrown856e08e2011-07-28 11:39:23 +1000648 sector_t first_bad;
649 int bad_sectors;
650 sector_t dev_sector;
651
NeilBrown56d99122011-05-11 14:27:03 +1000652 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000654 disk = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100655 rdev = rcu_dereference(conf->mirrors[disk].replacement);
656 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
657 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
658 rdev = rcu_dereference(conf->mirrors[disk].rdev);
NeilBrown56d99122011-05-11 14:27:03 +1000659 if (rdev == NULL)
660 continue;
NeilBrownabbf0982011-12-23 10:17:54 +1100661 if (test_bit(Faulty, &rdev->flags))
662 continue;
663 if (!test_bit(In_sync, &rdev->flags) &&
664 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
NeilBrown56d99122011-05-11 14:27:03 +1000665 continue;
666
NeilBrown856e08e2011-07-28 11:39:23 +1000667 dev_sector = r10_bio->devs[slot].addr;
668 if (is_badblock(rdev, dev_sector, sectors,
669 &first_bad, &bad_sectors)) {
670 if (best_dist < MaxSector)
671 /* Already have a better slot */
672 continue;
673 if (first_bad <= dev_sector) {
674 /* Cannot read here. If this is the
675 * 'primary' device, then we must not read
676 * beyond 'bad_sectors' from another device.
677 */
678 bad_sectors -= (dev_sector - first_bad);
679 if (!do_balance && sectors > bad_sectors)
680 sectors = bad_sectors;
681 if (best_good_sectors > sectors)
682 best_good_sectors = sectors;
683 } else {
684 sector_t good_sectors =
685 first_bad - dev_sector;
686 if (good_sectors > best_good_sectors) {
687 best_good_sectors = good_sectors;
688 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100689 best_rdev = rdev;
NeilBrown856e08e2011-07-28 11:39:23 +1000690 }
691 if (!do_balance)
692 /* Must read from here */
693 break;
694 }
695 continue;
696 } else
697 best_good_sectors = sectors;
698
NeilBrown56d99122011-05-11 14:27:03 +1000699 if (!do_balance)
700 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
NeilBrown22dfdf52005-11-28 13:44:09 -0800702 /* This optimisation is debatable, and completely destroys
703 * sequential read speed for 'far copies' arrays. So only
704 * keep it for 'near' arrays, and review those later.
705 */
NeilBrown56d99122011-05-11 14:27:03 +1000706 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 break;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800708
709 /* for far > 1 always use the lowest address */
710 if (conf->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000711 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800712 else
NeilBrown56d99122011-05-11 14:27:03 +1000713 new_distance = abs(r10_bio->devs[slot].addr -
714 conf->mirrors[disk].head_position);
715 if (new_distance < best_dist) {
716 best_dist = new_distance;
717 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100718 best_rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720 }
NeilBrownabbf0982011-12-23 10:17:54 +1100721 if (slot >= conf->copies) {
NeilBrown56d99122011-05-11 14:27:03 +1000722 slot = best_slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100723 rdev = best_rdev;
724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
NeilBrown56d99122011-05-11 14:27:03 +1000726 if (slot >= 0) {
NeilBrown56d99122011-05-11 14:27:03 +1000727 atomic_inc(&rdev->nr_pending);
728 if (test_bit(Faulty, &rdev->flags)) {
729 /* Cannot risk returning a device that failed
730 * before we inc'ed nr_pending
731 */
732 rdev_dec_pending(rdev, conf->mddev);
733 goto retry;
734 }
735 r10_bio->read_slot = slot;
736 } else
NeilBrown96c3fd12011-12-23 10:17:54 +1100737 rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 rcu_read_unlock();
NeilBrown856e08e2011-07-28 11:39:23 +1000739 *max_sectors = best_good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
NeilBrown96c3fd12011-12-23 10:17:54 +1100741 return rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
NeilBrown0d129222006-10-03 01:15:54 -0700744static int raid10_congested(void *data, int bits)
745{
NeilBrownfd01b882011-10-11 16:47:53 +1100746 struct mddev *mddev = data;
NeilBrowne879a872011-10-11 16:49:02 +1100747 struct r10conf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700748 int i, ret = 0;
749
NeilBrown34db0cd2011-10-11 16:50:01 +1100750 if ((bits & (1 << BDI_async_congested)) &&
751 conf->pending_count >= max_queued_requests)
752 return 1;
753
NeilBrown3fa841d2009-09-23 18:10:29 +1000754 if (mddev_congested(mddev, bits))
755 return 1;
NeilBrown0d129222006-10-03 01:15:54 -0700756 rcu_read_lock();
NeilBrown84707f32010-03-16 17:23:35 +1100757 for (i = 0; i < conf->raid_disks && ret == 0; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100758 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700759 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200760 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700761
762 ret |= bdi_congested(&q->backing_dev_info, bits);
763 }
764 }
765 rcu_read_unlock();
766 return ret;
767}
768
NeilBrowne879a872011-10-11 16:49:02 +1100769static void flush_pending_writes(struct r10conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800770{
771 /* Any writes that have been queued but are awaiting
772 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800773 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800774 spin_lock_irq(&conf->device_lock);
775
776 if (conf->pending_bio_list.head) {
777 struct bio *bio;
778 bio = bio_list_get(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +1100779 conf->pending_count = 0;
NeilBrowna35e63e2008-03-04 14:29:29 -0800780 spin_unlock_irq(&conf->device_lock);
781 /* flush any pending bitmap writes to disk
782 * before proceeding w/ I/O */
783 bitmap_unplug(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100784 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800785
786 while (bio) { /* submit pending writes */
787 struct bio *next = bio->bi_next;
788 bio->bi_next = NULL;
789 generic_make_request(bio);
790 bio = next;
791 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800792 } else
793 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800794}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100795
NeilBrown0a27ec92006-01-06 00:20:13 -0800796/* Barriers....
797 * Sometimes we need to suspend IO while we do something else,
798 * either some resync/recovery, or reconfigure the array.
799 * To do this we raise a 'barrier'.
800 * The 'barrier' is a counter that can be raised multiple times
801 * to count how many activities are happening which preclude
802 * normal IO.
803 * We can only raise the barrier if there is no pending IO.
804 * i.e. if nr_pending == 0.
805 * We choose only to raise the barrier if no-one is waiting for the
806 * barrier to go down. This means that as soon as an IO request
807 * is ready, no other operations which require a barrier will start
808 * until the IO request has had a chance.
809 *
810 * So: regular IO calls 'wait_barrier'. When that returns there
811 * is no backgroup IO happening, It must arrange to call
812 * allow_barrier when it has finished its IO.
813 * backgroup IO calls must call raise_barrier. Once that returns
814 * there is no normal IO happeing. It must arrange to call
815 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
NeilBrowne879a872011-10-11 16:49:02 +1100818static void raise_barrier(struct r10conf *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
NeilBrown6cce3b22006-01-06 00:20:16 -0800820 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
NeilBrown6cce3b22006-01-06 00:20:16 -0800823 /* Wait until no block IO is waiting (unless 'force') */
824 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrownc3b328a2011-04-18 18:25:43 +1000825 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800826
827 /* block any new IO from starting */
828 conf->barrier++;
829
NeilBrownc3b328a2011-04-18 18:25:43 +1000830 /* Now wait for all pending IO to complete */
NeilBrown0a27ec92006-01-06 00:20:13 -0800831 wait_event_lock_irq(conf->wait_barrier,
832 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
NeilBrownc3b328a2011-04-18 18:25:43 +1000833 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 spin_unlock_irq(&conf->resync_lock);
836}
837
NeilBrowne879a872011-10-11 16:49:02 +1100838static void lower_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800839{
840 unsigned long flags;
841 spin_lock_irqsave(&conf->resync_lock, flags);
842 conf->barrier--;
843 spin_unlock_irqrestore(&conf->resync_lock, flags);
844 wake_up(&conf->wait_barrier);
845}
846
NeilBrowne879a872011-10-11 16:49:02 +1100847static void wait_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800848{
849 spin_lock_irq(&conf->resync_lock);
850 if (conf->barrier) {
851 conf->nr_waiting++;
852 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
853 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000854 );
NeilBrown0a27ec92006-01-06 00:20:13 -0800855 conf->nr_waiting--;
856 }
857 conf->nr_pending++;
858 spin_unlock_irq(&conf->resync_lock);
859}
860
NeilBrowne879a872011-10-11 16:49:02 +1100861static void allow_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800862{
863 unsigned long flags;
864 spin_lock_irqsave(&conf->resync_lock, flags);
865 conf->nr_pending--;
866 spin_unlock_irqrestore(&conf->resync_lock, flags);
867 wake_up(&conf->wait_barrier);
868}
869
NeilBrowne879a872011-10-11 16:49:02 +1100870static void freeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -0800871{
872 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800873 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800874 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800875 * wait until nr_pending match nr_queued+1
876 * This is called in the context of one normal IO request
877 * that has failed. Thus any sync request that might be pending
878 * will be blocked by nr_pending, and we need to wait for
879 * pending IO requests to complete or be queued for re-try.
880 * Thus the number queued (nr_queued) plus this request (1)
881 * must match the number of pending IOs (nr_pending) before
882 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -0800883 */
884 spin_lock_irq(&conf->resync_lock);
885 conf->barrier++;
886 conf->nr_waiting++;
887 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -0800888 conf->nr_pending == conf->nr_queued+1,
NeilBrown4443ae12006-01-06 00:20:28 -0800889 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000890 flush_pending_writes(conf));
891
NeilBrown4443ae12006-01-06 00:20:28 -0800892 spin_unlock_irq(&conf->resync_lock);
893}
894
NeilBrowne879a872011-10-11 16:49:02 +1100895static void unfreeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -0800896{
897 /* reverse the effect of the freeze */
898 spin_lock_irq(&conf->resync_lock);
899 conf->barrier--;
900 conf->nr_waiting--;
901 wake_up(&conf->wait_barrier);
902 spin_unlock_irq(&conf->resync_lock);
903}
904
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -0700905static void make_request(struct mddev *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
NeilBrowne879a872011-10-11 16:49:02 +1100907 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100908 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 struct bio *read_bio;
910 int i;
911 int chunk_sects = conf->chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +0100912 const int rw = bio_data_dir(bio);
NeilBrown2c7d46e2010-08-18 16:16:05 +1000913 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
Tejun Heoe9c74692010-09-03 11:56:18 +0200914 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
NeilBrown6cce3b22006-01-06 00:20:16 -0800915 unsigned long flags;
NeilBrown3cb03002011-10-11 16:45:26 +1100916 struct md_rdev *blocked_rdev;
NeilBrownc3b328a2011-04-18 18:25:43 +1000917 int plugged;
NeilBrownd4432c22011-07-28 11:39:24 +1000918 int sectors_handled;
919 int max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Tejun Heoe9c74692010-09-03 11:56:18 +0200921 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
922 md_flush_request(mddev, bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200923 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -0700924 }
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 /* If this request crosses a chunk boundary, we need to
927 * split it. This will only happen for 1 PAGE (or less) requests.
928 */
929 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
930 > chunk_sects &&
931 conf->near_copies < conf->raid_disks)) {
932 struct bio_pair *bp;
933 /* Sanity check -- queue functions should prevent this happening */
934 if (bio->bi_vcnt != 1 ||
935 bio->bi_idx != 0)
936 goto bad_map;
937 /* This is a one page bio that upper layers
938 * refuse to split for us, so we need to split it.
939 */
Denis ChengRq6feef532008-10-09 08:57:05 +0200940 bp = bio_split(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
NeilBrown51e9ac72010-08-07 21:17:00 +1000942
943 /* Each of these 'make_request' calls will call 'wait_barrier'.
944 * If the first succeeds but the second blocks due to the resync
945 * thread raising the barrier, we will deadlock because the
946 * IO to the underlying device will be queued in generic_make_request
947 * and will never complete, so will never reduce nr_pending.
948 * So increment nr_waiting here so no new raise_barriers will
949 * succeed, and so the second wait_barrier cannot block.
950 */
951 spin_lock_irq(&conf->resync_lock);
952 conf->nr_waiting++;
953 spin_unlock_irq(&conf->resync_lock);
954
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200955 make_request(mddev, &bp->bio1);
956 make_request(mddev, &bp->bio2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
NeilBrown51e9ac72010-08-07 21:17:00 +1000958 spin_lock_irq(&conf->resync_lock);
959 conf->nr_waiting--;
960 wake_up(&conf->wait_barrier);
961 spin_unlock_irq(&conf->resync_lock);
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 bio_pair_release(bp);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200964 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 bad_map:
NeilBrown128595e2010-05-03 14:47:14 +1000966 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
967 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
969
NeilBrown6712ecf2007-09-27 12:47:43 +0200970 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200971 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973
NeilBrown3d310eb2005-06-21 17:17:26 -0700974 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -0700975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /*
977 * Register the new request and wait if the reconstruction
978 * thread has put up a bar for new requests.
979 * Continue immediately if no resync is active currently.
980 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800981 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
984
985 r10_bio->master_bio = bio;
986 r10_bio->sectors = bio->bi_size >> 9;
987
988 r10_bio->mddev = mddev;
989 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b22006-01-06 00:20:16 -0800990 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
NeilBrown856e08e2011-07-28 11:39:23 +1000992 /* We might need to issue multiple reads to different
993 * devices if there are bad blocks around, so we keep
994 * track of the number of reads in bio->bi_phys_segments.
995 * If this is 0, there is only one r10_bio and no locking
996 * will be needed when the request completes. If it is
997 * non-zero, then it is the number of not-completed requests.
998 */
999 bio->bi_phys_segments = 0;
1000 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
1001
Jens Axboea3623572005-11-01 09:26:16 +01001002 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 /*
1004 * read balancing logic:
1005 */
NeilBrown96c3fd12011-12-23 10:17:54 +11001006 struct md_rdev *rdev;
NeilBrown856e08e2011-07-28 11:39:23 +10001007 int slot;
1008
1009read_again:
NeilBrown96c3fd12011-12-23 10:17:54 +11001010 rdev = read_balance(conf, r10_bio, &max_sectors);
1011 if (!rdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 raid_end_bio_io(r10_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001013 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
NeilBrown96c3fd12011-12-23 10:17:54 +11001015 slot = r10_bio->read_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
NeilBrowna167f662010-10-26 18:31:13 +11001017 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrown856e08e2011-07-28 11:39:23 +10001018 md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
1019 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 r10_bio->devs[slot].bio = read_bio;
NeilBrownabbf0982011-12-23 10:17:54 +11001022 r10_bio->devs[slot].rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 read_bio->bi_sector = r10_bio->devs[slot].addr +
NeilBrown96c3fd12011-12-23 10:17:54 +11001025 rdev->data_offset;
1026 read_bio->bi_bdev = rdev->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 read_bio->bi_end_io = raid10_end_read_request;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001028 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 read_bio->bi_private = r10_bio;
1030
NeilBrown856e08e2011-07-28 11:39:23 +10001031 if (max_sectors < r10_bio->sectors) {
1032 /* Could not read all from this device, so we will
1033 * need another r10_bio.
1034 */
NeilBrown856e08e2011-07-28 11:39:23 +10001035 sectors_handled = (r10_bio->sectors + max_sectors
1036 - bio->bi_sector);
1037 r10_bio->sectors = max_sectors;
1038 spin_lock_irq(&conf->device_lock);
1039 if (bio->bi_phys_segments == 0)
1040 bio->bi_phys_segments = 2;
1041 else
1042 bio->bi_phys_segments++;
1043 spin_unlock(&conf->device_lock);
1044 /* Cannot call generic_make_request directly
1045 * as that will be queued in __generic_make_request
1046 * and subsequent mempool_alloc might block
1047 * waiting for it. so hand bio over to raid10d.
1048 */
1049 reschedule_retry(r10_bio);
1050
1051 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1052
1053 r10_bio->master_bio = bio;
1054 r10_bio->sectors = ((bio->bi_size >> 9)
1055 - sectors_handled);
1056 r10_bio->state = 0;
1057 r10_bio->mddev = mddev;
1058 r10_bio->sector = bio->bi_sector + sectors_handled;
1059 goto read_again;
1060 } else
1061 generic_make_request(read_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001062 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
1064
1065 /*
1066 * WRITE:
1067 */
NeilBrown34db0cd2011-10-11 16:50:01 +11001068 if (conf->pending_count >= max_queued_requests) {
1069 md_wakeup_thread(mddev->thread);
1070 wait_event(conf->wait_barrier,
1071 conf->pending_count < max_queued_requests);
1072 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001073 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 * inc refcount on their rdev. Record them by setting
1075 * bios[x] to bio
NeilBrownd4432c22011-07-28 11:39:24 +10001076 * If there are known/acknowledged bad blocks on any device
1077 * on which we have seen a write error, we want to avoid
1078 * writing to those blocks. This potentially requires several
1079 * writes to write around the bad blocks. Each set of writes
1080 * gets its own r10_bio with a set of bios attached. The number
1081 * of r10_bios is recored in bio->bi_phys_segments just as with
1082 * the read case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001084 plugged = mddev_check_plugged(mddev);
1085
NeilBrown69335ef2011-12-23 10:17:54 +11001086 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 raid10_find_phys(conf, r10_bio);
NeilBrownd4432c22011-07-28 11:39:24 +10001088retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -07001089 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 rcu_read_lock();
NeilBrownd4432c22011-07-28 11:39:24 +10001091 max_sectors = r10_bio->sectors;
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 for (i = 0; i < conf->copies; i++) {
1094 int d = r10_bio->devs[i].devnum;
NeilBrown3cb03002011-10-11 16:45:26 +11001095 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown475b0322011-12-23 10:17:55 +11001096 struct md_rdev *rrdev = rcu_dereference(
1097 conf->mirrors[d].replacement);
NeilBrown4ca40c22011-12-23 10:17:55 +11001098 if (rdev == rrdev)
1099 rrdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001100 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1101 atomic_inc(&rdev->nr_pending);
1102 blocked_rdev = rdev;
1103 break;
1104 }
NeilBrown475b0322011-12-23 10:17:55 +11001105 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1106 atomic_inc(&rrdev->nr_pending);
1107 blocked_rdev = rrdev;
1108 break;
1109 }
1110 if (rrdev && test_bit(Faulty, &rrdev->flags))
1111 rrdev = NULL;
1112
NeilBrownd4432c22011-07-28 11:39:24 +10001113 r10_bio->devs[i].bio = NULL;
NeilBrown475b0322011-12-23 10:17:55 +11001114 r10_bio->devs[i].repl_bio = NULL;
NeilBrownd4432c22011-07-28 11:39:24 +10001115 if (!rdev || test_bit(Faulty, &rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001116 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrownd4432c22011-07-28 11:39:24 +10001117 continue;
NeilBrown6cce3b22006-01-06 00:20:16 -08001118 }
NeilBrownd4432c22011-07-28 11:39:24 +10001119 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1120 sector_t first_bad;
1121 sector_t dev_sector = r10_bio->devs[i].addr;
1122 int bad_sectors;
1123 int is_bad;
1124
1125 is_bad = is_badblock(rdev, dev_sector,
1126 max_sectors,
1127 &first_bad, &bad_sectors);
1128 if (is_bad < 0) {
1129 /* Mustn't write here until the bad block
1130 * is acknowledged
1131 */
1132 atomic_inc(&rdev->nr_pending);
1133 set_bit(BlockedBadBlocks, &rdev->flags);
1134 blocked_rdev = rdev;
1135 break;
1136 }
1137 if (is_bad && first_bad <= dev_sector) {
1138 /* Cannot write here at all */
1139 bad_sectors -= (dev_sector - first_bad);
1140 if (bad_sectors < max_sectors)
1141 /* Mustn't write more than bad_sectors
1142 * to other devices yet
1143 */
1144 max_sectors = bad_sectors;
1145 /* We don't set R10BIO_Degraded as that
1146 * only applies if the disk is missing,
1147 * so it might be re-added, and we want to
1148 * know to recover this chunk.
1149 * In this case the device is here, and the
1150 * fact that this chunk is not in-sync is
1151 * recorded in the bad block log.
1152 */
1153 continue;
1154 }
1155 if (is_bad) {
1156 int good_sectors = first_bad - dev_sector;
1157 if (good_sectors < max_sectors)
1158 max_sectors = good_sectors;
1159 }
1160 }
1161 r10_bio->devs[i].bio = bio;
1162 atomic_inc(&rdev->nr_pending);
NeilBrown475b0322011-12-23 10:17:55 +11001163 if (rrdev) {
1164 r10_bio->devs[i].repl_bio = bio;
1165 atomic_inc(&rrdev->nr_pending);
1166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168 rcu_read_unlock();
1169
Dan Williams6bfe0b42008-04-30 00:52:32 -07001170 if (unlikely(blocked_rdev)) {
1171 /* Have to wait for this device to get unblocked, then retry */
1172 int j;
1173 int d;
1174
NeilBrown475b0322011-12-23 10:17:55 +11001175 for (j = 0; j < i; j++) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07001176 if (r10_bio->devs[j].bio) {
1177 d = r10_bio->devs[j].devnum;
1178 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1179 }
NeilBrown475b0322011-12-23 10:17:55 +11001180 if (r10_bio->devs[j].repl_bio) {
NeilBrown4ca40c22011-12-23 10:17:55 +11001181 struct md_rdev *rdev;
NeilBrown475b0322011-12-23 10:17:55 +11001182 d = r10_bio->devs[j].devnum;
NeilBrown4ca40c22011-12-23 10:17:55 +11001183 rdev = conf->mirrors[d].replacement;
1184 if (!rdev) {
1185 /* Race with remove_disk */
1186 smp_mb();
1187 rdev = conf->mirrors[d].rdev;
1188 }
1189 rdev_dec_pending(rdev, mddev);
NeilBrown475b0322011-12-23 10:17:55 +11001190 }
1191 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001192 allow_barrier(conf);
1193 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1194 wait_barrier(conf);
1195 goto retry_write;
1196 }
1197
NeilBrownd4432c22011-07-28 11:39:24 +10001198 if (max_sectors < r10_bio->sectors) {
1199 /* We are splitting this into multiple parts, so
1200 * we need to prepare for allocating another r10_bio.
1201 */
1202 r10_bio->sectors = max_sectors;
1203 spin_lock_irq(&conf->device_lock);
1204 if (bio->bi_phys_segments == 0)
1205 bio->bi_phys_segments = 2;
1206 else
1207 bio->bi_phys_segments++;
1208 spin_unlock_irq(&conf->device_lock);
1209 }
1210 sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
1211
NeilBrown4e780642010-10-19 12:54:01 +11001212 atomic_set(&r10_bio->remaining, 1);
NeilBrownd4432c22011-07-28 11:39:24 +10001213 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 for (i = 0; i < conf->copies; i++) {
1216 struct bio *mbio;
1217 int d = r10_bio->devs[i].devnum;
1218 if (!r10_bio->devs[i].bio)
1219 continue;
1220
NeilBrowna167f662010-10-26 18:31:13 +11001221 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrownd4432c22011-07-28 11:39:24 +10001222 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1223 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 r10_bio->devs[i].bio = mbio;
1225
NeilBrownd4432c22011-07-28 11:39:24 +10001226 mbio->bi_sector = (r10_bio->devs[i].addr+
1227 conf->mirrors[d].rdev->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1229 mbio->bi_end_io = raid10_end_write_request;
Tejun Heoe9c74692010-09-03 11:56:18 +02001230 mbio->bi_rw = WRITE | do_sync | do_fua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 mbio->bi_private = r10_bio;
1232
1233 atomic_inc(&r10_bio->remaining);
NeilBrown4e780642010-10-19 12:54:01 +11001234 spin_lock_irqsave(&conf->device_lock, flags);
1235 bio_list_add(&conf->pending_bio_list, mbio);
NeilBrown34db0cd2011-10-11 16:50:01 +11001236 conf->pending_count++;
NeilBrown4e780642010-10-19 12:54:01 +11001237 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown475b0322011-12-23 10:17:55 +11001238
1239 if (!r10_bio->devs[i].repl_bio)
1240 continue;
1241
1242 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1243 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1244 max_sectors);
1245 r10_bio->devs[i].repl_bio = mbio;
1246
NeilBrown4ca40c22011-12-23 10:17:55 +11001247 /* We are actively writing to the original device
1248 * so it cannot disappear, so the replacement cannot
1249 * become NULL here
1250 */
NeilBrown475b0322011-12-23 10:17:55 +11001251 mbio->bi_sector = (r10_bio->devs[i].addr+
1252 conf->mirrors[d].replacement->data_offset);
1253 mbio->bi_bdev = conf->mirrors[d].replacement->bdev;
1254 mbio->bi_end_io = raid10_end_write_request;
1255 mbio->bi_rw = WRITE | do_sync | do_fua;
1256 mbio->bi_private = r10_bio;
1257
1258 atomic_inc(&r10_bio->remaining);
1259 spin_lock_irqsave(&conf->device_lock, flags);
1260 bio_list_add(&conf->pending_bio_list, mbio);
1261 conf->pending_count++;
1262 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
1264
NeilBrown079fa162011-09-10 17:21:23 +10001265 /* Don't remove the bias on 'remaining' (one_write_done) until
1266 * after checking if we need to go around again.
1267 */
NeilBrowna35e63e2008-03-04 14:29:29 -08001268
NeilBrownd4432c22011-07-28 11:39:24 +10001269 if (sectors_handled < (bio->bi_size >> 9)) {
NeilBrown079fa162011-09-10 17:21:23 +10001270 one_write_done(r10_bio);
NeilBrown5e570282011-07-28 11:39:25 +10001271 /* We need another r10_bio. It has already been counted
NeilBrownd4432c22011-07-28 11:39:24 +10001272 * in bio->bi_phys_segments.
1273 */
1274 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1275
1276 r10_bio->master_bio = bio;
1277 r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1278
1279 r10_bio->mddev = mddev;
1280 r10_bio->sector = bio->bi_sector + sectors_handled;
1281 r10_bio->state = 0;
1282 goto retry_write;
1283 }
NeilBrown079fa162011-09-10 17:21:23 +10001284 one_write_done(r10_bio);
1285
1286 /* In case raid10d snuck in to freeze_array */
1287 wake_up(&conf->wait_barrier);
NeilBrownd4432c22011-07-28 11:39:24 +10001288
NeilBrownc3b328a2011-04-18 18:25:43 +10001289 if (do_sync || !mddev->bitmap || !plugged)
Lars Ellenberge3881a62007-01-10 23:15:37 -08001290 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
1292
NeilBrownfd01b882011-10-11 16:47:53 +11001293static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
NeilBrowne879a872011-10-11 16:49:02 +11001295 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 int i;
1297
1298 if (conf->near_copies < conf->raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +10001299 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (conf->near_copies > 1)
1301 seq_printf(seq, " %d near-copies", conf->near_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001302 if (conf->far_copies > 1) {
1303 if (conf->far_offset)
1304 seq_printf(seq, " %d offset-copies", conf->far_copies);
1305 else
1306 seq_printf(seq, " %d far-copies", conf->far_copies);
1307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown76186dd2006-10-03 01:15:48 -07001309 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 for (i = 0; i < conf->raid_disks; i++)
1311 seq_printf(seq, "%s",
1312 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001313 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 seq_printf(seq, "]");
1315}
1316
NeilBrown700c7212011-07-27 11:00:36 +10001317/* check if there are enough drives for
1318 * every block to appear on atleast one.
1319 * Don't consider the device numbered 'ignore'
1320 * as we might be about to remove it.
1321 */
NeilBrowne879a872011-10-11 16:49:02 +11001322static int enough(struct r10conf *conf, int ignore)
NeilBrown700c7212011-07-27 11:00:36 +10001323{
1324 int first = 0;
1325
1326 do {
1327 int n = conf->copies;
1328 int cnt = 0;
1329 while (n--) {
1330 if (conf->mirrors[first].rdev &&
1331 first != ignore)
1332 cnt++;
1333 first = (first+1) % conf->raid_disks;
1334 }
1335 if (cnt == 0)
1336 return 0;
1337 } while (first != 0);
1338 return 1;
1339}
1340
NeilBrownfd01b882011-10-11 16:47:53 +11001341static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 char b[BDEVNAME_SIZE];
NeilBrowne879a872011-10-11 16:49:02 +11001344 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 /*
1347 * If it is not operational, then we have already marked it as dead
1348 * else if it is the last working disks, ignore the error, let the
1349 * next level up know.
1350 * else mark the drive as failed
1351 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001352 if (test_bit(In_sync, &rdev->flags)
NeilBrown700c7212011-07-27 11:00:36 +10001353 && !enough(conf, rdev->raid_disk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 /*
1355 * Don't fail the drive, just return an IO error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 */
1357 return;
NeilBrownc04be0a2006-10-03 01:15:53 -07001358 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1359 unsigned long flags;
1360 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001362 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 /*
1364 * if recovery is running, make sure it aborts.
1365 */
NeilBrowndfc70642008-05-23 13:04:39 -07001366 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
NeilBrownde393cd2011-07-28 11:31:48 +10001368 set_bit(Blocked, &rdev->flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001369 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -07001370 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Joe Perches067032b2011-01-14 09:14:33 +11001371 printk(KERN_ALERT
1372 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1373 "md/raid10:%s: Operation continuing on %d devices.\n",
NeilBrown128595e2010-05-03 14:47:14 +10001374 mdname(mddev), bdevname(rdev->bdev, b),
1375 mdname(mddev), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376}
1377
NeilBrowne879a872011-10-11 16:49:02 +11001378static void print_conf(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
1380 int i;
NeilBrown0f6d02d2011-10-11 16:48:46 +11001381 struct mirror_info *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
NeilBrown128595e2010-05-03 14:47:14 +10001383 printk(KERN_DEBUG "RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (!conf) {
NeilBrown128595e2010-05-03 14:47:14 +10001385 printk(KERN_DEBUG "(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return;
1387 }
NeilBrown128595e2010-05-03 14:47:14 +10001388 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 conf->raid_disks);
1390
1391 for (i = 0; i < conf->raid_disks; i++) {
1392 char b[BDEVNAME_SIZE];
1393 tmp = conf->mirrors + i;
1394 if (tmp->rdev)
NeilBrown128595e2010-05-03 14:47:14 +10001395 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001396 i, !test_bit(In_sync, &tmp->rdev->flags),
1397 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 bdevname(tmp->rdev->bdev,b));
1399 }
1400}
1401
NeilBrowne879a872011-10-11 16:49:02 +11001402static void close_sync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
NeilBrown0a27ec92006-01-06 00:20:13 -08001404 wait_barrier(conf);
1405 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 mempool_destroy(conf->r10buf_pool);
1408 conf->r10buf_pool = NULL;
1409}
1410
NeilBrownfd01b882011-10-11 16:47:53 +11001411static int raid10_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
1413 int i;
NeilBrowne879a872011-10-11 16:49:02 +11001414 struct r10conf *conf = mddev->private;
NeilBrown0f6d02d2011-10-11 16:48:46 +11001415 struct mirror_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10001416 int count = 0;
1417 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 /*
1420 * Find all non-in_sync disks within the RAID10 configuration
1421 * and mark them in_sync
1422 */
1423 for (i = 0; i < conf->raid_disks; i++) {
1424 tmp = conf->mirrors + i;
NeilBrown4ca40c22011-12-23 10:17:55 +11001425 if (tmp->replacement
1426 && tmp->replacement->recovery_offset == MaxSector
1427 && !test_bit(Faulty, &tmp->replacement->flags)
1428 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1429 /* Replacement has just become active */
1430 if (!tmp->rdev
1431 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1432 count++;
1433 if (tmp->rdev) {
1434 /* Replaced device not technically faulty,
1435 * but we need to be sure it gets removed
1436 * and never re-added.
1437 */
1438 set_bit(Faulty, &tmp->rdev->flags);
1439 sysfs_notify_dirent_safe(
1440 tmp->rdev->sysfs_state);
1441 }
1442 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1443 } else if (tmp->rdev
1444 && !test_bit(Faulty, &tmp->rdev->flags)
1445 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001446 count++;
Adrian Drzewieckie6ffbcb2010-08-18 11:49:02 +10001447 sysfs_notify_dirent(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 }
1449 }
NeilBrown6b965622010-08-18 11:56:59 +10001450 spin_lock_irqsave(&conf->device_lock, flags);
1451 mddev->degraded -= count;
1452 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001455 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
1458
NeilBrownfd01b882011-10-11 16:47:53 +11001459static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
NeilBrowne879a872011-10-11 16:49:02 +11001461 struct r10conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001462 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 int mirror;
Neil Brown6c2fce22008-06-28 08:31:31 +10001464 int first = 0;
NeilBrown84707f32010-03-16 17:23:35 +11001465 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 if (mddev->recovery_cp < MaxSector)
1468 /* only hot-add to in-sync arrays, as recovery is
1469 * very different from resync
1470 */
Neil Brown199050e2008-06-28 08:31:33 +10001471 return -EBUSY;
NeilBrown700c7212011-07-27 11:00:36 +10001472 if (!enough(conf, -1))
Neil Brown199050e2008-06-28 08:31:33 +10001473 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
NeilBrowna53a6c82008-11-06 17:28:20 +11001475 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001476 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Namhyung Kim2c4193d2011-07-18 17:38:43 +10001478 if (rdev->saved_raid_disk >= first &&
NeilBrown6cce3b22006-01-06 00:20:16 -08001479 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1480 mirror = rdev->saved_raid_disk;
1481 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001482 mirror = first;
NeilBrown2bb77732011-07-27 11:00:36 +10001483 for ( ; mirror <= last ; mirror++) {
NeilBrown0f6d02d2011-10-11 16:48:46 +11001484 struct mirror_info *p = &conf->mirrors[mirror];
NeilBrown2bb77732011-07-27 11:00:36 +10001485 if (p->recovery_disabled == mddev->recovery_disabled)
1486 continue;
NeilBrownb7044d42011-12-23 10:17:56 +11001487 if (p->rdev) {
1488 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1489 p->replacement != NULL)
1490 continue;
1491 clear_bit(In_sync, &rdev->flags);
1492 set_bit(Replacement, &rdev->flags);
1493 rdev->raid_disk = mirror;
1494 err = 0;
1495 disk_stack_limits(mddev->gendisk, rdev->bdev,
1496 rdev->data_offset << 9);
1497 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1498 blk_queue_max_segments(mddev->queue, 1);
1499 blk_queue_segment_boundary(mddev->queue,
1500 PAGE_CACHE_SIZE - 1);
1501 }
1502 conf->fullsync = 1;
1503 rcu_assign_pointer(p->replacement, rdev);
1504 break;
1505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
NeilBrown2bb77732011-07-27 11:00:36 +10001507 disk_stack_limits(mddev->gendisk, rdev->bdev,
1508 rdev->data_offset << 9);
1509 /* as we don't honour merge_bvec_fn, we must
1510 * never risk violating it, so limit
1511 * ->max_segments to one lying with a single
1512 * page, as a one page request is never in
1513 * violation.
1514 */
1515 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1516 blk_queue_max_segments(mddev->queue, 1);
1517 blk_queue_segment_boundary(mddev->queue,
1518 PAGE_CACHE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 }
1520
NeilBrown2bb77732011-07-27 11:00:36 +10001521 p->head_position = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11001522 p->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown2bb77732011-07-27 11:00:36 +10001523 rdev->raid_disk = mirror;
1524 err = 0;
1525 if (rdev->saved_raid_disk != mirror)
1526 conf->fullsync = 1;
1527 rcu_assign_pointer(p->rdev, rdev);
1528 break;
1529 }
1530
Andre Nollac5e7112009-08-03 10:59:47 +10001531 md_integrity_add_rdev(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001533 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
NeilBrownb8321b62011-12-23 10:17:51 +11001536static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
NeilBrowne879a872011-10-11 16:49:02 +11001538 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11001540 int number = rdev->raid_disk;
NeilBrownc8ab9032011-12-23 10:17:54 +11001541 struct md_rdev **rdevp;
1542 struct mirror_info *p = conf->mirrors + number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 print_conf(conf);
NeilBrownc8ab9032011-12-23 10:17:54 +11001545 if (rdev == p->rdev)
1546 rdevp = &p->rdev;
1547 else if (rdev == p->replacement)
1548 rdevp = &p->replacement;
1549 else
1550 return 0;
1551
1552 if (test_bit(In_sync, &rdev->flags) ||
1553 atomic_read(&rdev->nr_pending)) {
1554 err = -EBUSY;
1555 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
NeilBrownc8ab9032011-12-23 10:17:54 +11001557 /* Only remove faulty devices if recovery
1558 * is not possible.
1559 */
1560 if (!test_bit(Faulty, &rdev->flags) &&
1561 mddev->recovery_disabled != p->recovery_disabled &&
NeilBrown4ca40c22011-12-23 10:17:55 +11001562 (!p->replacement || p->replacement == rdev) &&
NeilBrownc8ab9032011-12-23 10:17:54 +11001563 enough(conf, -1)) {
1564 err = -EBUSY;
1565 goto abort;
1566 }
1567 *rdevp = NULL;
1568 synchronize_rcu();
1569 if (atomic_read(&rdev->nr_pending)) {
1570 /* lost the race, try later */
1571 err = -EBUSY;
1572 *rdevp = rdev;
1573 goto abort;
NeilBrown4ca40c22011-12-23 10:17:55 +11001574 } else if (p->replacement) {
1575 /* We must have just cleared 'rdev' */
1576 p->rdev = p->replacement;
1577 clear_bit(Replacement, &p->replacement->flags);
1578 smp_mb(); /* Make sure other CPUs may see both as identical
1579 * but will never see neither -- if they are careful.
1580 */
1581 p->replacement = NULL;
1582 clear_bit(WantReplacement, &rdev->flags);
1583 } else
1584 /* We might have just remove the Replacement as faulty
1585 * Clear the flag just in case
1586 */
1587 clear_bit(WantReplacement, &rdev->flags);
1588
NeilBrownc8ab9032011-12-23 10:17:54 +11001589 err = md_integrity_register(mddev);
1590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591abort:
1592
1593 print_conf(conf);
1594 return err;
1595}
1596
1597
NeilBrown6712ecf2007-09-27 12:47:43 +02001598static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001600 struct r10bio *r10_bio = bio->bi_private;
NeilBrowne879a872011-10-11 16:49:02 +11001601 struct r10conf *conf = r10_bio->mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001602 int d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
NeilBrown69335ef2011-12-23 10:17:54 +11001604 d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001605
1606 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1607 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrowne684e412011-07-28 11:39:25 +10001608 else
1609 /* The write handler will notice the lack of
1610 * R10BIO_Uptodate and record any errors etc
1611 */
NeilBrown4dbcdc72006-01-06 00:20:52 -08001612 atomic_add(r10_bio->sectors,
1613 &conf->mirrors[d].rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 /* for reconstruct, we always reschedule after a read.
1616 * for resync, only after all reads
1617 */
NeilBrown73d5c382009-02-25 13:18:47 +11001618 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1620 atomic_dec_and_test(&r10_bio->remaining)) {
1621 /* we have read all the blocks,
1622 * do the comparison in process context in raid10d
1623 */
1624 reschedule_retry(r10_bio);
1625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
NeilBrown9f2c9d12011-10-11 16:48:43 +11001628static void end_sync_request(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001629{
NeilBrownfd01b882011-10-11 16:47:53 +11001630 struct mddev *mddev = r10_bio->mddev;
NeilBrown5e570282011-07-28 11:39:25 +10001631
1632 while (atomic_dec_and_test(&r10_bio->remaining)) {
1633 if (r10_bio->master_bio == NULL) {
1634 /* the primary of several recovery bios */
1635 sector_t s = r10_bio->sectors;
1636 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1637 test_bit(R10BIO_WriteError, &r10_bio->state))
1638 reschedule_retry(r10_bio);
1639 else
1640 put_buf(r10_bio);
1641 md_done_sync(mddev, s, 1);
1642 break;
1643 } else {
NeilBrown9f2c9d12011-10-11 16:48:43 +11001644 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
NeilBrown5e570282011-07-28 11:39:25 +10001645 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1646 test_bit(R10BIO_WriteError, &r10_bio->state))
1647 reschedule_retry(r10_bio);
1648 else
1649 put_buf(r10_bio);
1650 r10_bio = r10_bio2;
1651 }
1652 }
1653}
1654
NeilBrown6712ecf2007-09-27 12:47:43 +02001655static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
1657 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +11001658 struct r10bio *r10_bio = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11001659 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001660 struct r10conf *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001661 int d;
NeilBrown749c55e2011-07-28 11:39:24 +10001662 sector_t first_bad;
1663 int bad_sectors;
1664 int slot;
NeilBrown9ad1aef2011-12-23 10:17:55 +11001665 int repl;
NeilBrown4ca40c22011-12-23 10:17:55 +11001666 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
NeilBrown9ad1aef2011-12-23 10:17:55 +11001668 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1669 if (repl)
1670 rdev = conf->mirrors[d].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +11001671 if (!rdev) {
1672 smp_mb();
NeilBrown9ad1aef2011-12-23 10:17:55 +11001673 rdev = conf->mirrors[d].rdev;
NeilBrown4ca40c22011-12-23 10:17:55 +11001674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
NeilBrown1a0b7cd2011-07-28 11:39:25 +10001676 if (!uptodate) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11001677 if (repl)
1678 md_error(mddev, rdev);
1679 else {
1680 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11001681 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1682 set_bit(MD_RECOVERY_NEEDED,
1683 &rdev->mddev->recovery);
NeilBrown9ad1aef2011-12-23 10:17:55 +11001684 set_bit(R10BIO_WriteError, &r10_bio->state);
1685 }
1686 } else if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +10001687 r10_bio->devs[slot].addr,
1688 r10_bio->sectors,
1689 &first_bad, &bad_sectors))
1690 set_bit(R10BIO_MadeGood, &r10_bio->state);
NeilBrowndfc70642008-05-23 13:04:39 -07001691
NeilBrown9ad1aef2011-12-23 10:17:55 +11001692 rdev_dec_pending(rdev, mddev);
NeilBrown5e570282011-07-28 11:39:25 +10001693
1694 end_sync_request(r10_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
1696
1697/*
1698 * Note: sync and recover and handled very differently for raid10
1699 * This code is for resync.
1700 * For resync, we read through virtual addresses and read all blocks.
1701 * If there is any error, we schedule a write. The lowest numbered
1702 * drive is authoritative.
1703 * However requests come for physical address, so we need to map.
1704 * For every physical address there are raid_disks/copies virtual addresses,
1705 * which is always are least one, but is not necessarly an integer.
1706 * This means that a physical address can span multiple chunks, so we may
1707 * have to submit multiple io requests for a single sync request.
1708 */
1709/*
1710 * We check if all blocks are in-sync and only write to blocks that
1711 * aren't in sync
1712 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11001713static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
NeilBrowne879a872011-10-11 16:49:02 +11001715 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 int i, first;
1717 struct bio *tbio, *fbio;
1718
1719 atomic_set(&r10_bio->remaining, 1);
1720
1721 /* find the first device with a block */
1722 for (i=0; i<conf->copies; i++)
1723 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1724 break;
1725
1726 if (i == conf->copies)
1727 goto done;
1728
1729 first = i;
1730 fbio = r10_bio->devs[i].bio;
1731
1732 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001733 for (i=0 ; i < conf->copies ; i++) {
1734 int j, d;
1735 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001738
1739 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001741 if (i == first)
1742 continue;
1743 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1744 /* We know that the bi_io_vec layout is the same for
1745 * both 'first' and 'i', so we just compare them.
1746 * All vec entries are PAGE_SIZE;
1747 */
1748 for (j = 0; j < vcnt; j++)
1749 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1750 page_address(tbio->bi_io_vec[j].bv_page),
1751 PAGE_SIZE))
1752 break;
1753 if (j == vcnt)
1754 continue;
1755 mddev->resync_mismatches += r10_bio->sectors;
NeilBrownf84ee362011-07-28 11:39:25 +10001756 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1757 /* Don't fix anything. */
1758 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001759 }
NeilBrownf84ee362011-07-28 11:39:25 +10001760 /* Ok, we need to write this bio, either to correct an
1761 * inconsistency or to correct an unreadable block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 * First we need to fixup bv_offset, bv_len and
1763 * bi_vecs, as the read request might have corrupted these
1764 */
1765 tbio->bi_vcnt = vcnt;
1766 tbio->bi_size = r10_bio->sectors << 9;
1767 tbio->bi_idx = 0;
1768 tbio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1770 tbio->bi_flags |= 1 << BIO_UPTODATE;
1771 tbio->bi_next = NULL;
1772 tbio->bi_rw = WRITE;
1773 tbio->bi_private = r10_bio;
1774 tbio->bi_sector = r10_bio->devs[i].addr;
1775
1776 for (j=0; j < vcnt ; j++) {
1777 tbio->bi_io_vec[j].bv_offset = 0;
1778 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1779
1780 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1781 page_address(fbio->bi_io_vec[j].bv_page),
1782 PAGE_SIZE);
1783 }
1784 tbio->bi_end_io = end_sync_write;
1785
1786 d = r10_bio->devs[i].devnum;
1787 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1788 atomic_inc(&r10_bio->remaining);
1789 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1790
1791 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1792 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1793 generic_make_request(tbio);
1794 }
1795
NeilBrown9ad1aef2011-12-23 10:17:55 +11001796 /* Now write out to any replacement devices
1797 * that are active
1798 */
1799 for (i = 0; i < conf->copies; i++) {
1800 int j, d;
1801 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
1802
1803 tbio = r10_bio->devs[i].repl_bio;
1804 if (!tbio || !tbio->bi_end_io)
1805 continue;
1806 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
1807 && r10_bio->devs[i].bio != fbio)
1808 for (j = 0; j < vcnt; j++)
1809 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1810 page_address(fbio->bi_io_vec[j].bv_page),
1811 PAGE_SIZE);
1812 d = r10_bio->devs[i].devnum;
1813 atomic_inc(&r10_bio->remaining);
1814 md_sync_acct(conf->mirrors[d].replacement->bdev,
1815 tbio->bi_size >> 9);
1816 generic_make_request(tbio);
1817 }
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819done:
1820 if (atomic_dec_and_test(&r10_bio->remaining)) {
1821 md_done_sync(mddev, r10_bio->sectors, 1);
1822 put_buf(r10_bio);
1823 }
1824}
1825
1826/*
1827 * Now for the recovery code.
1828 * Recovery happens across physical sectors.
1829 * We recover all non-is_sync drives by finding the virtual address of
1830 * each, and then choose a working drive that also has that virt address.
1831 * There is a separate r10_bio for each non-in_sync drive.
1832 * Only the first two slots are in use. The first for reading,
1833 * The second for writing.
1834 *
1835 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11001836static void fix_recovery_read_error(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001837{
1838 /* We got a read error during recovery.
1839 * We repeat the read in smaller page-sized sections.
1840 * If a read succeeds, write it to the new device or record
1841 * a bad block if we cannot.
1842 * If a read fails, record a bad block on both old and
1843 * new devices.
1844 */
NeilBrownfd01b882011-10-11 16:47:53 +11001845 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001846 struct r10conf *conf = mddev->private;
NeilBrown5e570282011-07-28 11:39:25 +10001847 struct bio *bio = r10_bio->devs[0].bio;
1848 sector_t sect = 0;
1849 int sectors = r10_bio->sectors;
1850 int idx = 0;
1851 int dr = r10_bio->devs[0].devnum;
1852 int dw = r10_bio->devs[1].devnum;
1853
1854 while (sectors) {
1855 int s = sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11001856 struct md_rdev *rdev;
NeilBrown5e570282011-07-28 11:39:25 +10001857 sector_t addr;
1858 int ok;
1859
1860 if (s > (PAGE_SIZE>>9))
1861 s = PAGE_SIZE >> 9;
1862
1863 rdev = conf->mirrors[dr].rdev;
1864 addr = r10_bio->devs[0].addr + sect,
1865 ok = sync_page_io(rdev,
1866 addr,
1867 s << 9,
1868 bio->bi_io_vec[idx].bv_page,
1869 READ, false);
1870 if (ok) {
1871 rdev = conf->mirrors[dw].rdev;
1872 addr = r10_bio->devs[1].addr + sect;
1873 ok = sync_page_io(rdev,
1874 addr,
1875 s << 9,
1876 bio->bi_io_vec[idx].bv_page,
1877 WRITE, false);
NeilBrownb7044d42011-12-23 10:17:56 +11001878 if (!ok) {
NeilBrown5e570282011-07-28 11:39:25 +10001879 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11001880 if (!test_and_set_bit(WantReplacement,
1881 &rdev->flags))
1882 set_bit(MD_RECOVERY_NEEDED,
1883 &rdev->mddev->recovery);
1884 }
NeilBrown5e570282011-07-28 11:39:25 +10001885 }
1886 if (!ok) {
1887 /* We don't worry if we cannot set a bad block -
1888 * it really is bad so there is no loss in not
1889 * recording it yet
1890 */
1891 rdev_set_badblocks(rdev, addr, s, 0);
1892
1893 if (rdev != conf->mirrors[dw].rdev) {
1894 /* need bad block on destination too */
NeilBrown3cb03002011-10-11 16:45:26 +11001895 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
NeilBrown5e570282011-07-28 11:39:25 +10001896 addr = r10_bio->devs[1].addr + sect;
1897 ok = rdev_set_badblocks(rdev2, addr, s, 0);
1898 if (!ok) {
1899 /* just abort the recovery */
1900 printk(KERN_NOTICE
1901 "md/raid10:%s: recovery aborted"
1902 " due to read error\n",
1903 mdname(mddev));
1904
1905 conf->mirrors[dw].recovery_disabled
1906 = mddev->recovery_disabled;
1907 set_bit(MD_RECOVERY_INTR,
1908 &mddev->recovery);
1909 break;
1910 }
1911 }
1912 }
1913
1914 sectors -= s;
1915 sect += s;
1916 idx++;
1917 }
1918}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
NeilBrown9f2c9d12011-10-11 16:48:43 +11001920static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
NeilBrowne879a872011-10-11 16:49:02 +11001922 struct r10conf *conf = mddev->private;
Namhyung Kimc65060a2011-07-18 17:38:49 +10001923 int d;
NeilBrown24afd802011-12-23 10:17:55 +11001924 struct bio *wbio, *wbio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
NeilBrown5e570282011-07-28 11:39:25 +10001926 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
1927 fix_recovery_read_error(r10_bio);
1928 end_sync_request(r10_bio);
1929 return;
1930 }
1931
Namhyung Kimc65060a2011-07-18 17:38:49 +10001932 /*
1933 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 * and submit the write request
1935 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 d = r10_bio->devs[1].devnum;
NeilBrown24afd802011-12-23 10:17:55 +11001937 wbio = r10_bio->devs[1].bio;
1938 wbio2 = r10_bio->devs[1].repl_bio;
1939 if (wbio->bi_end_io) {
1940 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1941 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
1942 generic_make_request(wbio);
1943 }
1944 if (wbio2 && wbio2->bi_end_io) {
1945 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
1946 md_sync_acct(conf->mirrors[d].replacement->bdev,
1947 wbio2->bi_size >> 9);
1948 generic_make_request(wbio2);
1949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950}
1951
1952
1953/*
Robert Becker1e509152009-12-14 12:49:58 +11001954 * Used by fix_read_error() to decay the per rdev read_errors.
1955 * We halve the read error count for every hour that has elapsed
1956 * since the last recorded read error.
1957 *
1958 */
NeilBrownfd01b882011-10-11 16:47:53 +11001959static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
Robert Becker1e509152009-12-14 12:49:58 +11001960{
1961 struct timespec cur_time_mon;
1962 unsigned long hours_since_last;
1963 unsigned int read_errors = atomic_read(&rdev->read_errors);
1964
1965 ktime_get_ts(&cur_time_mon);
1966
1967 if (rdev->last_read_error.tv_sec == 0 &&
1968 rdev->last_read_error.tv_nsec == 0) {
1969 /* first time we've seen a read error */
1970 rdev->last_read_error = cur_time_mon;
1971 return;
1972 }
1973
1974 hours_since_last = (cur_time_mon.tv_sec -
1975 rdev->last_read_error.tv_sec) / 3600;
1976
1977 rdev->last_read_error = cur_time_mon;
1978
1979 /*
1980 * if hours_since_last is > the number of bits in read_errors
1981 * just set read errors to 0. We do this to avoid
1982 * overflowing the shift of read_errors by hours_since_last.
1983 */
1984 if (hours_since_last >= 8 * sizeof(read_errors))
1985 atomic_set(&rdev->read_errors, 0);
1986 else
1987 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
1988}
1989
NeilBrown3cb03002011-10-11 16:45:26 +11001990static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrown58c54fc2011-07-28 11:39:25 +10001991 int sectors, struct page *page, int rw)
1992{
1993 sector_t first_bad;
1994 int bad_sectors;
1995
1996 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
1997 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
1998 return -1;
1999 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
2000 /* success */
2001 return 1;
NeilBrownb7044d42011-12-23 10:17:56 +11002002 if (rw == WRITE) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002003 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002004 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2005 set_bit(MD_RECOVERY_NEEDED,
2006 &rdev->mddev->recovery);
2007 }
NeilBrown58c54fc2011-07-28 11:39:25 +10002008 /* need to record an error - either for the block or the device */
2009 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2010 md_error(rdev->mddev, rdev);
2011 return 0;
2012}
2013
Robert Becker1e509152009-12-14 12:49:58 +11002014/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 * This is a kernel thread which:
2016 *
2017 * 1. Retries failed read operations on working mirrors.
2018 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07002019 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 */
2021
NeilBrowne879a872011-10-11 16:49:02 +11002022static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown6814d532006-10-03 01:15:45 -07002023{
2024 int sect = 0; /* Offset from r10_bio->sector */
2025 int sectors = r10_bio->sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002026 struct md_rdev*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002027 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002028 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11002029
NeilBrown7c4e06f2011-05-11 14:53:17 +10002030 /* still own a reference to this rdev, so it cannot
2031 * have been cleared recently.
2032 */
2033 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002034
NeilBrown7c4e06f2011-05-11 14:53:17 +10002035 if (test_bit(Faulty, &rdev->flags))
2036 /* drive has already been failed, just ignore any
2037 more fix_read_error() attempts */
2038 return;
2039
2040 check_decay_read_errors(mddev, rdev);
2041 atomic_inc(&rdev->read_errors);
2042 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2043 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11002044 bdevname(rdev->bdev, b);
2045
NeilBrown7c4e06f2011-05-11 14:53:17 +10002046 printk(KERN_NOTICE
2047 "md/raid10:%s: %s: Raid device exceeded "
2048 "read_error threshold [cur %d:max %d]\n",
2049 mdname(mddev), b,
2050 atomic_read(&rdev->read_errors), max_read_errors);
2051 printk(KERN_NOTICE
2052 "md/raid10:%s: %s: Failing raid device\n",
2053 mdname(mddev), b);
2054 md_error(mddev, conf->mirrors[d].rdev);
2055 return;
Robert Becker1e509152009-12-14 12:49:58 +11002056 }
Robert Becker1e509152009-12-14 12:49:58 +11002057
NeilBrown6814d532006-10-03 01:15:45 -07002058 while(sectors) {
2059 int s = sectors;
2060 int sl = r10_bio->read_slot;
2061 int success = 0;
2062 int start;
2063
2064 if (s > (PAGE_SIZE>>9))
2065 s = PAGE_SIZE >> 9;
2066
2067 rcu_read_lock();
2068 do {
NeilBrown8dbed5c2011-07-28 11:39:24 +10002069 sector_t first_bad;
2070 int bad_sectors;
2071
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002072 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07002073 rdev = rcu_dereference(conf->mirrors[d].rdev);
2074 if (rdev &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002075 test_bit(In_sync, &rdev->flags) &&
2076 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2077 &first_bad, &bad_sectors) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07002078 atomic_inc(&rdev->nr_pending);
2079 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11002080 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07002081 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002082 sect,
NeilBrown6814d532006-10-03 01:15:45 -07002083 s<<9,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002084 conf->tmppage, READ, false);
NeilBrown6814d532006-10-03 01:15:45 -07002085 rdev_dec_pending(rdev, mddev);
2086 rcu_read_lock();
2087 if (success)
2088 break;
2089 }
2090 sl++;
2091 if (sl == conf->copies)
2092 sl = 0;
2093 } while (!success && sl != r10_bio->read_slot);
2094 rcu_read_unlock();
2095
2096 if (!success) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002097 /* Cannot read from anywhere, just mark the block
2098 * as bad on the first device to discourage future
2099 * reads.
2100 */
NeilBrown6814d532006-10-03 01:15:45 -07002101 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
NeilBrown58c54fc2011-07-28 11:39:25 +10002102 rdev = conf->mirrors[dn].rdev;
2103
2104 if (!rdev_set_badblocks(
2105 rdev,
2106 r10_bio->devs[r10_bio->read_slot].addr
2107 + sect,
2108 s, 0))
2109 md_error(mddev, rdev);
NeilBrown6814d532006-10-03 01:15:45 -07002110 break;
2111 }
2112
2113 start = sl;
2114 /* write it back and re-read */
2115 rcu_read_lock();
2116 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11002117 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002118
NeilBrown6814d532006-10-03 01:15:45 -07002119 if (sl==0)
2120 sl = conf->copies;
2121 sl--;
2122 d = r10_bio->devs[sl].devnum;
2123 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002124 if (!rdev ||
2125 !test_bit(In_sync, &rdev->flags))
2126 continue;
2127
2128 atomic_inc(&rdev->nr_pending);
2129 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002130 if (r10_sync_page_io(rdev,
2131 r10_bio->devs[sl].addr +
2132 sect,
2133 s<<9, conf->tmppage, WRITE)
NeilBrown1294b9c2011-07-28 11:39:23 +10002134 == 0) {
2135 /* Well, this device is dead */
2136 printk(KERN_NOTICE
2137 "md/raid10:%s: read correction "
2138 "write failed"
2139 " (%d sectors at %llu on %s)\n",
2140 mdname(mddev), s,
2141 (unsigned long long)(
2142 sect + rdev->data_offset),
2143 bdevname(rdev->bdev, b));
2144 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2145 "drive\n",
2146 mdname(mddev),
2147 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07002148 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002149 rdev_dec_pending(rdev, mddev);
2150 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002151 }
2152 sl = start;
2153 while (sl != r10_bio->read_slot) {
NeilBrown1294b9c2011-07-28 11:39:23 +10002154 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002155
NeilBrown6814d532006-10-03 01:15:45 -07002156 if (sl==0)
2157 sl = conf->copies;
2158 sl--;
2159 d = r10_bio->devs[sl].devnum;
2160 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002161 if (!rdev ||
2162 !test_bit(In_sync, &rdev->flags))
2163 continue;
Robert Becker67b8dc42009-12-14 12:49:57 +11002164
NeilBrown1294b9c2011-07-28 11:39:23 +10002165 atomic_inc(&rdev->nr_pending);
2166 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002167 switch (r10_sync_page_io(rdev,
2168 r10_bio->devs[sl].addr +
2169 sect,
2170 s<<9, conf->tmppage,
2171 READ)) {
2172 case 0:
NeilBrown1294b9c2011-07-28 11:39:23 +10002173 /* Well, this device is dead */
2174 printk(KERN_NOTICE
2175 "md/raid10:%s: unable to read back "
2176 "corrected sectors"
2177 " (%d sectors at %llu on %s)\n",
2178 mdname(mddev), s,
2179 (unsigned long long)(
2180 sect + rdev->data_offset),
2181 bdevname(rdev->bdev, b));
2182 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2183 "drive\n",
2184 mdname(mddev),
2185 bdevname(rdev->bdev, b));
NeilBrown58c54fc2011-07-28 11:39:25 +10002186 break;
2187 case 1:
NeilBrown1294b9c2011-07-28 11:39:23 +10002188 printk(KERN_INFO
2189 "md/raid10:%s: read error corrected"
2190 " (%d sectors at %llu on %s)\n",
2191 mdname(mddev), s,
2192 (unsigned long long)(
2193 sect + rdev->data_offset),
2194 bdevname(rdev->bdev, b));
2195 atomic_add(s, &rdev->corrected_errors);
NeilBrown6814d532006-10-03 01:15:45 -07002196 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002197
2198 rdev_dec_pending(rdev, mddev);
2199 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002200 }
2201 rcu_read_unlock();
2202
2203 sectors -= s;
2204 sect += s;
2205 }
2206}
2207
NeilBrownbd870a12011-07-28 11:39:24 +10002208static void bi_complete(struct bio *bio, int error)
2209{
2210 complete((struct completion *)bio->bi_private);
2211}
2212
2213static int submit_bio_wait(int rw, struct bio *bio)
2214{
2215 struct completion event;
2216 rw |= REQ_SYNC;
2217
2218 init_completion(&event);
2219 bio->bi_private = &event;
2220 bio->bi_end_io = bi_complete;
2221 submit_bio(rw, bio);
2222 wait_for_completion(&event);
2223
2224 return test_bit(BIO_UPTODATE, &bio->bi_flags);
2225}
2226
NeilBrown9f2c9d12011-10-11 16:48:43 +11002227static int narrow_write_error(struct r10bio *r10_bio, int i)
NeilBrownbd870a12011-07-28 11:39:24 +10002228{
2229 struct bio *bio = r10_bio->master_bio;
NeilBrownfd01b882011-10-11 16:47:53 +11002230 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002231 struct r10conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002232 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
NeilBrownbd870a12011-07-28 11:39:24 +10002233 /* bio has the data to be written to slot 'i' where
2234 * we just recently had a write error.
2235 * We repeatedly clone the bio and trim down to one block,
2236 * then try the write. Where the write fails we record
2237 * a bad block.
2238 * It is conceivable that the bio doesn't exactly align with
2239 * blocks. We must handle this.
2240 *
2241 * We currently own a reference to the rdev.
2242 */
2243
2244 int block_sectors;
2245 sector_t sector;
2246 int sectors;
2247 int sect_to_write = r10_bio->sectors;
2248 int ok = 1;
2249
2250 if (rdev->badblocks.shift < 0)
2251 return 0;
2252
2253 block_sectors = 1 << rdev->badblocks.shift;
2254 sector = r10_bio->sector;
2255 sectors = ((r10_bio->sector + block_sectors)
2256 & ~(sector_t)(block_sectors - 1))
2257 - sector;
2258
2259 while (sect_to_write) {
2260 struct bio *wbio;
2261 if (sectors > sect_to_write)
2262 sectors = sect_to_write;
2263 /* Write at 'sector' for 'sectors' */
2264 wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
2265 md_trim_bio(wbio, sector - bio->bi_sector, sectors);
2266 wbio->bi_sector = (r10_bio->devs[i].addr+
2267 rdev->data_offset+
2268 (sector - r10_bio->sector));
2269 wbio->bi_bdev = rdev->bdev;
2270 if (submit_bio_wait(WRITE, wbio) == 0)
2271 /* Failure! */
2272 ok = rdev_set_badblocks(rdev, sector,
2273 sectors, 0)
2274 && ok;
2275
2276 bio_put(wbio);
2277 sect_to_write -= sectors;
2278 sector += sectors;
2279 sectors = block_sectors;
2280 }
2281 return ok;
2282}
2283
NeilBrown9f2c9d12011-10-11 16:48:43 +11002284static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown560f8e52011-07-28 11:39:23 +10002285{
2286 int slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002287 struct bio *bio;
NeilBrowne879a872011-10-11 16:49:02 +11002288 struct r10conf *conf = mddev->private;
NeilBrownabbf0982011-12-23 10:17:54 +11002289 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002290 char b[BDEVNAME_SIZE];
2291 unsigned long do_sync;
NeilBrown856e08e2011-07-28 11:39:23 +10002292 int max_sectors;
NeilBrown560f8e52011-07-28 11:39:23 +10002293
2294 /* we got a read error. Maybe the drive is bad. Maybe just
2295 * the block and we can fix it.
2296 * We freeze all other IO, and try reading the block from
2297 * other devices. When we find one, we re-write
2298 * and check it that fixes the read error.
2299 * This is all done synchronously while the array is
2300 * frozen.
2301 */
2302 if (mddev->ro == 0) {
2303 freeze_array(conf);
2304 fix_read_error(conf, mddev, r10_bio);
2305 unfreeze_array(conf);
2306 }
NeilBrownabbf0982011-12-23 10:17:54 +11002307 rdev_dec_pending(rdev, mddev);
NeilBrown560f8e52011-07-28 11:39:23 +10002308
2309 bio = r10_bio->devs[slot].bio;
NeilBrown7399c312011-07-28 11:39:23 +10002310 bdevname(bio->bi_bdev, b);
NeilBrown560f8e52011-07-28 11:39:23 +10002311 r10_bio->devs[slot].bio =
2312 mddev->ro ? IO_BLOCKED : NULL;
NeilBrown7399c312011-07-28 11:39:23 +10002313read_more:
NeilBrown96c3fd12011-12-23 10:17:54 +11002314 rdev = read_balance(conf, r10_bio, &max_sectors);
2315 if (rdev == NULL) {
NeilBrown560f8e52011-07-28 11:39:23 +10002316 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
2317 " read error for block %llu\n",
NeilBrown7399c312011-07-28 11:39:23 +10002318 mdname(mddev), b,
NeilBrown560f8e52011-07-28 11:39:23 +10002319 (unsigned long long)r10_bio->sector);
2320 raid_end_bio_io(r10_bio);
2321 bio_put(bio);
2322 return;
2323 }
2324
2325 do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
NeilBrown7399c312011-07-28 11:39:23 +10002326 if (bio)
2327 bio_put(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002328 slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002329 printk_ratelimited(
2330 KERN_ERR
2331 "md/raid10:%s: %s: redirecting"
2332 "sector %llu to another mirror\n",
2333 mdname(mddev),
2334 bdevname(rdev->bdev, b),
2335 (unsigned long long)r10_bio->sector);
2336 bio = bio_clone_mddev(r10_bio->master_bio,
2337 GFP_NOIO, mddev);
NeilBrown7399c312011-07-28 11:39:23 +10002338 md_trim_bio(bio,
2339 r10_bio->sector - bio->bi_sector,
2340 max_sectors);
NeilBrown560f8e52011-07-28 11:39:23 +10002341 r10_bio->devs[slot].bio = bio;
NeilBrownabbf0982011-12-23 10:17:54 +11002342 r10_bio->devs[slot].rdev = rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002343 bio->bi_sector = r10_bio->devs[slot].addr
2344 + rdev->data_offset;
2345 bio->bi_bdev = rdev->bdev;
2346 bio->bi_rw = READ | do_sync;
2347 bio->bi_private = r10_bio;
2348 bio->bi_end_io = raid10_end_read_request;
NeilBrown7399c312011-07-28 11:39:23 +10002349 if (max_sectors < r10_bio->sectors) {
2350 /* Drat - have to split this up more */
2351 struct bio *mbio = r10_bio->master_bio;
2352 int sectors_handled =
2353 r10_bio->sector + max_sectors
2354 - mbio->bi_sector;
2355 r10_bio->sectors = max_sectors;
2356 spin_lock_irq(&conf->device_lock);
2357 if (mbio->bi_phys_segments == 0)
2358 mbio->bi_phys_segments = 2;
2359 else
2360 mbio->bi_phys_segments++;
2361 spin_unlock_irq(&conf->device_lock);
2362 generic_make_request(bio);
2363 bio = NULL;
2364
2365 r10_bio = mempool_alloc(conf->r10bio_pool,
2366 GFP_NOIO);
2367 r10_bio->master_bio = mbio;
2368 r10_bio->sectors = (mbio->bi_size >> 9)
2369 - sectors_handled;
2370 r10_bio->state = 0;
2371 set_bit(R10BIO_ReadError,
2372 &r10_bio->state);
2373 r10_bio->mddev = mddev;
2374 r10_bio->sector = mbio->bi_sector
2375 + sectors_handled;
2376
2377 goto read_more;
2378 } else
2379 generic_make_request(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002380}
2381
NeilBrowne879a872011-10-11 16:49:02 +11002382static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
NeilBrown749c55e2011-07-28 11:39:24 +10002383{
2384 /* Some sort of write request has finished and it
2385 * succeeded in writing where we thought there was a
2386 * bad block. So forget the bad block.
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002387 * Or possibly if failed and we need to record
2388 * a bad block.
NeilBrown749c55e2011-07-28 11:39:24 +10002389 */
2390 int m;
NeilBrown3cb03002011-10-11 16:45:26 +11002391 struct md_rdev *rdev;
NeilBrown749c55e2011-07-28 11:39:24 +10002392
2393 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2394 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002395 for (m = 0; m < conf->copies; m++) {
2396 int dev = r10_bio->devs[m].devnum;
2397 rdev = conf->mirrors[dev].rdev;
2398 if (r10_bio->devs[m].bio == NULL)
2399 continue;
2400 if (test_bit(BIO_UPTODATE,
NeilBrown749c55e2011-07-28 11:39:24 +10002401 &r10_bio->devs[m].bio->bi_flags)) {
NeilBrown749c55e2011-07-28 11:39:24 +10002402 rdev_clear_badblocks(
2403 rdev,
2404 r10_bio->devs[m].addr,
2405 r10_bio->sectors);
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002406 } else {
2407 if (!rdev_set_badblocks(
2408 rdev,
2409 r10_bio->devs[m].addr,
2410 r10_bio->sectors, 0))
2411 md_error(conf->mddev, rdev);
NeilBrown749c55e2011-07-28 11:39:24 +10002412 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002413 rdev = conf->mirrors[dev].replacement;
2414 if (r10_bio->devs[m].repl_bio == NULL)
2415 continue;
2416 if (test_bit(BIO_UPTODATE,
2417 &r10_bio->devs[m].repl_bio->bi_flags)) {
2418 rdev_clear_badblocks(
2419 rdev,
2420 r10_bio->devs[m].addr,
2421 r10_bio->sectors);
2422 } else {
2423 if (!rdev_set_badblocks(
2424 rdev,
2425 r10_bio->devs[m].addr,
2426 r10_bio->sectors, 0))
2427 md_error(conf->mddev, rdev);
2428 }
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002429 }
NeilBrown749c55e2011-07-28 11:39:24 +10002430 put_buf(r10_bio);
2431 } else {
NeilBrownbd870a12011-07-28 11:39:24 +10002432 for (m = 0; m < conf->copies; m++) {
2433 int dev = r10_bio->devs[m].devnum;
2434 struct bio *bio = r10_bio->devs[m].bio;
2435 rdev = conf->mirrors[dev].rdev;
2436 if (bio == IO_MADE_GOOD) {
NeilBrown749c55e2011-07-28 11:39:24 +10002437 rdev_clear_badblocks(
2438 rdev,
2439 r10_bio->devs[m].addr,
2440 r10_bio->sectors);
2441 rdev_dec_pending(rdev, conf->mddev);
NeilBrownbd870a12011-07-28 11:39:24 +10002442 } else if (bio != NULL &&
2443 !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2444 if (!narrow_write_error(r10_bio, m)) {
2445 md_error(conf->mddev, rdev);
2446 set_bit(R10BIO_Degraded,
2447 &r10_bio->state);
2448 }
2449 rdev_dec_pending(rdev, conf->mddev);
NeilBrown749c55e2011-07-28 11:39:24 +10002450 }
NeilBrown475b0322011-12-23 10:17:55 +11002451 bio = r10_bio->devs[m].repl_bio;
2452 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +11002453 if (rdev && bio == IO_MADE_GOOD) {
NeilBrown475b0322011-12-23 10:17:55 +11002454 rdev_clear_badblocks(
2455 rdev,
2456 r10_bio->devs[m].addr,
2457 r10_bio->sectors);
2458 rdev_dec_pending(rdev, conf->mddev);
2459 }
NeilBrownbd870a12011-07-28 11:39:24 +10002460 }
2461 if (test_bit(R10BIO_WriteError,
2462 &r10_bio->state))
2463 close_write(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10002464 raid_end_bio_io(r10_bio);
2465 }
2466}
2467
NeilBrownfd01b882011-10-11 16:47:53 +11002468static void raid10d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
NeilBrown9f2c9d12011-10-11 16:48:43 +11002470 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 unsigned long flags;
NeilBrowne879a872011-10-11 16:49:02 +11002472 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002474 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
2476 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002478 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002480
Jens Axboe7eaceac2011-03-10 08:52:07 +01002481 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002484 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002485 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002487 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002488 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08002490 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 spin_unlock_irqrestore(&conf->device_lock, flags);
2492
2493 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002494 conf = mddev->private;
NeilBrownbd870a12011-07-28 11:39:24 +10002495 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2496 test_bit(R10BIO_WriteError, &r10_bio->state))
NeilBrown749c55e2011-07-28 11:39:24 +10002497 handle_write_completed(conf, r10_bio);
2498 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01002500 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 recovery_request_write(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002502 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
NeilBrown560f8e52011-07-28 11:39:23 +10002503 handle_read_error(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002504 else {
2505 /* just a partial read to be scheduled from a
2506 * separate context
2507 */
2508 int slot = r10_bio->read_slot;
2509 generic_make_request(r10_bio->devs[slot].bio);
2510 }
NeilBrown4443ae12006-01-06 00:20:28 -08002511
NeilBrown1d9d5242009-10-16 15:55:32 +11002512 cond_resched();
NeilBrownde393cd2011-07-28 11:31:48 +10002513 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2514 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002516 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517}
2518
2519
NeilBrowne879a872011-10-11 16:49:02 +11002520static int init_resync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
2522 int buffs;
NeilBrown69335ef2011-12-23 10:17:54 +11002523 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
2525 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02002526 BUG_ON(conf->r10buf_pool);
NeilBrown69335ef2011-12-23 10:17:54 +11002527 conf->have_replacement = 0;
2528 for (i = 0; i < conf->raid_disks; i++)
2529 if (conf->mirrors[i].replacement)
2530 conf->have_replacement = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2532 if (!conf->r10buf_pool)
2533 return -ENOMEM;
2534 conf->next_resync = 0;
2535 return 0;
2536}
2537
2538/*
2539 * perform a "sync" on one "block"
2540 *
2541 * We need to make sure that no normal I/O request - particularly write
2542 * requests - conflict with active sync requests.
2543 *
2544 * This is achieved by tracking pending requests and a 'barrier' concept
2545 * that can be installed to exclude normal IO requests.
2546 *
2547 * Resync and recovery are handled very differently.
2548 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2549 *
2550 * For resync, we iterate over virtual addresses, read all copies,
2551 * and update if there are differences. If only one copy is live,
2552 * skip it.
2553 * For recovery, we iterate over physical addresses, read a good
2554 * value for each non-in_sync drive, and over-write.
2555 *
2556 * So, for recovery we may have several outstanding complex requests for a
2557 * given address, one for each out-of-sync device. We model this by allocating
2558 * a number of r10_bio structures, one for each out-of-sync device.
2559 * As we setup these structures, we collect all bio's together into a list
2560 * which we then process collectively to add pages, and then process again
2561 * to pass to generic_make_request.
2562 *
2563 * The r10_bio structures are linked using a borrowed master_bio pointer.
2564 * This link is counted in ->remaining. When the r10_bio that points to NULL
2565 * has its remaining count decremented to 0, the whole complex operation
2566 * is complete.
2567 *
2568 */
2569
NeilBrownfd01b882011-10-11 16:47:53 +11002570static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
NeilBrownab9d47e2011-05-11 14:54:41 +10002571 int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572{
NeilBrowne879a872011-10-11 16:49:02 +11002573 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002574 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 struct bio *biolist = NULL, *bio;
2576 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08002578 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11002579 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 sector_t sectors_skipped = 0;
2581 int chunks_skipped = 0;
2582
2583 if (!conf->r10buf_pool)
2584 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002585 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
2587 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11002588 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2590 max_sector = mddev->resync_max_sectors;
2591 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002592 /* If we aborted, we need to abort the
2593 * sync on the 'current' bitmap chucks (there can
2594 * be several when recovering multiple devices).
2595 * as we may have started syncing it but not finished.
2596 * We can find the current address in
2597 * mddev->curr_resync, but for recovery,
2598 * we need to convert that to several
2599 * virtual addresses.
2600 */
2601 if (mddev->curr_resync < max_sector) { /* aborted */
2602 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2603 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2604 &sync_blocks, 1);
2605 else for (i=0; i<conf->raid_disks; i++) {
2606 sector_t sect =
2607 raid10_find_virt(conf, mddev->curr_resync, i);
2608 bitmap_end_sync(mddev->bitmap, sect,
2609 &sync_blocks, 1);
2610 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002611 } else {
2612 /* completed sync */
2613 if ((!mddev->bitmap || conf->fullsync)
2614 && conf->have_replacement
2615 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2616 /* Completed a full sync so the replacements
2617 * are now fully recovered.
2618 */
2619 for (i = 0; i < conf->raid_disks; i++)
2620 if (conf->mirrors[i].replacement)
2621 conf->mirrors[i].replacement
2622 ->recovery_offset
2623 = MaxSector;
2624 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002625 conf->fullsync = 0;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002626 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002627 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07002629 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 return sectors_skipped;
2631 }
2632 if (chunks_skipped >= conf->raid_disks) {
2633 /* if there has been nothing to do on any drive,
2634 * then there is nothing to do at all..
2635 */
NeilBrown57afd892005-06-21 17:17:13 -07002636 *skipped = 1;
2637 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 }
2639
NeilBrownc6207272008-02-06 01:39:52 -08002640 if (max_sector > mddev->resync_max)
2641 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 /* make sure whole request will fit in a chunk - if chunks
2644 * are meaningful
2645 */
2646 if (conf->near_copies < conf->raid_disks &&
2647 max_sector > (sector_nr | conf->chunk_mask))
2648 max_sector = (sector_nr | conf->chunk_mask) + 1;
2649 /*
2650 * If there is non-resync activity waiting for us then
2651 * put in a delay to throttle resync.
2652 */
NeilBrown0a27ec92006-01-06 00:20:13 -08002653 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
2656 /* Again, very different code for resync and recovery.
2657 * Both must result in an r10bio with a list of bios that
2658 * have bi_end_io, bi_sector, bi_bdev set,
2659 * and bi_private set to the r10bio.
2660 * For recovery, we may actually create several r10bios
2661 * with 2 bios in each, that correspond to the bios in the main one.
2662 * In this case, the subordinate r10bios link back through a
2663 * borrowed master_bio pointer, and the counter in the master
2664 * includes a ref from each subordinate.
2665 */
2666 /* First, we decide what to do and set ->bi_end_io
2667 * To end_sync_read if we want to read, and
2668 * end_sync_write if we will want to write.
2669 */
2670
NeilBrown6cce3b22006-01-06 00:20:16 -08002671 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2673 /* recovery... the complicated one */
NeilBrowne875ece2011-07-28 11:39:24 +10002674 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 r10_bio = NULL;
2676
NeilBrownab9d47e2011-05-11 14:54:41 +10002677 for (i=0 ; i<conf->raid_disks; i++) {
2678 int still_degraded;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002679 struct r10bio *rb2;
NeilBrownab9d47e2011-05-11 14:54:41 +10002680 sector_t sect;
2681 int must_sync;
NeilBrowne875ece2011-07-28 11:39:24 +10002682 int any_working;
NeilBrown24afd802011-12-23 10:17:55 +11002683 struct mirror_info *mirror = &conf->mirrors[i];
NeilBrownab9d47e2011-05-11 14:54:41 +10002684
NeilBrown24afd802011-12-23 10:17:55 +11002685 if ((mirror->rdev == NULL ||
2686 test_bit(In_sync, &mirror->rdev->flags))
2687 &&
2688 (mirror->replacement == NULL ||
2689 test_bit(Faulty,
2690 &mirror->replacement->flags)))
NeilBrownab9d47e2011-05-11 14:54:41 +10002691 continue;
2692
2693 still_degraded = 0;
2694 /* want to reconstruct this device */
2695 rb2 = r10_bio;
2696 sect = raid10_find_virt(conf, sector_nr, i);
NeilBrown24afd802011-12-23 10:17:55 +11002697 /* Unless we are doing a full sync, or a replacement
2698 * we only need to recover the block if it is set in
2699 * the bitmap
NeilBrownab9d47e2011-05-11 14:54:41 +10002700 */
2701 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2702 &sync_blocks, 1);
2703 if (sync_blocks < max_sync)
2704 max_sync = sync_blocks;
2705 if (!must_sync &&
NeilBrown24afd802011-12-23 10:17:55 +11002706 mirror->replacement == NULL &&
NeilBrownab9d47e2011-05-11 14:54:41 +10002707 !conf->fullsync) {
2708 /* yep, skip the sync_blocks here, but don't assume
2709 * that there will never be anything to do here
NeilBrown6cce3b22006-01-06 00:20:16 -08002710 */
NeilBrownab9d47e2011-05-11 14:54:41 +10002711 chunks_skipped = -1;
2712 continue;
2713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
NeilBrownab9d47e2011-05-11 14:54:41 +10002715 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2716 raise_barrier(conf, rb2 != NULL);
2717 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
NeilBrownab9d47e2011-05-11 14:54:41 +10002719 r10_bio->master_bio = (struct bio*)rb2;
2720 if (rb2)
2721 atomic_inc(&rb2->remaining);
2722 r10_bio->mddev = mddev;
2723 set_bit(R10BIO_IsRecover, &r10_bio->state);
2724 r10_bio->sector = sect;
NeilBrown6cce3b22006-01-06 00:20:16 -08002725
NeilBrownab9d47e2011-05-11 14:54:41 +10002726 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10002727
NeilBrownab9d47e2011-05-11 14:54:41 +10002728 /* Need to check if the array will still be
2729 * degraded
2730 */
2731 for (j=0; j<conf->raid_disks; j++)
2732 if (conf->mirrors[j].rdev == NULL ||
2733 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
2734 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07002735 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002737
2738 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2739 &sync_blocks, still_degraded);
2740
NeilBrowne875ece2011-07-28 11:39:24 +10002741 any_working = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10002742 for (j=0; j<conf->copies;j++) {
NeilBrowne875ece2011-07-28 11:39:24 +10002743 int k;
NeilBrownab9d47e2011-05-11 14:54:41 +10002744 int d = r10_bio->devs[j].devnum;
NeilBrown5e570282011-07-28 11:39:25 +10002745 sector_t from_addr, to_addr;
NeilBrown3cb03002011-10-11 16:45:26 +11002746 struct md_rdev *rdev;
NeilBrown40c356c2011-07-28 11:39:24 +10002747 sector_t sector, first_bad;
2748 int bad_sectors;
NeilBrownab9d47e2011-05-11 14:54:41 +10002749 if (!conf->mirrors[d].rdev ||
2750 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
2751 continue;
2752 /* This is where we read from */
NeilBrowne875ece2011-07-28 11:39:24 +10002753 any_working = 1;
NeilBrown40c356c2011-07-28 11:39:24 +10002754 rdev = conf->mirrors[d].rdev;
2755 sector = r10_bio->devs[j].addr;
2756
2757 if (is_badblock(rdev, sector, max_sync,
2758 &first_bad, &bad_sectors)) {
2759 if (first_bad > sector)
2760 max_sync = first_bad - sector;
2761 else {
2762 bad_sectors -= (sector
2763 - first_bad);
2764 if (max_sync > bad_sectors)
2765 max_sync = bad_sectors;
2766 continue;
2767 }
2768 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002769 bio = r10_bio->devs[0].bio;
2770 bio->bi_next = biolist;
2771 biolist = bio;
2772 bio->bi_private = r10_bio;
2773 bio->bi_end_io = end_sync_read;
2774 bio->bi_rw = READ;
NeilBrown5e570282011-07-28 11:39:25 +10002775 from_addr = r10_bio->devs[j].addr;
NeilBrown24afd802011-12-23 10:17:55 +11002776 bio->bi_sector = from_addr + rdev->data_offset;
2777 bio->bi_bdev = rdev->bdev;
2778 atomic_inc(&rdev->nr_pending);
2779 /* and we write to 'i' (if not in_sync) */
NeilBrownab9d47e2011-05-11 14:54:41 +10002780
2781 for (k=0; k<conf->copies; k++)
2782 if (r10_bio->devs[k].devnum == i)
2783 break;
2784 BUG_ON(k == conf->copies);
NeilBrown5e570282011-07-28 11:39:25 +10002785 to_addr = r10_bio->devs[k].addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10002786 r10_bio->devs[0].devnum = d;
NeilBrown5e570282011-07-28 11:39:25 +10002787 r10_bio->devs[0].addr = from_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10002788 r10_bio->devs[1].devnum = i;
NeilBrown5e570282011-07-28 11:39:25 +10002789 r10_bio->devs[1].addr = to_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10002790
NeilBrown24afd802011-12-23 10:17:55 +11002791 rdev = mirror->rdev;
2792 if (!test_bit(In_sync, &rdev->flags)) {
2793 bio = r10_bio->devs[1].bio;
2794 bio->bi_next = biolist;
2795 biolist = bio;
2796 bio->bi_private = r10_bio;
2797 bio->bi_end_io = end_sync_write;
2798 bio->bi_rw = WRITE;
2799 bio->bi_sector = to_addr
2800 + rdev->data_offset;
2801 bio->bi_bdev = rdev->bdev;
2802 atomic_inc(&r10_bio->remaining);
2803 } else
2804 r10_bio->devs[1].bio->bi_end_io = NULL;
2805
2806 /* and maybe write to replacement */
2807 bio = r10_bio->devs[1].repl_bio;
2808 if (bio)
2809 bio->bi_end_io = NULL;
2810 rdev = mirror->replacement;
2811 /* Note: if rdev != NULL, then bio
2812 * cannot be NULL as r10buf_pool_alloc will
2813 * have allocated it.
2814 * So the second test here is pointless.
2815 * But it keeps semantic-checkers happy, and
2816 * this comment keeps human reviewers
2817 * happy.
2818 */
2819 if (rdev == NULL || bio == NULL ||
2820 test_bit(Faulty, &rdev->flags))
2821 break;
2822 bio->bi_next = biolist;
2823 biolist = bio;
2824 bio->bi_private = r10_bio;
2825 bio->bi_end_io = end_sync_write;
2826 bio->bi_rw = WRITE;
2827 bio->bi_sector = to_addr + rdev->data_offset;
2828 bio->bi_bdev = rdev->bdev;
2829 atomic_inc(&r10_bio->remaining);
NeilBrownab9d47e2011-05-11 14:54:41 +10002830 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002832 if (j == conf->copies) {
NeilBrowne875ece2011-07-28 11:39:24 +10002833 /* Cannot recover, so abort the recovery or
2834 * record a bad block */
NeilBrownab9d47e2011-05-11 14:54:41 +10002835 put_buf(r10_bio);
2836 if (rb2)
2837 atomic_dec(&rb2->remaining);
2838 r10_bio = rb2;
NeilBrowne875ece2011-07-28 11:39:24 +10002839 if (any_working) {
2840 /* problem is that there are bad blocks
2841 * on other device(s)
2842 */
2843 int k;
2844 for (k = 0; k < conf->copies; k++)
2845 if (r10_bio->devs[k].devnum == i)
2846 break;
NeilBrown24afd802011-12-23 10:17:55 +11002847 if (!test_bit(In_sync,
2848 &mirror->rdev->flags)
2849 && !rdev_set_badblocks(
2850 mirror->rdev,
2851 r10_bio->devs[k].addr,
2852 max_sync, 0))
2853 any_working = 0;
2854 if (mirror->replacement &&
2855 !rdev_set_badblocks(
2856 mirror->replacement,
NeilBrowne875ece2011-07-28 11:39:24 +10002857 r10_bio->devs[k].addr,
2858 max_sync, 0))
2859 any_working = 0;
2860 }
2861 if (!any_working) {
2862 if (!test_and_set_bit(MD_RECOVERY_INTR,
2863 &mddev->recovery))
2864 printk(KERN_INFO "md/raid10:%s: insufficient "
2865 "working devices for recovery.\n",
2866 mdname(mddev));
NeilBrown24afd802011-12-23 10:17:55 +11002867 mirror->recovery_disabled
NeilBrowne875ece2011-07-28 11:39:24 +10002868 = mddev->recovery_disabled;
2869 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002870 break;
2871 }
2872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 if (biolist == NULL) {
2874 while (r10_bio) {
NeilBrown9f2c9d12011-10-11 16:48:43 +11002875 struct r10bio *rb2 = r10_bio;
2876 r10_bio = (struct r10bio*) rb2->master_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 rb2->master_bio = NULL;
2878 put_buf(rb2);
2879 }
2880 goto giveup;
2881 }
2882 } else {
2883 /* resync. Schedule a read for every block at this virt offset */
2884 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08002885
NeilBrown78200d42009-02-25 13:18:47 +11002886 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
2887
NeilBrown6cce3b22006-01-06 00:20:16 -08002888 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2889 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10002890 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
2891 &mddev->recovery)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002892 /* We can skip this block */
2893 *skipped = 1;
2894 return sync_blocks + sectors_skipped;
2895 }
2896 if (sync_blocks < max_sync)
2897 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2899
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 r10_bio->mddev = mddev;
2901 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08002902 raise_barrier(conf, 0);
2903 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
2905 r10_bio->master_bio = NULL;
2906 r10_bio->sector = sector_nr;
2907 set_bit(R10BIO_IsSync, &r10_bio->state);
2908 raid10_find_phys(conf, r10_bio);
2909 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
2910
2911 for (i=0; i<conf->copies; i++) {
2912 int d = r10_bio->devs[i].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10002913 sector_t first_bad, sector;
2914 int bad_sectors;
2915
NeilBrown9ad1aef2011-12-23 10:17:55 +11002916 if (r10_bio->devs[i].repl_bio)
2917 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
2918
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 bio = r10_bio->devs[i].bio;
2920 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002921 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08002923 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 continue;
NeilBrown40c356c2011-07-28 11:39:24 +10002925 sector = r10_bio->devs[i].addr;
2926 if (is_badblock(conf->mirrors[d].rdev,
2927 sector, max_sync,
2928 &first_bad, &bad_sectors)) {
2929 if (first_bad > sector)
2930 max_sync = first_bad - sector;
2931 else {
2932 bad_sectors -= (sector - first_bad);
2933 if (max_sync > bad_sectors)
2934 max_sync = max_sync;
2935 continue;
2936 }
2937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2939 atomic_inc(&r10_bio->remaining);
2940 bio->bi_next = biolist;
2941 biolist = bio;
2942 bio->bi_private = r10_bio;
2943 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08002944 bio->bi_rw = READ;
NeilBrown40c356c2011-07-28 11:39:24 +10002945 bio->bi_sector = sector +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 conf->mirrors[d].rdev->data_offset;
2947 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2948 count++;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002949
2950 if (conf->mirrors[d].replacement == NULL ||
2951 test_bit(Faulty,
2952 &conf->mirrors[d].replacement->flags))
2953 continue;
2954
2955 /* Need to set up for writing to the replacement */
2956 bio = r10_bio->devs[i].repl_bio;
2957 clear_bit(BIO_UPTODATE, &bio->bi_flags);
2958
2959 sector = r10_bio->devs[i].addr;
2960 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2961 bio->bi_next = biolist;
2962 biolist = bio;
2963 bio->bi_private = r10_bio;
2964 bio->bi_end_io = end_sync_write;
2965 bio->bi_rw = WRITE;
2966 bio->bi_sector = sector +
2967 conf->mirrors[d].replacement->data_offset;
2968 bio->bi_bdev = conf->mirrors[d].replacement->bdev;
2969 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 }
2971
2972 if (count < 2) {
2973 for (i=0; i<conf->copies; i++) {
2974 int d = r10_bio->devs[i].devnum;
2975 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10002976 rdev_dec_pending(conf->mirrors[d].rdev,
2977 mddev);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002978 if (r10_bio->devs[i].repl_bio &&
2979 r10_bio->devs[i].repl_bio->bi_end_io)
2980 rdev_dec_pending(
2981 conf->mirrors[d].replacement,
2982 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 }
2984 put_buf(r10_bio);
2985 biolist = NULL;
2986 goto giveup;
2987 }
2988 }
2989
2990 for (bio = biolist; bio ; bio=bio->bi_next) {
2991
2992 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
2993 if (bio->bi_end_io)
2994 bio->bi_flags |= 1 << BIO_UPTODATE;
2995 bio->bi_vcnt = 0;
2996 bio->bi_idx = 0;
2997 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 bio->bi_size = 0;
2999 }
3000
3001 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003002 if (sector_nr + max_sync < max_sector)
3003 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 do {
3005 struct page *page;
3006 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 if (sector_nr + (len>>9) > max_sector)
3008 len = (max_sector - sector_nr) << 9;
3009 if (len == 0)
3010 break;
3011 for (bio= biolist ; bio ; bio=bio->bi_next) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003012 struct bio *bio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
NeilBrownab9d47e2011-05-11 14:54:41 +10003014 if (bio_add_page(bio, page, len, 0))
3015 continue;
3016
3017 /* stop here */
3018 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
3019 for (bio2 = biolist;
3020 bio2 && bio2 != bio;
3021 bio2 = bio2->bi_next) {
3022 /* remove last page from this bio */
3023 bio2->bi_vcnt--;
3024 bio2->bi_size -= len;
3025 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003027 goto bio_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 }
3029 nr_sectors += len>>9;
3030 sector_nr += len>>9;
3031 } while (biolist->bi_vcnt < RESYNC_PAGES);
3032 bio_full:
3033 r10_bio->sectors = nr_sectors;
3034
3035 while (biolist) {
3036 bio = biolist;
3037 biolist = biolist->bi_next;
3038
3039 bio->bi_next = NULL;
3040 r10_bio = bio->bi_private;
3041 r10_bio->sectors = nr_sectors;
3042
3043 if (bio->bi_end_io == end_sync_read) {
3044 md_sync_acct(bio->bi_bdev, nr_sectors);
3045 generic_make_request(bio);
3046 }
3047 }
3048
NeilBrown57afd892005-06-21 17:17:13 -07003049 if (sectors_skipped)
3050 /* pretend they weren't skipped, it makes
3051 * no important difference in this case
3052 */
3053 md_done_sync(mddev, sectors_skipped, 1);
3054
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 return sectors_skipped + nr_sectors;
3056 giveup:
3057 /* There is nowhere to write, so all non-sync
NeilBrowne875ece2011-07-28 11:39:24 +10003058 * drives must be failed or in resync, all drives
3059 * have a bad block, so try the next chunk...
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 */
NeilBrown09b40682009-02-25 13:18:47 +11003061 if (sector_nr + max_sync < max_sector)
3062 max_sector = sector_nr + max_sync;
3063
3064 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 chunks_skipped ++;
3066 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068}
3069
Dan Williams80c3a6c2009-03-17 18:10:40 -07003070static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11003071raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07003072{
3073 sector_t size;
NeilBrowne879a872011-10-11 16:49:02 +11003074 struct r10conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003075
3076 if (!raid_disks)
NeilBrown84707f32010-03-16 17:23:35 +11003077 raid_disks = conf->raid_disks;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003078 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003079 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003080
3081 size = sectors >> conf->chunk_shift;
3082 sector_div(size, conf->far_copies);
3083 size = size * raid_disks;
3084 sector_div(size, conf->near_copies);
3085
3086 return size << conf->chunk_shift;
3087}
3088
Trela, Maciejdab8b292010-03-08 16:02:45 +11003089
NeilBrowne879a872011-10-11 16:49:02 +11003090static struct r10conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091{
NeilBrowne879a872011-10-11 16:49:02 +11003092 struct r10conf *conf = NULL;
NeilBrownc93983b2006-06-26 00:27:41 -07003093 int nc, fc, fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 sector_t stride, size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003095 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
Maciej Trelaf73ea872010-06-16 11:46:29 +01003097 if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
3098 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown128595e2010-05-03 14:47:14 +10003099 printk(KERN_ERR "md/raid10:%s: chunk size must be "
3100 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
3101 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003102 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 }
NeilBrown2604b702006-01-06 00:20:36 -08003104
Maciej Trelaf73ea872010-06-16 11:46:29 +01003105 nc = mddev->new_layout & 255;
3106 fc = (mddev->new_layout >> 8) & 255;
3107 fo = mddev->new_layout & (1<<16);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003108
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
Maciej Trelaf73ea872010-06-16 11:46:29 +01003110 (mddev->new_layout >> 17)) {
NeilBrown128595e2010-05-03 14:47:14 +10003111 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
Maciej Trelaf73ea872010-06-16 11:46:29 +01003112 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 goto out;
3114 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11003115
3116 err = -ENOMEM;
NeilBrowne879a872011-10-11 16:49:02 +11003117 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003118 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003120
NeilBrown4443ae12006-01-06 00:20:28 -08003121 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Trela, Maciejdab8b292010-03-08 16:02:45 +11003122 GFP_KERNEL);
3123 if (!conf->mirrors)
3124 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08003125
3126 conf->tmppage = alloc_page(GFP_KERNEL);
3127 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003128 goto out;
3129
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
NeilBrown64a742b2007-02-28 20:11:18 -08003131 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 conf->near_copies = nc;
3133 conf->far_copies = fc;
3134 conf->copies = nc*fc;
NeilBrownc93983b2006-06-26 00:27:41 -07003135 conf->far_offset = fo;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003136 conf->chunk_mask = mddev->new_chunk_sectors - 1;
3137 conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
3138
3139 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
3140 r10bio_pool_free, conf);
3141 if (!conf->r10bio_pool)
3142 goto out;
3143
Andre Noll58c0fed2009-03-31 14:33:13 +11003144 size = mddev->dev_sectors >> conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08003145 sector_div(size, fc);
3146 size = size * conf->raid_disks;
3147 sector_div(size, nc);
3148 /* 'size' is now the number of chunks in the array */
3149 /* calculate "used chunks per device" in 'stride' */
3150 stride = size * conf->copies;
NeilBrownaf03b8e2007-06-16 10:16:06 -07003151
3152 /* We need to round up when dividing by raid_disks to
3153 * get the stride size.
3154 */
3155 stride += conf->raid_disks - 1;
NeilBrown64a742b2007-02-28 20:11:18 -08003156 sector_div(stride, conf->raid_disks);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003157
3158 conf->dev_sectors = stride << conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08003159
NeilBrownc93983b2006-06-26 00:27:41 -07003160 if (fo)
NeilBrown64a742b2007-02-28 20:11:18 -08003161 stride = 1;
3162 else
NeilBrownc93983b2006-06-26 00:27:41 -07003163 sector_div(stride, fc);
NeilBrown64a742b2007-02-28 20:11:18 -08003164 conf->stride = stride << conf->chunk_shift;
3165
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166
Neil Browne7e72bf2008-05-14 16:05:54 -07003167 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003168 INIT_LIST_HEAD(&conf->retry_list);
3169
3170 spin_lock_init(&conf->resync_lock);
3171 init_waitqueue_head(&conf->wait_barrier);
3172
3173 conf->thread = md_register_thread(raid10d, mddev, NULL);
3174 if (!conf->thread)
3175 goto out;
3176
Trela, Maciejdab8b292010-03-08 16:02:45 +11003177 conf->mddev = mddev;
3178 return conf;
3179
3180 out:
NeilBrown128595e2010-05-03 14:47:14 +10003181 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
Trela, Maciejdab8b292010-03-08 16:02:45 +11003182 mdname(mddev));
3183 if (conf) {
3184 if (conf->r10bio_pool)
3185 mempool_destroy(conf->r10bio_pool);
3186 kfree(conf->mirrors);
3187 safe_put_page(conf->tmppage);
3188 kfree(conf);
3189 }
3190 return ERR_PTR(err);
3191}
3192
NeilBrownfd01b882011-10-11 16:47:53 +11003193static int run(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003194{
NeilBrowne879a872011-10-11 16:49:02 +11003195 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003196 int i, disk_idx, chunk_size;
NeilBrown0f6d02d2011-10-11 16:48:46 +11003197 struct mirror_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11003198 struct md_rdev *rdev;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003199 sector_t size;
3200
3201 /*
3202 * copy the already verified devices into our private RAID10
3203 * bookkeeping area. [whatever we allocate in run(),
3204 * should be freed in stop()]
3205 */
3206
3207 if (mddev->private == NULL) {
3208 conf = setup_conf(mddev);
3209 if (IS_ERR(conf))
3210 return PTR_ERR(conf);
3211 mddev->private = conf;
3212 }
3213 conf = mddev->private;
3214 if (!conf)
3215 goto out;
3216
Trela, Maciejdab8b292010-03-08 16:02:45 +11003217 mddev->thread = conf->thread;
3218 conf->thread = NULL;
3219
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003220 chunk_size = mddev->chunk_sectors << 9;
3221 blk_queue_io_min(mddev->queue, chunk_size);
3222 if (conf->raid_disks % conf->near_copies)
3223 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
3224 else
3225 blk_queue_io_opt(mddev->queue, chunk_size *
3226 (conf->raid_disks / conf->near_copies));
3227
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003228 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown34b343c2011-07-28 11:31:47 +10003229
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 disk_idx = rdev->raid_disk;
NeilBrown84707f32010-03-16 17:23:35 +11003231 if (disk_idx >= conf->raid_disks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 || disk_idx < 0)
3233 continue;
3234 disk = conf->mirrors + disk_idx;
3235
NeilBrown56a2559b2011-12-23 10:17:55 +11003236 if (test_bit(Replacement, &rdev->flags)) {
3237 if (disk->replacement)
3238 goto out_free_conf;
3239 disk->replacement = rdev;
3240 } else {
3241 if (disk->rdev)
3242 goto out_free_conf;
3243 disk->rdev = rdev;
3244 }
3245
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 disk->rdev = rdev;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003247 disk_stack_limits(mddev->gendisk, rdev->bdev,
3248 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 /* as we don't honour merge_bvec_fn, we must never risk
NeilBrown627a2d32010-03-08 16:44:38 +11003250 * violating it, so limit max_segments to 1 lying
3251 * within a single page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 */
NeilBrown627a2d32010-03-08 16:44:38 +11003253 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
3254 blk_queue_max_segments(mddev->queue, 1);
3255 blk_queue_segment_boundary(mddev->queue,
3256 PAGE_CACHE_SIZE - 1);
3257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
3259 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 }
NeilBrown6d508242005-09-09 16:24:03 -07003261 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10003262 if (!enough(conf, -1)) {
NeilBrown128595e2010-05-03 14:47:14 +10003263 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07003264 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 goto out_free_conf;
3266 }
3267
3268 mddev->degraded = 0;
3269 for (i = 0; i < conf->raid_disks; i++) {
3270
3271 disk = conf->mirrors + i;
3272
NeilBrown56a2559b2011-12-23 10:17:55 +11003273 if (!disk->rdev && disk->replacement) {
3274 /* The replacement is all we have - use it */
3275 disk->rdev = disk->replacement;
3276 disk->replacement = NULL;
3277 clear_bit(Replacement, &disk->rdev->flags);
3278 }
3279
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003280 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07003281 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 disk->head_position = 0;
3283 mddev->degraded++;
Neil Brown8c2e8702008-06-28 08:30:52 +10003284 if (disk->rdev)
3285 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 }
NeilBrownd890fa22011-10-26 11:54:39 +11003287 disk->recovery_disabled = mddev->recovery_disabled - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 }
3289
Andre Noll8c6ac862009-06-18 08:48:06 +10003290 if (mddev->recovery_cp != MaxSector)
NeilBrown128595e2010-05-03 14:47:14 +10003291 printk(KERN_NOTICE "md/raid10:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10003292 " -- starting background reconstruction\n",
3293 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 printk(KERN_INFO
NeilBrown128595e2010-05-03 14:47:14 +10003295 "md/raid10:%s: active with %d out of %d devices\n",
NeilBrown84707f32010-03-16 17:23:35 +11003296 mdname(mddev), conf->raid_disks - mddev->degraded,
3297 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 /*
3299 * Ok, everything is just fine now
3300 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11003301 mddev->dev_sectors = conf->dev_sectors;
3302 size = raid10_size(mddev, 0, 0);
3303 md_set_array_sectors(mddev, size);
3304 mddev->resync_max_sectors = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
NeilBrown0d129222006-10-03 01:15:54 -07003306 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
3307 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown7a5febe2005-05-16 21:53:16 -07003308
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 /* Calculate max read-ahead size.
3310 * We need to readahead at least twice a whole stripe....
3311 * maybe...
3312 */
3313 {
Andre Noll9d8f0362009-06-18 08:45:01 +10003314 int stripe = conf->raid_disks *
3315 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 stripe /= conf->near_copies;
3317 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
3318 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
3319 }
3320
NeilBrown84707f32010-03-16 17:23:35 +11003321 if (conf->near_copies < conf->raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
Martin K. Petersena91a2782011-03-17 11:11:05 +01003323
3324 if (md_integrity_register(mddev))
3325 goto out_free_conf;
3326
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 return 0;
3328
3329out_free_conf:
NeilBrown01f96c02011-09-21 15:30:20 +10003330 md_unregister_thread(&mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 if (conf->r10bio_pool)
3332 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08003333 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003334 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 kfree(conf);
3336 mddev->private = NULL;
3337out:
3338 return -EIO;
3339}
3340
NeilBrownfd01b882011-10-11 16:47:53 +11003341static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342{
NeilBrowne879a872011-10-11 16:49:02 +11003343 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
NeilBrown409c57f2009-03-31 14:39:39 +11003345 raise_barrier(conf, 0);
3346 lower_barrier(conf);
3347
NeilBrown01f96c02011-09-21 15:30:20 +10003348 md_unregister_thread(&mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
3350 if (conf->r10bio_pool)
3351 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003352 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 kfree(conf);
3354 mddev->private = NULL;
3355 return 0;
3356}
3357
NeilBrownfd01b882011-10-11 16:47:53 +11003358static void raid10_quiesce(struct mddev *mddev, int state)
NeilBrown6cce3b22006-01-06 00:20:16 -08003359{
NeilBrowne879a872011-10-11 16:49:02 +11003360 struct r10conf *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08003361
3362 switch(state) {
3363 case 1:
3364 raise_barrier(conf, 0);
3365 break;
3366 case 0:
3367 lower_barrier(conf);
3368 break;
3369 }
NeilBrown6cce3b22006-01-06 00:20:16 -08003370}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
NeilBrownfd01b882011-10-11 16:47:53 +11003372static void *raid10_takeover_raid0(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003373{
NeilBrown3cb03002011-10-11 16:45:26 +11003374 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +11003375 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003376
3377 if (mddev->degraded > 0) {
NeilBrown128595e2010-05-03 14:47:14 +10003378 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
3379 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003380 return ERR_PTR(-EINVAL);
3381 }
3382
Trela, Maciejdab8b292010-03-08 16:02:45 +11003383 /* Set new parameters */
3384 mddev->new_level = 10;
3385 /* new layout: far_copies = 1, near_copies = 2 */
3386 mddev->new_layout = (1<<8) + 2;
3387 mddev->new_chunk_sectors = mddev->chunk_sectors;
3388 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003389 mddev->raid_disks *= 2;
3390 /* make sure it will be not marked as dirty */
3391 mddev->recovery_cp = MaxSector;
3392
3393 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003394 if (!IS_ERR(conf)) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003395 list_for_each_entry(rdev, &mddev->disks, same_set)
3396 if (rdev->raid_disk >= 0)
3397 rdev->new_raid_disk = rdev->raid_disk * 2;
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003398 conf->barrier = 1;
3399 }
3400
Trela, Maciejdab8b292010-03-08 16:02:45 +11003401 return conf;
3402}
3403
NeilBrownfd01b882011-10-11 16:47:53 +11003404static void *raid10_takeover(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003405{
NeilBrowne373ab12011-10-11 16:48:59 +11003406 struct r0conf *raid0_conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003407
3408 /* raid10 can take over:
3409 * raid0 - providing it has only two drives
3410 */
3411 if (mddev->level == 0) {
3412 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11003413 raid0_conf = mddev->private;
3414 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown128595e2010-05-03 14:47:14 +10003415 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
3416 " with more than one zone.\n",
3417 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003418 return ERR_PTR(-EINVAL);
3419 }
3420 return raid10_takeover_raid0(mddev);
3421 }
3422 return ERR_PTR(-EINVAL);
3423}
3424
NeilBrown84fc4b52011-10-11 16:49:58 +11003425static struct md_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426{
3427 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08003428 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 .owner = THIS_MODULE,
3430 .make_request = make_request,
3431 .run = run,
3432 .stop = stop,
3433 .status = status,
3434 .error_handler = error,
3435 .hot_add_disk = raid10_add_disk,
3436 .hot_remove_disk= raid10_remove_disk,
3437 .spare_active = raid10_spare_active,
3438 .sync_request = sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08003439 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07003440 .size = raid10_size,
Trela, Maciejdab8b292010-03-08 16:02:45 +11003441 .takeover = raid10_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442};
3443
3444static int __init raid_init(void)
3445{
NeilBrown2604b702006-01-06 00:20:36 -08003446 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447}
3448
3449static void raid_exit(void)
3450{
NeilBrown2604b702006-01-06 00:20:36 -08003451 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452}
3453
3454module_init(raid_init);
3455module_exit(raid_exit);
3456MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11003457MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08003459MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08003460MODULE_ALIAS("md-level-10");
NeilBrown34db0cd2011-10-11 16:50:01 +11003461
3462module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);