blob: 0cc798582b29e27919e195b74f4ea3434d483be8 [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);
NeilBrownfae8cc52012-02-14 11:10:10 +110070static int enough(struct r10conf *conf, int ignore);
NeilBrown0a27ec92006-01-06 00:20:13 -080071
Al Virodd0fc662005-10-07 07:46:04 +010072static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
NeilBrowne879a872011-10-11 16:49:02 +110074 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +110075 int size = offsetof(struct r10bio, devs[conf->copies]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
NeilBrown69335ef2011-12-23 10:17:54 +110077 /* allocate a r10bio with room for raid_disks entries in the
78 * bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +010079 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82static void r10bio_pool_free(void *r10_bio, void *data)
83{
84 kfree(r10_bio);
85}
86
NeilBrown0310fa22008-08-05 15:54:14 +100087/* Maximum size of each resync request */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define RESYNC_BLOCK_SIZE (64*1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
NeilBrown0310fa22008-08-05 15:54:14 +100090/* amount of memory to reserve for resync requests */
91#define RESYNC_WINDOW (1024*1024)
92/* maximum number of concurrent requests, memory permitting */
93#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/*
96 * When performing a resync, we need to read and compare, so
97 * we need as many pages are there are copies.
98 * When performing a recovery, we need 2 bios, one for read,
99 * one for write (we recover only one drive per r10buf)
100 *
101 */
Al Virodd0fc662005-10-07 07:46:04 +0100102static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
NeilBrowne879a872011-10-11 16:49:02 +1100104 struct r10conf *conf = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 struct page *page;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100106 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 struct bio *bio;
108 int i, j;
109 int nalloc;
110
111 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100112 if (!r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
116 nalloc = conf->copies; /* resync */
117 else
118 nalloc = 2; /* recovery */
119
120 /*
121 * Allocate bios.
122 */
123 for (j = nalloc ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +1100124 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (!bio)
126 goto out_free_bio;
127 r10_bio->devs[j].bio = bio;
NeilBrown69335ef2011-12-23 10:17:54 +1100128 if (!conf->have_replacement)
129 continue;
130 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
131 if (!bio)
132 goto out_free_bio;
133 r10_bio->devs[j].repl_bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135 /*
136 * Allocate RESYNC_PAGES data pages and attach them
137 * where needed.
138 */
139 for (j = 0 ; j < nalloc; j++) {
NeilBrown69335ef2011-12-23 10:17:54 +1100140 struct bio *rbio = r10_bio->devs[j].repl_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 bio = r10_bio->devs[j].bio;
142 for (i = 0; i < RESYNC_PAGES; i++) {
Namhyung Kimc65060a2011-07-18 17:38:49 +1000143 if (j == 1 && !test_bit(MD_RECOVERY_SYNC,
144 &conf->mddev->recovery)) {
145 /* we can share bv_page's during recovery */
146 struct bio *rbio = r10_bio->devs[0].bio;
147 page = rbio->bi_io_vec[i].bv_page;
148 get_page(page);
149 } else
150 page = alloc_page(gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (unlikely(!page))
152 goto out_free_pages;
153
154 bio->bi_io_vec[i].bv_page = page;
NeilBrown69335ef2011-12-23 10:17:54 +1100155 if (rbio)
156 rbio->bi_io_vec[i].bv_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
158 }
159
160 return r10_bio;
161
162out_free_pages:
163 for ( ; i > 0 ; i--)
NeilBrown1345b1d2006-01-06 00:20:40 -0800164 safe_put_page(bio->bi_io_vec[i-1].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 while (j--)
166 for (i = 0; i < RESYNC_PAGES ; i++)
NeilBrown1345b1d2006-01-06 00:20:40 -0800167 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 j = -1;
169out_free_bio:
NeilBrown69335ef2011-12-23 10:17:54 +1100170 while (++j < nalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 bio_put(r10_bio->devs[j].bio);
NeilBrown69335ef2011-12-23 10:17:54 +1100172 if (r10_bio->devs[j].repl_bio)
173 bio_put(r10_bio->devs[j].repl_bio);
174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 r10bio_pool_free(r10_bio, conf);
176 return NULL;
177}
178
179static void r10buf_pool_free(void *__r10_bio, void *data)
180{
181 int i;
NeilBrowne879a872011-10-11 16:49:02 +1100182 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100183 struct r10bio *r10bio = __r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 int j;
185
186 for (j=0; j < conf->copies; j++) {
187 struct bio *bio = r10bio->devs[j].bio;
188 if (bio) {
189 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown1345b1d2006-01-06 00:20:40 -0800190 safe_put_page(bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 bio->bi_io_vec[i].bv_page = NULL;
192 }
193 bio_put(bio);
194 }
NeilBrown69335ef2011-12-23 10:17:54 +1100195 bio = r10bio->devs[j].repl_bio;
196 if (bio)
197 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199 r10bio_pool_free(r10bio, conf);
200}
201
NeilBrowne879a872011-10-11 16:49:02 +1100202static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 int i;
205
206 for (i = 0; i < conf->copies; i++) {
207 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown749c55e2011-07-28 11:39:24 +1000208 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 bio_put(*bio);
210 *bio = NULL;
NeilBrown69335ef2011-12-23 10:17:54 +1100211 bio = &r10_bio->devs[i].repl_bio;
212 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
213 bio_put(*bio);
214 *bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216}
217
NeilBrown9f2c9d12011-10-11 16:48:43 +1100218static void free_r10bio(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
NeilBrowne879a872011-10-11 16:49:02 +1100220 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 put_all_bios(conf, r10_bio);
223 mempool_free(r10_bio, conf->r10bio_pool);
224}
225
NeilBrown9f2c9d12011-10-11 16:48:43 +1100226static void put_buf(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
NeilBrowne879a872011-10-11 16:49:02 +1100228 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 mempool_free(r10_bio, conf->r10buf_pool);
231
NeilBrown0a27ec92006-01-06 00:20:13 -0800232 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
NeilBrown9f2c9d12011-10-11 16:48:43 +1100235static void reschedule_retry(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +1100238 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +1100239 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 spin_lock_irqsave(&conf->device_lock, flags);
242 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800243 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 spin_unlock_irqrestore(&conf->device_lock, flags);
245
Arthur Jones388667b2008-07-25 12:03:38 -0700246 /* wake up frozen array... */
247 wake_up(&conf->wait_barrier);
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 md_wakeup_thread(mddev->thread);
250}
251
252/*
253 * raid_end_bio_io() is called when we have finished servicing a mirrored
254 * operation and are ready to return a success/failure code to the buffer
255 * cache layer.
256 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100257static void raid_end_bio_io(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct bio *bio = r10_bio->master_bio;
NeilBrown856e08e2011-07-28 11:39:23 +1000260 int done;
NeilBrowne879a872011-10-11 16:49:02 +1100261 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
NeilBrown856e08e2011-07-28 11:39:23 +1000263 if (bio->bi_phys_segments) {
264 unsigned long flags;
265 spin_lock_irqsave(&conf->device_lock, flags);
266 bio->bi_phys_segments--;
267 done = (bio->bi_phys_segments == 0);
268 spin_unlock_irqrestore(&conf->device_lock, flags);
269 } else
270 done = 1;
271 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
272 clear_bit(BIO_UPTODATE, &bio->bi_flags);
273 if (done) {
274 bio_endio(bio, 0);
275 /*
276 * Wake up any possible resync thread that waits for the device
277 * to go idle.
278 */
279 allow_barrier(conf);
280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 free_r10bio(r10_bio);
282}
283
284/*
285 * Update disk head position estimator based on IRQ completion info.
286 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100287static inline void update_head_pos(int slot, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
NeilBrowne879a872011-10-11 16:49:02 +1100289 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
292 r10_bio->devs[slot].addr + (r10_bio->sectors);
293}
294
Namhyung Kim778ca012011-07-18 17:38:47 +1000295/*
296 * Find the disk number which triggered given bio
297 */
NeilBrowne879a872011-10-11 16:49:02 +1100298static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
NeilBrown69335ef2011-12-23 10:17:54 +1100299 struct bio *bio, int *slotp, int *replp)
Namhyung Kim778ca012011-07-18 17:38:47 +1000300{
301 int slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100302 int repl = 0;
Namhyung Kim778ca012011-07-18 17:38:47 +1000303
NeilBrown69335ef2011-12-23 10:17:54 +1100304 for (slot = 0; slot < conf->copies; slot++) {
Namhyung Kim778ca012011-07-18 17:38:47 +1000305 if (r10_bio->devs[slot].bio == bio)
306 break;
NeilBrown69335ef2011-12-23 10:17:54 +1100307 if (r10_bio->devs[slot].repl_bio == bio) {
308 repl = 1;
309 break;
310 }
311 }
Namhyung Kim778ca012011-07-18 17:38:47 +1000312
313 BUG_ON(slot == conf->copies);
314 update_head_pos(slot, r10_bio);
315
NeilBrown749c55e2011-07-28 11:39:24 +1000316 if (slotp)
317 *slotp = slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100318 if (replp)
319 *replp = repl;
Namhyung Kim778ca012011-07-18 17:38:47 +1000320 return r10_bio->devs[slot].devnum;
321}
322
NeilBrown6712ecf2007-09-27 12:47:43 +0200323static void raid10_end_read_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +1100326 struct r10bio *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 int slot, dev;
NeilBrownabbf0982011-12-23 10:17:54 +1100328 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +1100329 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 slot = r10_bio->read_slot;
333 dev = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100334 rdev = r10_bio->devs[slot].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 /*
336 * this branch is our 'one mirror IO has finished' event handler:
337 */
NeilBrown4443ae12006-01-06 00:20:28 -0800338 update_head_pos(slot, r10_bio);
339
340 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 /*
342 * Set R10BIO_Uptodate in our master bio, so that
343 * we will return a good error code to the higher
344 * levels even if IO on some other mirrored buffer fails.
345 *
346 * The 'master' represents the composite IO operation to
347 * user-side. So if something waits for IO, then it will
348 * wait for the 'master' bio.
349 */
350 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrownfae8cc52012-02-14 11:10:10 +1100351 } else {
352 /* If all other devices that store this block have
353 * failed, we want to return the error upwards rather
354 * than fail the last device. Here we redefine
355 * "uptodate" to mean "Don't want to retry"
356 */
357 unsigned long flags;
358 spin_lock_irqsave(&conf->device_lock, flags);
359 if (!enough(conf, rdev->raid_disk))
360 uptodate = 1;
361 spin_unlock_irqrestore(&conf->device_lock, flags);
362 }
363 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 raid_end_bio_io(r10_bio);
NeilBrownabbf0982011-12-23 10:17:54 +1100365 rdev_dec_pending(rdev, conf->mddev);
NeilBrown4443ae12006-01-06 00:20:28 -0800366 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 /*
NeilBrown7c4e06f2011-05-11 14:53:17 +1000368 * oops, read error - keep the refcount on the rdev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 */
370 char b[BDEVNAME_SIZE];
Christian Dietrich8bda4702011-07-27 11:00:36 +1000371 printk_ratelimited(KERN_ERR
372 "md/raid10:%s: %s: rescheduling sector %llu\n",
373 mdname(conf->mddev),
NeilBrownabbf0982011-12-23 10:17:54 +1100374 bdevname(rdev->bdev, b),
Christian Dietrich8bda4702011-07-27 11:00:36 +1000375 (unsigned long long)r10_bio->sector);
NeilBrown856e08e2011-07-28 11:39:23 +1000376 set_bit(R10BIO_ReadError, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 reschedule_retry(r10_bio);
378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
NeilBrown9f2c9d12011-10-11 16:48:43 +1100381static void close_write(struct r10bio *r10_bio)
NeilBrownbd870a12011-07-28 11:39:24 +1000382{
383 /* clear the bitmap if all writes complete successfully */
384 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
385 r10_bio->sectors,
386 !test_bit(R10BIO_Degraded, &r10_bio->state),
387 0);
388 md_write_end(r10_bio->mddev);
389}
390
NeilBrown9f2c9d12011-10-11 16:48:43 +1100391static void one_write_done(struct r10bio *r10_bio)
NeilBrown19d5f832011-09-10 17:21:17 +1000392{
393 if (atomic_dec_and_test(&r10_bio->remaining)) {
394 if (test_bit(R10BIO_WriteError, &r10_bio->state))
395 reschedule_retry(r10_bio);
396 else {
397 close_write(r10_bio);
398 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
399 reschedule_retry(r10_bio);
400 else
401 raid_end_bio_io(r10_bio);
402 }
403 }
404}
405
NeilBrown6712ecf2007-09-27 12:47:43 +0200406static void raid10_end_write_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +1100409 struct r10bio *r10_bio = bio->bi_private;
Namhyung Kim778ca012011-07-18 17:38:47 +1000410 int dev;
NeilBrown749c55e2011-07-28 11:39:24 +1000411 int dec_rdev = 1;
NeilBrowne879a872011-10-11 16:49:02 +1100412 struct r10conf *conf = r10_bio->mddev->private;
NeilBrown475b0322011-12-23 10:17:55 +1100413 int slot, repl;
NeilBrown4ca40c22011-12-23 10:17:55 +1100414 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
NeilBrown475b0322011-12-23 10:17:55 +1100416 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
NeilBrown475b0322011-12-23 10:17:55 +1100418 if (repl)
419 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +1100420 if (!rdev) {
421 smp_rmb();
422 repl = 0;
NeilBrown475b0322011-12-23 10:17:55 +1100423 rdev = conf->mirrors[dev].rdev;
NeilBrown4ca40c22011-12-23 10:17:55 +1100424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 /*
426 * this branch is our 'one mirror IO has finished' event handler:
427 */
NeilBrown6cce3b22006-01-06 00:20:16 -0800428 if (!uptodate) {
NeilBrown475b0322011-12-23 10:17:55 +1100429 if (repl)
430 /* Never record new bad blocks to replacement,
431 * just fail it.
432 */
433 md_error(rdev->mddev, rdev);
434 else {
435 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +1100436 if (!test_and_set_bit(WantReplacement, &rdev->flags))
437 set_bit(MD_RECOVERY_NEEDED,
438 &rdev->mddev->recovery);
NeilBrown475b0322011-12-23 10:17:55 +1100439 set_bit(R10BIO_WriteError, &r10_bio->state);
440 dec_rdev = 0;
441 }
NeilBrown749c55e2011-07-28 11:39:24 +1000442 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /*
444 * Set R10BIO_Uptodate in our master bio, so that
445 * we will return a good error code for to the higher
446 * levels even if IO on some other mirrored buffer fails.
447 *
448 * The 'master' represents the composite IO operation to
449 * user-side. So if something waits for IO, then it will
450 * wait for the 'master' bio.
451 */
NeilBrown749c55e2011-07-28 11:39:24 +1000452 sector_t first_bad;
453 int bad_sectors;
454
Alex Lyakas0938e132013-06-04 20:42:21 +0300455 /*
456 * Do not set R10BIO_Uptodate if the current device is
457 * rebuilding or Faulty. This is because we cannot use
458 * such device for properly reading the data back (we could
459 * potentially use it, if the current write would have felt
460 * before rdev->recovery_offset, but for simplicity we don't
461 * check this here.
462 */
463 if (test_bit(In_sync, &rdev->flags) &&
464 !test_bit(Faulty, &rdev->flags))
465 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
NeilBrown749c55e2011-07-28 11:39:24 +1000467 /* Maybe we can clear some bad blocks. */
NeilBrown475b0322011-12-23 10:17:55 +1100468 if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +1000469 r10_bio->devs[slot].addr,
470 r10_bio->sectors,
471 &first_bad, &bad_sectors)) {
472 bio_put(bio);
NeilBrown475b0322011-12-23 10:17:55 +1100473 if (repl)
474 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
475 else
476 r10_bio->devs[slot].bio = IO_MADE_GOOD;
NeilBrown749c55e2011-07-28 11:39:24 +1000477 dec_rdev = 0;
478 set_bit(R10BIO_MadeGood, &r10_bio->state);
479 }
480 }
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 /*
483 *
484 * Let's see if all mirrored write operations have finished
485 * already.
486 */
NeilBrown19d5f832011-09-10 17:21:17 +1000487 one_write_done(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +1000488 if (dec_rdev)
NeilBrownecf049c2012-11-22 15:12:09 +1100489 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492/*
493 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300494 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 * parameters: near_copies and far_copies.
496 * near_copies * far_copies must be <= raid_disks.
497 * Normally one of these will be 1.
498 * If both are 1, we get raid0.
499 * If near_copies == raid_disks, we get raid1.
500 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300501 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 * first chunk, followed by near_copies copies of the next chunk and
503 * so on.
504 * If far_copies > 1, then after 1/far_copies of the array has been assigned
505 * as described above, we start again with a device offset of near_copies.
506 * So we effectively have another copy of the whole array further down all
507 * the drives, but with blocks on different drives.
508 * With this layout, and block is never stored twice on the one device.
509 *
510 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700511 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 *
513 * raid10_find_virt does the reverse mapping, from a device and a
514 * sector offset to a virtual address
515 */
516
NeilBrowne879a872011-10-11 16:49:02 +1100517static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 int n,f;
520 sector_t sector;
521 sector_t chunk;
522 sector_t stripe;
523 int dev;
524
525 int slot = 0;
526
527 /* now calculate first sector/dev */
528 chunk = r10bio->sector >> conf->chunk_shift;
529 sector = r10bio->sector & conf->chunk_mask;
530
531 chunk *= conf->near_copies;
532 stripe = chunk;
533 dev = sector_div(stripe, conf->raid_disks);
NeilBrownc93983b2006-06-26 00:27:41 -0700534 if (conf->far_offset)
535 stripe *= conf->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 sector += stripe << conf->chunk_shift;
538
539 /* and calculate all the others */
540 for (n=0; n < conf->near_copies; n++) {
541 int d = dev;
542 sector_t s = sector;
543 r10bio->devs[slot].addr = sector;
544 r10bio->devs[slot].devnum = d;
545 slot++;
546
547 for (f = 1; f < conf->far_copies; f++) {
548 d += conf->near_copies;
549 if (d >= conf->raid_disks)
550 d -= conf->raid_disks;
551 s += conf->stride;
552 r10bio->devs[slot].devnum = d;
553 r10bio->devs[slot].addr = s;
554 slot++;
555 }
556 dev++;
557 if (dev >= conf->raid_disks) {
558 dev = 0;
559 sector += (conf->chunk_mask + 1);
560 }
561 }
562 BUG_ON(slot != conf->copies);
563}
564
NeilBrowne879a872011-10-11 16:49:02 +1100565static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 sector_t offset, chunk, vchunk;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 offset = sector & conf->chunk_mask;
NeilBrownc93983b2006-06-26 00:27:41 -0700570 if (conf->far_offset) {
571 int fc;
572 chunk = sector >> conf->chunk_shift;
573 fc = sector_div(chunk, conf->far_copies);
574 dev -= fc * conf->near_copies;
575 if (dev < 0)
576 dev += conf->raid_disks;
577 } else {
NeilBrown64a742b2007-02-28 20:11:18 -0800578 while (sector >= conf->stride) {
NeilBrownc93983b2006-06-26 00:27:41 -0700579 sector -= conf->stride;
580 if (dev < conf->near_copies)
581 dev += conf->raid_disks - conf->near_copies;
582 else
583 dev -= conf->near_copies;
584 }
585 chunk = sector >> conf->chunk_shift;
586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 vchunk = chunk * conf->raid_disks + dev;
588 sector_div(vchunk, conf->near_copies);
589 return (vchunk << conf->chunk_shift) + offset;
590}
591
592/**
593 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
594 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200595 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 * @biovec: the request that could be merged to it.
597 *
598 * Return amount of bytes we can accept at this offset
NeilBrown050b6612012-03-19 12:46:39 +1100599 * This requires checking for end-of-chunk if near_copies != raid_disks,
600 * and for subordinate merge_bvec_fns if merge_check_needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200602static int raid10_mergeable_bvec(struct request_queue *q,
603 struct bvec_merge_data *bvm,
604 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
NeilBrownfd01b882011-10-11 16:47:53 +1100606 struct mddev *mddev = q->queuedata;
NeilBrown050b6612012-03-19 12:46:39 +1100607 struct r10conf *conf = mddev->private;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200608 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +1000610 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200611 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
NeilBrown050b6612012-03-19 12:46:39 +1100613 if (conf->near_copies < conf->raid_disks) {
614 max = (chunk_sectors - ((sector & (chunk_sectors - 1))
615 + bio_sectors)) << 9;
616 if (max < 0)
617 /* bio_add cannot handle a negative return */
618 max = 0;
619 if (max <= biovec->bv_len && bio_sectors == 0)
620 return biovec->bv_len;
621 } else
622 max = biovec->bv_len;
623
624 if (mddev->merge_check_needed) {
NeilBrown956b1652012-08-18 09:51:42 +1000625 struct {
626 struct r10bio r10_bio;
627 struct r10dev devs[conf->copies];
628 } on_stack;
629 struct r10bio *r10_bio = &on_stack.r10_bio;
NeilBrown050b6612012-03-19 12:46:39 +1100630 int s;
NeilBrown956b1652012-08-18 09:51:42 +1000631 r10_bio->sector = sector;
632 raid10_find_phys(conf, r10_bio);
NeilBrown050b6612012-03-19 12:46:39 +1100633 rcu_read_lock();
634 for (s = 0; s < conf->copies; s++) {
NeilBrown956b1652012-08-18 09:51:42 +1000635 int disk = r10_bio->devs[s].devnum;
NeilBrown050b6612012-03-19 12:46:39 +1100636 struct md_rdev *rdev = rcu_dereference(
637 conf->mirrors[disk].rdev);
638 if (rdev && !test_bit(Faulty, &rdev->flags)) {
639 struct request_queue *q =
640 bdev_get_queue(rdev->bdev);
641 if (q->merge_bvec_fn) {
NeilBrown956b1652012-08-18 09:51:42 +1000642 bvm->bi_sector = r10_bio->devs[s].addr
NeilBrown050b6612012-03-19 12:46:39 +1100643 + rdev->data_offset;
644 bvm->bi_bdev = rdev->bdev;
645 max = min(max, q->merge_bvec_fn(
646 q, bvm, biovec));
647 }
648 }
649 rdev = rcu_dereference(conf->mirrors[disk].replacement);
650 if (rdev && !test_bit(Faulty, &rdev->flags)) {
651 struct request_queue *q =
652 bdev_get_queue(rdev->bdev);
653 if (q->merge_bvec_fn) {
NeilBrown956b1652012-08-18 09:51:42 +1000654 bvm->bi_sector = r10_bio->devs[s].addr
NeilBrown050b6612012-03-19 12:46:39 +1100655 + rdev->data_offset;
656 bvm->bi_bdev = rdev->bdev;
657 max = min(max, q->merge_bvec_fn(
658 q, bvm, biovec));
659 }
660 }
661 }
662 rcu_read_unlock();
663 }
664 return max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
667/*
668 * This routine returns the disk from which the requested read should
669 * be done. There is a per-array 'next expected sequential IO' sector
670 * number - if this matches on the next IO then we use the last disk.
671 * There is also a per-disk 'last know head position' sector that is
672 * maintained from IRQ contexts, both the normal and the resync IO
673 * completion handlers update this position correctly. If there is no
674 * perfect sequential match then we pick the disk whose head is closest.
675 *
676 * If there are 2 mirrors in the same 2 devices, performance degrades
677 * because position is mirror, not device based.
678 *
679 * The rdev for the device selected will have nr_pending incremented.
680 */
681
682/*
683 * FIXME: possibly should rethink readbalancing and do it differently
684 * depending on near_copies / far_copies geometry.
685 */
NeilBrown96c3fd12011-12-23 10:17:54 +1100686static struct md_rdev *read_balance(struct r10conf *conf,
687 struct r10bio *r10_bio,
688 int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000690 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000691 int disk, slot;
NeilBrown856e08e2011-07-28 11:39:23 +1000692 int sectors = r10_bio->sectors;
693 int best_good_sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000694 sector_t new_distance, best_dist;
NeilBrownabbf0982011-12-23 10:17:54 +1100695 struct md_rdev *rdev, *best_rdev;
NeilBrown56d99122011-05-11 14:27:03 +1000696 int do_balance;
697 int best_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 raid10_find_phys(conf, r10_bio);
700 rcu_read_lock();
NeilBrown56d99122011-05-11 14:27:03 +1000701retry:
NeilBrown856e08e2011-07-28 11:39:23 +1000702 sectors = r10_bio->sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000703 best_slot = -1;
NeilBrownabbf0982011-12-23 10:17:54 +1100704 best_rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000705 best_dist = MaxSector;
NeilBrown856e08e2011-07-28 11:39:23 +1000706 best_good_sectors = 0;
NeilBrown56d99122011-05-11 14:27:03 +1000707 do_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 /*
709 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800710 * device if no resync is going on (recovery is ok), or below
711 * the resync window. We take the first readable disk when
712 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 */
714 if (conf->mddev->recovery_cp < MaxSector
NeilBrown56d99122011-05-11 14:27:03 +1000715 && (this_sector + sectors >= conf->next_resync))
716 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
NeilBrown56d99122011-05-11 14:27:03 +1000718 for (slot = 0; slot < conf->copies ; slot++) {
NeilBrown856e08e2011-07-28 11:39:23 +1000719 sector_t first_bad;
720 int bad_sectors;
721 sector_t dev_sector;
722
NeilBrown56d99122011-05-11 14:27:03 +1000723 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000725 disk = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100726 rdev = rcu_dereference(conf->mirrors[disk].replacement);
727 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
NeilBrown050b6612012-03-19 12:46:39 +1100728 test_bit(Unmerged, &rdev->flags) ||
NeilBrownabbf0982011-12-23 10:17:54 +1100729 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
730 rdev = rcu_dereference(conf->mirrors[disk].rdev);
NeilBrown050b6612012-03-19 12:46:39 +1100731 if (rdev == NULL ||
732 test_bit(Faulty, &rdev->flags) ||
733 test_bit(Unmerged, &rdev->flags))
NeilBrownabbf0982011-12-23 10:17:54 +1100734 continue;
735 if (!test_bit(In_sync, &rdev->flags) &&
736 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
NeilBrown56d99122011-05-11 14:27:03 +1000737 continue;
738
NeilBrown856e08e2011-07-28 11:39:23 +1000739 dev_sector = r10_bio->devs[slot].addr;
740 if (is_badblock(rdev, dev_sector, sectors,
741 &first_bad, &bad_sectors)) {
742 if (best_dist < MaxSector)
743 /* Already have a better slot */
744 continue;
745 if (first_bad <= dev_sector) {
746 /* Cannot read here. If this is the
747 * 'primary' device, then we must not read
748 * beyond 'bad_sectors' from another device.
749 */
750 bad_sectors -= (dev_sector - first_bad);
751 if (!do_balance && sectors > bad_sectors)
752 sectors = bad_sectors;
753 if (best_good_sectors > sectors)
754 best_good_sectors = sectors;
755 } else {
756 sector_t good_sectors =
757 first_bad - dev_sector;
758 if (good_sectors > best_good_sectors) {
759 best_good_sectors = good_sectors;
760 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100761 best_rdev = rdev;
NeilBrown856e08e2011-07-28 11:39:23 +1000762 }
763 if (!do_balance)
764 /* Must read from here */
765 break;
766 }
767 continue;
768 } else
769 best_good_sectors = sectors;
770
NeilBrown56d99122011-05-11 14:27:03 +1000771 if (!do_balance)
772 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
NeilBrown22dfdf52005-11-28 13:44:09 -0800774 /* This optimisation is debatable, and completely destroys
775 * sequential read speed for 'far copies' arrays. So only
776 * keep it for 'near' arrays, and review those later.
777 */
NeilBrown56d99122011-05-11 14:27:03 +1000778 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 break;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800780
781 /* for far > 1 always use the lowest address */
782 if (conf->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000783 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800784 else
NeilBrown56d99122011-05-11 14:27:03 +1000785 new_distance = abs(r10_bio->devs[slot].addr -
786 conf->mirrors[disk].head_position);
787 if (new_distance < best_dist) {
788 best_dist = new_distance;
789 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100790 best_rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792 }
NeilBrownabbf0982011-12-23 10:17:54 +1100793 if (slot >= conf->copies) {
NeilBrown56d99122011-05-11 14:27:03 +1000794 slot = best_slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100795 rdev = best_rdev;
796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
NeilBrown56d99122011-05-11 14:27:03 +1000798 if (slot >= 0) {
NeilBrown56d99122011-05-11 14:27:03 +1000799 atomic_inc(&rdev->nr_pending);
800 if (test_bit(Faulty, &rdev->flags)) {
801 /* Cannot risk returning a device that failed
802 * before we inc'ed nr_pending
803 */
804 rdev_dec_pending(rdev, conf->mddev);
805 goto retry;
806 }
807 r10_bio->read_slot = slot;
808 } else
NeilBrown96c3fd12011-12-23 10:17:54 +1100809 rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 rcu_read_unlock();
NeilBrown856e08e2011-07-28 11:39:23 +1000811 *max_sectors = best_good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
NeilBrown96c3fd12011-12-23 10:17:54 +1100813 return rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
NeilBrown0d129222006-10-03 01:15:54 -0700816static int raid10_congested(void *data, int bits)
817{
NeilBrownfd01b882011-10-11 16:47:53 +1100818 struct mddev *mddev = data;
NeilBrowne879a872011-10-11 16:49:02 +1100819 struct r10conf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700820 int i, ret = 0;
821
NeilBrown34db0cd2011-10-11 16:50:01 +1100822 if ((bits & (1 << BDI_async_congested)) &&
823 conf->pending_count >= max_queued_requests)
824 return 1;
825
NeilBrown3fa841d2009-09-23 18:10:29 +1000826 if (mddev_congested(mddev, bits))
827 return 1;
NeilBrown0d129222006-10-03 01:15:54 -0700828 rcu_read_lock();
NeilBrown84707f32010-03-16 17:23:35 +1100829 for (i = 0; i < conf->raid_disks && ret == 0; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100830 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700831 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200832 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700833
834 ret |= bdi_congested(&q->backing_dev_info, bits);
835 }
836 }
837 rcu_read_unlock();
838 return ret;
839}
840
NeilBrowne879a872011-10-11 16:49:02 +1100841static void flush_pending_writes(struct r10conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800842{
843 /* Any writes that have been queued but are awaiting
844 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800845 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800846 spin_lock_irq(&conf->device_lock);
847
848 if (conf->pending_bio_list.head) {
849 struct bio *bio;
850 bio = bio_list_get(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +1100851 conf->pending_count = 0;
NeilBrowna35e63e2008-03-04 14:29:29 -0800852 spin_unlock_irq(&conf->device_lock);
853 /* flush any pending bitmap writes to disk
854 * before proceeding w/ I/O */
855 bitmap_unplug(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100856 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800857
858 while (bio) { /* submit pending writes */
859 struct bio *next = bio->bi_next;
860 bio->bi_next = NULL;
861 generic_make_request(bio);
862 bio = next;
863 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800864 } else
865 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800866}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100867
NeilBrown0a27ec92006-01-06 00:20:13 -0800868/* Barriers....
869 * Sometimes we need to suspend IO while we do something else,
870 * either some resync/recovery, or reconfigure the array.
871 * To do this we raise a 'barrier'.
872 * The 'barrier' is a counter that can be raised multiple times
873 * to count how many activities are happening which preclude
874 * normal IO.
875 * We can only raise the barrier if there is no pending IO.
876 * i.e. if nr_pending == 0.
877 * We choose only to raise the barrier if no-one is waiting for the
878 * barrier to go down. This means that as soon as an IO request
879 * is ready, no other operations which require a barrier will start
880 * until the IO request has had a chance.
881 *
882 * So: regular IO calls 'wait_barrier'. When that returns there
883 * is no backgroup IO happening, It must arrange to call
884 * allow_barrier when it has finished its IO.
885 * backgroup IO calls must call raise_barrier. Once that returns
886 * there is no normal IO happeing. It must arrange to call
887 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
NeilBrowne879a872011-10-11 16:49:02 +1100890static void raise_barrier(struct r10conf *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
NeilBrown6cce3b22006-01-06 00:20:16 -0800892 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
NeilBrown6cce3b22006-01-06 00:20:16 -0800895 /* Wait until no block IO is waiting (unless 'force') */
896 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrownc3b328a2011-04-18 18:25:43 +1000897 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800898
899 /* block any new IO from starting */
900 conf->barrier++;
901
NeilBrownc3b328a2011-04-18 18:25:43 +1000902 /* Now wait for all pending IO to complete */
NeilBrown0a27ec92006-01-06 00:20:13 -0800903 wait_event_lock_irq(conf->wait_barrier,
904 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
NeilBrownc3b328a2011-04-18 18:25:43 +1000905 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 spin_unlock_irq(&conf->resync_lock);
908}
909
NeilBrowne879a872011-10-11 16:49:02 +1100910static void lower_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800911{
912 unsigned long flags;
913 spin_lock_irqsave(&conf->resync_lock, flags);
914 conf->barrier--;
915 spin_unlock_irqrestore(&conf->resync_lock, flags);
916 wake_up(&conf->wait_barrier);
917}
918
NeilBrowne879a872011-10-11 16:49:02 +1100919static void wait_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800920{
921 spin_lock_irq(&conf->resync_lock);
922 if (conf->barrier) {
923 conf->nr_waiting++;
NeilBrownd6b42dc2012-03-19 12:46:38 +1100924 /* Wait for the barrier to drop.
925 * However if there are already pending
926 * requests (preventing the barrier from
927 * rising completely), and the
928 * pre-process bio queue isn't empty,
929 * then don't wait, as we need to empty
930 * that queue to get the nr_pending
931 * count down.
932 */
933 wait_event_lock_irq(conf->wait_barrier,
934 !conf->barrier ||
935 (conf->nr_pending &&
936 current->bio_list &&
937 !bio_list_empty(current->bio_list)),
NeilBrown0a27ec92006-01-06 00:20:13 -0800938 conf->resync_lock,
NeilBrownd6b42dc2012-03-19 12:46:38 +1100939 );
NeilBrown0a27ec92006-01-06 00:20:13 -0800940 conf->nr_waiting--;
941 }
942 conf->nr_pending++;
943 spin_unlock_irq(&conf->resync_lock);
944}
945
NeilBrowne879a872011-10-11 16:49:02 +1100946static void allow_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800947{
948 unsigned long flags;
949 spin_lock_irqsave(&conf->resync_lock, flags);
950 conf->nr_pending--;
951 spin_unlock_irqrestore(&conf->resync_lock, flags);
952 wake_up(&conf->wait_barrier);
953}
954
NeilBrowne879a872011-10-11 16:49:02 +1100955static void freeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -0800956{
957 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800958 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800959 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800960 * wait until nr_pending match nr_queued+1
961 * This is called in the context of one normal IO request
962 * that has failed. Thus any sync request that might be pending
963 * will be blocked by nr_pending, and we need to wait for
964 * pending IO requests to complete or be queued for re-try.
965 * Thus the number queued (nr_queued) plus this request (1)
966 * must match the number of pending IOs (nr_pending) before
967 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -0800968 */
969 spin_lock_irq(&conf->resync_lock);
970 conf->barrier++;
971 conf->nr_waiting++;
972 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -0800973 conf->nr_pending == conf->nr_queued+1,
NeilBrown4443ae12006-01-06 00:20:28 -0800974 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000975 flush_pending_writes(conf));
976
NeilBrown4443ae12006-01-06 00:20:28 -0800977 spin_unlock_irq(&conf->resync_lock);
978}
979
NeilBrowne879a872011-10-11 16:49:02 +1100980static void unfreeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -0800981{
982 /* reverse the effect of the freeze */
983 spin_lock_irq(&conf->resync_lock);
984 conf->barrier--;
985 conf->nr_waiting--;
986 wake_up(&conf->wait_barrier);
987 spin_unlock_irq(&conf->resync_lock);
988}
989
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -0700990static void make_request(struct mddev *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
NeilBrowne879a872011-10-11 16:49:02 +1100992 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100993 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 struct bio *read_bio;
995 int i;
996 int chunk_sects = conf->chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +0100997 const int rw = bio_data_dir(bio);
NeilBrown2c7d46e2010-08-18 16:16:05 +1000998 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
Tejun Heoe9c74692010-09-03 11:56:18 +0200999 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
NeilBrown6cce3b22006-01-06 00:20:16 -08001000 unsigned long flags;
NeilBrown3cb03002011-10-11 16:45:26 +11001001 struct md_rdev *blocked_rdev;
NeilBrownc3b328a2011-04-18 18:25:43 +10001002 int plugged;
NeilBrownd4432c22011-07-28 11:39:24 +10001003 int sectors_handled;
1004 int max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Tejun Heoe9c74692010-09-03 11:56:18 +02001006 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
1007 md_flush_request(mddev, bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001008 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07001009 }
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 /* If this request crosses a chunk boundary, we need to
1012 * split it. This will only happen for 1 PAGE (or less) requests.
1013 */
1014 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
1015 > chunk_sects &&
1016 conf->near_copies < conf->raid_disks)) {
1017 struct bio_pair *bp;
1018 /* Sanity check -- queue functions should prevent this happening */
1019 if (bio->bi_vcnt != 1 ||
1020 bio->bi_idx != 0)
1021 goto bad_map;
1022 /* This is a one page bio that upper layers
1023 * refuse to split for us, so we need to split it.
1024 */
Denis ChengRq6feef532008-10-09 08:57:05 +02001025 bp = bio_split(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
NeilBrown51e9ac72010-08-07 21:17:00 +10001027
1028 /* Each of these 'make_request' calls will call 'wait_barrier'.
1029 * If the first succeeds but the second blocks due to the resync
1030 * thread raising the barrier, we will deadlock because the
1031 * IO to the underlying device will be queued in generic_make_request
1032 * and will never complete, so will never reduce nr_pending.
1033 * So increment nr_waiting here so no new raise_barriers will
1034 * succeed, and so the second wait_barrier cannot block.
1035 */
1036 spin_lock_irq(&conf->resync_lock);
1037 conf->nr_waiting++;
1038 spin_unlock_irq(&conf->resync_lock);
1039
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001040 make_request(mddev, &bp->bio1);
1041 make_request(mddev, &bp->bio2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
NeilBrown51e9ac72010-08-07 21:17:00 +10001043 spin_lock_irq(&conf->resync_lock);
1044 conf->nr_waiting--;
1045 wake_up(&conf->wait_barrier);
1046 spin_unlock_irq(&conf->resync_lock);
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 bio_pair_release(bp);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001049 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 bad_map:
NeilBrown128595e2010-05-03 14:47:14 +10001051 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
1052 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
1054
NeilBrown6712ecf2007-09-27 12:47:43 +02001055 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001056 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058
NeilBrown3d310eb2005-06-21 17:17:26 -07001059 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -07001060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 /*
1062 * Register the new request and wait if the reconstruction
1063 * thread has put up a bar for new requests.
1064 * Continue immediately if no resync is active currently.
1065 */
NeilBrown0a27ec92006-01-06 00:20:13 -08001066 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1069
1070 r10_bio->master_bio = bio;
1071 r10_bio->sectors = bio->bi_size >> 9;
1072
1073 r10_bio->mddev = mddev;
1074 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b22006-01-06 00:20:16 -08001075 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
NeilBrown856e08e2011-07-28 11:39:23 +10001077 /* We might need to issue multiple reads to different
1078 * devices if there are bad blocks around, so we keep
1079 * track of the number of reads in bio->bi_phys_segments.
1080 * If this is 0, there is only one r10_bio and no locking
1081 * will be needed when the request completes. If it is
1082 * non-zero, then it is the number of not-completed requests.
1083 */
1084 bio->bi_phys_segments = 0;
1085 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
1086
Jens Axboea3623572005-11-01 09:26:16 +01001087 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 /*
1089 * read balancing logic:
1090 */
NeilBrown96c3fd12011-12-23 10:17:54 +11001091 struct md_rdev *rdev;
NeilBrown856e08e2011-07-28 11:39:23 +10001092 int slot;
1093
1094read_again:
NeilBrown96c3fd12011-12-23 10:17:54 +11001095 rdev = read_balance(conf, r10_bio, &max_sectors);
1096 if (!rdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 raid_end_bio_io(r10_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001098 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
NeilBrown96c3fd12011-12-23 10:17:54 +11001100 slot = r10_bio->read_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
NeilBrowna167f662010-10-26 18:31:13 +11001102 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrown856e08e2011-07-28 11:39:23 +10001103 md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
1104 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 r10_bio->devs[slot].bio = read_bio;
NeilBrownabbf0982011-12-23 10:17:54 +11001107 r10_bio->devs[slot].rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 read_bio->bi_sector = r10_bio->devs[slot].addr +
NeilBrown96c3fd12011-12-23 10:17:54 +11001110 rdev->data_offset;
1111 read_bio->bi_bdev = rdev->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 read_bio->bi_end_io = raid10_end_read_request;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001113 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 read_bio->bi_private = r10_bio;
1115
NeilBrown856e08e2011-07-28 11:39:23 +10001116 if (max_sectors < r10_bio->sectors) {
1117 /* Could not read all from this device, so we will
1118 * need another r10_bio.
1119 */
NeilBrown856e08e2011-07-28 11:39:23 +10001120 sectors_handled = (r10_bio->sectors + max_sectors
1121 - bio->bi_sector);
1122 r10_bio->sectors = max_sectors;
1123 spin_lock_irq(&conf->device_lock);
1124 if (bio->bi_phys_segments == 0)
1125 bio->bi_phys_segments = 2;
1126 else
1127 bio->bi_phys_segments++;
1128 spin_unlock(&conf->device_lock);
1129 /* Cannot call generic_make_request directly
1130 * as that will be queued in __generic_make_request
1131 * and subsequent mempool_alloc might block
1132 * waiting for it. so hand bio over to raid10d.
1133 */
1134 reschedule_retry(r10_bio);
1135
1136 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1137
1138 r10_bio->master_bio = bio;
1139 r10_bio->sectors = ((bio->bi_size >> 9)
1140 - sectors_handled);
1141 r10_bio->state = 0;
1142 r10_bio->mddev = mddev;
1143 r10_bio->sector = bio->bi_sector + sectors_handled;
1144 goto read_again;
1145 } else
1146 generic_make_request(read_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001147 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149
1150 /*
1151 * WRITE:
1152 */
NeilBrown34db0cd2011-10-11 16:50:01 +11001153 if (conf->pending_count >= max_queued_requests) {
1154 md_wakeup_thread(mddev->thread);
1155 wait_event(conf->wait_barrier,
1156 conf->pending_count < max_queued_requests);
1157 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001158 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 * inc refcount on their rdev. Record them by setting
1160 * bios[x] to bio
NeilBrownd4432c22011-07-28 11:39:24 +10001161 * If there are known/acknowledged bad blocks on any device
1162 * on which we have seen a write error, we want to avoid
1163 * writing to those blocks. This potentially requires several
1164 * writes to write around the bad blocks. Each set of writes
1165 * gets its own r10_bio with a set of bios attached. The number
1166 * of r10_bios is recored in bio->bi_phys_segments just as with
1167 * the read case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001169 plugged = mddev_check_plugged(mddev);
1170
NeilBrown69335ef2011-12-23 10:17:54 +11001171 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 raid10_find_phys(conf, r10_bio);
NeilBrownd4432c22011-07-28 11:39:24 +10001173retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -07001174 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 rcu_read_lock();
NeilBrownd4432c22011-07-28 11:39:24 +10001176 max_sectors = r10_bio->sectors;
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 for (i = 0; i < conf->copies; i++) {
1179 int d = r10_bio->devs[i].devnum;
NeilBrown3cb03002011-10-11 16:45:26 +11001180 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown475b0322011-12-23 10:17:55 +11001181 struct md_rdev *rrdev = rcu_dereference(
1182 conf->mirrors[d].replacement);
NeilBrown4ca40c22011-12-23 10:17:55 +11001183 if (rdev == rrdev)
1184 rrdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001185 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1186 atomic_inc(&rdev->nr_pending);
1187 blocked_rdev = rdev;
1188 break;
1189 }
NeilBrown475b0322011-12-23 10:17:55 +11001190 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1191 atomic_inc(&rrdev->nr_pending);
1192 blocked_rdev = rrdev;
1193 break;
1194 }
NeilBrownf3921712012-11-22 14:42:49 +11001195 if (rdev && (test_bit(Faulty, &rdev->flags)
1196 || test_bit(Unmerged, &rdev->flags)))
1197 rdev = NULL;
NeilBrown050b6612012-03-19 12:46:39 +11001198 if (rrdev && (test_bit(Faulty, &rrdev->flags)
1199 || test_bit(Unmerged, &rrdev->flags)))
NeilBrown475b0322011-12-23 10:17:55 +11001200 rrdev = NULL;
1201
NeilBrownd4432c22011-07-28 11:39:24 +10001202 r10_bio->devs[i].bio = NULL;
NeilBrown475b0322011-12-23 10:17:55 +11001203 r10_bio->devs[i].repl_bio = NULL;
NeilBrownf3921712012-11-22 14:42:49 +11001204
1205 if (!rdev && !rrdev) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001206 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrownd4432c22011-07-28 11:39:24 +10001207 continue;
NeilBrown6cce3b22006-01-06 00:20:16 -08001208 }
NeilBrownf3921712012-11-22 14:42:49 +11001209 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
NeilBrownd4432c22011-07-28 11:39:24 +10001210 sector_t first_bad;
1211 sector_t dev_sector = r10_bio->devs[i].addr;
1212 int bad_sectors;
1213 int is_bad;
1214
1215 is_bad = is_badblock(rdev, dev_sector,
1216 max_sectors,
1217 &first_bad, &bad_sectors);
1218 if (is_bad < 0) {
1219 /* Mustn't write here until the bad block
1220 * is acknowledged
1221 */
1222 atomic_inc(&rdev->nr_pending);
1223 set_bit(BlockedBadBlocks, &rdev->flags);
1224 blocked_rdev = rdev;
1225 break;
1226 }
1227 if (is_bad && first_bad <= dev_sector) {
1228 /* Cannot write here at all */
1229 bad_sectors -= (dev_sector - first_bad);
1230 if (bad_sectors < max_sectors)
1231 /* Mustn't write more than bad_sectors
1232 * to other devices yet
1233 */
1234 max_sectors = bad_sectors;
1235 /* We don't set R10BIO_Degraded as that
1236 * only applies if the disk is missing,
1237 * so it might be re-added, and we want to
1238 * know to recover this chunk.
1239 * In this case the device is here, and the
1240 * fact that this chunk is not in-sync is
1241 * recorded in the bad block log.
1242 */
1243 continue;
1244 }
1245 if (is_bad) {
1246 int good_sectors = first_bad - dev_sector;
1247 if (good_sectors < max_sectors)
1248 max_sectors = good_sectors;
1249 }
1250 }
NeilBrownf3921712012-11-22 14:42:49 +11001251 if (rdev) {
1252 r10_bio->devs[i].bio = bio;
1253 atomic_inc(&rdev->nr_pending);
1254 }
NeilBrown475b0322011-12-23 10:17:55 +11001255 if (rrdev) {
1256 r10_bio->devs[i].repl_bio = bio;
1257 atomic_inc(&rrdev->nr_pending);
1258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260 rcu_read_unlock();
1261
Dan Williams6bfe0b42008-04-30 00:52:32 -07001262 if (unlikely(blocked_rdev)) {
1263 /* Have to wait for this device to get unblocked, then retry */
1264 int j;
1265 int d;
1266
NeilBrown475b0322011-12-23 10:17:55 +11001267 for (j = 0; j < i; j++) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07001268 if (r10_bio->devs[j].bio) {
1269 d = r10_bio->devs[j].devnum;
1270 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1271 }
NeilBrown475b0322011-12-23 10:17:55 +11001272 if (r10_bio->devs[j].repl_bio) {
NeilBrown4ca40c22011-12-23 10:17:55 +11001273 struct md_rdev *rdev;
NeilBrown475b0322011-12-23 10:17:55 +11001274 d = r10_bio->devs[j].devnum;
NeilBrown4ca40c22011-12-23 10:17:55 +11001275 rdev = conf->mirrors[d].replacement;
1276 if (!rdev) {
1277 /* Race with remove_disk */
1278 smp_mb();
1279 rdev = conf->mirrors[d].rdev;
1280 }
1281 rdev_dec_pending(rdev, mddev);
NeilBrown475b0322011-12-23 10:17:55 +11001282 }
1283 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001284 allow_barrier(conf);
1285 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1286 wait_barrier(conf);
1287 goto retry_write;
1288 }
1289
NeilBrownd4432c22011-07-28 11:39:24 +10001290 if (max_sectors < r10_bio->sectors) {
1291 /* We are splitting this into multiple parts, so
1292 * we need to prepare for allocating another r10_bio.
1293 */
1294 r10_bio->sectors = max_sectors;
1295 spin_lock_irq(&conf->device_lock);
1296 if (bio->bi_phys_segments == 0)
1297 bio->bi_phys_segments = 2;
1298 else
1299 bio->bi_phys_segments++;
1300 spin_unlock_irq(&conf->device_lock);
1301 }
1302 sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
1303
NeilBrown4e780642010-10-19 12:54:01 +11001304 atomic_set(&r10_bio->remaining, 1);
NeilBrownd4432c22011-07-28 11:39:24 +10001305 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07001306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 for (i = 0; i < conf->copies; i++) {
1308 struct bio *mbio;
1309 int d = r10_bio->devs[i].devnum;
NeilBrownf3921712012-11-22 14:42:49 +11001310 if (r10_bio->devs[i].bio) {
1311 struct md_rdev *rdev = conf->mirrors[d].rdev;
1312 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1313 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1314 max_sectors);
1315 r10_bio->devs[i].bio = mbio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
NeilBrownf3921712012-11-22 14:42:49 +11001317 mbio->bi_sector = (r10_bio->devs[i].addr+
1318 rdev->data_offset);
1319 mbio->bi_bdev = rdev->bdev;
1320 mbio->bi_end_io = raid10_end_write_request;
1321 mbio->bi_rw = WRITE | do_sync | do_fua;
1322 mbio->bi_private = r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
NeilBrownf3921712012-11-22 14:42:49 +11001324 atomic_inc(&r10_bio->remaining);
1325 spin_lock_irqsave(&conf->device_lock, flags);
1326 bio_list_add(&conf->pending_bio_list, mbio);
1327 conf->pending_count++;
1328 spin_unlock_irqrestore(&conf->device_lock, flags);
1329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
NeilBrownf3921712012-11-22 14:42:49 +11001331 if (r10_bio->devs[i].repl_bio) {
1332 struct md_rdev *rdev = conf->mirrors[d].replacement;
1333 if (rdev == NULL) {
1334 /* Replacement just got moved to main 'rdev' */
1335 smp_mb();
1336 rdev = conf->mirrors[d].rdev;
1337 }
1338 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1339 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1340 max_sectors);
1341 r10_bio->devs[i].repl_bio = mbio;
NeilBrown475b0322011-12-23 10:17:55 +11001342
NeilBrownf3921712012-11-22 14:42:49 +11001343 mbio->bi_sector = (r10_bio->devs[i].addr+
1344 rdev->data_offset);
1345 mbio->bi_bdev = rdev->bdev;
1346 mbio->bi_end_io = raid10_end_write_request;
1347 mbio->bi_rw = WRITE | do_sync | do_fua;
1348 mbio->bi_private = r10_bio;
NeilBrown475b0322011-12-23 10:17:55 +11001349
NeilBrownf3921712012-11-22 14:42:49 +11001350 atomic_inc(&r10_bio->remaining);
1351 spin_lock_irqsave(&conf->device_lock, flags);
1352 bio_list_add(&conf->pending_bio_list, mbio);
1353 conf->pending_count++;
1354 spin_unlock_irqrestore(&conf->device_lock, flags);
1355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
1357
NeilBrown079fa162011-09-10 17:21:23 +10001358 /* Don't remove the bias on 'remaining' (one_write_done) until
1359 * after checking if we need to go around again.
1360 */
NeilBrowna35e63e2008-03-04 14:29:29 -08001361
NeilBrownd4432c22011-07-28 11:39:24 +10001362 if (sectors_handled < (bio->bi_size >> 9)) {
NeilBrown079fa162011-09-10 17:21:23 +10001363 one_write_done(r10_bio);
NeilBrown5e570282011-07-28 11:39:25 +10001364 /* We need another r10_bio. It has already been counted
NeilBrownd4432c22011-07-28 11:39:24 +10001365 * in bio->bi_phys_segments.
1366 */
1367 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1368
1369 r10_bio->master_bio = bio;
1370 r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1371
1372 r10_bio->mddev = mddev;
1373 r10_bio->sector = bio->bi_sector + sectors_handled;
1374 r10_bio->state = 0;
1375 goto retry_write;
1376 }
NeilBrown079fa162011-09-10 17:21:23 +10001377 one_write_done(r10_bio);
1378
1379 /* In case raid10d snuck in to freeze_array */
1380 wake_up(&conf->wait_barrier);
NeilBrownd4432c22011-07-28 11:39:24 +10001381
NeilBrownc3b328a2011-04-18 18:25:43 +10001382 if (do_sync || !mddev->bitmap || !plugged)
Lars Ellenberge3881a62007-01-10 23:15:37 -08001383 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
NeilBrownfd01b882011-10-11 16:47:53 +11001386static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
NeilBrowne879a872011-10-11 16:49:02 +11001388 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 int i;
1390
1391 if (conf->near_copies < conf->raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +10001392 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 if (conf->near_copies > 1)
1394 seq_printf(seq, " %d near-copies", conf->near_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001395 if (conf->far_copies > 1) {
1396 if (conf->far_offset)
1397 seq_printf(seq, " %d offset-copies", conf->far_copies);
1398 else
1399 seq_printf(seq, " %d far-copies", conf->far_copies);
1400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown76186dd2006-10-03 01:15:48 -07001402 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 for (i = 0; i < conf->raid_disks; i++)
1404 seq_printf(seq, "%s",
1405 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001406 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 seq_printf(seq, "]");
1408}
1409
NeilBrown700c7212011-07-27 11:00:36 +10001410/* check if there are enough drives for
1411 * every block to appear on atleast one.
1412 * Don't consider the device numbered 'ignore'
1413 * as we might be about to remove it.
1414 */
NeilBrowne879a872011-10-11 16:49:02 +11001415static int enough(struct r10conf *conf, int ignore)
NeilBrown700c7212011-07-27 11:00:36 +10001416{
1417 int first = 0;
1418
1419 do {
1420 int n = conf->copies;
1421 int cnt = 0;
1422 while (n--) {
1423 if (conf->mirrors[first].rdev &&
1424 first != ignore)
1425 cnt++;
1426 first = (first+1) % conf->raid_disks;
1427 }
1428 if (cnt == 0)
1429 return 0;
1430 } while (first != 0);
1431 return 1;
1432}
1433
NeilBrownfd01b882011-10-11 16:47:53 +11001434static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
1436 char b[BDEVNAME_SIZE];
NeilBrowne879a872011-10-11 16:49:02 +11001437 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 /*
1440 * If it is not operational, then we have already marked it as dead
1441 * else if it is the last working disks, ignore the error, let the
1442 * next level up know.
1443 * else mark the drive as failed
1444 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001445 if (test_bit(In_sync, &rdev->flags)
NeilBrown700c7212011-07-27 11:00:36 +10001446 && !enough(conf, rdev->raid_disk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 /*
1448 * Don't fail the drive, just return an IO error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 */
1450 return;
NeilBrownc04be0a2006-10-03 01:15:53 -07001451 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1452 unsigned long flags;
1453 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001455 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 /*
1457 * if recovery is running, make sure it aborts.
1458 */
NeilBrowndfc70642008-05-23 13:04:39 -07001459 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
NeilBrownde393cd2011-07-28 11:31:48 +10001461 set_bit(Blocked, &rdev->flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001462 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -07001463 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Joe Perches067032b2011-01-14 09:14:33 +11001464 printk(KERN_ALERT
1465 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1466 "md/raid10:%s: Operation continuing on %d devices.\n",
NeilBrown128595e2010-05-03 14:47:14 +10001467 mdname(mddev), bdevname(rdev->bdev, b),
1468 mdname(mddev), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
NeilBrowne879a872011-10-11 16:49:02 +11001471static void print_conf(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
1473 int i;
NeilBrown0f6d02d2011-10-11 16:48:46 +11001474 struct mirror_info *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
NeilBrown128595e2010-05-03 14:47:14 +10001476 printk(KERN_DEBUG "RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (!conf) {
NeilBrown128595e2010-05-03 14:47:14 +10001478 printk(KERN_DEBUG "(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return;
1480 }
NeilBrown128595e2010-05-03 14:47:14 +10001481 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 conf->raid_disks);
1483
1484 for (i = 0; i < conf->raid_disks; i++) {
1485 char b[BDEVNAME_SIZE];
1486 tmp = conf->mirrors + i;
1487 if (tmp->rdev)
NeilBrown128595e2010-05-03 14:47:14 +10001488 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001489 i, !test_bit(In_sync, &tmp->rdev->flags),
1490 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 bdevname(tmp->rdev->bdev,b));
1492 }
1493}
1494
NeilBrowne879a872011-10-11 16:49:02 +11001495static void close_sync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
NeilBrown0a27ec92006-01-06 00:20:13 -08001497 wait_barrier(conf);
1498 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 mempool_destroy(conf->r10buf_pool);
1501 conf->r10buf_pool = NULL;
1502}
1503
NeilBrownfd01b882011-10-11 16:47:53 +11001504static int raid10_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
1506 int i;
NeilBrowne879a872011-10-11 16:49:02 +11001507 struct r10conf *conf = mddev->private;
NeilBrown0f6d02d2011-10-11 16:48:46 +11001508 struct mirror_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10001509 int count = 0;
1510 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 /*
1513 * Find all non-in_sync disks within the RAID10 configuration
1514 * and mark them in_sync
1515 */
1516 for (i = 0; i < conf->raid_disks; i++) {
1517 tmp = conf->mirrors + i;
NeilBrown4ca40c22011-12-23 10:17:55 +11001518 if (tmp->replacement
1519 && tmp->replacement->recovery_offset == MaxSector
1520 && !test_bit(Faulty, &tmp->replacement->flags)
1521 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1522 /* Replacement has just become active */
1523 if (!tmp->rdev
1524 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1525 count++;
1526 if (tmp->rdev) {
1527 /* Replaced device not technically faulty,
1528 * but we need to be sure it gets removed
1529 * and never re-added.
1530 */
1531 set_bit(Faulty, &tmp->rdev->flags);
1532 sysfs_notify_dirent_safe(
1533 tmp->rdev->sysfs_state);
1534 }
1535 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1536 } else if (tmp->rdev
1537 && !test_bit(Faulty, &tmp->rdev->flags)
1538 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001539 count++;
Adrian Drzewieckie6ffbcb2010-08-18 11:49:02 +10001540 sysfs_notify_dirent(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
1542 }
NeilBrown6b965622010-08-18 11:56:59 +10001543 spin_lock_irqsave(&conf->device_lock, flags);
1544 mddev->degraded -= count;
1545 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001548 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
1551
NeilBrownfd01b882011-10-11 16:47:53 +11001552static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
NeilBrowne879a872011-10-11 16:49:02 +11001554 struct r10conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001555 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 int mirror;
Neil Brown6c2fce22008-06-28 08:31:31 +10001557 int first = 0;
NeilBrown84707f32010-03-16 17:23:35 +11001558 int last = conf->raid_disks - 1;
NeilBrown050b6612012-03-19 12:46:39 +11001559 struct request_queue *q = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 if (mddev->recovery_cp < MaxSector)
1562 /* only hot-add to in-sync arrays, as recovery is
1563 * very different from resync
1564 */
Neil Brown199050e2008-06-28 08:31:33 +10001565 return -EBUSY;
NeilBrowndc10c642012-03-19 12:46:37 +11001566 if (rdev->saved_raid_disk < 0 && !enough(conf, -1))
Neil Brown199050e2008-06-28 08:31:33 +10001567 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
NeilBrowna53a6c82008-11-06 17:28:20 +11001569 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001570 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
NeilBrown050b6612012-03-19 12:46:39 +11001572 if (q->merge_bvec_fn) {
1573 set_bit(Unmerged, &rdev->flags);
1574 mddev->merge_check_needed = 1;
1575 }
1576
Namhyung Kim2c4193d2011-07-18 17:38:43 +10001577 if (rdev->saved_raid_disk >= first &&
NeilBrown6cce3b22006-01-06 00:20:16 -08001578 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1579 mirror = rdev->saved_raid_disk;
1580 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001581 mirror = first;
NeilBrown2bb77732011-07-27 11:00:36 +10001582 for ( ; mirror <= last ; mirror++) {
NeilBrown0f6d02d2011-10-11 16:48:46 +11001583 struct mirror_info *p = &conf->mirrors[mirror];
NeilBrown2bb77732011-07-27 11:00:36 +10001584 if (p->recovery_disabled == mddev->recovery_disabled)
1585 continue;
NeilBrownb7044d42011-12-23 10:17:56 +11001586 if (p->rdev) {
1587 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1588 p->replacement != NULL)
1589 continue;
1590 clear_bit(In_sync, &rdev->flags);
1591 set_bit(Replacement, &rdev->flags);
1592 rdev->raid_disk = mirror;
1593 err = 0;
1594 disk_stack_limits(mddev->gendisk, rdev->bdev,
1595 rdev->data_offset << 9);
NeilBrownb7044d42011-12-23 10:17:56 +11001596 conf->fullsync = 1;
1597 rcu_assign_pointer(p->replacement, rdev);
1598 break;
1599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
NeilBrown2bb77732011-07-27 11:00:36 +10001601 disk_stack_limits(mddev->gendisk, rdev->bdev,
1602 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
NeilBrown2bb77732011-07-27 11:00:36 +10001604 p->head_position = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11001605 p->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown2bb77732011-07-27 11:00:36 +10001606 rdev->raid_disk = mirror;
1607 err = 0;
1608 if (rdev->saved_raid_disk != mirror)
1609 conf->fullsync = 1;
1610 rcu_assign_pointer(p->rdev, rdev);
1611 break;
1612 }
NeilBrown050b6612012-03-19 12:46:39 +11001613 if (err == 0 && test_bit(Unmerged, &rdev->flags)) {
1614 /* Some requests might not have seen this new
1615 * merge_bvec_fn. We must wait for them to complete
1616 * before merging the device fully.
1617 * First we make sure any code which has tested
1618 * our function has submitted the request, then
1619 * we wait for all outstanding requests to complete.
1620 */
1621 synchronize_sched();
1622 raise_barrier(conf, 0);
1623 lower_barrier(conf);
1624 clear_bit(Unmerged, &rdev->flags);
1625 }
Andre Nollac5e7112009-08-03 10:59:47 +10001626 md_integrity_add_rdev(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001628 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
NeilBrownb8321b62011-12-23 10:17:51 +11001631static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
NeilBrowne879a872011-10-11 16:49:02 +11001633 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11001635 int number = rdev->raid_disk;
NeilBrownc8ab9032011-12-23 10:17:54 +11001636 struct md_rdev **rdevp;
1637 struct mirror_info *p = conf->mirrors + number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 print_conf(conf);
NeilBrownc8ab9032011-12-23 10:17:54 +11001640 if (rdev == p->rdev)
1641 rdevp = &p->rdev;
1642 else if (rdev == p->replacement)
1643 rdevp = &p->replacement;
1644 else
1645 return 0;
1646
1647 if (test_bit(In_sync, &rdev->flags) ||
1648 atomic_read(&rdev->nr_pending)) {
1649 err = -EBUSY;
1650 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
NeilBrownc8ab9032011-12-23 10:17:54 +11001652 /* Only remove faulty devices if recovery
1653 * is not possible.
1654 */
1655 if (!test_bit(Faulty, &rdev->flags) &&
1656 mddev->recovery_disabled != p->recovery_disabled &&
NeilBrown4ca40c22011-12-23 10:17:55 +11001657 (!p->replacement || p->replacement == rdev) &&
NeilBrownc8ab9032011-12-23 10:17:54 +11001658 enough(conf, -1)) {
1659 err = -EBUSY;
1660 goto abort;
1661 }
1662 *rdevp = NULL;
1663 synchronize_rcu();
1664 if (atomic_read(&rdev->nr_pending)) {
1665 /* lost the race, try later */
1666 err = -EBUSY;
1667 *rdevp = rdev;
1668 goto abort;
NeilBrown4ca40c22011-12-23 10:17:55 +11001669 } else if (p->replacement) {
1670 /* We must have just cleared 'rdev' */
1671 p->rdev = p->replacement;
1672 clear_bit(Replacement, &p->replacement->flags);
1673 smp_mb(); /* Make sure other CPUs may see both as identical
1674 * but will never see neither -- if they are careful.
1675 */
1676 p->replacement = NULL;
1677 clear_bit(WantReplacement, &rdev->flags);
1678 } else
1679 /* We might have just remove the Replacement as faulty
1680 * Clear the flag just in case
1681 */
1682 clear_bit(WantReplacement, &rdev->flags);
1683
NeilBrownc8ab9032011-12-23 10:17:54 +11001684 err = md_integrity_register(mddev);
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686abort:
1687
1688 print_conf(conf);
1689 return err;
1690}
1691
1692
NeilBrown6712ecf2007-09-27 12:47:43 +02001693static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001695 struct r10bio *r10_bio = bio->bi_private;
NeilBrowne879a872011-10-11 16:49:02 +11001696 struct r10conf *conf = r10_bio->mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001697 int d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
NeilBrown69335ef2011-12-23 10:17:54 +11001699 d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001700
1701 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1702 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrowne684e412011-07-28 11:39:25 +10001703 else
1704 /* The write handler will notice the lack of
1705 * R10BIO_Uptodate and record any errors etc
1706 */
NeilBrown4dbcdc72006-01-06 00:20:52 -08001707 atomic_add(r10_bio->sectors,
1708 &conf->mirrors[d].rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710 /* for reconstruct, we always reschedule after a read.
1711 * for resync, only after all reads
1712 */
NeilBrown73d5c382009-02-25 13:18:47 +11001713 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1715 atomic_dec_and_test(&r10_bio->remaining)) {
1716 /* we have read all the blocks,
1717 * do the comparison in process context in raid10d
1718 */
1719 reschedule_retry(r10_bio);
1720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721}
1722
NeilBrown9f2c9d12011-10-11 16:48:43 +11001723static void end_sync_request(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001724{
NeilBrownfd01b882011-10-11 16:47:53 +11001725 struct mddev *mddev = r10_bio->mddev;
NeilBrown5e570282011-07-28 11:39:25 +10001726
1727 while (atomic_dec_and_test(&r10_bio->remaining)) {
1728 if (r10_bio->master_bio == NULL) {
1729 /* the primary of several recovery bios */
1730 sector_t s = r10_bio->sectors;
1731 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1732 test_bit(R10BIO_WriteError, &r10_bio->state))
1733 reschedule_retry(r10_bio);
1734 else
1735 put_buf(r10_bio);
1736 md_done_sync(mddev, s, 1);
1737 break;
1738 } else {
NeilBrown9f2c9d12011-10-11 16:48:43 +11001739 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
NeilBrown5e570282011-07-28 11:39:25 +10001740 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1741 test_bit(R10BIO_WriteError, &r10_bio->state))
1742 reschedule_retry(r10_bio);
1743 else
1744 put_buf(r10_bio);
1745 r10_bio = r10_bio2;
1746 }
1747 }
1748}
1749
NeilBrown6712ecf2007-09-27 12:47:43 +02001750static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
1752 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +11001753 struct r10bio *r10_bio = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11001754 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001755 struct r10conf *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001756 int d;
NeilBrown749c55e2011-07-28 11:39:24 +10001757 sector_t first_bad;
1758 int bad_sectors;
1759 int slot;
NeilBrown9ad1aef2011-12-23 10:17:55 +11001760 int repl;
NeilBrown4ca40c22011-12-23 10:17:55 +11001761 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
NeilBrown9ad1aef2011-12-23 10:17:55 +11001763 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1764 if (repl)
1765 rdev = conf->mirrors[d].replacement;
NeilBrown547414d2012-03-13 11:21:20 +11001766 else
NeilBrown9ad1aef2011-12-23 10:17:55 +11001767 rdev = conf->mirrors[d].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
NeilBrown1a0b7cd2011-07-28 11:39:25 +10001769 if (!uptodate) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11001770 if (repl)
1771 md_error(mddev, rdev);
1772 else {
1773 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11001774 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1775 set_bit(MD_RECOVERY_NEEDED,
1776 &rdev->mddev->recovery);
NeilBrown9ad1aef2011-12-23 10:17:55 +11001777 set_bit(R10BIO_WriteError, &r10_bio->state);
1778 }
1779 } else if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +10001780 r10_bio->devs[slot].addr,
1781 r10_bio->sectors,
1782 &first_bad, &bad_sectors))
1783 set_bit(R10BIO_MadeGood, &r10_bio->state);
NeilBrowndfc70642008-05-23 13:04:39 -07001784
NeilBrown9ad1aef2011-12-23 10:17:55 +11001785 rdev_dec_pending(rdev, mddev);
NeilBrown5e570282011-07-28 11:39:25 +10001786
1787 end_sync_request(r10_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
1790/*
1791 * Note: sync and recover and handled very differently for raid10
1792 * This code is for resync.
1793 * For resync, we read through virtual addresses and read all blocks.
1794 * If there is any error, we schedule a write. The lowest numbered
1795 * drive is authoritative.
1796 * However requests come for physical address, so we need to map.
1797 * For every physical address there are raid_disks/copies virtual addresses,
1798 * which is always are least one, but is not necessarly an integer.
1799 * This means that a physical address can span multiple chunks, so we may
1800 * have to submit multiple io requests for a single sync request.
1801 */
1802/*
1803 * We check if all blocks are in-sync and only write to blocks that
1804 * aren't in sync
1805 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11001806static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
NeilBrowne879a872011-10-11 16:49:02 +11001808 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 int i, first;
1810 struct bio *tbio, *fbio;
majianpengf4380a92012-04-12 16:04:47 +10001811 int vcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
1813 atomic_set(&r10_bio->remaining, 1);
1814
1815 /* find the first device with a block */
1816 for (i=0; i<conf->copies; i++)
1817 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1818 break;
1819
1820 if (i == conf->copies)
1821 goto done;
1822
1823 first = i;
1824 fbio = r10_bio->devs[i].bio;
1825
majianpengf4380a92012-04-12 16:04:47 +10001826 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001828 for (i=0 ; i < conf->copies ; i++) {
1829 int j, d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001832
1833 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001835 if (i == first)
1836 continue;
1837 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1838 /* We know that the bi_io_vec layout is the same for
1839 * both 'first' and 'i', so we just compare them.
1840 * All vec entries are PAGE_SIZE;
1841 */
1842 for (j = 0; j < vcnt; j++)
1843 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1844 page_address(tbio->bi_io_vec[j].bv_page),
NeilBrown5020ad72012-04-02 01:39:05 +10001845 fbio->bi_io_vec[j].bv_len))
NeilBrown0eb3ff12006-01-06 00:20:29 -08001846 break;
1847 if (j == vcnt)
1848 continue;
1849 mddev->resync_mismatches += r10_bio->sectors;
NeilBrownf84ee362011-07-28 11:39:25 +10001850 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1851 /* Don't fix anything. */
1852 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001853 }
NeilBrownf84ee362011-07-28 11:39:25 +10001854 /* Ok, we need to write this bio, either to correct an
1855 * inconsistency or to correct an unreadable block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 * First we need to fixup bv_offset, bv_len and
1857 * bi_vecs, as the read request might have corrupted these
1858 */
1859 tbio->bi_vcnt = vcnt;
1860 tbio->bi_size = r10_bio->sectors << 9;
1861 tbio->bi_idx = 0;
1862 tbio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1864 tbio->bi_flags |= 1 << BIO_UPTODATE;
1865 tbio->bi_next = NULL;
1866 tbio->bi_rw = WRITE;
1867 tbio->bi_private = r10_bio;
1868 tbio->bi_sector = r10_bio->devs[i].addr;
1869
1870 for (j=0; j < vcnt ; j++) {
1871 tbio->bi_io_vec[j].bv_offset = 0;
1872 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1873
1874 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1875 page_address(fbio->bi_io_vec[j].bv_page),
1876 PAGE_SIZE);
1877 }
1878 tbio->bi_end_io = end_sync_write;
1879
1880 d = r10_bio->devs[i].devnum;
1881 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1882 atomic_inc(&r10_bio->remaining);
1883 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1884
1885 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1886 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1887 generic_make_request(tbio);
1888 }
1889
NeilBrown9ad1aef2011-12-23 10:17:55 +11001890 /* Now write out to any replacement devices
1891 * that are active
1892 */
1893 for (i = 0; i < conf->copies; i++) {
1894 int j, d;
NeilBrown9ad1aef2011-12-23 10:17:55 +11001895
1896 tbio = r10_bio->devs[i].repl_bio;
1897 if (!tbio || !tbio->bi_end_io)
1898 continue;
1899 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
1900 && r10_bio->devs[i].bio != fbio)
1901 for (j = 0; j < vcnt; j++)
1902 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1903 page_address(fbio->bi_io_vec[j].bv_page),
1904 PAGE_SIZE);
1905 d = r10_bio->devs[i].devnum;
1906 atomic_inc(&r10_bio->remaining);
1907 md_sync_acct(conf->mirrors[d].replacement->bdev,
1908 tbio->bi_size >> 9);
1909 generic_make_request(tbio);
1910 }
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912done:
1913 if (atomic_dec_and_test(&r10_bio->remaining)) {
1914 md_done_sync(mddev, r10_bio->sectors, 1);
1915 put_buf(r10_bio);
1916 }
1917}
1918
1919/*
1920 * Now for the recovery code.
1921 * Recovery happens across physical sectors.
1922 * We recover all non-is_sync drives by finding the virtual address of
1923 * each, and then choose a working drive that also has that virt address.
1924 * There is a separate r10_bio for each non-in_sync drive.
1925 * Only the first two slots are in use. The first for reading,
1926 * The second for writing.
1927 *
1928 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11001929static void fix_recovery_read_error(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001930{
1931 /* We got a read error during recovery.
1932 * We repeat the read in smaller page-sized sections.
1933 * If a read succeeds, write it to the new device or record
1934 * a bad block if we cannot.
1935 * If a read fails, record a bad block on both old and
1936 * new devices.
1937 */
NeilBrownfd01b882011-10-11 16:47:53 +11001938 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001939 struct r10conf *conf = mddev->private;
NeilBrown5e570282011-07-28 11:39:25 +10001940 struct bio *bio = r10_bio->devs[0].bio;
1941 sector_t sect = 0;
1942 int sectors = r10_bio->sectors;
1943 int idx = 0;
1944 int dr = r10_bio->devs[0].devnum;
1945 int dw = r10_bio->devs[1].devnum;
1946
1947 while (sectors) {
1948 int s = sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11001949 struct md_rdev *rdev;
NeilBrown5e570282011-07-28 11:39:25 +10001950 sector_t addr;
1951 int ok;
1952
1953 if (s > (PAGE_SIZE>>9))
1954 s = PAGE_SIZE >> 9;
1955
1956 rdev = conf->mirrors[dr].rdev;
1957 addr = r10_bio->devs[0].addr + sect,
1958 ok = sync_page_io(rdev,
1959 addr,
1960 s << 9,
1961 bio->bi_io_vec[idx].bv_page,
1962 READ, false);
1963 if (ok) {
1964 rdev = conf->mirrors[dw].rdev;
1965 addr = r10_bio->devs[1].addr + sect;
1966 ok = sync_page_io(rdev,
1967 addr,
1968 s << 9,
1969 bio->bi_io_vec[idx].bv_page,
1970 WRITE, false);
NeilBrownb7044d42011-12-23 10:17:56 +11001971 if (!ok) {
NeilBrown5e570282011-07-28 11:39:25 +10001972 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11001973 if (!test_and_set_bit(WantReplacement,
1974 &rdev->flags))
1975 set_bit(MD_RECOVERY_NEEDED,
1976 &rdev->mddev->recovery);
1977 }
NeilBrown5e570282011-07-28 11:39:25 +10001978 }
1979 if (!ok) {
1980 /* We don't worry if we cannot set a bad block -
1981 * it really is bad so there is no loss in not
1982 * recording it yet
1983 */
1984 rdev_set_badblocks(rdev, addr, s, 0);
1985
1986 if (rdev != conf->mirrors[dw].rdev) {
1987 /* need bad block on destination too */
NeilBrown3cb03002011-10-11 16:45:26 +11001988 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
NeilBrown5e570282011-07-28 11:39:25 +10001989 addr = r10_bio->devs[1].addr + sect;
1990 ok = rdev_set_badblocks(rdev2, addr, s, 0);
1991 if (!ok) {
1992 /* just abort the recovery */
1993 printk(KERN_NOTICE
1994 "md/raid10:%s: recovery aborted"
1995 " due to read error\n",
1996 mdname(mddev));
1997
1998 conf->mirrors[dw].recovery_disabled
1999 = mddev->recovery_disabled;
2000 set_bit(MD_RECOVERY_INTR,
2001 &mddev->recovery);
2002 break;
2003 }
2004 }
2005 }
2006
2007 sectors -= s;
2008 sect += s;
2009 idx++;
2010 }
2011}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
NeilBrown9f2c9d12011-10-11 16:48:43 +11002013static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
NeilBrowne879a872011-10-11 16:49:02 +11002015 struct r10conf *conf = mddev->private;
Namhyung Kimc65060a2011-07-18 17:38:49 +10002016 int d;
NeilBrown24afd802011-12-23 10:17:55 +11002017 struct bio *wbio, *wbio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
NeilBrown5e570282011-07-28 11:39:25 +10002019 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2020 fix_recovery_read_error(r10_bio);
2021 end_sync_request(r10_bio);
2022 return;
2023 }
2024
Namhyung Kimc65060a2011-07-18 17:38:49 +10002025 /*
2026 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 * and submit the write request
2028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 d = r10_bio->devs[1].devnum;
NeilBrown24afd802011-12-23 10:17:55 +11002030 wbio = r10_bio->devs[1].bio;
2031 wbio2 = r10_bio->devs[1].repl_bio;
2032 if (wbio->bi_end_io) {
2033 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2034 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
2035 generic_make_request(wbio);
2036 }
2037 if (wbio2 && wbio2->bi_end_io) {
2038 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2039 md_sync_acct(conf->mirrors[d].replacement->bdev,
2040 wbio2->bi_size >> 9);
2041 generic_make_request(wbio2);
2042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043}
2044
2045
2046/*
Robert Becker1e509152009-12-14 12:49:58 +11002047 * Used by fix_read_error() to decay the per rdev read_errors.
2048 * We halve the read error count for every hour that has elapsed
2049 * since the last recorded read error.
2050 *
2051 */
NeilBrownfd01b882011-10-11 16:47:53 +11002052static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
Robert Becker1e509152009-12-14 12:49:58 +11002053{
2054 struct timespec cur_time_mon;
2055 unsigned long hours_since_last;
2056 unsigned int read_errors = atomic_read(&rdev->read_errors);
2057
2058 ktime_get_ts(&cur_time_mon);
2059
2060 if (rdev->last_read_error.tv_sec == 0 &&
2061 rdev->last_read_error.tv_nsec == 0) {
2062 /* first time we've seen a read error */
2063 rdev->last_read_error = cur_time_mon;
2064 return;
2065 }
2066
2067 hours_since_last = (cur_time_mon.tv_sec -
2068 rdev->last_read_error.tv_sec) / 3600;
2069
2070 rdev->last_read_error = cur_time_mon;
2071
2072 /*
2073 * if hours_since_last is > the number of bits in read_errors
2074 * just set read errors to 0. We do this to avoid
2075 * overflowing the shift of read_errors by hours_since_last.
2076 */
2077 if (hours_since_last >= 8 * sizeof(read_errors))
2078 atomic_set(&rdev->read_errors, 0);
2079 else
2080 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2081}
2082
NeilBrown3cb03002011-10-11 16:45:26 +11002083static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrown58c54fc2011-07-28 11:39:25 +10002084 int sectors, struct page *page, int rw)
2085{
2086 sector_t first_bad;
2087 int bad_sectors;
2088
2089 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2090 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2091 return -1;
2092 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
2093 /* success */
2094 return 1;
NeilBrownb7044d42011-12-23 10:17:56 +11002095 if (rw == WRITE) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002096 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002097 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2098 set_bit(MD_RECOVERY_NEEDED,
2099 &rdev->mddev->recovery);
2100 }
NeilBrown58c54fc2011-07-28 11:39:25 +10002101 /* need to record an error - either for the block or the device */
2102 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2103 md_error(rdev->mddev, rdev);
2104 return 0;
2105}
2106
Robert Becker1e509152009-12-14 12:49:58 +11002107/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 * This is a kernel thread which:
2109 *
2110 * 1. Retries failed read operations on working mirrors.
2111 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07002112 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 */
2114
NeilBrowne879a872011-10-11 16:49:02 +11002115static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown6814d532006-10-03 01:15:45 -07002116{
2117 int sect = 0; /* Offset from r10_bio->sector */
2118 int sectors = r10_bio->sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002119 struct md_rdev*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002120 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002121 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11002122
NeilBrown7c4e06f2011-05-11 14:53:17 +10002123 /* still own a reference to this rdev, so it cannot
2124 * have been cleared recently.
2125 */
2126 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002127
NeilBrown7c4e06f2011-05-11 14:53:17 +10002128 if (test_bit(Faulty, &rdev->flags))
2129 /* drive has already been failed, just ignore any
2130 more fix_read_error() attempts */
2131 return;
2132
2133 check_decay_read_errors(mddev, rdev);
2134 atomic_inc(&rdev->read_errors);
2135 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2136 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11002137 bdevname(rdev->bdev, b);
2138
NeilBrown7c4e06f2011-05-11 14:53:17 +10002139 printk(KERN_NOTICE
2140 "md/raid10:%s: %s: Raid device exceeded "
2141 "read_error threshold [cur %d:max %d]\n",
2142 mdname(mddev), b,
2143 atomic_read(&rdev->read_errors), max_read_errors);
2144 printk(KERN_NOTICE
2145 "md/raid10:%s: %s: Failing raid device\n",
2146 mdname(mddev), b);
2147 md_error(mddev, conf->mirrors[d].rdev);
NeilBrownfae8cc52012-02-14 11:10:10 +11002148 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
NeilBrown7c4e06f2011-05-11 14:53:17 +10002149 return;
Robert Becker1e509152009-12-14 12:49:58 +11002150 }
Robert Becker1e509152009-12-14 12:49:58 +11002151
NeilBrown6814d532006-10-03 01:15:45 -07002152 while(sectors) {
2153 int s = sectors;
2154 int sl = r10_bio->read_slot;
2155 int success = 0;
2156 int start;
2157
2158 if (s > (PAGE_SIZE>>9))
2159 s = PAGE_SIZE >> 9;
2160
2161 rcu_read_lock();
2162 do {
NeilBrown8dbed5c2011-07-28 11:39:24 +10002163 sector_t first_bad;
2164 int bad_sectors;
2165
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002166 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07002167 rdev = rcu_dereference(conf->mirrors[d].rdev);
2168 if (rdev &&
NeilBrown050b6612012-03-19 12:46:39 +11002169 !test_bit(Unmerged, &rdev->flags) &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002170 test_bit(In_sync, &rdev->flags) &&
2171 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2172 &first_bad, &bad_sectors) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07002173 atomic_inc(&rdev->nr_pending);
2174 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11002175 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07002176 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002177 sect,
NeilBrown6814d532006-10-03 01:15:45 -07002178 s<<9,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002179 conf->tmppage, READ, false);
NeilBrown6814d532006-10-03 01:15:45 -07002180 rdev_dec_pending(rdev, mddev);
2181 rcu_read_lock();
2182 if (success)
2183 break;
2184 }
2185 sl++;
2186 if (sl == conf->copies)
2187 sl = 0;
2188 } while (!success && sl != r10_bio->read_slot);
2189 rcu_read_unlock();
2190
2191 if (!success) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002192 /* Cannot read from anywhere, just mark the block
2193 * as bad on the first device to discourage future
2194 * reads.
2195 */
NeilBrown6814d532006-10-03 01:15:45 -07002196 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
NeilBrown58c54fc2011-07-28 11:39:25 +10002197 rdev = conf->mirrors[dn].rdev;
2198
2199 if (!rdev_set_badblocks(
2200 rdev,
2201 r10_bio->devs[r10_bio->read_slot].addr
2202 + sect,
NeilBrownfae8cc52012-02-14 11:10:10 +11002203 s, 0)) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002204 md_error(mddev, rdev);
NeilBrownfae8cc52012-02-14 11:10:10 +11002205 r10_bio->devs[r10_bio->read_slot].bio
2206 = IO_BLOCKED;
2207 }
NeilBrown6814d532006-10-03 01:15:45 -07002208 break;
2209 }
2210
2211 start = sl;
2212 /* write it back and re-read */
2213 rcu_read_lock();
2214 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11002215 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002216
NeilBrown6814d532006-10-03 01:15:45 -07002217 if (sl==0)
2218 sl = conf->copies;
2219 sl--;
2220 d = r10_bio->devs[sl].devnum;
2221 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002222 if (!rdev ||
NeilBrown050b6612012-03-19 12:46:39 +11002223 test_bit(Unmerged, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002224 !test_bit(In_sync, &rdev->flags))
2225 continue;
2226
2227 atomic_inc(&rdev->nr_pending);
2228 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002229 if (r10_sync_page_io(rdev,
2230 r10_bio->devs[sl].addr +
2231 sect,
NeilBrown65c3f182012-07-03 15:55:33 +10002232 s, conf->tmppage, WRITE)
NeilBrown1294b9c2011-07-28 11:39:23 +10002233 == 0) {
2234 /* Well, this device is dead */
2235 printk(KERN_NOTICE
2236 "md/raid10:%s: read correction "
2237 "write failed"
2238 " (%d sectors at %llu on %s)\n",
2239 mdname(mddev), s,
2240 (unsigned long long)(
2241 sect + rdev->data_offset),
2242 bdevname(rdev->bdev, b));
2243 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2244 "drive\n",
2245 mdname(mddev),
2246 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07002247 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002248 rdev_dec_pending(rdev, mddev);
2249 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002250 }
2251 sl = start;
2252 while (sl != r10_bio->read_slot) {
NeilBrown1294b9c2011-07-28 11:39:23 +10002253 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002254
NeilBrown6814d532006-10-03 01:15:45 -07002255 if (sl==0)
2256 sl = conf->copies;
2257 sl--;
2258 d = r10_bio->devs[sl].devnum;
2259 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002260 if (!rdev ||
2261 !test_bit(In_sync, &rdev->flags))
2262 continue;
Robert Becker67b8dc42009-12-14 12:49:57 +11002263
NeilBrown1294b9c2011-07-28 11:39:23 +10002264 atomic_inc(&rdev->nr_pending);
2265 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002266 switch (r10_sync_page_io(rdev,
2267 r10_bio->devs[sl].addr +
2268 sect,
NeilBrown65c3f182012-07-03 15:55:33 +10002269 s, conf->tmppage,
NeilBrown58c54fc2011-07-28 11:39:25 +10002270 READ)) {
2271 case 0:
NeilBrown1294b9c2011-07-28 11:39:23 +10002272 /* Well, this device is dead */
2273 printk(KERN_NOTICE
2274 "md/raid10:%s: unable to read back "
2275 "corrected sectors"
2276 " (%d sectors at %llu on %s)\n",
2277 mdname(mddev), s,
2278 (unsigned long long)(
2279 sect + rdev->data_offset),
2280 bdevname(rdev->bdev, b));
2281 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2282 "drive\n",
2283 mdname(mddev),
2284 bdevname(rdev->bdev, b));
NeilBrown58c54fc2011-07-28 11:39:25 +10002285 break;
2286 case 1:
NeilBrown1294b9c2011-07-28 11:39:23 +10002287 printk(KERN_INFO
2288 "md/raid10:%s: read error corrected"
2289 " (%d sectors at %llu on %s)\n",
2290 mdname(mddev), s,
2291 (unsigned long long)(
2292 sect + rdev->data_offset),
2293 bdevname(rdev->bdev, b));
2294 atomic_add(s, &rdev->corrected_errors);
NeilBrown6814d532006-10-03 01:15:45 -07002295 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002296
2297 rdev_dec_pending(rdev, mddev);
2298 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002299 }
2300 rcu_read_unlock();
2301
2302 sectors -= s;
2303 sect += s;
2304 }
2305}
2306
NeilBrownbd870a12011-07-28 11:39:24 +10002307static void bi_complete(struct bio *bio, int error)
2308{
2309 complete((struct completion *)bio->bi_private);
2310}
2311
2312static int submit_bio_wait(int rw, struct bio *bio)
2313{
2314 struct completion event;
2315 rw |= REQ_SYNC;
2316
2317 init_completion(&event);
2318 bio->bi_private = &event;
2319 bio->bi_end_io = bi_complete;
2320 submit_bio(rw, bio);
2321 wait_for_completion(&event);
2322
2323 return test_bit(BIO_UPTODATE, &bio->bi_flags);
2324}
2325
NeilBrown9f2c9d12011-10-11 16:48:43 +11002326static int narrow_write_error(struct r10bio *r10_bio, int i)
NeilBrownbd870a12011-07-28 11:39:24 +10002327{
2328 struct bio *bio = r10_bio->master_bio;
NeilBrownfd01b882011-10-11 16:47:53 +11002329 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002330 struct r10conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002331 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
NeilBrownbd870a12011-07-28 11:39:24 +10002332 /* bio has the data to be written to slot 'i' where
2333 * we just recently had a write error.
2334 * We repeatedly clone the bio and trim down to one block,
2335 * then try the write. Where the write fails we record
2336 * a bad block.
2337 * It is conceivable that the bio doesn't exactly align with
2338 * blocks. We must handle this.
2339 *
2340 * We currently own a reference to the rdev.
2341 */
2342
2343 int block_sectors;
2344 sector_t sector;
2345 int sectors;
2346 int sect_to_write = r10_bio->sectors;
2347 int ok = 1;
2348
2349 if (rdev->badblocks.shift < 0)
2350 return 0;
2351
2352 block_sectors = 1 << rdev->badblocks.shift;
2353 sector = r10_bio->sector;
2354 sectors = ((r10_bio->sector + block_sectors)
2355 & ~(sector_t)(block_sectors - 1))
2356 - sector;
2357
2358 while (sect_to_write) {
2359 struct bio *wbio;
2360 if (sectors > sect_to_write)
2361 sectors = sect_to_write;
2362 /* Write at 'sector' for 'sectors' */
2363 wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
2364 md_trim_bio(wbio, sector - bio->bi_sector, sectors);
2365 wbio->bi_sector = (r10_bio->devs[i].addr+
2366 rdev->data_offset+
2367 (sector - r10_bio->sector));
2368 wbio->bi_bdev = rdev->bdev;
2369 if (submit_bio_wait(WRITE, wbio) == 0)
2370 /* Failure! */
2371 ok = rdev_set_badblocks(rdev, sector,
2372 sectors, 0)
2373 && ok;
2374
2375 bio_put(wbio);
2376 sect_to_write -= sectors;
2377 sector += sectors;
2378 sectors = block_sectors;
2379 }
2380 return ok;
2381}
2382
NeilBrown9f2c9d12011-10-11 16:48:43 +11002383static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown560f8e52011-07-28 11:39:23 +10002384{
2385 int slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002386 struct bio *bio;
NeilBrowne879a872011-10-11 16:49:02 +11002387 struct r10conf *conf = mddev->private;
NeilBrownabbf0982011-12-23 10:17:54 +11002388 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002389 char b[BDEVNAME_SIZE];
2390 unsigned long do_sync;
NeilBrown856e08e2011-07-28 11:39:23 +10002391 int max_sectors;
NeilBrown560f8e52011-07-28 11:39:23 +10002392
2393 /* we got a read error. Maybe the drive is bad. Maybe just
2394 * the block and we can fix it.
2395 * We freeze all other IO, and try reading the block from
2396 * other devices. When we find one, we re-write
2397 * and check it that fixes the read error.
2398 * This is all done synchronously while the array is
2399 * frozen.
2400 */
NeilBrownfae8cc52012-02-14 11:10:10 +11002401 bio = r10_bio->devs[slot].bio;
2402 bdevname(bio->bi_bdev, b);
2403 bio_put(bio);
2404 r10_bio->devs[slot].bio = NULL;
2405
NeilBrown560f8e52011-07-28 11:39:23 +10002406 if (mddev->ro == 0) {
2407 freeze_array(conf);
2408 fix_read_error(conf, mddev, r10_bio);
2409 unfreeze_array(conf);
NeilBrownfae8cc52012-02-14 11:10:10 +11002410 } else
2411 r10_bio->devs[slot].bio = IO_BLOCKED;
2412
NeilBrownabbf0982011-12-23 10:17:54 +11002413 rdev_dec_pending(rdev, mddev);
NeilBrown560f8e52011-07-28 11:39:23 +10002414
NeilBrown7399c312011-07-28 11:39:23 +10002415read_more:
NeilBrown96c3fd12011-12-23 10:17:54 +11002416 rdev = read_balance(conf, r10_bio, &max_sectors);
2417 if (rdev == NULL) {
NeilBrown560f8e52011-07-28 11:39:23 +10002418 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
2419 " read error for block %llu\n",
NeilBrown7399c312011-07-28 11:39:23 +10002420 mdname(mddev), b,
NeilBrown560f8e52011-07-28 11:39:23 +10002421 (unsigned long long)r10_bio->sector);
2422 raid_end_bio_io(r10_bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002423 return;
2424 }
2425
2426 do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
NeilBrown560f8e52011-07-28 11:39:23 +10002427 slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002428 printk_ratelimited(
2429 KERN_ERR
NeilBrown65c3f182012-07-03 15:55:33 +10002430 "md/raid10:%s: %s: redirecting "
NeilBrown560f8e52011-07-28 11:39:23 +10002431 "sector %llu to another mirror\n",
2432 mdname(mddev),
2433 bdevname(rdev->bdev, b),
2434 (unsigned long long)r10_bio->sector);
2435 bio = bio_clone_mddev(r10_bio->master_bio,
2436 GFP_NOIO, mddev);
NeilBrown7399c312011-07-28 11:39:23 +10002437 md_trim_bio(bio,
2438 r10_bio->sector - bio->bi_sector,
2439 max_sectors);
NeilBrown560f8e52011-07-28 11:39:23 +10002440 r10_bio->devs[slot].bio = bio;
NeilBrownabbf0982011-12-23 10:17:54 +11002441 r10_bio->devs[slot].rdev = rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002442 bio->bi_sector = r10_bio->devs[slot].addr
2443 + rdev->data_offset;
2444 bio->bi_bdev = rdev->bdev;
2445 bio->bi_rw = READ | do_sync;
2446 bio->bi_private = r10_bio;
2447 bio->bi_end_io = raid10_end_read_request;
NeilBrown7399c312011-07-28 11:39:23 +10002448 if (max_sectors < r10_bio->sectors) {
2449 /* Drat - have to split this up more */
2450 struct bio *mbio = r10_bio->master_bio;
2451 int sectors_handled =
2452 r10_bio->sector + max_sectors
2453 - mbio->bi_sector;
2454 r10_bio->sectors = max_sectors;
2455 spin_lock_irq(&conf->device_lock);
2456 if (mbio->bi_phys_segments == 0)
2457 mbio->bi_phys_segments = 2;
2458 else
2459 mbio->bi_phys_segments++;
2460 spin_unlock_irq(&conf->device_lock);
2461 generic_make_request(bio);
NeilBrown7399c312011-07-28 11:39:23 +10002462
2463 r10_bio = mempool_alloc(conf->r10bio_pool,
2464 GFP_NOIO);
2465 r10_bio->master_bio = mbio;
2466 r10_bio->sectors = (mbio->bi_size >> 9)
2467 - sectors_handled;
2468 r10_bio->state = 0;
2469 set_bit(R10BIO_ReadError,
2470 &r10_bio->state);
2471 r10_bio->mddev = mddev;
2472 r10_bio->sector = mbio->bi_sector
2473 + sectors_handled;
2474
2475 goto read_more;
2476 } else
2477 generic_make_request(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002478}
2479
NeilBrowne879a872011-10-11 16:49:02 +11002480static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
NeilBrown749c55e2011-07-28 11:39:24 +10002481{
2482 /* Some sort of write request has finished and it
2483 * succeeded in writing where we thought there was a
2484 * bad block. So forget the bad block.
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002485 * Or possibly if failed and we need to record
2486 * a bad block.
NeilBrown749c55e2011-07-28 11:39:24 +10002487 */
2488 int m;
NeilBrown3cb03002011-10-11 16:45:26 +11002489 struct md_rdev *rdev;
NeilBrown749c55e2011-07-28 11:39:24 +10002490
2491 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2492 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002493 for (m = 0; m < conf->copies; m++) {
2494 int dev = r10_bio->devs[m].devnum;
2495 rdev = conf->mirrors[dev].rdev;
2496 if (r10_bio->devs[m].bio == NULL)
2497 continue;
2498 if (test_bit(BIO_UPTODATE,
NeilBrown749c55e2011-07-28 11:39:24 +10002499 &r10_bio->devs[m].bio->bi_flags)) {
NeilBrown749c55e2011-07-28 11:39:24 +10002500 rdev_clear_badblocks(
2501 rdev,
2502 r10_bio->devs[m].addr,
2503 r10_bio->sectors);
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002504 } else {
2505 if (!rdev_set_badblocks(
2506 rdev,
2507 r10_bio->devs[m].addr,
2508 r10_bio->sectors, 0))
2509 md_error(conf->mddev, rdev);
NeilBrown749c55e2011-07-28 11:39:24 +10002510 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002511 rdev = conf->mirrors[dev].replacement;
2512 if (r10_bio->devs[m].repl_bio == NULL)
2513 continue;
2514 if (test_bit(BIO_UPTODATE,
2515 &r10_bio->devs[m].repl_bio->bi_flags)) {
2516 rdev_clear_badblocks(
2517 rdev,
2518 r10_bio->devs[m].addr,
2519 r10_bio->sectors);
2520 } else {
2521 if (!rdev_set_badblocks(
2522 rdev,
2523 r10_bio->devs[m].addr,
2524 r10_bio->sectors, 0))
2525 md_error(conf->mddev, rdev);
2526 }
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002527 }
NeilBrown749c55e2011-07-28 11:39:24 +10002528 put_buf(r10_bio);
2529 } else {
NeilBrownbd870a12011-07-28 11:39:24 +10002530 for (m = 0; m < conf->copies; m++) {
2531 int dev = r10_bio->devs[m].devnum;
2532 struct bio *bio = r10_bio->devs[m].bio;
2533 rdev = conf->mirrors[dev].rdev;
2534 if (bio == IO_MADE_GOOD) {
NeilBrown749c55e2011-07-28 11:39:24 +10002535 rdev_clear_badblocks(
2536 rdev,
2537 r10_bio->devs[m].addr,
2538 r10_bio->sectors);
2539 rdev_dec_pending(rdev, conf->mddev);
NeilBrownbd870a12011-07-28 11:39:24 +10002540 } else if (bio != NULL &&
2541 !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2542 if (!narrow_write_error(r10_bio, m)) {
2543 md_error(conf->mddev, rdev);
2544 set_bit(R10BIO_Degraded,
2545 &r10_bio->state);
2546 }
2547 rdev_dec_pending(rdev, conf->mddev);
NeilBrown749c55e2011-07-28 11:39:24 +10002548 }
NeilBrown475b0322011-12-23 10:17:55 +11002549 bio = r10_bio->devs[m].repl_bio;
2550 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +11002551 if (rdev && bio == IO_MADE_GOOD) {
NeilBrown475b0322011-12-23 10:17:55 +11002552 rdev_clear_badblocks(
2553 rdev,
2554 r10_bio->devs[m].addr,
2555 r10_bio->sectors);
2556 rdev_dec_pending(rdev, conf->mddev);
2557 }
NeilBrownbd870a12011-07-28 11:39:24 +10002558 }
2559 if (test_bit(R10BIO_WriteError,
2560 &r10_bio->state))
2561 close_write(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10002562 raid_end_bio_io(r10_bio);
2563 }
2564}
2565
NeilBrownfd01b882011-10-11 16:47:53 +11002566static void raid10d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
NeilBrown9f2c9d12011-10-11 16:48:43 +11002568 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 unsigned long flags;
NeilBrowne879a872011-10-11 16:49:02 +11002570 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002572 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
2574 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002576 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002578
Jens Axboe7eaceac2011-03-10 08:52:07 +01002579 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002582 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002583 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002585 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002586 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08002588 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 spin_unlock_irqrestore(&conf->device_lock, flags);
2590
2591 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002592 conf = mddev->private;
NeilBrownbd870a12011-07-28 11:39:24 +10002593 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2594 test_bit(R10BIO_WriteError, &r10_bio->state))
NeilBrown749c55e2011-07-28 11:39:24 +10002595 handle_write_completed(conf, r10_bio);
2596 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01002598 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 recovery_request_write(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002600 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
NeilBrown560f8e52011-07-28 11:39:23 +10002601 handle_read_error(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002602 else {
2603 /* just a partial read to be scheduled from a
2604 * separate context
2605 */
2606 int slot = r10_bio->read_slot;
2607 generic_make_request(r10_bio->devs[slot].bio);
2608 }
NeilBrown4443ae12006-01-06 00:20:28 -08002609
NeilBrown1d9d5242009-10-16 15:55:32 +11002610 cond_resched();
NeilBrownde393cd2011-07-28 11:31:48 +10002611 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2612 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002614 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615}
2616
2617
NeilBrowne879a872011-10-11 16:49:02 +11002618static int init_resync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619{
2620 int buffs;
NeilBrown69335ef2011-12-23 10:17:54 +11002621 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
2623 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02002624 BUG_ON(conf->r10buf_pool);
NeilBrown69335ef2011-12-23 10:17:54 +11002625 conf->have_replacement = 0;
2626 for (i = 0; i < conf->raid_disks; i++)
2627 if (conf->mirrors[i].replacement)
2628 conf->have_replacement = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2630 if (!conf->r10buf_pool)
2631 return -ENOMEM;
2632 conf->next_resync = 0;
2633 return 0;
2634}
2635
2636/*
2637 * perform a "sync" on one "block"
2638 *
2639 * We need to make sure that no normal I/O request - particularly write
2640 * requests - conflict with active sync requests.
2641 *
2642 * This is achieved by tracking pending requests and a 'barrier' concept
2643 * that can be installed to exclude normal IO requests.
2644 *
2645 * Resync and recovery are handled very differently.
2646 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2647 *
2648 * For resync, we iterate over virtual addresses, read all copies,
2649 * and update if there are differences. If only one copy is live,
2650 * skip it.
2651 * For recovery, we iterate over physical addresses, read a good
2652 * value for each non-in_sync drive, and over-write.
2653 *
2654 * So, for recovery we may have several outstanding complex requests for a
2655 * given address, one for each out-of-sync device. We model this by allocating
2656 * a number of r10_bio structures, one for each out-of-sync device.
2657 * As we setup these structures, we collect all bio's together into a list
2658 * which we then process collectively to add pages, and then process again
2659 * to pass to generic_make_request.
2660 *
2661 * The r10_bio structures are linked using a borrowed master_bio pointer.
2662 * This link is counted in ->remaining. When the r10_bio that points to NULL
2663 * has its remaining count decremented to 0, the whole complex operation
2664 * is complete.
2665 *
2666 */
2667
NeilBrownfd01b882011-10-11 16:47:53 +11002668static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
NeilBrownab9d47e2011-05-11 14:54:41 +10002669 int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670{
NeilBrowne879a872011-10-11 16:49:02 +11002671 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002672 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 struct bio *biolist = NULL, *bio;
2674 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08002676 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11002677 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 sector_t sectors_skipped = 0;
2679 int chunks_skipped = 0;
2680
2681 if (!conf->r10buf_pool)
2682 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002683 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
2685 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11002686 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2688 max_sector = mddev->resync_max_sectors;
2689 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002690 /* If we aborted, we need to abort the
2691 * sync on the 'current' bitmap chucks (there can
2692 * be several when recovering multiple devices).
2693 * as we may have started syncing it but not finished.
2694 * We can find the current address in
2695 * mddev->curr_resync, but for recovery,
2696 * we need to convert that to several
2697 * virtual addresses.
2698 */
2699 if (mddev->curr_resync < max_sector) { /* aborted */
2700 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2701 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2702 &sync_blocks, 1);
2703 else for (i=0; i<conf->raid_disks; i++) {
2704 sector_t sect =
2705 raid10_find_virt(conf, mddev->curr_resync, i);
2706 bitmap_end_sync(mddev->bitmap, sect,
2707 &sync_blocks, 1);
2708 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002709 } else {
2710 /* completed sync */
2711 if ((!mddev->bitmap || conf->fullsync)
2712 && conf->have_replacement
2713 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2714 /* Completed a full sync so the replacements
2715 * are now fully recovered.
2716 */
2717 for (i = 0; i < conf->raid_disks; i++)
2718 if (conf->mirrors[i].replacement)
2719 conf->mirrors[i].replacement
2720 ->recovery_offset
2721 = MaxSector;
2722 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002723 conf->fullsync = 0;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002724 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002725 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07002727 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 return sectors_skipped;
2729 }
2730 if (chunks_skipped >= conf->raid_disks) {
2731 /* if there has been nothing to do on any drive,
2732 * then there is nothing to do at all..
2733 */
NeilBrown57afd892005-06-21 17:17:13 -07002734 *skipped = 1;
2735 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 }
2737
NeilBrownc6207272008-02-06 01:39:52 -08002738 if (max_sector > mddev->resync_max)
2739 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 /* make sure whole request will fit in a chunk - if chunks
2742 * are meaningful
2743 */
2744 if (conf->near_copies < conf->raid_disks &&
2745 max_sector > (sector_nr | conf->chunk_mask))
2746 max_sector = (sector_nr | conf->chunk_mask) + 1;
2747 /*
2748 * If there is non-resync activity waiting for us then
2749 * put in a delay to throttle resync.
2750 */
NeilBrown0a27ec92006-01-06 00:20:13 -08002751 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
2754 /* Again, very different code for resync and recovery.
2755 * Both must result in an r10bio with a list of bios that
2756 * have bi_end_io, bi_sector, bi_bdev set,
2757 * and bi_private set to the r10bio.
2758 * For recovery, we may actually create several r10bios
2759 * with 2 bios in each, that correspond to the bios in the main one.
2760 * In this case, the subordinate r10bios link back through a
2761 * borrowed master_bio pointer, and the counter in the master
2762 * includes a ref from each subordinate.
2763 */
2764 /* First, we decide what to do and set ->bi_end_io
2765 * To end_sync_read if we want to read, and
2766 * end_sync_write if we will want to write.
2767 */
2768
NeilBrown6cce3b22006-01-06 00:20:16 -08002769 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2771 /* recovery... the complicated one */
NeilBrowne875ece2011-07-28 11:39:24 +10002772 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 r10_bio = NULL;
2774
NeilBrownab9d47e2011-05-11 14:54:41 +10002775 for (i=0 ; i<conf->raid_disks; i++) {
2776 int still_degraded;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002777 struct r10bio *rb2;
NeilBrownab9d47e2011-05-11 14:54:41 +10002778 sector_t sect;
2779 int must_sync;
NeilBrowne875ece2011-07-28 11:39:24 +10002780 int any_working;
NeilBrown24afd802011-12-23 10:17:55 +11002781 struct mirror_info *mirror = &conf->mirrors[i];
NeilBrownab9d47e2011-05-11 14:54:41 +10002782
NeilBrown24afd802011-12-23 10:17:55 +11002783 if ((mirror->rdev == NULL ||
2784 test_bit(In_sync, &mirror->rdev->flags))
2785 &&
2786 (mirror->replacement == NULL ||
2787 test_bit(Faulty,
2788 &mirror->replacement->flags)))
NeilBrownab9d47e2011-05-11 14:54:41 +10002789 continue;
2790
2791 still_degraded = 0;
2792 /* want to reconstruct this device */
2793 rb2 = r10_bio;
2794 sect = raid10_find_virt(conf, sector_nr, i);
NeilBrown04e0f692012-07-03 10:37:30 +10002795 if (sect >= mddev->resync_max_sectors) {
2796 /* last stripe is not complete - don't
2797 * try to recover this sector.
2798 */
2799 continue;
2800 }
NeilBrown24afd802011-12-23 10:17:55 +11002801 /* Unless we are doing a full sync, or a replacement
2802 * we only need to recover the block if it is set in
2803 * the bitmap
NeilBrownab9d47e2011-05-11 14:54:41 +10002804 */
2805 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2806 &sync_blocks, 1);
2807 if (sync_blocks < max_sync)
2808 max_sync = sync_blocks;
2809 if (!must_sync &&
NeilBrown24afd802011-12-23 10:17:55 +11002810 mirror->replacement == NULL &&
NeilBrownab9d47e2011-05-11 14:54:41 +10002811 !conf->fullsync) {
2812 /* yep, skip the sync_blocks here, but don't assume
2813 * that there will never be anything to do here
NeilBrown6cce3b22006-01-06 00:20:16 -08002814 */
NeilBrownab9d47e2011-05-11 14:54:41 +10002815 chunks_skipped = -1;
2816 continue;
2817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
NeilBrownab9d47e2011-05-11 14:54:41 +10002819 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2820 raise_barrier(conf, rb2 != NULL);
2821 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
NeilBrownab9d47e2011-05-11 14:54:41 +10002823 r10_bio->master_bio = (struct bio*)rb2;
2824 if (rb2)
2825 atomic_inc(&rb2->remaining);
2826 r10_bio->mddev = mddev;
2827 set_bit(R10BIO_IsRecover, &r10_bio->state);
2828 r10_bio->sector = sect;
NeilBrown6cce3b22006-01-06 00:20:16 -08002829
NeilBrownab9d47e2011-05-11 14:54:41 +10002830 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10002831
NeilBrownab9d47e2011-05-11 14:54:41 +10002832 /* Need to check if the array will still be
2833 * degraded
2834 */
2835 for (j=0; j<conf->raid_disks; j++)
2836 if (conf->mirrors[j].rdev == NULL ||
2837 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
2838 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07002839 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002841
2842 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2843 &sync_blocks, still_degraded);
2844
NeilBrowne875ece2011-07-28 11:39:24 +10002845 any_working = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10002846 for (j=0; j<conf->copies;j++) {
NeilBrowne875ece2011-07-28 11:39:24 +10002847 int k;
NeilBrownab9d47e2011-05-11 14:54:41 +10002848 int d = r10_bio->devs[j].devnum;
NeilBrown5e570282011-07-28 11:39:25 +10002849 sector_t from_addr, to_addr;
NeilBrown3cb03002011-10-11 16:45:26 +11002850 struct md_rdev *rdev;
NeilBrown40c356c2011-07-28 11:39:24 +10002851 sector_t sector, first_bad;
2852 int bad_sectors;
NeilBrownab9d47e2011-05-11 14:54:41 +10002853 if (!conf->mirrors[d].rdev ||
2854 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
2855 continue;
2856 /* This is where we read from */
NeilBrowne875ece2011-07-28 11:39:24 +10002857 any_working = 1;
NeilBrown40c356c2011-07-28 11:39:24 +10002858 rdev = conf->mirrors[d].rdev;
2859 sector = r10_bio->devs[j].addr;
2860
2861 if (is_badblock(rdev, sector, max_sync,
2862 &first_bad, &bad_sectors)) {
2863 if (first_bad > sector)
2864 max_sync = first_bad - sector;
2865 else {
2866 bad_sectors -= (sector
2867 - first_bad);
2868 if (max_sync > bad_sectors)
2869 max_sync = bad_sectors;
2870 continue;
2871 }
2872 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002873 bio = r10_bio->devs[0].bio;
2874 bio->bi_next = biolist;
2875 biolist = bio;
2876 bio->bi_private = r10_bio;
2877 bio->bi_end_io = end_sync_read;
2878 bio->bi_rw = READ;
NeilBrown5e570282011-07-28 11:39:25 +10002879 from_addr = r10_bio->devs[j].addr;
NeilBrown24afd802011-12-23 10:17:55 +11002880 bio->bi_sector = from_addr + rdev->data_offset;
2881 bio->bi_bdev = rdev->bdev;
2882 atomic_inc(&rdev->nr_pending);
2883 /* and we write to 'i' (if not in_sync) */
NeilBrownab9d47e2011-05-11 14:54:41 +10002884
2885 for (k=0; k<conf->copies; k++)
2886 if (r10_bio->devs[k].devnum == i)
2887 break;
2888 BUG_ON(k == conf->copies);
NeilBrown5e570282011-07-28 11:39:25 +10002889 to_addr = r10_bio->devs[k].addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10002890 r10_bio->devs[0].devnum = d;
NeilBrown5e570282011-07-28 11:39:25 +10002891 r10_bio->devs[0].addr = from_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10002892 r10_bio->devs[1].devnum = i;
NeilBrown5e570282011-07-28 11:39:25 +10002893 r10_bio->devs[1].addr = to_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10002894
NeilBrown24afd802011-12-23 10:17:55 +11002895 rdev = mirror->rdev;
2896 if (!test_bit(In_sync, &rdev->flags)) {
2897 bio = r10_bio->devs[1].bio;
2898 bio->bi_next = biolist;
2899 biolist = bio;
2900 bio->bi_private = r10_bio;
2901 bio->bi_end_io = end_sync_write;
2902 bio->bi_rw = WRITE;
2903 bio->bi_sector = to_addr
2904 + rdev->data_offset;
2905 bio->bi_bdev = rdev->bdev;
2906 atomic_inc(&r10_bio->remaining);
2907 } else
2908 r10_bio->devs[1].bio->bi_end_io = NULL;
2909
2910 /* and maybe write to replacement */
2911 bio = r10_bio->devs[1].repl_bio;
2912 if (bio)
2913 bio->bi_end_io = NULL;
2914 rdev = mirror->replacement;
2915 /* Note: if rdev != NULL, then bio
2916 * cannot be NULL as r10buf_pool_alloc will
2917 * have allocated it.
2918 * So the second test here is pointless.
2919 * But it keeps semantic-checkers happy, and
2920 * this comment keeps human reviewers
2921 * happy.
2922 */
2923 if (rdev == NULL || bio == NULL ||
2924 test_bit(Faulty, &rdev->flags))
2925 break;
2926 bio->bi_next = biolist;
2927 biolist = bio;
2928 bio->bi_private = r10_bio;
2929 bio->bi_end_io = end_sync_write;
2930 bio->bi_rw = WRITE;
2931 bio->bi_sector = to_addr + rdev->data_offset;
2932 bio->bi_bdev = rdev->bdev;
2933 atomic_inc(&r10_bio->remaining);
NeilBrownab9d47e2011-05-11 14:54:41 +10002934 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002936 if (j == conf->copies) {
NeilBrowne875ece2011-07-28 11:39:24 +10002937 /* Cannot recover, so abort the recovery or
2938 * record a bad block */
NeilBrownab9d47e2011-05-11 14:54:41 +10002939 put_buf(r10_bio);
2940 if (rb2)
2941 atomic_dec(&rb2->remaining);
2942 r10_bio = rb2;
NeilBrowne875ece2011-07-28 11:39:24 +10002943 if (any_working) {
2944 /* problem is that there are bad blocks
2945 * on other device(s)
2946 */
2947 int k;
2948 for (k = 0; k < conf->copies; k++)
2949 if (r10_bio->devs[k].devnum == i)
2950 break;
NeilBrown24afd802011-12-23 10:17:55 +11002951 if (!test_bit(In_sync,
2952 &mirror->rdev->flags)
2953 && !rdev_set_badblocks(
2954 mirror->rdev,
2955 r10_bio->devs[k].addr,
2956 max_sync, 0))
2957 any_working = 0;
2958 if (mirror->replacement &&
2959 !rdev_set_badblocks(
2960 mirror->replacement,
NeilBrowne875ece2011-07-28 11:39:24 +10002961 r10_bio->devs[k].addr,
2962 max_sync, 0))
2963 any_working = 0;
2964 }
2965 if (!any_working) {
2966 if (!test_and_set_bit(MD_RECOVERY_INTR,
2967 &mddev->recovery))
2968 printk(KERN_INFO "md/raid10:%s: insufficient "
2969 "working devices for recovery.\n",
2970 mdname(mddev));
NeilBrown24afd802011-12-23 10:17:55 +11002971 mirror->recovery_disabled
NeilBrowne875ece2011-07-28 11:39:24 +10002972 = mddev->recovery_disabled;
2973 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002974 break;
2975 }
2976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 if (biolist == NULL) {
2978 while (r10_bio) {
NeilBrown9f2c9d12011-10-11 16:48:43 +11002979 struct r10bio *rb2 = r10_bio;
2980 r10_bio = (struct r10bio*) rb2->master_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 rb2->master_bio = NULL;
2982 put_buf(rb2);
2983 }
2984 goto giveup;
2985 }
2986 } else {
2987 /* resync. Schedule a read for every block at this virt offset */
2988 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08002989
NeilBrown78200d42009-02-25 13:18:47 +11002990 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
2991
NeilBrown6cce3b22006-01-06 00:20:16 -08002992 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2993 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10002994 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
2995 &mddev->recovery)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002996 /* We can skip this block */
2997 *skipped = 1;
2998 return sync_blocks + sectors_skipped;
2999 }
3000 if (sync_blocks < max_sync)
3001 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
3003
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 r10_bio->mddev = mddev;
3005 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08003006 raise_barrier(conf, 0);
3007 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
3009 r10_bio->master_bio = NULL;
3010 r10_bio->sector = sector_nr;
3011 set_bit(R10BIO_IsSync, &r10_bio->state);
3012 raid10_find_phys(conf, r10_bio);
3013 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
3014
3015 for (i=0; i<conf->copies; i++) {
3016 int d = r10_bio->devs[i].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10003017 sector_t first_bad, sector;
3018 int bad_sectors;
3019
NeilBrown9ad1aef2011-12-23 10:17:55 +11003020 if (r10_bio->devs[i].repl_bio)
3021 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3022
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 bio = r10_bio->devs[i].bio;
3024 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07003025 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08003027 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 continue;
NeilBrown40c356c2011-07-28 11:39:24 +10003029 sector = r10_bio->devs[i].addr;
3030 if (is_badblock(conf->mirrors[d].rdev,
3031 sector, max_sync,
3032 &first_bad, &bad_sectors)) {
3033 if (first_bad > sector)
3034 max_sync = first_bad - sector;
3035 else {
3036 bad_sectors -= (sector - first_bad);
3037 if (max_sync > bad_sectors)
Dan Carpenter9b38cc42012-10-11 14:20:58 +11003038 max_sync = bad_sectors;
NeilBrown40c356c2011-07-28 11:39:24 +10003039 continue;
3040 }
3041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
3043 atomic_inc(&r10_bio->remaining);
3044 bio->bi_next = biolist;
3045 biolist = bio;
3046 bio->bi_private = r10_bio;
3047 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08003048 bio->bi_rw = READ;
NeilBrown40c356c2011-07-28 11:39:24 +10003049 bio->bi_sector = sector +
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 conf->mirrors[d].rdev->data_offset;
3051 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
3052 count++;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003053
3054 if (conf->mirrors[d].replacement == NULL ||
3055 test_bit(Faulty,
3056 &conf->mirrors[d].replacement->flags))
3057 continue;
3058
3059 /* Need to set up for writing to the replacement */
3060 bio = r10_bio->devs[i].repl_bio;
3061 clear_bit(BIO_UPTODATE, &bio->bi_flags);
3062
3063 sector = r10_bio->devs[i].addr;
3064 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
3065 bio->bi_next = biolist;
3066 biolist = bio;
3067 bio->bi_private = r10_bio;
3068 bio->bi_end_io = end_sync_write;
3069 bio->bi_rw = WRITE;
3070 bio->bi_sector = sector +
3071 conf->mirrors[d].replacement->data_offset;
3072 bio->bi_bdev = conf->mirrors[d].replacement->bdev;
3073 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 }
3075
3076 if (count < 2) {
3077 for (i=0; i<conf->copies; i++) {
3078 int d = r10_bio->devs[i].devnum;
3079 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10003080 rdev_dec_pending(conf->mirrors[d].rdev,
3081 mddev);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003082 if (r10_bio->devs[i].repl_bio &&
3083 r10_bio->devs[i].repl_bio->bi_end_io)
3084 rdev_dec_pending(
3085 conf->mirrors[d].replacement,
3086 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 }
3088 put_buf(r10_bio);
3089 biolist = NULL;
3090 goto giveup;
3091 }
3092 }
3093
3094 for (bio = biolist; bio ; bio=bio->bi_next) {
3095
3096 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
3097 if (bio->bi_end_io)
3098 bio->bi_flags |= 1 << BIO_UPTODATE;
3099 bio->bi_vcnt = 0;
3100 bio->bi_idx = 0;
3101 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 bio->bi_size = 0;
3103 }
3104
3105 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003106 if (sector_nr + max_sync < max_sector)
3107 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 do {
3109 struct page *page;
3110 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 if (sector_nr + (len>>9) > max_sector)
3112 len = (max_sector - sector_nr) << 9;
3113 if (len == 0)
3114 break;
3115 for (bio= biolist ; bio ; bio=bio->bi_next) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003116 struct bio *bio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
NeilBrownab9d47e2011-05-11 14:54:41 +10003118 if (bio_add_page(bio, page, len, 0))
3119 continue;
3120
3121 /* stop here */
3122 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
3123 for (bio2 = biolist;
3124 bio2 && bio2 != bio;
3125 bio2 = bio2->bi_next) {
3126 /* remove last page from this bio */
3127 bio2->bi_vcnt--;
3128 bio2->bi_size -= len;
3129 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003131 goto bio_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 }
3133 nr_sectors += len>>9;
3134 sector_nr += len>>9;
3135 } while (biolist->bi_vcnt < RESYNC_PAGES);
3136 bio_full:
3137 r10_bio->sectors = nr_sectors;
3138
3139 while (biolist) {
3140 bio = biolist;
3141 biolist = biolist->bi_next;
3142
3143 bio->bi_next = NULL;
3144 r10_bio = bio->bi_private;
3145 r10_bio->sectors = nr_sectors;
3146
3147 if (bio->bi_end_io == end_sync_read) {
3148 md_sync_acct(bio->bi_bdev, nr_sectors);
3149 generic_make_request(bio);
3150 }
3151 }
3152
NeilBrown57afd892005-06-21 17:17:13 -07003153 if (sectors_skipped)
3154 /* pretend they weren't skipped, it makes
3155 * no important difference in this case
3156 */
3157 md_done_sync(mddev, sectors_skipped, 1);
3158
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 return sectors_skipped + nr_sectors;
3160 giveup:
3161 /* There is nowhere to write, so all non-sync
NeilBrowne875ece2011-07-28 11:39:24 +10003162 * drives must be failed or in resync, all drives
3163 * have a bad block, so try the next chunk...
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 */
NeilBrown09b40682009-02-25 13:18:47 +11003165 if (sector_nr + max_sync < max_sector)
3166 max_sector = sector_nr + max_sync;
3167
3168 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 chunks_skipped ++;
3170 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172}
3173
Dan Williams80c3a6c2009-03-17 18:10:40 -07003174static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11003175raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07003176{
3177 sector_t size;
NeilBrowne879a872011-10-11 16:49:02 +11003178 struct r10conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003179
3180 if (!raid_disks)
NeilBrown84707f32010-03-16 17:23:35 +11003181 raid_disks = conf->raid_disks;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003182 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003183 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003184
3185 size = sectors >> conf->chunk_shift;
3186 sector_div(size, conf->far_copies);
3187 size = size * raid_disks;
3188 sector_div(size, conf->near_copies);
3189
3190 return size << conf->chunk_shift;
3191}
3192
NeilBrown6508fdb2012-05-17 10:08:45 +10003193static void calc_sectors(struct r10conf *conf, sector_t size)
3194{
3195 /* Calculate the number of sectors-per-device that will
3196 * actually be used, and set conf->dev_sectors and
3197 * conf->stride
3198 */
3199
3200 size = size >> conf->chunk_shift;
3201 sector_div(size, conf->far_copies);
3202 size = size * conf->raid_disks;
3203 sector_div(size, conf->near_copies);
3204 /* 'size' is now the number of chunks in the array */
3205 /* calculate "used chunks per device" */
3206 size = size * conf->copies;
3207
3208 /* We need to round up when dividing by raid_disks to
3209 * get the stride size.
3210 */
3211 size = DIV_ROUND_UP_SECTOR_T(size, conf->raid_disks);
3212
3213 conf->dev_sectors = size << conf->chunk_shift;
3214
3215 if (conf->far_offset)
3216 conf->stride = 1 << conf->chunk_shift;
3217 else {
NeilBrownb0d634d2012-05-19 09:01:13 +10003218 sector_div(size, conf->far_copies);
NeilBrown6508fdb2012-05-17 10:08:45 +10003219 conf->stride = size << conf->chunk_shift;
3220 }
3221}
Trela, Maciejdab8b292010-03-08 16:02:45 +11003222
NeilBrowne879a872011-10-11 16:49:02 +11003223static struct r10conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224{
NeilBrowne879a872011-10-11 16:49:02 +11003225 struct r10conf *conf = NULL;
NeilBrownc93983b2006-06-26 00:27:41 -07003226 int nc, fc, fo;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003227 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
Maciej Trelaf73ea872010-06-16 11:46:29 +01003229 if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
3230 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown128595e2010-05-03 14:47:14 +10003231 printk(KERN_ERR "md/raid10:%s: chunk size must be "
3232 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
3233 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003234 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 }
NeilBrown2604b702006-01-06 00:20:36 -08003236
Maciej Trelaf73ea872010-06-16 11:46:29 +01003237 nc = mddev->new_layout & 255;
3238 fc = (mddev->new_layout >> 8) & 255;
3239 fo = mddev->new_layout & (1<<16);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003240
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
Maciej Trelaf73ea872010-06-16 11:46:29 +01003242 (mddev->new_layout >> 17)) {
NeilBrown128595e2010-05-03 14:47:14 +10003243 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
Maciej Trelaf73ea872010-06-16 11:46:29 +01003244 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 goto out;
3246 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11003247
3248 err = -ENOMEM;
NeilBrowne879a872011-10-11 16:49:02 +11003249 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003250 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003252
NeilBrown4443ae12006-01-06 00:20:28 -08003253 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Trela, Maciejdab8b292010-03-08 16:02:45 +11003254 GFP_KERNEL);
3255 if (!conf->mirrors)
3256 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08003257
3258 conf->tmppage = alloc_page(GFP_KERNEL);
3259 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003260 goto out;
3261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
NeilBrown64a742b2007-02-28 20:11:18 -08003263 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 conf->near_copies = nc;
3265 conf->far_copies = fc;
3266 conf->copies = nc*fc;
NeilBrownc93983b2006-06-26 00:27:41 -07003267 conf->far_offset = fo;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003268 conf->chunk_mask = mddev->new_chunk_sectors - 1;
3269 conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
3270
3271 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
3272 r10bio_pool_free, conf);
3273 if (!conf->r10bio_pool)
3274 goto out;
3275
NeilBrown6508fdb2012-05-17 10:08:45 +10003276 calc_sectors(conf, mddev->dev_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
Neil Browne7e72bf2008-05-14 16:05:54 -07003278 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003279 INIT_LIST_HEAD(&conf->retry_list);
3280
3281 spin_lock_init(&conf->resync_lock);
3282 init_waitqueue_head(&conf->wait_barrier);
3283
3284 conf->thread = md_register_thread(raid10d, mddev, NULL);
3285 if (!conf->thread)
3286 goto out;
3287
Trela, Maciejdab8b292010-03-08 16:02:45 +11003288 conf->mddev = mddev;
3289 return conf;
3290
3291 out:
NeilBrown128595e2010-05-03 14:47:14 +10003292 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
Trela, Maciejdab8b292010-03-08 16:02:45 +11003293 mdname(mddev));
3294 if (conf) {
3295 if (conf->r10bio_pool)
3296 mempool_destroy(conf->r10bio_pool);
3297 kfree(conf->mirrors);
3298 safe_put_page(conf->tmppage);
3299 kfree(conf);
3300 }
3301 return ERR_PTR(err);
3302}
3303
NeilBrownfd01b882011-10-11 16:47:53 +11003304static int run(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003305{
NeilBrowne879a872011-10-11 16:49:02 +11003306 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003307 int i, disk_idx, chunk_size;
NeilBrown0f6d02d2011-10-11 16:48:46 +11003308 struct mirror_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11003309 struct md_rdev *rdev;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003310 sector_t size;
3311
3312 /*
3313 * copy the already verified devices into our private RAID10
3314 * bookkeeping area. [whatever we allocate in run(),
3315 * should be freed in stop()]
3316 */
3317
3318 if (mddev->private == NULL) {
3319 conf = setup_conf(mddev);
3320 if (IS_ERR(conf))
3321 return PTR_ERR(conf);
3322 mddev->private = conf;
3323 }
3324 conf = mddev->private;
3325 if (!conf)
3326 goto out;
3327
Trela, Maciejdab8b292010-03-08 16:02:45 +11003328 mddev->thread = conf->thread;
3329 conf->thread = NULL;
3330
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003331 chunk_size = mddev->chunk_sectors << 9;
3332 blk_queue_io_min(mddev->queue, chunk_size);
3333 if (conf->raid_disks % conf->near_copies)
3334 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
3335 else
3336 blk_queue_io_opt(mddev->queue, chunk_size *
3337 (conf->raid_disks / conf->near_copies));
3338
NeilBrowndafb20f2012-03-19 12:46:39 +11003339 rdev_for_each(rdev, mddev) {
NeilBrown584b8862012-05-31 15:39:11 +10003340 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 disk_idx = rdev->raid_disk;
NeilBrown84707f32010-03-16 17:23:35 +11003342 if (disk_idx >= conf->raid_disks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 || disk_idx < 0)
3344 continue;
3345 disk = conf->mirrors + disk_idx;
3346
NeilBrown56a25592011-12-23 10:17:55 +11003347 if (test_bit(Replacement, &rdev->flags)) {
3348 if (disk->replacement)
3349 goto out_free_conf;
3350 disk->replacement = rdev;
3351 } else {
3352 if (disk->rdev)
3353 goto out_free_conf;
3354 disk->rdev = rdev;
3355 }
NeilBrown584b8862012-05-31 15:39:11 +10003356 q = bdev_get_queue(rdev->bdev);
3357 if (q->merge_bvec_fn)
3358 mddev->merge_check_needed = 1;
NeilBrown56a25592011-12-23 10:17:55 +11003359
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003360 disk_stack_limits(mddev->gendisk, rdev->bdev,
3361 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
3363 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 }
NeilBrown6d508242005-09-09 16:24:03 -07003365 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10003366 if (!enough(conf, -1)) {
NeilBrown128595e2010-05-03 14:47:14 +10003367 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07003368 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 goto out_free_conf;
3370 }
3371
3372 mddev->degraded = 0;
3373 for (i = 0; i < conf->raid_disks; i++) {
3374
3375 disk = conf->mirrors + i;
3376
NeilBrown56a25592011-12-23 10:17:55 +11003377 if (!disk->rdev && disk->replacement) {
3378 /* The replacement is all we have - use it */
3379 disk->rdev = disk->replacement;
3380 disk->replacement = NULL;
3381 clear_bit(Replacement, &disk->rdev->flags);
3382 }
3383
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003384 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07003385 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 disk->head_position = 0;
3387 mddev->degraded++;
Neil Brown8c2e8702008-06-28 08:30:52 +10003388 if (disk->rdev)
3389 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 }
NeilBrownd890fa22011-10-26 11:54:39 +11003391 disk->recovery_disabled = mddev->recovery_disabled - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 }
3393
Andre Noll8c6ac862009-06-18 08:48:06 +10003394 if (mddev->recovery_cp != MaxSector)
NeilBrown128595e2010-05-03 14:47:14 +10003395 printk(KERN_NOTICE "md/raid10:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10003396 " -- starting background reconstruction\n",
3397 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 printk(KERN_INFO
NeilBrown128595e2010-05-03 14:47:14 +10003399 "md/raid10:%s: active with %d out of %d devices\n",
NeilBrown84707f32010-03-16 17:23:35 +11003400 mdname(mddev), conf->raid_disks - mddev->degraded,
3401 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 /*
3403 * Ok, everything is just fine now
3404 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11003405 mddev->dev_sectors = conf->dev_sectors;
3406 size = raid10_size(mddev, 0, 0);
3407 md_set_array_sectors(mddev, size);
3408 mddev->resync_max_sectors = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409
NeilBrown0d129222006-10-03 01:15:54 -07003410 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
3411 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown7a5febe2005-05-16 21:53:16 -07003412
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 /* Calculate max read-ahead size.
3414 * We need to readahead at least twice a whole stripe....
3415 * maybe...
3416 */
3417 {
Andre Noll9d8f0362009-06-18 08:45:01 +10003418 int stripe = conf->raid_disks *
3419 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 stripe /= conf->near_copies;
3421 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
3422 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
3423 }
3424
NeilBrown050b6612012-03-19 12:46:39 +11003425 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
Martin K. Petersena91a2782011-03-17 11:11:05 +01003426
3427 if (md_integrity_register(mddev))
3428 goto out_free_conf;
3429
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 return 0;
3431
3432out_free_conf:
NeilBrown01f96c02011-09-21 15:30:20 +10003433 md_unregister_thread(&mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 if (conf->r10bio_pool)
3435 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08003436 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003437 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 kfree(conf);
3439 mddev->private = NULL;
3440out:
3441 return -EIO;
3442}
3443
NeilBrownfd01b882011-10-11 16:47:53 +11003444static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445{
NeilBrowne879a872011-10-11 16:49:02 +11003446 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
NeilBrown409c57f2009-03-31 14:39:39 +11003448 raise_barrier(conf, 0);
3449 lower_barrier(conf);
3450
NeilBrown01f96c02011-09-21 15:30:20 +10003451 md_unregister_thread(&mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
3453 if (conf->r10bio_pool)
3454 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003455 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 kfree(conf);
3457 mddev->private = NULL;
3458 return 0;
3459}
3460
NeilBrownfd01b882011-10-11 16:47:53 +11003461static void raid10_quiesce(struct mddev *mddev, int state)
NeilBrown6cce3b22006-01-06 00:20:16 -08003462{
NeilBrowne879a872011-10-11 16:49:02 +11003463 struct r10conf *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08003464
3465 switch(state) {
3466 case 1:
3467 raise_barrier(conf, 0);
3468 break;
3469 case 0:
3470 lower_barrier(conf);
3471 break;
3472 }
NeilBrown6cce3b22006-01-06 00:20:16 -08003473}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474
NeilBrown006a09a2012-03-19 12:46:40 +11003475static int raid10_resize(struct mddev *mddev, sector_t sectors)
3476{
3477 /* Resize of 'far' arrays is not supported.
3478 * For 'near' and 'offset' arrays we can set the
3479 * number of sectors used to be an appropriate multiple
3480 * of the chunk size.
3481 * For 'offset', this is far_copies*chunksize.
3482 * For 'near' the multiplier is the LCM of
3483 * near_copies and raid_disks.
3484 * So if far_copies > 1 && !far_offset, fail.
3485 * Else find LCM(raid_disks, near_copy)*far_copies and
3486 * multiply by chunk_size. Then round to this number.
3487 * This is mostly done by raid10_size()
3488 */
3489 struct r10conf *conf = mddev->private;
3490 sector_t oldsize, size;
3491
3492 if (conf->far_copies > 1 && !conf->far_offset)
3493 return -EINVAL;
3494
3495 oldsize = raid10_size(mddev, 0, 0);
3496 size = raid10_size(mddev, sectors, 0);
3497 md_set_array_sectors(mddev, size);
3498 if (mddev->array_sectors > size)
3499 return -EINVAL;
3500 set_capacity(mddev->gendisk, mddev->array_sectors);
3501 revalidate_disk(mddev->gendisk);
3502 if (sectors > mddev->dev_sectors &&
3503 mddev->recovery_cp > oldsize) {
3504 mddev->recovery_cp = oldsize;
3505 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3506 }
NeilBrown6508fdb2012-05-17 10:08:45 +10003507 calc_sectors(conf, sectors);
3508 mddev->dev_sectors = conf->dev_sectors;
NeilBrown006a09a2012-03-19 12:46:40 +11003509 mddev->resync_max_sectors = size;
3510 return 0;
3511}
3512
NeilBrownfd01b882011-10-11 16:47:53 +11003513static void *raid10_takeover_raid0(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003514{
NeilBrown3cb03002011-10-11 16:45:26 +11003515 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +11003516 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003517
3518 if (mddev->degraded > 0) {
NeilBrown128595e2010-05-03 14:47:14 +10003519 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
3520 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003521 return ERR_PTR(-EINVAL);
3522 }
3523
Trela, Maciejdab8b292010-03-08 16:02:45 +11003524 /* Set new parameters */
3525 mddev->new_level = 10;
3526 /* new layout: far_copies = 1, near_copies = 2 */
3527 mddev->new_layout = (1<<8) + 2;
3528 mddev->new_chunk_sectors = mddev->chunk_sectors;
3529 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003530 mddev->raid_disks *= 2;
3531 /* make sure it will be not marked as dirty */
3532 mddev->recovery_cp = MaxSector;
3533
3534 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003535 if (!IS_ERR(conf)) {
NeilBrowndafb20f2012-03-19 12:46:39 +11003536 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003537 if (rdev->raid_disk >= 0)
3538 rdev->new_raid_disk = rdev->raid_disk * 2;
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003539 conf->barrier = 1;
3540 }
3541
Trela, Maciejdab8b292010-03-08 16:02:45 +11003542 return conf;
3543}
3544
NeilBrownfd01b882011-10-11 16:47:53 +11003545static void *raid10_takeover(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003546{
NeilBrowne373ab12011-10-11 16:48:59 +11003547 struct r0conf *raid0_conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003548
3549 /* raid10 can take over:
3550 * raid0 - providing it has only two drives
3551 */
3552 if (mddev->level == 0) {
3553 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11003554 raid0_conf = mddev->private;
3555 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown128595e2010-05-03 14:47:14 +10003556 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
3557 " with more than one zone.\n",
3558 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003559 return ERR_PTR(-EINVAL);
3560 }
3561 return raid10_takeover_raid0(mddev);
3562 }
3563 return ERR_PTR(-EINVAL);
3564}
3565
NeilBrown84fc4b52011-10-11 16:49:58 +11003566static struct md_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567{
3568 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08003569 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 .owner = THIS_MODULE,
3571 .make_request = make_request,
3572 .run = run,
3573 .stop = stop,
3574 .status = status,
3575 .error_handler = error,
3576 .hot_add_disk = raid10_add_disk,
3577 .hot_remove_disk= raid10_remove_disk,
3578 .spare_active = raid10_spare_active,
3579 .sync_request = sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08003580 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07003581 .size = raid10_size,
NeilBrown006a09a2012-03-19 12:46:40 +11003582 .resize = raid10_resize,
Trela, Maciejdab8b292010-03-08 16:02:45 +11003583 .takeover = raid10_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584};
3585
3586static int __init raid_init(void)
3587{
NeilBrown2604b702006-01-06 00:20:36 -08003588 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589}
3590
3591static void raid_exit(void)
3592{
NeilBrown2604b702006-01-06 00:20:36 -08003593 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594}
3595
3596module_init(raid_init);
3597module_exit(raid_exit);
3598MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11003599MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08003601MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08003602MODULE_ALIAS("md-level-10");
NeilBrown34db0cd2011-10-11 16:50:01 +11003603
3604module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);