blob: 9722065022fa7734873c518c5dd92cb44347c59f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid10.c : Multiple Devices driver for Linux
3 *
4 * Copyright (C) 2000-2004 Neil Brown
5 *
6 * RAID-10 support for md.
7 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008 * Base on code in raid1.c. See raid1.c for further copyright information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110022#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110023#include <linux/blkdev.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040024#include <linux/module.h>
NeilBrownbff61972009-03-31 14:33:13 +110025#include <linux/seq_file.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100026#include <linux/ratelimit.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110027#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110028#include "raid10.h"
Trela, Maciejdab8b292010-03-08 16:02:45 +110029#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110030#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/*
33 * RAID10 provides a combination of RAID0 and RAID1 functionality.
34 * The layout of data is defined by
35 * chunk_size
36 * raid_disks
37 * near_copies (stored in low byte of layout)
38 * far_copies (stored in second byte of layout)
NeilBrownc93983b2006-06-26 00:27:41 -070039 * far_offset (stored in bit 16 of layout )
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 *
41 * The data to be stored is divided into chunks using chunksize.
42 * Each device is divided into far_copies sections.
43 * In each section, chunks are laid out in a style similar to raid0, but
44 * near_copies copies of each chunk is stored (each on a different drive).
45 * The starting device for each section is offset near_copies from the starting
46 * device of the previous section.
NeilBrownc93983b2006-06-26 00:27:41 -070047 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * drive.
49 * near_copies and far_copies must be at least one, and their product is at most
50 * raid_disks.
NeilBrownc93983b2006-06-26 00:27:41 -070051 *
52 * If far_offset is true, then the far_copies are handled a bit differently.
53 * The copies are still in different stripes, but instead of be very far apart
54 * on disk, there are adjacent stripes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 */
56
57/*
58 * Number of guaranteed r10bios in case of extreme VM load:
59 */
60#define NR_RAID10_BIOS 256
61
NeilBrown34db0cd2011-10-11 16:50:01 +110062/* When there are this many requests queue to be written by
63 * the raid10 thread, we become 'congested' to provide back-pressure
64 * for writeback.
65 */
66static int max_queued_requests = 1024;
67
NeilBrowne879a872011-10-11 16:49:02 +110068static void allow_barrier(struct r10conf *conf);
69static void lower_barrier(struct r10conf *conf);
NeilBrown0a27ec92006-01-06 00:20:13 -080070
Al Virodd0fc662005-10-07 07:46:04 +010071static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
NeilBrowne879a872011-10-11 16:49:02 +110073 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +110074 int size = offsetof(struct r10bio, devs[conf->copies]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
NeilBrown69335ef2011-12-23 10:17:54 +110076 /* allocate a r10bio with room for raid_disks entries in the
77 * bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +010078 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
81static void r10bio_pool_free(void *r10_bio, void *data)
82{
83 kfree(r10_bio);
84}
85
NeilBrown0310fa22008-08-05 15:54:14 +100086/* Maximum size of each resync request */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#define RESYNC_BLOCK_SIZE (64*1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
NeilBrown0310fa22008-08-05 15:54:14 +100089/* amount of memory to reserve for resync requests */
90#define RESYNC_WINDOW (1024*1024)
91/* maximum number of concurrent requests, memory permitting */
92#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/*
95 * When performing a resync, we need to read and compare, so
96 * we need as many pages are there are copies.
97 * When performing a recovery, we need 2 bios, one for read,
98 * one for write (we recover only one drive per r10buf)
99 *
100 */
Al Virodd0fc662005-10-07 07:46:04 +0100101static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
NeilBrowne879a872011-10-11 16:49:02 +1100103 struct r10conf *conf = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct page *page;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100105 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct bio *bio;
107 int i, j;
108 int nalloc;
109
110 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100111 if (!r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
115 nalloc = conf->copies; /* resync */
116 else
117 nalloc = 2; /* recovery */
118
119 /*
120 * Allocate bios.
121 */
122 for (j = nalloc ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +1100123 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (!bio)
125 goto out_free_bio;
126 r10_bio->devs[j].bio = bio;
NeilBrown69335ef2011-12-23 10:17:54 +1100127 if (!conf->have_replacement)
128 continue;
129 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
130 if (!bio)
131 goto out_free_bio;
132 r10_bio->devs[j].repl_bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
134 /*
135 * Allocate RESYNC_PAGES data pages and attach them
136 * where needed.
137 */
138 for (j = 0 ; j < nalloc; j++) {
NeilBrown69335ef2011-12-23 10:17:54 +1100139 struct bio *rbio = r10_bio->devs[j].repl_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 bio = r10_bio->devs[j].bio;
141 for (i = 0; i < RESYNC_PAGES; i++) {
Namhyung Kimc65060a2011-07-18 17:38:49 +1000142 if (j == 1 && !test_bit(MD_RECOVERY_SYNC,
143 &conf->mddev->recovery)) {
144 /* we can share bv_page's during recovery */
145 struct bio *rbio = r10_bio->devs[0].bio;
146 page = rbio->bi_io_vec[i].bv_page;
147 get_page(page);
148 } else
149 page = alloc_page(gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (unlikely(!page))
151 goto out_free_pages;
152
153 bio->bi_io_vec[i].bv_page = page;
NeilBrown69335ef2011-12-23 10:17:54 +1100154 if (rbio)
155 rbio->bi_io_vec[i].bv_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157 }
158
159 return r10_bio;
160
161out_free_pages:
162 for ( ; i > 0 ; i--)
NeilBrown1345b1d2006-01-06 00:20:40 -0800163 safe_put_page(bio->bi_io_vec[i-1].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 while (j--)
165 for (i = 0; i < RESYNC_PAGES ; i++)
NeilBrown1345b1d2006-01-06 00:20:40 -0800166 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 j = -1;
168out_free_bio:
NeilBrown69335ef2011-12-23 10:17:54 +1100169 while (++j < nalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 bio_put(r10_bio->devs[j].bio);
NeilBrown69335ef2011-12-23 10:17:54 +1100171 if (r10_bio->devs[j].repl_bio)
172 bio_put(r10_bio->devs[j].repl_bio);
173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 r10bio_pool_free(r10_bio, conf);
175 return NULL;
176}
177
178static void r10buf_pool_free(void *__r10_bio, void *data)
179{
180 int i;
NeilBrowne879a872011-10-11 16:49:02 +1100181 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100182 struct r10bio *r10bio = __r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 int j;
184
185 for (j=0; j < conf->copies; j++) {
186 struct bio *bio = r10bio->devs[j].bio;
187 if (bio) {
188 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown1345b1d2006-01-06 00:20:40 -0800189 safe_put_page(bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 bio->bi_io_vec[i].bv_page = NULL;
191 }
192 bio_put(bio);
193 }
NeilBrown69335ef2011-12-23 10:17:54 +1100194 bio = r10bio->devs[j].repl_bio;
195 if (bio)
196 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198 r10bio_pool_free(r10bio, conf);
199}
200
NeilBrowne879a872011-10-11 16:49:02 +1100201static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 int i;
204
205 for (i = 0; i < conf->copies; i++) {
206 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown749c55e2011-07-28 11:39:24 +1000207 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 bio_put(*bio);
209 *bio = NULL;
NeilBrown69335ef2011-12-23 10:17:54 +1100210 bio = &r10_bio->devs[i].repl_bio;
211 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
212 bio_put(*bio);
213 *bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215}
216
NeilBrown9f2c9d12011-10-11 16:48:43 +1100217static void free_r10bio(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
NeilBrowne879a872011-10-11 16:49:02 +1100219 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 put_all_bios(conf, r10_bio);
222 mempool_free(r10_bio, conf->r10bio_pool);
223}
224
NeilBrown9f2c9d12011-10-11 16:48:43 +1100225static void put_buf(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
NeilBrowne879a872011-10-11 16:49:02 +1100227 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 mempool_free(r10_bio, conf->r10buf_pool);
230
NeilBrown0a27ec92006-01-06 00:20:13 -0800231 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
NeilBrown9f2c9d12011-10-11 16:48:43 +1100234static void reschedule_retry(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +1100237 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +1100238 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 spin_lock_irqsave(&conf->device_lock, flags);
241 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800242 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 spin_unlock_irqrestore(&conf->device_lock, flags);
244
Arthur Jones388667b2008-07-25 12:03:38 -0700245 /* wake up frozen array... */
246 wake_up(&conf->wait_barrier);
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 md_wakeup_thread(mddev->thread);
249}
250
251/*
252 * raid_end_bio_io() is called when we have finished servicing a mirrored
253 * operation and are ready to return a success/failure code to the buffer
254 * cache layer.
255 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100256static void raid_end_bio_io(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 struct bio *bio = r10_bio->master_bio;
NeilBrown856e08e2011-07-28 11:39:23 +1000259 int done;
NeilBrowne879a872011-10-11 16:49:02 +1100260 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
NeilBrown856e08e2011-07-28 11:39:23 +1000262 if (bio->bi_phys_segments) {
263 unsigned long flags;
264 spin_lock_irqsave(&conf->device_lock, flags);
265 bio->bi_phys_segments--;
266 done = (bio->bi_phys_segments == 0);
267 spin_unlock_irqrestore(&conf->device_lock, flags);
268 } else
269 done = 1;
270 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
271 clear_bit(BIO_UPTODATE, &bio->bi_flags);
272 if (done) {
273 bio_endio(bio, 0);
274 /*
275 * Wake up any possible resync thread that waits for the device
276 * to go idle.
277 */
278 allow_barrier(conf);
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 free_r10bio(r10_bio);
281}
282
283/*
284 * Update disk head position estimator based on IRQ completion info.
285 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100286static inline void update_head_pos(int slot, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
NeilBrowne879a872011-10-11 16:49:02 +1100288 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
291 r10_bio->devs[slot].addr + (r10_bio->sectors);
292}
293
Namhyung Kim778ca012011-07-18 17:38:47 +1000294/*
295 * Find the disk number which triggered given bio
296 */
NeilBrowne879a872011-10-11 16:49:02 +1100297static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
NeilBrown69335ef2011-12-23 10:17:54 +1100298 struct bio *bio, int *slotp, int *replp)
Namhyung Kim778ca012011-07-18 17:38:47 +1000299{
300 int slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100301 int repl = 0;
Namhyung Kim778ca012011-07-18 17:38:47 +1000302
NeilBrown69335ef2011-12-23 10:17:54 +1100303 for (slot = 0; slot < conf->copies; slot++) {
Namhyung Kim778ca012011-07-18 17:38:47 +1000304 if (r10_bio->devs[slot].bio == bio)
305 break;
NeilBrown69335ef2011-12-23 10:17:54 +1100306 if (r10_bio->devs[slot].repl_bio == bio) {
307 repl = 1;
308 break;
309 }
310 }
Namhyung Kim778ca012011-07-18 17:38:47 +1000311
312 BUG_ON(slot == conf->copies);
313 update_head_pos(slot, r10_bio);
314
NeilBrown749c55e2011-07-28 11:39:24 +1000315 if (slotp)
316 *slotp = slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100317 if (replp)
318 *replp = repl;
Namhyung Kim778ca012011-07-18 17:38:47 +1000319 return r10_bio->devs[slot].devnum;
320}
321
NeilBrown6712ecf2007-09-27 12:47:43 +0200322static void raid10_end_read_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +1100325 struct r10bio *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 int slot, dev;
NeilBrowne879a872011-10-11 16:49:02 +1100327 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 slot = r10_bio->read_slot;
331 dev = r10_bio->devs[slot].devnum;
332 /*
333 * this branch is our 'one mirror IO has finished' event handler:
334 */
NeilBrown4443ae12006-01-06 00:20:28 -0800335 update_head_pos(slot, r10_bio);
336
337 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 /*
339 * Set R10BIO_Uptodate in our master bio, so that
340 * we will return a good error code to the higher
341 * levels even if IO on some other mirrored buffer fails.
342 *
343 * The 'master' represents the composite IO operation to
344 * user-side. So if something waits for IO, then it will
345 * wait for the 'master' bio.
346 */
347 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 raid_end_bio_io(r10_bio);
NeilBrown7c4e06f2011-05-11 14:53:17 +1000349 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
NeilBrown4443ae12006-01-06 00:20:28 -0800350 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 /*
NeilBrown7c4e06f2011-05-11 14:53:17 +1000352 * oops, read error - keep the refcount on the rdev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 */
354 char b[BDEVNAME_SIZE];
Christian Dietrich8bda4702011-07-27 11:00:36 +1000355 printk_ratelimited(KERN_ERR
356 "md/raid10:%s: %s: rescheduling sector %llu\n",
357 mdname(conf->mddev),
358 bdevname(conf->mirrors[dev].rdev->bdev, b),
359 (unsigned long long)r10_bio->sector);
NeilBrown856e08e2011-07-28 11:39:23 +1000360 set_bit(R10BIO_ReadError, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 reschedule_retry(r10_bio);
362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
NeilBrown9f2c9d12011-10-11 16:48:43 +1100365static void close_write(struct r10bio *r10_bio)
NeilBrownbd870a12011-07-28 11:39:24 +1000366{
367 /* clear the bitmap if all writes complete successfully */
368 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
369 r10_bio->sectors,
370 !test_bit(R10BIO_Degraded, &r10_bio->state),
371 0);
372 md_write_end(r10_bio->mddev);
373}
374
NeilBrown9f2c9d12011-10-11 16:48:43 +1100375static void one_write_done(struct r10bio *r10_bio)
NeilBrown19d5f832011-09-10 17:21:17 +1000376{
377 if (atomic_dec_and_test(&r10_bio->remaining)) {
378 if (test_bit(R10BIO_WriteError, &r10_bio->state))
379 reschedule_retry(r10_bio);
380 else {
381 close_write(r10_bio);
382 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
383 reschedule_retry(r10_bio);
384 else
385 raid_end_bio_io(r10_bio);
386 }
387 }
388}
389
NeilBrown6712ecf2007-09-27 12:47:43 +0200390static void raid10_end_write_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +1100393 struct r10bio *r10_bio = bio->bi_private;
Namhyung Kim778ca012011-07-18 17:38:47 +1000394 int dev;
NeilBrown749c55e2011-07-28 11:39:24 +1000395 int dec_rdev = 1;
NeilBrowne879a872011-10-11 16:49:02 +1100396 struct r10conf *conf = r10_bio->mddev->private;
NeilBrown749c55e2011-07-28 11:39:24 +1000397 int slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
NeilBrown69335ef2011-12-23 10:17:54 +1100399 dev = find_bio_disk(conf, r10_bio, bio, &slot, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /*
402 * this branch is our 'one mirror IO has finished' event handler:
403 */
NeilBrown6cce3b22006-01-06 00:20:16 -0800404 if (!uptodate) {
NeilBrownbd870a12011-07-28 11:39:24 +1000405 set_bit(WriteErrorSeen, &conf->mirrors[dev].rdev->flags);
406 set_bit(R10BIO_WriteError, &r10_bio->state);
407 dec_rdev = 0;
NeilBrown749c55e2011-07-28 11:39:24 +1000408 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /*
410 * Set R10BIO_Uptodate in our master bio, so that
411 * we will return a good error code for to the higher
412 * levels even if IO on some other mirrored buffer fails.
413 *
414 * The 'master' represents the composite IO operation to
415 * user-side. So if something waits for IO, then it will
416 * wait for the 'master' bio.
417 */
NeilBrown749c55e2011-07-28 11:39:24 +1000418 sector_t first_bad;
419 int bad_sectors;
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 set_bit(R10BIO_Uptodate, &r10_bio->state);
422
NeilBrown749c55e2011-07-28 11:39:24 +1000423 /* Maybe we can clear some bad blocks. */
424 if (is_badblock(conf->mirrors[dev].rdev,
425 r10_bio->devs[slot].addr,
426 r10_bio->sectors,
427 &first_bad, &bad_sectors)) {
428 bio_put(bio);
429 r10_bio->devs[slot].bio = IO_MADE_GOOD;
430 dec_rdev = 0;
431 set_bit(R10BIO_MadeGood, &r10_bio->state);
432 }
433 }
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 /*
436 *
437 * Let's see if all mirrored write operations have finished
438 * already.
439 */
NeilBrown19d5f832011-09-10 17:21:17 +1000440 one_write_done(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +1000441 if (dec_rdev)
442 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
445
446/*
447 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300448 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 * parameters: near_copies and far_copies.
450 * near_copies * far_copies must be <= raid_disks.
451 * Normally one of these will be 1.
452 * If both are 1, we get raid0.
453 * If near_copies == raid_disks, we get raid1.
454 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300455 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * first chunk, followed by near_copies copies of the next chunk and
457 * so on.
458 * If far_copies > 1, then after 1/far_copies of the array has been assigned
459 * as described above, we start again with a device offset of near_copies.
460 * So we effectively have another copy of the whole array further down all
461 * the drives, but with blocks on different drives.
462 * With this layout, and block is never stored twice on the one device.
463 *
464 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700465 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 *
467 * raid10_find_virt does the reverse mapping, from a device and a
468 * sector offset to a virtual address
469 */
470
NeilBrowne879a872011-10-11 16:49:02 +1100471static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 int n,f;
474 sector_t sector;
475 sector_t chunk;
476 sector_t stripe;
477 int dev;
478
479 int slot = 0;
480
481 /* now calculate first sector/dev */
482 chunk = r10bio->sector >> conf->chunk_shift;
483 sector = r10bio->sector & conf->chunk_mask;
484
485 chunk *= conf->near_copies;
486 stripe = chunk;
487 dev = sector_div(stripe, conf->raid_disks);
NeilBrownc93983b2006-06-26 00:27:41 -0700488 if (conf->far_offset)
489 stripe *= conf->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 sector += stripe << conf->chunk_shift;
492
493 /* and calculate all the others */
494 for (n=0; n < conf->near_copies; n++) {
495 int d = dev;
496 sector_t s = sector;
497 r10bio->devs[slot].addr = sector;
498 r10bio->devs[slot].devnum = d;
499 slot++;
500
501 for (f = 1; f < conf->far_copies; f++) {
502 d += conf->near_copies;
503 if (d >= conf->raid_disks)
504 d -= conf->raid_disks;
505 s += conf->stride;
506 r10bio->devs[slot].devnum = d;
507 r10bio->devs[slot].addr = s;
508 slot++;
509 }
510 dev++;
511 if (dev >= conf->raid_disks) {
512 dev = 0;
513 sector += (conf->chunk_mask + 1);
514 }
515 }
516 BUG_ON(slot != conf->copies);
517}
518
NeilBrowne879a872011-10-11 16:49:02 +1100519static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 sector_t offset, chunk, vchunk;
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 offset = sector & conf->chunk_mask;
NeilBrownc93983b2006-06-26 00:27:41 -0700524 if (conf->far_offset) {
525 int fc;
526 chunk = sector >> conf->chunk_shift;
527 fc = sector_div(chunk, conf->far_copies);
528 dev -= fc * conf->near_copies;
529 if (dev < 0)
530 dev += conf->raid_disks;
531 } else {
NeilBrown64a742b2007-02-28 20:11:18 -0800532 while (sector >= conf->stride) {
NeilBrownc93983b2006-06-26 00:27:41 -0700533 sector -= conf->stride;
534 if (dev < conf->near_copies)
535 dev += conf->raid_disks - conf->near_copies;
536 else
537 dev -= conf->near_copies;
538 }
539 chunk = sector >> conf->chunk_shift;
540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 vchunk = chunk * conf->raid_disks + dev;
542 sector_div(vchunk, conf->near_copies);
543 return (vchunk << conf->chunk_shift) + offset;
544}
545
546/**
547 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
548 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200549 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 * @biovec: the request that could be merged to it.
551 *
552 * Return amount of bytes we can accept at this offset
553 * If near_copies == raid_disk, there are no striping issues,
554 * but in that case, the function isn't called at all.
555 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200556static int raid10_mergeable_bvec(struct request_queue *q,
557 struct bvec_merge_data *bvm,
558 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
NeilBrownfd01b882011-10-11 16:47:53 +1100560 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200561 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +1000563 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200564 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
567 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200568 if (max <= biovec->bv_len && bio_sectors == 0)
569 return biovec->bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 else
571 return max;
572}
573
574/*
575 * This routine returns the disk from which the requested read should
576 * be done. There is a per-array 'next expected sequential IO' sector
577 * number - if this matches on the next IO then we use the last disk.
578 * There is also a per-disk 'last know head position' sector that is
579 * maintained from IRQ contexts, both the normal and the resync IO
580 * completion handlers update this position correctly. If there is no
581 * perfect sequential match then we pick the disk whose head is closest.
582 *
583 * If there are 2 mirrors in the same 2 devices, performance degrades
584 * because position is mirror, not device based.
585 *
586 * The rdev for the device selected will have nr_pending incremented.
587 */
588
589/*
590 * FIXME: possibly should rethink readbalancing and do it differently
591 * depending on near_copies / far_copies geometry.
592 */
NeilBrowne879a872011-10-11 16:49:02 +1100593static int read_balance(struct r10conf *conf, struct r10bio *r10_bio, int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000595 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000596 int disk, slot;
NeilBrown856e08e2011-07-28 11:39:23 +1000597 int sectors = r10_bio->sectors;
598 int best_good_sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000599 sector_t new_distance, best_dist;
NeilBrown3cb03002011-10-11 16:45:26 +1100600 struct md_rdev *rdev;
NeilBrown56d99122011-05-11 14:27:03 +1000601 int do_balance;
602 int best_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 raid10_find_phys(conf, r10_bio);
605 rcu_read_lock();
NeilBrown56d99122011-05-11 14:27:03 +1000606retry:
NeilBrown856e08e2011-07-28 11:39:23 +1000607 sectors = r10_bio->sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000608 best_slot = -1;
609 best_dist = MaxSector;
NeilBrown856e08e2011-07-28 11:39:23 +1000610 best_good_sectors = 0;
NeilBrown56d99122011-05-11 14:27:03 +1000611 do_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /*
613 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800614 * device if no resync is going on (recovery is ok), or below
615 * the resync window. We take the first readable disk when
616 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 */
618 if (conf->mddev->recovery_cp < MaxSector
NeilBrown56d99122011-05-11 14:27:03 +1000619 && (this_sector + sectors >= conf->next_resync))
620 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
NeilBrown56d99122011-05-11 14:27:03 +1000622 for (slot = 0; slot < conf->copies ; slot++) {
NeilBrown856e08e2011-07-28 11:39:23 +1000623 sector_t first_bad;
624 int bad_sectors;
625 sector_t dev_sector;
626
NeilBrown56d99122011-05-11 14:27:03 +1000627 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000629 disk = r10_bio->devs[slot].devnum;
630 rdev = rcu_dereference(conf->mirrors[disk].rdev);
631 if (rdev == NULL)
632 continue;
633 if (!test_bit(In_sync, &rdev->flags))
634 continue;
635
NeilBrown856e08e2011-07-28 11:39:23 +1000636 dev_sector = r10_bio->devs[slot].addr;
637 if (is_badblock(rdev, dev_sector, sectors,
638 &first_bad, &bad_sectors)) {
639 if (best_dist < MaxSector)
640 /* Already have a better slot */
641 continue;
642 if (first_bad <= dev_sector) {
643 /* Cannot read here. If this is the
644 * 'primary' device, then we must not read
645 * beyond 'bad_sectors' from another device.
646 */
647 bad_sectors -= (dev_sector - first_bad);
648 if (!do_balance && sectors > bad_sectors)
649 sectors = bad_sectors;
650 if (best_good_sectors > sectors)
651 best_good_sectors = sectors;
652 } else {
653 sector_t good_sectors =
654 first_bad - dev_sector;
655 if (good_sectors > best_good_sectors) {
656 best_good_sectors = good_sectors;
657 best_slot = slot;
658 }
659 if (!do_balance)
660 /* Must read from here */
661 break;
662 }
663 continue;
664 } else
665 best_good_sectors = sectors;
666
NeilBrown56d99122011-05-11 14:27:03 +1000667 if (!do_balance)
668 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
NeilBrown22dfdf52005-11-28 13:44:09 -0800670 /* This optimisation is debatable, and completely destroys
671 * sequential read speed for 'far copies' arrays. So only
672 * keep it for 'near' arrays, and review those later.
673 */
NeilBrown56d99122011-05-11 14:27:03 +1000674 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 break;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800676
677 /* for far > 1 always use the lowest address */
678 if (conf->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000679 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800680 else
NeilBrown56d99122011-05-11 14:27:03 +1000681 new_distance = abs(r10_bio->devs[slot].addr -
682 conf->mirrors[disk].head_position);
683 if (new_distance < best_dist) {
684 best_dist = new_distance;
685 best_slot = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687 }
NeilBrown56d99122011-05-11 14:27:03 +1000688 if (slot == conf->copies)
689 slot = best_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
NeilBrown56d99122011-05-11 14:27:03 +1000691 if (slot >= 0) {
692 disk = r10_bio->devs[slot].devnum;
693 rdev = rcu_dereference(conf->mirrors[disk].rdev);
694 if (!rdev)
695 goto retry;
696 atomic_inc(&rdev->nr_pending);
697 if (test_bit(Faulty, &rdev->flags)) {
698 /* Cannot risk returning a device that failed
699 * before we inc'ed nr_pending
700 */
701 rdev_dec_pending(rdev, conf->mddev);
702 goto retry;
703 }
704 r10_bio->read_slot = slot;
705 } else
NeilBrown29fc7e32006-02-03 03:03:41 -0800706 disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 rcu_read_unlock();
NeilBrown856e08e2011-07-28 11:39:23 +1000708 *max_sectors = best_good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 return disk;
711}
712
NeilBrown0d129222006-10-03 01:15:54 -0700713static int raid10_congested(void *data, int bits)
714{
NeilBrownfd01b882011-10-11 16:47:53 +1100715 struct mddev *mddev = data;
NeilBrowne879a872011-10-11 16:49:02 +1100716 struct r10conf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700717 int i, ret = 0;
718
NeilBrown34db0cd2011-10-11 16:50:01 +1100719 if ((bits & (1 << BDI_async_congested)) &&
720 conf->pending_count >= max_queued_requests)
721 return 1;
722
NeilBrown3fa841d2009-09-23 18:10:29 +1000723 if (mddev_congested(mddev, bits))
724 return 1;
NeilBrown0d129222006-10-03 01:15:54 -0700725 rcu_read_lock();
NeilBrown84707f32010-03-16 17:23:35 +1100726 for (i = 0; i < conf->raid_disks && ret == 0; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100727 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700728 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200729 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700730
731 ret |= bdi_congested(&q->backing_dev_info, bits);
732 }
733 }
734 rcu_read_unlock();
735 return ret;
736}
737
NeilBrowne879a872011-10-11 16:49:02 +1100738static void flush_pending_writes(struct r10conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800739{
740 /* Any writes that have been queued but are awaiting
741 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800742 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800743 spin_lock_irq(&conf->device_lock);
744
745 if (conf->pending_bio_list.head) {
746 struct bio *bio;
747 bio = bio_list_get(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +1100748 conf->pending_count = 0;
NeilBrowna35e63e2008-03-04 14:29:29 -0800749 spin_unlock_irq(&conf->device_lock);
750 /* flush any pending bitmap writes to disk
751 * before proceeding w/ I/O */
752 bitmap_unplug(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100753 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800754
755 while (bio) { /* submit pending writes */
756 struct bio *next = bio->bi_next;
757 bio->bi_next = NULL;
758 generic_make_request(bio);
759 bio = next;
760 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800761 } else
762 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800763}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100764
NeilBrown0a27ec92006-01-06 00:20:13 -0800765/* Barriers....
766 * Sometimes we need to suspend IO while we do something else,
767 * either some resync/recovery, or reconfigure the array.
768 * To do this we raise a 'barrier'.
769 * The 'barrier' is a counter that can be raised multiple times
770 * to count how many activities are happening which preclude
771 * normal IO.
772 * We can only raise the barrier if there is no pending IO.
773 * i.e. if nr_pending == 0.
774 * We choose only to raise the barrier if no-one is waiting for the
775 * barrier to go down. This means that as soon as an IO request
776 * is ready, no other operations which require a barrier will start
777 * until the IO request has had a chance.
778 *
779 * So: regular IO calls 'wait_barrier'. When that returns there
780 * is no backgroup IO happening, It must arrange to call
781 * allow_barrier when it has finished its IO.
782 * backgroup IO calls must call raise_barrier. Once that returns
783 * there is no normal IO happeing. It must arrange to call
784 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
NeilBrowne879a872011-10-11 16:49:02 +1100787static void raise_barrier(struct r10conf *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
NeilBrown6cce3b22006-01-06 00:20:16 -0800789 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
NeilBrown6cce3b22006-01-06 00:20:16 -0800792 /* Wait until no block IO is waiting (unless 'force') */
793 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrownc3b328a2011-04-18 18:25:43 +1000794 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800795
796 /* block any new IO from starting */
797 conf->barrier++;
798
NeilBrownc3b328a2011-04-18 18:25:43 +1000799 /* Now wait for all pending IO to complete */
NeilBrown0a27ec92006-01-06 00:20:13 -0800800 wait_event_lock_irq(conf->wait_barrier,
801 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
NeilBrownc3b328a2011-04-18 18:25:43 +1000802 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 spin_unlock_irq(&conf->resync_lock);
805}
806
NeilBrowne879a872011-10-11 16:49:02 +1100807static void lower_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800808{
809 unsigned long flags;
810 spin_lock_irqsave(&conf->resync_lock, flags);
811 conf->barrier--;
812 spin_unlock_irqrestore(&conf->resync_lock, flags);
813 wake_up(&conf->wait_barrier);
814}
815
NeilBrowne879a872011-10-11 16:49:02 +1100816static void wait_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800817{
818 spin_lock_irq(&conf->resync_lock);
819 if (conf->barrier) {
820 conf->nr_waiting++;
821 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
822 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000823 );
NeilBrown0a27ec92006-01-06 00:20:13 -0800824 conf->nr_waiting--;
825 }
826 conf->nr_pending++;
827 spin_unlock_irq(&conf->resync_lock);
828}
829
NeilBrowne879a872011-10-11 16:49:02 +1100830static void allow_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800831{
832 unsigned long flags;
833 spin_lock_irqsave(&conf->resync_lock, flags);
834 conf->nr_pending--;
835 spin_unlock_irqrestore(&conf->resync_lock, flags);
836 wake_up(&conf->wait_barrier);
837}
838
NeilBrowne879a872011-10-11 16:49:02 +1100839static void freeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -0800840{
841 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800842 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800843 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800844 * wait until nr_pending match nr_queued+1
845 * This is called in the context of one normal IO request
846 * that has failed. Thus any sync request that might be pending
847 * will be blocked by nr_pending, and we need to wait for
848 * pending IO requests to complete or be queued for re-try.
849 * Thus the number queued (nr_queued) plus this request (1)
850 * must match the number of pending IOs (nr_pending) before
851 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -0800852 */
853 spin_lock_irq(&conf->resync_lock);
854 conf->barrier++;
855 conf->nr_waiting++;
856 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -0800857 conf->nr_pending == conf->nr_queued+1,
NeilBrown4443ae12006-01-06 00:20:28 -0800858 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000859 flush_pending_writes(conf));
860
NeilBrown4443ae12006-01-06 00:20:28 -0800861 spin_unlock_irq(&conf->resync_lock);
862}
863
NeilBrowne879a872011-10-11 16:49:02 +1100864static void unfreeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -0800865{
866 /* reverse the effect of the freeze */
867 spin_lock_irq(&conf->resync_lock);
868 conf->barrier--;
869 conf->nr_waiting--;
870 wake_up(&conf->wait_barrier);
871 spin_unlock_irq(&conf->resync_lock);
872}
873
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -0700874static void make_request(struct mddev *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
NeilBrowne879a872011-10-11 16:49:02 +1100876 struct r10conf *conf = mddev->private;
NeilBrown0f6d02d2011-10-11 16:48:46 +1100877 struct mirror_info *mirror;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100878 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 struct bio *read_bio;
880 int i;
881 int chunk_sects = conf->chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +0100882 const int rw = bio_data_dir(bio);
NeilBrown2c7d46e2010-08-18 16:16:05 +1000883 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
Tejun Heoe9c74692010-09-03 11:56:18 +0200884 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
NeilBrown6cce3b22006-01-06 00:20:16 -0800885 unsigned long flags;
NeilBrown3cb03002011-10-11 16:45:26 +1100886 struct md_rdev *blocked_rdev;
NeilBrownc3b328a2011-04-18 18:25:43 +1000887 int plugged;
NeilBrownd4432c22011-07-28 11:39:24 +1000888 int sectors_handled;
889 int max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Tejun Heoe9c74692010-09-03 11:56:18 +0200891 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
892 md_flush_request(mddev, bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200893 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -0700894 }
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 /* If this request crosses a chunk boundary, we need to
897 * split it. This will only happen for 1 PAGE (or less) requests.
898 */
899 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
900 > chunk_sects &&
901 conf->near_copies < conf->raid_disks)) {
902 struct bio_pair *bp;
903 /* Sanity check -- queue functions should prevent this happening */
904 if (bio->bi_vcnt != 1 ||
905 bio->bi_idx != 0)
906 goto bad_map;
907 /* This is a one page bio that upper layers
908 * refuse to split for us, so we need to split it.
909 */
Denis ChengRq6feef532008-10-09 08:57:05 +0200910 bp = bio_split(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
NeilBrown51e9ac72010-08-07 21:17:00 +1000912
913 /* Each of these 'make_request' calls will call 'wait_barrier'.
914 * If the first succeeds but the second blocks due to the resync
915 * thread raising the barrier, we will deadlock because the
916 * IO to the underlying device will be queued in generic_make_request
917 * and will never complete, so will never reduce nr_pending.
918 * So increment nr_waiting here so no new raise_barriers will
919 * succeed, and so the second wait_barrier cannot block.
920 */
921 spin_lock_irq(&conf->resync_lock);
922 conf->nr_waiting++;
923 spin_unlock_irq(&conf->resync_lock);
924
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200925 make_request(mddev, &bp->bio1);
926 make_request(mddev, &bp->bio2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
NeilBrown51e9ac72010-08-07 21:17:00 +1000928 spin_lock_irq(&conf->resync_lock);
929 conf->nr_waiting--;
930 wake_up(&conf->wait_barrier);
931 spin_unlock_irq(&conf->resync_lock);
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 bio_pair_release(bp);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200934 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 bad_map:
NeilBrown128595e2010-05-03 14:47:14 +1000936 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
937 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
939
NeilBrown6712ecf2007-09-27 12:47:43 +0200940 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200941 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943
NeilBrown3d310eb2005-06-21 17:17:26 -0700944 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -0700945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 /*
947 * Register the new request and wait if the reconstruction
948 * thread has put up a bar for new requests.
949 * Continue immediately if no resync is active currently.
950 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800951 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
954
955 r10_bio->master_bio = bio;
956 r10_bio->sectors = bio->bi_size >> 9;
957
958 r10_bio->mddev = mddev;
959 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b22006-01-06 00:20:16 -0800960 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
NeilBrown856e08e2011-07-28 11:39:23 +1000962 /* We might need to issue multiple reads to different
963 * devices if there are bad blocks around, so we keep
964 * track of the number of reads in bio->bi_phys_segments.
965 * If this is 0, there is only one r10_bio and no locking
966 * will be needed when the request completes. If it is
967 * non-zero, then it is the number of not-completed requests.
968 */
969 bio->bi_phys_segments = 0;
970 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
971
Jens Axboea3623572005-11-01 09:26:16 +0100972 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 /*
974 * read balancing logic:
975 */
NeilBrown856e08e2011-07-28 11:39:23 +1000976 int disk;
977 int slot;
978
979read_again:
980 disk = read_balance(conf, r10_bio, &max_sectors);
981 slot = r10_bio->read_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (disk < 0) {
983 raid_end_bio_io(r10_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200984 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986 mirror = conf->mirrors + disk;
987
NeilBrowna167f662010-10-26 18:31:13 +1100988 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrown856e08e2011-07-28 11:39:23 +1000989 md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
990 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 r10_bio->devs[slot].bio = read_bio;
993
994 read_bio->bi_sector = r10_bio->devs[slot].addr +
995 mirror->rdev->data_offset;
996 read_bio->bi_bdev = mirror->rdev->bdev;
997 read_bio->bi_end_io = raid10_end_read_request;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200998 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 read_bio->bi_private = r10_bio;
1000
NeilBrown856e08e2011-07-28 11:39:23 +10001001 if (max_sectors < r10_bio->sectors) {
1002 /* Could not read all from this device, so we will
1003 * need another r10_bio.
1004 */
NeilBrown856e08e2011-07-28 11:39:23 +10001005 sectors_handled = (r10_bio->sectors + max_sectors
1006 - bio->bi_sector);
1007 r10_bio->sectors = max_sectors;
1008 spin_lock_irq(&conf->device_lock);
1009 if (bio->bi_phys_segments == 0)
1010 bio->bi_phys_segments = 2;
1011 else
1012 bio->bi_phys_segments++;
1013 spin_unlock(&conf->device_lock);
1014 /* Cannot call generic_make_request directly
1015 * as that will be queued in __generic_make_request
1016 * and subsequent mempool_alloc might block
1017 * waiting for it. so hand bio over to raid10d.
1018 */
1019 reschedule_retry(r10_bio);
1020
1021 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1022
1023 r10_bio->master_bio = bio;
1024 r10_bio->sectors = ((bio->bi_size >> 9)
1025 - sectors_handled);
1026 r10_bio->state = 0;
1027 r10_bio->mddev = mddev;
1028 r10_bio->sector = bio->bi_sector + sectors_handled;
1029 goto read_again;
1030 } else
1031 generic_make_request(read_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001032 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034
1035 /*
1036 * WRITE:
1037 */
NeilBrown34db0cd2011-10-11 16:50:01 +11001038 if (conf->pending_count >= max_queued_requests) {
1039 md_wakeup_thread(mddev->thread);
1040 wait_event(conf->wait_barrier,
1041 conf->pending_count < max_queued_requests);
1042 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001043 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 * inc refcount on their rdev. Record them by setting
1045 * bios[x] to bio
NeilBrownd4432c22011-07-28 11:39:24 +10001046 * If there are known/acknowledged bad blocks on any device
1047 * on which we have seen a write error, we want to avoid
1048 * writing to those blocks. This potentially requires several
1049 * writes to write around the bad blocks. Each set of writes
1050 * gets its own r10_bio with a set of bios attached. The number
1051 * of r10_bios is recored in bio->bi_phys_segments just as with
1052 * the read case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001054 plugged = mddev_check_plugged(mddev);
1055
NeilBrown69335ef2011-12-23 10:17:54 +11001056 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 raid10_find_phys(conf, r10_bio);
NeilBrownd4432c22011-07-28 11:39:24 +10001058retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -07001059 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 rcu_read_lock();
NeilBrownd4432c22011-07-28 11:39:24 +10001061 max_sectors = r10_bio->sectors;
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 for (i = 0; i < conf->copies; i++) {
1064 int d = r10_bio->devs[i].devnum;
NeilBrown3cb03002011-10-11 16:45:26 +11001065 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001066 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1067 atomic_inc(&rdev->nr_pending);
1068 blocked_rdev = rdev;
1069 break;
1070 }
NeilBrownd4432c22011-07-28 11:39:24 +10001071 r10_bio->devs[i].bio = NULL;
1072 if (!rdev || test_bit(Faulty, &rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001073 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrownd4432c22011-07-28 11:39:24 +10001074 continue;
NeilBrown6cce3b22006-01-06 00:20:16 -08001075 }
NeilBrownd4432c22011-07-28 11:39:24 +10001076 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1077 sector_t first_bad;
1078 sector_t dev_sector = r10_bio->devs[i].addr;
1079 int bad_sectors;
1080 int is_bad;
1081
1082 is_bad = is_badblock(rdev, dev_sector,
1083 max_sectors,
1084 &first_bad, &bad_sectors);
1085 if (is_bad < 0) {
1086 /* Mustn't write here until the bad block
1087 * is acknowledged
1088 */
1089 atomic_inc(&rdev->nr_pending);
1090 set_bit(BlockedBadBlocks, &rdev->flags);
1091 blocked_rdev = rdev;
1092 break;
1093 }
1094 if (is_bad && first_bad <= dev_sector) {
1095 /* Cannot write here at all */
1096 bad_sectors -= (dev_sector - first_bad);
1097 if (bad_sectors < max_sectors)
1098 /* Mustn't write more than bad_sectors
1099 * to other devices yet
1100 */
1101 max_sectors = bad_sectors;
1102 /* We don't set R10BIO_Degraded as that
1103 * only applies if the disk is missing,
1104 * so it might be re-added, and we want to
1105 * know to recover this chunk.
1106 * In this case the device is here, and the
1107 * fact that this chunk is not in-sync is
1108 * recorded in the bad block log.
1109 */
1110 continue;
1111 }
1112 if (is_bad) {
1113 int good_sectors = first_bad - dev_sector;
1114 if (good_sectors < max_sectors)
1115 max_sectors = good_sectors;
1116 }
1117 }
1118 r10_bio->devs[i].bio = bio;
1119 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121 rcu_read_unlock();
1122
Dan Williams6bfe0b42008-04-30 00:52:32 -07001123 if (unlikely(blocked_rdev)) {
1124 /* Have to wait for this device to get unblocked, then retry */
1125 int j;
1126 int d;
1127
1128 for (j = 0; j < i; j++)
1129 if (r10_bio->devs[j].bio) {
1130 d = r10_bio->devs[j].devnum;
1131 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1132 }
1133 allow_barrier(conf);
1134 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1135 wait_barrier(conf);
1136 goto retry_write;
1137 }
1138
NeilBrownd4432c22011-07-28 11:39:24 +10001139 if (max_sectors < r10_bio->sectors) {
1140 /* We are splitting this into multiple parts, so
1141 * we need to prepare for allocating another r10_bio.
1142 */
1143 r10_bio->sectors = max_sectors;
1144 spin_lock_irq(&conf->device_lock);
1145 if (bio->bi_phys_segments == 0)
1146 bio->bi_phys_segments = 2;
1147 else
1148 bio->bi_phys_segments++;
1149 spin_unlock_irq(&conf->device_lock);
1150 }
1151 sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
1152
NeilBrown4e780642010-10-19 12:54:01 +11001153 atomic_set(&r10_bio->remaining, 1);
NeilBrownd4432c22011-07-28 11:39:24 +10001154 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07001155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 for (i = 0; i < conf->copies; i++) {
1157 struct bio *mbio;
1158 int d = r10_bio->devs[i].devnum;
1159 if (!r10_bio->devs[i].bio)
1160 continue;
1161
NeilBrowna167f662010-10-26 18:31:13 +11001162 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrownd4432c22011-07-28 11:39:24 +10001163 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1164 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 r10_bio->devs[i].bio = mbio;
1166
NeilBrownd4432c22011-07-28 11:39:24 +10001167 mbio->bi_sector = (r10_bio->devs[i].addr+
1168 conf->mirrors[d].rdev->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1170 mbio->bi_end_io = raid10_end_write_request;
Tejun Heoe9c74692010-09-03 11:56:18 +02001171 mbio->bi_rw = WRITE | do_sync | do_fua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 mbio->bi_private = r10_bio;
1173
1174 atomic_inc(&r10_bio->remaining);
NeilBrown4e780642010-10-19 12:54:01 +11001175 spin_lock_irqsave(&conf->device_lock, flags);
1176 bio_list_add(&conf->pending_bio_list, mbio);
NeilBrown34db0cd2011-10-11 16:50:01 +11001177 conf->pending_count++;
NeilBrown4e780642010-10-19 12:54:01 +11001178 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
1180
NeilBrown079fa162011-09-10 17:21:23 +10001181 /* Don't remove the bias on 'remaining' (one_write_done) until
1182 * after checking if we need to go around again.
1183 */
NeilBrowna35e63e2008-03-04 14:29:29 -08001184
NeilBrownd4432c22011-07-28 11:39:24 +10001185 if (sectors_handled < (bio->bi_size >> 9)) {
NeilBrown079fa162011-09-10 17:21:23 +10001186 one_write_done(r10_bio);
NeilBrown5e570282011-07-28 11:39:25 +10001187 /* We need another r10_bio. It has already been counted
NeilBrownd4432c22011-07-28 11:39:24 +10001188 * in bio->bi_phys_segments.
1189 */
1190 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1191
1192 r10_bio->master_bio = bio;
1193 r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1194
1195 r10_bio->mddev = mddev;
1196 r10_bio->sector = bio->bi_sector + sectors_handled;
1197 r10_bio->state = 0;
1198 goto retry_write;
1199 }
NeilBrown079fa162011-09-10 17:21:23 +10001200 one_write_done(r10_bio);
1201
1202 /* In case raid10d snuck in to freeze_array */
1203 wake_up(&conf->wait_barrier);
NeilBrownd4432c22011-07-28 11:39:24 +10001204
NeilBrownc3b328a2011-04-18 18:25:43 +10001205 if (do_sync || !mddev->bitmap || !plugged)
Lars Ellenberge3881a62007-01-10 23:15:37 -08001206 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
NeilBrownfd01b882011-10-11 16:47:53 +11001209static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
NeilBrowne879a872011-10-11 16:49:02 +11001211 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 int i;
1213
1214 if (conf->near_copies < conf->raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +10001215 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (conf->near_copies > 1)
1217 seq_printf(seq, " %d near-copies", conf->near_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001218 if (conf->far_copies > 1) {
1219 if (conf->far_offset)
1220 seq_printf(seq, " %d offset-copies", conf->far_copies);
1221 else
1222 seq_printf(seq, " %d far-copies", conf->far_copies);
1223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown76186dd2006-10-03 01:15:48 -07001225 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 for (i = 0; i < conf->raid_disks; i++)
1227 seq_printf(seq, "%s",
1228 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001229 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 seq_printf(seq, "]");
1231}
1232
NeilBrown700c7212011-07-27 11:00:36 +10001233/* check if there are enough drives for
1234 * every block to appear on atleast one.
1235 * Don't consider the device numbered 'ignore'
1236 * as we might be about to remove it.
1237 */
NeilBrowne879a872011-10-11 16:49:02 +11001238static int enough(struct r10conf *conf, int ignore)
NeilBrown700c7212011-07-27 11:00:36 +10001239{
1240 int first = 0;
1241
1242 do {
1243 int n = conf->copies;
1244 int cnt = 0;
1245 while (n--) {
1246 if (conf->mirrors[first].rdev &&
1247 first != ignore)
1248 cnt++;
1249 first = (first+1) % conf->raid_disks;
1250 }
1251 if (cnt == 0)
1252 return 0;
1253 } while (first != 0);
1254 return 1;
1255}
1256
NeilBrownfd01b882011-10-11 16:47:53 +11001257static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
1259 char b[BDEVNAME_SIZE];
NeilBrowne879a872011-10-11 16:49:02 +11001260 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 /*
1263 * If it is not operational, then we have already marked it as dead
1264 * else if it is the last working disks, ignore the error, let the
1265 * next level up know.
1266 * else mark the drive as failed
1267 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001268 if (test_bit(In_sync, &rdev->flags)
NeilBrown700c7212011-07-27 11:00:36 +10001269 && !enough(conf, rdev->raid_disk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /*
1271 * Don't fail the drive, just return an IO error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 */
1273 return;
NeilBrownc04be0a2006-10-03 01:15:53 -07001274 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1275 unsigned long flags;
1276 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001278 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 /*
1280 * if recovery is running, make sure it aborts.
1281 */
NeilBrowndfc70642008-05-23 13:04:39 -07001282 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
NeilBrownde393cd2011-07-28 11:31:48 +10001284 set_bit(Blocked, &rdev->flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001285 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -07001286 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Joe Perches067032b2011-01-14 09:14:33 +11001287 printk(KERN_ALERT
1288 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1289 "md/raid10:%s: Operation continuing on %d devices.\n",
NeilBrown128595e2010-05-03 14:47:14 +10001290 mdname(mddev), bdevname(rdev->bdev, b),
1291 mdname(mddev), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
NeilBrowne879a872011-10-11 16:49:02 +11001294static void print_conf(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
1296 int i;
NeilBrown0f6d02d2011-10-11 16:48:46 +11001297 struct mirror_info *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
NeilBrown128595e2010-05-03 14:47:14 +10001299 printk(KERN_DEBUG "RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (!conf) {
NeilBrown128595e2010-05-03 14:47:14 +10001301 printk(KERN_DEBUG "(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return;
1303 }
NeilBrown128595e2010-05-03 14:47:14 +10001304 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 conf->raid_disks);
1306
1307 for (i = 0; i < conf->raid_disks; i++) {
1308 char b[BDEVNAME_SIZE];
1309 tmp = conf->mirrors + i;
1310 if (tmp->rdev)
NeilBrown128595e2010-05-03 14:47:14 +10001311 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001312 i, !test_bit(In_sync, &tmp->rdev->flags),
1313 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 bdevname(tmp->rdev->bdev,b));
1315 }
1316}
1317
NeilBrowne879a872011-10-11 16:49:02 +11001318static void close_sync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
NeilBrown0a27ec92006-01-06 00:20:13 -08001320 wait_barrier(conf);
1321 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 mempool_destroy(conf->r10buf_pool);
1324 conf->r10buf_pool = NULL;
1325}
1326
NeilBrownfd01b882011-10-11 16:47:53 +11001327static int raid10_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 int i;
NeilBrowne879a872011-10-11 16:49:02 +11001330 struct r10conf *conf = mddev->private;
NeilBrown0f6d02d2011-10-11 16:48:46 +11001331 struct mirror_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10001332 int count = 0;
1333 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 /*
1336 * Find all non-in_sync disks within the RAID10 configuration
1337 * and mark them in_sync
1338 */
1339 for (i = 0; i < conf->raid_disks; i++) {
1340 tmp = conf->mirrors + i;
1341 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08001342 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001343 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001344 count++;
Adrian Drzewieckie6ffbcb2010-08-18 11:49:02 +10001345 sysfs_notify_dirent(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
1347 }
NeilBrown6b965622010-08-18 11:56:59 +10001348 spin_lock_irqsave(&conf->device_lock, flags);
1349 mddev->degraded -= count;
1350 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001353 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
1355
1356
NeilBrownfd01b882011-10-11 16:47:53 +11001357static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
NeilBrowne879a872011-10-11 16:49:02 +11001359 struct r10conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001360 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 int mirror;
Neil Brown6c2fce22008-06-28 08:31:31 +10001362 int first = 0;
NeilBrown84707f32010-03-16 17:23:35 +11001363 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 if (mddev->recovery_cp < MaxSector)
1366 /* only hot-add to in-sync arrays, as recovery is
1367 * very different from resync
1368 */
Neil Brown199050e2008-06-28 08:31:33 +10001369 return -EBUSY;
NeilBrown700c7212011-07-27 11:00:36 +10001370 if (!enough(conf, -1))
Neil Brown199050e2008-06-28 08:31:33 +10001371 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
NeilBrowna53a6c82008-11-06 17:28:20 +11001373 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001374 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Namhyung Kim2c4193d2011-07-18 17:38:43 +10001376 if (rdev->saved_raid_disk >= first &&
NeilBrown6cce3b22006-01-06 00:20:16 -08001377 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1378 mirror = rdev->saved_raid_disk;
1379 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001380 mirror = first;
NeilBrown2bb77732011-07-27 11:00:36 +10001381 for ( ; mirror <= last ; mirror++) {
NeilBrown0f6d02d2011-10-11 16:48:46 +11001382 struct mirror_info *p = &conf->mirrors[mirror];
NeilBrown2bb77732011-07-27 11:00:36 +10001383 if (p->recovery_disabled == mddev->recovery_disabled)
1384 continue;
NeilBrown7fcc7c82011-10-31 12:59:44 +11001385 if (p->rdev)
NeilBrown2bb77732011-07-27 11:00:36 +10001386 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
NeilBrown2bb77732011-07-27 11:00:36 +10001388 disk_stack_limits(mddev->gendisk, rdev->bdev,
1389 rdev->data_offset << 9);
1390 /* as we don't honour merge_bvec_fn, we must
1391 * never risk violating it, so limit
1392 * ->max_segments to one lying with a single
1393 * page, as a one page request is never in
1394 * violation.
1395 */
1396 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1397 blk_queue_max_segments(mddev->queue, 1);
1398 blk_queue_segment_boundary(mddev->queue,
1399 PAGE_CACHE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401
NeilBrown2bb77732011-07-27 11:00:36 +10001402 p->head_position = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11001403 p->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown2bb77732011-07-27 11:00:36 +10001404 rdev->raid_disk = mirror;
1405 err = 0;
1406 if (rdev->saved_raid_disk != mirror)
1407 conf->fullsync = 1;
1408 rcu_assign_pointer(p->rdev, rdev);
1409 break;
1410 }
1411
Andre Nollac5e7112009-08-03 10:59:47 +10001412 md_integrity_add_rdev(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001414 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
NeilBrownb8321b62011-12-23 10:17:51 +11001417static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
NeilBrowne879a872011-10-11 16:49:02 +11001419 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11001421 int number = rdev->raid_disk;
NeilBrown0f6d02d2011-10-11 16:48:46 +11001422 struct mirror_info *p = conf->mirrors+ number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 print_conf(conf);
NeilBrownb8321b62011-12-23 10:17:51 +11001425 if (rdev == p->rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001426 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 atomic_read(&rdev->nr_pending)) {
1428 err = -EBUSY;
1429 goto abort;
1430 }
NeilBrowndfc70642008-05-23 13:04:39 -07001431 /* Only remove faulty devices in recovery
1432 * is not possible.
1433 */
1434 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown2bb77732011-07-27 11:00:36 +10001435 mddev->recovery_disabled != p->recovery_disabled &&
NeilBrown700c7212011-07-27 11:00:36 +10001436 enough(conf, -1)) {
NeilBrowndfc70642008-05-23 13:04:39 -07001437 err = -EBUSY;
1438 goto abort;
1439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001441 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 if (atomic_read(&rdev->nr_pending)) {
1443 /* lost the race, try later */
1444 err = -EBUSY;
1445 p->rdev = rdev;
Andre Nollac5e7112009-08-03 10:59:47 +10001446 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001448 err = md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450abort:
1451
1452 print_conf(conf);
1453 return err;
1454}
1455
1456
NeilBrown6712ecf2007-09-27 12:47:43 +02001457static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001459 struct r10bio *r10_bio = bio->bi_private;
NeilBrowne879a872011-10-11 16:49:02 +11001460 struct r10conf *conf = r10_bio->mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001461 int d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
NeilBrown69335ef2011-12-23 10:17:54 +11001463 d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001464
1465 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1466 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrowne684e412011-07-28 11:39:25 +10001467 else
1468 /* The write handler will notice the lack of
1469 * R10BIO_Uptodate and record any errors etc
1470 */
NeilBrown4dbcdc72006-01-06 00:20:52 -08001471 atomic_add(r10_bio->sectors,
1472 &conf->mirrors[d].rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 /* for reconstruct, we always reschedule after a read.
1475 * for resync, only after all reads
1476 */
NeilBrown73d5c382009-02-25 13:18:47 +11001477 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1479 atomic_dec_and_test(&r10_bio->remaining)) {
1480 /* we have read all the blocks,
1481 * do the comparison in process context in raid10d
1482 */
1483 reschedule_retry(r10_bio);
1484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
NeilBrown9f2c9d12011-10-11 16:48:43 +11001487static void end_sync_request(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001488{
NeilBrownfd01b882011-10-11 16:47:53 +11001489 struct mddev *mddev = r10_bio->mddev;
NeilBrown5e570282011-07-28 11:39:25 +10001490
1491 while (atomic_dec_and_test(&r10_bio->remaining)) {
1492 if (r10_bio->master_bio == NULL) {
1493 /* the primary of several recovery bios */
1494 sector_t s = r10_bio->sectors;
1495 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1496 test_bit(R10BIO_WriteError, &r10_bio->state))
1497 reschedule_retry(r10_bio);
1498 else
1499 put_buf(r10_bio);
1500 md_done_sync(mddev, s, 1);
1501 break;
1502 } else {
NeilBrown9f2c9d12011-10-11 16:48:43 +11001503 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
NeilBrown5e570282011-07-28 11:39:25 +10001504 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1505 test_bit(R10BIO_WriteError, &r10_bio->state))
1506 reschedule_retry(r10_bio);
1507 else
1508 put_buf(r10_bio);
1509 r10_bio = r10_bio2;
1510 }
1511 }
1512}
1513
NeilBrown6712ecf2007-09-27 12:47:43 +02001514static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +11001517 struct r10bio *r10_bio = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11001518 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001519 struct r10conf *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001520 int d;
NeilBrown749c55e2011-07-28 11:39:24 +10001521 sector_t first_bad;
1522 int bad_sectors;
1523 int slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
NeilBrown69335ef2011-12-23 10:17:54 +11001525 d = find_bio_disk(conf, r10_bio, bio, &slot, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
NeilBrown1a0b7cd2011-07-28 11:39:25 +10001527 if (!uptodate) {
1528 set_bit(WriteErrorSeen, &conf->mirrors[d].rdev->flags);
1529 set_bit(R10BIO_WriteError, &r10_bio->state);
1530 } else if (is_badblock(conf->mirrors[d].rdev,
NeilBrown749c55e2011-07-28 11:39:24 +10001531 r10_bio->devs[slot].addr,
1532 r10_bio->sectors,
1533 &first_bad, &bad_sectors))
1534 set_bit(R10BIO_MadeGood, &r10_bio->state);
NeilBrowndfc70642008-05-23 13:04:39 -07001535
NeilBrown73d5c382009-02-25 13:18:47 +11001536 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
NeilBrown5e570282011-07-28 11:39:25 +10001537
1538 end_sync_request(r10_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
1541/*
1542 * Note: sync and recover and handled very differently for raid10
1543 * This code is for resync.
1544 * For resync, we read through virtual addresses and read all blocks.
1545 * If there is any error, we schedule a write. The lowest numbered
1546 * drive is authoritative.
1547 * However requests come for physical address, so we need to map.
1548 * For every physical address there are raid_disks/copies virtual addresses,
1549 * which is always are least one, but is not necessarly an integer.
1550 * This means that a physical address can span multiple chunks, so we may
1551 * have to submit multiple io requests for a single sync request.
1552 */
1553/*
1554 * We check if all blocks are in-sync and only write to blocks that
1555 * aren't in sync
1556 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11001557static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
NeilBrowne879a872011-10-11 16:49:02 +11001559 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 int i, first;
1561 struct bio *tbio, *fbio;
1562
1563 atomic_set(&r10_bio->remaining, 1);
1564
1565 /* find the first device with a block */
1566 for (i=0; i<conf->copies; i++)
1567 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1568 break;
1569
1570 if (i == conf->copies)
1571 goto done;
1572
1573 first = i;
1574 fbio = r10_bio->devs[i].bio;
1575
1576 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001577 for (i=0 ; i < conf->copies ; i++) {
1578 int j, d;
1579 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001582
1583 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001585 if (i == first)
1586 continue;
1587 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1588 /* We know that the bi_io_vec layout is the same for
1589 * both 'first' and 'i', so we just compare them.
1590 * All vec entries are PAGE_SIZE;
1591 */
1592 for (j = 0; j < vcnt; j++)
1593 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1594 page_address(tbio->bi_io_vec[j].bv_page),
1595 PAGE_SIZE))
1596 break;
1597 if (j == vcnt)
1598 continue;
1599 mddev->resync_mismatches += r10_bio->sectors;
NeilBrownf84ee362011-07-28 11:39:25 +10001600 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1601 /* Don't fix anything. */
1602 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001603 }
NeilBrownf84ee362011-07-28 11:39:25 +10001604 /* Ok, we need to write this bio, either to correct an
1605 * inconsistency or to correct an unreadable block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 * First we need to fixup bv_offset, bv_len and
1607 * bi_vecs, as the read request might have corrupted these
1608 */
1609 tbio->bi_vcnt = vcnt;
1610 tbio->bi_size = r10_bio->sectors << 9;
1611 tbio->bi_idx = 0;
1612 tbio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1614 tbio->bi_flags |= 1 << BIO_UPTODATE;
1615 tbio->bi_next = NULL;
1616 tbio->bi_rw = WRITE;
1617 tbio->bi_private = r10_bio;
1618 tbio->bi_sector = r10_bio->devs[i].addr;
1619
1620 for (j=0; j < vcnt ; j++) {
1621 tbio->bi_io_vec[j].bv_offset = 0;
1622 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1623
1624 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1625 page_address(fbio->bi_io_vec[j].bv_page),
1626 PAGE_SIZE);
1627 }
1628 tbio->bi_end_io = end_sync_write;
1629
1630 d = r10_bio->devs[i].devnum;
1631 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1632 atomic_inc(&r10_bio->remaining);
1633 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1634
1635 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1636 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1637 generic_make_request(tbio);
1638 }
1639
1640done:
1641 if (atomic_dec_and_test(&r10_bio->remaining)) {
1642 md_done_sync(mddev, r10_bio->sectors, 1);
1643 put_buf(r10_bio);
1644 }
1645}
1646
1647/*
1648 * Now for the recovery code.
1649 * Recovery happens across physical sectors.
1650 * We recover all non-is_sync drives by finding the virtual address of
1651 * each, and then choose a working drive that also has that virt address.
1652 * There is a separate r10_bio for each non-in_sync drive.
1653 * Only the first two slots are in use. The first for reading,
1654 * The second for writing.
1655 *
1656 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11001657static void fix_recovery_read_error(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001658{
1659 /* We got a read error during recovery.
1660 * We repeat the read in smaller page-sized sections.
1661 * If a read succeeds, write it to the new device or record
1662 * a bad block if we cannot.
1663 * If a read fails, record a bad block on both old and
1664 * new devices.
1665 */
NeilBrownfd01b882011-10-11 16:47:53 +11001666 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001667 struct r10conf *conf = mddev->private;
NeilBrown5e570282011-07-28 11:39:25 +10001668 struct bio *bio = r10_bio->devs[0].bio;
1669 sector_t sect = 0;
1670 int sectors = r10_bio->sectors;
1671 int idx = 0;
1672 int dr = r10_bio->devs[0].devnum;
1673 int dw = r10_bio->devs[1].devnum;
1674
1675 while (sectors) {
1676 int s = sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11001677 struct md_rdev *rdev;
NeilBrown5e570282011-07-28 11:39:25 +10001678 sector_t addr;
1679 int ok;
1680
1681 if (s > (PAGE_SIZE>>9))
1682 s = PAGE_SIZE >> 9;
1683
1684 rdev = conf->mirrors[dr].rdev;
1685 addr = r10_bio->devs[0].addr + sect,
1686 ok = sync_page_io(rdev,
1687 addr,
1688 s << 9,
1689 bio->bi_io_vec[idx].bv_page,
1690 READ, false);
1691 if (ok) {
1692 rdev = conf->mirrors[dw].rdev;
1693 addr = r10_bio->devs[1].addr + sect;
1694 ok = sync_page_io(rdev,
1695 addr,
1696 s << 9,
1697 bio->bi_io_vec[idx].bv_page,
1698 WRITE, false);
1699 if (!ok)
1700 set_bit(WriteErrorSeen, &rdev->flags);
1701 }
1702 if (!ok) {
1703 /* We don't worry if we cannot set a bad block -
1704 * it really is bad so there is no loss in not
1705 * recording it yet
1706 */
1707 rdev_set_badblocks(rdev, addr, s, 0);
1708
1709 if (rdev != conf->mirrors[dw].rdev) {
1710 /* need bad block on destination too */
NeilBrown3cb03002011-10-11 16:45:26 +11001711 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
NeilBrown5e570282011-07-28 11:39:25 +10001712 addr = r10_bio->devs[1].addr + sect;
1713 ok = rdev_set_badblocks(rdev2, addr, s, 0);
1714 if (!ok) {
1715 /* just abort the recovery */
1716 printk(KERN_NOTICE
1717 "md/raid10:%s: recovery aborted"
1718 " due to read error\n",
1719 mdname(mddev));
1720
1721 conf->mirrors[dw].recovery_disabled
1722 = mddev->recovery_disabled;
1723 set_bit(MD_RECOVERY_INTR,
1724 &mddev->recovery);
1725 break;
1726 }
1727 }
1728 }
1729
1730 sectors -= s;
1731 sect += s;
1732 idx++;
1733 }
1734}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
NeilBrown9f2c9d12011-10-11 16:48:43 +11001736static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
NeilBrowne879a872011-10-11 16:49:02 +11001738 struct r10conf *conf = mddev->private;
Namhyung Kimc65060a2011-07-18 17:38:49 +10001739 int d;
1740 struct bio *wbio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
NeilBrown5e570282011-07-28 11:39:25 +10001742 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
1743 fix_recovery_read_error(r10_bio);
1744 end_sync_request(r10_bio);
1745 return;
1746 }
1747
Namhyung Kimc65060a2011-07-18 17:38:49 +10001748 /*
1749 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 * and submit the write request
1751 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 wbio = r10_bio->devs[1].bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 d = r10_bio->devs[1].devnum;
1754
1755 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1756 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
NeilBrown5e570282011-07-28 11:39:25 +10001757 generic_make_request(wbio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
1760
1761/*
Robert Becker1e509152009-12-14 12:49:58 +11001762 * Used by fix_read_error() to decay the per rdev read_errors.
1763 * We halve the read error count for every hour that has elapsed
1764 * since the last recorded read error.
1765 *
1766 */
NeilBrownfd01b882011-10-11 16:47:53 +11001767static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
Robert Becker1e509152009-12-14 12:49:58 +11001768{
1769 struct timespec cur_time_mon;
1770 unsigned long hours_since_last;
1771 unsigned int read_errors = atomic_read(&rdev->read_errors);
1772
1773 ktime_get_ts(&cur_time_mon);
1774
1775 if (rdev->last_read_error.tv_sec == 0 &&
1776 rdev->last_read_error.tv_nsec == 0) {
1777 /* first time we've seen a read error */
1778 rdev->last_read_error = cur_time_mon;
1779 return;
1780 }
1781
1782 hours_since_last = (cur_time_mon.tv_sec -
1783 rdev->last_read_error.tv_sec) / 3600;
1784
1785 rdev->last_read_error = cur_time_mon;
1786
1787 /*
1788 * if hours_since_last is > the number of bits in read_errors
1789 * just set read errors to 0. We do this to avoid
1790 * overflowing the shift of read_errors by hours_since_last.
1791 */
1792 if (hours_since_last >= 8 * sizeof(read_errors))
1793 atomic_set(&rdev->read_errors, 0);
1794 else
1795 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
1796}
1797
NeilBrown3cb03002011-10-11 16:45:26 +11001798static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrown58c54fc2011-07-28 11:39:25 +10001799 int sectors, struct page *page, int rw)
1800{
1801 sector_t first_bad;
1802 int bad_sectors;
1803
1804 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
1805 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
1806 return -1;
1807 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
1808 /* success */
1809 return 1;
1810 if (rw == WRITE)
1811 set_bit(WriteErrorSeen, &rdev->flags);
1812 /* need to record an error - either for the block or the device */
1813 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
1814 md_error(rdev->mddev, rdev);
1815 return 0;
1816}
1817
Robert Becker1e509152009-12-14 12:49:58 +11001818/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 * This is a kernel thread which:
1820 *
1821 * 1. Retries failed read operations on working mirrors.
1822 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07001823 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 */
1825
NeilBrowne879a872011-10-11 16:49:02 +11001826static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown6814d532006-10-03 01:15:45 -07001827{
1828 int sect = 0; /* Offset from r10_bio->sector */
1829 int sectors = r10_bio->sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11001830 struct md_rdev*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11001831 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001832 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11001833
NeilBrown7c4e06f2011-05-11 14:53:17 +10001834 /* still own a reference to this rdev, so it cannot
1835 * have been cleared recently.
1836 */
1837 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11001838
NeilBrown7c4e06f2011-05-11 14:53:17 +10001839 if (test_bit(Faulty, &rdev->flags))
1840 /* drive has already been failed, just ignore any
1841 more fix_read_error() attempts */
1842 return;
1843
1844 check_decay_read_errors(mddev, rdev);
1845 atomic_inc(&rdev->read_errors);
1846 if (atomic_read(&rdev->read_errors) > max_read_errors) {
1847 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11001848 bdevname(rdev->bdev, b);
1849
NeilBrown7c4e06f2011-05-11 14:53:17 +10001850 printk(KERN_NOTICE
1851 "md/raid10:%s: %s: Raid device exceeded "
1852 "read_error threshold [cur %d:max %d]\n",
1853 mdname(mddev), b,
1854 atomic_read(&rdev->read_errors), max_read_errors);
1855 printk(KERN_NOTICE
1856 "md/raid10:%s: %s: Failing raid device\n",
1857 mdname(mddev), b);
1858 md_error(mddev, conf->mirrors[d].rdev);
1859 return;
Robert Becker1e509152009-12-14 12:49:58 +11001860 }
Robert Becker1e509152009-12-14 12:49:58 +11001861
NeilBrown6814d532006-10-03 01:15:45 -07001862 while(sectors) {
1863 int s = sectors;
1864 int sl = r10_bio->read_slot;
1865 int success = 0;
1866 int start;
1867
1868 if (s > (PAGE_SIZE>>9))
1869 s = PAGE_SIZE >> 9;
1870
1871 rcu_read_lock();
1872 do {
NeilBrown8dbed5c2011-07-28 11:39:24 +10001873 sector_t first_bad;
1874 int bad_sectors;
1875
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001876 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07001877 rdev = rcu_dereference(conf->mirrors[d].rdev);
1878 if (rdev &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10001879 test_bit(In_sync, &rdev->flags) &&
1880 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
1881 &first_bad, &bad_sectors) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07001882 atomic_inc(&rdev->nr_pending);
1883 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11001884 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07001885 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001886 sect,
NeilBrown6814d532006-10-03 01:15:45 -07001887 s<<9,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001888 conf->tmppage, READ, false);
NeilBrown6814d532006-10-03 01:15:45 -07001889 rdev_dec_pending(rdev, mddev);
1890 rcu_read_lock();
1891 if (success)
1892 break;
1893 }
1894 sl++;
1895 if (sl == conf->copies)
1896 sl = 0;
1897 } while (!success && sl != r10_bio->read_slot);
1898 rcu_read_unlock();
1899
1900 if (!success) {
NeilBrown58c54fc2011-07-28 11:39:25 +10001901 /* Cannot read from anywhere, just mark the block
1902 * as bad on the first device to discourage future
1903 * reads.
1904 */
NeilBrown6814d532006-10-03 01:15:45 -07001905 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
NeilBrown58c54fc2011-07-28 11:39:25 +10001906 rdev = conf->mirrors[dn].rdev;
1907
1908 if (!rdev_set_badblocks(
1909 rdev,
1910 r10_bio->devs[r10_bio->read_slot].addr
1911 + sect,
1912 s, 0))
1913 md_error(mddev, rdev);
NeilBrown6814d532006-10-03 01:15:45 -07001914 break;
1915 }
1916
1917 start = sl;
1918 /* write it back and re-read */
1919 rcu_read_lock();
1920 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11001921 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001922
NeilBrown6814d532006-10-03 01:15:45 -07001923 if (sl==0)
1924 sl = conf->copies;
1925 sl--;
1926 d = r10_bio->devs[sl].devnum;
1927 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10001928 if (!rdev ||
1929 !test_bit(In_sync, &rdev->flags))
1930 continue;
1931
1932 atomic_inc(&rdev->nr_pending);
1933 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10001934 if (r10_sync_page_io(rdev,
1935 r10_bio->devs[sl].addr +
1936 sect,
1937 s<<9, conf->tmppage, WRITE)
NeilBrown1294b9c2011-07-28 11:39:23 +10001938 == 0) {
1939 /* Well, this device is dead */
1940 printk(KERN_NOTICE
1941 "md/raid10:%s: read correction "
1942 "write failed"
1943 " (%d sectors at %llu on %s)\n",
1944 mdname(mddev), s,
1945 (unsigned long long)(
1946 sect + rdev->data_offset),
1947 bdevname(rdev->bdev, b));
1948 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
1949 "drive\n",
1950 mdname(mddev),
1951 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07001952 }
NeilBrown1294b9c2011-07-28 11:39:23 +10001953 rdev_dec_pending(rdev, mddev);
1954 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07001955 }
1956 sl = start;
1957 while (sl != r10_bio->read_slot) {
NeilBrown1294b9c2011-07-28 11:39:23 +10001958 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001959
NeilBrown6814d532006-10-03 01:15:45 -07001960 if (sl==0)
1961 sl = conf->copies;
1962 sl--;
1963 d = r10_bio->devs[sl].devnum;
1964 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10001965 if (!rdev ||
1966 !test_bit(In_sync, &rdev->flags))
1967 continue;
Robert Becker67b8dc42009-12-14 12:49:57 +11001968
NeilBrown1294b9c2011-07-28 11:39:23 +10001969 atomic_inc(&rdev->nr_pending);
1970 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10001971 switch (r10_sync_page_io(rdev,
1972 r10_bio->devs[sl].addr +
1973 sect,
1974 s<<9, conf->tmppage,
1975 READ)) {
1976 case 0:
NeilBrown1294b9c2011-07-28 11:39:23 +10001977 /* Well, this device is dead */
1978 printk(KERN_NOTICE
1979 "md/raid10:%s: unable to read back "
1980 "corrected sectors"
1981 " (%d sectors at %llu on %s)\n",
1982 mdname(mddev), s,
1983 (unsigned long long)(
1984 sect + rdev->data_offset),
1985 bdevname(rdev->bdev, b));
1986 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
1987 "drive\n",
1988 mdname(mddev),
1989 bdevname(rdev->bdev, b));
NeilBrown58c54fc2011-07-28 11:39:25 +10001990 break;
1991 case 1:
NeilBrown1294b9c2011-07-28 11:39:23 +10001992 printk(KERN_INFO
1993 "md/raid10:%s: read error corrected"
1994 " (%d sectors at %llu on %s)\n",
1995 mdname(mddev), s,
1996 (unsigned long long)(
1997 sect + rdev->data_offset),
1998 bdevname(rdev->bdev, b));
1999 atomic_add(s, &rdev->corrected_errors);
NeilBrown6814d532006-10-03 01:15:45 -07002000 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002001
2002 rdev_dec_pending(rdev, mddev);
2003 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002004 }
2005 rcu_read_unlock();
2006
2007 sectors -= s;
2008 sect += s;
2009 }
2010}
2011
NeilBrownbd870a12011-07-28 11:39:24 +10002012static void bi_complete(struct bio *bio, int error)
2013{
2014 complete((struct completion *)bio->bi_private);
2015}
2016
2017static int submit_bio_wait(int rw, struct bio *bio)
2018{
2019 struct completion event;
2020 rw |= REQ_SYNC;
2021
2022 init_completion(&event);
2023 bio->bi_private = &event;
2024 bio->bi_end_io = bi_complete;
2025 submit_bio(rw, bio);
2026 wait_for_completion(&event);
2027
2028 return test_bit(BIO_UPTODATE, &bio->bi_flags);
2029}
2030
NeilBrown9f2c9d12011-10-11 16:48:43 +11002031static int narrow_write_error(struct r10bio *r10_bio, int i)
NeilBrownbd870a12011-07-28 11:39:24 +10002032{
2033 struct bio *bio = r10_bio->master_bio;
NeilBrownfd01b882011-10-11 16:47:53 +11002034 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002035 struct r10conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002036 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
NeilBrownbd870a12011-07-28 11:39:24 +10002037 /* bio has the data to be written to slot 'i' where
2038 * we just recently had a write error.
2039 * We repeatedly clone the bio and trim down to one block,
2040 * then try the write. Where the write fails we record
2041 * a bad block.
2042 * It is conceivable that the bio doesn't exactly align with
2043 * blocks. We must handle this.
2044 *
2045 * We currently own a reference to the rdev.
2046 */
2047
2048 int block_sectors;
2049 sector_t sector;
2050 int sectors;
2051 int sect_to_write = r10_bio->sectors;
2052 int ok = 1;
2053
2054 if (rdev->badblocks.shift < 0)
2055 return 0;
2056
2057 block_sectors = 1 << rdev->badblocks.shift;
2058 sector = r10_bio->sector;
2059 sectors = ((r10_bio->sector + block_sectors)
2060 & ~(sector_t)(block_sectors - 1))
2061 - sector;
2062
2063 while (sect_to_write) {
2064 struct bio *wbio;
2065 if (sectors > sect_to_write)
2066 sectors = sect_to_write;
2067 /* Write at 'sector' for 'sectors' */
2068 wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
2069 md_trim_bio(wbio, sector - bio->bi_sector, sectors);
2070 wbio->bi_sector = (r10_bio->devs[i].addr+
2071 rdev->data_offset+
2072 (sector - r10_bio->sector));
2073 wbio->bi_bdev = rdev->bdev;
2074 if (submit_bio_wait(WRITE, wbio) == 0)
2075 /* Failure! */
2076 ok = rdev_set_badblocks(rdev, sector,
2077 sectors, 0)
2078 && ok;
2079
2080 bio_put(wbio);
2081 sect_to_write -= sectors;
2082 sector += sectors;
2083 sectors = block_sectors;
2084 }
2085 return ok;
2086}
2087
NeilBrown9f2c9d12011-10-11 16:48:43 +11002088static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown560f8e52011-07-28 11:39:23 +10002089{
2090 int slot = r10_bio->read_slot;
2091 int mirror = r10_bio->devs[slot].devnum;
2092 struct bio *bio;
NeilBrowne879a872011-10-11 16:49:02 +11002093 struct r10conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002094 struct md_rdev *rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002095 char b[BDEVNAME_SIZE];
2096 unsigned long do_sync;
NeilBrown856e08e2011-07-28 11:39:23 +10002097 int max_sectors;
NeilBrown560f8e52011-07-28 11:39:23 +10002098
2099 /* we got a read error. Maybe the drive is bad. Maybe just
2100 * the block and we can fix it.
2101 * We freeze all other IO, and try reading the block from
2102 * other devices. When we find one, we re-write
2103 * and check it that fixes the read error.
2104 * This is all done synchronously while the array is
2105 * frozen.
2106 */
2107 if (mddev->ro == 0) {
2108 freeze_array(conf);
2109 fix_read_error(conf, mddev, r10_bio);
2110 unfreeze_array(conf);
2111 }
2112 rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
2113
2114 bio = r10_bio->devs[slot].bio;
NeilBrown7399c312011-07-28 11:39:23 +10002115 bdevname(bio->bi_bdev, b);
NeilBrown560f8e52011-07-28 11:39:23 +10002116 r10_bio->devs[slot].bio =
2117 mddev->ro ? IO_BLOCKED : NULL;
NeilBrown7399c312011-07-28 11:39:23 +10002118read_more:
NeilBrown856e08e2011-07-28 11:39:23 +10002119 mirror = read_balance(conf, r10_bio, &max_sectors);
NeilBrown7399c312011-07-28 11:39:23 +10002120 if (mirror == -1) {
NeilBrown560f8e52011-07-28 11:39:23 +10002121 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
2122 " read error for block %llu\n",
NeilBrown7399c312011-07-28 11:39:23 +10002123 mdname(mddev), b,
NeilBrown560f8e52011-07-28 11:39:23 +10002124 (unsigned long long)r10_bio->sector);
2125 raid_end_bio_io(r10_bio);
2126 bio_put(bio);
2127 return;
2128 }
2129
2130 do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
NeilBrown7399c312011-07-28 11:39:23 +10002131 if (bio)
2132 bio_put(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002133 slot = r10_bio->read_slot;
2134 rdev = conf->mirrors[mirror].rdev;
2135 printk_ratelimited(
2136 KERN_ERR
2137 "md/raid10:%s: %s: redirecting"
2138 "sector %llu to another mirror\n",
2139 mdname(mddev),
2140 bdevname(rdev->bdev, b),
2141 (unsigned long long)r10_bio->sector);
2142 bio = bio_clone_mddev(r10_bio->master_bio,
2143 GFP_NOIO, mddev);
NeilBrown7399c312011-07-28 11:39:23 +10002144 md_trim_bio(bio,
2145 r10_bio->sector - bio->bi_sector,
2146 max_sectors);
NeilBrown560f8e52011-07-28 11:39:23 +10002147 r10_bio->devs[slot].bio = bio;
2148 bio->bi_sector = r10_bio->devs[slot].addr
2149 + rdev->data_offset;
2150 bio->bi_bdev = rdev->bdev;
2151 bio->bi_rw = READ | do_sync;
2152 bio->bi_private = r10_bio;
2153 bio->bi_end_io = raid10_end_read_request;
NeilBrown7399c312011-07-28 11:39:23 +10002154 if (max_sectors < r10_bio->sectors) {
2155 /* Drat - have to split this up more */
2156 struct bio *mbio = r10_bio->master_bio;
2157 int sectors_handled =
2158 r10_bio->sector + max_sectors
2159 - mbio->bi_sector;
2160 r10_bio->sectors = max_sectors;
2161 spin_lock_irq(&conf->device_lock);
2162 if (mbio->bi_phys_segments == 0)
2163 mbio->bi_phys_segments = 2;
2164 else
2165 mbio->bi_phys_segments++;
2166 spin_unlock_irq(&conf->device_lock);
2167 generic_make_request(bio);
2168 bio = NULL;
2169
2170 r10_bio = mempool_alloc(conf->r10bio_pool,
2171 GFP_NOIO);
2172 r10_bio->master_bio = mbio;
2173 r10_bio->sectors = (mbio->bi_size >> 9)
2174 - sectors_handled;
2175 r10_bio->state = 0;
2176 set_bit(R10BIO_ReadError,
2177 &r10_bio->state);
2178 r10_bio->mddev = mddev;
2179 r10_bio->sector = mbio->bi_sector
2180 + sectors_handled;
2181
2182 goto read_more;
2183 } else
2184 generic_make_request(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002185}
2186
NeilBrowne879a872011-10-11 16:49:02 +11002187static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
NeilBrown749c55e2011-07-28 11:39:24 +10002188{
2189 /* Some sort of write request has finished and it
2190 * succeeded in writing where we thought there was a
2191 * bad block. So forget the bad block.
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002192 * Or possibly if failed and we need to record
2193 * a bad block.
NeilBrown749c55e2011-07-28 11:39:24 +10002194 */
2195 int m;
NeilBrown3cb03002011-10-11 16:45:26 +11002196 struct md_rdev *rdev;
NeilBrown749c55e2011-07-28 11:39:24 +10002197
2198 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2199 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002200 for (m = 0; m < conf->copies; m++) {
2201 int dev = r10_bio->devs[m].devnum;
2202 rdev = conf->mirrors[dev].rdev;
2203 if (r10_bio->devs[m].bio == NULL)
2204 continue;
2205 if (test_bit(BIO_UPTODATE,
NeilBrown749c55e2011-07-28 11:39:24 +10002206 &r10_bio->devs[m].bio->bi_flags)) {
NeilBrown749c55e2011-07-28 11:39:24 +10002207 rdev_clear_badblocks(
2208 rdev,
2209 r10_bio->devs[m].addr,
2210 r10_bio->sectors);
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002211 } else {
2212 if (!rdev_set_badblocks(
2213 rdev,
2214 r10_bio->devs[m].addr,
2215 r10_bio->sectors, 0))
2216 md_error(conf->mddev, rdev);
NeilBrown749c55e2011-07-28 11:39:24 +10002217 }
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002218 }
NeilBrown749c55e2011-07-28 11:39:24 +10002219 put_buf(r10_bio);
2220 } else {
NeilBrownbd870a12011-07-28 11:39:24 +10002221 for (m = 0; m < conf->copies; m++) {
2222 int dev = r10_bio->devs[m].devnum;
2223 struct bio *bio = r10_bio->devs[m].bio;
2224 rdev = conf->mirrors[dev].rdev;
2225 if (bio == IO_MADE_GOOD) {
NeilBrown749c55e2011-07-28 11:39:24 +10002226 rdev_clear_badblocks(
2227 rdev,
2228 r10_bio->devs[m].addr,
2229 r10_bio->sectors);
2230 rdev_dec_pending(rdev, conf->mddev);
NeilBrownbd870a12011-07-28 11:39:24 +10002231 } else if (bio != NULL &&
2232 !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2233 if (!narrow_write_error(r10_bio, m)) {
2234 md_error(conf->mddev, rdev);
2235 set_bit(R10BIO_Degraded,
2236 &r10_bio->state);
2237 }
2238 rdev_dec_pending(rdev, conf->mddev);
NeilBrown749c55e2011-07-28 11:39:24 +10002239 }
NeilBrownbd870a12011-07-28 11:39:24 +10002240 }
2241 if (test_bit(R10BIO_WriteError,
2242 &r10_bio->state))
2243 close_write(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10002244 raid_end_bio_io(r10_bio);
2245 }
2246}
2247
NeilBrownfd01b882011-10-11 16:47:53 +11002248static void raid10d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
NeilBrown9f2c9d12011-10-11 16:48:43 +11002250 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 unsigned long flags;
NeilBrowne879a872011-10-11 16:49:02 +11002252 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002254 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002258 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002260
Jens Axboe7eaceac2011-03-10 08:52:07 +01002261 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002264 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002265 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002267 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002268 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08002270 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 spin_unlock_irqrestore(&conf->device_lock, flags);
2272
2273 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002274 conf = mddev->private;
NeilBrownbd870a12011-07-28 11:39:24 +10002275 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2276 test_bit(R10BIO_WriteError, &r10_bio->state))
NeilBrown749c55e2011-07-28 11:39:24 +10002277 handle_write_completed(conf, r10_bio);
2278 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01002280 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 recovery_request_write(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002282 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
NeilBrown560f8e52011-07-28 11:39:23 +10002283 handle_read_error(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002284 else {
2285 /* just a partial read to be scheduled from a
2286 * separate context
2287 */
2288 int slot = r10_bio->read_slot;
2289 generic_make_request(r10_bio->devs[slot].bio);
2290 }
NeilBrown4443ae12006-01-06 00:20:28 -08002291
NeilBrown1d9d5242009-10-16 15:55:32 +11002292 cond_resched();
NeilBrownde393cd2011-07-28 11:31:48 +10002293 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2294 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002296 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297}
2298
2299
NeilBrowne879a872011-10-11 16:49:02 +11002300static int init_resync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301{
2302 int buffs;
NeilBrown69335ef2011-12-23 10:17:54 +11002303 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02002306 BUG_ON(conf->r10buf_pool);
NeilBrown69335ef2011-12-23 10:17:54 +11002307 conf->have_replacement = 0;
2308 for (i = 0; i < conf->raid_disks; i++)
2309 if (conf->mirrors[i].replacement)
2310 conf->have_replacement = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2312 if (!conf->r10buf_pool)
2313 return -ENOMEM;
2314 conf->next_resync = 0;
2315 return 0;
2316}
2317
2318/*
2319 * perform a "sync" on one "block"
2320 *
2321 * We need to make sure that no normal I/O request - particularly write
2322 * requests - conflict with active sync requests.
2323 *
2324 * This is achieved by tracking pending requests and a 'barrier' concept
2325 * that can be installed to exclude normal IO requests.
2326 *
2327 * Resync and recovery are handled very differently.
2328 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2329 *
2330 * For resync, we iterate over virtual addresses, read all copies,
2331 * and update if there are differences. If only one copy is live,
2332 * skip it.
2333 * For recovery, we iterate over physical addresses, read a good
2334 * value for each non-in_sync drive, and over-write.
2335 *
2336 * So, for recovery we may have several outstanding complex requests for a
2337 * given address, one for each out-of-sync device. We model this by allocating
2338 * a number of r10_bio structures, one for each out-of-sync device.
2339 * As we setup these structures, we collect all bio's together into a list
2340 * which we then process collectively to add pages, and then process again
2341 * to pass to generic_make_request.
2342 *
2343 * The r10_bio structures are linked using a borrowed master_bio pointer.
2344 * This link is counted in ->remaining. When the r10_bio that points to NULL
2345 * has its remaining count decremented to 0, the whole complex operation
2346 * is complete.
2347 *
2348 */
2349
NeilBrownfd01b882011-10-11 16:47:53 +11002350static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
NeilBrownab9d47e2011-05-11 14:54:41 +10002351 int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
NeilBrowne879a872011-10-11 16:49:02 +11002353 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002354 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 struct bio *biolist = NULL, *bio;
2356 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08002358 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11002359 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 sector_t sectors_skipped = 0;
2361 int chunks_skipped = 0;
2362
2363 if (!conf->r10buf_pool)
2364 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002365 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
2367 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11002368 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2370 max_sector = mddev->resync_max_sectors;
2371 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002372 /* If we aborted, we need to abort the
2373 * sync on the 'current' bitmap chucks (there can
2374 * be several when recovering multiple devices).
2375 * as we may have started syncing it but not finished.
2376 * We can find the current address in
2377 * mddev->curr_resync, but for recovery,
2378 * we need to convert that to several
2379 * virtual addresses.
2380 */
2381 if (mddev->curr_resync < max_sector) { /* aborted */
2382 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2383 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2384 &sync_blocks, 1);
2385 else for (i=0; i<conf->raid_disks; i++) {
2386 sector_t sect =
2387 raid10_find_virt(conf, mddev->curr_resync, i);
2388 bitmap_end_sync(mddev->bitmap, sect,
2389 &sync_blocks, 1);
2390 }
2391 } else /* completed sync */
2392 conf->fullsync = 0;
2393
2394 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07002396 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 return sectors_skipped;
2398 }
2399 if (chunks_skipped >= conf->raid_disks) {
2400 /* if there has been nothing to do on any drive,
2401 * then there is nothing to do at all..
2402 */
NeilBrown57afd892005-06-21 17:17:13 -07002403 *skipped = 1;
2404 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 }
2406
NeilBrownc6207272008-02-06 01:39:52 -08002407 if (max_sector > mddev->resync_max)
2408 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2409
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 /* make sure whole request will fit in a chunk - if chunks
2411 * are meaningful
2412 */
2413 if (conf->near_copies < conf->raid_disks &&
2414 max_sector > (sector_nr | conf->chunk_mask))
2415 max_sector = (sector_nr | conf->chunk_mask) + 1;
2416 /*
2417 * If there is non-resync activity waiting for us then
2418 * put in a delay to throttle resync.
2419 */
NeilBrown0a27ec92006-01-06 00:20:13 -08002420 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
2423 /* Again, very different code for resync and recovery.
2424 * Both must result in an r10bio with a list of bios that
2425 * have bi_end_io, bi_sector, bi_bdev set,
2426 * and bi_private set to the r10bio.
2427 * For recovery, we may actually create several r10bios
2428 * with 2 bios in each, that correspond to the bios in the main one.
2429 * In this case, the subordinate r10bios link back through a
2430 * borrowed master_bio pointer, and the counter in the master
2431 * includes a ref from each subordinate.
2432 */
2433 /* First, we decide what to do and set ->bi_end_io
2434 * To end_sync_read if we want to read, and
2435 * end_sync_write if we will want to write.
2436 */
2437
NeilBrown6cce3b22006-01-06 00:20:16 -08002438 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2440 /* recovery... the complicated one */
NeilBrowne875ece2011-07-28 11:39:24 +10002441 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 r10_bio = NULL;
2443
NeilBrownab9d47e2011-05-11 14:54:41 +10002444 for (i=0 ; i<conf->raid_disks; i++) {
2445 int still_degraded;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002446 struct r10bio *rb2;
NeilBrownab9d47e2011-05-11 14:54:41 +10002447 sector_t sect;
2448 int must_sync;
NeilBrowne875ece2011-07-28 11:39:24 +10002449 int any_working;
NeilBrownab9d47e2011-05-11 14:54:41 +10002450
2451 if (conf->mirrors[i].rdev == NULL ||
2452 test_bit(In_sync, &conf->mirrors[i].rdev->flags))
2453 continue;
2454
2455 still_degraded = 0;
2456 /* want to reconstruct this device */
2457 rb2 = r10_bio;
2458 sect = raid10_find_virt(conf, sector_nr, i);
2459 /* Unless we are doing a full sync, we only need
2460 * to recover the block if it is set in the bitmap
2461 */
2462 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2463 &sync_blocks, 1);
2464 if (sync_blocks < max_sync)
2465 max_sync = sync_blocks;
2466 if (!must_sync &&
2467 !conf->fullsync) {
2468 /* yep, skip the sync_blocks here, but don't assume
2469 * that there will never be anything to do here
NeilBrown6cce3b22006-01-06 00:20:16 -08002470 */
NeilBrownab9d47e2011-05-11 14:54:41 +10002471 chunks_skipped = -1;
2472 continue;
2473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
NeilBrownab9d47e2011-05-11 14:54:41 +10002475 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2476 raise_barrier(conf, rb2 != NULL);
2477 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
NeilBrownab9d47e2011-05-11 14:54:41 +10002479 r10_bio->master_bio = (struct bio*)rb2;
2480 if (rb2)
2481 atomic_inc(&rb2->remaining);
2482 r10_bio->mddev = mddev;
2483 set_bit(R10BIO_IsRecover, &r10_bio->state);
2484 r10_bio->sector = sect;
NeilBrown6cce3b22006-01-06 00:20:16 -08002485
NeilBrownab9d47e2011-05-11 14:54:41 +10002486 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10002487
NeilBrownab9d47e2011-05-11 14:54:41 +10002488 /* Need to check if the array will still be
2489 * degraded
2490 */
2491 for (j=0; j<conf->raid_disks; j++)
2492 if (conf->mirrors[j].rdev == NULL ||
2493 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
2494 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07002495 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002497
2498 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2499 &sync_blocks, still_degraded);
2500
NeilBrowne875ece2011-07-28 11:39:24 +10002501 any_working = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10002502 for (j=0; j<conf->copies;j++) {
NeilBrowne875ece2011-07-28 11:39:24 +10002503 int k;
NeilBrownab9d47e2011-05-11 14:54:41 +10002504 int d = r10_bio->devs[j].devnum;
NeilBrown5e570282011-07-28 11:39:25 +10002505 sector_t from_addr, to_addr;
NeilBrown3cb03002011-10-11 16:45:26 +11002506 struct md_rdev *rdev;
NeilBrown40c356c2011-07-28 11:39:24 +10002507 sector_t sector, first_bad;
2508 int bad_sectors;
NeilBrownab9d47e2011-05-11 14:54:41 +10002509 if (!conf->mirrors[d].rdev ||
2510 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
2511 continue;
2512 /* This is where we read from */
NeilBrowne875ece2011-07-28 11:39:24 +10002513 any_working = 1;
NeilBrown40c356c2011-07-28 11:39:24 +10002514 rdev = conf->mirrors[d].rdev;
2515 sector = r10_bio->devs[j].addr;
2516
2517 if (is_badblock(rdev, sector, max_sync,
2518 &first_bad, &bad_sectors)) {
2519 if (first_bad > sector)
2520 max_sync = first_bad - sector;
2521 else {
2522 bad_sectors -= (sector
2523 - first_bad);
2524 if (max_sync > bad_sectors)
2525 max_sync = bad_sectors;
2526 continue;
2527 }
2528 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002529 bio = r10_bio->devs[0].bio;
2530 bio->bi_next = biolist;
2531 biolist = bio;
2532 bio->bi_private = r10_bio;
2533 bio->bi_end_io = end_sync_read;
2534 bio->bi_rw = READ;
NeilBrown5e570282011-07-28 11:39:25 +10002535 from_addr = r10_bio->devs[j].addr;
2536 bio->bi_sector = from_addr +
NeilBrownab9d47e2011-05-11 14:54:41 +10002537 conf->mirrors[d].rdev->data_offset;
2538 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2539 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2540 atomic_inc(&r10_bio->remaining);
2541 /* and we write to 'i' */
2542
2543 for (k=0; k<conf->copies; k++)
2544 if (r10_bio->devs[k].devnum == i)
2545 break;
2546 BUG_ON(k == conf->copies);
2547 bio = r10_bio->devs[1].bio;
2548 bio->bi_next = biolist;
2549 biolist = bio;
2550 bio->bi_private = r10_bio;
2551 bio->bi_end_io = end_sync_write;
2552 bio->bi_rw = WRITE;
NeilBrown5e570282011-07-28 11:39:25 +10002553 to_addr = r10_bio->devs[k].addr;
2554 bio->bi_sector = to_addr +
NeilBrownab9d47e2011-05-11 14:54:41 +10002555 conf->mirrors[i].rdev->data_offset;
2556 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
2557
2558 r10_bio->devs[0].devnum = d;
NeilBrown5e570282011-07-28 11:39:25 +10002559 r10_bio->devs[0].addr = from_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10002560 r10_bio->devs[1].devnum = i;
NeilBrown5e570282011-07-28 11:39:25 +10002561 r10_bio->devs[1].addr = to_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10002562
2563 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002565 if (j == conf->copies) {
NeilBrowne875ece2011-07-28 11:39:24 +10002566 /* Cannot recover, so abort the recovery or
2567 * record a bad block */
NeilBrownab9d47e2011-05-11 14:54:41 +10002568 put_buf(r10_bio);
2569 if (rb2)
2570 atomic_dec(&rb2->remaining);
2571 r10_bio = rb2;
NeilBrowne875ece2011-07-28 11:39:24 +10002572 if (any_working) {
2573 /* problem is that there are bad blocks
2574 * on other device(s)
2575 */
2576 int k;
2577 for (k = 0; k < conf->copies; k++)
2578 if (r10_bio->devs[k].devnum == i)
2579 break;
2580 if (!rdev_set_badblocks(
2581 conf->mirrors[i].rdev,
2582 r10_bio->devs[k].addr,
2583 max_sync, 0))
2584 any_working = 0;
2585 }
2586 if (!any_working) {
2587 if (!test_and_set_bit(MD_RECOVERY_INTR,
2588 &mddev->recovery))
2589 printk(KERN_INFO "md/raid10:%s: insufficient "
2590 "working devices for recovery.\n",
2591 mdname(mddev));
2592 conf->mirrors[i].recovery_disabled
2593 = mddev->recovery_disabled;
2594 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002595 break;
2596 }
2597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 if (biolist == NULL) {
2599 while (r10_bio) {
NeilBrown9f2c9d12011-10-11 16:48:43 +11002600 struct r10bio *rb2 = r10_bio;
2601 r10_bio = (struct r10bio*) rb2->master_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 rb2->master_bio = NULL;
2603 put_buf(rb2);
2604 }
2605 goto giveup;
2606 }
2607 } else {
2608 /* resync. Schedule a read for every block at this virt offset */
2609 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08002610
NeilBrown78200d42009-02-25 13:18:47 +11002611 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
2612
NeilBrown6cce3b22006-01-06 00:20:16 -08002613 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2614 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10002615 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
2616 &mddev->recovery)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002617 /* We can skip this block */
2618 *skipped = 1;
2619 return sync_blocks + sectors_skipped;
2620 }
2621 if (sync_blocks < max_sync)
2622 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 r10_bio->mddev = mddev;
2626 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08002627 raise_barrier(conf, 0);
2628 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 r10_bio->master_bio = NULL;
2631 r10_bio->sector = sector_nr;
2632 set_bit(R10BIO_IsSync, &r10_bio->state);
2633 raid10_find_phys(conf, r10_bio);
2634 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
2635
2636 for (i=0; i<conf->copies; i++) {
2637 int d = r10_bio->devs[i].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10002638 sector_t first_bad, sector;
2639 int bad_sectors;
2640
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 bio = r10_bio->devs[i].bio;
2642 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002643 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08002645 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 continue;
NeilBrown40c356c2011-07-28 11:39:24 +10002647 sector = r10_bio->devs[i].addr;
2648 if (is_badblock(conf->mirrors[d].rdev,
2649 sector, max_sync,
2650 &first_bad, &bad_sectors)) {
2651 if (first_bad > sector)
2652 max_sync = first_bad - sector;
2653 else {
2654 bad_sectors -= (sector - first_bad);
2655 if (max_sync > bad_sectors)
2656 max_sync = max_sync;
2657 continue;
2658 }
2659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2661 atomic_inc(&r10_bio->remaining);
2662 bio->bi_next = biolist;
2663 biolist = bio;
2664 bio->bi_private = r10_bio;
2665 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08002666 bio->bi_rw = READ;
NeilBrown40c356c2011-07-28 11:39:24 +10002667 bio->bi_sector = sector +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 conf->mirrors[d].rdev->data_offset;
2669 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2670 count++;
2671 }
2672
2673 if (count < 2) {
2674 for (i=0; i<conf->copies; i++) {
2675 int d = r10_bio->devs[i].devnum;
2676 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10002677 rdev_dec_pending(conf->mirrors[d].rdev,
2678 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 }
2680 put_buf(r10_bio);
2681 biolist = NULL;
2682 goto giveup;
2683 }
2684 }
2685
2686 for (bio = biolist; bio ; bio=bio->bi_next) {
2687
2688 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
2689 if (bio->bi_end_io)
2690 bio->bi_flags |= 1 << BIO_UPTODATE;
2691 bio->bi_vcnt = 0;
2692 bio->bi_idx = 0;
2693 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 bio->bi_size = 0;
2695 }
2696
2697 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08002698 if (sector_nr + max_sync < max_sector)
2699 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 do {
2701 struct page *page;
2702 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 if (sector_nr + (len>>9) > max_sector)
2704 len = (max_sector - sector_nr) << 9;
2705 if (len == 0)
2706 break;
2707 for (bio= biolist ; bio ; bio=bio->bi_next) {
NeilBrownab9d47e2011-05-11 14:54:41 +10002708 struct bio *bio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
NeilBrownab9d47e2011-05-11 14:54:41 +10002710 if (bio_add_page(bio, page, len, 0))
2711 continue;
2712
2713 /* stop here */
2714 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
2715 for (bio2 = biolist;
2716 bio2 && bio2 != bio;
2717 bio2 = bio2->bi_next) {
2718 /* remove last page from this bio */
2719 bio2->bi_vcnt--;
2720 bio2->bi_size -= len;
2721 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002723 goto bio_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 }
2725 nr_sectors += len>>9;
2726 sector_nr += len>>9;
2727 } while (biolist->bi_vcnt < RESYNC_PAGES);
2728 bio_full:
2729 r10_bio->sectors = nr_sectors;
2730
2731 while (biolist) {
2732 bio = biolist;
2733 biolist = biolist->bi_next;
2734
2735 bio->bi_next = NULL;
2736 r10_bio = bio->bi_private;
2737 r10_bio->sectors = nr_sectors;
2738
2739 if (bio->bi_end_io == end_sync_read) {
2740 md_sync_acct(bio->bi_bdev, nr_sectors);
2741 generic_make_request(bio);
2742 }
2743 }
2744
NeilBrown57afd892005-06-21 17:17:13 -07002745 if (sectors_skipped)
2746 /* pretend they weren't skipped, it makes
2747 * no important difference in this case
2748 */
2749 md_done_sync(mddev, sectors_skipped, 1);
2750
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 return sectors_skipped + nr_sectors;
2752 giveup:
2753 /* There is nowhere to write, so all non-sync
NeilBrowne875ece2011-07-28 11:39:24 +10002754 * drives must be failed or in resync, all drives
2755 * have a bad block, so try the next chunk...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 */
NeilBrown09b40682009-02-25 13:18:47 +11002757 if (sector_nr + max_sync < max_sector)
2758 max_sector = sector_nr + max_sync;
2759
2760 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 chunks_skipped ++;
2762 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764}
2765
Dan Williams80c3a6c2009-03-17 18:10:40 -07002766static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11002767raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07002768{
2769 sector_t size;
NeilBrowne879a872011-10-11 16:49:02 +11002770 struct r10conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002771
2772 if (!raid_disks)
NeilBrown84707f32010-03-16 17:23:35 +11002773 raid_disks = conf->raid_disks;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002774 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002775 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002776
2777 size = sectors >> conf->chunk_shift;
2778 sector_div(size, conf->far_copies);
2779 size = size * raid_disks;
2780 sector_div(size, conf->near_copies);
2781
2782 return size << conf->chunk_shift;
2783}
2784
Trela, Maciejdab8b292010-03-08 16:02:45 +11002785
NeilBrowne879a872011-10-11 16:49:02 +11002786static struct r10conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787{
NeilBrowne879a872011-10-11 16:49:02 +11002788 struct r10conf *conf = NULL;
NeilBrownc93983b2006-06-26 00:27:41 -07002789 int nc, fc, fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 sector_t stride, size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002791 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Maciej Trelaf73ea872010-06-16 11:46:29 +01002793 if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
2794 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown128595e2010-05-03 14:47:14 +10002795 printk(KERN_ERR "md/raid10:%s: chunk size must be "
2796 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
2797 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002798 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 }
NeilBrown2604b702006-01-06 00:20:36 -08002800
Maciej Trelaf73ea872010-06-16 11:46:29 +01002801 nc = mddev->new_layout & 255;
2802 fc = (mddev->new_layout >> 8) & 255;
2803 fo = mddev->new_layout & (1<<16);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002804
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
Maciej Trelaf73ea872010-06-16 11:46:29 +01002806 (mddev->new_layout >> 17)) {
NeilBrown128595e2010-05-03 14:47:14 +10002807 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
Maciej Trelaf73ea872010-06-16 11:46:29 +01002808 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 goto out;
2810 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11002811
2812 err = -ENOMEM;
NeilBrowne879a872011-10-11 16:49:02 +11002813 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002814 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002816
NeilBrown4443ae12006-01-06 00:20:28 -08002817 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Trela, Maciejdab8b292010-03-08 16:02:45 +11002818 GFP_KERNEL);
2819 if (!conf->mirrors)
2820 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08002821
2822 conf->tmppage = alloc_page(GFP_KERNEL);
2823 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002824 goto out;
2825
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
NeilBrown64a742b2007-02-28 20:11:18 -08002827 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 conf->near_copies = nc;
2829 conf->far_copies = fc;
2830 conf->copies = nc*fc;
NeilBrownc93983b2006-06-26 00:27:41 -07002831 conf->far_offset = fo;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002832 conf->chunk_mask = mddev->new_chunk_sectors - 1;
2833 conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
2834
2835 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2836 r10bio_pool_free, conf);
2837 if (!conf->r10bio_pool)
2838 goto out;
2839
Andre Noll58c0fed2009-03-31 14:33:13 +11002840 size = mddev->dev_sectors >> conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002841 sector_div(size, fc);
2842 size = size * conf->raid_disks;
2843 sector_div(size, nc);
2844 /* 'size' is now the number of chunks in the array */
2845 /* calculate "used chunks per device" in 'stride' */
2846 stride = size * conf->copies;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002847
2848 /* We need to round up when dividing by raid_disks to
2849 * get the stride size.
2850 */
2851 stride += conf->raid_disks - 1;
NeilBrown64a742b2007-02-28 20:11:18 -08002852 sector_div(stride, conf->raid_disks);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002853
2854 conf->dev_sectors = stride << conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002855
NeilBrownc93983b2006-06-26 00:27:41 -07002856 if (fo)
NeilBrown64a742b2007-02-28 20:11:18 -08002857 stride = 1;
2858 else
NeilBrownc93983b2006-06-26 00:27:41 -07002859 sector_div(stride, fc);
NeilBrown64a742b2007-02-28 20:11:18 -08002860 conf->stride = stride << conf->chunk_shift;
2861
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
Neil Browne7e72bf2008-05-14 16:05:54 -07002863 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002864 INIT_LIST_HEAD(&conf->retry_list);
2865
2866 spin_lock_init(&conf->resync_lock);
2867 init_waitqueue_head(&conf->wait_barrier);
2868
2869 conf->thread = md_register_thread(raid10d, mddev, NULL);
2870 if (!conf->thread)
2871 goto out;
2872
Trela, Maciejdab8b292010-03-08 16:02:45 +11002873 conf->mddev = mddev;
2874 return conf;
2875
2876 out:
NeilBrown128595e2010-05-03 14:47:14 +10002877 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
Trela, Maciejdab8b292010-03-08 16:02:45 +11002878 mdname(mddev));
2879 if (conf) {
2880 if (conf->r10bio_pool)
2881 mempool_destroy(conf->r10bio_pool);
2882 kfree(conf->mirrors);
2883 safe_put_page(conf->tmppage);
2884 kfree(conf);
2885 }
2886 return ERR_PTR(err);
2887}
2888
NeilBrownfd01b882011-10-11 16:47:53 +11002889static int run(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002890{
NeilBrowne879a872011-10-11 16:49:02 +11002891 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002892 int i, disk_idx, chunk_size;
NeilBrown0f6d02d2011-10-11 16:48:46 +11002893 struct mirror_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11002894 struct md_rdev *rdev;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002895 sector_t size;
2896
2897 /*
2898 * copy the already verified devices into our private RAID10
2899 * bookkeeping area. [whatever we allocate in run(),
2900 * should be freed in stop()]
2901 */
2902
2903 if (mddev->private == NULL) {
2904 conf = setup_conf(mddev);
2905 if (IS_ERR(conf))
2906 return PTR_ERR(conf);
2907 mddev->private = conf;
2908 }
2909 conf = mddev->private;
2910 if (!conf)
2911 goto out;
2912
Trela, Maciejdab8b292010-03-08 16:02:45 +11002913 mddev->thread = conf->thread;
2914 conf->thread = NULL;
2915
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002916 chunk_size = mddev->chunk_sectors << 9;
2917 blk_queue_io_min(mddev->queue, chunk_size);
2918 if (conf->raid_disks % conf->near_copies)
2919 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
2920 else
2921 blk_queue_io_opt(mddev->queue, chunk_size *
2922 (conf->raid_disks / conf->near_copies));
2923
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002924 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown34b343c2011-07-28 11:31:47 +10002925
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 disk_idx = rdev->raid_disk;
NeilBrown84707f32010-03-16 17:23:35 +11002927 if (disk_idx >= conf->raid_disks
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 || disk_idx < 0)
2929 continue;
2930 disk = conf->mirrors + disk_idx;
2931
2932 disk->rdev = rdev;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002933 disk_stack_limits(mddev->gendisk, rdev->bdev,
2934 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 /* as we don't honour merge_bvec_fn, we must never risk
NeilBrown627a2d32010-03-08 16:44:38 +11002936 * violating it, so limit max_segments to 1 lying
2937 * within a single page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 */
NeilBrown627a2d32010-03-08 16:44:38 +11002939 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2940 blk_queue_max_segments(mddev->queue, 1);
2941 blk_queue_segment_boundary(mddev->queue,
2942 PAGE_CACHE_SIZE - 1);
2943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
2945 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 }
NeilBrown6d508242005-09-09 16:24:03 -07002947 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10002948 if (!enough(conf, -1)) {
NeilBrown128595e2010-05-03 14:47:14 +10002949 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07002950 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 goto out_free_conf;
2952 }
2953
2954 mddev->degraded = 0;
2955 for (i = 0; i < conf->raid_disks; i++) {
2956
2957 disk = conf->mirrors + i;
2958
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002959 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07002960 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 disk->head_position = 0;
2962 mddev->degraded++;
Neil Brown8c2e8702008-06-28 08:30:52 +10002963 if (disk->rdev)
2964 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 }
NeilBrownd890fa22011-10-26 11:54:39 +11002966 disk->recovery_disabled = mddev->recovery_disabled - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 }
2968
Andre Noll8c6ac862009-06-18 08:48:06 +10002969 if (mddev->recovery_cp != MaxSector)
NeilBrown128595e2010-05-03 14:47:14 +10002970 printk(KERN_NOTICE "md/raid10:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10002971 " -- starting background reconstruction\n",
2972 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 printk(KERN_INFO
NeilBrown128595e2010-05-03 14:47:14 +10002974 "md/raid10:%s: active with %d out of %d devices\n",
NeilBrown84707f32010-03-16 17:23:35 +11002975 mdname(mddev), conf->raid_disks - mddev->degraded,
2976 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 /*
2978 * Ok, everything is just fine now
2979 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11002980 mddev->dev_sectors = conf->dev_sectors;
2981 size = raid10_size(mddev, 0, 0);
2982 md_set_array_sectors(mddev, size);
2983 mddev->resync_max_sectors = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
NeilBrown0d129222006-10-03 01:15:54 -07002985 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2986 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown7a5febe2005-05-16 21:53:16 -07002987
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 /* Calculate max read-ahead size.
2989 * We need to readahead at least twice a whole stripe....
2990 * maybe...
2991 */
2992 {
Andre Noll9d8f0362009-06-18 08:45:01 +10002993 int stripe = conf->raid_disks *
2994 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 stripe /= conf->near_copies;
2996 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2997 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2998 }
2999
NeilBrown84707f32010-03-16 17:23:35 +11003000 if (conf->near_copies < conf->raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
Martin K. Petersena91a2782011-03-17 11:11:05 +01003002
3003 if (md_integrity_register(mddev))
3004 goto out_free_conf;
3005
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 return 0;
3007
3008out_free_conf:
NeilBrown01f96c02011-09-21 15:30:20 +10003009 md_unregister_thread(&mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 if (conf->r10bio_pool)
3011 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08003012 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003013 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 kfree(conf);
3015 mddev->private = NULL;
3016out:
3017 return -EIO;
3018}
3019
NeilBrownfd01b882011-10-11 16:47:53 +11003020static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021{
NeilBrowne879a872011-10-11 16:49:02 +11003022 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
NeilBrown409c57f2009-03-31 14:39:39 +11003024 raise_barrier(conf, 0);
3025 lower_barrier(conf);
3026
NeilBrown01f96c02011-09-21 15:30:20 +10003027 md_unregister_thread(&mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
3029 if (conf->r10bio_pool)
3030 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003031 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 kfree(conf);
3033 mddev->private = NULL;
3034 return 0;
3035}
3036
NeilBrownfd01b882011-10-11 16:47:53 +11003037static void raid10_quiesce(struct mddev *mddev, int state)
NeilBrown6cce3b22006-01-06 00:20:16 -08003038{
NeilBrowne879a872011-10-11 16:49:02 +11003039 struct r10conf *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08003040
3041 switch(state) {
3042 case 1:
3043 raise_barrier(conf, 0);
3044 break;
3045 case 0:
3046 lower_barrier(conf);
3047 break;
3048 }
NeilBrown6cce3b22006-01-06 00:20:16 -08003049}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
NeilBrownfd01b882011-10-11 16:47:53 +11003051static void *raid10_takeover_raid0(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003052{
NeilBrown3cb03002011-10-11 16:45:26 +11003053 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +11003054 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003055
3056 if (mddev->degraded > 0) {
NeilBrown128595e2010-05-03 14:47:14 +10003057 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
3058 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003059 return ERR_PTR(-EINVAL);
3060 }
3061
Trela, Maciejdab8b292010-03-08 16:02:45 +11003062 /* Set new parameters */
3063 mddev->new_level = 10;
3064 /* new layout: far_copies = 1, near_copies = 2 */
3065 mddev->new_layout = (1<<8) + 2;
3066 mddev->new_chunk_sectors = mddev->chunk_sectors;
3067 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003068 mddev->raid_disks *= 2;
3069 /* make sure it will be not marked as dirty */
3070 mddev->recovery_cp = MaxSector;
3071
3072 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003073 if (!IS_ERR(conf)) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003074 list_for_each_entry(rdev, &mddev->disks, same_set)
3075 if (rdev->raid_disk >= 0)
3076 rdev->new_raid_disk = rdev->raid_disk * 2;
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003077 conf->barrier = 1;
3078 }
3079
Trela, Maciejdab8b292010-03-08 16:02:45 +11003080 return conf;
3081}
3082
NeilBrownfd01b882011-10-11 16:47:53 +11003083static void *raid10_takeover(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003084{
NeilBrowne373ab12011-10-11 16:48:59 +11003085 struct r0conf *raid0_conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003086
3087 /* raid10 can take over:
3088 * raid0 - providing it has only two drives
3089 */
3090 if (mddev->level == 0) {
3091 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11003092 raid0_conf = mddev->private;
3093 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown128595e2010-05-03 14:47:14 +10003094 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
3095 " with more than one zone.\n",
3096 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003097 return ERR_PTR(-EINVAL);
3098 }
3099 return raid10_takeover_raid0(mddev);
3100 }
3101 return ERR_PTR(-EINVAL);
3102}
3103
NeilBrown84fc4b52011-10-11 16:49:58 +11003104static struct md_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105{
3106 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08003107 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 .owner = THIS_MODULE,
3109 .make_request = make_request,
3110 .run = run,
3111 .stop = stop,
3112 .status = status,
3113 .error_handler = error,
3114 .hot_add_disk = raid10_add_disk,
3115 .hot_remove_disk= raid10_remove_disk,
3116 .spare_active = raid10_spare_active,
3117 .sync_request = sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08003118 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07003119 .size = raid10_size,
Trela, Maciejdab8b292010-03-08 16:02:45 +11003120 .takeover = raid10_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121};
3122
3123static int __init raid_init(void)
3124{
NeilBrown2604b702006-01-06 00:20:36 -08003125 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126}
3127
3128static void raid_exit(void)
3129{
NeilBrown2604b702006-01-06 00:20:36 -08003130 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131}
3132
3133module_init(raid_init);
3134module_exit(raid_exit);
3135MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11003136MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08003138MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08003139MODULE_ALIAS("md-level-10");
NeilBrown34db0cd2011-10-11 16:50:01 +11003140
3141module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);