blob: ec271ae4318faf6f15c8dd3402572168c2dfd308 [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);
NeilBrownfae8cc5e2012-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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 j = -1;
177out_free_bio:
NeilBrown69335ef2011-12-23 10:17:54 +1100178 while (++j < nalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 bio_put(r10_bio->devs[j].bio);
NeilBrown69335ef2011-12-23 10:17:54 +1100180 if (r10_bio->devs[j].repl_bio)
181 bio_put(r10_bio->devs[j].repl_bio);
182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 r10bio_pool_free(r10_bio, conf);
184 return NULL;
185}
186
187static void r10buf_pool_free(void *__r10_bio, void *data)
188{
189 int i;
NeilBrowne879a872011-10-11 16:49:02 +1100190 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100191 struct r10bio *r10bio = __r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 int j;
193
194 for (j=0; j < conf->copies; j++) {
195 struct bio *bio = r10bio->devs[j].bio;
196 if (bio) {
197 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown1345b1d2006-01-06 00:20:40 -0800198 safe_put_page(bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 bio->bi_io_vec[i].bv_page = NULL;
200 }
201 bio_put(bio);
202 }
NeilBrown69335ef2011-12-23 10:17:54 +1100203 bio = r10bio->devs[j].repl_bio;
204 if (bio)
205 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207 r10bio_pool_free(r10bio, conf);
208}
209
NeilBrowne879a872011-10-11 16:49:02 +1100210static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 int i;
213
214 for (i = 0; i < conf->copies; i++) {
215 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown749c55e2011-07-28 11:39:24 +1000216 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 bio_put(*bio);
218 *bio = NULL;
NeilBrown69335ef2011-12-23 10:17:54 +1100219 bio = &r10_bio->devs[i].repl_bio;
220 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
221 bio_put(*bio);
222 *bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224}
225
NeilBrown9f2c9d12011-10-11 16:48:43 +1100226static void free_r10bio(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
NeilBrowne879a872011-10-11 16:49:02 +1100228 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 put_all_bios(conf, r10_bio);
231 mempool_free(r10_bio, conf->r10bio_pool);
232}
233
NeilBrown9f2c9d12011-10-11 16:48:43 +1100234static void put_buf(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
NeilBrowne879a872011-10-11 16:49:02 +1100236 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 mempool_free(r10_bio, conf->r10buf_pool);
239
NeilBrown0a27ec92006-01-06 00:20:13 -0800240 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
NeilBrown9f2c9d12011-10-11 16:48:43 +1100243static void reschedule_retry(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +1100246 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +1100247 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 spin_lock_irqsave(&conf->device_lock, flags);
250 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800251 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 spin_unlock_irqrestore(&conf->device_lock, flags);
253
Arthur Jones388667b2008-07-25 12:03:38 -0700254 /* wake up frozen array... */
255 wake_up(&conf->wait_barrier);
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 md_wakeup_thread(mddev->thread);
258}
259
260/*
261 * raid_end_bio_io() is called when we have finished servicing a mirrored
262 * operation and are ready to return a success/failure code to the buffer
263 * cache layer.
264 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100265static void raid_end_bio_io(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 struct bio *bio = r10_bio->master_bio;
NeilBrown856e08e2011-07-28 11:39:23 +1000268 int done;
NeilBrowne879a872011-10-11 16:49:02 +1100269 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
NeilBrown856e08e2011-07-28 11:39:23 +1000271 if (bio->bi_phys_segments) {
272 unsigned long flags;
273 spin_lock_irqsave(&conf->device_lock, flags);
274 bio->bi_phys_segments--;
275 done = (bio->bi_phys_segments == 0);
276 spin_unlock_irqrestore(&conf->device_lock, flags);
277 } else
278 done = 1;
279 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
280 clear_bit(BIO_UPTODATE, &bio->bi_flags);
281 if (done) {
282 bio_endio(bio, 0);
283 /*
284 * Wake up any possible resync thread that waits for the device
285 * to go idle.
286 */
287 allow_barrier(conf);
288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 free_r10bio(r10_bio);
290}
291
292/*
293 * Update disk head position estimator based on IRQ completion info.
294 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100295static inline void update_head_pos(int slot, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
NeilBrowne879a872011-10-11 16:49:02 +1100297 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
300 r10_bio->devs[slot].addr + (r10_bio->sectors);
301}
302
Namhyung Kim778ca012011-07-18 17:38:47 +1000303/*
304 * Find the disk number which triggered given bio
305 */
NeilBrowne879a872011-10-11 16:49:02 +1100306static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
NeilBrown69335ef2011-12-23 10:17:54 +1100307 struct bio *bio, int *slotp, int *replp)
Namhyung Kim778ca012011-07-18 17:38:47 +1000308{
309 int slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100310 int repl = 0;
Namhyung Kim778ca012011-07-18 17:38:47 +1000311
NeilBrown69335ef2011-12-23 10:17:54 +1100312 for (slot = 0; slot < conf->copies; slot++) {
Namhyung Kim778ca012011-07-18 17:38:47 +1000313 if (r10_bio->devs[slot].bio == bio)
314 break;
NeilBrown69335ef2011-12-23 10:17:54 +1100315 if (r10_bio->devs[slot].repl_bio == bio) {
316 repl = 1;
317 break;
318 }
319 }
Namhyung Kim778ca012011-07-18 17:38:47 +1000320
321 BUG_ON(slot == conf->copies);
322 update_head_pos(slot, r10_bio);
323
NeilBrown749c55e2011-07-28 11:39:24 +1000324 if (slotp)
325 *slotp = slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100326 if (replp)
327 *replp = repl;
Namhyung Kim778ca012011-07-18 17:38:47 +1000328 return r10_bio->devs[slot].devnum;
329}
330
NeilBrown6712ecf2007-09-27 12:47:43 +0200331static void raid10_end_read_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +1100334 struct r10bio *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 int slot, dev;
NeilBrownabbf0982011-12-23 10:17:54 +1100336 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +1100337 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 slot = r10_bio->read_slot;
341 dev = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100342 rdev = r10_bio->devs[slot].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 /*
344 * this branch is our 'one mirror IO has finished' event handler:
345 */
NeilBrown4443ae12006-01-06 00:20:28 -0800346 update_head_pos(slot, r10_bio);
347
348 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /*
350 * Set R10BIO_Uptodate in our master bio, so that
351 * we will return a good error code to the higher
352 * levels even if IO on some other mirrored buffer fails.
353 *
354 * The 'master' represents the composite IO operation to
355 * user-side. So if something waits for IO, then it will
356 * wait for the 'master' bio.
357 */
358 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrownfae8cc5e2012-02-14 11:10:10 +1100359 } else {
360 /* If all other devices that store this block have
361 * failed, we want to return the error upwards rather
362 * than fail the last device. Here we redefine
363 * "uptodate" to mean "Don't want to retry"
364 */
365 unsigned long flags;
366 spin_lock_irqsave(&conf->device_lock, flags);
367 if (!enough(conf, rdev->raid_disk))
368 uptodate = 1;
369 spin_unlock_irqrestore(&conf->device_lock, flags);
370 }
371 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 raid_end_bio_io(r10_bio);
NeilBrownabbf0982011-12-23 10:17:54 +1100373 rdev_dec_pending(rdev, conf->mddev);
NeilBrown4443ae12006-01-06 00:20:28 -0800374 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /*
NeilBrown7c4e06f2011-05-11 14:53:17 +1000376 * oops, read error - keep the refcount on the rdev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 */
378 char b[BDEVNAME_SIZE];
Christian Dietrich8bda4702011-07-27 11:00:36 +1000379 printk_ratelimited(KERN_ERR
380 "md/raid10:%s: %s: rescheduling sector %llu\n",
381 mdname(conf->mddev),
NeilBrownabbf0982011-12-23 10:17:54 +1100382 bdevname(rdev->bdev, b),
Christian Dietrich8bda4702011-07-27 11:00:36 +1000383 (unsigned long long)r10_bio->sector);
NeilBrown856e08e2011-07-28 11:39:23 +1000384 set_bit(R10BIO_ReadError, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 reschedule_retry(r10_bio);
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
NeilBrown9f2c9d12011-10-11 16:48:43 +1100389static void close_write(struct r10bio *r10_bio)
NeilBrownbd870a12011-07-28 11:39:24 +1000390{
391 /* clear the bitmap if all writes complete successfully */
392 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
393 r10_bio->sectors,
394 !test_bit(R10BIO_Degraded, &r10_bio->state),
395 0);
396 md_write_end(r10_bio->mddev);
397}
398
NeilBrown9f2c9d12011-10-11 16:48:43 +1100399static void one_write_done(struct r10bio *r10_bio)
NeilBrown19d5f832011-09-10 17:21:17 +1000400{
401 if (atomic_dec_and_test(&r10_bio->remaining)) {
402 if (test_bit(R10BIO_WriteError, &r10_bio->state))
403 reschedule_retry(r10_bio);
404 else {
405 close_write(r10_bio);
406 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
407 reschedule_retry(r10_bio);
408 else
409 raid_end_bio_io(r10_bio);
410 }
411 }
412}
413
NeilBrown6712ecf2007-09-27 12:47:43 +0200414static void raid10_end_write_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +1100417 struct r10bio *r10_bio = bio->bi_private;
Namhyung Kim778ca012011-07-18 17:38:47 +1000418 int dev;
NeilBrown749c55e2011-07-28 11:39:24 +1000419 int dec_rdev = 1;
NeilBrowne879a872011-10-11 16:49:02 +1100420 struct r10conf *conf = r10_bio->mddev->private;
NeilBrown475b0322011-12-23 10:17:55 +1100421 int slot, repl;
NeilBrown4ca40c22011-12-23 10:17:55 +1100422 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
NeilBrown475b0322011-12-23 10:17:55 +1100424 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
NeilBrown475b0322011-12-23 10:17:55 +1100426 if (repl)
427 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +1100428 if (!rdev) {
429 smp_rmb();
430 repl = 0;
NeilBrown475b0322011-12-23 10:17:55 +1100431 rdev = conf->mirrors[dev].rdev;
NeilBrown4ca40c22011-12-23 10:17:55 +1100432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 /*
434 * this branch is our 'one mirror IO has finished' event handler:
435 */
NeilBrown6cce3b22006-01-06 00:20:16 -0800436 if (!uptodate) {
NeilBrown475b0322011-12-23 10:17:55 +1100437 if (repl)
438 /* Never record new bad blocks to replacement,
439 * just fail it.
440 */
441 md_error(rdev->mddev, rdev);
442 else {
443 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +1100444 if (!test_and_set_bit(WantReplacement, &rdev->flags))
445 set_bit(MD_RECOVERY_NEEDED,
446 &rdev->mddev->recovery);
NeilBrown475b0322011-12-23 10:17:55 +1100447 set_bit(R10BIO_WriteError, &r10_bio->state);
448 dec_rdev = 0;
449 }
NeilBrown749c55e2011-07-28 11:39:24 +1000450 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /*
452 * Set R10BIO_Uptodate in our master bio, so that
453 * we will return a good error code for to the higher
454 * levels even if IO on some other mirrored buffer fails.
455 *
456 * The 'master' represents the composite IO operation to
457 * user-side. So if something waits for IO, then it will
458 * wait for the 'master' bio.
459 */
NeilBrown749c55e2011-07-28 11:39:24 +1000460 sector_t first_bad;
461 int bad_sectors;
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 set_bit(R10BIO_Uptodate, &r10_bio->state);
464
NeilBrown749c55e2011-07-28 11:39:24 +1000465 /* Maybe we can clear some bad blocks. */
NeilBrown475b0322011-12-23 10:17:55 +1100466 if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +1000467 r10_bio->devs[slot].addr,
468 r10_bio->sectors,
469 &first_bad, &bad_sectors)) {
470 bio_put(bio);
NeilBrown475b0322011-12-23 10:17:55 +1100471 if (repl)
472 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
473 else
474 r10_bio->devs[slot].bio = IO_MADE_GOOD;
NeilBrown749c55e2011-07-28 11:39:24 +1000475 dec_rdev = 0;
476 set_bit(R10BIO_MadeGood, &r10_bio->state);
477 }
478 }
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 /*
481 *
482 * Let's see if all mirrored write operations have finished
483 * already.
484 */
NeilBrown19d5f832011-09-10 17:21:17 +1000485 one_write_done(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +1000486 if (dec_rdev)
487 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490/*
491 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300492 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 * parameters: near_copies and far_copies.
494 * near_copies * far_copies must be <= raid_disks.
495 * Normally one of these will be 1.
496 * If both are 1, we get raid0.
497 * If near_copies == raid_disks, we get raid1.
498 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300499 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 * first chunk, followed by near_copies copies of the next chunk and
501 * so on.
502 * If far_copies > 1, then after 1/far_copies of the array has been assigned
503 * as described above, we start again with a device offset of near_copies.
504 * So we effectively have another copy of the whole array further down all
505 * the drives, but with blocks on different drives.
506 * With this layout, and block is never stored twice on the one device.
507 *
508 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700509 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 *
511 * raid10_find_virt does the reverse mapping, from a device and a
512 * sector offset to a virtual address
513 */
514
NeilBrownf8c9e742012-05-21 09:28:33 +1000515static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
517 int n,f;
518 sector_t sector;
519 sector_t chunk;
520 sector_t stripe;
521 int dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 int slot = 0;
523
524 /* now calculate first sector/dev */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000525 chunk = r10bio->sector >> geo->chunk_shift;
526 sector = r10bio->sector & geo->chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
NeilBrown5cf00fc2012-05-21 09:28:20 +1000528 chunk *= geo->near_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 stripe = chunk;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000530 dev = sector_div(stripe, geo->raid_disks);
531 if (geo->far_offset)
532 stripe *= geo->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
NeilBrown5cf00fc2012-05-21 09:28:20 +1000534 sector += stripe << geo->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 /* and calculate all the others */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000537 for (n = 0; n < geo->near_copies; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 int d = dev;
539 sector_t s = sector;
540 r10bio->devs[slot].addr = sector;
541 r10bio->devs[slot].devnum = d;
542 slot++;
543
NeilBrown5cf00fc2012-05-21 09:28:20 +1000544 for (f = 1; f < geo->far_copies; f++) {
545 d += geo->near_copies;
546 if (d >= geo->raid_disks)
547 d -= geo->raid_disks;
548 s += geo->stride;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 r10bio->devs[slot].devnum = d;
550 r10bio->devs[slot].addr = s;
551 slot++;
552 }
553 dev++;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000554 if (dev >= geo->raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 dev = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000556 sector += (geo->chunk_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558 }
NeilBrownf8c9e742012-05-21 09:28:33 +1000559}
560
561static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
562{
563 struct geom *geo = &conf->geo;
564
565 if (conf->reshape_progress != MaxSector &&
566 ((r10bio->sector >= conf->reshape_progress) !=
567 conf->mddev->reshape_backwards)) {
568 set_bit(R10BIO_Previous, &r10bio->state);
569 geo = &conf->prev;
570 } else
571 clear_bit(R10BIO_Previous, &r10bio->state);
572
573 __raid10_find_phys(geo, r10bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
NeilBrowne879a872011-10-11 16:49:02 +1100576static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 sector_t offset, chunk, vchunk;
NeilBrownf8c9e742012-05-21 09:28:33 +1000579 /* Never use conf->prev as this is only called during resync
580 * or recovery, so reshape isn't happening
581 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000582 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
NeilBrown5cf00fc2012-05-21 09:28:20 +1000584 offset = sector & geo->chunk_mask;
585 if (geo->far_offset) {
NeilBrownc93983b2006-06-26 00:27:41 -0700586 int fc;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000587 chunk = sector >> geo->chunk_shift;
588 fc = sector_div(chunk, geo->far_copies);
589 dev -= fc * geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700590 if (dev < 0)
NeilBrown5cf00fc2012-05-21 09:28:20 +1000591 dev += geo->raid_disks;
NeilBrownc93983b2006-06-26 00:27:41 -0700592 } else {
NeilBrown5cf00fc2012-05-21 09:28:20 +1000593 while (sector >= geo->stride) {
594 sector -= geo->stride;
595 if (dev < geo->near_copies)
596 dev += geo->raid_disks - geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700597 else
NeilBrown5cf00fc2012-05-21 09:28:20 +1000598 dev -= geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700599 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000600 chunk = sector >> geo->chunk_shift;
NeilBrownc93983b2006-06-26 00:27:41 -0700601 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000602 vchunk = chunk * geo->raid_disks + dev;
603 sector_div(vchunk, geo->near_copies);
604 return (vchunk << geo->chunk_shift) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607/**
608 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
609 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200610 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 * @biovec: the request that could be merged to it.
612 *
613 * Return amount of bytes we can accept at this offset
NeilBrown050b6612012-03-19 12:46:39 +1100614 * This requires checking for end-of-chunk if near_copies != raid_disks,
615 * and for subordinate merge_bvec_fns if merge_check_needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200617static int raid10_mergeable_bvec(struct request_queue *q,
618 struct bvec_merge_data *bvm,
619 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
NeilBrownfd01b882011-10-11 16:47:53 +1100621 struct mddev *mddev = q->queuedata;
NeilBrown050b6612012-03-19 12:46:39 +1100622 struct r10conf *conf = mddev->private;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200623 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 int max;
NeilBrown3ea7daa2012-05-22 13:53:47 +1000625 unsigned int chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200626 unsigned int bio_sectors = bvm->bi_size >> 9;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000627 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
NeilBrown3ea7daa2012-05-22 13:53:47 +1000629 chunk_sectors = (conf->geo.chunk_mask & conf->prev.chunk_mask) + 1;
NeilBrownf8c9e742012-05-21 09:28:33 +1000630 if (conf->reshape_progress != MaxSector &&
631 ((sector >= conf->reshape_progress) !=
632 conf->mddev->reshape_backwards))
633 geo = &conf->prev;
634
NeilBrown5cf00fc2012-05-21 09:28:20 +1000635 if (geo->near_copies < geo->raid_disks) {
NeilBrown050b6612012-03-19 12:46:39 +1100636 max = (chunk_sectors - ((sector & (chunk_sectors - 1))
637 + bio_sectors)) << 9;
638 if (max < 0)
639 /* bio_add cannot handle a negative return */
640 max = 0;
641 if (max <= biovec->bv_len && bio_sectors == 0)
642 return biovec->bv_len;
643 } else
644 max = biovec->bv_len;
645
646 if (mddev->merge_check_needed) {
647 struct r10bio r10_bio;
648 int s;
NeilBrownf8c9e742012-05-21 09:28:33 +1000649 if (conf->reshape_progress != MaxSector) {
650 /* Cannot give any guidance during reshape */
651 if (max <= biovec->bv_len && bio_sectors == 0)
652 return biovec->bv_len;
653 return 0;
654 }
NeilBrown050b6612012-03-19 12:46:39 +1100655 r10_bio.sector = sector;
656 raid10_find_phys(conf, &r10_bio);
657 rcu_read_lock();
658 for (s = 0; s < conf->copies; s++) {
659 int disk = r10_bio.devs[s].devnum;
660 struct md_rdev *rdev = rcu_dereference(
661 conf->mirrors[disk].rdev);
662 if (rdev && !test_bit(Faulty, &rdev->flags)) {
663 struct request_queue *q =
664 bdev_get_queue(rdev->bdev);
665 if (q->merge_bvec_fn) {
666 bvm->bi_sector = r10_bio.devs[s].addr
667 + rdev->data_offset;
668 bvm->bi_bdev = rdev->bdev;
669 max = min(max, q->merge_bvec_fn(
670 q, bvm, biovec));
671 }
672 }
673 rdev = rcu_dereference(conf->mirrors[disk].replacement);
674 if (rdev && !test_bit(Faulty, &rdev->flags)) {
675 struct request_queue *q =
676 bdev_get_queue(rdev->bdev);
677 if (q->merge_bvec_fn) {
678 bvm->bi_sector = r10_bio.devs[s].addr
679 + rdev->data_offset;
680 bvm->bi_bdev = rdev->bdev;
681 max = min(max, q->merge_bvec_fn(
682 q, bvm, biovec));
683 }
684 }
685 }
686 rcu_read_unlock();
687 }
688 return max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
691/*
692 * This routine returns the disk from which the requested read should
693 * be done. There is a per-array 'next expected sequential IO' sector
694 * number - if this matches on the next IO then we use the last disk.
695 * There is also a per-disk 'last know head position' sector that is
696 * maintained from IRQ contexts, both the normal and the resync IO
697 * completion handlers update this position correctly. If there is no
698 * perfect sequential match then we pick the disk whose head is closest.
699 *
700 * If there are 2 mirrors in the same 2 devices, performance degrades
701 * because position is mirror, not device based.
702 *
703 * The rdev for the device selected will have nr_pending incremented.
704 */
705
706/*
707 * FIXME: possibly should rethink readbalancing and do it differently
708 * depending on near_copies / far_copies geometry.
709 */
NeilBrown96c3fd12011-12-23 10:17:54 +1100710static struct md_rdev *read_balance(struct r10conf *conf,
711 struct r10bio *r10_bio,
712 int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000714 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000715 int disk, slot;
NeilBrown856e08e2011-07-28 11:39:23 +1000716 int sectors = r10_bio->sectors;
717 int best_good_sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000718 sector_t new_distance, best_dist;
NeilBrownabbf0982011-12-23 10:17:54 +1100719 struct md_rdev *rdev, *best_rdev;
NeilBrown56d99122011-05-11 14:27:03 +1000720 int do_balance;
721 int best_slot;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000722 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 raid10_find_phys(conf, r10_bio);
725 rcu_read_lock();
NeilBrown56d99122011-05-11 14:27:03 +1000726retry:
NeilBrown856e08e2011-07-28 11:39:23 +1000727 sectors = r10_bio->sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000728 best_slot = -1;
NeilBrownabbf0982011-12-23 10:17:54 +1100729 best_rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000730 best_dist = MaxSector;
NeilBrown856e08e2011-07-28 11:39:23 +1000731 best_good_sectors = 0;
NeilBrown56d99122011-05-11 14:27:03 +1000732 do_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /*
734 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800735 * device if no resync is going on (recovery is ok), or below
736 * the resync window. We take the first readable disk when
737 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 */
739 if (conf->mddev->recovery_cp < MaxSector
NeilBrown56d99122011-05-11 14:27:03 +1000740 && (this_sector + sectors >= conf->next_resync))
741 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
NeilBrown56d99122011-05-11 14:27:03 +1000743 for (slot = 0; slot < conf->copies ; slot++) {
NeilBrown856e08e2011-07-28 11:39:23 +1000744 sector_t first_bad;
745 int bad_sectors;
746 sector_t dev_sector;
747
NeilBrown56d99122011-05-11 14:27:03 +1000748 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000750 disk = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100751 rdev = rcu_dereference(conf->mirrors[disk].replacement);
752 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
NeilBrown050b6612012-03-19 12:46:39 +1100753 test_bit(Unmerged, &rdev->flags) ||
NeilBrownabbf0982011-12-23 10:17:54 +1100754 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
755 rdev = rcu_dereference(conf->mirrors[disk].rdev);
NeilBrown050b6612012-03-19 12:46:39 +1100756 if (rdev == NULL ||
757 test_bit(Faulty, &rdev->flags) ||
758 test_bit(Unmerged, &rdev->flags))
NeilBrownabbf0982011-12-23 10:17:54 +1100759 continue;
760 if (!test_bit(In_sync, &rdev->flags) &&
761 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
NeilBrown56d99122011-05-11 14:27:03 +1000762 continue;
763
NeilBrown856e08e2011-07-28 11:39:23 +1000764 dev_sector = r10_bio->devs[slot].addr;
765 if (is_badblock(rdev, dev_sector, sectors,
766 &first_bad, &bad_sectors)) {
767 if (best_dist < MaxSector)
768 /* Already have a better slot */
769 continue;
770 if (first_bad <= dev_sector) {
771 /* Cannot read here. If this is the
772 * 'primary' device, then we must not read
773 * beyond 'bad_sectors' from another device.
774 */
775 bad_sectors -= (dev_sector - first_bad);
776 if (!do_balance && sectors > bad_sectors)
777 sectors = bad_sectors;
778 if (best_good_sectors > sectors)
779 best_good_sectors = sectors;
780 } else {
781 sector_t good_sectors =
782 first_bad - dev_sector;
783 if (good_sectors > best_good_sectors) {
784 best_good_sectors = good_sectors;
785 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100786 best_rdev = rdev;
NeilBrown856e08e2011-07-28 11:39:23 +1000787 }
788 if (!do_balance)
789 /* Must read from here */
790 break;
791 }
792 continue;
793 } else
794 best_good_sectors = sectors;
795
NeilBrown56d99122011-05-11 14:27:03 +1000796 if (!do_balance)
797 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
NeilBrown22dfdf52005-11-28 13:44:09 -0800799 /* This optimisation is debatable, and completely destroys
800 * sequential read speed for 'far copies' arrays. So only
801 * keep it for 'near' arrays, and review those later.
802 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000803 if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 break;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800805
806 /* for far > 1 always use the lowest address */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000807 if (geo->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000808 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800809 else
NeilBrown56d99122011-05-11 14:27:03 +1000810 new_distance = abs(r10_bio->devs[slot].addr -
811 conf->mirrors[disk].head_position);
812 if (new_distance < best_dist) {
813 best_dist = new_distance;
814 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100815 best_rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
817 }
NeilBrownabbf0982011-12-23 10:17:54 +1100818 if (slot >= conf->copies) {
NeilBrown56d99122011-05-11 14:27:03 +1000819 slot = best_slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100820 rdev = best_rdev;
821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
NeilBrown56d99122011-05-11 14:27:03 +1000823 if (slot >= 0) {
NeilBrown56d99122011-05-11 14:27:03 +1000824 atomic_inc(&rdev->nr_pending);
825 if (test_bit(Faulty, &rdev->flags)) {
826 /* Cannot risk returning a device that failed
827 * before we inc'ed nr_pending
828 */
829 rdev_dec_pending(rdev, conf->mddev);
830 goto retry;
831 }
832 r10_bio->read_slot = slot;
833 } else
NeilBrown96c3fd12011-12-23 10:17:54 +1100834 rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 rcu_read_unlock();
NeilBrown856e08e2011-07-28 11:39:23 +1000836 *max_sectors = best_good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
NeilBrown96c3fd12011-12-23 10:17:54 +1100838 return rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
NeilBrown0d129222006-10-03 01:15:54 -0700841static int raid10_congested(void *data, int bits)
842{
NeilBrownfd01b882011-10-11 16:47:53 +1100843 struct mddev *mddev = data;
NeilBrowne879a872011-10-11 16:49:02 +1100844 struct r10conf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700845 int i, ret = 0;
846
NeilBrown34db0cd2011-10-11 16:50:01 +1100847 if ((bits & (1 << BDI_async_congested)) &&
848 conf->pending_count >= max_queued_requests)
849 return 1;
850
NeilBrown3fa841d2009-09-23 18:10:29 +1000851 if (mddev_congested(mddev, bits))
852 return 1;
NeilBrown0d129222006-10-03 01:15:54 -0700853 rcu_read_lock();
NeilBrownf8c9e742012-05-21 09:28:33 +1000854 for (i = 0;
855 (i < conf->geo.raid_disks || i < conf->prev.raid_disks)
856 && ret == 0;
857 i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100858 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700859 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200860 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700861
862 ret |= bdi_congested(&q->backing_dev_info, bits);
863 }
864 }
865 rcu_read_unlock();
866 return ret;
867}
868
NeilBrowne879a872011-10-11 16:49:02 +1100869static void flush_pending_writes(struct r10conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800870{
871 /* Any writes that have been queued but are awaiting
872 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800873 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800874 spin_lock_irq(&conf->device_lock);
875
876 if (conf->pending_bio_list.head) {
877 struct bio *bio;
878 bio = bio_list_get(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +1100879 conf->pending_count = 0;
NeilBrowna35e63e2008-03-04 14:29:29 -0800880 spin_unlock_irq(&conf->device_lock);
881 /* flush any pending bitmap writes to disk
882 * before proceeding w/ I/O */
883 bitmap_unplug(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100884 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800885
886 while (bio) { /* submit pending writes */
887 struct bio *next = bio->bi_next;
888 bio->bi_next = NULL;
889 generic_make_request(bio);
890 bio = next;
891 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800892 } else
893 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800894}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100895
NeilBrown0a27ec92006-01-06 00:20:13 -0800896/* Barriers....
897 * Sometimes we need to suspend IO while we do something else,
898 * either some resync/recovery, or reconfigure the array.
899 * To do this we raise a 'barrier'.
900 * The 'barrier' is a counter that can be raised multiple times
901 * to count how many activities are happening which preclude
902 * normal IO.
903 * We can only raise the barrier if there is no pending IO.
904 * i.e. if nr_pending == 0.
905 * We choose only to raise the barrier if no-one is waiting for the
906 * barrier to go down. This means that as soon as an IO request
907 * is ready, no other operations which require a barrier will start
908 * until the IO request has had a chance.
909 *
910 * So: regular IO calls 'wait_barrier'. When that returns there
911 * is no backgroup IO happening, It must arrange to call
912 * allow_barrier when it has finished its IO.
913 * backgroup IO calls must call raise_barrier. Once that returns
914 * there is no normal IO happeing. It must arrange to call
915 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
NeilBrowne879a872011-10-11 16:49:02 +1100918static void raise_barrier(struct r10conf *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
NeilBrown6cce3b22006-01-06 00:20:16 -0800920 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
NeilBrown6cce3b22006-01-06 00:20:16 -0800923 /* Wait until no block IO is waiting (unless 'force') */
924 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrownc3b328a2011-04-18 18:25:43 +1000925 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800926
927 /* block any new IO from starting */
928 conf->barrier++;
929
NeilBrownc3b328a2011-04-18 18:25:43 +1000930 /* Now wait for all pending IO to complete */
NeilBrown0a27ec92006-01-06 00:20:13 -0800931 wait_event_lock_irq(conf->wait_barrier,
932 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
NeilBrownc3b328a2011-04-18 18:25:43 +1000933 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 spin_unlock_irq(&conf->resync_lock);
936}
937
NeilBrowne879a872011-10-11 16:49:02 +1100938static void lower_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800939{
940 unsigned long flags;
941 spin_lock_irqsave(&conf->resync_lock, flags);
942 conf->barrier--;
943 spin_unlock_irqrestore(&conf->resync_lock, flags);
944 wake_up(&conf->wait_barrier);
945}
946
NeilBrowne879a872011-10-11 16:49:02 +1100947static void wait_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800948{
949 spin_lock_irq(&conf->resync_lock);
950 if (conf->barrier) {
951 conf->nr_waiting++;
NeilBrownd6b42dc2012-03-19 12:46:38 +1100952 /* Wait for the barrier to drop.
953 * However if there are already pending
954 * requests (preventing the barrier from
955 * rising completely), and the
956 * pre-process bio queue isn't empty,
957 * then don't wait, as we need to empty
958 * that queue to get the nr_pending
959 * count down.
960 */
961 wait_event_lock_irq(conf->wait_barrier,
962 !conf->barrier ||
963 (conf->nr_pending &&
964 current->bio_list &&
965 !bio_list_empty(current->bio_list)),
NeilBrown0a27ec92006-01-06 00:20:13 -0800966 conf->resync_lock,
NeilBrownd6b42dc2012-03-19 12:46:38 +1100967 );
NeilBrown0a27ec92006-01-06 00:20:13 -0800968 conf->nr_waiting--;
969 }
970 conf->nr_pending++;
971 spin_unlock_irq(&conf->resync_lock);
972}
973
NeilBrowne879a872011-10-11 16:49:02 +1100974static void allow_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800975{
976 unsigned long flags;
977 spin_lock_irqsave(&conf->resync_lock, flags);
978 conf->nr_pending--;
979 spin_unlock_irqrestore(&conf->resync_lock, flags);
980 wake_up(&conf->wait_barrier);
981}
982
NeilBrowne879a872011-10-11 16:49:02 +1100983static void freeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -0800984{
985 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800986 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800987 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800988 * wait until nr_pending match nr_queued+1
989 * This is called in the context of one normal IO request
990 * that has failed. Thus any sync request that might be pending
991 * will be blocked by nr_pending, and we need to wait for
992 * pending IO requests to complete or be queued for re-try.
993 * Thus the number queued (nr_queued) plus this request (1)
994 * must match the number of pending IOs (nr_pending) before
995 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -0800996 */
997 spin_lock_irq(&conf->resync_lock);
998 conf->barrier++;
999 conf->nr_waiting++;
1000 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -08001001 conf->nr_pending == conf->nr_queued+1,
NeilBrown4443ae12006-01-06 00:20:28 -08001002 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +10001003 flush_pending_writes(conf));
1004
NeilBrown4443ae12006-01-06 00:20:28 -08001005 spin_unlock_irq(&conf->resync_lock);
1006}
1007
NeilBrowne879a872011-10-11 16:49:02 +11001008static void unfreeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -08001009{
1010 /* reverse the effect of the freeze */
1011 spin_lock_irq(&conf->resync_lock);
1012 conf->barrier--;
1013 conf->nr_waiting--;
1014 wake_up(&conf->wait_barrier);
1015 spin_unlock_irq(&conf->resync_lock);
1016}
1017
NeilBrownf8c9e742012-05-21 09:28:33 +10001018static sector_t choose_data_offset(struct r10bio *r10_bio,
1019 struct md_rdev *rdev)
1020{
1021 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1022 test_bit(R10BIO_Previous, &r10_bio->state))
1023 return rdev->data_offset;
1024 else
1025 return rdev->new_data_offset;
1026}
1027
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07001028static void make_request(struct mddev *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
NeilBrowne879a872011-10-11 16:49:02 +11001030 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11001031 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 struct bio *read_bio;
1033 int i;
NeilBrownf8c9e742012-05-21 09:28:33 +10001034 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
NeilBrown5cf00fc2012-05-21 09:28:20 +10001035 int chunk_sects = chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +01001036 const int rw = bio_data_dir(bio);
NeilBrown2c7d46e2010-08-18 16:16:05 +10001037 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
Tejun Heoe9c74692010-09-03 11:56:18 +02001038 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
NeilBrown6cce3b22006-01-06 00:20:16 -08001039 unsigned long flags;
NeilBrown3cb03002011-10-11 16:45:26 +11001040 struct md_rdev *blocked_rdev;
NeilBrownc3b328a2011-04-18 18:25:43 +10001041 int plugged;
NeilBrownd4432c22011-07-28 11:39:24 +10001042 int sectors_handled;
1043 int max_sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10001044 int sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Tejun Heoe9c74692010-09-03 11:56:18 +02001046 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
1047 md_flush_request(mddev, bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001048 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07001049 }
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 /* If this request crosses a chunk boundary, we need to
1052 * split it. This will only happen for 1 PAGE (or less) requests.
1053 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10001054 if (unlikely((bio->bi_sector & chunk_mask) + (bio->bi_size >> 9)
1055 > chunk_sects
NeilBrownf8c9e742012-05-21 09:28:33 +10001056 && (conf->geo.near_copies < conf->geo.raid_disks
1057 || conf->prev.near_copies < conf->prev.raid_disks))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 struct bio_pair *bp;
1059 /* Sanity check -- queue functions should prevent this happening */
1060 if (bio->bi_vcnt != 1 ||
1061 bio->bi_idx != 0)
1062 goto bad_map;
1063 /* This is a one page bio that upper layers
1064 * refuse to split for us, so we need to split it.
1065 */
Denis ChengRq6feef532008-10-09 08:57:05 +02001066 bp = bio_split(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
NeilBrown51e9ac72010-08-07 21:17:00 +10001068
1069 /* Each of these 'make_request' calls will call 'wait_barrier'.
1070 * If the first succeeds but the second blocks due to the resync
1071 * thread raising the barrier, we will deadlock because the
1072 * IO to the underlying device will be queued in generic_make_request
1073 * and will never complete, so will never reduce nr_pending.
1074 * So increment nr_waiting here so no new raise_barriers will
1075 * succeed, and so the second wait_barrier cannot block.
1076 */
1077 spin_lock_irq(&conf->resync_lock);
1078 conf->nr_waiting++;
1079 spin_unlock_irq(&conf->resync_lock);
1080
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001081 make_request(mddev, &bp->bio1);
1082 make_request(mddev, &bp->bio2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
NeilBrown51e9ac72010-08-07 21:17:00 +10001084 spin_lock_irq(&conf->resync_lock);
1085 conf->nr_waiting--;
1086 wake_up(&conf->wait_barrier);
1087 spin_unlock_irq(&conf->resync_lock);
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 bio_pair_release(bp);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001090 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 bad_map:
NeilBrown128595e2010-05-03 14:47:14 +10001092 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
1093 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
1095
NeilBrown6712ecf2007-09-27 12:47:43 +02001096 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001097 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 }
1099
NeilBrown3d310eb2005-06-21 17:17:26 -07001100 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -07001101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 /*
1103 * Register the new request and wait if the reconstruction
1104 * thread has put up a bar for new requests.
1105 * Continue immediately if no resync is active currently.
1106 */
NeilBrown0a27ec92006-01-06 00:20:13 -08001107 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
NeilBrown3ea7daa2012-05-22 13:53:47 +10001109 sectors = bio->bi_size >> 9;
1110 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1111 bio->bi_sector < conf->reshape_progress &&
1112 bio->bi_sector + sectors > conf->reshape_progress) {
1113 /* IO spans the reshape position. Need to wait for
1114 * reshape to pass
1115 */
1116 allow_barrier(conf);
1117 wait_event(conf->wait_barrier,
1118 conf->reshape_progress <= bio->bi_sector ||
1119 conf->reshape_progress >= bio->bi_sector + sectors);
1120 wait_barrier(conf);
1121 }
1122 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1123 bio_data_dir(bio) == WRITE &&
1124 (mddev->reshape_backwards
1125 ? (bio->bi_sector < conf->reshape_safe &&
1126 bio->bi_sector + sectors > conf->reshape_progress)
1127 : (bio->bi_sector + sectors > conf->reshape_safe &&
1128 bio->bi_sector < conf->reshape_progress))) {
1129 /* Need to update reshape_position in metadata */
1130 mddev->reshape_position = conf->reshape_progress;
1131 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1132 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1133 md_wakeup_thread(mddev->thread);
1134 wait_event(mddev->sb_wait,
1135 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
1136
1137 conf->reshape_safe = mddev->reshape_position;
1138 }
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1141
1142 r10_bio->master_bio = bio;
NeilBrown3ea7daa2012-05-22 13:53:47 +10001143 r10_bio->sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 r10_bio->mddev = mddev;
1146 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b22006-01-06 00:20:16 -08001147 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
NeilBrown856e08e2011-07-28 11:39:23 +10001149 /* We might need to issue multiple reads to different
1150 * devices if there are bad blocks around, so we keep
1151 * track of the number of reads in bio->bi_phys_segments.
1152 * If this is 0, there is only one r10_bio and no locking
1153 * will be needed when the request completes. If it is
1154 * non-zero, then it is the number of not-completed requests.
1155 */
1156 bio->bi_phys_segments = 0;
1157 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
1158
Jens Axboea3623572005-11-01 09:26:16 +01001159 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 /*
1161 * read balancing logic:
1162 */
NeilBrown96c3fd12011-12-23 10:17:54 +11001163 struct md_rdev *rdev;
NeilBrown856e08e2011-07-28 11:39:23 +10001164 int slot;
1165
1166read_again:
NeilBrown96c3fd12011-12-23 10:17:54 +11001167 rdev = read_balance(conf, r10_bio, &max_sectors);
1168 if (!rdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 raid_end_bio_io(r10_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001170 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
NeilBrown96c3fd12011-12-23 10:17:54 +11001172 slot = r10_bio->read_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
NeilBrowna167f662010-10-26 18:31:13 +11001174 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrown856e08e2011-07-28 11:39:23 +10001175 md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
1176 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 r10_bio->devs[slot].bio = read_bio;
NeilBrownabbf0982011-12-23 10:17:54 +11001179 r10_bio->devs[slot].rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 read_bio->bi_sector = r10_bio->devs[slot].addr +
NeilBrownf8c9e742012-05-21 09:28:33 +10001182 choose_data_offset(r10_bio, rdev);
NeilBrown96c3fd12011-12-23 10:17:54 +11001183 read_bio->bi_bdev = rdev->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 read_bio->bi_end_io = raid10_end_read_request;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001185 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 read_bio->bi_private = r10_bio;
1187
NeilBrown856e08e2011-07-28 11:39:23 +10001188 if (max_sectors < r10_bio->sectors) {
1189 /* Could not read all from this device, so we will
1190 * need another r10_bio.
1191 */
NeilBrown856e08e2011-07-28 11:39:23 +10001192 sectors_handled = (r10_bio->sectors + max_sectors
1193 - bio->bi_sector);
1194 r10_bio->sectors = max_sectors;
1195 spin_lock_irq(&conf->device_lock);
1196 if (bio->bi_phys_segments == 0)
1197 bio->bi_phys_segments = 2;
1198 else
1199 bio->bi_phys_segments++;
1200 spin_unlock(&conf->device_lock);
1201 /* Cannot call generic_make_request directly
1202 * as that will be queued in __generic_make_request
1203 * and subsequent mempool_alloc might block
1204 * waiting for it. so hand bio over to raid10d.
1205 */
1206 reschedule_retry(r10_bio);
1207
1208 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1209
1210 r10_bio->master_bio = bio;
1211 r10_bio->sectors = ((bio->bi_size >> 9)
1212 - sectors_handled);
1213 r10_bio->state = 0;
1214 r10_bio->mddev = mddev;
1215 r10_bio->sector = bio->bi_sector + sectors_handled;
1216 goto read_again;
1217 } else
1218 generic_make_request(read_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001219 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
1221
1222 /*
1223 * WRITE:
1224 */
NeilBrown34db0cd2011-10-11 16:50:01 +11001225 if (conf->pending_count >= max_queued_requests) {
1226 md_wakeup_thread(mddev->thread);
1227 wait_event(conf->wait_barrier,
1228 conf->pending_count < max_queued_requests);
1229 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001230 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 * inc refcount on their rdev. Record them by setting
1232 * bios[x] to bio
NeilBrownd4432c22011-07-28 11:39:24 +10001233 * If there are known/acknowledged bad blocks on any device
1234 * on which we have seen a write error, we want to avoid
1235 * writing to those blocks. This potentially requires several
1236 * writes to write around the bad blocks. Each set of writes
1237 * gets its own r10_bio with a set of bios attached. The number
1238 * of r10_bios is recored in bio->bi_phys_segments just as with
1239 * the read case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001241 plugged = mddev_check_plugged(mddev);
1242
NeilBrown69335ef2011-12-23 10:17:54 +11001243 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 raid10_find_phys(conf, r10_bio);
NeilBrownd4432c22011-07-28 11:39:24 +10001245retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -07001246 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 rcu_read_lock();
NeilBrownd4432c22011-07-28 11:39:24 +10001248 max_sectors = r10_bio->sectors;
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 for (i = 0; i < conf->copies; i++) {
1251 int d = r10_bio->devs[i].devnum;
NeilBrown3cb03002011-10-11 16:45:26 +11001252 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown475b0322011-12-23 10:17:55 +11001253 struct md_rdev *rrdev = rcu_dereference(
1254 conf->mirrors[d].replacement);
NeilBrown4ca40c22011-12-23 10:17:55 +11001255 if (rdev == rrdev)
1256 rrdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001257 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1258 atomic_inc(&rdev->nr_pending);
1259 blocked_rdev = rdev;
1260 break;
1261 }
NeilBrown475b0322011-12-23 10:17:55 +11001262 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1263 atomic_inc(&rrdev->nr_pending);
1264 blocked_rdev = rrdev;
1265 break;
1266 }
NeilBrown050b6612012-03-19 12:46:39 +11001267 if (rrdev && (test_bit(Faulty, &rrdev->flags)
1268 || test_bit(Unmerged, &rrdev->flags)))
NeilBrown475b0322011-12-23 10:17:55 +11001269 rrdev = NULL;
1270
NeilBrownd4432c22011-07-28 11:39:24 +10001271 r10_bio->devs[i].bio = NULL;
NeilBrown475b0322011-12-23 10:17:55 +11001272 r10_bio->devs[i].repl_bio = NULL;
NeilBrown050b6612012-03-19 12:46:39 +11001273 if (!rdev || test_bit(Faulty, &rdev->flags) ||
1274 test_bit(Unmerged, &rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001275 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrownd4432c22011-07-28 11:39:24 +10001276 continue;
NeilBrown6cce3b22006-01-06 00:20:16 -08001277 }
NeilBrownd4432c22011-07-28 11:39:24 +10001278 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1279 sector_t first_bad;
1280 sector_t dev_sector = r10_bio->devs[i].addr;
1281 int bad_sectors;
1282 int is_bad;
1283
1284 is_bad = is_badblock(rdev, dev_sector,
1285 max_sectors,
1286 &first_bad, &bad_sectors);
1287 if (is_bad < 0) {
1288 /* Mustn't write here until the bad block
1289 * is acknowledged
1290 */
1291 atomic_inc(&rdev->nr_pending);
1292 set_bit(BlockedBadBlocks, &rdev->flags);
1293 blocked_rdev = rdev;
1294 break;
1295 }
1296 if (is_bad && first_bad <= dev_sector) {
1297 /* Cannot write here at all */
1298 bad_sectors -= (dev_sector - first_bad);
1299 if (bad_sectors < max_sectors)
1300 /* Mustn't write more than bad_sectors
1301 * to other devices yet
1302 */
1303 max_sectors = bad_sectors;
1304 /* We don't set R10BIO_Degraded as that
1305 * only applies if the disk is missing,
1306 * so it might be re-added, and we want to
1307 * know to recover this chunk.
1308 * In this case the device is here, and the
1309 * fact that this chunk is not in-sync is
1310 * recorded in the bad block log.
1311 */
1312 continue;
1313 }
1314 if (is_bad) {
1315 int good_sectors = first_bad - dev_sector;
1316 if (good_sectors < max_sectors)
1317 max_sectors = good_sectors;
1318 }
1319 }
1320 r10_bio->devs[i].bio = bio;
1321 atomic_inc(&rdev->nr_pending);
NeilBrown475b0322011-12-23 10:17:55 +11001322 if (rrdev) {
1323 r10_bio->devs[i].repl_bio = bio;
1324 atomic_inc(&rrdev->nr_pending);
1325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
1327 rcu_read_unlock();
1328
Dan Williams6bfe0b42008-04-30 00:52:32 -07001329 if (unlikely(blocked_rdev)) {
1330 /* Have to wait for this device to get unblocked, then retry */
1331 int j;
1332 int d;
1333
NeilBrown475b0322011-12-23 10:17:55 +11001334 for (j = 0; j < i; j++) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07001335 if (r10_bio->devs[j].bio) {
1336 d = r10_bio->devs[j].devnum;
1337 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1338 }
NeilBrown475b0322011-12-23 10:17:55 +11001339 if (r10_bio->devs[j].repl_bio) {
NeilBrown4ca40c22011-12-23 10:17:55 +11001340 struct md_rdev *rdev;
NeilBrown475b0322011-12-23 10:17:55 +11001341 d = r10_bio->devs[j].devnum;
NeilBrown4ca40c22011-12-23 10:17:55 +11001342 rdev = conf->mirrors[d].replacement;
1343 if (!rdev) {
1344 /* Race with remove_disk */
1345 smp_mb();
1346 rdev = conf->mirrors[d].rdev;
1347 }
1348 rdev_dec_pending(rdev, mddev);
NeilBrown475b0322011-12-23 10:17:55 +11001349 }
1350 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001351 allow_barrier(conf);
1352 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1353 wait_barrier(conf);
1354 goto retry_write;
1355 }
1356
NeilBrownd4432c22011-07-28 11:39:24 +10001357 if (max_sectors < r10_bio->sectors) {
1358 /* We are splitting this into multiple parts, so
1359 * we need to prepare for allocating another r10_bio.
1360 */
1361 r10_bio->sectors = max_sectors;
1362 spin_lock_irq(&conf->device_lock);
1363 if (bio->bi_phys_segments == 0)
1364 bio->bi_phys_segments = 2;
1365 else
1366 bio->bi_phys_segments++;
1367 spin_unlock_irq(&conf->device_lock);
1368 }
1369 sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
1370
NeilBrown4e780642010-10-19 12:54:01 +11001371 atomic_set(&r10_bio->remaining, 1);
NeilBrownd4432c22011-07-28 11:39:24 +10001372 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07001373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 for (i = 0; i < conf->copies; i++) {
1375 struct bio *mbio;
1376 int d = r10_bio->devs[i].devnum;
1377 if (!r10_bio->devs[i].bio)
1378 continue;
1379
NeilBrowna167f662010-10-26 18:31:13 +11001380 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrownd4432c22011-07-28 11:39:24 +10001381 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1382 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 r10_bio->devs[i].bio = mbio;
1384
NeilBrownd4432c22011-07-28 11:39:24 +10001385 mbio->bi_sector = (r10_bio->devs[i].addr+
NeilBrownf8c9e742012-05-21 09:28:33 +10001386 choose_data_offset(r10_bio,
1387 conf->mirrors[d].rdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1389 mbio->bi_end_io = raid10_end_write_request;
Tejun Heoe9c74692010-09-03 11:56:18 +02001390 mbio->bi_rw = WRITE | do_sync | do_fua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 mbio->bi_private = r10_bio;
1392
1393 atomic_inc(&r10_bio->remaining);
NeilBrown4e780642010-10-19 12:54:01 +11001394 spin_lock_irqsave(&conf->device_lock, flags);
1395 bio_list_add(&conf->pending_bio_list, mbio);
NeilBrown34db0cd2011-10-11 16:50:01 +11001396 conf->pending_count++;
NeilBrown4e780642010-10-19 12:54:01 +11001397 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown475b0322011-12-23 10:17:55 +11001398
1399 if (!r10_bio->devs[i].repl_bio)
1400 continue;
1401
1402 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1403 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1404 max_sectors);
1405 r10_bio->devs[i].repl_bio = mbio;
1406
NeilBrown4ca40c22011-12-23 10:17:55 +11001407 /* We are actively writing to the original device
1408 * so it cannot disappear, so the replacement cannot
1409 * become NULL here
1410 */
NeilBrownf8c9e742012-05-21 09:28:33 +10001411 mbio->bi_sector = (r10_bio->devs[i].addr +
1412 choose_data_offset(
1413 r10_bio,
1414 conf->mirrors[d].replacement));
NeilBrown475b0322011-12-23 10:17:55 +11001415 mbio->bi_bdev = conf->mirrors[d].replacement->bdev;
1416 mbio->bi_end_io = raid10_end_write_request;
1417 mbio->bi_rw = WRITE | do_sync | do_fua;
1418 mbio->bi_private = r10_bio;
1419
1420 atomic_inc(&r10_bio->remaining);
1421 spin_lock_irqsave(&conf->device_lock, flags);
1422 bio_list_add(&conf->pending_bio_list, mbio);
1423 conf->pending_count++;
1424 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
1426
NeilBrown079fa162011-09-10 17:21:23 +10001427 /* Don't remove the bias on 'remaining' (one_write_done) until
1428 * after checking if we need to go around again.
1429 */
NeilBrowna35e63e2008-03-04 14:29:29 -08001430
NeilBrownd4432c22011-07-28 11:39:24 +10001431 if (sectors_handled < (bio->bi_size >> 9)) {
NeilBrown079fa162011-09-10 17:21:23 +10001432 one_write_done(r10_bio);
NeilBrown5e570282011-07-28 11:39:25 +10001433 /* We need another r10_bio. It has already been counted
NeilBrownd4432c22011-07-28 11:39:24 +10001434 * in bio->bi_phys_segments.
1435 */
1436 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1437
1438 r10_bio->master_bio = bio;
1439 r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1440
1441 r10_bio->mddev = mddev;
1442 r10_bio->sector = bio->bi_sector + sectors_handled;
1443 r10_bio->state = 0;
1444 goto retry_write;
1445 }
NeilBrown079fa162011-09-10 17:21:23 +10001446 one_write_done(r10_bio);
1447
1448 /* In case raid10d snuck in to freeze_array */
1449 wake_up(&conf->wait_barrier);
NeilBrownd4432c22011-07-28 11:39:24 +10001450
NeilBrownc3b328a2011-04-18 18:25:43 +10001451 if (do_sync || !mddev->bitmap || !plugged)
Lars Ellenberge3881a62007-01-10 23:15:37 -08001452 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
NeilBrownfd01b882011-10-11 16:47:53 +11001455static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
NeilBrowne879a872011-10-11 16:49:02 +11001457 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 int i;
1459
NeilBrown5cf00fc2012-05-21 09:28:20 +10001460 if (conf->geo.near_copies < conf->geo.raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +10001461 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
NeilBrown5cf00fc2012-05-21 09:28:20 +10001462 if (conf->geo.near_copies > 1)
1463 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1464 if (conf->geo.far_copies > 1) {
1465 if (conf->geo.far_offset)
1466 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001467 else
NeilBrown5cf00fc2012-05-21 09:28:20 +10001468 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001469 }
NeilBrown5cf00fc2012-05-21 09:28:20 +10001470 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1471 conf->geo.raid_disks - mddev->degraded);
1472 for (i = 0; i < conf->geo.raid_disks; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 seq_printf(seq, "%s",
1474 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001475 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 seq_printf(seq, "]");
1477}
1478
NeilBrown700c7212011-07-27 11:00:36 +10001479/* check if there are enough drives for
1480 * every block to appear on atleast one.
1481 * Don't consider the device numbered 'ignore'
1482 * as we might be about to remove it.
1483 */
NeilBrownf8c9e742012-05-21 09:28:33 +10001484static int _enough(struct r10conf *conf, struct geom *geo, int ignore)
NeilBrown700c7212011-07-27 11:00:36 +10001485{
1486 int first = 0;
1487
1488 do {
1489 int n = conf->copies;
1490 int cnt = 0;
1491 while (n--) {
1492 if (conf->mirrors[first].rdev &&
1493 first != ignore)
1494 cnt++;
NeilBrownf8c9e742012-05-21 09:28:33 +10001495 first = (first+1) % geo->raid_disks;
NeilBrown700c7212011-07-27 11:00:36 +10001496 }
1497 if (cnt == 0)
1498 return 0;
1499 } while (first != 0);
1500 return 1;
1501}
1502
NeilBrownf8c9e742012-05-21 09:28:33 +10001503static int enough(struct r10conf *conf, int ignore)
1504{
1505 return _enough(conf, &conf->geo, ignore) &&
1506 _enough(conf, &conf->prev, ignore);
1507}
1508
NeilBrownfd01b882011-10-11 16:47:53 +11001509static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
1511 char b[BDEVNAME_SIZE];
NeilBrowne879a872011-10-11 16:49:02 +11001512 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 /*
1515 * If it is not operational, then we have already marked it as dead
1516 * else if it is the last working disks, ignore the error, let the
1517 * next level up know.
1518 * else mark the drive as failed
1519 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001520 if (test_bit(In_sync, &rdev->flags)
NeilBrown700c7212011-07-27 11:00:36 +10001521 && !enough(conf, rdev->raid_disk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 /*
1523 * Don't fail the drive, just return an IO error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 */
1525 return;
NeilBrownc04be0a2006-10-03 01:15:53 -07001526 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1527 unsigned long flags;
1528 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001530 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 /*
1532 * if recovery is running, make sure it aborts.
1533 */
NeilBrowndfc70642008-05-23 13:04:39 -07001534 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
NeilBrownde393cd2011-07-28 11:31:48 +10001536 set_bit(Blocked, &rdev->flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001537 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b422006-10-03 01:15:46 -07001538 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Joe Perches067032b2011-01-14 09:14:33 +11001539 printk(KERN_ALERT
1540 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1541 "md/raid10:%s: Operation continuing on %d devices.\n",
NeilBrown128595e2010-05-03 14:47:14 +10001542 mdname(mddev), bdevname(rdev->bdev, b),
NeilBrown5cf00fc2012-05-21 09:28:20 +10001543 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
NeilBrowne879a872011-10-11 16:49:02 +11001546static void print_conf(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 int i;
NeilBrown0f6d02d2011-10-11 16:48:46 +11001549 struct mirror_info *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
NeilBrown128595e2010-05-03 14:47:14 +10001551 printk(KERN_DEBUG "RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if (!conf) {
NeilBrown128595e2010-05-03 14:47:14 +10001553 printk(KERN_DEBUG "(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return;
1555 }
NeilBrown5cf00fc2012-05-21 09:28:20 +10001556 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
1557 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
NeilBrown5cf00fc2012-05-21 09:28:20 +10001559 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 char b[BDEVNAME_SIZE];
1561 tmp = conf->mirrors + i;
1562 if (tmp->rdev)
NeilBrown128595e2010-05-03 14:47:14 +10001563 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001564 i, !test_bit(In_sync, &tmp->rdev->flags),
1565 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 bdevname(tmp->rdev->bdev,b));
1567 }
1568}
1569
NeilBrowne879a872011-10-11 16:49:02 +11001570static void close_sync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
NeilBrown0a27ec92006-01-06 00:20:13 -08001572 wait_barrier(conf);
1573 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 mempool_destroy(conf->r10buf_pool);
1576 conf->r10buf_pool = NULL;
1577}
1578
NeilBrownfd01b882011-10-11 16:47:53 +11001579static int raid10_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
1581 int i;
NeilBrowne879a872011-10-11 16:49:02 +11001582 struct r10conf *conf = mddev->private;
NeilBrown0f6d02d2011-10-11 16:48:46 +11001583 struct mirror_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10001584 int count = 0;
1585 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 /*
1588 * Find all non-in_sync disks within the RAID10 configuration
1589 * and mark them in_sync
1590 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10001591 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 tmp = conf->mirrors + i;
NeilBrown4ca40c22011-12-23 10:17:55 +11001593 if (tmp->replacement
1594 && tmp->replacement->recovery_offset == MaxSector
1595 && !test_bit(Faulty, &tmp->replacement->flags)
1596 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1597 /* Replacement has just become active */
1598 if (!tmp->rdev
1599 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1600 count++;
1601 if (tmp->rdev) {
1602 /* Replaced device not technically faulty,
1603 * but we need to be sure it gets removed
1604 * and never re-added.
1605 */
1606 set_bit(Faulty, &tmp->rdev->flags);
1607 sysfs_notify_dirent_safe(
1608 tmp->rdev->sysfs_state);
1609 }
1610 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1611 } else if (tmp->rdev
1612 && !test_bit(Faulty, &tmp->rdev->flags)
1613 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001614 count++;
Adrian Drzewieckie6ffbcb2010-08-18 11:49:02 +10001615 sysfs_notify_dirent(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617 }
NeilBrown6b965622010-08-18 11:56:59 +10001618 spin_lock_irqsave(&conf->device_lock, flags);
1619 mddev->degraded -= count;
1620 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001623 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624}
1625
1626
NeilBrownfd01b882011-10-11 16:47:53 +11001627static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
NeilBrowne879a872011-10-11 16:49:02 +11001629 struct r10conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001630 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 int mirror;
Neil Brown6c2fce22008-06-28 08:31:31 +10001632 int first = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10001633 int last = conf->geo.raid_disks - 1;
NeilBrown050b6612012-03-19 12:46:39 +11001634 struct request_queue *q = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 if (mddev->recovery_cp < MaxSector)
1637 /* only hot-add to in-sync arrays, as recovery is
1638 * very different from resync
1639 */
Neil Brown199050e2008-06-28 08:31:33 +10001640 return -EBUSY;
NeilBrownf8c9e742012-05-21 09:28:33 +10001641 if (rdev->saved_raid_disk < 0 && !_enough(conf, &conf->prev, -1))
Neil Brown199050e2008-06-28 08:31:33 +10001642 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
NeilBrowna53a6c82008-11-06 17:28:20 +11001644 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001645 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
NeilBrown050b6612012-03-19 12:46:39 +11001647 if (q->merge_bvec_fn) {
1648 set_bit(Unmerged, &rdev->flags);
1649 mddev->merge_check_needed = 1;
1650 }
1651
Namhyung Kim2c4193d2011-07-18 17:38:43 +10001652 if (rdev->saved_raid_disk >= first &&
NeilBrown6cce3b22006-01-06 00:20:16 -08001653 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1654 mirror = rdev->saved_raid_disk;
1655 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001656 mirror = first;
NeilBrown2bb77732011-07-27 11:00:36 +10001657 for ( ; mirror <= last ; mirror++) {
NeilBrown0f6d02d2011-10-11 16:48:46 +11001658 struct mirror_info *p = &conf->mirrors[mirror];
NeilBrown2bb77732011-07-27 11:00:36 +10001659 if (p->recovery_disabled == mddev->recovery_disabled)
1660 continue;
NeilBrownb7044d42011-12-23 10:17:56 +11001661 if (p->rdev) {
1662 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1663 p->replacement != NULL)
1664 continue;
1665 clear_bit(In_sync, &rdev->flags);
1666 set_bit(Replacement, &rdev->flags);
1667 rdev->raid_disk = mirror;
1668 err = 0;
1669 disk_stack_limits(mddev->gendisk, rdev->bdev,
1670 rdev->data_offset << 9);
NeilBrownb7044d42011-12-23 10:17:56 +11001671 conf->fullsync = 1;
1672 rcu_assign_pointer(p->replacement, rdev);
1673 break;
1674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
NeilBrown2bb77732011-07-27 11:00:36 +10001676 disk_stack_limits(mddev->gendisk, rdev->bdev,
1677 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
NeilBrown2bb77732011-07-27 11:00:36 +10001679 p->head_position = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11001680 p->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown2bb77732011-07-27 11:00:36 +10001681 rdev->raid_disk = mirror;
1682 err = 0;
1683 if (rdev->saved_raid_disk != mirror)
1684 conf->fullsync = 1;
1685 rcu_assign_pointer(p->rdev, rdev);
1686 break;
1687 }
NeilBrown050b6612012-03-19 12:46:39 +11001688 if (err == 0 && test_bit(Unmerged, &rdev->flags)) {
1689 /* Some requests might not have seen this new
1690 * merge_bvec_fn. We must wait for them to complete
1691 * before merging the device fully.
1692 * First we make sure any code which has tested
1693 * our function has submitted the request, then
1694 * we wait for all outstanding requests to complete.
1695 */
1696 synchronize_sched();
1697 raise_barrier(conf, 0);
1698 lower_barrier(conf);
1699 clear_bit(Unmerged, &rdev->flags);
1700 }
Andre Nollac5e7112009-08-03 10:59:47 +10001701 md_integrity_add_rdev(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001703 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
NeilBrownb8321b62011-12-23 10:17:51 +11001706static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
NeilBrowne879a872011-10-11 16:49:02 +11001708 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11001710 int number = rdev->raid_disk;
NeilBrownc8ab9032011-12-23 10:17:54 +11001711 struct md_rdev **rdevp;
1712 struct mirror_info *p = conf->mirrors + number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 print_conf(conf);
NeilBrownc8ab9032011-12-23 10:17:54 +11001715 if (rdev == p->rdev)
1716 rdevp = &p->rdev;
1717 else if (rdev == p->replacement)
1718 rdevp = &p->replacement;
1719 else
1720 return 0;
1721
1722 if (test_bit(In_sync, &rdev->flags) ||
1723 atomic_read(&rdev->nr_pending)) {
1724 err = -EBUSY;
1725 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 }
NeilBrownc8ab9032011-12-23 10:17:54 +11001727 /* Only remove faulty devices if recovery
1728 * is not possible.
1729 */
1730 if (!test_bit(Faulty, &rdev->flags) &&
1731 mddev->recovery_disabled != p->recovery_disabled &&
NeilBrown4ca40c22011-12-23 10:17:55 +11001732 (!p->replacement || p->replacement == rdev) &&
NeilBrownc8ab9032011-12-23 10:17:54 +11001733 enough(conf, -1)) {
1734 err = -EBUSY;
1735 goto abort;
1736 }
1737 *rdevp = NULL;
1738 synchronize_rcu();
1739 if (atomic_read(&rdev->nr_pending)) {
1740 /* lost the race, try later */
1741 err = -EBUSY;
1742 *rdevp = rdev;
1743 goto abort;
NeilBrown4ca40c22011-12-23 10:17:55 +11001744 } else if (p->replacement) {
1745 /* We must have just cleared 'rdev' */
1746 p->rdev = p->replacement;
1747 clear_bit(Replacement, &p->replacement->flags);
1748 smp_mb(); /* Make sure other CPUs may see both as identical
1749 * but will never see neither -- if they are careful.
1750 */
1751 p->replacement = NULL;
1752 clear_bit(WantReplacement, &rdev->flags);
1753 } else
1754 /* We might have just remove the Replacement as faulty
1755 * Clear the flag just in case
1756 */
1757 clear_bit(WantReplacement, &rdev->flags);
1758
NeilBrownc8ab9032011-12-23 10:17:54 +11001759 err = md_integrity_register(mddev);
1760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761abort:
1762
1763 print_conf(conf);
1764 return err;
1765}
1766
1767
NeilBrown6712ecf2007-09-27 12:47:43 +02001768static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001770 struct r10bio *r10_bio = bio->bi_private;
NeilBrowne879a872011-10-11 16:49:02 +11001771 struct r10conf *conf = r10_bio->mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001772 int d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
NeilBrown3ea7daa2012-05-22 13:53:47 +10001774 if (bio == r10_bio->master_bio) {
1775 /* this is a reshape read */
1776 d = r10_bio->read_slot; /* really the read dev */
1777 } else
1778 d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001779
1780 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1781 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrowne684e412011-07-28 11:39:25 +10001782 else
1783 /* The write handler will notice the lack of
1784 * R10BIO_Uptodate and record any errors etc
1785 */
NeilBrown4dbcdc72006-01-06 00:20:52 -08001786 atomic_add(r10_bio->sectors,
1787 &conf->mirrors[d].rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 /* for reconstruct, we always reschedule after a read.
1790 * for resync, only after all reads
1791 */
NeilBrown73d5c382009-02-25 13:18:47 +11001792 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1794 atomic_dec_and_test(&r10_bio->remaining)) {
1795 /* we have read all the blocks,
1796 * do the comparison in process context in raid10d
1797 */
1798 reschedule_retry(r10_bio);
1799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800}
1801
NeilBrown9f2c9d12011-10-11 16:48:43 +11001802static void end_sync_request(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001803{
NeilBrownfd01b882011-10-11 16:47:53 +11001804 struct mddev *mddev = r10_bio->mddev;
NeilBrown5e570282011-07-28 11:39:25 +10001805
1806 while (atomic_dec_and_test(&r10_bio->remaining)) {
1807 if (r10_bio->master_bio == NULL) {
1808 /* the primary of several recovery bios */
1809 sector_t s = r10_bio->sectors;
1810 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1811 test_bit(R10BIO_WriteError, &r10_bio->state))
1812 reschedule_retry(r10_bio);
1813 else
1814 put_buf(r10_bio);
1815 md_done_sync(mddev, s, 1);
1816 break;
1817 } else {
NeilBrown9f2c9d12011-10-11 16:48:43 +11001818 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
NeilBrown5e570282011-07-28 11:39:25 +10001819 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1820 test_bit(R10BIO_WriteError, &r10_bio->state))
1821 reschedule_retry(r10_bio);
1822 else
1823 put_buf(r10_bio);
1824 r10_bio = r10_bio2;
1825 }
1826 }
1827}
1828
NeilBrown6712ecf2007-09-27 12:47:43 +02001829static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
1831 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +11001832 struct r10bio *r10_bio = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11001833 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001834 struct r10conf *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001835 int d;
NeilBrown749c55e2011-07-28 11:39:24 +10001836 sector_t first_bad;
1837 int bad_sectors;
1838 int slot;
NeilBrown9ad1aef2011-12-23 10:17:55 +11001839 int repl;
NeilBrown4ca40c22011-12-23 10:17:55 +11001840 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
NeilBrown9ad1aef2011-12-23 10:17:55 +11001842 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1843 if (repl)
1844 rdev = conf->mirrors[d].replacement;
NeilBrown547414d2012-03-13 11:21:20 +11001845 else
NeilBrown9ad1aef2011-12-23 10:17:55 +11001846 rdev = conf->mirrors[d].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
NeilBrown1a0b7cd2011-07-28 11:39:25 +10001848 if (!uptodate) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11001849 if (repl)
1850 md_error(mddev, rdev);
1851 else {
1852 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11001853 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1854 set_bit(MD_RECOVERY_NEEDED,
1855 &rdev->mddev->recovery);
NeilBrown9ad1aef2011-12-23 10:17:55 +11001856 set_bit(R10BIO_WriteError, &r10_bio->state);
1857 }
1858 } else if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +10001859 r10_bio->devs[slot].addr,
1860 r10_bio->sectors,
1861 &first_bad, &bad_sectors))
1862 set_bit(R10BIO_MadeGood, &r10_bio->state);
NeilBrowndfc70642008-05-23 13:04:39 -07001863
NeilBrown9ad1aef2011-12-23 10:17:55 +11001864 rdev_dec_pending(rdev, mddev);
NeilBrown5e570282011-07-28 11:39:25 +10001865
1866 end_sync_request(r10_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
1868
1869/*
1870 * Note: sync and recover and handled very differently for raid10
1871 * This code is for resync.
1872 * For resync, we read through virtual addresses and read all blocks.
1873 * If there is any error, we schedule a write. The lowest numbered
1874 * drive is authoritative.
1875 * However requests come for physical address, so we need to map.
1876 * For every physical address there are raid_disks/copies virtual addresses,
1877 * which is always are least one, but is not necessarly an integer.
1878 * This means that a physical address can span multiple chunks, so we may
1879 * have to submit multiple io requests for a single sync request.
1880 */
1881/*
1882 * We check if all blocks are in-sync and only write to blocks that
1883 * aren't in sync
1884 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11001885static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
NeilBrowne879a872011-10-11 16:49:02 +11001887 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 int i, first;
1889 struct bio *tbio, *fbio;
majianpengf4380a92012-04-12 16:04:47 +10001890 int vcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 atomic_set(&r10_bio->remaining, 1);
1893
1894 /* find the first device with a block */
1895 for (i=0; i<conf->copies; i++)
1896 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1897 break;
1898
1899 if (i == conf->copies)
1900 goto done;
1901
1902 first = i;
1903 fbio = r10_bio->devs[i].bio;
1904
majianpengf4380a92012-04-12 16:04:47 +10001905 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001907 for (i=0 ; i < conf->copies ; i++) {
1908 int j, d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001911
1912 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001914 if (i == first)
1915 continue;
1916 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1917 /* We know that the bi_io_vec layout is the same for
1918 * both 'first' and 'i', so we just compare them.
1919 * All vec entries are PAGE_SIZE;
1920 */
1921 for (j = 0; j < vcnt; j++)
1922 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1923 page_address(tbio->bi_io_vec[j].bv_page),
NeilBrown5020ad72012-04-02 01:39:05 +10001924 fbio->bi_io_vec[j].bv_len))
NeilBrown0eb3ff12006-01-06 00:20:29 -08001925 break;
1926 if (j == vcnt)
1927 continue;
1928 mddev->resync_mismatches += r10_bio->sectors;
NeilBrownf84ee362011-07-28 11:39:25 +10001929 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1930 /* Don't fix anything. */
1931 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001932 }
NeilBrownf84ee362011-07-28 11:39:25 +10001933 /* Ok, we need to write this bio, either to correct an
1934 * inconsistency or to correct an unreadable block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 * First we need to fixup bv_offset, bv_len and
1936 * bi_vecs, as the read request might have corrupted these
1937 */
1938 tbio->bi_vcnt = vcnt;
1939 tbio->bi_size = r10_bio->sectors << 9;
1940 tbio->bi_idx = 0;
1941 tbio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1943 tbio->bi_flags |= 1 << BIO_UPTODATE;
1944 tbio->bi_next = NULL;
1945 tbio->bi_rw = WRITE;
1946 tbio->bi_private = r10_bio;
1947 tbio->bi_sector = r10_bio->devs[i].addr;
1948
1949 for (j=0; j < vcnt ; j++) {
1950 tbio->bi_io_vec[j].bv_offset = 0;
1951 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1952
1953 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1954 page_address(fbio->bi_io_vec[j].bv_page),
1955 PAGE_SIZE);
1956 }
1957 tbio->bi_end_io = end_sync_write;
1958
1959 d = r10_bio->devs[i].devnum;
1960 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1961 atomic_inc(&r10_bio->remaining);
1962 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1963
1964 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1965 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1966 generic_make_request(tbio);
1967 }
1968
NeilBrown9ad1aef2011-12-23 10:17:55 +11001969 /* Now write out to any replacement devices
1970 * that are active
1971 */
1972 for (i = 0; i < conf->copies; i++) {
1973 int j, d;
NeilBrown9ad1aef2011-12-23 10:17:55 +11001974
1975 tbio = r10_bio->devs[i].repl_bio;
1976 if (!tbio || !tbio->bi_end_io)
1977 continue;
1978 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
1979 && r10_bio->devs[i].bio != fbio)
1980 for (j = 0; j < vcnt; j++)
1981 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1982 page_address(fbio->bi_io_vec[j].bv_page),
1983 PAGE_SIZE);
1984 d = r10_bio->devs[i].devnum;
1985 atomic_inc(&r10_bio->remaining);
1986 md_sync_acct(conf->mirrors[d].replacement->bdev,
1987 tbio->bi_size >> 9);
1988 generic_make_request(tbio);
1989 }
1990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991done:
1992 if (atomic_dec_and_test(&r10_bio->remaining)) {
1993 md_done_sync(mddev, r10_bio->sectors, 1);
1994 put_buf(r10_bio);
1995 }
1996}
1997
1998/*
1999 * Now for the recovery code.
2000 * Recovery happens across physical sectors.
2001 * We recover all non-is_sync drives by finding the virtual address of
2002 * each, and then choose a working drive that also has that virt address.
2003 * There is a separate r10_bio for each non-in_sync drive.
2004 * Only the first two slots are in use. The first for reading,
2005 * The second for writing.
2006 *
2007 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11002008static void fix_recovery_read_error(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10002009{
2010 /* We got a read error during recovery.
2011 * We repeat the read in smaller page-sized sections.
2012 * If a read succeeds, write it to the new device or record
2013 * a bad block if we cannot.
2014 * If a read fails, record a bad block on both old and
2015 * new devices.
2016 */
NeilBrownfd01b882011-10-11 16:47:53 +11002017 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002018 struct r10conf *conf = mddev->private;
NeilBrown5e570282011-07-28 11:39:25 +10002019 struct bio *bio = r10_bio->devs[0].bio;
2020 sector_t sect = 0;
2021 int sectors = r10_bio->sectors;
2022 int idx = 0;
2023 int dr = r10_bio->devs[0].devnum;
2024 int dw = r10_bio->devs[1].devnum;
2025
2026 while (sectors) {
2027 int s = sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002028 struct md_rdev *rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002029 sector_t addr;
2030 int ok;
2031
2032 if (s > (PAGE_SIZE>>9))
2033 s = PAGE_SIZE >> 9;
2034
2035 rdev = conf->mirrors[dr].rdev;
2036 addr = r10_bio->devs[0].addr + sect,
2037 ok = sync_page_io(rdev,
2038 addr,
2039 s << 9,
2040 bio->bi_io_vec[idx].bv_page,
2041 READ, false);
2042 if (ok) {
2043 rdev = conf->mirrors[dw].rdev;
2044 addr = r10_bio->devs[1].addr + sect;
2045 ok = sync_page_io(rdev,
2046 addr,
2047 s << 9,
2048 bio->bi_io_vec[idx].bv_page,
2049 WRITE, false);
NeilBrownb7044d42011-12-23 10:17:56 +11002050 if (!ok) {
NeilBrown5e570282011-07-28 11:39:25 +10002051 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002052 if (!test_and_set_bit(WantReplacement,
2053 &rdev->flags))
2054 set_bit(MD_RECOVERY_NEEDED,
2055 &rdev->mddev->recovery);
2056 }
NeilBrown5e570282011-07-28 11:39:25 +10002057 }
2058 if (!ok) {
2059 /* We don't worry if we cannot set a bad block -
2060 * it really is bad so there is no loss in not
2061 * recording it yet
2062 */
2063 rdev_set_badblocks(rdev, addr, s, 0);
2064
2065 if (rdev != conf->mirrors[dw].rdev) {
2066 /* need bad block on destination too */
NeilBrown3cb03002011-10-11 16:45:26 +11002067 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002068 addr = r10_bio->devs[1].addr + sect;
2069 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2070 if (!ok) {
2071 /* just abort the recovery */
2072 printk(KERN_NOTICE
2073 "md/raid10:%s: recovery aborted"
2074 " due to read error\n",
2075 mdname(mddev));
2076
2077 conf->mirrors[dw].recovery_disabled
2078 = mddev->recovery_disabled;
2079 set_bit(MD_RECOVERY_INTR,
2080 &mddev->recovery);
2081 break;
2082 }
2083 }
2084 }
2085
2086 sectors -= s;
2087 sect += s;
2088 idx++;
2089 }
2090}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
NeilBrown9f2c9d12011-10-11 16:48:43 +11002092static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
NeilBrowne879a872011-10-11 16:49:02 +11002094 struct r10conf *conf = mddev->private;
Namhyung Kimc65060a2011-07-18 17:38:49 +10002095 int d;
NeilBrown24afd802011-12-23 10:17:55 +11002096 struct bio *wbio, *wbio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
NeilBrown5e570282011-07-28 11:39:25 +10002098 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2099 fix_recovery_read_error(r10_bio);
2100 end_sync_request(r10_bio);
2101 return;
2102 }
2103
Namhyung Kimc65060a2011-07-18 17:38:49 +10002104 /*
2105 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 * and submit the write request
2107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 d = r10_bio->devs[1].devnum;
NeilBrown24afd802011-12-23 10:17:55 +11002109 wbio = r10_bio->devs[1].bio;
2110 wbio2 = r10_bio->devs[1].repl_bio;
2111 if (wbio->bi_end_io) {
2112 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2113 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
2114 generic_make_request(wbio);
2115 }
2116 if (wbio2 && wbio2->bi_end_io) {
2117 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2118 md_sync_acct(conf->mirrors[d].replacement->bdev,
2119 wbio2->bi_size >> 9);
2120 generic_make_request(wbio2);
2121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
2124
2125/*
Robert Becker1e509152009-12-14 12:49:58 +11002126 * Used by fix_read_error() to decay the per rdev read_errors.
2127 * We halve the read error count for every hour that has elapsed
2128 * since the last recorded read error.
2129 *
2130 */
NeilBrownfd01b882011-10-11 16:47:53 +11002131static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
Robert Becker1e509152009-12-14 12:49:58 +11002132{
2133 struct timespec cur_time_mon;
2134 unsigned long hours_since_last;
2135 unsigned int read_errors = atomic_read(&rdev->read_errors);
2136
2137 ktime_get_ts(&cur_time_mon);
2138
2139 if (rdev->last_read_error.tv_sec == 0 &&
2140 rdev->last_read_error.tv_nsec == 0) {
2141 /* first time we've seen a read error */
2142 rdev->last_read_error = cur_time_mon;
2143 return;
2144 }
2145
2146 hours_since_last = (cur_time_mon.tv_sec -
2147 rdev->last_read_error.tv_sec) / 3600;
2148
2149 rdev->last_read_error = cur_time_mon;
2150
2151 /*
2152 * if hours_since_last is > the number of bits in read_errors
2153 * just set read errors to 0. We do this to avoid
2154 * overflowing the shift of read_errors by hours_since_last.
2155 */
2156 if (hours_since_last >= 8 * sizeof(read_errors))
2157 atomic_set(&rdev->read_errors, 0);
2158 else
2159 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2160}
2161
NeilBrown3cb03002011-10-11 16:45:26 +11002162static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrown58c54fc2011-07-28 11:39:25 +10002163 int sectors, struct page *page, int rw)
2164{
2165 sector_t first_bad;
2166 int bad_sectors;
2167
2168 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2169 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2170 return -1;
2171 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
2172 /* success */
2173 return 1;
NeilBrownb7044d42011-12-23 10:17:56 +11002174 if (rw == WRITE) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002175 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002176 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2177 set_bit(MD_RECOVERY_NEEDED,
2178 &rdev->mddev->recovery);
2179 }
NeilBrown58c54fc2011-07-28 11:39:25 +10002180 /* need to record an error - either for the block or the device */
2181 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2182 md_error(rdev->mddev, rdev);
2183 return 0;
2184}
2185
Robert Becker1e509152009-12-14 12:49:58 +11002186/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 * This is a kernel thread which:
2188 *
2189 * 1. Retries failed read operations on working mirrors.
2190 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07002191 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 */
2193
NeilBrowne879a872011-10-11 16:49:02 +11002194static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown6814d532006-10-03 01:15:45 -07002195{
2196 int sect = 0; /* Offset from r10_bio->sector */
2197 int sectors = r10_bio->sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002198 struct md_rdev*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002199 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002200 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11002201
NeilBrown7c4e06f2011-05-11 14:53:17 +10002202 /* still own a reference to this rdev, so it cannot
2203 * have been cleared recently.
2204 */
2205 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002206
NeilBrown7c4e06f2011-05-11 14:53:17 +10002207 if (test_bit(Faulty, &rdev->flags))
2208 /* drive has already been failed, just ignore any
2209 more fix_read_error() attempts */
2210 return;
2211
2212 check_decay_read_errors(mddev, rdev);
2213 atomic_inc(&rdev->read_errors);
2214 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2215 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11002216 bdevname(rdev->bdev, b);
2217
NeilBrown7c4e06f2011-05-11 14:53:17 +10002218 printk(KERN_NOTICE
2219 "md/raid10:%s: %s: Raid device exceeded "
2220 "read_error threshold [cur %d:max %d]\n",
2221 mdname(mddev), b,
2222 atomic_read(&rdev->read_errors), max_read_errors);
2223 printk(KERN_NOTICE
2224 "md/raid10:%s: %s: Failing raid device\n",
2225 mdname(mddev), b);
2226 md_error(mddev, conf->mirrors[d].rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002227 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
NeilBrown7c4e06f2011-05-11 14:53:17 +10002228 return;
Robert Becker1e509152009-12-14 12:49:58 +11002229 }
Robert Becker1e509152009-12-14 12:49:58 +11002230
NeilBrown6814d532006-10-03 01:15:45 -07002231 while(sectors) {
2232 int s = sectors;
2233 int sl = r10_bio->read_slot;
2234 int success = 0;
2235 int start;
2236
2237 if (s > (PAGE_SIZE>>9))
2238 s = PAGE_SIZE >> 9;
2239
2240 rcu_read_lock();
2241 do {
NeilBrown8dbed5c2011-07-28 11:39:24 +10002242 sector_t first_bad;
2243 int bad_sectors;
2244
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002245 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07002246 rdev = rcu_dereference(conf->mirrors[d].rdev);
2247 if (rdev &&
NeilBrown050b6612012-03-19 12:46:39 +11002248 !test_bit(Unmerged, &rdev->flags) &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002249 test_bit(In_sync, &rdev->flags) &&
2250 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2251 &first_bad, &bad_sectors) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07002252 atomic_inc(&rdev->nr_pending);
2253 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11002254 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07002255 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002256 sect,
NeilBrown6814d532006-10-03 01:15:45 -07002257 s<<9,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002258 conf->tmppage, READ, false);
NeilBrown6814d532006-10-03 01:15:45 -07002259 rdev_dec_pending(rdev, mddev);
2260 rcu_read_lock();
2261 if (success)
2262 break;
2263 }
2264 sl++;
2265 if (sl == conf->copies)
2266 sl = 0;
2267 } while (!success && sl != r10_bio->read_slot);
2268 rcu_read_unlock();
2269
2270 if (!success) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002271 /* Cannot read from anywhere, just mark the block
2272 * as bad on the first device to discourage future
2273 * reads.
2274 */
NeilBrown6814d532006-10-03 01:15:45 -07002275 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
NeilBrown58c54fc2011-07-28 11:39:25 +10002276 rdev = conf->mirrors[dn].rdev;
2277
2278 if (!rdev_set_badblocks(
2279 rdev,
2280 r10_bio->devs[r10_bio->read_slot].addr
2281 + sect,
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002282 s, 0)) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002283 md_error(mddev, rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002284 r10_bio->devs[r10_bio->read_slot].bio
2285 = IO_BLOCKED;
2286 }
NeilBrown6814d532006-10-03 01:15:45 -07002287 break;
2288 }
2289
2290 start = sl;
2291 /* write it back and re-read */
2292 rcu_read_lock();
2293 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11002294 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002295
NeilBrown6814d532006-10-03 01:15:45 -07002296 if (sl==0)
2297 sl = conf->copies;
2298 sl--;
2299 d = r10_bio->devs[sl].devnum;
2300 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002301 if (!rdev ||
NeilBrown050b6612012-03-19 12:46:39 +11002302 test_bit(Unmerged, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002303 !test_bit(In_sync, &rdev->flags))
2304 continue;
2305
2306 atomic_inc(&rdev->nr_pending);
2307 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002308 if (r10_sync_page_io(rdev,
2309 r10_bio->devs[sl].addr +
2310 sect,
2311 s<<9, conf->tmppage, WRITE)
NeilBrown1294b9c2011-07-28 11:39:23 +10002312 == 0) {
2313 /* Well, this device is dead */
2314 printk(KERN_NOTICE
2315 "md/raid10:%s: read correction "
2316 "write failed"
2317 " (%d sectors at %llu on %s)\n",
2318 mdname(mddev), s,
2319 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002320 sect +
2321 choose_data_offset(r10_bio,
2322 rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002323 bdevname(rdev->bdev, b));
2324 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2325 "drive\n",
2326 mdname(mddev),
2327 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07002328 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002329 rdev_dec_pending(rdev, mddev);
2330 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002331 }
2332 sl = start;
2333 while (sl != r10_bio->read_slot) {
NeilBrown1294b9c2011-07-28 11:39:23 +10002334 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002335
NeilBrown6814d532006-10-03 01:15:45 -07002336 if (sl==0)
2337 sl = conf->copies;
2338 sl--;
2339 d = r10_bio->devs[sl].devnum;
2340 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002341 if (!rdev ||
2342 !test_bit(In_sync, &rdev->flags))
2343 continue;
Robert Becker67b8dc42009-12-14 12:49:57 +11002344
NeilBrown1294b9c2011-07-28 11:39:23 +10002345 atomic_inc(&rdev->nr_pending);
2346 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002347 switch (r10_sync_page_io(rdev,
2348 r10_bio->devs[sl].addr +
2349 sect,
2350 s<<9, conf->tmppage,
2351 READ)) {
2352 case 0:
NeilBrown1294b9c2011-07-28 11:39:23 +10002353 /* Well, this device is dead */
2354 printk(KERN_NOTICE
2355 "md/raid10:%s: unable to read back "
2356 "corrected sectors"
2357 " (%d sectors at %llu on %s)\n",
2358 mdname(mddev), s,
2359 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002360 sect +
2361 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002362 bdevname(rdev->bdev, b));
2363 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2364 "drive\n",
2365 mdname(mddev),
2366 bdevname(rdev->bdev, b));
NeilBrown58c54fc2011-07-28 11:39:25 +10002367 break;
2368 case 1:
NeilBrown1294b9c2011-07-28 11:39:23 +10002369 printk(KERN_INFO
2370 "md/raid10:%s: read error corrected"
2371 " (%d sectors at %llu on %s)\n",
2372 mdname(mddev), s,
2373 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002374 sect +
2375 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002376 bdevname(rdev->bdev, b));
2377 atomic_add(s, &rdev->corrected_errors);
NeilBrown6814d532006-10-03 01:15:45 -07002378 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002379
2380 rdev_dec_pending(rdev, mddev);
2381 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002382 }
2383 rcu_read_unlock();
2384
2385 sectors -= s;
2386 sect += s;
2387 }
2388}
2389
NeilBrownbd870a12011-07-28 11:39:24 +10002390static void bi_complete(struct bio *bio, int error)
2391{
2392 complete((struct completion *)bio->bi_private);
2393}
2394
2395static int submit_bio_wait(int rw, struct bio *bio)
2396{
2397 struct completion event;
2398 rw |= REQ_SYNC;
2399
2400 init_completion(&event);
2401 bio->bi_private = &event;
2402 bio->bi_end_io = bi_complete;
2403 submit_bio(rw, bio);
2404 wait_for_completion(&event);
2405
2406 return test_bit(BIO_UPTODATE, &bio->bi_flags);
2407}
2408
NeilBrown9f2c9d12011-10-11 16:48:43 +11002409static int narrow_write_error(struct r10bio *r10_bio, int i)
NeilBrownbd870a12011-07-28 11:39:24 +10002410{
2411 struct bio *bio = r10_bio->master_bio;
NeilBrownfd01b882011-10-11 16:47:53 +11002412 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002413 struct r10conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002414 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
NeilBrownbd870a12011-07-28 11:39:24 +10002415 /* bio has the data to be written to slot 'i' where
2416 * we just recently had a write error.
2417 * We repeatedly clone the bio and trim down to one block,
2418 * then try the write. Where the write fails we record
2419 * a bad block.
2420 * It is conceivable that the bio doesn't exactly align with
2421 * blocks. We must handle this.
2422 *
2423 * We currently own a reference to the rdev.
2424 */
2425
2426 int block_sectors;
2427 sector_t sector;
2428 int sectors;
2429 int sect_to_write = r10_bio->sectors;
2430 int ok = 1;
2431
2432 if (rdev->badblocks.shift < 0)
2433 return 0;
2434
2435 block_sectors = 1 << rdev->badblocks.shift;
2436 sector = r10_bio->sector;
2437 sectors = ((r10_bio->sector + block_sectors)
2438 & ~(sector_t)(block_sectors - 1))
2439 - sector;
2440
2441 while (sect_to_write) {
2442 struct bio *wbio;
2443 if (sectors > sect_to_write)
2444 sectors = sect_to_write;
2445 /* Write at 'sector' for 'sectors' */
2446 wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
2447 md_trim_bio(wbio, sector - bio->bi_sector, sectors);
2448 wbio->bi_sector = (r10_bio->devs[i].addr+
NeilBrownf8c9e742012-05-21 09:28:33 +10002449 choose_data_offset(r10_bio, rdev) +
NeilBrownbd870a12011-07-28 11:39:24 +10002450 (sector - r10_bio->sector));
2451 wbio->bi_bdev = rdev->bdev;
2452 if (submit_bio_wait(WRITE, wbio) == 0)
2453 /* Failure! */
2454 ok = rdev_set_badblocks(rdev, sector,
2455 sectors, 0)
2456 && ok;
2457
2458 bio_put(wbio);
2459 sect_to_write -= sectors;
2460 sector += sectors;
2461 sectors = block_sectors;
2462 }
2463 return ok;
2464}
2465
NeilBrown9f2c9d12011-10-11 16:48:43 +11002466static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown560f8e52011-07-28 11:39:23 +10002467{
2468 int slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002469 struct bio *bio;
NeilBrowne879a872011-10-11 16:49:02 +11002470 struct r10conf *conf = mddev->private;
NeilBrownabbf0982011-12-23 10:17:54 +11002471 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002472 char b[BDEVNAME_SIZE];
2473 unsigned long do_sync;
NeilBrown856e08e2011-07-28 11:39:23 +10002474 int max_sectors;
NeilBrown560f8e52011-07-28 11:39:23 +10002475
2476 /* we got a read error. Maybe the drive is bad. Maybe just
2477 * the block and we can fix it.
2478 * We freeze all other IO, and try reading the block from
2479 * other devices. When we find one, we re-write
2480 * and check it that fixes the read error.
2481 * This is all done synchronously while the array is
2482 * frozen.
2483 */
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002484 bio = r10_bio->devs[slot].bio;
2485 bdevname(bio->bi_bdev, b);
2486 bio_put(bio);
2487 r10_bio->devs[slot].bio = NULL;
2488
NeilBrown560f8e52011-07-28 11:39:23 +10002489 if (mddev->ro == 0) {
2490 freeze_array(conf);
2491 fix_read_error(conf, mddev, r10_bio);
2492 unfreeze_array(conf);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002493 } else
2494 r10_bio->devs[slot].bio = IO_BLOCKED;
2495
NeilBrownabbf0982011-12-23 10:17:54 +11002496 rdev_dec_pending(rdev, mddev);
NeilBrown560f8e52011-07-28 11:39:23 +10002497
NeilBrown7399c312011-07-28 11:39:23 +10002498read_more:
NeilBrown96c3fd12011-12-23 10:17:54 +11002499 rdev = read_balance(conf, r10_bio, &max_sectors);
2500 if (rdev == NULL) {
NeilBrown560f8e52011-07-28 11:39:23 +10002501 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
2502 " read error for block %llu\n",
NeilBrown7399c312011-07-28 11:39:23 +10002503 mdname(mddev), b,
NeilBrown560f8e52011-07-28 11:39:23 +10002504 (unsigned long long)r10_bio->sector);
2505 raid_end_bio_io(r10_bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002506 return;
2507 }
2508
2509 do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
NeilBrown560f8e52011-07-28 11:39:23 +10002510 slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002511 printk_ratelimited(
2512 KERN_ERR
2513 "md/raid10:%s: %s: redirecting"
2514 "sector %llu to another mirror\n",
2515 mdname(mddev),
2516 bdevname(rdev->bdev, b),
2517 (unsigned long long)r10_bio->sector);
2518 bio = bio_clone_mddev(r10_bio->master_bio,
2519 GFP_NOIO, mddev);
NeilBrown7399c312011-07-28 11:39:23 +10002520 md_trim_bio(bio,
2521 r10_bio->sector - bio->bi_sector,
2522 max_sectors);
NeilBrown560f8e52011-07-28 11:39:23 +10002523 r10_bio->devs[slot].bio = bio;
NeilBrownabbf0982011-12-23 10:17:54 +11002524 r10_bio->devs[slot].rdev = rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002525 bio->bi_sector = r10_bio->devs[slot].addr
NeilBrownf8c9e742012-05-21 09:28:33 +10002526 + choose_data_offset(r10_bio, rdev);
NeilBrown560f8e52011-07-28 11:39:23 +10002527 bio->bi_bdev = rdev->bdev;
2528 bio->bi_rw = READ | do_sync;
2529 bio->bi_private = r10_bio;
2530 bio->bi_end_io = raid10_end_read_request;
NeilBrown7399c312011-07-28 11:39:23 +10002531 if (max_sectors < r10_bio->sectors) {
2532 /* Drat - have to split this up more */
2533 struct bio *mbio = r10_bio->master_bio;
2534 int sectors_handled =
2535 r10_bio->sector + max_sectors
2536 - mbio->bi_sector;
2537 r10_bio->sectors = max_sectors;
2538 spin_lock_irq(&conf->device_lock);
2539 if (mbio->bi_phys_segments == 0)
2540 mbio->bi_phys_segments = 2;
2541 else
2542 mbio->bi_phys_segments++;
2543 spin_unlock_irq(&conf->device_lock);
2544 generic_make_request(bio);
NeilBrown7399c312011-07-28 11:39:23 +10002545
2546 r10_bio = mempool_alloc(conf->r10bio_pool,
2547 GFP_NOIO);
2548 r10_bio->master_bio = mbio;
2549 r10_bio->sectors = (mbio->bi_size >> 9)
2550 - sectors_handled;
2551 r10_bio->state = 0;
2552 set_bit(R10BIO_ReadError,
2553 &r10_bio->state);
2554 r10_bio->mddev = mddev;
2555 r10_bio->sector = mbio->bi_sector
2556 + sectors_handled;
2557
2558 goto read_more;
2559 } else
2560 generic_make_request(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002561}
2562
NeilBrowne879a872011-10-11 16:49:02 +11002563static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
NeilBrown749c55e2011-07-28 11:39:24 +10002564{
2565 /* Some sort of write request has finished and it
2566 * succeeded in writing where we thought there was a
2567 * bad block. So forget the bad block.
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002568 * Or possibly if failed and we need to record
2569 * a bad block.
NeilBrown749c55e2011-07-28 11:39:24 +10002570 */
2571 int m;
NeilBrown3cb03002011-10-11 16:45:26 +11002572 struct md_rdev *rdev;
NeilBrown749c55e2011-07-28 11:39:24 +10002573
2574 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2575 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002576 for (m = 0; m < conf->copies; m++) {
2577 int dev = r10_bio->devs[m].devnum;
2578 rdev = conf->mirrors[dev].rdev;
2579 if (r10_bio->devs[m].bio == NULL)
2580 continue;
2581 if (test_bit(BIO_UPTODATE,
NeilBrown749c55e2011-07-28 11:39:24 +10002582 &r10_bio->devs[m].bio->bi_flags)) {
NeilBrown749c55e2011-07-28 11:39:24 +10002583 rdev_clear_badblocks(
2584 rdev,
2585 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002586 r10_bio->sectors, 0);
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002587 } else {
2588 if (!rdev_set_badblocks(
2589 rdev,
2590 r10_bio->devs[m].addr,
2591 r10_bio->sectors, 0))
2592 md_error(conf->mddev, rdev);
NeilBrown749c55e2011-07-28 11:39:24 +10002593 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002594 rdev = conf->mirrors[dev].replacement;
2595 if (r10_bio->devs[m].repl_bio == NULL)
2596 continue;
2597 if (test_bit(BIO_UPTODATE,
2598 &r10_bio->devs[m].repl_bio->bi_flags)) {
2599 rdev_clear_badblocks(
2600 rdev,
2601 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002602 r10_bio->sectors, 0);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002603 } else {
2604 if (!rdev_set_badblocks(
2605 rdev,
2606 r10_bio->devs[m].addr,
2607 r10_bio->sectors, 0))
2608 md_error(conf->mddev, rdev);
2609 }
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002610 }
NeilBrown749c55e2011-07-28 11:39:24 +10002611 put_buf(r10_bio);
2612 } else {
NeilBrownbd870a12011-07-28 11:39:24 +10002613 for (m = 0; m < conf->copies; m++) {
2614 int dev = r10_bio->devs[m].devnum;
2615 struct bio *bio = r10_bio->devs[m].bio;
2616 rdev = conf->mirrors[dev].rdev;
2617 if (bio == IO_MADE_GOOD) {
NeilBrown749c55e2011-07-28 11:39:24 +10002618 rdev_clear_badblocks(
2619 rdev,
2620 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002621 r10_bio->sectors, 0);
NeilBrown749c55e2011-07-28 11:39:24 +10002622 rdev_dec_pending(rdev, conf->mddev);
NeilBrownbd870a12011-07-28 11:39:24 +10002623 } else if (bio != NULL &&
2624 !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2625 if (!narrow_write_error(r10_bio, m)) {
2626 md_error(conf->mddev, rdev);
2627 set_bit(R10BIO_Degraded,
2628 &r10_bio->state);
2629 }
2630 rdev_dec_pending(rdev, conf->mddev);
NeilBrown749c55e2011-07-28 11:39:24 +10002631 }
NeilBrown475b0322011-12-23 10:17:55 +11002632 bio = r10_bio->devs[m].repl_bio;
2633 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +11002634 if (rdev && bio == IO_MADE_GOOD) {
NeilBrown475b0322011-12-23 10:17:55 +11002635 rdev_clear_badblocks(
2636 rdev,
2637 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002638 r10_bio->sectors, 0);
NeilBrown475b0322011-12-23 10:17:55 +11002639 rdev_dec_pending(rdev, conf->mddev);
2640 }
NeilBrownbd870a12011-07-28 11:39:24 +10002641 }
2642 if (test_bit(R10BIO_WriteError,
2643 &r10_bio->state))
2644 close_write(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10002645 raid_end_bio_io(r10_bio);
2646 }
2647}
2648
NeilBrownfd01b882011-10-11 16:47:53 +11002649static void raid10d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
NeilBrown9f2c9d12011-10-11 16:48:43 +11002651 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 unsigned long flags;
NeilBrowne879a872011-10-11 16:49:02 +11002653 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002655 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
2657 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002659 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002661
Jens Axboe7eaceac2011-03-10 08:52:07 +01002662 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002663
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002665 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002666 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002668 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002669 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08002671 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 spin_unlock_irqrestore(&conf->device_lock, flags);
2673
2674 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002675 conf = mddev->private;
NeilBrownbd870a12011-07-28 11:39:24 +10002676 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2677 test_bit(R10BIO_WriteError, &r10_bio->state))
NeilBrown749c55e2011-07-28 11:39:24 +10002678 handle_write_completed(conf, r10_bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10002679 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
2680 reshape_request_write(mddev, r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10002681 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01002683 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 recovery_request_write(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002685 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
NeilBrown560f8e52011-07-28 11:39:23 +10002686 handle_read_error(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002687 else {
2688 /* just a partial read to be scheduled from a
2689 * separate context
2690 */
2691 int slot = r10_bio->read_slot;
2692 generic_make_request(r10_bio->devs[slot].bio);
2693 }
NeilBrown4443ae12006-01-06 00:20:28 -08002694
NeilBrown1d9d5242009-10-16 15:55:32 +11002695 cond_resched();
NeilBrownde393cd2011-07-28 11:31:48 +10002696 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2697 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002699 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700}
2701
2702
NeilBrowne879a872011-10-11 16:49:02 +11002703static int init_resync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704{
2705 int buffs;
NeilBrown69335ef2011-12-23 10:17:54 +11002706 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
2708 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02002709 BUG_ON(conf->r10buf_pool);
NeilBrown69335ef2011-12-23 10:17:54 +11002710 conf->have_replacement = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002711 for (i = 0; i < conf->geo.raid_disks; i++)
NeilBrown69335ef2011-12-23 10:17:54 +11002712 if (conf->mirrors[i].replacement)
2713 conf->have_replacement = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2715 if (!conf->r10buf_pool)
2716 return -ENOMEM;
2717 conf->next_resync = 0;
2718 return 0;
2719}
2720
2721/*
2722 * perform a "sync" on one "block"
2723 *
2724 * We need to make sure that no normal I/O request - particularly write
2725 * requests - conflict with active sync requests.
2726 *
2727 * This is achieved by tracking pending requests and a 'barrier' concept
2728 * that can be installed to exclude normal IO requests.
2729 *
2730 * Resync and recovery are handled very differently.
2731 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2732 *
2733 * For resync, we iterate over virtual addresses, read all copies,
2734 * and update if there are differences. If only one copy is live,
2735 * skip it.
2736 * For recovery, we iterate over physical addresses, read a good
2737 * value for each non-in_sync drive, and over-write.
2738 *
2739 * So, for recovery we may have several outstanding complex requests for a
2740 * given address, one for each out-of-sync device. We model this by allocating
2741 * a number of r10_bio structures, one for each out-of-sync device.
2742 * As we setup these structures, we collect all bio's together into a list
2743 * which we then process collectively to add pages, and then process again
2744 * to pass to generic_make_request.
2745 *
2746 * The r10_bio structures are linked using a borrowed master_bio pointer.
2747 * This link is counted in ->remaining. When the r10_bio that points to NULL
2748 * has its remaining count decremented to 0, the whole complex operation
2749 * is complete.
2750 *
2751 */
2752
NeilBrownfd01b882011-10-11 16:47:53 +11002753static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
NeilBrownab9d47e2011-05-11 14:54:41 +10002754 int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755{
NeilBrowne879a872011-10-11 16:49:02 +11002756 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002757 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 struct bio *biolist = NULL, *bio;
2759 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08002761 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11002762 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 sector_t sectors_skipped = 0;
2764 int chunks_skipped = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002765 sector_t chunk_mask = conf->geo.chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
2767 if (!conf->r10buf_pool)
2768 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002769 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
2771 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11002772 max_sector = mddev->dev_sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10002773 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
2774 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 max_sector = mddev->resync_max_sectors;
2776 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002777 /* If we aborted, we need to abort the
2778 * sync on the 'current' bitmap chucks (there can
2779 * be several when recovering multiple devices).
2780 * as we may have started syncing it but not finished.
2781 * We can find the current address in
2782 * mddev->curr_resync, but for recovery,
2783 * we need to convert that to several
2784 * virtual addresses.
2785 */
NeilBrown3ea7daa2012-05-22 13:53:47 +10002786 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
2787 end_reshape(conf);
2788 return 0;
2789 }
2790
NeilBrown6cce3b22006-01-06 00:20:16 -08002791 if (mddev->curr_resync < max_sector) { /* aborted */
2792 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2793 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2794 &sync_blocks, 1);
NeilBrown5cf00fc2012-05-21 09:28:20 +10002795 else for (i = 0; i < conf->geo.raid_disks; i++) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002796 sector_t sect =
2797 raid10_find_virt(conf, mddev->curr_resync, i);
2798 bitmap_end_sync(mddev->bitmap, sect,
2799 &sync_blocks, 1);
2800 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002801 } else {
2802 /* completed sync */
2803 if ((!mddev->bitmap || conf->fullsync)
2804 && conf->have_replacement
2805 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2806 /* Completed a full sync so the replacements
2807 * are now fully recovered.
2808 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10002809 for (i = 0; i < conf->geo.raid_disks; i++)
NeilBrown9ad1aef2011-12-23 10:17:55 +11002810 if (conf->mirrors[i].replacement)
2811 conf->mirrors[i].replacement
2812 ->recovery_offset
2813 = MaxSector;
2814 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002815 conf->fullsync = 0;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002816 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002817 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07002819 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 return sectors_skipped;
2821 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10002822
2823 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2824 return reshape_request(mddev, sector_nr, skipped);
2825
NeilBrown5cf00fc2012-05-21 09:28:20 +10002826 if (chunks_skipped >= conf->geo.raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 /* if there has been nothing to do on any drive,
2828 * then there is nothing to do at all..
2829 */
NeilBrown57afd892005-06-21 17:17:13 -07002830 *skipped = 1;
2831 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 }
2833
NeilBrownc6207272008-02-06 01:39:52 -08002834 if (max_sector > mddev->resync_max)
2835 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 /* make sure whole request will fit in a chunk - if chunks
2838 * are meaningful
2839 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10002840 if (conf->geo.near_copies < conf->geo.raid_disks &&
2841 max_sector > (sector_nr | chunk_mask))
2842 max_sector = (sector_nr | chunk_mask) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 /*
2844 * If there is non-resync activity waiting for us then
2845 * put in a delay to throttle resync.
2846 */
NeilBrown0a27ec92006-01-06 00:20:13 -08002847 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850 /* Again, very different code for resync and recovery.
2851 * Both must result in an r10bio with a list of bios that
2852 * have bi_end_io, bi_sector, bi_bdev set,
2853 * and bi_private set to the r10bio.
2854 * For recovery, we may actually create several r10bios
2855 * with 2 bios in each, that correspond to the bios in the main one.
2856 * In this case, the subordinate r10bios link back through a
2857 * borrowed master_bio pointer, and the counter in the master
2858 * includes a ref from each subordinate.
2859 */
2860 /* First, we decide what to do and set ->bi_end_io
2861 * To end_sync_read if we want to read, and
2862 * end_sync_write if we will want to write.
2863 */
2864
NeilBrown6cce3b22006-01-06 00:20:16 -08002865 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2867 /* recovery... the complicated one */
NeilBrowne875ece2011-07-28 11:39:24 +10002868 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 r10_bio = NULL;
2870
NeilBrown5cf00fc2012-05-21 09:28:20 +10002871 for (i = 0 ; i < conf->geo.raid_disks; i++) {
NeilBrownab9d47e2011-05-11 14:54:41 +10002872 int still_degraded;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002873 struct r10bio *rb2;
NeilBrownab9d47e2011-05-11 14:54:41 +10002874 sector_t sect;
2875 int must_sync;
NeilBrowne875ece2011-07-28 11:39:24 +10002876 int any_working;
NeilBrown24afd802011-12-23 10:17:55 +11002877 struct mirror_info *mirror = &conf->mirrors[i];
NeilBrownab9d47e2011-05-11 14:54:41 +10002878
NeilBrown24afd802011-12-23 10:17:55 +11002879 if ((mirror->rdev == NULL ||
2880 test_bit(In_sync, &mirror->rdev->flags))
2881 &&
2882 (mirror->replacement == NULL ||
2883 test_bit(Faulty,
2884 &mirror->replacement->flags)))
NeilBrownab9d47e2011-05-11 14:54:41 +10002885 continue;
2886
2887 still_degraded = 0;
2888 /* want to reconstruct this device */
2889 rb2 = r10_bio;
2890 sect = raid10_find_virt(conf, sector_nr, i);
NeilBrown24afd802011-12-23 10:17:55 +11002891 /* Unless we are doing a full sync, or a replacement
2892 * we only need to recover the block if it is set in
2893 * the bitmap
NeilBrownab9d47e2011-05-11 14:54:41 +10002894 */
2895 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2896 &sync_blocks, 1);
2897 if (sync_blocks < max_sync)
2898 max_sync = sync_blocks;
2899 if (!must_sync &&
NeilBrown24afd802011-12-23 10:17:55 +11002900 mirror->replacement == NULL &&
NeilBrownab9d47e2011-05-11 14:54:41 +10002901 !conf->fullsync) {
2902 /* yep, skip the sync_blocks here, but don't assume
2903 * that there will never be anything to do here
NeilBrown6cce3b22006-01-06 00:20:16 -08002904 */
NeilBrownab9d47e2011-05-11 14:54:41 +10002905 chunks_skipped = -1;
2906 continue;
2907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
NeilBrownab9d47e2011-05-11 14:54:41 +10002909 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2910 raise_barrier(conf, rb2 != NULL);
2911 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
NeilBrownab9d47e2011-05-11 14:54:41 +10002913 r10_bio->master_bio = (struct bio*)rb2;
2914 if (rb2)
2915 atomic_inc(&rb2->remaining);
2916 r10_bio->mddev = mddev;
2917 set_bit(R10BIO_IsRecover, &r10_bio->state);
2918 r10_bio->sector = sect;
NeilBrown6cce3b22006-01-06 00:20:16 -08002919
NeilBrownab9d47e2011-05-11 14:54:41 +10002920 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10002921
NeilBrownab9d47e2011-05-11 14:54:41 +10002922 /* Need to check if the array will still be
2923 * degraded
2924 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10002925 for (j = 0; j < conf->geo.raid_disks; j++)
NeilBrownab9d47e2011-05-11 14:54:41 +10002926 if (conf->mirrors[j].rdev == NULL ||
2927 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
2928 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07002929 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002931
2932 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2933 &sync_blocks, still_degraded);
2934
NeilBrowne875ece2011-07-28 11:39:24 +10002935 any_working = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10002936 for (j=0; j<conf->copies;j++) {
NeilBrowne875ece2011-07-28 11:39:24 +10002937 int k;
NeilBrownab9d47e2011-05-11 14:54:41 +10002938 int d = r10_bio->devs[j].devnum;
NeilBrown5e570282011-07-28 11:39:25 +10002939 sector_t from_addr, to_addr;
NeilBrown3cb03002011-10-11 16:45:26 +11002940 struct md_rdev *rdev;
NeilBrown40c356c2011-07-28 11:39:24 +10002941 sector_t sector, first_bad;
2942 int bad_sectors;
NeilBrownab9d47e2011-05-11 14:54:41 +10002943 if (!conf->mirrors[d].rdev ||
2944 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
2945 continue;
2946 /* This is where we read from */
NeilBrowne875ece2011-07-28 11:39:24 +10002947 any_working = 1;
NeilBrown40c356c2011-07-28 11:39:24 +10002948 rdev = conf->mirrors[d].rdev;
2949 sector = r10_bio->devs[j].addr;
2950
2951 if (is_badblock(rdev, sector, max_sync,
2952 &first_bad, &bad_sectors)) {
2953 if (first_bad > sector)
2954 max_sync = first_bad - sector;
2955 else {
2956 bad_sectors -= (sector
2957 - first_bad);
2958 if (max_sync > bad_sectors)
2959 max_sync = bad_sectors;
2960 continue;
2961 }
2962 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002963 bio = r10_bio->devs[0].bio;
2964 bio->bi_next = biolist;
2965 biolist = bio;
2966 bio->bi_private = r10_bio;
2967 bio->bi_end_io = end_sync_read;
2968 bio->bi_rw = READ;
NeilBrown5e570282011-07-28 11:39:25 +10002969 from_addr = r10_bio->devs[j].addr;
NeilBrown24afd802011-12-23 10:17:55 +11002970 bio->bi_sector = from_addr + rdev->data_offset;
2971 bio->bi_bdev = rdev->bdev;
2972 atomic_inc(&rdev->nr_pending);
2973 /* and we write to 'i' (if not in_sync) */
NeilBrownab9d47e2011-05-11 14:54:41 +10002974
2975 for (k=0; k<conf->copies; k++)
2976 if (r10_bio->devs[k].devnum == i)
2977 break;
2978 BUG_ON(k == conf->copies);
NeilBrown5e570282011-07-28 11:39:25 +10002979 to_addr = r10_bio->devs[k].addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10002980 r10_bio->devs[0].devnum = d;
NeilBrown5e570282011-07-28 11:39:25 +10002981 r10_bio->devs[0].addr = from_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10002982 r10_bio->devs[1].devnum = i;
NeilBrown5e570282011-07-28 11:39:25 +10002983 r10_bio->devs[1].addr = to_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10002984
NeilBrown24afd802011-12-23 10:17:55 +11002985 rdev = mirror->rdev;
2986 if (!test_bit(In_sync, &rdev->flags)) {
2987 bio = r10_bio->devs[1].bio;
2988 bio->bi_next = biolist;
2989 biolist = bio;
2990 bio->bi_private = r10_bio;
2991 bio->bi_end_io = end_sync_write;
2992 bio->bi_rw = WRITE;
2993 bio->bi_sector = to_addr
2994 + rdev->data_offset;
2995 bio->bi_bdev = rdev->bdev;
2996 atomic_inc(&r10_bio->remaining);
2997 } else
2998 r10_bio->devs[1].bio->bi_end_io = NULL;
2999
3000 /* and maybe write to replacement */
3001 bio = r10_bio->devs[1].repl_bio;
3002 if (bio)
3003 bio->bi_end_io = NULL;
3004 rdev = mirror->replacement;
3005 /* Note: if rdev != NULL, then bio
3006 * cannot be NULL as r10buf_pool_alloc will
3007 * have allocated it.
3008 * So the second test here is pointless.
3009 * But it keeps semantic-checkers happy, and
3010 * this comment keeps human reviewers
3011 * happy.
3012 */
3013 if (rdev == NULL || bio == NULL ||
3014 test_bit(Faulty, &rdev->flags))
3015 break;
3016 bio->bi_next = biolist;
3017 biolist = bio;
3018 bio->bi_private = r10_bio;
3019 bio->bi_end_io = end_sync_write;
3020 bio->bi_rw = WRITE;
3021 bio->bi_sector = to_addr + rdev->data_offset;
3022 bio->bi_bdev = rdev->bdev;
3023 atomic_inc(&r10_bio->remaining);
NeilBrownab9d47e2011-05-11 14:54:41 +10003024 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003026 if (j == conf->copies) {
NeilBrowne875ece2011-07-28 11:39:24 +10003027 /* Cannot recover, so abort the recovery or
3028 * record a bad block */
NeilBrownab9d47e2011-05-11 14:54:41 +10003029 put_buf(r10_bio);
3030 if (rb2)
3031 atomic_dec(&rb2->remaining);
3032 r10_bio = rb2;
NeilBrowne875ece2011-07-28 11:39:24 +10003033 if (any_working) {
3034 /* problem is that there are bad blocks
3035 * on other device(s)
3036 */
3037 int k;
3038 for (k = 0; k < conf->copies; k++)
3039 if (r10_bio->devs[k].devnum == i)
3040 break;
NeilBrown24afd802011-12-23 10:17:55 +11003041 if (!test_bit(In_sync,
3042 &mirror->rdev->flags)
3043 && !rdev_set_badblocks(
3044 mirror->rdev,
3045 r10_bio->devs[k].addr,
3046 max_sync, 0))
3047 any_working = 0;
3048 if (mirror->replacement &&
3049 !rdev_set_badblocks(
3050 mirror->replacement,
NeilBrowne875ece2011-07-28 11:39:24 +10003051 r10_bio->devs[k].addr,
3052 max_sync, 0))
3053 any_working = 0;
3054 }
3055 if (!any_working) {
3056 if (!test_and_set_bit(MD_RECOVERY_INTR,
3057 &mddev->recovery))
3058 printk(KERN_INFO "md/raid10:%s: insufficient "
3059 "working devices for recovery.\n",
3060 mdname(mddev));
NeilBrown24afd802011-12-23 10:17:55 +11003061 mirror->recovery_disabled
NeilBrowne875ece2011-07-28 11:39:24 +10003062 = mddev->recovery_disabled;
3063 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003064 break;
3065 }
3066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 if (biolist == NULL) {
3068 while (r10_bio) {
NeilBrown9f2c9d12011-10-11 16:48:43 +11003069 struct r10bio *rb2 = r10_bio;
3070 r10_bio = (struct r10bio*) rb2->master_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 rb2->master_bio = NULL;
3072 put_buf(rb2);
3073 }
3074 goto giveup;
3075 }
3076 } else {
3077 /* resync. Schedule a read for every block at this virt offset */
3078 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003079
NeilBrown78200d42009-02-25 13:18:47 +11003080 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
3081
NeilBrown6cce3b22006-01-06 00:20:16 -08003082 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
3083 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003084 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3085 &mddev->recovery)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08003086 /* We can skip this block */
3087 *skipped = 1;
3088 return sync_blocks + sectors_skipped;
3089 }
3090 if (sync_blocks < max_sync)
3091 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
3093
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 r10_bio->mddev = mddev;
3095 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08003096 raise_barrier(conf, 0);
3097 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
3099 r10_bio->master_bio = NULL;
3100 r10_bio->sector = sector_nr;
3101 set_bit(R10BIO_IsSync, &r10_bio->state);
3102 raid10_find_phys(conf, r10_bio);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003103 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
NeilBrown5cf00fc2012-05-21 09:28:20 +10003105 for (i = 0; i < conf->copies; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 int d = r10_bio->devs[i].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10003107 sector_t first_bad, sector;
3108 int bad_sectors;
3109
NeilBrown9ad1aef2011-12-23 10:17:55 +11003110 if (r10_bio->devs[i].repl_bio)
3111 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3112
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 bio = r10_bio->devs[i].bio;
3114 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07003115 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08003117 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 continue;
NeilBrown40c356c2011-07-28 11:39:24 +10003119 sector = r10_bio->devs[i].addr;
3120 if (is_badblock(conf->mirrors[d].rdev,
3121 sector, max_sync,
3122 &first_bad, &bad_sectors)) {
3123 if (first_bad > sector)
3124 max_sync = first_bad - sector;
3125 else {
3126 bad_sectors -= (sector - first_bad);
3127 if (max_sync > bad_sectors)
3128 max_sync = max_sync;
3129 continue;
3130 }
3131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
3133 atomic_inc(&r10_bio->remaining);
3134 bio->bi_next = biolist;
3135 biolist = bio;
3136 bio->bi_private = r10_bio;
3137 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08003138 bio->bi_rw = READ;
NeilBrown40c356c2011-07-28 11:39:24 +10003139 bio->bi_sector = sector +
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 conf->mirrors[d].rdev->data_offset;
3141 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
3142 count++;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003143
3144 if (conf->mirrors[d].replacement == NULL ||
3145 test_bit(Faulty,
3146 &conf->mirrors[d].replacement->flags))
3147 continue;
3148
3149 /* Need to set up for writing to the replacement */
3150 bio = r10_bio->devs[i].repl_bio;
3151 clear_bit(BIO_UPTODATE, &bio->bi_flags);
3152
3153 sector = r10_bio->devs[i].addr;
3154 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
3155 bio->bi_next = biolist;
3156 biolist = bio;
3157 bio->bi_private = r10_bio;
3158 bio->bi_end_io = end_sync_write;
3159 bio->bi_rw = WRITE;
3160 bio->bi_sector = sector +
3161 conf->mirrors[d].replacement->data_offset;
3162 bio->bi_bdev = conf->mirrors[d].replacement->bdev;
3163 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 }
3165
3166 if (count < 2) {
3167 for (i=0; i<conf->copies; i++) {
3168 int d = r10_bio->devs[i].devnum;
3169 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10003170 rdev_dec_pending(conf->mirrors[d].rdev,
3171 mddev);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003172 if (r10_bio->devs[i].repl_bio &&
3173 r10_bio->devs[i].repl_bio->bi_end_io)
3174 rdev_dec_pending(
3175 conf->mirrors[d].replacement,
3176 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 }
3178 put_buf(r10_bio);
3179 biolist = NULL;
3180 goto giveup;
3181 }
3182 }
3183
3184 for (bio = biolist; bio ; bio=bio->bi_next) {
3185
3186 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
3187 if (bio->bi_end_io)
3188 bio->bi_flags |= 1 << BIO_UPTODATE;
3189 bio->bi_vcnt = 0;
3190 bio->bi_idx = 0;
3191 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 bio->bi_size = 0;
3193 }
3194
3195 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003196 if (sector_nr + max_sync < max_sector)
3197 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 do {
3199 struct page *page;
3200 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 if (sector_nr + (len>>9) > max_sector)
3202 len = (max_sector - sector_nr) << 9;
3203 if (len == 0)
3204 break;
3205 for (bio= biolist ; bio ; bio=bio->bi_next) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003206 struct bio *bio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
NeilBrownab9d47e2011-05-11 14:54:41 +10003208 if (bio_add_page(bio, page, len, 0))
3209 continue;
3210
3211 /* stop here */
3212 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
3213 for (bio2 = biolist;
3214 bio2 && bio2 != bio;
3215 bio2 = bio2->bi_next) {
3216 /* remove last page from this bio */
3217 bio2->bi_vcnt--;
3218 bio2->bi_size -= len;
3219 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003221 goto bio_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 }
3223 nr_sectors += len>>9;
3224 sector_nr += len>>9;
3225 } while (biolist->bi_vcnt < RESYNC_PAGES);
3226 bio_full:
3227 r10_bio->sectors = nr_sectors;
3228
3229 while (biolist) {
3230 bio = biolist;
3231 biolist = biolist->bi_next;
3232
3233 bio->bi_next = NULL;
3234 r10_bio = bio->bi_private;
3235 r10_bio->sectors = nr_sectors;
3236
3237 if (bio->bi_end_io == end_sync_read) {
3238 md_sync_acct(bio->bi_bdev, nr_sectors);
3239 generic_make_request(bio);
3240 }
3241 }
3242
NeilBrown57afd892005-06-21 17:17:13 -07003243 if (sectors_skipped)
3244 /* pretend they weren't skipped, it makes
3245 * no important difference in this case
3246 */
3247 md_done_sync(mddev, sectors_skipped, 1);
3248
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 return sectors_skipped + nr_sectors;
3250 giveup:
3251 /* There is nowhere to write, so all non-sync
NeilBrowne875ece2011-07-28 11:39:24 +10003252 * drives must be failed or in resync, all drives
3253 * have a bad block, so try the next chunk...
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 */
NeilBrown09b40682009-02-25 13:18:47 +11003255 if (sector_nr + max_sync < max_sector)
3256 max_sector = sector_nr + max_sync;
3257
3258 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 chunks_skipped ++;
3260 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262}
3263
Dan Williams80c3a6c2009-03-17 18:10:40 -07003264static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11003265raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07003266{
3267 sector_t size;
NeilBrowne879a872011-10-11 16:49:02 +11003268 struct r10conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003269
3270 if (!raid_disks)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003271 raid_disks = min(conf->geo.raid_disks,
3272 conf->prev.raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003273 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003274 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003275
NeilBrown5cf00fc2012-05-21 09:28:20 +10003276 size = sectors >> conf->geo.chunk_shift;
3277 sector_div(size, conf->geo.far_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003278 size = size * raid_disks;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003279 sector_div(size, conf->geo.near_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003280
NeilBrown5cf00fc2012-05-21 09:28:20 +10003281 return size << conf->geo.chunk_shift;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003282}
3283
NeilBrown6508fdb2012-05-17 10:08:45 +10003284static void calc_sectors(struct r10conf *conf, sector_t size)
3285{
3286 /* Calculate the number of sectors-per-device that will
3287 * actually be used, and set conf->dev_sectors and
3288 * conf->stride
3289 */
3290
NeilBrown5cf00fc2012-05-21 09:28:20 +10003291 size = size >> conf->geo.chunk_shift;
3292 sector_div(size, conf->geo.far_copies);
3293 size = size * conf->geo.raid_disks;
3294 sector_div(size, conf->geo.near_copies);
NeilBrown6508fdb2012-05-17 10:08:45 +10003295 /* 'size' is now the number of chunks in the array */
3296 /* calculate "used chunks per device" */
3297 size = size * conf->copies;
3298
3299 /* We need to round up when dividing by raid_disks to
3300 * get the stride size.
3301 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003302 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
NeilBrown6508fdb2012-05-17 10:08:45 +10003303
NeilBrown5cf00fc2012-05-21 09:28:20 +10003304 conf->dev_sectors = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003305
NeilBrown5cf00fc2012-05-21 09:28:20 +10003306 if (conf->geo.far_offset)
3307 conf->geo.stride = 1 << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003308 else {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003309 sector_div(size, conf->geo.far_copies);
3310 conf->geo.stride = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003311 }
3312}
Trela, Maciejdab8b292010-03-08 16:02:45 +11003313
NeilBrowndeb200d2012-05-21 09:28:33 +10003314enum geo_type {geo_new, geo_old, geo_start};
3315static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3316{
3317 int nc, fc, fo;
3318 int layout, chunk, disks;
3319 switch (new) {
3320 case geo_old:
3321 layout = mddev->layout;
3322 chunk = mddev->chunk_sectors;
3323 disks = mddev->raid_disks - mddev->delta_disks;
3324 break;
3325 case geo_new:
3326 layout = mddev->new_layout;
3327 chunk = mddev->new_chunk_sectors;
3328 disks = mddev->raid_disks;
3329 break;
3330 default: /* avoid 'may be unused' warnings */
3331 case geo_start: /* new when starting reshape - raid_disks not
3332 * updated yet. */
3333 layout = mddev->new_layout;
3334 chunk = mddev->new_chunk_sectors;
3335 disks = mddev->raid_disks + mddev->delta_disks;
3336 break;
3337 }
3338 if (layout >> 17)
3339 return -1;
3340 if (chunk < (PAGE_SIZE >> 9) ||
3341 !is_power_of_2(chunk))
3342 return -2;
3343 nc = layout & 255;
3344 fc = (layout >> 8) & 255;
3345 fo = layout & (1<<16);
3346 geo->raid_disks = disks;
3347 geo->near_copies = nc;
3348 geo->far_copies = fc;
3349 geo->far_offset = fo;
3350 geo->chunk_mask = chunk - 1;
3351 geo->chunk_shift = ffz(~chunk);
3352 return nc*fc;
3353}
3354
NeilBrowne879a872011-10-11 16:49:02 +11003355static struct r10conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356{
NeilBrowne879a872011-10-11 16:49:02 +11003357 struct r10conf *conf = NULL;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003358 int err = -EINVAL;
NeilBrowndeb200d2012-05-21 09:28:33 +10003359 struct geom geo;
3360 int copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
NeilBrowndeb200d2012-05-21 09:28:33 +10003362 copies = setup_geo(&geo, mddev, geo_new);
3363
3364 if (copies == -2) {
NeilBrown128595e2010-05-03 14:47:14 +10003365 printk(KERN_ERR "md/raid10:%s: chunk size must be "
3366 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
3367 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003368 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 }
NeilBrown2604b702006-01-06 00:20:36 -08003370
NeilBrowndeb200d2012-05-21 09:28:33 +10003371 if (copies < 2 || copies > mddev->raid_disks) {
NeilBrown128595e2010-05-03 14:47:14 +10003372 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
Maciej Trelaf73ea872010-06-16 11:46:29 +01003373 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 goto out;
3375 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11003376
3377 err = -ENOMEM;
NeilBrowne879a872011-10-11 16:49:02 +11003378 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003379 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003381
NeilBrown3ea7daa2012-05-22 13:53:47 +10003382 /* FIXME calc properly */
3383 conf->mirrors = kzalloc(sizeof(struct mirror_info)*(mddev->raid_disks +
3384 max(0,mddev->delta_disks)),
Trela, Maciejdab8b292010-03-08 16:02:45 +11003385 GFP_KERNEL);
3386 if (!conf->mirrors)
3387 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08003388
3389 conf->tmppage = alloc_page(GFP_KERNEL);
3390 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003391 goto out;
3392
NeilBrowndeb200d2012-05-21 09:28:33 +10003393 conf->geo = geo;
3394 conf->copies = copies;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003395 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
3396 r10bio_pool_free, conf);
3397 if (!conf->r10bio_pool)
3398 goto out;
3399
NeilBrown6508fdb2012-05-17 10:08:45 +10003400 calc_sectors(conf, mddev->dev_sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003401 if (mddev->reshape_position == MaxSector) {
3402 conf->prev = conf->geo;
3403 conf->reshape_progress = MaxSector;
3404 } else {
3405 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
3406 err = -EINVAL;
3407 goto out;
3408 }
3409 conf->reshape_progress = mddev->reshape_position;
3410 if (conf->prev.far_offset)
3411 conf->prev.stride = 1 << conf->prev.chunk_shift;
3412 else
3413 /* far_copies must be 1 */
3414 conf->prev.stride = conf->dev_sectors;
3415 }
Neil Browne7e72bf2008-05-14 16:05:54 -07003416 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003417 INIT_LIST_HEAD(&conf->retry_list);
3418
3419 spin_lock_init(&conf->resync_lock);
3420 init_waitqueue_head(&conf->wait_barrier);
3421
3422 conf->thread = md_register_thread(raid10d, mddev, NULL);
3423 if (!conf->thread)
3424 goto out;
3425
Trela, Maciejdab8b292010-03-08 16:02:45 +11003426 conf->mddev = mddev;
3427 return conf;
3428
3429 out:
NeilBrown3ea7daa2012-05-22 13:53:47 +10003430 if (err == -ENOMEM)
3431 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
3432 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003433 if (conf) {
3434 if (conf->r10bio_pool)
3435 mempool_destroy(conf->r10bio_pool);
3436 kfree(conf->mirrors);
3437 safe_put_page(conf->tmppage);
3438 kfree(conf);
3439 }
3440 return ERR_PTR(err);
3441}
3442
NeilBrownfd01b882011-10-11 16:47:53 +11003443static int run(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003444{
NeilBrowne879a872011-10-11 16:49:02 +11003445 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003446 int i, disk_idx, chunk_size;
NeilBrown0f6d02d2011-10-11 16:48:46 +11003447 struct mirror_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11003448 struct md_rdev *rdev;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003449 sector_t size;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003450 sector_t min_offset_diff = 0;
3451 int first = 1;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003452
3453 if (mddev->private == NULL) {
3454 conf = setup_conf(mddev);
3455 if (IS_ERR(conf))
3456 return PTR_ERR(conf);
3457 mddev->private = conf;
3458 }
3459 conf = mddev->private;
3460 if (!conf)
3461 goto out;
3462
Trela, Maciejdab8b292010-03-08 16:02:45 +11003463 mddev->thread = conf->thread;
3464 conf->thread = NULL;
3465
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003466 chunk_size = mddev->chunk_sectors << 9;
3467 blk_queue_io_min(mddev->queue, chunk_size);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003468 if (conf->geo.raid_disks % conf->geo.near_copies)
3469 blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003470 else
3471 blk_queue_io_opt(mddev->queue, chunk_size *
NeilBrown5cf00fc2012-05-21 09:28:20 +10003472 (conf->geo.raid_disks / conf->geo.near_copies));
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003473
NeilBrowndafb20f2012-03-19 12:46:39 +11003474 rdev_for_each(rdev, mddev) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10003475 long long diff;
NeilBrown34b343c2011-07-28 11:31:47 +10003476
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 disk_idx = rdev->raid_disk;
NeilBrownf8c9e742012-05-21 09:28:33 +10003478 if (disk_idx < 0)
3479 continue;
3480 if (disk_idx >= conf->geo.raid_disks &&
3481 disk_idx >= conf->prev.raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 continue;
3483 disk = conf->mirrors + disk_idx;
3484
NeilBrown56a2559b2011-12-23 10:17:55 +11003485 if (test_bit(Replacement, &rdev->flags)) {
3486 if (disk->replacement)
3487 goto out_free_conf;
3488 disk->replacement = rdev;
3489 } else {
3490 if (disk->rdev)
3491 goto out_free_conf;
3492 disk->rdev = rdev;
3493 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10003494 diff = (rdev->new_data_offset - rdev->data_offset);
3495 if (!mddev->reshape_backwards)
3496 diff = -diff;
3497 if (diff < 0)
3498 diff = 0;
3499 if (first || diff < min_offset_diff)
3500 min_offset_diff = diff;
NeilBrown56a2559b2011-12-23 10:17:55 +11003501
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003502 disk_stack_limits(mddev->gendisk, rdev->bdev,
3503 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
3505 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10003507
NeilBrown6d508242005-09-09 16:24:03 -07003508 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10003509 if (!enough(conf, -1)) {
NeilBrown128595e2010-05-03 14:47:14 +10003510 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07003511 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 goto out_free_conf;
3513 }
3514
NeilBrown3ea7daa2012-05-22 13:53:47 +10003515 if (conf->reshape_progress != MaxSector) {
3516 /* must ensure that shape change is supported */
3517 if (conf->geo.far_copies != 1 &&
3518 conf->geo.far_offset == 0)
3519 goto out_free_conf;
3520 if (conf->prev.far_copies != 1 &&
3521 conf->geo.far_offset == 0)
3522 goto out_free_conf;
3523 }
3524
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 mddev->degraded = 0;
NeilBrownf8c9e742012-05-21 09:28:33 +10003526 for (i = 0;
3527 i < conf->geo.raid_disks
3528 || i < conf->prev.raid_disks;
3529 i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
3531 disk = conf->mirrors + i;
3532
NeilBrown56a2559b2011-12-23 10:17:55 +11003533 if (!disk->rdev && disk->replacement) {
3534 /* The replacement is all we have - use it */
3535 disk->rdev = disk->replacement;
3536 disk->replacement = NULL;
3537 clear_bit(Replacement, &disk->rdev->flags);
3538 }
3539
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003540 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07003541 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 disk->head_position = 0;
3543 mddev->degraded++;
Neil Brown8c2e8702008-06-28 08:30:52 +10003544 if (disk->rdev)
3545 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 }
NeilBrownd890fa22011-10-26 11:54:39 +11003547 disk->recovery_disabled = mddev->recovery_disabled - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 }
3549
Andre Noll8c6ac8682009-06-18 08:48:06 +10003550 if (mddev->recovery_cp != MaxSector)
NeilBrown128595e2010-05-03 14:47:14 +10003551 printk(KERN_NOTICE "md/raid10:%s: not clean"
Andre Noll8c6ac8682009-06-18 08:48:06 +10003552 " -- starting background reconstruction\n",
3553 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 printk(KERN_INFO
NeilBrown128595e2010-05-03 14:47:14 +10003555 "md/raid10:%s: active with %d out of %d devices\n",
NeilBrown5cf00fc2012-05-21 09:28:20 +10003556 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
3557 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 /*
3559 * Ok, everything is just fine now
3560 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11003561 mddev->dev_sectors = conf->dev_sectors;
3562 size = raid10_size(mddev, 0, 0);
3563 md_set_array_sectors(mddev, size);
3564 mddev->resync_max_sectors = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
NeilBrown0d129222006-10-03 01:15:54 -07003566 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
3567 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown7a5febe2005-05-16 21:53:16 -07003568
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 /* Calculate max read-ahead size.
3570 * We need to readahead at least twice a whole stripe....
3571 * maybe...
3572 */
3573 {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003574 int stripe = conf->geo.raid_disks *
Andre Noll9d8f0362009-06-18 08:45:01 +10003575 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003576 stripe /= conf->geo.near_copies;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003577 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3578 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 }
3580
NeilBrown050b6612012-03-19 12:46:39 +11003581 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
Martin K. Petersena91a2782011-03-17 11:11:05 +01003582
3583 if (md_integrity_register(mddev))
3584 goto out_free_conf;
3585
NeilBrown3ea7daa2012-05-22 13:53:47 +10003586 if (conf->reshape_progress != MaxSector) {
3587 unsigned long before_length, after_length;
3588
3589 before_length = ((1 << conf->prev.chunk_shift) *
3590 conf->prev.far_copies);
3591 after_length = ((1 << conf->geo.chunk_shift) *
3592 conf->geo.far_copies);
3593
3594 if (max(before_length, after_length) > min_offset_diff) {
3595 /* This cannot work */
3596 printk("md/raid10: offset difference not enough to continue reshape\n");
3597 goto out_free_conf;
3598 }
3599 conf->offset_diff = min_offset_diff;
3600
3601 conf->reshape_safe = conf->reshape_progress;
3602 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3603 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3604 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3605 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3606 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3607 "reshape");
3608 }
3609
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 return 0;
3611
3612out_free_conf:
NeilBrown01f96c02011-09-21 15:30:20 +10003613 md_unregister_thread(&mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 if (conf->r10bio_pool)
3615 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08003616 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003617 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 kfree(conf);
3619 mddev->private = NULL;
3620out:
3621 return -EIO;
3622}
3623
NeilBrownfd01b882011-10-11 16:47:53 +11003624static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625{
NeilBrowne879a872011-10-11 16:49:02 +11003626 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
NeilBrown409c57f2009-03-31 14:39:39 +11003628 raise_barrier(conf, 0);
3629 lower_barrier(conf);
3630
NeilBrown01f96c02011-09-21 15:30:20 +10003631 md_unregister_thread(&mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
3633 if (conf->r10bio_pool)
3634 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003635 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 kfree(conf);
3637 mddev->private = NULL;
3638 return 0;
3639}
3640
NeilBrownfd01b882011-10-11 16:47:53 +11003641static void raid10_quiesce(struct mddev *mddev, int state)
NeilBrown6cce3b22006-01-06 00:20:16 -08003642{
NeilBrowne879a872011-10-11 16:49:02 +11003643 struct r10conf *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08003644
3645 switch(state) {
3646 case 1:
3647 raise_barrier(conf, 0);
3648 break;
3649 case 0:
3650 lower_barrier(conf);
3651 break;
3652 }
NeilBrown6cce3b22006-01-06 00:20:16 -08003653}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
NeilBrown006a09a2012-03-19 12:46:40 +11003655static int raid10_resize(struct mddev *mddev, sector_t sectors)
3656{
3657 /* Resize of 'far' arrays is not supported.
3658 * For 'near' and 'offset' arrays we can set the
3659 * number of sectors used to be an appropriate multiple
3660 * of the chunk size.
3661 * For 'offset', this is far_copies*chunksize.
3662 * For 'near' the multiplier is the LCM of
3663 * near_copies and raid_disks.
3664 * So if far_copies > 1 && !far_offset, fail.
3665 * Else find LCM(raid_disks, near_copy)*far_copies and
3666 * multiply by chunk_size. Then round to this number.
3667 * This is mostly done by raid10_size()
3668 */
3669 struct r10conf *conf = mddev->private;
3670 sector_t oldsize, size;
3671
NeilBrownf8c9e742012-05-21 09:28:33 +10003672 if (mddev->reshape_position != MaxSector)
3673 return -EBUSY;
3674
NeilBrown5cf00fc2012-05-21 09:28:20 +10003675 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
NeilBrown006a09a2012-03-19 12:46:40 +11003676 return -EINVAL;
3677
3678 oldsize = raid10_size(mddev, 0, 0);
3679 size = raid10_size(mddev, sectors, 0);
3680 md_set_array_sectors(mddev, size);
3681 if (mddev->array_sectors > size)
3682 return -EINVAL;
3683 set_capacity(mddev->gendisk, mddev->array_sectors);
3684 revalidate_disk(mddev->gendisk);
3685 if (sectors > mddev->dev_sectors &&
3686 mddev->recovery_cp > oldsize) {
3687 mddev->recovery_cp = oldsize;
3688 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3689 }
NeilBrown6508fdb2012-05-17 10:08:45 +10003690 calc_sectors(conf, sectors);
3691 mddev->dev_sectors = conf->dev_sectors;
NeilBrown006a09a2012-03-19 12:46:40 +11003692 mddev->resync_max_sectors = size;
3693 return 0;
3694}
3695
NeilBrownfd01b882011-10-11 16:47:53 +11003696static void *raid10_takeover_raid0(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003697{
NeilBrown3cb03002011-10-11 16:45:26 +11003698 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +11003699 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003700
3701 if (mddev->degraded > 0) {
NeilBrown128595e2010-05-03 14:47:14 +10003702 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
3703 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003704 return ERR_PTR(-EINVAL);
3705 }
3706
Trela, Maciejdab8b292010-03-08 16:02:45 +11003707 /* Set new parameters */
3708 mddev->new_level = 10;
3709 /* new layout: far_copies = 1, near_copies = 2 */
3710 mddev->new_layout = (1<<8) + 2;
3711 mddev->new_chunk_sectors = mddev->chunk_sectors;
3712 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003713 mddev->raid_disks *= 2;
3714 /* make sure it will be not marked as dirty */
3715 mddev->recovery_cp = MaxSector;
3716
3717 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003718 if (!IS_ERR(conf)) {
NeilBrowndafb20f2012-03-19 12:46:39 +11003719 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003720 if (rdev->raid_disk >= 0)
3721 rdev->new_raid_disk = rdev->raid_disk * 2;
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003722 conf->barrier = 1;
3723 }
3724
Trela, Maciejdab8b292010-03-08 16:02:45 +11003725 return conf;
3726}
3727
NeilBrownfd01b882011-10-11 16:47:53 +11003728static void *raid10_takeover(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003729{
NeilBrowne373ab12011-10-11 16:48:59 +11003730 struct r0conf *raid0_conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003731
3732 /* raid10 can take over:
3733 * raid0 - providing it has only two drives
3734 */
3735 if (mddev->level == 0) {
3736 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11003737 raid0_conf = mddev->private;
3738 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown128595e2010-05-03 14:47:14 +10003739 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
3740 " with more than one zone.\n",
3741 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003742 return ERR_PTR(-EINVAL);
3743 }
3744 return raid10_takeover_raid0(mddev);
3745 }
3746 return ERR_PTR(-EINVAL);
3747}
3748
NeilBrown3ea7daa2012-05-22 13:53:47 +10003749static int raid10_check_reshape(struct mddev *mddev)
3750{
3751 /* Called when there is a request to change
3752 * - layout (to ->new_layout)
3753 * - chunk size (to ->new_chunk_sectors)
3754 * - raid_disks (by delta_disks)
3755 * or when trying to restart a reshape that was ongoing.
3756 *
3757 * We need to validate the request and possibly allocate
3758 * space if that might be an issue later.
3759 *
3760 * Currently we reject any reshape of a 'far' mode array,
3761 * allow chunk size to change if new is generally acceptable,
3762 * allow raid_disks to increase, and allow
3763 * a switch between 'near' mode and 'offset' mode.
3764 */
3765 struct r10conf *conf = mddev->private;
3766 struct geom geo;
3767
3768 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
3769 return -EINVAL;
3770
3771 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
3772 /* mustn't change number of copies */
3773 return -EINVAL;
3774 if (geo.far_copies > 1 && !geo.far_offset)
3775 /* Cannot switch to 'far' mode */
3776 return -EINVAL;
3777
3778 if (mddev->array_sectors & geo.chunk_mask)
3779 /* not factor of array size */
3780 return -EINVAL;
3781
3782 if (mddev->bitmap)
3783 return -EBUSY;
3784 if (!enough(conf, -1))
3785 return -EINVAL;
3786
3787 kfree(conf->mirrors_new);
3788 conf->mirrors_new = NULL;
3789 if (mddev->delta_disks > 0) {
3790 /* allocate new 'mirrors' list */
3791 conf->mirrors_new = kzalloc(
3792 sizeof(struct mirror_info)
3793 *(mddev->raid_disks +
3794 mddev->delta_disks),
3795 GFP_KERNEL);
3796 if (!conf->mirrors_new)
3797 return -ENOMEM;
3798 }
3799 return 0;
3800}
3801
3802/*
3803 * Need to check if array has failed when deciding whether to:
3804 * - start an array
3805 * - remove non-faulty devices
3806 * - add a spare
3807 * - allow a reshape
3808 * This determination is simple when no reshape is happening.
3809 * However if there is a reshape, we need to carefully check
3810 * both the before and after sections.
3811 * This is because some failed devices may only affect one
3812 * of the two sections, and some non-in_sync devices may
3813 * be insync in the section most affected by failed devices.
3814 */
3815static int calc_degraded(struct r10conf *conf)
3816{
3817 int degraded, degraded2;
3818 int i;
3819
3820 rcu_read_lock();
3821 degraded = 0;
3822 /* 'prev' section first */
3823 for (i = 0; i < conf->prev.raid_disks; i++) {
3824 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
3825 if (!rdev || test_bit(Faulty, &rdev->flags))
3826 degraded++;
3827 else if (!test_bit(In_sync, &rdev->flags))
3828 /* When we can reduce the number of devices in
3829 * an array, this might not contribute to
3830 * 'degraded'. It does now.
3831 */
3832 degraded++;
3833 }
3834 rcu_read_unlock();
3835 if (conf->geo.raid_disks == conf->prev.raid_disks)
3836 return degraded;
3837 rcu_read_lock();
3838 degraded2 = 0;
3839 for (i = 0; i < conf->geo.raid_disks; i++) {
3840 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
3841 if (!rdev || test_bit(Faulty, &rdev->flags))
3842 degraded2++;
3843 else if (!test_bit(In_sync, &rdev->flags)) {
3844 /* If reshape is increasing the number of devices,
3845 * this section has already been recovered, so
3846 * it doesn't contribute to degraded.
3847 * else it does.
3848 */
3849 if (conf->geo.raid_disks <= conf->prev.raid_disks)
3850 degraded2++;
3851 }
3852 }
3853 rcu_read_unlock();
3854 if (degraded2 > degraded)
3855 return degraded2;
3856 return degraded;
3857}
3858
3859static int raid10_start_reshape(struct mddev *mddev)
3860{
3861 /* A 'reshape' has been requested. This commits
3862 * the various 'new' fields and sets MD_RECOVER_RESHAPE
3863 * This also checks if there are enough spares and adds them
3864 * to the array.
3865 * We currently require enough spares to make the final
3866 * array non-degraded. We also require that the difference
3867 * between old and new data_offset - on each device - is
3868 * enough that we never risk over-writing.
3869 */
3870
3871 unsigned long before_length, after_length;
3872 sector_t min_offset_diff = 0;
3873 int first = 1;
3874 struct geom new;
3875 struct r10conf *conf = mddev->private;
3876 struct md_rdev *rdev;
3877 int spares = 0;
3878
3879 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3880 return -EBUSY;
3881
3882 if (setup_geo(&new, mddev, geo_start) != conf->copies)
3883 return -EINVAL;
3884
3885 before_length = ((1 << conf->prev.chunk_shift) *
3886 conf->prev.far_copies);
3887 after_length = ((1 << conf->geo.chunk_shift) *
3888 conf->geo.far_copies);
3889
3890 rdev_for_each(rdev, mddev) {
3891 if (!test_bit(In_sync, &rdev->flags)
3892 && !test_bit(Faulty, &rdev->flags))
3893 spares++;
3894 if (rdev->raid_disk >= 0) {
3895 long long diff = (rdev->new_data_offset
3896 - rdev->data_offset);
3897 if (!mddev->reshape_backwards)
3898 diff = -diff;
3899 if (diff < 0)
3900 diff = 0;
3901 if (first || diff < min_offset_diff)
3902 min_offset_diff = diff;
3903 }
3904 }
3905
3906 if (max(before_length, after_length) > min_offset_diff)
3907 return -EINVAL;
3908
3909 if (spares < mddev->delta_disks)
3910 return -EINVAL;
3911
3912 conf->offset_diff = min_offset_diff;
3913 spin_lock_irq(&conf->device_lock);
3914 if (conf->mirrors_new) {
3915 memcpy(conf->mirrors_new, conf->mirrors,
3916 sizeof(struct mirror_info)*conf->prev.raid_disks);
3917 smp_mb();
3918 kfree(conf->mirrors_old); /* FIXME and elsewhere */
3919 conf->mirrors_old = conf->mirrors;
3920 conf->mirrors = conf->mirrors_new;
3921 conf->mirrors_new = NULL;
3922 }
3923 setup_geo(&conf->geo, mddev, geo_start);
3924 smp_mb();
3925 if (mddev->reshape_backwards) {
3926 sector_t size = raid10_size(mddev, 0, 0);
3927 if (size < mddev->array_sectors) {
3928 spin_unlock_irq(&conf->device_lock);
3929 printk(KERN_ERR "md/raid10:%s: array size must be reduce before number of disks\n",
3930 mdname(mddev));
3931 return -EINVAL;
3932 }
3933 mddev->resync_max_sectors = size;
3934 conf->reshape_progress = size;
3935 } else
3936 conf->reshape_progress = 0;
3937 spin_unlock_irq(&conf->device_lock);
3938
3939 if (mddev->delta_disks > 0) {
3940 rdev_for_each(rdev, mddev)
3941 if (rdev->raid_disk < 0 &&
3942 !test_bit(Faulty, &rdev->flags)) {
3943 if (raid10_add_disk(mddev, rdev) == 0) {
3944 if (rdev->raid_disk >=
3945 conf->prev.raid_disks)
3946 set_bit(In_sync, &rdev->flags);
3947 else
3948 rdev->recovery_offset = 0;
3949
3950 if (sysfs_link_rdev(mddev, rdev))
3951 /* Failure here is OK */;
3952 }
3953 } else if (rdev->raid_disk >= conf->prev.raid_disks
3954 && !test_bit(Faulty, &rdev->flags)) {
3955 /* This is a spare that was manually added */
3956 set_bit(In_sync, &rdev->flags);
3957 }
3958 }
3959 /* When a reshape changes the number of devices,
3960 * ->degraded is measured against the larger of the
3961 * pre and post numbers.
3962 */
3963 spin_lock_irq(&conf->device_lock);
3964 mddev->degraded = calc_degraded(conf);
3965 spin_unlock_irq(&conf->device_lock);
3966 mddev->raid_disks = conf->geo.raid_disks;
3967 mddev->reshape_position = conf->reshape_progress;
3968 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3969
3970 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3971 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3972 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3973 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3974
3975 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3976 "reshape");
3977 if (!mddev->sync_thread) {
3978 mddev->recovery = 0;
3979 spin_lock_irq(&conf->device_lock);
3980 conf->geo = conf->prev;
3981 mddev->raid_disks = conf->geo.raid_disks;
3982 rdev_for_each(rdev, mddev)
3983 rdev->new_data_offset = rdev->data_offset;
3984 smp_wmb();
3985 conf->reshape_progress = MaxSector;
3986 mddev->reshape_position = MaxSector;
3987 spin_unlock_irq(&conf->device_lock);
3988 return -EAGAIN;
3989 }
3990 conf->reshape_checkpoint = jiffies;
3991 md_wakeup_thread(mddev->sync_thread);
3992 md_new_event(mddev);
3993 return 0;
3994}
3995
3996/* Calculate the last device-address that could contain
3997 * any block from the chunk that includes the array-address 's'
3998 * and report the next address.
3999 * i.e. the address returned will be chunk-aligned and after
4000 * any data that is in the chunk containing 's'.
4001 */
4002static sector_t last_dev_address(sector_t s, struct geom *geo)
4003{
4004 s = (s | geo->chunk_mask) + 1;
4005 s >>= geo->chunk_shift;
4006 s *= geo->near_copies;
4007 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4008 s *= geo->far_copies;
4009 s <<= geo->chunk_shift;
4010 return s;
4011}
4012
4013/* Calculate the first device-address that could contain
4014 * any block from the chunk that includes the array-address 's'.
4015 * This too will be the start of a chunk
4016 */
4017static sector_t first_dev_address(sector_t s, struct geom *geo)
4018{
4019 s >>= geo->chunk_shift;
4020 s *= geo->near_copies;
4021 sector_div(s, geo->raid_disks);
4022 s *= geo->far_copies;
4023 s <<= geo->chunk_shift;
4024 return s;
4025}
4026
4027static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4028 int *skipped)
4029{
4030 /* We simply copy at most one chunk (smallest of old and new)
4031 * at a time, possibly less if that exceeds RESYNC_PAGES,
4032 * or we hit a bad block or something.
4033 * This might mean we pause for normal IO in the middle of
4034 * a chunk, but that is not a problem was mddev->reshape_position
4035 * can record any location.
4036 *
4037 * If we will want to write to a location that isn't
4038 * yet recorded as 'safe' (i.e. in metadata on disk) then
4039 * we need to flush all reshape requests and update the metadata.
4040 *
4041 * When reshaping forwards (e.g. to more devices), we interpret
4042 * 'safe' as the earliest block which might not have been copied
4043 * down yet. We divide this by previous stripe size and multiply
4044 * by previous stripe length to get lowest device offset that we
4045 * cannot write to yet.
4046 * We interpret 'sector_nr' as an address that we want to write to.
4047 * From this we use last_device_address() to find where we might
4048 * write to, and first_device_address on the 'safe' position.
4049 * If this 'next' write position is after the 'safe' position,
4050 * we must update the metadata to increase the 'safe' position.
4051 *
4052 * When reshaping backwards, we round in the opposite direction
4053 * and perform the reverse test: next write position must not be
4054 * less than current safe position.
4055 *
4056 * In all this the minimum difference in data offsets
4057 * (conf->offset_diff - always positive) allows a bit of slack,
4058 * so next can be after 'safe', but not by more than offset_disk
4059 *
4060 * We need to prepare all the bios here before we start any IO
4061 * to ensure the size we choose is acceptable to all devices.
4062 * The means one for each copy for write-out and an extra one for
4063 * read-in.
4064 * We store the read-in bio in ->master_bio and the others in
4065 * ->devs[x].bio and ->devs[x].repl_bio.
4066 */
4067 struct r10conf *conf = mddev->private;
4068 struct r10bio *r10_bio;
4069 sector_t next, safe, last;
4070 int max_sectors;
4071 int nr_sectors;
4072 int s;
4073 struct md_rdev *rdev;
4074 int need_flush = 0;
4075 struct bio *blist;
4076 struct bio *bio, *read_bio;
4077 int sectors_done = 0;
4078
4079 if (sector_nr == 0) {
4080 /* If restarting in the middle, skip the initial sectors */
4081 if (mddev->reshape_backwards &&
4082 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4083 sector_nr = (raid10_size(mddev, 0, 0)
4084 - conf->reshape_progress);
4085 } else if (!mddev->reshape_backwards &&
4086 conf->reshape_progress > 0)
4087 sector_nr = conf->reshape_progress;
4088 if (sector_nr) {
4089 mddev->curr_resync_completed = sector_nr;
4090 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4091 *skipped = 1;
4092 return sector_nr;
4093 }
4094 }
4095
4096 /* We don't use sector_nr to track where we are up to
4097 * as that doesn't work well for ->reshape_backwards.
4098 * So just use ->reshape_progress.
4099 */
4100 if (mddev->reshape_backwards) {
4101 /* 'next' is the earliest device address that we might
4102 * write to for this chunk in the new layout
4103 */
4104 next = first_dev_address(conf->reshape_progress - 1,
4105 &conf->geo);
4106
4107 /* 'safe' is the last device address that we might read from
4108 * in the old layout after a restart
4109 */
4110 safe = last_dev_address(conf->reshape_safe - 1,
4111 &conf->prev);
4112
4113 if (next + conf->offset_diff < safe)
4114 need_flush = 1;
4115
4116 last = conf->reshape_progress - 1;
4117 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4118 & conf->prev.chunk_mask);
4119 if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
4120 sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
4121 } else {
4122 /* 'next' is after the last device address that we
4123 * might write to for this chunk in the new layout
4124 */
4125 next = last_dev_address(conf->reshape_progress, &conf->geo);
4126
4127 /* 'safe' is the earliest device address that we might
4128 * read from in the old layout after a restart
4129 */
4130 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4131
4132 /* Need to update metadata if 'next' might be beyond 'safe'
4133 * as that would possibly corrupt data
4134 */
4135 if (next > safe + conf->offset_diff)
4136 need_flush = 1;
4137
4138 sector_nr = conf->reshape_progress;
4139 last = sector_nr | (conf->geo.chunk_mask
4140 & conf->prev.chunk_mask);
4141
4142 if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
4143 last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
4144 }
4145
4146 if (need_flush ||
4147 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4148 /* Need to update reshape_position in metadata */
4149 wait_barrier(conf);
4150 mddev->reshape_position = conf->reshape_progress;
4151 if (mddev->reshape_backwards)
4152 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4153 - conf->reshape_progress;
4154 else
4155 mddev->curr_resync_completed = conf->reshape_progress;
4156 conf->reshape_checkpoint = jiffies;
4157 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4158 md_wakeup_thread(mddev->thread);
4159 wait_event(mddev->sb_wait, mddev->flags == 0 ||
4160 kthread_should_stop());
4161 conf->reshape_safe = mddev->reshape_position;
4162 allow_barrier(conf);
4163 }
4164
4165read_more:
4166 /* Now schedule reads for blocks from sector_nr to last */
4167 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
4168 raise_barrier(conf, sectors_done != 0);
4169 atomic_set(&r10_bio->remaining, 0);
4170 r10_bio->mddev = mddev;
4171 r10_bio->sector = sector_nr;
4172 set_bit(R10BIO_IsReshape, &r10_bio->state);
4173 r10_bio->sectors = last - sector_nr + 1;
4174 rdev = read_balance(conf, r10_bio, &max_sectors);
4175 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4176
4177 if (!rdev) {
4178 /* Cannot read from here, so need to record bad blocks
4179 * on all the target devices.
4180 */
4181 // FIXME
4182 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4183 return sectors_done;
4184 }
4185
4186 read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
4187
4188 read_bio->bi_bdev = rdev->bdev;
4189 read_bio->bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
4190 + rdev->data_offset);
4191 read_bio->bi_private = r10_bio;
4192 read_bio->bi_end_io = end_sync_read;
4193 read_bio->bi_rw = READ;
4194 read_bio->bi_flags &= ~(BIO_POOL_MASK - 1);
4195 read_bio->bi_flags |= 1 << BIO_UPTODATE;
4196 read_bio->bi_vcnt = 0;
4197 read_bio->bi_idx = 0;
4198 read_bio->bi_size = 0;
4199 r10_bio->master_bio = read_bio;
4200 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4201
4202 /* Now find the locations in the new layout */
4203 __raid10_find_phys(&conf->geo, r10_bio);
4204
4205 blist = read_bio;
4206 read_bio->bi_next = NULL;
4207
4208 for (s = 0; s < conf->copies*2; s++) {
4209 struct bio *b;
4210 int d = r10_bio->devs[s/2].devnum;
4211 struct md_rdev *rdev2;
4212 if (s&1) {
4213 rdev2 = conf->mirrors[d].replacement;
4214 b = r10_bio->devs[s/2].repl_bio;
4215 } else {
4216 rdev2 = conf->mirrors[d].rdev;
4217 b = r10_bio->devs[s/2].bio;
4218 }
4219 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4220 continue;
4221 b->bi_bdev = rdev2->bdev;
4222 b->bi_sector = r10_bio->devs[s/2].addr + rdev2->new_data_offset;
4223 b->bi_private = r10_bio;
4224 b->bi_end_io = end_reshape_write;
4225 b->bi_rw = WRITE;
4226 b->bi_flags &= ~(BIO_POOL_MASK - 1);
4227 b->bi_flags |= 1 << BIO_UPTODATE;
4228 b->bi_next = blist;
4229 b->bi_vcnt = 0;
4230 b->bi_idx = 0;
4231 b->bi_size = 0;
4232 blist = b;
4233 }
4234
4235 /* Now add as many pages as possible to all of these bios. */
4236
4237 nr_sectors = 0;
4238 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
4239 struct page *page = r10_bio->devs[0].bio->bi_io_vec[s/(PAGE_SIZE>>9)].bv_page;
4240 int len = (max_sectors - s) << 9;
4241 if (len > PAGE_SIZE)
4242 len = PAGE_SIZE;
4243 for (bio = blist; bio ; bio = bio->bi_next) {
4244 struct bio *bio2;
4245 if (bio_add_page(bio, page, len, 0))
4246 continue;
4247
4248 /* Didn't fit, must stop */
4249 for (bio2 = blist;
4250 bio2 && bio2 != bio;
4251 bio2 = bio2->bi_next) {
4252 /* Remove last page from this bio */
4253 bio2->bi_vcnt--;
4254 bio2->bi_size -= len;
4255 bio2->bi_flags &= ~(1<<BIO_SEG_VALID);
4256 }
4257 goto bio_full;
4258 }
4259 sector_nr += len >> 9;
4260 nr_sectors += len >> 9;
4261 }
4262bio_full:
4263 r10_bio->sectors = nr_sectors;
4264
4265 /* Now submit the read */
4266 md_sync_acct(read_bio->bi_bdev, r10_bio->sectors);
4267 atomic_inc(&r10_bio->remaining);
4268 read_bio->bi_next = NULL;
4269 generic_make_request(read_bio);
4270 sector_nr += nr_sectors;
4271 sectors_done += nr_sectors;
4272 if (sector_nr <= last)
4273 goto read_more;
4274
4275 /* Now that we have done the whole section we can
4276 * update reshape_progress
4277 */
4278 if (mddev->reshape_backwards)
4279 conf->reshape_progress -= sectors_done;
4280 else
4281 conf->reshape_progress += sectors_done;
4282
4283 return sectors_done;
4284}
4285
4286static void end_reshape_request(struct r10bio *r10_bio);
4287static int handle_reshape_read_error(struct mddev *mddev,
4288 struct r10bio *r10_bio);
4289static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
4290{
4291 /* Reshape read completed. Hopefully we have a block
4292 * to write out.
4293 * If we got a read error then we do sync 1-page reads from
4294 * elsewhere until we find the data - or give up.
4295 */
4296 struct r10conf *conf = mddev->private;
4297 int s;
4298
4299 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
4300 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
4301 /* Reshape has been aborted */
4302 md_done_sync(mddev, r10_bio->sectors, 0);
4303 return;
4304 }
4305
4306 /* We definitely have the data in the pages, schedule the
4307 * writes.
4308 */
4309 atomic_set(&r10_bio->remaining, 1);
4310 for (s = 0; s < conf->copies*2; s++) {
4311 struct bio *b;
4312 int d = r10_bio->devs[s/2].devnum;
4313 struct md_rdev *rdev;
4314 if (s&1) {
4315 rdev = conf->mirrors[d].replacement;
4316 b = r10_bio->devs[s/2].repl_bio;
4317 } else {
4318 rdev = conf->mirrors[d].rdev;
4319 b = r10_bio->devs[s/2].bio;
4320 }
4321 if (!rdev || test_bit(Faulty, &rdev->flags))
4322 continue;
4323 atomic_inc(&rdev->nr_pending);
4324 md_sync_acct(b->bi_bdev, r10_bio->sectors);
4325 atomic_inc(&r10_bio->remaining);
4326 b->bi_next = NULL;
4327 generic_make_request(b);
4328 }
4329 end_reshape_request(r10_bio);
4330}
4331
4332static void end_reshape(struct r10conf *conf)
4333{
4334 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
4335 return;
4336
4337 spin_lock_irq(&conf->device_lock);
4338 conf->prev = conf->geo;
4339 md_finish_reshape(conf->mddev);
4340 smp_wmb();
4341 conf->reshape_progress = MaxSector;
4342 spin_unlock_irq(&conf->device_lock);
4343
4344 /* read-ahead size must cover two whole stripes, which is
4345 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4346 */
4347 if (conf->mddev->queue) {
4348 int stripe = conf->geo.raid_disks *
4349 ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
4350 stripe /= conf->geo.near_copies;
4351 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4352 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4353 }
4354 conf->fullsync = 0;
4355}
4356
4357
4358static int handle_reshape_read_error(struct mddev *mddev,
4359 struct r10bio *r10_bio)
4360{
4361 /* Use sync reads to get the blocks from somewhere else */
4362 int sectors = r10_bio->sectors;
4363 struct r10bio r10b;
4364 struct r10conf *conf = mddev->private;
4365 int slot = 0;
4366 int idx = 0;
4367 struct bio_vec *bvec = r10_bio->master_bio->bi_io_vec;
4368
4369 r10b.sector = r10_bio->sector;
4370 __raid10_find_phys(&conf->prev, &r10b);
4371
4372 while (sectors) {
4373 int s = sectors;
4374 int success = 0;
4375 int first_slot = slot;
4376
4377 if (s > (PAGE_SIZE >> 9))
4378 s = PAGE_SIZE >> 9;
4379
4380 while (!success) {
4381 int d = r10b.devs[slot].devnum;
4382 struct md_rdev *rdev = conf->mirrors[d].rdev;
4383 sector_t addr;
4384 if (rdev == NULL ||
4385 test_bit(Faulty, &rdev->flags) ||
4386 !test_bit(In_sync, &rdev->flags))
4387 goto failed;
4388
4389 addr = r10b.devs[slot].addr + idx * PAGE_SIZE;
4390 success = sync_page_io(rdev,
4391 addr,
4392 s << 9,
4393 bvec[idx].bv_page,
4394 READ, false);
4395 if (success)
4396 break;
4397 failed:
4398 slot++;
4399 if (slot >= conf->copies)
4400 slot = 0;
4401 if (slot == first_slot)
4402 break;
4403 }
4404 if (!success) {
4405 /* couldn't read this block, must give up */
4406 set_bit(MD_RECOVERY_INTR,
4407 &mddev->recovery);
4408 return -EIO;
4409 }
4410 sectors -= s;
4411 idx++;
4412 }
4413 return 0;
4414}
4415
4416static void end_reshape_write(struct bio *bio, int error)
4417{
4418 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
4419 struct r10bio *r10_bio = bio->bi_private;
4420 struct mddev *mddev = r10_bio->mddev;
4421 struct r10conf *conf = mddev->private;
4422 int d;
4423 int slot;
4424 int repl;
4425 struct md_rdev *rdev = NULL;
4426
4427 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
4428 if (repl)
4429 rdev = conf->mirrors[d].replacement;
4430 if (!rdev) {
4431 smp_mb();
4432 rdev = conf->mirrors[d].rdev;
4433 }
4434
4435 if (!uptodate) {
4436 /* FIXME should record badblock */
4437 md_error(mddev, rdev);
4438 }
4439
4440 rdev_dec_pending(rdev, mddev);
4441 end_reshape_request(r10_bio);
4442}
4443
4444static void end_reshape_request(struct r10bio *r10_bio)
4445{
4446 if (!atomic_dec_and_test(&r10_bio->remaining))
4447 return;
4448 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
4449 bio_put(r10_bio->master_bio);
4450 put_buf(r10_bio);
4451}
4452
4453static void raid10_finish_reshape(struct mddev *mddev)
4454{
4455 struct r10conf *conf = mddev->private;
4456
4457 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4458 return;
4459
4460 if (mddev->delta_disks > 0) {
4461 sector_t size = raid10_size(mddev, 0, 0);
4462 md_set_array_sectors(mddev, size);
4463 if (mddev->recovery_cp > mddev->resync_max_sectors) {
4464 mddev->recovery_cp = mddev->resync_max_sectors;
4465 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4466 }
4467 mddev->resync_max_sectors = size;
4468 set_capacity(mddev->gendisk, mddev->array_sectors);
4469 revalidate_disk(mddev->gendisk);
4470 }
4471 mddev->layout = mddev->new_layout;
4472 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
4473 mddev->reshape_position = MaxSector;
4474 mddev->delta_disks = 0;
4475 mddev->reshape_backwards = 0;
4476}
4477
NeilBrown84fc4b52011-10-11 16:49:58 +11004478static struct md_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479{
4480 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08004481 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 .owner = THIS_MODULE,
4483 .make_request = make_request,
4484 .run = run,
4485 .stop = stop,
4486 .status = status,
4487 .error_handler = error,
4488 .hot_add_disk = raid10_add_disk,
4489 .hot_remove_disk= raid10_remove_disk,
4490 .spare_active = raid10_spare_active,
4491 .sync_request = sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08004492 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07004493 .size = raid10_size,
NeilBrown006a09a2012-03-19 12:46:40 +11004494 .resize = raid10_resize,
Trela, Maciejdab8b292010-03-08 16:02:45 +11004495 .takeover = raid10_takeover,
NeilBrown3ea7daa2012-05-22 13:53:47 +10004496 .check_reshape = raid10_check_reshape,
4497 .start_reshape = raid10_start_reshape,
4498 .finish_reshape = raid10_finish_reshape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499};
4500
4501static int __init raid_init(void)
4502{
NeilBrown2604b702006-01-06 00:20:36 -08004503 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504}
4505
4506static void raid_exit(void)
4507{
NeilBrown2604b702006-01-06 00:20:36 -08004508 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509}
4510
4511module_init(raid_init);
4512module_exit(raid_exit);
4513MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11004514MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08004516MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08004517MODULE_ALIAS("md-level-10");
NeilBrown34db0cd2011-10-11 16:50:01 +11004518
4519module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);