blob: 36f445f9e11d961bbac43e74a7ebb8e18e49e642 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 set_bit(R10BIO_Uptodate, &r10_bio->state);
456
NeilBrown749c55e2011-07-28 11:39:24 +1000457 /* Maybe we can clear some bad blocks. */
NeilBrown475b0322011-12-23 10:17:55 +1100458 if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +1000459 r10_bio->devs[slot].addr,
460 r10_bio->sectors,
461 &first_bad, &bad_sectors)) {
462 bio_put(bio);
NeilBrown475b0322011-12-23 10:17:55 +1100463 if (repl)
464 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
465 else
466 r10_bio->devs[slot].bio = IO_MADE_GOOD;
NeilBrown749c55e2011-07-28 11:39:24 +1000467 dec_rdev = 0;
468 set_bit(R10BIO_MadeGood, &r10_bio->state);
469 }
470 }
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /*
473 *
474 * Let's see if all mirrored write operations have finished
475 * already.
476 */
NeilBrown19d5f832011-09-10 17:21:17 +1000477 one_write_done(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +1000478 if (dec_rdev)
479 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482/*
483 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300484 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 * parameters: near_copies and far_copies.
486 * near_copies * far_copies must be <= raid_disks.
487 * Normally one of these will be 1.
488 * If both are 1, we get raid0.
489 * If near_copies == raid_disks, we get raid1.
490 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300491 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 * first chunk, followed by near_copies copies of the next chunk and
493 * so on.
494 * If far_copies > 1, then after 1/far_copies of the array has been assigned
495 * as described above, we start again with a device offset of near_copies.
496 * So we effectively have another copy of the whole array further down all
497 * the drives, but with blocks on different drives.
498 * With this layout, and block is never stored twice on the one device.
499 *
500 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700501 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 *
503 * raid10_find_virt does the reverse mapping, from a device and a
504 * sector offset to a virtual address
505 */
506
NeilBrowne879a872011-10-11 16:49:02 +1100507static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 int n,f;
510 sector_t sector;
511 sector_t chunk;
512 sector_t stripe;
513 int dev;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000514 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 int slot = 0;
517
518 /* now calculate first sector/dev */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000519 chunk = r10bio->sector >> geo->chunk_shift;
520 sector = r10bio->sector & geo->chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
NeilBrown5cf00fc2012-05-21 09:28:20 +1000522 chunk *= geo->near_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 stripe = chunk;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000524 dev = sector_div(stripe, geo->raid_disks);
525 if (geo->far_offset)
526 stripe *= geo->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
NeilBrown5cf00fc2012-05-21 09:28:20 +1000528 sector += stripe << geo->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 /* and calculate all the others */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000531 for (n = 0; n < geo->near_copies; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 int d = dev;
533 sector_t s = sector;
534 r10bio->devs[slot].addr = sector;
535 r10bio->devs[slot].devnum = d;
536 slot++;
537
NeilBrown5cf00fc2012-05-21 09:28:20 +1000538 for (f = 1; f < geo->far_copies; f++) {
539 d += geo->near_copies;
540 if (d >= geo->raid_disks)
541 d -= geo->raid_disks;
542 s += geo->stride;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 r10bio->devs[slot].devnum = d;
544 r10bio->devs[slot].addr = s;
545 slot++;
546 }
547 dev++;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000548 if (dev >= geo->raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 dev = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000550 sector += (geo->chunk_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552 }
553 BUG_ON(slot != conf->copies);
554}
555
NeilBrowne879a872011-10-11 16:49:02 +1100556static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 sector_t offset, chunk, vchunk;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000559 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
NeilBrown5cf00fc2012-05-21 09:28:20 +1000561 offset = sector & geo->chunk_mask;
562 if (geo->far_offset) {
NeilBrownc93983b2006-06-26 00:27:41 -0700563 int fc;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000564 chunk = sector >> geo->chunk_shift;
565 fc = sector_div(chunk, geo->far_copies);
566 dev -= fc * geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700567 if (dev < 0)
NeilBrown5cf00fc2012-05-21 09:28:20 +1000568 dev += geo->raid_disks;
NeilBrownc93983b2006-06-26 00:27:41 -0700569 } else {
NeilBrown5cf00fc2012-05-21 09:28:20 +1000570 while (sector >= geo->stride) {
571 sector -= geo->stride;
572 if (dev < geo->near_copies)
573 dev += geo->raid_disks - geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700574 else
NeilBrown5cf00fc2012-05-21 09:28:20 +1000575 dev -= geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700576 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000577 chunk = sector >> geo->chunk_shift;
NeilBrownc93983b2006-06-26 00:27:41 -0700578 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000579 vchunk = chunk * geo->raid_disks + dev;
580 sector_div(vchunk, geo->near_copies);
581 return (vchunk << geo->chunk_shift) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
584/**
585 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
586 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200587 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 * @biovec: the request that could be merged to it.
589 *
590 * Return amount of bytes we can accept at this offset
NeilBrown050b6612012-03-19 12:46:39 +1100591 * This requires checking for end-of-chunk if near_copies != raid_disks,
592 * and for subordinate merge_bvec_fns if merge_check_needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200594static int raid10_mergeable_bvec(struct request_queue *q,
595 struct bvec_merge_data *bvm,
596 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
NeilBrownfd01b882011-10-11 16:47:53 +1100598 struct mddev *mddev = q->queuedata;
NeilBrown050b6612012-03-19 12:46:39 +1100599 struct r10conf *conf = mddev->private;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200600 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +1000602 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200603 unsigned int bio_sectors = bvm->bi_size >> 9;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000604 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
NeilBrown5cf00fc2012-05-21 09:28:20 +1000606 if (geo->near_copies < geo->raid_disks) {
NeilBrown050b6612012-03-19 12:46:39 +1100607 max = (chunk_sectors - ((sector & (chunk_sectors - 1))
608 + bio_sectors)) << 9;
609 if (max < 0)
610 /* bio_add cannot handle a negative return */
611 max = 0;
612 if (max <= biovec->bv_len && bio_sectors == 0)
613 return biovec->bv_len;
614 } else
615 max = biovec->bv_len;
616
617 if (mddev->merge_check_needed) {
618 struct r10bio r10_bio;
619 int s;
620 r10_bio.sector = sector;
621 raid10_find_phys(conf, &r10_bio);
622 rcu_read_lock();
623 for (s = 0; s < conf->copies; s++) {
624 int disk = r10_bio.devs[s].devnum;
625 struct md_rdev *rdev = rcu_dereference(
626 conf->mirrors[disk].rdev);
627 if (rdev && !test_bit(Faulty, &rdev->flags)) {
628 struct request_queue *q =
629 bdev_get_queue(rdev->bdev);
630 if (q->merge_bvec_fn) {
631 bvm->bi_sector = r10_bio.devs[s].addr
632 + rdev->data_offset;
633 bvm->bi_bdev = rdev->bdev;
634 max = min(max, q->merge_bvec_fn(
635 q, bvm, biovec));
636 }
637 }
638 rdev = rcu_dereference(conf->mirrors[disk].replacement);
639 if (rdev && !test_bit(Faulty, &rdev->flags)) {
640 struct request_queue *q =
641 bdev_get_queue(rdev->bdev);
642 if (q->merge_bvec_fn) {
643 bvm->bi_sector = r10_bio.devs[s].addr
644 + rdev->data_offset;
645 bvm->bi_bdev = rdev->bdev;
646 max = min(max, q->merge_bvec_fn(
647 q, bvm, biovec));
648 }
649 }
650 }
651 rcu_read_unlock();
652 }
653 return max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656/*
657 * This routine returns the disk from which the requested read should
658 * be done. There is a per-array 'next expected sequential IO' sector
659 * number - if this matches on the next IO then we use the last disk.
660 * There is also a per-disk 'last know head position' sector that is
661 * maintained from IRQ contexts, both the normal and the resync IO
662 * completion handlers update this position correctly. If there is no
663 * perfect sequential match then we pick the disk whose head is closest.
664 *
665 * If there are 2 mirrors in the same 2 devices, performance degrades
666 * because position is mirror, not device based.
667 *
668 * The rdev for the device selected will have nr_pending incremented.
669 */
670
671/*
672 * FIXME: possibly should rethink readbalancing and do it differently
673 * depending on near_copies / far_copies geometry.
674 */
NeilBrown96c3fd12011-12-23 10:17:54 +1100675static struct md_rdev *read_balance(struct r10conf *conf,
676 struct r10bio *r10_bio,
677 int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000679 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000680 int disk, slot;
NeilBrown856e08e2011-07-28 11:39:23 +1000681 int sectors = r10_bio->sectors;
682 int best_good_sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000683 sector_t new_distance, best_dist;
NeilBrownabbf0982011-12-23 10:17:54 +1100684 struct md_rdev *rdev, *best_rdev;
NeilBrown56d99122011-05-11 14:27:03 +1000685 int do_balance;
686 int best_slot;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000687 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 raid10_find_phys(conf, r10_bio);
690 rcu_read_lock();
NeilBrown56d99122011-05-11 14:27:03 +1000691retry:
NeilBrown856e08e2011-07-28 11:39:23 +1000692 sectors = r10_bio->sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000693 best_slot = -1;
NeilBrownabbf0982011-12-23 10:17:54 +1100694 best_rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000695 best_dist = MaxSector;
NeilBrown856e08e2011-07-28 11:39:23 +1000696 best_good_sectors = 0;
NeilBrown56d99122011-05-11 14:27:03 +1000697 do_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 /*
699 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800700 * device if no resync is going on (recovery is ok), or below
701 * the resync window. We take the first readable disk when
702 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 */
704 if (conf->mddev->recovery_cp < MaxSector
NeilBrown56d99122011-05-11 14:27:03 +1000705 && (this_sector + sectors >= conf->next_resync))
706 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
NeilBrown56d99122011-05-11 14:27:03 +1000708 for (slot = 0; slot < conf->copies ; slot++) {
NeilBrown856e08e2011-07-28 11:39:23 +1000709 sector_t first_bad;
710 int bad_sectors;
711 sector_t dev_sector;
712
NeilBrown56d99122011-05-11 14:27:03 +1000713 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000715 disk = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100716 rdev = rcu_dereference(conf->mirrors[disk].replacement);
717 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
NeilBrown050b6612012-03-19 12:46:39 +1100718 test_bit(Unmerged, &rdev->flags) ||
NeilBrownabbf0982011-12-23 10:17:54 +1100719 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
720 rdev = rcu_dereference(conf->mirrors[disk].rdev);
NeilBrown050b6612012-03-19 12:46:39 +1100721 if (rdev == NULL ||
722 test_bit(Faulty, &rdev->flags) ||
723 test_bit(Unmerged, &rdev->flags))
NeilBrownabbf0982011-12-23 10:17:54 +1100724 continue;
725 if (!test_bit(In_sync, &rdev->flags) &&
726 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
NeilBrown56d99122011-05-11 14:27:03 +1000727 continue;
728
NeilBrown856e08e2011-07-28 11:39:23 +1000729 dev_sector = r10_bio->devs[slot].addr;
730 if (is_badblock(rdev, dev_sector, sectors,
731 &first_bad, &bad_sectors)) {
732 if (best_dist < MaxSector)
733 /* Already have a better slot */
734 continue;
735 if (first_bad <= dev_sector) {
736 /* Cannot read here. If this is the
737 * 'primary' device, then we must not read
738 * beyond 'bad_sectors' from another device.
739 */
740 bad_sectors -= (dev_sector - first_bad);
741 if (!do_balance && sectors > bad_sectors)
742 sectors = bad_sectors;
743 if (best_good_sectors > sectors)
744 best_good_sectors = sectors;
745 } else {
746 sector_t good_sectors =
747 first_bad - dev_sector;
748 if (good_sectors > best_good_sectors) {
749 best_good_sectors = good_sectors;
750 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100751 best_rdev = rdev;
NeilBrown856e08e2011-07-28 11:39:23 +1000752 }
753 if (!do_balance)
754 /* Must read from here */
755 break;
756 }
757 continue;
758 } else
759 best_good_sectors = sectors;
760
NeilBrown56d99122011-05-11 14:27:03 +1000761 if (!do_balance)
762 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
NeilBrown22dfdf52005-11-28 13:44:09 -0800764 /* This optimisation is debatable, and completely destroys
765 * sequential read speed for 'far copies' arrays. So only
766 * keep it for 'near' arrays, and review those later.
767 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000768 if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 break;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800770
771 /* for far > 1 always use the lowest address */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000772 if (geo->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000773 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800774 else
NeilBrown56d99122011-05-11 14:27:03 +1000775 new_distance = abs(r10_bio->devs[slot].addr -
776 conf->mirrors[disk].head_position);
777 if (new_distance < best_dist) {
778 best_dist = new_distance;
779 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100780 best_rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782 }
NeilBrownabbf0982011-12-23 10:17:54 +1100783 if (slot >= conf->copies) {
NeilBrown56d99122011-05-11 14:27:03 +1000784 slot = best_slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100785 rdev = best_rdev;
786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
NeilBrown56d99122011-05-11 14:27:03 +1000788 if (slot >= 0) {
NeilBrown56d99122011-05-11 14:27:03 +1000789 atomic_inc(&rdev->nr_pending);
790 if (test_bit(Faulty, &rdev->flags)) {
791 /* Cannot risk returning a device that failed
792 * before we inc'ed nr_pending
793 */
794 rdev_dec_pending(rdev, conf->mddev);
795 goto retry;
796 }
797 r10_bio->read_slot = slot;
798 } else
NeilBrown96c3fd12011-12-23 10:17:54 +1100799 rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 rcu_read_unlock();
NeilBrown856e08e2011-07-28 11:39:23 +1000801 *max_sectors = best_good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
NeilBrown96c3fd12011-12-23 10:17:54 +1100803 return rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
NeilBrown0d129222006-10-03 01:15:54 -0700806static int raid10_congested(void *data, int bits)
807{
NeilBrownfd01b882011-10-11 16:47:53 +1100808 struct mddev *mddev = data;
NeilBrowne879a872011-10-11 16:49:02 +1100809 struct r10conf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700810 int i, ret = 0;
811
NeilBrown34db0cd2011-10-11 16:50:01 +1100812 if ((bits & (1 << BDI_async_congested)) &&
813 conf->pending_count >= max_queued_requests)
814 return 1;
815
NeilBrown3fa841d2009-09-23 18:10:29 +1000816 if (mddev_congested(mddev, bits))
817 return 1;
NeilBrown0d129222006-10-03 01:15:54 -0700818 rcu_read_lock();
NeilBrown5cf00fc2012-05-21 09:28:20 +1000819 for (i = 0; i < conf->geo.raid_disks && ret == 0; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100820 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700821 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200822 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700823
824 ret |= bdi_congested(&q->backing_dev_info, bits);
825 }
826 }
827 rcu_read_unlock();
828 return ret;
829}
830
NeilBrowne879a872011-10-11 16:49:02 +1100831static void flush_pending_writes(struct r10conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800832{
833 /* Any writes that have been queued but are awaiting
834 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800835 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800836 spin_lock_irq(&conf->device_lock);
837
838 if (conf->pending_bio_list.head) {
839 struct bio *bio;
840 bio = bio_list_get(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +1100841 conf->pending_count = 0;
NeilBrowna35e63e2008-03-04 14:29:29 -0800842 spin_unlock_irq(&conf->device_lock);
843 /* flush any pending bitmap writes to disk
844 * before proceeding w/ I/O */
845 bitmap_unplug(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100846 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800847
848 while (bio) { /* submit pending writes */
849 struct bio *next = bio->bi_next;
850 bio->bi_next = NULL;
851 generic_make_request(bio);
852 bio = next;
853 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800854 } else
855 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800856}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100857
NeilBrown0a27ec92006-01-06 00:20:13 -0800858/* Barriers....
859 * Sometimes we need to suspend IO while we do something else,
860 * either some resync/recovery, or reconfigure the array.
861 * To do this we raise a 'barrier'.
862 * The 'barrier' is a counter that can be raised multiple times
863 * to count how many activities are happening which preclude
864 * normal IO.
865 * We can only raise the barrier if there is no pending IO.
866 * i.e. if nr_pending == 0.
867 * We choose only to raise the barrier if no-one is waiting for the
868 * barrier to go down. This means that as soon as an IO request
869 * is ready, no other operations which require a barrier will start
870 * until the IO request has had a chance.
871 *
872 * So: regular IO calls 'wait_barrier'. When that returns there
873 * is no backgroup IO happening, It must arrange to call
874 * allow_barrier when it has finished its IO.
875 * backgroup IO calls must call raise_barrier. Once that returns
876 * there is no normal IO happeing. It must arrange to call
877 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
NeilBrowne879a872011-10-11 16:49:02 +1100880static void raise_barrier(struct r10conf *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
NeilBrown6cce3b22006-01-06 00:20:16 -0800882 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
NeilBrown6cce3b22006-01-06 00:20:16 -0800885 /* Wait until no block IO is waiting (unless 'force') */
886 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrownc3b328a2011-04-18 18:25:43 +1000887 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800888
889 /* block any new IO from starting */
890 conf->barrier++;
891
NeilBrownc3b328a2011-04-18 18:25:43 +1000892 /* Now wait for all pending IO to complete */
NeilBrown0a27ec92006-01-06 00:20:13 -0800893 wait_event_lock_irq(conf->wait_barrier,
894 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
NeilBrownc3b328a2011-04-18 18:25:43 +1000895 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 spin_unlock_irq(&conf->resync_lock);
898}
899
NeilBrowne879a872011-10-11 16:49:02 +1100900static void lower_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800901{
902 unsigned long flags;
903 spin_lock_irqsave(&conf->resync_lock, flags);
904 conf->barrier--;
905 spin_unlock_irqrestore(&conf->resync_lock, flags);
906 wake_up(&conf->wait_barrier);
907}
908
NeilBrowne879a872011-10-11 16:49:02 +1100909static void wait_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800910{
911 spin_lock_irq(&conf->resync_lock);
912 if (conf->barrier) {
913 conf->nr_waiting++;
NeilBrownd6b42dc2012-03-19 12:46:38 +1100914 /* Wait for the barrier to drop.
915 * However if there are already pending
916 * requests (preventing the barrier from
917 * rising completely), and the
918 * pre-process bio queue isn't empty,
919 * then don't wait, as we need to empty
920 * that queue to get the nr_pending
921 * count down.
922 */
923 wait_event_lock_irq(conf->wait_barrier,
924 !conf->barrier ||
925 (conf->nr_pending &&
926 current->bio_list &&
927 !bio_list_empty(current->bio_list)),
NeilBrown0a27ec92006-01-06 00:20:13 -0800928 conf->resync_lock,
NeilBrownd6b42dc2012-03-19 12:46:38 +1100929 );
NeilBrown0a27ec92006-01-06 00:20:13 -0800930 conf->nr_waiting--;
931 }
932 conf->nr_pending++;
933 spin_unlock_irq(&conf->resync_lock);
934}
935
NeilBrowne879a872011-10-11 16:49:02 +1100936static void allow_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800937{
938 unsigned long flags;
939 spin_lock_irqsave(&conf->resync_lock, flags);
940 conf->nr_pending--;
941 spin_unlock_irqrestore(&conf->resync_lock, flags);
942 wake_up(&conf->wait_barrier);
943}
944
NeilBrowne879a872011-10-11 16:49:02 +1100945static void freeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -0800946{
947 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800948 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800949 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800950 * wait until nr_pending match nr_queued+1
951 * This is called in the context of one normal IO request
952 * that has failed. Thus any sync request that might be pending
953 * will be blocked by nr_pending, and we need to wait for
954 * pending IO requests to complete or be queued for re-try.
955 * Thus the number queued (nr_queued) plus this request (1)
956 * must match the number of pending IOs (nr_pending) before
957 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -0800958 */
959 spin_lock_irq(&conf->resync_lock);
960 conf->barrier++;
961 conf->nr_waiting++;
962 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -0800963 conf->nr_pending == conf->nr_queued+1,
NeilBrown4443ae12006-01-06 00:20:28 -0800964 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000965 flush_pending_writes(conf));
966
NeilBrown4443ae12006-01-06 00:20:28 -0800967 spin_unlock_irq(&conf->resync_lock);
968}
969
NeilBrowne879a872011-10-11 16:49:02 +1100970static void unfreeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -0800971{
972 /* reverse the effect of the freeze */
973 spin_lock_irq(&conf->resync_lock);
974 conf->barrier--;
975 conf->nr_waiting--;
976 wake_up(&conf->wait_barrier);
977 spin_unlock_irq(&conf->resync_lock);
978}
979
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -0700980static void make_request(struct mddev *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
NeilBrowne879a872011-10-11 16:49:02 +1100982 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100983 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 struct bio *read_bio;
985 int i;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000986 sector_t chunk_mask = conf->geo.chunk_mask;
987 int chunk_sects = chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +0100988 const int rw = bio_data_dir(bio);
NeilBrown2c7d46e2010-08-18 16:16:05 +1000989 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
Tejun Heoe9c74692010-09-03 11:56:18 +0200990 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
NeilBrown6cce3b22006-01-06 00:20:16 -0800991 unsigned long flags;
NeilBrown3cb03002011-10-11 16:45:26 +1100992 struct md_rdev *blocked_rdev;
NeilBrownc3b328a2011-04-18 18:25:43 +1000993 int plugged;
NeilBrownd4432c22011-07-28 11:39:24 +1000994 int sectors_handled;
995 int max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Tejun Heoe9c74692010-09-03 11:56:18 +0200997 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
998 md_flush_request(mddev, bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200999 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07001000 }
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 /* If this request crosses a chunk boundary, we need to
1003 * split it. This will only happen for 1 PAGE (or less) requests.
1004 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10001005 if (unlikely((bio->bi_sector & chunk_mask) + (bio->bi_size >> 9)
1006 > chunk_sects
1007 && conf->geo.near_copies < conf->geo.raid_disks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 struct bio_pair *bp;
1009 /* Sanity check -- queue functions should prevent this happening */
1010 if (bio->bi_vcnt != 1 ||
1011 bio->bi_idx != 0)
1012 goto bad_map;
1013 /* This is a one page bio that upper layers
1014 * refuse to split for us, so we need to split it.
1015 */
Denis ChengRq6feef532008-10-09 08:57:05 +02001016 bp = bio_split(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
NeilBrown51e9ac72010-08-07 21:17:00 +10001018
1019 /* Each of these 'make_request' calls will call 'wait_barrier'.
1020 * If the first succeeds but the second blocks due to the resync
1021 * thread raising the barrier, we will deadlock because the
1022 * IO to the underlying device will be queued in generic_make_request
1023 * and will never complete, so will never reduce nr_pending.
1024 * So increment nr_waiting here so no new raise_barriers will
1025 * succeed, and so the second wait_barrier cannot block.
1026 */
1027 spin_lock_irq(&conf->resync_lock);
1028 conf->nr_waiting++;
1029 spin_unlock_irq(&conf->resync_lock);
1030
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001031 make_request(mddev, &bp->bio1);
1032 make_request(mddev, &bp->bio2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
NeilBrown51e9ac72010-08-07 21:17:00 +10001034 spin_lock_irq(&conf->resync_lock);
1035 conf->nr_waiting--;
1036 wake_up(&conf->wait_barrier);
1037 spin_unlock_irq(&conf->resync_lock);
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 bio_pair_release(bp);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001040 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 bad_map:
NeilBrown128595e2010-05-03 14:47:14 +10001042 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
1043 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
1045
NeilBrown6712ecf2007-09-27 12:47:43 +02001046 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001047 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049
NeilBrown3d310eb2005-06-21 17:17:26 -07001050 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -07001051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 /*
1053 * Register the new request and wait if the reconstruction
1054 * thread has put up a bar for new requests.
1055 * Continue immediately if no resync is active currently.
1056 */
NeilBrown0a27ec92006-01-06 00:20:13 -08001057 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1060
1061 r10_bio->master_bio = bio;
1062 r10_bio->sectors = bio->bi_size >> 9;
1063
1064 r10_bio->mddev = mddev;
1065 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b22006-01-06 00:20:16 -08001066 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
NeilBrown856e08e2011-07-28 11:39:23 +10001068 /* We might need to issue multiple reads to different
1069 * devices if there are bad blocks around, so we keep
1070 * track of the number of reads in bio->bi_phys_segments.
1071 * If this is 0, there is only one r10_bio and no locking
1072 * will be needed when the request completes. If it is
1073 * non-zero, then it is the number of not-completed requests.
1074 */
1075 bio->bi_phys_segments = 0;
1076 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
1077
Jens Axboea3623572005-11-01 09:26:16 +01001078 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /*
1080 * read balancing logic:
1081 */
NeilBrown96c3fd12011-12-23 10:17:54 +11001082 struct md_rdev *rdev;
NeilBrown856e08e2011-07-28 11:39:23 +10001083 int slot;
1084
1085read_again:
NeilBrown96c3fd12011-12-23 10:17:54 +11001086 rdev = read_balance(conf, r10_bio, &max_sectors);
1087 if (!rdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 raid_end_bio_io(r10_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001089 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
NeilBrown96c3fd12011-12-23 10:17:54 +11001091 slot = r10_bio->read_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
NeilBrowna167f662010-10-26 18:31:13 +11001093 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrown856e08e2011-07-28 11:39:23 +10001094 md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
1095 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 r10_bio->devs[slot].bio = read_bio;
NeilBrownabbf0982011-12-23 10:17:54 +11001098 r10_bio->devs[slot].rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 read_bio->bi_sector = r10_bio->devs[slot].addr +
NeilBrown96c3fd12011-12-23 10:17:54 +11001101 rdev->data_offset;
1102 read_bio->bi_bdev = rdev->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 read_bio->bi_end_io = raid10_end_read_request;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001104 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 read_bio->bi_private = r10_bio;
1106
NeilBrown856e08e2011-07-28 11:39:23 +10001107 if (max_sectors < r10_bio->sectors) {
1108 /* Could not read all from this device, so we will
1109 * need another r10_bio.
1110 */
NeilBrown856e08e2011-07-28 11:39:23 +10001111 sectors_handled = (r10_bio->sectors + max_sectors
1112 - bio->bi_sector);
1113 r10_bio->sectors = max_sectors;
1114 spin_lock_irq(&conf->device_lock);
1115 if (bio->bi_phys_segments == 0)
1116 bio->bi_phys_segments = 2;
1117 else
1118 bio->bi_phys_segments++;
1119 spin_unlock(&conf->device_lock);
1120 /* Cannot call generic_make_request directly
1121 * as that will be queued in __generic_make_request
1122 * and subsequent mempool_alloc might block
1123 * waiting for it. so hand bio over to raid10d.
1124 */
1125 reschedule_retry(r10_bio);
1126
1127 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1128
1129 r10_bio->master_bio = bio;
1130 r10_bio->sectors = ((bio->bi_size >> 9)
1131 - sectors_handled);
1132 r10_bio->state = 0;
1133 r10_bio->mddev = mddev;
1134 r10_bio->sector = bio->bi_sector + sectors_handled;
1135 goto read_again;
1136 } else
1137 generic_make_request(read_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001138 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140
1141 /*
1142 * WRITE:
1143 */
NeilBrown34db0cd2011-10-11 16:50:01 +11001144 if (conf->pending_count >= max_queued_requests) {
1145 md_wakeup_thread(mddev->thread);
1146 wait_event(conf->wait_barrier,
1147 conf->pending_count < max_queued_requests);
1148 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001149 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 * inc refcount on their rdev. Record them by setting
1151 * bios[x] to bio
NeilBrownd4432c22011-07-28 11:39:24 +10001152 * If there are known/acknowledged bad blocks on any device
1153 * on which we have seen a write error, we want to avoid
1154 * writing to those blocks. This potentially requires several
1155 * writes to write around the bad blocks. Each set of writes
1156 * gets its own r10_bio with a set of bios attached. The number
1157 * of r10_bios is recored in bio->bi_phys_segments just as with
1158 * the read case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001160 plugged = mddev_check_plugged(mddev);
1161
NeilBrown69335ef2011-12-23 10:17:54 +11001162 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 raid10_find_phys(conf, r10_bio);
NeilBrownd4432c22011-07-28 11:39:24 +10001164retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -07001165 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 rcu_read_lock();
NeilBrownd4432c22011-07-28 11:39:24 +10001167 max_sectors = r10_bio->sectors;
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 for (i = 0; i < conf->copies; i++) {
1170 int d = r10_bio->devs[i].devnum;
NeilBrown3cb03002011-10-11 16:45:26 +11001171 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown475b0322011-12-23 10:17:55 +11001172 struct md_rdev *rrdev = rcu_dereference(
1173 conf->mirrors[d].replacement);
NeilBrown4ca40c22011-12-23 10:17:55 +11001174 if (rdev == rrdev)
1175 rrdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001176 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1177 atomic_inc(&rdev->nr_pending);
1178 blocked_rdev = rdev;
1179 break;
1180 }
NeilBrown475b0322011-12-23 10:17:55 +11001181 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1182 atomic_inc(&rrdev->nr_pending);
1183 blocked_rdev = rrdev;
1184 break;
1185 }
NeilBrown050b6612012-03-19 12:46:39 +11001186 if (rrdev && (test_bit(Faulty, &rrdev->flags)
1187 || test_bit(Unmerged, &rrdev->flags)))
NeilBrown475b0322011-12-23 10:17:55 +11001188 rrdev = NULL;
1189
NeilBrownd4432c22011-07-28 11:39:24 +10001190 r10_bio->devs[i].bio = NULL;
NeilBrown475b0322011-12-23 10:17:55 +11001191 r10_bio->devs[i].repl_bio = NULL;
NeilBrown050b6612012-03-19 12:46:39 +11001192 if (!rdev || test_bit(Faulty, &rdev->flags) ||
1193 test_bit(Unmerged, &rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001194 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrownd4432c22011-07-28 11:39:24 +10001195 continue;
NeilBrown6cce3b22006-01-06 00:20:16 -08001196 }
NeilBrownd4432c22011-07-28 11:39:24 +10001197 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1198 sector_t first_bad;
1199 sector_t dev_sector = r10_bio->devs[i].addr;
1200 int bad_sectors;
1201 int is_bad;
1202
1203 is_bad = is_badblock(rdev, dev_sector,
1204 max_sectors,
1205 &first_bad, &bad_sectors);
1206 if (is_bad < 0) {
1207 /* Mustn't write here until the bad block
1208 * is acknowledged
1209 */
1210 atomic_inc(&rdev->nr_pending);
1211 set_bit(BlockedBadBlocks, &rdev->flags);
1212 blocked_rdev = rdev;
1213 break;
1214 }
1215 if (is_bad && first_bad <= dev_sector) {
1216 /* Cannot write here at all */
1217 bad_sectors -= (dev_sector - first_bad);
1218 if (bad_sectors < max_sectors)
1219 /* Mustn't write more than bad_sectors
1220 * to other devices yet
1221 */
1222 max_sectors = bad_sectors;
1223 /* We don't set R10BIO_Degraded as that
1224 * only applies if the disk is missing,
1225 * so it might be re-added, and we want to
1226 * know to recover this chunk.
1227 * In this case the device is here, and the
1228 * fact that this chunk is not in-sync is
1229 * recorded in the bad block log.
1230 */
1231 continue;
1232 }
1233 if (is_bad) {
1234 int good_sectors = first_bad - dev_sector;
1235 if (good_sectors < max_sectors)
1236 max_sectors = good_sectors;
1237 }
1238 }
1239 r10_bio->devs[i].bio = bio;
1240 atomic_inc(&rdev->nr_pending);
NeilBrown475b0322011-12-23 10:17:55 +11001241 if (rrdev) {
1242 r10_bio->devs[i].repl_bio = bio;
1243 atomic_inc(&rrdev->nr_pending);
1244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
1246 rcu_read_unlock();
1247
Dan Williams6bfe0b42008-04-30 00:52:32 -07001248 if (unlikely(blocked_rdev)) {
1249 /* Have to wait for this device to get unblocked, then retry */
1250 int j;
1251 int d;
1252
NeilBrown475b0322011-12-23 10:17:55 +11001253 for (j = 0; j < i; j++) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07001254 if (r10_bio->devs[j].bio) {
1255 d = r10_bio->devs[j].devnum;
1256 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1257 }
NeilBrown475b0322011-12-23 10:17:55 +11001258 if (r10_bio->devs[j].repl_bio) {
NeilBrown4ca40c22011-12-23 10:17:55 +11001259 struct md_rdev *rdev;
NeilBrown475b0322011-12-23 10:17:55 +11001260 d = r10_bio->devs[j].devnum;
NeilBrown4ca40c22011-12-23 10:17:55 +11001261 rdev = conf->mirrors[d].replacement;
1262 if (!rdev) {
1263 /* Race with remove_disk */
1264 smp_mb();
1265 rdev = conf->mirrors[d].rdev;
1266 }
1267 rdev_dec_pending(rdev, mddev);
NeilBrown475b0322011-12-23 10:17:55 +11001268 }
1269 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001270 allow_barrier(conf);
1271 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1272 wait_barrier(conf);
1273 goto retry_write;
1274 }
1275
NeilBrownd4432c22011-07-28 11:39:24 +10001276 if (max_sectors < r10_bio->sectors) {
1277 /* We are splitting this into multiple parts, so
1278 * we need to prepare for allocating another r10_bio.
1279 */
1280 r10_bio->sectors = max_sectors;
1281 spin_lock_irq(&conf->device_lock);
1282 if (bio->bi_phys_segments == 0)
1283 bio->bi_phys_segments = 2;
1284 else
1285 bio->bi_phys_segments++;
1286 spin_unlock_irq(&conf->device_lock);
1287 }
1288 sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
1289
NeilBrown4e780642010-10-19 12:54:01 +11001290 atomic_set(&r10_bio->remaining, 1);
NeilBrownd4432c22011-07-28 11:39:24 +10001291 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07001292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 for (i = 0; i < conf->copies; i++) {
1294 struct bio *mbio;
1295 int d = r10_bio->devs[i].devnum;
1296 if (!r10_bio->devs[i].bio)
1297 continue;
1298
NeilBrowna167f662010-10-26 18:31:13 +11001299 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrownd4432c22011-07-28 11:39:24 +10001300 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1301 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 r10_bio->devs[i].bio = mbio;
1303
NeilBrownd4432c22011-07-28 11:39:24 +10001304 mbio->bi_sector = (r10_bio->devs[i].addr+
1305 conf->mirrors[d].rdev->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1307 mbio->bi_end_io = raid10_end_write_request;
Tejun Heoe9c74692010-09-03 11:56:18 +02001308 mbio->bi_rw = WRITE | do_sync | do_fua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 mbio->bi_private = r10_bio;
1310
1311 atomic_inc(&r10_bio->remaining);
NeilBrown4e780642010-10-19 12:54:01 +11001312 spin_lock_irqsave(&conf->device_lock, flags);
1313 bio_list_add(&conf->pending_bio_list, mbio);
NeilBrown34db0cd2011-10-11 16:50:01 +11001314 conf->pending_count++;
NeilBrown4e780642010-10-19 12:54:01 +11001315 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown475b0322011-12-23 10:17:55 +11001316
1317 if (!r10_bio->devs[i].repl_bio)
1318 continue;
1319
1320 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1321 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1322 max_sectors);
1323 r10_bio->devs[i].repl_bio = mbio;
1324
NeilBrown4ca40c22011-12-23 10:17:55 +11001325 /* We are actively writing to the original device
1326 * so it cannot disappear, so the replacement cannot
1327 * become NULL here
1328 */
NeilBrown475b0322011-12-23 10:17:55 +11001329 mbio->bi_sector = (r10_bio->devs[i].addr+
1330 conf->mirrors[d].replacement->data_offset);
1331 mbio->bi_bdev = conf->mirrors[d].replacement->bdev;
1332 mbio->bi_end_io = raid10_end_write_request;
1333 mbio->bi_rw = WRITE | do_sync | do_fua;
1334 mbio->bi_private = r10_bio;
1335
1336 atomic_inc(&r10_bio->remaining);
1337 spin_lock_irqsave(&conf->device_lock, flags);
1338 bio_list_add(&conf->pending_bio_list, mbio);
1339 conf->pending_count++;
1340 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342
NeilBrown079fa162011-09-10 17:21:23 +10001343 /* Don't remove the bias on 'remaining' (one_write_done) until
1344 * after checking if we need to go around again.
1345 */
NeilBrowna35e63e2008-03-04 14:29:29 -08001346
NeilBrownd4432c22011-07-28 11:39:24 +10001347 if (sectors_handled < (bio->bi_size >> 9)) {
NeilBrown079fa162011-09-10 17:21:23 +10001348 one_write_done(r10_bio);
NeilBrown5e570282011-07-28 11:39:25 +10001349 /* We need another r10_bio. It has already been counted
NeilBrownd4432c22011-07-28 11:39:24 +10001350 * in bio->bi_phys_segments.
1351 */
1352 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1353
1354 r10_bio->master_bio = bio;
1355 r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1356
1357 r10_bio->mddev = mddev;
1358 r10_bio->sector = bio->bi_sector + sectors_handled;
1359 r10_bio->state = 0;
1360 goto retry_write;
1361 }
NeilBrown079fa162011-09-10 17:21:23 +10001362 one_write_done(r10_bio);
1363
1364 /* In case raid10d snuck in to freeze_array */
1365 wake_up(&conf->wait_barrier);
NeilBrownd4432c22011-07-28 11:39:24 +10001366
NeilBrownc3b328a2011-04-18 18:25:43 +10001367 if (do_sync || !mddev->bitmap || !plugged)
Lars Ellenberge3881a62007-01-10 23:15:37 -08001368 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369}
1370
NeilBrownfd01b882011-10-11 16:47:53 +11001371static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
NeilBrowne879a872011-10-11 16:49:02 +11001373 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 int i;
1375
NeilBrown5cf00fc2012-05-21 09:28:20 +10001376 if (conf->geo.near_copies < conf->geo.raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +10001377 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
NeilBrown5cf00fc2012-05-21 09:28:20 +10001378 if (conf->geo.near_copies > 1)
1379 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1380 if (conf->geo.far_copies > 1) {
1381 if (conf->geo.far_offset)
1382 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001383 else
NeilBrown5cf00fc2012-05-21 09:28:20 +10001384 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001385 }
NeilBrown5cf00fc2012-05-21 09:28:20 +10001386 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1387 conf->geo.raid_disks - mddev->degraded);
1388 for (i = 0; i < conf->geo.raid_disks; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 seq_printf(seq, "%s",
1390 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001391 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 seq_printf(seq, "]");
1393}
1394
NeilBrown700c7212011-07-27 11:00:36 +10001395/* check if there are enough drives for
1396 * every block to appear on atleast one.
1397 * Don't consider the device numbered 'ignore'
1398 * as we might be about to remove it.
1399 */
NeilBrowne879a872011-10-11 16:49:02 +11001400static int enough(struct r10conf *conf, int ignore)
NeilBrown700c7212011-07-27 11:00:36 +10001401{
1402 int first = 0;
1403
1404 do {
1405 int n = conf->copies;
1406 int cnt = 0;
1407 while (n--) {
1408 if (conf->mirrors[first].rdev &&
1409 first != ignore)
1410 cnt++;
NeilBrown5cf00fc2012-05-21 09:28:20 +10001411 first = (first+1) % conf->geo.raid_disks;
NeilBrown700c7212011-07-27 11:00:36 +10001412 }
1413 if (cnt == 0)
1414 return 0;
1415 } while (first != 0);
1416 return 1;
1417}
1418
NeilBrownfd01b882011-10-11 16:47:53 +11001419static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
1421 char b[BDEVNAME_SIZE];
NeilBrowne879a872011-10-11 16:49:02 +11001422 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 /*
1425 * If it is not operational, then we have already marked it as dead
1426 * else if it is the last working disks, ignore the error, let the
1427 * next level up know.
1428 * else mark the drive as failed
1429 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001430 if (test_bit(In_sync, &rdev->flags)
NeilBrown700c7212011-07-27 11:00:36 +10001431 && !enough(conf, rdev->raid_disk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 /*
1433 * Don't fail the drive, just return an IO error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 */
1435 return;
NeilBrownc04be0a2006-10-03 01:15:53 -07001436 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1437 unsigned long flags;
1438 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001440 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 /*
1442 * if recovery is running, make sure it aborts.
1443 */
NeilBrowndfc70642008-05-23 13:04:39 -07001444 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
NeilBrownde393cd2011-07-28 11:31:48 +10001446 set_bit(Blocked, &rdev->flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001447 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -07001448 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Joe Perches067032b2011-01-14 09:14:33 +11001449 printk(KERN_ALERT
1450 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1451 "md/raid10:%s: Operation continuing on %d devices.\n",
NeilBrown128595e2010-05-03 14:47:14 +10001452 mdname(mddev), bdevname(rdev->bdev, b),
NeilBrown5cf00fc2012-05-21 09:28:20 +10001453 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455
NeilBrowne879a872011-10-11 16:49:02 +11001456static void print_conf(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
1458 int i;
NeilBrown0f6d02d2011-10-11 16:48:46 +11001459 struct mirror_info *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
NeilBrown128595e2010-05-03 14:47:14 +10001461 printk(KERN_DEBUG "RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (!conf) {
NeilBrown128595e2010-05-03 14:47:14 +10001463 printk(KERN_DEBUG "(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return;
1465 }
NeilBrown5cf00fc2012-05-21 09:28:20 +10001466 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
1467 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
NeilBrown5cf00fc2012-05-21 09:28:20 +10001469 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 char b[BDEVNAME_SIZE];
1471 tmp = conf->mirrors + i;
1472 if (tmp->rdev)
NeilBrown128595e2010-05-03 14:47:14 +10001473 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001474 i, !test_bit(In_sync, &tmp->rdev->flags),
1475 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 bdevname(tmp->rdev->bdev,b));
1477 }
1478}
1479
NeilBrowne879a872011-10-11 16:49:02 +11001480static void close_sync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
NeilBrown0a27ec92006-01-06 00:20:13 -08001482 wait_barrier(conf);
1483 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 mempool_destroy(conf->r10buf_pool);
1486 conf->r10buf_pool = NULL;
1487}
1488
NeilBrownfd01b882011-10-11 16:47:53 +11001489static int raid10_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
1491 int i;
NeilBrowne879a872011-10-11 16:49:02 +11001492 struct r10conf *conf = mddev->private;
NeilBrown0f6d02d2011-10-11 16:48:46 +11001493 struct mirror_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10001494 int count = 0;
1495 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 /*
1498 * Find all non-in_sync disks within the RAID10 configuration
1499 * and mark them in_sync
1500 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10001501 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 tmp = conf->mirrors + i;
NeilBrown4ca40c22011-12-23 10:17:55 +11001503 if (tmp->replacement
1504 && tmp->replacement->recovery_offset == MaxSector
1505 && !test_bit(Faulty, &tmp->replacement->flags)
1506 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1507 /* Replacement has just become active */
1508 if (!tmp->rdev
1509 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1510 count++;
1511 if (tmp->rdev) {
1512 /* Replaced device not technically faulty,
1513 * but we need to be sure it gets removed
1514 * and never re-added.
1515 */
1516 set_bit(Faulty, &tmp->rdev->flags);
1517 sysfs_notify_dirent_safe(
1518 tmp->rdev->sysfs_state);
1519 }
1520 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1521 } else if (tmp->rdev
1522 && !test_bit(Faulty, &tmp->rdev->flags)
1523 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001524 count++;
Adrian Drzewieckie6ffbcb2010-08-18 11:49:02 +10001525 sysfs_notify_dirent(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
1527 }
NeilBrown6b965622010-08-18 11:56:59 +10001528 spin_lock_irqsave(&conf->device_lock, flags);
1529 mddev->degraded -= count;
1530 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001533 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
1536
NeilBrownfd01b882011-10-11 16:47:53 +11001537static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
NeilBrowne879a872011-10-11 16:49:02 +11001539 struct r10conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001540 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 int mirror;
Neil Brown6c2fce22008-06-28 08:31:31 +10001542 int first = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10001543 int last = conf->geo.raid_disks - 1;
NeilBrown050b6612012-03-19 12:46:39 +11001544 struct request_queue *q = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 if (mddev->recovery_cp < MaxSector)
1547 /* only hot-add to in-sync arrays, as recovery is
1548 * very different from resync
1549 */
Neil Brown199050e2008-06-28 08:31:33 +10001550 return -EBUSY;
NeilBrowndc10c642012-03-19 12:46:37 +11001551 if (rdev->saved_raid_disk < 0 && !enough(conf, -1))
Neil Brown199050e2008-06-28 08:31:33 +10001552 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
NeilBrowna53a6c82008-11-06 17:28:20 +11001554 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001555 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
NeilBrown050b6612012-03-19 12:46:39 +11001557 if (q->merge_bvec_fn) {
1558 set_bit(Unmerged, &rdev->flags);
1559 mddev->merge_check_needed = 1;
1560 }
1561
Namhyung Kim2c4193d2011-07-18 17:38:43 +10001562 if (rdev->saved_raid_disk >= first &&
NeilBrown6cce3b22006-01-06 00:20:16 -08001563 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1564 mirror = rdev->saved_raid_disk;
1565 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001566 mirror = first;
NeilBrown2bb77732011-07-27 11:00:36 +10001567 for ( ; mirror <= last ; mirror++) {
NeilBrown0f6d02d2011-10-11 16:48:46 +11001568 struct mirror_info *p = &conf->mirrors[mirror];
NeilBrown2bb77732011-07-27 11:00:36 +10001569 if (p->recovery_disabled == mddev->recovery_disabled)
1570 continue;
NeilBrownb7044d42011-12-23 10:17:56 +11001571 if (p->rdev) {
1572 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1573 p->replacement != NULL)
1574 continue;
1575 clear_bit(In_sync, &rdev->flags);
1576 set_bit(Replacement, &rdev->flags);
1577 rdev->raid_disk = mirror;
1578 err = 0;
1579 disk_stack_limits(mddev->gendisk, rdev->bdev,
1580 rdev->data_offset << 9);
NeilBrownb7044d42011-12-23 10:17:56 +11001581 conf->fullsync = 1;
1582 rcu_assign_pointer(p->replacement, rdev);
1583 break;
1584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
NeilBrown2bb77732011-07-27 11:00:36 +10001586 disk_stack_limits(mddev->gendisk, rdev->bdev,
1587 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
NeilBrown2bb77732011-07-27 11:00:36 +10001589 p->head_position = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11001590 p->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown2bb77732011-07-27 11:00:36 +10001591 rdev->raid_disk = mirror;
1592 err = 0;
1593 if (rdev->saved_raid_disk != mirror)
1594 conf->fullsync = 1;
1595 rcu_assign_pointer(p->rdev, rdev);
1596 break;
1597 }
NeilBrown050b6612012-03-19 12:46:39 +11001598 if (err == 0 && test_bit(Unmerged, &rdev->flags)) {
1599 /* Some requests might not have seen this new
1600 * merge_bvec_fn. We must wait for them to complete
1601 * before merging the device fully.
1602 * First we make sure any code which has tested
1603 * our function has submitted the request, then
1604 * we wait for all outstanding requests to complete.
1605 */
1606 synchronize_sched();
1607 raise_barrier(conf, 0);
1608 lower_barrier(conf);
1609 clear_bit(Unmerged, &rdev->flags);
1610 }
Andre Nollac5e7112009-08-03 10:59:47 +10001611 md_integrity_add_rdev(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001613 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
1615
NeilBrownb8321b62011-12-23 10:17:51 +11001616static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
NeilBrowne879a872011-10-11 16:49:02 +11001618 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11001620 int number = rdev->raid_disk;
NeilBrownc8ab9032011-12-23 10:17:54 +11001621 struct md_rdev **rdevp;
1622 struct mirror_info *p = conf->mirrors + number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 print_conf(conf);
NeilBrownc8ab9032011-12-23 10:17:54 +11001625 if (rdev == p->rdev)
1626 rdevp = &p->rdev;
1627 else if (rdev == p->replacement)
1628 rdevp = &p->replacement;
1629 else
1630 return 0;
1631
1632 if (test_bit(In_sync, &rdev->flags) ||
1633 atomic_read(&rdev->nr_pending)) {
1634 err = -EBUSY;
1635 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
NeilBrownc8ab9032011-12-23 10:17:54 +11001637 /* Only remove faulty devices if recovery
1638 * is not possible.
1639 */
1640 if (!test_bit(Faulty, &rdev->flags) &&
1641 mddev->recovery_disabled != p->recovery_disabled &&
NeilBrown4ca40c22011-12-23 10:17:55 +11001642 (!p->replacement || p->replacement == rdev) &&
NeilBrownc8ab9032011-12-23 10:17:54 +11001643 enough(conf, -1)) {
1644 err = -EBUSY;
1645 goto abort;
1646 }
1647 *rdevp = NULL;
1648 synchronize_rcu();
1649 if (atomic_read(&rdev->nr_pending)) {
1650 /* lost the race, try later */
1651 err = -EBUSY;
1652 *rdevp = rdev;
1653 goto abort;
NeilBrown4ca40c22011-12-23 10:17:55 +11001654 } else if (p->replacement) {
1655 /* We must have just cleared 'rdev' */
1656 p->rdev = p->replacement;
1657 clear_bit(Replacement, &p->replacement->flags);
1658 smp_mb(); /* Make sure other CPUs may see both as identical
1659 * but will never see neither -- if they are careful.
1660 */
1661 p->replacement = NULL;
1662 clear_bit(WantReplacement, &rdev->flags);
1663 } else
1664 /* We might have just remove the Replacement as faulty
1665 * Clear the flag just in case
1666 */
1667 clear_bit(WantReplacement, &rdev->flags);
1668
NeilBrownc8ab9032011-12-23 10:17:54 +11001669 err = md_integrity_register(mddev);
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671abort:
1672
1673 print_conf(conf);
1674 return err;
1675}
1676
1677
NeilBrown6712ecf2007-09-27 12:47:43 +02001678static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001680 struct r10bio *r10_bio = bio->bi_private;
NeilBrowne879a872011-10-11 16:49:02 +11001681 struct r10conf *conf = r10_bio->mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001682 int d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
NeilBrown69335ef2011-12-23 10:17:54 +11001684 d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001685
1686 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1687 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrowne684e412011-07-28 11:39:25 +10001688 else
1689 /* The write handler will notice the lack of
1690 * R10BIO_Uptodate and record any errors etc
1691 */
NeilBrown4dbcdc72006-01-06 00:20:52 -08001692 atomic_add(r10_bio->sectors,
1693 &conf->mirrors[d].rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 /* for reconstruct, we always reschedule after a read.
1696 * for resync, only after all reads
1697 */
NeilBrown73d5c382009-02-25 13:18:47 +11001698 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1700 atomic_dec_and_test(&r10_bio->remaining)) {
1701 /* we have read all the blocks,
1702 * do the comparison in process context in raid10d
1703 */
1704 reschedule_retry(r10_bio);
1705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
NeilBrown9f2c9d12011-10-11 16:48:43 +11001708static void end_sync_request(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001709{
NeilBrownfd01b882011-10-11 16:47:53 +11001710 struct mddev *mddev = r10_bio->mddev;
NeilBrown5e570282011-07-28 11:39:25 +10001711
1712 while (atomic_dec_and_test(&r10_bio->remaining)) {
1713 if (r10_bio->master_bio == NULL) {
1714 /* the primary of several recovery bios */
1715 sector_t s = r10_bio->sectors;
1716 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1717 test_bit(R10BIO_WriteError, &r10_bio->state))
1718 reschedule_retry(r10_bio);
1719 else
1720 put_buf(r10_bio);
1721 md_done_sync(mddev, s, 1);
1722 break;
1723 } else {
NeilBrown9f2c9d12011-10-11 16:48:43 +11001724 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
NeilBrown5e570282011-07-28 11:39:25 +10001725 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1726 test_bit(R10BIO_WriteError, &r10_bio->state))
1727 reschedule_retry(r10_bio);
1728 else
1729 put_buf(r10_bio);
1730 r10_bio = r10_bio2;
1731 }
1732 }
1733}
1734
NeilBrown6712ecf2007-09-27 12:47:43 +02001735static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
1737 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +11001738 struct r10bio *r10_bio = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11001739 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001740 struct r10conf *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001741 int d;
NeilBrown749c55e2011-07-28 11:39:24 +10001742 sector_t first_bad;
1743 int bad_sectors;
1744 int slot;
NeilBrown9ad1aef2011-12-23 10:17:55 +11001745 int repl;
NeilBrown4ca40c22011-12-23 10:17:55 +11001746 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
NeilBrown9ad1aef2011-12-23 10:17:55 +11001748 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1749 if (repl)
1750 rdev = conf->mirrors[d].replacement;
NeilBrown547414d2012-03-13 11:21:20 +11001751 else
NeilBrown9ad1aef2011-12-23 10:17:55 +11001752 rdev = conf->mirrors[d].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
NeilBrown1a0b7cd2011-07-28 11:39:25 +10001754 if (!uptodate) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11001755 if (repl)
1756 md_error(mddev, rdev);
1757 else {
1758 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11001759 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1760 set_bit(MD_RECOVERY_NEEDED,
1761 &rdev->mddev->recovery);
NeilBrown9ad1aef2011-12-23 10:17:55 +11001762 set_bit(R10BIO_WriteError, &r10_bio->state);
1763 }
1764 } else if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +10001765 r10_bio->devs[slot].addr,
1766 r10_bio->sectors,
1767 &first_bad, &bad_sectors))
1768 set_bit(R10BIO_MadeGood, &r10_bio->state);
NeilBrowndfc70642008-05-23 13:04:39 -07001769
NeilBrown9ad1aef2011-12-23 10:17:55 +11001770 rdev_dec_pending(rdev, mddev);
NeilBrown5e570282011-07-28 11:39:25 +10001771
1772 end_sync_request(r10_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773}
1774
1775/*
1776 * Note: sync and recover and handled very differently for raid10
1777 * This code is for resync.
1778 * For resync, we read through virtual addresses and read all blocks.
1779 * If there is any error, we schedule a write. The lowest numbered
1780 * drive is authoritative.
1781 * However requests come for physical address, so we need to map.
1782 * For every physical address there are raid_disks/copies virtual addresses,
1783 * which is always are least one, but is not necessarly an integer.
1784 * This means that a physical address can span multiple chunks, so we may
1785 * have to submit multiple io requests for a single sync request.
1786 */
1787/*
1788 * We check if all blocks are in-sync and only write to blocks that
1789 * aren't in sync
1790 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11001791static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
NeilBrowne879a872011-10-11 16:49:02 +11001793 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 int i, first;
1795 struct bio *tbio, *fbio;
majianpengf4380a92012-04-12 16:04:47 +10001796 int vcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 atomic_set(&r10_bio->remaining, 1);
1799
1800 /* find the first device with a block */
1801 for (i=0; i<conf->copies; i++)
1802 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1803 break;
1804
1805 if (i == conf->copies)
1806 goto done;
1807
1808 first = i;
1809 fbio = r10_bio->devs[i].bio;
1810
majianpengf4380a92012-04-12 16:04:47 +10001811 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001813 for (i=0 ; i < conf->copies ; i++) {
1814 int j, d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001817
1818 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001820 if (i == first)
1821 continue;
1822 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1823 /* We know that the bi_io_vec layout is the same for
1824 * both 'first' and 'i', so we just compare them.
1825 * All vec entries are PAGE_SIZE;
1826 */
1827 for (j = 0; j < vcnt; j++)
1828 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1829 page_address(tbio->bi_io_vec[j].bv_page),
NeilBrown5020ad72012-04-02 01:39:05 +10001830 fbio->bi_io_vec[j].bv_len))
NeilBrown0eb3ff12006-01-06 00:20:29 -08001831 break;
1832 if (j == vcnt)
1833 continue;
1834 mddev->resync_mismatches += r10_bio->sectors;
NeilBrownf84ee362011-07-28 11:39:25 +10001835 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1836 /* Don't fix anything. */
1837 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001838 }
NeilBrownf84ee362011-07-28 11:39:25 +10001839 /* Ok, we need to write this bio, either to correct an
1840 * inconsistency or to correct an unreadable block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 * First we need to fixup bv_offset, bv_len and
1842 * bi_vecs, as the read request might have corrupted these
1843 */
1844 tbio->bi_vcnt = vcnt;
1845 tbio->bi_size = r10_bio->sectors << 9;
1846 tbio->bi_idx = 0;
1847 tbio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1849 tbio->bi_flags |= 1 << BIO_UPTODATE;
1850 tbio->bi_next = NULL;
1851 tbio->bi_rw = WRITE;
1852 tbio->bi_private = r10_bio;
1853 tbio->bi_sector = r10_bio->devs[i].addr;
1854
1855 for (j=0; j < vcnt ; j++) {
1856 tbio->bi_io_vec[j].bv_offset = 0;
1857 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1858
1859 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1860 page_address(fbio->bi_io_vec[j].bv_page),
1861 PAGE_SIZE);
1862 }
1863 tbio->bi_end_io = end_sync_write;
1864
1865 d = r10_bio->devs[i].devnum;
1866 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1867 atomic_inc(&r10_bio->remaining);
1868 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1869
1870 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1871 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1872 generic_make_request(tbio);
1873 }
1874
NeilBrown9ad1aef2011-12-23 10:17:55 +11001875 /* Now write out to any replacement devices
1876 * that are active
1877 */
1878 for (i = 0; i < conf->copies; i++) {
1879 int j, d;
NeilBrown9ad1aef2011-12-23 10:17:55 +11001880
1881 tbio = r10_bio->devs[i].repl_bio;
1882 if (!tbio || !tbio->bi_end_io)
1883 continue;
1884 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
1885 && r10_bio->devs[i].bio != fbio)
1886 for (j = 0; j < vcnt; j++)
1887 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1888 page_address(fbio->bi_io_vec[j].bv_page),
1889 PAGE_SIZE);
1890 d = r10_bio->devs[i].devnum;
1891 atomic_inc(&r10_bio->remaining);
1892 md_sync_acct(conf->mirrors[d].replacement->bdev,
1893 tbio->bi_size >> 9);
1894 generic_make_request(tbio);
1895 }
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897done:
1898 if (atomic_dec_and_test(&r10_bio->remaining)) {
1899 md_done_sync(mddev, r10_bio->sectors, 1);
1900 put_buf(r10_bio);
1901 }
1902}
1903
1904/*
1905 * Now for the recovery code.
1906 * Recovery happens across physical sectors.
1907 * We recover all non-is_sync drives by finding the virtual address of
1908 * each, and then choose a working drive that also has that virt address.
1909 * There is a separate r10_bio for each non-in_sync drive.
1910 * Only the first two slots are in use. The first for reading,
1911 * The second for writing.
1912 *
1913 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11001914static void fix_recovery_read_error(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001915{
1916 /* We got a read error during recovery.
1917 * We repeat the read in smaller page-sized sections.
1918 * If a read succeeds, write it to the new device or record
1919 * a bad block if we cannot.
1920 * If a read fails, record a bad block on both old and
1921 * new devices.
1922 */
NeilBrownfd01b882011-10-11 16:47:53 +11001923 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001924 struct r10conf *conf = mddev->private;
NeilBrown5e570282011-07-28 11:39:25 +10001925 struct bio *bio = r10_bio->devs[0].bio;
1926 sector_t sect = 0;
1927 int sectors = r10_bio->sectors;
1928 int idx = 0;
1929 int dr = r10_bio->devs[0].devnum;
1930 int dw = r10_bio->devs[1].devnum;
1931
1932 while (sectors) {
1933 int s = sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11001934 struct md_rdev *rdev;
NeilBrown5e570282011-07-28 11:39:25 +10001935 sector_t addr;
1936 int ok;
1937
1938 if (s > (PAGE_SIZE>>9))
1939 s = PAGE_SIZE >> 9;
1940
1941 rdev = conf->mirrors[dr].rdev;
1942 addr = r10_bio->devs[0].addr + sect,
1943 ok = sync_page_io(rdev,
1944 addr,
1945 s << 9,
1946 bio->bi_io_vec[idx].bv_page,
1947 READ, false);
1948 if (ok) {
1949 rdev = conf->mirrors[dw].rdev;
1950 addr = r10_bio->devs[1].addr + sect;
1951 ok = sync_page_io(rdev,
1952 addr,
1953 s << 9,
1954 bio->bi_io_vec[idx].bv_page,
1955 WRITE, false);
NeilBrownb7044d42011-12-23 10:17:56 +11001956 if (!ok) {
NeilBrown5e570282011-07-28 11:39:25 +10001957 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11001958 if (!test_and_set_bit(WantReplacement,
1959 &rdev->flags))
1960 set_bit(MD_RECOVERY_NEEDED,
1961 &rdev->mddev->recovery);
1962 }
NeilBrown5e570282011-07-28 11:39:25 +10001963 }
1964 if (!ok) {
1965 /* We don't worry if we cannot set a bad block -
1966 * it really is bad so there is no loss in not
1967 * recording it yet
1968 */
1969 rdev_set_badblocks(rdev, addr, s, 0);
1970
1971 if (rdev != conf->mirrors[dw].rdev) {
1972 /* need bad block on destination too */
NeilBrown3cb03002011-10-11 16:45:26 +11001973 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
NeilBrown5e570282011-07-28 11:39:25 +10001974 addr = r10_bio->devs[1].addr + sect;
1975 ok = rdev_set_badblocks(rdev2, addr, s, 0);
1976 if (!ok) {
1977 /* just abort the recovery */
1978 printk(KERN_NOTICE
1979 "md/raid10:%s: recovery aborted"
1980 " due to read error\n",
1981 mdname(mddev));
1982
1983 conf->mirrors[dw].recovery_disabled
1984 = mddev->recovery_disabled;
1985 set_bit(MD_RECOVERY_INTR,
1986 &mddev->recovery);
1987 break;
1988 }
1989 }
1990 }
1991
1992 sectors -= s;
1993 sect += s;
1994 idx++;
1995 }
1996}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
NeilBrown9f2c9d12011-10-11 16:48:43 +11001998static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
NeilBrowne879a872011-10-11 16:49:02 +11002000 struct r10conf *conf = mddev->private;
Namhyung Kimc65060a2011-07-18 17:38:49 +10002001 int d;
NeilBrown24afd802011-12-23 10:17:55 +11002002 struct bio *wbio, *wbio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
NeilBrown5e570282011-07-28 11:39:25 +10002004 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2005 fix_recovery_read_error(r10_bio);
2006 end_sync_request(r10_bio);
2007 return;
2008 }
2009
Namhyung Kimc65060a2011-07-18 17:38:49 +10002010 /*
2011 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 * and submit the write request
2013 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 d = r10_bio->devs[1].devnum;
NeilBrown24afd802011-12-23 10:17:55 +11002015 wbio = r10_bio->devs[1].bio;
2016 wbio2 = r10_bio->devs[1].repl_bio;
2017 if (wbio->bi_end_io) {
2018 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2019 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
2020 generic_make_request(wbio);
2021 }
2022 if (wbio2 && wbio2->bi_end_io) {
2023 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2024 md_sync_acct(conf->mirrors[d].replacement->bdev,
2025 wbio2->bi_size >> 9);
2026 generic_make_request(wbio2);
2027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
2030
2031/*
Robert Becker1e509152009-12-14 12:49:58 +11002032 * Used by fix_read_error() to decay the per rdev read_errors.
2033 * We halve the read error count for every hour that has elapsed
2034 * since the last recorded read error.
2035 *
2036 */
NeilBrownfd01b882011-10-11 16:47:53 +11002037static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
Robert Becker1e509152009-12-14 12:49:58 +11002038{
2039 struct timespec cur_time_mon;
2040 unsigned long hours_since_last;
2041 unsigned int read_errors = atomic_read(&rdev->read_errors);
2042
2043 ktime_get_ts(&cur_time_mon);
2044
2045 if (rdev->last_read_error.tv_sec == 0 &&
2046 rdev->last_read_error.tv_nsec == 0) {
2047 /* first time we've seen a read error */
2048 rdev->last_read_error = cur_time_mon;
2049 return;
2050 }
2051
2052 hours_since_last = (cur_time_mon.tv_sec -
2053 rdev->last_read_error.tv_sec) / 3600;
2054
2055 rdev->last_read_error = cur_time_mon;
2056
2057 /*
2058 * if hours_since_last is > the number of bits in read_errors
2059 * just set read errors to 0. We do this to avoid
2060 * overflowing the shift of read_errors by hours_since_last.
2061 */
2062 if (hours_since_last >= 8 * sizeof(read_errors))
2063 atomic_set(&rdev->read_errors, 0);
2064 else
2065 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2066}
2067
NeilBrown3cb03002011-10-11 16:45:26 +11002068static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrown58c54fc2011-07-28 11:39:25 +10002069 int sectors, struct page *page, int rw)
2070{
2071 sector_t first_bad;
2072 int bad_sectors;
2073
2074 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2075 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2076 return -1;
2077 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
2078 /* success */
2079 return 1;
NeilBrownb7044d42011-12-23 10:17:56 +11002080 if (rw == WRITE) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002081 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002082 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2083 set_bit(MD_RECOVERY_NEEDED,
2084 &rdev->mddev->recovery);
2085 }
NeilBrown58c54fc2011-07-28 11:39:25 +10002086 /* need to record an error - either for the block or the device */
2087 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2088 md_error(rdev->mddev, rdev);
2089 return 0;
2090}
2091
Robert Becker1e509152009-12-14 12:49:58 +11002092/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 * This is a kernel thread which:
2094 *
2095 * 1. Retries failed read operations on working mirrors.
2096 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07002097 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 */
2099
NeilBrowne879a872011-10-11 16:49:02 +11002100static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown6814d532006-10-03 01:15:45 -07002101{
2102 int sect = 0; /* Offset from r10_bio->sector */
2103 int sectors = r10_bio->sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002104 struct md_rdev*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002105 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002106 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11002107
NeilBrown7c4e06f2011-05-11 14:53:17 +10002108 /* still own a reference to this rdev, so it cannot
2109 * have been cleared recently.
2110 */
2111 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002112
NeilBrown7c4e06f2011-05-11 14:53:17 +10002113 if (test_bit(Faulty, &rdev->flags))
2114 /* drive has already been failed, just ignore any
2115 more fix_read_error() attempts */
2116 return;
2117
2118 check_decay_read_errors(mddev, rdev);
2119 atomic_inc(&rdev->read_errors);
2120 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2121 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11002122 bdevname(rdev->bdev, b);
2123
NeilBrown7c4e06f2011-05-11 14:53:17 +10002124 printk(KERN_NOTICE
2125 "md/raid10:%s: %s: Raid device exceeded "
2126 "read_error threshold [cur %d:max %d]\n",
2127 mdname(mddev), b,
2128 atomic_read(&rdev->read_errors), max_read_errors);
2129 printk(KERN_NOTICE
2130 "md/raid10:%s: %s: Failing raid device\n",
2131 mdname(mddev), b);
2132 md_error(mddev, conf->mirrors[d].rdev);
NeilBrownfae8cc52012-02-14 11:10:10 +11002133 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
NeilBrown7c4e06f2011-05-11 14:53:17 +10002134 return;
Robert Becker1e509152009-12-14 12:49:58 +11002135 }
Robert Becker1e509152009-12-14 12:49:58 +11002136
NeilBrown6814d532006-10-03 01:15:45 -07002137 while(sectors) {
2138 int s = sectors;
2139 int sl = r10_bio->read_slot;
2140 int success = 0;
2141 int start;
2142
2143 if (s > (PAGE_SIZE>>9))
2144 s = PAGE_SIZE >> 9;
2145
2146 rcu_read_lock();
2147 do {
NeilBrown8dbed5c2011-07-28 11:39:24 +10002148 sector_t first_bad;
2149 int bad_sectors;
2150
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002151 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07002152 rdev = rcu_dereference(conf->mirrors[d].rdev);
2153 if (rdev &&
NeilBrown050b6612012-03-19 12:46:39 +11002154 !test_bit(Unmerged, &rdev->flags) &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002155 test_bit(In_sync, &rdev->flags) &&
2156 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2157 &first_bad, &bad_sectors) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07002158 atomic_inc(&rdev->nr_pending);
2159 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11002160 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07002161 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002162 sect,
NeilBrown6814d532006-10-03 01:15:45 -07002163 s<<9,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002164 conf->tmppage, READ, false);
NeilBrown6814d532006-10-03 01:15:45 -07002165 rdev_dec_pending(rdev, mddev);
2166 rcu_read_lock();
2167 if (success)
2168 break;
2169 }
2170 sl++;
2171 if (sl == conf->copies)
2172 sl = 0;
2173 } while (!success && sl != r10_bio->read_slot);
2174 rcu_read_unlock();
2175
2176 if (!success) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002177 /* Cannot read from anywhere, just mark the block
2178 * as bad on the first device to discourage future
2179 * reads.
2180 */
NeilBrown6814d532006-10-03 01:15:45 -07002181 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
NeilBrown58c54fc2011-07-28 11:39:25 +10002182 rdev = conf->mirrors[dn].rdev;
2183
2184 if (!rdev_set_badblocks(
2185 rdev,
2186 r10_bio->devs[r10_bio->read_slot].addr
2187 + sect,
NeilBrownfae8cc52012-02-14 11:10:10 +11002188 s, 0)) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002189 md_error(mddev, rdev);
NeilBrownfae8cc52012-02-14 11:10:10 +11002190 r10_bio->devs[r10_bio->read_slot].bio
2191 = IO_BLOCKED;
2192 }
NeilBrown6814d532006-10-03 01:15:45 -07002193 break;
2194 }
2195
2196 start = sl;
2197 /* write it back and re-read */
2198 rcu_read_lock();
2199 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11002200 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002201
NeilBrown6814d532006-10-03 01:15:45 -07002202 if (sl==0)
2203 sl = conf->copies;
2204 sl--;
2205 d = r10_bio->devs[sl].devnum;
2206 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002207 if (!rdev ||
NeilBrown050b6612012-03-19 12:46:39 +11002208 test_bit(Unmerged, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002209 !test_bit(In_sync, &rdev->flags))
2210 continue;
2211
2212 atomic_inc(&rdev->nr_pending);
2213 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002214 if (r10_sync_page_io(rdev,
2215 r10_bio->devs[sl].addr +
2216 sect,
2217 s<<9, conf->tmppage, WRITE)
NeilBrown1294b9c2011-07-28 11:39:23 +10002218 == 0) {
2219 /* Well, this device is dead */
2220 printk(KERN_NOTICE
2221 "md/raid10:%s: read correction "
2222 "write failed"
2223 " (%d sectors at %llu on %s)\n",
2224 mdname(mddev), s,
2225 (unsigned long long)(
2226 sect + rdev->data_offset),
2227 bdevname(rdev->bdev, b));
2228 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2229 "drive\n",
2230 mdname(mddev),
2231 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07002232 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002233 rdev_dec_pending(rdev, mddev);
2234 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002235 }
2236 sl = start;
2237 while (sl != r10_bio->read_slot) {
NeilBrown1294b9c2011-07-28 11:39:23 +10002238 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002239
NeilBrown6814d532006-10-03 01:15:45 -07002240 if (sl==0)
2241 sl = conf->copies;
2242 sl--;
2243 d = r10_bio->devs[sl].devnum;
2244 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002245 if (!rdev ||
2246 !test_bit(In_sync, &rdev->flags))
2247 continue;
Robert Becker67b8dc42009-12-14 12:49:57 +11002248
NeilBrown1294b9c2011-07-28 11:39:23 +10002249 atomic_inc(&rdev->nr_pending);
2250 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002251 switch (r10_sync_page_io(rdev,
2252 r10_bio->devs[sl].addr +
2253 sect,
2254 s<<9, conf->tmppage,
2255 READ)) {
2256 case 0:
NeilBrown1294b9c2011-07-28 11:39:23 +10002257 /* Well, this device is dead */
2258 printk(KERN_NOTICE
2259 "md/raid10:%s: unable to read back "
2260 "corrected sectors"
2261 " (%d sectors at %llu on %s)\n",
2262 mdname(mddev), s,
2263 (unsigned long long)(
2264 sect + rdev->data_offset),
2265 bdevname(rdev->bdev, b));
2266 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2267 "drive\n",
2268 mdname(mddev),
2269 bdevname(rdev->bdev, b));
NeilBrown58c54fc2011-07-28 11:39:25 +10002270 break;
2271 case 1:
NeilBrown1294b9c2011-07-28 11:39:23 +10002272 printk(KERN_INFO
2273 "md/raid10:%s: read error corrected"
2274 " (%d sectors at %llu on %s)\n",
2275 mdname(mddev), s,
2276 (unsigned long long)(
2277 sect + rdev->data_offset),
2278 bdevname(rdev->bdev, b));
2279 atomic_add(s, &rdev->corrected_errors);
NeilBrown6814d532006-10-03 01:15:45 -07002280 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002281
2282 rdev_dec_pending(rdev, mddev);
2283 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002284 }
2285 rcu_read_unlock();
2286
2287 sectors -= s;
2288 sect += s;
2289 }
2290}
2291
NeilBrownbd870a12011-07-28 11:39:24 +10002292static void bi_complete(struct bio *bio, int error)
2293{
2294 complete((struct completion *)bio->bi_private);
2295}
2296
2297static int submit_bio_wait(int rw, struct bio *bio)
2298{
2299 struct completion event;
2300 rw |= REQ_SYNC;
2301
2302 init_completion(&event);
2303 bio->bi_private = &event;
2304 bio->bi_end_io = bi_complete;
2305 submit_bio(rw, bio);
2306 wait_for_completion(&event);
2307
2308 return test_bit(BIO_UPTODATE, &bio->bi_flags);
2309}
2310
NeilBrown9f2c9d12011-10-11 16:48:43 +11002311static int narrow_write_error(struct r10bio *r10_bio, int i)
NeilBrownbd870a12011-07-28 11:39:24 +10002312{
2313 struct bio *bio = r10_bio->master_bio;
NeilBrownfd01b882011-10-11 16:47:53 +11002314 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002315 struct r10conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002316 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
NeilBrownbd870a12011-07-28 11:39:24 +10002317 /* bio has the data to be written to slot 'i' where
2318 * we just recently had a write error.
2319 * We repeatedly clone the bio and trim down to one block,
2320 * then try the write. Where the write fails we record
2321 * a bad block.
2322 * It is conceivable that the bio doesn't exactly align with
2323 * blocks. We must handle this.
2324 *
2325 * We currently own a reference to the rdev.
2326 */
2327
2328 int block_sectors;
2329 sector_t sector;
2330 int sectors;
2331 int sect_to_write = r10_bio->sectors;
2332 int ok = 1;
2333
2334 if (rdev->badblocks.shift < 0)
2335 return 0;
2336
2337 block_sectors = 1 << rdev->badblocks.shift;
2338 sector = r10_bio->sector;
2339 sectors = ((r10_bio->sector + block_sectors)
2340 & ~(sector_t)(block_sectors - 1))
2341 - sector;
2342
2343 while (sect_to_write) {
2344 struct bio *wbio;
2345 if (sectors > sect_to_write)
2346 sectors = sect_to_write;
2347 /* Write at 'sector' for 'sectors' */
2348 wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
2349 md_trim_bio(wbio, sector - bio->bi_sector, sectors);
2350 wbio->bi_sector = (r10_bio->devs[i].addr+
2351 rdev->data_offset+
2352 (sector - r10_bio->sector));
2353 wbio->bi_bdev = rdev->bdev;
2354 if (submit_bio_wait(WRITE, wbio) == 0)
2355 /* Failure! */
2356 ok = rdev_set_badblocks(rdev, sector,
2357 sectors, 0)
2358 && ok;
2359
2360 bio_put(wbio);
2361 sect_to_write -= sectors;
2362 sector += sectors;
2363 sectors = block_sectors;
2364 }
2365 return ok;
2366}
2367
NeilBrown9f2c9d12011-10-11 16:48:43 +11002368static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown560f8e52011-07-28 11:39:23 +10002369{
2370 int slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002371 struct bio *bio;
NeilBrowne879a872011-10-11 16:49:02 +11002372 struct r10conf *conf = mddev->private;
NeilBrownabbf0982011-12-23 10:17:54 +11002373 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002374 char b[BDEVNAME_SIZE];
2375 unsigned long do_sync;
NeilBrown856e08e2011-07-28 11:39:23 +10002376 int max_sectors;
NeilBrown560f8e52011-07-28 11:39:23 +10002377
2378 /* we got a read error. Maybe the drive is bad. Maybe just
2379 * the block and we can fix it.
2380 * We freeze all other IO, and try reading the block from
2381 * other devices. When we find one, we re-write
2382 * and check it that fixes the read error.
2383 * This is all done synchronously while the array is
2384 * frozen.
2385 */
NeilBrownfae8cc52012-02-14 11:10:10 +11002386 bio = r10_bio->devs[slot].bio;
2387 bdevname(bio->bi_bdev, b);
2388 bio_put(bio);
2389 r10_bio->devs[slot].bio = NULL;
2390
NeilBrown560f8e52011-07-28 11:39:23 +10002391 if (mddev->ro == 0) {
2392 freeze_array(conf);
2393 fix_read_error(conf, mddev, r10_bio);
2394 unfreeze_array(conf);
NeilBrownfae8cc52012-02-14 11:10:10 +11002395 } else
2396 r10_bio->devs[slot].bio = IO_BLOCKED;
2397
NeilBrownabbf0982011-12-23 10:17:54 +11002398 rdev_dec_pending(rdev, mddev);
NeilBrown560f8e52011-07-28 11:39:23 +10002399
NeilBrown7399c312011-07-28 11:39:23 +10002400read_more:
NeilBrown96c3fd12011-12-23 10:17:54 +11002401 rdev = read_balance(conf, r10_bio, &max_sectors);
2402 if (rdev == NULL) {
NeilBrown560f8e52011-07-28 11:39:23 +10002403 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
2404 " read error for block %llu\n",
NeilBrown7399c312011-07-28 11:39:23 +10002405 mdname(mddev), b,
NeilBrown560f8e52011-07-28 11:39:23 +10002406 (unsigned long long)r10_bio->sector);
2407 raid_end_bio_io(r10_bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002408 return;
2409 }
2410
2411 do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
NeilBrown560f8e52011-07-28 11:39:23 +10002412 slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002413 printk_ratelimited(
2414 KERN_ERR
2415 "md/raid10:%s: %s: redirecting"
2416 "sector %llu to another mirror\n",
2417 mdname(mddev),
2418 bdevname(rdev->bdev, b),
2419 (unsigned long long)r10_bio->sector);
2420 bio = bio_clone_mddev(r10_bio->master_bio,
2421 GFP_NOIO, mddev);
NeilBrown7399c312011-07-28 11:39:23 +10002422 md_trim_bio(bio,
2423 r10_bio->sector - bio->bi_sector,
2424 max_sectors);
NeilBrown560f8e52011-07-28 11:39:23 +10002425 r10_bio->devs[slot].bio = bio;
NeilBrownabbf0982011-12-23 10:17:54 +11002426 r10_bio->devs[slot].rdev = rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002427 bio->bi_sector = r10_bio->devs[slot].addr
2428 + rdev->data_offset;
2429 bio->bi_bdev = rdev->bdev;
2430 bio->bi_rw = READ | do_sync;
2431 bio->bi_private = r10_bio;
2432 bio->bi_end_io = raid10_end_read_request;
NeilBrown7399c312011-07-28 11:39:23 +10002433 if (max_sectors < r10_bio->sectors) {
2434 /* Drat - have to split this up more */
2435 struct bio *mbio = r10_bio->master_bio;
2436 int sectors_handled =
2437 r10_bio->sector + max_sectors
2438 - mbio->bi_sector;
2439 r10_bio->sectors = max_sectors;
2440 spin_lock_irq(&conf->device_lock);
2441 if (mbio->bi_phys_segments == 0)
2442 mbio->bi_phys_segments = 2;
2443 else
2444 mbio->bi_phys_segments++;
2445 spin_unlock_irq(&conf->device_lock);
2446 generic_make_request(bio);
NeilBrown7399c312011-07-28 11:39:23 +10002447
2448 r10_bio = mempool_alloc(conf->r10bio_pool,
2449 GFP_NOIO);
2450 r10_bio->master_bio = mbio;
2451 r10_bio->sectors = (mbio->bi_size >> 9)
2452 - sectors_handled;
2453 r10_bio->state = 0;
2454 set_bit(R10BIO_ReadError,
2455 &r10_bio->state);
2456 r10_bio->mddev = mddev;
2457 r10_bio->sector = mbio->bi_sector
2458 + sectors_handled;
2459
2460 goto read_more;
2461 } else
2462 generic_make_request(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002463}
2464
NeilBrowne879a872011-10-11 16:49:02 +11002465static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
NeilBrown749c55e2011-07-28 11:39:24 +10002466{
2467 /* Some sort of write request has finished and it
2468 * succeeded in writing where we thought there was a
2469 * bad block. So forget the bad block.
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002470 * Or possibly if failed and we need to record
2471 * a bad block.
NeilBrown749c55e2011-07-28 11:39:24 +10002472 */
2473 int m;
NeilBrown3cb03002011-10-11 16:45:26 +11002474 struct md_rdev *rdev;
NeilBrown749c55e2011-07-28 11:39:24 +10002475
2476 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2477 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002478 for (m = 0; m < conf->copies; m++) {
2479 int dev = r10_bio->devs[m].devnum;
2480 rdev = conf->mirrors[dev].rdev;
2481 if (r10_bio->devs[m].bio == NULL)
2482 continue;
2483 if (test_bit(BIO_UPTODATE,
NeilBrown749c55e2011-07-28 11:39:24 +10002484 &r10_bio->devs[m].bio->bi_flags)) {
NeilBrown749c55e2011-07-28 11:39:24 +10002485 rdev_clear_badblocks(
2486 rdev,
2487 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002488 r10_bio->sectors, 0);
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002489 } else {
2490 if (!rdev_set_badblocks(
2491 rdev,
2492 r10_bio->devs[m].addr,
2493 r10_bio->sectors, 0))
2494 md_error(conf->mddev, rdev);
NeilBrown749c55e2011-07-28 11:39:24 +10002495 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002496 rdev = conf->mirrors[dev].replacement;
2497 if (r10_bio->devs[m].repl_bio == NULL)
2498 continue;
2499 if (test_bit(BIO_UPTODATE,
2500 &r10_bio->devs[m].repl_bio->bi_flags)) {
2501 rdev_clear_badblocks(
2502 rdev,
2503 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002504 r10_bio->sectors, 0);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002505 } else {
2506 if (!rdev_set_badblocks(
2507 rdev,
2508 r10_bio->devs[m].addr,
2509 r10_bio->sectors, 0))
2510 md_error(conf->mddev, rdev);
2511 }
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002512 }
NeilBrown749c55e2011-07-28 11:39:24 +10002513 put_buf(r10_bio);
2514 } else {
NeilBrownbd870a12011-07-28 11:39:24 +10002515 for (m = 0; m < conf->copies; m++) {
2516 int dev = r10_bio->devs[m].devnum;
2517 struct bio *bio = r10_bio->devs[m].bio;
2518 rdev = conf->mirrors[dev].rdev;
2519 if (bio == IO_MADE_GOOD) {
NeilBrown749c55e2011-07-28 11:39:24 +10002520 rdev_clear_badblocks(
2521 rdev,
2522 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002523 r10_bio->sectors, 0);
NeilBrown749c55e2011-07-28 11:39:24 +10002524 rdev_dec_pending(rdev, conf->mddev);
NeilBrownbd870a12011-07-28 11:39:24 +10002525 } else if (bio != NULL &&
2526 !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2527 if (!narrow_write_error(r10_bio, m)) {
2528 md_error(conf->mddev, rdev);
2529 set_bit(R10BIO_Degraded,
2530 &r10_bio->state);
2531 }
2532 rdev_dec_pending(rdev, conf->mddev);
NeilBrown749c55e2011-07-28 11:39:24 +10002533 }
NeilBrown475b0322011-12-23 10:17:55 +11002534 bio = r10_bio->devs[m].repl_bio;
2535 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +11002536 if (rdev && bio == IO_MADE_GOOD) {
NeilBrown475b0322011-12-23 10:17:55 +11002537 rdev_clear_badblocks(
2538 rdev,
2539 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002540 r10_bio->sectors, 0);
NeilBrown475b0322011-12-23 10:17:55 +11002541 rdev_dec_pending(rdev, conf->mddev);
2542 }
NeilBrownbd870a12011-07-28 11:39:24 +10002543 }
2544 if (test_bit(R10BIO_WriteError,
2545 &r10_bio->state))
2546 close_write(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10002547 raid_end_bio_io(r10_bio);
2548 }
2549}
2550
NeilBrownfd01b882011-10-11 16:47:53 +11002551static void raid10d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552{
NeilBrown9f2c9d12011-10-11 16:48:43 +11002553 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 unsigned long flags;
NeilBrowne879a872011-10-11 16:49:02 +11002555 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002557 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
2559 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002561 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002563
Jens Axboe7eaceac2011-03-10 08:52:07 +01002564 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002565
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002567 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002568 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002570 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002571 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08002573 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 spin_unlock_irqrestore(&conf->device_lock, flags);
2575
2576 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002577 conf = mddev->private;
NeilBrownbd870a12011-07-28 11:39:24 +10002578 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2579 test_bit(R10BIO_WriteError, &r10_bio->state))
NeilBrown749c55e2011-07-28 11:39:24 +10002580 handle_write_completed(conf, r10_bio);
2581 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01002583 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 recovery_request_write(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002585 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
NeilBrown560f8e52011-07-28 11:39:23 +10002586 handle_read_error(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002587 else {
2588 /* just a partial read to be scheduled from a
2589 * separate context
2590 */
2591 int slot = r10_bio->read_slot;
2592 generic_make_request(r10_bio->devs[slot].bio);
2593 }
NeilBrown4443ae12006-01-06 00:20:28 -08002594
NeilBrown1d9d5242009-10-16 15:55:32 +11002595 cond_resched();
NeilBrownde393cd2011-07-28 11:31:48 +10002596 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2597 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002599 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600}
2601
2602
NeilBrowne879a872011-10-11 16:49:02 +11002603static int init_resync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
2605 int buffs;
NeilBrown69335ef2011-12-23 10:17:54 +11002606 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
2608 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02002609 BUG_ON(conf->r10buf_pool);
NeilBrown69335ef2011-12-23 10:17:54 +11002610 conf->have_replacement = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002611 for (i = 0; i < conf->geo.raid_disks; i++)
NeilBrown69335ef2011-12-23 10:17:54 +11002612 if (conf->mirrors[i].replacement)
2613 conf->have_replacement = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2615 if (!conf->r10buf_pool)
2616 return -ENOMEM;
2617 conf->next_resync = 0;
2618 return 0;
2619}
2620
2621/*
2622 * perform a "sync" on one "block"
2623 *
2624 * We need to make sure that no normal I/O request - particularly write
2625 * requests - conflict with active sync requests.
2626 *
2627 * This is achieved by tracking pending requests and a 'barrier' concept
2628 * that can be installed to exclude normal IO requests.
2629 *
2630 * Resync and recovery are handled very differently.
2631 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2632 *
2633 * For resync, we iterate over virtual addresses, read all copies,
2634 * and update if there are differences. If only one copy is live,
2635 * skip it.
2636 * For recovery, we iterate over physical addresses, read a good
2637 * value for each non-in_sync drive, and over-write.
2638 *
2639 * So, for recovery we may have several outstanding complex requests for a
2640 * given address, one for each out-of-sync device. We model this by allocating
2641 * a number of r10_bio structures, one for each out-of-sync device.
2642 * As we setup these structures, we collect all bio's together into a list
2643 * which we then process collectively to add pages, and then process again
2644 * to pass to generic_make_request.
2645 *
2646 * The r10_bio structures are linked using a borrowed master_bio pointer.
2647 * This link is counted in ->remaining. When the r10_bio that points to NULL
2648 * has its remaining count decremented to 0, the whole complex operation
2649 * is complete.
2650 *
2651 */
2652
NeilBrownfd01b882011-10-11 16:47:53 +11002653static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
NeilBrownab9d47e2011-05-11 14:54:41 +10002654 int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655{
NeilBrowne879a872011-10-11 16:49:02 +11002656 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002657 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 struct bio *biolist = NULL, *bio;
2659 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08002661 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11002662 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 sector_t sectors_skipped = 0;
2664 int chunks_skipped = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002665 sector_t chunk_mask = conf->geo.chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
2667 if (!conf->r10buf_pool)
2668 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002669 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
2671 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11002672 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2674 max_sector = mddev->resync_max_sectors;
2675 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002676 /* If we aborted, we need to abort the
2677 * sync on the 'current' bitmap chucks (there can
2678 * be several when recovering multiple devices).
2679 * as we may have started syncing it but not finished.
2680 * We can find the current address in
2681 * mddev->curr_resync, but for recovery,
2682 * we need to convert that to several
2683 * virtual addresses.
2684 */
2685 if (mddev->curr_resync < max_sector) { /* aborted */
2686 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2687 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2688 &sync_blocks, 1);
NeilBrown5cf00fc2012-05-21 09:28:20 +10002689 else for (i = 0; i < conf->geo.raid_disks; i++) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002690 sector_t sect =
2691 raid10_find_virt(conf, mddev->curr_resync, i);
2692 bitmap_end_sync(mddev->bitmap, sect,
2693 &sync_blocks, 1);
2694 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002695 } else {
2696 /* completed sync */
2697 if ((!mddev->bitmap || conf->fullsync)
2698 && conf->have_replacement
2699 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2700 /* Completed a full sync so the replacements
2701 * are now fully recovered.
2702 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10002703 for (i = 0; i < conf->geo.raid_disks; i++)
NeilBrown9ad1aef2011-12-23 10:17:55 +11002704 if (conf->mirrors[i].replacement)
2705 conf->mirrors[i].replacement
2706 ->recovery_offset
2707 = MaxSector;
2708 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002709 conf->fullsync = 0;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002710 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002711 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07002713 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 return sectors_skipped;
2715 }
NeilBrown5cf00fc2012-05-21 09:28:20 +10002716 if (chunks_skipped >= conf->geo.raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 /* if there has been nothing to do on any drive,
2718 * then there is nothing to do at all..
2719 */
NeilBrown57afd892005-06-21 17:17:13 -07002720 *skipped = 1;
2721 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 }
2723
NeilBrownc6207272008-02-06 01:39:52 -08002724 if (max_sector > mddev->resync_max)
2725 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 /* make sure whole request will fit in a chunk - if chunks
2728 * are meaningful
2729 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10002730 if (conf->geo.near_copies < conf->geo.raid_disks &&
2731 max_sector > (sector_nr | chunk_mask))
2732 max_sector = (sector_nr | chunk_mask) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 /*
2734 * If there is non-resync activity waiting for us then
2735 * put in a delay to throttle resync.
2736 */
NeilBrown0a27ec92006-01-06 00:20:13 -08002737 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
2740 /* Again, very different code for resync and recovery.
2741 * Both must result in an r10bio with a list of bios that
2742 * have bi_end_io, bi_sector, bi_bdev set,
2743 * and bi_private set to the r10bio.
2744 * For recovery, we may actually create several r10bios
2745 * with 2 bios in each, that correspond to the bios in the main one.
2746 * In this case, the subordinate r10bios link back through a
2747 * borrowed master_bio pointer, and the counter in the master
2748 * includes a ref from each subordinate.
2749 */
2750 /* First, we decide what to do and set ->bi_end_io
2751 * To end_sync_read if we want to read, and
2752 * end_sync_write if we will want to write.
2753 */
2754
NeilBrown6cce3b22006-01-06 00:20:16 -08002755 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2757 /* recovery... the complicated one */
NeilBrowne875ece2011-07-28 11:39:24 +10002758 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 r10_bio = NULL;
2760
NeilBrown5cf00fc2012-05-21 09:28:20 +10002761 for (i = 0 ; i < conf->geo.raid_disks; i++) {
NeilBrownab9d47e2011-05-11 14:54:41 +10002762 int still_degraded;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002763 struct r10bio *rb2;
NeilBrownab9d47e2011-05-11 14:54:41 +10002764 sector_t sect;
2765 int must_sync;
NeilBrowne875ece2011-07-28 11:39:24 +10002766 int any_working;
NeilBrown24afd802011-12-23 10:17:55 +11002767 struct mirror_info *mirror = &conf->mirrors[i];
NeilBrownab9d47e2011-05-11 14:54:41 +10002768
NeilBrown24afd802011-12-23 10:17:55 +11002769 if ((mirror->rdev == NULL ||
2770 test_bit(In_sync, &mirror->rdev->flags))
2771 &&
2772 (mirror->replacement == NULL ||
2773 test_bit(Faulty,
2774 &mirror->replacement->flags)))
NeilBrownab9d47e2011-05-11 14:54:41 +10002775 continue;
2776
2777 still_degraded = 0;
2778 /* want to reconstruct this device */
2779 rb2 = r10_bio;
2780 sect = raid10_find_virt(conf, sector_nr, i);
NeilBrown24afd802011-12-23 10:17:55 +11002781 /* Unless we are doing a full sync, or a replacement
2782 * we only need to recover the block if it is set in
2783 * the bitmap
NeilBrownab9d47e2011-05-11 14:54:41 +10002784 */
2785 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2786 &sync_blocks, 1);
2787 if (sync_blocks < max_sync)
2788 max_sync = sync_blocks;
2789 if (!must_sync &&
NeilBrown24afd802011-12-23 10:17:55 +11002790 mirror->replacement == NULL &&
NeilBrownab9d47e2011-05-11 14:54:41 +10002791 !conf->fullsync) {
2792 /* yep, skip the sync_blocks here, but don't assume
2793 * that there will never be anything to do here
NeilBrown6cce3b22006-01-06 00:20:16 -08002794 */
NeilBrownab9d47e2011-05-11 14:54:41 +10002795 chunks_skipped = -1;
2796 continue;
2797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
NeilBrownab9d47e2011-05-11 14:54:41 +10002799 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2800 raise_barrier(conf, rb2 != NULL);
2801 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
NeilBrownab9d47e2011-05-11 14:54:41 +10002803 r10_bio->master_bio = (struct bio*)rb2;
2804 if (rb2)
2805 atomic_inc(&rb2->remaining);
2806 r10_bio->mddev = mddev;
2807 set_bit(R10BIO_IsRecover, &r10_bio->state);
2808 r10_bio->sector = sect;
NeilBrown6cce3b22006-01-06 00:20:16 -08002809
NeilBrownab9d47e2011-05-11 14:54:41 +10002810 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10002811
NeilBrownab9d47e2011-05-11 14:54:41 +10002812 /* Need to check if the array will still be
2813 * degraded
2814 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10002815 for (j = 0; j < conf->geo.raid_disks; j++)
NeilBrownab9d47e2011-05-11 14:54:41 +10002816 if (conf->mirrors[j].rdev == NULL ||
2817 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
2818 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07002819 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002821
2822 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2823 &sync_blocks, still_degraded);
2824
NeilBrowne875ece2011-07-28 11:39:24 +10002825 any_working = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10002826 for (j=0; j<conf->copies;j++) {
NeilBrowne875ece2011-07-28 11:39:24 +10002827 int k;
NeilBrownab9d47e2011-05-11 14:54:41 +10002828 int d = r10_bio->devs[j].devnum;
NeilBrown5e570282011-07-28 11:39:25 +10002829 sector_t from_addr, to_addr;
NeilBrown3cb03002011-10-11 16:45:26 +11002830 struct md_rdev *rdev;
NeilBrown40c356c2011-07-28 11:39:24 +10002831 sector_t sector, first_bad;
2832 int bad_sectors;
NeilBrownab9d47e2011-05-11 14:54:41 +10002833 if (!conf->mirrors[d].rdev ||
2834 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
2835 continue;
2836 /* This is where we read from */
NeilBrowne875ece2011-07-28 11:39:24 +10002837 any_working = 1;
NeilBrown40c356c2011-07-28 11:39:24 +10002838 rdev = conf->mirrors[d].rdev;
2839 sector = r10_bio->devs[j].addr;
2840
2841 if (is_badblock(rdev, sector, max_sync,
2842 &first_bad, &bad_sectors)) {
2843 if (first_bad > sector)
2844 max_sync = first_bad - sector;
2845 else {
2846 bad_sectors -= (sector
2847 - first_bad);
2848 if (max_sync > bad_sectors)
2849 max_sync = bad_sectors;
2850 continue;
2851 }
2852 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002853 bio = r10_bio->devs[0].bio;
2854 bio->bi_next = biolist;
2855 biolist = bio;
2856 bio->bi_private = r10_bio;
2857 bio->bi_end_io = end_sync_read;
2858 bio->bi_rw = READ;
NeilBrown5e570282011-07-28 11:39:25 +10002859 from_addr = r10_bio->devs[j].addr;
NeilBrown24afd802011-12-23 10:17:55 +11002860 bio->bi_sector = from_addr + rdev->data_offset;
2861 bio->bi_bdev = rdev->bdev;
2862 atomic_inc(&rdev->nr_pending);
2863 /* and we write to 'i' (if not in_sync) */
NeilBrownab9d47e2011-05-11 14:54:41 +10002864
2865 for (k=0; k<conf->copies; k++)
2866 if (r10_bio->devs[k].devnum == i)
2867 break;
2868 BUG_ON(k == conf->copies);
NeilBrown5e570282011-07-28 11:39:25 +10002869 to_addr = r10_bio->devs[k].addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10002870 r10_bio->devs[0].devnum = d;
NeilBrown5e570282011-07-28 11:39:25 +10002871 r10_bio->devs[0].addr = from_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10002872 r10_bio->devs[1].devnum = i;
NeilBrown5e570282011-07-28 11:39:25 +10002873 r10_bio->devs[1].addr = to_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10002874
NeilBrown24afd802011-12-23 10:17:55 +11002875 rdev = mirror->rdev;
2876 if (!test_bit(In_sync, &rdev->flags)) {
2877 bio = r10_bio->devs[1].bio;
2878 bio->bi_next = biolist;
2879 biolist = bio;
2880 bio->bi_private = r10_bio;
2881 bio->bi_end_io = end_sync_write;
2882 bio->bi_rw = WRITE;
2883 bio->bi_sector = to_addr
2884 + rdev->data_offset;
2885 bio->bi_bdev = rdev->bdev;
2886 atomic_inc(&r10_bio->remaining);
2887 } else
2888 r10_bio->devs[1].bio->bi_end_io = NULL;
2889
2890 /* and maybe write to replacement */
2891 bio = r10_bio->devs[1].repl_bio;
2892 if (bio)
2893 bio->bi_end_io = NULL;
2894 rdev = mirror->replacement;
2895 /* Note: if rdev != NULL, then bio
2896 * cannot be NULL as r10buf_pool_alloc will
2897 * have allocated it.
2898 * So the second test here is pointless.
2899 * But it keeps semantic-checkers happy, and
2900 * this comment keeps human reviewers
2901 * happy.
2902 */
2903 if (rdev == NULL || bio == NULL ||
2904 test_bit(Faulty, &rdev->flags))
2905 break;
2906 bio->bi_next = biolist;
2907 biolist = bio;
2908 bio->bi_private = r10_bio;
2909 bio->bi_end_io = end_sync_write;
2910 bio->bi_rw = WRITE;
2911 bio->bi_sector = to_addr + rdev->data_offset;
2912 bio->bi_bdev = rdev->bdev;
2913 atomic_inc(&r10_bio->remaining);
NeilBrownab9d47e2011-05-11 14:54:41 +10002914 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002916 if (j == conf->copies) {
NeilBrowne875ece2011-07-28 11:39:24 +10002917 /* Cannot recover, so abort the recovery or
2918 * record a bad block */
NeilBrownab9d47e2011-05-11 14:54:41 +10002919 put_buf(r10_bio);
2920 if (rb2)
2921 atomic_dec(&rb2->remaining);
2922 r10_bio = rb2;
NeilBrowne875ece2011-07-28 11:39:24 +10002923 if (any_working) {
2924 /* problem is that there are bad blocks
2925 * on other device(s)
2926 */
2927 int k;
2928 for (k = 0; k < conf->copies; k++)
2929 if (r10_bio->devs[k].devnum == i)
2930 break;
NeilBrown24afd802011-12-23 10:17:55 +11002931 if (!test_bit(In_sync,
2932 &mirror->rdev->flags)
2933 && !rdev_set_badblocks(
2934 mirror->rdev,
2935 r10_bio->devs[k].addr,
2936 max_sync, 0))
2937 any_working = 0;
2938 if (mirror->replacement &&
2939 !rdev_set_badblocks(
2940 mirror->replacement,
NeilBrowne875ece2011-07-28 11:39:24 +10002941 r10_bio->devs[k].addr,
2942 max_sync, 0))
2943 any_working = 0;
2944 }
2945 if (!any_working) {
2946 if (!test_and_set_bit(MD_RECOVERY_INTR,
2947 &mddev->recovery))
2948 printk(KERN_INFO "md/raid10:%s: insufficient "
2949 "working devices for recovery.\n",
2950 mdname(mddev));
NeilBrown24afd802011-12-23 10:17:55 +11002951 mirror->recovery_disabled
NeilBrowne875ece2011-07-28 11:39:24 +10002952 = mddev->recovery_disabled;
2953 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002954 break;
2955 }
2956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 if (biolist == NULL) {
2958 while (r10_bio) {
NeilBrown9f2c9d12011-10-11 16:48:43 +11002959 struct r10bio *rb2 = r10_bio;
2960 r10_bio = (struct r10bio*) rb2->master_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 rb2->master_bio = NULL;
2962 put_buf(rb2);
2963 }
2964 goto giveup;
2965 }
2966 } else {
2967 /* resync. Schedule a read for every block at this virt offset */
2968 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08002969
NeilBrown78200d42009-02-25 13:18:47 +11002970 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
2971
NeilBrown6cce3b22006-01-06 00:20:16 -08002972 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2973 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10002974 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
2975 &mddev->recovery)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002976 /* We can skip this block */
2977 *skipped = 1;
2978 return sync_blocks + sectors_skipped;
2979 }
2980 if (sync_blocks < max_sync)
2981 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2983
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 r10_bio->mddev = mddev;
2985 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08002986 raise_barrier(conf, 0);
2987 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
2989 r10_bio->master_bio = NULL;
2990 r10_bio->sector = sector_nr;
2991 set_bit(R10BIO_IsSync, &r10_bio->state);
2992 raid10_find_phys(conf, r10_bio);
NeilBrown5cf00fc2012-05-21 09:28:20 +10002993 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
NeilBrown5cf00fc2012-05-21 09:28:20 +10002995 for (i = 0; i < conf->copies; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 int d = r10_bio->devs[i].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10002997 sector_t first_bad, sector;
2998 int bad_sectors;
2999
NeilBrown9ad1aef2011-12-23 10:17:55 +11003000 if (r10_bio->devs[i].repl_bio)
3001 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 bio = r10_bio->devs[i].bio;
3004 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07003005 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08003007 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 continue;
NeilBrown40c356c2011-07-28 11:39:24 +10003009 sector = r10_bio->devs[i].addr;
3010 if (is_badblock(conf->mirrors[d].rdev,
3011 sector, max_sync,
3012 &first_bad, &bad_sectors)) {
3013 if (first_bad > sector)
3014 max_sync = first_bad - sector;
3015 else {
3016 bad_sectors -= (sector - first_bad);
3017 if (max_sync > bad_sectors)
3018 max_sync = max_sync;
3019 continue;
3020 }
3021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
3023 atomic_inc(&r10_bio->remaining);
3024 bio->bi_next = biolist;
3025 biolist = bio;
3026 bio->bi_private = r10_bio;
3027 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08003028 bio->bi_rw = READ;
NeilBrown40c356c2011-07-28 11:39:24 +10003029 bio->bi_sector = sector +
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 conf->mirrors[d].rdev->data_offset;
3031 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
3032 count++;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003033
3034 if (conf->mirrors[d].replacement == NULL ||
3035 test_bit(Faulty,
3036 &conf->mirrors[d].replacement->flags))
3037 continue;
3038
3039 /* Need to set up for writing to the replacement */
3040 bio = r10_bio->devs[i].repl_bio;
3041 clear_bit(BIO_UPTODATE, &bio->bi_flags);
3042
3043 sector = r10_bio->devs[i].addr;
3044 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
3045 bio->bi_next = biolist;
3046 biolist = bio;
3047 bio->bi_private = r10_bio;
3048 bio->bi_end_io = end_sync_write;
3049 bio->bi_rw = WRITE;
3050 bio->bi_sector = sector +
3051 conf->mirrors[d].replacement->data_offset;
3052 bio->bi_bdev = conf->mirrors[d].replacement->bdev;
3053 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 }
3055
3056 if (count < 2) {
3057 for (i=0; i<conf->copies; i++) {
3058 int d = r10_bio->devs[i].devnum;
3059 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10003060 rdev_dec_pending(conf->mirrors[d].rdev,
3061 mddev);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003062 if (r10_bio->devs[i].repl_bio &&
3063 r10_bio->devs[i].repl_bio->bi_end_io)
3064 rdev_dec_pending(
3065 conf->mirrors[d].replacement,
3066 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 }
3068 put_buf(r10_bio);
3069 biolist = NULL;
3070 goto giveup;
3071 }
3072 }
3073
3074 for (bio = biolist; bio ; bio=bio->bi_next) {
3075
3076 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
3077 if (bio->bi_end_io)
3078 bio->bi_flags |= 1 << BIO_UPTODATE;
3079 bio->bi_vcnt = 0;
3080 bio->bi_idx = 0;
3081 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 bio->bi_size = 0;
3083 }
3084
3085 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003086 if (sector_nr + max_sync < max_sector)
3087 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 do {
3089 struct page *page;
3090 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 if (sector_nr + (len>>9) > max_sector)
3092 len = (max_sector - sector_nr) << 9;
3093 if (len == 0)
3094 break;
3095 for (bio= biolist ; bio ; bio=bio->bi_next) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003096 struct bio *bio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
NeilBrownab9d47e2011-05-11 14:54:41 +10003098 if (bio_add_page(bio, page, len, 0))
3099 continue;
3100
3101 /* stop here */
3102 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
3103 for (bio2 = biolist;
3104 bio2 && bio2 != bio;
3105 bio2 = bio2->bi_next) {
3106 /* remove last page from this bio */
3107 bio2->bi_vcnt--;
3108 bio2->bi_size -= len;
3109 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003111 goto bio_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 }
3113 nr_sectors += len>>9;
3114 sector_nr += len>>9;
3115 } while (biolist->bi_vcnt < RESYNC_PAGES);
3116 bio_full:
3117 r10_bio->sectors = nr_sectors;
3118
3119 while (biolist) {
3120 bio = biolist;
3121 biolist = biolist->bi_next;
3122
3123 bio->bi_next = NULL;
3124 r10_bio = bio->bi_private;
3125 r10_bio->sectors = nr_sectors;
3126
3127 if (bio->bi_end_io == end_sync_read) {
3128 md_sync_acct(bio->bi_bdev, nr_sectors);
3129 generic_make_request(bio);
3130 }
3131 }
3132
NeilBrown57afd892005-06-21 17:17:13 -07003133 if (sectors_skipped)
3134 /* pretend they weren't skipped, it makes
3135 * no important difference in this case
3136 */
3137 md_done_sync(mddev, sectors_skipped, 1);
3138
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 return sectors_skipped + nr_sectors;
3140 giveup:
3141 /* There is nowhere to write, so all non-sync
NeilBrowne875ece2011-07-28 11:39:24 +10003142 * drives must be failed or in resync, all drives
3143 * have a bad block, so try the next chunk...
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 */
NeilBrown09b40682009-02-25 13:18:47 +11003145 if (sector_nr + max_sync < max_sector)
3146 max_sector = sector_nr + max_sync;
3147
3148 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 chunks_skipped ++;
3150 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152}
3153
Dan Williams80c3a6c2009-03-17 18:10:40 -07003154static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11003155raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07003156{
3157 sector_t size;
NeilBrowne879a872011-10-11 16:49:02 +11003158 struct r10conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003159
3160 if (!raid_disks)
NeilBrown5cf00fc2012-05-21 09:28:20 +10003161 raid_disks = conf->geo.raid_disks;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003162 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003163 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003164
NeilBrown5cf00fc2012-05-21 09:28:20 +10003165 size = sectors >> conf->geo.chunk_shift;
3166 sector_div(size, conf->geo.far_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003167 size = size * raid_disks;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003168 sector_div(size, conf->geo.near_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003169
NeilBrown5cf00fc2012-05-21 09:28:20 +10003170 return size << conf->geo.chunk_shift;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003171}
3172
NeilBrown6508fdb2012-05-17 10:08:45 +10003173static void calc_sectors(struct r10conf *conf, sector_t size)
3174{
3175 /* Calculate the number of sectors-per-device that will
3176 * actually be used, and set conf->dev_sectors and
3177 * conf->stride
3178 */
3179
NeilBrown5cf00fc2012-05-21 09:28:20 +10003180 size = size >> conf->geo.chunk_shift;
3181 sector_div(size, conf->geo.far_copies);
3182 size = size * conf->geo.raid_disks;
3183 sector_div(size, conf->geo.near_copies);
NeilBrown6508fdb2012-05-17 10:08:45 +10003184 /* 'size' is now the number of chunks in the array */
3185 /* calculate "used chunks per device" */
3186 size = size * conf->copies;
3187
3188 /* We need to round up when dividing by raid_disks to
3189 * get the stride size.
3190 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003191 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
NeilBrown6508fdb2012-05-17 10:08:45 +10003192
NeilBrown5cf00fc2012-05-21 09:28:20 +10003193 conf->dev_sectors = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003194
NeilBrown5cf00fc2012-05-21 09:28:20 +10003195 if (conf->geo.far_offset)
3196 conf->geo.stride = 1 << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003197 else {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003198 sector_div(size, conf->geo.far_copies);
3199 conf->geo.stride = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003200 }
3201}
Trela, Maciejdab8b292010-03-08 16:02:45 +11003202
NeilBrowne879a872011-10-11 16:49:02 +11003203static struct r10conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204{
NeilBrowne879a872011-10-11 16:49:02 +11003205 struct r10conf *conf = NULL;
NeilBrownc93983b2006-06-26 00:27:41 -07003206 int nc, fc, fo;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003207 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Maciej Trelaf73ea872010-06-16 11:46:29 +01003209 if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
3210 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown128595e2010-05-03 14:47:14 +10003211 printk(KERN_ERR "md/raid10:%s: chunk size must be "
3212 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
3213 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003214 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 }
NeilBrown2604b702006-01-06 00:20:36 -08003216
Maciej Trelaf73ea872010-06-16 11:46:29 +01003217 nc = mddev->new_layout & 255;
3218 fc = (mddev->new_layout >> 8) & 255;
3219 fo = mddev->new_layout & (1<<16);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003220
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
Maciej Trelaf73ea872010-06-16 11:46:29 +01003222 (mddev->new_layout >> 17)) {
NeilBrown128595e2010-05-03 14:47:14 +10003223 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
Maciej Trelaf73ea872010-06-16 11:46:29 +01003224 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 goto out;
3226 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11003227
3228 err = -ENOMEM;
NeilBrowne879a872011-10-11 16:49:02 +11003229 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003230 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003232
NeilBrown4443ae12006-01-06 00:20:28 -08003233 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Trela, Maciejdab8b292010-03-08 16:02:45 +11003234 GFP_KERNEL);
3235 if (!conf->mirrors)
3236 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08003237
3238 conf->tmppage = alloc_page(GFP_KERNEL);
3239 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003240 goto out;
3241
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
NeilBrown5cf00fc2012-05-21 09:28:20 +10003243 conf->geo.raid_disks = mddev->raid_disks;
3244 conf->geo.near_copies = nc;
3245 conf->geo.far_copies = fc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 conf->copies = nc*fc;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003247 conf->geo.far_offset = fo;
3248 conf->geo.chunk_mask = mddev->new_chunk_sectors - 1;
3249 conf->geo.chunk_shift = ffz(~mddev->new_chunk_sectors);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003250
3251 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
3252 r10bio_pool_free, conf);
3253 if (!conf->r10bio_pool)
3254 goto out;
3255
NeilBrown6508fdb2012-05-17 10:08:45 +10003256 calc_sectors(conf, mddev->dev_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
Neil Browne7e72bf2008-05-14 16:05:54 -07003258 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003259 INIT_LIST_HEAD(&conf->retry_list);
3260
3261 spin_lock_init(&conf->resync_lock);
3262 init_waitqueue_head(&conf->wait_barrier);
3263
3264 conf->thread = md_register_thread(raid10d, mddev, NULL);
3265 if (!conf->thread)
3266 goto out;
3267
Trela, Maciejdab8b292010-03-08 16:02:45 +11003268 conf->mddev = mddev;
3269 return conf;
3270
3271 out:
NeilBrown128595e2010-05-03 14:47:14 +10003272 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
Trela, Maciejdab8b292010-03-08 16:02:45 +11003273 mdname(mddev));
3274 if (conf) {
3275 if (conf->r10bio_pool)
3276 mempool_destroy(conf->r10bio_pool);
3277 kfree(conf->mirrors);
3278 safe_put_page(conf->tmppage);
3279 kfree(conf);
3280 }
3281 return ERR_PTR(err);
3282}
3283
NeilBrownfd01b882011-10-11 16:47:53 +11003284static int run(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003285{
NeilBrowne879a872011-10-11 16:49:02 +11003286 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003287 int i, disk_idx, chunk_size;
NeilBrown0f6d02d2011-10-11 16:48:46 +11003288 struct mirror_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11003289 struct md_rdev *rdev;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003290 sector_t size;
3291
3292 /*
3293 * copy the already verified devices into our private RAID10
3294 * bookkeeping area. [whatever we allocate in run(),
3295 * should be freed in stop()]
3296 */
3297
3298 if (mddev->private == NULL) {
3299 conf = setup_conf(mddev);
3300 if (IS_ERR(conf))
3301 return PTR_ERR(conf);
3302 mddev->private = conf;
3303 }
3304 conf = mddev->private;
3305 if (!conf)
3306 goto out;
3307
Trela, Maciejdab8b292010-03-08 16:02:45 +11003308 mddev->thread = conf->thread;
3309 conf->thread = NULL;
3310
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003311 chunk_size = mddev->chunk_sectors << 9;
3312 blk_queue_io_min(mddev->queue, chunk_size);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003313 if (conf->geo.raid_disks % conf->geo.near_copies)
3314 blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003315 else
3316 blk_queue_io_opt(mddev->queue, chunk_size *
NeilBrown5cf00fc2012-05-21 09:28:20 +10003317 (conf->geo.raid_disks / conf->geo.near_copies));
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003318
NeilBrowndafb20f2012-03-19 12:46:39 +11003319 rdev_for_each(rdev, mddev) {
NeilBrown34b343c2011-07-28 11:31:47 +10003320
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 disk_idx = rdev->raid_disk;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003322 if (disk_idx >= conf->geo.raid_disks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 || disk_idx < 0)
3324 continue;
3325 disk = conf->mirrors + disk_idx;
3326
NeilBrown56a2559b2011-12-23 10:17:55 +11003327 if (test_bit(Replacement, &rdev->flags)) {
3328 if (disk->replacement)
3329 goto out_free_conf;
3330 disk->replacement = rdev;
3331 } else {
3332 if (disk->rdev)
3333 goto out_free_conf;
3334 disk->rdev = rdev;
3335 }
3336
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003337 disk_stack_limits(mddev->gendisk, rdev->bdev,
3338 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
3340 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 }
NeilBrown6d508242005-09-09 16:24:03 -07003342 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10003343 if (!enough(conf, -1)) {
NeilBrown128595e2010-05-03 14:47:14 +10003344 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07003345 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 goto out_free_conf;
3347 }
3348
3349 mddev->degraded = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003350 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351
3352 disk = conf->mirrors + i;
3353
NeilBrown56a2559b2011-12-23 10:17:55 +11003354 if (!disk->rdev && disk->replacement) {
3355 /* The replacement is all we have - use it */
3356 disk->rdev = disk->replacement;
3357 disk->replacement = NULL;
3358 clear_bit(Replacement, &disk->rdev->flags);
3359 }
3360
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003361 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07003362 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 disk->head_position = 0;
3364 mddev->degraded++;
Neil Brown8c2e8702008-06-28 08:30:52 +10003365 if (disk->rdev)
3366 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 }
NeilBrownd890fa22011-10-26 11:54:39 +11003368 disk->recovery_disabled = mddev->recovery_disabled - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 }
3370
Andre Noll8c6ac862009-06-18 08:48:06 +10003371 if (mddev->recovery_cp != MaxSector)
NeilBrown128595e2010-05-03 14:47:14 +10003372 printk(KERN_NOTICE "md/raid10:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10003373 " -- starting background reconstruction\n",
3374 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 printk(KERN_INFO
NeilBrown128595e2010-05-03 14:47:14 +10003376 "md/raid10:%s: active with %d out of %d devices\n",
NeilBrown5cf00fc2012-05-21 09:28:20 +10003377 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
3378 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 /*
3380 * Ok, everything is just fine now
3381 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11003382 mddev->dev_sectors = conf->dev_sectors;
3383 size = raid10_size(mddev, 0, 0);
3384 md_set_array_sectors(mddev, size);
3385 mddev->resync_max_sectors = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386
NeilBrown0d129222006-10-03 01:15:54 -07003387 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
3388 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown7a5febe2005-05-16 21:53:16 -07003389
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 /* Calculate max read-ahead size.
3391 * We need to readahead at least twice a whole stripe....
3392 * maybe...
3393 */
3394 {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003395 int stripe = conf->geo.raid_disks *
Andre Noll9d8f0362009-06-18 08:45:01 +10003396 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003397 stripe /= conf->geo.near_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
3399 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
3400 }
3401
NeilBrown050b6612012-03-19 12:46:39 +11003402 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
Martin K. Petersena91a2782011-03-17 11:11:05 +01003403
3404 if (md_integrity_register(mddev))
3405 goto out_free_conf;
3406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 return 0;
3408
3409out_free_conf:
NeilBrown01f96c02011-09-21 15:30:20 +10003410 md_unregister_thread(&mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 if (conf->r10bio_pool)
3412 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08003413 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003414 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 kfree(conf);
3416 mddev->private = NULL;
3417out:
3418 return -EIO;
3419}
3420
NeilBrownfd01b882011-10-11 16:47:53 +11003421static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422{
NeilBrowne879a872011-10-11 16:49:02 +11003423 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
NeilBrown409c57f2009-03-31 14:39:39 +11003425 raise_barrier(conf, 0);
3426 lower_barrier(conf);
3427
NeilBrown01f96c02011-09-21 15:30:20 +10003428 md_unregister_thread(&mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
3430 if (conf->r10bio_pool)
3431 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003432 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 kfree(conf);
3434 mddev->private = NULL;
3435 return 0;
3436}
3437
NeilBrownfd01b882011-10-11 16:47:53 +11003438static void raid10_quiesce(struct mddev *mddev, int state)
NeilBrown6cce3b22006-01-06 00:20:16 -08003439{
NeilBrowne879a872011-10-11 16:49:02 +11003440 struct r10conf *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08003441
3442 switch(state) {
3443 case 1:
3444 raise_barrier(conf, 0);
3445 break;
3446 case 0:
3447 lower_barrier(conf);
3448 break;
3449 }
NeilBrown6cce3b22006-01-06 00:20:16 -08003450}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
NeilBrown006a09a2012-03-19 12:46:40 +11003452static int raid10_resize(struct mddev *mddev, sector_t sectors)
3453{
3454 /* Resize of 'far' arrays is not supported.
3455 * For 'near' and 'offset' arrays we can set the
3456 * number of sectors used to be an appropriate multiple
3457 * of the chunk size.
3458 * For 'offset', this is far_copies*chunksize.
3459 * For 'near' the multiplier is the LCM of
3460 * near_copies and raid_disks.
3461 * So if far_copies > 1 && !far_offset, fail.
3462 * Else find LCM(raid_disks, near_copy)*far_copies and
3463 * multiply by chunk_size. Then round to this number.
3464 * This is mostly done by raid10_size()
3465 */
3466 struct r10conf *conf = mddev->private;
3467 sector_t oldsize, size;
3468
NeilBrown5cf00fc2012-05-21 09:28:20 +10003469 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
NeilBrown006a09a2012-03-19 12:46:40 +11003470 return -EINVAL;
3471
3472 oldsize = raid10_size(mddev, 0, 0);
3473 size = raid10_size(mddev, sectors, 0);
3474 md_set_array_sectors(mddev, size);
3475 if (mddev->array_sectors > size)
3476 return -EINVAL;
3477 set_capacity(mddev->gendisk, mddev->array_sectors);
3478 revalidate_disk(mddev->gendisk);
3479 if (sectors > mddev->dev_sectors &&
3480 mddev->recovery_cp > oldsize) {
3481 mddev->recovery_cp = oldsize;
3482 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3483 }
NeilBrown6508fdb2012-05-17 10:08:45 +10003484 calc_sectors(conf, sectors);
3485 mddev->dev_sectors = conf->dev_sectors;
NeilBrown006a09a2012-03-19 12:46:40 +11003486 mddev->resync_max_sectors = size;
3487 return 0;
3488}
3489
NeilBrownfd01b882011-10-11 16:47:53 +11003490static void *raid10_takeover_raid0(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003491{
NeilBrown3cb03002011-10-11 16:45:26 +11003492 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +11003493 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003494
3495 if (mddev->degraded > 0) {
NeilBrown128595e2010-05-03 14:47:14 +10003496 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
3497 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003498 return ERR_PTR(-EINVAL);
3499 }
3500
Trela, Maciejdab8b292010-03-08 16:02:45 +11003501 /* Set new parameters */
3502 mddev->new_level = 10;
3503 /* new layout: far_copies = 1, near_copies = 2 */
3504 mddev->new_layout = (1<<8) + 2;
3505 mddev->new_chunk_sectors = mddev->chunk_sectors;
3506 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003507 mddev->raid_disks *= 2;
3508 /* make sure it will be not marked as dirty */
3509 mddev->recovery_cp = MaxSector;
3510
3511 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003512 if (!IS_ERR(conf)) {
NeilBrowndafb20f2012-03-19 12:46:39 +11003513 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003514 if (rdev->raid_disk >= 0)
3515 rdev->new_raid_disk = rdev->raid_disk * 2;
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003516 conf->barrier = 1;
3517 }
3518
Trela, Maciejdab8b292010-03-08 16:02:45 +11003519 return conf;
3520}
3521
NeilBrownfd01b882011-10-11 16:47:53 +11003522static void *raid10_takeover(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003523{
NeilBrowne373ab12011-10-11 16:48:59 +11003524 struct r0conf *raid0_conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003525
3526 /* raid10 can take over:
3527 * raid0 - providing it has only two drives
3528 */
3529 if (mddev->level == 0) {
3530 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11003531 raid0_conf = mddev->private;
3532 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown128595e2010-05-03 14:47:14 +10003533 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
3534 " with more than one zone.\n",
3535 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003536 return ERR_PTR(-EINVAL);
3537 }
3538 return raid10_takeover_raid0(mddev);
3539 }
3540 return ERR_PTR(-EINVAL);
3541}
3542
NeilBrown84fc4b52011-10-11 16:49:58 +11003543static struct md_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544{
3545 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08003546 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 .owner = THIS_MODULE,
3548 .make_request = make_request,
3549 .run = run,
3550 .stop = stop,
3551 .status = status,
3552 .error_handler = error,
3553 .hot_add_disk = raid10_add_disk,
3554 .hot_remove_disk= raid10_remove_disk,
3555 .spare_active = raid10_spare_active,
3556 .sync_request = sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08003557 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07003558 .size = raid10_size,
NeilBrown006a09a2012-03-19 12:46:40 +11003559 .resize = raid10_resize,
Trela, Maciejdab8b292010-03-08 16:02:45 +11003560 .takeover = raid10_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561};
3562
3563static int __init raid_init(void)
3564{
NeilBrown2604b702006-01-06 00:20:36 -08003565 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566}
3567
3568static void raid_exit(void)
3569{
NeilBrown2604b702006-01-06 00:20:36 -08003570 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571}
3572
3573module_init(raid_init);
3574module_exit(raid_exit);
3575MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11003576MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08003578MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08003579MODULE_ALIAS("md-level-10");
NeilBrown34db0cd2011-10-11 16:50:01 +11003580
3581module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);