blob: d1295aff41739eea048f0e43513dfba6ade4c8f1 [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>
NeilBrown3ea7daa2012-05-22 13:53:47 +100027#include <linux/kthread.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110028#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110029#include "raid10.h"
Trela, Maciejdab8b292010-03-08 16:02:45 +110030#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110031#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/*
34 * RAID10 provides a combination of RAID0 and RAID1 functionality.
35 * The layout of data is defined by
36 * chunk_size
37 * raid_disks
38 * near_copies (stored in low byte of layout)
39 * far_copies (stored in second byte of layout)
NeilBrownc93983b2006-06-26 00:27:41 -070040 * far_offset (stored in bit 16 of layout )
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 *
42 * The data to be stored is divided into chunks using chunksize.
43 * Each device is divided into far_copies sections.
44 * In each section, chunks are laid out in a style similar to raid0, but
45 * near_copies copies of each chunk is stored (each on a different drive).
46 * The starting device for each section is offset near_copies from the starting
47 * device of the previous section.
NeilBrownc93983b2006-06-26 00:27:41 -070048 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * drive.
50 * near_copies and far_copies must be at least one, and their product is at most
51 * raid_disks.
NeilBrownc93983b2006-06-26 00:27:41 -070052 *
53 * If far_offset is true, then the far_copies are handled a bit differently.
54 * The copies are still in different stripes, but instead of be very far apart
55 * on disk, there are adjacent stripes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 */
57
58/*
59 * Number of guaranteed r10bios in case of extreme VM load:
60 */
61#define NR_RAID10_BIOS 256
62
Jonathan Brassow473e87c2012-07-31 10:03:52 +100063/* when we get a read error on a read-only array, we redirect to another
64 * device without failing the first device, or trying to over-write to
65 * correct the read error. To keep track of bad blocks on a per-bio
66 * level, we store IO_BLOCKED in the appropriate 'bios' pointer
67 */
68#define IO_BLOCKED ((struct bio *)1)
69/* When we successfully write to a known bad-block, we need to remove the
70 * bad-block marking which must be done from process context. So we record
71 * the success by setting devs[n].bio to IO_MADE_GOOD
72 */
73#define IO_MADE_GOOD ((struct bio *)2)
74
75#define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
76
77/* When there are this many requests queued to be written by
NeilBrown34db0cd2011-10-11 16:50:01 +110078 * the raid10 thread, we become 'congested' to provide back-pressure
79 * for writeback.
80 */
81static int max_queued_requests = 1024;
82
NeilBrowne879a872011-10-11 16:49:02 +110083static void allow_barrier(struct r10conf *conf);
84static void lower_barrier(struct r10conf *conf);
NeilBrownfae8cc52012-02-14 11:10:10 +110085static int enough(struct r10conf *conf, int ignore);
NeilBrown3ea7daa2012-05-22 13:53:47 +100086static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
87 int *skipped);
88static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio);
89static void end_reshape_write(struct bio *bio, int error);
90static void end_reshape(struct r10conf *conf);
NeilBrown0a27ec92006-01-06 00:20:13 -080091
Al Virodd0fc662005-10-07 07:46:04 +010092static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
NeilBrowne879a872011-10-11 16:49:02 +110094 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +110095 int size = offsetof(struct r10bio, devs[conf->copies]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
NeilBrown69335ef2011-12-23 10:17:54 +110097 /* allocate a r10bio with room for raid_disks entries in the
98 * bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +010099 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102static void r10bio_pool_free(void *r10_bio, void *data)
103{
104 kfree(r10_bio);
105}
106
NeilBrown0310fa22008-08-05 15:54:14 +1000107/* Maximum size of each resync request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#define RESYNC_BLOCK_SIZE (64*1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
NeilBrown0310fa22008-08-05 15:54:14 +1000110/* amount of memory to reserve for resync requests */
111#define RESYNC_WINDOW (1024*1024)
112/* maximum number of concurrent requests, memory permitting */
113#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115/*
116 * When performing a resync, we need to read and compare, so
117 * we need as many pages are there are copies.
118 * When performing a recovery, we need 2 bios, one for read,
119 * one for write (we recover only one drive per r10buf)
120 *
121 */
Al Virodd0fc662005-10-07 07:46:04 +0100122static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
NeilBrowne879a872011-10-11 16:49:02 +1100124 struct r10conf *conf = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 struct page *page;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100126 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 struct bio *bio;
128 int i, j;
129 int nalloc;
130
131 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100132 if (!r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
NeilBrown3ea7daa2012-05-22 13:53:47 +1000135 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
136 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 nalloc = conf->copies; /* resync */
138 else
139 nalloc = 2; /* recovery */
140
141 /*
142 * Allocate bios.
143 */
144 for (j = nalloc ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +1100145 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (!bio)
147 goto out_free_bio;
148 r10_bio->devs[j].bio = bio;
NeilBrown69335ef2011-12-23 10:17:54 +1100149 if (!conf->have_replacement)
150 continue;
151 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
152 if (!bio)
153 goto out_free_bio;
154 r10_bio->devs[j].repl_bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156 /*
157 * Allocate RESYNC_PAGES data pages and attach them
158 * where needed.
159 */
160 for (j = 0 ; j < nalloc; j++) {
NeilBrown69335ef2011-12-23 10:17:54 +1100161 struct bio *rbio = r10_bio->devs[j].repl_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 bio = r10_bio->devs[j].bio;
163 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown3ea7daa2012-05-22 13:53:47 +1000164 if (j > 0 && !test_bit(MD_RECOVERY_SYNC,
165 &conf->mddev->recovery)) {
166 /* we can share bv_page's during recovery
167 * and reshape */
Namhyung Kimc65060a2011-07-18 17:38:49 +1000168 struct bio *rbio = r10_bio->devs[0].bio;
169 page = rbio->bi_io_vec[i].bv_page;
170 get_page(page);
171 } else
172 page = alloc_page(gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (unlikely(!page))
174 goto out_free_pages;
175
176 bio->bi_io_vec[i].bv_page = page;
NeilBrown69335ef2011-12-23 10:17:54 +1100177 if (rbio)
178 rbio->bi_io_vec[i].bv_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
180 }
181
182 return r10_bio;
183
184out_free_pages:
185 for ( ; i > 0 ; i--)
NeilBrown1345b1d2006-01-06 00:20:40 -0800186 safe_put_page(bio->bi_io_vec[i-1].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 while (j--)
188 for (i = 0; i < RESYNC_PAGES ; i++)
NeilBrown1345b1d2006-01-06 00:20:40 -0800189 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
majianpeng5fdd2cf2012-05-22 13:55:03 +1000190 j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191out_free_bio:
majianpeng5fdd2cf2012-05-22 13:55:03 +1000192 for ( ; j < nalloc; j++) {
193 if (r10_bio->devs[j].bio)
194 bio_put(r10_bio->devs[j].bio);
NeilBrown69335ef2011-12-23 10:17:54 +1100195 if (r10_bio->devs[j].repl_bio)
196 bio_put(r10_bio->devs[j].repl_bio);
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 r10bio_pool_free(r10_bio, conf);
199 return NULL;
200}
201
202static void r10buf_pool_free(void *__r10_bio, void *data)
203{
204 int i;
NeilBrowne879a872011-10-11 16:49:02 +1100205 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100206 struct r10bio *r10bio = __r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 int j;
208
209 for (j=0; j < conf->copies; j++) {
210 struct bio *bio = r10bio->devs[j].bio;
211 if (bio) {
212 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown1345b1d2006-01-06 00:20:40 -0800213 safe_put_page(bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 bio->bi_io_vec[i].bv_page = NULL;
215 }
216 bio_put(bio);
217 }
NeilBrown69335ef2011-12-23 10:17:54 +1100218 bio = r10bio->devs[j].repl_bio;
219 if (bio)
220 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222 r10bio_pool_free(r10bio, conf);
223}
224
NeilBrowne879a872011-10-11 16:49:02 +1100225static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 int i;
228
229 for (i = 0; i < conf->copies; i++) {
230 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown749c55e2011-07-28 11:39:24 +1000231 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 bio_put(*bio);
233 *bio = NULL;
NeilBrown69335ef2011-12-23 10:17:54 +1100234 bio = &r10_bio->devs[i].repl_bio;
235 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
236 bio_put(*bio);
237 *bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239}
240
NeilBrown9f2c9d12011-10-11 16:48:43 +1100241static void free_r10bio(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
NeilBrowne879a872011-10-11 16:49:02 +1100243 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 put_all_bios(conf, r10_bio);
246 mempool_free(r10_bio, conf->r10bio_pool);
247}
248
NeilBrown9f2c9d12011-10-11 16:48:43 +1100249static void put_buf(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
NeilBrowne879a872011-10-11 16:49:02 +1100251 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 mempool_free(r10_bio, conf->r10buf_pool);
254
NeilBrown0a27ec92006-01-06 00:20:13 -0800255 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
NeilBrown9f2c9d12011-10-11 16:48:43 +1100258static void reschedule_retry(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +1100261 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +1100262 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 spin_lock_irqsave(&conf->device_lock, flags);
265 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800266 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 spin_unlock_irqrestore(&conf->device_lock, flags);
268
Arthur Jones388667b2008-07-25 12:03:38 -0700269 /* wake up frozen array... */
270 wake_up(&conf->wait_barrier);
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 md_wakeup_thread(mddev->thread);
273}
274
275/*
276 * raid_end_bio_io() is called when we have finished servicing a mirrored
277 * operation and are ready to return a success/failure code to the buffer
278 * cache layer.
279 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100280static void raid_end_bio_io(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 struct bio *bio = r10_bio->master_bio;
NeilBrown856e08e2011-07-28 11:39:23 +1000283 int done;
NeilBrowne879a872011-10-11 16:49:02 +1100284 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
NeilBrown856e08e2011-07-28 11:39:23 +1000286 if (bio->bi_phys_segments) {
287 unsigned long flags;
288 spin_lock_irqsave(&conf->device_lock, flags);
289 bio->bi_phys_segments--;
290 done = (bio->bi_phys_segments == 0);
291 spin_unlock_irqrestore(&conf->device_lock, flags);
292 } else
293 done = 1;
294 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
295 clear_bit(BIO_UPTODATE, &bio->bi_flags);
296 if (done) {
297 bio_endio(bio, 0);
298 /*
299 * Wake up any possible resync thread that waits for the device
300 * to go idle.
301 */
302 allow_barrier(conf);
303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 free_r10bio(r10_bio);
305}
306
307/*
308 * Update disk head position estimator based on IRQ completion info.
309 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100310static inline void update_head_pos(int slot, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
NeilBrowne879a872011-10-11 16:49:02 +1100312 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
315 r10_bio->devs[slot].addr + (r10_bio->sectors);
316}
317
Namhyung Kim778ca012011-07-18 17:38:47 +1000318/*
319 * Find the disk number which triggered given bio
320 */
NeilBrowne879a872011-10-11 16:49:02 +1100321static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
NeilBrown69335ef2011-12-23 10:17:54 +1100322 struct bio *bio, int *slotp, int *replp)
Namhyung Kim778ca012011-07-18 17:38:47 +1000323{
324 int slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100325 int repl = 0;
Namhyung Kim778ca012011-07-18 17:38:47 +1000326
NeilBrown69335ef2011-12-23 10:17:54 +1100327 for (slot = 0; slot < conf->copies; slot++) {
Namhyung Kim778ca012011-07-18 17:38:47 +1000328 if (r10_bio->devs[slot].bio == bio)
329 break;
NeilBrown69335ef2011-12-23 10:17:54 +1100330 if (r10_bio->devs[slot].repl_bio == bio) {
331 repl = 1;
332 break;
333 }
334 }
Namhyung Kim778ca012011-07-18 17:38:47 +1000335
336 BUG_ON(slot == conf->copies);
337 update_head_pos(slot, r10_bio);
338
NeilBrown749c55e2011-07-28 11:39:24 +1000339 if (slotp)
340 *slotp = slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100341 if (replp)
342 *replp = repl;
Namhyung Kim778ca012011-07-18 17:38:47 +1000343 return r10_bio->devs[slot].devnum;
344}
345
NeilBrown6712ecf2007-09-27 12:47:43 +0200346static void raid10_end_read_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +1100349 struct r10bio *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 int slot, dev;
NeilBrownabbf0982011-12-23 10:17:54 +1100351 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +1100352 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 slot = r10_bio->read_slot;
356 dev = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100357 rdev = r10_bio->devs[slot].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 /*
359 * this branch is our 'one mirror IO has finished' event handler:
360 */
NeilBrown4443ae12006-01-06 00:20:28 -0800361 update_head_pos(slot, r10_bio);
362
363 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /*
365 * Set R10BIO_Uptodate in our master bio, so that
366 * we will return a good error code to the higher
367 * levels even if IO on some other mirrored buffer fails.
368 *
369 * The 'master' represents the composite IO operation to
370 * user-side. So if something waits for IO, then it will
371 * wait for the 'master' bio.
372 */
373 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrownfae8cc52012-02-14 11:10:10 +1100374 } else {
375 /* If all other devices that store this block have
376 * failed, we want to return the error upwards rather
377 * than fail the last device. Here we redefine
378 * "uptodate" to mean "Don't want to retry"
379 */
380 unsigned long flags;
381 spin_lock_irqsave(&conf->device_lock, flags);
382 if (!enough(conf, rdev->raid_disk))
383 uptodate = 1;
384 spin_unlock_irqrestore(&conf->device_lock, flags);
385 }
386 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 raid_end_bio_io(r10_bio);
NeilBrownabbf0982011-12-23 10:17:54 +1100388 rdev_dec_pending(rdev, conf->mddev);
NeilBrown4443ae12006-01-06 00:20:28 -0800389 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /*
NeilBrown7c4e06f2011-05-11 14:53:17 +1000391 * oops, read error - keep the refcount on the rdev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 */
393 char b[BDEVNAME_SIZE];
Christian Dietrich8bda4702011-07-27 11:00:36 +1000394 printk_ratelimited(KERN_ERR
395 "md/raid10:%s: %s: rescheduling sector %llu\n",
396 mdname(conf->mddev),
NeilBrownabbf0982011-12-23 10:17:54 +1100397 bdevname(rdev->bdev, b),
Christian Dietrich8bda4702011-07-27 11:00:36 +1000398 (unsigned long long)r10_bio->sector);
NeilBrown856e08e2011-07-28 11:39:23 +1000399 set_bit(R10BIO_ReadError, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 reschedule_retry(r10_bio);
401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
NeilBrown9f2c9d12011-10-11 16:48:43 +1100404static void close_write(struct r10bio *r10_bio)
NeilBrownbd870a12011-07-28 11:39:24 +1000405{
406 /* clear the bitmap if all writes complete successfully */
407 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
408 r10_bio->sectors,
409 !test_bit(R10BIO_Degraded, &r10_bio->state),
410 0);
411 md_write_end(r10_bio->mddev);
412}
413
NeilBrown9f2c9d12011-10-11 16:48:43 +1100414static void one_write_done(struct r10bio *r10_bio)
NeilBrown19d5f832011-09-10 17:21:17 +1000415{
416 if (atomic_dec_and_test(&r10_bio->remaining)) {
417 if (test_bit(R10BIO_WriteError, &r10_bio->state))
418 reschedule_retry(r10_bio);
419 else {
420 close_write(r10_bio);
421 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
422 reschedule_retry(r10_bio);
423 else
424 raid_end_bio_io(r10_bio);
425 }
426 }
427}
428
NeilBrown6712ecf2007-09-27 12:47:43 +0200429static void raid10_end_write_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +1100432 struct r10bio *r10_bio = bio->bi_private;
Namhyung Kim778ca012011-07-18 17:38:47 +1000433 int dev;
NeilBrown749c55e2011-07-28 11:39:24 +1000434 int dec_rdev = 1;
NeilBrowne879a872011-10-11 16:49:02 +1100435 struct r10conf *conf = r10_bio->mddev->private;
NeilBrown475b0322011-12-23 10:17:55 +1100436 int slot, repl;
NeilBrown4ca40c22011-12-23 10:17:55 +1100437 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
NeilBrown475b0322011-12-23 10:17:55 +1100439 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
NeilBrown475b0322011-12-23 10:17:55 +1100441 if (repl)
442 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +1100443 if (!rdev) {
444 smp_rmb();
445 repl = 0;
NeilBrown475b0322011-12-23 10:17:55 +1100446 rdev = conf->mirrors[dev].rdev;
NeilBrown4ca40c22011-12-23 10:17:55 +1100447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 /*
449 * this branch is our 'one mirror IO has finished' event handler:
450 */
NeilBrown6cce3b232006-01-06 00:20:16 -0800451 if (!uptodate) {
NeilBrown475b0322011-12-23 10:17:55 +1100452 if (repl)
453 /* Never record new bad blocks to replacement,
454 * just fail it.
455 */
456 md_error(rdev->mddev, rdev);
457 else {
458 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +1100459 if (!test_and_set_bit(WantReplacement, &rdev->flags))
460 set_bit(MD_RECOVERY_NEEDED,
461 &rdev->mddev->recovery);
NeilBrown475b0322011-12-23 10:17:55 +1100462 set_bit(R10BIO_WriteError, &r10_bio->state);
463 dec_rdev = 0;
464 }
NeilBrown749c55e2011-07-28 11:39:24 +1000465 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /*
467 * Set R10BIO_Uptodate in our master bio, so that
468 * we will return a good error code for to the higher
469 * levels even if IO on some other mirrored buffer fails.
470 *
471 * The 'master' represents the composite IO operation to
472 * user-side. So if something waits for IO, then it will
473 * wait for the 'master' bio.
474 */
NeilBrown749c55e2011-07-28 11:39:24 +1000475 sector_t first_bad;
476 int bad_sectors;
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 set_bit(R10BIO_Uptodate, &r10_bio->state);
479
NeilBrown749c55e2011-07-28 11:39:24 +1000480 /* Maybe we can clear some bad blocks. */
NeilBrown475b0322011-12-23 10:17:55 +1100481 if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +1000482 r10_bio->devs[slot].addr,
483 r10_bio->sectors,
484 &first_bad, &bad_sectors)) {
485 bio_put(bio);
NeilBrown475b0322011-12-23 10:17:55 +1100486 if (repl)
487 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
488 else
489 r10_bio->devs[slot].bio = IO_MADE_GOOD;
NeilBrown749c55e2011-07-28 11:39:24 +1000490 dec_rdev = 0;
491 set_bit(R10BIO_MadeGood, &r10_bio->state);
492 }
493 }
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 /*
496 *
497 * Let's see if all mirrored write operations have finished
498 * already.
499 */
NeilBrown19d5f832011-09-10 17:21:17 +1000500 one_write_done(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +1000501 if (dec_rdev)
502 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505/*
506 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300507 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 * parameters: near_copies and far_copies.
509 * near_copies * far_copies must be <= raid_disks.
510 * Normally one of these will be 1.
511 * If both are 1, we get raid0.
512 * If near_copies == raid_disks, we get raid1.
513 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300514 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * first chunk, followed by near_copies copies of the next chunk and
516 * so on.
517 * If far_copies > 1, then after 1/far_copies of the array has been assigned
518 * as described above, we start again with a device offset of near_copies.
519 * So we effectively have another copy of the whole array further down all
520 * the drives, but with blocks on different drives.
521 * With this layout, and block is never stored twice on the one device.
522 *
523 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700524 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 *
526 * raid10_find_virt does the reverse mapping, from a device and a
527 * sector offset to a virtual address
528 */
529
NeilBrownf8c9e742012-05-21 09:28:33 +1000530static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 int n,f;
533 sector_t sector;
534 sector_t chunk;
535 sector_t stripe;
536 int dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 int slot = 0;
538
539 /* now calculate first sector/dev */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000540 chunk = r10bio->sector >> geo->chunk_shift;
541 sector = r10bio->sector & geo->chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
NeilBrown5cf00fc2012-05-21 09:28:20 +1000543 chunk *= geo->near_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 stripe = chunk;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000545 dev = sector_div(stripe, geo->raid_disks);
546 if (geo->far_offset)
547 stripe *= geo->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
NeilBrown5cf00fc2012-05-21 09:28:20 +1000549 sector += stripe << geo->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /* and calculate all the others */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000552 for (n = 0; n < geo->near_copies; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 int d = dev;
554 sector_t s = sector;
555 r10bio->devs[slot].addr = sector;
556 r10bio->devs[slot].devnum = d;
557 slot++;
558
NeilBrown5cf00fc2012-05-21 09:28:20 +1000559 for (f = 1; f < geo->far_copies; f++) {
560 d += geo->near_copies;
561 if (d >= geo->raid_disks)
562 d -= geo->raid_disks;
563 s += geo->stride;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 r10bio->devs[slot].devnum = d;
565 r10bio->devs[slot].addr = s;
566 slot++;
567 }
568 dev++;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000569 if (dev >= geo->raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 dev = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000571 sector += (geo->chunk_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573 }
NeilBrownf8c9e742012-05-21 09:28:33 +1000574}
575
576static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
577{
578 struct geom *geo = &conf->geo;
579
580 if (conf->reshape_progress != MaxSector &&
581 ((r10bio->sector >= conf->reshape_progress) !=
582 conf->mddev->reshape_backwards)) {
583 set_bit(R10BIO_Previous, &r10bio->state);
584 geo = &conf->prev;
585 } else
586 clear_bit(R10BIO_Previous, &r10bio->state);
587
588 __raid10_find_phys(geo, r10bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
NeilBrowne879a872011-10-11 16:49:02 +1100591static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 sector_t offset, chunk, vchunk;
NeilBrownf8c9e742012-05-21 09:28:33 +1000594 /* Never use conf->prev as this is only called during resync
595 * or recovery, so reshape isn't happening
596 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000597 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
NeilBrown5cf00fc2012-05-21 09:28:20 +1000599 offset = sector & geo->chunk_mask;
600 if (geo->far_offset) {
NeilBrownc93983b2006-06-26 00:27:41 -0700601 int fc;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000602 chunk = sector >> geo->chunk_shift;
603 fc = sector_div(chunk, geo->far_copies);
604 dev -= fc * geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700605 if (dev < 0)
NeilBrown5cf00fc2012-05-21 09:28:20 +1000606 dev += geo->raid_disks;
NeilBrownc93983b2006-06-26 00:27:41 -0700607 } else {
NeilBrown5cf00fc2012-05-21 09:28:20 +1000608 while (sector >= geo->stride) {
609 sector -= geo->stride;
610 if (dev < geo->near_copies)
611 dev += geo->raid_disks - geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700612 else
NeilBrown5cf00fc2012-05-21 09:28:20 +1000613 dev -= geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700614 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000615 chunk = sector >> geo->chunk_shift;
NeilBrownc93983b2006-06-26 00:27:41 -0700616 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000617 vchunk = chunk * geo->raid_disks + dev;
618 sector_div(vchunk, geo->near_copies);
619 return (vchunk << geo->chunk_shift) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
622/**
623 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
624 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200625 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 * @biovec: the request that could be merged to it.
627 *
628 * Return amount of bytes we can accept at this offset
NeilBrown050b6612012-03-19 12:46:39 +1100629 * This requires checking for end-of-chunk if near_copies != raid_disks,
630 * and for subordinate merge_bvec_fns if merge_check_needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200632static int raid10_mergeable_bvec(struct request_queue *q,
633 struct bvec_merge_data *bvm,
634 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
NeilBrownfd01b882011-10-11 16:47:53 +1100636 struct mddev *mddev = q->queuedata;
NeilBrown050b6612012-03-19 12:46:39 +1100637 struct r10conf *conf = mddev->private;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200638 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 int max;
NeilBrown3ea7daa2012-05-22 13:53:47 +1000640 unsigned int chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200641 unsigned int bio_sectors = bvm->bi_size >> 9;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000642 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
NeilBrown3ea7daa2012-05-22 13:53:47 +1000644 chunk_sectors = (conf->geo.chunk_mask & conf->prev.chunk_mask) + 1;
NeilBrownf8c9e742012-05-21 09:28:33 +1000645 if (conf->reshape_progress != MaxSector &&
646 ((sector >= conf->reshape_progress) !=
647 conf->mddev->reshape_backwards))
648 geo = &conf->prev;
649
NeilBrown5cf00fc2012-05-21 09:28:20 +1000650 if (geo->near_copies < geo->raid_disks) {
NeilBrown050b6612012-03-19 12:46:39 +1100651 max = (chunk_sectors - ((sector & (chunk_sectors - 1))
652 + bio_sectors)) << 9;
653 if (max < 0)
654 /* bio_add cannot handle a negative return */
655 max = 0;
656 if (max <= biovec->bv_len && bio_sectors == 0)
657 return biovec->bv_len;
658 } else
659 max = biovec->bv_len;
660
661 if (mddev->merge_check_needed) {
NeilBrowne0ee7782012-08-18 09:51:42 +1000662 struct {
663 struct r10bio r10_bio;
664 struct r10dev devs[conf->copies];
665 } on_stack;
666 struct r10bio *r10_bio = &on_stack.r10_bio;
NeilBrown050b6612012-03-19 12:46:39 +1100667 int s;
NeilBrownf8c9e742012-05-21 09:28:33 +1000668 if (conf->reshape_progress != MaxSector) {
669 /* Cannot give any guidance during reshape */
670 if (max <= biovec->bv_len && bio_sectors == 0)
671 return biovec->bv_len;
672 return 0;
673 }
NeilBrowne0ee7782012-08-18 09:51:42 +1000674 r10_bio->sector = sector;
675 raid10_find_phys(conf, r10_bio);
NeilBrown050b6612012-03-19 12:46:39 +1100676 rcu_read_lock();
677 for (s = 0; s < conf->copies; s++) {
NeilBrowne0ee7782012-08-18 09:51:42 +1000678 int disk = r10_bio->devs[s].devnum;
NeilBrown050b6612012-03-19 12:46:39 +1100679 struct md_rdev *rdev = rcu_dereference(
680 conf->mirrors[disk].rdev);
681 if (rdev && !test_bit(Faulty, &rdev->flags)) {
682 struct request_queue *q =
683 bdev_get_queue(rdev->bdev);
684 if (q->merge_bvec_fn) {
NeilBrowne0ee7782012-08-18 09:51:42 +1000685 bvm->bi_sector = r10_bio->devs[s].addr
NeilBrown050b6612012-03-19 12:46:39 +1100686 + rdev->data_offset;
687 bvm->bi_bdev = rdev->bdev;
688 max = min(max, q->merge_bvec_fn(
689 q, bvm, biovec));
690 }
691 }
692 rdev = rcu_dereference(conf->mirrors[disk].replacement);
693 if (rdev && !test_bit(Faulty, &rdev->flags)) {
694 struct request_queue *q =
695 bdev_get_queue(rdev->bdev);
696 if (q->merge_bvec_fn) {
NeilBrowne0ee7782012-08-18 09:51:42 +1000697 bvm->bi_sector = r10_bio->devs[s].addr
NeilBrown050b6612012-03-19 12:46:39 +1100698 + rdev->data_offset;
699 bvm->bi_bdev = rdev->bdev;
700 max = min(max, q->merge_bvec_fn(
701 q, bvm, biovec));
702 }
703 }
704 }
705 rcu_read_unlock();
706 }
707 return max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710/*
711 * This routine returns the disk from which the requested read should
712 * be done. There is a per-array 'next expected sequential IO' sector
713 * number - if this matches on the next IO then we use the last disk.
714 * There is also a per-disk 'last know head position' sector that is
715 * maintained from IRQ contexts, both the normal and the resync IO
716 * completion handlers update this position correctly. If there is no
717 * perfect sequential match then we pick the disk whose head is closest.
718 *
719 * If there are 2 mirrors in the same 2 devices, performance degrades
720 * because position is mirror, not device based.
721 *
722 * The rdev for the device selected will have nr_pending incremented.
723 */
724
725/*
726 * FIXME: possibly should rethink readbalancing and do it differently
727 * depending on near_copies / far_copies geometry.
728 */
NeilBrown96c3fd12011-12-23 10:17:54 +1100729static struct md_rdev *read_balance(struct r10conf *conf,
730 struct r10bio *r10_bio,
731 int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000733 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000734 int disk, slot;
NeilBrown856e08e2011-07-28 11:39:23 +1000735 int sectors = r10_bio->sectors;
736 int best_good_sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000737 sector_t new_distance, best_dist;
Jonathan Brassow3bbae042012-07-31 10:03:52 +1000738 struct md_rdev *best_rdev, *rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000739 int do_balance;
740 int best_slot;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000741 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 raid10_find_phys(conf, r10_bio);
744 rcu_read_lock();
NeilBrown56d99122011-05-11 14:27:03 +1000745retry:
NeilBrown856e08e2011-07-28 11:39:23 +1000746 sectors = r10_bio->sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000747 best_slot = -1;
NeilBrownabbf0982011-12-23 10:17:54 +1100748 best_rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000749 best_dist = MaxSector;
NeilBrown856e08e2011-07-28 11:39:23 +1000750 best_good_sectors = 0;
NeilBrown56d99122011-05-11 14:27:03 +1000751 do_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 /*
753 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b232006-01-06 00:20:16 -0800754 * device if no resync is going on (recovery is ok), or below
755 * the resync window. We take the first readable disk when
756 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 */
758 if (conf->mddev->recovery_cp < MaxSector
NeilBrown56d99122011-05-11 14:27:03 +1000759 && (this_sector + sectors >= conf->next_resync))
760 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
NeilBrown56d99122011-05-11 14:27:03 +1000762 for (slot = 0; slot < conf->copies ; slot++) {
NeilBrown856e08e2011-07-28 11:39:23 +1000763 sector_t first_bad;
764 int bad_sectors;
765 sector_t dev_sector;
766
NeilBrown56d99122011-05-11 14:27:03 +1000767 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000769 disk = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100770 rdev = rcu_dereference(conf->mirrors[disk].replacement);
771 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
NeilBrown050b6612012-03-19 12:46:39 +1100772 test_bit(Unmerged, &rdev->flags) ||
NeilBrownabbf0982011-12-23 10:17:54 +1100773 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
774 rdev = rcu_dereference(conf->mirrors[disk].rdev);
NeilBrown050b6612012-03-19 12:46:39 +1100775 if (rdev == NULL ||
776 test_bit(Faulty, &rdev->flags) ||
777 test_bit(Unmerged, &rdev->flags))
NeilBrownabbf0982011-12-23 10:17:54 +1100778 continue;
779 if (!test_bit(In_sync, &rdev->flags) &&
780 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
NeilBrown56d99122011-05-11 14:27:03 +1000781 continue;
782
NeilBrown856e08e2011-07-28 11:39:23 +1000783 dev_sector = r10_bio->devs[slot].addr;
784 if (is_badblock(rdev, dev_sector, sectors,
785 &first_bad, &bad_sectors)) {
786 if (best_dist < MaxSector)
787 /* Already have a better slot */
788 continue;
789 if (first_bad <= dev_sector) {
790 /* Cannot read here. If this is the
791 * 'primary' device, then we must not read
792 * beyond 'bad_sectors' from another device.
793 */
794 bad_sectors -= (dev_sector - first_bad);
795 if (!do_balance && sectors > bad_sectors)
796 sectors = bad_sectors;
797 if (best_good_sectors > sectors)
798 best_good_sectors = sectors;
799 } else {
800 sector_t good_sectors =
801 first_bad - dev_sector;
802 if (good_sectors > best_good_sectors) {
803 best_good_sectors = good_sectors;
804 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100805 best_rdev = rdev;
NeilBrown856e08e2011-07-28 11:39:23 +1000806 }
807 if (!do_balance)
808 /* Must read from here */
809 break;
810 }
811 continue;
812 } else
813 best_good_sectors = sectors;
814
NeilBrown56d99122011-05-11 14:27:03 +1000815 if (!do_balance)
816 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
NeilBrown22dfdf52005-11-28 13:44:09 -0800818 /* This optimisation is debatable, and completely destroys
819 * sequential read speed for 'far copies' arrays. So only
820 * keep it for 'near' arrays, and review those later.
821 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000822 if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 break;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800824
825 /* for far > 1 always use the lowest address */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000826 if (geo->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000827 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800828 else
NeilBrown56d99122011-05-11 14:27:03 +1000829 new_distance = abs(r10_bio->devs[slot].addr -
830 conf->mirrors[disk].head_position);
831 if (new_distance < best_dist) {
832 best_dist = new_distance;
833 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100834 best_rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836 }
NeilBrownabbf0982011-12-23 10:17:54 +1100837 if (slot >= conf->copies) {
NeilBrown56d99122011-05-11 14:27:03 +1000838 slot = best_slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100839 rdev = best_rdev;
840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
NeilBrown56d99122011-05-11 14:27:03 +1000842 if (slot >= 0) {
NeilBrown56d99122011-05-11 14:27:03 +1000843 atomic_inc(&rdev->nr_pending);
844 if (test_bit(Faulty, &rdev->flags)) {
845 /* Cannot risk returning a device that failed
846 * before we inc'ed nr_pending
847 */
848 rdev_dec_pending(rdev, conf->mddev);
849 goto retry;
850 }
851 r10_bio->read_slot = slot;
852 } else
NeilBrown96c3fd12011-12-23 10:17:54 +1100853 rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 rcu_read_unlock();
NeilBrown856e08e2011-07-28 11:39:23 +1000855 *max_sectors = best_good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
NeilBrown96c3fd12011-12-23 10:17:54 +1100857 return rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +1000860int md_raid10_congested(struct mddev *mddev, int bits)
NeilBrown0d129222006-10-03 01:15:54 -0700861{
NeilBrowne879a872011-10-11 16:49:02 +1100862 struct r10conf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700863 int i, ret = 0;
864
NeilBrown34db0cd2011-10-11 16:50:01 +1100865 if ((bits & (1 << BDI_async_congested)) &&
866 conf->pending_count >= max_queued_requests)
867 return 1;
868
NeilBrown0d129222006-10-03 01:15:54 -0700869 rcu_read_lock();
NeilBrownf8c9e742012-05-21 09:28:33 +1000870 for (i = 0;
871 (i < conf->geo.raid_disks || i < conf->prev.raid_disks)
872 && ret == 0;
873 i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100874 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700875 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200876 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700877
878 ret |= bdi_congested(&q->backing_dev_info, bits);
879 }
880 }
881 rcu_read_unlock();
882 return ret;
883}
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +1000884EXPORT_SYMBOL_GPL(md_raid10_congested);
885
886static int raid10_congested(void *data, int bits)
887{
888 struct mddev *mddev = data;
889
890 return mddev_congested(mddev, bits) ||
891 md_raid10_congested(mddev, bits);
892}
NeilBrown0d129222006-10-03 01:15:54 -0700893
NeilBrowne879a872011-10-11 16:49:02 +1100894static void flush_pending_writes(struct r10conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800895{
896 /* Any writes that have been queued but are awaiting
897 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800898 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800899 spin_lock_irq(&conf->device_lock);
900
901 if (conf->pending_bio_list.head) {
902 struct bio *bio;
903 bio = bio_list_get(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +1100904 conf->pending_count = 0;
NeilBrowna35e63e2008-03-04 14:29:29 -0800905 spin_unlock_irq(&conf->device_lock);
906 /* flush any pending bitmap writes to disk
907 * before proceeding w/ I/O */
908 bitmap_unplug(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100909 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800910
911 while (bio) { /* submit pending writes */
912 struct bio *next = bio->bi_next;
913 bio->bi_next = NULL;
Shaohua Li532a2a32012-10-11 13:30:52 +1100914 if (unlikely((bio->bi_rw & REQ_DISCARD) &&
915 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
916 /* Just ignore it */
917 bio_endio(bio, 0);
918 else
919 generic_make_request(bio);
NeilBrowna35e63e2008-03-04 14:29:29 -0800920 bio = next;
921 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800922 } else
923 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800924}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100925
NeilBrown0a27ec92006-01-06 00:20:13 -0800926/* Barriers....
927 * Sometimes we need to suspend IO while we do something else,
928 * either some resync/recovery, or reconfigure the array.
929 * To do this we raise a 'barrier'.
930 * The 'barrier' is a counter that can be raised multiple times
931 * to count how many activities are happening which preclude
932 * normal IO.
933 * We can only raise the barrier if there is no pending IO.
934 * i.e. if nr_pending == 0.
935 * We choose only to raise the barrier if no-one is waiting for the
936 * barrier to go down. This means that as soon as an IO request
937 * is ready, no other operations which require a barrier will start
938 * until the IO request has had a chance.
939 *
940 * So: regular IO calls 'wait_barrier'. When that returns there
941 * is no backgroup IO happening, It must arrange to call
942 * allow_barrier when it has finished its IO.
943 * backgroup IO calls must call raise_barrier. Once that returns
944 * there is no normal IO happeing. It must arrange to call
945 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
NeilBrowne879a872011-10-11 16:49:02 +1100948static void raise_barrier(struct r10conf *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
NeilBrown6cce3b232006-01-06 00:20:16 -0800950 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
NeilBrown6cce3b232006-01-06 00:20:16 -0800953 /* Wait until no block IO is waiting (unless 'force') */
954 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrownc3b328a2011-04-18 18:25:43 +1000955 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800956
957 /* block any new IO from starting */
958 conf->barrier++;
959
NeilBrownc3b328a2011-04-18 18:25:43 +1000960 /* Now wait for all pending IO to complete */
NeilBrown0a27ec92006-01-06 00:20:13 -0800961 wait_event_lock_irq(conf->wait_barrier,
962 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
NeilBrownc3b328a2011-04-18 18:25:43 +1000963 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 spin_unlock_irq(&conf->resync_lock);
966}
967
NeilBrowne879a872011-10-11 16:49:02 +1100968static void lower_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800969{
970 unsigned long flags;
971 spin_lock_irqsave(&conf->resync_lock, flags);
972 conf->barrier--;
973 spin_unlock_irqrestore(&conf->resync_lock, flags);
974 wake_up(&conf->wait_barrier);
975}
976
NeilBrowne879a872011-10-11 16:49:02 +1100977static void wait_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800978{
979 spin_lock_irq(&conf->resync_lock);
980 if (conf->barrier) {
981 conf->nr_waiting++;
NeilBrownd6b42dc2012-03-19 12:46:38 +1100982 /* Wait for the barrier to drop.
983 * However if there are already pending
984 * requests (preventing the barrier from
985 * rising completely), and the
986 * pre-process bio queue isn't empty,
987 * then don't wait, as we need to empty
988 * that queue to get the nr_pending
989 * count down.
990 */
991 wait_event_lock_irq(conf->wait_barrier,
992 !conf->barrier ||
993 (conf->nr_pending &&
994 current->bio_list &&
995 !bio_list_empty(current->bio_list)),
NeilBrown0a27ec92006-01-06 00:20:13 -0800996 conf->resync_lock,
NeilBrownd6b42dc2012-03-19 12:46:38 +1100997 );
NeilBrown0a27ec92006-01-06 00:20:13 -0800998 conf->nr_waiting--;
999 }
1000 conf->nr_pending++;
1001 spin_unlock_irq(&conf->resync_lock);
1002}
1003
NeilBrowne879a872011-10-11 16:49:02 +11001004static void allow_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -08001005{
1006 unsigned long flags;
1007 spin_lock_irqsave(&conf->resync_lock, flags);
1008 conf->nr_pending--;
1009 spin_unlock_irqrestore(&conf->resync_lock, flags);
1010 wake_up(&conf->wait_barrier);
1011}
1012
NeilBrowne879a872011-10-11 16:49:02 +11001013static void freeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -08001014{
1015 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -08001016 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -08001017 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -08001018 * wait until nr_pending match nr_queued+1
1019 * This is called in the context of one normal IO request
1020 * that has failed. Thus any sync request that might be pending
1021 * will be blocked by nr_pending, and we need to wait for
1022 * pending IO requests to complete or be queued for re-try.
1023 * Thus the number queued (nr_queued) plus this request (1)
1024 * must match the number of pending IOs (nr_pending) before
1025 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -08001026 */
1027 spin_lock_irq(&conf->resync_lock);
1028 conf->barrier++;
1029 conf->nr_waiting++;
1030 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -08001031 conf->nr_pending == conf->nr_queued+1,
NeilBrown4443ae12006-01-06 00:20:28 -08001032 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +10001033 flush_pending_writes(conf));
1034
NeilBrown4443ae12006-01-06 00:20:28 -08001035 spin_unlock_irq(&conf->resync_lock);
1036}
1037
NeilBrowne879a872011-10-11 16:49:02 +11001038static void unfreeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -08001039{
1040 /* reverse the effect of the freeze */
1041 spin_lock_irq(&conf->resync_lock);
1042 conf->barrier--;
1043 conf->nr_waiting--;
1044 wake_up(&conf->wait_barrier);
1045 spin_unlock_irq(&conf->resync_lock);
1046}
1047
NeilBrownf8c9e742012-05-21 09:28:33 +10001048static sector_t choose_data_offset(struct r10bio *r10_bio,
1049 struct md_rdev *rdev)
1050{
1051 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1052 test_bit(R10BIO_Previous, &r10_bio->state))
1053 return rdev->data_offset;
1054 else
1055 return rdev->new_data_offset;
1056}
1057
NeilBrown57c67df2012-10-11 13:32:13 +11001058struct raid10_plug_cb {
1059 struct blk_plug_cb cb;
1060 struct bio_list pending;
1061 int pending_cnt;
1062};
1063
1064static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1065{
1066 struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb,
1067 cb);
1068 struct mddev *mddev = plug->cb.data;
1069 struct r10conf *conf = mddev->private;
1070 struct bio *bio;
1071
1072 if (from_schedule) {
1073 spin_lock_irq(&conf->device_lock);
1074 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1075 conf->pending_count += plug->pending_cnt;
1076 spin_unlock_irq(&conf->device_lock);
1077 md_wakeup_thread(mddev->thread);
1078 kfree(plug);
1079 return;
1080 }
1081
1082 /* we aren't scheduling, so we can do the write-out directly. */
1083 bio = bio_list_get(&plug->pending);
1084 bitmap_unplug(mddev->bitmap);
1085 wake_up(&conf->wait_barrier);
1086
1087 while (bio) { /* submit pending writes */
1088 struct bio *next = bio->bi_next;
1089 bio->bi_next = NULL;
1090 generic_make_request(bio);
1091 bio = next;
1092 }
1093 kfree(plug);
1094}
1095
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07001096static void make_request(struct mddev *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
NeilBrowne879a872011-10-11 16:49:02 +11001098 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11001099 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 struct bio *read_bio;
1101 int i;
NeilBrownf8c9e742012-05-21 09:28:33 +10001102 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
NeilBrown5cf00fc2012-05-21 09:28:20 +10001103 int chunk_sects = chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +01001104 const int rw = bio_data_dir(bio);
NeilBrown2c7d46e2010-08-18 16:16:05 +10001105 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
Tejun Heoe9c74692010-09-03 11:56:18 +02001106 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
Shaohua Li532a2a32012-10-11 13:30:52 +11001107 const unsigned long do_discard = (bio->bi_rw
1108 & (REQ_DISCARD | REQ_SECURE));
NeilBrown6cce3b232006-01-06 00:20:16 -08001109 unsigned long flags;
NeilBrown3cb03002011-10-11 16:45:26 +11001110 struct md_rdev *blocked_rdev;
NeilBrown57c67df2012-10-11 13:32:13 +11001111 struct blk_plug_cb *cb;
1112 struct raid10_plug_cb *plug = NULL;
NeilBrownd4432c22011-07-28 11:39:24 +10001113 int sectors_handled;
1114 int max_sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10001115 int sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Tejun Heoe9c74692010-09-03 11:56:18 +02001117 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
1118 md_flush_request(mddev, bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001119 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07001120 }
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 /* If this request crosses a chunk boundary, we need to
1123 * split it. This will only happen for 1 PAGE (or less) requests.
1124 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10001125 if (unlikely((bio->bi_sector & chunk_mask) + (bio->bi_size >> 9)
1126 > chunk_sects
NeilBrownf8c9e742012-05-21 09:28:33 +10001127 && (conf->geo.near_copies < conf->geo.raid_disks
1128 || conf->prev.near_copies < conf->prev.raid_disks))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 struct bio_pair *bp;
1130 /* Sanity check -- queue functions should prevent this happening */
Shaohua Li532a2a32012-10-11 13:30:52 +11001131 if ((bio->bi_vcnt != 1 && bio->bi_vcnt != 0) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 bio->bi_idx != 0)
1133 goto bad_map;
1134 /* This is a one page bio that upper layers
1135 * refuse to split for us, so we need to split it.
1136 */
Denis ChengRq6feef532008-10-09 08:57:05 +02001137 bp = bio_split(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
NeilBrown51e9ac72010-08-07 21:17:00 +10001139
1140 /* Each of these 'make_request' calls will call 'wait_barrier'.
1141 * If the first succeeds but the second blocks due to the resync
1142 * thread raising the barrier, we will deadlock because the
1143 * IO to the underlying device will be queued in generic_make_request
1144 * and will never complete, so will never reduce nr_pending.
1145 * So increment nr_waiting here so no new raise_barriers will
1146 * succeed, and so the second wait_barrier cannot block.
1147 */
1148 spin_lock_irq(&conf->resync_lock);
1149 conf->nr_waiting++;
1150 spin_unlock_irq(&conf->resync_lock);
1151
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001152 make_request(mddev, &bp->bio1);
1153 make_request(mddev, &bp->bio2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
NeilBrown51e9ac72010-08-07 21:17:00 +10001155 spin_lock_irq(&conf->resync_lock);
1156 conf->nr_waiting--;
1157 wake_up(&conf->wait_barrier);
1158 spin_unlock_irq(&conf->resync_lock);
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 bio_pair_release(bp);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001161 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 bad_map:
NeilBrown128595e2010-05-03 14:47:14 +10001163 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
1164 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
1166
NeilBrown6712ecf2007-09-27 12:47:43 +02001167 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001168 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
1170
NeilBrown3d310eb2005-06-21 17:17:26 -07001171 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -07001172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 /*
1174 * Register the new request and wait if the reconstruction
1175 * thread has put up a bar for new requests.
1176 * Continue immediately if no resync is active currently.
1177 */
NeilBrown0a27ec92006-01-06 00:20:13 -08001178 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
NeilBrown3ea7daa2012-05-22 13:53:47 +10001180 sectors = bio->bi_size >> 9;
1181 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1182 bio->bi_sector < conf->reshape_progress &&
1183 bio->bi_sector + sectors > conf->reshape_progress) {
1184 /* IO spans the reshape position. Need to wait for
1185 * reshape to pass
1186 */
1187 allow_barrier(conf);
1188 wait_event(conf->wait_barrier,
1189 conf->reshape_progress <= bio->bi_sector ||
1190 conf->reshape_progress >= bio->bi_sector + sectors);
1191 wait_barrier(conf);
1192 }
1193 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1194 bio_data_dir(bio) == WRITE &&
1195 (mddev->reshape_backwards
1196 ? (bio->bi_sector < conf->reshape_safe &&
1197 bio->bi_sector + sectors > conf->reshape_progress)
1198 : (bio->bi_sector + sectors > conf->reshape_safe &&
1199 bio->bi_sector < conf->reshape_progress))) {
1200 /* Need to update reshape_position in metadata */
1201 mddev->reshape_position = conf->reshape_progress;
1202 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1203 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1204 md_wakeup_thread(mddev->thread);
1205 wait_event(mddev->sb_wait,
1206 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
1207
1208 conf->reshape_safe = mddev->reshape_position;
1209 }
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1212
1213 r10_bio->master_bio = bio;
NeilBrown3ea7daa2012-05-22 13:53:47 +10001214 r10_bio->sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 r10_bio->mddev = mddev;
1217 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b232006-01-06 00:20:16 -08001218 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
NeilBrown856e08e2011-07-28 11:39:23 +10001220 /* We might need to issue multiple reads to different
1221 * devices if there are bad blocks around, so we keep
1222 * track of the number of reads in bio->bi_phys_segments.
1223 * If this is 0, there is only one r10_bio and no locking
1224 * will be needed when the request completes. If it is
1225 * non-zero, then it is the number of not-completed requests.
1226 */
1227 bio->bi_phys_segments = 0;
1228 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
1229
Jens Axboea3623572005-11-01 09:26:16 +01001230 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /*
1232 * read balancing logic:
1233 */
NeilBrown96c3fd12011-12-23 10:17:54 +11001234 struct md_rdev *rdev;
NeilBrown856e08e2011-07-28 11:39:23 +10001235 int slot;
1236
1237read_again:
NeilBrown96c3fd12011-12-23 10:17:54 +11001238 rdev = read_balance(conf, r10_bio, &max_sectors);
1239 if (!rdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 raid_end_bio_io(r10_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001241 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 }
NeilBrown96c3fd12011-12-23 10:17:54 +11001243 slot = r10_bio->read_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
NeilBrowna167f662010-10-26 18:31:13 +11001245 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrown856e08e2011-07-28 11:39:23 +10001246 md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
1247 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 r10_bio->devs[slot].bio = read_bio;
NeilBrownabbf0982011-12-23 10:17:54 +11001250 r10_bio->devs[slot].rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 read_bio->bi_sector = r10_bio->devs[slot].addr +
NeilBrownf8c9e742012-05-21 09:28:33 +10001253 choose_data_offset(r10_bio, rdev);
NeilBrown96c3fd12011-12-23 10:17:54 +11001254 read_bio->bi_bdev = rdev->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 read_bio->bi_end_io = raid10_end_read_request;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001256 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 read_bio->bi_private = r10_bio;
1258
NeilBrown856e08e2011-07-28 11:39:23 +10001259 if (max_sectors < r10_bio->sectors) {
1260 /* Could not read all from this device, so we will
1261 * need another r10_bio.
1262 */
NeilBrown856e08e2011-07-28 11:39:23 +10001263 sectors_handled = (r10_bio->sectors + max_sectors
1264 - bio->bi_sector);
1265 r10_bio->sectors = max_sectors;
1266 spin_lock_irq(&conf->device_lock);
1267 if (bio->bi_phys_segments == 0)
1268 bio->bi_phys_segments = 2;
1269 else
1270 bio->bi_phys_segments++;
1271 spin_unlock(&conf->device_lock);
1272 /* Cannot call generic_make_request directly
1273 * as that will be queued in __generic_make_request
1274 * and subsequent mempool_alloc might block
1275 * waiting for it. so hand bio over to raid10d.
1276 */
1277 reschedule_retry(r10_bio);
1278
1279 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1280
1281 r10_bio->master_bio = bio;
1282 r10_bio->sectors = ((bio->bi_size >> 9)
1283 - sectors_handled);
1284 r10_bio->state = 0;
1285 r10_bio->mddev = mddev;
1286 r10_bio->sector = bio->bi_sector + sectors_handled;
1287 goto read_again;
1288 } else
1289 generic_make_request(read_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001290 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
1292
1293 /*
1294 * WRITE:
1295 */
NeilBrown34db0cd2011-10-11 16:50:01 +11001296 if (conf->pending_count >= max_queued_requests) {
1297 md_wakeup_thread(mddev->thread);
1298 wait_event(conf->wait_barrier,
1299 conf->pending_count < max_queued_requests);
1300 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001301 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 * inc refcount on their rdev. Record them by setting
1303 * bios[x] to bio
NeilBrownd4432c22011-07-28 11:39:24 +10001304 * If there are known/acknowledged bad blocks on any device
1305 * on which we have seen a write error, we want to avoid
1306 * writing to those blocks. This potentially requires several
1307 * writes to write around the bad blocks. Each set of writes
1308 * gets its own r10_bio with a set of bios attached. The number
1309 * of r10_bios is recored in bio->bi_phys_segments just as with
1310 * the read case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001312
NeilBrown69335ef2011-12-23 10:17:54 +11001313 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 raid10_find_phys(conf, r10_bio);
NeilBrownd4432c22011-07-28 11:39:24 +10001315retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -07001316 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 rcu_read_lock();
NeilBrownd4432c22011-07-28 11:39:24 +10001318 max_sectors = r10_bio->sectors;
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 for (i = 0; i < conf->copies; i++) {
1321 int d = r10_bio->devs[i].devnum;
NeilBrown3cb03002011-10-11 16:45:26 +11001322 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown475b0322011-12-23 10:17:55 +11001323 struct md_rdev *rrdev = rcu_dereference(
1324 conf->mirrors[d].replacement);
NeilBrown4ca40c22011-12-23 10:17:55 +11001325 if (rdev == rrdev)
1326 rrdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001327 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1328 atomic_inc(&rdev->nr_pending);
1329 blocked_rdev = rdev;
1330 break;
1331 }
NeilBrown475b0322011-12-23 10:17:55 +11001332 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1333 atomic_inc(&rrdev->nr_pending);
1334 blocked_rdev = rrdev;
1335 break;
1336 }
NeilBrown050b6612012-03-19 12:46:39 +11001337 if (rrdev && (test_bit(Faulty, &rrdev->flags)
1338 || test_bit(Unmerged, &rrdev->flags)))
NeilBrown475b0322011-12-23 10:17:55 +11001339 rrdev = NULL;
1340
NeilBrownd4432c22011-07-28 11:39:24 +10001341 r10_bio->devs[i].bio = NULL;
NeilBrown475b0322011-12-23 10:17:55 +11001342 r10_bio->devs[i].repl_bio = NULL;
NeilBrown050b6612012-03-19 12:46:39 +11001343 if (!rdev || test_bit(Faulty, &rdev->flags) ||
1344 test_bit(Unmerged, &rdev->flags)) {
NeilBrown6cce3b232006-01-06 00:20:16 -08001345 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrownd4432c22011-07-28 11:39:24 +10001346 continue;
NeilBrown6cce3b232006-01-06 00:20:16 -08001347 }
NeilBrownd4432c22011-07-28 11:39:24 +10001348 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1349 sector_t first_bad;
1350 sector_t dev_sector = r10_bio->devs[i].addr;
1351 int bad_sectors;
1352 int is_bad;
1353
1354 is_bad = is_badblock(rdev, dev_sector,
1355 max_sectors,
1356 &first_bad, &bad_sectors);
1357 if (is_bad < 0) {
1358 /* Mustn't write here until the bad block
1359 * is acknowledged
1360 */
1361 atomic_inc(&rdev->nr_pending);
1362 set_bit(BlockedBadBlocks, &rdev->flags);
1363 blocked_rdev = rdev;
1364 break;
1365 }
1366 if (is_bad && first_bad <= dev_sector) {
1367 /* Cannot write here at all */
1368 bad_sectors -= (dev_sector - first_bad);
1369 if (bad_sectors < max_sectors)
1370 /* Mustn't write more than bad_sectors
1371 * to other devices yet
1372 */
1373 max_sectors = bad_sectors;
1374 /* We don't set R10BIO_Degraded as that
1375 * only applies if the disk is missing,
1376 * so it might be re-added, and we want to
1377 * know to recover this chunk.
1378 * In this case the device is here, and the
1379 * fact that this chunk is not in-sync is
1380 * recorded in the bad block log.
1381 */
1382 continue;
1383 }
1384 if (is_bad) {
1385 int good_sectors = first_bad - dev_sector;
1386 if (good_sectors < max_sectors)
1387 max_sectors = good_sectors;
1388 }
1389 }
1390 r10_bio->devs[i].bio = bio;
1391 atomic_inc(&rdev->nr_pending);
NeilBrown475b0322011-12-23 10:17:55 +11001392 if (rrdev) {
1393 r10_bio->devs[i].repl_bio = bio;
1394 atomic_inc(&rrdev->nr_pending);
1395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397 rcu_read_unlock();
1398
Dan Williams6bfe0b42008-04-30 00:52:32 -07001399 if (unlikely(blocked_rdev)) {
1400 /* Have to wait for this device to get unblocked, then retry */
1401 int j;
1402 int d;
1403
NeilBrown475b0322011-12-23 10:17:55 +11001404 for (j = 0; j < i; j++) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07001405 if (r10_bio->devs[j].bio) {
1406 d = r10_bio->devs[j].devnum;
1407 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1408 }
NeilBrown475b0322011-12-23 10:17:55 +11001409 if (r10_bio->devs[j].repl_bio) {
NeilBrown4ca40c22011-12-23 10:17:55 +11001410 struct md_rdev *rdev;
NeilBrown475b0322011-12-23 10:17:55 +11001411 d = r10_bio->devs[j].devnum;
NeilBrown4ca40c22011-12-23 10:17:55 +11001412 rdev = conf->mirrors[d].replacement;
1413 if (!rdev) {
1414 /* Race with remove_disk */
1415 smp_mb();
1416 rdev = conf->mirrors[d].rdev;
1417 }
1418 rdev_dec_pending(rdev, mddev);
NeilBrown475b0322011-12-23 10:17:55 +11001419 }
1420 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001421 allow_barrier(conf);
1422 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1423 wait_barrier(conf);
1424 goto retry_write;
1425 }
1426
NeilBrownd4432c22011-07-28 11:39:24 +10001427 if (max_sectors < r10_bio->sectors) {
1428 /* We are splitting this into multiple parts, so
1429 * we need to prepare for allocating another r10_bio.
1430 */
1431 r10_bio->sectors = max_sectors;
1432 spin_lock_irq(&conf->device_lock);
1433 if (bio->bi_phys_segments == 0)
1434 bio->bi_phys_segments = 2;
1435 else
1436 bio->bi_phys_segments++;
1437 spin_unlock_irq(&conf->device_lock);
1438 }
1439 sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
1440
NeilBrown4e780642010-10-19 12:54:01 +11001441 atomic_set(&r10_bio->remaining, 1);
NeilBrownd4432c22011-07-28 11:39:24 +10001442 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07001443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 for (i = 0; i < conf->copies; i++) {
1445 struct bio *mbio;
1446 int d = r10_bio->devs[i].devnum;
1447 if (!r10_bio->devs[i].bio)
1448 continue;
1449
NeilBrowna167f662010-10-26 18:31:13 +11001450 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrownd4432c22011-07-28 11:39:24 +10001451 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1452 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 r10_bio->devs[i].bio = mbio;
1454
NeilBrownd4432c22011-07-28 11:39:24 +10001455 mbio->bi_sector = (r10_bio->devs[i].addr+
NeilBrownf8c9e742012-05-21 09:28:33 +10001456 choose_data_offset(r10_bio,
1457 conf->mirrors[d].rdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1459 mbio->bi_end_io = raid10_end_write_request;
Shaohua Li532a2a32012-10-11 13:30:52 +11001460 mbio->bi_rw = WRITE | do_sync | do_fua | do_discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 mbio->bi_private = r10_bio;
1462
1463 atomic_inc(&r10_bio->remaining);
NeilBrown57c67df2012-10-11 13:32:13 +11001464
1465 cb = blk_check_plugged(raid10_unplug, mddev, sizeof(*plug));
1466 if (cb)
1467 plug = container_of(cb, struct raid10_plug_cb, cb);
1468 else
1469 plug = NULL;
NeilBrown4e780642010-10-19 12:54:01 +11001470 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown57c67df2012-10-11 13:32:13 +11001471 if (plug) {
1472 bio_list_add(&plug->pending, mbio);
1473 plug->pending_cnt++;
1474 } else {
1475 bio_list_add(&conf->pending_bio_list, mbio);
1476 conf->pending_count++;
1477 }
NeilBrown4e780642010-10-19 12:54:01 +11001478 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown57c67df2012-10-11 13:32:13 +11001479 if (!plug)
NeilBrownb357f042012-07-03 17:45:31 +10001480 md_wakeup_thread(mddev->thread);
NeilBrown475b0322011-12-23 10:17:55 +11001481
1482 if (!r10_bio->devs[i].repl_bio)
1483 continue;
1484
1485 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1486 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1487 max_sectors);
1488 r10_bio->devs[i].repl_bio = mbio;
1489
NeilBrown4ca40c22011-12-23 10:17:55 +11001490 /* We are actively writing to the original device
1491 * so it cannot disappear, so the replacement cannot
1492 * become NULL here
1493 */
NeilBrownf8c9e742012-05-21 09:28:33 +10001494 mbio->bi_sector = (r10_bio->devs[i].addr +
1495 choose_data_offset(
1496 r10_bio,
1497 conf->mirrors[d].replacement));
NeilBrown475b0322011-12-23 10:17:55 +11001498 mbio->bi_bdev = conf->mirrors[d].replacement->bdev;
1499 mbio->bi_end_io = raid10_end_write_request;
Shaohua Li532a2a32012-10-11 13:30:52 +11001500 mbio->bi_rw = WRITE | do_sync | do_fua | do_discard;
NeilBrown475b0322011-12-23 10:17:55 +11001501 mbio->bi_private = r10_bio;
1502
1503 atomic_inc(&r10_bio->remaining);
1504 spin_lock_irqsave(&conf->device_lock, flags);
1505 bio_list_add(&conf->pending_bio_list, mbio);
1506 conf->pending_count++;
1507 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrownb357f042012-07-03 17:45:31 +10001508 if (!mddev_check_plugged(mddev))
1509 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
1511
NeilBrown079fa162011-09-10 17:21:23 +10001512 /* Don't remove the bias on 'remaining' (one_write_done) until
1513 * after checking if we need to go around again.
1514 */
NeilBrowna35e63e2008-03-04 14:29:29 -08001515
NeilBrownd4432c22011-07-28 11:39:24 +10001516 if (sectors_handled < (bio->bi_size >> 9)) {
NeilBrown079fa162011-09-10 17:21:23 +10001517 one_write_done(r10_bio);
NeilBrown5e570282011-07-28 11:39:25 +10001518 /* We need another r10_bio. It has already been counted
NeilBrownd4432c22011-07-28 11:39:24 +10001519 * in bio->bi_phys_segments.
1520 */
1521 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1522
1523 r10_bio->master_bio = bio;
1524 r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1525
1526 r10_bio->mddev = mddev;
1527 r10_bio->sector = bio->bi_sector + sectors_handled;
1528 r10_bio->state = 0;
1529 goto retry_write;
1530 }
NeilBrown079fa162011-09-10 17:21:23 +10001531 one_write_done(r10_bio);
1532
1533 /* In case raid10d snuck in to freeze_array */
1534 wake_up(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
NeilBrownfd01b882011-10-11 16:47:53 +11001537static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
NeilBrowne879a872011-10-11 16:49:02 +11001539 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 int i;
1541
NeilBrown5cf00fc2012-05-21 09:28:20 +10001542 if (conf->geo.near_copies < conf->geo.raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +10001543 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
NeilBrown5cf00fc2012-05-21 09:28:20 +10001544 if (conf->geo.near_copies > 1)
1545 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1546 if (conf->geo.far_copies > 1) {
1547 if (conf->geo.far_offset)
1548 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001549 else
NeilBrown5cf00fc2012-05-21 09:28:20 +10001550 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001551 }
NeilBrown5cf00fc2012-05-21 09:28:20 +10001552 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1553 conf->geo.raid_disks - mddev->degraded);
1554 for (i = 0; i < conf->geo.raid_disks; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 seq_printf(seq, "%s",
1556 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001557 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 seq_printf(seq, "]");
1559}
1560
NeilBrown700c7212011-07-27 11:00:36 +10001561/* check if there are enough drives for
1562 * every block to appear on atleast one.
1563 * Don't consider the device numbered 'ignore'
1564 * as we might be about to remove it.
1565 */
NeilBrownf8c9e742012-05-21 09:28:33 +10001566static int _enough(struct r10conf *conf, struct geom *geo, int ignore)
NeilBrown700c7212011-07-27 11:00:36 +10001567{
1568 int first = 0;
1569
1570 do {
1571 int n = conf->copies;
1572 int cnt = 0;
NeilBrown80b48122012-09-27 12:35:21 +10001573 int this = first;
NeilBrown700c7212011-07-27 11:00:36 +10001574 while (n--) {
NeilBrown80b48122012-09-27 12:35:21 +10001575 if (conf->mirrors[this].rdev &&
1576 this != ignore)
NeilBrown700c7212011-07-27 11:00:36 +10001577 cnt++;
NeilBrown80b48122012-09-27 12:35:21 +10001578 this = (this+1) % geo->raid_disks;
NeilBrown700c7212011-07-27 11:00:36 +10001579 }
1580 if (cnt == 0)
1581 return 0;
NeilBrown80b48122012-09-27 12:35:21 +10001582 first = (first + geo->near_copies) % geo->raid_disks;
NeilBrown700c7212011-07-27 11:00:36 +10001583 } while (first != 0);
1584 return 1;
1585}
1586
NeilBrownf8c9e742012-05-21 09:28:33 +10001587static int enough(struct r10conf *conf, int ignore)
1588{
1589 return _enough(conf, &conf->geo, ignore) &&
1590 _enough(conf, &conf->prev, ignore);
1591}
1592
NeilBrownfd01b882011-10-11 16:47:53 +11001593static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
1595 char b[BDEVNAME_SIZE];
NeilBrowne879a872011-10-11 16:49:02 +11001596 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 /*
1599 * If it is not operational, then we have already marked it as dead
1600 * else if it is the last working disks, ignore the error, let the
1601 * next level up know.
1602 * else mark the drive as failed
1603 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001604 if (test_bit(In_sync, &rdev->flags)
NeilBrown700c7212011-07-27 11:00:36 +10001605 && !enough(conf, rdev->raid_disk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 /*
1607 * Don't fail the drive, just return an IO error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 */
1609 return;
NeilBrownc04be0a2006-10-03 01:15:53 -07001610 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1611 unsigned long flags;
1612 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001614 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 /*
1616 * if recovery is running, make sure it aborts.
1617 */
NeilBrowndfc70642008-05-23 13:04:39 -07001618 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 }
NeilBrownde393cd2011-07-28 11:31:48 +10001620 set_bit(Blocked, &rdev->flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001621 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -07001622 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Joe Perches067032b2011-01-14 09:14:33 +11001623 printk(KERN_ALERT
1624 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1625 "md/raid10:%s: Operation continuing on %d devices.\n",
NeilBrown128595e2010-05-03 14:47:14 +10001626 mdname(mddev), bdevname(rdev->bdev, b),
NeilBrown5cf00fc2012-05-21 09:28:20 +10001627 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
1629
NeilBrowne879a872011-10-11 16:49:02 +11001630static void print_conf(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
1632 int i;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001633 struct raid10_info *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
NeilBrown128595e2010-05-03 14:47:14 +10001635 printk(KERN_DEBUG "RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (!conf) {
NeilBrown128595e2010-05-03 14:47:14 +10001637 printk(KERN_DEBUG "(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 return;
1639 }
NeilBrown5cf00fc2012-05-21 09:28:20 +10001640 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
1641 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
NeilBrown5cf00fc2012-05-21 09:28:20 +10001643 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 char b[BDEVNAME_SIZE];
1645 tmp = conf->mirrors + i;
1646 if (tmp->rdev)
NeilBrown128595e2010-05-03 14:47:14 +10001647 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001648 i, !test_bit(In_sync, &tmp->rdev->flags),
1649 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 bdevname(tmp->rdev->bdev,b));
1651 }
1652}
1653
NeilBrowne879a872011-10-11 16:49:02 +11001654static void close_sync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
NeilBrown0a27ec92006-01-06 00:20:13 -08001656 wait_barrier(conf);
1657 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 mempool_destroy(conf->r10buf_pool);
1660 conf->r10buf_pool = NULL;
1661}
1662
NeilBrownfd01b882011-10-11 16:47:53 +11001663static int raid10_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
1665 int i;
NeilBrowne879a872011-10-11 16:49:02 +11001666 struct r10conf *conf = mddev->private;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001667 struct raid10_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10001668 int count = 0;
1669 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671 /*
1672 * Find all non-in_sync disks within the RAID10 configuration
1673 * and mark them in_sync
1674 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10001675 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 tmp = conf->mirrors + i;
NeilBrown4ca40c22011-12-23 10:17:55 +11001677 if (tmp->replacement
1678 && tmp->replacement->recovery_offset == MaxSector
1679 && !test_bit(Faulty, &tmp->replacement->flags)
1680 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1681 /* Replacement has just become active */
1682 if (!tmp->rdev
1683 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1684 count++;
1685 if (tmp->rdev) {
1686 /* Replaced device not technically faulty,
1687 * but we need to be sure it gets removed
1688 * and never re-added.
1689 */
1690 set_bit(Faulty, &tmp->rdev->flags);
1691 sysfs_notify_dirent_safe(
1692 tmp->rdev->sysfs_state);
1693 }
1694 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1695 } else if (tmp->rdev
1696 && !test_bit(Faulty, &tmp->rdev->flags)
1697 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001698 count++;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11001699 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701 }
NeilBrown6b965622010-08-18 11:56:59 +10001702 spin_lock_irqsave(&conf->device_lock, flags);
1703 mddev->degraded -= count;
1704 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001707 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
1710
NeilBrownfd01b882011-10-11 16:47:53 +11001711static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
NeilBrowne879a872011-10-11 16:49:02 +11001713 struct r10conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001714 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 int mirror;
Neil Brown6c2fce22008-06-28 08:31:31 +10001716 int first = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10001717 int last = conf->geo.raid_disks - 1;
NeilBrown050b6612012-03-19 12:46:39 +11001718 struct request_queue *q = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 if (mddev->recovery_cp < MaxSector)
1721 /* only hot-add to in-sync arrays, as recovery is
1722 * very different from resync
1723 */
Neil Brown199050e2008-06-28 08:31:33 +10001724 return -EBUSY;
NeilBrownf8c9e742012-05-21 09:28:33 +10001725 if (rdev->saved_raid_disk < 0 && !_enough(conf, &conf->prev, -1))
Neil Brown199050e2008-06-28 08:31:33 +10001726 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
NeilBrowna53a6c82008-11-06 17:28:20 +11001728 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001729 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
NeilBrown050b6612012-03-19 12:46:39 +11001731 if (q->merge_bvec_fn) {
1732 set_bit(Unmerged, &rdev->flags);
1733 mddev->merge_check_needed = 1;
1734 }
1735
Namhyung Kim2c4193d2011-07-18 17:38:43 +10001736 if (rdev->saved_raid_disk >= first &&
NeilBrown6cce3b232006-01-06 00:20:16 -08001737 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1738 mirror = rdev->saved_raid_disk;
1739 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001740 mirror = first;
NeilBrown2bb77732011-07-27 11:00:36 +10001741 for ( ; mirror <= last ; mirror++) {
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001742 struct raid10_info *p = &conf->mirrors[mirror];
NeilBrown2bb77732011-07-27 11:00:36 +10001743 if (p->recovery_disabled == mddev->recovery_disabled)
1744 continue;
NeilBrownb7044d42011-12-23 10:17:56 +11001745 if (p->rdev) {
1746 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1747 p->replacement != NULL)
1748 continue;
1749 clear_bit(In_sync, &rdev->flags);
1750 set_bit(Replacement, &rdev->flags);
1751 rdev->raid_disk = mirror;
1752 err = 0;
1753 disk_stack_limits(mddev->gendisk, rdev->bdev,
1754 rdev->data_offset << 9);
NeilBrownb7044d42011-12-23 10:17:56 +11001755 conf->fullsync = 1;
1756 rcu_assign_pointer(p->replacement, rdev);
1757 break;
1758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
NeilBrown2bb77732011-07-27 11:00:36 +10001760 disk_stack_limits(mddev->gendisk, rdev->bdev,
1761 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
NeilBrown2bb77732011-07-27 11:00:36 +10001763 p->head_position = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11001764 p->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown2bb77732011-07-27 11:00:36 +10001765 rdev->raid_disk = mirror;
1766 err = 0;
1767 if (rdev->saved_raid_disk != mirror)
1768 conf->fullsync = 1;
1769 rcu_assign_pointer(p->rdev, rdev);
1770 break;
1771 }
NeilBrown050b6612012-03-19 12:46:39 +11001772 if (err == 0 && test_bit(Unmerged, &rdev->flags)) {
1773 /* Some requests might not have seen this new
1774 * merge_bvec_fn. We must wait for them to complete
1775 * before merging the device fully.
1776 * First we make sure any code which has tested
1777 * our function has submitted the request, then
1778 * we wait for all outstanding requests to complete.
1779 */
1780 synchronize_sched();
1781 raise_barrier(conf, 0);
1782 lower_barrier(conf);
1783 clear_bit(Unmerged, &rdev->flags);
1784 }
Andre Nollac5e7112009-08-03 10:59:47 +10001785 md_integrity_add_rdev(rdev, mddev);
Jonathan Brassowed30be02012-10-31 11:42:30 +11001786 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
Shaohua Li532a2a32012-10-11 13:30:52 +11001787 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001790 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791}
1792
NeilBrownb8321b62011-12-23 10:17:51 +11001793static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
NeilBrowne879a872011-10-11 16:49:02 +11001795 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11001797 int number = rdev->raid_disk;
NeilBrownc8ab9032011-12-23 10:17:54 +11001798 struct md_rdev **rdevp;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001799 struct raid10_info *p = conf->mirrors + number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 print_conf(conf);
NeilBrownc8ab9032011-12-23 10:17:54 +11001802 if (rdev == p->rdev)
1803 rdevp = &p->rdev;
1804 else if (rdev == p->replacement)
1805 rdevp = &p->replacement;
1806 else
1807 return 0;
1808
1809 if (test_bit(In_sync, &rdev->flags) ||
1810 atomic_read(&rdev->nr_pending)) {
1811 err = -EBUSY;
1812 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
NeilBrownc8ab9032011-12-23 10:17:54 +11001814 /* Only remove faulty devices if recovery
1815 * is not possible.
1816 */
1817 if (!test_bit(Faulty, &rdev->flags) &&
1818 mddev->recovery_disabled != p->recovery_disabled &&
NeilBrown4ca40c22011-12-23 10:17:55 +11001819 (!p->replacement || p->replacement == rdev) &&
NeilBrown63aced62012-05-22 13:55:33 +10001820 number < conf->geo.raid_disks &&
NeilBrownc8ab9032011-12-23 10:17:54 +11001821 enough(conf, -1)) {
1822 err = -EBUSY;
1823 goto abort;
1824 }
1825 *rdevp = NULL;
1826 synchronize_rcu();
1827 if (atomic_read(&rdev->nr_pending)) {
1828 /* lost the race, try later */
1829 err = -EBUSY;
1830 *rdevp = rdev;
1831 goto abort;
NeilBrown4ca40c22011-12-23 10:17:55 +11001832 } else if (p->replacement) {
1833 /* We must have just cleared 'rdev' */
1834 p->rdev = p->replacement;
1835 clear_bit(Replacement, &p->replacement->flags);
1836 smp_mb(); /* Make sure other CPUs may see both as identical
1837 * but will never see neither -- if they are careful.
1838 */
1839 p->replacement = NULL;
1840 clear_bit(WantReplacement, &rdev->flags);
1841 } else
1842 /* We might have just remove the Replacement as faulty
1843 * Clear the flag just in case
1844 */
1845 clear_bit(WantReplacement, &rdev->flags);
1846
NeilBrownc8ab9032011-12-23 10:17:54 +11001847 err = md_integrity_register(mddev);
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849abort:
1850
1851 print_conf(conf);
1852 return err;
1853}
1854
1855
NeilBrown6712ecf2007-09-27 12:47:43 +02001856static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001858 struct r10bio *r10_bio = bio->bi_private;
NeilBrowne879a872011-10-11 16:49:02 +11001859 struct r10conf *conf = r10_bio->mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001860 int d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
NeilBrown3ea7daa2012-05-22 13:53:47 +10001862 if (bio == r10_bio->master_bio) {
1863 /* this is a reshape read */
1864 d = r10_bio->read_slot; /* really the read dev */
1865 } else
1866 d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001867
1868 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1869 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrowne684e412011-07-28 11:39:25 +10001870 else
1871 /* The write handler will notice the lack of
1872 * R10BIO_Uptodate and record any errors etc
1873 */
NeilBrown4dbcdc72006-01-06 00:20:52 -08001874 atomic_add(r10_bio->sectors,
1875 &conf->mirrors[d].rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 /* for reconstruct, we always reschedule after a read.
1878 * for resync, only after all reads
1879 */
NeilBrown73d5c382009-02-25 13:18:47 +11001880 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1882 atomic_dec_and_test(&r10_bio->remaining)) {
1883 /* we have read all the blocks,
1884 * do the comparison in process context in raid10d
1885 */
1886 reschedule_retry(r10_bio);
1887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888}
1889
NeilBrown9f2c9d12011-10-11 16:48:43 +11001890static void end_sync_request(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001891{
NeilBrownfd01b882011-10-11 16:47:53 +11001892 struct mddev *mddev = r10_bio->mddev;
NeilBrown5e570282011-07-28 11:39:25 +10001893
1894 while (atomic_dec_and_test(&r10_bio->remaining)) {
1895 if (r10_bio->master_bio == NULL) {
1896 /* the primary of several recovery bios */
1897 sector_t s = r10_bio->sectors;
1898 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1899 test_bit(R10BIO_WriteError, &r10_bio->state))
1900 reschedule_retry(r10_bio);
1901 else
1902 put_buf(r10_bio);
1903 md_done_sync(mddev, s, 1);
1904 break;
1905 } else {
NeilBrown9f2c9d12011-10-11 16:48:43 +11001906 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
NeilBrown5e570282011-07-28 11:39:25 +10001907 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1908 test_bit(R10BIO_WriteError, &r10_bio->state))
1909 reschedule_retry(r10_bio);
1910 else
1911 put_buf(r10_bio);
1912 r10_bio = r10_bio2;
1913 }
1914 }
1915}
1916
NeilBrown6712ecf2007-09-27 12:47:43 +02001917static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
1919 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +11001920 struct r10bio *r10_bio = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11001921 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001922 struct r10conf *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001923 int d;
NeilBrown749c55e2011-07-28 11:39:24 +10001924 sector_t first_bad;
1925 int bad_sectors;
1926 int slot;
NeilBrown9ad1aef2011-12-23 10:17:55 +11001927 int repl;
NeilBrown4ca40c22011-12-23 10:17:55 +11001928 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
NeilBrown9ad1aef2011-12-23 10:17:55 +11001930 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1931 if (repl)
1932 rdev = conf->mirrors[d].replacement;
NeilBrown547414d2012-03-13 11:21:20 +11001933 else
NeilBrown9ad1aef2011-12-23 10:17:55 +11001934 rdev = conf->mirrors[d].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
NeilBrown1a0b7cd2011-07-28 11:39:25 +10001936 if (!uptodate) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11001937 if (repl)
1938 md_error(mddev, rdev);
1939 else {
1940 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11001941 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1942 set_bit(MD_RECOVERY_NEEDED,
1943 &rdev->mddev->recovery);
NeilBrown9ad1aef2011-12-23 10:17:55 +11001944 set_bit(R10BIO_WriteError, &r10_bio->state);
1945 }
1946 } else if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +10001947 r10_bio->devs[slot].addr,
1948 r10_bio->sectors,
1949 &first_bad, &bad_sectors))
1950 set_bit(R10BIO_MadeGood, &r10_bio->state);
NeilBrowndfc70642008-05-23 13:04:39 -07001951
NeilBrown9ad1aef2011-12-23 10:17:55 +11001952 rdev_dec_pending(rdev, mddev);
NeilBrown5e570282011-07-28 11:39:25 +10001953
1954 end_sync_request(r10_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
1957/*
1958 * Note: sync and recover and handled very differently for raid10
1959 * This code is for resync.
1960 * For resync, we read through virtual addresses and read all blocks.
1961 * If there is any error, we schedule a write. The lowest numbered
1962 * drive is authoritative.
1963 * However requests come for physical address, so we need to map.
1964 * For every physical address there are raid_disks/copies virtual addresses,
1965 * which is always are least one, but is not necessarly an integer.
1966 * This means that a physical address can span multiple chunks, so we may
1967 * have to submit multiple io requests for a single sync request.
1968 */
1969/*
1970 * We check if all blocks are in-sync and only write to blocks that
1971 * aren't in sync
1972 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11001973static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
NeilBrowne879a872011-10-11 16:49:02 +11001975 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 int i, first;
1977 struct bio *tbio, *fbio;
majianpengf4380a92012-04-12 16:04:47 +10001978 int vcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 atomic_set(&r10_bio->remaining, 1);
1981
1982 /* find the first device with a block */
1983 for (i=0; i<conf->copies; i++)
1984 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1985 break;
1986
1987 if (i == conf->copies)
1988 goto done;
1989
1990 first = i;
1991 fbio = r10_bio->devs[i].bio;
1992
majianpengf4380a92012-04-12 16:04:47 +10001993 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001995 for (i=0 ; i < conf->copies ; i++) {
1996 int j, d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001999
2000 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002002 if (i == first)
2003 continue;
2004 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
2005 /* We know that the bi_io_vec layout is the same for
2006 * both 'first' and 'i', so we just compare them.
2007 * All vec entries are PAGE_SIZE;
2008 */
2009 for (j = 0; j < vcnt; j++)
2010 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
2011 page_address(tbio->bi_io_vec[j].bv_page),
NeilBrown5020ad72012-04-02 01:39:05 +10002012 fbio->bi_io_vec[j].bv_len))
NeilBrown0eb3ff12006-01-06 00:20:29 -08002013 break;
2014 if (j == vcnt)
2015 continue;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002016 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
NeilBrownf84ee362011-07-28 11:39:25 +10002017 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2018 /* Don't fix anything. */
2019 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002020 }
NeilBrownf84ee362011-07-28 11:39:25 +10002021 /* Ok, we need to write this bio, either to correct an
2022 * inconsistency or to correct an unreadable block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 * First we need to fixup bv_offset, bv_len and
2024 * bi_vecs, as the read request might have corrupted these
2025 */
2026 tbio->bi_vcnt = vcnt;
2027 tbio->bi_size = r10_bio->sectors << 9;
2028 tbio->bi_idx = 0;
2029 tbio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
2031 tbio->bi_flags |= 1 << BIO_UPTODATE;
2032 tbio->bi_next = NULL;
2033 tbio->bi_rw = WRITE;
2034 tbio->bi_private = r10_bio;
2035 tbio->bi_sector = r10_bio->devs[i].addr;
2036
2037 for (j=0; j < vcnt ; j++) {
2038 tbio->bi_io_vec[j].bv_offset = 0;
2039 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
2040
2041 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
2042 page_address(fbio->bi_io_vec[j].bv_page),
2043 PAGE_SIZE);
2044 }
2045 tbio->bi_end_io = end_sync_write;
2046
2047 d = r10_bio->devs[i].devnum;
2048 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2049 atomic_inc(&r10_bio->remaining);
2050 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
2051
2052 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
2053 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
2054 generic_make_request(tbio);
2055 }
2056
NeilBrown9ad1aef2011-12-23 10:17:55 +11002057 /* Now write out to any replacement devices
2058 * that are active
2059 */
2060 for (i = 0; i < conf->copies; i++) {
2061 int j, d;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002062
2063 tbio = r10_bio->devs[i].repl_bio;
2064 if (!tbio || !tbio->bi_end_io)
2065 continue;
2066 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2067 && r10_bio->devs[i].bio != fbio)
2068 for (j = 0; j < vcnt; j++)
2069 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
2070 page_address(fbio->bi_io_vec[j].bv_page),
2071 PAGE_SIZE);
2072 d = r10_bio->devs[i].devnum;
2073 atomic_inc(&r10_bio->remaining);
2074 md_sync_acct(conf->mirrors[d].replacement->bdev,
2075 tbio->bi_size >> 9);
2076 generic_make_request(tbio);
2077 }
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079done:
2080 if (atomic_dec_and_test(&r10_bio->remaining)) {
2081 md_done_sync(mddev, r10_bio->sectors, 1);
2082 put_buf(r10_bio);
2083 }
2084}
2085
2086/*
2087 * Now for the recovery code.
2088 * Recovery happens across physical sectors.
2089 * We recover all non-is_sync drives by finding the virtual address of
2090 * each, and then choose a working drive that also has that virt address.
2091 * There is a separate r10_bio for each non-in_sync drive.
2092 * Only the first two slots are in use. The first for reading,
2093 * The second for writing.
2094 *
2095 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11002096static void fix_recovery_read_error(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10002097{
2098 /* We got a read error during recovery.
2099 * We repeat the read in smaller page-sized sections.
2100 * If a read succeeds, write it to the new device or record
2101 * a bad block if we cannot.
2102 * If a read fails, record a bad block on both old and
2103 * new devices.
2104 */
NeilBrownfd01b882011-10-11 16:47:53 +11002105 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002106 struct r10conf *conf = mddev->private;
NeilBrown5e570282011-07-28 11:39:25 +10002107 struct bio *bio = r10_bio->devs[0].bio;
2108 sector_t sect = 0;
2109 int sectors = r10_bio->sectors;
2110 int idx = 0;
2111 int dr = r10_bio->devs[0].devnum;
2112 int dw = r10_bio->devs[1].devnum;
2113
2114 while (sectors) {
2115 int s = sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002116 struct md_rdev *rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002117 sector_t addr;
2118 int ok;
2119
2120 if (s > (PAGE_SIZE>>9))
2121 s = PAGE_SIZE >> 9;
2122
2123 rdev = conf->mirrors[dr].rdev;
2124 addr = r10_bio->devs[0].addr + sect,
2125 ok = sync_page_io(rdev,
2126 addr,
2127 s << 9,
2128 bio->bi_io_vec[idx].bv_page,
2129 READ, false);
2130 if (ok) {
2131 rdev = conf->mirrors[dw].rdev;
2132 addr = r10_bio->devs[1].addr + sect;
2133 ok = sync_page_io(rdev,
2134 addr,
2135 s << 9,
2136 bio->bi_io_vec[idx].bv_page,
2137 WRITE, false);
NeilBrownb7044d42011-12-23 10:17:56 +11002138 if (!ok) {
NeilBrown5e570282011-07-28 11:39:25 +10002139 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002140 if (!test_and_set_bit(WantReplacement,
2141 &rdev->flags))
2142 set_bit(MD_RECOVERY_NEEDED,
2143 &rdev->mddev->recovery);
2144 }
NeilBrown5e570282011-07-28 11:39:25 +10002145 }
2146 if (!ok) {
2147 /* We don't worry if we cannot set a bad block -
2148 * it really is bad so there is no loss in not
2149 * recording it yet
2150 */
2151 rdev_set_badblocks(rdev, addr, s, 0);
2152
2153 if (rdev != conf->mirrors[dw].rdev) {
2154 /* need bad block on destination too */
NeilBrown3cb03002011-10-11 16:45:26 +11002155 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002156 addr = r10_bio->devs[1].addr + sect;
2157 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2158 if (!ok) {
2159 /* just abort the recovery */
2160 printk(KERN_NOTICE
2161 "md/raid10:%s: recovery aborted"
2162 " due to read error\n",
2163 mdname(mddev));
2164
2165 conf->mirrors[dw].recovery_disabled
2166 = mddev->recovery_disabled;
2167 set_bit(MD_RECOVERY_INTR,
2168 &mddev->recovery);
2169 break;
2170 }
2171 }
2172 }
2173
2174 sectors -= s;
2175 sect += s;
2176 idx++;
2177 }
2178}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
NeilBrown9f2c9d12011-10-11 16:48:43 +11002180static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
NeilBrowne879a872011-10-11 16:49:02 +11002182 struct r10conf *conf = mddev->private;
Namhyung Kimc65060a2011-07-18 17:38:49 +10002183 int d;
NeilBrown24afd802011-12-23 10:17:55 +11002184 struct bio *wbio, *wbio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
NeilBrown5e570282011-07-28 11:39:25 +10002186 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2187 fix_recovery_read_error(r10_bio);
2188 end_sync_request(r10_bio);
2189 return;
2190 }
2191
Namhyung Kimc65060a2011-07-18 17:38:49 +10002192 /*
2193 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 * and submit the write request
2195 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 d = r10_bio->devs[1].devnum;
NeilBrown24afd802011-12-23 10:17:55 +11002197 wbio = r10_bio->devs[1].bio;
2198 wbio2 = r10_bio->devs[1].repl_bio;
2199 if (wbio->bi_end_io) {
2200 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2201 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
2202 generic_make_request(wbio);
2203 }
2204 if (wbio2 && wbio2->bi_end_io) {
2205 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2206 md_sync_acct(conf->mirrors[d].replacement->bdev,
2207 wbio2->bi_size >> 9);
2208 generic_make_request(wbio2);
2209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
2211
2212
2213/*
Robert Becker1e509152009-12-14 12:49:58 +11002214 * Used by fix_read_error() to decay the per rdev read_errors.
2215 * We halve the read error count for every hour that has elapsed
2216 * since the last recorded read error.
2217 *
2218 */
NeilBrownfd01b882011-10-11 16:47:53 +11002219static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
Robert Becker1e509152009-12-14 12:49:58 +11002220{
2221 struct timespec cur_time_mon;
2222 unsigned long hours_since_last;
2223 unsigned int read_errors = atomic_read(&rdev->read_errors);
2224
2225 ktime_get_ts(&cur_time_mon);
2226
2227 if (rdev->last_read_error.tv_sec == 0 &&
2228 rdev->last_read_error.tv_nsec == 0) {
2229 /* first time we've seen a read error */
2230 rdev->last_read_error = cur_time_mon;
2231 return;
2232 }
2233
2234 hours_since_last = (cur_time_mon.tv_sec -
2235 rdev->last_read_error.tv_sec) / 3600;
2236
2237 rdev->last_read_error = cur_time_mon;
2238
2239 /*
2240 * if hours_since_last is > the number of bits in read_errors
2241 * just set read errors to 0. We do this to avoid
2242 * overflowing the shift of read_errors by hours_since_last.
2243 */
2244 if (hours_since_last >= 8 * sizeof(read_errors))
2245 atomic_set(&rdev->read_errors, 0);
2246 else
2247 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2248}
2249
NeilBrown3cb03002011-10-11 16:45:26 +11002250static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrown58c54fc2011-07-28 11:39:25 +10002251 int sectors, struct page *page, int rw)
2252{
2253 sector_t first_bad;
2254 int bad_sectors;
2255
2256 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2257 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2258 return -1;
2259 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
2260 /* success */
2261 return 1;
NeilBrownb7044d42011-12-23 10:17:56 +11002262 if (rw == WRITE) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002263 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002264 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2265 set_bit(MD_RECOVERY_NEEDED,
2266 &rdev->mddev->recovery);
2267 }
NeilBrown58c54fc2011-07-28 11:39:25 +10002268 /* need to record an error - either for the block or the device */
2269 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2270 md_error(rdev->mddev, rdev);
2271 return 0;
2272}
2273
Robert Becker1e509152009-12-14 12:49:58 +11002274/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 * This is a kernel thread which:
2276 *
2277 * 1. Retries failed read operations on working mirrors.
2278 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07002279 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 */
2281
NeilBrowne879a872011-10-11 16:49:02 +11002282static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown6814d532006-10-03 01:15:45 -07002283{
2284 int sect = 0; /* Offset from r10_bio->sector */
2285 int sectors = r10_bio->sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002286 struct md_rdev*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002287 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002288 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11002289
NeilBrown7c4e06f2011-05-11 14:53:17 +10002290 /* still own a reference to this rdev, so it cannot
2291 * have been cleared recently.
2292 */
2293 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002294
NeilBrown7c4e06f2011-05-11 14:53:17 +10002295 if (test_bit(Faulty, &rdev->flags))
2296 /* drive has already been failed, just ignore any
2297 more fix_read_error() attempts */
2298 return;
2299
2300 check_decay_read_errors(mddev, rdev);
2301 atomic_inc(&rdev->read_errors);
2302 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2303 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11002304 bdevname(rdev->bdev, b);
2305
NeilBrown7c4e06f2011-05-11 14:53:17 +10002306 printk(KERN_NOTICE
2307 "md/raid10:%s: %s: Raid device exceeded "
2308 "read_error threshold [cur %d:max %d]\n",
2309 mdname(mddev), b,
2310 atomic_read(&rdev->read_errors), max_read_errors);
2311 printk(KERN_NOTICE
2312 "md/raid10:%s: %s: Failing raid device\n",
2313 mdname(mddev), b);
2314 md_error(mddev, conf->mirrors[d].rdev);
NeilBrownfae8cc52012-02-14 11:10:10 +11002315 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
NeilBrown7c4e06f2011-05-11 14:53:17 +10002316 return;
Robert Becker1e509152009-12-14 12:49:58 +11002317 }
Robert Becker1e509152009-12-14 12:49:58 +11002318
NeilBrown6814d532006-10-03 01:15:45 -07002319 while(sectors) {
2320 int s = sectors;
2321 int sl = r10_bio->read_slot;
2322 int success = 0;
2323 int start;
2324
2325 if (s > (PAGE_SIZE>>9))
2326 s = PAGE_SIZE >> 9;
2327
2328 rcu_read_lock();
2329 do {
NeilBrown8dbed5c2011-07-28 11:39:24 +10002330 sector_t first_bad;
2331 int bad_sectors;
2332
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002333 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07002334 rdev = rcu_dereference(conf->mirrors[d].rdev);
2335 if (rdev &&
NeilBrown050b6612012-03-19 12:46:39 +11002336 !test_bit(Unmerged, &rdev->flags) &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002337 test_bit(In_sync, &rdev->flags) &&
2338 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2339 &first_bad, &bad_sectors) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07002340 atomic_inc(&rdev->nr_pending);
2341 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11002342 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07002343 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002344 sect,
NeilBrown6814d532006-10-03 01:15:45 -07002345 s<<9,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002346 conf->tmppage, READ, false);
NeilBrown6814d532006-10-03 01:15:45 -07002347 rdev_dec_pending(rdev, mddev);
2348 rcu_read_lock();
2349 if (success)
2350 break;
2351 }
2352 sl++;
2353 if (sl == conf->copies)
2354 sl = 0;
2355 } while (!success && sl != r10_bio->read_slot);
2356 rcu_read_unlock();
2357
2358 if (!success) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002359 /* Cannot read from anywhere, just mark the block
2360 * as bad on the first device to discourage future
2361 * reads.
2362 */
NeilBrown6814d532006-10-03 01:15:45 -07002363 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
NeilBrown58c54fc2011-07-28 11:39:25 +10002364 rdev = conf->mirrors[dn].rdev;
2365
2366 if (!rdev_set_badblocks(
2367 rdev,
2368 r10_bio->devs[r10_bio->read_slot].addr
2369 + sect,
NeilBrownfae8cc52012-02-14 11:10:10 +11002370 s, 0)) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002371 md_error(mddev, rdev);
NeilBrownfae8cc52012-02-14 11:10:10 +11002372 r10_bio->devs[r10_bio->read_slot].bio
2373 = IO_BLOCKED;
2374 }
NeilBrown6814d532006-10-03 01:15:45 -07002375 break;
2376 }
2377
2378 start = sl;
2379 /* write it back and re-read */
2380 rcu_read_lock();
2381 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11002382 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002383
NeilBrown6814d532006-10-03 01:15:45 -07002384 if (sl==0)
2385 sl = conf->copies;
2386 sl--;
2387 d = r10_bio->devs[sl].devnum;
2388 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002389 if (!rdev ||
NeilBrown050b6612012-03-19 12:46:39 +11002390 test_bit(Unmerged, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002391 !test_bit(In_sync, &rdev->flags))
2392 continue;
2393
2394 atomic_inc(&rdev->nr_pending);
2395 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002396 if (r10_sync_page_io(rdev,
2397 r10_bio->devs[sl].addr +
2398 sect,
NeilBrown055d3742012-07-03 15:55:33 +10002399 s, conf->tmppage, WRITE)
NeilBrown1294b9c2011-07-28 11:39:23 +10002400 == 0) {
2401 /* Well, this device is dead */
2402 printk(KERN_NOTICE
2403 "md/raid10:%s: read correction "
2404 "write failed"
2405 " (%d sectors at %llu on %s)\n",
2406 mdname(mddev), s,
2407 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002408 sect +
2409 choose_data_offset(r10_bio,
2410 rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002411 bdevname(rdev->bdev, b));
2412 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2413 "drive\n",
2414 mdname(mddev),
2415 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07002416 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002417 rdev_dec_pending(rdev, mddev);
2418 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002419 }
2420 sl = start;
2421 while (sl != r10_bio->read_slot) {
NeilBrown1294b9c2011-07-28 11:39:23 +10002422 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002423
NeilBrown6814d532006-10-03 01:15:45 -07002424 if (sl==0)
2425 sl = conf->copies;
2426 sl--;
2427 d = r10_bio->devs[sl].devnum;
2428 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002429 if (!rdev ||
2430 !test_bit(In_sync, &rdev->flags))
2431 continue;
Robert Becker67b8dc42009-12-14 12:49:57 +11002432
NeilBrown1294b9c2011-07-28 11:39:23 +10002433 atomic_inc(&rdev->nr_pending);
2434 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002435 switch (r10_sync_page_io(rdev,
2436 r10_bio->devs[sl].addr +
2437 sect,
NeilBrown055d3742012-07-03 15:55:33 +10002438 s, conf->tmppage,
NeilBrown58c54fc2011-07-28 11:39:25 +10002439 READ)) {
2440 case 0:
NeilBrown1294b9c2011-07-28 11:39:23 +10002441 /* Well, this device is dead */
2442 printk(KERN_NOTICE
2443 "md/raid10:%s: unable to read back "
2444 "corrected sectors"
2445 " (%d sectors at %llu on %s)\n",
2446 mdname(mddev), s,
2447 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002448 sect +
2449 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002450 bdevname(rdev->bdev, b));
2451 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2452 "drive\n",
2453 mdname(mddev),
2454 bdevname(rdev->bdev, b));
NeilBrown58c54fc2011-07-28 11:39:25 +10002455 break;
2456 case 1:
NeilBrown1294b9c2011-07-28 11:39:23 +10002457 printk(KERN_INFO
2458 "md/raid10:%s: read error corrected"
2459 " (%d sectors at %llu on %s)\n",
2460 mdname(mddev), s,
2461 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002462 sect +
2463 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002464 bdevname(rdev->bdev, b));
2465 atomic_add(s, &rdev->corrected_errors);
NeilBrown6814d532006-10-03 01:15:45 -07002466 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002467
2468 rdev_dec_pending(rdev, mddev);
2469 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002470 }
2471 rcu_read_unlock();
2472
2473 sectors -= s;
2474 sect += s;
2475 }
2476}
2477
NeilBrownbd870a12011-07-28 11:39:24 +10002478static void bi_complete(struct bio *bio, int error)
2479{
2480 complete((struct completion *)bio->bi_private);
2481}
2482
2483static int submit_bio_wait(int rw, struct bio *bio)
2484{
2485 struct completion event;
2486 rw |= REQ_SYNC;
2487
2488 init_completion(&event);
2489 bio->bi_private = &event;
2490 bio->bi_end_io = bi_complete;
2491 submit_bio(rw, bio);
2492 wait_for_completion(&event);
2493
2494 return test_bit(BIO_UPTODATE, &bio->bi_flags);
2495}
2496
NeilBrown9f2c9d12011-10-11 16:48:43 +11002497static int narrow_write_error(struct r10bio *r10_bio, int i)
NeilBrownbd870a12011-07-28 11:39:24 +10002498{
2499 struct bio *bio = r10_bio->master_bio;
NeilBrownfd01b882011-10-11 16:47:53 +11002500 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002501 struct r10conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002502 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
NeilBrownbd870a12011-07-28 11:39:24 +10002503 /* bio has the data to be written to slot 'i' where
2504 * we just recently had a write error.
2505 * We repeatedly clone the bio and trim down to one block,
2506 * then try the write. Where the write fails we record
2507 * a bad block.
2508 * It is conceivable that the bio doesn't exactly align with
2509 * blocks. We must handle this.
2510 *
2511 * We currently own a reference to the rdev.
2512 */
2513
2514 int block_sectors;
2515 sector_t sector;
2516 int sectors;
2517 int sect_to_write = r10_bio->sectors;
2518 int ok = 1;
2519
2520 if (rdev->badblocks.shift < 0)
2521 return 0;
2522
2523 block_sectors = 1 << rdev->badblocks.shift;
2524 sector = r10_bio->sector;
2525 sectors = ((r10_bio->sector + block_sectors)
2526 & ~(sector_t)(block_sectors - 1))
2527 - sector;
2528
2529 while (sect_to_write) {
2530 struct bio *wbio;
2531 if (sectors > sect_to_write)
2532 sectors = sect_to_write;
2533 /* Write at 'sector' for 'sectors' */
2534 wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
2535 md_trim_bio(wbio, sector - bio->bi_sector, sectors);
2536 wbio->bi_sector = (r10_bio->devs[i].addr+
NeilBrownf8c9e742012-05-21 09:28:33 +10002537 choose_data_offset(r10_bio, rdev) +
NeilBrownbd870a12011-07-28 11:39:24 +10002538 (sector - r10_bio->sector));
2539 wbio->bi_bdev = rdev->bdev;
2540 if (submit_bio_wait(WRITE, wbio) == 0)
2541 /* Failure! */
2542 ok = rdev_set_badblocks(rdev, sector,
2543 sectors, 0)
2544 && ok;
2545
2546 bio_put(wbio);
2547 sect_to_write -= sectors;
2548 sector += sectors;
2549 sectors = block_sectors;
2550 }
2551 return ok;
2552}
2553
NeilBrown9f2c9d12011-10-11 16:48:43 +11002554static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown560f8e52011-07-28 11:39:23 +10002555{
2556 int slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002557 struct bio *bio;
NeilBrowne879a872011-10-11 16:49:02 +11002558 struct r10conf *conf = mddev->private;
NeilBrownabbf0982011-12-23 10:17:54 +11002559 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002560 char b[BDEVNAME_SIZE];
2561 unsigned long do_sync;
NeilBrown856e08e2011-07-28 11:39:23 +10002562 int max_sectors;
NeilBrown560f8e52011-07-28 11:39:23 +10002563
2564 /* we got a read error. Maybe the drive is bad. Maybe just
2565 * the block and we can fix it.
2566 * We freeze all other IO, and try reading the block from
2567 * other devices. When we find one, we re-write
2568 * and check it that fixes the read error.
2569 * This is all done synchronously while the array is
2570 * frozen.
2571 */
NeilBrownfae8cc52012-02-14 11:10:10 +11002572 bio = r10_bio->devs[slot].bio;
2573 bdevname(bio->bi_bdev, b);
2574 bio_put(bio);
2575 r10_bio->devs[slot].bio = NULL;
2576
NeilBrown560f8e52011-07-28 11:39:23 +10002577 if (mddev->ro == 0) {
2578 freeze_array(conf);
2579 fix_read_error(conf, mddev, r10_bio);
2580 unfreeze_array(conf);
NeilBrownfae8cc52012-02-14 11:10:10 +11002581 } else
2582 r10_bio->devs[slot].bio = IO_BLOCKED;
2583
NeilBrownabbf0982011-12-23 10:17:54 +11002584 rdev_dec_pending(rdev, mddev);
NeilBrown560f8e52011-07-28 11:39:23 +10002585
NeilBrown7399c312011-07-28 11:39:23 +10002586read_more:
NeilBrown96c3fd12011-12-23 10:17:54 +11002587 rdev = read_balance(conf, r10_bio, &max_sectors);
2588 if (rdev == NULL) {
NeilBrown560f8e52011-07-28 11:39:23 +10002589 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
2590 " read error for block %llu\n",
NeilBrown7399c312011-07-28 11:39:23 +10002591 mdname(mddev), b,
NeilBrown560f8e52011-07-28 11:39:23 +10002592 (unsigned long long)r10_bio->sector);
2593 raid_end_bio_io(r10_bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002594 return;
2595 }
2596
2597 do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
NeilBrown560f8e52011-07-28 11:39:23 +10002598 slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002599 printk_ratelimited(
2600 KERN_ERR
NeilBrown055d3742012-07-03 15:55:33 +10002601 "md/raid10:%s: %s: redirecting "
NeilBrown560f8e52011-07-28 11:39:23 +10002602 "sector %llu to another mirror\n",
2603 mdname(mddev),
2604 bdevname(rdev->bdev, b),
2605 (unsigned long long)r10_bio->sector);
2606 bio = bio_clone_mddev(r10_bio->master_bio,
2607 GFP_NOIO, mddev);
NeilBrown7399c312011-07-28 11:39:23 +10002608 md_trim_bio(bio,
2609 r10_bio->sector - bio->bi_sector,
2610 max_sectors);
NeilBrown560f8e52011-07-28 11:39:23 +10002611 r10_bio->devs[slot].bio = bio;
NeilBrownabbf0982011-12-23 10:17:54 +11002612 r10_bio->devs[slot].rdev = rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002613 bio->bi_sector = r10_bio->devs[slot].addr
NeilBrownf8c9e742012-05-21 09:28:33 +10002614 + choose_data_offset(r10_bio, rdev);
NeilBrown560f8e52011-07-28 11:39:23 +10002615 bio->bi_bdev = rdev->bdev;
2616 bio->bi_rw = READ | do_sync;
2617 bio->bi_private = r10_bio;
2618 bio->bi_end_io = raid10_end_read_request;
NeilBrown7399c312011-07-28 11:39:23 +10002619 if (max_sectors < r10_bio->sectors) {
2620 /* Drat - have to split this up more */
2621 struct bio *mbio = r10_bio->master_bio;
2622 int sectors_handled =
2623 r10_bio->sector + max_sectors
2624 - mbio->bi_sector;
2625 r10_bio->sectors = max_sectors;
2626 spin_lock_irq(&conf->device_lock);
2627 if (mbio->bi_phys_segments == 0)
2628 mbio->bi_phys_segments = 2;
2629 else
2630 mbio->bi_phys_segments++;
2631 spin_unlock_irq(&conf->device_lock);
2632 generic_make_request(bio);
NeilBrown7399c312011-07-28 11:39:23 +10002633
2634 r10_bio = mempool_alloc(conf->r10bio_pool,
2635 GFP_NOIO);
2636 r10_bio->master_bio = mbio;
2637 r10_bio->sectors = (mbio->bi_size >> 9)
2638 - sectors_handled;
2639 r10_bio->state = 0;
2640 set_bit(R10BIO_ReadError,
2641 &r10_bio->state);
2642 r10_bio->mddev = mddev;
2643 r10_bio->sector = mbio->bi_sector
2644 + sectors_handled;
2645
2646 goto read_more;
2647 } else
2648 generic_make_request(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002649}
2650
NeilBrowne879a872011-10-11 16:49:02 +11002651static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
NeilBrown749c55e2011-07-28 11:39:24 +10002652{
2653 /* Some sort of write request has finished and it
2654 * succeeded in writing where we thought there was a
2655 * bad block. So forget the bad block.
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002656 * Or possibly if failed and we need to record
2657 * a bad block.
NeilBrown749c55e2011-07-28 11:39:24 +10002658 */
2659 int m;
NeilBrown3cb03002011-10-11 16:45:26 +11002660 struct md_rdev *rdev;
NeilBrown749c55e2011-07-28 11:39:24 +10002661
2662 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2663 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002664 for (m = 0; m < conf->copies; m++) {
2665 int dev = r10_bio->devs[m].devnum;
2666 rdev = conf->mirrors[dev].rdev;
2667 if (r10_bio->devs[m].bio == NULL)
2668 continue;
2669 if (test_bit(BIO_UPTODATE,
NeilBrown749c55e2011-07-28 11:39:24 +10002670 &r10_bio->devs[m].bio->bi_flags)) {
NeilBrown749c55e2011-07-28 11:39:24 +10002671 rdev_clear_badblocks(
2672 rdev,
2673 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002674 r10_bio->sectors, 0);
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002675 } else {
2676 if (!rdev_set_badblocks(
2677 rdev,
2678 r10_bio->devs[m].addr,
2679 r10_bio->sectors, 0))
2680 md_error(conf->mddev, rdev);
NeilBrown749c55e2011-07-28 11:39:24 +10002681 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002682 rdev = conf->mirrors[dev].replacement;
2683 if (r10_bio->devs[m].repl_bio == NULL)
2684 continue;
2685 if (test_bit(BIO_UPTODATE,
2686 &r10_bio->devs[m].repl_bio->bi_flags)) {
2687 rdev_clear_badblocks(
2688 rdev,
2689 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002690 r10_bio->sectors, 0);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002691 } else {
2692 if (!rdev_set_badblocks(
2693 rdev,
2694 r10_bio->devs[m].addr,
2695 r10_bio->sectors, 0))
2696 md_error(conf->mddev, rdev);
2697 }
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002698 }
NeilBrown749c55e2011-07-28 11:39:24 +10002699 put_buf(r10_bio);
2700 } else {
NeilBrownbd870a12011-07-28 11:39:24 +10002701 for (m = 0; m < conf->copies; m++) {
2702 int dev = r10_bio->devs[m].devnum;
2703 struct bio *bio = r10_bio->devs[m].bio;
2704 rdev = conf->mirrors[dev].rdev;
2705 if (bio == IO_MADE_GOOD) {
NeilBrown749c55e2011-07-28 11:39:24 +10002706 rdev_clear_badblocks(
2707 rdev,
2708 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002709 r10_bio->sectors, 0);
NeilBrown749c55e2011-07-28 11:39:24 +10002710 rdev_dec_pending(rdev, conf->mddev);
NeilBrownbd870a12011-07-28 11:39:24 +10002711 } else if (bio != NULL &&
2712 !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2713 if (!narrow_write_error(r10_bio, m)) {
2714 md_error(conf->mddev, rdev);
2715 set_bit(R10BIO_Degraded,
2716 &r10_bio->state);
2717 }
2718 rdev_dec_pending(rdev, conf->mddev);
NeilBrown749c55e2011-07-28 11:39:24 +10002719 }
NeilBrown475b0322011-12-23 10:17:55 +11002720 bio = r10_bio->devs[m].repl_bio;
2721 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +11002722 if (rdev && bio == IO_MADE_GOOD) {
NeilBrown475b0322011-12-23 10:17:55 +11002723 rdev_clear_badblocks(
2724 rdev,
2725 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002726 r10_bio->sectors, 0);
NeilBrown475b0322011-12-23 10:17:55 +11002727 rdev_dec_pending(rdev, conf->mddev);
2728 }
NeilBrownbd870a12011-07-28 11:39:24 +10002729 }
2730 if (test_bit(R10BIO_WriteError,
2731 &r10_bio->state))
2732 close_write(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10002733 raid_end_bio_io(r10_bio);
2734 }
2735}
2736
Shaohua Li4ed87312012-10-11 13:34:00 +11002737static void raid10d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
Shaohua Li4ed87312012-10-11 13:34:00 +11002739 struct mddev *mddev = thread->mddev;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002740 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 unsigned long flags;
NeilBrowne879a872011-10-11 16:49:02 +11002742 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002744 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
2746 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002748 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002750
NeilBrown0021b7b2012-07-31 09:08:14 +02002751 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002752
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002754 if (list_empty(head)) {
NeilBrown6cce3b232006-01-06 00:20:16 -08002755 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002757 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002758 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08002760 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 spin_unlock_irqrestore(&conf->device_lock, flags);
2762
2763 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002764 conf = mddev->private;
NeilBrownbd870a12011-07-28 11:39:24 +10002765 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2766 test_bit(R10BIO_WriteError, &r10_bio->state))
NeilBrown749c55e2011-07-28 11:39:24 +10002767 handle_write_completed(conf, r10_bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10002768 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
2769 reshape_request_write(mddev, r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10002770 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01002772 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 recovery_request_write(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002774 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
NeilBrown560f8e52011-07-28 11:39:23 +10002775 handle_read_error(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002776 else {
2777 /* just a partial read to be scheduled from a
2778 * separate context
2779 */
2780 int slot = r10_bio->read_slot;
2781 generic_make_request(r10_bio->devs[slot].bio);
2782 }
NeilBrown4443ae12006-01-06 00:20:28 -08002783
NeilBrown1d9d5242009-10-16 15:55:32 +11002784 cond_resched();
NeilBrownde393cd2011-07-28 11:31:48 +10002785 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2786 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002788 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789}
2790
2791
NeilBrowne879a872011-10-11 16:49:02 +11002792static int init_resync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793{
2794 int buffs;
NeilBrown69335ef2011-12-23 10:17:54 +11002795 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
2797 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02002798 BUG_ON(conf->r10buf_pool);
NeilBrown69335ef2011-12-23 10:17:54 +11002799 conf->have_replacement = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002800 for (i = 0; i < conf->geo.raid_disks; i++)
NeilBrown69335ef2011-12-23 10:17:54 +11002801 if (conf->mirrors[i].replacement)
2802 conf->have_replacement = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2804 if (!conf->r10buf_pool)
2805 return -ENOMEM;
2806 conf->next_resync = 0;
2807 return 0;
2808}
2809
2810/*
2811 * perform a "sync" on one "block"
2812 *
2813 * We need to make sure that no normal I/O request - particularly write
2814 * requests - conflict with active sync requests.
2815 *
2816 * This is achieved by tracking pending requests and a 'barrier' concept
2817 * that can be installed to exclude normal IO requests.
2818 *
2819 * Resync and recovery are handled very differently.
2820 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2821 *
2822 * For resync, we iterate over virtual addresses, read all copies,
2823 * and update if there are differences. If only one copy is live,
2824 * skip it.
2825 * For recovery, we iterate over physical addresses, read a good
2826 * value for each non-in_sync drive, and over-write.
2827 *
2828 * So, for recovery we may have several outstanding complex requests for a
2829 * given address, one for each out-of-sync device. We model this by allocating
2830 * a number of r10_bio structures, one for each out-of-sync device.
2831 * As we setup these structures, we collect all bio's together into a list
2832 * which we then process collectively to add pages, and then process again
2833 * to pass to generic_make_request.
2834 *
2835 * The r10_bio structures are linked using a borrowed master_bio pointer.
2836 * This link is counted in ->remaining. When the r10_bio that points to NULL
2837 * has its remaining count decremented to 0, the whole complex operation
2838 * is complete.
2839 *
2840 */
2841
NeilBrownfd01b882011-10-11 16:47:53 +11002842static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
NeilBrownab9d47e2011-05-11 14:54:41 +10002843 int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
NeilBrowne879a872011-10-11 16:49:02 +11002845 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002846 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 struct bio *biolist = NULL, *bio;
2848 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 int i;
NeilBrown6cce3b232006-01-06 00:20:16 -08002850 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11002851 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 sector_t sectors_skipped = 0;
2853 int chunks_skipped = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002854 sector_t chunk_mask = conf->geo.chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
2856 if (!conf->r10buf_pool)
2857 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002858 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11002861 max_sector = mddev->dev_sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10002862 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
2863 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 max_sector = mddev->resync_max_sectors;
2865 if (sector_nr >= max_sector) {
NeilBrown6cce3b232006-01-06 00:20:16 -08002866 /* If we aborted, we need to abort the
2867 * sync on the 'current' bitmap chucks (there can
2868 * be several when recovering multiple devices).
2869 * as we may have started syncing it but not finished.
2870 * We can find the current address in
2871 * mddev->curr_resync, but for recovery,
2872 * we need to convert that to several
2873 * virtual addresses.
2874 */
NeilBrown3ea7daa2012-05-22 13:53:47 +10002875 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
2876 end_reshape(conf);
2877 return 0;
2878 }
2879
NeilBrown6cce3b232006-01-06 00:20:16 -08002880 if (mddev->curr_resync < max_sector) { /* aborted */
2881 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2882 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2883 &sync_blocks, 1);
NeilBrown5cf00fc2012-05-21 09:28:20 +10002884 else for (i = 0; i < conf->geo.raid_disks; i++) {
NeilBrown6cce3b232006-01-06 00:20:16 -08002885 sector_t sect =
2886 raid10_find_virt(conf, mddev->curr_resync, i);
2887 bitmap_end_sync(mddev->bitmap, sect,
2888 &sync_blocks, 1);
2889 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002890 } else {
2891 /* completed sync */
2892 if ((!mddev->bitmap || conf->fullsync)
2893 && conf->have_replacement
2894 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2895 /* Completed a full sync so the replacements
2896 * are now fully recovered.
2897 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10002898 for (i = 0; i < conf->geo.raid_disks; i++)
NeilBrown9ad1aef2011-12-23 10:17:55 +11002899 if (conf->mirrors[i].replacement)
2900 conf->mirrors[i].replacement
2901 ->recovery_offset
2902 = MaxSector;
2903 }
NeilBrown6cce3b232006-01-06 00:20:16 -08002904 conf->fullsync = 0;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002905 }
NeilBrown6cce3b232006-01-06 00:20:16 -08002906 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07002908 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 return sectors_skipped;
2910 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10002911
2912 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2913 return reshape_request(mddev, sector_nr, skipped);
2914
NeilBrown5cf00fc2012-05-21 09:28:20 +10002915 if (chunks_skipped >= conf->geo.raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 /* if there has been nothing to do on any drive,
2917 * then there is nothing to do at all..
2918 */
NeilBrown57afd892005-06-21 17:17:13 -07002919 *skipped = 1;
2920 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 }
2922
NeilBrownc6207272008-02-06 01:39:52 -08002923 if (max_sector > mddev->resync_max)
2924 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2925
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 /* make sure whole request will fit in a chunk - if chunks
2927 * are meaningful
2928 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10002929 if (conf->geo.near_copies < conf->geo.raid_disks &&
2930 max_sector > (sector_nr | chunk_mask))
2931 max_sector = (sector_nr | chunk_mask) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 /*
2933 * If there is non-resync activity waiting for us then
2934 * put in a delay to throttle resync.
2935 */
NeilBrown0a27ec92006-01-06 00:20:13 -08002936 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
2939 /* Again, very different code for resync and recovery.
2940 * Both must result in an r10bio with a list of bios that
2941 * have bi_end_io, bi_sector, bi_bdev set,
2942 * and bi_private set to the r10bio.
2943 * For recovery, we may actually create several r10bios
2944 * with 2 bios in each, that correspond to the bios in the main one.
2945 * In this case, the subordinate r10bios link back through a
2946 * borrowed master_bio pointer, and the counter in the master
2947 * includes a ref from each subordinate.
2948 */
2949 /* First, we decide what to do and set ->bi_end_io
2950 * To end_sync_read if we want to read, and
2951 * end_sync_write if we will want to write.
2952 */
2953
NeilBrown6cce3b232006-01-06 00:20:16 -08002954 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2956 /* recovery... the complicated one */
NeilBrowne875ece2011-07-28 11:39:24 +10002957 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 r10_bio = NULL;
2959
NeilBrown5cf00fc2012-05-21 09:28:20 +10002960 for (i = 0 ; i < conf->geo.raid_disks; i++) {
NeilBrownab9d47e2011-05-11 14:54:41 +10002961 int still_degraded;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002962 struct r10bio *rb2;
NeilBrownab9d47e2011-05-11 14:54:41 +10002963 sector_t sect;
2964 int must_sync;
NeilBrowne875ece2011-07-28 11:39:24 +10002965 int any_working;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10002966 struct raid10_info *mirror = &conf->mirrors[i];
NeilBrownab9d47e2011-05-11 14:54:41 +10002967
NeilBrown24afd802011-12-23 10:17:55 +11002968 if ((mirror->rdev == NULL ||
2969 test_bit(In_sync, &mirror->rdev->flags))
2970 &&
2971 (mirror->replacement == NULL ||
2972 test_bit(Faulty,
2973 &mirror->replacement->flags)))
NeilBrownab9d47e2011-05-11 14:54:41 +10002974 continue;
2975
2976 still_degraded = 0;
2977 /* want to reconstruct this device */
2978 rb2 = r10_bio;
2979 sect = raid10_find_virt(conf, sector_nr, i);
NeilBrownfc448a12012-07-03 10:37:30 +10002980 if (sect >= mddev->resync_max_sectors) {
2981 /* last stripe is not complete - don't
2982 * try to recover this sector.
2983 */
2984 continue;
2985 }
NeilBrown24afd802011-12-23 10:17:55 +11002986 /* Unless we are doing a full sync, or a replacement
2987 * we only need to recover the block if it is set in
2988 * the bitmap
NeilBrownab9d47e2011-05-11 14:54:41 +10002989 */
2990 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2991 &sync_blocks, 1);
2992 if (sync_blocks < max_sync)
2993 max_sync = sync_blocks;
2994 if (!must_sync &&
NeilBrown24afd802011-12-23 10:17:55 +11002995 mirror->replacement == NULL &&
NeilBrownab9d47e2011-05-11 14:54:41 +10002996 !conf->fullsync) {
2997 /* yep, skip the sync_blocks here, but don't assume
2998 * that there will never be anything to do here
NeilBrown6cce3b232006-01-06 00:20:16 -08002999 */
NeilBrownab9d47e2011-05-11 14:54:41 +10003000 chunks_skipped = -1;
3001 continue;
3002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
NeilBrownab9d47e2011-05-11 14:54:41 +10003004 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
3005 raise_barrier(conf, rb2 != NULL);
3006 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
NeilBrownab9d47e2011-05-11 14:54:41 +10003008 r10_bio->master_bio = (struct bio*)rb2;
3009 if (rb2)
3010 atomic_inc(&rb2->remaining);
3011 r10_bio->mddev = mddev;
3012 set_bit(R10BIO_IsRecover, &r10_bio->state);
3013 r10_bio->sector = sect;
NeilBrown6cce3b232006-01-06 00:20:16 -08003014
NeilBrownab9d47e2011-05-11 14:54:41 +10003015 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10003016
NeilBrownab9d47e2011-05-11 14:54:41 +10003017 /* Need to check if the array will still be
3018 * degraded
3019 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003020 for (j = 0; j < conf->geo.raid_disks; j++)
NeilBrownab9d47e2011-05-11 14:54:41 +10003021 if (conf->mirrors[j].rdev == NULL ||
3022 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
3023 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07003024 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003026
3027 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3028 &sync_blocks, still_degraded);
3029
NeilBrowne875ece2011-07-28 11:39:24 +10003030 any_working = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10003031 for (j=0; j<conf->copies;j++) {
NeilBrowne875ece2011-07-28 11:39:24 +10003032 int k;
NeilBrownab9d47e2011-05-11 14:54:41 +10003033 int d = r10_bio->devs[j].devnum;
NeilBrown5e570282011-07-28 11:39:25 +10003034 sector_t from_addr, to_addr;
NeilBrown3cb03002011-10-11 16:45:26 +11003035 struct md_rdev *rdev;
NeilBrown40c356c2011-07-28 11:39:24 +10003036 sector_t sector, first_bad;
3037 int bad_sectors;
NeilBrownab9d47e2011-05-11 14:54:41 +10003038 if (!conf->mirrors[d].rdev ||
3039 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
3040 continue;
3041 /* This is where we read from */
NeilBrowne875ece2011-07-28 11:39:24 +10003042 any_working = 1;
NeilBrown40c356c2011-07-28 11:39:24 +10003043 rdev = conf->mirrors[d].rdev;
3044 sector = r10_bio->devs[j].addr;
3045
3046 if (is_badblock(rdev, sector, max_sync,
3047 &first_bad, &bad_sectors)) {
3048 if (first_bad > sector)
3049 max_sync = first_bad - sector;
3050 else {
3051 bad_sectors -= (sector
3052 - first_bad);
3053 if (max_sync > bad_sectors)
3054 max_sync = bad_sectors;
3055 continue;
3056 }
3057 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003058 bio = r10_bio->devs[0].bio;
3059 bio->bi_next = biolist;
3060 biolist = bio;
3061 bio->bi_private = r10_bio;
3062 bio->bi_end_io = end_sync_read;
3063 bio->bi_rw = READ;
NeilBrown5e570282011-07-28 11:39:25 +10003064 from_addr = r10_bio->devs[j].addr;
NeilBrown24afd802011-12-23 10:17:55 +11003065 bio->bi_sector = from_addr + rdev->data_offset;
3066 bio->bi_bdev = rdev->bdev;
3067 atomic_inc(&rdev->nr_pending);
3068 /* and we write to 'i' (if not in_sync) */
NeilBrownab9d47e2011-05-11 14:54:41 +10003069
3070 for (k=0; k<conf->copies; k++)
3071 if (r10_bio->devs[k].devnum == i)
3072 break;
3073 BUG_ON(k == conf->copies);
NeilBrown5e570282011-07-28 11:39:25 +10003074 to_addr = r10_bio->devs[k].addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003075 r10_bio->devs[0].devnum = d;
NeilBrown5e570282011-07-28 11:39:25 +10003076 r10_bio->devs[0].addr = from_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003077 r10_bio->devs[1].devnum = i;
NeilBrown5e570282011-07-28 11:39:25 +10003078 r10_bio->devs[1].addr = to_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003079
NeilBrown24afd802011-12-23 10:17:55 +11003080 rdev = mirror->rdev;
3081 if (!test_bit(In_sync, &rdev->flags)) {
3082 bio = r10_bio->devs[1].bio;
3083 bio->bi_next = biolist;
3084 biolist = bio;
3085 bio->bi_private = r10_bio;
3086 bio->bi_end_io = end_sync_write;
3087 bio->bi_rw = WRITE;
3088 bio->bi_sector = to_addr
3089 + rdev->data_offset;
3090 bio->bi_bdev = rdev->bdev;
3091 atomic_inc(&r10_bio->remaining);
3092 } else
3093 r10_bio->devs[1].bio->bi_end_io = NULL;
3094
3095 /* and maybe write to replacement */
3096 bio = r10_bio->devs[1].repl_bio;
3097 if (bio)
3098 bio->bi_end_io = NULL;
3099 rdev = mirror->replacement;
3100 /* Note: if rdev != NULL, then bio
3101 * cannot be NULL as r10buf_pool_alloc will
3102 * have allocated it.
3103 * So the second test here is pointless.
3104 * But it keeps semantic-checkers happy, and
3105 * this comment keeps human reviewers
3106 * happy.
3107 */
3108 if (rdev == NULL || bio == NULL ||
3109 test_bit(Faulty, &rdev->flags))
3110 break;
3111 bio->bi_next = biolist;
3112 biolist = bio;
3113 bio->bi_private = r10_bio;
3114 bio->bi_end_io = end_sync_write;
3115 bio->bi_rw = WRITE;
3116 bio->bi_sector = to_addr + rdev->data_offset;
3117 bio->bi_bdev = rdev->bdev;
3118 atomic_inc(&r10_bio->remaining);
NeilBrownab9d47e2011-05-11 14:54:41 +10003119 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003121 if (j == conf->copies) {
NeilBrowne875ece2011-07-28 11:39:24 +10003122 /* Cannot recover, so abort the recovery or
3123 * record a bad block */
NeilBrownab9d47e2011-05-11 14:54:41 +10003124 put_buf(r10_bio);
3125 if (rb2)
3126 atomic_dec(&rb2->remaining);
3127 r10_bio = rb2;
NeilBrowne875ece2011-07-28 11:39:24 +10003128 if (any_working) {
3129 /* problem is that there are bad blocks
3130 * on other device(s)
3131 */
3132 int k;
3133 for (k = 0; k < conf->copies; k++)
3134 if (r10_bio->devs[k].devnum == i)
3135 break;
NeilBrown24afd802011-12-23 10:17:55 +11003136 if (!test_bit(In_sync,
3137 &mirror->rdev->flags)
3138 && !rdev_set_badblocks(
3139 mirror->rdev,
3140 r10_bio->devs[k].addr,
3141 max_sync, 0))
3142 any_working = 0;
3143 if (mirror->replacement &&
3144 !rdev_set_badblocks(
3145 mirror->replacement,
NeilBrowne875ece2011-07-28 11:39:24 +10003146 r10_bio->devs[k].addr,
3147 max_sync, 0))
3148 any_working = 0;
3149 }
3150 if (!any_working) {
3151 if (!test_and_set_bit(MD_RECOVERY_INTR,
3152 &mddev->recovery))
3153 printk(KERN_INFO "md/raid10:%s: insufficient "
3154 "working devices for recovery.\n",
3155 mdname(mddev));
NeilBrown24afd802011-12-23 10:17:55 +11003156 mirror->recovery_disabled
NeilBrowne875ece2011-07-28 11:39:24 +10003157 = mddev->recovery_disabled;
3158 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003159 break;
3160 }
3161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 if (biolist == NULL) {
3163 while (r10_bio) {
NeilBrown9f2c9d12011-10-11 16:48:43 +11003164 struct r10bio *rb2 = r10_bio;
3165 r10_bio = (struct r10bio*) rb2->master_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 rb2->master_bio = NULL;
3167 put_buf(rb2);
3168 }
3169 goto giveup;
3170 }
3171 } else {
3172 /* resync. Schedule a read for every block at this virt offset */
3173 int count = 0;
NeilBrown6cce3b232006-01-06 00:20:16 -08003174
NeilBrown78200d42009-02-25 13:18:47 +11003175 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
3176
NeilBrown6cce3b232006-01-06 00:20:16 -08003177 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
3178 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003179 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3180 &mddev->recovery)) {
NeilBrown6cce3b232006-01-06 00:20:16 -08003181 /* We can skip this block */
3182 *skipped = 1;
3183 return sync_blocks + sectors_skipped;
3184 }
3185 if (sync_blocks < max_sync)
3186 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
3188
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 r10_bio->mddev = mddev;
3190 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b232006-01-06 00:20:16 -08003191 raise_barrier(conf, 0);
3192 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
3194 r10_bio->master_bio = NULL;
3195 r10_bio->sector = sector_nr;
3196 set_bit(R10BIO_IsSync, &r10_bio->state);
3197 raid10_find_phys(conf, r10_bio);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003198 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
NeilBrown5cf00fc2012-05-21 09:28:20 +10003200 for (i = 0; i < conf->copies; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 int d = r10_bio->devs[i].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10003202 sector_t first_bad, sector;
3203 int bad_sectors;
3204
NeilBrown9ad1aef2011-12-23 10:17:55 +11003205 if (r10_bio->devs[i].repl_bio)
3206 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3207
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 bio = r10_bio->devs[i].bio;
3209 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07003210 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08003212 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 continue;
NeilBrown40c356c2011-07-28 11:39:24 +10003214 sector = r10_bio->devs[i].addr;
3215 if (is_badblock(conf->mirrors[d].rdev,
3216 sector, max_sync,
3217 &first_bad, &bad_sectors)) {
3218 if (first_bad > sector)
3219 max_sync = first_bad - sector;
3220 else {
3221 bad_sectors -= (sector - first_bad);
3222 if (max_sync > bad_sectors)
Dan Carpenter91502f02012-10-11 14:20:58 +11003223 max_sync = bad_sectors;
NeilBrown40c356c2011-07-28 11:39:24 +10003224 continue;
3225 }
3226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
3228 atomic_inc(&r10_bio->remaining);
3229 bio->bi_next = biolist;
3230 biolist = bio;
3231 bio->bi_private = r10_bio;
3232 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08003233 bio->bi_rw = READ;
NeilBrown40c356c2011-07-28 11:39:24 +10003234 bio->bi_sector = sector +
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 conf->mirrors[d].rdev->data_offset;
3236 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
3237 count++;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003238
3239 if (conf->mirrors[d].replacement == NULL ||
3240 test_bit(Faulty,
3241 &conf->mirrors[d].replacement->flags))
3242 continue;
3243
3244 /* Need to set up for writing to the replacement */
3245 bio = r10_bio->devs[i].repl_bio;
3246 clear_bit(BIO_UPTODATE, &bio->bi_flags);
3247
3248 sector = r10_bio->devs[i].addr;
3249 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
3250 bio->bi_next = biolist;
3251 biolist = bio;
3252 bio->bi_private = r10_bio;
3253 bio->bi_end_io = end_sync_write;
3254 bio->bi_rw = WRITE;
3255 bio->bi_sector = sector +
3256 conf->mirrors[d].replacement->data_offset;
3257 bio->bi_bdev = conf->mirrors[d].replacement->bdev;
3258 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 }
3260
3261 if (count < 2) {
3262 for (i=0; i<conf->copies; i++) {
3263 int d = r10_bio->devs[i].devnum;
3264 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10003265 rdev_dec_pending(conf->mirrors[d].rdev,
3266 mddev);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003267 if (r10_bio->devs[i].repl_bio &&
3268 r10_bio->devs[i].repl_bio->bi_end_io)
3269 rdev_dec_pending(
3270 conf->mirrors[d].replacement,
3271 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 }
3273 put_buf(r10_bio);
3274 biolist = NULL;
3275 goto giveup;
3276 }
3277 }
3278
3279 for (bio = biolist; bio ; bio=bio->bi_next) {
3280
3281 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
3282 if (bio->bi_end_io)
3283 bio->bi_flags |= 1 << BIO_UPTODATE;
3284 bio->bi_vcnt = 0;
3285 bio->bi_idx = 0;
3286 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 bio->bi_size = 0;
3288 }
3289
3290 nr_sectors = 0;
NeilBrown6cce3b232006-01-06 00:20:16 -08003291 if (sector_nr + max_sync < max_sector)
3292 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 do {
3294 struct page *page;
3295 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 if (sector_nr + (len>>9) > max_sector)
3297 len = (max_sector - sector_nr) << 9;
3298 if (len == 0)
3299 break;
3300 for (bio= biolist ; bio ; bio=bio->bi_next) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003301 struct bio *bio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
NeilBrownab9d47e2011-05-11 14:54:41 +10003303 if (bio_add_page(bio, page, len, 0))
3304 continue;
3305
3306 /* stop here */
3307 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
3308 for (bio2 = biolist;
3309 bio2 && bio2 != bio;
3310 bio2 = bio2->bi_next) {
3311 /* remove last page from this bio */
3312 bio2->bi_vcnt--;
3313 bio2->bi_size -= len;
3314 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003316 goto bio_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 }
3318 nr_sectors += len>>9;
3319 sector_nr += len>>9;
3320 } while (biolist->bi_vcnt < RESYNC_PAGES);
3321 bio_full:
3322 r10_bio->sectors = nr_sectors;
3323
3324 while (biolist) {
3325 bio = biolist;
3326 biolist = biolist->bi_next;
3327
3328 bio->bi_next = NULL;
3329 r10_bio = bio->bi_private;
3330 r10_bio->sectors = nr_sectors;
3331
3332 if (bio->bi_end_io == end_sync_read) {
3333 md_sync_acct(bio->bi_bdev, nr_sectors);
3334 generic_make_request(bio);
3335 }
3336 }
3337
NeilBrown57afd892005-06-21 17:17:13 -07003338 if (sectors_skipped)
3339 /* pretend they weren't skipped, it makes
3340 * no important difference in this case
3341 */
3342 md_done_sync(mddev, sectors_skipped, 1);
3343
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 return sectors_skipped + nr_sectors;
3345 giveup:
3346 /* There is nowhere to write, so all non-sync
NeilBrowne875ece2011-07-28 11:39:24 +10003347 * drives must be failed or in resync, all drives
3348 * have a bad block, so try the next chunk...
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 */
NeilBrown09b40682009-02-25 13:18:47 +11003350 if (sector_nr + max_sync < max_sector)
3351 max_sector = sector_nr + max_sync;
3352
3353 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 chunks_skipped ++;
3355 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357}
3358
Dan Williams80c3a6c2009-03-17 18:10:40 -07003359static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11003360raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07003361{
3362 sector_t size;
NeilBrowne879a872011-10-11 16:49:02 +11003363 struct r10conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003364
3365 if (!raid_disks)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003366 raid_disks = min(conf->geo.raid_disks,
3367 conf->prev.raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003368 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003369 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003370
NeilBrown5cf00fc2012-05-21 09:28:20 +10003371 size = sectors >> conf->geo.chunk_shift;
3372 sector_div(size, conf->geo.far_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003373 size = size * raid_disks;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003374 sector_div(size, conf->geo.near_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003375
NeilBrown5cf00fc2012-05-21 09:28:20 +10003376 return size << conf->geo.chunk_shift;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003377}
3378
NeilBrown6508fdb2012-05-17 10:08:45 +10003379static void calc_sectors(struct r10conf *conf, sector_t size)
3380{
3381 /* Calculate the number of sectors-per-device that will
3382 * actually be used, and set conf->dev_sectors and
3383 * conf->stride
3384 */
3385
NeilBrown5cf00fc2012-05-21 09:28:20 +10003386 size = size >> conf->geo.chunk_shift;
3387 sector_div(size, conf->geo.far_copies);
3388 size = size * conf->geo.raid_disks;
3389 sector_div(size, conf->geo.near_copies);
NeilBrown6508fdb2012-05-17 10:08:45 +10003390 /* 'size' is now the number of chunks in the array */
3391 /* calculate "used chunks per device" */
3392 size = size * conf->copies;
3393
3394 /* We need to round up when dividing by raid_disks to
3395 * get the stride size.
3396 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003397 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
NeilBrown6508fdb2012-05-17 10:08:45 +10003398
NeilBrown5cf00fc2012-05-21 09:28:20 +10003399 conf->dev_sectors = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003400
NeilBrown5cf00fc2012-05-21 09:28:20 +10003401 if (conf->geo.far_offset)
3402 conf->geo.stride = 1 << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003403 else {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003404 sector_div(size, conf->geo.far_copies);
3405 conf->geo.stride = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003406 }
3407}
Trela, Maciejdab8b292010-03-08 16:02:45 +11003408
NeilBrowndeb200d2012-05-21 09:28:33 +10003409enum geo_type {geo_new, geo_old, geo_start};
3410static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3411{
3412 int nc, fc, fo;
3413 int layout, chunk, disks;
3414 switch (new) {
3415 case geo_old:
3416 layout = mddev->layout;
3417 chunk = mddev->chunk_sectors;
3418 disks = mddev->raid_disks - mddev->delta_disks;
3419 break;
3420 case geo_new:
3421 layout = mddev->new_layout;
3422 chunk = mddev->new_chunk_sectors;
3423 disks = mddev->raid_disks;
3424 break;
3425 default: /* avoid 'may be unused' warnings */
3426 case geo_start: /* new when starting reshape - raid_disks not
3427 * updated yet. */
3428 layout = mddev->new_layout;
3429 chunk = mddev->new_chunk_sectors;
3430 disks = mddev->raid_disks + mddev->delta_disks;
3431 break;
3432 }
3433 if (layout >> 17)
3434 return -1;
3435 if (chunk < (PAGE_SIZE >> 9) ||
3436 !is_power_of_2(chunk))
3437 return -2;
3438 nc = layout & 255;
3439 fc = (layout >> 8) & 255;
3440 fo = layout & (1<<16);
3441 geo->raid_disks = disks;
3442 geo->near_copies = nc;
3443 geo->far_copies = fc;
3444 geo->far_offset = fo;
3445 geo->chunk_mask = chunk - 1;
3446 geo->chunk_shift = ffz(~chunk);
3447 return nc*fc;
3448}
3449
NeilBrowne879a872011-10-11 16:49:02 +11003450static struct r10conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451{
NeilBrowne879a872011-10-11 16:49:02 +11003452 struct r10conf *conf = NULL;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003453 int err = -EINVAL;
NeilBrowndeb200d2012-05-21 09:28:33 +10003454 struct geom geo;
3455 int copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
NeilBrowndeb200d2012-05-21 09:28:33 +10003457 copies = setup_geo(&geo, mddev, geo_new);
3458
3459 if (copies == -2) {
NeilBrown128595e2010-05-03 14:47:14 +10003460 printk(KERN_ERR "md/raid10:%s: chunk size must be "
3461 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
3462 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003463 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 }
NeilBrown2604b702006-01-06 00:20:36 -08003465
NeilBrowndeb200d2012-05-21 09:28:33 +10003466 if (copies < 2 || copies > mddev->raid_disks) {
NeilBrown128595e2010-05-03 14:47:14 +10003467 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
Maciej Trelaf73ea872010-06-16 11:46:29 +01003468 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 goto out;
3470 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11003471
3472 err = -ENOMEM;
NeilBrowne879a872011-10-11 16:49:02 +11003473 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003474 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003476
NeilBrown3ea7daa2012-05-22 13:53:47 +10003477 /* FIXME calc properly */
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003478 conf->mirrors = kzalloc(sizeof(struct raid10_info)*(mddev->raid_disks +
NeilBrown3ea7daa2012-05-22 13:53:47 +10003479 max(0,mddev->delta_disks)),
Trela, Maciejdab8b292010-03-08 16:02:45 +11003480 GFP_KERNEL);
3481 if (!conf->mirrors)
3482 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08003483
3484 conf->tmppage = alloc_page(GFP_KERNEL);
3485 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003486 goto out;
3487
NeilBrowndeb200d2012-05-21 09:28:33 +10003488 conf->geo = geo;
3489 conf->copies = copies;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003490 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
3491 r10bio_pool_free, conf);
3492 if (!conf->r10bio_pool)
3493 goto out;
3494
NeilBrown6508fdb2012-05-17 10:08:45 +10003495 calc_sectors(conf, mddev->dev_sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003496 if (mddev->reshape_position == MaxSector) {
3497 conf->prev = conf->geo;
3498 conf->reshape_progress = MaxSector;
3499 } else {
3500 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
3501 err = -EINVAL;
3502 goto out;
3503 }
3504 conf->reshape_progress = mddev->reshape_position;
3505 if (conf->prev.far_offset)
3506 conf->prev.stride = 1 << conf->prev.chunk_shift;
3507 else
3508 /* far_copies must be 1 */
3509 conf->prev.stride = conf->dev_sectors;
3510 }
Neil Browne7e72bf2008-05-14 16:05:54 -07003511 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003512 INIT_LIST_HEAD(&conf->retry_list);
3513
3514 spin_lock_init(&conf->resync_lock);
3515 init_waitqueue_head(&conf->wait_barrier);
3516
NeilBrown02326052012-07-03 15:56:52 +10003517 conf->thread = md_register_thread(raid10d, mddev, "raid10");
Trela, Maciejdab8b292010-03-08 16:02:45 +11003518 if (!conf->thread)
3519 goto out;
3520
Trela, Maciejdab8b292010-03-08 16:02:45 +11003521 conf->mddev = mddev;
3522 return conf;
3523
3524 out:
NeilBrown3ea7daa2012-05-22 13:53:47 +10003525 if (err == -ENOMEM)
3526 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
3527 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003528 if (conf) {
3529 if (conf->r10bio_pool)
3530 mempool_destroy(conf->r10bio_pool);
3531 kfree(conf->mirrors);
3532 safe_put_page(conf->tmppage);
3533 kfree(conf);
3534 }
3535 return ERR_PTR(err);
3536}
3537
NeilBrownfd01b882011-10-11 16:47:53 +11003538static int run(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003539{
NeilBrowne879a872011-10-11 16:49:02 +11003540 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003541 int i, disk_idx, chunk_size;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003542 struct raid10_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11003543 struct md_rdev *rdev;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003544 sector_t size;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003545 sector_t min_offset_diff = 0;
3546 int first = 1;
Shaohua Li532a2a32012-10-11 13:30:52 +11003547 bool discard_supported = false;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003548
3549 if (mddev->private == NULL) {
3550 conf = setup_conf(mddev);
3551 if (IS_ERR(conf))
3552 return PTR_ERR(conf);
3553 mddev->private = conf;
3554 }
3555 conf = mddev->private;
3556 if (!conf)
3557 goto out;
3558
Trela, Maciejdab8b292010-03-08 16:02:45 +11003559 mddev->thread = conf->thread;
3560 conf->thread = NULL;
3561
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003562 chunk_size = mddev->chunk_sectors << 9;
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003563 if (mddev->queue) {
Shaohua Li532a2a32012-10-11 13:30:52 +11003564 blk_queue_max_discard_sectors(mddev->queue,
3565 mddev->chunk_sectors);
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003566 blk_queue_io_min(mddev->queue, chunk_size);
3567 if (conf->geo.raid_disks % conf->geo.near_copies)
3568 blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
3569 else
3570 blk_queue_io_opt(mddev->queue, chunk_size *
3571 (conf->geo.raid_disks / conf->geo.near_copies));
3572 }
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003573
NeilBrowndafb20f2012-03-19 12:46:39 +11003574 rdev_for_each(rdev, mddev) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10003575 long long diff;
NeilBrownaba336b2012-05-31 15:39:11 +10003576 struct request_queue *q;
NeilBrown34b343c2011-07-28 11:31:47 +10003577
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 disk_idx = rdev->raid_disk;
NeilBrownf8c9e742012-05-21 09:28:33 +10003579 if (disk_idx < 0)
3580 continue;
3581 if (disk_idx >= conf->geo.raid_disks &&
3582 disk_idx >= conf->prev.raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 continue;
3584 disk = conf->mirrors + disk_idx;
3585
NeilBrown56a25592011-12-23 10:17:55 +11003586 if (test_bit(Replacement, &rdev->flags)) {
3587 if (disk->replacement)
3588 goto out_free_conf;
3589 disk->replacement = rdev;
3590 } else {
3591 if (disk->rdev)
3592 goto out_free_conf;
3593 disk->rdev = rdev;
3594 }
NeilBrownaba336b2012-05-31 15:39:11 +10003595 q = bdev_get_queue(rdev->bdev);
3596 if (q->merge_bvec_fn)
3597 mddev->merge_check_needed = 1;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003598 diff = (rdev->new_data_offset - rdev->data_offset);
3599 if (!mddev->reshape_backwards)
3600 diff = -diff;
3601 if (diff < 0)
3602 diff = 0;
3603 if (first || diff < min_offset_diff)
3604 min_offset_diff = diff;
NeilBrown56a25592011-12-23 10:17:55 +11003605
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003606 if (mddev->gendisk)
3607 disk_stack_limits(mddev->gendisk, rdev->bdev,
3608 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
3610 disk->head_position = 0;
Shaohua Li532a2a32012-10-11 13:30:52 +11003611
3612 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3613 discard_supported = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10003615
Jonathan Brassowed30be02012-10-31 11:42:30 +11003616 if (mddev->queue) {
3617 if (discard_supported)
3618 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
3619 mddev->queue);
3620 else
3621 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
3622 mddev->queue);
3623 }
NeilBrown6d508242005-09-09 16:24:03 -07003624 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10003625 if (!enough(conf, -1)) {
NeilBrown128595e2010-05-03 14:47:14 +10003626 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07003627 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 goto out_free_conf;
3629 }
3630
NeilBrown3ea7daa2012-05-22 13:53:47 +10003631 if (conf->reshape_progress != MaxSector) {
3632 /* must ensure that shape change is supported */
3633 if (conf->geo.far_copies != 1 &&
3634 conf->geo.far_offset == 0)
3635 goto out_free_conf;
3636 if (conf->prev.far_copies != 1 &&
3637 conf->geo.far_offset == 0)
3638 goto out_free_conf;
3639 }
3640
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 mddev->degraded = 0;
NeilBrownf8c9e742012-05-21 09:28:33 +10003642 for (i = 0;
3643 i < conf->geo.raid_disks
3644 || i < conf->prev.raid_disks;
3645 i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
3647 disk = conf->mirrors + i;
3648
NeilBrown56a25592011-12-23 10:17:55 +11003649 if (!disk->rdev && disk->replacement) {
3650 /* The replacement is all we have - use it */
3651 disk->rdev = disk->replacement;
3652 disk->replacement = NULL;
3653 clear_bit(Replacement, &disk->rdev->flags);
3654 }
3655
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003656 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07003657 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 disk->head_position = 0;
3659 mddev->degraded++;
Neil Brown8c2e8702008-06-28 08:30:52 +10003660 if (disk->rdev)
3661 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 }
NeilBrownd890fa22011-10-26 11:54:39 +11003663 disk->recovery_disabled = mddev->recovery_disabled - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 }
3665
Andre Noll8c6ac862009-06-18 08:48:06 +10003666 if (mddev->recovery_cp != MaxSector)
NeilBrown128595e2010-05-03 14:47:14 +10003667 printk(KERN_NOTICE "md/raid10:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10003668 " -- starting background reconstruction\n",
3669 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 printk(KERN_INFO
NeilBrown128595e2010-05-03 14:47:14 +10003671 "md/raid10:%s: active with %d out of %d devices\n",
NeilBrown5cf00fc2012-05-21 09:28:20 +10003672 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
3673 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 /*
3675 * Ok, everything is just fine now
3676 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11003677 mddev->dev_sectors = conf->dev_sectors;
3678 size = raid10_size(mddev, 0, 0);
3679 md_set_array_sectors(mddev, size);
3680 mddev->resync_max_sectors = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003682 if (mddev->queue) {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003683 int stripe = conf->geo.raid_disks *
Andre Noll9d8f0362009-06-18 08:45:01 +10003684 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003685 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
3686 mddev->queue->backing_dev_info.congested_data = mddev;
3687
3688 /* Calculate max read-ahead size.
3689 * We need to readahead at least twice a whole stripe....
3690 * maybe...
3691 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003692 stripe /= conf->geo.near_copies;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003693 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3694 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003695 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 }
3697
Martin K. Petersena91a2782011-03-17 11:11:05 +01003698
3699 if (md_integrity_register(mddev))
3700 goto out_free_conf;
3701
NeilBrown3ea7daa2012-05-22 13:53:47 +10003702 if (conf->reshape_progress != MaxSector) {
3703 unsigned long before_length, after_length;
3704
3705 before_length = ((1 << conf->prev.chunk_shift) *
3706 conf->prev.far_copies);
3707 after_length = ((1 << conf->geo.chunk_shift) *
3708 conf->geo.far_copies);
3709
3710 if (max(before_length, after_length) > min_offset_diff) {
3711 /* This cannot work */
3712 printk("md/raid10: offset difference not enough to continue reshape\n");
3713 goto out_free_conf;
3714 }
3715 conf->offset_diff = min_offset_diff;
3716
3717 conf->reshape_safe = conf->reshape_progress;
3718 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3719 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3720 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3721 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3722 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3723 "reshape");
3724 }
3725
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 return 0;
3727
3728out_free_conf:
NeilBrown01f96c02011-09-21 15:30:20 +10003729 md_unregister_thread(&mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 if (conf->r10bio_pool)
3731 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08003732 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003733 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 kfree(conf);
3735 mddev->private = NULL;
3736out:
3737 return -EIO;
3738}
3739
NeilBrownfd01b882011-10-11 16:47:53 +11003740static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741{
NeilBrowne879a872011-10-11 16:49:02 +11003742 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
NeilBrown409c57f2009-03-31 14:39:39 +11003744 raise_barrier(conf, 0);
3745 lower_barrier(conf);
3746
NeilBrown01f96c02011-09-21 15:30:20 +10003747 md_unregister_thread(&mddev->thread);
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003748 if (mddev->queue)
3749 /* the unplug fn references 'conf'*/
3750 blk_sync_queue(mddev->queue);
3751
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 if (conf->r10bio_pool)
3753 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003754 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 kfree(conf);
3756 mddev->private = NULL;
3757 return 0;
3758}
3759
NeilBrownfd01b882011-10-11 16:47:53 +11003760static void raid10_quiesce(struct mddev *mddev, int state)
NeilBrown6cce3b232006-01-06 00:20:16 -08003761{
NeilBrowne879a872011-10-11 16:49:02 +11003762 struct r10conf *conf = mddev->private;
NeilBrown6cce3b232006-01-06 00:20:16 -08003763
3764 switch(state) {
3765 case 1:
3766 raise_barrier(conf, 0);
3767 break;
3768 case 0:
3769 lower_barrier(conf);
3770 break;
3771 }
NeilBrown6cce3b232006-01-06 00:20:16 -08003772}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
NeilBrown006a09a2012-03-19 12:46:40 +11003774static int raid10_resize(struct mddev *mddev, sector_t sectors)
3775{
3776 /* Resize of 'far' arrays is not supported.
3777 * For 'near' and 'offset' arrays we can set the
3778 * number of sectors used to be an appropriate multiple
3779 * of the chunk size.
3780 * For 'offset', this is far_copies*chunksize.
3781 * For 'near' the multiplier is the LCM of
3782 * near_copies and raid_disks.
3783 * So if far_copies > 1 && !far_offset, fail.
3784 * Else find LCM(raid_disks, near_copy)*far_copies and
3785 * multiply by chunk_size. Then round to this number.
3786 * This is mostly done by raid10_size()
3787 */
3788 struct r10conf *conf = mddev->private;
3789 sector_t oldsize, size;
3790
NeilBrownf8c9e742012-05-21 09:28:33 +10003791 if (mddev->reshape_position != MaxSector)
3792 return -EBUSY;
3793
NeilBrown5cf00fc2012-05-21 09:28:20 +10003794 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
NeilBrown006a09a2012-03-19 12:46:40 +11003795 return -EINVAL;
3796
3797 oldsize = raid10_size(mddev, 0, 0);
3798 size = raid10_size(mddev, sectors, 0);
NeilBrowna4a61252012-05-22 13:55:27 +10003799 if (mddev->external_size &&
3800 mddev->array_sectors > size)
NeilBrown006a09a2012-03-19 12:46:40 +11003801 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10003802 if (mddev->bitmap) {
3803 int ret = bitmap_resize(mddev->bitmap, size, 0, 0);
3804 if (ret)
3805 return ret;
3806 }
3807 md_set_array_sectors(mddev, size);
NeilBrown006a09a2012-03-19 12:46:40 +11003808 set_capacity(mddev->gendisk, mddev->array_sectors);
3809 revalidate_disk(mddev->gendisk);
3810 if (sectors > mddev->dev_sectors &&
3811 mddev->recovery_cp > oldsize) {
3812 mddev->recovery_cp = oldsize;
3813 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3814 }
NeilBrown6508fdb2012-05-17 10:08:45 +10003815 calc_sectors(conf, sectors);
3816 mddev->dev_sectors = conf->dev_sectors;
NeilBrown006a09a2012-03-19 12:46:40 +11003817 mddev->resync_max_sectors = size;
3818 return 0;
3819}
3820
NeilBrownfd01b882011-10-11 16:47:53 +11003821static void *raid10_takeover_raid0(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003822{
NeilBrown3cb03002011-10-11 16:45:26 +11003823 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +11003824 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003825
3826 if (mddev->degraded > 0) {
NeilBrown128595e2010-05-03 14:47:14 +10003827 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
3828 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003829 return ERR_PTR(-EINVAL);
3830 }
3831
Trela, Maciejdab8b292010-03-08 16:02:45 +11003832 /* Set new parameters */
3833 mddev->new_level = 10;
3834 /* new layout: far_copies = 1, near_copies = 2 */
3835 mddev->new_layout = (1<<8) + 2;
3836 mddev->new_chunk_sectors = mddev->chunk_sectors;
3837 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003838 mddev->raid_disks *= 2;
3839 /* make sure it will be not marked as dirty */
3840 mddev->recovery_cp = MaxSector;
3841
3842 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003843 if (!IS_ERR(conf)) {
NeilBrowndafb20f2012-03-19 12:46:39 +11003844 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003845 if (rdev->raid_disk >= 0)
3846 rdev->new_raid_disk = rdev->raid_disk * 2;
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003847 conf->barrier = 1;
3848 }
3849
Trela, Maciejdab8b292010-03-08 16:02:45 +11003850 return conf;
3851}
3852
NeilBrownfd01b882011-10-11 16:47:53 +11003853static void *raid10_takeover(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003854{
NeilBrowne373ab12011-10-11 16:48:59 +11003855 struct r0conf *raid0_conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003856
3857 /* raid10 can take over:
3858 * raid0 - providing it has only two drives
3859 */
3860 if (mddev->level == 0) {
3861 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11003862 raid0_conf = mddev->private;
3863 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown128595e2010-05-03 14:47:14 +10003864 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
3865 " with more than one zone.\n",
3866 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003867 return ERR_PTR(-EINVAL);
3868 }
3869 return raid10_takeover_raid0(mddev);
3870 }
3871 return ERR_PTR(-EINVAL);
3872}
3873
NeilBrown3ea7daa2012-05-22 13:53:47 +10003874static int raid10_check_reshape(struct mddev *mddev)
3875{
3876 /* Called when there is a request to change
3877 * - layout (to ->new_layout)
3878 * - chunk size (to ->new_chunk_sectors)
3879 * - raid_disks (by delta_disks)
3880 * or when trying to restart a reshape that was ongoing.
3881 *
3882 * We need to validate the request and possibly allocate
3883 * space if that might be an issue later.
3884 *
3885 * Currently we reject any reshape of a 'far' mode array,
3886 * allow chunk size to change if new is generally acceptable,
3887 * allow raid_disks to increase, and allow
3888 * a switch between 'near' mode and 'offset' mode.
3889 */
3890 struct r10conf *conf = mddev->private;
3891 struct geom geo;
3892
3893 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
3894 return -EINVAL;
3895
3896 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
3897 /* mustn't change number of copies */
3898 return -EINVAL;
3899 if (geo.far_copies > 1 && !geo.far_offset)
3900 /* Cannot switch to 'far' mode */
3901 return -EINVAL;
3902
3903 if (mddev->array_sectors & geo.chunk_mask)
3904 /* not factor of array size */
3905 return -EINVAL;
3906
NeilBrown3ea7daa2012-05-22 13:53:47 +10003907 if (!enough(conf, -1))
3908 return -EINVAL;
3909
3910 kfree(conf->mirrors_new);
3911 conf->mirrors_new = NULL;
3912 if (mddev->delta_disks > 0) {
3913 /* allocate new 'mirrors' list */
3914 conf->mirrors_new = kzalloc(
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003915 sizeof(struct raid10_info)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003916 *(mddev->raid_disks +
3917 mddev->delta_disks),
3918 GFP_KERNEL);
3919 if (!conf->mirrors_new)
3920 return -ENOMEM;
3921 }
3922 return 0;
3923}
3924
3925/*
3926 * Need to check if array has failed when deciding whether to:
3927 * - start an array
3928 * - remove non-faulty devices
3929 * - add a spare
3930 * - allow a reshape
3931 * This determination is simple when no reshape is happening.
3932 * However if there is a reshape, we need to carefully check
3933 * both the before and after sections.
3934 * This is because some failed devices may only affect one
3935 * of the two sections, and some non-in_sync devices may
3936 * be insync in the section most affected by failed devices.
3937 */
3938static int calc_degraded(struct r10conf *conf)
3939{
3940 int degraded, degraded2;
3941 int i;
3942
3943 rcu_read_lock();
3944 degraded = 0;
3945 /* 'prev' section first */
3946 for (i = 0; i < conf->prev.raid_disks; i++) {
3947 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
3948 if (!rdev || test_bit(Faulty, &rdev->flags))
3949 degraded++;
3950 else if (!test_bit(In_sync, &rdev->flags))
3951 /* When we can reduce the number of devices in
3952 * an array, this might not contribute to
3953 * 'degraded'. It does now.
3954 */
3955 degraded++;
3956 }
3957 rcu_read_unlock();
3958 if (conf->geo.raid_disks == conf->prev.raid_disks)
3959 return degraded;
3960 rcu_read_lock();
3961 degraded2 = 0;
3962 for (i = 0; i < conf->geo.raid_disks; i++) {
3963 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
3964 if (!rdev || test_bit(Faulty, &rdev->flags))
3965 degraded2++;
3966 else if (!test_bit(In_sync, &rdev->flags)) {
3967 /* If reshape is increasing the number of devices,
3968 * this section has already been recovered, so
3969 * it doesn't contribute to degraded.
3970 * else it does.
3971 */
3972 if (conf->geo.raid_disks <= conf->prev.raid_disks)
3973 degraded2++;
3974 }
3975 }
3976 rcu_read_unlock();
3977 if (degraded2 > degraded)
3978 return degraded2;
3979 return degraded;
3980}
3981
3982static int raid10_start_reshape(struct mddev *mddev)
3983{
3984 /* A 'reshape' has been requested. This commits
3985 * the various 'new' fields and sets MD_RECOVER_RESHAPE
3986 * This also checks if there are enough spares and adds them
3987 * to the array.
3988 * We currently require enough spares to make the final
3989 * array non-degraded. We also require that the difference
3990 * between old and new data_offset - on each device - is
3991 * enough that we never risk over-writing.
3992 */
3993
3994 unsigned long before_length, after_length;
3995 sector_t min_offset_diff = 0;
3996 int first = 1;
3997 struct geom new;
3998 struct r10conf *conf = mddev->private;
3999 struct md_rdev *rdev;
4000 int spares = 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004001 int ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004002
4003 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4004 return -EBUSY;
4005
4006 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4007 return -EINVAL;
4008
4009 before_length = ((1 << conf->prev.chunk_shift) *
4010 conf->prev.far_copies);
4011 after_length = ((1 << conf->geo.chunk_shift) *
4012 conf->geo.far_copies);
4013
4014 rdev_for_each(rdev, mddev) {
4015 if (!test_bit(In_sync, &rdev->flags)
4016 && !test_bit(Faulty, &rdev->flags))
4017 spares++;
4018 if (rdev->raid_disk >= 0) {
4019 long long diff = (rdev->new_data_offset
4020 - rdev->data_offset);
4021 if (!mddev->reshape_backwards)
4022 diff = -diff;
4023 if (diff < 0)
4024 diff = 0;
4025 if (first || diff < min_offset_diff)
4026 min_offset_diff = diff;
4027 }
4028 }
4029
4030 if (max(before_length, after_length) > min_offset_diff)
4031 return -EINVAL;
4032
4033 if (spares < mddev->delta_disks)
4034 return -EINVAL;
4035
4036 conf->offset_diff = min_offset_diff;
4037 spin_lock_irq(&conf->device_lock);
4038 if (conf->mirrors_new) {
4039 memcpy(conf->mirrors_new, conf->mirrors,
Jonathan Brassowdc280d982012-07-31 10:03:52 +10004040 sizeof(struct raid10_info)*conf->prev.raid_disks);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004041 smp_mb();
4042 kfree(conf->mirrors_old); /* FIXME and elsewhere */
4043 conf->mirrors_old = conf->mirrors;
4044 conf->mirrors = conf->mirrors_new;
4045 conf->mirrors_new = NULL;
4046 }
4047 setup_geo(&conf->geo, mddev, geo_start);
4048 smp_mb();
4049 if (mddev->reshape_backwards) {
4050 sector_t size = raid10_size(mddev, 0, 0);
4051 if (size < mddev->array_sectors) {
4052 spin_unlock_irq(&conf->device_lock);
4053 printk(KERN_ERR "md/raid10:%s: array size must be reduce before number of disks\n",
4054 mdname(mddev));
4055 return -EINVAL;
4056 }
4057 mddev->resync_max_sectors = size;
4058 conf->reshape_progress = size;
4059 } else
4060 conf->reshape_progress = 0;
4061 spin_unlock_irq(&conf->device_lock);
4062
NeilBrownbb63a702012-05-22 13:55:28 +10004063 if (mddev->delta_disks && mddev->bitmap) {
4064 ret = bitmap_resize(mddev->bitmap,
4065 raid10_size(mddev, 0,
4066 conf->geo.raid_disks),
4067 0, 0);
4068 if (ret)
4069 goto abort;
4070 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004071 if (mddev->delta_disks > 0) {
4072 rdev_for_each(rdev, mddev)
4073 if (rdev->raid_disk < 0 &&
4074 !test_bit(Faulty, &rdev->flags)) {
4075 if (raid10_add_disk(mddev, rdev) == 0) {
4076 if (rdev->raid_disk >=
4077 conf->prev.raid_disks)
4078 set_bit(In_sync, &rdev->flags);
4079 else
4080 rdev->recovery_offset = 0;
4081
4082 if (sysfs_link_rdev(mddev, rdev))
4083 /* Failure here is OK */;
4084 }
4085 } else if (rdev->raid_disk >= conf->prev.raid_disks
4086 && !test_bit(Faulty, &rdev->flags)) {
4087 /* This is a spare that was manually added */
4088 set_bit(In_sync, &rdev->flags);
4089 }
4090 }
4091 /* When a reshape changes the number of devices,
4092 * ->degraded is measured against the larger of the
4093 * pre and post numbers.
4094 */
4095 spin_lock_irq(&conf->device_lock);
4096 mddev->degraded = calc_degraded(conf);
4097 spin_unlock_irq(&conf->device_lock);
4098 mddev->raid_disks = conf->geo.raid_disks;
4099 mddev->reshape_position = conf->reshape_progress;
4100 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4101
4102 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4103 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4104 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4105 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4106
4107 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4108 "reshape");
4109 if (!mddev->sync_thread) {
NeilBrownbb63a702012-05-22 13:55:28 +10004110 ret = -EAGAIN;
4111 goto abort;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004112 }
4113 conf->reshape_checkpoint = jiffies;
4114 md_wakeup_thread(mddev->sync_thread);
4115 md_new_event(mddev);
4116 return 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004117
4118abort:
4119 mddev->recovery = 0;
4120 spin_lock_irq(&conf->device_lock);
4121 conf->geo = conf->prev;
4122 mddev->raid_disks = conf->geo.raid_disks;
4123 rdev_for_each(rdev, mddev)
4124 rdev->new_data_offset = rdev->data_offset;
4125 smp_wmb();
4126 conf->reshape_progress = MaxSector;
4127 mddev->reshape_position = MaxSector;
4128 spin_unlock_irq(&conf->device_lock);
4129 return ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004130}
4131
4132/* Calculate the last device-address that could contain
4133 * any block from the chunk that includes the array-address 's'
4134 * and report the next address.
4135 * i.e. the address returned will be chunk-aligned and after
4136 * any data that is in the chunk containing 's'.
4137 */
4138static sector_t last_dev_address(sector_t s, struct geom *geo)
4139{
4140 s = (s | geo->chunk_mask) + 1;
4141 s >>= geo->chunk_shift;
4142 s *= geo->near_copies;
4143 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4144 s *= geo->far_copies;
4145 s <<= geo->chunk_shift;
4146 return s;
4147}
4148
4149/* Calculate the first device-address that could contain
4150 * any block from the chunk that includes the array-address 's'.
4151 * This too will be the start of a chunk
4152 */
4153static sector_t first_dev_address(sector_t s, struct geom *geo)
4154{
4155 s >>= geo->chunk_shift;
4156 s *= geo->near_copies;
4157 sector_div(s, geo->raid_disks);
4158 s *= geo->far_copies;
4159 s <<= geo->chunk_shift;
4160 return s;
4161}
4162
4163static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4164 int *skipped)
4165{
4166 /* We simply copy at most one chunk (smallest of old and new)
4167 * at a time, possibly less if that exceeds RESYNC_PAGES,
4168 * or we hit a bad block or something.
4169 * This might mean we pause for normal IO in the middle of
4170 * a chunk, but that is not a problem was mddev->reshape_position
4171 * can record any location.
4172 *
4173 * If we will want to write to a location that isn't
4174 * yet recorded as 'safe' (i.e. in metadata on disk) then
4175 * we need to flush all reshape requests and update the metadata.
4176 *
4177 * When reshaping forwards (e.g. to more devices), we interpret
4178 * 'safe' as the earliest block which might not have been copied
4179 * down yet. We divide this by previous stripe size and multiply
4180 * by previous stripe length to get lowest device offset that we
4181 * cannot write to yet.
4182 * We interpret 'sector_nr' as an address that we want to write to.
4183 * From this we use last_device_address() to find where we might
4184 * write to, and first_device_address on the 'safe' position.
4185 * If this 'next' write position is after the 'safe' position,
4186 * we must update the metadata to increase the 'safe' position.
4187 *
4188 * When reshaping backwards, we round in the opposite direction
4189 * and perform the reverse test: next write position must not be
4190 * less than current safe position.
4191 *
4192 * In all this the minimum difference in data offsets
4193 * (conf->offset_diff - always positive) allows a bit of slack,
4194 * so next can be after 'safe', but not by more than offset_disk
4195 *
4196 * We need to prepare all the bios here before we start any IO
4197 * to ensure the size we choose is acceptable to all devices.
4198 * The means one for each copy for write-out and an extra one for
4199 * read-in.
4200 * We store the read-in bio in ->master_bio and the others in
4201 * ->devs[x].bio and ->devs[x].repl_bio.
4202 */
4203 struct r10conf *conf = mddev->private;
4204 struct r10bio *r10_bio;
4205 sector_t next, safe, last;
4206 int max_sectors;
4207 int nr_sectors;
4208 int s;
4209 struct md_rdev *rdev;
4210 int need_flush = 0;
4211 struct bio *blist;
4212 struct bio *bio, *read_bio;
4213 int sectors_done = 0;
4214
4215 if (sector_nr == 0) {
4216 /* If restarting in the middle, skip the initial sectors */
4217 if (mddev->reshape_backwards &&
4218 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4219 sector_nr = (raid10_size(mddev, 0, 0)
4220 - conf->reshape_progress);
4221 } else if (!mddev->reshape_backwards &&
4222 conf->reshape_progress > 0)
4223 sector_nr = conf->reshape_progress;
4224 if (sector_nr) {
4225 mddev->curr_resync_completed = sector_nr;
4226 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4227 *skipped = 1;
4228 return sector_nr;
4229 }
4230 }
4231
4232 /* We don't use sector_nr to track where we are up to
4233 * as that doesn't work well for ->reshape_backwards.
4234 * So just use ->reshape_progress.
4235 */
4236 if (mddev->reshape_backwards) {
4237 /* 'next' is the earliest device address that we might
4238 * write to for this chunk in the new layout
4239 */
4240 next = first_dev_address(conf->reshape_progress - 1,
4241 &conf->geo);
4242
4243 /* 'safe' is the last device address that we might read from
4244 * in the old layout after a restart
4245 */
4246 safe = last_dev_address(conf->reshape_safe - 1,
4247 &conf->prev);
4248
4249 if (next + conf->offset_diff < safe)
4250 need_flush = 1;
4251
4252 last = conf->reshape_progress - 1;
4253 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4254 & conf->prev.chunk_mask);
4255 if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
4256 sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
4257 } else {
4258 /* 'next' is after the last device address that we
4259 * might write to for this chunk in the new layout
4260 */
4261 next = last_dev_address(conf->reshape_progress, &conf->geo);
4262
4263 /* 'safe' is the earliest device address that we might
4264 * read from in the old layout after a restart
4265 */
4266 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4267
4268 /* Need to update metadata if 'next' might be beyond 'safe'
4269 * as that would possibly corrupt data
4270 */
4271 if (next > safe + conf->offset_diff)
4272 need_flush = 1;
4273
4274 sector_nr = conf->reshape_progress;
4275 last = sector_nr | (conf->geo.chunk_mask
4276 & conf->prev.chunk_mask);
4277
4278 if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
4279 last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
4280 }
4281
4282 if (need_flush ||
4283 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4284 /* Need to update reshape_position in metadata */
4285 wait_barrier(conf);
4286 mddev->reshape_position = conf->reshape_progress;
4287 if (mddev->reshape_backwards)
4288 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4289 - conf->reshape_progress;
4290 else
4291 mddev->curr_resync_completed = conf->reshape_progress;
4292 conf->reshape_checkpoint = jiffies;
4293 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4294 md_wakeup_thread(mddev->thread);
4295 wait_event(mddev->sb_wait, mddev->flags == 0 ||
4296 kthread_should_stop());
4297 conf->reshape_safe = mddev->reshape_position;
4298 allow_barrier(conf);
4299 }
4300
4301read_more:
4302 /* Now schedule reads for blocks from sector_nr to last */
4303 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
4304 raise_barrier(conf, sectors_done != 0);
4305 atomic_set(&r10_bio->remaining, 0);
4306 r10_bio->mddev = mddev;
4307 r10_bio->sector = sector_nr;
4308 set_bit(R10BIO_IsReshape, &r10_bio->state);
4309 r10_bio->sectors = last - sector_nr + 1;
4310 rdev = read_balance(conf, r10_bio, &max_sectors);
4311 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4312
4313 if (!rdev) {
4314 /* Cannot read from here, so need to record bad blocks
4315 * on all the target devices.
4316 */
4317 // FIXME
4318 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4319 return sectors_done;
4320 }
4321
4322 read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
4323
4324 read_bio->bi_bdev = rdev->bdev;
4325 read_bio->bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
4326 + rdev->data_offset);
4327 read_bio->bi_private = r10_bio;
4328 read_bio->bi_end_io = end_sync_read;
4329 read_bio->bi_rw = READ;
4330 read_bio->bi_flags &= ~(BIO_POOL_MASK - 1);
4331 read_bio->bi_flags |= 1 << BIO_UPTODATE;
4332 read_bio->bi_vcnt = 0;
4333 read_bio->bi_idx = 0;
4334 read_bio->bi_size = 0;
4335 r10_bio->master_bio = read_bio;
4336 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4337
4338 /* Now find the locations in the new layout */
4339 __raid10_find_phys(&conf->geo, r10_bio);
4340
4341 blist = read_bio;
4342 read_bio->bi_next = NULL;
4343
4344 for (s = 0; s < conf->copies*2; s++) {
4345 struct bio *b;
4346 int d = r10_bio->devs[s/2].devnum;
4347 struct md_rdev *rdev2;
4348 if (s&1) {
4349 rdev2 = conf->mirrors[d].replacement;
4350 b = r10_bio->devs[s/2].repl_bio;
4351 } else {
4352 rdev2 = conf->mirrors[d].rdev;
4353 b = r10_bio->devs[s/2].bio;
4354 }
4355 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4356 continue;
4357 b->bi_bdev = rdev2->bdev;
4358 b->bi_sector = r10_bio->devs[s/2].addr + rdev2->new_data_offset;
4359 b->bi_private = r10_bio;
4360 b->bi_end_io = end_reshape_write;
4361 b->bi_rw = WRITE;
4362 b->bi_flags &= ~(BIO_POOL_MASK - 1);
4363 b->bi_flags |= 1 << BIO_UPTODATE;
4364 b->bi_next = blist;
4365 b->bi_vcnt = 0;
4366 b->bi_idx = 0;
4367 b->bi_size = 0;
4368 blist = b;
4369 }
4370
4371 /* Now add as many pages as possible to all of these bios. */
4372
4373 nr_sectors = 0;
4374 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
4375 struct page *page = r10_bio->devs[0].bio->bi_io_vec[s/(PAGE_SIZE>>9)].bv_page;
4376 int len = (max_sectors - s) << 9;
4377 if (len > PAGE_SIZE)
4378 len = PAGE_SIZE;
4379 for (bio = blist; bio ; bio = bio->bi_next) {
4380 struct bio *bio2;
4381 if (bio_add_page(bio, page, len, 0))
4382 continue;
4383
4384 /* Didn't fit, must stop */
4385 for (bio2 = blist;
4386 bio2 && bio2 != bio;
4387 bio2 = bio2->bi_next) {
4388 /* Remove last page from this bio */
4389 bio2->bi_vcnt--;
4390 bio2->bi_size -= len;
4391 bio2->bi_flags &= ~(1<<BIO_SEG_VALID);
4392 }
4393 goto bio_full;
4394 }
4395 sector_nr += len >> 9;
4396 nr_sectors += len >> 9;
4397 }
4398bio_full:
4399 r10_bio->sectors = nr_sectors;
4400
4401 /* Now submit the read */
4402 md_sync_acct(read_bio->bi_bdev, r10_bio->sectors);
4403 atomic_inc(&r10_bio->remaining);
4404 read_bio->bi_next = NULL;
4405 generic_make_request(read_bio);
4406 sector_nr += nr_sectors;
4407 sectors_done += nr_sectors;
4408 if (sector_nr <= last)
4409 goto read_more;
4410
4411 /* Now that we have done the whole section we can
4412 * update reshape_progress
4413 */
4414 if (mddev->reshape_backwards)
4415 conf->reshape_progress -= sectors_done;
4416 else
4417 conf->reshape_progress += sectors_done;
4418
4419 return sectors_done;
4420}
4421
4422static void end_reshape_request(struct r10bio *r10_bio);
4423static int handle_reshape_read_error(struct mddev *mddev,
4424 struct r10bio *r10_bio);
4425static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
4426{
4427 /* Reshape read completed. Hopefully we have a block
4428 * to write out.
4429 * If we got a read error then we do sync 1-page reads from
4430 * elsewhere until we find the data - or give up.
4431 */
4432 struct r10conf *conf = mddev->private;
4433 int s;
4434
4435 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
4436 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
4437 /* Reshape has been aborted */
4438 md_done_sync(mddev, r10_bio->sectors, 0);
4439 return;
4440 }
4441
4442 /* We definitely have the data in the pages, schedule the
4443 * writes.
4444 */
4445 atomic_set(&r10_bio->remaining, 1);
4446 for (s = 0; s < conf->copies*2; s++) {
4447 struct bio *b;
4448 int d = r10_bio->devs[s/2].devnum;
4449 struct md_rdev *rdev;
4450 if (s&1) {
4451 rdev = conf->mirrors[d].replacement;
4452 b = r10_bio->devs[s/2].repl_bio;
4453 } else {
4454 rdev = conf->mirrors[d].rdev;
4455 b = r10_bio->devs[s/2].bio;
4456 }
4457 if (!rdev || test_bit(Faulty, &rdev->flags))
4458 continue;
4459 atomic_inc(&rdev->nr_pending);
4460 md_sync_acct(b->bi_bdev, r10_bio->sectors);
4461 atomic_inc(&r10_bio->remaining);
4462 b->bi_next = NULL;
4463 generic_make_request(b);
4464 }
4465 end_reshape_request(r10_bio);
4466}
4467
4468static void end_reshape(struct r10conf *conf)
4469{
4470 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
4471 return;
4472
4473 spin_lock_irq(&conf->device_lock);
4474 conf->prev = conf->geo;
4475 md_finish_reshape(conf->mddev);
4476 smp_wmb();
4477 conf->reshape_progress = MaxSector;
4478 spin_unlock_irq(&conf->device_lock);
4479
4480 /* read-ahead size must cover two whole stripes, which is
4481 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4482 */
4483 if (conf->mddev->queue) {
4484 int stripe = conf->geo.raid_disks *
4485 ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
4486 stripe /= conf->geo.near_copies;
4487 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4488 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4489 }
4490 conf->fullsync = 0;
4491}
4492
4493
4494static int handle_reshape_read_error(struct mddev *mddev,
4495 struct r10bio *r10_bio)
4496{
4497 /* Use sync reads to get the blocks from somewhere else */
4498 int sectors = r10_bio->sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004499 struct r10conf *conf = mddev->private;
NeilBrowne0ee7782012-08-18 09:51:42 +10004500 struct {
4501 struct r10bio r10_bio;
4502 struct r10dev devs[conf->copies];
4503 } on_stack;
4504 struct r10bio *r10b = &on_stack.r10_bio;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004505 int slot = 0;
4506 int idx = 0;
4507 struct bio_vec *bvec = r10_bio->master_bio->bi_io_vec;
4508
NeilBrowne0ee7782012-08-18 09:51:42 +10004509 r10b->sector = r10_bio->sector;
4510 __raid10_find_phys(&conf->prev, r10b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004511
4512 while (sectors) {
4513 int s = sectors;
4514 int success = 0;
4515 int first_slot = slot;
4516
4517 if (s > (PAGE_SIZE >> 9))
4518 s = PAGE_SIZE >> 9;
4519
4520 while (!success) {
NeilBrowne0ee7782012-08-18 09:51:42 +10004521 int d = r10b->devs[slot].devnum;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004522 struct md_rdev *rdev = conf->mirrors[d].rdev;
4523 sector_t addr;
4524 if (rdev == NULL ||
4525 test_bit(Faulty, &rdev->flags) ||
4526 !test_bit(In_sync, &rdev->flags))
4527 goto failed;
4528
NeilBrowne0ee7782012-08-18 09:51:42 +10004529 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004530 success = sync_page_io(rdev,
4531 addr,
4532 s << 9,
4533 bvec[idx].bv_page,
4534 READ, false);
4535 if (success)
4536 break;
4537 failed:
4538 slot++;
4539 if (slot >= conf->copies)
4540 slot = 0;
4541 if (slot == first_slot)
4542 break;
4543 }
4544 if (!success) {
4545 /* couldn't read this block, must give up */
4546 set_bit(MD_RECOVERY_INTR,
4547 &mddev->recovery);
4548 return -EIO;
4549 }
4550 sectors -= s;
4551 idx++;
4552 }
4553 return 0;
4554}
4555
4556static void end_reshape_write(struct bio *bio, int error)
4557{
4558 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
4559 struct r10bio *r10_bio = bio->bi_private;
4560 struct mddev *mddev = r10_bio->mddev;
4561 struct r10conf *conf = mddev->private;
4562 int d;
4563 int slot;
4564 int repl;
4565 struct md_rdev *rdev = NULL;
4566
4567 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
4568 if (repl)
4569 rdev = conf->mirrors[d].replacement;
4570 if (!rdev) {
4571 smp_mb();
4572 rdev = conf->mirrors[d].rdev;
4573 }
4574
4575 if (!uptodate) {
4576 /* FIXME should record badblock */
4577 md_error(mddev, rdev);
4578 }
4579
4580 rdev_dec_pending(rdev, mddev);
4581 end_reshape_request(r10_bio);
4582}
4583
4584static void end_reshape_request(struct r10bio *r10_bio)
4585{
4586 if (!atomic_dec_and_test(&r10_bio->remaining))
4587 return;
4588 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
4589 bio_put(r10_bio->master_bio);
4590 put_buf(r10_bio);
4591}
4592
4593static void raid10_finish_reshape(struct mddev *mddev)
4594{
4595 struct r10conf *conf = mddev->private;
4596
4597 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4598 return;
4599
4600 if (mddev->delta_disks > 0) {
4601 sector_t size = raid10_size(mddev, 0, 0);
4602 md_set_array_sectors(mddev, size);
4603 if (mddev->recovery_cp > mddev->resync_max_sectors) {
4604 mddev->recovery_cp = mddev->resync_max_sectors;
4605 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4606 }
4607 mddev->resync_max_sectors = size;
4608 set_capacity(mddev->gendisk, mddev->array_sectors);
4609 revalidate_disk(mddev->gendisk);
NeilBrown63aced62012-05-22 13:55:33 +10004610 } else {
4611 int d;
4612 for (d = conf->geo.raid_disks ;
4613 d < conf->geo.raid_disks - mddev->delta_disks;
4614 d++) {
4615 struct md_rdev *rdev = conf->mirrors[d].rdev;
4616 if (rdev)
4617 clear_bit(In_sync, &rdev->flags);
4618 rdev = conf->mirrors[d].replacement;
4619 if (rdev)
4620 clear_bit(In_sync, &rdev->flags);
4621 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004622 }
4623 mddev->layout = mddev->new_layout;
4624 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
4625 mddev->reshape_position = MaxSector;
4626 mddev->delta_disks = 0;
4627 mddev->reshape_backwards = 0;
4628}
4629
NeilBrown84fc4b52011-10-11 16:49:58 +11004630static struct md_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631{
4632 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08004633 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 .owner = THIS_MODULE,
4635 .make_request = make_request,
4636 .run = run,
4637 .stop = stop,
4638 .status = status,
4639 .error_handler = error,
4640 .hot_add_disk = raid10_add_disk,
4641 .hot_remove_disk= raid10_remove_disk,
4642 .spare_active = raid10_spare_active,
4643 .sync_request = sync_request,
NeilBrown6cce3b232006-01-06 00:20:16 -08004644 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07004645 .size = raid10_size,
NeilBrown006a09a2012-03-19 12:46:40 +11004646 .resize = raid10_resize,
Trela, Maciejdab8b292010-03-08 16:02:45 +11004647 .takeover = raid10_takeover,
NeilBrown3ea7daa2012-05-22 13:53:47 +10004648 .check_reshape = raid10_check_reshape,
4649 .start_reshape = raid10_start_reshape,
4650 .finish_reshape = raid10_finish_reshape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651};
4652
4653static int __init raid_init(void)
4654{
NeilBrown2604b702006-01-06 00:20:36 -08004655 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656}
4657
4658static void raid_exit(void)
4659{
NeilBrown2604b702006-01-06 00:20:36 -08004660 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661}
4662
4663module_init(raid_init);
4664module_exit(raid_exit);
4665MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11004666MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08004668MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08004669MODULE_ALIAS("md-level-10");
NeilBrown34db0cd2011-10-11 16:50:01 +11004670
4671module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);