blob: 99ae6068e456992ef2b16c98e01d5c2ee0362128 [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
NeilBrown34db0cd2011-10-11 16:50:01 +110063/* When there are this many requests queue to be written by
64 * the raid10 thread, we become 'congested' to provide back-pressure
65 * for writeback.
66 */
67static int max_queued_requests = 1024;
68
NeilBrowne879a872011-10-11 16:49:02 +110069static void allow_barrier(struct r10conf *conf);
70static void lower_barrier(struct r10conf *conf);
NeilBrownfae8cc52012-02-14 11:10:10 +110071static int enough(struct r10conf *conf, int ignore);
NeilBrown3ea7daa2012-05-22 13:53:47 +100072static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
73 int *skipped);
74static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio);
75static void end_reshape_write(struct bio *bio, int error);
76static void end_reshape(struct r10conf *conf);
NeilBrown0a27ec92006-01-06 00:20:13 -080077
Al Virodd0fc662005-10-07 07:46:04 +010078static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
NeilBrowne879a872011-10-11 16:49:02 +110080 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +110081 int size = offsetof(struct r10bio, devs[conf->copies]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
NeilBrown69335ef2011-12-23 10:17:54 +110083 /* allocate a r10bio with room for raid_disks entries in the
84 * bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +010085 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
88static void r10bio_pool_free(void *r10_bio, void *data)
89{
90 kfree(r10_bio);
91}
92
NeilBrown0310fa22008-08-05 15:54:14 +100093/* Maximum size of each resync request */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#define RESYNC_BLOCK_SIZE (64*1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
NeilBrown0310fa22008-08-05 15:54:14 +100096/* amount of memory to reserve for resync requests */
97#define RESYNC_WINDOW (1024*1024)
98/* maximum number of concurrent requests, memory permitting */
99#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101/*
102 * When performing a resync, we need to read and compare, so
103 * we need as many pages are there are copies.
104 * When performing a recovery, we need 2 bios, one for read,
105 * one for write (we recover only one drive per r10buf)
106 *
107 */
Al Virodd0fc662005-10-07 07:46:04 +0100108static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
NeilBrowne879a872011-10-11 16:49:02 +1100110 struct r10conf *conf = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 struct page *page;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100112 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 struct bio *bio;
114 int i, j;
115 int nalloc;
116
117 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100118 if (!r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
NeilBrown3ea7daa2012-05-22 13:53:47 +1000121 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
122 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 nalloc = conf->copies; /* resync */
124 else
125 nalloc = 2; /* recovery */
126
127 /*
128 * Allocate bios.
129 */
130 for (j = nalloc ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +1100131 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (!bio)
133 goto out_free_bio;
134 r10_bio->devs[j].bio = bio;
NeilBrown69335ef2011-12-23 10:17:54 +1100135 if (!conf->have_replacement)
136 continue;
137 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
138 if (!bio)
139 goto out_free_bio;
140 r10_bio->devs[j].repl_bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142 /*
143 * Allocate RESYNC_PAGES data pages and attach them
144 * where needed.
145 */
146 for (j = 0 ; j < nalloc; j++) {
NeilBrown69335ef2011-12-23 10:17:54 +1100147 struct bio *rbio = r10_bio->devs[j].repl_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 bio = r10_bio->devs[j].bio;
149 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown3ea7daa2012-05-22 13:53:47 +1000150 if (j > 0 && !test_bit(MD_RECOVERY_SYNC,
151 &conf->mddev->recovery)) {
152 /* we can share bv_page's during recovery
153 * and reshape */
Namhyung Kimc65060a2011-07-18 17:38:49 +1000154 struct bio *rbio = r10_bio->devs[0].bio;
155 page = rbio->bi_io_vec[i].bv_page;
156 get_page(page);
157 } else
158 page = alloc_page(gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (unlikely(!page))
160 goto out_free_pages;
161
162 bio->bi_io_vec[i].bv_page = page;
NeilBrown69335ef2011-12-23 10:17:54 +1100163 if (rbio)
164 rbio->bi_io_vec[i].bv_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166 }
167
168 return r10_bio;
169
170out_free_pages:
171 for ( ; i > 0 ; i--)
NeilBrown1345b1d2006-01-06 00:20:40 -0800172 safe_put_page(bio->bi_io_vec[i-1].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 while (j--)
174 for (i = 0; i < RESYNC_PAGES ; i++)
NeilBrown1345b1d2006-01-06 00:20:40 -0800175 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
majianpeng5fdd2cf2012-05-22 13:55:03 +1000176 j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177out_free_bio:
majianpeng5fdd2cf2012-05-22 13:55:03 +1000178 for ( ; j < nalloc; j++) {
179 if (r10_bio->devs[j].bio)
180 bio_put(r10_bio->devs[j].bio);
NeilBrown69335ef2011-12-23 10:17:54 +1100181 if (r10_bio->devs[j].repl_bio)
182 bio_put(r10_bio->devs[j].repl_bio);
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 r10bio_pool_free(r10_bio, conf);
185 return NULL;
186}
187
188static void r10buf_pool_free(void *__r10_bio, void *data)
189{
190 int i;
NeilBrowne879a872011-10-11 16:49:02 +1100191 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100192 struct r10bio *r10bio = __r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 int j;
194
195 for (j=0; j < conf->copies; j++) {
196 struct bio *bio = r10bio->devs[j].bio;
197 if (bio) {
198 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown1345b1d2006-01-06 00:20:40 -0800199 safe_put_page(bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 bio->bi_io_vec[i].bv_page = NULL;
201 }
202 bio_put(bio);
203 }
NeilBrown69335ef2011-12-23 10:17:54 +1100204 bio = r10bio->devs[j].repl_bio;
205 if (bio)
206 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208 r10bio_pool_free(r10bio, conf);
209}
210
NeilBrowne879a872011-10-11 16:49:02 +1100211static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 int i;
214
215 for (i = 0; i < conf->copies; i++) {
216 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown749c55e2011-07-28 11:39:24 +1000217 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 bio_put(*bio);
219 *bio = NULL;
NeilBrown69335ef2011-12-23 10:17:54 +1100220 bio = &r10_bio->devs[i].repl_bio;
221 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
222 bio_put(*bio);
223 *bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225}
226
NeilBrown9f2c9d12011-10-11 16:48:43 +1100227static void free_r10bio(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
NeilBrowne879a872011-10-11 16:49:02 +1100229 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 put_all_bios(conf, r10_bio);
232 mempool_free(r10_bio, conf->r10bio_pool);
233}
234
NeilBrown9f2c9d12011-10-11 16:48:43 +1100235static void put_buf(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
NeilBrowne879a872011-10-11 16:49:02 +1100237 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 mempool_free(r10_bio, conf->r10buf_pool);
240
NeilBrown0a27ec92006-01-06 00:20:13 -0800241 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
NeilBrown9f2c9d12011-10-11 16:48:43 +1100244static void reschedule_retry(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +1100247 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +1100248 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 spin_lock_irqsave(&conf->device_lock, flags);
251 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800252 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 spin_unlock_irqrestore(&conf->device_lock, flags);
254
Arthur Jones388667b2008-07-25 12:03:38 -0700255 /* wake up frozen array... */
256 wake_up(&conf->wait_barrier);
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 md_wakeup_thread(mddev->thread);
259}
260
261/*
262 * raid_end_bio_io() is called when we have finished servicing a mirrored
263 * operation and are ready to return a success/failure code to the buffer
264 * cache layer.
265 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100266static void raid_end_bio_io(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 struct bio *bio = r10_bio->master_bio;
NeilBrown856e08e2011-07-28 11:39:23 +1000269 int done;
NeilBrowne879a872011-10-11 16:49:02 +1100270 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
NeilBrown856e08e2011-07-28 11:39:23 +1000272 if (bio->bi_phys_segments) {
273 unsigned long flags;
274 spin_lock_irqsave(&conf->device_lock, flags);
275 bio->bi_phys_segments--;
276 done = (bio->bi_phys_segments == 0);
277 spin_unlock_irqrestore(&conf->device_lock, flags);
278 } else
279 done = 1;
280 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
281 clear_bit(BIO_UPTODATE, &bio->bi_flags);
282 if (done) {
283 bio_endio(bio, 0);
284 /*
285 * Wake up any possible resync thread that waits for the device
286 * to go idle.
287 */
288 allow_barrier(conf);
289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 free_r10bio(r10_bio);
291}
292
293/*
294 * Update disk head position estimator based on IRQ completion info.
295 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100296static inline void update_head_pos(int slot, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
NeilBrowne879a872011-10-11 16:49:02 +1100298 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
301 r10_bio->devs[slot].addr + (r10_bio->sectors);
302}
303
Namhyung Kim778ca012011-07-18 17:38:47 +1000304/*
305 * Find the disk number which triggered given bio
306 */
NeilBrowne879a872011-10-11 16:49:02 +1100307static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
NeilBrown69335ef2011-12-23 10:17:54 +1100308 struct bio *bio, int *slotp, int *replp)
Namhyung Kim778ca012011-07-18 17:38:47 +1000309{
310 int slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100311 int repl = 0;
Namhyung Kim778ca012011-07-18 17:38:47 +1000312
NeilBrown69335ef2011-12-23 10:17:54 +1100313 for (slot = 0; slot < conf->copies; slot++) {
Namhyung Kim778ca012011-07-18 17:38:47 +1000314 if (r10_bio->devs[slot].bio == bio)
315 break;
NeilBrown69335ef2011-12-23 10:17:54 +1100316 if (r10_bio->devs[slot].repl_bio == bio) {
317 repl = 1;
318 break;
319 }
320 }
Namhyung Kim778ca012011-07-18 17:38:47 +1000321
322 BUG_ON(slot == conf->copies);
323 update_head_pos(slot, r10_bio);
324
NeilBrown749c55e2011-07-28 11:39:24 +1000325 if (slotp)
326 *slotp = slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100327 if (replp)
328 *replp = repl;
Namhyung Kim778ca012011-07-18 17:38:47 +1000329 return r10_bio->devs[slot].devnum;
330}
331
NeilBrown6712ecf2007-09-27 12:47:43 +0200332static void raid10_end_read_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +1100335 struct r10bio *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 int slot, dev;
NeilBrownabbf0982011-12-23 10:17:54 +1100337 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +1100338 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 slot = r10_bio->read_slot;
342 dev = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100343 rdev = r10_bio->devs[slot].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /*
345 * this branch is our 'one mirror IO has finished' event handler:
346 */
NeilBrown4443ae12006-01-06 00:20:28 -0800347 update_head_pos(slot, r10_bio);
348
349 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 /*
351 * Set R10BIO_Uptodate in our master bio, so that
352 * we will return a good error code to the higher
353 * levels even if IO on some other mirrored buffer fails.
354 *
355 * The 'master' represents the composite IO operation to
356 * user-side. So if something waits for IO, then it will
357 * wait for the 'master' bio.
358 */
359 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrownfae8cc52012-02-14 11:10:10 +1100360 } else {
361 /* If all other devices that store this block have
362 * failed, we want to return the error upwards rather
363 * than fail the last device. Here we redefine
364 * "uptodate" to mean "Don't want to retry"
365 */
366 unsigned long flags;
367 spin_lock_irqsave(&conf->device_lock, flags);
368 if (!enough(conf, rdev->raid_disk))
369 uptodate = 1;
370 spin_unlock_irqrestore(&conf->device_lock, flags);
371 }
372 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 raid_end_bio_io(r10_bio);
NeilBrownabbf0982011-12-23 10:17:54 +1100374 rdev_dec_pending(rdev, conf->mddev);
NeilBrown4443ae12006-01-06 00:20:28 -0800375 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 /*
NeilBrown7c4e06f2011-05-11 14:53:17 +1000377 * oops, read error - keep the refcount on the rdev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 */
379 char b[BDEVNAME_SIZE];
Christian Dietrich8bda4702011-07-27 11:00:36 +1000380 printk_ratelimited(KERN_ERR
381 "md/raid10:%s: %s: rescheduling sector %llu\n",
382 mdname(conf->mddev),
NeilBrownabbf0982011-12-23 10:17:54 +1100383 bdevname(rdev->bdev, b),
Christian Dietrich8bda4702011-07-27 11:00:36 +1000384 (unsigned long long)r10_bio->sector);
NeilBrown856e08e2011-07-28 11:39:23 +1000385 set_bit(R10BIO_ReadError, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 reschedule_retry(r10_bio);
387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
NeilBrown9f2c9d12011-10-11 16:48:43 +1100390static void close_write(struct r10bio *r10_bio)
NeilBrownbd870a12011-07-28 11:39:24 +1000391{
392 /* clear the bitmap if all writes complete successfully */
393 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
394 r10_bio->sectors,
395 !test_bit(R10BIO_Degraded, &r10_bio->state),
396 0);
397 md_write_end(r10_bio->mddev);
398}
399
NeilBrown9f2c9d12011-10-11 16:48:43 +1100400static void one_write_done(struct r10bio *r10_bio)
NeilBrown19d5f832011-09-10 17:21:17 +1000401{
402 if (atomic_dec_and_test(&r10_bio->remaining)) {
403 if (test_bit(R10BIO_WriteError, &r10_bio->state))
404 reschedule_retry(r10_bio);
405 else {
406 close_write(r10_bio);
407 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
408 reschedule_retry(r10_bio);
409 else
410 raid_end_bio_io(r10_bio);
411 }
412 }
413}
414
NeilBrown6712ecf2007-09-27 12:47:43 +0200415static void raid10_end_write_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +1100418 struct r10bio *r10_bio = bio->bi_private;
Namhyung Kim778ca012011-07-18 17:38:47 +1000419 int dev;
NeilBrown749c55e2011-07-28 11:39:24 +1000420 int dec_rdev = 1;
NeilBrowne879a872011-10-11 16:49:02 +1100421 struct r10conf *conf = r10_bio->mddev->private;
NeilBrown475b0322011-12-23 10:17:55 +1100422 int slot, repl;
NeilBrown4ca40c22011-12-23 10:17:55 +1100423 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
NeilBrown475b0322011-12-23 10:17:55 +1100425 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
NeilBrown475b0322011-12-23 10:17:55 +1100427 if (repl)
428 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +1100429 if (!rdev) {
430 smp_rmb();
431 repl = 0;
NeilBrown475b0322011-12-23 10:17:55 +1100432 rdev = conf->mirrors[dev].rdev;
NeilBrown4ca40c22011-12-23 10:17:55 +1100433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /*
435 * this branch is our 'one mirror IO has finished' event handler:
436 */
NeilBrown6cce3b232006-01-06 00:20:16 -0800437 if (!uptodate) {
NeilBrown475b0322011-12-23 10:17:55 +1100438 if (repl)
439 /* Never record new bad blocks to replacement,
440 * just fail it.
441 */
442 md_error(rdev->mddev, rdev);
443 else {
444 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +1100445 if (!test_and_set_bit(WantReplacement, &rdev->flags))
446 set_bit(MD_RECOVERY_NEEDED,
447 &rdev->mddev->recovery);
NeilBrown475b0322011-12-23 10:17:55 +1100448 set_bit(R10BIO_WriteError, &r10_bio->state);
449 dec_rdev = 0;
450 }
NeilBrown749c55e2011-07-28 11:39:24 +1000451 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /*
453 * Set R10BIO_Uptodate in our master bio, so that
454 * we will return a good error code for to the higher
455 * levels even if IO on some other mirrored buffer fails.
456 *
457 * The 'master' represents the composite IO operation to
458 * user-side. So if something waits for IO, then it will
459 * wait for the 'master' bio.
460 */
NeilBrown749c55e2011-07-28 11:39:24 +1000461 sector_t first_bad;
462 int bad_sectors;
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 set_bit(R10BIO_Uptodate, &r10_bio->state);
465
NeilBrown749c55e2011-07-28 11:39:24 +1000466 /* Maybe we can clear some bad blocks. */
NeilBrown475b0322011-12-23 10:17:55 +1100467 if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +1000468 r10_bio->devs[slot].addr,
469 r10_bio->sectors,
470 &first_bad, &bad_sectors)) {
471 bio_put(bio);
NeilBrown475b0322011-12-23 10:17:55 +1100472 if (repl)
473 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
474 else
475 r10_bio->devs[slot].bio = IO_MADE_GOOD;
NeilBrown749c55e2011-07-28 11:39:24 +1000476 dec_rdev = 0;
477 set_bit(R10BIO_MadeGood, &r10_bio->state);
478 }
479 }
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 /*
482 *
483 * Let's see if all mirrored write operations have finished
484 * already.
485 */
NeilBrown19d5f832011-09-10 17:21:17 +1000486 one_write_done(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +1000487 if (dec_rdev)
488 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491/*
492 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300493 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 * parameters: near_copies and far_copies.
495 * near_copies * far_copies must be <= raid_disks.
496 * Normally one of these will be 1.
497 * If both are 1, we get raid0.
498 * If near_copies == raid_disks, we get raid1.
499 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300500 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * first chunk, followed by near_copies copies of the next chunk and
502 * so on.
503 * If far_copies > 1, then after 1/far_copies of the array has been assigned
504 * as described above, we start again with a device offset of near_copies.
505 * So we effectively have another copy of the whole array further down all
506 * the drives, but with blocks on different drives.
507 * With this layout, and block is never stored twice on the one device.
508 *
509 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700510 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 *
512 * raid10_find_virt does the reverse mapping, from a device and a
513 * sector offset to a virtual address
514 */
515
NeilBrownf8c9e742012-05-21 09:28:33 +1000516static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 int n,f;
519 sector_t sector;
520 sector_t chunk;
521 sector_t stripe;
522 int dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 int slot = 0;
524
525 /* now calculate first sector/dev */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000526 chunk = r10bio->sector >> geo->chunk_shift;
527 sector = r10bio->sector & geo->chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
NeilBrown5cf00fc2012-05-21 09:28:20 +1000529 chunk *= geo->near_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 stripe = chunk;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000531 dev = sector_div(stripe, geo->raid_disks);
532 if (geo->far_offset)
533 stripe *= geo->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
NeilBrown5cf00fc2012-05-21 09:28:20 +1000535 sector += stripe << geo->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 /* and calculate all the others */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000538 for (n = 0; n < geo->near_copies; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 int d = dev;
540 sector_t s = sector;
541 r10bio->devs[slot].addr = sector;
542 r10bio->devs[slot].devnum = d;
543 slot++;
544
NeilBrown5cf00fc2012-05-21 09:28:20 +1000545 for (f = 1; f < geo->far_copies; f++) {
546 d += geo->near_copies;
547 if (d >= geo->raid_disks)
548 d -= geo->raid_disks;
549 s += geo->stride;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 r10bio->devs[slot].devnum = d;
551 r10bio->devs[slot].addr = s;
552 slot++;
553 }
554 dev++;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000555 if (dev >= geo->raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 dev = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000557 sector += (geo->chunk_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559 }
NeilBrownf8c9e742012-05-21 09:28:33 +1000560}
561
562static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
563{
564 struct geom *geo = &conf->geo;
565
566 if (conf->reshape_progress != MaxSector &&
567 ((r10bio->sector >= conf->reshape_progress) !=
568 conf->mddev->reshape_backwards)) {
569 set_bit(R10BIO_Previous, &r10bio->state);
570 geo = &conf->prev;
571 } else
572 clear_bit(R10BIO_Previous, &r10bio->state);
573
574 __raid10_find_phys(geo, r10bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
NeilBrowne879a872011-10-11 16:49:02 +1100577static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 sector_t offset, chunk, vchunk;
NeilBrownf8c9e742012-05-21 09:28:33 +1000580 /* Never use conf->prev as this is only called during resync
581 * or recovery, so reshape isn't happening
582 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000583 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
NeilBrown5cf00fc2012-05-21 09:28:20 +1000585 offset = sector & geo->chunk_mask;
586 if (geo->far_offset) {
NeilBrownc93983b2006-06-26 00:27:41 -0700587 int fc;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000588 chunk = sector >> geo->chunk_shift;
589 fc = sector_div(chunk, geo->far_copies);
590 dev -= fc * geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700591 if (dev < 0)
NeilBrown5cf00fc2012-05-21 09:28:20 +1000592 dev += geo->raid_disks;
NeilBrownc93983b2006-06-26 00:27:41 -0700593 } else {
NeilBrown5cf00fc2012-05-21 09:28:20 +1000594 while (sector >= geo->stride) {
595 sector -= geo->stride;
596 if (dev < geo->near_copies)
597 dev += geo->raid_disks - geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700598 else
NeilBrown5cf00fc2012-05-21 09:28:20 +1000599 dev -= geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700600 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000601 chunk = sector >> geo->chunk_shift;
NeilBrownc93983b2006-06-26 00:27:41 -0700602 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000603 vchunk = chunk * geo->raid_disks + dev;
604 sector_div(vchunk, geo->near_copies);
605 return (vchunk << geo->chunk_shift) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
608/**
609 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
610 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200611 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 * @biovec: the request that could be merged to it.
613 *
614 * Return amount of bytes we can accept at this offset
NeilBrown050b6612012-03-19 12:46:39 +1100615 * This requires checking for end-of-chunk if near_copies != raid_disks,
616 * and for subordinate merge_bvec_fns if merge_check_needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200618static int raid10_mergeable_bvec(struct request_queue *q,
619 struct bvec_merge_data *bvm,
620 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
NeilBrownfd01b882011-10-11 16:47:53 +1100622 struct mddev *mddev = q->queuedata;
NeilBrown050b6612012-03-19 12:46:39 +1100623 struct r10conf *conf = mddev->private;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200624 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 int max;
NeilBrown3ea7daa2012-05-22 13:53:47 +1000626 unsigned int chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200627 unsigned int bio_sectors = bvm->bi_size >> 9;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000628 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
NeilBrown3ea7daa2012-05-22 13:53:47 +1000630 chunk_sectors = (conf->geo.chunk_mask & conf->prev.chunk_mask) + 1;
NeilBrownf8c9e742012-05-21 09:28:33 +1000631 if (conf->reshape_progress != MaxSector &&
632 ((sector >= conf->reshape_progress) !=
633 conf->mddev->reshape_backwards))
634 geo = &conf->prev;
635
NeilBrown5cf00fc2012-05-21 09:28:20 +1000636 if (geo->near_copies < geo->raid_disks) {
NeilBrown050b6612012-03-19 12:46:39 +1100637 max = (chunk_sectors - ((sector & (chunk_sectors - 1))
638 + bio_sectors)) << 9;
639 if (max < 0)
640 /* bio_add cannot handle a negative return */
641 max = 0;
642 if (max <= biovec->bv_len && bio_sectors == 0)
643 return biovec->bv_len;
644 } else
645 max = biovec->bv_len;
646
647 if (mddev->merge_check_needed) {
648 struct r10bio r10_bio;
649 int s;
NeilBrownf8c9e742012-05-21 09:28:33 +1000650 if (conf->reshape_progress != MaxSector) {
651 /* Cannot give any guidance during reshape */
652 if (max <= biovec->bv_len && bio_sectors == 0)
653 return biovec->bv_len;
654 return 0;
655 }
NeilBrown050b6612012-03-19 12:46:39 +1100656 r10_bio.sector = sector;
657 raid10_find_phys(conf, &r10_bio);
658 rcu_read_lock();
659 for (s = 0; s < conf->copies; s++) {
660 int disk = r10_bio.devs[s].devnum;
661 struct md_rdev *rdev = rcu_dereference(
662 conf->mirrors[disk].rdev);
663 if (rdev && !test_bit(Faulty, &rdev->flags)) {
664 struct request_queue *q =
665 bdev_get_queue(rdev->bdev);
666 if (q->merge_bvec_fn) {
667 bvm->bi_sector = r10_bio.devs[s].addr
668 + rdev->data_offset;
669 bvm->bi_bdev = rdev->bdev;
670 max = min(max, q->merge_bvec_fn(
671 q, bvm, biovec));
672 }
673 }
674 rdev = rcu_dereference(conf->mirrors[disk].replacement);
675 if (rdev && !test_bit(Faulty, &rdev->flags)) {
676 struct request_queue *q =
677 bdev_get_queue(rdev->bdev);
678 if (q->merge_bvec_fn) {
679 bvm->bi_sector = r10_bio.devs[s].addr
680 + rdev->data_offset;
681 bvm->bi_bdev = rdev->bdev;
682 max = min(max, q->merge_bvec_fn(
683 q, bvm, biovec));
684 }
685 }
686 }
687 rcu_read_unlock();
688 }
689 return max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
692/*
693 * This routine returns the disk from which the requested read should
694 * be done. There is a per-array 'next expected sequential IO' sector
695 * number - if this matches on the next IO then we use the last disk.
696 * There is also a per-disk 'last know head position' sector that is
697 * maintained from IRQ contexts, both the normal and the resync IO
698 * completion handlers update this position correctly. If there is no
699 * perfect sequential match then we pick the disk whose head is closest.
700 *
701 * If there are 2 mirrors in the same 2 devices, performance degrades
702 * because position is mirror, not device based.
703 *
704 * The rdev for the device selected will have nr_pending incremented.
705 */
706
707/*
708 * FIXME: possibly should rethink readbalancing and do it differently
709 * depending on near_copies / far_copies geometry.
710 */
NeilBrown96c3fd12011-12-23 10:17:54 +1100711static struct md_rdev *read_balance(struct r10conf *conf,
712 struct r10bio *r10_bio,
713 int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000715 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000716 int disk, slot;
NeilBrown856e08e2011-07-28 11:39:23 +1000717 int sectors = r10_bio->sectors;
718 int best_good_sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000719 sector_t new_distance, best_dist;
NeilBrownabbf0982011-12-23 10:17:54 +1100720 struct md_rdev *rdev, *best_rdev;
NeilBrown56d99122011-05-11 14:27:03 +1000721 int do_balance;
722 int best_slot;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000723 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 raid10_find_phys(conf, r10_bio);
726 rcu_read_lock();
NeilBrown56d99122011-05-11 14:27:03 +1000727retry:
NeilBrown856e08e2011-07-28 11:39:23 +1000728 sectors = r10_bio->sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000729 best_slot = -1;
NeilBrownabbf0982011-12-23 10:17:54 +1100730 best_rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000731 best_dist = MaxSector;
NeilBrown856e08e2011-07-28 11:39:23 +1000732 best_good_sectors = 0;
NeilBrown56d99122011-05-11 14:27:03 +1000733 do_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /*
735 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b232006-01-06 00:20:16 -0800736 * device if no resync is going on (recovery is ok), or below
737 * the resync window. We take the first readable disk when
738 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 */
740 if (conf->mddev->recovery_cp < MaxSector
NeilBrown56d99122011-05-11 14:27:03 +1000741 && (this_sector + sectors >= conf->next_resync))
742 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
NeilBrown56d99122011-05-11 14:27:03 +1000744 for (slot = 0; slot < conf->copies ; slot++) {
NeilBrown856e08e2011-07-28 11:39:23 +1000745 sector_t first_bad;
746 int bad_sectors;
747 sector_t dev_sector;
748
NeilBrown56d99122011-05-11 14:27:03 +1000749 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000751 disk = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100752 rdev = rcu_dereference(conf->mirrors[disk].replacement);
753 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
NeilBrown050b6612012-03-19 12:46:39 +1100754 test_bit(Unmerged, &rdev->flags) ||
NeilBrownabbf0982011-12-23 10:17:54 +1100755 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
756 rdev = rcu_dereference(conf->mirrors[disk].rdev);
NeilBrown050b6612012-03-19 12:46:39 +1100757 if (rdev == NULL ||
758 test_bit(Faulty, &rdev->flags) ||
759 test_bit(Unmerged, &rdev->flags))
NeilBrownabbf0982011-12-23 10:17:54 +1100760 continue;
761 if (!test_bit(In_sync, &rdev->flags) &&
762 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
NeilBrown56d99122011-05-11 14:27:03 +1000763 continue;
764
NeilBrown856e08e2011-07-28 11:39:23 +1000765 dev_sector = r10_bio->devs[slot].addr;
766 if (is_badblock(rdev, dev_sector, sectors,
767 &first_bad, &bad_sectors)) {
768 if (best_dist < MaxSector)
769 /* Already have a better slot */
770 continue;
771 if (first_bad <= dev_sector) {
772 /* Cannot read here. If this is the
773 * 'primary' device, then we must not read
774 * beyond 'bad_sectors' from another device.
775 */
776 bad_sectors -= (dev_sector - first_bad);
777 if (!do_balance && sectors > bad_sectors)
778 sectors = bad_sectors;
779 if (best_good_sectors > sectors)
780 best_good_sectors = sectors;
781 } else {
782 sector_t good_sectors =
783 first_bad - dev_sector;
784 if (good_sectors > best_good_sectors) {
785 best_good_sectors = good_sectors;
786 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100787 best_rdev = rdev;
NeilBrown856e08e2011-07-28 11:39:23 +1000788 }
789 if (!do_balance)
790 /* Must read from here */
791 break;
792 }
793 continue;
794 } else
795 best_good_sectors = sectors;
796
NeilBrown56d99122011-05-11 14:27:03 +1000797 if (!do_balance)
798 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
NeilBrown22dfdf52005-11-28 13:44:09 -0800800 /* This optimisation is debatable, and completely destroys
801 * sequential read speed for 'far copies' arrays. So only
802 * keep it for 'near' arrays, and review those later.
803 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000804 if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 break;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800806
807 /* for far > 1 always use the lowest address */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000808 if (geo->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000809 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800810 else
NeilBrown56d99122011-05-11 14:27:03 +1000811 new_distance = abs(r10_bio->devs[slot].addr -
812 conf->mirrors[disk].head_position);
813 if (new_distance < best_dist) {
814 best_dist = new_distance;
815 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100816 best_rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818 }
NeilBrownabbf0982011-12-23 10:17:54 +1100819 if (slot >= conf->copies) {
NeilBrown56d99122011-05-11 14:27:03 +1000820 slot = best_slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100821 rdev = best_rdev;
822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
NeilBrown56d99122011-05-11 14:27:03 +1000824 if (slot >= 0) {
NeilBrown56d99122011-05-11 14:27:03 +1000825 atomic_inc(&rdev->nr_pending);
826 if (test_bit(Faulty, &rdev->flags)) {
827 /* Cannot risk returning a device that failed
828 * before we inc'ed nr_pending
829 */
830 rdev_dec_pending(rdev, conf->mddev);
831 goto retry;
832 }
833 r10_bio->read_slot = slot;
834 } else
NeilBrown96c3fd12011-12-23 10:17:54 +1100835 rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 rcu_read_unlock();
NeilBrown856e08e2011-07-28 11:39:23 +1000837 *max_sectors = best_good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
NeilBrown96c3fd12011-12-23 10:17:54 +1100839 return rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
NeilBrown0d129222006-10-03 01:15:54 -0700842static int raid10_congested(void *data, int bits)
843{
NeilBrownfd01b882011-10-11 16:47:53 +1100844 struct mddev *mddev = data;
NeilBrowne879a872011-10-11 16:49:02 +1100845 struct r10conf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700846 int i, ret = 0;
847
NeilBrown34db0cd2011-10-11 16:50:01 +1100848 if ((bits & (1 << BDI_async_congested)) &&
849 conf->pending_count >= max_queued_requests)
850 return 1;
851
NeilBrown3fa841d2009-09-23 18:10:29 +1000852 if (mddev_congested(mddev, bits))
853 return 1;
NeilBrown0d129222006-10-03 01:15:54 -0700854 rcu_read_lock();
NeilBrownf8c9e742012-05-21 09:28:33 +1000855 for (i = 0;
856 (i < conf->geo.raid_disks || i < conf->prev.raid_disks)
857 && ret == 0;
858 i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100859 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700860 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200861 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700862
863 ret |= bdi_congested(&q->backing_dev_info, bits);
864 }
865 }
866 rcu_read_unlock();
867 return ret;
868}
869
NeilBrowne879a872011-10-11 16:49:02 +1100870static void flush_pending_writes(struct r10conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800871{
872 /* Any writes that have been queued but are awaiting
873 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800874 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800875 spin_lock_irq(&conf->device_lock);
876
877 if (conf->pending_bio_list.head) {
878 struct bio *bio;
879 bio = bio_list_get(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +1100880 conf->pending_count = 0;
NeilBrowna35e63e2008-03-04 14:29:29 -0800881 spin_unlock_irq(&conf->device_lock);
882 /* flush any pending bitmap writes to disk
883 * before proceeding w/ I/O */
884 bitmap_unplug(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100885 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800886
887 while (bio) { /* submit pending writes */
888 struct bio *next = bio->bi_next;
889 bio->bi_next = NULL;
890 generic_make_request(bio);
891 bio = next;
892 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800893 } else
894 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800895}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100896
NeilBrown0a27ec92006-01-06 00:20:13 -0800897/* Barriers....
898 * Sometimes we need to suspend IO while we do something else,
899 * either some resync/recovery, or reconfigure the array.
900 * To do this we raise a 'barrier'.
901 * The 'barrier' is a counter that can be raised multiple times
902 * to count how many activities are happening which preclude
903 * normal IO.
904 * We can only raise the barrier if there is no pending IO.
905 * i.e. if nr_pending == 0.
906 * We choose only to raise the barrier if no-one is waiting for the
907 * barrier to go down. This means that as soon as an IO request
908 * is ready, no other operations which require a barrier will start
909 * until the IO request has had a chance.
910 *
911 * So: regular IO calls 'wait_barrier'. When that returns there
912 * is no backgroup IO happening, It must arrange to call
913 * allow_barrier when it has finished its IO.
914 * backgroup IO calls must call raise_barrier. Once that returns
915 * there is no normal IO happeing. It must arrange to call
916 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
NeilBrowne879a872011-10-11 16:49:02 +1100919static void raise_barrier(struct r10conf *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
NeilBrown6cce3b232006-01-06 00:20:16 -0800921 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
NeilBrown6cce3b232006-01-06 00:20:16 -0800924 /* Wait until no block IO is waiting (unless 'force') */
925 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrownc3b328a2011-04-18 18:25:43 +1000926 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800927
928 /* block any new IO from starting */
929 conf->barrier++;
930
NeilBrownc3b328a2011-04-18 18:25:43 +1000931 /* Now wait for all pending IO to complete */
NeilBrown0a27ec92006-01-06 00:20:13 -0800932 wait_event_lock_irq(conf->wait_barrier,
933 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
NeilBrownc3b328a2011-04-18 18:25:43 +1000934 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 spin_unlock_irq(&conf->resync_lock);
937}
938
NeilBrowne879a872011-10-11 16:49:02 +1100939static void lower_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800940{
941 unsigned long flags;
942 spin_lock_irqsave(&conf->resync_lock, flags);
943 conf->barrier--;
944 spin_unlock_irqrestore(&conf->resync_lock, flags);
945 wake_up(&conf->wait_barrier);
946}
947
NeilBrowne879a872011-10-11 16:49:02 +1100948static void wait_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800949{
950 spin_lock_irq(&conf->resync_lock);
951 if (conf->barrier) {
952 conf->nr_waiting++;
NeilBrownd6b42dc2012-03-19 12:46:38 +1100953 /* Wait for the barrier to drop.
954 * However if there are already pending
955 * requests (preventing the barrier from
956 * rising completely), and the
957 * pre-process bio queue isn't empty,
958 * then don't wait, as we need to empty
959 * that queue to get the nr_pending
960 * count down.
961 */
962 wait_event_lock_irq(conf->wait_barrier,
963 !conf->barrier ||
964 (conf->nr_pending &&
965 current->bio_list &&
966 !bio_list_empty(current->bio_list)),
NeilBrown0a27ec92006-01-06 00:20:13 -0800967 conf->resync_lock,
NeilBrownd6b42dc2012-03-19 12:46:38 +1100968 );
NeilBrown0a27ec92006-01-06 00:20:13 -0800969 conf->nr_waiting--;
970 }
971 conf->nr_pending++;
972 spin_unlock_irq(&conf->resync_lock);
973}
974
NeilBrowne879a872011-10-11 16:49:02 +1100975static void allow_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800976{
977 unsigned long flags;
978 spin_lock_irqsave(&conf->resync_lock, flags);
979 conf->nr_pending--;
980 spin_unlock_irqrestore(&conf->resync_lock, flags);
981 wake_up(&conf->wait_barrier);
982}
983
NeilBrowne879a872011-10-11 16:49:02 +1100984static void freeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -0800985{
986 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800987 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800988 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800989 * wait until nr_pending match nr_queued+1
990 * This is called in the context of one normal IO request
991 * that has failed. Thus any sync request that might be pending
992 * will be blocked by nr_pending, and we need to wait for
993 * pending IO requests to complete or be queued for re-try.
994 * Thus the number queued (nr_queued) plus this request (1)
995 * must match the number of pending IOs (nr_pending) before
996 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -0800997 */
998 spin_lock_irq(&conf->resync_lock);
999 conf->barrier++;
1000 conf->nr_waiting++;
1001 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -08001002 conf->nr_pending == conf->nr_queued+1,
NeilBrown4443ae12006-01-06 00:20:28 -08001003 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +10001004 flush_pending_writes(conf));
1005
NeilBrown4443ae12006-01-06 00:20:28 -08001006 spin_unlock_irq(&conf->resync_lock);
1007}
1008
NeilBrowne879a872011-10-11 16:49:02 +11001009static void unfreeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -08001010{
1011 /* reverse the effect of the freeze */
1012 spin_lock_irq(&conf->resync_lock);
1013 conf->barrier--;
1014 conf->nr_waiting--;
1015 wake_up(&conf->wait_barrier);
1016 spin_unlock_irq(&conf->resync_lock);
1017}
1018
NeilBrownf8c9e742012-05-21 09:28:33 +10001019static sector_t choose_data_offset(struct r10bio *r10_bio,
1020 struct md_rdev *rdev)
1021{
1022 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1023 test_bit(R10BIO_Previous, &r10_bio->state))
1024 return rdev->data_offset;
1025 else
1026 return rdev->new_data_offset;
1027}
1028
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07001029static void make_request(struct mddev *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
NeilBrowne879a872011-10-11 16:49:02 +11001031 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11001032 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 struct bio *read_bio;
1034 int i;
NeilBrownf8c9e742012-05-21 09:28:33 +10001035 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
NeilBrown5cf00fc2012-05-21 09:28:20 +10001036 int chunk_sects = chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +01001037 const int rw = bio_data_dir(bio);
NeilBrown2c7d46e2010-08-18 16:16:05 +10001038 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
Tejun Heoe9c74692010-09-03 11:56:18 +02001039 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
NeilBrown6cce3b232006-01-06 00:20:16 -08001040 unsigned long flags;
NeilBrown3cb03002011-10-11 16:45:26 +11001041 struct md_rdev *blocked_rdev;
NeilBrownc3b328a2011-04-18 18:25:43 +10001042 int plugged;
NeilBrownd4432c22011-07-28 11:39:24 +10001043 int sectors_handled;
1044 int max_sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10001045 int sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Tejun Heoe9c74692010-09-03 11:56:18 +02001047 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
1048 md_flush_request(mddev, bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001049 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07001050 }
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 /* If this request crosses a chunk boundary, we need to
1053 * split it. This will only happen for 1 PAGE (or less) requests.
1054 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10001055 if (unlikely((bio->bi_sector & chunk_mask) + (bio->bi_size >> 9)
1056 > chunk_sects
NeilBrownf8c9e742012-05-21 09:28:33 +10001057 && (conf->geo.near_copies < conf->geo.raid_disks
1058 || conf->prev.near_copies < conf->prev.raid_disks))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 struct bio_pair *bp;
1060 /* Sanity check -- queue functions should prevent this happening */
1061 if (bio->bi_vcnt != 1 ||
1062 bio->bi_idx != 0)
1063 goto bad_map;
1064 /* This is a one page bio that upper layers
1065 * refuse to split for us, so we need to split it.
1066 */
Denis ChengRq6feef532008-10-09 08:57:05 +02001067 bp = bio_split(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
NeilBrown51e9ac72010-08-07 21:17:00 +10001069
1070 /* Each of these 'make_request' calls will call 'wait_barrier'.
1071 * If the first succeeds but the second blocks due to the resync
1072 * thread raising the barrier, we will deadlock because the
1073 * IO to the underlying device will be queued in generic_make_request
1074 * and will never complete, so will never reduce nr_pending.
1075 * So increment nr_waiting here so no new raise_barriers will
1076 * succeed, and so the second wait_barrier cannot block.
1077 */
1078 spin_lock_irq(&conf->resync_lock);
1079 conf->nr_waiting++;
1080 spin_unlock_irq(&conf->resync_lock);
1081
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001082 make_request(mddev, &bp->bio1);
1083 make_request(mddev, &bp->bio2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
NeilBrown51e9ac72010-08-07 21:17:00 +10001085 spin_lock_irq(&conf->resync_lock);
1086 conf->nr_waiting--;
1087 wake_up(&conf->wait_barrier);
1088 spin_unlock_irq(&conf->resync_lock);
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 bio_pair_release(bp);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001091 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 bad_map:
NeilBrown128595e2010-05-03 14:47:14 +10001093 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
1094 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
1096
NeilBrown6712ecf2007-09-27 12:47:43 +02001097 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001098 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100
NeilBrown3d310eb2005-06-21 17:17:26 -07001101 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -07001102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 /*
1104 * Register the new request and wait if the reconstruction
1105 * thread has put up a bar for new requests.
1106 * Continue immediately if no resync is active currently.
1107 */
NeilBrown0a27ec92006-01-06 00:20:13 -08001108 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
NeilBrown3ea7daa2012-05-22 13:53:47 +10001110 sectors = bio->bi_size >> 9;
1111 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1112 bio->bi_sector < conf->reshape_progress &&
1113 bio->bi_sector + sectors > conf->reshape_progress) {
1114 /* IO spans the reshape position. Need to wait for
1115 * reshape to pass
1116 */
1117 allow_barrier(conf);
1118 wait_event(conf->wait_barrier,
1119 conf->reshape_progress <= bio->bi_sector ||
1120 conf->reshape_progress >= bio->bi_sector + sectors);
1121 wait_barrier(conf);
1122 }
1123 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1124 bio_data_dir(bio) == WRITE &&
1125 (mddev->reshape_backwards
1126 ? (bio->bi_sector < conf->reshape_safe &&
1127 bio->bi_sector + sectors > conf->reshape_progress)
1128 : (bio->bi_sector + sectors > conf->reshape_safe &&
1129 bio->bi_sector < conf->reshape_progress))) {
1130 /* Need to update reshape_position in metadata */
1131 mddev->reshape_position = conf->reshape_progress;
1132 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1133 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1134 md_wakeup_thread(mddev->thread);
1135 wait_event(mddev->sb_wait,
1136 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
1137
1138 conf->reshape_safe = mddev->reshape_position;
1139 }
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1142
1143 r10_bio->master_bio = bio;
NeilBrown3ea7daa2012-05-22 13:53:47 +10001144 r10_bio->sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 r10_bio->mddev = mddev;
1147 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b232006-01-06 00:20:16 -08001148 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
NeilBrown856e08e2011-07-28 11:39:23 +10001150 /* We might need to issue multiple reads to different
1151 * devices if there are bad blocks around, so we keep
1152 * track of the number of reads in bio->bi_phys_segments.
1153 * If this is 0, there is only one r10_bio and no locking
1154 * will be needed when the request completes. If it is
1155 * non-zero, then it is the number of not-completed requests.
1156 */
1157 bio->bi_phys_segments = 0;
1158 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
1159
Jens Axboea3623572005-11-01 09:26:16 +01001160 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 /*
1162 * read balancing logic:
1163 */
NeilBrown96c3fd12011-12-23 10:17:54 +11001164 struct md_rdev *rdev;
NeilBrown856e08e2011-07-28 11:39:23 +10001165 int slot;
1166
1167read_again:
NeilBrown96c3fd12011-12-23 10:17:54 +11001168 rdev = read_balance(conf, r10_bio, &max_sectors);
1169 if (!rdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 raid_end_bio_io(r10_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001171 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
NeilBrown96c3fd12011-12-23 10:17:54 +11001173 slot = r10_bio->read_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
NeilBrowna167f662010-10-26 18:31:13 +11001175 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrown856e08e2011-07-28 11:39:23 +10001176 md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
1177 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 r10_bio->devs[slot].bio = read_bio;
NeilBrownabbf0982011-12-23 10:17:54 +11001180 r10_bio->devs[slot].rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 read_bio->bi_sector = r10_bio->devs[slot].addr +
NeilBrownf8c9e742012-05-21 09:28:33 +10001183 choose_data_offset(r10_bio, rdev);
NeilBrown96c3fd12011-12-23 10:17:54 +11001184 read_bio->bi_bdev = rdev->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 read_bio->bi_end_io = raid10_end_read_request;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001186 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 read_bio->bi_private = r10_bio;
1188
NeilBrown856e08e2011-07-28 11:39:23 +10001189 if (max_sectors < r10_bio->sectors) {
1190 /* Could not read all from this device, so we will
1191 * need another r10_bio.
1192 */
NeilBrown856e08e2011-07-28 11:39:23 +10001193 sectors_handled = (r10_bio->sectors + max_sectors
1194 - bio->bi_sector);
1195 r10_bio->sectors = max_sectors;
1196 spin_lock_irq(&conf->device_lock);
1197 if (bio->bi_phys_segments == 0)
1198 bio->bi_phys_segments = 2;
1199 else
1200 bio->bi_phys_segments++;
1201 spin_unlock(&conf->device_lock);
1202 /* Cannot call generic_make_request directly
1203 * as that will be queued in __generic_make_request
1204 * and subsequent mempool_alloc might block
1205 * waiting for it. so hand bio over to raid10d.
1206 */
1207 reschedule_retry(r10_bio);
1208
1209 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1210
1211 r10_bio->master_bio = bio;
1212 r10_bio->sectors = ((bio->bi_size >> 9)
1213 - sectors_handled);
1214 r10_bio->state = 0;
1215 r10_bio->mddev = mddev;
1216 r10_bio->sector = bio->bi_sector + sectors_handled;
1217 goto read_again;
1218 } else
1219 generic_make_request(read_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001220 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222
1223 /*
1224 * WRITE:
1225 */
NeilBrown34db0cd2011-10-11 16:50:01 +11001226 if (conf->pending_count >= max_queued_requests) {
1227 md_wakeup_thread(mddev->thread);
1228 wait_event(conf->wait_barrier,
1229 conf->pending_count < max_queued_requests);
1230 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001231 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 * inc refcount on their rdev. Record them by setting
1233 * bios[x] to bio
NeilBrownd4432c22011-07-28 11:39:24 +10001234 * If there are known/acknowledged bad blocks on any device
1235 * on which we have seen a write error, we want to avoid
1236 * writing to those blocks. This potentially requires several
1237 * writes to write around the bad blocks. Each set of writes
1238 * gets its own r10_bio with a set of bios attached. The number
1239 * of r10_bios is recored in bio->bi_phys_segments just as with
1240 * the read case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001242 plugged = mddev_check_plugged(mddev);
1243
NeilBrown69335ef2011-12-23 10:17:54 +11001244 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 raid10_find_phys(conf, r10_bio);
NeilBrownd4432c22011-07-28 11:39:24 +10001246retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -07001247 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 rcu_read_lock();
NeilBrownd4432c22011-07-28 11:39:24 +10001249 max_sectors = r10_bio->sectors;
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 for (i = 0; i < conf->copies; i++) {
1252 int d = r10_bio->devs[i].devnum;
NeilBrown3cb03002011-10-11 16:45:26 +11001253 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown475b0322011-12-23 10:17:55 +11001254 struct md_rdev *rrdev = rcu_dereference(
1255 conf->mirrors[d].replacement);
NeilBrown4ca40c22011-12-23 10:17:55 +11001256 if (rdev == rrdev)
1257 rrdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001258 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1259 atomic_inc(&rdev->nr_pending);
1260 blocked_rdev = rdev;
1261 break;
1262 }
NeilBrown475b0322011-12-23 10:17:55 +11001263 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1264 atomic_inc(&rrdev->nr_pending);
1265 blocked_rdev = rrdev;
1266 break;
1267 }
NeilBrown050b6612012-03-19 12:46:39 +11001268 if (rrdev && (test_bit(Faulty, &rrdev->flags)
1269 || test_bit(Unmerged, &rrdev->flags)))
NeilBrown475b0322011-12-23 10:17:55 +11001270 rrdev = NULL;
1271
NeilBrownd4432c22011-07-28 11:39:24 +10001272 r10_bio->devs[i].bio = NULL;
NeilBrown475b0322011-12-23 10:17:55 +11001273 r10_bio->devs[i].repl_bio = NULL;
NeilBrown050b6612012-03-19 12:46:39 +11001274 if (!rdev || test_bit(Faulty, &rdev->flags) ||
1275 test_bit(Unmerged, &rdev->flags)) {
NeilBrown6cce3b232006-01-06 00:20:16 -08001276 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrownd4432c22011-07-28 11:39:24 +10001277 continue;
NeilBrown6cce3b232006-01-06 00:20:16 -08001278 }
NeilBrownd4432c22011-07-28 11:39:24 +10001279 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1280 sector_t first_bad;
1281 sector_t dev_sector = r10_bio->devs[i].addr;
1282 int bad_sectors;
1283 int is_bad;
1284
1285 is_bad = is_badblock(rdev, dev_sector,
1286 max_sectors,
1287 &first_bad, &bad_sectors);
1288 if (is_bad < 0) {
1289 /* Mustn't write here until the bad block
1290 * is acknowledged
1291 */
1292 atomic_inc(&rdev->nr_pending);
1293 set_bit(BlockedBadBlocks, &rdev->flags);
1294 blocked_rdev = rdev;
1295 break;
1296 }
1297 if (is_bad && first_bad <= dev_sector) {
1298 /* Cannot write here at all */
1299 bad_sectors -= (dev_sector - first_bad);
1300 if (bad_sectors < max_sectors)
1301 /* Mustn't write more than bad_sectors
1302 * to other devices yet
1303 */
1304 max_sectors = bad_sectors;
1305 /* We don't set R10BIO_Degraded as that
1306 * only applies if the disk is missing,
1307 * so it might be re-added, and we want to
1308 * know to recover this chunk.
1309 * In this case the device is here, and the
1310 * fact that this chunk is not in-sync is
1311 * recorded in the bad block log.
1312 */
1313 continue;
1314 }
1315 if (is_bad) {
1316 int good_sectors = first_bad - dev_sector;
1317 if (good_sectors < max_sectors)
1318 max_sectors = good_sectors;
1319 }
1320 }
1321 r10_bio->devs[i].bio = bio;
1322 atomic_inc(&rdev->nr_pending);
NeilBrown475b0322011-12-23 10:17:55 +11001323 if (rrdev) {
1324 r10_bio->devs[i].repl_bio = bio;
1325 atomic_inc(&rrdev->nr_pending);
1326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328 rcu_read_unlock();
1329
Dan Williams6bfe0b42008-04-30 00:52:32 -07001330 if (unlikely(blocked_rdev)) {
1331 /* Have to wait for this device to get unblocked, then retry */
1332 int j;
1333 int d;
1334
NeilBrown475b0322011-12-23 10:17:55 +11001335 for (j = 0; j < i; j++) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07001336 if (r10_bio->devs[j].bio) {
1337 d = r10_bio->devs[j].devnum;
1338 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1339 }
NeilBrown475b0322011-12-23 10:17:55 +11001340 if (r10_bio->devs[j].repl_bio) {
NeilBrown4ca40c22011-12-23 10:17:55 +11001341 struct md_rdev *rdev;
NeilBrown475b0322011-12-23 10:17:55 +11001342 d = r10_bio->devs[j].devnum;
NeilBrown4ca40c22011-12-23 10:17:55 +11001343 rdev = conf->mirrors[d].replacement;
1344 if (!rdev) {
1345 /* Race with remove_disk */
1346 smp_mb();
1347 rdev = conf->mirrors[d].rdev;
1348 }
1349 rdev_dec_pending(rdev, mddev);
NeilBrown475b0322011-12-23 10:17:55 +11001350 }
1351 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001352 allow_barrier(conf);
1353 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1354 wait_barrier(conf);
1355 goto retry_write;
1356 }
1357
NeilBrownd4432c22011-07-28 11:39:24 +10001358 if (max_sectors < r10_bio->sectors) {
1359 /* We are splitting this into multiple parts, so
1360 * we need to prepare for allocating another r10_bio.
1361 */
1362 r10_bio->sectors = max_sectors;
1363 spin_lock_irq(&conf->device_lock);
1364 if (bio->bi_phys_segments == 0)
1365 bio->bi_phys_segments = 2;
1366 else
1367 bio->bi_phys_segments++;
1368 spin_unlock_irq(&conf->device_lock);
1369 }
1370 sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
1371
NeilBrown4e780642010-10-19 12:54:01 +11001372 atomic_set(&r10_bio->remaining, 1);
NeilBrownd4432c22011-07-28 11:39:24 +10001373 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07001374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 for (i = 0; i < conf->copies; i++) {
1376 struct bio *mbio;
1377 int d = r10_bio->devs[i].devnum;
1378 if (!r10_bio->devs[i].bio)
1379 continue;
1380
NeilBrowna167f662010-10-26 18:31:13 +11001381 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrownd4432c22011-07-28 11:39:24 +10001382 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1383 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 r10_bio->devs[i].bio = mbio;
1385
NeilBrownd4432c22011-07-28 11:39:24 +10001386 mbio->bi_sector = (r10_bio->devs[i].addr+
NeilBrownf8c9e742012-05-21 09:28:33 +10001387 choose_data_offset(r10_bio,
1388 conf->mirrors[d].rdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1390 mbio->bi_end_io = raid10_end_write_request;
Tejun Heoe9c74692010-09-03 11:56:18 +02001391 mbio->bi_rw = WRITE | do_sync | do_fua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 mbio->bi_private = r10_bio;
1393
1394 atomic_inc(&r10_bio->remaining);
NeilBrown4e780642010-10-19 12:54:01 +11001395 spin_lock_irqsave(&conf->device_lock, flags);
1396 bio_list_add(&conf->pending_bio_list, mbio);
NeilBrown34db0cd2011-10-11 16:50:01 +11001397 conf->pending_count++;
NeilBrown4e780642010-10-19 12:54:01 +11001398 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown475b0322011-12-23 10:17:55 +11001399
1400 if (!r10_bio->devs[i].repl_bio)
1401 continue;
1402
1403 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1404 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1405 max_sectors);
1406 r10_bio->devs[i].repl_bio = mbio;
1407
NeilBrown4ca40c22011-12-23 10:17:55 +11001408 /* We are actively writing to the original device
1409 * so it cannot disappear, so the replacement cannot
1410 * become NULL here
1411 */
NeilBrownf8c9e742012-05-21 09:28:33 +10001412 mbio->bi_sector = (r10_bio->devs[i].addr +
1413 choose_data_offset(
1414 r10_bio,
1415 conf->mirrors[d].replacement));
NeilBrown475b0322011-12-23 10:17:55 +11001416 mbio->bi_bdev = conf->mirrors[d].replacement->bdev;
1417 mbio->bi_end_io = raid10_end_write_request;
1418 mbio->bi_rw = WRITE | do_sync | do_fua;
1419 mbio->bi_private = r10_bio;
1420
1421 atomic_inc(&r10_bio->remaining);
1422 spin_lock_irqsave(&conf->device_lock, flags);
1423 bio_list_add(&conf->pending_bio_list, mbio);
1424 conf->pending_count++;
1425 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
1427
NeilBrown079fa162011-09-10 17:21:23 +10001428 /* Don't remove the bias on 'remaining' (one_write_done) until
1429 * after checking if we need to go around again.
1430 */
NeilBrowna35e63e2008-03-04 14:29:29 -08001431
NeilBrownd4432c22011-07-28 11:39:24 +10001432 if (sectors_handled < (bio->bi_size >> 9)) {
NeilBrown079fa162011-09-10 17:21:23 +10001433 one_write_done(r10_bio);
NeilBrown5e570282011-07-28 11:39:25 +10001434 /* We need another r10_bio. It has already been counted
NeilBrownd4432c22011-07-28 11:39:24 +10001435 * in bio->bi_phys_segments.
1436 */
1437 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1438
1439 r10_bio->master_bio = bio;
1440 r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1441
1442 r10_bio->mddev = mddev;
1443 r10_bio->sector = bio->bi_sector + sectors_handled;
1444 r10_bio->state = 0;
1445 goto retry_write;
1446 }
NeilBrown079fa162011-09-10 17:21:23 +10001447 one_write_done(r10_bio);
1448
1449 /* In case raid10d snuck in to freeze_array */
1450 wake_up(&conf->wait_barrier);
NeilBrownd4432c22011-07-28 11:39:24 +10001451
NeilBrownc3b328a2011-04-18 18:25:43 +10001452 if (do_sync || !mddev->bitmap || !plugged)
Lars Ellenberge3881a62007-01-10 23:15:37 -08001453 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455
NeilBrownfd01b882011-10-11 16:47:53 +11001456static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
NeilBrowne879a872011-10-11 16:49:02 +11001458 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 int i;
1460
NeilBrown5cf00fc2012-05-21 09:28:20 +10001461 if (conf->geo.near_copies < conf->geo.raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +10001462 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
NeilBrown5cf00fc2012-05-21 09:28:20 +10001463 if (conf->geo.near_copies > 1)
1464 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1465 if (conf->geo.far_copies > 1) {
1466 if (conf->geo.far_offset)
1467 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001468 else
NeilBrown5cf00fc2012-05-21 09:28:20 +10001469 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001470 }
NeilBrown5cf00fc2012-05-21 09:28:20 +10001471 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1472 conf->geo.raid_disks - mddev->degraded);
1473 for (i = 0; i < conf->geo.raid_disks; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 seq_printf(seq, "%s",
1475 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001476 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 seq_printf(seq, "]");
1478}
1479
NeilBrown700c7212011-07-27 11:00:36 +10001480/* check if there are enough drives for
1481 * every block to appear on atleast one.
1482 * Don't consider the device numbered 'ignore'
1483 * as we might be about to remove it.
1484 */
NeilBrownf8c9e742012-05-21 09:28:33 +10001485static int _enough(struct r10conf *conf, struct geom *geo, int ignore)
NeilBrown700c7212011-07-27 11:00:36 +10001486{
1487 int first = 0;
1488
1489 do {
1490 int n = conf->copies;
1491 int cnt = 0;
1492 while (n--) {
1493 if (conf->mirrors[first].rdev &&
1494 first != ignore)
1495 cnt++;
NeilBrownf8c9e742012-05-21 09:28:33 +10001496 first = (first+1) % geo->raid_disks;
NeilBrown700c7212011-07-27 11:00:36 +10001497 }
1498 if (cnt == 0)
1499 return 0;
1500 } while (first != 0);
1501 return 1;
1502}
1503
NeilBrownf8c9e742012-05-21 09:28:33 +10001504static int enough(struct r10conf *conf, int ignore)
1505{
1506 return _enough(conf, &conf->geo, ignore) &&
1507 _enough(conf, &conf->prev, ignore);
1508}
1509
NeilBrownfd01b882011-10-11 16:47:53 +11001510static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
1512 char b[BDEVNAME_SIZE];
NeilBrowne879a872011-10-11 16:49:02 +11001513 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 /*
1516 * If it is not operational, then we have already marked it as dead
1517 * else if it is the last working disks, ignore the error, let the
1518 * next level up know.
1519 * else mark the drive as failed
1520 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001521 if (test_bit(In_sync, &rdev->flags)
NeilBrown700c7212011-07-27 11:00:36 +10001522 && !enough(conf, rdev->raid_disk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 /*
1524 * Don't fail the drive, just return an IO error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 */
1526 return;
NeilBrownc04be0a2006-10-03 01:15:53 -07001527 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1528 unsigned long flags;
1529 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001531 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 /*
1533 * if recovery is running, make sure it aborts.
1534 */
NeilBrowndfc70642008-05-23 13:04:39 -07001535 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
NeilBrownde393cd2011-07-28 11:31:48 +10001537 set_bit(Blocked, &rdev->flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001538 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -07001539 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Joe Perches067032b2011-01-14 09:14:33 +11001540 printk(KERN_ALERT
1541 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1542 "md/raid10:%s: Operation continuing on %d devices.\n",
NeilBrown128595e2010-05-03 14:47:14 +10001543 mdname(mddev), bdevname(rdev->bdev, b),
NeilBrown5cf00fc2012-05-21 09:28:20 +10001544 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545}
1546
NeilBrowne879a872011-10-11 16:49:02 +11001547static void print_conf(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
1549 int i;
NeilBrown0f6d02d2011-10-11 16:48:46 +11001550 struct mirror_info *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
NeilBrown128595e2010-05-03 14:47:14 +10001552 printk(KERN_DEBUG "RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (!conf) {
NeilBrown128595e2010-05-03 14:47:14 +10001554 printk(KERN_DEBUG "(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return;
1556 }
NeilBrown5cf00fc2012-05-21 09:28:20 +10001557 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
1558 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
NeilBrown5cf00fc2012-05-21 09:28:20 +10001560 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 char b[BDEVNAME_SIZE];
1562 tmp = conf->mirrors + i;
1563 if (tmp->rdev)
NeilBrown128595e2010-05-03 14:47:14 +10001564 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001565 i, !test_bit(In_sync, &tmp->rdev->flags),
1566 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 bdevname(tmp->rdev->bdev,b));
1568 }
1569}
1570
NeilBrowne879a872011-10-11 16:49:02 +11001571static void close_sync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
NeilBrown0a27ec92006-01-06 00:20:13 -08001573 wait_barrier(conf);
1574 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 mempool_destroy(conf->r10buf_pool);
1577 conf->r10buf_pool = NULL;
1578}
1579
NeilBrownfd01b882011-10-11 16:47:53 +11001580static int raid10_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
1582 int i;
NeilBrowne879a872011-10-11 16:49:02 +11001583 struct r10conf *conf = mddev->private;
NeilBrown0f6d02d2011-10-11 16:48:46 +11001584 struct mirror_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10001585 int count = 0;
1586 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 /*
1589 * Find all non-in_sync disks within the RAID10 configuration
1590 * and mark them in_sync
1591 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10001592 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 tmp = conf->mirrors + i;
NeilBrown4ca40c22011-12-23 10:17:55 +11001594 if (tmp->replacement
1595 && tmp->replacement->recovery_offset == MaxSector
1596 && !test_bit(Faulty, &tmp->replacement->flags)
1597 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1598 /* Replacement has just become active */
1599 if (!tmp->rdev
1600 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1601 count++;
1602 if (tmp->rdev) {
1603 /* Replaced device not technically faulty,
1604 * but we need to be sure it gets removed
1605 * and never re-added.
1606 */
1607 set_bit(Faulty, &tmp->rdev->flags);
1608 sysfs_notify_dirent_safe(
1609 tmp->rdev->sysfs_state);
1610 }
1611 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1612 } else if (tmp->rdev
1613 && !test_bit(Faulty, &tmp->rdev->flags)
1614 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001615 count++;
Adrian Drzewieckie6ffbcb2010-08-18 11:49:02 +10001616 sysfs_notify_dirent(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
1618 }
NeilBrown6b965622010-08-18 11:56:59 +10001619 spin_lock_irqsave(&conf->device_lock, flags);
1620 mddev->degraded -= count;
1621 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001624 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
1627
NeilBrownfd01b882011-10-11 16:47:53 +11001628static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
NeilBrowne879a872011-10-11 16:49:02 +11001630 struct r10conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001631 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 int mirror;
Neil Brown6c2fce22008-06-28 08:31:31 +10001633 int first = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10001634 int last = conf->geo.raid_disks - 1;
NeilBrown050b6612012-03-19 12:46:39 +11001635 struct request_queue *q = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 if (mddev->recovery_cp < MaxSector)
1638 /* only hot-add to in-sync arrays, as recovery is
1639 * very different from resync
1640 */
Neil Brown199050e2008-06-28 08:31:33 +10001641 return -EBUSY;
NeilBrownf8c9e742012-05-21 09:28:33 +10001642 if (rdev->saved_raid_disk < 0 && !_enough(conf, &conf->prev, -1))
Neil Brown199050e2008-06-28 08:31:33 +10001643 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
NeilBrowna53a6c82008-11-06 17:28:20 +11001645 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001646 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
NeilBrown050b6612012-03-19 12:46:39 +11001648 if (q->merge_bvec_fn) {
1649 set_bit(Unmerged, &rdev->flags);
1650 mddev->merge_check_needed = 1;
1651 }
1652
Namhyung Kim2c4193d2011-07-18 17:38:43 +10001653 if (rdev->saved_raid_disk >= first &&
NeilBrown6cce3b232006-01-06 00:20:16 -08001654 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1655 mirror = rdev->saved_raid_disk;
1656 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001657 mirror = first;
NeilBrown2bb77732011-07-27 11:00:36 +10001658 for ( ; mirror <= last ; mirror++) {
NeilBrown0f6d02d2011-10-11 16:48:46 +11001659 struct mirror_info *p = &conf->mirrors[mirror];
NeilBrown2bb77732011-07-27 11:00:36 +10001660 if (p->recovery_disabled == mddev->recovery_disabled)
1661 continue;
NeilBrownb7044d42011-12-23 10:17:56 +11001662 if (p->rdev) {
1663 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1664 p->replacement != NULL)
1665 continue;
1666 clear_bit(In_sync, &rdev->flags);
1667 set_bit(Replacement, &rdev->flags);
1668 rdev->raid_disk = mirror;
1669 err = 0;
1670 disk_stack_limits(mddev->gendisk, rdev->bdev,
1671 rdev->data_offset << 9);
NeilBrownb7044d42011-12-23 10:17:56 +11001672 conf->fullsync = 1;
1673 rcu_assign_pointer(p->replacement, rdev);
1674 break;
1675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
NeilBrown2bb77732011-07-27 11:00:36 +10001677 disk_stack_limits(mddev->gendisk, rdev->bdev,
1678 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
NeilBrown2bb77732011-07-27 11:00:36 +10001680 p->head_position = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11001681 p->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown2bb77732011-07-27 11:00:36 +10001682 rdev->raid_disk = mirror;
1683 err = 0;
1684 if (rdev->saved_raid_disk != mirror)
1685 conf->fullsync = 1;
1686 rcu_assign_pointer(p->rdev, rdev);
1687 break;
1688 }
NeilBrown050b6612012-03-19 12:46:39 +11001689 if (err == 0 && test_bit(Unmerged, &rdev->flags)) {
1690 /* Some requests might not have seen this new
1691 * merge_bvec_fn. We must wait for them to complete
1692 * before merging the device fully.
1693 * First we make sure any code which has tested
1694 * our function has submitted the request, then
1695 * we wait for all outstanding requests to complete.
1696 */
1697 synchronize_sched();
1698 raise_barrier(conf, 0);
1699 lower_barrier(conf);
1700 clear_bit(Unmerged, &rdev->flags);
1701 }
Andre Nollac5e7112009-08-03 10:59:47 +10001702 md_integrity_add_rdev(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001704 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
1706
NeilBrownb8321b62011-12-23 10:17:51 +11001707static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
NeilBrowne879a872011-10-11 16:49:02 +11001709 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11001711 int number = rdev->raid_disk;
NeilBrownc8ab9032011-12-23 10:17:54 +11001712 struct md_rdev **rdevp;
1713 struct mirror_info *p = conf->mirrors + number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 print_conf(conf);
NeilBrownc8ab9032011-12-23 10:17:54 +11001716 if (rdev == p->rdev)
1717 rdevp = &p->rdev;
1718 else if (rdev == p->replacement)
1719 rdevp = &p->replacement;
1720 else
1721 return 0;
1722
1723 if (test_bit(In_sync, &rdev->flags) ||
1724 atomic_read(&rdev->nr_pending)) {
1725 err = -EBUSY;
1726 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 }
NeilBrownc8ab9032011-12-23 10:17:54 +11001728 /* Only remove faulty devices if recovery
1729 * is not possible.
1730 */
1731 if (!test_bit(Faulty, &rdev->flags) &&
1732 mddev->recovery_disabled != p->recovery_disabled &&
NeilBrown4ca40c22011-12-23 10:17:55 +11001733 (!p->replacement || p->replacement == rdev) &&
NeilBrown63aced62012-05-22 13:55:33 +10001734 number < conf->geo.raid_disks &&
NeilBrownc8ab9032011-12-23 10:17:54 +11001735 enough(conf, -1)) {
1736 err = -EBUSY;
1737 goto abort;
1738 }
1739 *rdevp = NULL;
1740 synchronize_rcu();
1741 if (atomic_read(&rdev->nr_pending)) {
1742 /* lost the race, try later */
1743 err = -EBUSY;
1744 *rdevp = rdev;
1745 goto abort;
NeilBrown4ca40c22011-12-23 10:17:55 +11001746 } else if (p->replacement) {
1747 /* We must have just cleared 'rdev' */
1748 p->rdev = p->replacement;
1749 clear_bit(Replacement, &p->replacement->flags);
1750 smp_mb(); /* Make sure other CPUs may see both as identical
1751 * but will never see neither -- if they are careful.
1752 */
1753 p->replacement = NULL;
1754 clear_bit(WantReplacement, &rdev->flags);
1755 } else
1756 /* We might have just remove the Replacement as faulty
1757 * Clear the flag just in case
1758 */
1759 clear_bit(WantReplacement, &rdev->flags);
1760
NeilBrownc8ab9032011-12-23 10:17:54 +11001761 err = md_integrity_register(mddev);
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763abort:
1764
1765 print_conf(conf);
1766 return err;
1767}
1768
1769
NeilBrown6712ecf2007-09-27 12:47:43 +02001770static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001772 struct r10bio *r10_bio = bio->bi_private;
NeilBrowne879a872011-10-11 16:49:02 +11001773 struct r10conf *conf = r10_bio->mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001774 int d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
NeilBrown3ea7daa2012-05-22 13:53:47 +10001776 if (bio == r10_bio->master_bio) {
1777 /* this is a reshape read */
1778 d = r10_bio->read_slot; /* really the read dev */
1779 } else
1780 d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001781
1782 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1783 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrowne684e412011-07-28 11:39:25 +10001784 else
1785 /* The write handler will notice the lack of
1786 * R10BIO_Uptodate and record any errors etc
1787 */
NeilBrown4dbcdc72006-01-06 00:20:52 -08001788 atomic_add(r10_bio->sectors,
1789 &conf->mirrors[d].rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
1791 /* for reconstruct, we always reschedule after a read.
1792 * for resync, only after all reads
1793 */
NeilBrown73d5c382009-02-25 13:18:47 +11001794 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1796 atomic_dec_and_test(&r10_bio->remaining)) {
1797 /* we have read all the blocks,
1798 * do the comparison in process context in raid10d
1799 */
1800 reschedule_retry(r10_bio);
1801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
1803
NeilBrown9f2c9d12011-10-11 16:48:43 +11001804static void end_sync_request(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001805{
NeilBrownfd01b882011-10-11 16:47:53 +11001806 struct mddev *mddev = r10_bio->mddev;
NeilBrown5e570282011-07-28 11:39:25 +10001807
1808 while (atomic_dec_and_test(&r10_bio->remaining)) {
1809 if (r10_bio->master_bio == NULL) {
1810 /* the primary of several recovery bios */
1811 sector_t s = r10_bio->sectors;
1812 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1813 test_bit(R10BIO_WriteError, &r10_bio->state))
1814 reschedule_retry(r10_bio);
1815 else
1816 put_buf(r10_bio);
1817 md_done_sync(mddev, s, 1);
1818 break;
1819 } else {
NeilBrown9f2c9d12011-10-11 16:48:43 +11001820 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
NeilBrown5e570282011-07-28 11:39:25 +10001821 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1822 test_bit(R10BIO_WriteError, &r10_bio->state))
1823 reschedule_retry(r10_bio);
1824 else
1825 put_buf(r10_bio);
1826 r10_bio = r10_bio2;
1827 }
1828 }
1829}
1830
NeilBrown6712ecf2007-09-27 12:47:43 +02001831static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
1833 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +11001834 struct r10bio *r10_bio = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11001835 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001836 struct r10conf *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001837 int d;
NeilBrown749c55e2011-07-28 11:39:24 +10001838 sector_t first_bad;
1839 int bad_sectors;
1840 int slot;
NeilBrown9ad1aef2011-12-23 10:17:55 +11001841 int repl;
NeilBrown4ca40c22011-12-23 10:17:55 +11001842 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
NeilBrown9ad1aef2011-12-23 10:17:55 +11001844 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1845 if (repl)
1846 rdev = conf->mirrors[d].replacement;
NeilBrown547414d2012-03-13 11:21:20 +11001847 else
NeilBrown9ad1aef2011-12-23 10:17:55 +11001848 rdev = conf->mirrors[d].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
NeilBrown1a0b7cd2011-07-28 11:39:25 +10001850 if (!uptodate) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11001851 if (repl)
1852 md_error(mddev, rdev);
1853 else {
1854 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11001855 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1856 set_bit(MD_RECOVERY_NEEDED,
1857 &rdev->mddev->recovery);
NeilBrown9ad1aef2011-12-23 10:17:55 +11001858 set_bit(R10BIO_WriteError, &r10_bio->state);
1859 }
1860 } else if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +10001861 r10_bio->devs[slot].addr,
1862 r10_bio->sectors,
1863 &first_bad, &bad_sectors))
1864 set_bit(R10BIO_MadeGood, &r10_bio->state);
NeilBrowndfc70642008-05-23 13:04:39 -07001865
NeilBrown9ad1aef2011-12-23 10:17:55 +11001866 rdev_dec_pending(rdev, mddev);
NeilBrown5e570282011-07-28 11:39:25 +10001867
1868 end_sync_request(r10_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
1870
1871/*
1872 * Note: sync and recover and handled very differently for raid10
1873 * This code is for resync.
1874 * For resync, we read through virtual addresses and read all blocks.
1875 * If there is any error, we schedule a write. The lowest numbered
1876 * drive is authoritative.
1877 * However requests come for physical address, so we need to map.
1878 * For every physical address there are raid_disks/copies virtual addresses,
1879 * which is always are least one, but is not necessarly an integer.
1880 * This means that a physical address can span multiple chunks, so we may
1881 * have to submit multiple io requests for a single sync request.
1882 */
1883/*
1884 * We check if all blocks are in-sync and only write to blocks that
1885 * aren't in sync
1886 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11001887static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
NeilBrowne879a872011-10-11 16:49:02 +11001889 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 int i, first;
1891 struct bio *tbio, *fbio;
majianpengf4380a92012-04-12 16:04:47 +10001892 int vcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
1894 atomic_set(&r10_bio->remaining, 1);
1895
1896 /* find the first device with a block */
1897 for (i=0; i<conf->copies; i++)
1898 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1899 break;
1900
1901 if (i == conf->copies)
1902 goto done;
1903
1904 first = i;
1905 fbio = r10_bio->devs[i].bio;
1906
majianpengf4380a92012-04-12 16:04:47 +10001907 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001909 for (i=0 ; i < conf->copies ; i++) {
1910 int j, d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001913
1914 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001916 if (i == first)
1917 continue;
1918 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1919 /* We know that the bi_io_vec layout is the same for
1920 * both 'first' and 'i', so we just compare them.
1921 * All vec entries are PAGE_SIZE;
1922 */
1923 for (j = 0; j < vcnt; j++)
1924 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1925 page_address(tbio->bi_io_vec[j].bv_page),
NeilBrown5020ad72012-04-02 01:39:05 +10001926 fbio->bi_io_vec[j].bv_len))
NeilBrown0eb3ff12006-01-06 00:20:29 -08001927 break;
1928 if (j == vcnt)
1929 continue;
1930 mddev->resync_mismatches += r10_bio->sectors;
NeilBrownf84ee362011-07-28 11:39:25 +10001931 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1932 /* Don't fix anything. */
1933 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001934 }
NeilBrownf84ee362011-07-28 11:39:25 +10001935 /* Ok, we need to write this bio, either to correct an
1936 * inconsistency or to correct an unreadable block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 * First we need to fixup bv_offset, bv_len and
1938 * bi_vecs, as the read request might have corrupted these
1939 */
1940 tbio->bi_vcnt = vcnt;
1941 tbio->bi_size = r10_bio->sectors << 9;
1942 tbio->bi_idx = 0;
1943 tbio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1945 tbio->bi_flags |= 1 << BIO_UPTODATE;
1946 tbio->bi_next = NULL;
1947 tbio->bi_rw = WRITE;
1948 tbio->bi_private = r10_bio;
1949 tbio->bi_sector = r10_bio->devs[i].addr;
1950
1951 for (j=0; j < vcnt ; j++) {
1952 tbio->bi_io_vec[j].bv_offset = 0;
1953 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1954
1955 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1956 page_address(fbio->bi_io_vec[j].bv_page),
1957 PAGE_SIZE);
1958 }
1959 tbio->bi_end_io = end_sync_write;
1960
1961 d = r10_bio->devs[i].devnum;
1962 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1963 atomic_inc(&r10_bio->remaining);
1964 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1965
1966 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1967 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1968 generic_make_request(tbio);
1969 }
1970
NeilBrown9ad1aef2011-12-23 10:17:55 +11001971 /* Now write out to any replacement devices
1972 * that are active
1973 */
1974 for (i = 0; i < conf->copies; i++) {
1975 int j, d;
NeilBrown9ad1aef2011-12-23 10:17:55 +11001976
1977 tbio = r10_bio->devs[i].repl_bio;
1978 if (!tbio || !tbio->bi_end_io)
1979 continue;
1980 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
1981 && r10_bio->devs[i].bio != fbio)
1982 for (j = 0; j < vcnt; j++)
1983 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1984 page_address(fbio->bi_io_vec[j].bv_page),
1985 PAGE_SIZE);
1986 d = r10_bio->devs[i].devnum;
1987 atomic_inc(&r10_bio->remaining);
1988 md_sync_acct(conf->mirrors[d].replacement->bdev,
1989 tbio->bi_size >> 9);
1990 generic_make_request(tbio);
1991 }
1992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993done:
1994 if (atomic_dec_and_test(&r10_bio->remaining)) {
1995 md_done_sync(mddev, r10_bio->sectors, 1);
1996 put_buf(r10_bio);
1997 }
1998}
1999
2000/*
2001 * Now for the recovery code.
2002 * Recovery happens across physical sectors.
2003 * We recover all non-is_sync drives by finding the virtual address of
2004 * each, and then choose a working drive that also has that virt address.
2005 * There is a separate r10_bio for each non-in_sync drive.
2006 * Only the first two slots are in use. The first for reading,
2007 * The second for writing.
2008 *
2009 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11002010static void fix_recovery_read_error(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10002011{
2012 /* We got a read error during recovery.
2013 * We repeat the read in smaller page-sized sections.
2014 * If a read succeeds, write it to the new device or record
2015 * a bad block if we cannot.
2016 * If a read fails, record a bad block on both old and
2017 * new devices.
2018 */
NeilBrownfd01b882011-10-11 16:47:53 +11002019 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002020 struct r10conf *conf = mddev->private;
NeilBrown5e570282011-07-28 11:39:25 +10002021 struct bio *bio = r10_bio->devs[0].bio;
2022 sector_t sect = 0;
2023 int sectors = r10_bio->sectors;
2024 int idx = 0;
2025 int dr = r10_bio->devs[0].devnum;
2026 int dw = r10_bio->devs[1].devnum;
2027
2028 while (sectors) {
2029 int s = sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002030 struct md_rdev *rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002031 sector_t addr;
2032 int ok;
2033
2034 if (s > (PAGE_SIZE>>9))
2035 s = PAGE_SIZE >> 9;
2036
2037 rdev = conf->mirrors[dr].rdev;
2038 addr = r10_bio->devs[0].addr + sect,
2039 ok = sync_page_io(rdev,
2040 addr,
2041 s << 9,
2042 bio->bi_io_vec[idx].bv_page,
2043 READ, false);
2044 if (ok) {
2045 rdev = conf->mirrors[dw].rdev;
2046 addr = r10_bio->devs[1].addr + sect;
2047 ok = sync_page_io(rdev,
2048 addr,
2049 s << 9,
2050 bio->bi_io_vec[idx].bv_page,
2051 WRITE, false);
NeilBrownb7044d42011-12-23 10:17:56 +11002052 if (!ok) {
NeilBrown5e570282011-07-28 11:39:25 +10002053 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002054 if (!test_and_set_bit(WantReplacement,
2055 &rdev->flags))
2056 set_bit(MD_RECOVERY_NEEDED,
2057 &rdev->mddev->recovery);
2058 }
NeilBrown5e570282011-07-28 11:39:25 +10002059 }
2060 if (!ok) {
2061 /* We don't worry if we cannot set a bad block -
2062 * it really is bad so there is no loss in not
2063 * recording it yet
2064 */
2065 rdev_set_badblocks(rdev, addr, s, 0);
2066
2067 if (rdev != conf->mirrors[dw].rdev) {
2068 /* need bad block on destination too */
NeilBrown3cb03002011-10-11 16:45:26 +11002069 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002070 addr = r10_bio->devs[1].addr + sect;
2071 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2072 if (!ok) {
2073 /* just abort the recovery */
2074 printk(KERN_NOTICE
2075 "md/raid10:%s: recovery aborted"
2076 " due to read error\n",
2077 mdname(mddev));
2078
2079 conf->mirrors[dw].recovery_disabled
2080 = mddev->recovery_disabled;
2081 set_bit(MD_RECOVERY_INTR,
2082 &mddev->recovery);
2083 break;
2084 }
2085 }
2086 }
2087
2088 sectors -= s;
2089 sect += s;
2090 idx++;
2091 }
2092}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
NeilBrown9f2c9d12011-10-11 16:48:43 +11002094static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
NeilBrowne879a872011-10-11 16:49:02 +11002096 struct r10conf *conf = mddev->private;
Namhyung Kimc65060a2011-07-18 17:38:49 +10002097 int d;
NeilBrown24afd802011-12-23 10:17:55 +11002098 struct bio *wbio, *wbio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
NeilBrown5e570282011-07-28 11:39:25 +10002100 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2101 fix_recovery_read_error(r10_bio);
2102 end_sync_request(r10_bio);
2103 return;
2104 }
2105
Namhyung Kimc65060a2011-07-18 17:38:49 +10002106 /*
2107 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 * and submit the write request
2109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 d = r10_bio->devs[1].devnum;
NeilBrown24afd802011-12-23 10:17:55 +11002111 wbio = r10_bio->devs[1].bio;
2112 wbio2 = r10_bio->devs[1].repl_bio;
2113 if (wbio->bi_end_io) {
2114 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2115 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
2116 generic_make_request(wbio);
2117 }
2118 if (wbio2 && wbio2->bi_end_io) {
2119 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2120 md_sync_acct(conf->mirrors[d].replacement->bdev,
2121 wbio2->bi_size >> 9);
2122 generic_make_request(wbio2);
2123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124}
2125
2126
2127/*
Robert Becker1e509152009-12-14 12:49:58 +11002128 * Used by fix_read_error() to decay the per rdev read_errors.
2129 * We halve the read error count for every hour that has elapsed
2130 * since the last recorded read error.
2131 *
2132 */
NeilBrownfd01b882011-10-11 16:47:53 +11002133static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
Robert Becker1e509152009-12-14 12:49:58 +11002134{
2135 struct timespec cur_time_mon;
2136 unsigned long hours_since_last;
2137 unsigned int read_errors = atomic_read(&rdev->read_errors);
2138
2139 ktime_get_ts(&cur_time_mon);
2140
2141 if (rdev->last_read_error.tv_sec == 0 &&
2142 rdev->last_read_error.tv_nsec == 0) {
2143 /* first time we've seen a read error */
2144 rdev->last_read_error = cur_time_mon;
2145 return;
2146 }
2147
2148 hours_since_last = (cur_time_mon.tv_sec -
2149 rdev->last_read_error.tv_sec) / 3600;
2150
2151 rdev->last_read_error = cur_time_mon;
2152
2153 /*
2154 * if hours_since_last is > the number of bits in read_errors
2155 * just set read errors to 0. We do this to avoid
2156 * overflowing the shift of read_errors by hours_since_last.
2157 */
2158 if (hours_since_last >= 8 * sizeof(read_errors))
2159 atomic_set(&rdev->read_errors, 0);
2160 else
2161 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2162}
2163
NeilBrown3cb03002011-10-11 16:45:26 +11002164static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrown58c54fc2011-07-28 11:39:25 +10002165 int sectors, struct page *page, int rw)
2166{
2167 sector_t first_bad;
2168 int bad_sectors;
2169
2170 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2171 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2172 return -1;
2173 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
2174 /* success */
2175 return 1;
NeilBrownb7044d42011-12-23 10:17:56 +11002176 if (rw == WRITE) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002177 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002178 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2179 set_bit(MD_RECOVERY_NEEDED,
2180 &rdev->mddev->recovery);
2181 }
NeilBrown58c54fc2011-07-28 11:39:25 +10002182 /* need to record an error - either for the block or the device */
2183 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2184 md_error(rdev->mddev, rdev);
2185 return 0;
2186}
2187
Robert Becker1e509152009-12-14 12:49:58 +11002188/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 * This is a kernel thread which:
2190 *
2191 * 1. Retries failed read operations on working mirrors.
2192 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07002193 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 */
2195
NeilBrowne879a872011-10-11 16:49:02 +11002196static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown6814d532006-10-03 01:15:45 -07002197{
2198 int sect = 0; /* Offset from r10_bio->sector */
2199 int sectors = r10_bio->sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002200 struct md_rdev*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002201 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002202 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11002203
NeilBrown7c4e06f2011-05-11 14:53:17 +10002204 /* still own a reference to this rdev, so it cannot
2205 * have been cleared recently.
2206 */
2207 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002208
NeilBrown7c4e06f2011-05-11 14:53:17 +10002209 if (test_bit(Faulty, &rdev->flags))
2210 /* drive has already been failed, just ignore any
2211 more fix_read_error() attempts */
2212 return;
2213
2214 check_decay_read_errors(mddev, rdev);
2215 atomic_inc(&rdev->read_errors);
2216 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2217 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11002218 bdevname(rdev->bdev, b);
2219
NeilBrown7c4e06f2011-05-11 14:53:17 +10002220 printk(KERN_NOTICE
2221 "md/raid10:%s: %s: Raid device exceeded "
2222 "read_error threshold [cur %d:max %d]\n",
2223 mdname(mddev), b,
2224 atomic_read(&rdev->read_errors), max_read_errors);
2225 printk(KERN_NOTICE
2226 "md/raid10:%s: %s: Failing raid device\n",
2227 mdname(mddev), b);
2228 md_error(mddev, conf->mirrors[d].rdev);
NeilBrownfae8cc52012-02-14 11:10:10 +11002229 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
NeilBrown7c4e06f2011-05-11 14:53:17 +10002230 return;
Robert Becker1e509152009-12-14 12:49:58 +11002231 }
Robert Becker1e509152009-12-14 12:49:58 +11002232
NeilBrown6814d532006-10-03 01:15:45 -07002233 while(sectors) {
2234 int s = sectors;
2235 int sl = r10_bio->read_slot;
2236 int success = 0;
2237 int start;
2238
2239 if (s > (PAGE_SIZE>>9))
2240 s = PAGE_SIZE >> 9;
2241
2242 rcu_read_lock();
2243 do {
NeilBrown8dbed5c2011-07-28 11:39:24 +10002244 sector_t first_bad;
2245 int bad_sectors;
2246
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002247 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07002248 rdev = rcu_dereference(conf->mirrors[d].rdev);
2249 if (rdev &&
NeilBrown050b6612012-03-19 12:46:39 +11002250 !test_bit(Unmerged, &rdev->flags) &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002251 test_bit(In_sync, &rdev->flags) &&
2252 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2253 &first_bad, &bad_sectors) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07002254 atomic_inc(&rdev->nr_pending);
2255 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11002256 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07002257 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002258 sect,
NeilBrown6814d532006-10-03 01:15:45 -07002259 s<<9,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002260 conf->tmppage, READ, false);
NeilBrown6814d532006-10-03 01:15:45 -07002261 rdev_dec_pending(rdev, mddev);
2262 rcu_read_lock();
2263 if (success)
2264 break;
2265 }
2266 sl++;
2267 if (sl == conf->copies)
2268 sl = 0;
2269 } while (!success && sl != r10_bio->read_slot);
2270 rcu_read_unlock();
2271
2272 if (!success) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002273 /* Cannot read from anywhere, just mark the block
2274 * as bad on the first device to discourage future
2275 * reads.
2276 */
NeilBrown6814d532006-10-03 01:15:45 -07002277 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
NeilBrown58c54fc2011-07-28 11:39:25 +10002278 rdev = conf->mirrors[dn].rdev;
2279
2280 if (!rdev_set_badblocks(
2281 rdev,
2282 r10_bio->devs[r10_bio->read_slot].addr
2283 + sect,
NeilBrownfae8cc52012-02-14 11:10:10 +11002284 s, 0)) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002285 md_error(mddev, rdev);
NeilBrownfae8cc52012-02-14 11:10:10 +11002286 r10_bio->devs[r10_bio->read_slot].bio
2287 = IO_BLOCKED;
2288 }
NeilBrown6814d532006-10-03 01:15:45 -07002289 break;
2290 }
2291
2292 start = sl;
2293 /* write it back and re-read */
2294 rcu_read_lock();
2295 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11002296 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002297
NeilBrown6814d532006-10-03 01:15:45 -07002298 if (sl==0)
2299 sl = conf->copies;
2300 sl--;
2301 d = r10_bio->devs[sl].devnum;
2302 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002303 if (!rdev ||
NeilBrown050b6612012-03-19 12:46:39 +11002304 test_bit(Unmerged, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002305 !test_bit(In_sync, &rdev->flags))
2306 continue;
2307
2308 atomic_inc(&rdev->nr_pending);
2309 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002310 if (r10_sync_page_io(rdev,
2311 r10_bio->devs[sl].addr +
2312 sect,
2313 s<<9, conf->tmppage, WRITE)
NeilBrown1294b9c2011-07-28 11:39:23 +10002314 == 0) {
2315 /* Well, this device is dead */
2316 printk(KERN_NOTICE
2317 "md/raid10:%s: read correction "
2318 "write failed"
2319 " (%d sectors at %llu on %s)\n",
2320 mdname(mddev), s,
2321 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002322 sect +
2323 choose_data_offset(r10_bio,
2324 rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002325 bdevname(rdev->bdev, b));
2326 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2327 "drive\n",
2328 mdname(mddev),
2329 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07002330 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002331 rdev_dec_pending(rdev, mddev);
2332 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002333 }
2334 sl = start;
2335 while (sl != r10_bio->read_slot) {
NeilBrown1294b9c2011-07-28 11:39:23 +10002336 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002337
NeilBrown6814d532006-10-03 01:15:45 -07002338 if (sl==0)
2339 sl = conf->copies;
2340 sl--;
2341 d = r10_bio->devs[sl].devnum;
2342 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002343 if (!rdev ||
2344 !test_bit(In_sync, &rdev->flags))
2345 continue;
Robert Becker67b8dc42009-12-14 12:49:57 +11002346
NeilBrown1294b9c2011-07-28 11:39:23 +10002347 atomic_inc(&rdev->nr_pending);
2348 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002349 switch (r10_sync_page_io(rdev,
2350 r10_bio->devs[sl].addr +
2351 sect,
2352 s<<9, conf->tmppage,
2353 READ)) {
2354 case 0:
NeilBrown1294b9c2011-07-28 11:39:23 +10002355 /* Well, this device is dead */
2356 printk(KERN_NOTICE
2357 "md/raid10:%s: unable to read back "
2358 "corrected sectors"
2359 " (%d sectors at %llu on %s)\n",
2360 mdname(mddev), s,
2361 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002362 sect +
2363 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002364 bdevname(rdev->bdev, b));
2365 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2366 "drive\n",
2367 mdname(mddev),
2368 bdevname(rdev->bdev, b));
NeilBrown58c54fc2011-07-28 11:39:25 +10002369 break;
2370 case 1:
NeilBrown1294b9c2011-07-28 11:39:23 +10002371 printk(KERN_INFO
2372 "md/raid10:%s: read error corrected"
2373 " (%d sectors at %llu on %s)\n",
2374 mdname(mddev), s,
2375 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002376 sect +
2377 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002378 bdevname(rdev->bdev, b));
2379 atomic_add(s, &rdev->corrected_errors);
NeilBrown6814d532006-10-03 01:15:45 -07002380 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002381
2382 rdev_dec_pending(rdev, mddev);
2383 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002384 }
2385 rcu_read_unlock();
2386
2387 sectors -= s;
2388 sect += s;
2389 }
2390}
2391
NeilBrownbd870a12011-07-28 11:39:24 +10002392static void bi_complete(struct bio *bio, int error)
2393{
2394 complete((struct completion *)bio->bi_private);
2395}
2396
2397static int submit_bio_wait(int rw, struct bio *bio)
2398{
2399 struct completion event;
2400 rw |= REQ_SYNC;
2401
2402 init_completion(&event);
2403 bio->bi_private = &event;
2404 bio->bi_end_io = bi_complete;
2405 submit_bio(rw, bio);
2406 wait_for_completion(&event);
2407
2408 return test_bit(BIO_UPTODATE, &bio->bi_flags);
2409}
2410
NeilBrown9f2c9d12011-10-11 16:48:43 +11002411static int narrow_write_error(struct r10bio *r10_bio, int i)
NeilBrownbd870a12011-07-28 11:39:24 +10002412{
2413 struct bio *bio = r10_bio->master_bio;
NeilBrownfd01b882011-10-11 16:47:53 +11002414 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002415 struct r10conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002416 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
NeilBrownbd870a12011-07-28 11:39:24 +10002417 /* bio has the data to be written to slot 'i' where
2418 * we just recently had a write error.
2419 * We repeatedly clone the bio and trim down to one block,
2420 * then try the write. Where the write fails we record
2421 * a bad block.
2422 * It is conceivable that the bio doesn't exactly align with
2423 * blocks. We must handle this.
2424 *
2425 * We currently own a reference to the rdev.
2426 */
2427
2428 int block_sectors;
2429 sector_t sector;
2430 int sectors;
2431 int sect_to_write = r10_bio->sectors;
2432 int ok = 1;
2433
2434 if (rdev->badblocks.shift < 0)
2435 return 0;
2436
2437 block_sectors = 1 << rdev->badblocks.shift;
2438 sector = r10_bio->sector;
2439 sectors = ((r10_bio->sector + block_sectors)
2440 & ~(sector_t)(block_sectors - 1))
2441 - sector;
2442
2443 while (sect_to_write) {
2444 struct bio *wbio;
2445 if (sectors > sect_to_write)
2446 sectors = sect_to_write;
2447 /* Write at 'sector' for 'sectors' */
2448 wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
2449 md_trim_bio(wbio, sector - bio->bi_sector, sectors);
2450 wbio->bi_sector = (r10_bio->devs[i].addr+
NeilBrownf8c9e742012-05-21 09:28:33 +10002451 choose_data_offset(r10_bio, rdev) +
NeilBrownbd870a12011-07-28 11:39:24 +10002452 (sector - r10_bio->sector));
2453 wbio->bi_bdev = rdev->bdev;
2454 if (submit_bio_wait(WRITE, wbio) == 0)
2455 /* Failure! */
2456 ok = rdev_set_badblocks(rdev, sector,
2457 sectors, 0)
2458 && ok;
2459
2460 bio_put(wbio);
2461 sect_to_write -= sectors;
2462 sector += sectors;
2463 sectors = block_sectors;
2464 }
2465 return ok;
2466}
2467
NeilBrown9f2c9d12011-10-11 16:48:43 +11002468static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown560f8e52011-07-28 11:39:23 +10002469{
2470 int slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002471 struct bio *bio;
NeilBrowne879a872011-10-11 16:49:02 +11002472 struct r10conf *conf = mddev->private;
NeilBrownabbf0982011-12-23 10:17:54 +11002473 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002474 char b[BDEVNAME_SIZE];
2475 unsigned long do_sync;
NeilBrown856e08e2011-07-28 11:39:23 +10002476 int max_sectors;
NeilBrown560f8e52011-07-28 11:39:23 +10002477
2478 /* we got a read error. Maybe the drive is bad. Maybe just
2479 * the block and we can fix it.
2480 * We freeze all other IO, and try reading the block from
2481 * other devices. When we find one, we re-write
2482 * and check it that fixes the read error.
2483 * This is all done synchronously while the array is
2484 * frozen.
2485 */
NeilBrownfae8cc52012-02-14 11:10:10 +11002486 bio = r10_bio->devs[slot].bio;
2487 bdevname(bio->bi_bdev, b);
2488 bio_put(bio);
2489 r10_bio->devs[slot].bio = NULL;
2490
NeilBrown560f8e52011-07-28 11:39:23 +10002491 if (mddev->ro == 0) {
2492 freeze_array(conf);
2493 fix_read_error(conf, mddev, r10_bio);
2494 unfreeze_array(conf);
NeilBrownfae8cc52012-02-14 11:10:10 +11002495 } else
2496 r10_bio->devs[slot].bio = IO_BLOCKED;
2497
NeilBrownabbf0982011-12-23 10:17:54 +11002498 rdev_dec_pending(rdev, mddev);
NeilBrown560f8e52011-07-28 11:39:23 +10002499
NeilBrown7399c312011-07-28 11:39:23 +10002500read_more:
NeilBrown96c3fd12011-12-23 10:17:54 +11002501 rdev = read_balance(conf, r10_bio, &max_sectors);
2502 if (rdev == NULL) {
NeilBrown560f8e52011-07-28 11:39:23 +10002503 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
2504 " read error for block %llu\n",
NeilBrown7399c312011-07-28 11:39:23 +10002505 mdname(mddev), b,
NeilBrown560f8e52011-07-28 11:39:23 +10002506 (unsigned long long)r10_bio->sector);
2507 raid_end_bio_io(r10_bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002508 return;
2509 }
2510
2511 do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
NeilBrown560f8e52011-07-28 11:39:23 +10002512 slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002513 printk_ratelimited(
2514 KERN_ERR
2515 "md/raid10:%s: %s: redirecting"
2516 "sector %llu to another mirror\n",
2517 mdname(mddev),
2518 bdevname(rdev->bdev, b),
2519 (unsigned long long)r10_bio->sector);
2520 bio = bio_clone_mddev(r10_bio->master_bio,
2521 GFP_NOIO, mddev);
NeilBrown7399c312011-07-28 11:39:23 +10002522 md_trim_bio(bio,
2523 r10_bio->sector - bio->bi_sector,
2524 max_sectors);
NeilBrown560f8e52011-07-28 11:39:23 +10002525 r10_bio->devs[slot].bio = bio;
NeilBrownabbf0982011-12-23 10:17:54 +11002526 r10_bio->devs[slot].rdev = rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002527 bio->bi_sector = r10_bio->devs[slot].addr
NeilBrownf8c9e742012-05-21 09:28:33 +10002528 + choose_data_offset(r10_bio, rdev);
NeilBrown560f8e52011-07-28 11:39:23 +10002529 bio->bi_bdev = rdev->bdev;
2530 bio->bi_rw = READ | do_sync;
2531 bio->bi_private = r10_bio;
2532 bio->bi_end_io = raid10_end_read_request;
NeilBrown7399c312011-07-28 11:39:23 +10002533 if (max_sectors < r10_bio->sectors) {
2534 /* Drat - have to split this up more */
2535 struct bio *mbio = r10_bio->master_bio;
2536 int sectors_handled =
2537 r10_bio->sector + max_sectors
2538 - mbio->bi_sector;
2539 r10_bio->sectors = max_sectors;
2540 spin_lock_irq(&conf->device_lock);
2541 if (mbio->bi_phys_segments == 0)
2542 mbio->bi_phys_segments = 2;
2543 else
2544 mbio->bi_phys_segments++;
2545 spin_unlock_irq(&conf->device_lock);
2546 generic_make_request(bio);
NeilBrown7399c312011-07-28 11:39:23 +10002547
2548 r10_bio = mempool_alloc(conf->r10bio_pool,
2549 GFP_NOIO);
2550 r10_bio->master_bio = mbio;
2551 r10_bio->sectors = (mbio->bi_size >> 9)
2552 - sectors_handled;
2553 r10_bio->state = 0;
2554 set_bit(R10BIO_ReadError,
2555 &r10_bio->state);
2556 r10_bio->mddev = mddev;
2557 r10_bio->sector = mbio->bi_sector
2558 + sectors_handled;
2559
2560 goto read_more;
2561 } else
2562 generic_make_request(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002563}
2564
NeilBrowne879a872011-10-11 16:49:02 +11002565static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
NeilBrown749c55e2011-07-28 11:39:24 +10002566{
2567 /* Some sort of write request has finished and it
2568 * succeeded in writing where we thought there was a
2569 * bad block. So forget the bad block.
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002570 * Or possibly if failed and we need to record
2571 * a bad block.
NeilBrown749c55e2011-07-28 11:39:24 +10002572 */
2573 int m;
NeilBrown3cb03002011-10-11 16:45:26 +11002574 struct md_rdev *rdev;
NeilBrown749c55e2011-07-28 11:39:24 +10002575
2576 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2577 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002578 for (m = 0; m < conf->copies; m++) {
2579 int dev = r10_bio->devs[m].devnum;
2580 rdev = conf->mirrors[dev].rdev;
2581 if (r10_bio->devs[m].bio == NULL)
2582 continue;
2583 if (test_bit(BIO_UPTODATE,
NeilBrown749c55e2011-07-28 11:39:24 +10002584 &r10_bio->devs[m].bio->bi_flags)) {
NeilBrown749c55e2011-07-28 11:39:24 +10002585 rdev_clear_badblocks(
2586 rdev,
2587 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002588 r10_bio->sectors, 0);
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002589 } else {
2590 if (!rdev_set_badblocks(
2591 rdev,
2592 r10_bio->devs[m].addr,
2593 r10_bio->sectors, 0))
2594 md_error(conf->mddev, rdev);
NeilBrown749c55e2011-07-28 11:39:24 +10002595 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002596 rdev = conf->mirrors[dev].replacement;
2597 if (r10_bio->devs[m].repl_bio == NULL)
2598 continue;
2599 if (test_bit(BIO_UPTODATE,
2600 &r10_bio->devs[m].repl_bio->bi_flags)) {
2601 rdev_clear_badblocks(
2602 rdev,
2603 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002604 r10_bio->sectors, 0);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002605 } else {
2606 if (!rdev_set_badblocks(
2607 rdev,
2608 r10_bio->devs[m].addr,
2609 r10_bio->sectors, 0))
2610 md_error(conf->mddev, rdev);
2611 }
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002612 }
NeilBrown749c55e2011-07-28 11:39:24 +10002613 put_buf(r10_bio);
2614 } else {
NeilBrownbd870a12011-07-28 11:39:24 +10002615 for (m = 0; m < conf->copies; m++) {
2616 int dev = r10_bio->devs[m].devnum;
2617 struct bio *bio = r10_bio->devs[m].bio;
2618 rdev = conf->mirrors[dev].rdev;
2619 if (bio == IO_MADE_GOOD) {
NeilBrown749c55e2011-07-28 11:39:24 +10002620 rdev_clear_badblocks(
2621 rdev,
2622 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002623 r10_bio->sectors, 0);
NeilBrown749c55e2011-07-28 11:39:24 +10002624 rdev_dec_pending(rdev, conf->mddev);
NeilBrownbd870a12011-07-28 11:39:24 +10002625 } else if (bio != NULL &&
2626 !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2627 if (!narrow_write_error(r10_bio, m)) {
2628 md_error(conf->mddev, rdev);
2629 set_bit(R10BIO_Degraded,
2630 &r10_bio->state);
2631 }
2632 rdev_dec_pending(rdev, conf->mddev);
NeilBrown749c55e2011-07-28 11:39:24 +10002633 }
NeilBrown475b0322011-12-23 10:17:55 +11002634 bio = r10_bio->devs[m].repl_bio;
2635 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +11002636 if (rdev && bio == IO_MADE_GOOD) {
NeilBrown475b0322011-12-23 10:17:55 +11002637 rdev_clear_badblocks(
2638 rdev,
2639 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002640 r10_bio->sectors, 0);
NeilBrown475b0322011-12-23 10:17:55 +11002641 rdev_dec_pending(rdev, conf->mddev);
2642 }
NeilBrownbd870a12011-07-28 11:39:24 +10002643 }
2644 if (test_bit(R10BIO_WriteError,
2645 &r10_bio->state))
2646 close_write(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10002647 raid_end_bio_io(r10_bio);
2648 }
2649}
2650
NeilBrownfd01b882011-10-11 16:47:53 +11002651static void raid10d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652{
NeilBrown9f2c9d12011-10-11 16:48:43 +11002653 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 unsigned long flags;
NeilBrowne879a872011-10-11 16:49:02 +11002655 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002657 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
2659 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002661 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002663
Jens Axboe7eaceac2011-03-10 08:52:07 +01002664 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002667 if (list_empty(head)) {
NeilBrown6cce3b232006-01-06 00:20:16 -08002668 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002670 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002671 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08002673 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 spin_unlock_irqrestore(&conf->device_lock, flags);
2675
2676 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002677 conf = mddev->private;
NeilBrownbd870a12011-07-28 11:39:24 +10002678 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2679 test_bit(R10BIO_WriteError, &r10_bio->state))
NeilBrown749c55e2011-07-28 11:39:24 +10002680 handle_write_completed(conf, r10_bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10002681 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
2682 reshape_request_write(mddev, r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10002683 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01002685 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 recovery_request_write(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002687 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
NeilBrown560f8e52011-07-28 11:39:23 +10002688 handle_read_error(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002689 else {
2690 /* just a partial read to be scheduled from a
2691 * separate context
2692 */
2693 int slot = r10_bio->read_slot;
2694 generic_make_request(r10_bio->devs[slot].bio);
2695 }
NeilBrown4443ae12006-01-06 00:20:28 -08002696
NeilBrown1d9d5242009-10-16 15:55:32 +11002697 cond_resched();
NeilBrownde393cd2011-07-28 11:31:48 +10002698 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2699 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002701 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702}
2703
2704
NeilBrowne879a872011-10-11 16:49:02 +11002705static int init_resync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706{
2707 int buffs;
NeilBrown69335ef2011-12-23 10:17:54 +11002708 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
2710 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02002711 BUG_ON(conf->r10buf_pool);
NeilBrown69335ef2011-12-23 10:17:54 +11002712 conf->have_replacement = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002713 for (i = 0; i < conf->geo.raid_disks; i++)
NeilBrown69335ef2011-12-23 10:17:54 +11002714 if (conf->mirrors[i].replacement)
2715 conf->have_replacement = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2717 if (!conf->r10buf_pool)
2718 return -ENOMEM;
2719 conf->next_resync = 0;
2720 return 0;
2721}
2722
2723/*
2724 * perform a "sync" on one "block"
2725 *
2726 * We need to make sure that no normal I/O request - particularly write
2727 * requests - conflict with active sync requests.
2728 *
2729 * This is achieved by tracking pending requests and a 'barrier' concept
2730 * that can be installed to exclude normal IO requests.
2731 *
2732 * Resync and recovery are handled very differently.
2733 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2734 *
2735 * For resync, we iterate over virtual addresses, read all copies,
2736 * and update if there are differences. If only one copy is live,
2737 * skip it.
2738 * For recovery, we iterate over physical addresses, read a good
2739 * value for each non-in_sync drive, and over-write.
2740 *
2741 * So, for recovery we may have several outstanding complex requests for a
2742 * given address, one for each out-of-sync device. We model this by allocating
2743 * a number of r10_bio structures, one for each out-of-sync device.
2744 * As we setup these structures, we collect all bio's together into a list
2745 * which we then process collectively to add pages, and then process again
2746 * to pass to generic_make_request.
2747 *
2748 * The r10_bio structures are linked using a borrowed master_bio pointer.
2749 * This link is counted in ->remaining. When the r10_bio that points to NULL
2750 * has its remaining count decremented to 0, the whole complex operation
2751 * is complete.
2752 *
2753 */
2754
NeilBrownfd01b882011-10-11 16:47:53 +11002755static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
NeilBrownab9d47e2011-05-11 14:54:41 +10002756 int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757{
NeilBrowne879a872011-10-11 16:49:02 +11002758 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002759 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 struct bio *biolist = NULL, *bio;
2761 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 int i;
NeilBrown6cce3b232006-01-06 00:20:16 -08002763 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11002764 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 sector_t sectors_skipped = 0;
2766 int chunks_skipped = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002767 sector_t chunk_mask = conf->geo.chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
2769 if (!conf->r10buf_pool)
2770 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002771 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
2773 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11002774 max_sector = mddev->dev_sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10002775 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
2776 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 max_sector = mddev->resync_max_sectors;
2778 if (sector_nr >= max_sector) {
NeilBrown6cce3b232006-01-06 00:20:16 -08002779 /* If we aborted, we need to abort the
2780 * sync on the 'current' bitmap chucks (there can
2781 * be several when recovering multiple devices).
2782 * as we may have started syncing it but not finished.
2783 * We can find the current address in
2784 * mddev->curr_resync, but for recovery,
2785 * we need to convert that to several
2786 * virtual addresses.
2787 */
NeilBrown3ea7daa2012-05-22 13:53:47 +10002788 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
2789 end_reshape(conf);
2790 return 0;
2791 }
2792
NeilBrown6cce3b232006-01-06 00:20:16 -08002793 if (mddev->curr_resync < max_sector) { /* aborted */
2794 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2795 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2796 &sync_blocks, 1);
NeilBrown5cf00fc2012-05-21 09:28:20 +10002797 else for (i = 0; i < conf->geo.raid_disks; i++) {
NeilBrown6cce3b232006-01-06 00:20:16 -08002798 sector_t sect =
2799 raid10_find_virt(conf, mddev->curr_resync, i);
2800 bitmap_end_sync(mddev->bitmap, sect,
2801 &sync_blocks, 1);
2802 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002803 } else {
2804 /* completed sync */
2805 if ((!mddev->bitmap || conf->fullsync)
2806 && conf->have_replacement
2807 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2808 /* Completed a full sync so the replacements
2809 * are now fully recovered.
2810 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10002811 for (i = 0; i < conf->geo.raid_disks; i++)
NeilBrown9ad1aef2011-12-23 10:17:55 +11002812 if (conf->mirrors[i].replacement)
2813 conf->mirrors[i].replacement
2814 ->recovery_offset
2815 = MaxSector;
2816 }
NeilBrown6cce3b232006-01-06 00:20:16 -08002817 conf->fullsync = 0;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002818 }
NeilBrown6cce3b232006-01-06 00:20:16 -08002819 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07002821 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 return sectors_skipped;
2823 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10002824
2825 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2826 return reshape_request(mddev, sector_nr, skipped);
2827
NeilBrown5cf00fc2012-05-21 09:28:20 +10002828 if (chunks_skipped >= conf->geo.raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 /* if there has been nothing to do on any drive,
2830 * then there is nothing to do at all..
2831 */
NeilBrown57afd892005-06-21 17:17:13 -07002832 *skipped = 1;
2833 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 }
2835
NeilBrownc6207272008-02-06 01:39:52 -08002836 if (max_sector > mddev->resync_max)
2837 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 /* make sure whole request will fit in a chunk - if chunks
2840 * are meaningful
2841 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10002842 if (conf->geo.near_copies < conf->geo.raid_disks &&
2843 max_sector > (sector_nr | chunk_mask))
2844 max_sector = (sector_nr | chunk_mask) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 /*
2846 * If there is non-resync activity waiting for us then
2847 * put in a delay to throttle resync.
2848 */
NeilBrown0a27ec92006-01-06 00:20:13 -08002849 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
2852 /* Again, very different code for resync and recovery.
2853 * Both must result in an r10bio with a list of bios that
2854 * have bi_end_io, bi_sector, bi_bdev set,
2855 * and bi_private set to the r10bio.
2856 * For recovery, we may actually create several r10bios
2857 * with 2 bios in each, that correspond to the bios in the main one.
2858 * In this case, the subordinate r10bios link back through a
2859 * borrowed master_bio pointer, and the counter in the master
2860 * includes a ref from each subordinate.
2861 */
2862 /* First, we decide what to do and set ->bi_end_io
2863 * To end_sync_read if we want to read, and
2864 * end_sync_write if we will want to write.
2865 */
2866
NeilBrown6cce3b232006-01-06 00:20:16 -08002867 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2869 /* recovery... the complicated one */
NeilBrowne875ece2011-07-28 11:39:24 +10002870 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 r10_bio = NULL;
2872
NeilBrown5cf00fc2012-05-21 09:28:20 +10002873 for (i = 0 ; i < conf->geo.raid_disks; i++) {
NeilBrownab9d47e2011-05-11 14:54:41 +10002874 int still_degraded;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002875 struct r10bio *rb2;
NeilBrownab9d47e2011-05-11 14:54:41 +10002876 sector_t sect;
2877 int must_sync;
NeilBrowne875ece2011-07-28 11:39:24 +10002878 int any_working;
NeilBrown24afd802011-12-23 10:17:55 +11002879 struct mirror_info *mirror = &conf->mirrors[i];
NeilBrownab9d47e2011-05-11 14:54:41 +10002880
NeilBrown24afd802011-12-23 10:17:55 +11002881 if ((mirror->rdev == NULL ||
2882 test_bit(In_sync, &mirror->rdev->flags))
2883 &&
2884 (mirror->replacement == NULL ||
2885 test_bit(Faulty,
2886 &mirror->replacement->flags)))
NeilBrownab9d47e2011-05-11 14:54:41 +10002887 continue;
2888
2889 still_degraded = 0;
2890 /* want to reconstruct this device */
2891 rb2 = r10_bio;
2892 sect = raid10_find_virt(conf, sector_nr, i);
NeilBrown24afd802011-12-23 10:17:55 +11002893 /* Unless we are doing a full sync, or a replacement
2894 * we only need to recover the block if it is set in
2895 * the bitmap
NeilBrownab9d47e2011-05-11 14:54:41 +10002896 */
2897 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2898 &sync_blocks, 1);
2899 if (sync_blocks < max_sync)
2900 max_sync = sync_blocks;
2901 if (!must_sync &&
NeilBrown24afd802011-12-23 10:17:55 +11002902 mirror->replacement == NULL &&
NeilBrownab9d47e2011-05-11 14:54:41 +10002903 !conf->fullsync) {
2904 /* yep, skip the sync_blocks here, but don't assume
2905 * that there will never be anything to do here
NeilBrown6cce3b232006-01-06 00:20:16 -08002906 */
NeilBrownab9d47e2011-05-11 14:54:41 +10002907 chunks_skipped = -1;
2908 continue;
2909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
NeilBrownab9d47e2011-05-11 14:54:41 +10002911 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2912 raise_barrier(conf, rb2 != NULL);
2913 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
NeilBrownab9d47e2011-05-11 14:54:41 +10002915 r10_bio->master_bio = (struct bio*)rb2;
2916 if (rb2)
2917 atomic_inc(&rb2->remaining);
2918 r10_bio->mddev = mddev;
2919 set_bit(R10BIO_IsRecover, &r10_bio->state);
2920 r10_bio->sector = sect;
NeilBrown6cce3b232006-01-06 00:20:16 -08002921
NeilBrownab9d47e2011-05-11 14:54:41 +10002922 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10002923
NeilBrownab9d47e2011-05-11 14:54:41 +10002924 /* Need to check if the array will still be
2925 * degraded
2926 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10002927 for (j = 0; j < conf->geo.raid_disks; j++)
NeilBrownab9d47e2011-05-11 14:54:41 +10002928 if (conf->mirrors[j].rdev == NULL ||
2929 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
2930 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07002931 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002933
2934 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2935 &sync_blocks, still_degraded);
2936
NeilBrowne875ece2011-07-28 11:39:24 +10002937 any_working = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10002938 for (j=0; j<conf->copies;j++) {
NeilBrowne875ece2011-07-28 11:39:24 +10002939 int k;
NeilBrownab9d47e2011-05-11 14:54:41 +10002940 int d = r10_bio->devs[j].devnum;
NeilBrown5e570282011-07-28 11:39:25 +10002941 sector_t from_addr, to_addr;
NeilBrown3cb03002011-10-11 16:45:26 +11002942 struct md_rdev *rdev;
NeilBrown40c356c2011-07-28 11:39:24 +10002943 sector_t sector, first_bad;
2944 int bad_sectors;
NeilBrownab9d47e2011-05-11 14:54:41 +10002945 if (!conf->mirrors[d].rdev ||
2946 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
2947 continue;
2948 /* This is where we read from */
NeilBrowne875ece2011-07-28 11:39:24 +10002949 any_working = 1;
NeilBrown40c356c2011-07-28 11:39:24 +10002950 rdev = conf->mirrors[d].rdev;
2951 sector = r10_bio->devs[j].addr;
2952
2953 if (is_badblock(rdev, sector, max_sync,
2954 &first_bad, &bad_sectors)) {
2955 if (first_bad > sector)
2956 max_sync = first_bad - sector;
2957 else {
2958 bad_sectors -= (sector
2959 - first_bad);
2960 if (max_sync > bad_sectors)
2961 max_sync = bad_sectors;
2962 continue;
2963 }
2964 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002965 bio = r10_bio->devs[0].bio;
2966 bio->bi_next = biolist;
2967 biolist = bio;
2968 bio->bi_private = r10_bio;
2969 bio->bi_end_io = end_sync_read;
2970 bio->bi_rw = READ;
NeilBrown5e570282011-07-28 11:39:25 +10002971 from_addr = r10_bio->devs[j].addr;
NeilBrown24afd802011-12-23 10:17:55 +11002972 bio->bi_sector = from_addr + rdev->data_offset;
2973 bio->bi_bdev = rdev->bdev;
2974 atomic_inc(&rdev->nr_pending);
2975 /* and we write to 'i' (if not in_sync) */
NeilBrownab9d47e2011-05-11 14:54:41 +10002976
2977 for (k=0; k<conf->copies; k++)
2978 if (r10_bio->devs[k].devnum == i)
2979 break;
2980 BUG_ON(k == conf->copies);
NeilBrown5e570282011-07-28 11:39:25 +10002981 to_addr = r10_bio->devs[k].addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10002982 r10_bio->devs[0].devnum = d;
NeilBrown5e570282011-07-28 11:39:25 +10002983 r10_bio->devs[0].addr = from_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10002984 r10_bio->devs[1].devnum = i;
NeilBrown5e570282011-07-28 11:39:25 +10002985 r10_bio->devs[1].addr = to_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10002986
NeilBrown24afd802011-12-23 10:17:55 +11002987 rdev = mirror->rdev;
2988 if (!test_bit(In_sync, &rdev->flags)) {
2989 bio = r10_bio->devs[1].bio;
2990 bio->bi_next = biolist;
2991 biolist = bio;
2992 bio->bi_private = r10_bio;
2993 bio->bi_end_io = end_sync_write;
2994 bio->bi_rw = WRITE;
2995 bio->bi_sector = to_addr
2996 + rdev->data_offset;
2997 bio->bi_bdev = rdev->bdev;
2998 atomic_inc(&r10_bio->remaining);
2999 } else
3000 r10_bio->devs[1].bio->bi_end_io = NULL;
3001
3002 /* and maybe write to replacement */
3003 bio = r10_bio->devs[1].repl_bio;
3004 if (bio)
3005 bio->bi_end_io = NULL;
3006 rdev = mirror->replacement;
3007 /* Note: if rdev != NULL, then bio
3008 * cannot be NULL as r10buf_pool_alloc will
3009 * have allocated it.
3010 * So the second test here is pointless.
3011 * But it keeps semantic-checkers happy, and
3012 * this comment keeps human reviewers
3013 * happy.
3014 */
3015 if (rdev == NULL || bio == NULL ||
3016 test_bit(Faulty, &rdev->flags))
3017 break;
3018 bio->bi_next = biolist;
3019 biolist = bio;
3020 bio->bi_private = r10_bio;
3021 bio->bi_end_io = end_sync_write;
3022 bio->bi_rw = WRITE;
3023 bio->bi_sector = to_addr + rdev->data_offset;
3024 bio->bi_bdev = rdev->bdev;
3025 atomic_inc(&r10_bio->remaining);
NeilBrownab9d47e2011-05-11 14:54:41 +10003026 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003028 if (j == conf->copies) {
NeilBrowne875ece2011-07-28 11:39:24 +10003029 /* Cannot recover, so abort the recovery or
3030 * record a bad block */
NeilBrownab9d47e2011-05-11 14:54:41 +10003031 put_buf(r10_bio);
3032 if (rb2)
3033 atomic_dec(&rb2->remaining);
3034 r10_bio = rb2;
NeilBrowne875ece2011-07-28 11:39:24 +10003035 if (any_working) {
3036 /* problem is that there are bad blocks
3037 * on other device(s)
3038 */
3039 int k;
3040 for (k = 0; k < conf->copies; k++)
3041 if (r10_bio->devs[k].devnum == i)
3042 break;
NeilBrown24afd802011-12-23 10:17:55 +11003043 if (!test_bit(In_sync,
3044 &mirror->rdev->flags)
3045 && !rdev_set_badblocks(
3046 mirror->rdev,
3047 r10_bio->devs[k].addr,
3048 max_sync, 0))
3049 any_working = 0;
3050 if (mirror->replacement &&
3051 !rdev_set_badblocks(
3052 mirror->replacement,
NeilBrowne875ece2011-07-28 11:39:24 +10003053 r10_bio->devs[k].addr,
3054 max_sync, 0))
3055 any_working = 0;
3056 }
3057 if (!any_working) {
3058 if (!test_and_set_bit(MD_RECOVERY_INTR,
3059 &mddev->recovery))
3060 printk(KERN_INFO "md/raid10:%s: insufficient "
3061 "working devices for recovery.\n",
3062 mdname(mddev));
NeilBrown24afd802011-12-23 10:17:55 +11003063 mirror->recovery_disabled
NeilBrowne875ece2011-07-28 11:39:24 +10003064 = mddev->recovery_disabled;
3065 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003066 break;
3067 }
3068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 if (biolist == NULL) {
3070 while (r10_bio) {
NeilBrown9f2c9d12011-10-11 16:48:43 +11003071 struct r10bio *rb2 = r10_bio;
3072 r10_bio = (struct r10bio*) rb2->master_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 rb2->master_bio = NULL;
3074 put_buf(rb2);
3075 }
3076 goto giveup;
3077 }
3078 } else {
3079 /* resync. Schedule a read for every block at this virt offset */
3080 int count = 0;
NeilBrown6cce3b232006-01-06 00:20:16 -08003081
NeilBrown78200d42009-02-25 13:18:47 +11003082 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
3083
NeilBrown6cce3b232006-01-06 00:20:16 -08003084 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
3085 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003086 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3087 &mddev->recovery)) {
NeilBrown6cce3b232006-01-06 00:20:16 -08003088 /* We can skip this block */
3089 *skipped = 1;
3090 return sync_blocks + sectors_skipped;
3091 }
3092 if (sync_blocks < max_sync)
3093 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
3095
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 r10_bio->mddev = mddev;
3097 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b232006-01-06 00:20:16 -08003098 raise_barrier(conf, 0);
3099 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
3101 r10_bio->master_bio = NULL;
3102 r10_bio->sector = sector_nr;
3103 set_bit(R10BIO_IsSync, &r10_bio->state);
3104 raid10_find_phys(conf, r10_bio);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003105 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
NeilBrown5cf00fc2012-05-21 09:28:20 +10003107 for (i = 0; i < conf->copies; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 int d = r10_bio->devs[i].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10003109 sector_t first_bad, sector;
3110 int bad_sectors;
3111
NeilBrown9ad1aef2011-12-23 10:17:55 +11003112 if (r10_bio->devs[i].repl_bio)
3113 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3114
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 bio = r10_bio->devs[i].bio;
3116 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07003117 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08003119 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 continue;
NeilBrown40c356c2011-07-28 11:39:24 +10003121 sector = r10_bio->devs[i].addr;
3122 if (is_badblock(conf->mirrors[d].rdev,
3123 sector, max_sync,
3124 &first_bad, &bad_sectors)) {
3125 if (first_bad > sector)
3126 max_sync = first_bad - sector;
3127 else {
3128 bad_sectors -= (sector - first_bad);
3129 if (max_sync > bad_sectors)
3130 max_sync = max_sync;
3131 continue;
3132 }
3133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
3135 atomic_inc(&r10_bio->remaining);
3136 bio->bi_next = biolist;
3137 biolist = bio;
3138 bio->bi_private = r10_bio;
3139 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08003140 bio->bi_rw = READ;
NeilBrown40c356c2011-07-28 11:39:24 +10003141 bio->bi_sector = sector +
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 conf->mirrors[d].rdev->data_offset;
3143 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
3144 count++;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003145
3146 if (conf->mirrors[d].replacement == NULL ||
3147 test_bit(Faulty,
3148 &conf->mirrors[d].replacement->flags))
3149 continue;
3150
3151 /* Need to set up for writing to the replacement */
3152 bio = r10_bio->devs[i].repl_bio;
3153 clear_bit(BIO_UPTODATE, &bio->bi_flags);
3154
3155 sector = r10_bio->devs[i].addr;
3156 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
3157 bio->bi_next = biolist;
3158 biolist = bio;
3159 bio->bi_private = r10_bio;
3160 bio->bi_end_io = end_sync_write;
3161 bio->bi_rw = WRITE;
3162 bio->bi_sector = sector +
3163 conf->mirrors[d].replacement->data_offset;
3164 bio->bi_bdev = conf->mirrors[d].replacement->bdev;
3165 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 }
3167
3168 if (count < 2) {
3169 for (i=0; i<conf->copies; i++) {
3170 int d = r10_bio->devs[i].devnum;
3171 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10003172 rdev_dec_pending(conf->mirrors[d].rdev,
3173 mddev);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003174 if (r10_bio->devs[i].repl_bio &&
3175 r10_bio->devs[i].repl_bio->bi_end_io)
3176 rdev_dec_pending(
3177 conf->mirrors[d].replacement,
3178 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 }
3180 put_buf(r10_bio);
3181 biolist = NULL;
3182 goto giveup;
3183 }
3184 }
3185
3186 for (bio = biolist; bio ; bio=bio->bi_next) {
3187
3188 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
3189 if (bio->bi_end_io)
3190 bio->bi_flags |= 1 << BIO_UPTODATE;
3191 bio->bi_vcnt = 0;
3192 bio->bi_idx = 0;
3193 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 bio->bi_size = 0;
3195 }
3196
3197 nr_sectors = 0;
NeilBrown6cce3b232006-01-06 00:20:16 -08003198 if (sector_nr + max_sync < max_sector)
3199 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 do {
3201 struct page *page;
3202 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 if (sector_nr + (len>>9) > max_sector)
3204 len = (max_sector - sector_nr) << 9;
3205 if (len == 0)
3206 break;
3207 for (bio= biolist ; bio ; bio=bio->bi_next) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003208 struct bio *bio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
NeilBrownab9d47e2011-05-11 14:54:41 +10003210 if (bio_add_page(bio, page, len, 0))
3211 continue;
3212
3213 /* stop here */
3214 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
3215 for (bio2 = biolist;
3216 bio2 && bio2 != bio;
3217 bio2 = bio2->bi_next) {
3218 /* remove last page from this bio */
3219 bio2->bi_vcnt--;
3220 bio2->bi_size -= len;
3221 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003223 goto bio_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 }
3225 nr_sectors += len>>9;
3226 sector_nr += len>>9;
3227 } while (biolist->bi_vcnt < RESYNC_PAGES);
3228 bio_full:
3229 r10_bio->sectors = nr_sectors;
3230
3231 while (biolist) {
3232 bio = biolist;
3233 biolist = biolist->bi_next;
3234
3235 bio->bi_next = NULL;
3236 r10_bio = bio->bi_private;
3237 r10_bio->sectors = nr_sectors;
3238
3239 if (bio->bi_end_io == end_sync_read) {
3240 md_sync_acct(bio->bi_bdev, nr_sectors);
3241 generic_make_request(bio);
3242 }
3243 }
3244
NeilBrown57afd892005-06-21 17:17:13 -07003245 if (sectors_skipped)
3246 /* pretend they weren't skipped, it makes
3247 * no important difference in this case
3248 */
3249 md_done_sync(mddev, sectors_skipped, 1);
3250
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 return sectors_skipped + nr_sectors;
3252 giveup:
3253 /* There is nowhere to write, so all non-sync
NeilBrowne875ece2011-07-28 11:39:24 +10003254 * drives must be failed or in resync, all drives
3255 * have a bad block, so try the next chunk...
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 */
NeilBrown09b40682009-02-25 13:18:47 +11003257 if (sector_nr + max_sync < max_sector)
3258 max_sector = sector_nr + max_sync;
3259
3260 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 chunks_skipped ++;
3262 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264}
3265
Dan Williams80c3a6c2009-03-17 18:10:40 -07003266static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11003267raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07003268{
3269 sector_t size;
NeilBrowne879a872011-10-11 16:49:02 +11003270 struct r10conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003271
3272 if (!raid_disks)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003273 raid_disks = min(conf->geo.raid_disks,
3274 conf->prev.raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003275 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003276 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003277
NeilBrown5cf00fc2012-05-21 09:28:20 +10003278 size = sectors >> conf->geo.chunk_shift;
3279 sector_div(size, conf->geo.far_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003280 size = size * raid_disks;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003281 sector_div(size, conf->geo.near_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003282
NeilBrown5cf00fc2012-05-21 09:28:20 +10003283 return size << conf->geo.chunk_shift;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003284}
3285
NeilBrown6508fdb2012-05-17 10:08:45 +10003286static void calc_sectors(struct r10conf *conf, sector_t size)
3287{
3288 /* Calculate the number of sectors-per-device that will
3289 * actually be used, and set conf->dev_sectors and
3290 * conf->stride
3291 */
3292
NeilBrown5cf00fc2012-05-21 09:28:20 +10003293 size = size >> conf->geo.chunk_shift;
3294 sector_div(size, conf->geo.far_copies);
3295 size = size * conf->geo.raid_disks;
3296 sector_div(size, conf->geo.near_copies);
NeilBrown6508fdb2012-05-17 10:08:45 +10003297 /* 'size' is now the number of chunks in the array */
3298 /* calculate "used chunks per device" */
3299 size = size * conf->copies;
3300
3301 /* We need to round up when dividing by raid_disks to
3302 * get the stride size.
3303 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003304 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
NeilBrown6508fdb2012-05-17 10:08:45 +10003305
NeilBrown5cf00fc2012-05-21 09:28:20 +10003306 conf->dev_sectors = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003307
NeilBrown5cf00fc2012-05-21 09:28:20 +10003308 if (conf->geo.far_offset)
3309 conf->geo.stride = 1 << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003310 else {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003311 sector_div(size, conf->geo.far_copies);
3312 conf->geo.stride = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003313 }
3314}
Trela, Maciejdab8b292010-03-08 16:02:45 +11003315
NeilBrowndeb200d2012-05-21 09:28:33 +10003316enum geo_type {geo_new, geo_old, geo_start};
3317static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3318{
3319 int nc, fc, fo;
3320 int layout, chunk, disks;
3321 switch (new) {
3322 case geo_old:
3323 layout = mddev->layout;
3324 chunk = mddev->chunk_sectors;
3325 disks = mddev->raid_disks - mddev->delta_disks;
3326 break;
3327 case geo_new:
3328 layout = mddev->new_layout;
3329 chunk = mddev->new_chunk_sectors;
3330 disks = mddev->raid_disks;
3331 break;
3332 default: /* avoid 'may be unused' warnings */
3333 case geo_start: /* new when starting reshape - raid_disks not
3334 * updated yet. */
3335 layout = mddev->new_layout;
3336 chunk = mddev->new_chunk_sectors;
3337 disks = mddev->raid_disks + mddev->delta_disks;
3338 break;
3339 }
3340 if (layout >> 17)
3341 return -1;
3342 if (chunk < (PAGE_SIZE >> 9) ||
3343 !is_power_of_2(chunk))
3344 return -2;
3345 nc = layout & 255;
3346 fc = (layout >> 8) & 255;
3347 fo = layout & (1<<16);
3348 geo->raid_disks = disks;
3349 geo->near_copies = nc;
3350 geo->far_copies = fc;
3351 geo->far_offset = fo;
3352 geo->chunk_mask = chunk - 1;
3353 geo->chunk_shift = ffz(~chunk);
3354 return nc*fc;
3355}
3356
NeilBrowne879a872011-10-11 16:49:02 +11003357static struct r10conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358{
NeilBrowne879a872011-10-11 16:49:02 +11003359 struct r10conf *conf = NULL;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003360 int err = -EINVAL;
NeilBrowndeb200d2012-05-21 09:28:33 +10003361 struct geom geo;
3362 int copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
NeilBrowndeb200d2012-05-21 09:28:33 +10003364 copies = setup_geo(&geo, mddev, geo_new);
3365
3366 if (copies == -2) {
NeilBrown128595e2010-05-03 14:47:14 +10003367 printk(KERN_ERR "md/raid10:%s: chunk size must be "
3368 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
3369 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003370 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 }
NeilBrown2604b702006-01-06 00:20:36 -08003372
NeilBrowndeb200d2012-05-21 09:28:33 +10003373 if (copies < 2 || copies > mddev->raid_disks) {
NeilBrown128595e2010-05-03 14:47:14 +10003374 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
Maciej Trelaf73ea872010-06-16 11:46:29 +01003375 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 goto out;
3377 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11003378
3379 err = -ENOMEM;
NeilBrowne879a872011-10-11 16:49:02 +11003380 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003381 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003383
NeilBrown3ea7daa2012-05-22 13:53:47 +10003384 /* FIXME calc properly */
3385 conf->mirrors = kzalloc(sizeof(struct mirror_info)*(mddev->raid_disks +
3386 max(0,mddev->delta_disks)),
Trela, Maciejdab8b292010-03-08 16:02:45 +11003387 GFP_KERNEL);
3388 if (!conf->mirrors)
3389 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08003390
3391 conf->tmppage = alloc_page(GFP_KERNEL);
3392 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003393 goto out;
3394
NeilBrowndeb200d2012-05-21 09:28:33 +10003395 conf->geo = geo;
3396 conf->copies = copies;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003397 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
3398 r10bio_pool_free, conf);
3399 if (!conf->r10bio_pool)
3400 goto out;
3401
NeilBrown6508fdb2012-05-17 10:08:45 +10003402 calc_sectors(conf, mddev->dev_sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003403 if (mddev->reshape_position == MaxSector) {
3404 conf->prev = conf->geo;
3405 conf->reshape_progress = MaxSector;
3406 } else {
3407 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
3408 err = -EINVAL;
3409 goto out;
3410 }
3411 conf->reshape_progress = mddev->reshape_position;
3412 if (conf->prev.far_offset)
3413 conf->prev.stride = 1 << conf->prev.chunk_shift;
3414 else
3415 /* far_copies must be 1 */
3416 conf->prev.stride = conf->dev_sectors;
3417 }
Neil Browne7e72bf2008-05-14 16:05:54 -07003418 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003419 INIT_LIST_HEAD(&conf->retry_list);
3420
3421 spin_lock_init(&conf->resync_lock);
3422 init_waitqueue_head(&conf->wait_barrier);
3423
3424 conf->thread = md_register_thread(raid10d, mddev, NULL);
3425 if (!conf->thread)
3426 goto out;
3427
Trela, Maciejdab8b292010-03-08 16:02:45 +11003428 conf->mddev = mddev;
3429 return conf;
3430
3431 out:
NeilBrown3ea7daa2012-05-22 13:53:47 +10003432 if (err == -ENOMEM)
3433 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
3434 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003435 if (conf) {
3436 if (conf->r10bio_pool)
3437 mempool_destroy(conf->r10bio_pool);
3438 kfree(conf->mirrors);
3439 safe_put_page(conf->tmppage);
3440 kfree(conf);
3441 }
3442 return ERR_PTR(err);
3443}
3444
NeilBrownfd01b882011-10-11 16:47:53 +11003445static int run(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003446{
NeilBrowne879a872011-10-11 16:49:02 +11003447 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003448 int i, disk_idx, chunk_size;
NeilBrown0f6d02d2011-10-11 16:48:46 +11003449 struct mirror_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11003450 struct md_rdev *rdev;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003451 sector_t size;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003452 sector_t min_offset_diff = 0;
3453 int first = 1;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003454
3455 if (mddev->private == NULL) {
3456 conf = setup_conf(mddev);
3457 if (IS_ERR(conf))
3458 return PTR_ERR(conf);
3459 mddev->private = conf;
3460 }
3461 conf = mddev->private;
3462 if (!conf)
3463 goto out;
3464
Trela, Maciejdab8b292010-03-08 16:02:45 +11003465 mddev->thread = conf->thread;
3466 conf->thread = NULL;
3467
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003468 chunk_size = mddev->chunk_sectors << 9;
3469 blk_queue_io_min(mddev->queue, chunk_size);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003470 if (conf->geo.raid_disks % conf->geo.near_copies)
3471 blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003472 else
3473 blk_queue_io_opt(mddev->queue, chunk_size *
NeilBrown5cf00fc2012-05-21 09:28:20 +10003474 (conf->geo.raid_disks / conf->geo.near_copies));
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003475
NeilBrowndafb20f2012-03-19 12:46:39 +11003476 rdev_for_each(rdev, mddev) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10003477 long long diff;
NeilBrownaba336b2012-05-31 15:39:11 +10003478 struct request_queue *q;
NeilBrown34b343c2011-07-28 11:31:47 +10003479
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 disk_idx = rdev->raid_disk;
NeilBrownf8c9e742012-05-21 09:28:33 +10003481 if (disk_idx < 0)
3482 continue;
3483 if (disk_idx >= conf->geo.raid_disks &&
3484 disk_idx >= conf->prev.raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 continue;
3486 disk = conf->mirrors + disk_idx;
3487
NeilBrown56a25592011-12-23 10:17:55 +11003488 if (test_bit(Replacement, &rdev->flags)) {
3489 if (disk->replacement)
3490 goto out_free_conf;
3491 disk->replacement = rdev;
3492 } else {
3493 if (disk->rdev)
3494 goto out_free_conf;
3495 disk->rdev = rdev;
3496 }
NeilBrownaba336b2012-05-31 15:39:11 +10003497 q = bdev_get_queue(rdev->bdev);
3498 if (q->merge_bvec_fn)
3499 mddev->merge_check_needed = 1;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003500 diff = (rdev->new_data_offset - rdev->data_offset);
3501 if (!mddev->reshape_backwards)
3502 diff = -diff;
3503 if (diff < 0)
3504 diff = 0;
3505 if (first || diff < min_offset_diff)
3506 min_offset_diff = diff;
NeilBrown56a25592011-12-23 10:17:55 +11003507
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003508 disk_stack_limits(mddev->gendisk, rdev->bdev,
3509 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510
3511 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10003513
NeilBrown6d508242005-09-09 16:24:03 -07003514 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10003515 if (!enough(conf, -1)) {
NeilBrown128595e2010-05-03 14:47:14 +10003516 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07003517 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 goto out_free_conf;
3519 }
3520
NeilBrown3ea7daa2012-05-22 13:53:47 +10003521 if (conf->reshape_progress != MaxSector) {
3522 /* must ensure that shape change is supported */
3523 if (conf->geo.far_copies != 1 &&
3524 conf->geo.far_offset == 0)
3525 goto out_free_conf;
3526 if (conf->prev.far_copies != 1 &&
3527 conf->geo.far_offset == 0)
3528 goto out_free_conf;
3529 }
3530
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 mddev->degraded = 0;
NeilBrownf8c9e742012-05-21 09:28:33 +10003532 for (i = 0;
3533 i < conf->geo.raid_disks
3534 || i < conf->prev.raid_disks;
3535 i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
3537 disk = conf->mirrors + i;
3538
NeilBrown56a25592011-12-23 10:17:55 +11003539 if (!disk->rdev && disk->replacement) {
3540 /* The replacement is all we have - use it */
3541 disk->rdev = disk->replacement;
3542 disk->replacement = NULL;
3543 clear_bit(Replacement, &disk->rdev->flags);
3544 }
3545
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003546 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07003547 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 disk->head_position = 0;
3549 mddev->degraded++;
Neil Brown8c2e8702008-06-28 08:30:52 +10003550 if (disk->rdev)
3551 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 }
NeilBrownd890fa22011-10-26 11:54:39 +11003553 disk->recovery_disabled = mddev->recovery_disabled - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 }
3555
Andre Noll8c6ac862009-06-18 08:48:06 +10003556 if (mddev->recovery_cp != MaxSector)
NeilBrown128595e2010-05-03 14:47:14 +10003557 printk(KERN_NOTICE "md/raid10:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10003558 " -- starting background reconstruction\n",
3559 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 printk(KERN_INFO
NeilBrown128595e2010-05-03 14:47:14 +10003561 "md/raid10:%s: active with %d out of %d devices\n",
NeilBrown5cf00fc2012-05-21 09:28:20 +10003562 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
3563 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 /*
3565 * Ok, everything is just fine now
3566 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11003567 mddev->dev_sectors = conf->dev_sectors;
3568 size = raid10_size(mddev, 0, 0);
3569 md_set_array_sectors(mddev, size);
3570 mddev->resync_max_sectors = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
NeilBrown0d129222006-10-03 01:15:54 -07003572 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
3573 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown7a5febe2005-05-16 21:53:16 -07003574
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 /* Calculate max read-ahead size.
3576 * We need to readahead at least twice a whole stripe....
3577 * maybe...
3578 */
3579 {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003580 int stripe = conf->geo.raid_disks *
Andre Noll9d8f0362009-06-18 08:45:01 +10003581 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003582 stripe /= conf->geo.near_copies;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003583 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3584 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 }
3586
NeilBrown050b6612012-03-19 12:46:39 +11003587 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
Martin K. Petersena91a2782011-03-17 11:11:05 +01003588
3589 if (md_integrity_register(mddev))
3590 goto out_free_conf;
3591
NeilBrown3ea7daa2012-05-22 13:53:47 +10003592 if (conf->reshape_progress != MaxSector) {
3593 unsigned long before_length, after_length;
3594
3595 before_length = ((1 << conf->prev.chunk_shift) *
3596 conf->prev.far_copies);
3597 after_length = ((1 << conf->geo.chunk_shift) *
3598 conf->geo.far_copies);
3599
3600 if (max(before_length, after_length) > min_offset_diff) {
3601 /* This cannot work */
3602 printk("md/raid10: offset difference not enough to continue reshape\n");
3603 goto out_free_conf;
3604 }
3605 conf->offset_diff = min_offset_diff;
3606
3607 conf->reshape_safe = conf->reshape_progress;
3608 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3609 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3610 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3611 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3612 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3613 "reshape");
3614 }
3615
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 return 0;
3617
3618out_free_conf:
NeilBrown01f96c02011-09-21 15:30:20 +10003619 md_unregister_thread(&mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 if (conf->r10bio_pool)
3621 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08003622 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003623 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 kfree(conf);
3625 mddev->private = NULL;
3626out:
3627 return -EIO;
3628}
3629
NeilBrownfd01b882011-10-11 16:47:53 +11003630static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631{
NeilBrowne879a872011-10-11 16:49:02 +11003632 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
NeilBrown409c57f2009-03-31 14:39:39 +11003634 raise_barrier(conf, 0);
3635 lower_barrier(conf);
3636
NeilBrown01f96c02011-09-21 15:30:20 +10003637 md_unregister_thread(&mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
3639 if (conf->r10bio_pool)
3640 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003641 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 kfree(conf);
3643 mddev->private = NULL;
3644 return 0;
3645}
3646
NeilBrownfd01b882011-10-11 16:47:53 +11003647static void raid10_quiesce(struct mddev *mddev, int state)
NeilBrown6cce3b232006-01-06 00:20:16 -08003648{
NeilBrowne879a872011-10-11 16:49:02 +11003649 struct r10conf *conf = mddev->private;
NeilBrown6cce3b232006-01-06 00:20:16 -08003650
3651 switch(state) {
3652 case 1:
3653 raise_barrier(conf, 0);
3654 break;
3655 case 0:
3656 lower_barrier(conf);
3657 break;
3658 }
NeilBrown6cce3b232006-01-06 00:20:16 -08003659}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
NeilBrown006a09a2012-03-19 12:46:40 +11003661static int raid10_resize(struct mddev *mddev, sector_t sectors)
3662{
3663 /* Resize of 'far' arrays is not supported.
3664 * For 'near' and 'offset' arrays we can set the
3665 * number of sectors used to be an appropriate multiple
3666 * of the chunk size.
3667 * For 'offset', this is far_copies*chunksize.
3668 * For 'near' the multiplier is the LCM of
3669 * near_copies and raid_disks.
3670 * So if far_copies > 1 && !far_offset, fail.
3671 * Else find LCM(raid_disks, near_copy)*far_copies and
3672 * multiply by chunk_size. Then round to this number.
3673 * This is mostly done by raid10_size()
3674 */
3675 struct r10conf *conf = mddev->private;
3676 sector_t oldsize, size;
3677
NeilBrownf8c9e742012-05-21 09:28:33 +10003678 if (mddev->reshape_position != MaxSector)
3679 return -EBUSY;
3680
NeilBrown5cf00fc2012-05-21 09:28:20 +10003681 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
NeilBrown006a09a2012-03-19 12:46:40 +11003682 return -EINVAL;
3683
3684 oldsize = raid10_size(mddev, 0, 0);
3685 size = raid10_size(mddev, sectors, 0);
NeilBrowna4a61252012-05-22 13:55:27 +10003686 if (mddev->external_size &&
3687 mddev->array_sectors > size)
NeilBrown006a09a2012-03-19 12:46:40 +11003688 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10003689 if (mddev->bitmap) {
3690 int ret = bitmap_resize(mddev->bitmap, size, 0, 0);
3691 if (ret)
3692 return ret;
3693 }
3694 md_set_array_sectors(mddev, size);
NeilBrown006a09a2012-03-19 12:46:40 +11003695 set_capacity(mddev->gendisk, mddev->array_sectors);
3696 revalidate_disk(mddev->gendisk);
3697 if (sectors > mddev->dev_sectors &&
3698 mddev->recovery_cp > oldsize) {
3699 mddev->recovery_cp = oldsize;
3700 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3701 }
NeilBrown6508fdb2012-05-17 10:08:45 +10003702 calc_sectors(conf, sectors);
3703 mddev->dev_sectors = conf->dev_sectors;
NeilBrown006a09a2012-03-19 12:46:40 +11003704 mddev->resync_max_sectors = size;
3705 return 0;
3706}
3707
NeilBrownfd01b882011-10-11 16:47:53 +11003708static void *raid10_takeover_raid0(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003709{
NeilBrown3cb03002011-10-11 16:45:26 +11003710 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +11003711 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003712
3713 if (mddev->degraded > 0) {
NeilBrown128595e2010-05-03 14:47:14 +10003714 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
3715 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003716 return ERR_PTR(-EINVAL);
3717 }
3718
Trela, Maciejdab8b292010-03-08 16:02:45 +11003719 /* Set new parameters */
3720 mddev->new_level = 10;
3721 /* new layout: far_copies = 1, near_copies = 2 */
3722 mddev->new_layout = (1<<8) + 2;
3723 mddev->new_chunk_sectors = mddev->chunk_sectors;
3724 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003725 mddev->raid_disks *= 2;
3726 /* make sure it will be not marked as dirty */
3727 mddev->recovery_cp = MaxSector;
3728
3729 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003730 if (!IS_ERR(conf)) {
NeilBrowndafb20f2012-03-19 12:46:39 +11003731 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003732 if (rdev->raid_disk >= 0)
3733 rdev->new_raid_disk = rdev->raid_disk * 2;
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003734 conf->barrier = 1;
3735 }
3736
Trela, Maciejdab8b292010-03-08 16:02:45 +11003737 return conf;
3738}
3739
NeilBrownfd01b882011-10-11 16:47:53 +11003740static void *raid10_takeover(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003741{
NeilBrowne373ab12011-10-11 16:48:59 +11003742 struct r0conf *raid0_conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003743
3744 /* raid10 can take over:
3745 * raid0 - providing it has only two drives
3746 */
3747 if (mddev->level == 0) {
3748 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11003749 raid0_conf = mddev->private;
3750 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown128595e2010-05-03 14:47:14 +10003751 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
3752 " with more than one zone.\n",
3753 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003754 return ERR_PTR(-EINVAL);
3755 }
3756 return raid10_takeover_raid0(mddev);
3757 }
3758 return ERR_PTR(-EINVAL);
3759}
3760
NeilBrown3ea7daa2012-05-22 13:53:47 +10003761static int raid10_check_reshape(struct mddev *mddev)
3762{
3763 /* Called when there is a request to change
3764 * - layout (to ->new_layout)
3765 * - chunk size (to ->new_chunk_sectors)
3766 * - raid_disks (by delta_disks)
3767 * or when trying to restart a reshape that was ongoing.
3768 *
3769 * We need to validate the request and possibly allocate
3770 * space if that might be an issue later.
3771 *
3772 * Currently we reject any reshape of a 'far' mode array,
3773 * allow chunk size to change if new is generally acceptable,
3774 * allow raid_disks to increase, and allow
3775 * a switch between 'near' mode and 'offset' mode.
3776 */
3777 struct r10conf *conf = mddev->private;
3778 struct geom geo;
3779
3780 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
3781 return -EINVAL;
3782
3783 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
3784 /* mustn't change number of copies */
3785 return -EINVAL;
3786 if (geo.far_copies > 1 && !geo.far_offset)
3787 /* Cannot switch to 'far' mode */
3788 return -EINVAL;
3789
3790 if (mddev->array_sectors & geo.chunk_mask)
3791 /* not factor of array size */
3792 return -EINVAL;
3793
NeilBrown3ea7daa2012-05-22 13:53:47 +10003794 if (!enough(conf, -1))
3795 return -EINVAL;
3796
3797 kfree(conf->mirrors_new);
3798 conf->mirrors_new = NULL;
3799 if (mddev->delta_disks > 0) {
3800 /* allocate new 'mirrors' list */
3801 conf->mirrors_new = kzalloc(
3802 sizeof(struct mirror_info)
3803 *(mddev->raid_disks +
3804 mddev->delta_disks),
3805 GFP_KERNEL);
3806 if (!conf->mirrors_new)
3807 return -ENOMEM;
3808 }
3809 return 0;
3810}
3811
3812/*
3813 * Need to check if array has failed when deciding whether to:
3814 * - start an array
3815 * - remove non-faulty devices
3816 * - add a spare
3817 * - allow a reshape
3818 * This determination is simple when no reshape is happening.
3819 * However if there is a reshape, we need to carefully check
3820 * both the before and after sections.
3821 * This is because some failed devices may only affect one
3822 * of the two sections, and some non-in_sync devices may
3823 * be insync in the section most affected by failed devices.
3824 */
3825static int calc_degraded(struct r10conf *conf)
3826{
3827 int degraded, degraded2;
3828 int i;
3829
3830 rcu_read_lock();
3831 degraded = 0;
3832 /* 'prev' section first */
3833 for (i = 0; i < conf->prev.raid_disks; i++) {
3834 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
3835 if (!rdev || test_bit(Faulty, &rdev->flags))
3836 degraded++;
3837 else if (!test_bit(In_sync, &rdev->flags))
3838 /* When we can reduce the number of devices in
3839 * an array, this might not contribute to
3840 * 'degraded'. It does now.
3841 */
3842 degraded++;
3843 }
3844 rcu_read_unlock();
3845 if (conf->geo.raid_disks == conf->prev.raid_disks)
3846 return degraded;
3847 rcu_read_lock();
3848 degraded2 = 0;
3849 for (i = 0; i < conf->geo.raid_disks; i++) {
3850 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
3851 if (!rdev || test_bit(Faulty, &rdev->flags))
3852 degraded2++;
3853 else if (!test_bit(In_sync, &rdev->flags)) {
3854 /* If reshape is increasing the number of devices,
3855 * this section has already been recovered, so
3856 * it doesn't contribute to degraded.
3857 * else it does.
3858 */
3859 if (conf->geo.raid_disks <= conf->prev.raid_disks)
3860 degraded2++;
3861 }
3862 }
3863 rcu_read_unlock();
3864 if (degraded2 > degraded)
3865 return degraded2;
3866 return degraded;
3867}
3868
3869static int raid10_start_reshape(struct mddev *mddev)
3870{
3871 /* A 'reshape' has been requested. This commits
3872 * the various 'new' fields and sets MD_RECOVER_RESHAPE
3873 * This also checks if there are enough spares and adds them
3874 * to the array.
3875 * We currently require enough spares to make the final
3876 * array non-degraded. We also require that the difference
3877 * between old and new data_offset - on each device - is
3878 * enough that we never risk over-writing.
3879 */
3880
3881 unsigned long before_length, after_length;
3882 sector_t min_offset_diff = 0;
3883 int first = 1;
3884 struct geom new;
3885 struct r10conf *conf = mddev->private;
3886 struct md_rdev *rdev;
3887 int spares = 0;
NeilBrownbb63a702012-05-22 13:55:28 +10003888 int ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003889
3890 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3891 return -EBUSY;
3892
3893 if (setup_geo(&new, mddev, geo_start) != conf->copies)
3894 return -EINVAL;
3895
3896 before_length = ((1 << conf->prev.chunk_shift) *
3897 conf->prev.far_copies);
3898 after_length = ((1 << conf->geo.chunk_shift) *
3899 conf->geo.far_copies);
3900
3901 rdev_for_each(rdev, mddev) {
3902 if (!test_bit(In_sync, &rdev->flags)
3903 && !test_bit(Faulty, &rdev->flags))
3904 spares++;
3905 if (rdev->raid_disk >= 0) {
3906 long long diff = (rdev->new_data_offset
3907 - rdev->data_offset);
3908 if (!mddev->reshape_backwards)
3909 diff = -diff;
3910 if (diff < 0)
3911 diff = 0;
3912 if (first || diff < min_offset_diff)
3913 min_offset_diff = diff;
3914 }
3915 }
3916
3917 if (max(before_length, after_length) > min_offset_diff)
3918 return -EINVAL;
3919
3920 if (spares < mddev->delta_disks)
3921 return -EINVAL;
3922
3923 conf->offset_diff = min_offset_diff;
3924 spin_lock_irq(&conf->device_lock);
3925 if (conf->mirrors_new) {
3926 memcpy(conf->mirrors_new, conf->mirrors,
3927 sizeof(struct mirror_info)*conf->prev.raid_disks);
3928 smp_mb();
3929 kfree(conf->mirrors_old); /* FIXME and elsewhere */
3930 conf->mirrors_old = conf->mirrors;
3931 conf->mirrors = conf->mirrors_new;
3932 conf->mirrors_new = NULL;
3933 }
3934 setup_geo(&conf->geo, mddev, geo_start);
3935 smp_mb();
3936 if (mddev->reshape_backwards) {
3937 sector_t size = raid10_size(mddev, 0, 0);
3938 if (size < mddev->array_sectors) {
3939 spin_unlock_irq(&conf->device_lock);
3940 printk(KERN_ERR "md/raid10:%s: array size must be reduce before number of disks\n",
3941 mdname(mddev));
3942 return -EINVAL;
3943 }
3944 mddev->resync_max_sectors = size;
3945 conf->reshape_progress = size;
3946 } else
3947 conf->reshape_progress = 0;
3948 spin_unlock_irq(&conf->device_lock);
3949
NeilBrownbb63a702012-05-22 13:55:28 +10003950 if (mddev->delta_disks && mddev->bitmap) {
3951 ret = bitmap_resize(mddev->bitmap,
3952 raid10_size(mddev, 0,
3953 conf->geo.raid_disks),
3954 0, 0);
3955 if (ret)
3956 goto abort;
3957 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10003958 if (mddev->delta_disks > 0) {
3959 rdev_for_each(rdev, mddev)
3960 if (rdev->raid_disk < 0 &&
3961 !test_bit(Faulty, &rdev->flags)) {
3962 if (raid10_add_disk(mddev, rdev) == 0) {
3963 if (rdev->raid_disk >=
3964 conf->prev.raid_disks)
3965 set_bit(In_sync, &rdev->flags);
3966 else
3967 rdev->recovery_offset = 0;
3968
3969 if (sysfs_link_rdev(mddev, rdev))
3970 /* Failure here is OK */;
3971 }
3972 } else if (rdev->raid_disk >= conf->prev.raid_disks
3973 && !test_bit(Faulty, &rdev->flags)) {
3974 /* This is a spare that was manually added */
3975 set_bit(In_sync, &rdev->flags);
3976 }
3977 }
3978 /* When a reshape changes the number of devices,
3979 * ->degraded is measured against the larger of the
3980 * pre and post numbers.
3981 */
3982 spin_lock_irq(&conf->device_lock);
3983 mddev->degraded = calc_degraded(conf);
3984 spin_unlock_irq(&conf->device_lock);
3985 mddev->raid_disks = conf->geo.raid_disks;
3986 mddev->reshape_position = conf->reshape_progress;
3987 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3988
3989 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3990 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3991 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3992 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3993
3994 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3995 "reshape");
3996 if (!mddev->sync_thread) {
NeilBrownbb63a702012-05-22 13:55:28 +10003997 ret = -EAGAIN;
3998 goto abort;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003999 }
4000 conf->reshape_checkpoint = jiffies;
4001 md_wakeup_thread(mddev->sync_thread);
4002 md_new_event(mddev);
4003 return 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004004
4005abort:
4006 mddev->recovery = 0;
4007 spin_lock_irq(&conf->device_lock);
4008 conf->geo = conf->prev;
4009 mddev->raid_disks = conf->geo.raid_disks;
4010 rdev_for_each(rdev, mddev)
4011 rdev->new_data_offset = rdev->data_offset;
4012 smp_wmb();
4013 conf->reshape_progress = MaxSector;
4014 mddev->reshape_position = MaxSector;
4015 spin_unlock_irq(&conf->device_lock);
4016 return ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004017}
4018
4019/* Calculate the last device-address that could contain
4020 * any block from the chunk that includes the array-address 's'
4021 * and report the next address.
4022 * i.e. the address returned will be chunk-aligned and after
4023 * any data that is in the chunk containing 's'.
4024 */
4025static sector_t last_dev_address(sector_t s, struct geom *geo)
4026{
4027 s = (s | geo->chunk_mask) + 1;
4028 s >>= geo->chunk_shift;
4029 s *= geo->near_copies;
4030 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4031 s *= geo->far_copies;
4032 s <<= geo->chunk_shift;
4033 return s;
4034}
4035
4036/* Calculate the first device-address that could contain
4037 * any block from the chunk that includes the array-address 's'.
4038 * This too will be the start of a chunk
4039 */
4040static sector_t first_dev_address(sector_t s, struct geom *geo)
4041{
4042 s >>= geo->chunk_shift;
4043 s *= geo->near_copies;
4044 sector_div(s, geo->raid_disks);
4045 s *= geo->far_copies;
4046 s <<= geo->chunk_shift;
4047 return s;
4048}
4049
4050static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4051 int *skipped)
4052{
4053 /* We simply copy at most one chunk (smallest of old and new)
4054 * at a time, possibly less if that exceeds RESYNC_PAGES,
4055 * or we hit a bad block or something.
4056 * This might mean we pause for normal IO in the middle of
4057 * a chunk, but that is not a problem was mddev->reshape_position
4058 * can record any location.
4059 *
4060 * If we will want to write to a location that isn't
4061 * yet recorded as 'safe' (i.e. in metadata on disk) then
4062 * we need to flush all reshape requests and update the metadata.
4063 *
4064 * When reshaping forwards (e.g. to more devices), we interpret
4065 * 'safe' as the earliest block which might not have been copied
4066 * down yet. We divide this by previous stripe size and multiply
4067 * by previous stripe length to get lowest device offset that we
4068 * cannot write to yet.
4069 * We interpret 'sector_nr' as an address that we want to write to.
4070 * From this we use last_device_address() to find where we might
4071 * write to, and first_device_address on the 'safe' position.
4072 * If this 'next' write position is after the 'safe' position,
4073 * we must update the metadata to increase the 'safe' position.
4074 *
4075 * When reshaping backwards, we round in the opposite direction
4076 * and perform the reverse test: next write position must not be
4077 * less than current safe position.
4078 *
4079 * In all this the minimum difference in data offsets
4080 * (conf->offset_diff - always positive) allows a bit of slack,
4081 * so next can be after 'safe', but not by more than offset_disk
4082 *
4083 * We need to prepare all the bios here before we start any IO
4084 * to ensure the size we choose is acceptable to all devices.
4085 * The means one for each copy for write-out and an extra one for
4086 * read-in.
4087 * We store the read-in bio in ->master_bio and the others in
4088 * ->devs[x].bio and ->devs[x].repl_bio.
4089 */
4090 struct r10conf *conf = mddev->private;
4091 struct r10bio *r10_bio;
4092 sector_t next, safe, last;
4093 int max_sectors;
4094 int nr_sectors;
4095 int s;
4096 struct md_rdev *rdev;
4097 int need_flush = 0;
4098 struct bio *blist;
4099 struct bio *bio, *read_bio;
4100 int sectors_done = 0;
4101
4102 if (sector_nr == 0) {
4103 /* If restarting in the middle, skip the initial sectors */
4104 if (mddev->reshape_backwards &&
4105 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4106 sector_nr = (raid10_size(mddev, 0, 0)
4107 - conf->reshape_progress);
4108 } else if (!mddev->reshape_backwards &&
4109 conf->reshape_progress > 0)
4110 sector_nr = conf->reshape_progress;
4111 if (sector_nr) {
4112 mddev->curr_resync_completed = sector_nr;
4113 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4114 *skipped = 1;
4115 return sector_nr;
4116 }
4117 }
4118
4119 /* We don't use sector_nr to track where we are up to
4120 * as that doesn't work well for ->reshape_backwards.
4121 * So just use ->reshape_progress.
4122 */
4123 if (mddev->reshape_backwards) {
4124 /* 'next' is the earliest device address that we might
4125 * write to for this chunk in the new layout
4126 */
4127 next = first_dev_address(conf->reshape_progress - 1,
4128 &conf->geo);
4129
4130 /* 'safe' is the last device address that we might read from
4131 * in the old layout after a restart
4132 */
4133 safe = last_dev_address(conf->reshape_safe - 1,
4134 &conf->prev);
4135
4136 if (next + conf->offset_diff < safe)
4137 need_flush = 1;
4138
4139 last = conf->reshape_progress - 1;
4140 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4141 & conf->prev.chunk_mask);
4142 if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
4143 sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
4144 } else {
4145 /* 'next' is after the last device address that we
4146 * might write to for this chunk in the new layout
4147 */
4148 next = last_dev_address(conf->reshape_progress, &conf->geo);
4149
4150 /* 'safe' is the earliest device address that we might
4151 * read from in the old layout after a restart
4152 */
4153 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4154
4155 /* Need to update metadata if 'next' might be beyond 'safe'
4156 * as that would possibly corrupt data
4157 */
4158 if (next > safe + conf->offset_diff)
4159 need_flush = 1;
4160
4161 sector_nr = conf->reshape_progress;
4162 last = sector_nr | (conf->geo.chunk_mask
4163 & conf->prev.chunk_mask);
4164
4165 if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
4166 last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
4167 }
4168
4169 if (need_flush ||
4170 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4171 /* Need to update reshape_position in metadata */
4172 wait_barrier(conf);
4173 mddev->reshape_position = conf->reshape_progress;
4174 if (mddev->reshape_backwards)
4175 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4176 - conf->reshape_progress;
4177 else
4178 mddev->curr_resync_completed = conf->reshape_progress;
4179 conf->reshape_checkpoint = jiffies;
4180 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4181 md_wakeup_thread(mddev->thread);
4182 wait_event(mddev->sb_wait, mddev->flags == 0 ||
4183 kthread_should_stop());
4184 conf->reshape_safe = mddev->reshape_position;
4185 allow_barrier(conf);
4186 }
4187
4188read_more:
4189 /* Now schedule reads for blocks from sector_nr to last */
4190 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
4191 raise_barrier(conf, sectors_done != 0);
4192 atomic_set(&r10_bio->remaining, 0);
4193 r10_bio->mddev = mddev;
4194 r10_bio->sector = sector_nr;
4195 set_bit(R10BIO_IsReshape, &r10_bio->state);
4196 r10_bio->sectors = last - sector_nr + 1;
4197 rdev = read_balance(conf, r10_bio, &max_sectors);
4198 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4199
4200 if (!rdev) {
4201 /* Cannot read from here, so need to record bad blocks
4202 * on all the target devices.
4203 */
4204 // FIXME
4205 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4206 return sectors_done;
4207 }
4208
4209 read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
4210
4211 read_bio->bi_bdev = rdev->bdev;
4212 read_bio->bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
4213 + rdev->data_offset);
4214 read_bio->bi_private = r10_bio;
4215 read_bio->bi_end_io = end_sync_read;
4216 read_bio->bi_rw = READ;
4217 read_bio->bi_flags &= ~(BIO_POOL_MASK - 1);
4218 read_bio->bi_flags |= 1 << BIO_UPTODATE;
4219 read_bio->bi_vcnt = 0;
4220 read_bio->bi_idx = 0;
4221 read_bio->bi_size = 0;
4222 r10_bio->master_bio = read_bio;
4223 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4224
4225 /* Now find the locations in the new layout */
4226 __raid10_find_phys(&conf->geo, r10_bio);
4227
4228 blist = read_bio;
4229 read_bio->bi_next = NULL;
4230
4231 for (s = 0; s < conf->copies*2; s++) {
4232 struct bio *b;
4233 int d = r10_bio->devs[s/2].devnum;
4234 struct md_rdev *rdev2;
4235 if (s&1) {
4236 rdev2 = conf->mirrors[d].replacement;
4237 b = r10_bio->devs[s/2].repl_bio;
4238 } else {
4239 rdev2 = conf->mirrors[d].rdev;
4240 b = r10_bio->devs[s/2].bio;
4241 }
4242 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4243 continue;
4244 b->bi_bdev = rdev2->bdev;
4245 b->bi_sector = r10_bio->devs[s/2].addr + rdev2->new_data_offset;
4246 b->bi_private = r10_bio;
4247 b->bi_end_io = end_reshape_write;
4248 b->bi_rw = WRITE;
4249 b->bi_flags &= ~(BIO_POOL_MASK - 1);
4250 b->bi_flags |= 1 << BIO_UPTODATE;
4251 b->bi_next = blist;
4252 b->bi_vcnt = 0;
4253 b->bi_idx = 0;
4254 b->bi_size = 0;
4255 blist = b;
4256 }
4257
4258 /* Now add as many pages as possible to all of these bios. */
4259
4260 nr_sectors = 0;
4261 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
4262 struct page *page = r10_bio->devs[0].bio->bi_io_vec[s/(PAGE_SIZE>>9)].bv_page;
4263 int len = (max_sectors - s) << 9;
4264 if (len > PAGE_SIZE)
4265 len = PAGE_SIZE;
4266 for (bio = blist; bio ; bio = bio->bi_next) {
4267 struct bio *bio2;
4268 if (bio_add_page(bio, page, len, 0))
4269 continue;
4270
4271 /* Didn't fit, must stop */
4272 for (bio2 = blist;
4273 bio2 && bio2 != bio;
4274 bio2 = bio2->bi_next) {
4275 /* Remove last page from this bio */
4276 bio2->bi_vcnt--;
4277 bio2->bi_size -= len;
4278 bio2->bi_flags &= ~(1<<BIO_SEG_VALID);
4279 }
4280 goto bio_full;
4281 }
4282 sector_nr += len >> 9;
4283 nr_sectors += len >> 9;
4284 }
4285bio_full:
4286 r10_bio->sectors = nr_sectors;
4287
4288 /* Now submit the read */
4289 md_sync_acct(read_bio->bi_bdev, r10_bio->sectors);
4290 atomic_inc(&r10_bio->remaining);
4291 read_bio->bi_next = NULL;
4292 generic_make_request(read_bio);
4293 sector_nr += nr_sectors;
4294 sectors_done += nr_sectors;
4295 if (sector_nr <= last)
4296 goto read_more;
4297
4298 /* Now that we have done the whole section we can
4299 * update reshape_progress
4300 */
4301 if (mddev->reshape_backwards)
4302 conf->reshape_progress -= sectors_done;
4303 else
4304 conf->reshape_progress += sectors_done;
4305
4306 return sectors_done;
4307}
4308
4309static void end_reshape_request(struct r10bio *r10_bio);
4310static int handle_reshape_read_error(struct mddev *mddev,
4311 struct r10bio *r10_bio);
4312static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
4313{
4314 /* Reshape read completed. Hopefully we have a block
4315 * to write out.
4316 * If we got a read error then we do sync 1-page reads from
4317 * elsewhere until we find the data - or give up.
4318 */
4319 struct r10conf *conf = mddev->private;
4320 int s;
4321
4322 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
4323 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
4324 /* Reshape has been aborted */
4325 md_done_sync(mddev, r10_bio->sectors, 0);
4326 return;
4327 }
4328
4329 /* We definitely have the data in the pages, schedule the
4330 * writes.
4331 */
4332 atomic_set(&r10_bio->remaining, 1);
4333 for (s = 0; s < conf->copies*2; s++) {
4334 struct bio *b;
4335 int d = r10_bio->devs[s/2].devnum;
4336 struct md_rdev *rdev;
4337 if (s&1) {
4338 rdev = conf->mirrors[d].replacement;
4339 b = r10_bio->devs[s/2].repl_bio;
4340 } else {
4341 rdev = conf->mirrors[d].rdev;
4342 b = r10_bio->devs[s/2].bio;
4343 }
4344 if (!rdev || test_bit(Faulty, &rdev->flags))
4345 continue;
4346 atomic_inc(&rdev->nr_pending);
4347 md_sync_acct(b->bi_bdev, r10_bio->sectors);
4348 atomic_inc(&r10_bio->remaining);
4349 b->bi_next = NULL;
4350 generic_make_request(b);
4351 }
4352 end_reshape_request(r10_bio);
4353}
4354
4355static void end_reshape(struct r10conf *conf)
4356{
4357 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
4358 return;
4359
4360 spin_lock_irq(&conf->device_lock);
4361 conf->prev = conf->geo;
4362 md_finish_reshape(conf->mddev);
4363 smp_wmb();
4364 conf->reshape_progress = MaxSector;
4365 spin_unlock_irq(&conf->device_lock);
4366
4367 /* read-ahead size must cover two whole stripes, which is
4368 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4369 */
4370 if (conf->mddev->queue) {
4371 int stripe = conf->geo.raid_disks *
4372 ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
4373 stripe /= conf->geo.near_copies;
4374 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4375 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4376 }
4377 conf->fullsync = 0;
4378}
4379
4380
4381static int handle_reshape_read_error(struct mddev *mddev,
4382 struct r10bio *r10_bio)
4383{
4384 /* Use sync reads to get the blocks from somewhere else */
4385 int sectors = r10_bio->sectors;
4386 struct r10bio r10b;
4387 struct r10conf *conf = mddev->private;
4388 int slot = 0;
4389 int idx = 0;
4390 struct bio_vec *bvec = r10_bio->master_bio->bi_io_vec;
4391
4392 r10b.sector = r10_bio->sector;
4393 __raid10_find_phys(&conf->prev, &r10b);
4394
4395 while (sectors) {
4396 int s = sectors;
4397 int success = 0;
4398 int first_slot = slot;
4399
4400 if (s > (PAGE_SIZE >> 9))
4401 s = PAGE_SIZE >> 9;
4402
4403 while (!success) {
4404 int d = r10b.devs[slot].devnum;
4405 struct md_rdev *rdev = conf->mirrors[d].rdev;
4406 sector_t addr;
4407 if (rdev == NULL ||
4408 test_bit(Faulty, &rdev->flags) ||
4409 !test_bit(In_sync, &rdev->flags))
4410 goto failed;
4411
4412 addr = r10b.devs[slot].addr + idx * PAGE_SIZE;
4413 success = sync_page_io(rdev,
4414 addr,
4415 s << 9,
4416 bvec[idx].bv_page,
4417 READ, false);
4418 if (success)
4419 break;
4420 failed:
4421 slot++;
4422 if (slot >= conf->copies)
4423 slot = 0;
4424 if (slot == first_slot)
4425 break;
4426 }
4427 if (!success) {
4428 /* couldn't read this block, must give up */
4429 set_bit(MD_RECOVERY_INTR,
4430 &mddev->recovery);
4431 return -EIO;
4432 }
4433 sectors -= s;
4434 idx++;
4435 }
4436 return 0;
4437}
4438
4439static void end_reshape_write(struct bio *bio, int error)
4440{
4441 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
4442 struct r10bio *r10_bio = bio->bi_private;
4443 struct mddev *mddev = r10_bio->mddev;
4444 struct r10conf *conf = mddev->private;
4445 int d;
4446 int slot;
4447 int repl;
4448 struct md_rdev *rdev = NULL;
4449
4450 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
4451 if (repl)
4452 rdev = conf->mirrors[d].replacement;
4453 if (!rdev) {
4454 smp_mb();
4455 rdev = conf->mirrors[d].rdev;
4456 }
4457
4458 if (!uptodate) {
4459 /* FIXME should record badblock */
4460 md_error(mddev, rdev);
4461 }
4462
4463 rdev_dec_pending(rdev, mddev);
4464 end_reshape_request(r10_bio);
4465}
4466
4467static void end_reshape_request(struct r10bio *r10_bio)
4468{
4469 if (!atomic_dec_and_test(&r10_bio->remaining))
4470 return;
4471 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
4472 bio_put(r10_bio->master_bio);
4473 put_buf(r10_bio);
4474}
4475
4476static void raid10_finish_reshape(struct mddev *mddev)
4477{
4478 struct r10conf *conf = mddev->private;
4479
4480 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4481 return;
4482
4483 if (mddev->delta_disks > 0) {
4484 sector_t size = raid10_size(mddev, 0, 0);
4485 md_set_array_sectors(mddev, size);
4486 if (mddev->recovery_cp > mddev->resync_max_sectors) {
4487 mddev->recovery_cp = mddev->resync_max_sectors;
4488 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4489 }
4490 mddev->resync_max_sectors = size;
4491 set_capacity(mddev->gendisk, mddev->array_sectors);
4492 revalidate_disk(mddev->gendisk);
NeilBrown63aced62012-05-22 13:55:33 +10004493 } else {
4494 int d;
4495 for (d = conf->geo.raid_disks ;
4496 d < conf->geo.raid_disks - mddev->delta_disks;
4497 d++) {
4498 struct md_rdev *rdev = conf->mirrors[d].rdev;
4499 if (rdev)
4500 clear_bit(In_sync, &rdev->flags);
4501 rdev = conf->mirrors[d].replacement;
4502 if (rdev)
4503 clear_bit(In_sync, &rdev->flags);
4504 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004505 }
4506 mddev->layout = mddev->new_layout;
4507 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
4508 mddev->reshape_position = MaxSector;
4509 mddev->delta_disks = 0;
4510 mddev->reshape_backwards = 0;
4511}
4512
NeilBrown84fc4b52011-10-11 16:49:58 +11004513static struct md_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514{
4515 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08004516 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 .owner = THIS_MODULE,
4518 .make_request = make_request,
4519 .run = run,
4520 .stop = stop,
4521 .status = status,
4522 .error_handler = error,
4523 .hot_add_disk = raid10_add_disk,
4524 .hot_remove_disk= raid10_remove_disk,
4525 .spare_active = raid10_spare_active,
4526 .sync_request = sync_request,
NeilBrown6cce3b232006-01-06 00:20:16 -08004527 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07004528 .size = raid10_size,
NeilBrown006a09a2012-03-19 12:46:40 +11004529 .resize = raid10_resize,
Trela, Maciejdab8b292010-03-08 16:02:45 +11004530 .takeover = raid10_takeover,
NeilBrown3ea7daa2012-05-22 13:53:47 +10004531 .check_reshape = raid10_check_reshape,
4532 .start_reshape = raid10_start_reshape,
4533 .finish_reshape = raid10_finish_reshape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534};
4535
4536static int __init raid_init(void)
4537{
NeilBrown2604b702006-01-06 00:20:36 -08004538 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539}
4540
4541static void raid_exit(void)
4542{
NeilBrown2604b702006-01-06 00:20:36 -08004543 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544}
4545
4546module_init(raid_init);
4547module_exit(raid_exit);
4548MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11004549MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08004551MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08004552MODULE_ALIAS("md-level-10");
NeilBrown34db0cd2011-10-11 16:50:01 +11004553
4554module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);